spindrift 0.1.0

A HTML/CSS to PDF renderer, targeting spec compliance and efficiency
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
use super::*;

/// A CSS background positioning or clipping area in one bottom-left-origin
/// coordinate space.
///
/// CSS Backgrounds resolves positioning, clipping, and repeat geometry in a
/// common coordinate system.  Keeping the marker on that rectangle prevents
/// document-canvas geometry from reaching page-local primitive emission before
/// the page-projection boundary:
/// <https://www.w3.org/TR/css-backgrounds-3/#backgrounds>.
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(transparent)]
pub(in crate::layout) struct BackgroundArea<Space>(euclid::Rect<f32, Space>);

/// A background area ready for page-local paint primitive emission.
pub(in crate::layout) type PaintBackgroundArea = BackgroundArea<PaintSpace>;
/// A background area positioned on the assembled document canvas.
pub(in crate::layout) type DocumentCanvasBackgroundArea = BackgroundArea<DocumentCanvasSpace>;

/// Construct the paged-media viewport positioning area for a fixed
/// background.
///
/// A page's viewport-equivalent area is its margin box, whose extent is the
/// complete page size. It is distinct from [`PageContext::area_width`] and
/// [`PageContext::area_height`], which describe only the document page area.
/// The caller supplies the margin-box origin in its own coordinate space so
/// page-local and document-canvas background paint share this definition:
/// <https://www.w3.org/TR/css-page-3/#page-backgrounds>.
pub(in crate::layout) fn fixed_background_page_margin_box<Space>(
    origin: euclid::Point2D<f32, Space>,
    page_size: PageSize,
) -> BackgroundArea<Space> {
    BackgroundArea::new(
        origin,
        euclid::Size2D::new(page_size.width(), page_size.height()),
    )
}

impl<Space> BackgroundArea<Space> {
    pub(in crate::layout) fn new(
        origin: euclid::Point2D<f32, Space>,
        size: euclid::Size2D<f32, Space>,
    ) -> Self {
        Self(euclid::Rect::new(
            origin,
            euclid::Size2D::new(size.width.max(0.0), size.height.max(0.0)),
        ))
    }

    pub(in crate::layout) fn x(&self) -> f32 {
        self.0.origin.x
    }

    pub(in crate::layout) fn y(&self) -> f32 {
        self.0.origin.y
    }

    pub(in crate::layout) fn width(&self) -> f32 {
        self.0.size.width
    }

    pub(in crate::layout) fn height(&self) -> f32 {
        self.0.size.height
    }

    pub(in crate::layout) fn size(&self) -> euclid::Size2D<f32, Space> {
        self.0.size
    }

    pub(in crate::layout) fn inset(self, edges: css::Edges) -> Self {
        Self::new(
            euclid::Point2D::new(self.x() + edges.left, self.y() + edges.bottom),
            euclid::Size2D::new(
                (self.width() - edges.left - edges.right).max(0.0),
                (self.height() - edges.top - edges.bottom).max(0.0),
            ),
        )
    }

    pub(in crate::layout) fn intersect(self, other: Self) -> Option<Self> {
        self.0.intersection(&other.0).map(Self)
    }
}

impl BackgroundArea<PaintSpace> {
    pub(in crate::layout) fn from_paint_rect(rect: PaintRect) -> Self {
        Self(rect)
    }

    pub(in crate::layout) fn paint_rect(self) -> PaintRect {
        self.0
    }
}

impl BackgroundArea<DocumentCanvasSpace> {
    pub(in crate::layout) fn from_document_canvas_rect(rect: DocumentCanvasRect) -> Self {
        Self(rect)
    }

    /// Project a document-canvas area onto one page before page-local
    /// primitive emission.
    pub(in crate::layout) fn project_to_paint(
        self,
        page_document_bottom: f32,
    ) -> PaintBackgroundArea {
        PaintBackgroundArea::new(
            PaintPoint::new(self.x(), self.y() - page_document_bottom),
            PaintSize::new(self.width(), self.height()),
        )
    }
}

/// The distinct coordinate areas used to resolve a background image.
///
/// The selected `background-origin` area positions a normal layer, its
/// `background-clip` area bounds painting, and a fixed layer may instead use
/// a viewport-equivalent positioning area. CSS defines these independently:
/// <https://www.w3.org/TR/css-backgrounds-3/#the-background-origin>,
/// <https://www.w3.org/TR/css-backgrounds-3/#the-background-attachment>.
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct BackgroundPaintAreas<Space> {
    pub(in crate::layout) positioning_border_area: BackgroundArea<Space>,
    pub(in crate::layout) clip_border_area: BackgroundArea<Space>,
    pub(in crate::layout) fixed_positioning_area: Option<BackgroundArea<Space>>,
    pub(in crate::layout) fixed_attachment_is_scrolled_by_transform: bool,
}

/// Fully resolved geometry for one CSS background layer.
///
/// CSS Backgrounds resolves the positioning area, used image size, position,
/// and repeat behavior before painting the image. Keeping that result together
/// ensures vector and raster image sources use identical tile geometry:
/// <https://www.w3.org/TR/css-backgrounds-3/#the-background-image>.
#[derive(Debug, Clone)]
pub(in crate::layout) struct ResolvedBackgroundTile {
    pub(in crate::layout) positioning_area: PaintBackgroundArea,
    pub(in crate::layout) clip_area: PaintBackgroundArea,
    pub(in crate::layout) rounded_clip: Option<RenderedPathClip>,
    pub(in crate::layout) size: PaintSize,
    pub(in crate::layout) offset: PaintBackgroundOffset,
    pub(in crate::layout) repeat: css::BackgroundRepeat,
}

impl ResolvedBackgroundTile {
    pub(in crate::layout) fn new(
        positioning_area: PaintBackgroundArea,
        clip_area: PaintBackgroundArea,
        rounded_clip: Option<RenderedPathClip>,
        layer: &css::BackgroundLayer,
        size: PaintSize,
    ) -> Self {
        let offset = background_position(layer.position.clone(), positioning_area.size(), size);
        Self {
            positioning_area,
            clip_area,
            rounded_clip,
            size,
            offset,
            repeat: layer.repeat,
        }
    }

    pub(in crate::layout) fn tile_xs(&self) -> Vec<f32> {
        let repeat = self.repeat.x_axis();
        let (area_start, area_size) = if repeat == css::BackgroundRepeatAxis::Space {
            (self.positioning_area.x(), self.positioning_area.width())
        } else {
            (self.clip_area.x(), self.clip_area.width())
        };
        background_tile_positions(
            (f64::from(self.positioning_area.x()) + self.offset.x) as f32,
            area_start,
            area_size,
            self.size.width,
            repeat,
        )
    }

    pub(in crate::layout) fn tile_ys(&self) -> Vec<f32> {
        let repeat = self.repeat.y_axis();
        let (area_start, area_size) = if repeat == css::BackgroundRepeatAxis::Space {
            (self.positioning_area.y(), self.positioning_area.height())
        } else {
            (self.clip_area.y(), self.clip_area.height())
        };
        background_tile_positions(
            (f64::from(self.positioning_area.y()) + self.offset.y) as f32,
            area_start,
            area_size,
            self.size.height,
            repeat,
        )
    }

    pub(in crate::layout) fn tiles(&self) -> Vec<PaintBackgroundArea> {
        let tile_xs = self.tile_xs();
        self.tile_ys()
            .into_iter()
            .flat_map(|y| {
                tile_xs
                    .iter()
                    .cloned()
                    .map(move |x| PaintBackgroundArea::new(PaintPoint::new(x, y), self.size))
            })
            .collect()
    }
}
pub(in crate::layout) fn used_background_layer_size(
    decoded: &DecodedPngImage,
    layer: &css::BackgroundLayer,
    positioning_area: PaintSize,
) -> PaintSize {
    let Some(image) = layer.image.as_image() else {
        return PaintSize::new(0.0, 0.0);
    };
    let selected_image = image.selected_image();
    let generated_image = matches!(
        selected_image,
        BackgroundImage::LinearGradient(_)
            | BackgroundImage::RadialGradient(_)
            | BackgroundImage::ConicGradient(_)
            | BackgroundImage::CssColor(_)
    );
    let mut size = if generated_image {
        used_generated_background_size(positioning_area, layer.size.clone())
    } else {
        used_background_size(
            decoded,
            positioning_area,
            layer.size.clone(),
            image.intrinsic_resolution(),
        )
    };

    let (width_is_auto, height_is_auto) = match &layer.size {
        css::BackgroundSize::Auto => (true, true),
        css::BackgroundSize::Explicit { width, height } => (
            matches!(width, css::BackgroundSizeAxis::Auto),
            matches!(height, css::BackgroundSizeAxis::Auto),
        ),
        css::BackgroundSize::Cover | css::BackgroundSize::Contain => (false, false),
    };
    let natural_size = decoded.natural_layout_size();
    let aspect_ratio = (!generated_image && natural_size.height > 0.0)
        .then(|| natural_size.width / natural_size.height);
    if layer.repeat.x_axis() == css::BackgroundRepeatAxis::Round {
        size.width = rounded_background_tile_size(size.width, positioning_area.width);
        if height_is_auto && let Some(aspect_ratio) = aspect_ratio {
            size.height = size.width / aspect_ratio;
        }
    }
    if layer.repeat.y_axis() == css::BackgroundRepeatAxis::Round {
        size.height = rounded_background_tile_size(size.height, positioning_area.height);
        if width_is_auto && let Some(aspect_ratio) = aspect_ratio {
            size.width = size.height * aspect_ratio;
        }
    }
    size
}

pub(in crate::layout) fn used_generated_background_layer_size(
    layer: &css::BackgroundLayer,
    positioning_area: PaintSize,
) -> PaintSize {
    let mut size = used_generated_background_size(positioning_area, layer.size.clone());
    if layer.repeat.x_axis() == css::BackgroundRepeatAxis::Round {
        size.width = rounded_background_tile_size(size.width, positioning_area.width);
    }
    if layer.repeat.y_axis() == css::BackgroundRepeatAxis::Round {
        size.height = rounded_background_tile_size(size.height, positioning_area.height);
    }
    size
}

pub(in crate::layout) fn rounded_background_tile_size(tile_size: f32, area_size: f32) -> f32 {
    if tile_size <= 0.0 || area_size <= 0.0 {
        return tile_size;
    }
    let count = (area_size / tile_size).round().max(1.0);
    area_size / count
}

pub(in crate::layout) fn used_generated_background_size(
    positioning_area: PaintSize,
    value: css::BackgroundSize,
) -> PaintSize {
    match value {
        css::BackgroundSize::Auto | css::BackgroundSize::Cover | css::BackgroundSize::Contain => {
            positioning_area
        }
        css::BackgroundSize::Explicit { width, height } => {
            let used_width = used_background_size_axis(width, positioning_area.width)
                .unwrap_or(positioning_area.width);
            let used_height = used_background_size_axis(height, positioning_area.height)
                .unwrap_or(positioning_area.height);
            PaintSize::new(used_width, used_height)
        }
    }
}

pub(in crate::layout) fn background_layers_for_paint(
    style: &ComputedStyle,
) -> Vec<css::BackgroundLayer> {
    if !style.background.background_layers.is_empty() {
        return style.background.background_layers.clone();
    }
    vec![css::BackgroundLayer {
        image: style.background.background_image.clone(),
        position: style.background.background_position.clone(),
        size: style.background.background_size.clone(),
        repeat: style.background.background_repeat,
        attachment: style.background.background_attachment,
        origin: style.background.background_origin,
        clip: style.background.background_clip,
    }]
}

pub(in crate::layout) fn background_paint_area_for_box<Space>(
    area: BackgroundArea<Space>,
    style: &ComputedStyle,
    box_: css::BackgroundBox,
) -> BackgroundArea<Space> {
    let border = used_border_widths(style);
    match box_ {
        css::BackgroundBox::Border | css::BackgroundBox::BorderArea => area,
        css::BackgroundBox::Padding => area.inset(border),
        css::BackgroundBox::Content => area.inset(border).inset(style.padding),
    }
}

/// Resolve the background positioning area for one layer.
///
/// `background-origin` selects an element box for scroll/local layers. A
/// fixed layer instead uses the viewport-equivalent area supplied by layout,
/// so its origin keyword has no effect.
/// <https://www.w3.org/TR/css-backgrounds-3/#the-background-origin> and
/// <https://www.w3.org/TR/css-backgrounds-3/#the-background-attachment>
pub(in crate::layout) fn background_positioning_area_for_layer<Space>(
    positioning_border_area: BackgroundArea<Space>,
    fixed_positioning_area: Option<BackgroundArea<Space>>,
    fixed_attachment_is_scrolled_by_transform: bool,
    style: &ComputedStyle,
    layer: &css::BackgroundLayer,
) -> BackgroundArea<Space> {
    match (layer.attachment, fixed_positioning_area) {
        // A transform turns a non-root fixed background into a scroll
        // background. Its image is part of the transformed element's paint
        // subtree rather than a viewport-fixed source.
        // <https://drafts.csswg.org/css-transforms-1/#transform-rendering>
        (css::BackgroundAttachment::Fixed, _) if fixed_attachment_is_scrolled_by_transform => {
            background_paint_area_for_box(positioning_border_area, style, layer.origin)
        }
        (css::BackgroundAttachment::Fixed, Some(area)) => area,
        _ => background_paint_area_for_box(positioning_border_area, style, layer.origin),
    }
}

/// Clip a raster background image to its destination-space paint area.
///
/// A CSS clip constrains the image's output; it must not change the mapping
/// between source pixels and its original destination tile. In particular,
/// converting a fractional source-pixel edge to an integer PDF source rect
/// would rescale the retained pixels. Keep the image geometry intact and
/// express any partial tile through a PDF destination clip instead.
/// <https://www.w3.org/TR/css-backgrounds-3/#the-background-clip>
pub(in crate::layout) fn clip_background_image_to_paint_area(
    mut image: RenderedImage,
    clip: PaintBackgroundArea,
    rounded_clip: Option<RenderedPathClip>,
) -> Option<RenderedImage> {
    let image_rect = image.paint_rect();
    let visible = image_rect.intersection(&clip.paint_rect())?;
    let clip = if visible != image_rect {
        let mut rectangular_clip = RenderedPathClip::new(
            paint_rect_path_commands(visible),
            RenderedPathFillRule::NonZero,
            Vec::new(),
        );
        if let Some(rounded_clip) = rounded_clip {
            rectangular_clip
                .additional_clips
                .push(RenderedPathClipPath::new(
                    rounded_clip.commands,
                    rounded_clip.fill_rule,
                ));
            rectangular_clip
                .additional_clips
                .extend(rounded_clip.additional_clips);
        }
        Some(rectangular_clip)
    } else {
        rounded_clip
    };
    if let Some(clip) = clip {
        image = image.with_clip(clip);
    }
    Some(image)
}

/// Returns tile origins that intersect a background positioning area.
///
/// CSS Backgrounds and Borders repeats from the positioned first tile in both
/// directions as needed, but PDF emission needs a finite set of image
/// placements for the current painted area:
/// <https://www.w3.org/TR/css-backgrounds-3/#the-background-repeat>.
pub(in crate::layout) fn background_tile_positions(
    positioned_start: f32,
    area_start: f32,
    area_size: f32,
    tile_size: f32,
    repeat: css::BackgroundRepeatAxis,
) -> Vec<f32> {
    if tile_size <= 0.0 {
        return Vec::new();
    }
    match repeat {
        css::BackgroundRepeatAxis::NoRepeat => return vec![positioned_start],
        css::BackgroundRepeatAxis::Space => {
            let count = spaced_background_tile_count(tile_size, area_size);
            if count < 2 {
                return vec![positioned_start];
            }
            let step = spaced_background_tile_step(tile_size, area_size, count);
            return (0..count)
                .map(|index| area_start + index as f32 * step)
                .collect();
        }
        css::BackgroundRepeatAxis::Repeat | css::BackgroundRepeatAxis::Round => {}
    }
    if area_size <= 0.0 {
        return Vec::new();
    }

    let area_end = area_start + area_size;
    let mut first = positioned_start;
    while first > area_start {
        first -= tile_size;
    }
    while first + tile_size <= area_start {
        first += tile_size;
    }

    let mut positions = Vec::new();
    let mut current = first;
    while current < area_end {
        positions.push(current);
        current += tile_size;
    }
    positions
}

pub(in crate::layout) fn background_first_tile_position(
    positioned_start: f32,
    area_start: f32,
    area_size: f32,
    tile_size: f32,
    repeat: css::BackgroundRepeatAxis,
) -> f32 {
    match repeat {
        css::BackgroundRepeatAxis::NoRepeat => return positioned_start,
        css::BackgroundRepeatAxis::Space => {
            if spaced_background_tile_count(tile_size, area_size) >= 2 {
                return area_start;
            }
            return positioned_start;
        }
        css::BackgroundRepeatAxis::Repeat | css::BackgroundRepeatAxis::Round => {}
    }
    if tile_size <= 0.0 {
        return positioned_start;
    }
    let mut first = positioned_start;
    while first > area_start {
        first -= tile_size;
    }
    while first + tile_size <= area_start {
        first += tile_size;
    }
    first
}

pub(in crate::layout) fn background_pattern_step(
    tile_size: f32,
    area_size: f32,
    repeat: css::BackgroundRepeatAxis,
) -> f32 {
    match repeat {
        css::BackgroundRepeatAxis::Repeat | css::BackgroundRepeatAxis::Round => tile_size,
        css::BackgroundRepeatAxis::Space => {
            let count = spaced_background_tile_count(tile_size, area_size);
            if count >= 2 {
                spaced_background_tile_step(tile_size, area_size, count)
            } else {
                non_repeating_pattern_step(tile_size, area_size)
            }
        }
        css::BackgroundRepeatAxis::NoRepeat => non_repeating_pattern_step(tile_size, area_size),
    }
}

pub(in crate::layout) fn spaced_background_tile_count(tile_size: f32, area_size: f32) -> usize {
    if tile_size <= 0.0 || area_size <= 0.0 {
        return 0;
    }
    (area_size / tile_size).floor().max(1.0) as usize
}

pub(in crate::layout) fn spaced_background_tile_step(
    tile_size: f32,
    area_size: f32,
    count: usize,
) -> f32 {
    if count < 2 {
        return non_repeating_pattern_step(tile_size, area_size);
    }
    (area_size - tile_size) / (count - 1) as f32
}

pub(in crate::layout) fn non_repeating_pattern_step(tile_size: f32, area_size: f32) -> f32 {
    tile_size.max(area_size.abs() * 2.0 + tile_size)
}