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
use crate::fonts::{FontBook, FontId};
use crate::graphics::Projection;
use crate::layout::TextLine;
use crate::text::{GlyphRun, PositionedGlyph};
use crate::writer::PdfError;
use krilla::{
    geom::{Point, Transform},
    surface::Surface,
    text::{Font, Glyph, GlyphId, PaletteColor, Tag},
};
use std::collections::HashMap;
use std::ops::Range;

struct PdfGlyph<'a> {
    glyph: &'a PositionedGlyph,
    advance: f32,
    orientation: crate::text::GlyphOrientation,
    scale: f32,
}
impl Glyph for PdfGlyph<'_> {
    fn glyph_id(&self) -> GlyphId {
        GlyphId::new(self.glyph.id)
    }
    fn text_range(&self) -> Range<usize> {
        self.glyph.text_range.clone()
    }
    fn x_advance(&self, size: f32) -> f32 {
        self.orientation
            .font_vector([self.advance, self.glyph.y_advance])[0]
            * size
            / self.scale
    }
    fn x_offset(&self, size: f32) -> f32 {
        self.orientation
            .font_vector([self.glyph.x_offset, self.glyph.y_offset])[0]
            * size
            / self.scale
    }
    // Krilla's vertical offsets and advances point up, as OpenType's do; positioned glyphs'
    // point down.
    fn y_offset(&self, size: f32) -> f32 {
        -self
            .orientation
            .font_vector([self.glyph.x_offset, self.glyph.y_offset])[1]
            * size
    }
    fn y_advance(&self, size: f32) -> f32 {
        -self
            .orientation
            .font_vector([self.advance, self.glyph.y_advance])[1]
            * size
    }
    fn location(&self) -> Option<krilla::surface::Location> {
        None
    }
}

/// A face's variation coordinates and palette entries, which select distinct PDF fonts.
type InstanceKey = (FontId, Vec<([u8; 4], u32)>, Option<Vec<PaletteColor>>);

const CHANNEL_MAX: f32 = 255.0;

/// OpenType tables holding glyphs drawn in their own fixed colours: bitmaps and SVG.
const FIXED_COLOR_TABLES: [&[u8; 4]; 3] = [b"sbix", b"CBDT", b"SVG "];

/// A `font-palette` colour as a krilla palette entry; colours are already sRGB.
pub(crate) fn palette_color(color: crate::graphics::Color) -> Result<PaletteColor, PdfError> {
    let alpha = (color.alpha * CHANNEL_MAX).round();
    if !(0.0..=CHANNEL_MAX).contains(&alpha) {
        return Err(PdfError::InvalidAlpha(color.alpha));
    }
    let [red, green, blue] = color.rgb;
    Ok([red, green, blue, alpha as u8])
}

pub(super) struct TextWriter<'a> {
    fonts: &'a FontBook,
    data: Vec<Option<krilla::Data>>,
    embedded: HashMap<InstanceKey, Font>,
    /// A number of a run left out since the last [`Self::take_unwritable`], which PDF cannot
    /// write.
    unwritable: Option<f64>,
}
impl<'a> TextWriter<'a> {
    pub(crate) fn new(fonts: &'a FontBook) -> Self {
        Self {
            fonts,
            data: vec![None; fonts.faces().len()],
            embedded: HashMap::new(),
            unwritable: None,
        }
    }
    /// A number of a run left out since the last call, which PDF cannot write.
    pub(crate) fn take_unwritable(&mut self) -> Option<f64> {
        self.unwritable.take()
    }
    /// Whether every colour `runs` paint comes from their colours, strokes, palettes and
    /// marks, which excludes bitmap and SVG colour glyphs.
    pub(crate) fn paints_run_colors(&self, runs: &[GlyphRun]) -> Result<bool, PdfError> {
        for run in runs {
            let face = self
                .fonts
                .face(run.font_id)
                .ok_or(PdfError::InvalidFontId(run.font_id))?;
            let data = face.data().ok_or(PdfError::InvalidFont)?;
            let parsed = ttf_parser::RawFace::parse(data, 0).map_err(|_| PdfError::InvalidFont)?;
            if FIXED_COLOR_TABLES
                .iter()
                .any(|tag| parsed.table(ttf_parser::Tag::from_bytes(tag)).is_some())
                || !self.paints_run_colors(&run.marks)?
            {
                return Ok(false);
            }
        }
        Ok(true)
    }
    pub(crate) fn draw(
        &mut self,
        line: &TextLine,
        surface: &mut Surface<'_>,
    ) -> Result<(), PdfError> {
        self.draw_line(line, Plane::Flat, surface)
    }
    /// Draws `line` under `projection`, each cluster of glyphs through the projection's tangent
    /// at the cluster's ink centre; the line's runs must all have clusters.
    pub(crate) fn draw_projected(
        &mut self,
        line: &TextLine,
        projection: &Projection,
        surface: &mut Surface<'_>,
    ) -> Result<(), PdfError> {
        self.draw_line(line, Plane::Projected(projection), surface)
    }
    fn draw_line(
        &mut self,
        line: &TextLine,
        plane: Plane<'_>,
        surface: &mut Surface<'_>,
    ) -> Result<(), PdfError> {
        let origin = [line.x, line.baseline];
        if line.runs.iter().any(|run| run.rtl || run.has_bidi_scopes()) {
            surface.with_actual_text(&line.text, |surface| {
                self.draw_runs(&line.runs, origin, Paint::Text, plane, surface)
            })
        } else {
            self.draw_runs(&line.runs, origin, Paint::Text, plane, surface)
        }
    }
    /// The outlines of `runs` placed from their line's left edge and baseline `origin`, with
    /// their strokes, filled opaquely as paths: the shape they cast as a text shadow.
    pub(crate) fn draw_silhouette(
        &mut self,
        runs: &[GlyphRun],
        origin: [f32; 2],
        surface: &mut Surface<'_>,
    ) -> Result<(), PdfError> {
        self.draw_runs(runs, origin, Paint::Silhouette, Plane::Flat, surface)
    }
    /// The clusters of `run` drawn from `pen` on a line from `origin`, bounded by their glyphs'
    /// outlines where the face has no colour glyphs.
    pub(crate) fn clusters(
        &self,
        run: &GlyphRun,
        origin: [f32; 2],
        pen: [f32; 2],
    ) -> Result<Option<Vec<crate::text::Cluster>>, PdfError> {
        let face = self
            .fonts
            .face(run.font_id)
            .ok_or(PdfError::InvalidFontId(run.font_id))?;
        let data = face.data().ok_or(PdfError::InvalidFont)?;
        let outlines =
            crate::fonts::Silhouettes::new(data, &run.variations).ok_or(PdfError::InvalidFont)?;
        Ok(run.clusters(origin, pen, |glyph| outlines.outline_box(glyph)))
    }
    fn font(&mut self, run: &GlyphRun) -> Result<Font, PdfError> {
        let palette = run
            .palette
            .as_deref()
            .map(|colors| colors.iter().copied().map(palette_color).collect())
            .transpose()?;
        let key = (
            run.font_id,
            run.variations
                .iter()
                .map(|variation| (variation.tag, variation.value.to_bits()))
                .collect(),
            palette,
        );
        if let Some(font) = self.embedded.get(&key) {
            return Ok(font.clone());
        }
        let data = self
            .data
            .get_mut(run.font_id.index())
            .ok_or(PdfError::InvalidFontId(run.font_id))?;
        if data.is_none() {
            let face = self
                .fonts
                .face(run.font_id)
                .ok_or(PdfError::InvalidFontId(run.font_id))?;
            *data = Some(face.data().ok_or(PdfError::InvalidFont)?.to_vec().into());
        }
        let coordinates: Vec<_> = run
            .variations
            .iter()
            .map(|variation| (Tag::new(&variation.tag), variation.value))
            .collect();
        let font = Font::new_variable_with_palette(
            data.as_ref().unwrap().clone(),
            0,
            &coordinates,
            key.2.clone(),
        )
        .ok_or(PdfError::InvalidFont)?;
        self.embedded.insert(key, font.clone());
        Ok(font)
    }
    fn draw_runs(
        &mut self,
        runs: &[GlyphRun],
        origin: [f32; 2],
        paint: Paint,
        plane: Plane<'_>,
        surface: &mut Surface<'_>,
    ) -> Result<(), PdfError> {
        let mut glyphs = Vec::new();
        for run in runs {
            if run.font_size == 0.0 {
                continue;
            }
            // A run placed past what PDF writes is left out, not the rest of its line.
            let placed = [origin[0] + run.x, origin[1] + run.baseline_shift];
            if let Some(number) = crate::writer::numbers::unwritable_number(&(run, placed)) {
                self.unwritable.get_or_insert(number);
                continue;
            }
            let font = self.font(run)?;
            glyphs.clear();
            for glyph in run.glyphs() {
                glyphs.push(PdfGlyph {
                    glyph,
                    orientation: run.orientation,
                    scale: run.inline_scale,
                    advance: glyph
                        .normalized_advance(run.font_size)
                        .ok_or(PdfError::InvalidGeometry)?,
                });
            }
            for [x, y] in run.copies(origin) {
                let oriented = run.orientation != crate::text::GlyphOrientation::Horizontal
                    || run.inline_scale != 1.0;
                if oriented {
                    let [a, b, c, d] = run.orientation.matrix();
                    let (a, b) = (a * run.inline_scale, b * run.inline_scale);
                    surface.push_transform(&Transform::from_row(
                        a,
                        b,
                        c,
                        d,
                        x - a * x - c * y,
                        y - b * x - d * y,
                    ));
                }
                let shear = run.shear(y);
                let sheared =
                    |surface: &mut Surface<'_>, pen: [f32; 2], glyphs: &[PdfGlyph<'_>]| {
                        if let Some([a, b, c, d, e, f]) = shear {
                            surface.push_transform(&Transform::from_row(a, b, c, d, e, f));
                        }
                        let drawn = paint_glyphs(
                            surface,
                            run,
                            Point::from_xy(pen[0], pen[1]),
                            glyphs,
                            &font,
                            paint,
                        );
                        if shear.is_some() {
                            surface.pop();
                        }
                        drawn
                    };
                match plane {
                    Plane::Flat => sheared(surface, [x, y], &glyphs)?,
                    Plane::Projected(projection) => {
                        for cluster in self
                            .clusters(run, origin, [x, y])?
                            .ok_or(PdfError::InvalidGeometry)?
                        {
                            let ink = cluster.ink;
                            let centre = [ink.x + ink.width / 2.0, ink.y + ink.height / 2.0];
                            let [a, b, c, d, e, f] =
                                crate::paint::projection::tangent(*projection, centre);
                            surface.push_transform(&Transform::from_row(a, b, c, d, e, f));
                            let drawn = sheared(surface, cluster.pen, &glyphs[cluster.glyphs]);
                            surface.pop();
                            drawn?;
                        }
                    }
                }
                if oriented {
                    surface.pop();
                }
            }
            if !run.marks.is_empty() {
                let marks = if paint == Paint::Silhouette {
                    Paint::Silhouette
                } else {
                    Paint::Annotation
                };
                self.draw_runs(&run.marks, origin, marks, plane, surface)?;
            }
            if matches!(plane, Plane::Projected(_)) && !run.mark_shapes.is_empty() {
                return Err(PdfError::InvalidGeometry);
            }
            for mark in &run.mark_shapes {
                let color = if paint == Paint::Silhouette {
                    crate::graphics::Color::BLACK
                } else {
                    mark.color
                };
                surface.push_transform(&Transform::from_translate(origin[0], origin[1]));
                surface.set_fill(Some(krilla::paint::Fill {
                    rule: krilla::paint::FillRule::EvenOdd,
                    ..crate::writer::painting::fill(color)?
                }));
                surface.draw_path(&crate::writer::painting::contour_path(&mark.contours)?);
                surface.pop();
            }
        }
        Ok(())
    }
}

/// Where glyphs draw: in the surface's plane, or projected onto it cluster by cluster.
#[derive(Clone, Copy)]
enum Plane<'p> {
    Flat,
    Projected(&'p Projection),
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Paint {
    Text,
    /// Glyphs that are not text content, such as emphasis marks, as outline paths.
    Annotation,
    Silhouette,
}

/// Fills a run's glyphs and strokes them with `-webkit-text-stroke` as one text object, in
/// stroke-only or fill-then-stroke render mode. A PDF text object strokes and fills at one
/// opacity, so a fill and stroke of different opacities stroke the outlines separately.
/// Annotations are outline paths and silhouettes opaque ones.
fn paint_glyphs(
    surface: &mut Surface<'_>,
    run: &GlyphRun,
    origin: Point,
    glyphs: &[PdfGlyph<'_>],
    font: &Font,
    paint: Paint,
) -> Result<(), PdfError> {
    let silhouette = paint == Paint::Silhouette;
    let paint_color = |color| {
        if silhouette {
            crate::graphics::Color::BLACK
        } else {
            color
        }
    };
    let fill = crate::writer::painting::fill(paint_color(run.color))?;
    let stroke = run
        .stroke
        .map(|stroke| crate::writer::painting::stroke(paint_color(stroke.color), stroke.width))
        .transpose()?;
    let draw = |surface: &mut Surface<'_>, outlined: bool| {
        surface.draw_glyphs(
            origin,
            glyphs,
            font.clone(),
            &run.text,
            run.font_size,
            outlined,
        );
    };
    match stroke {
        None => {
            surface.set_fill(Some(fill));
            draw(surface, paint != Paint::Text);
        }
        Some(stroke) if silhouette => {
            surface.set_fill(Some(fill));
            surface.set_stroke(Some(stroke));
            draw(surface, true);
        }
        Some(stroke) if run.color.alpha == 0.0 || stroke.opacity == fill.opacity => {
            surface.set_fill((run.color.alpha > 0.0).then_some(fill));
            surface.set_stroke(Some(stroke));
            draw(surface, false);
        }
        Some(stroke) => {
            surface.set_fill(Some(fill));
            draw(surface, false);
            surface.set_fill(None);
            surface.set_stroke(Some(stroke));
            draw(surface, true);
        }
    }
    surface.set_stroke(None);
    Ok(())
}