ironpress 1.2.1

Pure Rust HTML/CSS/Markdown to PDF converter with layout engine, tables, images, custom fonts, and streaming output. No browser, no system dependencies.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//! SVG tree to PDF content stream renderer.

use crate::parser::svg::{PathCommand, SvgNode, SvgStyle, SvgTransform, SvgTree};

/// Render an SVG tree to PDF content stream operators.
///
/// The caller must wrap this in a `q ... Q` block and set up the coordinate
/// transform (position on page + y-axis flip).
pub fn render_svg_tree(tree: &SvgTree, out: &mut String) {
    for node in &tree.children {
        render_node(node, out);
    }
}

fn render_node(node: &SvgNode, out: &mut String) {
    match node {
        SvgNode::Group {
            transform,
            children,
            ..
        } => {
            out.push_str("q\n");
            if let Some(SvgTransform::Matrix(a, b, c, d, e, f)) = transform {
                out.push_str(&format!("{a} {b} {c} {d} {e} {f} cm\n"));
            }
            for child in children {
                render_node(child, out);
            }
            out.push_str("Q\n");
        }
        SvgNode::Rect {
            x,
            y,
            width,
            height,
            style,
            ..
        } => {
            apply_style(style, out);
            out.push_str(&format!("{x} {y} {width} {height} re\n"));
            paint(style, out);
        }
        SvgNode::Circle { cx, cy, r, style } => {
            apply_style(style, out);
            // Approximate circle with 4 cubic bezier curves
            emit_circle(*cx, *cy, *r, out);
            paint(style, out);
        }
        SvgNode::Ellipse {
            cx,
            cy,
            rx,
            ry,
            style,
        } => {
            apply_style(style, out);
            emit_ellipse(*cx, *cy, *rx, *ry, out);
            paint(style, out);
        }
        SvgNode::Line {
            x1,
            y1,
            x2,
            y2,
            style,
        } => {
            apply_style(style, out);
            out.push_str(&format!("{x1} {y1} m {x2} {y2} l S\n"));
        }
        SvgNode::Polyline { points, style } => {
            apply_style(style, out);
            emit_polyline(points, false, out);
            out.push_str("S\n"); // stroke only for polyline
        }
        SvgNode::Polygon { points, style } => {
            apply_style(style, out);
            emit_polyline(points, true, out);
            paint(style, out);
        }
        SvgNode::Path { commands, style } => {
            apply_style(style, out);
            emit_path(commands, out);
            paint(style, out);
        }
    }
}

fn apply_style(style: &SvgStyle, out: &mut String) {
    // Fill color
    if let Some((r, g, b)) = style.fill {
        out.push_str(&format!("{r} {g} {b} rg\n"));
    }
    // Stroke color
    if let Some((r, g, b)) = style.stroke {
        out.push_str(&format!("{r} {g} {b} RG\n"));
    }
    // Stroke width
    if style.stroke_width > 0.0 {
        out.push_str(&format!("{} w\n", style.stroke_width));
    }
}

fn paint(style: &SvgStyle, out: &mut String) {
    let has_fill = style.fill.is_some();
    let has_stroke = style.stroke.is_some() && style.stroke_width > 0.0;
    match (has_fill, has_stroke) {
        (true, true) => out.push_str("B\n"),   // fill + stroke
        (true, false) => out.push_str("f\n"),  // fill only
        (false, true) => out.push_str("S\n"),  // stroke only
        (false, false) => out.push_str("n\n"), // no paint
    }
}

// Emit a circle approximation using 4 cubic bezier curves
fn emit_circle(cx: f32, cy: f32, r: f32, out: &mut String) {
    emit_ellipse(cx, cy, r, r, out);
}

fn emit_ellipse(cx: f32, cy: f32, rx: f32, ry: f32, out: &mut String) {
    let k = 0.552_284_8_f32;
    let kx = rx * k;
    let ky = ry * k;
    // Start at (cx+rx, cy)
    out.push_str(&format!("{} {} m\n", cx + rx, cy));
    // Top-right quadrant
    out.push_str(&format!(
        "{} {} {} {} {} {} c\n",
        cx + rx,
        cy + ky,
        cx + kx,
        cy + ry,
        cx,
        cy + ry
    ));
    // Top-left quadrant
    out.push_str(&format!(
        "{} {} {} {} {} {} c\n",
        cx - kx,
        cy + ry,
        cx - rx,
        cy + ky,
        cx - rx,
        cy
    ));
    // Bottom-left quadrant
    out.push_str(&format!(
        "{} {} {} {} {} {} c\n",
        cx - rx,
        cy - ky,
        cx - kx,
        cy - ry,
        cx,
        cy - ry
    ));
    // Bottom-right quadrant
    out.push_str(&format!(
        "{} {} {} {} {} {} c\n",
        cx + kx,
        cy - ry,
        cx + rx,
        cy - ky,
        cx + rx,
        cy
    ));
    out.push_str("h\n"); // close path
}

fn emit_polyline(points: &[(f32, f32)], close: bool, out: &mut String) {
    for (i, (x, y)) in points.iter().enumerate() {
        if i == 0 {
            out.push_str(&format!("{x} {y} m\n"));
        } else {
            out.push_str(&format!("{x} {y} l\n"));
        }
    }
    if close {
        out.push_str("h\n");
    }
}

fn emit_path(commands: &[PathCommand], out: &mut String) {
    for cmd in commands {
        match cmd {
            PathCommand::MoveTo(x, y) => out.push_str(&format!("{x} {y} m\n")),
            PathCommand::LineTo(x, y) => out.push_str(&format!("{x} {y} l\n")),
            PathCommand::CubicTo(x1, y1, x2, y2, x, y) => {
                out.push_str(&format!("{x1} {y1} {x2} {y2} {x} {y} c\n"));
            }
            PathCommand::QuadTo(_cx, _cy, x, y) => {
                // Convert quadratic to cubic bezier (would need current point tracking)
                // For simplicity, approximate as line to endpoint
                out.push_str(&format!("{x} {y} l\n"));
            }
            PathCommand::ClosePath => out.push_str("h\n"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::svg::{PathCommand, SvgNode, SvgStyle, SvgTransform, SvgTree};

    fn style_fill(r: f32, g: f32, b: f32) -> SvgStyle {
        SvgStyle {
            fill: Some((r, g, b)),
            stroke: None,
            stroke_width: 0.0,
            opacity: 1.0,
        }
    }

    fn style_stroke(r: f32, g: f32, b: f32, w: f32) -> SvgStyle {
        SvgStyle {
            fill: None,
            stroke: Some((r, g, b)),
            stroke_width: w,
            opacity: 1.0,
        }
    }

    fn style_fill_and_stroke() -> SvgStyle {
        SvgStyle {
            fill: Some((1.0, 0.0, 0.0)),
            stroke: Some((0.0, 0.0, 1.0)),
            stroke_width: 2.0,
            opacity: 1.0,
        }
    }

    fn style_none() -> SvgStyle {
        SvgStyle {
            fill: None,
            stroke: None,
            stroke_width: 0.0,
            opacity: 1.0,
        }
    }

    fn tree_with(children: Vec<SvgNode>) -> SvgTree {
        SvgTree {
            width: 100.0,
            height: 100.0,
            view_box: None,
            children,
        }
    }

    // ---- Rect tests ----

    #[test]
    fn render_rect_with_fill() {
        let tree = tree_with(vec![SvgNode::Rect {
            x: 10.0,
            y: 20.0,
            width: 80.0,
            height: 60.0,
            rx: 0.0,
            ry: 0.0,
            style: style_fill(1.0, 0.0, 0.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("1 0 0 rg\n"), "should set red fill");
        assert!(out.contains("10 20 80 60 re\n"), "should emit rect");
        assert!(out.contains("f\n"), "should paint fill only");
    }

    #[test]
    fn render_rect_with_stroke_only() {
        let tree = tree_with(vec![SvgNode::Rect {
            x: 0.0,
            y: 0.0,
            width: 50.0,
            height: 50.0,
            rx: 0.0,
            ry: 0.0,
            style: style_stroke(0.0, 1.0, 0.0, 3.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("0 1 0 RG\n"), "should set green stroke");
        assert!(out.contains("3 w\n"), "should set stroke width");
        assert!(out.contains("0 0 50 50 re\n"), "should emit rect");
        assert!(out.contains("S\n"), "should paint stroke only");
    }

    #[test]
    fn render_rect_fill_and_stroke() {
        let tree = tree_with(vec![SvgNode::Rect {
            x: 0.0,
            y: 0.0,
            width: 50.0,
            height: 50.0,
            rx: 0.0,
            ry: 0.0,
            style: style_fill_and_stroke(),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("1 0 0 rg\n"), "should set fill color");
        assert!(out.contains("0 0 1 RG\n"), "should set stroke color");
        assert!(out.contains("2 w\n"), "should set stroke width");
        assert!(out.contains("B\n"), "should paint fill+stroke");
    }

    #[test]
    fn render_rect_no_paint() {
        let tree = tree_with(vec![SvgNode::Rect {
            x: 0.0,
            y: 0.0,
            width: 10.0,
            height: 10.0,
            rx: 0.0,
            ry: 0.0,
            style: style_none(),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("n\n"), "should emit no-paint operator");
    }

    // ---- Circle tests ----

    #[test]
    fn render_circle_with_fill() {
        let tree = tree_with(vec![SvgNode::Circle {
            cx: 50.0,
            cy: 50.0,
            r: 25.0,
            style: style_fill(0.0, 0.0, 1.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("0 0 1 rg\n"), "should set blue fill");
        // Circle start point: cx+r = 75, cy = 50
        assert!(out.contains("75 50 m\n"), "should move to circle start");
        // Should have 4 cubic bezier curves
        assert_eq!(out.matches(" c\n").count(), 4, "should have 4 cubic curves");
        assert!(out.contains("h\n"), "should close path");
        assert!(out.contains("f\n"), "should paint fill");
    }

    #[test]
    fn render_circle_with_stroke() {
        let tree = tree_with(vec![SvgNode::Circle {
            cx: 50.0,
            cy: 50.0,
            r: 10.0,
            style: style_stroke(1.0, 0.0, 0.0, 1.5),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("1 0 0 RG\n"), "should set stroke color");
        assert!(out.contains("1.5 w\n"), "should set stroke width");
        assert!(out.contains("S\n"), "should stroke only");
    }

    // ---- Ellipse tests ----

    #[test]
    fn render_ellipse_with_fill() {
        let tree = tree_with(vec![SvgNode::Ellipse {
            cx: 50.0,
            cy: 50.0,
            rx: 30.0,
            ry: 20.0,
            style: style_fill(0.0, 1.0, 0.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("0 1 0 rg\n"), "should set green fill");
        // Ellipse start point: cx+rx = 80, cy = 50
        assert!(out.contains("80 50 m\n"), "should move to ellipse start");
        assert_eq!(out.matches(" c\n").count(), 4, "should have 4 cubic curves");
        assert!(out.contains("h\n"), "should close path");
        assert!(out.contains("f\n"), "should paint fill");
    }

    #[test]
    fn render_ellipse_fill_and_stroke() {
        let tree = tree_with(vec![SvgNode::Ellipse {
            cx: 0.0,
            cy: 0.0,
            rx: 10.0,
            ry: 5.0,
            style: style_fill_and_stroke(),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("B\n"), "should paint fill+stroke");
    }

    // ---- Line tests ----

    #[test]
    fn render_line() {
        let tree = tree_with(vec![SvgNode::Line {
            x1: 0.0,
            y1: 0.0,
            x2: 100.0,
            y2: 100.0,
            style: style_stroke(0.0, 0.0, 0.0, 1.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(
            out.contains("0 0 m 100 100 l S\n"),
            "should emit line with stroke"
        );
    }

    #[test]
    fn render_line_with_fill_style() {
        // Lines use apply_style but always stroke via the inline S operator
        let tree = tree_with(vec![SvgNode::Line {
            x1: 5.0,
            y1: 10.0,
            x2: 50.0,
            y2: 60.0,
            style: style_fill(1.0, 1.0, 0.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("1 1 0 rg\n"), "should apply fill style");
        assert!(out.contains("5 10 m 50 60 l S\n"), "should emit line");
    }

    // ---- Polyline tests ----

    #[test]
    fn render_polyline() {
        let tree = tree_with(vec![SvgNode::Polyline {
            points: vec![(0.0, 0.0), (10.0, 20.0), (30.0, 40.0)],
            style: style_stroke(1.0, 0.0, 0.0, 2.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("0 0 m\n"), "first point should be moveto");
        assert!(out.contains("10 20 l\n"), "second point should be lineto");
        assert!(out.contains("30 40 l\n"), "third point should be lineto");
        assert!(!out.contains("h\n"), "polyline should not close path");
        assert!(out.contains("S\n"), "polyline should stroke");
    }

    #[test]
    fn render_polyline_empty() {
        let tree = tree_with(vec![SvgNode::Polyline {
            points: vec![],
            style: style_stroke(0.0, 0.0, 0.0, 1.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        // Should still emit S even with no points
        assert!(out.contains("S\n"));
    }

    // ---- Polygon tests ----

    #[test]
    fn render_polygon_with_fill() {
        let tree = tree_with(vec![SvgNode::Polygon {
            points: vec![(0.0, 0.0), (50.0, 0.0), (25.0, 50.0)],
            style: style_fill(0.0, 0.0, 1.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("0 0 m\n"), "first point should be moveto");
        assert!(out.contains("50 0 l\n"), "second point should be lineto");
        assert!(out.contains("25 50 l\n"), "third point should be lineto");
        assert!(out.contains("h\n"), "polygon should close path");
        assert!(out.contains("f\n"), "polygon should paint fill");
    }

    #[test]
    fn render_polygon_fill_and_stroke() {
        let tree = tree_with(vec![SvgNode::Polygon {
            points: vec![(0.0, 0.0), (10.0, 0.0), (10.0, 10.0)],
            style: style_fill_and_stroke(),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("h\n"), "polygon should close path");
        assert!(out.contains("B\n"), "should paint fill+stroke");
    }

    // ---- Path tests ----

    #[test]
    fn render_path_moveto_lineto() {
        let tree = tree_with(vec![SvgNode::Path {
            commands: vec![
                PathCommand::MoveTo(0.0, 0.0),
                PathCommand::LineTo(10.0, 10.0),
            ],
            style: style_fill(1.0, 0.0, 0.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("0 0 m\n"), "should emit moveto");
        assert!(out.contains("10 10 l\n"), "should emit lineto");
        assert!(out.contains("f\n"), "should paint fill");
    }

    #[test]
    fn render_path_cubic_to() {
        let tree = tree_with(vec![SvgNode::Path {
            commands: vec![
                PathCommand::MoveTo(0.0, 0.0),
                PathCommand::CubicTo(1.0, 2.0, 3.0, 4.0, 5.0, 6.0),
            ],
            style: style_stroke(0.0, 0.0, 0.0, 1.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("1 2 3 4 5 6 c\n"), "should emit cubic bezier");
        assert!(out.contains("S\n"), "should stroke");
    }

    #[test]
    fn render_path_quad_to() {
        let tree = tree_with(vec![SvgNode::Path {
            commands: vec![
                PathCommand::MoveTo(0.0, 0.0),
                PathCommand::QuadTo(5.0, 5.0, 10.0, 10.0),
            ],
            style: style_fill(0.0, 1.0, 0.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        // QuadTo is approximated as lineto to endpoint
        assert!(
            out.contains("10 10 l\n"),
            "QuadTo should approximate as lineto"
        );
    }

    #[test]
    fn render_path_close() {
        let tree = tree_with(vec![SvgNode::Path {
            commands: vec![
                PathCommand::MoveTo(0.0, 0.0),
                PathCommand::LineTo(10.0, 0.0),
                PathCommand::LineTo(10.0, 10.0),
                PathCommand::ClosePath,
            ],
            style: style_fill_and_stroke(),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("h\n"), "should emit close path");
        assert!(out.contains("B\n"), "should paint fill+stroke");
    }

    #[test]
    fn render_path_all_commands() {
        let tree = tree_with(vec![SvgNode::Path {
            commands: vec![
                PathCommand::MoveTo(0.0, 0.0),
                PathCommand::LineTo(10.0, 0.0),
                PathCommand::CubicTo(20.0, 0.0, 20.0, 10.0, 10.0, 10.0),
                PathCommand::QuadTo(5.0, 15.0, 0.0, 10.0),
                PathCommand::ClosePath,
            ],
            style: style_fill(0.5, 0.5, 0.5),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("0 0 m\n"));
        assert!(out.contains("10 0 l\n"));
        assert!(out.contains("20 0 20 10 10 10 c\n"));
        assert!(out.contains("0 10 l\n")); // QuadTo approximated
        assert!(out.contains("h\n"));
        assert!(out.contains("f\n"));
    }

    // ---- Group tests ----

    #[test]
    fn render_group_without_transform() {
        let tree = tree_with(vec![SvgNode::Group {
            transform: None,
            children: vec![SvgNode::Rect {
                x: 0.0,
                y: 0.0,
                width: 10.0,
                height: 10.0,
                rx: 0.0,
                ry: 0.0,
                style: style_fill(1.0, 0.0, 0.0),
            }],
            style: SvgStyle::default(),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.starts_with("q\n"), "should save graphics state");
        assert!(out.contains("0 0 10 10 re\n"), "should render child rect");
        assert!(out.ends_with("Q\n"), "should restore graphics state");
        // No cm operator since no transform
        assert!(
            !out.contains(" cm\n"),
            "should not have cm without transform"
        );
    }

    #[test]
    fn render_group_with_transform() {
        let tree = tree_with(vec![SvgNode::Group {
            transform: Some(SvgTransform::Matrix(1.0, 0.0, 0.0, 1.0, 10.0, 20.0)),
            children: vec![SvgNode::Rect {
                x: 0.0,
                y: 0.0,
                width: 5.0,
                height: 5.0,
                rx: 0.0,
                ry: 0.0,
                style: style_fill(0.0, 0.0, 0.0),
            }],
            style: SvgStyle::default(),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("q\n"), "should save state");
        assert!(out.contains("1 0 0 1 10 20 cm\n"), "should apply transform");
        assert!(out.contains("0 0 5 5 re\n"), "should render child");
        assert!(out.contains("Q\n"), "should restore state");
    }

    #[test]
    fn render_nested_groups() {
        let tree = tree_with(vec![SvgNode::Group {
            transform: None,
            children: vec![SvgNode::Group {
                transform: Some(SvgTransform::Matrix(2.0, 0.0, 0.0, 2.0, 0.0, 0.0)),
                children: vec![SvgNode::Circle {
                    cx: 10.0,
                    cy: 10.0,
                    r: 5.0,
                    style: style_fill(1.0, 1.0, 0.0),
                }],
                style: SvgStyle::default(),
            }],
            style: SvgStyle::default(),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        // Should have two q/Q pairs (nested groups)
        assert_eq!(out.matches("q\n").count(), 2, "two nested save states");
        assert_eq!(out.matches("Q\n").count(), 2, "two nested restore states");
        assert!(out.contains("2 0 0 2 0 0 cm\n"), "inner transform");
    }

    // ---- Empty tree ----

    #[test]
    fn render_empty_tree() {
        let tree = tree_with(vec![]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.is_empty(), "empty tree should produce no output");
    }

    // ---- Multiple children ----

    #[test]
    fn render_multiple_children() {
        let tree = tree_with(vec![
            SvgNode::Rect {
                x: 0.0,
                y: 0.0,
                width: 10.0,
                height: 10.0,
                rx: 0.0,
                ry: 0.0,
                style: style_fill(1.0, 0.0, 0.0),
            },
            SvgNode::Circle {
                cx: 50.0,
                cy: 50.0,
                r: 10.0,
                style: style_fill(0.0, 1.0, 0.0),
            },
        ]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("0 0 10 10 re\n"), "should render rect");
        assert!(out.contains("60 50 m\n"), "should render circle start");
    }

    // ---- apply_style edge cases ----

    #[test]
    fn apply_style_stroke_with_zero_width_not_emitted_in_paint() {
        // stroke is Some but stroke_width is 0 => paint treats as no stroke
        let tree = tree_with(vec![SvgNode::Rect {
            x: 0.0,
            y: 0.0,
            width: 10.0,
            height: 10.0,
            rx: 0.0,
            ry: 0.0,
            style: SvgStyle {
                fill: Some((1.0, 0.0, 0.0)),
                stroke: Some((0.0, 0.0, 0.0)),
                stroke_width: 0.0,
                opacity: 1.0,
            },
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        // stroke color is emitted by apply_style (it doesn't check width)
        assert!(out.contains("0 0 0 RG\n"), "stroke color still applied");
        // but paint should be fill-only because stroke_width is 0
        assert!(out.contains("f\n"), "paint should be fill only");
        assert!(!out.contains("B\n"), "should not be fill+stroke");
    }

    // ---- paint edge: stroke present but no fill ----

    #[test]
    fn paint_stroke_only_no_fill() {
        let tree = tree_with(vec![SvgNode::Path {
            commands: vec![
                PathCommand::MoveTo(0.0, 0.0),
                PathCommand::LineTo(10.0, 10.0),
            ],
            style: style_stroke(0.0, 0.0, 0.0, 1.0),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("S\n"), "should stroke only");
        assert!(!out.contains("f\n"), "should not fill");
        assert!(!out.contains("B\n"), "should not fill+stroke");
    }

    // ---- paint edge: neither fill nor stroke ----

    #[test]
    fn paint_no_fill_no_stroke() {
        let tree = tree_with(vec![SvgNode::Ellipse {
            cx: 0.0,
            cy: 0.0,
            rx: 10.0,
            ry: 10.0,
            style: style_none(),
        }]);
        let mut out = String::new();
        render_svg_tree(&tree, &mut out);
        assert!(out.contains("n\n"), "should emit no-paint");
    }
}