1use super::*;
2use crate::Engine;
3
4#[test]
5fn test_s_render_ngon_transform_merge_and_boolean() {
6 let src = r#"Composition {
7 CurrentTime = 0,
8 RenderRange = { 0, 1 },
9 GlobalRange = { 0, 1 },
10 Tools = ordered() {
11 NGon1 = sNGon {
12 Inputs = {
13 Sides = Input { Value = 6, },
14 Width = Input { Value = 0.4, },
15 Red = Input { Value = 1.0, },
16 Green = Input { Value = 0.0, },
17 Blue = Input { Value = 0.0, },
18 },
19 },
20 XF1 = sTransform {
21 Inputs = {
22 XOffset = Input { Value = -0.2, },
23 YOffset = Input { Value = 0.0, },
24 XSize = Input { Value = 1.0, },
25 YSize = Input { Value = 1.0, },
26 ZRotation = Input { Value = 15.0, },
27 Input = Input { SourceOp = "NGon1", Source = "Output", },
28 },
29 },
30 Rect1 = sRectangle {
31 Inputs = {
32 Width = Input { Value = 0.3, },
33 Height = Input { Value = 0.3, },
34 CornerRadius = Input { Value = 0.05, },
35 Translate = Input { Value = { 0.0, 0.0 }, },
36 ["Translate.X"] = Input { Value = 0.2, },
37 Red = Input { Value = 0.0, },
38 Green = Input { Value = 0.0, },
39 Blue = Input { Value = 1.0, },
40 },
41 },
42 Merge1 = sMerge {
43 Inputs = {
44 Input1 = Input { SourceOp = "XF1", Source = "Output", },
45 Input2 = Input { SourceOp = "Rect1", Source = "Output", },
46 },
47 },
48 Render1 = sRender {
49 Inputs = {
50 Width = Input { Value = 128, },
51 Height = Input { Value = 128, },
52 Input = Input { SourceOp = "Merge1", Source = "Output", },
53 },
54 },
55 },
56 }"#;
57 let comp = Comp::from_fusion_str(src).expect("parse sRender comp");
58 let Ok(mut eng) = Engine::new_headless() else {
59 return;
60 };
61 let render_id = comp.find_node("Render1").unwrap();
62 let (tex, stats) = eng.evaluate(&comp, render_id, 0.0);
63 assert!(stats.unsupported_nodes.is_empty(), "unsupported: {:?}", stats.unsupported_nodes);
64 assert_eq!(tex.size, [128, 128]);
65 let px = eng.readback_rgba(&tex).expect("readback sRender");
66
67 // Left shape center (-0.2 -> x ~ 38, y ~ 64) should be solid red
68 let left = (64 * 128 + 38) * 4;
69 assert!(px[left] > 240 && px[left + 2] < 10 && px[left + 3] > 240, "left={:?}", &px[left..left + 4]);
70 // Right shape center (+0.2 -> x ~ 90, y ~ 64) should be solid blue
71 let right = (64 * 128 + 90) * 4;
72 assert!(px[right + 2] > 240 && px[right] < 10 && px[right + 3] > 240, "right={:?}", &px[right..right + 4]);
73 // Top-left corner (0, 0) should be transparent
74 assert_eq!(&px[0..4], &[0, 0, 0, 0], "corner should be transparent");
75}
76
77#[test]
78fn test_s_boolean_uses_own_color_and_subtract_xor() {
79 let src = r#"Composition {
80 CurrentTime = 0,
81 RenderRange = { 0, 1 },
82 GlobalRange = { 0, 1 },
83 Tools = ordered() {
84 NGon1 = sNGon {
85 Inputs = {
86 Sides = Input { Value = 5, },
87 Width = Input { Value = 0.5, },
88 Height = Input { Value = 0.5, },
89 Red = Input { Value = 1.0, },
90 Green = Input { Value = 0.0, },
91 Blue = Input { Value = 0.0, },
92 },
93 },
94 Rect1 = sRectangle {
95 Inputs = {
96 Width = Input { Value = 0.35, },
97 Height = Input { Value = 0.35, },
98 ["Translate.X"] = Input { Value = 0.18, },
99 Red = Input { Value = 0.0, },
100 Green = Input { Value = 1.0, },
101 Blue = Input { Value = 0.0, },
102 },
103 },
104 BoolSub = sBoolean {
105 Inputs = {
106 Operation = Input { Value = FuID { "Subtract" }, },
107 Input1 = Input { SourceOp = "Rect1", Source = "Output", },
108 Input2 = Input { SourceOp = "NGon1", Source = "Output", },
109 },
110 },
111 BoolXor = sBoolean {
112 Inputs = {
113 Operation = Input { Value = FuID { "Xor" }, },
114 Input1 = Input { SourceOp = "NGon1", Source = "Output", },
115 Input2 = Input { SourceOp = "Rect1", Source = "Output", },
116 },
117 },
118 RSub = sRender {
119 Inputs = {
120 Width = Input { Value = 128, },
121 Height = Input { Value = 128, },
122 Input = Input { SourceOp = "BoolSub", Source = "Output", },
123 },
124 },
125 RXor = sRender {
126 Inputs = {
127 Width = Input { Value = 128, },
128 Height = Input { Value = 128, },
129 Input = Input { SourceOp = "BoolXor", Source = "Output", },
130 },
131 },
132 },
133 }"#;
134 let comp = Comp::from_fusion_str(src).unwrap();
135 let Ok(mut eng) = Engine::new_headless() else {
136 return;
137 };
138 let (tex_sub, _) = eng.evaluate(&comp, comp.find_node("RSub").unwrap(), 0.0);
139 let (tex_xor, _) = eng.evaluate(&comp, comp.find_node("RXor").unwrap(), 0.0);
140 let px_sub = eng.readback_rgba(&tex_sub).unwrap();
141 let px_xor = eng.readback_rgba(&tex_xor).unwrap();
142
143 // Far right of Rect (x=0.30 -> px=102, y=64): outside NGon (max x=0.25), inside Rect -> white!
144 let rect_only = (64 * 128 + 102) * 4;
145 assert_eq!(&px_sub[rect_only..rect_only + 4], &[255, 255, 255, 255]);
146 assert_eq!(&px_xor[rect_only..rect_only + 4], &[255, 255, 255, 255]);
147
148 // Overlap region (x=0.10 -> px=77, y=64): subtracted/xored out -> transparent!
149 let overlap = (64 * 128 + 77) * 4;
150 assert_eq!(&px_sub[overlap..overlap + 4], &[0, 0, 0, 0]);
151 assert_eq!(&px_xor[overlap..overlap + 4], &[0, 0, 0, 0]);
152
153 // Center (0,0 -> px=64, y=64): inside NGon, outside Rect (starts at 0.005) -> white in Xor!
154 let ngon_only = (64 * 128 + 54) * 4;
155 assert_eq!(&px_xor[ngon_only..ngon_only + 4], &[255, 255, 255, 255]);
156}
157
158#[test]
159fn test_s_boolean_operation_names_and_missing_inputs() {
160 let boolean = |name: &str, op: &str, inputs: &str| {
161 format!(
162 r#"{name} = sBoolean {{ Inputs = {{ {op} {inputs} }}, }},
163 R{name} = sRender {{ Inputs = {{ Width = Input {{ Value = 128, }}, Height = Input {{ Value = 128, }},
164 Input = Input {{ SourceOp = "{name}", Source = "Output", }}, }}, }},"#
165 )
166 };
167 let both = r#"Input1 = Input { SourceOp = "Rect", Source = "Output", }, Input2 = Input { SourceOp = "Ell", Source = "Output", },"#;
168 let first = r#"Input1 = Input { SourceOp = "Rect", Source = "Output", },"#;
169 let second = r#"Input2 = Input { SourceOp = "Ell", Source = "Output", },"#;
170 let fu = |v: &str| format!(r#"Operation = Input {{ Value = FuID {{ "{v}" }}, }},"#);
171 let src = format!(
172 r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{
173 Rect = sRectangle {{ Inputs = {{ Width = Input {{ Value = 0.5, }}, Height = Input {{ Value = 0.2, }},
174 Red = Input {{ Value = 1, }}, Green = Input {{ Value = 0, }}, Blue = Input {{ Value = 0, }}, }}, }},
175 Ell = sEllipse {{ Inputs = {{ Width = Input {{ Value = 0.3, }}, Height = Input {{ Value = 0.3, }}, }}, }},
176 {} {} {} {} {} {}
177 }} }}"#,
178 boolean("Inter", &fu("Intersection"), both),
179 boolean("Default", "", both),
180 boolean("Unknown", &fu("Intersect"), both),
181 boolean("SubFirst", &fu("Subtract"), first),
182 boolean("SubSecond", &fu("Subtract"), second),
183 boolean("UnionBoth", &fu("Union"), both),
184 );
185 let comp = Comp::from_fusion_str(&src).unwrap();
186 let Ok(mut eng) = Engine::new_headless() else {
187 return;
188 };
189 let mut px = |name: &str, x: usize| {
190 let (tex, _) = eng.evaluate(&comp, comp.find_node(&format!("R{name}")).unwrap(), 0.0);
191 let rgba = eng.readback_rgba(&tex).unwrap();
192 let i = (64 * 128 + x) * 4;
193 [rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]]
194 };
195 let white = [255; 4];
196 let empty = [0; 4];
197 // x=64 is inside both shapes, x=90 is inside only the rectangle.
198 assert_eq!(px("Inter", 64), white);
199 assert_eq!(px("Inter", 90), empty);
200 assert_eq!(px("Default", 64), white, "Operation defaults to Intersection");
201 assert_eq!(px("Unknown", 64), empty, "unrecognized operation names render nothing");
202 assert_eq!(px("SubFirst", 90), white, "a missing Input2 passes Input1 through in the boolean's own color");
203 assert_eq!(px("SubSecond", 64), empty, "a missing Input1 renders nothing");
204 assert_eq!(px("UnionBoth", 90), white);
205}
206
207#[test]
208fn test_s_grid() {
209 let src = r#"{
210 Tools = ordered() {
211 El1 = sEllipse {
212 Inputs = { Width = Input { Value = 0.1, }, Height = Input { Value = 0.1, }, },
213 },
214 Gr1 = sGrid {
215 Inputs = {
216 Input = Input { SourceOp = "El1", Source = "Output", },
217 CellsX = Input { Value = 3, },
218 CellsY = Input { Value = 3, },
219 XOffset = Input { Value = 0.25, },
220 YOffset = Input { Value = 0.25, },
221 },
222 },
223 SR1 = sRender {
224 Inputs = {
225 Width = Input { Value = 64, },
226 Height = Input { Value = 64, },
227 Input = Input { SourceOp = "Gr1", Source = "Output", },
228 },
229 },
230 },
231 }"#;
232 let comp = Comp::from_fusion_str(src).unwrap();
233 let Ok(mut eng) = Engine::new_headless() else { return; };
234 let (t_sr, _) = eng.evaluate(&comp, comp.find_node("SR1").unwrap(), 0.0);
235 let px_sr = eng.readback_rgba(&t_sr).unwrap();
236 assert_eq!(&px_sr[(32 * 64 + 32) * 4..(32 * 64 + 32) * 4 + 4], &[255, 255, 255, 255]);
237 assert_eq!(&px_sr[(32 * 64 + 48) * 4..(32 * 64 + 48) * 4 + 4], &[255, 255, 255, 255]);
238 assert_eq!(&px_sr[0..4], &[0, 0, 0, 0]);
239}
240
241#[test]
242fn test_giant_rotated_shapes_clip_instead_of_hanging_and_alpha_stays_unpremultiplied() {
243 let src = r#"Composition {
244 CurrentTime = 0,
245 RenderRange = { 0, 1 },
246 GlobalRange = { 0, 1 },
247 Tools = ordered() {
248 NG = sNGon { Inputs = { Width = Input { Value = 256, }, Height = Input { Value = 256, }, Alpha = Input { Value = 0.25, }, Opacity = Input { Value = 0.5, }, Blue = Input { Value = 0.5, }, }, },
249 Rect = sRectangle { Inputs = { Width = Input { Value = 256, }, Height = Input { Value = 256, }, Angle = Input { Value = 33, }, }, },
250 SRN = sRender { Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, }, Input = Input { SourceOp = "NG", Source = "Output", }, }, },
251 SRR = sRender { Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, }, Input = Input { SourceOp = "Rect", Source = "Output", }, }, },
252 },
253 }"#;
254 let comp = Comp::from_fusion_str(src).unwrap();
255 let Ok(mut eng) = Engine::new_headless() else { return; };
256 let (t, _) = eng.evaluate(&comp, comp.find_node("SRN").unwrap(), 0.0);
257 let px = eng.readback_rgba(&t).unwrap();
258 let mid = (16 * 32 + 16) * 4;
259 assert_eq!(&px[mid..mid + 4], &[128, 128, 64, 32], "Opacity scales RGBA, Alpha scales alpha only");
260 let (t, _) = eng.evaluate(&comp, comp.find_node("SRR").unwrap(), 0.0);
261 let px = eng.readback_rgba(&t).unwrap();
262 assert_eq!(&px[mid..mid + 4], &[255, 255, 255, 255]);
263 assert_eq!(&px[0..4], &[255, 255, 255, 255], "giant rotated rectangle covers the corner");
264}
265
266#[test]
267fn s_outline_negative_thickness_s_boolean_keep_style_and_s_render_blend() {
268 let src = r#"Composition {
269 CurrentTime = 0,
270 RenderRange = { 0, 1 },
271 GlobalRange = { 0, 1 },
272 Tools = ordered() {
273 R1 = sRectangle { Inputs = { Width = Input { Value = 0.8, }, Height = Input { Value = 0.8, }, Red = Input { Value = 1.0, }, Green = Input { Value = 0.0, }, Blue = Input { Value = 0.0, }, }, },
274 R2 = sRectangle { Inputs = { Width = Input { Value = 0.8, }, Height = Input { Value = 0.8, }, Blue = Input { Value = 1.0, }, }, },
275 OutNeg = sOutline { Inputs = { Input = Input { SourceOp = "R1", Source = "Output", }, Thickness = Input { Value = -0.05, }, }, },
276 SROutNeg = sRender { Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, }, Input = Input { SourceOp = "OutNeg", Source = "Output", }, }, },
277 BoolKeep = sBoolean { Inputs = {
278 Input1 = Input { SourceOp = "R1", Source = "Output", },
279 Input2 = Input { SourceOp = "R2", Source = "Output", },
280 Operation = Input { Value = FuID { "Intersection" }, },
281 StyleMode = Input { Value = FuID { "Keep" }, },
282 Opacity = Input { Value = 0.2, },
283 Green = Input { Value = 1.0, },
284 }, },
285 SRBool = sRender { Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, }, Blend = Input { Value = 0.2, }, Input = Input { SourceOp = "BoolKeep", Source = "Output", }, }, },
286 },
287 }"#;
288 let comp = Comp::from_fusion_str(src).unwrap();
289 let Ok(mut eng) = Engine::new_headless() else { return; };
290 let (t_out, _) = eng.evaluate(&comp, comp.find_node("SROutNeg").unwrap(), 0.0);
291 let px_out = eng.readback_rgba(&t_out).unwrap();
292 assert!(px_out.iter().all(|&b| b == 0), "negative sOutline thickness should render empty");
293
294 let (t_bool, _) = eng.evaluate(&comp, comp.find_node("SRBool").unwrap(), 0.0);
295 let px_bool = eng.readback_rgba(&t_bool).unwrap();
296 let mid = (16 * 32 + 16) * 4;
297 assert_eq!(&px_bool[mid..mid + 4], &[255, 0, 0, 255], "sBoolean Keep preserves Input1 style ignoring Opacity, and sRender ignores Blend");
298}
299
300#[test]
301fn test_s_star_points_depth_rotation_and_outline() {
302 let src = r#"Composition {
303 CurrentTime = 0,
304 RenderRange = { 0, 1 },
305 GlobalRange = { 0, 1 },
306 Tools = ordered() {
307 Star5 = sStar {
308 Inputs = {
309 Points = Input { Value = 5, },
310 Depth = Input { Value = 0.5, },
311 Width = Input { Value = 0.6, },
312 Height = Input { Value = 0.6, },
313 Red = Input { Value = 1.0, },
314 Green = Input { Value = 0.5, },
315 Blue = Input { Value = 0.0, },
316 },
317 },
318 StarOutline = sStar {
319 Inputs = {
320 NumberOfPoints = Input { Value = 6, },
321 Depth = Input { Value = 0.4, },
322 Width = Input { Value = 0.6, },
323 Height = Input { Value = 0.6, },
324 Solid = Input { Value = 0, },
325 BorderWidth = Input { Value = 0.04, },
326 CornerRadius = Input { Value = 0.02, },
327 ZRotation = Input { Value = 30.0, },
328 },
329 },
330 SRFill = sRender {
331 Inputs = {
332 Width = Input { Value = 64, },
333 Height = Input { Value = 64, },
334 Input = Input { SourceOp = "Star5", Source = "Output", },
335 },
336 },
337 SROutline = sRender {
338 Inputs = {
339 Width = Input { Value = 64, },
340 Height = Input { Value = 64, },
341 Input = Input { SourceOp = "StarOutline", Source = "Output", },
342 },
343 },
344 },
345 }"#;
346 let comp = Comp::from_fusion_str(src).expect("parse sStar comp");
347 let Ok(mut eng) = Engine::new_headless() else { return };
348
349 let (tf, sf) = eng.evaluate(&comp, comp.find_node("SRFill").unwrap(), 0.0);
350 assert!(sf.unsupported_nodes.is_empty());
351 let pf = eng.readback_rgba(&tf).unwrap();
352 let px = |buf: &[u8], x: usize, y: usize| -> [u8; 4] {
353 let i = (y * 64 + x) * 4;
354 [buf[i], buf[i + 1], buf[i + 2], buf[i + 3]]
355 };
356 // Center (32, 32) is solid orange.
357 let ctr = px(&pf, 32, 32);
358 assert!(ctr[0] > 240 && (ctr[1] as i32 - 128).abs() <= 4 && ctr[2] < 5 && ctr[3] == 255, "center={ctr:?}");
359 // Rightmost outer tip at +X (r = 0.30 -> x ~ 50, y = 32) is covered!
360 assert!(px(&pf, 49, 32)[3] > 200, "outer tip at +X must be covered");
361 // Opposite -X valley for odd 5-point star is at r = 0.15 (x ~ 22.4): x=24 inside, x=15 outside!
362 assert!(px(&pf, 24, 32)[3] > 200, "inner valley interior must be covered");
363 assert_eq!(px(&pf, 15, 32)[3], 0, "outside -X inner valley must be empty");
364
365 // Outline mode: hollow center, non-empty stroke ring.
366 let (to, _) = eng.evaluate(&comp, comp.find_node("SROutline").unwrap(), 0.0);
367 let po = eng.readback_rgba(&to).unwrap();
368 assert_eq!(px(&po, 32, 32)[3], 0, "outline sStar center must be hollow");
369 let lit_px = po.chunks_exact(4).filter(|c| c[3] > 127).count();
370 assert!(lit_px > 50, "outline sStar should render visible stroke pixels, got {lit_px}");
371}
372
373#[test]
374fn test_s_duplicate_cumulative_transform_and_time_offset() {
375 let src = r#"Composition {
376 CurrentTime = 0,
377 RenderRange = { 0, 10 },
378 GlobalRange = { 0, 10 },
379 Tools = ordered() {
380 AnimRed = BezierSpline {
381 KeyFrames = {
382 [0] = { 0.0, Flags = { Linear = true } },
383 [10] = { 1.0, Flags = { Linear = true } },
384 },
385 },
386 Dot = sRectangle {
387 Inputs = {
388 Width = Input { Value = 0.1, },
389 Height = Input { Value = 0.1, },
390 Red = Input { SourceOp = "AnimRed", Source = "Value", },
391 Green = Input { Value = 0.0, },
392 Blue = Input { Value = 1.0, },
393 },
394 },
395 Dup = sDuplicate {
396 Inputs = {
397 Input = Input { SourceOp = "Dot", Source = "Output", },
398 Copies = Input { Value = 3, },
399 XOffset = Input { Value = 0.2, },
400 YOffset = Input { Value = 0.0, },
401 XSize = Input { Value = 1.0, },
402 YSize = Input { Value = 1.0, },
403 ZRotation = Input { Value = 0.0, },
404 TimeOffset = Input { Value = 5.0, },
405 },
406 },
407 DupRot = sDuplicate {
408 Inputs = {
409 Input = Input { SourceOp = "Dot", Source = "Output", },
410 Copies = Input { Value = 4, },
411 ZRotation = Input { Value = 90.0, },
412 XPivot = Input { Value = 0.2, },
413 YPivot = Input { Value = 0.0, },
414 },
415 },
416 SR = sRender {
417 Inputs = {
418 Width = Input { Value = 128, },
419 Height = Input { Value = 128, },
420 Input = Input { SourceOp = "Dup", Source = "Output", },
421 },
422 },
423 SRRot = sRender {
424 Inputs = {
425 Width = Input { Value = 128, },
426 Height = Input { Value = 128, },
427 Input = Input { SourceOp = "DupRot", Source = "Output", },
428 },
429 },
430 },
431 }"#;
432 let comp = Comp::from_fusion_str(src).expect("parse sDuplicate comp");
433 let Ok(mut eng) = Engine::new_headless() else { return };
434
435 let (tex, stats) = eng.evaluate(&comp, comp.find_node("SR").unwrap(), 0.0);
436 assert!(stats.unsupported_nodes.is_empty(), "unsupported: {:?}", stats.unsupported_nodes);
437 let buf = eng.readback_rgba(&tex).unwrap();
438 let sample = |x: usize, y: usize| -> [u8; 4] {
439 let i = (y * 128 + x) * 4;
440 [buf[i], buf[i + 1], buf[i + 2], buf[i + 3]]
441 };
442 // Copy 0 at x=0.0 -> px=64 (t=0 -> Red=0, Blue=255)
443 let c0 = sample(64, 64);
444 assert!(c0[0] < 5 && c0[2] > 250 && c0[3] == 255, "copy0={c0:?}");
445 // Copy 1 at x=+0.2 -> px=90 (t=5 -> Red~128, Blue=255)
446 let c1 = sample(90, 64);
447 assert!((c1[0] as i32 - 128).abs() <= 4 && c1[2] > 250 && c1[3] == 255, "copy1={c1:?}");
448 // Copy 2 at x=+0.4 -> px=115 (t=10 -> Red=255, Blue=255)
449 let c2 = sample(115, 64);
450 assert!(c2[0] > 250 && c2[2] > 250 && c2[3] == 255, "copy2={c2:?}");
451 // Left side should remain transparent
452 assert_eq!(sample(20, 64)[3], 0);
453
454 // Pivot rotation check: copy 0 at (0,0), copy 1 rotated 90 deg CCW around (0.2, 0) -> (0.2, -0.2) -> px=(90, 90)
455 let (tex_r, _) = eng.evaluate(&comp, comp.find_node("SRRot").unwrap(), 0.0);
456 let buf_r = eng.readback_rgba(&tex_r).unwrap();
457 assert_eq!(buf_r[(64 * 128 + 64) * 4 + 3], 255, "rot copy0 at origin");
458 assert_eq!(buf_r[(90 * 128 + 90) * 4 + 3], 255, "rot copy1 at (0.2, -0.2)");
459 assert_eq!(buf_r[(64 * 128 + 115) * 4 + 3], 255, "rot copy2 at (0.4, 0.0)");
460 assert_eq!(buf_r[(38 * 128 + 90) * 4 + 3], 255, "rot copy3 at (0.2, +0.2)");
461}
462
463#[test]
464fn test_s_jitter_deterministic_seed_smoothness_and_per_shape() {
465 let src = r#"Composition {
466 CurrentTime = 0,
467 RenderRange = { 0, 1 },
468 GlobalRange = { 0, 1 },
469 Tools = ordered() {
470 Rect = sRectangle {
471 Inputs = { Width = Input { Value = 0.4, }, Height = Input { Value = 0.4, }, },
472 },
473 JitA = sJitter {
474 Inputs = {
475 Input = Input { SourceOp = "Rect", Source = "Output", },
476 XAmount = Input { Value = 0.08, },
477 YAmount = Input { Value = 0.08, },
478 Seed = Input { Value = 7, },
479 Smoothness = Input { Value = 0.0, },
480 },
481 },
482 JitB = sJitter {
483 Inputs = {
484 Input = Input { SourceOp = "Rect", Source = "Output", },
485 XAmount = Input { Value = 0.08, },
486 YAmount = Input { Value = 0.08, },
487 Seed = Input { Value = 99, },
488 Smoothness = Input { Value = 0.0, },
489 },
490 },
491 JitSmooth = sJitter {
492 Inputs = {
493 Input = Input { SourceOp = "Rect", Source = "Output", },
494 XAmount = Input { Value = 0.08, },
495 YAmount = Input { Value = 0.08, },
496 Seed = Input { Value = 7, },
497 Smoothness = Input { Value = 2.0, },
498 },
499 },
500 SRef = sRender { Inputs = { Width = Input { Value = 96, }, Height = Input { Value = 96, }, Input = Input { SourceOp = "Rect", Source = "Output", }, }, },
501 SJA = sRender { Inputs = { Width = Input { Value = 96, }, Height = Input { Value = 96, }, Input = Input { SourceOp = "JitA", Source = "Output", }, }, },
502 SJB = sRender { Inputs = { Width = Input { Value = 96, }, Height = Input { Value = 96, }, Input = Input { SourceOp = "JitB", Source = "Output", }, }, },
503 SJS = sRender { Inputs = { Width = Input { Value = 96, }, Height = Input { Value = 96, }, Input = Input { SourceOp = "JitSmooth", Source = "Output", }, }, },
504 },
505 }"#;
506 let comp = Comp::from_fusion_str(src).expect("parse sJitter comp");
507 let Ok(mut eng) = Engine::new_headless() else { return };
508
509 let (t_ref, _) = eng.evaluate(&comp, comp.find_node("SRef").unwrap(), 0.0);
510 let (t_a1, s_a) = eng.evaluate(&comp, comp.find_node("SJA").unwrap(), 0.0);
511 let (t_b, _) = eng.evaluate(&comp, comp.find_node("SJB").unwrap(), 0.0);
512 let (t_sm, _) = eng.evaluate(&comp, comp.find_node("SJS").unwrap(), 0.0);
513 assert!(s_a.unsupported_nodes.is_empty());
514
515 let p_ref = eng.readback_rgba(&t_ref).unwrap();
516 let p_a1 = eng.readback_rgba(&t_a1).unwrap();
517 let p_b = eng.readback_rgba(&t_b).unwrap();
518 let p_sm = eng.readback_rgba(&t_sm).unwrap();
519
520 assert_ne!(p_ref, p_a1, "sJitter must deform the rectangle");
521 assert_ne!(p_a1, p_b, "different Seed must produce different jitter");
522 assert_ne!(p_a1, p_sm, "Smoothness > 0 must alter contour jitter");
523
524 // Re-evaluating with a fresh engine must be bit-identical (deterministic).
525 let Ok(mut eng2) = Engine::new_headless() else { return };
526 let (t_a2, _) = eng2.evaluate(&comp, comp.find_node("SJA").unwrap(), 0.0);
527 assert_eq!(p_a1, eng2.readback_rgba(&t_a2).unwrap());
528}
529
530#[test]
531fn test_s_expand_inflate_deflate_joins_and_collapse() {
532 let src = r#"Composition {
533 CurrentTime = 0,
534 RenderRange = { 0, 1 },
535 GlobalRange = { 0, 1 },
536 Tools = ordered() {
537 Rect = sRectangle {
538 Inputs = { Width = Input { Value = 0.3, }, Height = Input { Value = 0.3, }, },
539 },
540 ExpOut = sExpand {
541 Inputs = {
542 Input = Input { SourceOp = "Rect", Source = "Output", },
543 Amount = Input { Value = 0.1, },
544 JoinStyle = Input { Value = 2, },
545 },
546 },
547 ExpBevel = sExpand {
548 Inputs = {
549 Input = Input { SourceOp = "Rect", Source = "Output", },
550 Amount = Input { Value = 0.1, },
551 JoinStyle = Input { Value = 0, },
552 },
553 },
554 ExpIn = sExpand {
555 Inputs = {
556 Input = Input { SourceOp = "Rect", Source = "Output", },
557 Distance = Input { Value = -0.075, },
558 },
559 },
560 ExpCollapse = sExpand {
561 Inputs = {
562 Input = Input { SourceOp = "Rect", Source = "Output", },
563 Amount = Input { Value = -0.25, },
564 },
565 },
566 SRef = sRender { Inputs = { Width = Input { Value = 100, }, Height = Input { Value = 100, }, Input = Input { SourceOp = "Rect", Source = "Output", }, }, },
567 SOut = sRender { Inputs = { Width = Input { Value = 100, }, Height = Input { Value = 100, }, Input = Input { SourceOp = "ExpOut", Source = "Output", }, }, },
568 SBevel = sRender { Inputs = { Width = Input { Value = 100, }, Height = Input { Value = 100, }, Input = Input { SourceOp = "ExpBevel", Source = "Output", }, }, },
569 SIn = sRender { Inputs = { Width = Input { Value = 100, }, Height = Input { Value = 100, }, Input = Input { SourceOp = "ExpIn", Source = "Output", }, }, },
570 SCol = sRender { Inputs = { Width = Input { Value = 100, }, Height = Input { Value = 100, }, Input = Input { SourceOp = "ExpCollapse", Source = "Output", }, }, },
571 },
572 }"#;
573 let comp = Comp::from_fusion_str(src).expect("parse sExpand comp");
574 let Ok(mut eng) = Engine::new_headless() else { return };
575
576 let count_lit = |buf: &[u8]| buf.chunks_exact(4).filter(|c| c[3] > 127).count();
577 let (t_ref, _) = eng.evaluate(&comp, comp.find_node("SRef").unwrap(), 0.0);
578 let (t_out, s_out) = eng.evaluate(&comp, comp.find_node("SOut").unwrap(), 0.0);
579 let (t_bev, _) = eng.evaluate(&comp, comp.find_node("SBevel").unwrap(), 0.0);
580 let (t_in, _) = eng.evaluate(&comp, comp.find_node("SIn").unwrap(), 0.0);
581 let (t_col, _) = eng.evaluate(&comp, comp.find_node("SCol").unwrap(), 0.0);
582 assert!(s_out.unsupported_nodes.is_empty());
583
584 let lit_ref = count_lit(&eng.readback_rgba(&t_ref).unwrap()); // ~30x30 = 900
585 let lit_out = count_lit(&eng.readback_rgba(&t_out).unwrap()); // ~50x50 = 2500
586 let lit_bev = count_lit(&eng.readback_rgba(&t_bev).unwrap()); // ~2500 - 4*50 = 2300
587 let lit_in = count_lit(&eng.readback_rgba(&t_in).unwrap()); // ~15x15 = 225
588 let lit_col = count_lit(&eng.readback_rgba(&t_col).unwrap()); // 0
589
590 assert!(lit_out > lit_ref * 2, "expanded area should exceed 2x original (ref={lit_ref}, out={lit_out})");
591 assert!(lit_bev < lit_out && lit_bev > lit_ref * 2, "bevel join should trim miter corners (bev={lit_bev}, out={lit_out})");
592 assert!(lit_in > 100 && lit_in < lit_ref / 2, "deflated area should shrink substantially (ref={lit_ref}, in={lit_in})");
593 assert_eq!(lit_col, 0, "over-deflated shape must collapse cleanly to empty");
594}
595
596#[test]
597fn test_s_offset_translate_contour_and_path_position() {
598 let src = r#"Composition {
599 CurrentTime = 0,
600 RenderRange = { 0, 1 },
601 GlobalRange = { 0, 1 },
602 Tools = ordered() {
603 Rect = sRectangle {
604 Inputs = { Width = Input { Value = 0.2, }, Height = Input { Value = 0.2, }, },
605 },
606 OffXY = sOffset {
607 Inputs = {
608 Input = Input { SourceOp = "Rect", Source = "Output", },
609 XOffset = Input { Value = 0.25, },
610 YOffset = Input { Value = -0.15, },
611 },
612 },
613 OffContour = sOffset {
614 Inputs = {
615 Input = Input { SourceOp = "Rect", Source = "Output", },
616 Offset = Input { Value = 0.08, },
617 },
618 },
619 OffPos = sOffset {
620 Inputs = {
621 Input = Input { SourceOp = "Rect", Source = "Output", },
622 Position = Input { Value = 0.5, },
623 },
624 },
625 Out0 = sOutline {
626 Inputs = {
627 Input = Input { SourceOp = "Rect", Source = "Output", },
628 Thickness = Input { Value = 0.04, },
629 WriteLength = Input { Value = 0.25, },
630 },
631 },
632 OutShifted = sOutline {
633 Inputs = {
634 Input = Input { SourceOp = "OffPos", Source = "Output", },
635 Thickness = Input { Value = 0.04, },
636 WriteLength = Input { Value = 0.25, },
637 },
638 },
639 SXY = sRender { Inputs = { Width = Input { Value = 100, }, Height = Input { Value = 100, }, Input = Input { SourceOp = "OffXY", Source = "Output", }, }, },
640 SCtr = sRender { Inputs = { Width = Input { Value = 100, }, Height = Input { Value = 100, }, Input = Input { SourceOp = "OffContour", Source = "Output", }, }, },
641 SOut0 = sRender { Inputs = { Width = Input { Value = 100, }, Height = Input { Value = 100, }, Input = Input { SourceOp = "Out0", Source = "Output", }, }, },
642 SOutS = sRender { Inputs = { Width = Input { Value = 100, }, Height = Input { Value = 100, }, Input = Input { SourceOp = "OutShifted", Source = "Output", }, }, },
643 },
644 }"#;
645 let comp = Comp::from_fusion_str(src).expect("parse sOffset comp");
646 let Ok(mut eng) = Engine::new_headless() else { return };
647
648 let (t_xy, s_xy) = eng.evaluate(&comp, comp.find_node("SXY").unwrap(), 0.0);
649 assert!(s_xy.unsupported_nodes.is_empty());
650 let p_xy = eng.readback_rgba(&t_xy).unwrap();
651 // Center shifted to (+0.25, -0.15) -> px = (75, 65); origin (50, 50) should be empty.
652 assert_eq!(p_xy[(65 * 100 + 75) * 4 + 3], 255, "translated rect center must be covered");
653 assert_eq!(p_xy[(50 * 100 + 50) * 4 + 3], 0, "original center must be empty");
654
655 // Scalar Offset inflates 0.2x0.2 -> 0.36x0.36 (~1296 px vs ~400 px).
656 let (t_ctr, _) = eng.evaluate(&comp, comp.find_node("SCtr").unwrap(), 0.0);
657 let lit_ctr = eng.readback_rgba(&t_ctr).unwrap().chunks_exact(4).filter(|c| c[3] > 127).count();
658 assert!(lit_ctr > 900, "scalar sOffset should expand contour area, got {lit_ctr}");
659
660 // Position=0.5 shifts the trimmed 25% outline stroke to the opposite side of the contour!
661 let (t_o0, _) = eng.evaluate(&comp, comp.find_node("SOut0").unwrap(), 0.0);
662 let (t_os, _) = eng.evaluate(&comp, comp.find_node("SOutS").unwrap(), 0.0);
663 let p_o0 = eng.readback_rgba(&t_o0).unwrap();
664 let p_os = eng.readback_rgba(&t_os).unwrap();
665 assert_ne!(p_o0, p_os, "sOffset Position must shift parametric stroke origin along the path");
666}
667
668#[test]
669fn test_s_polygon_closed_open_bezier_and_transform() {
670 let src = r#"Composition {
671 CurrentTime = 0,
672 RenderRange = { 0, 1 },
673 GlobalRange = { 0, 1 },
674 Tools = ordered() {
675 PolyClosed = sPolygon {
676 Inputs = {
677 Solid = Input { Value = 1, },
678 Red = Input { Value = 0.0, },
679 Green = Input { Value = 1.0, },
680 Blue = Input { Value = 0.0, },
681 Scale = Input { Value = 1.5, },
682 ["Translate.X"] = Input { Value = 0.05, },
683 Polyline = Input {
684 Value = Polyline {
685 Closed = true,
686 Points = {
687 { Linear = true, X = -0.2, Y = -0.2 },
688 { Linear = true, X = 0.2, Y = -0.2 },
689 { Linear = true, X = 0.0, Y = 0.2 },
690 },
691 },
692 },
693 },
694 },
695 PolyOpen = sPolygon {
696 Inputs = {
697 Solid = Input { Value = 0, },
698 BorderWidth = Input { Value = 0.04, },
699 Red = Input { Value = 1.0, },
700 Green = Input { Value = 0.0, },
701 Blue = Input { Value = 0.0, },
702 Polyline = Input {
703 Value = Polyline {
704 Closed = false,
705 Points = {
706 { X = -0.35, Y = 0.0, RX = 0.1, RY = 0.2 },
707 { X = 0.35, Y = 0.0, LX = -0.1, LY = 0.2 },
708 },
709 },
710 },
711 },
712 },
713 SRClosed = sRender {
714 Inputs = {
715 Width = Input { Value = 100, },
716 Height = Input { Value = 100, },
717 Input = Input { SourceOp = "PolyClosed", Source = "Output", },
718 },
719 },
720 SROpen = sRender {
721 Inputs = {
722 Width = Input { Value = 100, },
723 Height = Input { Value = 100, },
724 Input = Input { SourceOp = "PolyOpen", Source = "Output", },
725 },
726 },
727 },
728 }"#;
729 let comp = Comp::from_fusion_str(src).expect("parse sPolygon comp");
730 let Ok(mut eng) = Engine::new_headless() else { return };
731
732 let (t_cl, s_cl) = eng.evaluate(&comp, comp.find_node("SRClosed").unwrap(), 0.0);
733 assert!(s_cl.unsupported_nodes.is_empty(), "unsupported: {:?}", s_cl.unsupported_nodes);
734 let p_cl = eng.readback_rgba(&t_cl).unwrap();
735 // Triangle shifted by +0.05 -> interior at px=(55, 55) should be solid green.
736 let i_mid = (55 * 100 + 55) * 4;
737 assert_eq!(&p_cl[i_mid..i_mid + 4], &[0, 255, 0, 255]);
738 assert_eq!(&p_cl[0..4], &[0, 0, 0, 0]);
739
740 // Open Bezier arch arcs upward above y=0 (so y=50, x=50 is empty, while upper apex y~35, x=50 is red!).
741 let (t_op, _) = eng.evaluate(&comp, comp.find_node("SROpen").unwrap(), 0.0);
742 let p_op = eng.readback_rgba(&t_op).unwrap();
743 assert_eq!(p_op[(50 * 100 + 50) * 4 + 3], 0, "open arch chord midpoint must remain hollow");
744 let apex_hit = (25..45).any(|y| p_op[(y * 100 + 50) * 4] > 200 && p_op[(y * 100 + 50) * 4 + 3] > 200);
745 assert!(apex_hit, "open Bezier sPolygon arch apex must be stroked in red");
746}
747
748#[test]
749fn test_s_bspline_closed_and_open() {
750 let src = r#"Composition {
751 CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 },
752 Tools = ordered() {
753 BS1 = sBSpline {
754 Inputs = {
755 Red = Input { Value = 1, }, Green = Input { Value = 0, }, Blue = Input { Value = 0, },
756 Polyline = Input {
757 Value = BSplinePolyline {
758 Closed = true,
759 Points = {
760 { X = -0.2, Y = -0.2, W = 1 },
761 { X = 0.2, Y = -0.2, W = 1 },
762 { X = 0.2, Y = 0.2, W = 1 },
763 { X = -0.2, Y = 0.2, W = 1 },
764 },
765 },
766 },
767 },
768 },
769 R = sRender { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, Input = Input { SourceOp = "BS1", Source = "Output", }, }, },
770 },
771 }"#;
772 let comp = Comp::from_fusion_str(src).unwrap();
773 let Ok(mut eng) = Engine::new_headless() else { return };
774 let (tex, stats) = eng.evaluate(&comp, comp.find_node("R").unwrap(), 0.0);
775 assert!(stats.unsupported_nodes.is_empty(), "unsupported: {:?}", stats.unsupported_nodes);
776 let rgba = eng.readback_rgba(&tex).unwrap();
777 let mid = (32 * 64 + 32) * 4;
778 assert_eq!(&rgba[mid..mid + 4], &[255, 0, 0, 255]);
779 assert_eq!(&rgba[0..4], &[0, 0, 0, 0]);
780
781 // Open stroked sBSpline: center (32, 32) must remain transparent, while curve stroke along y=0 (32) near x=32 is red.
782 let open_src = r#"Composition {
783 CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 },
784 Tools = ordered() {
785 BSOpen = sBSpline {
786 Inputs = {
787 Solid = Input { Value = 0, },
788 BorderWidth = Input { Value = 0.06, },
789 Red = Input { Value = 0, }, Green = Input { Value = 1, }, Blue = Input { Value = 0, },
790 Polyline = Input {
791 Value = BSplinePolyline {
792 Closed = false,
793 Points = {
794 { X = -0.35, Y = -0.2, W = 1 },
795 { X = 0.0, Y = 0.2, W = 1 },
796 { X = 0.35, Y = -0.2, W = 1 },
797 },
798 },
799 },
800 },
801 },
802 R = sRender { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, Input = Input { SourceOp = "BSOpen", Source = "Output", }, }, },
803 },
804 }"#;
805 let open_comp = Comp::from_fusion_str(open_src).unwrap();
806 let (tex_o, _) = eng.evaluate(&open_comp, open_comp.find_node("R").unwrap(), 0.0);
807 let po = eng.readback_rgba(&tex_o).unwrap();
808 let stroke_hits = (0..64)
809 .flat_map(|y| (0..64).map(move |x| (y * 64 + x) * 4))
810 .filter(|&i| po[i + 1] > 150 && po[i] < 20 && po[i + 3] > 150)
811 .count();
812 assert!(stroke_hits > 30, "open stroked sBSpline should render green curve, hits={stroke_hits}");
813 assert_eq!(po[(56 * 64 + 32) * 4 + 3], 0, "open sBSpline bottom background stays hollow");
814}
815
816#[test]
817fn test_s_change_style_color_opacity_and_stroke_override() {
818 let src = r#"Composition {
819 CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 },
820 Tools = ordered() {
821 RectA = sRectangle {
822 Inputs = {
823 Width = Input { Value = 0.5, },
824 Height = Input { Value = 0.5, },
825 Red = Input { Value = 1, }, Green = Input { Value = 0, }, Blue = Input { Value = 0, },
826 },
827 },
828 CSColor = sChangeStyle {
829 Inputs = {
830 Input = Input { SourceOp = "RectA", Source = "Output", },
831 Red = Input { Value = 0, }, Green = Input { Value = 1, }, Blue = Input { Value = 0.5, },
832 Alpha = Input { Value = 0.5, }, Opacity = Input { Value = 0.5, },
833 },
834 },
835 RColor = sRender { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, Input = Input { SourceOp = "CSColor", Source = "Output", }, }, },
836 CSOutline = sChangeStyle {
837 Inputs = {
838 Input = Input { SourceOp = "RectA", Source = "Output", },
839 Red = Input { Value = 0, }, Green = Input { Value = 0, }, Blue = Input { Value = 1, },
840 Solid = Input { Value = 0, },
841 BorderWidth = Input { Value = 0.05, },
842 },
843 },
844 ROutline = sRender { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, Input = Input { SourceOp = "CSOutline", Source = "Output", }, }, },
845 },
846 }"#;
847 let comp = Comp::from_fusion_str(src).unwrap();
848 let Ok(mut eng) = Engine::new_headless() else { return };
849
850 let (t1, s1) = eng.evaluate(&comp, comp.find_node("RColor").unwrap(), 0.0);
851 assert!(s1.unsupported_nodes.is_empty());
852 let p1 = eng.readback_rgba(&t1).unwrap();
853 let mid = (32 * 64 + 32) * 4;
854 // Alpha = 0.5 * 0.5 = 0.25 -> [0, 128, 64, 64] in straight RGBA output (matching Fusion GT).
855 assert_eq!(p1[mid], 0);
856 assert!(p1[mid + 1] > 115 && p1[mid + 1] < 140, "expected green ~128, got {:?}", &p1[mid..mid + 4]);
857 assert!(p1[mid + 2] > 55 && p1[mid + 2] < 75, "expected blue ~64, got {:?}", &p1[mid..mid + 4]);
858 assert!(p1[mid + 3] > 55 && p1[mid + 3] < 75, "expected alpha ~64, got {}", p1[mid + 3]);
859
860 let (t2, s2) = eng.evaluate(&comp, comp.find_node("ROutline").unwrap(), 0.0);
861 assert!(s2.unsupported_nodes.is_empty());
862 let p2 = eng.readback_rgba(&t2).unwrap();
863 assert_eq!(p2[mid + 3], 0, "Solid=0 override must hollow out rectangle interior");
864 let border_hits = (0..64)
865 .flat_map(|y| (0..64).map(move |x| (y * 64 + x) * 4))
866 .filter(|&i| p2[i + 2] > 180 && p2[i + 3] > 180)
867 .count();
868 assert!(border_hits > 60, "blue stroke override must render border, hits={border_hits}");
869}
870
871#[test]
872fn test_s_text_contours_and_composition() {
873 let src = r#"Composition {
874 CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 },
875 Tools = ordered() {
876 Txt = sText {
877 Inputs = {
878 StyledText = Input { Value = "HI", },
879 Font = Input { Value = "Open Sans", },
880 Style = Input { Value = "Bold", },
881 Size = Input { Value = 0.35, },
882 Red1 = Input { Value = 1, }, Green1 = Input { Value = 0.5, }, Blue1 = Input { Value = 0, },
883 },
884 },
885 Out = sOutline {
886 Inputs = {
887 Input = Input { SourceOp = "Txt", Source = "Output", },
888 Thickness = Input { Value = 0.02, },
889 },
890 },
891 Exp = sExpand {
892 Inputs = {
893 Input = Input { SourceOp = "Txt", Source = "Output", },
894 Expand = Input { Value = 0.015, },
895 },
896 },
897 Cut = sRectangle {
898 Inputs = {
899 Width = Input { Value = 0.12, }, Height = Input { Value = 0.12, },
900 Red = Input { Value = 0, }, Green = Input { Value = 0, }, Blue = Input { Value = 1, },
901 },
902 },
903 Bool = sBoolean {
904 Inputs = {
905 Operation = Input { Value = FuID { "Union" }, },
906 Input1 = Input { SourceOp = "Txt", Source = "Output", },
907 Input2 = Input { SourceOp = "Cut", Source = "Output", },
908 },
909 },
910 Xform = sTransform {
911 Inputs = {
912 Input = Input { SourceOp = "Bool", Source = "Output", },
913 XOffset = Input { Value = 0.05, },
914 },
915 },
916 RTxt = sRender { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 128, }, Input = Input { SourceOp = "Txt", Source = "Output", }, }, },
917 ROut = sRender { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 128, }, Input = Input { SourceOp = "Out", Source = "Output", }, }, },
918 RExp = sRender { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 128, }, Input = Input { SourceOp = "Exp", Source = "Output", }, }, },
919 RXform = sRender { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 128, }, Input = Input { SourceOp = "Xform", Source = "Output", }, }, },
920 },
921 }"#;
922 let comp = Comp::from_fusion_str(src).unwrap();
923 let Ok(mut eng) = Engine::new_headless() else { return };
924 for name in ["RTxt", "ROut", "RExp", "RXform"] {
925 let (tex, stats) = eng.evaluate(&comp, comp.find_node(name).unwrap(), 0.0);
926 assert!(stats.unsupported_nodes.is_empty(), "{name} unsupported: {:?}", stats.unsupported_nodes);
927 let px = eng.readback_rgba(&tex).unwrap();
928 let lit = px.chunks_exact(4).filter(|c| c[3] > 20).count();
929 assert!(lit > 50, "{name} must rasterize visible glyph pixels, lit={lit}");
930 }
931}