ferri-pdf 0.1.3

HTML and CSS to PDF renderer for print, in pure Rust
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
//! CSS Shapes 1 §3 `<basic-shape>` computed values and §6.1 `shape-outside`, with the outlines
//! basic shapes trace in their reference box.
use crate::paint::{
    BackgroundImage, BoxArea, BoxPaint, Contour, LengthPercentage, POINTS_PER_PIXEL, Position,
    RadialExtent, Radii, Rect, Shape, rounded,
};
use serde::Serialize;
use std::convert::Infallible;

/// `<shape-box>`: a `<visual-box>` or `margin-box`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub(crate) enum ShapeBox {
    Margin,
    Visual(BoxArea),
}

/// `<geometry-box>` (CSS Masking 1 §6.1): a `<shape-box>`, or a box of SVG content.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub(crate) enum GeometryBox {
    Shape(ShapeBox),
    Fill,
    Stroke,
    View,
}

impl GeometryBox {
    /// The box of an element with a CSS layout box: the content box for `fill-box` and the
    /// border box for `stroke-box` and `view-box`.
    pub(crate) fn layout(self) -> ShapeBox {
        match self {
            Self::Shape(reference) => reference,
            Self::Fill => ShapeBox::Visual(BoxArea::ContentBox),
            Self::Stroke | Self::View => ShapeBox::Visual(BoxArea::BorderBox),
        }
    }
}

/// A `circle()` radius: an extent keyword, or a length whose percentages refer to the
/// reference box's direction-agnostic size (CSS Images 4 §3.2.2).
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) enum CircleRadius {
    Extent(RadialExtent),
    Length(LengthPercentage),
}

/// One `ellipse()` radius along its axis.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) enum AxisRadius {
    ClosestSide,
    FarthestSide,
    Length(LengthPercentage),
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) enum EllipseRadii {
    /// One extent for both radii, as for an elliptical radial gradient.
    Extent(RadialExtent),
    /// Horizontal then vertical.
    Axes([AxisRadius; 2]),
}

/// Corner radii as `[horizontal, vertical]`, top-left first.
pub(crate) type CornerRadii = [[LengthPercentage; 2]; 4];

#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) enum BasicShape {
    /// Insets from the top, right, bottom and left edges; `xywh()` computes to it.
    Inset {
        insets: [LengthPercentage; 4],
        radii: CornerRadii,
    },
    /// `rect()`: top, right, bottom and left edges from the top-left corner, `auto` resolved.
    Rect {
        edges: [LengthPercentage; 4],
        radii: CornerRadii,
    },
    Circle {
        radius: CircleRadius,
        center: Position,
    },
    Ellipse {
        radii: EllipseRadii,
        center: Position,
    },
    Polygon {
        even_odd: bool,
        points: Vec<[LengthPercentage; 2]>,
    },
    /// SVG path data in CSS pixels from the reference box origin, one contour per subpath.
    Path {
        even_odd: bool,
        contours: Vec<Contour>,
    },
}

impl ShapeBox {
    /// The assembled box contour, including its corner shape and signed margin offsets.
    pub(crate) fn contours<I>(self, paint: &BoxPaint<I>, margins: [f32; 4]) -> Vec<Contour> {
        if *paint.decoration.corner_shapes == [crate::paint::CornerShape::ROUND; 4] {
            let (rect, radii) = self.resolve(paint, margins);
            return vec![rounded::rounded_rect(rect, radii)];
        }
        let clip = match self {
            Self::Margin => paint.box_clip(margins.map(|margin| -margin), [true; 2]),
            Self::Visual(BoxArea::BorderBox) => paint.box_clip([0.0; 4], [true; 2]),
            Self::Visual(area) => paint.inner_clip(
                if area == BoxArea::ContentBox {
                    paint.padding
                } else {
                    [0.0; 4]
                },
                [true; 2],
            ),
        };
        clip.contours().into_owned()
    }

    /// The box's rectangle and corner radii in `paint`, a sliced fragment's assembled box,
    /// with `margins` around the border box for `margin-box` (CSS Shapes 1 §5).
    pub(crate) fn resolve<I>(self, paint: &BoxPaint<I>, margins: [f32; 4]) -> (Rect, Radii) {
        match self {
            Self::Margin => (
                crate::paint::outset(paint.decoration_rect(), margins),
                crate::paint::outset_radii(paint.radii(), margins),
            ),
            Self::Visual(BoxArea::BorderBox) => (paint.decoration_rect(), paint.radii()),
            Self::Visual(area) => {
                let padding = if area == BoxArea::ContentBox {
                    paint.padding
                } else {
                    [0.0; 4]
                };
                let clip = paint.inner_clip(padding, [true; 2]);
                (clip.reference_box.unwrap_or(clip.rect), clip.radii)
            }
        }
    }
}

/// CSS Masking 1 §6.1 `clip-path`: a basic shape in its reference box, `border-box` unless
/// specified, or the reference box alone with its corner curvature.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) struct ClipPath {
    pub shape: Option<BasicShape>,
    pub reference: GeometryBox,
}

impl ClipPath {
    /// The clipping region of a box fragment, whose used margins `paint` carries.
    pub(crate) fn outline<I>(&self, paint: &BoxPaint<I>) -> Shape {
        let reference = self.reference.layout();
        match &self.shape {
            Some(shape) => shape.outline(reference.resolve(paint, paint.margins).0),
            None => Shape::Path {
                contours: reference.contours(paint, paint.margins),
                even_odd: false,
                clip: None,
            },
        }
    }
}

/// The `shape-outside` computed value; `I` is a loaded image.
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[serde(bound(serialize = ""))]
pub(crate) enum ShapeOutside<I = Infallible> {
    #[default]
    None,
    Box(ShapeBox),
    /// A basic shape in its reference box, `margin-box` unless specified.
    Shape {
        shape: BasicShape,
        reference: ShapeBox,
    },
    Image(BackgroundImage<I>),
}

impl ShapeOutside<Infallible> {
    /// An image that cannot be loaded and has no fallback color leaves `none` (§6.1).
    pub(crate) fn load_image<I, E>(
        &self,
        load: &mut impl FnMut(&crate::paint::ImageReference<'_>) -> Result<Option<I>, E>,
    ) -> Result<ShapeOutside<I>, E> {
        Ok(match self {
            Self::None => ShapeOutside::None,
            Self::Box(reference) => ShapeOutside::Box(*reference),
            Self::Shape { shape, reference } => ShapeOutside::Shape {
                shape: shape.clone(),
                reference: *reference,
            },
            Self::Image(image) => {
                let mut loaded = image.with_image_type();
                loaded.load(load)?;
                if matches!(image, BackgroundImage::Url { .. })
                    && matches!(loaded, BackgroundImage::Invalid)
                {
                    ShapeOutside::None
                } else {
                    ShapeOutside::Image(loaded)
                }
            }
        })
    }
}

/// The length of a box's diagonal divided by √2 (CSS Shapes 1 §1.3).
fn direction_agnostic([width, height]: [f32; 2]) -> f32 {
    width.hypot(height) / std::f32::consts::SQRT_2
}

/// CSS Backgrounds 3 §4.5: radii resolved against `basis`, scaled down together until
/// adjacent radii fit along every side of `size`.
fn used_radii(radii: &CornerRadii, basis: [f32; 2], size: [f32; 2]) -> Radii {
    let resolved = std::array::from_fn(|corner| {
        let [horizontal, vertical] = &radii[corner];
        let used = [horizontal.resolve(basis[0]), vertical.resolve(basis[1])];
        if used[0] > 0.0 && used[1] > 0.0 {
            used
        } else {
            [0.0; 2]
        }
    });
    rounded::constrain(resolved, size)
}

/// A pair of opposite insets reduced proportionally when they exceed `extent` (§3.1).
fn fitted([start, end]: [f32; 2], extent: f32) -> [f32; 2] {
    let sum = start + end;
    if sum > extent && sum > 0.0 {
        let scale = extent / sum;
        [start * scale, end * scale]
    } else {
        [start, end]
    }
}

fn ellipse(center: [f32; 2], [rx, ry]: [f32; 2]) -> Contour {
    rounded::rounded_rect(
        Rect {
            x: center[0] - rx,
            y: center[1] - ry,
            width: 2.0 * rx,
            height: 2.0 * ry,
        },
        [[rx, ry]; 4],
    )
}

impl AxisRadius {
    fn resolve(&self, center: f32, extent: f32) -> f32 {
        let sides = [center.abs(), (extent - center).abs()];
        match self {
            Self::ClosestSide => sides[0].min(sides[1]),
            Self::FarthestSide => sides[0].max(sides[1]),
            Self::Length(length) => length.resolve(extent),
        }
    }
}

impl BasicShape {
    /// The rectangle an `inset()` or `rect()` shape bounds in `reference`, before its rounding;
    /// `None` for other shapes.
    pub(crate) fn rectangle(&self, reference: Rect) -> Option<Rect> {
        let size = [reference.width, reference.height];
        let [left, top, right, bottom] = match self {
            Self::Inset { insets, .. } => {
                let [top, bottom] = fitted(
                    [insets[0].resolve(size[1]), insets[2].resolve(size[1])],
                    size[1],
                );
                let [left, right] = fitted(
                    [insets[3].resolve(size[0]), insets[1].resolve(size[0])],
                    size[0],
                );
                [
                    left,
                    top,
                    (size[0] - right).max(left),
                    (size[1] - bottom).max(top),
                ]
            }
            Self::Rect { edges, .. } => {
                let top = edges[0].resolve(size[1]);
                let left = edges[3].resolve(size[0]);
                [
                    left,
                    top,
                    edges[1].resolve(size[0]).max(left),
                    edges[2].resolve(size[1]).max(top),
                ]
            }
            _ => return None,
        };
        Some(Rect {
            x: reference.x + left,
            y: reference.y + top,
            width: right - left,
            height: bottom - top,
        })
    }
    /// The closed outline the shape traces in `reference` and whether it fills even-odd.
    pub(crate) fn outline(&self, reference: Rect) -> Shape {
        let (mut contours, even_odd) = self.path(reference);
        for contour in &mut contours {
            contour.closed = true;
        }
        Shape::Path {
            contours,
            even_odd,
            clip: None,
        }
    }
    /// Equivalent path, retaining explicit SVG subpath closure.
    pub(crate) fn path(&self, reference: Rect) -> (Vec<Contour>, bool) {
        let size = [reference.width, reference.height];
        let origin = [reference.x, reference.y];
        let (contours, even_odd) = match self {
            Self::Inset { radii, .. } | Self::Rect { radii, .. } => {
                let rect = self
                    .rectangle(reference)
                    .expect("inset() and rect() are rectangles");
                (vec![rounded_inset(rect, radii, size)], false)
            }
            Self::Circle { radius, center } => {
                let center = [center[0].resolve(size[0]), center[1].resolve(size[1])];
                let radius = match radius {
                    CircleRadius::Extent(extent) => extent.circle(center, size),
                    CircleRadius::Length(length) => length.resolve(direction_agnostic(size)),
                };
                (
                    vec![ellipse(
                        [origin[0] + center[0], origin[1] + center[1]],
                        [radius; 2],
                    )],
                    false,
                )
            }
            Self::Ellipse { radii, center } => {
                let center = [center[0].resolve(size[0]), center[1].resolve(size[1])];
                let radii = match radii {
                    EllipseRadii::Extent(extent) => extent.ellipse(center, size),
                    EllipseRadii::Axes([x, y]) => {
                        [x.resolve(center[0], size[0]), y.resolve(center[1], size[1])]
                    }
                };
                (
                    vec![ellipse(
                        [origin[0] + center[0], origin[1] + center[1]],
                        radii,
                    )],
                    false,
                )
            }
            Self::Polygon { even_odd, points } => {
                let points = points
                    .iter()
                    .map(|[x, y]| {
                        [
                            origin[0] + x.resolve(size[0]),
                            origin[1] + y.resolve(size[1]),
                        ]
                    })
                    .collect::<Vec<_>>();
                (vec![Contour::polygon(&points)], *even_odd)
            }
            Self::Path { even_odd, contours } => {
                let place = |[x, y]: [f32; 2]| {
                    [
                        origin[0] + x * POINTS_PER_PIXEL,
                        origin[1] + y * POINTS_PER_PIXEL,
                    ]
                };
                let contours = contours.iter().map(|contour| contour.map(place)).collect();
                (contours, *even_odd)
            }
        };
        (contours, even_odd)
    }
}

/// Inset rectangle radii: percentages of the reference box, constrained to the rectangle.
fn rounded_inset(rect: Rect, radii: &CornerRadii, reference: [f32; 2]) -> Contour {
    rounded::rounded_rect(
        rect,
        used_radii(radii, reference, [rect.width, rect.height]),
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::paint::{PositionComponent, Segment};

    const REFERENCE: Rect = Rect {
        x: 10.0,
        y: 20.0,
        width: 100.0,
        height: 200.0,
    };

    fn square() -> CornerRadii {
        std::array::from_fn(|_| [LengthPercentage::ZERO; 2])
    }

    fn contours(shape: &BasicShape) -> Vec<Contour> {
        let Shape::Path { contours, .. } = shape.outline(REFERENCE) else {
            unreachable!("basic shapes outline as paths")
        };
        contours
    }

    #[test]
    fn rectangle_functions_resolve_overlapping_edges_as_their_examples_show() {
        let percent = LengthPercentage::Percentage;
        let point = LengthPercentage::Length;
        for (label, shape, equivalent) in [
            (
                "§3.1: inset(75% 0 50% 0) reduces its vertical insets to 60% and 40%",
                BasicShape::Inset {
                    insets: [percent(0.75), point(0.0), percent(0.5), point(0.0)],
                    radii: square(),
                },
                BasicShape::Inset {
                    insets: [percent(0.6), point(0.0), percent(0.4), point(0.0)],
                    radii: square(),
                },
            ),
            (
                "§3.1: rect(10px 0 0 20px) floors its right and bottom edges to rect(10px 20px \
                 10px 20px)",
                BasicShape::Rect {
                    edges: [point(7.5), point(0.0), point(0.0), point(15.0)],
                    radii: square(),
                },
                BasicShape::Rect {
                    edges: [point(7.5), point(15.0), point(7.5), point(15.0)],
                    radii: square(),
                },
            ),
        ] {
            assert_eq!(contours(&shape), contours(&equivalent), "{label}");
        }
    }

    #[test]
    fn circle_percentages_and_extents_resolve_against_the_reference_box() {
        let center = [PositionComponent::START; 2];
        for (label, radius, expected) in [
            // §1.3: the direction-agnostic size of a 100 × 200 box is √(100² + 200²) / √2.
            (
                "50% of the direction-agnostic size",
                CircleRadius::Length(LengthPercentage::Percentage(0.5)),
                0.5 * 100.0_f32.hypot(200.0) / std::f32::consts::SQRT_2,
            ),
            (
                "closest-side from the top-left corner",
                CircleRadius::Extent(RadialExtent::ClosestSide),
                0.0,
            ),
            (
                "farthest-corner from the top-left corner",
                CircleRadius::Extent(RadialExtent::FarthestCorner),
                100.0_f32.hypot(200.0),
            ),
        ] {
            let contour = &contours(&BasicShape::Circle {
                radius,
                center: center.clone(),
            })[0];
            let right = contour
                .segments
                .iter()
                .flat_map(|segment| match segment {
                    Segment::Line(point) => vec![*point],
                    Segment::Cubic(points) => points.to_vec(),
                })
                .map(|point| point[0])
                .fold(contour.start[0], f32::max);
            assert!(
                (right - REFERENCE.x - expected).abs() < 1e-3,
                "{label}: the circle reaches {right}, expected radius {expected}"
            );
        }
    }
}