ironpress 1.4.4

Pure Rust HTML/CSS/Markdown to PDF converter with layout engine, LaTeX math, 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
    /// render_nested_layout_elements: rowspan == 0 skips the cell
    #[test]
    fn layout_elements_nested_rowspan_zero_skips_cell() {
        let run = test_text_run("Skipped");
        let run_visible = TextRun {
            text: "Visible".to_string(),
            ..run.clone()
        };
        // rowspan=0 means "continuation" — renderer skips it
        let mut cell_skip = table_cell(vec![TextLine {
                runs: vec![run],
                height: 14.0,
                baseline_ascent: None,
                x_offset: 0.0,
                metadata: Default::default(),
            }]);
        cell_skip.span.rows = 0;
        let cell_visible = table_cell(vec![TextLine {
                runs: vec![run_visible],
                height: 14.0,
                baseline_ascent: None,
                x_offset: 0.0,
                metadata: Default::default(),
            }]);
        let element = test_table_row(vec![cell_skip, cell_visible], vec![100.0, 100.0]).boxed();
        let custom_fonts = HashMap::new();
        let prepared_custom_fonts = PreparedCustomFonts::new();
        let mut pdf_writer = PdfWriter::new();
        let mut page_images = Vec::new();
        let mut shadings = Vec::new();
        let mut shading_counter = 0usize;
        let mut page_ext_gstates = Vec::new();
        let mut bg_alpha_counter = 0usize;
        let mut annotations = Vec::new();
        let mut ctx = PageRenderContext::new(
            &mut pdf_writer,
            &mut page_images,
            &custom_fonts,
            &prepared_custom_fonts,
            &mut shadings,
            &mut shading_counter,
            &mut page_ext_gstates,
            &mut bg_alpha_counter,
            &mut annotations,
            TEST_PAGE_PAINT_BOX,
            TEST_PAGE_PAINT_BOX.height,
        );
        let mut content = String::new();
        render_nested_layout_elements(
            &mut content,
            &[element],
            NestedLayoutFrame::new(PdfPoint::new(0.0, 100.0), PdfPoint::new(0.0, 100.0), 200.0),
            &mut ctx,
        );
        assert!(
            content.contains("(Visible)"),
            "Visible cell should be rendered"
        );
        assert!(
            !content.contains("(Skipped)"),
            "rowspan=0 cell should be skipped"
        );
    }

    /// render_nested_layout_elements: cell with background_color in nested table
    #[test]
    fn layout_elements_nested_table_cell_background_color() {
        let run = test_text_run("BgCell");
        let mut cell = table_cell(vec![TextLine {
                runs: vec![run],
                height: 14.0,
                baseline_ascent: None,
                x_offset: 0.0,
                metadata: Default::default(),
            }]);
        cell.layout.paint.background.color = Some(Color::rgb(255, 0, 0));
        cell.layout.box_model.content_insets = EdgeSizes::uniform(2.0);
        let element = test_table_row(vec![cell], vec![100.0]).boxed();
        let custom_fonts = HashMap::new();
        let prepared_custom_fonts = PreparedCustomFonts::new();
        let mut pdf_writer = PdfWriter::new();
        let mut page_images = Vec::new();
        let mut shadings = Vec::new();
        let mut shading_counter = 0usize;
        let mut page_ext_gstates = Vec::new();
        let mut bg_alpha_counter = 0usize;
        let mut annotations = Vec::new();
        let mut ctx = PageRenderContext::new(
            &mut pdf_writer,
            &mut page_images,
            &custom_fonts,
            &prepared_custom_fonts,
            &mut shadings,
            &mut shading_counter,
            &mut page_ext_gstates,
            &mut bg_alpha_counter,
            &mut annotations,
            TEST_PAGE_PAINT_BOX,
            TEST_PAGE_PAINT_BOX.height,
        );
        let mut content = String::new();
        render_nested_layout_elements(
            &mut content,
            &[element],
            NestedLayoutFrame::new(PdfPoint::new(0.0, 100.0), PdfPoint::new(0.0, 100.0), 100.0),
            &mut ctx,
        );
        assert!(
            content.contains("1 0 0 rg"),
            "Should have red cell background fill"
        );
        assert!(
            content.contains("re\nf\n"),
            "Should have filled rect for cell background"
        );
        assert!(content.contains("(BgCell)"), "Should render cell text");
    }

    /// render_cell_text: text-align right and center in nested table cell
    #[test]
    fn layout_elements_cell_text_align_right_and_center() {
        let custom_fonts = HashMap::new();
        let prepared_custom_fonts = PreparedCustomFonts::new();
        let mut annotations = Vec::new();
        let mut ts_pdf_writer = PdfWriter::new();
        let mut ts_page_images = Vec::new();
        let mut ctx = TextRenderContext::new(
            TEST_PAGE_PAINT_BOX.height,
            &custom_fonts,
            &prepared_custom_fonts,
            &mut annotations,
            &mut ts_pdf_writer,
            &mut ts_page_images,
        );

        let run = test_text_run("Aligned");

        // Test right-align
        let cell_right = text_cell(
            vec![TextLine {
                runs: vec![run.clone()],
                height: 14.0,
                baseline_ascent: None,
                x_offset: 0.0,
                metadata: Default::default(),
            }],
            TextAlign::Right,
        );
        let mut content_right = String::new();
        render_cell_text(
            &mut content_right,
            &cell_right,
            CellTextPlacement::new(PdfPoint::new(0.0, 100.0), 200.0),
            &mut ctx,
        );
        assert!(
            content_right.contains("(Aligned)"),
            "Should render right-aligned text"
        );

        // Test center-align
        let cell_center = text_cell(
            vec![TextLine {
                runs: vec![run],
                height: 14.0,
                baseline_ascent: None,
                x_offset: 0.0,
                metadata: Default::default(),
            }],
            TextAlign::Center,
        );
        let mut content_center = String::new();
        render_cell_text(
            &mut content_center,
            &cell_center,
            CellTextPlacement::new(PdfPoint::new(0.0, 100.0), 200.0),
            &mut ctx,
        );
        assert!(
            content_center.contains("(Aligned)"),
            "Should render center-aligned text"
        );
    }

    /// render_cell_text: underline and line_through in nested table cell
    #[test]
    fn layout_elements_cell_text_underline_and_line_through() {
        let custom_fonts = HashMap::new();
        let prepared_custom_fonts = PreparedCustomFonts::new();
        let mut annotations = Vec::new();
        let mut ts_pdf_writer = PdfWriter::new();
        let mut ts_page_images = Vec::new();
        let mut ctx = TextRenderContext::new(
            TEST_PAGE_PAINT_BOX.height,
            &custom_fonts,
            &prepared_custom_fonts,
            &mut annotations,
            &mut ts_pdf_writer,
            &mut ts_page_images,
        );

        let underline_run = TextRun {
            decorations: vec![crate::style::computed::TextDecoration {
                lines: crate::style::computed::TextDecorationLines {
                    underline: true,
                    ..Default::default()
                },
                ..Default::default()
            }],
            ..test_text_run("Under")
        };
        let strike_run = TextRun {
            decorations: vec![crate::style::computed::TextDecoration {
                lines: crate::style::computed::TextDecorationLines {
                    line_through: true,
                    ..Default::default()
                },
                ..Default::default()
            }],
            ..test_text_run("Strike")
        };

        let cell = text_cell(
            vec![
                TextLine {
                    runs: vec![underline_run],
                    height: 14.0,
                    baseline_ascent: None,
                    x_offset: 0.0,
                    metadata: Default::default(),
                },
                TextLine {
                    runs: vec![strike_run],
                    height: 14.0,
                    baseline_ascent: None,
                    x_offset: 0.0,
                    metadata: Default::default(),
                },
            ],
            TextAlign::Left,
        );

        let mut content = String::new();
        render_cell_text(
            &mut content,
            &cell,
            CellTextPlacement::new(PdfPoint::new(10.0, 200.0), 150.0),
            &mut ctx,
        );
        assert!(content.contains("(Under)"), "Should render underlined text");
        assert!(
            content.contains("(Strike)"),
            "Should render struck-through text"
        );
        // Both decorations draw filled rectangles in the cell text path.
        let decoration_count = filled_rect_count(&content);
        assert!(
            decoration_count >= 2,
            "Should have filled decorations for underline and line-through, got {decoration_count}"
        );
    }

    /// render_cell_text: inline span with background_color and border_radius in nested cell
    #[test]
    fn layout_elements_cell_text_inline_bg_with_border_radius() {
        let custom_fonts = HashMap::new();
        let prepared_custom_fonts = PreparedCustomFonts::new();
        let mut annotations = Vec::new();
        let mut ts_pdf_writer = PdfWriter::new();
        let mut ts_page_images = Vec::new();
        let mut ctx = TextRenderContext::new(
            TEST_PAGE_PAINT_BOX.height,
            &custom_fonts,
            &prepared_custom_fonts,
            &mut annotations,
            &mut ts_pdf_writer,
            &mut ts_page_images,
        );

        let run = TextRun {
            color: Color::WHITE,
            background_color: Some(Color::from_srgb(0.2, 0.4, 0.8, 1.0)),
            padding: EdgeSizes::axes(3.0, 2.0),
            border_radii: CornerRadii::circular(4.0),
            ..test_text_run("Badge")
        };

        let cell = text_cell(
            vec![TextLine {
                runs: vec![run],
                height: 14.0,
                baseline_ascent: None,
                x_offset: 0.0,
                metadata: Default::default(),
            }],
            TextAlign::Left,
        );

        let mut content = String::new();
        render_cell_text(
            &mut content,
            &cell,
            CellTextPlacement::new(PdfPoint::new(10.0, 200.0), 150.0),
            &mut ctx,
        );
        assert!(content.contains("(Badge)"), "Should render badge text");
        // Inline background fill (rounded rect uses Bezier c operator)
        assert!(
            content.contains("0.2 0.4 0.8 rg"),
            "Should have blue inline background color"
        );
        assert!(
            content.contains(" c\n"),
            "Should have Bezier curves for rounded inline bg"
        );
    }

    /// render_cell_text: inline span with background_color but no border_radius (rect path)
    #[test]
    fn layout_elements_cell_text_inline_bg_no_border_radius() {
        let custom_fonts = HashMap::new();
        let prepared_custom_fonts = PreparedCustomFonts::new();
        let mut annotations = Vec::new();
        let mut ts_pdf_writer = PdfWriter::new();
        let mut ts_page_images = Vec::new();
        let mut ctx = TextRenderContext::new(
            TEST_PAGE_PAINT_BOX.height,
            &custom_fonts,
            &prepared_custom_fonts,
            &mut annotations,
            &mut ts_pdf_writer,
            &mut ts_page_images,
        );

        let run = TextRun {
            background_color: Some(Color::rgb(255, 255, 0)), // yellow
            padding: EdgeSizes::axes(2.0, 1.0),
            ..test_text_run("Tag")
        };

        let cell = text_cell(
            vec![TextLine {
                runs: vec![run],
                height: 14.0,
                baseline_ascent: None,
                x_offset: 0.0,
                metadata: Default::default(),
            }],
            TextAlign::Left,
        );

        let mut content = String::new();
        render_cell_text(
            &mut content,
            &cell,
            CellTextPlacement::new(PdfPoint::new(10.0, 200.0), 150.0),
            &mut ctx,
        );
        assert!(content.contains("(Tag)"), "Should render tag text");
        assert!(
            content.contains("1 1 0 rg"),
            "Should have yellow inline background color"
        );
        // No border-radius: should use rectangle re operator
        assert!(
            content.contains(" re\nf\n"),
            "Should use rectangle fill for zero-radius inline bg"
        );
    }

    /// plan_nested_layout_elements: Position::Relative with positioned_depth registers origin
    #[test]
    fn layout_elements_plan_relative_with_positioned_depth() {
        let mut relative = test_text_block_from_runs(vec![test_text_run("Relative")]);
        relative.update_text(|block| {
            block.positioning.scheme = Position::Relative;
            block.positioning.insets.top = 5.0;
            block.positioning.insets.left = 15.0;
            block.positioning.containing_block_depth = 1;
        });
        let elements = [relative];
        let planned = plan_nested_layout_elements(
            &elements,
            NestedLayoutFrame::new(PdfPoint::new(20.0, 80.0), PdfPoint::new(10.0, 120.0), 100.0),
        );
        assert_eq!(planned.len(), 1);
        // Relative: uses local origin (20.0) + offset_left (15.0)
        assert!(
            (planned[0].origin.x - 35.0).abs() < 0.01,
            "Relative block origin_x should be frame.origin_x + offset_left"
        );
        // top_y: cursor_y (80.0) - margin_top (0) - offset_top (5) = 75.0
        assert!(
            (planned[0].origin.y - 75.0).abs() < 0.01,
            "Relative block top_y should be cursor_y - offset_top"
        );
    }

    /// plan_nested_layout_elements: absolute with containing_block sets blur_canvas_box
    #[test]
    fn layout_elements_plan_absolute_with_containing_block_sets_blur_canvas_box() {
        let containing = crate::layout::engine::ContainingBlock {
            x: 5.0,
            width: 200.0,
            height: 100.0,
            depth: 2,
        };
        let mut absolute = test_text_block_from_runs(vec![test_text_run("Abs")]);
        absolute.update_text(|block| {
            block.positioning.scheme = Position::Absolute;
            block.positioning.containing_block = Some(containing);
            block.positioning.containing_block_depth = 0;
        });
        // First register a positioned origin for depth 2 by planning a relative block
        let mut relative_parent = test_text_block_from_runs(vec![test_text_run("Parent")]);
        relative_parent.update_text(|block| {
            block.positioning.scheme = Position::Relative;
            block.positioning.containing_block_depth = 2;
        });
        let elements = [relative_parent, absolute];
        let planned = plan_nested_layout_elements(
            &elements,
            NestedLayoutFrame::new(
                PdfPoint::new(10.0, 200.0),
                PdfPoint::new(10.0, 200.0),
                300.0,
            ),
        );
        // The absolute element should have a blur_canvas_box derived from the containing block
        let _abs_planned = planned
            .iter()
            .find(|planned| planned.element.inspect_text(|_| ()).is_some());
        // Just verify the plan succeeds without panic and produces 2 elements
        assert_eq!(planned.len(), 2, "Should plan both elements");
    }

    /// table_row_total_height: returns 0 for non-TableRow variant
    #[test]
    fn layout_elements_table_row_total_height_non_row_returns_zero() {
        let non_row = PageBreak::default();
        assert_eq!(
            table_row_total_height(&non_row),
            0.0,
            "Non-TableRow element should return 0 height"
        );
        let text_block = test_text_block_from_runs(vec![test_text_run("Hello")]);
        assert_eq!(
            table_row_total_height(&text_block),
            0.0,
            "TextBlock element should return 0 height"
        );
    }

    /// Integration: nested table with vertical-align middle exercises layout_elements paths
    #[test]
    fn layout_elements_nested_table_cell_vertical_align_middle_integration() {
        let html = r#"<table>
            <tr>
                <td>
                    <table>
                        <tr>
                            <td style="vertical-align: middle; height: 50pt">Inner</td>
                            <td style="height: 50pt">Other</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>"#;
        let nodes = parse_html(html).unwrap();
        let pages = layout(&nodes, PageSize::A4, Margin::default());
        let pdf = render_pdf(&pages, PageSize::A4, Margin::default()).unwrap();
        let pdf_str = String::from_utf8_lossy(&pdf);
        assert!(
            pdf_str.contains("(Inner)"),
            "Should render inner nested cell text"
        );
    }

    /// Integration: nested div inside table cell with SVG background
    /// exercises render_nested_text_block with background_svg via nested cell rows
    #[test]
    fn layout_elements_nested_svg_background_in_table_cell() {
        // A div with SVG background inside a td triggers render_nested_layout_elements
        // and render_nested_text_block with background_svg set
        let html = r#"<table>
            <tr>
                <td>
                    <div style="background-image: url('data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%2210%22 height=%2210%22%3E%3Crect width=%2210%22 height=%2210%22 fill=%22red%22/%3E%3C/svg%3E'); background-size: cover; width: 40pt; height: 20pt;">CellSVG</div>
                </td>
            </tr>
        </table>"#;
        let nodes = parse_html(html).unwrap();
        let pages = layout(&nodes, PageSize::A4, Margin::default());
        let pdf = render_pdf(&pages, PageSize::A4, Margin::default()).unwrap();
        let pdf_str = String::from_utf8_lossy(&pdf);
        // The text should render
        assert!(
            pdf_str.contains("(CellSVG)"),
            "Should render text inside nested cell div"
        );
        // The overall PDF should be valid (no crash on SVG background in nested context)
        assert!(pdf_str.contains("%PDF-1.4"), "Should produce a valid PDF");
    }

    /// Integration: border-collapse collapse with nested elements
    #[test]
    fn layout_elements_nested_border_collapse() {
        let html = r#"<table style="border-collapse: collapse">
            <tr>
                <td style="border: 1pt solid black">CollapseA</td>
                <td style="border: 1pt solid black">CollapseB</td>
            </tr>
        </table>"#;
        let nodes = parse_html(html).unwrap();
        let pages = layout(&nodes, PageSize::A4, Margin::default());
        let pdf = render_pdf(&pages, PageSize::A4, Margin::default()).unwrap();
        let pdf_str = String::from_utf8_lossy(&pdf);
        assert!(pdf_str.contains("(CollapseA)"), "Should render first cell");
        assert!(pdf_str.contains("(CollapseB)"), "Should render second cell");
    }

    /// Integration: nested table with rowspan > 1 spanning into future rows
    #[test]
    fn layout_elements_nested_rowspan_spans_future_rows() {
        let html = r#"<table>
            <tr>
                <td>
                    <table>
                        <tr>
                            <td rowspan="2">SpanInner</td>
                            <td>A</td>
                        </tr>
                        <tr>
                            <td>B</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>"#;
        let nodes = parse_html(html).unwrap();
        let pages = layout(&nodes, PageSize::A4, Margin::default());
        let pdf = render_pdf(&pages, PageSize::A4, Margin::default()).unwrap();
        let pdf_str = String::from_utf8_lossy(&pdf);
        assert!(
            pdf_str.contains("(SpanInner)"),
            "Should render spanning nested cell"
        );
        assert!(
            pdf_str.contains("(A)"),
            "Should render first row second cell"
        );
        assert!(
            pdf_str.contains("(B)"),
            "Should render second row second cell"
        );
    }
fn text_cell(lines: Vec<TextLine>, inline: TextAlign) -> CellBox {
    CellBox {
        content: crate::layout::cells::CellContent {
            lines,
            ..Default::default()
        },
        alignment: crate::layout::cells::CellAlignment {
            inline,
            block: VerticalAlign::Top,
        },
        ..Default::default()
    }
}

fn table_cell(lines: Vec<TextLine>) -> TableCell {
    TableCell {
        layout: text_cell(lines, TextAlign::Left),
        ..Default::default()
    }
}