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
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
use super::*;
use crate::layout::elements::{
    ColumnRule, Container, FlexRow, FragmentPlacementOwner, GridRow, HorizontalRule, Image,
    LayoutNode, LayoutVisitor, MathBlock, ProgressBar, Svg, TableRow, TextBlock,
};

mod flex;
mod media;
mod nested;
mod text;

use flex::render_flex_child;
use media::{render_image_child, render_svg_child};
use nested::render_nested_container;
use text::render_text_child;

#[derive(Clone, Copy)]
pub(super) struct ContainerFrame {
    content_origin: PdfPoint,
    size: crate::types::Size,
    padding_origin: PdfPoint,
}

impl ContainerFrame {
    pub(super) const fn new(
        content_origin: PdfPoint,
        size: crate::types::Size,
        padding_origin: PdfPoint,
    ) -> Self {
        Self {
            content_origin,
            size,
            padding_origin,
        }
    }

    const fn width(self) -> f32 {
        self.size.width
    }
}

#[derive(Clone, Copy)]
pub(super) struct ContainerRenderOptions {
    pub(super) device_space_available: bool,
    pub(super) paint_phase: ElementPaintPhase,
    pub(super) stacking_scope: StackingScope,
}

impl Default for ContainerRenderOptions {
    fn default() -> Self {
        Self {
            device_space_available: false,
            paint_phase: ElementPaintPhase::All,
            stacking_scope: StackingScope::Local,
        }
    }
}

#[derive(Clone, Copy)]
struct ContainerFlowContext<'a> {
    frame: ContainerFrame,
    container_top_y: f32,
    flow_top_by_index: &'a HashMap<usize, f32>,
    float_top_by_index: &'a HashMap<usize, f32>,
    left_float_bottom: f32,
    right_float_bottom: f32,
    device_space_available: bool,
    paint_phase: ElementPaintPhase,
}

impl ContainerFlowContext<'_> {
    fn with_paint_phase(mut self, paint_phase: ElementPaintPhase) -> Self {
        self.paint_phase = paint_phase;
        self
    }
}

fn is_nested_row(element: &dyn LayoutElement) -> bool {
    #[derive(Default)]
    struct NestedRow(bool);

    impl LayoutVisitor for NestedRow {
        fn visit_table_row(&mut self, _element: &TableRow) {
            self.0 = true;
        }

        fn visit_grid_row(&mut self, _element: &GridRow) {
            self.0 = true;
        }
    }

    let mut nested = NestedRow::default();
    element.accept(&mut nested);
    nested.0
}

struct DirectChildRenderer<'call, 'flow, 'page> {
    content: &'call mut String,
    child_index: usize,
    flow: &'call ContainerFlowContext<'flow>,
    position: FlowPosition,
    abs_origins: &'call mut HashMap<usize, PdfPoint>,
    ctx: &'call mut PageRenderContext<'page>,
    handled: bool,
    result: Option<FlowPosition>,
}

impl DirectChildRenderer<'_, '_, '_> {
    fn finish(&mut self, position: FlowPosition) {
        self.handled = true;
        self.result = Some(position);
    }

    fn render(&mut self, element: &dyn LayoutElement) {
        if let Some(placed) = element.fragment_placement_owner() {
            self.render_placed_fragment(placed);
        } else {
            element.accept(self);
        }
    }

    fn render_placed_fragment(&mut self, placed: &dyn FragmentPlacementOwner) {
        let placement = placed.fragment_placement();
        let anchor = if placement.uses_padding_box() {
            self.flow.frame.padding_origin
        } else {
            self.flow.frame.content_origin
        };
        let offset = placement.offset();
        let origin = PdfPoint::new(anchor.x + offset.x, anchor.y - offset.y);
        let planned_flow_top = HashMap::from([(0, origin.y)]);
        let flow = ContainerFlowContext {
            frame: ContainerFrame::new(origin, placement.size, origin),
            container_top_y: origin.y,
            flow_top_by_index: &planned_flow_top,
            float_top_by_index: self.flow.float_top_by_index,
            left_float_bottom: self.flow.left_float_bottom,
            right_float_bottom: self.flow.right_float_bottom,
            device_space_available: self.flow.device_space_available,
            paint_phase: self.flow.paint_phase,
        };
        let position = FlowPosition::new(origin.y, origin.y, 0.0);
        let mut renderer = DirectChildRenderer {
            content: self.content,
            child_index: 0,
            flow: &flow,
            position,
            abs_origins: self.abs_origins,
            ctx: self.ctx,
            handled: false,
            result: None,
        };
        placed.fragment_source().accept(&mut renderer);
        self.handled = renderer.handled;
        self.result = Some(self.position);
    }
}

impl LayoutVisitor for DirectChildRenderer<'_, '_, '_> {
    fn visit_text_block(&mut self, element: &TextBlock) {
        let result = render_text_child(
            self.content,
            element,
            self.child_index,
            self.flow,
            self.position,
            self.abs_origins,
            self.ctx,
        );
        self.finish(result);
    }

    fn visit_column_rule(&mut self, element: &ColumnRule) {
        if self.flow.paint_phase.paints_decoration() {
            paint_column_rule_line(
                self.content,
                self.flow.frame.content_origin.x,
                self.flow.frame.content_origin.y,
                element.paint.width,
                element.height,
                &element.paint,
                self.ctx.page_ext_gstates,
                self.ctx.bg_alpha_counter,
            );
        }
        self.finish(self.position);
    }

    fn visit_container(&mut self, element: &Container) {
        let result = render_nested_container(
            self.content,
            element,
            self.child_index,
            self.flow,
            self.position,
            self.abs_origins,
            self.ctx,
        );
        self.finish(result);
    }

    fn visit_image(&mut self, element: &Image) {
        let result = render_image_child(
            self.content,
            element,
            self.child_index,
            self.flow,
            self.position,
            self.abs_origins,
            self.ctx,
        );
        self.finish(result);
    }

    fn visit_svg(&mut self, element: &Svg) {
        let result = render_svg_child(
            self.content,
            element,
            self.child_index,
            self.flow,
            self.position,
            self.abs_origins,
            self.ctx,
        );
        self.finish(result);
    }

    fn visit_flex_row(&mut self, element: &FlexRow) {
        let result = render_flex_child(
            self.content,
            element,
            self.child_index,
            self.flow,
            self.position,
            self.abs_origins,
            self.ctx,
        );
        self.finish(result);
    }

    fn visit_horizontal_rule(&mut self, element: &HorizontalRule) {
        let mut position = self.position;
        let planned_flow_top = self.flow.flow_top_by_index.get(&self.child_index).copied();
        if let Some(top) = planned_flow_top {
            position.y = top;
        } else {
            position.cursor_y -=
                collapsed_margin_top_extra(element.margins.start, position.previous_margin_bottom);
            position.y = position.cursor_y;
        }
        let layout_geometry = LayoutBoxGeometry::new(
            PdfRect::from_top(
                self.flow.frame.content_origin.x,
                position.y,
                self.flow.frame.width(),
                1.0,
            ),
            EdgeSizes::ZERO,
            EdgeSizes::ZERO,
        );
        let box_geometry = self
            .ctx
            .text
            .pdf_writer
            .resolve_box_geometry(layout_geometry);
        let paint_geometry = box_geometry.painting();
        let geometry = box_geometry.fragment(Default::default());
        if self.flow.paint_phase.paints_contents() {
            let group = PaintGroupScope::begin(self.content, element, geometry, self.ctx);
            paint_horizontal_rule(
                self.content,
                PdfPoint::new(
                    paint_geometry.border_box.left,
                    paint_geometry.border_box.top(),
                ),
                paint_geometry.border_box.width,
            );
            group.finish(self.content, self.ctx);
        }
        if planned_flow_top.is_none() {
            position.cursor_y -= 1.0 + element.margins.end;
            position.y = position.cursor_y;
        }
        position.previous_margin_bottom = element.margins.end;
        self.finish(position);
    }

    fn visit_progress_bar(&mut self, element: &ProgressBar) {
        let mut position = self.position;
        let planned_flow_top = self.flow.flow_top_by_index.get(&self.child_index).copied();
        let top = if let Some(top) = planned_flow_top {
            top
        } else {
            position.cursor_y -=
                collapsed_margin_top_extra(element.margins.start, position.previous_margin_bottom);
            position.cursor_y
        };
        let rect = PdfRect::from_top(
            self.flow.frame.content_origin.x,
            top,
            element.size.width,
            element.size.height,
        );
        let layout_geometry = LayoutBoxGeometry::new(rect, EdgeSizes::ZERO, EdgeSizes::ZERO);
        let box_geometry = self
            .ctx
            .text
            .pdf_writer
            .resolve_box_geometry(layout_geometry);
        let paint_geometry = box_geometry.painting();
        let geometry = box_geometry.fragment(Default::default());
        if self.flow.paint_phase.paints_contents() {
            let group = PaintGroupScope::begin(self.content, element, geometry, self.ctx);
            paint_progress_bar(self.content, element, paint_geometry.border_box);
            group.finish(self.content, self.ctx);
        }
        if planned_flow_top.is_none() {
            position.cursor_y -= element.size.height + element.margins.end;
            position.y = position.cursor_y;
        }
        position.previous_margin_bottom = element.margins.end;
        self.finish(position);
    }

    fn visit_math_block(&mut self, element: &MathBlock) {
        let mut position = self.position;
        let planned_flow_top = self.flow.flow_top_by_index.get(&self.child_index).copied();
        let top = if let Some(top) = planned_flow_top {
            top
        } else {
            position.cursor_y -=
                collapsed_margin_top_extra(element.margins.start, position.previous_margin_bottom);
            position.cursor_y
        };
        let layout_geometry = LayoutBoxGeometry::new(
            PdfRect::from_top(
                self.flow.frame.content_origin.x,
                top,
                self.flow.frame.width(),
                element.layout.height(),
            ),
            EdgeSizes::ZERO,
            EdgeSizes::ZERO,
        );
        let geometry = self
            .ctx
            .text
            .pdf_writer
            .resolve_box_geometry(layout_geometry)
            .fragment(Default::default());
        if self.flow.paint_phase.paints_contents() {
            let group = PaintGroupScope::begin(self.content, element, geometry, self.ctx);
            paint_math_block(
                self.content,
                element,
                PdfPoint::new(self.flow.frame.content_origin.x, top),
                self.flow.frame.width(),
            );
            group.finish(self.content, self.ctx);
        }
        if planned_flow_top.is_none() {
            position.cursor_y -= element.layout.height() + element.margins.end;
            position.y = position.cursor_y;
        }
        position.previous_margin_bottom = element.margins.end;
        self.finish(position);
    }
}

pub(super) fn render_container_children(
    content: &mut String,
    children: &[LayoutNode],
    frame: ContainerFrame,
    abs_origins: &mut HashMap<usize, PdfPoint>,
    ctx: &mut PageRenderContext<'_>,
    options: ContainerRenderOptions,
) {
    // Depth zero denotes the initial containing block. In paged media fixed
    // positioned descendants resolve against the page area, regardless of the
    // positioned ancestors they happen to be nested under.
    abs_origins.entry(0).or_insert(ctx.initial_fixed_origin);
    // Padding-box origin (left x, top y in PDF coords) of THIS container, used
    // as the default anchor for absolutely-positioned children. An abs child
    // whose containing block names a *different* positioned ancestor (because it
    // is nested inside static intermediates) overrides this via `abs_origins`.
    let x = frame.content_origin.x;
    let mut y = frame.content_origin.y;
    let device_space_available = options.device_space_available;
    let paint_phase = options.paint_phase;
    let stacking_scope = options.stacking_scope;
    let mut stacking_plan = StackingPaintPlan::default();

    // Separate children handled by render_nested_table_rows from those rendered
    // directly (Container, Svg, etc.).
    // We process all children in order, flushing accumulated nested-layout
    // batches when we hit a directly-handled type.
    let mut nested_batch: Vec<&dyn LayoutElement> = Vec::new();
    let mut cursor_y = y;
    // Save original y for absolute positioning (must not be affected by
    // flow children advancing the cursor).
    let container_top_y = y;
    // Track the previous in-flow block sibling's margin-bottom so adjacent
    // vertical margins collapse (CSS) instead of summing. Nested table batches
    // return the same state, while true barriers and out-of-flow content reset
    // it according to their flow capability.
    let mut prev_margin_bottom: f32 = 0.0;
    let mut nested_batch_position = FlowPosition::new(y, cursor_y, prev_margin_bottom);

    // Simplified CSS floats: precompute each floated child's top (relative to the
    // content-box top) and per-side running bottoms via the shared flow
    // simulator, keyed by source index. Floats are removed from normal flow — the
    // cursor below does not advance for them — but in-flow blocks with `clear`
    // are pushed below the relevant float bottoms. Only computed when a child
    // actually floats, so the common case pays nothing.
    let has_floats = children
        .iter()
        .any(|c| crate::layout::paginate::element_float(c) != Float::None);
    let (float_top_by_index, left_float_bottom, right_float_bottom) = if has_floats {
        let flow = crate::layout::paginate::simulate_block_flow(children);
        let tops: HashMap<usize, f32> = flow.floats.iter().map(|f| (f.index, f.top)).collect();
        // Float bottoms in PDF y (down = smaller y) for `clear`. Floats always
        // precede the blocks that clear them in source order, so the per-side
        // totals from the simulator are exactly what those blocks must clear.
        (
            tops,
            container_top_y - flow.left_float_bottom,
            container_top_y - flow.right_float_bottom,
        )
    } else {
        (HashMap::new(), container_top_y, container_top_y)
    };

    // Geometry and flow are resolved in source order. Paint order is a
    // separate concern: non-in-flow fragments are either collected by this
    // box's stacking context or deferred through ordinary ancestors to the
    // nearest context. This is required for a positioned grandchild to stack
    // against its ancestor's siblings instead of being trapped in the
    // grandparent's direct-child fragment.
    let flow_top_by_index = HashMap::new();
    let flow_context = ContainerFlowContext {
        frame,
        container_top_y,
        flow_top_by_index: &flow_top_by_index,
        float_top_by_index: &float_top_by_index,
        left_float_bottom,
        right_float_bottom,
        device_space_available,
        paint_phase,
    };

    for (child_index, child) in children
        .iter()
        .enumerate()
        .map(|(index, child)| (index, child.as_ref()))
    {
        let handled_by_nested = is_nested_row(child);
        if handled_by_nested {
            if nested_batch.is_empty() {
                nested_batch_position = FlowPosition::new(y, cursor_y, prev_margin_bottom);
            }
            nested_batch.push(child);
            continue;
        }

        // Flush any accumulated nested batch before handling this element
        if !nested_batch.is_empty() {
            let marker = ctx.stacking.marker();
            let mut batch_content = String::new();
            let position = render_rows(
                &mut batch_content,
                &nested_batch,
                x,
                NestedRowsFlow::pending(nested_batch_position),
                paint_phase.paints_contents(),
                abs_origins,
                ctx,
            );
            let descendants = ctx.stacking.take_since(marker);
            ctx.stacking.commit(
                stacking_scope,
                content,
                &mut stacking_plan,
                crate::layout::elements::StackingLevel::in_flow(),
                batch_content,
                descendants,
            );
            y = position.y;
            cursor_y = position.cursor_y;
            prev_margin_bottom = position.previous_margin_bottom;
            nested_batch.clear();
        }

        let child_position = FlowPosition::new(y, cursor_y, prev_margin_bottom);
        let split_in_flow = paint_phase == ElementPaintPhase::All
            && child_paint_order(child).is_in_flow()
            && child
                .in_flow_paint_phase_owner()
                .is_some_and(crate::layout::elements::BoxPaintOwner::supports_phased_paint);
        let (handled, result) = if split_in_flow {
            let render_phase = |phase,
                                abs_origins: &mut HashMap<usize, PdfPoint>,
                                ctx: &mut PageRenderContext<'_>| {
                let marker = ctx.stacking.marker();
                let mut phase_content = String::new();
                let phase_flow = flow_context.with_paint_phase(phase);
                let (handled, result) = {
                    let mut renderer = DirectChildRenderer {
                        content: &mut phase_content,
                        child_index,
                        flow: &phase_flow,
                        position: child_position,
                        abs_origins,
                        ctx,
                        handled: false,
                        result: None,
                    };
                    renderer.render(child);
                    (renderer.handled, renderer.result)
                };
                let descendants = ctx.stacking.take_since(marker);
                (handled, result, phase_content, descendants)
            };
            let (decoration_handled, _, decoration, decoration_descendants) =
                render_phase(ElementPaintPhase::Decoration, abs_origins, ctx);
            ctx.stacking.commit(
                stacking_scope,
                content,
                &mut stacking_plan,
                crate::layout::elements::StackingLevel::in_flow_decoration(),
                decoration,
                decoration_descendants,
            );
            let (contents_handled, result, contents, contents_descendants) =
                render_phase(ElementPaintPhase::Contents, abs_origins, ctx);
            ctx.stacking.commit(
                stacking_scope,
                content,
                &mut stacking_plan,
                crate::layout::elements::StackingLevel::in_flow_contents(),
                contents,
                contents_descendants,
            );
            (decoration_handled || contents_handled, result)
        } else {
            let marker = ctx.stacking.marker();
            let mut child_content = String::new();
            let (handled, result) = {
                let mut renderer = DirectChildRenderer {
                    content: &mut child_content,
                    child_index,
                    flow: &flow_context,
                    position: child_position,
                    abs_origins,
                    ctx,
                    handled: false,
                    result: None,
                };
                renderer.render(child);
                (renderer.handled, renderer.result)
            };
            let descendants = ctx.stacking.take_since(marker);
            ctx.stacking.commit(
                stacking_scope,
                content,
                &mut stacking_plan,
                child_paint_order(child),
                child_content,
                descendants,
            );
            (handled, result)
        };
        if let Some(position) = result {
            y = position.y;
            cursor_y = position.cursor_y;
            prev_margin_bottom = position.previous_margin_bottom;
        } else if !handled {
            let height = crate::layout::paginate::estimate_element_height(child);
            if let Some(top) = flow_top_by_index.get(&child_index).copied() {
                y = top - height;
            } else {
                cursor_y -= height;
                y = cursor_y;
            }
            // Unknown/other element: its full estimated height (including any
            // margins) was consumed; do not collapse the next sibling.
            prev_margin_bottom = 0.0;
        }
    }

    // Flush any remaining nested batch
    if !nested_batch.is_empty() {
        let marker = ctx.stacking.marker();
        let mut batch_content = String::new();
        let _ = render_rows(
            &mut batch_content,
            &nested_batch,
            x,
            NestedRowsFlow::pending(nested_batch_position),
            paint_phase.paints_contents(),
            abs_origins,
            ctx,
        );
        let descendants = ctx.stacking.take_since(marker);
        ctx.stacking.commit(
            stacking_scope,
            content,
            &mut stacking_plan,
            crate::layout::elements::StackingLevel::in_flow(),
            batch_content,
            descendants,
        );
    }

    if stacking_scope.is_local() {
        ctx.stacking.paint_plan(stacking_plan, content);
    }
}