ferri-pdf 0.1.11

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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// Copyright 2022 the Resvg Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::mem;
use std::sync::Arc;

use fontdb::{Database, ID};
use rustybuzz::ttf_parser;
use rustybuzz::ttf_parser::{GlyphId, RasterImageFormat};
use tiny_skia_path::{NonZeroRect, Size, Transform};

use crate::svg::usvg::*;

fn resolve_rendering_mode(text: &Text) -> ShapeRendering {
    match text.rendering_mode {
        TextRendering::OptimizeSpeed => ShapeRendering::CrispEdges,
        TextRendering::OptimizeLegibility => ShapeRendering::GeometricPrecision,
        TextRendering::GeometricPrecision => ShapeRendering::GeometricPrecision,
    }
}

fn push_outline_paths(
    span: &layout::Span,
    builder: &mut tiny_skia_path::PathBuilder,
    new_children: &mut Vec<Node>,
    rendering_mode: ShapeRendering,
    host: Option<Transform>,
) {
    let builder = mem::replace(builder, tiny_skia_path::PathBuilder::new());

    if let Some(path) = builder.finish().and_then(|p| {
        Path::new(
            String::new(),
            span.visible,
            span.fill.clone(),
            span.stroke.clone(),
            span.paint_order,
            rendering_mode,
            Arc::new(p),
            Transform::default(),
        )
    }) && let Some(path) = host_stroked(path, host)
    {
        new_children.push(Node::Path(Box::new(path)));
    }
}

/// `path`, stroked in the host coordinate space when `host`, the text's absolute transform,
/// is given for `vector-effect: non-scaling-stroke` (SVG 2 ยง13.6).
fn host_stroked(path: Path, host: Option<Transform>) -> Option<Path> {
    let Some(host) = host else {
        return Some(path);
    };
    Path::new(
        path.id,
        path.visible,
        path.fill,
        path.stroke,
        path.paint_order,
        path.rendering_mode,
        path.data,
        host,
    )?
    .with_non_scaling_stroke()
}

/// The text's outlines, their stroke bounding box, and the layers its spans' `text-shadow`
/// casts beneath them.
pub(crate) fn flatten(
    text: &mut Text,
    cache: &mut Cache,
) -> Option<(Group, NonZeroRect, Vec<Node>)> {
    let mut new_children = vec![];
    let mut shadows = vec![];

    let rendering_mode = resolve_rendering_mode(text);
    let host = text.non_scaling_stroke.then_some(text.abs_transform);

    for span in &text.layouted {
        let span_start = new_children.len();
        if let Some(path) = span.overline.as_ref() {
            let mut path = path.clone();
            path.rendering_mode = rendering_mode;
            if let Some(path) = host_stroked(path, host) {
                new_children.push(Node::Path(Box::new(path)));
            }
        }

        if let Some(path) = span.underline.as_ref() {
            let mut path = path.clone();
            path.rendering_mode = rendering_mode;
            if let Some(path) = host_stroked(path, host) {
                new_children.push(Node::Path(Box::new(path)));
            }
        }

        // Instead of always processing each glyph separately, we always collect
        // as many outline glyphs as possible by pushing them into the span_builder
        // and only if we encounter a different glyph, or we reach the very end of the
        // span to we push the actual outline paths into new_children. This way, we don't need
        // to create a new path for every glyph if we have many consecutive glyphs
        // with just outlines (which is the most common case).
        let mut span_builder = tiny_skia_path::PathBuilder::new();

        // For variable fonts, we need to extract the outline with variations applied.
        // We can't use the cache here since the outline depends on variation values.
        let has_explicit_variations = !span.variations.is_empty();
        let foreground = colr_foreground(span);

        for glyph in span
            .positioned_glyphs
            .iter()
            .flat_map(layout::PositionedGlyph::copies)
        {
            // A (best-effort conversion of a) COLR glyph.
            if let Some(tree) = cache.fontdb_colr(
                glyph.font,
                glyph.id,
                &ColrPaint {
                    variations: &span.variations,
                    font_size: glyph.font_size(),
                    optical_sizing: span.font_optical_sizing,
                    palette: glyph.paint.palette.as_deref(),
                    foreground,
                },
            ) {
                let mut group = Group {
                    transform: glyph.colr_transform(),
                    ..Group::empty()
                };
                // TODO: Probably need to update abs_transform of children?
                group.children.push(Node::Group(Box::new(tree.root)));
                group.calculate_bounding_boxes();

                new_children.push(Node::Group(Box::new(group)));
            }
            // An SVG glyph. Will return the usvg node containing the glyph descriptions.
            else if let Some(node) = cache.fontdb_svg(glyph.font, glyph.id) {
                push_outline_paths(
                    span,
                    &mut span_builder,
                    &mut new_children,
                    rendering_mode,
                    host,
                );

                let mut group = Group {
                    transform: glyph.svg_transform(),
                    ..Group::empty()
                };
                // TODO: Probably need to update abs_transform of children?
                group.children.push(node);
                group.calculate_bounding_boxes();

                new_children.push(Node::Group(Box::new(group)));
            }
            // A bitmap glyph.
            else if let Some(img) = cache.fontdb_raster(glyph.font, glyph.id) {
                push_outline_paths(
                    span,
                    &mut span_builder,
                    &mut new_children,
                    rendering_mode,
                    host,
                );

                let transform = if img.is_sbix {
                    glyph.sbix_transform(
                        img.x as f32,
                        img.y as f32,
                        img.glyph_bbox.map(|bbox| bbox.x_min).unwrap_or(0) as f32,
                        img.glyph_bbox.map(|bbox| bbox.y_min).unwrap_or(0) as f32,
                        img.pixels_per_em as f32,
                        img.image.size.height(),
                    )
                } else {
                    glyph.cbdt_transform(
                        img.x as f32,
                        img.y as f32,
                        img.pixels_per_em as f32,
                        img.image.size.height(),
                    )
                };

                let mut group = Group {
                    transform,
                    ..Group::empty()
                };
                group.children.push(Node::Image(Box::new(img.image)));
                group.calculate_bounding_boxes();

                new_children.push(Node::Group(Box::new(group)));
            } else {
                // Only bypass cache if: explicit variations OR (auto opsz AND font has opsz axis)
                let needs_variations = has_explicit_variations
                    || (span.font_optical_sizing == FontOpticalSizing::Auto
                        && cache.has_opsz_axis(glyph.font));

                let outline = if needs_variations {
                    cache.fontdb.outline_with_variations(
                        glyph.font,
                        glyph.id,
                        &span.variations,
                        glyph.font_size(),
                        span.font_optical_sizing,
                    )
                } else {
                    cache.fontdb_outline(glyph.font, glyph.id)
                };

                if let Some(outline) = outline.and_then(|p| p.transform(glyph.outline_transform()))
                {
                    span_builder.push_path(&outline);
                }
            }
        }

        push_outline_paths(
            span,
            &mut span_builder,
            &mut new_children,
            rendering_mode,
            host,
        );

        for path in [&span.line_through, &span.emphasis].into_iter().flatten() {
            let mut path = path.clone();
            path.rendering_mode = rendering_mode;
            if let Some(path) = host_stroked(path, host) {
                new_children.push(Node::Path(Box::new(path)));
            }
        }

        shadows.extend(shadow_layers(
            &span.shadows,
            &new_children[span_start..],
            cache,
        ));

        // A span composites in its own element's colour space (SVG 2 ยง13.9).
        if span.linear_light != text.linear_light && new_children.len() > span_start {
            let mut group = Group {
                linear_light: span.linear_light,
                ..Group::empty()
            };
            group.children = new_children.split_off(span_start);
            group.calculate_bounding_boxes();
            new_children.push(Node::Group(Box::new(group)));
        }
    }

    let mut group = Group {
        id: text.id.clone(),
        linear_light: text.linear_light,
        ..Group::empty()
    };

    for child in new_children {
        group.children.push(child);
    }

    group.calculate_bounding_boxes();
    let stroke_bbox = group.stroke_bounding_box().to_non_zero_rect()?;
    Some((group, stroke_bbox, shadows))
}

/// The layers `shadows` cast from `ink`, a span's flattened glyphs and decorations (CSS Text
/// Decoration 3 ยง4): each fills them opaquely in its colour, offset and blurred, with the
/// colour's alpha as the layer's opacity, so overlapping shapes composite together. The first
/// shadow is on top.
fn shadow_layers(
    shadows: &[crate::style::TextShadow],
    ink: &[Node],
    cache: &mut Cache,
) -> Vec<Node> {
    let mut layers = Vec::new();
    for shadow in shadows.iter().rev() {
        let color = shadow.color.used;
        let paint = Paint::Color(Color::new_rgb(color.rgb[0], color.rgb[1], color.rgb[2]));
        let [dx, dy] = shadow
            .offset
            .map(|length| length / crate::style::POINTS_PER_PIXEL);
        let transform = Transform::from_translate(dx, dy);
        let mut layer = Group {
            transform,
            abs_transform: transform,
            opacity: Opacity::new_clamped(color.alpha),
            children: ink
                .iter()
                .map(|node| silhouette(node, &paint, cache))
                .collect(),
            ..Group::empty()
        };
        layer.calculate_bounding_boxes();
        if shadow.blur > 0.0 {
            let cast = crate::paint::BoxShadow {
                offset: shadow.offset,
                blur: shadow.blur,
                spread: 0.0,
                color,
                inset: false,
            };
            let extent = cast.extent() / crate::style::POINTS_PER_PIXEL;
            let deviation = PositiveF32::new(cast.sigma() / crate::style::POINTS_PER_PIXEL)
                .unwrap_or(PositiveF32::ZERO);
            // The layer's bounding box is in its own coordinates, before its offset.
            let Some(region) = outset(layer.layer_bounding_box(), extent) else {
                continue;
            };
            layer.filters = vec![Arc::new(filter::Filter {
                id: cache.gen_filter_id(),
                rect: region,
                primitives: vec![filter::Primitive {
                    rect: region,
                    color_interpolation: filter::ColorInterpolation::SRGB,
                    result: SHADOW_RESULT.to_owned(),
                    kind: filter::Kind::GaussianBlur(filter::GaussianBlur {
                        input: filter::Input::SourceGraphic,
                        std_dev_x: deviation,
                        std_dev_y: deviation,
                    }),
                }],
            })];
            layer.calculate_bounding_boxes();
        }
        layers.push(Node::Group(Box::new(layer)));
    }
    layers
}

/// The filter result naming a blurred shadow.
const SHADOW_RESULT: &str = "shadow";

fn outset(rect: NonZeroRect, by: f32) -> Option<NonZeroRect> {
    NonZeroRect::from_ltrb(
        rect.left() - by,
        rect.top() - by,
        rect.right() + by,
        rect.bottom() + by,
    )
}

/// `node` with every fill and stroke opaque `paint`, and images recoloured to it, keeping their
/// coverage.
fn silhouette(node: &Node, paint: &Paint, cache: &mut Cache) -> Node {
    match node {
        Node::Path(path) => {
            let mut path = path.clone();
            if let Some(fill) = &mut path.fill {
                fill.paint = paint.clone();
                fill.opacity = Opacity::ONE;
                fill.context_element = None;
            }
            if let Some(stroke) = &mut path.stroke {
                stroke.paint = paint.clone();
                stroke.opacity = Opacity::ONE;
                stroke.context_element = None;
            }
            Node::Path(path)
        }
        Node::Group(group) => {
            let mut group = group.clone();
            group.children = group
                .children
                .iter()
                .map(|child| silhouette(child, paint, cache))
                .collect();
            Node::Group(group)
        }
        Node::Image(image) => {
            let Paint::Color(color) = paint else {
                unreachable!("silhouettes are painted in a shadow colour")
            };
            let channel = |value: u8| f32::from(value) / f32::from(u8::MAX);
            let [red, green, blue] = [color.red, color.green, color.blue].map(channel);
            let region = image.abs_bounding_box;
            let mut group = Group {
                filters: vec![Arc::new(filter::Filter {
                    id: cache.gen_filter_id(),
                    rect: region,
                    primitives: vec![filter::Primitive {
                        rect: region,
                        color_interpolation: filter::ColorInterpolation::SRGB,
                        result: SHADOW_RESULT.to_owned(),
                        kind: filter::Kind::ColorMatrix(filter::ColorMatrix {
                            input: filter::Input::SourceGraphic,
                            kind: filter::ColorMatrixKind::Matrix(vec![
                                0.0, 0.0, 0.0, 0.0, red, //
                                0.0, 0.0, 0.0, 0.0, green, //
                                0.0, 0.0, 0.0, 0.0, blue, //
                                0.0, 0.0, 0.0, 1.0, 0.0,
                            ]),
                        }),
                    }],
                })],
                children: vec![Node::Image(image.clone())],
                ..Group::empty()
            };
            group.calculate_bounding_boxes();
            Node::Group(Box::new(group))
        }
        Node::Text(_) => node.clone(),
    }
}

struct PathBuilder {
    builder: tiny_skia_path::PathBuilder,
}

impl ttf_parser::OutlineBuilder for PathBuilder {
    fn move_to(&mut self, x: f32, y: f32) {
        self.builder.move_to(x, y);
    }

    fn line_to(&mut self, x: f32, y: f32) {
        self.builder.line_to(x, y);
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        self.builder.quad_to(x1, y1, x, y);
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        self.builder.cubic_to(x1, y1, x2, y2, x, y);
    }

    fn close(&mut self) {
        self.builder.close();
    }
}

pub(crate) trait DatabaseExt {
    fn outline(&self, id: ID, glyph_id: GlyphId) -> Option<tiny_skia_path::Path>;
    fn outline_with_variations(
        &self,
        id: ID,
        glyph_id: GlyphId,
        variations: &[FontVariation],
        font_size: f32,
        font_optical_sizing: FontOpticalSizing,
    ) -> Option<tiny_skia_path::Path>;
    fn has_opsz_axis(&self, id: ID) -> bool;
    fn raster(&self, id: ID, glyph_id: GlyphId) -> Option<BitmapImage>;
    fn svg(&self, id: ID, glyph_id: GlyphId) -> Option<Node>;
    fn colr(&self, id: ID, glyph_id: GlyphId, paint: &ColrPaint<'_>) -> Option<Tree>;
}

/// How a COLR glyph paints: at its span's variation coordinates, with its palette, or the
/// face's default, and its foreground colour.
pub(crate) struct ColrPaint<'a> {
    variations: &'a [FontVariation],
    font_size: f32,
    optical_sizing: FontOpticalSizing,
    palette: Option<&'a [crate::paint::Color]>,
    foreground: crate::paint::Color,
}
impl ColrPaint<'_> {
    /// The bits identifying the painting.
    pub(crate) fn key(&self) -> Vec<u32> {
        let mut key = vec![
            self.font_size.to_bits(),
            u32::from(self.optical_sizing == FontOpticalSizing::Auto),
        ];
        for variation in self.variations {
            key.extend([u32::from_be_bytes(variation.tag), variation.value.to_bits()]);
        }
        for color in self.palette.into_iter().flatten().chain([&self.foreground]) {
            let [red, green, blue] = color.rgb;
            key.extend([
                u32::from_be_bytes([0, red, green, blue]),
                color.alpha.to_bits(),
            ]);
        }
        key
    }
    /// The variation settings, with the font size on an `opsz` axis that `font-optical-sizing:
    /// auto` sets where they do not.
    fn settings(&self) -> Vec<(skrifa::Tag, f32)> {
        const OPSZ: [u8; 4] = *b"opsz";
        let mut settings: Vec<_> = self
            .variations
            .iter()
            .map(|variation| (skrifa::Tag::new(&variation.tag), variation.value))
            .collect();
        if self.optical_sizing == FontOpticalSizing::Auto
            && !self
                .variations
                .iter()
                .any(|variation| variation.tag == OPSZ)
        {
            settings.push((skrifa::Tag::new(&OPSZ), self.font_size));
        }
        settings
    }
}

/// The colour COLR glyphs take as the foreground, as the vector renderer takes it: the span's
/// fill colour, else its stroke's, else black.
fn colr_foreground(span: &layout::Span) -> crate::paint::Color {
    let color = |paint: &Paint| match paint {
        Paint::Color(color) => Some(crate::paint::Color {
            rgb: [color.red, color.green, color.blue],
            alpha: 1.0,
        }),
        _ => None,
    };
    span.fill
        .as_ref()
        .and_then(|fill| color(&fill.paint))
        .or_else(|| span.stroke.as_ref().and_then(|stroke| color(&stroke.paint)))
        .unwrap_or(crate::paint::Color::BLACK)
}

#[derive(Clone)]
pub(crate) struct BitmapImage {
    image: Image,
    x: i16,
    y: i16,
    pixels_per_em: u16,
    glyph_bbox: Option<ttf_parser::Rect>,
    is_sbix: bool,
}

impl DatabaseExt for Database {
    #[inline(never)]
    fn outline(&self, id: ID, glyph_id: GlyphId) -> Option<tiny_skia_path::Path> {
        self.with_face_data(id, |data, face_index| -> Option<tiny_skia_path::Path> {
            let mut font = ttf_parser::Face::parse(data, face_index).ok()?;

            // For variable fonts, we need to set default variation values to get proper outlines
            if font.is_variable() {
                for axis in font.variation_axes() {
                    font.set_variation(axis.tag, axis.def_value);
                }
            }

            let mut builder = PathBuilder {
                builder: tiny_skia_path::PathBuilder::new(),
            };

            font.outline_glyph(glyph_id, &mut builder)?;
            builder.builder.finish()
        })?
    }

    #[inline(never)]
    fn outline_with_variations(
        &self,
        id: ID,
        glyph_id: GlyphId,
        variations: &[FontVariation],
        font_size: f32,
        font_optical_sizing: FontOpticalSizing,
    ) -> Option<tiny_skia_path::Path> {
        self.with_face_data(id, |data, face_index| -> Option<tiny_skia_path::Path> {
            let mut font = ttf_parser::Face::parse(data, face_index).ok()?;

            for v in variations {
                font.set_variation(ttf_parser::Tag::from_bytes(&v.tag), v.value);
            }

            // Auto-set opsz if font-optical-sizing is auto and not explicitly set
            if font_optical_sizing == FontOpticalSizing::Auto {
                let has_explicit_opsz = variations.iter().any(|v| v.tag == *b"opsz");
                if !has_explicit_opsz {
                    // Check if font has opsz axis
                    if let Some(axes) = font.tables().fvar {
                        let has_opsz_axis = axes
                            .axes
                            .into_iter()
                            .any(|axis| axis.tag == ttf_parser::Tag::from_bytes(b"opsz"));
                        if has_opsz_axis {
                            font.set_variation(ttf_parser::Tag::from_bytes(b"opsz"), font_size);
                        }
                    }
                }
            }

            let mut builder = PathBuilder {
                builder: tiny_skia_path::PathBuilder::new(),
            };

            font.outline_glyph(glyph_id, &mut builder)?;
            builder.builder.finish()
        })?
    }

    fn has_opsz_axis(&self, id: ID) -> bool {
        self.with_face_data(id, |data, face_index| -> Option<bool> {
            let font = ttf_parser::Face::parse(data, face_index).ok()?;
            let has_opsz = font.tables().fvar.map_or(false, |axes| {
                axes.axes
                    .into_iter()
                    .any(|axis| axis.tag == ttf_parser::Tag::from_bytes(b"opsz"))
            });
            Some(has_opsz)
        })
        .flatten()
        .unwrap_or(false)
    }

    fn raster(&self, id: ID, glyph_id: GlyphId) -> Option<BitmapImage> {
        self.with_face_data(id, |data, face_index| -> Option<BitmapImage> {
            let font = ttf_parser::Face::parse(data, face_index).ok()?;
            let image = font.glyph_raster_image(glyph_id, u16::MAX)?;

            if image.format == RasterImageFormat::PNG {
                let bitmap_image = BitmapImage {
                    image: Image {
                        id: String::new(),
                        visible: true,
                        size: Size::from_wh(image.width as f32, image.height as f32)?,
                        rendering_mode: ImageRendering::OptimizeQuality,
                        kind: ImageKind::PNG(Arc::new(image.data.into())),
                        abs_transform: Transform::default(),
                        abs_bounding_box: NonZeroRect::from_xywh(
                            0.0,
                            0.0,
                            image.width as f32,
                            image.height as f32,
                        )?,
                    },
                    x: image.x,
                    y: image.y,
                    pixels_per_em: image.pixels_per_em,
                    glyph_bbox: font.glyph_bounding_box(glyph_id),
                    // ttf-parser always checks sbix first, so if this table exists, it was used.
                    is_sbix: font.tables().sbix.is_some(),
                };

                return Some(bitmap_image);
            }

            None
        })?
    }

    fn svg(&self, id: ID, glyph_id: GlyphId) -> Option<Node> {
        // TODO: Technically not 100% accurate because the SVG format in a OTF font
        // is actually a subset/superset of a normal SVG, but it seems to work fine
        // for Twitter Color Emoji, so might as well use what we already have.

        // TODO: Glyph records can contain the data for multiple glyphs. We should
        // add a cache so we don't need to reparse the data every time.
        self.with_face_data(id, |data, face_index| -> Option<Node> {
            let font = ttf_parser::Face::parse(data, face_index).ok()?;
            let image = font.glyph_svg_image(glyph_id)?;
            let tree = Tree::from_data(image.data, &Options::default()).ok()?;

            // Twitter Color Emoji seems to always have one SVG record per glyph,
            // while Noto Color Emoji sometimes contains multiple ones. It's kind of hacky,
            // but the best we have for now.
            let node = if image.start_glyph_id == image.end_glyph_id {
                Node::Group(Box::new(tree.root))
            } else {
                tree.node_by_id(&format!("glyph{}", glyph_id.0))
                    .log_none(|| {
                        log::warn!("Failed to find SVG glyph node for glyph {}", glyph_id.0);
                    })
                    .cloned()?
            };

            Some(node)
        })?
    }

    fn colr(&self, id: ID, glyph_id: GlyphId, paint: &ColrPaint<'_>) -> Option<Tree> {
        use skrifa::MetadataProvider;
        self.with_face_data(id, |data, face_index| -> Option<Tree> {
            let font = skrifa::FontRef::from_index(data, face_index).ok()?;
            let location = font.axes().location(paint.settings());
            let default;
            let palette = match paint.palette {
                Some(palette) => palette,
                None => {
                    default = crate::fonts::default_palette(&font);
                    &default
                }
            };
            let document = super::colr::document(
                &font,
                (&location).into(),
                palette,
                paint.foreground,
                skrifa::GlyphId::new(u32::from(glyph_id.0)),
            )?;
            Tree::from_data(document.as_bytes(), &Options::default()).ok()
        })?
    }
}