ferri-pdf 0.1.7

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
//! CSS Transforms 2 §4.2 layers under a perspective whose `w` varies across them, which no
//! PDF content matrix expresses. Text stays text: each cluster of glyphs draws through the
//! projection's tangent at its centre wherever that strays less than the resampling tolerance
//! from the exact projection at the output raster density. Every other paint renders to
//! pixels in the layer's own plane and is resampled through the projection onto output
//! pixels. Groups that composite with what lies behind them, and groups holding vector text,
//! open in place around their projected content; the rest render to pixels with their content,
//! in the layer's plane, as filters and masks apply. Additive groups (Compositing 2 §9.1.14,
//! §9.1.15) composite in the plane of their enclosing group, whose content before them is
//! their backdrop.
use crate::graphics::{BlendMode, Clip, DisplayItem, Projection, Rect};
use crate::layout::TextLine;
use crate::paint::{
    projection,
    resampling::{Band, Plane, PlaneError, Resampled, horizon_square},
};
use crate::writer::{
    PdfError, bounds,
    display::{Painter, Space, group_length},
    painting,
    raster::{self, Raster},
};
use krilla::{geom::Transform, num::NormalizedF32, paint::FillRule, surface::Surface};

impl From<PlaneError> for PdfError {
    fn from(error: PlaneError) -> Self {
        match error {
            PlaneError::Size { width, height } => Self::RasterSize { width, height },
            PlaneError::Degenerate => Self::InvalidGeometry,
            PlaneError::Pixmap => Self::Raster,
        }
    }
}

/// Draws `items` through `projection`, which maps their plane into the current drawing space.
pub(super) fn draw(
    painter: &mut Painter<'_, '_, '_>,
    projection: Projection,
    items: &[DisplayItem],
    space: Space,
    surface: &mut Surface<'_>,
) -> Result<(), PdfError> {
    let visible = painting::local_bounds(space.viewport, surface)?;
    let plane = Plane::new(projection, visible, painter.density(space, surface));
    Layer {
        plane: plane.clone(),
        group: Group { items, plane },
        to_group: projection::IDENTITY,
    }
    .draw(painter, items, surface)
}

/// The innermost transparency group open in a layer: its content, whose items before an
/// additive group are that group's backdrop, and its plane.
#[derive(Clone)]
struct Group<'i> {
    items: &'i [DisplayItem],
    plane: Plane,
}

/// A plane under a projection into the drawing space.
struct Layer<'i> {
    plane: Plane,
    group: Group<'i>,
    /// The map from this plane to its group's.
    to_group: Projection,
}

impl Layer<'_> {
    /// The plane of content that `m` maps into this one.
    fn mapped(&self, m: Projection) -> Self {
        Self {
            plane: self.plane.mapped(m),
            group: self.group.clone(),
            to_group: projection::compose(self.to_group, m),
        }
    }

    /// This plane holding the transparency group whose content is `items`.
    fn grouped<'g>(&self, items: &'g [DisplayItem]) -> Layer<'g> {
        Layer {
            plane: self.plane.clone(),
            group: Group {
                items,
                plane: self.plane.clone(),
            },
            to_group: projection::IDENTITY,
        }
    }

    fn draw(
        &self,
        painter: &mut Painter<'_, '_, '_>,
        items: &[DisplayItem],
        surface: &mut Surface<'_>,
    ) -> Result<(), PdfError> {
        let mut run = 0;
        let mut index = 0;
        while let Some(item) = items.get(index) {
            match item {
                DisplayItem::Text { line }
                    if self.vector(painter, line_at(painter.lines, *line)?)? =>
                {
                    self.rasterize(painter, &items[run..index], surface)?;
                    let line = line_at(painter.lines, *line)?;
                    let drawn = painter
                        .text
                        .draw_projected(line, &self.plane.projection, surface);
                    painter.record_unwritable_runs();
                    drawn?;
                    index += 1;
                    run = index;
                }
                DisplayItem::Pop => return Err(PdfError::InvalidGeometry),
                DisplayItem::PushClip(_)
                | DisplayItem::PushTransform(_)
                | DisplayItem::PushProjection(_)
                | DisplayItem::PushOpacity(_)
                | DisplayItem::PushBlend(_)
                | DisplayItem::PushFilter(_)
                | DisplayItem::PushMask(_)
                | DisplayItem::PushBackdropRoot => {
                    let end = index + 1 + group_length(&items[index + 1..])?;
                    let content = &items[index + 1..end];
                    if self.opens(painter, item, content)? {
                        self.rasterize(painter, &items[run..index], surface)?;
                        self.open(painter, item, content, item, surface)?;
                        run = end + 1;
                    }
                    index = end + 1;
                }
                DisplayItem::Text { .. }
                | DisplayItem::Image(_)
                | DisplayItem::Frame(_)
                | DisplayItem::Scene(_)
                | DisplayItem::Box(_)
                | DisplayItem::Decoration(_)
                | DisplayItem::TextShadow(_)
                | DisplayItem::Backdrop(_) => index += 1,
            }
        }
        self.rasterize(painter, &items[run..], surface)
    }

    /// Whether the group `push` opens around `content` must open in place: it composites with
    /// what lies behind it, or holds vector text or such a group outside an isolated one.
    fn opens(
        &self,
        painter: &Painter<'_, '_, '_>,
        push: &DisplayItem,
        content: &[DisplayItem],
    ) -> Result<bool, PdfError> {
        Ok(match push {
            DisplayItem::PushBlend(mode) if *mode != BlendMode::Normal => true,
            DisplayItem::PushBackdropRoot => hoisted_blend(content)?.is_some(),
            DisplayItem::PushFilter(_) | DisplayItem::PushMask(_) => false,
            DisplayItem::PushTransform(matrix) => self
                .mapped(projection::from_affine(*matrix))
                .holds_open(painter, content)?,
            DisplayItem::PushProjection(matrix) => {
                self.mapped(*matrix).holds_open(painter, content)?
            }
            _ => self.holds_open(painter, content)?,
        })
    }

    /// Whether `items` hold vector text or a group that must open in place.
    fn holds_open(
        &self,
        painter: &Painter<'_, '_, '_>,
        items: &[DisplayItem],
    ) -> Result<bool, PdfError> {
        let mut index = 0;
        while let Some(item) = items.get(index) {
            index += 1;
            match item {
                DisplayItem::Text { line } => {
                    if self.vector(painter, line_at(painter.lines, *line)?)? {
                        return Ok(true);
                    }
                }
                DisplayItem::PushClip(_)
                | DisplayItem::PushTransform(_)
                | DisplayItem::PushProjection(_)
                | DisplayItem::PushOpacity(_)
                | DisplayItem::PushBlend(_)
                | DisplayItem::PushFilter(_)
                | DisplayItem::PushMask(_)
                | DisplayItem::PushBackdropRoot => {
                    let end = index + group_length(&items[index..])?;
                    if self.opens(painter, item, &items[index..end])? {
                        return Ok(true);
                    }
                    index = end + 1;
                }
                _ => {}
            }
        }
        Ok(false)
    }

    /// Opens the group `push` around its projected `content`, which item `at` of the walked
    /// display list opens.
    fn open(
        &self,
        painter: &mut Painter<'_, '_, '_>,
        push: &DisplayItem,
        content: &[DisplayItem],
        at: &DisplayItem,
        surface: &mut Surface<'_>,
    ) -> Result<(), PdfError> {
        match push {
            DisplayItem::PushTransform(matrix) => self
                .mapped(projection::from_affine(*matrix))
                .draw(painter, content, surface),
            DisplayItem::PushProjection(matrix) => {
                self.mapped(*matrix).draw(painter, content, surface)
            }
            DisplayItem::PushOpacity(alpha) => {
                let opacity = NormalizedF32::new(*alpha).ok_or(PdfError::InvalidAlpha(*alpha))?;
                surface.push_opacity(opacity);
                let drawn = self.grouped(content).draw(painter, content, surface);
                surface.pop();
                drawn
            }
            DisplayItem::PushBlend(mode @ (BlendMode::PlusDarker | BlendMode::PlusLighter)) => {
                self.additive(painter, *mode, content, at, surface)
            }
            DisplayItem::PushBlend(mode) => {
                let pops = painting::push_blend(*mode, surface)?;
                let drawn = self.grouped(content).draw(painter, content, surface);
                painting::pop_clips(pops, surface);
                drawn
            }
            DisplayItem::PushClip(clip) => {
                self.push_clip(clip, surface)?;
                let drawn = self.draw(painter, content, surface);
                surface.pop();
                drawn
            }
            DisplayItem::PushBackdropRoot => {
                let (mode, inner) = hoisted_blend(content)?.ok_or(PdfError::InvalidGeometry)?;
                // The root only marks where backdrops start, so it opens inside the blend.
                let mut root = Vec::with_capacity(inner.len() + 2);
                root.push(DisplayItem::PushBackdropRoot);
                root.extend_from_slice(inner);
                root.push(DisplayItem::Pop);
                self.open(painter, &DisplayItem::PushBlend(mode), &root, at, surface)
            }
            DisplayItem::PushFilter(_) | DisplayItem::PushMask(_) => Err(PdfError::InvalidGeometry),
            DisplayItem::Text { .. }
            | DisplayItem::Image(_)
            | DisplayItem::Frame(_)
            | DisplayItem::Scene(_)
            | DisplayItem::Box(_)
            | DisplayItem::Decoration(_)
            | DisplayItem::TextShadow(_)
            | DisplayItem::Backdrop(_)
            | DisplayItem::Pop => Err(PdfError::InvalidGeometry),
        }
    }

    /// Draws the additive `operator` group of `content`, which item `at` opens: in the plane of
    /// the enclosing group it composites with that group's content before `at`, and draws as
    /// the colour that source-over compositing onto that content turns into the result.
    fn additive(
        &self,
        painter: &mut Painter<'_, '_, '_>,
        operator: BlendMode,
        content: &[DisplayItem],
        at: &DisplayItem,
        surface: &mut Surface<'_>,
    ) -> Result<(), PdfError> {
        let prefix = &self.group.items[..self.group.offset(at)?];
        let placed: Vec<DisplayItem> = if self.to_group == projection::IDENTITY {
            content.to_vec()
        } else {
            let push = projection::affine(self.to_group).map_or(
                DisplayItem::PushProjection(self.to_group),
                DisplayItem::PushTransform,
            );
            std::iter::once(push)
                .chain(content.iter().cloned())
                .chain(std::iter::once(DisplayItem::Pop))
                .collect()
        };
        let definitions = painter.raster.definitions;
        let plane = &self.group.plane;
        let Some(ink) = bounds::ink(&placed, painter.lines, definitions, plane.density)? else {
            return Ok(());
        };
        let scale = 1.0 / painter.raster.pixels_per_point;
        let resampled = plane.render(ink, |band| -> Result<_, PdfError> {
            let Some(mut source) = band_pixels(painter, band, scale, |painter, space, surface| {
                painter.draw(&placed, space, surface)
            })?
            else {
                return Ok(None);
            };
            let backdrop = band_pixels(painter, band, scale, |painter, space, surface| {
                painter.draw_prefix(prefix, space, surface)
            })?
            .ok_or(PdfError::Raster)?;
            crate::writer::backdrops::composite_onto(operator, &mut source, &backdrop)?;
            Ok(Some(source))
        })?;
        embed_resampled(resampled, plane.density, surface)
    }

    /// Whether every cluster of `line` lies in front of the viewer and draws through the
    /// projection's tangent at its centre within the tolerance.
    fn vector(&self, painter: &Painter<'_, '_, '_>, line: &TextLine) -> Result<bool, PdfError> {
        let origin = [line.x, line.baseline];
        for run in line.runs.iter().flat_map(crate::text::GlyphRun::with_marks) {
            if !run.mark_shapes.is_empty() {
                return Ok(false);
            }
            for pen in run.copies(origin) {
                let Some(clusters) = painter.text.clusters(run, origin, pen)? else {
                    return Ok(false);
                };
                if !clusters
                    .iter()
                    .all(|cluster| self.plane.tangent_fits(cluster.ink))
                {
                    return Ok(false);
                }
            }
        }
        Ok(true)
    }

    /// Clips the drawing space to the projection of `clip`'s outline within the layer.
    fn push_clip(&self, clip: &Clip, surface: &mut Surface<'_>) -> Result<(), PdfError> {
        let mut lines = self.plane.lines.clone();
        lines.extend(clip.bounds().edges().map(|(axis, bound, lower)| {
            let sign = if lower { 1.0 } else { -1.0 };
            let mut line = [0.0; 3];
            line[axis] = sign;
            line[2] = -sign * f64::from(bound);
            line
        }));
        let polygons: Vec<Vec<[f32; 2]>> = if clip.axes == [true; 2] {
            let tolerance = self.plane.tolerance() / self.plane.largest_stretch(&lines);
            clip.contours()
                .iter()
                .map(|contour| {
                    contour
                        .polygon_points(tolerance)
                        .into_iter()
                        .map(|point| point.map(|value| value as f32))
                        .collect()
                })
                .collect()
        } else {
            vec![horizon_square()]
        };
        let contours: Vec<crate::graphics::Contour> = polygons
            .into_iter()
            .map(|polygon| projection::clip_all(polygon, &lines))
            .filter(|polygon| polygon.len() >= 3)
            .map(|polygon| {
                let projected: Vec<_> = polygon
                    .into_iter()
                    .map(|point| projection::apply(self.plane.projection, point))
                    .collect();
                crate::graphics::Contour::polygon(&projected)
            })
            .collect();
        let path = if contours.is_empty() {
            painting::rect_path(Rect {
                width: 0.0,
                height: 0.0,
                ..clip.rect
            })?
        } else {
            painting::contour_path(&contours)?
        };
        surface.push_clip_path(&path, &FillRule::NonZero);
        Ok(())
    }

    /// Renders `items` in the plane and draws them resampled through the projection.
    fn rasterize(
        &self,
        painter: &mut Painter<'_, '_, '_>,
        items: &[DisplayItem],
        surface: &mut Surface<'_>,
    ) -> Result<(), PdfError> {
        if items.is_empty() {
            return Ok(());
        }
        let definitions = painter.raster.definitions;
        let density = self.plane.density;
        let Some(ink) = bounds::ink(items, painter.lines, definitions, density)? else {
            return Ok(());
        };
        let scale = 1.0 / painter.raster.pixels_per_point;
        let resampled = self.plane.render(ink, |band| {
            band_pixels(painter, band, scale, |painter, space, surface| {
                painter.draw(items, space, surface)
            })
        })?;
        embed_resampled(resampled, density, surface)
    }
}

impl Group<'_> {
    /// The index of `item`, which must be one of the group's items.
    fn offset(&self, item: &DisplayItem) -> Result<usize, PdfError> {
        let address = std::ptr::from_ref(item) as usize;
        let first = self.items.as_ptr() as usize;
        address
            .checked_sub(first)
            .map(|bytes| bytes / size_of::<DisplayItem>())
            .filter(|&offset| offset < self.items.len())
            .ok_or(PdfError::InvalidGeometry)
    }
}

/// The pixels of what `draw` paints over `band`, in a space whose units span `scale` points.
fn band_pixels(
    painter: &mut Painter<'_, '_, '_>,
    band: &Band,
    scale: f32,
    draw: impl FnOnce(&mut Painter<'_, '_, '_>, Space, &mut Surface<'_>) -> Result<(), PdfError>,
) -> Result<Option<tiny_skia::Pixmap>, PdfError> {
    let [a, b, c, d, e, f] = band.to_pixels;
    let raster = raster::render_surface(band.area(), 1.0, |viewport, surface| {
        surface.push_transform(&Transform::from_row(a, b, c, d, e, f));
        let drawn = draw(painter, Space { viewport, scale }, surface);
        surface.pop();
        drawn
    })?;
    Ok(raster.map(|raster| raster.pixmap))
}

/// Draws `resampled` output pixels, at `density` pixels per unit, in place.
fn embed_resampled(
    resampled: Option<Resampled>,
    density: f32,
    surface: &mut Surface<'_>,
) -> Result<(), PdfError> {
    match resampled {
        Some(Resampled { pixmap, area }) => raster::embed(
            &Raster {
                pixmap,
                area,
                density,
            },
            surface,
        ),
        None => Ok(()),
    }
}

/// Draws `raster`, whose drawing space `to_target` projects into the current one, over
/// `target` at `density` pixels per unit.
pub(super) fn embed(
    raster: &Raster,
    to_target: Projection,
    target: Rect,
    density: f32,
    surface: &mut Surface<'_>,
) -> Result<(), PdfError> {
    let to_pixels = [
        raster.density,
        0.0,
        0.0,
        raster.density,
        -raster.area.x * raster.density,
        -raster.area.y * raster.density,
    ];
    let resampled = crate::paint::resampling::resample_pixmap(
        &raster.pixmap,
        to_pixels,
        to_target,
        target,
        density,
    )?;
    embed_resampled(resampled, density, surface)
}

/// The text line a display item draws.
fn line_at(lines: &[TextLine], line: usize) -> Result<&TextLine, PdfError> {
    lines.get(line).ok_or(PdfError::InvalidGeometry)
}

/// A Backdrop Root group's content that is wholly one group blending with what lies behind
/// it, as an element with both opens them: that blend and its content.
fn hoisted_blend(content: &[DisplayItem]) -> Result<Option<(BlendMode, &[DisplayItem])>, PdfError> {
    let Some(DisplayItem::PushBlend(mode)) = content.first() else {
        return Ok(None);
    };
    if *mode == BlendMode::Normal {
        return Ok(None);
    }
    let end = 1 + group_length(&content[1..])?;
    Ok((end + 1 == content.len()).then(|| (*mode, &content[1..end])))
}