read-fonts 0.46.0

Reading OpenType font files.
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
//! Outlines in font units, as points rather than path commands.
//!
//! An autohinter needs outline points in the form the `glyf` table stores
//! them: whole font units, marked on-curve or off-curve, and grouped by
//! contour. [`UnscaledOutlineSink`] receives these points. A `glyf` outline
//! provides its points directly. A charstring or a `VARC` composite is drawn
//! as a path and converted to points.

use super::PathSink;
use crate::tables::glyf::PointFlags;

/// Receives the points of an outline in font units, one contour at a time.
pub trait UnscaledOutlineSink {
    /// Begins a contour.
    fn start_contour(&mut self);

    /// Adds a point to the open contour.
    ///
    /// `flags` indicate whether the point is on the curve or is an off-curve
    /// control point of a quadratic or cubic curve. The interpreter marker
    /// flags are always clear.
    fn point(&mut self, x: i32, y: i32, flags: PointFlags);

    /// Ends the open contour.
    fn end_contour(&mut self);
}

impl<S: UnscaledOutlineSink + ?Sized> UnscaledOutlineSink for &mut S {
    fn start_contour(&mut self) {
        (**self).start_contour()
    }

    fn point(&mut self, x: i32, y: i32, flags: PointFlags) {
        (**self).point(x, y, flags)
    }

    fn end_contour(&mut self) {
        (**self).end_contour()
    }
}

#[derive(Copy, Clone)]
enum PendingElement {
    Line([f32; 2]),
    Quad([f32; 4]),
    Cubic([f32; 6]),
}

/// A [`PathSink`] that converts a path into points and sends them to an
/// [`UnscaledOutlineSink`].
///
/// This is used for sources that can only draw a path. The path must be in
/// font units. Each segment's end point is held until the next command. When
/// a contour closes at its start point, that point is not repeated. This
/// matches how FreeType builds a TrueType outline from a charstring.
pub(crate) struct PointsFromPath<'a, S: ?Sized> {
    sink: &'a mut S,
    /// True while a contour is open.
    open: bool,
    last_start: Option<(f32, f32)>,
    pending: Option<PendingElement>,
}

impl<'a, S: ?Sized> PointsFromPath<'a, S> {
    pub(crate) fn new(sink: &'a mut S) -> Self {
        Self {
            sink,
            open: false,
            last_start: None,
            pending: None,
        }
    }
}

impl<S: UnscaledOutlineSink + ?Sized> PointsFromPath<'_, S> {
    fn push(&mut self, x: f32, y: f32, flags: PointFlags) {
        if !self.open {
            self.sink.start_contour();
            self.open = true;
        }
        // Truncation toward zero, as a charstring coordinate has always been
        // taken into TrueType points here.
        self.sink.point(x as i32, y as i32, flags);
    }

    fn flush_pending(&mut self, for_close: bool) {
        if let Some(element) = self.pending.take() {
            let [x, y] = match element {
                PendingElement::Line([x, y]) => [x, y],
                PendingElement::Quad([x0, y0, x, y]) => {
                    self.push(x0, y0, PointFlags::off_curve_quad());
                    [x, y]
                }
                PendingElement::Cubic([x0, y0, x1, y1, x, y]) => {
                    self.push(x0, y0, PointFlags::off_curve_cubic());
                    self.push(x1, y1, PointFlags::off_curve_cubic());
                    [x, y]
                }
            };
            if !for_close || self.last_start != Some((x, y)) {
                self.push(x, y, PointFlags::on_curve());
            }
        }
    }

    fn close_contour(&mut self) {
        self.flush_pending(true);
        if self.open {
            self.sink.end_contour();
            self.open = false;
        }
    }

    /// Ends the open contour, if any.
    pub(crate) fn finish(mut self) {
        self.close_contour();
    }
}

impl<S: UnscaledOutlineSink + ?Sized> PathSink for PointsFromPath<'_, S> {
    fn move_to(&mut self, x: f32, y: f32) {
        self.close_contour();
        self.push(x, y, PointFlags::on_curve());
        self.last_start = Some((x, y));
    }

    fn line_to(&mut self, x: f32, y: f32) {
        self.flush_pending(false);
        self.pending = Some(PendingElement::Line([x, y]));
    }

    fn quad_to(&mut self, cx0: f32, cy0: f32, x: f32, y: f32) {
        self.flush_pending(false);
        self.pending = Some(PendingElement::Quad([cx0, cy0, x, y]));
    }

    fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32) {
        self.flush_pending(false);
        self.pending = Some(PendingElement::Cubic([cx0, cy0, cx1, cy1, x, y]));
    }

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::glyph::outline::DrawMemory;
    use crate::{model::Font, types::GlyphId};
    use alloc::vec::Vec;

    /// Records the calls made to a sink, for testing.
    #[derive(Default)]
    struct Recorded {
        points: Vec<(i32, i32, u8)>,
        starts: Vec<usize>,
        open: bool,
        ended: usize,
    }

    impl UnscaledOutlineSink for Recorded {
        fn start_contour(&mut self) {
            assert!(!self.open, "a contour was started inside another");
            self.open = true;
            self.starts.push(self.points.len());
        }

        fn point(&mut self, x: i32, y: i32, flags: PointFlags) {
            assert!(self.open, "a point arrived outside a contour");
            self.points.push((x, y, flags.to_bits()));
        }

        fn end_contour(&mut self) {
            assert!(self.open, "a contour ended that never started");
            self.open = false;
            self.ended += 1;
        }
    }

    fn record(data: &[u8], glyph_id: u32) -> Recorded {
        let font = Font::new(data.to_vec(), 0).unwrap();
        let glyph = font.outline_glyphs().get(GlyphId::new(glyph_id)).unwrap();
        let mut recorded = Recorded::default();
        glyph
            .draw_unscaled(DrawMemory::Scratch, &mut recorded)
            .unwrap();
        assert!(!recorded.open);
        assert_eq!(recorded.ended, recorded.starts.len());
        recorded
    }

    #[test]
    fn read_glyf_outline() {
        let recorded = record(font_test_data::MATERIAL_SYMBOLS_SUBSET, 5);
        let expected = [
            // contour 0
            (400, 80, 1),
            (400, 360, 1),
            (320, 360, 1),
            (320, 600, 1),
            (320, 633, 0),
            (367, 680, 0),
            (400, 680, 1),
            (560, 680, 1),
            (593, 680, 0),
            (640, 633, 0),
            (640, 600, 1),
            (640, 360, 1),
            (560, 360, 1),
            (560, 80, 1),
            // contour 1
            (480, 720, 1),
            (447, 720, 0),
            (400, 767, 0),
            (400, 800, 1),
            (400, 833, 0),
            (447, 880, 0),
            (480, 880, 1),
            (513, 880, 0),
            (560, 833, 0),
            (560, 800, 1),
            (560, 767, 0),
            (513, 720, 0),
        ];
        assert_eq!(recorded.points, expected);
        assert_eq!(recorded.starts, [0, 14]);
    }

    #[test]
    fn the_advance_comes_with_the_points() {
        let font = Font::new(font_test_data::MATERIAL_SYMBOLS_SUBSET.to_vec(), 0).unwrap();
        let glyph = font.outline_glyphs().get(GlyphId::new(5)).unwrap();
        let metrics = glyph
            .draw_unscaled(DrawMemory::Scratch, &mut Recorded::default())
            .unwrap();
        assert_eq!(
            metrics.h_advance,
            Some(font.glyph_metrics().h_advance(GlyphId::new(5)))
        );
    }

    #[test]
    fn read_cubic_glyf_outline() {
        let recorded = record(font_test_data::CUBIC_GLYF, 2);
        let expected = [
            (278, 710, 1),
            (278, 470, 1),
            (300, 500, 128),
            (800, 500, 128),
            (998, 470, 1),
            (998, 710, 1),
        ];
        assert_eq!(recorded.points, expected);
        assert_eq!(recorded.starts, [0]);
    }

    #[test]
    fn read_cff_outline() {
        let recorded = record(font_test_data::CANTARELL_VF_TRIMMED, 2);
        let expected = [
            // contour 0
            (83, 0, 1),
            (163, 0, 1),
            (163, 482, 1),
            (83, 482, 1),
            // contour 1
            (124, 595, 1),
            (160, 595, 128),
            (181, 616, 128),
            (181, 652, 1),
            (181, 688, 128),
            (160, 709, 128),
            (124, 709, 1),
            (88, 709, 128),
            (67, 688, 128),
            (67, 652, 1),
            (67, 616, 128),
            (88, 595, 128),
        ];
        assert_eq!(recorded.points, expected);
        assert_eq!(recorded.starts, [0, 4]);
    }

    #[test]
    fn a_varc_composite_rebuilds_from_its_path() {
        let font = Font::new(font_test_data::varc::CJK_6868.to_vec(), 0).unwrap();
        let outlines = font.outline_glyphs();
        let gid = (0..font.num_glyphs())
            .map(GlyphId::new)
            .find(|gid| {
                matches!(
                    outlines.get(*gid),
                    Some(crate::model::glyph::outline::OutlineGlyph::Varc(_))
                )
            })
            .unwrap();
        let mut recorded = Recorded::default();
        outlines
            .get(gid)
            .unwrap()
            .draw_unscaled(DrawMemory::Scratch, &mut recorded)
            .unwrap();
        assert!(recorded.points.len() > 2);
        assert_eq!(recorded.starts.first(), Some(&0));
        assert!(!recorded.open);
    }

    // A contour that closes with a quad does not repeat its start point.
    #[test]
    fn a_closing_quad_does_not_repeat_the_start() {
        let mut recorded = Recorded::default();
        let mut pen = PointsFromPath::new(&mut recorded);
        pen.move_to(0.0, 0.0);
        pen.line_to(10.0, 0.0);
        pen.quad_to(10.0, 10.0, 0.0, 0.0);
        pen.close();
        pen.finish();
        let on = PointFlags::on_curve().to_bits();
        let off = PointFlags::off_curve_quad().to_bits();
        assert_eq!(recorded.points, [(0, 0, on), (10, 0, on), (10, 10, off)]);
    }

    // No contour of a VARC composite, whose glyf components draw as quads,
    // repeats its start point.
    #[test]
    fn varc_contours_do_not_repeat_their_start() {
        let font = Font::new(font_test_data::varc::CJK_6868.to_vec(), 0).unwrap();
        let outlines = font.outline_glyphs();
        let mut contours = 0;
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            let Some(glyph @ crate::model::glyph::outline::OutlineGlyph::Varc(_)) =
                outlines.get(gid)
            else {
                continue;
            };
            let mut recorded = Recorded::default();
            glyph
                .draw_unscaled(DrawMemory::Scratch, &mut recorded)
                .unwrap();
            let ends = recorded.starts[1..]
                .iter()
                .copied()
                .chain([recorded.points.len()]);
            for (start, end) in recorded.starts.iter().copied().zip(ends) {
                let (first, last) = (recorded.points[start], recorded.points[end - 1]);
                assert!(
                    end - start < 2 || (first.0, first.1) != (last.0, last.1),
                    "glyph {gid} contour at {start}"
                );
                contours += 1;
            }
        }
        assert!(contours > 0);
    }

    /// When a contour ends with a line or cubic whose end matches the start
    /// point, omit the last on curve. This matches FreeType behavior when
    /// constructing a TrueType style outline from a CFF font.
    #[test]
    fn omit_unnecessary_trailing_oncurves() {
        let mut recorded = Recorded::default();
        let mut pen = PointsFromPath::new(&mut recorded);
        pen.move_to(0.5, 1.5);
        pen.line_to(1.0, 2.0);
        // matches start, omit last on curve
        pen.line_to(0.5, 1.5);
        pen.close();
        pen.move_to(5.0, 6.0);
        pen.curve_to(1.0, 1.0, 2.0, 2.0, 2.0, 3.0);
        // matches start, omit last on curve
        pen.curve_to(1.0, 1.0, 2.0, 2.0, 5.0, 6.0);
        pen.close();
        pen.move_to(5.0, 6.0);
        // doesn't match start, keep on curve
        pen.curve_to(1.0, 1.0, 2.0, 2.0, 2.0, 3.0);
        // and an unclosed contour is still ended
        pen.finish();
        let on_curves: Vec<_> = recorded
            .points
            .iter()
            .map(|(_, _, flags)| PointFlags::from_bits(*flags).is_on_curve())
            .collect();
        #[rustfmt::skip]
        let expected_on_curves = [
            true,  // move
            true,  // line
                   // trailing line omitted
            true,  // move
            false, // cubic 1 off curve 1
            false, // cubic 1 off curve 2
            true,  // cubic 1 on curve
            false, // cubic 2 off curve 1
            false, // cubic 2 off curve 2
                   // trailing on curve omitted
            true,  // move
            false, // cubic 1 off curve 1
            false, // cubic 1 off curve 2
            true,  // trailing on curve retained
        ];
        assert_eq!(on_curves, expected_on_curves);
        assert_eq!(recorded.starts, [0, 2, 8]);
        assert_eq!(recorded.ended, 3);
    }
}