ferri-pdf 0.1.8

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
//! Filter Effects 1 §5 `filter`. Chains of colour matrices that PDF paints exactly stay
//! vector: `grayscale()`, `invert()`, `opacity()`, and `brightness()`, `contrast()` and
//! `saturate()` up to 100%, recolour the group's paints. Every other chain, and any group
//! with paints a matrix cannot recolour, renders to pixels in the group's own coordinates;
//! each function or referenced `filter` element then runs on them as its §13.1 equivalent
//! primitive through the SVG renderer's filter implementation, and the result draws in place:
//! `blur()`, `drop-shadow()`, `sepia()` and `hue-rotate()` other than zero, and
//! `brightness()`, `contrast()` and `saturate()` above 100%, whose results leave the
//! channel range and are clamped per pixel.
use crate::graphics::{FilterFunction, FilterPaint, POINTS_PER_PIXEL, Rect};
use crate::paint::{
    ColorMatrix as ColorMatrixValues, IDENTITY_MATRIX, alpha_scale, compose, preserves_unit_range,
    transform_color,
};
use crate::svg::usvg::filter::{
    ColorInterpolation, ColorMatrix, ColorMatrixKind, DropShadow, Filter, GaussianBlur, Input,
    Kind, Primitive,
};
use crate::writer::{
    PdfError,
    display::{Painter, Space},
    painting, raster,
};
use krilla::surface::Surface;
use std::sync::Arc;

/// The primitive result name of single-primitive function filters.
const RESULT: &str = "result";
/// Pixels a lighting primitive's surface normal kernel reads around each pixel.
const LIGHTING_KERNEL_RADIUS: f32 = 1.0;

/// One entry of the filter chain, applied to the previous entry's output.
pub(super) enum Step<'f> {
    Function(&'f FilterFunction),
    /// A referenced `filter` element; its user units are CSS pixels.
    Reference(Arc<Filter>),
}

pub(super) fn draw(
    painter: &mut Painter<'_, '_, '_>,
    filter: &FilterPaint,
    items: &[crate::graphics::DisplayItem],
    space: Space,
    surface: &mut Surface<'_>,
) -> Result<(), PdfError> {
    if filter.bounds.is_none()
        && filter
            .functions
            .iter()
            .any(|function| matches!(function, FilterFunction::Url(_)))
    {
        return Err(PdfError::UnplacedEffect);
    }
    // §5: a reference to anything but a filter element ignores the whole chain.
    let Some(steps) = steps(filter, painter.raster.definitions) else {
        return painter.draw(items, space, surface);
    };
    if let Some((matrix, alpha)) = color_chain(&steps)
        && let Some((items, lines)) = recolor(painter, items, &matrix)?
    {
        return draw_recolored(painter, &items, &lines, alpha, space, surface);
    }
    let visible = painting::local_bounds(space.viewport, surface)?;
    let density = painter.raster.pixels_per_point
        * space.scale
        * painting::transform_scale(surface.cur_transform());
    // §13.2: the region covers the visible content and the ink the chain moves into it.
    let spread: f32 = steps.iter().map(|step| step.spread(density)).sum();
    let area = expand(visible, spread);
    let Some(raster) = raster::rasterize(painter, items, area, density)? else {
        return Ok(());
    };
    // Only pixels within the chain's reach of the ink, or inside a referenced filter's region
    // where its primitives may generate content, can change.
    let reach = raster.ink().map(|ink| expand(ink, spread));
    let reach = steps
        .iter()
        .filter_map(Step::region)
        .fold(reach, |reach, region| {
            Some(reach.map_or(region, |reach| reach.union(region)))
        });
    let Some(mut raster) = reach.and_then(|reach| raster.cropped(reach)) else {
        return Ok(());
    };
    apply(painter, &steps, &mut raster)?;
    match raster
        .ink()
        .and_then(|ink| raster.cropped(ink.intersection(visible)))
    {
        Some(raster) => raster::embed(&raster, surface),
        None => Ok(()),
    }
}

/// Runs the chain on `raster`'s pixels in place; `painter` draws the CSS mask layers of SVG
/// content that referenced filters' images show.
pub(super) fn apply(
    painter: &mut Painter<'_, '_, '_>,
    steps: &[Step<'_>],
    raster: &mut raster::Raster,
) -> Result<(), PdfError> {
    let transform = raster.transform();
    let layer_masks = crate::writer::masks::SvgLayerMasks::new(painter, raster.density, &[]);
    let applied = steps.iter().try_for_each(|step| {
        match step {
            Step::Function(function) => {
                let filter = function_filter(function, raster.area)?;
                crate::svg::resvg::filter::apply(&filter, transform, &mut raster.pixmap, None);
            }
            Step::Reference(filter) => crate::svg::resvg::filter::apply(
                filter,
                transform.pre_scale(POINTS_PER_PIXEL, POINTS_PER_PIXEL),
                &mut raster.pixmap,
                Some(&layer_masks),
            ),
        }
        Ok(())
    });
    layer_masks.finish()?;
    applied
}

pub(super) fn expand(rect: Rect, by: f32) -> Rect {
    Rect {
        x: rect.x - by,
        y: rect.y - by,
        width: rect.width + 2.0 * by,
        height: rect.height + 2.0 * by,
    }
}

fn scaled(rect: crate::svg::usvg::NonZeroRect, factor: f32) -> Rect {
    Rect {
        x: rect.x() * factor,
        y: rect.y() * factor,
        width: rect.width() * factor,
        height: rect.height() * factor,
    }
}

/// A chain of colour-matrix functions that each keep every channel in `0..=1` and leave
/// alpha to a constant factor, composed, with that factor. Such a chain commutes with the
/// group's compositing, so it applies exactly to each paint's colour and, for alpha, to the
/// group as a constant opacity: the result stays vector, text included.
pub(super) fn color_chain(steps: &[Step<'_>]) -> Option<(ColorMatrixValues, f32)> {
    let matrix = steps
        .iter()
        .try_fold(IDENTITY_MATRIX, |chain, step| match step {
            Step::Function(function) => function
                .matrix()
                .filter(preserves_unit_range)
                .map(|matrix| compose(&chain, &matrix)),
            Step::Reference(_) => None,
        })?;
    alpha_scale(&matrix).map(|alpha| (matrix, alpha))
}

fn draw_recolored(
    painter: &mut Painter<'_, '_, '_>,
    items: &[crate::graphics::DisplayItem],
    lines: &[crate::layout::TextLine],
    alpha: f32,
    space: Space,
    surface: &mut Surface<'_>,
) -> Result<(), PdfError> {
    let opacity = krilla::num::NormalizedF32::new(alpha).ok_or(PdfError::InvalidAlpha(alpha))?;
    let translucent = opacity != krilla::num::NormalizedF32::ONE;
    if translucent {
        surface.push_opacity(opacity);
    }
    let mut recolored = Painter {
        lines,
        page: painter.page,
        page_area: painter.page_area,
        text: painter.text,
        fonts: painter.fonts,
        images: painter.images,
        raster: painter.raster,
        prepared: crate::writer::backdrops::Prepared::default(),
    };
    let drawn = if translucent {
        let nested = Space {
            viewport: painting::local_bounds(space.viewport, surface)?,
            scale: space.scale * painting::transform_scale(surface.cur_transform()),
        };
        recolored.draw(items, nested, surface)
    } else {
        recolored.draw(items, space, surface)
    };
    if translucent {
        surface.pop();
    }
    drawn
}

/// Display items and the text lines they draw.
type Recolored = (
    Vec<crate::graphics::DisplayItem>,
    Vec<crate::layout::TextLine>,
);

/// `items` and the text lines they draw with every colour mapped by `matrix`, or `None` when
/// something paints colours the matrix cannot reach exactly: images, gradients and border
/// images, three-dimensional border styles, whose shades scale the mapped colour, blending,
/// nested filters, backdrops, which read unmapped colours, and bitmap or SVG colour glyphs.
fn recolor(
    painter: &Painter<'_, '_, '_>,
    items: &[crate::graphics::DisplayItem],
    matrix: &ColorMatrixValues,
) -> Result<Option<Recolored>, PdfError> {
    use crate::graphics::DisplayItem;
    let map = |color: crate::graphics::Color| map_color(matrix, color);
    let mut lines = Vec::new();
    let mut mapped = Vec::with_capacity(items.len());
    for item in items {
        mapped.push(match item {
            DisplayItem::Box(paint) => match recolor_box(paint, &map) {
                Some(paint) => DisplayItem::Box(paint),
                None => return Ok(None),
            },
            DisplayItem::Text { line } => {
                let source = painter
                    .lines
                    .get(*line)
                    .ok_or(PdfError::InvalidTextReference {
                        page: painter.page,
                        line: *line,
                    })?;
                if !painter.text.paints_run_colors(&source.runs)? {
                    return Ok(None);
                }
                let mut line = source.clone();
                line.color = map(line.color);
                recolor_runs(&mut line.runs, &map);
                lines.push(line);
                DisplayItem::Text {
                    line: lines.len() - 1,
                }
            }
            DisplayItem::Decoration(decoration) => {
                let mut decoration = *decoration;
                decoration.color = map(decoration.color);
                DisplayItem::Decoration(decoration)
            }
            // A text shadow paints only its colour through the runs' silhouettes.
            DisplayItem::TextShadow(shadow) => {
                let mut shadow = shadow.clone();
                shadow.color = map(shadow.color);
                DisplayItem::TextShadow(shadow)
            }
            item @ (DisplayItem::PushClip(_)
            | DisplayItem::PushTransform(_)
            | DisplayItem::PushProjection(_)
            | DisplayItem::PushOpacity(_)
            | DisplayItem::Pop) => item.clone(),
            item @ DisplayItem::PushBackdropRoot => item.clone(),
            DisplayItem::Frame(_)
            | DisplayItem::Scene(_)
            | DisplayItem::Image(_)
            | DisplayItem::Backdrop(_)
            | DisplayItem::PushBlend(_)
            | DisplayItem::PushFilter(_)
            | DisplayItem::PushMask(_) => {
                return Ok(None);
            }
        });
    }
    Ok(Some((mapped, lines)))
}

/// Glyph fills, strokes, palettes and emphasis marks of `runs` mapped by `map`.
fn recolor_runs(
    runs: &mut [crate::text::GlyphRun],
    map: &impl Fn(crate::graphics::Color) -> crate::graphics::Color,
) {
    for run in runs {
        run.color = map(run.color);
        if let Some(palette) = &mut run.palette {
            *palette = palette.iter().copied().map(map).collect();
        }
        if let Some(stroke) = &mut run.stroke {
            stroke.color = map(stroke.color);
        }
        for mark in &mut run.mark_shapes {
            mark.color = map(mark.color);
        }
        recolor_runs(&mut run.marks, map);
    }
}

fn recolor_box(
    paint: &crate::graphics::BoxPaint,
    map: &impl Fn(crate::graphics::Color) -> crate::graphics::Color,
) -> Option<crate::graphics::BoxPaint> {
    use crate::graphics::{BorderStyle, OutlineStyle};
    let shaded = |style: BorderStyle| {
        matches!(
            style,
            BorderStyle::Groove | BorderStyle::Ridge | BorderStyle::Inset | BorderStyle::Outset
        )
    };
    let decoration = &paint.decoration;
    if decoration.backgrounds.has_images()
        || decoration.border_image.is_drawn()
        || decoration.borders.iter().any(|border| shaded(border.style))
        || matches!(decoration.outline.style, OutlineStyle::Line(style) if shaded(style))
    {
        return None;
    }
    let mut paint = paint.clone();
    let decoration = &mut paint.decoration;
    decoration.background = map(decoration.background);
    for border in &mut decoration.borders {
        border.color = map(border.color);
    }
    decoration.outline.color = map(decoration.outline.color);
    decoration.shadows = Arc::new(
        decoration
            .shadows
            .iter()
            .map(|shadow| crate::paint::BoxShadow {
                color: map(shadow.color),
                ..*shadow
            })
            .collect(),
    );
    Some(paint)
}

/// A colour's channels mapped by `matrix`; its alpha is left to the group.
pub(super) fn map_color(
    matrix: &ColorMatrixValues,
    color: crate::graphics::Color,
) -> crate::graphics::Color {
    const CHANNEL_MAX: f32 = 255.0;
    let [red, green, blue] = color.rgb.map(|channel| f32::from(channel) / CHANNEL_MAX);
    let mapped = transform_color(matrix, [red, green, blue, color.alpha]);
    crate::graphics::Color {
        rgb: [mapped[0], mapped[1], mapped[2]]
            .map(|channel| (channel.clamp(0.0, 1.0) * CHANNEL_MAX).round() as u8),
        alpha: color.alpha,
    }
}

/// The chain's steps, or `None` when a reference does not name a usable filter element.
pub(super) fn steps<'f>(
    filter: &'f FilterPaint,
    definitions: &crate::document::svg::SvgDefinitions,
) -> Option<Vec<Step<'f>>> {
    filter
        .functions
        .iter()
        .map(|function| match function {
            FilterFunction::Url(reference) => {
                crate::svg::referenced_filter(reference, filter.bounds, definitions)
                    .map(Step::Reference)
            }
            function => Some(Step::Function(function)),
        })
        .collect()
}

impl Step<'_> {
    /// How far the step moves ink, in drawing units, at `density` pixels per unit.
    /// The region of a referenced filter element, where its primitives may generate content.
    pub(super) fn region(&self) -> Option<Rect> {
        match self {
            Self::Reference(filter) => Some(scaled(filter.rect(), POINTS_PER_PIXEL)),
            Self::Function(_) => None,
        }
    }
    pub(super) fn spread(&self, density: f32) -> f32 {
        match self {
            Self::Function(function) => function.spread(),
            Self::Reference(filter) => filter
                .primitives()
                .iter()
                .map(|primitive| primitive_spread(primitive, filter.rect(), density))
                .sum::<f32>(),
        }
    }
}

/// A primitive's reach in drawing units; its own units are CSS pixels, and kernels that
/// address whole pixels reach as far at `density`.
fn primitive_spread(
    primitive: &Primitive,
    region: crate::svg::usvg::NonZeroRect,
    density: f32,
) -> f32 {
    let pixel = 1.0 / density;
    let blur = |x: strict_num::PositiveF32, y: strict_num::PositiveF32| {
        crate::paint::BLUR_EXTENT * x.get().max(y.get()) * POINTS_PER_PIXEL
    };
    match primitive.kind() {
        Kind::GaussianBlur(blur_kind) => blur(blur_kind.std_dev_x, blur_kind.std_dev_y),
        Kind::DropShadow(shadow) => {
            shadow.dx.abs().max(shadow.dy.abs()) * POINTS_PER_PIXEL
                + blur(shadow.std_dev_x, shadow.std_dev_y)
        }
        Kind::Offset(offset) => offset.dx.abs().max(offset.dy.abs()) * POINTS_PER_PIXEL,
        Kind::Morphology(morphology) => {
            morphology.radius_x.get().max(morphology.radius_y.get()) * POINTS_PER_PIXEL
        }
        Kind::DisplacementMap(map) => map.scale.abs() / 2.0 * POINTS_PER_PIXEL,
        Kind::ConvolveMatrix(convolve) => {
            convolve.matrix.columns.max(convolve.matrix.rows) as f32 * pixel
        }
        Kind::DiffuseLighting(_) | Kind::SpecularLighting(_) => LIGHTING_KERNEL_RADIUS * pixel,
        // Tiling repeats the input anywhere in the filter region.
        Kind::Tile(_) => region.width().max(region.height()) * POINTS_PER_PIXEL,
        Kind::Blend(_)
        | Kind::ColorMatrix(_)
        | Kind::ComponentTransfer(_)
        | Kind::Composite(_)
        | Kind::Flood(_)
        | Kind::Image(_)
        | Kind::Merge(_)
        | Kind::Turbulence(_) => 0.0,
    }
}

/// §13.1: a function as its one-primitive filter over `area`, operating in sRGB (§5).
pub(super) fn function_filter(function: &FilterFunction, area: Rect) -> Result<Filter, PdfError> {
    let rect = crate::svg::usvg::NonZeroRect::from_xywh(area.x, area.y, area.width, area.height)
        .ok_or(PdfError::InvalidGeometry)?;
    let deviation =
        |value: f32| strict_num::PositiveF32::new(value).ok_or(PdfError::InvalidGeometry);
    let kind = match *function {
        FilterFunction::Blur(value) => Kind::GaussianBlur(GaussianBlur {
            input: Input::SourceGraphic,
            std_dev_x: deviation(value)?,
            std_dev_y: deviation(value)?,
        }),
        FilterFunction::DropShadow {
            offset: [dx, dy],
            deviation: value,
            color,
        } => Kind::DropShadow(DropShadow {
            input: Input::SourceGraphic,
            dx,
            dy,
            std_dev_x: deviation(value)?,
            std_dev_y: deviation(value)?,
            color: crate::svg::usvg::Color::new_rgb(color.rgb[0], color.rgb[1], color.rgb[2]),
            opacity: crate::svg::usvg::Opacity::new_clamped(color.alpha),
        }),
        ref function => Kind::ColorMatrix(ColorMatrix {
            input: Input::SourceGraphic,
            kind: ColorMatrixKind::Matrix(
                function.matrix().ok_or(PdfError::InvalidGeometry)?.to_vec(),
            ),
        }),
    };
    Ok(Filter {
        id: crate::svg::usvg::NonEmptyString::new(RESULT.into())
            .ok_or(PdfError::InvalidGeometry)?,
        rect,
        primitives: vec![Primitive {
            rect,
            color_interpolation: ColorInterpolation::SRGB,
            result: RESULT.into(),
            kind,
        }],
    })
}