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
use std::rc::Rc;

use super::geometry::{PaintPoint, PaintRect, PaintSize, PaintTransform, PaintTranslation};
use super::images::{InlineRasterImage, RasterSampling, RenderedImageSource};
use super::paths::{RenderedGradient, RenderedPath, RenderedPathClip, RenderedPathClipPath};
use crate::image_store::ImageId;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PaintPatternTiling {
    pub tile_size: PaintSize,
    pub step: PaintSize,
    pub origin: PaintPoint,
}

impl PaintPatternTiling {
    pub fn new(tile_size: PaintSize, step: PaintSize, origin: PaintPoint) -> Self {
        Self {
            tile_size,
            step,
            origin,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct RenderedImagePattern {
    pub background: bool,
    pub(crate) source: RenderedImageSource,
    pub(in crate::document) rect: PaintRect,
    pub tiling: PaintPatternTiling,
    pub(crate) sampling: RasterSampling,
    clip: Option<RenderedPathClip>,
    transform: PaintTransform,
}

#[allow(dead_code)]
impl RenderedImagePattern {
    /// Paint one retained raster image through a single-cell tiling pattern.
    ///
    /// Border-image tiles and CSS background-image tiles both paint a raster
    /// source into a finite CSS rectangle.  Keeping the source (including a
    /// source crop) intact here lets the PDF backend use the same coverage
    /// model for those otherwise equivalent operations.
    pub(crate) fn single_tile_from_image(image: super::images::RenderedImage) -> Self {
        let rect = image.rect;
        Self {
            background: image.background,
            source: image.source,
            rect,
            tiling: PaintPatternTiling::new(rect.size, rect.size, rect.origin),
            sampling: image.sampling,
            // A border-image source tile has no destination-space clip: its
            // paint rectangle is the tile's complete region.
            clip: None,
            transform: image.transform.unwrap_or_else(PaintTransform::identity),
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn from_paint_rect(
        rect: PaintRect,
        background: bool,
        tiling: PaintPatternTiling,
        pixel_width: u32,
        pixel_height: u32,
        sampling: impl Into<RasterSampling>,
        rgb: Rc<[u8]>,
        alpha: Option<Rc<[u8]>>,
    ) -> Self {
        Self {
            background,
            source: RenderedImageSource::Inline {
                raster: InlineRasterImage {
                    pixel_width,
                    pixel_height,
                    natural_size: crate::units::CssPixelSize::new(pixel_width, pixel_height),
                    color_space: crate::color::RasterColorSpace::SRGB,
                    sample_depth: crate::image_store::RasterSampleDepth::Eight,
                    rgb,
                    alpha,
                },
                source_rect: None,
            },
            rect,
            tiling,
            sampling: sampling.into(),
            clip: None,
            transform: PaintTransform::identity(),
        }
    }

    pub fn x(&self) -> f32 {
        self.rect.origin.x
    }

    pub fn y(&self) -> f32 {
        self.rect.origin.y
    }

    pub fn width(&self) -> f32 {
        self.rect.size.width
    }

    pub fn height(&self) -> f32 {
        self.rect.size.height
    }

    pub(crate) fn paint_rect(&self) -> PaintRect {
        self.rect
    }

    /// Attach a PDF path clipping scope to a repeated background pattern.
    ///
    /// CSS Backgrounds clips each repeated image layer to the selected
    /// `background-clip` area, including rounded corners:
    /// <https://www.w3.org/TR/css-backgrounds-3/#background-clip>.
    pub(crate) fn with_clip(mut self, clip: RenderedPathClip) -> Self {
        self.clip = Some(clip);
        self
    }

    pub(crate) fn with_intersected_clip(mut self, clip: RenderedPathClip) -> Self {
        if let Some(existing) = &mut self.clip {
            existing
                .additional_clips
                .push(RenderedPathClipPath::new(clip.commands, clip.fill_rule));
            existing.additional_clips.extend(clip.additional_clips);
        } else {
            self.clip = Some(clip);
        }
        self
    }

    /// Attach a source crop before a pattern is promoted to a stored PDF
    /// image resource. The crop stays in source pixels, independently of the
    /// CSS tile geometry.
    pub(crate) fn with_source_rect(mut self, source_rect: Option<RenderedImageSourceRect>) -> Self {
        if let RenderedImageSource::Inline {
            source_rect: current,
            ..
        } = &mut self.source
        {
            *current = source_rect;
        }
        self
    }

    pub(crate) fn clip(&self) -> Option<&RenderedPathClip> {
        self.clip.as_ref()
    }

    pub(crate) fn transform(&self) -> PaintTransform {
        self.transform
    }

    pub(crate) fn with_image_id(mut self, image_id: Option<ImageId>) -> Self {
        if let Some(image_id) = image_id {
            let RenderedImageSource::Inline { raster, .. } = &self.source else {
                return self;
            };
            self.source = RenderedImageSource::Stored {
                image_id,
                source_rect: RenderedImageSourceRect {
                    x: 0,
                    y: 0,
                    width: raster.pixel_width,
                    height: raster.pixel_height,
                },
                pixel_width: raster.pixel_width,
                pixel_height: raster.pixel_height,
            };
        }
        self
    }

    /// Preserve the calibrated component space of generated inline samples.
    pub(crate) fn with_raster_color_space(
        mut self,
        color_space: crate::color::RasterColorSpace,
    ) -> Self {
        if let RenderedImageSource::Inline { raster, .. } = &mut self.source {
            raster.color_space = color_space;
        }
        self
    }

    pub(crate) fn with_raster_sample_depth(
        mut self,
        sample_depth: crate::image_store::RasterSampleDepth,
    ) -> Self {
        if let RenderedImageSource::Inline { raster, .. } = &mut self.source {
            raster.sample_depth = sample_depth;
        }
        self
    }

    pub(in crate::document) fn translated(mut self, offset: PaintTranslation) -> Self {
        self.rect = offset.transform_rect(&self.rect);
        self.tiling.origin = offset.transform_point(self.tiling.origin);
        if let Some(clip) = &mut self.clip {
            for command in &mut clip.commands {
                command.translate(offset);
            }
            for nested_clip in &mut clip.additional_clips {
                for command in &mut nested_clip.commands {
                    command.translate(offset);
                }
            }
        }
        self
    }

    /// Move the destination paint geometry while retaining CSS tile phase.
    ///
    /// Table fragmentation projects a source clip into a destination
    /// fragmentainer after the image has already been resolved against its
    /// unfragmented background positioning area. The clip and filled region
    /// move, but the positioning area's tile origin remains in source space.
    /// <https://www.w3.org/TR/css-backgrounds-3/#background-position>
    /// <https://www.w3.org/TR/css-break-3/#break-decoration>
    pub(in crate::document) fn translated_geometry_preserving_tile_origin(
        mut self,
        offset: PaintTranslation,
    ) -> Self {
        self.rect = offset.transform_rect(&self.rect);
        translate_pattern_clip(&mut self.clip, offset);
        self
    }
}

/// A repeated CSS gradient painted by a reusable PDF tiling pattern.
///
/// The pattern stores resolved CSS tile geometry independently from its PDF
/// resource allocation. Its cell paints the shared axial or radial shading;
/// the outer path applies CSS `background-clip`.
/// <https://www.w3.org/TR/css-backgrounds-3/#the-background-repeat>
#[derive(Debug, Clone, PartialEq)]
pub struct RenderedGradientPattern {
    pub(crate) rect: PaintRect,
    pub(crate) tiling: PaintPatternTiling,
    pub(crate) gradient: RenderedGradient,
    clip: Option<RenderedPathClip>,
    /// Maps retained source geometry into its destination fragmentainer.
    transform: PaintTransform,
}

#[allow(dead_code)]
impl RenderedGradientPattern {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new(
        rect: PaintRect,
        tiling: PaintPatternTiling,
        gradient: RenderedGradient,
        clip: Option<RenderedPathClip>,
    ) -> Self {
        Self {
            rect,
            tiling,
            gradient,
            clip,
            transform: PaintTransform::identity(),
        }
    }

    pub(crate) fn paint_rect(&self) -> PaintRect {
        self.rect
    }

    pub(crate) fn paint_bounds(&self) -> PaintRect {
        self.transform
            .apply_clip_to_aabb(super::geometry::PaintClip::from_paint_rect(self.rect))
            .paint_rect()
    }

    pub(crate) fn transform(&self) -> PaintTransform {
        self.transform
    }

    #[cfg(test)]
    pub(crate) fn transformed(mut self, transform: PaintTransform) -> Self {
        // The retained transform is emitted around the source-local pattern
        // fill; leave the CSS tile geometry in that source space.
        self.transform = transform.multiply(self.transform);
        self
    }

    pub fn x(&self) -> f32 {
        self.rect.origin.x
    }

    pub fn y(&self) -> f32 {
        self.rect.origin.y
    }

    pub fn width(&self) -> f32 {
        self.rect.size.width
    }

    pub fn height(&self) -> f32 {
        self.rect.size.height
    }
    pub(crate) fn clip(&self) -> Option<&RenderedPathClip> {
        self.clip.as_ref()
    }

    /// Intersect this pattern's existing CSS clip with a destination
    /// fragmentainer rectangle without changing its source-resolved tiling
    /// phase. Fragmentation uses this after a source-to-destination transform
    /// has been selected: the transform belongs to the positioning area,
    /// while the added clip remains in destination paint coordinates.
    pub(crate) fn with_intersected_clip(mut self, clip: RenderedPathClip) -> Self {
        if let Some(existing) = &mut self.clip {
            existing
                .additional_clips
                .push(RenderedPathClipPath::new(clip.commands, clip.fill_rule));
            existing.additional_clips.extend(clip.additional_clips);
        } else {
            self.clip = Some(clip);
        }
        self
    }

    pub(in crate::document) fn translated(mut self, offset: PaintTranslation) -> Self {
        if self.transform != PaintTransform::identity() {
            // Preserve source-local pattern geometry while composing the
            // retained destination transform.
            self.transform = PaintTransform::translate(offset).multiply(self.transform);
            return self;
        }
        self.rect = offset.transform_rect(&self.rect);
        self.tiling.origin = offset.transform_point(self.tiling.origin);
        self.gradient.transform =
            PaintTransform::translate(offset).multiply(self.gradient.transform);
        if let Some(clip) = &mut self.clip {
            for command in &mut clip.commands {
                command.translate(offset);
            }
            for nested in &mut clip.additional_clips {
                for command in &mut nested.commands {
                    command.translate(offset);
                }
            }
        }
        self
    }

    /// See [`RenderedImagePattern::translated_geometry_preserving_tile_origin`].
    pub(in crate::document) fn translated_geometry_preserving_tile_origin(
        mut self,
        offset: PaintTranslation,
    ) -> Self {
        self.rect = offset.transform_rect(&self.rect);
        translate_pattern_clip(&mut self.clip, offset);
        self
    }
}

/// A reusable vector tile for a repeated URL SVG background.
///
/// PDF emission serializes its paths once into a Form XObject and invokes that
/// form from a Type 1 tiling pattern, avoiding one page primitive per CSS tile.
#[derive(Debug, Clone, PartialEq)]
pub struct RenderedSvgPattern {
    pub(crate) rect: PaintRect,
    pub(crate) tiling: PaintPatternTiling,
    pub(crate) paths: Vec<RenderedPath>,
    clip: Option<RenderedPathClip>,
    transform: PaintTransform,
}

impl RenderedSvgPattern {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new(
        rect: PaintRect,
        tiling: PaintPatternTiling,
        paths: Vec<RenderedPath>,
        clip: Option<RenderedPathClip>,
    ) -> Self {
        Self {
            rect,
            tiling,
            paths,
            clip,
            transform: PaintTransform::identity(),
        }
    }

    pub(crate) fn paint_rect(&self) -> PaintRect {
        self.rect
    }

    pub(crate) fn clip(&self) -> Option<&RenderedPathClip> {
        self.clip.as_ref()
    }

    pub(crate) fn with_intersected_clip(mut self, clip: RenderedPathClip) -> Self {
        if let Some(existing) = &mut self.clip {
            existing
                .additional_clips
                .push(RenderedPathClipPath::new(clip.commands, clip.fill_rule));
            existing.additional_clips.extend(clip.additional_clips);
        } else {
            self.clip = Some(clip);
        }
        self
    }

    pub(crate) fn transform(&self) -> PaintTransform {
        self.transform
    }

    pub(in crate::document) fn translated(mut self, offset: PaintTranslation) -> Self {
        self.rect = offset.transform_rect(&self.rect);
        self.tiling.origin = offset.transform_point(self.tiling.origin);
        // `paths` are local to the Form XObject cell and deliberately remain
        // at its origin. Only the page placement and its CSS clip move.
        if let Some(clip) = &mut self.clip {
            for command in &mut clip.commands {
                command.translate(offset);
            }
            for nested in &mut clip.additional_clips {
                for command in &mut nested.commands {
                    command.translate(offset);
                }
            }
        }
        self
    }

    /// See [`RenderedImagePattern::translated_geometry_preserving_tile_origin`].
    pub(in crate::document) fn translated_geometry_preserving_tile_origin(
        mut self,
        offset: PaintTranslation,
    ) -> Self {
        self.rect = offset.transform_rect(&self.rect);
        translate_pattern_clip(&mut self.clip, offset);
        self
    }
}

fn translate_pattern_clip(clip: &mut Option<RenderedPathClip>, offset: PaintTranslation) {
    if let Some(clip) = clip {
        for command in &mut clip.commands {
            command.translate(offset);
        }
        for nested_clip in &mut clip.additional_clips {
            for command in &mut nested_clip.commands {
                command.translate(offset);
            }
        }
    }
}

/// Pixel-space source rectangle for drawing a cropped PDF image XObject.
///
/// CSS Border Images use nine-slice scaling: each destination border segment
/// maps to a source image slice. PDF image XObjects have fixed pixel data, so
/// source cropping is normalized before resource emission:
/// <https://www.w3.org/TR/css-backgrounds-3/#border-images> and ISO
/// 32000-1:2008, 8.9 "Images".
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RenderedImageSourceRect {
    pub x: u32,
    pub y: u32,
    pub width: u32,
    pub height: u32,
}

impl RenderedImageSourceRect {
    pub fn x(&self) -> u32 {
        self.x
    }

    pub fn y(&self) -> u32 {
        self.y
    }

    pub fn width(&self) -> u32 {
        self.width
    }

    pub fn height(&self) -> u32 {
        self.height
    }
}