glifo 0.1.0

Glifo provides APIs for efficiently rendering text.
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
// Copyright 2025 the Vello Authors and the Parley Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Drawing COLR glyphs.

use crate::atlas::commands::AtlasPaint;
use crate::color::Srgb;
use crate::color::{AlphaColor, DynamicColor};
use crate::glyph::{GlyphColr, OutlinePath};
use crate::interface::DrawSink;
use crate::kurbo::{Affine, Point, Rect, Shape};
use crate::peniko::{self, BlendMode, ColorStops, Compose, Extend, Gradient, Mix};
use crate::util::FloatExt;
use alloc::vec;
use alloc::vec::Vec;
use core::fmt::Debug;
use peniko::{LinearGradientPosition, RadialGradientPosition, SweepGradientPosition};
use skrifa::color::{Brush, ColorPainter, ColorStop, CompositeMode, Transform};
use skrifa::instance::LocationRef;
use skrifa::outline::{DrawSettings, OutlineGlyphCollection, pen::ControlBoundsPen};
use skrifa::raw::TableProvider;
use skrifa::raw::types::BoundingBox;
use skrifa::{FontRef, GlyphId, MetadataProvider};
use smallvec::SmallVec;

trait ColrDrawSinkExt: DrawSink {
    fn fill_with_paint(&mut self, rect: &Rect, paint: AtlasPaint) {
        self.set_paint(paint);
        self.fill_rect(rect);
    }

    fn fill_solid(&mut self, rect: &Rect, color: AlphaColor<Srgb>) {
        self.fill_with_paint(rect, AtlasPaint::Solid(color));
    }

    fn fill_gradient(&mut self, rect: &Rect, gradient: Gradient) {
        self.fill_with_paint(rect, AtlasPaint::Gradient(gradient));
    }
}

impl<T: DrawSink + ?Sized> ColrDrawSinkExt for T {}

/// An abstraction for painting COLR glyphs.
pub(crate) struct ColrPainter<'a> {
    transforms: Vec<Affine>,
    colr_glyph: &'a GlyphColr<'a>,
    outline_glyphs: OutlineGlyphCollection<'a>,
    clip_outline: OutlinePath,
    context_color: AlphaColor<Srgb>,
    painter: &'a mut dyn DrawSink,
    stack: Vec<ColrStackEntry>,
    skip_blend_layers: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ColrStackEntry {
    ClipPath,
    BlendLayer,
}

impl Debug for ColrPainter<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("ColrPainter()").finish()
    }
}

impl<'a> ColrPainter<'a> {
    /// Create a new COLR painter.
    pub(crate) fn new(
        colr_glyph: &'a GlyphColr<'a>,
        context_color: AlphaColor<Srgb>,
        painter: &'a mut dyn DrawSink,
    ) -> Self {
        Self {
            transforms: vec![colr_glyph.draw_transform],
            colr_glyph,
            outline_glyphs: colr_glyph.font_ref.outline_glyphs(),
            clip_outline: OutlinePath::new(),
            context_color,
            painter,
            stack: Vec::new(),
            // In case the emoji doesn't use non-default blending, we can ignore layers
            // completely and use src-over compositing throughout.
            skip_blend_layers: !colr_glyph.has_non_default_blend,
        }
    }

    /// Paint the underlying glyph.
    pub(crate) fn paint(&mut self) {
        let skrifa_glyph = self.colr_glyph.skrifa_glyph.clone();
        let location_ref = self.colr_glyph.location;
        // Ignore errors for now.
        let _ = skrifa_glyph.paint(location_ref, self);

        // In certain malformed fonts (i.e. if there is a cycle), skrifa will not
        // ensure that the push/pop count is the same, so we pop the remaining ones here.
        while let Some(entry) = self.stack.pop() {
            match entry {
                ColrStackEntry::ClipPath => self.painter.pop_clip_path(),
                ColrStackEntry::BlendLayer => self.painter.pop_layer(),
            }
        }
    }

    fn cur_transform(&self) -> Affine {
        self.transforms.last().copied().unwrap_or_default()
    }

    fn palette_index_to_color(&self, palette_index: u16, alpha: f32) -> Option<AlphaColor<Srgb>> {
        if palette_index != u16::MAX {
            let color = self
                .colr_glyph
                .font_ref
                .cpal()
                .ok()?
                .color_records_array()?
                .ok()?[palette_index as usize];

            Some(
                AlphaColor::from_rgba8(color.red, color.green, color.blue, color.alpha)
                    .multiply_alpha(alpha),
            )
        } else {
            Some(self.context_color.multiply_alpha(alpha))
        }
    }

    fn convert_stops(&self, stops: &[ColorStop]) -> ColorStops {
        let mut stops = stops
            .iter()
            .map(|s| {
                let color = self
                    .palette_index_to_color(s.palette_index, s.alpha)
                    .unwrap_or(AlphaColor::BLACK);

                peniko::ColorStop {
                    offset: s.offset,
                    color: DynamicColor::from_alpha_color(color),
                }
            })
            .collect::<SmallVec<[peniko::ColorStop; 4]>>();

        // Pad stops if necessary, since vello requires offsets
        // to start at 0.0 and end at 1.0.
        let first_stop = stops[0];
        let last_stop = *stops.last().unwrap();

        if first_stop.offset != 0.0 {
            let mut new_stop = first_stop;
            new_stop.offset = 0.0;
            stops.insert(0, new_stop);
        }

        if last_stop.offset != 1.0 {
            let mut new_stop = last_stop;
            new_stop.offset = 1.0;
            stops.push(new_stop);
        }

        // The COLR spec has the very specific requirement that if there are multiple stops with the
        // offset 1.0, only the last one should be used. We abstract this away by removing all such
        // superfluous stops.
        while let Some(stop) = stops.get(stops.len() - 2).map(|s| s.offset) {
            if (stop - 1.0).is_nearly_zero() {
                stops.remove(stops.len() - 2);
            } else {
                break;
            }
        }

        ColorStops(stops)
    }

    fn push_clip(&mut self) {
        self.painter.push_clip_path(&self.clip_outline.path);
        self.stack.push(ColrStackEntry::ClipPath);
    }

    fn pop_stack_entry(&mut self, expected: ColrStackEntry) -> bool {
        // This should only be false for malformed fonts. Assuming that our
        // implementation is correct, this shouldn't ever be reached for valid fonts.
        #[cfg(test)]
        assert_eq!(
            self.stack.last().copied(),
            Some(expected),
            "assertion should always be true for valid fonts"
        );

        if self.stack.last().copied() == Some(expected) {
            self.stack.pop();

            true
        } else {
            false
        }
    }
}

pub(crate) struct ColrGlyphInfo {
    /// A conservative bounding box of the glyph.
    ///
    /// Is `None` in case the glyph is empty (i.e. doesn't contain any drawable content).
    pub(crate) bbox: Option<Rect>,
    /// Whether the glyph uses any non-default blending.
    pub(crate) has_non_default_blend: bool,
}

pub(crate) fn get_colr_info<'a>(
    font_ref: &'a FontRef<'a>,
    color_glyph: &skrifa::color::ColorGlyph<'a>,
    location: LocationRef<'a>,
) -> ColrGlyphInfo {
    let mut extractor = GlyphInfoExtractor::new(font_ref, location);
    let _ = color_glyph.paint(location, &mut extractor);
    extractor.finish()
}

struct GlyphInfoExtractor<'a> {
    transforms: Vec<Affine>,
    clip_stack: Vec<Rect>,
    coarse_bbox: Option<Rect>,
    has_non_default_blend: bool,
    outline_glyphs: OutlineGlyphCollection<'a>,
    location: LocationRef<'a>,
}

impl<'a> GlyphInfoExtractor<'a> {
    fn new(font_ref: &'a FontRef<'a>, location: LocationRef<'a>) -> Self {
        Self {
            transforms: vec![Affine::IDENTITY],
            clip_stack: Vec::new(),
            coarse_bbox: None,
            has_non_default_blend: false,
            outline_glyphs: font_ref.outline_glyphs(),
            location,
        }
    }

    fn cur_transform(&self) -> Affine {
        self.transforms.last().copied().unwrap_or_default()
    }

    fn push_clip_bbox(&mut self, clip_bbox: Rect) {
        let active = self
            .clip_stack
            .last()
            .copied()
            .map_or(clip_bbox, |parent| parent.intersect(clip_bbox));
        self.coarse_bbox = Some(
            self.coarse_bbox
                .map_or(active, |coarse_bbox| coarse_bbox.union(active)),
        );
        self.clip_stack.push(active);
    }

    fn transform_rect(&self, rect: Rect) -> Rect {
        self.cur_transform().transform_rect_bbox(rect)
    }

    fn finish(self) -> ColrGlyphInfo {
        ColrGlyphInfo {
            bbox: self.coarse_bbox,
            has_non_default_blend: self.has_non_default_blend,
        }
    }
}

impl ColorPainter for ColrPainter<'_> {
    fn push_transform(&mut self, t: Transform) {
        self.transforms
            .push(self.cur_transform() * convert_affine(t));
    }

    fn pop_transform(&mut self) {
        self.transforms.pop();
    }

    fn push_clip_glyph(&mut self, glyph_id: GlyphId) {
        // TODO: Make it possible to use the outline cache for this.
        let Some(outline_glyph) = self.outline_glyphs.get(glyph_id) else {
            return;
        };

        self.clip_outline.reuse();
        let _ = outline_glyph.draw(
            DrawSettings::unhinted(skrifa::instance::Size::unscaled(), self.colr_glyph.location),
            &mut self.clip_outline,
        );

        // Note that the bbox will become stale, but we don't need it anyway here.
        self.clip_outline.path.apply_affine(self.cur_transform());
        self.push_clip();
    }

    fn push_clip_box(&mut self, clip_box: BoundingBox<f32>) {
        let rect = Rect::new(
            f64::from(clip_box.x_min),
            f64::from(clip_box.y_min),
            f64::from(clip_box.x_max),
            f64::from(clip_box.y_max),
        );
        let transformed = self.cur_transform().transform_rect_bbox(rect);
        self.clip_outline.reuse();
        // Note that the bbox will become stale, but we don't need it anyway here.
        self.clip_outline
            .path
            .extend(transformed.path_elements(0.1));
        self.push_clip();
    }

    fn pop_clip(&mut self) {
        if self.pop_stack_entry(ColrStackEntry::ClipPath) {
            self.painter.pop_clip_path();
        }
    }

    fn fill(&mut self, brush: Brush<'_>) {
        // Ceil so that we don't apply unnecessary anti-aliasing in case the
        // glyph area is at a sub-pixel position.
        let fill_rect = &self.colr_glyph.area.ceil();

        match brush {
            Brush::Solid {
                palette_index,
                alpha,
            } => {
                let color = self
                    .palette_index_to_color(palette_index, alpha)
                    .unwrap_or(AlphaColor::BLACK);

                self.painter.fill_solid(fill_rect, color);
            }
            Brush::LinearGradient {
                p0,
                p1,
                color_stops,
                extend,
            } => {
                let p0 = convert_point(p0);
                let p1 = convert_point(p1);
                let extend = convert_extend(extend);
                let stops = self.convert_stops(color_stops);

                if stops.len() == 1 {
                    self.painter
                        .fill_solid(fill_rect, stops[0].color.to_alpha_color());
                } else {
                    let grad = Gradient {
                        kind: LinearGradientPosition { start: p0, end: p1 }.into(),
                        stops,
                        extend,
                        ..Default::default()
                    };
                    self.painter.set_paint_transform(self.cur_transform());
                    self.painter.fill_gradient(fill_rect, grad);
                }
            }
            Brush::RadialGradient {
                c0,
                r0,
                c1,
                r1,
                color_stops,
                extend,
            } => {
                // TODO: Radial gradients with negative r0.

                let p0 = convert_point(c0);
                let p1 = convert_point(c1);
                let extend = convert_extend(extend);
                let stops = self.convert_stops(color_stops);

                if r1 <= 0.0 || stops.len() == 1 {
                    self.painter
                        .fill_solid(fill_rect, stops[0].color.to_alpha_color());

                    return;
                }

                let grad = Gradient {
                    kind: RadialGradientPosition {
                        start_center: p0,
                        start_radius: r0,
                        end_center: p1,
                        end_radius: r1,
                    }
                    .into(),
                    stops,
                    extend,
                    ..Default::default()
                };

                self.painter.set_paint_transform(self.cur_transform());
                self.painter.fill_gradient(fill_rect, grad);
            }
            Brush::SweepGradient {
                c0,
                start_angle,
                mut end_angle,
                color_stops,
                extend,
            } => {
                let p0 = convert_point(c0);
                let extend = convert_extend(extend);
                let stops = self.convert_stops(color_stops);

                if stops.len() == 1 {
                    self.painter
                        .fill_solid(fill_rect, stops[0].color.to_alpha_color());

                    return;
                }

                if start_angle == end_angle {
                    match extend {
                        Extend::Pad => {
                            // Vello doesn't accept sweep gradient with same start and end
                            // angle, so add an artificial, small offset.
                            end_angle += 0.01;
                        }
                        _ => {
                            // Cannot be reached,
                            // see https://github.com/googlefonts/fontations/issues/1017.
                            unreachable!()
                        }
                    }
                }

                // We need to invert the direction of the gradient to bridge the gap between
                // peniko and COLR.
                let grad = Gradient {
                    kind: SweepGradientPosition {
                        center: Point::new(p0.x, -p0.y),
                        start_angle: start_angle.to_radians(),
                        end_angle: end_angle.to_radians(),
                    }
                    .into(),
                    stops,
                    extend,
                    ..Default::default()
                };

                let paint_transform = self.cur_transform() * Affine::scale_non_uniform(1.0, -1.0);

                self.painter.set_paint_transform(paint_transform);
                self.painter.fill_gradient(fill_rect, grad);
            }
        };
    }

    fn push_layer(&mut self, composite_mode: CompositeMode) {
        let blend_mode = convert_composite_mode(composite_mode);

        if !self.skip_blend_layers {
            self.painter.push_blend_layer(blend_mode);
            self.stack.push(ColrStackEntry::BlendLayer);
        }
    }

    fn pop_layer(&mut self) {
        if !self.skip_blend_layers && self.pop_stack_entry(ColrStackEntry::BlendLayer) {
            self.painter.pop_layer();
        }
    }
}

impl ColorPainter for GlyphInfoExtractor<'_> {
    fn push_transform(&mut self, t: Transform) {
        self.transforms
            .push(self.cur_transform() * convert_affine(t));
    }

    fn pop_transform(&mut self) {
        self.transforms.pop();
    }

    fn push_clip_glyph(&mut self, glyph_id: GlyphId) {
        let mut outline_bbox = ControlBoundsPen::default();

        // TODO: Make it possible to use the outline cache for this.
        let Some(outline_glyph) = self.outline_glyphs.get(glyph_id) else {
            return;
        };

        let _ = outline_glyph.draw(
            DrawSettings::unhinted(skrifa::instance::Size::unscaled(), self.location),
            &mut outline_bbox,
        );

        if let Some(outline_bbox) = outline_bbox.bounding_box().map(convert_bounding_box) {
            self.push_clip_bbox(self.transform_rect(outline_bbox));
        }
    }

    fn push_clip_box(&mut self, clip_box: BoundingBox<f32>) {
        self.push_clip_bbox(self.transform_rect(convert_bounding_box(clip_box)));
    }

    fn pop_clip(&mut self) {
        self.clip_stack.pop();
    }

    fn fill(&mut self, _brush: Brush<'_>) {}

    fn push_layer(&mut self, composite_mode: CompositeMode) {
        self.has_non_default_blend |=
            convert_composite_mode(composite_mode) != BlendMode::default();
    }

    fn pop_layer(&mut self) {}
}

fn convert_composite_mode(composite_mode: CompositeMode) -> BlendMode {
    match composite_mode {
        CompositeMode::Clear => BlendMode::new(Mix::Normal, Compose::Clear),
        CompositeMode::Src => BlendMode::new(Mix::Normal, Compose::Copy),
        CompositeMode::Dest => BlendMode::new(Mix::Normal, Compose::Dest),
        CompositeMode::SrcOver => BlendMode::new(Mix::Normal, Compose::SrcOver),
        CompositeMode::DestOver => BlendMode::new(Mix::Normal, Compose::DestOver),
        CompositeMode::SrcIn => BlendMode::new(Mix::Normal, Compose::SrcIn),
        CompositeMode::DestIn => BlendMode::new(Mix::Normal, Compose::DestIn),
        CompositeMode::SrcOut => BlendMode::new(Mix::Normal, Compose::SrcOut),
        CompositeMode::DestOut => BlendMode::new(Mix::Normal, Compose::DestOut),
        CompositeMode::SrcAtop => BlendMode::new(Mix::Normal, Compose::SrcAtop),
        CompositeMode::DestAtop => BlendMode::new(Mix::Normal, Compose::DestAtop),
        CompositeMode::Xor => BlendMode::new(Mix::Normal, Compose::Xor),
        CompositeMode::Plus => BlendMode::new(Mix::Normal, Compose::Plus),
        CompositeMode::Screen => BlendMode::new(Mix::Screen, Compose::SrcOver),
        CompositeMode::Overlay => BlendMode::new(Mix::Overlay, Compose::SrcOver),
        CompositeMode::Darken => BlendMode::new(Mix::Darken, Compose::SrcOver),
        CompositeMode::Lighten => BlendMode::new(Mix::Lighten, Compose::SrcOver),
        CompositeMode::ColorDodge => BlendMode::new(Mix::ColorDodge, Compose::SrcOver),
        CompositeMode::ColorBurn => BlendMode::new(Mix::ColorBurn, Compose::SrcOver),
        CompositeMode::HardLight => BlendMode::new(Mix::HardLight, Compose::SrcOver),
        CompositeMode::SoftLight => BlendMode::new(Mix::SoftLight, Compose::SrcOver),
        CompositeMode::Difference => BlendMode::new(Mix::Difference, Compose::SrcOver),
        CompositeMode::Exclusion => BlendMode::new(Mix::Exclusion, Compose::SrcOver),
        CompositeMode::Multiply => BlendMode::new(Mix::Multiply, Compose::SrcOver),
        CompositeMode::HslHue => BlendMode::new(Mix::Hue, Compose::SrcOver),
        CompositeMode::HslSaturation => BlendMode::new(Mix::Saturation, Compose::SrcOver),
        CompositeMode::HslColor => BlendMode::new(Mix::Color, Compose::SrcOver),
        CompositeMode::HslLuminosity => BlendMode::new(Mix::Luminosity, Compose::SrcOver),
        CompositeMode::Unknown => BlendMode::default(),
    }
}

fn convert_affine(transform: Transform) -> Affine {
    Affine::new([
        f64::from(transform.xx),
        f64::from(transform.yx),
        f64::from(transform.xy),
        f64::from(transform.yy),
        f64::from(transform.dx),
        f64::from(transform.dy),
    ])
}

fn convert_extend(extend: skrifa::color::Extend) -> Extend {
    match extend {
        skrifa::color::Extend::Pad => Extend::Pad,
        skrifa::color::Extend::Repeat => Extend::Repeat,
        skrifa::color::Extend::Reflect => Extend::Reflect,
        skrifa::color::Extend::Unknown => Extend::Pad,
    }
}

fn convert_point(point: skrifa::raw::types::Point<f32>) -> Point {
    Point::new(f64::from(point.x), f64::from(point.y))
}

pub(crate) fn convert_bounding_box(rect: BoundingBox<f32>) -> Rect {
    Rect::new(
        f64::from(rect.x_min),
        f64::from(rect.y_min),
        f64::from(rect.x_max),
        f64::from(rect.y_max),
    )
}