ferri-pdf 0.1.1

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
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
//! CSS Backgrounds 3 layers: computed value lists, image sizing, tiling and painting areas.
use crate::paint::gradient::{Gradient, ResolvedGradient};
use crate::paint::{Border, BoxPaint, Clip, LengthPercentage, POINTS_PER_PIXEL, Rect};
use crate::paint::{ImageInfo, NaturalDimensions};
use serde::Serialize;
use std::convert::Infallible;
use std::sync::Arc;

/// Tolerance for `space` counting tiles that fit exactly.
const SPACE_FIT_TOLERANCE: f32 = 1.0e-4;

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
#[expect(
    clippy::enum_variant_names,
    reason = "variants name the CSS border-box, padding-box and content-box keywords"
)]
pub enum BoxArea {
    #[default]
    BorderBox,
    PaddingBox,
    ContentBox,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
#[expect(
    clippy::enum_variant_names,
    reason = "variants name the CSS repeat and no-repeat keywords"
)]
pub enum Repeat {
    #[default]
    Repeat,
    Space,
    Round,
    NoRepeat,
}
/// `local` behaves as `scroll` because paged boxes never scroll.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub enum Attachment {
    #[default]
    Scroll,
    Fixed,
    Local,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub enum SizeComponent {
    Auto,
    Value(LengthPercentage),
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub enum BackgroundSize {
    Cover,
    Contain,
    Explicit([SizeComponent; 2]),
}
impl Default for BackgroundSize {
    fn default() -> Self {
        Self::Explicit([const { SizeComponent::Auto }; 2])
    }
}
/// One axis of a `<position>`: an offset from the start edge or from the end edge.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct PositionComponent {
    pub from_end: bool,
    pub offset: LengthPercentage,
}
impl PositionComponent {
    /// The initial `background-position` component, `0%`.
    pub const START: Self = Self {
        from_end: false,
        offset: LengthPercentage::Percentage(0.0),
    };
    /// Center alignment, initially used by object-position and gradient positions.
    pub const CENTER: Self = Self {
        from_end: false,
        offset: LengthPercentage::Percentage(0.5),
    };
    /// Top-left offsets with explicit length and percentage terms for computed-value comparisons.
    #[must_use]
    pub fn canonical(&self) -> Self {
        use crate::paint::Calculation;
        let offset = match &self.offset {
            LengthPercentage::Length(value) => Calculation::Value(*value),
            LengthPercentage::Percentage(value) => Calculation::Percentage(*value),
            LengthPercentage::Calc(value) => (**value).clone(),
        };
        let offset = if self.from_end {
            Calculation::Sum(vec![Calculation::Percentage(1.0), offset.negate()]).simplify()
        } else {
            offset
        };
        Self {
            from_end: false,
            offset: Calculation::Sum(vec![
                Calculation::Value(0.0),
                Calculation::Percentage(0.0),
                offset,
            ])
            .into(),
        }
    }
    /// Offset within `free` space; percentages align that point of the object and the area.
    pub fn resolve(&self, free: f32) -> f32 {
        let offset = self.offset.resolve(free);
        if self.from_end { free - offset } else { offset }
    }
}
/// Horizontal and vertical components.
pub(crate) type Position = [PositionComponent; 2];

#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(bound(serialize = ""))]
pub enum BackgroundImage<I = Infallible> {
    None,
    /// Absolute reference that the document builder loads into `Image`; `resolution` is a
    /// raster image's natural resolution in dots per CSS pixel (CSS Images 4 ยง2.4).
    Url {
        url: String,
        resolution: f32,
    },
    Image {
        #[serde(skip)]
        image: I,
        resolution: f32,
    },
    Gradient(Gradient),
}

/// Computed background longhands; layer `i` uses item `i` of each list, cycling short lists.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(bound(serialize = ""))]
pub struct Backgrounds<I = Infallible> {
    pub images: Vec<BackgroundImage<I>>,
    /// `background-position-x` and `background-position-y`, which cycle independently.
    pub positions: [Vec<PositionComponent>; 2],
    pub sizes: Vec<BackgroundSize>,
    pub repeats: Vec<[Repeat; 2]>,
    pub origins: Vec<BoxArea>,
    pub clips: Vec<BoxArea>,
    pub attachments: Vec<Attachment>,
    pub blend_modes: Vec<crate::paint::BlendMode>,
}
impl<I> Default for Backgrounds<I> {
    fn default() -> Self {
        Self {
            images: vec![BackgroundImage::None],
            positions: [
                vec![PositionComponent::START],
                vec![PositionComponent::START],
            ],
            sizes: vec![BackgroundSize::default()],
            repeats: vec![[Repeat::Repeat; 2]],
            origins: vec![BoxArea::PaddingBox],
            clips: vec![BoxArea::BorderBox],
            attachments: vec![Attachment::Scroll],
            blend_modes: vec![crate::paint::BlendMode::Normal],
        }
    }
}
fn cycle<T>(values: &[T], index: usize) -> &T {
    &values[index % values.len()]
}
impl<I> Backgrounds<I> {
    pub fn has_images(&self) -> bool {
        self.images
            .iter()
            .any(|image| !matches!(image, BackgroundImage::None))
    }
    /// The background colour is clipped like the bottom layer.
    pub fn color_clip(&self) -> BoxArea {
        *cycle(&self.clips, self.images.len() - 1)
    }
    /// Treats `fixed` layers as `scroll`, as in a transformed box or its descendants
    /// (CSS Transforms 1 ยง6).
    pub fn scroll_fixed(backgrounds: &mut Arc<Self>)
    where
        I: Clone,
    {
        if backgrounds.attachments.contains(&Attachment::Fixed) {
            for attachment in &mut Arc::make_mut(backgrounds).attachments {
                if *attachment == Attachment::Fixed {
                    *attachment = Attachment::Scroll;
                }
            }
        }
    }
    /// Replaces every `Url` image with the image `load` returns for it, copying shared lists
    /// only when one is present.
    pub(crate) fn resolve_images<E>(
        backgrounds: &mut Arc<Self>,
        mut load: impl FnMut(&str) -> Result<I, E>,
    ) -> Result<(), E>
    where
        I: Clone,
    {
        if !backgrounds
            .images
            .iter()
            .any(|image| matches!(image, BackgroundImage::Url { .. }))
        {
            return Ok(());
        }
        for image in &mut Arc::make_mut(backgrounds).images {
            image.load(&mut load)?;
        }
        Ok(())
    }
}
impl<I> BackgroundImage<I> {
    /// Replaces a `Url` with the image `load` returns for it.
    pub fn load<E>(&mut self, load: &mut impl FnMut(&str) -> Result<I, E>) -> Result<(), E> {
        if let Self::Url { url, resolution } = self {
            *self = Self::Image {
                image: load(url)?,
                resolution: *resolution,
            };
        }
        Ok(())
    }
}

impl Backgrounds<Infallible> {
    pub fn with_image_type<I>(&self) -> Backgrounds<I> {
        Backgrounds {
            images: self
                .images
                .iter()
                .map(BackgroundImage::with_image_type)
                .collect(),
            positions: self.positions.clone(),
            sizes: self.sizes.clone(),
            repeats: self.repeats.clone(),
            origins: self.origins.clone(),
            clips: self.clips.clone(),
            attachments: self.attachments.clone(),
            blend_modes: self.blend_modes.clone(),
        }
    }
}

impl BackgroundImage<Infallible> {
    pub fn with_image_type<I>(&self) -> BackgroundImage<I> {
        match self {
            Self::None => BackgroundImage::None,
            Self::Url { url, resolution } => BackgroundImage::Url {
                url: url.clone(),
                resolution: *resolution,
            },
            Self::Gradient(gradient) => BackgroundImage::Gradient(gradient.clone()),
            Self::Image { image, .. } => match *image {},
        }
    }
}

/// A `url()` image that reached painting without being loaded.
#[derive(Debug)]
pub struct UnresolvedImage(pub String);
impl std::fmt::Display for UnresolvedImage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "background image {} was not loaded", self.0)
    }
}
impl std::error::Error for UnresolvedImage {}

pub(crate) enum Source<'a, I> {
    /// A loaded image and its natural resolution in dots per CSS pixel.
    Image(&'a I, f32),
    Gradient(&'a Gradient),
}

impl<I> Copy for Source<'_, I> {}
impl<I> Clone for Source<'_, I> {
    fn clone(&self) -> Self {
        *self
    }
}

/// Content of one tile.
#[derive(Clone, Debug)]
pub enum TileImage<'a, I = Infallible> {
    Image(&'a I),
    Gradient(ResolvedGradient),
}
/// One background layer ready to paint.
#[derive(Clone, Debug)]
pub struct LayerPaint<'a, I = Infallible> {
    pub image: TileImage<'a, I>,
    /// Background painting area.
    pub clip: Clip,
    /// The tile placed by `background-position`; repeated axes tile from it both ways.
    pub tile: Rect,
    /// Distance between tile origins, including `space` gaps.
    pub step: [f32; 2],
    pub repeat: [bool; 2],
    /// How the layer blends with the layers and colour below it (Compositing 1 ยง3.4.3).
    pub blend: crate::paint::BlendMode,
}

/// Natural dimensions converted to points.
/// Natural dimensions in points; a raster image's pixels span `1 / resolution` CSS pixels.
pub(crate) fn natural_points<I: ImageInfo>(image: &I, resolution: f32) -> NaturalDimensions {
    let natural = image.natural_size();
    let scale = if image.is_raster() {
        POINTS_PER_PIXEL / resolution
    } else {
        POINTS_PER_PIXEL
    };
    NaturalDimensions {
        width: natural.width.map(|width| width * scale),
        height: natural.height.map(|height| height * scale),
        ratio: natural.ratio,
    }
}

fn contain(area: [f32; 2], ratio: f32, cover: bool) -> [f32; 2] {
    let width_limited = area[0] / ratio <= area[1];
    if width_limited == cover {
        [area[1] * ratio, area[1]]
    } else {
        [area[0], area[0] / ratio]
    }
}

/// `background-size` and the CSS Images 3 default sizing algorithm, before `round`;
/// natural dimensions are in points.
pub(crate) fn image_size(
    size: &BackgroundSize,
    natural: NaturalDimensions,
    area: [f32; 2],
) -> [f32; 2] {
    let NaturalDimensions {
        width,
        height,
        ratio,
    } = natural;
    let resolve = |component: &SizeComponent, basis: f32| match component {
        SizeComponent::Auto => None,
        SizeComponent::Value(value) => Some(value.resolve(basis)),
    };
    match size {
        BackgroundSize::Cover | BackgroundSize::Contain => ratio.map_or(area, |ratio| {
            contain(area, ratio, *size == BackgroundSize::Cover)
        }),
        BackgroundSize::Explicit([x, y]) => match (resolve(x, area[0]), resolve(y, area[1])) {
            (Some(x), Some(y)) => [x, y],
            (Some(x), None) => [x, ratio.map(|r| x / r).or(height).unwrap_or(area[1])],
            (None, Some(y)) => [ratio.map(|r| y * r).or(width).unwrap_or(area[0]), y],
            (None, None) => match (width, height, ratio) {
                (Some(x), Some(y), _) => [x, y],
                (Some(x), None, Some(r)) => [x, x / r],
                (None, Some(y), Some(r)) => [y * r, y],
                (Some(x), None, None) => [x, area[1]],
                (None, Some(y), None) => [area[0], y],
                (None, None, Some(r)) => contain(area, r, false),
                (None, None, None) => area,
            },
        },
    }
}

/// Second and third `round` steps of CSS Backgrounds 3 ยง3.9.
pub(crate) fn rounded_size(
    mut tile: [f32; 2],
    size: &BackgroundSize,
    repeat: [Repeat; 2],
    area: [f32; 2],
) -> [f32; 2] {
    let original = tile;
    for axis in 0..2 {
        if repeat[axis] == Repeat::Round && tile[axis] > 0.0 {
            tile[axis] = area[axis] / (area[axis] / tile[axis]).round().max(1.0);
        }
    }
    if let BackgroundSize::Explicit(components) = size {
        for axis in 0..2 {
            let other = 1 - axis;
            if repeat[axis] == Repeat::Round
                && repeat[other] != Repeat::Round
                && components[other] == SizeComponent::Auto
                && original[axis] > 0.0
            {
                tile[other] = original[other] * tile[axis] / original[axis];
            }
        }
    }
    tile
}

/// Tile origin, pitch and repetition on one axis.
fn place(
    repeat: Repeat,
    position: &PositionComponent,
    start: f32,
    area: f32,
    tile: f32,
) -> (f32, f32, bool) {
    if repeat == Repeat::Space {
        let count = (area / tile + SPACE_FIT_TOLERANCE).floor();
        if count >= 2.0 {
            return (start, tile + (area - count * tile) / (count - 1.0), true);
        }
    }
    (
        start + position.resolve(area - tile),
        tile,
        matches!(repeat, Repeat::Repeat | Repeat::Round),
    )
}

impl<I> BoxPaint<I> {
    /// Padding without the sliced edges of a fragment.
    pub(super) fn fragment_padding(&self) -> [f32; 4] {
        let mut padding = self.padding;
        if self.assembled_box().is_some() {
            return padding;
        }
        for (side, sliced) in padding.iter_mut().zip(self.sliced()) {
            if sliced {
                *side = 0.0;
            }
        }
        padding
    }
    /// The border, padding or content box of this fragment with its curves.
    pub fn box_area(&self, area: BoxArea) -> Clip {
        match area {
            BoxArea::BorderBox => self.slice_clip(Clip {
                rect: self.decoration_rect(),
                radii: self.radii(),
                reference_box: None,
                axes: [true; 2],
            }),
            BoxArea::PaddingBox => self.inner_clip([0.0; 4], [true; 2]),
            BoxArea::ContentBox => self.inner_clip(self.fragment_padding(), [true; 2]),
        }
    }
    pub fn background_clips(&self) -> crate::paint::BoxAreas<Clip> {
        crate::paint::BoxAreas {
            border: self.box_area(BoxArea::BorderBox),
            padding: self.box_area(BoxArea::PaddingBox),
            content: self.box_area(BoxArea::ContentBox),
        }
    }
    pub fn background_origins(&self) -> crate::paint::BoxAreas<Rect> {
        crate::paint::BoxAreas {
            border: self.positioning_area(BoxArea::BorderBox),
            padding: self.positioning_area(BoxArea::PaddingBox),
            content: self.positioning_area(BoxArea::ContentBox),
        }
    }
    pub(crate) fn background_clip(&self, area: BoxArea) -> Clip {
        self.background_geometry
            .as_ref()
            .map_or_else(|| self.box_area(area), |geometry| geometry.clips.area(area))
    }
    /// Positioning area, taken from `background_box` when another box positions the layers.
    fn positioning_area(&self, area: BoxArea) -> Rect {
        if let Some(origins) = self
            .background_geometry
            .as_ref()
            .and_then(|geometry| geometry.origins)
        {
            return origins.area(area);
        }
        let Some(outer) = self.background_box else {
            return self.box_area(area).rect;
        };
        let borders = self.decoration.borders.map(Border::used_width);
        let insets: [f32; 4] = match area {
            BoxArea::BorderBox => [0.0; 4],
            BoxArea::PaddingBox => borders,
            BoxArea::ContentBox => std::array::from_fn(|side| borders[side] + self.padding[side]),
        };
        Rect {
            x: outer.x + insets[3],
            y: outer.y + insets[0],
            width: outer.width - insets[1] - insets[3],
            height: outer.height - insets[0] - insets[2],
        }
    }
}

impl<I: ImageInfo> BoxPaint<I> {
    /// Image layers bottom-most first; `page_area` positions `fixed` layers.
    pub fn background_layers(
        &self,
        page_area: Rect,
    ) -> Result<Vec<LayerPaint<'_, I>>, UnresolvedImage> {
        if !self.draw_background {
            return Ok(Vec::new());
        }
        Ok(self
            .layers(&self.decoration.backgrounds, page_area)?
            .into_iter()
            .map(|(_, layer)| layer)
            .collect())
    }
    /// Mask image layers bottom-most first with their list indices, placed and clipped like
    /// background layers (CSS Masking 1 ยง7).
    pub fn mask_layers(
        &self,
        page_area: Rect,
    ) -> Result<Vec<(usize, LayerPaint<'_, I>)>, UnresolvedImage> {
        self.layers(&self.decoration.masks.layers, page_area)
    }
    fn layers<'a>(
        &'a self,
        backgrounds: &'a Backgrounds<I>,
        page_area: Rect,
    ) -> Result<Vec<(usize, LayerPaint<'a, I>)>, UnresolvedImage> {
        let mut layers = Vec::new();
        let page_area = self
            .background_geometry
            .as_ref()
            .and_then(|geometry| geometry.viewport)
            .unwrap_or(page_area);
        for (index, image) in backgrounds.images.iter().enumerate().rev() {
            let source = match image {
                BackgroundImage::None => continue,
                BackgroundImage::Url { url, .. } => {
                    return Err(UnresolvedImage(url.clone()));
                }
                BackgroundImage::Image { image, resolution } => Source::Image(image, *resolution),
                BackgroundImage::Gradient(gradient) => Source::Gradient(gradient),
            };
            let natural = match source {
                Source::Image(image, resolution) => natural_points(image, resolution),
                Source::Gradient(_) => NaturalDimensions::default(),
            };
            let area = if *cycle(&backgrounds.attachments, index) == Attachment::Fixed {
                page_area
            } else {
                self.positioning_area(*cycle(&backgrounds.origins, index))
            };
            let extent = [area.width, area.height];
            let size = cycle(&backgrounds.sizes, index);
            let repeat = *cycle(&backgrounds.repeats, index);
            let tile = rounded_size(image_size(size, natural, extent), size, repeat, extent);
            if !(tile[0] > 0.0 && tile[1] > 0.0 && tile.iter().all(|v| v.is_finite())) {
                continue;
            }
            let [position_x, position_y] = &backgrounds.positions;
            let (x, step_x, repeat_x) = place(
                repeat[0],
                cycle(position_x, index),
                area.x,
                extent[0],
                tile[0],
            );
            let (y, step_y, repeat_y) = place(
                repeat[1],
                cycle(position_y, index),
                area.y,
                extent[1],
                tile[1],
            );
            let image = match source {
                Source::Image(image, _) => TileImage::Image(image),
                Source::Gradient(gradient) => match gradient.resolve(tile) {
                    Some(resolved) => TileImage::Gradient(resolved),
                    None => continue,
                },
            };
            layers.push((
                index,
                LayerPaint {
                    image,
                    clip: self.background_clip(*cycle(&backgrounds.clips, index)),
                    tile: Rect {
                        x,
                        y,
                        width: tile[0],
                        height: tile[1],
                    },
                    step: [step_x, step_y],
                    repeat: [repeat_x, repeat_y],
                    blend: *cycle(&backgrounds.blend_modes, index),
                },
            ));
        }
        Ok(layers)
    }
}