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
use super::*;
use crate::layout::elements::TextBlock;

pub(super) fn render_text_block_lines(
    content: &mut String,
    element: &TextBlock,
    box_geometry: BoxPaintGeometry,
    frame: PageElementFrame<'_>,
    opacity_active: bool,
    text_space: PdfContentSpace,
    ctx: &mut PageRenderContext<'_>,
) {
    let geometry = box_geometry.layout();
    let lines = &element.lines;
    let padding = &element.box_model.padding;
    let border = &element.box_model.border;
    let writing_mode = &element.text.writing_mode;
    let text_align = &element.text.alignment;
    let text_indent = &element.text.indent;
    let background_blur_radius = &element.paint.background.layers.blur_radius;
    let background_color = &element.paint.background.color;
    let background_gradient = &element.paint.background.layers.gradient;
    let background_radial_gradient = &element.paint.background.layers.radial_gradient;
    let background_conic_gradient = &element.paint.background.layers.conic_gradient;
    let background_size = &element.paint.background.layers.size;
    let background_position = &element.paint.background.layers.position;
    let background_repeat = &element.paint.background.layers.repeat;
    let background_origin = &element.paint.background.layers.origin;
    let background_clip = &element.paint.background.layers.clip;
    let background_blend_mode = &element.paint.background.blend_mode;
    let opacity = &element.paint.group.effects.opacity;
    let block_x = geometry.border_box.left;
    let block_y = geometry.border_box.top();
    let render_width = geometry.border_box.width;
    let elem_idx = frame.element_index;
    let page_size = frame.page_size;
    let needs_opacity = opacity_active;
    let background_geometry = box_geometry.background(
        *background_origin,
        *background_clip,
        element.paint.border_radii,
    );
    let tb_reference = background_geometry.positioning_area.generated_image_box();
    let tb_text_clip_background = *background_clip == BackgroundClip::Text;
    let tb_layer_box =
        background_layer_box(*background_size, *background_position, *background_repeat);
    let tb_bg_blend_mode = background_blend_mode.background_layer(0);
    let tb_bg_blended = tb_bg_blend_mode != crate::style::computed::BlendMode::Normal;
    let gradient_area = |attachment| {
        if attachment == Some(BackgroundAttachment::Fixed) {
            LayerPaintArea::new(
                PdfRect::new(0.0, 0.0, page_size.width, page_size.height),
                background_geometry.painting_box.rect,
            )
        } else {
            LayerPaintArea::new(tb_reference, background_geometry.image_destination_box)
        }
    };

    // Text content is inset from the border-box top by the top
    // border width and the top padding.
    let content_top = block_y - border.top.width - padding.top;
    let mut baseline_cursor =
        TextBaselineCursor::new(content_top, ctx.text.pdf_writer.page_content_transform);

    // Horizontal insets: `block_x` / `render_width` are the
    // border-box left / width, so the content area starts after
    // the left border + left padding and is narrowed by the
    // horizontal borders + paddings.
    let border_left = border.left.width;
    let border_right = border.right.width;
    // Content-box left edge and width (content + padding ⇒ here we
    // keep padding in `content_x`/`content_width` because the text
    // branches add `padding.left`/`padding.right` themselves; this
    // pair is the PADDING box).
    let padding_box_x = block_x + border_left;
    let padding_box_w = (render_width - border_left - border_right).max(0.0);

    // CSS `writing-mode: vertical-rl` (css-writing-modes-4 §3.1).
    // The box geometry stays physical/axis-aligned (already laid
    // out above); only the inline text is set vertically. With the
    // default `text-orientation: mixed`, Latin runs are rotated 90°
    // clockwise (set sideways) and flow top-to-bottom in the first
    // (right-most) column.
    //
    // We lay the run out horizontally as usual, then apply a single
    // `cm` that rotates 90° clockwise (PDF `[0 -1 1 0]`, which maps
    // local +x→PDF −y "down" and local +y→PDF +x "right") and
    // translates so the horizontal text's content-top-left anchors
    // at the content box and the column hugs the right edge. The
    // wrapper is scoped to the glyph-drawing loop only, so the
    // background/border/outline (painted earlier) stay upright.
    let line_metadata = lines
        .first()
        .map_or(Default::default(), |line| line.metadata);
    let vertical_lr = line_metadata.writing_mode.is_vertical_lr();
    let upright_vertical = line_metadata.text_orientation_upright;
    let vertical = writing_mode.is_vertical() && !upright_vertical;
    let content_right = padding_box_x + padding_box_w - padding.right;
    let content_left = padding_box_x + padding.left;
    let vertical_transform = if vertical {
        // Content-box edges in PDF (y-up) coordinates. `text_y`
        // currently sits at the content-area top (block_y − top
        // border − top padding) before any line advance.
        // matrix maps (gx, gy) → (gy + e, −gx + f):
        //   glyph top (gy = content_top) → X = content_right (column
        //     hugs the right edge), and
        //   text start (gx = content_left) → Y = content_top (text
        //     begins at the top of the column, flowing downward).
        let column_x = if vertical_lr {
            content_left + lines.first().map_or(0.0, |line| line.height)
        } else {
            content_right
        };
        let e = column_x - content_top;
        let f = content_top + content_left;
        content.push_str("q\n");
        content.push_str(&format!("0 -1 1 0 {e} {f} cm\n"));
        Some((e, f))
    } else {
        None
    };
    let line_count = lines.len();
    for (line_idx, line) in lines.iter().enumerate() {
        let metrics = if upright_vertical {
            upright_vertical_line_metrics(line, ctx.text.custom_fonts)
        } else {
            line_box_metrics(line, ctx.text.custom_fonts)
        };
        let text_y = if vertical {
            baseline_cursor.next_raw(metrics)
        } else {
            baseline_cursor.next_horizontal(metrics)
        };
        let line_annotation_top = text_y + metrics.ascender + metrics.half_leading;
        let line_annotation_bottom = text_y - metrics.descender - metrics.half_leading;

        let line_text = line_text_content(line);
        let has_inline_box = line.runs.iter().any(|r| r.inline_box.is_some());
        if line_text.is_empty() && !has_inline_box {
            continue;
        }

        let line_width = if upright_vertical {
            line.runs
                .iter()
                .map(|run| {
                    text_combine_advance(run, ctx.text.custom_fonts).unwrap_or_else(|| {
                        estimate_run_width_with_fonts(run, ctx.text.custom_fonts)
                    })
                })
                .sum()
        } else {
            estimate_line_width_with_fonts(line, ctx.text.custom_fonts)
        };
        let is_last_line = line_idx == line_count - 1;

        // Calculate word spacing for justified text
        let justify_ws = if *text_align == TextAlign::Justify && !is_last_line {
            let first_line_indent = if line_idx == 0 { *text_indent } else { 0.0 };
            let content_width = padding_box_w - padding.horizontal() - first_line_indent;
            let remaining = content_width - line_width;
            let space_count = line_text.matches(' ').count();
            if space_count > 0 && remaining > 0.0 {
                remaining / space_count as f32
            } else {
                0.0
            }
        } else {
            0.0
        };
        // CSS `text-indent` shifts the start of the FIRST line's
        // inline content. For start-edge alignment (left/justify)
        // it offsets the text origin; for center/right it consumes
        // available width on the start side, recentring/reflowing
        // the first line within the remaining space.
        let first_line_indent = if line_idx == 0 { *text_indent } else { 0.0 };
        // Drop-cap float exclusion: the line is shifted right so
        // its inline content wraps beside the floated
        // `::first-letter` (css-pseudo-4 §2.2 + css2 §9.5).
        let line_inset = line.x_offset;
        let text_x = match text_align {
            TextAlign::Left | TextAlign::Justify => {
                if upright_vertical {
                    let upright_box_width = line
                        .runs
                        .iter()
                        .find(|r| r.inline_box.is_none())
                        .map(run_line_height_for_vertical_align)
                        .unwrap_or(line.height);
                    content_right - (upright_box_width + line_width) / 2.0 + line_inset
                } else {
                    padding_box_x + padding.left + first_line_indent + line_inset
                }
            }
            TextAlign::Center => {
                let first_pad = line.runs.first().map_or(0.0, |r| r.padding.left);
                padding_box_x
                    + first_line_indent
                    + (padding_box_w - first_line_indent - line_width) / 2.0
                    + first_pad
            }
            TextAlign::Right => {
                // Account for inline padding: text_x is where the
                // text characters start, but line_width includes the
                // full visual width (with left+right padding of inline
                // spans).  Offset by the first run's left padding so
                // the visual right edge aligns with the right boundary.
                let first_pad = line.runs.first().map_or(0.0, |r| r.padding.left);
                padding_box_x + padding_box_w - padding.right - line_width + first_pad
            }
        };
        // Merge consecutive runs with the same style so
        // spaces between words stay in a single PDF text
        // string, preventing viewers from dropping them.
        let merged = crate::text::coalesce_text_runs(&line.runs);
        let mut text_clip_line_painted = false;
        if tb_text_clip_background {
            if let Some(gradient) = background_gradient {
                let gradient = linear_with_background_layer(gradient, tb_layer_box);
                if gradient.layer_box.attachment != Some(BackgroundAttachment::Local) {
                    content.push_str("q\n");
                    if push_line_text_clip(
                        content,
                        &merged,
                        text_x,
                        text_y,
                        ctx.text.custom_fonts,
                        ctx.text.prepared_custom_fonts,
                        justify_ws,
                        line_text_top(line, ctx.text.custom_fonts),
                    ) {
                        if tb_bg_blended {
                            content.push_str("q\n");
                            begin_blend_mode(content, ctx.page_ext_gstates, tb_bg_blend_mode);
                        }
                        render_linear_gradient(
                            content,
                            &gradient,
                            GradientBackdrop::isolated_linear_layer(
                                *background_color,
                                background_radial_gradient.is_some()
                                    || background_conic_gradient.is_some(),
                                tb_bg_blend_mode,
                            ),
                            gradient_area(gradient.layer_box.attachment),
                            ctx.shadings,
                            ctx.shading_counter,
                            ctx.text.pdf_writer,
                            ctx.text.page_images,
                        );
                        if tb_bg_blended {
                            content.push_str("Q\n");
                        }
                        text_clip_line_painted = true;
                    }
                    content.push_str("Q\n");
                }
            } else if let Some(gradient) = background_radial_gradient {
                let gradient = radial_with_background_layer(gradient, tb_layer_box);
                if gradient.layer_box.attachment != Some(BackgroundAttachment::Local) {
                    content.push_str("q\n");
                    if push_line_text_clip(
                        content,
                        &merged,
                        text_x,
                        text_y,
                        ctx.text.custom_fonts,
                        ctx.text.prepared_custom_fonts,
                        justify_ws,
                        line_text_top(line, ctx.text.custom_fonts),
                    ) {
                        if tb_bg_blended {
                            content.push_str("q\n");
                            begin_blend_mode(content, ctx.page_ext_gstates, tb_bg_blend_mode);
                        }
                        render_radial_gradient(
                            content,
                            &gradient,
                            gradient_area(gradient.layer_box.attachment),
                            ctx.shadings,
                            ctx.shading_counter,
                            ctx.text.pdf_writer,
                            ctx.text.page_images,
                        );
                        if tb_bg_blended {
                            content.push_str("Q\n");
                        }
                        text_clip_line_painted = true;
                    }
                    content.push_str("Q\n");
                }
            } else if let Some(gradient) = background_conic_gradient {
                let gradient = conic_with_background_layer(gradient, tb_layer_box);
                if gradient.layer_box.attachment != Some(BackgroundAttachment::Local) {
                    content.push_str("q\n");
                    if push_line_text_clip(
                        content,
                        &merged,
                        text_x,
                        text_y,
                        ctx.text.custom_fonts,
                        ctx.text.prepared_custom_fonts,
                        justify_ws,
                        line_text_top(line, ctx.text.custom_fonts),
                    ) {
                        if tb_bg_blended {
                            content.push_str("q\n");
                            begin_blend_mode(content, ctx.page_ext_gstates, tb_bg_blend_mode);
                        }
                        render_conic_gradient(
                            content,
                            &gradient,
                            gradient_area(gradient.layer_box.attachment),
                            ctx.text.pdf_writer,
                            ctx.text.page_images,
                        );
                        if tb_bg_blended {
                            content.push_str("Q\n");
                        }
                        text_clip_line_painted = true;
                    }
                    content.push_str("Q\n");
                }
            }
        }

        // Phase 1: Draw backgrounds, decorations, and link
        // ctx.text.annotations at estimated positions (visual-only).
        let line_top_y = text_y + metrics.ascender + metrics.half_leading;
        let line_bottom_y = text_y - metrics.descender - metrics.half_leading;
        // Parent text content-area edges for `text-top`/`text-bottom`
        // (parent glyph ascent/descent, no half-leading). Fall back to
        // the line-box edges when the line carries no parent text.
        let (text_ascent, text_descent) = line_text_content_extents(line, ctx.text.custom_fonts);
        let line_text_top_y = if text_ascent > 0.0 {
            text_y + text_ascent
        } else {
            line_top_y
        };
        let line_text_bottom_y = if text_descent > 0.0 {
            text_y - text_descent
        } else {
            line_bottom_y
        };
        let mut bg_x = text_x;
        // Relatively-positioned inline boxes paint in the positioned
        // layer — above in-flow siblings on the line, in source order
        // (CSS 2.1 §9.9.1 painting order). Defer them so a later
        // in-flow inline-block can't paint over an earlier offset one.
        let mut deferred_inline: Vec<(&crate::layout::engine::InlineBox, f32, f32, f32)> =
            Vec::new();
        for (run_idx, run) in merged.iter().enumerate() {
            // Atomic inline box (display: inline-block): paint the
            // box and its inner content, then advance the cursor.
            if let Some(inline) = run.inline_box.as_deref() {
                let ibx = bg_x + inline.margin_left;
                let run_line_height = run_line_height_for_vertical_align(run);
                if inline.rel_offset_x != 0.0 || inline.rel_offset_y != 0.0 {
                    deferred_inline.push((inline, ibx, run.font_size, run_line_height));
                } else {
                    render_inline_box(
                        content,
                        inline,
                        ibx,
                        text_y,
                        page_size.height,
                        line_top_y,
                        line_bottom_y,
                        line_text_top_y,
                        line_text_bottom_y,
                        run.font_size,
                        run_line_height,
                        line_primary_x_height_ratio(&merged, ctx.text.custom_fonts),
                        ctx.text.custom_fonts,
                        ctx.text.prepared_custom_fonts,
                        ctx.page_ext_gstates,
                        ctx.bg_alpha_counter,
                        ctx.shadings,
                        ctx.shading_counter,
                        ctx.text.pdf_writer,
                        ctx.text.page_images,
                    );
                }
                bg_x += run.atomic_inline_advance().unwrap_or_default();
                continue;
            }
            if run.text.is_empty() {
                continue;
            }
            let run_width = if upright_vertical {
                text_combine_advance(run, ctx.text.custom_fonts)
                    .unwrap_or_else(|| estimate_run_width_with_fonts(run, ctx.text.custom_fonts))
            } else {
                estimate_run_width_with_fonts(run, ctx.text.custom_fonts)
            };
            // Draw background rectangle for inline spans
            if let Some(background) = run.background_color {
                let (br, bg, bb, ba) = background.to_f32_rgba();
                let needs_inline_bg_alpha = ba < 1.0;
                if needs_inline_bg_alpha {
                    let effective_alpha = ba * *opacity;
                    let gs_name = format!("GSba{}", ctx.bg_alpha_counter);
                    *ctx.bg_alpha_counter += 1;
                    ctx.page_ext_gstates
                        .push((gs_name.clone(), effective_alpha));
                    content.push_str(&format!("/{gs_name} gs\n"));
                }
                let rect_x = bg_x - run.padding.left;
                let rect_w = run_width + run.padding.horizontal();
                let (rect_y, rect_h) =
                    inline_background_y_and_height(run, text_y, run.padding, ctx.text.custom_fonts);
                content.push_str(&format!("{br} {bg} {bb} rg\n"));
                content.push_str(
                    &PdfRect::new(rect_x, rect_y, rect_w, rect_h)
                        .rounded(run.border_radii)
                        .path_or_rect(),
                );
                content.push_str("f\n");
                if needs_inline_bg_alpha {
                    if needs_opacity {
                        let gs_name = format!("GS{elem_idx}");
                        content.push_str(&format!("/{gs_name} gs\n"));
                    } else {
                        content.push_str("/GSDefault gs\n");
                    }
                }
            }

            if vertical {
                let previous = merged[..run_idx]
                    .iter()
                    .rev()
                    .find(|previous| previous.inline_box.is_none() && !previous.text.is_empty());
                let decoration = HorizontalRunDecorations::new(
                    run,
                    bg_x,
                    run_width,
                    text_y,
                    ctx.text.custom_fonts,
                )
                .continuing_after(previous);
                decoration.paint_shadows(content);
                decoration.paint_below_text(content);
                decoration.paint_above_text(content);
                if decoration_is_emphasis(run) {
                    render_text_emphasis_marks(
                        content,
                        run,
                        TextEmphasisPlacement {
                            origin: PdfPoint::new(bg_x, text_y),
                            color: run.metadata.emphasis.color,
                        },
                        ctx.text.custom_fonts,
                        ctx.text.prepared_custom_fonts,
                        ctx.text.pdf_writer,
                        ctx.text.page_images,
                    );
                }
            }

            // Track link annotation
            if let Some(annotation) = text_run_link_annotation(
                run,
                PdfRect::new(
                    bg_x,
                    line_annotation_bottom,
                    run_width,
                    line_annotation_top - line_annotation_bottom,
                ),
            ) {
                ctx.text.annotations.push(annotation);
            }

            bg_x += run_width;
        }

        // Paint deferred relatively-positioned inline boxes on top
        // of the in-flow line content, preserving source order.
        for (inline, ibx, fs, run_line_height) in deferred_inline {
            render_inline_box(
                content,
                inline,
                ibx,
                text_y,
                page_size.height,
                line_top_y,
                line_bottom_y,
                line_text_top_y,
                line_text_bottom_y,
                fs,
                run_line_height,
                line_primary_x_height_ratio(&merged, ctx.text.custom_fonts),
                ctx.text.custom_fonts,
                ctx.text.prepared_custom_fonts,
                ctx.page_ext_gstates,
                ctx.bg_alpha_counter,
                ctx.shadings,
                ctx.shading_counter,
                ctx.text.pdf_writer,
                ctx.text.page_images,
            );
        }

        // Phase 2: Render all text in a single BT/ET block
        // so the PDF viewer advances the cursor naturally.
        let mut blurred_line = false;
        if *background_blur_radius > 0.0 {
            let mut lx = text_x;
            blurred_line = true;
            for run in &merged {
                if run.inline_box.is_some() {
                    lx += run.atomic_inline_advance().unwrap_or_default();
                    continue;
                }
                if run.text.is_empty() {
                    continue;
                }
                if !render_text_shadow_blur(
                    content,
                    run,
                    lx,
                    text_y,
                    *background_blur_radius * 2.0,
                    run.color.to_f32_rgba(),
                    ctx.text.custom_fonts,
                    ctx.text.pdf_writer,
                    ctx.text.page_images,
                ) {
                    blurred_line = false;
                    break;
                }
                lx += if upright_vertical {
                    text_combine_advance(run, ctx.text.custom_fonts).unwrap_or_else(|| {
                        estimate_run_width_with_fonts(run, ctx.text.custom_fonts)
                    })
                } else {
                    estimate_run_width_with_fonts(run, ctx.text.custom_fonts)
                };
            }
        }
        if !blurred_line && !text_clip_line_painted {
            if upright_vertical {
                render_upright_vertical_line_text(
                    content,
                    &merged,
                    UprightLinePosition::new(
                        PdfPoint::new(text_x, text_y),
                        VerticalLineCrossAxis::from_content_edges(
                            content_left,
                            content_right,
                            vertical_lr,
                        ),
                    ),
                    ctx.text.custom_fonts,
                    ctx.text.prepared_custom_fonts,
                    justify_ws,
                    line_text_top(line, ctx.text.custom_fonts),
                    ctx.text.pdf_writer,
                    ctx.text.page_images,
                );
            } else if let Some((vertical_e, vertical_f)) = vertical_transform {
                render_vertical_mixed_line_text(
                    content,
                    &merged,
                    text_x,
                    text_y,
                    ctx.text.custom_fonts,
                    ctx.text.prepared_custom_fonts,
                    justify_ws,
                    line_text_top(line, ctx.text.custom_fonts),
                    vertical_e,
                    vertical_f,
                    ctx.text.pdf_writer,
                    ctx.text.page_images,
                );
            } else {
                paint_horizontal_line_text(
                    content,
                    &merged,
                    HorizontalLinePaint {
                        origin: PdfPoint::new(text_x, text_y),
                        line_ascender: line_text_top(line, ctx.text.custom_fonts),
                        justification_word_spacing: justify_ws,
                        text_space,
                    },
                    ctx.text.custom_fonts,
                    ctx.text.prepared_custom_fonts,
                    ctx.text.pdf_writer,
                    ctx.text.page_images,
                );
            }
        }
    }

    // Close the `vertical-rl` rotation wrapper opened before the
    // line loop (scoped to glyph drawing only).
    if vertical {
        content.push_str("Q\n");
    }
}