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
use super::backgrounds::{PdfBackgroundPaintContext, PdfBackgroundResources};
use super::geometry::{BoxPaintGeometry, PdfRect};
use super::patterns::{
    LayerPaintArea, LayerTilePattern, RepeatModes, paint_page_tiling_pattern, paint_tiling_pattern,
};
use super::{SvgPageImageSink, begin_blend_mode};
use crate::render::background::{
    BackgroundPaintContext, RasterBackgroundRequest, SvgVisualOverflow, overflow_from_viewport_box,
    register_background_image, svg_visual_overflow, synthetic_raster_background,
    viewport_box_from_overflow,
};
use crate::render::svg_geometry::SvgViewportBox;
use crate::style::computed::{
    BackgroundClip, BackgroundOrigin, BackgroundPosition, BackgroundRepeat, BackgroundSize,
    BlendMode,
};
use crate::types::CornerRadii;
use crate::util::AxisRepeatPattern;

#[derive(Debug, Clone, Copy)]
pub(super) struct BlockBackground {
    pub(super) geometry: BoxPaintGeometry,
    pub(super) border_radii: CornerRadii,
    pub(super) size: BackgroundSize,
    pub(super) position: BackgroundPosition,
    pub(super) repeat: BackgroundRepeat,
    pub(super) origin: BackgroundOrigin,
    pub(super) clip: BackgroundClip,
    pub(super) blur_radius: f32,
    pub(super) blend_mode: BlendMode,
}

/// Map the complete visual cell from the SVG's top-down tile coordinates into
/// the PDF page's bottom-up coordinate system.
fn placed_tile_visual_box(
    tile: PdfRect,
    viewport: SvgViewportBox,
    overflow: SvgVisualOverflow,
) -> PdfRect {
    let visual = viewport_box_from_overflow(viewport, overflow);
    PdfRect::new(
        tile.left + visual.x,
        tile.top() - visual.y - visual.height,
        visual.width,
        visual.height,
    )
}

/// A no-repeat background whose complete visual cell misses the fragment clip
/// cannot contribute paint. Resolve this before allocating page resources so
/// continuation fragments do not retain invisible image or SVG objects.
fn single_tile_misses_clip(
    pattern: LayerTilePattern,
    tile: PdfRect,
    viewport: SvgViewportBox,
    clip: SvgViewportBox,
    overflow: SvgVisualOverflow,
) -> bool {
    if !pattern.is_single() {
        return false;
    }

    let tile = placed_tile_visual_box(tile, viewport, overflow);
    let clip = viewport_box_from_overflow(clip, overflow);
    let clip = PdfRect::new(clip.x, clip.y, clip.width, clip.height);
    tile.intersection(clip).is_none()
}

pub(super) fn render_block_svg_background(
    content: &mut String,
    tree: &crate::parser::svg::SvgTree,
    mut resources: PdfBackgroundResources<'_>,
    background: BlockBackground,
) {
    let geometry =
        background
            .geometry
            .background(background.origin, background.clip, background.border_radii);
    let clip = geometry.painting_box;
    let reference = geometry.positioning_area.intrinsic_image_box();
    let blended = background.blend_mode != BlendMode::Normal
        && resources.ext_gstates.as_deref_mut().is_some_and(|states| {
            content.push_str("q\n");
            begin_blend_mode(content, states, background.blend_mode);
            true
        });
    render_svg_background(
        content,
        tree,
        resources,
        PdfBackgroundPaintContext::local(BackgroundPaintContext::new(
            reference.into(),
            geometry.image_destination_box.into(),
            clip.radii,
            background.blur_radius,
            background.size,
            background.position,
            background.repeat,
        )),
    );
    if blended {
        content.push_str("Q\n");
    }
}

pub(super) fn render_svg_background(
    content: &mut String,
    tree: &crate::parser::svg::SvgTree,
    resources: PdfBackgroundResources<'_>,
    paint: PdfBackgroundPaintContext,
) {
    let PdfBackgroundResources {
        writer: pdf_writer,
        images: page_images,
        shadings,
        shading_counter,
        ext_gstates,
    } = resources;
    let PdfBackgroundPaintContext {
        background: paint,
        paint_space,
    } = paint;
    // SVG image resources frequently omit explicit width/height and only provide
    // a viewBox. Browsers still use that intrinsic aspect ratio for background
    // sizing, so fall back to the viewBox dimensions before giving up.
    let intrinsic_width = if tree.width > 0.0 {
        tree.width
    } else {
        tree.view_box
            .as_ref()
            .map_or(0.0, |view_box| view_box.width)
    };
    let intrinsic_height = if tree.height > 0.0 {
        tree.height
    } else {
        tree.view_box
            .as_ref()
            .map_or(0.0, |view_box| view_box.height)
    };
    if intrinsic_width <= 0.0 || intrinsic_height <= 0.0 {
        return;
    }

    let (vb_w, vb_h) = if let Some(ref vb) = tree.view_box {
        (vb.width, vb.height)
    } else {
        (intrinsic_width, intrinsic_height)
    };
    if vb_w <= 0.0 || vb_h <= 0.0 {
        return;
    }

    let resolve_axis = |value: f32, is_percent: bool, extent: f32| {
        if is_percent {
            extent * (value / 100.0)
        } else {
            value
        }
    };

    // Compute the rendered size of one SVG tile based on background-size.
    let (scaled_w, scaled_h) = match paint.size {
        BackgroundSize::Cover => {
            let s = (paint.reference_box.width / vb_w).max(paint.reference_box.height / vb_h);
            (vb_w * s, vb_h * s)
        }
        BackgroundSize::Contain => {
            let s = (paint.reference_box.width / vb_w).min(paint.reference_box.height / vb_h);
            (vb_w * s, vb_h * s)
        }
        BackgroundSize::Auto => {
            // SVG dimensions are in CSS pixels; convert to points (1px = 0.75pt)
            (intrinsic_width * 0.75, intrinsic_height * 0.75)
        }
        BackgroundSize::Explicit {
            width: explicit_width,
            height: explicit_height,
            width_is_percent,
            height_is_percent,
        } => {
            let scaled_w =
                resolve_axis(explicit_width, width_is_percent, paint.reference_box.width);
            let scaled_h = explicit_height
                .map(|value| resolve_axis(value, height_is_percent, paint.reference_box.height))
                .unwrap_or_else(|| scaled_w * vb_h / vb_w);
            (scaled_w, scaled_h)
        }
        BackgroundSize::ExplicitAuto {
            width,
            height,
            width_is_percent,
            height_is_percent,
        } => match (width, height) {
            (Some(w), Some(h)) => (
                resolve_axis(w, width_is_percent, paint.reference_box.width),
                resolve_axis(h, height_is_percent, paint.reference_box.height),
            ),
            (Some(w), None) => {
                let scaled_w = resolve_axis(w, width_is_percent, paint.reference_box.width);
                (scaled_w, scaled_w * vb_h / vb_w)
            }
            (None, Some(h)) => {
                let scaled_h = resolve_axis(h, height_is_percent, paint.reference_box.height);
                (scaled_h * vb_w / vb_h, scaled_h)
            }
            (None, None) => (intrinsic_width * 0.75, intrinsic_height * 0.75),
        },
    };

    if scaled_w <= 0.0 || scaled_h <= 0.0 {
        return;
    }

    // When `background-size` fixes BOTH dimensions explicitly, the image is
    // scaled to exactly that box, ignoring its intrinsic aspect ratio
    // (css-backgrounds-3 §3.9). `cover`/`contain` already derive an
    // aspect-correct target box, so the source's `preserveAspectRatio` (which
    // would re-fit it) must be neutralised; only `auto` keeps the ratio.
    let stretch_to_box = matches!(
        paint.size,
        BackgroundSize::Cover
            | BackgroundSize::Contain
            | BackgroundSize::Explicit {
                height: Some(_),
                ..
            }
    );
    let placement_par = if stretch_to_box {
        crate::parser::svg::SvgPreserveAspectRatio::None
    } else {
        tree.preserve_aspect_ratio
    };
    // Compute background-position offset (in the CSS coordinate system,
    // origin at top-left of the element box).
    let offset_x = if paint.position.x_is_percent {
        (paint.reference_box.width - scaled_w) * paint.position.x
    } else if paint.position.x < 0.0 {
        (paint.reference_box.width - scaled_w) + paint.position.x
    } else {
        paint.position.x
    };
    let offset_y = if paint.position.y_is_percent {
        (paint.reference_box.height - scaled_h) * paint.position.y
    } else if paint.position.y < 0.0 {
        (paint.reference_box.height - scaled_h) + paint.position.y
    } else {
        paint.position.y
    };

    let repeat = RepeatModes::from(paint.repeat);
    let Some(x_pattern) = AxisRepeatPattern::new_layout(
        repeat.horizontal,
        offset_x,
        scaled_w,
        paint.reference_box.width,
    ) else {
        return;
    };
    let Some(y_pattern) = AxisRepeatPattern::new_layout(
        repeat.vertical,
        offset_y,
        scaled_h,
        paint.reference_box.height,
    ) else {
        return;
    };
    let (scaled_w, scaled_h) = (x_pattern.tile_size(), y_pattern.tile_size());
    let tile_pattern = LayerTilePattern::new(
        LayerPaintArea::new(
            PdfRect::new(
                paint.reference_box.x,
                paint.reference_box.y,
                paint.reference_box.width,
                paint.reference_box.height,
            ),
            PdfRect::new(
                paint.clip_box.x,
                paint.clip_box.y,
                paint.clip_box.width,
                paint.clip_box.height,
            ),
        ),
        x_pattern,
        y_pattern,
    );
    let Some(first_tile) = tile_pattern.first_tile() else {
        return;
    };
    let placement = crate::render::svg_geometry::compute_svg_placement(
        tree,
        crate::render::svg_geometry::SvgPlacementRequest::from_rect(
            0.0,
            0.0,
            scaled_w,
            scaled_h,
            placement_par,
        ),
    );
    let Some(placement) = placement else {
        return;
    };
    let source_visual_overflow =
        svg_visual_overflow(tree).scale(placement.scale_x, placement.scale_y);
    if paint.blur_radius <= 0.0
        && single_tile_misses_clip(
            tile_pattern,
            first_tile,
            placement.viewport,
            paint.clip_box,
            source_visual_overflow,
        )
    {
        return;
    }
    let raster_background = synthetic_raster_background(tree).and_then(|(href, source_box)| {
        let image_box = SvgViewportBox::new(
            placement.translate_x + source_box.x * placement.scale_x,
            placement.translate_y + source_box.y * placement.scale_y,
            source_box.width * placement.scale_x,
            source_box.height * placement.scale_y,
        );
        let request = (paint.blur_radius > 0.0).then_some(RasterBackgroundRequest {
            canvas_box: paint.local_blur_canvas_box(),
            image_box,
            blur_radius: paint.blur_radius,
            filter_dpi: pdf_writer.opts.raster_quality.filter_dpi,
        });
        register_background_image(pdf_writer, page_images, href, image_box, request)
            .map(|registered| (image_box, registered))
    });
    let visual_overflow = raster_background.as_ref().map_or_else(
        || source_visual_overflow,
        |(image_box, registered)| {
            overflow_from_viewport_box(
                placement.viewport,
                registered.draw_box.unwrap_or(*image_box),
            )
        },
    );
    let tile_clip_box = viewport_box_from_overflow(placement.viewport, visual_overflow);

    let mut cell = String::from("q\n");
    cell.push_str(&tile_clip_box.clip_path());
    if let Some((image_box, registered_image)) = &raster_background {
        let draw_box = registered_image.draw_box.unwrap_or(*image_box);
        cell.push_str(&format!(
            "q\n{width} 0 0 -{height} {x} {y} cm\n/{name} Do\nQ\n",
            width = draw_box.width,
            height = draw_box.height,
            x = draw_box.x,
            y = draw_box.y + draw_box.height,
            name = registered_image.name,
        ));
    } else {
        cell.push_str(&format!(
            "{sx} 0 0 {sy} {tx} {ty} cm\n",
            sx = placement.scale_x,
            sy = placement.scale_y,
            tx = placement.translate_x,
            ty = placement.translate_y,
        ));
        let mut image_sink = SvgPageImageSink {
            pdf_writer,
            page_images,
        };
        let mut resources = crate::render::svg_to_pdf::SvgPdfResources {
            shadings,
            shading_counter,
            ext_gstates,
            image_sink: Some(&mut image_sink),
            raster_scale_x: placement.scale_x.abs(),
            raster_scale_y: placement.scale_y.abs(),
            // SVG used as a CSS background image: custom-font text in
            // background SVGs is out of scope here (no font context is threaded
            // this far), so fall back to standard fonts.
            custom_fonts: None,
            prepared_custom_fonts: None,
        };
        crate::render::svg_to_pdf::render_svg_tree_with_resources(tree, &mut cell, &mut resources);
    }
    cell.push_str("Q\n");

    // Clip to the element box.
    content.push_str("q\n");
    let expanded_clip_box = viewport_box_from_overflow(paint.clip_box, visual_overflow);
    content.push_str(
        &PdfRect::new(
            expanded_clip_box.x,
            expanded_clip_box.y,
            expanded_clip_box.width,
            expanded_clip_box.height,
        )
        .rounded(paint.border_radii)
        .path_or_rect(),
    );
    content.push_str("W n\n");

    if tile_pattern.is_single() {
        content.push_str("q\n");
        content.push_str(&format!(
            "1 0 0 -1 {} {} cm\n",
            first_tile.left,
            first_tile.top()
        ));
        content.push_str(&cell);
        content.push_str("Q\n");
    } else {
        if let Some((image_box, registered)) = &raster_background
            && registered.draw_box.is_none()
            && image_box.x == 0.0
            && image_box.y == 0.0
            && image_box.width == scaled_w
            && image_box.height == scaled_h
        {
            let source = registered.pixel_dimensions;
            if let Some(paint_space) = paint_space {
                if let Some(pattern) = tile_pattern.pdf_page_raster_pattern(source, paint_space) {
                    let stream = format!(
                        "q\n{width} 0 0 -{height} {left} {top} cm\n/{name} Do\nQ\n",
                        width = source.width,
                        height = source.height,
                        left = pattern.bbox.left,
                        top = pattern.bbox.top(),
                        name = registered.name,
                    );
                    if let Some(name) = pdf_writer.add_page_tiling_pattern(stream, pattern) {
                        paint_page_tiling_pattern(content, &name, tile_pattern.paint_box());
                        content.push_str("Q\n");
                        return;
                    }
                }
            } else if let Some(pattern) = tile_pattern.pdf_raster_pattern(source)
                && let stream = format!(
                    "q\n0 0 {width} {height} re W n\n{width} 0 0 {height} 0 0 cm\n/{name} Do\nQ\n",
                    width = source.width,
                    height = source.height,
                    name = registered.name,
                )
                && let Some(form) = pdf_writer.add_tiling_pattern(stream, pattern)
            {
                paint_tiling_pattern(content, &form, tile_pattern.paint_box());
                page_images.push(form);
                content.push_str("Q\n");
                return;
            }
        }
        let mut stream = format!("q\n1 0 0 -1 0 {scaled_h} cm\n");
        stream.push_str(&cell);
        stream.push_str("Q\n");
        let bbox = PdfRect::new(
            tile_clip_box.x,
            scaled_h - tile_clip_box.y - tile_clip_box.height,
            tile_clip_box.width,
            tile_clip_box.height,
        );
        if let Some(form) = tile_pattern
            .pdf_pattern(bbox)
            .and_then(|spec| pdf_writer.add_tiling_pattern(stream, spec))
        {
            paint_tiling_pattern(content, &form, tile_pattern.paint_box());
            page_images.push(form);
        }
    }
    content.push_str("Q\n");
}