read-fonts 0.43.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
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
//! The [Font Variations](https://docs.microsoft.com/en-us/typography/opentype/spec/fvar) table

include!("../../generated/generated_fvar.rs");

#[path = "./instance_record.rs"]
mod instance_record;

use super::{avar::Avar, variations::DeltaSetIndex};
use alloc::vec::Vec;

pub use instance_record::InstanceRecord;

const MAX_INLINE_AVAR2_AXES: usize = 64;
/// Maximum number of axes for which we will use a quadratic path to normalize user coordinates.
const MAX_NORMALIZE_QUADRATIC_AXES: usize = 64;
const MAX_INLINE_NORMALIZE_AXES: usize = 128;

#[inline]
fn round_f64_to_i32(value: f64) -> i32 {
    if value >= 0.0 {
        (value + 0.5) as i32
    } else {
        (value - 0.5) as i32
    }
}

#[inline]
fn apply_avar2_delta(coord: Fixed, delta_2dot14: f64) -> Fixed {
    // HarfBuzz keeps the avar1 result in 16.16 through the avar2 add, and
    // converts the avar2 delta from 2.14 units by multiplying by four.
    Fixed::from_bits(
        coord
            .to_bits()
            .wrapping_add(round_f64_to_i32(delta_2dot14 * 4.0)),
    )
}

fn normalize_user_coords<T>(
    axes: &[VariationAxisRecord],
    user_coords: impl IntoIterator<Item = (Tag, Fixed)>,
    coords: &mut [T],
    convert: impl Fn(Fixed) -> T,
) {
    let axis_count = axes.len().min(coords.len());
    let axes = &axes[..axis_count];
    let coords = &mut coords[..axis_count];
    if axis_count <= MAX_NORMALIZE_QUADRATIC_AXES {
        for (tag, user_coord) in user_coords {
            // To permit non-linear interpolation, iterate over all axes to
            // ensure we match multiple axes with the same tag:
            // https://github.com/PeterConstable/OT_Drafts/blob/master/NLI/UnderstandingNLI.md
            for (axis, coord) in axes
                .iter()
                .zip(coords.iter_mut())
                .filter(|(axis, _)| axis.axis_tag() == tag)
            {
                *coord = convert(axis.normalize(user_coord));
            }
        }
        return;
    }
    // Above MAX_NORMALIZE_QUADRATIC_AXES, use an indexed path to avoid O(n^2) behavior
    let mut stack_axis_order = [0u16; MAX_INLINE_NORMALIZE_AXES];
    let mut heap_axis_order = Vec::new();
    let axis_order = if axis_count > MAX_INLINE_NORMALIZE_AXES {
        heap_axis_order.resize(axis_count, 0);
        heap_axis_order.as_mut_slice()
    } else {
        &mut stack_axis_order[..axis_count]
    };
    // Initialize axis_order with the identity mapping
    for (i, axis_index) in axis_order.iter_mut().enumerate() {
        *axis_index = i as u16;
    }
    // Then sort by tag to group axes with the same tag together, so we can use
    // partition_point to find the range of axes for each tag
    axis_order.sort_unstable_by_key(|&i| axes[i as usize].axis_tag());
    for (tag, user_coord) in user_coords {
        let start = axis_order.partition_point(|&i| axes[i as usize].axis_tag() < tag);
        let end = axis_order.partition_point(|&i| axes[i as usize].axis_tag() <= tag);
        for &i in &axis_order[start..end] {
            coords[i as usize] = convert(axes[i as usize].normalize(user_coord));
        }
    }
}

fn apply_avar_mappings<T>(
    avar: Option<&Avar>,
    coords: &mut [T],
    to_fixed: impl Fn(&T) -> Fixed,
    from_fixed: impl Fn(Fixed) -> T,
) {
    if let Some(maps) = avar.map(|avar| avar.axis_segment_maps()) {
        for (coord, map) in coords.iter_mut().zip(maps.iter()) {
            if let Ok(map) = map {
                *coord = from_fixed(map.apply(to_fixed(coord)));
            }
        }
    }
}

fn to_normalized_coords(fixed_coords: &[Fixed], normalized_coords: &mut [F2Dot14]) {
    for (target_coord, coord) in normalized_coords.iter_mut().zip(fixed_coords.iter()) {
        *target_coord = coord.to_f2dot14();
    }
}

impl<'a> Fvar<'a> {
    /// Returns the array of variation axis records.
    pub fn axes(&self) -> Result<&'a [VariationAxisRecord], ReadError> {
        Ok(self.axis_instance_arrays()?.axes())
    }

    /// Returns the array of instance records.
    pub fn instances(&self) -> Result<ComputedArray<'a, InstanceRecord<'a>>, ReadError> {
        Ok(self.axis_instance_arrays()?.instances())
    }

    /// Converts user space coordinates provided by an unordered iterator
    /// of `(tag, value)` pairs to normalized coordinates in axis list order.
    ///
    /// Stores the resulting normalized coordinates in the given slice.
    ///
    /// * User coordinate tags that don't match an axis are ignored.
    /// * User coordinate values are clamped to the range of their associated
    ///   axis before normalization.
    /// * If more than one user coordinate is provided for the same tag, the
    ///   last one is used.
    /// * If no user coordinate for an axis is provided, the associated
    ///   coordinate is set to the normalized value 0.0, representing the
    ///   default location in variation space.
    /// * The length of `normalized_coords` should equal the number of axes
    ///   present in the this table. If the length is smaller, axes at
    ///   out of bounds indices are ignored. If the length is larger, the
    ///   excess entries will be filled with zeros.
    ///
    /// If the [`Avar`] table is provided, applies remapping of coordinates
    /// according to the specification.
    pub fn user_to_normalized(
        &self,
        avar: Option<&Avar>,
        user_coords: impl IntoIterator<Item = (Tag, Fixed)>,
        normalized_coords: &mut [F2Dot14],
    ) {
        normalized_coords.fill(F2Dot14::ZERO);
        let axes = self.axes().unwrap_or_default();
        let actual_len = axes.len().min(normalized_coords.len());
        let normalized_coords = &mut normalized_coords[..actual_len];

        let mut stack_fixed_coords = [Fixed::ZERO; MAX_INLINE_AVAR2_AXES];
        let mut heap_fixed_coords = Vec::new();
        let fixed_coords = if actual_len > MAX_INLINE_AVAR2_AXES {
            heap_fixed_coords.resize(actual_len, Fixed::ZERO);
            heap_fixed_coords.as_mut_slice()
        } else {
            &mut stack_fixed_coords[..actual_len]
        };
        normalize_user_coords(axes, user_coords, fixed_coords, core::convert::identity);
        apply_avar_mappings(avar, fixed_coords, |coord| *coord, core::convert::identity);

        let Some(avar) = avar else {
            to_normalized_coords(fixed_coords, normalized_coords);
            return;
        };
        if avar.version() == MajorMinor::VERSION_1_0 {
            to_normalized_coords(fixed_coords, normalized_coords);
            return;
        }

        let var_store = avar.var_store();
        let var_index_map = avar.axis_index_map();

        let mut stack_coords_2dot14 = [F2Dot14::ZERO; MAX_INLINE_AVAR2_AXES];
        let mut heap_coords_2dot14 = Vec::new();
        let coords_2dot14 = if actual_len > MAX_INLINE_AVAR2_AXES {
            heap_coords_2dot14.resize(actual_len, F2Dot14::ZERO);
            heap_coords_2dot14.as_mut_slice()
        } else {
            &mut stack_coords_2dot14[..actual_len]
        };
        for (coord_2dot14, coord) in coords_2dot14.iter_mut().zip(fixed_coords.iter()) {
            *coord_2dot14 = coord.to_f2dot14();
        }

        if let Some(Ok(varstore)) = var_store.as_ref() {
            for (i, coord) in fixed_coords.iter_mut().enumerate() {
                let var_index = if let Some(Ok(ref map)) = var_index_map {
                    match map.get(i as u32) {
                        Ok(index) => index,
                        Err(_) => continue,
                    }
                } else {
                    DeltaSetIndex {
                        outer: 0,
                        inner: i as u16,
                    }
                };
                if let Ok(delta) = varstore.compute_float_delta(var_index, coords_2dot14) {
                    *coord =
                        apply_avar2_delta(*coord, delta.to_f64()).clamp(Fixed::NEG_ONE, Fixed::ONE);
                }
            }
        }

        to_normalized_coords(fixed_coords, normalized_coords);
    }
}

impl VariationAxisRecord {
    /// Returns a normalized coordinate for the given value.
    pub fn normalize(&self, mut value: Fixed) -> Fixed {
        use core::cmp::Ordering::*;
        let min_value = self.min_value();
        let default_value = self.default_value();
        // Make sure max is >= min to avoid potential panic in clamp.
        let max_value = self.max_value().max(min_value);
        value = value.clamp(min_value, max_value);
        value = match value.cmp(&default_value) {
            Less => {
                -((default_value.saturating_sub(value)) / (default_value.saturating_sub(min_value)))
            }
            Greater => {
                (value.saturating_sub(default_value)) / (max_value.saturating_sub(default_value))
            }
            Equal => Fixed::ZERO,
        };
        value.clamp(Fixed::NEG_ONE, Fixed::ONE)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{FontRef, TableProvider};
    use types::{BigEndian, F2Dot14, Fixed, NameId, Tag};

    #[test]
    fn axes() {
        let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
        let fvar = font.fvar().unwrap();
        assert_eq!(fvar.axis_count(), 1);
        let wght = &fvar.axes().unwrap().first().unwrap();
        assert_eq!(wght.axis_tag(), Tag::new(b"wght"));
        assert_eq!(wght.min_value(), Fixed::from_f64(100.0));
        assert_eq!(wght.default_value(), Fixed::from_f64(400.0));
        assert_eq!(wght.max_value(), Fixed::from_f64(900.0));
        assert_eq!(wght.flags(), 0);
        assert_eq!(wght.axis_name_id(), NameId::new(257));
    }

    #[test]
    fn instances() {
        let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
        let fvar = font.fvar().unwrap();
        assert_eq!(fvar.instance_count(), 9);
        // There are 9 instances equally spaced from 100.0 to 900.0
        // with name id monotonically increasing starting at 258.
        let instances = fvar.instances().unwrap();
        for i in 0..9 {
            let value = 100.0 * (i + 1) as f64;
            let name_id = NameId::new(258 + i as u16);
            let instance = instances.get(i).unwrap();
            assert_eq!(instance.coordinates.len(), 1);
            assert_eq!(
                instance.coordinates.first().unwrap().get(),
                Fixed::from_f64(value)
            );
            assert_eq!(instance.subfamily_name_id, name_id);
            assert_eq!(instance.post_script_name_id, None);
        }
    }

    #[test]
    fn normalize() {
        let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
        let fvar = font.fvar().unwrap();
        let axis = fvar.axes().unwrap().first().unwrap();
        let values = [100.0, 220.0, 250.0, 400.0, 650.0, 900.0];
        let expected = [-1.0, -0.60001, -0.5, 0.0, 0.5, 1.0];
        for (value, expected) in values.into_iter().zip(expected) {
            assert_eq!(
                axis.normalize(Fixed::from_f64(value)),
                Fixed::from_f64(expected)
            );
        }
    }

    #[test]
    fn normalize_overflow() {
        // From: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=69787
        // & https://oss-fuzz.com/testcase?key=6159008335986688
        // fvar entry triggering overflow:
        // min: -26335.87451171875 def 8224.12548828125 max 8224.12548828125
        let test_case = &[
            79, 84, 84, 79, 0, 1, 32, 32, 255, 32, 32, 32, 102, 118, 97, 114, 32, 32, 32, 32, 0, 0,
            0, 28, 0, 0, 0, 41, 32, 0, 0, 0, 0, 1, 32, 32, 0, 2, 32, 32, 32, 32, 0, 0, 32, 32, 32,
            32, 32, 0, 0, 0, 0, 153, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
        ];
        let font = FontRef::new(test_case).unwrap();
        let fvar = font.fvar().unwrap();
        let axis = fvar.axes().unwrap()[1];
        // Should not panic with "attempt to subtract with overflow".
        assert_eq!(
            axis.normalize(Fixed::from_f64(0.0)),
            Fixed::from_f64(-0.2509765625)
        );
    }

    #[test]
    fn normalize_user_coords_uses_indexed_path_for_large_axis_counts() {
        let axis = VariationAxisRecord {
            axis_tag: BigEndian::from(Tag::new(b"wght")),
            min_value: BigEndian::from(Fixed::from_f64(-1.0)),
            default_value: BigEndian::from(Fixed::ZERO),
            max_value: BigEndian::from(Fixed::ONE),
            flags: BigEndian::from(0),
            axis_name_id: BigEndian::from(NameId::new(1)),
        };
        let axes = vec![axis; MAX_NORMALIZE_QUADRATIC_AXES + 1];
        let mut coords = vec![Fixed::ZERO; axes.len()];
        normalize_user_coords(
            &axes,
            [(Tag::new(b"wght"), Fixed::from_f64(0.5))],
            &mut coords,
            core::convert::identity,
        );
        assert!(coords.iter().all(|coord| *coord == Fixed::from_f64(0.5)));
    }

    #[test]
    fn normalize_user_coords_handles_duplicate_tags_in_indexed_path() {
        let wght_axis = VariationAxisRecord {
            axis_tag: BigEndian::from(Tag::new(b"wght")),
            min_value: BigEndian::from(Fixed::from_f64(-1.0)),
            default_value: BigEndian::from(Fixed::ZERO),
            max_value: BigEndian::from(Fixed::ONE),
            flags: BigEndian::from(0),
            axis_name_id: BigEndian::from(NameId::new(1)),
        };
        let wdth_axis = VariationAxisRecord {
            axis_tag: BigEndian::from(Tag::new(b"wdth")),
            min_value: BigEndian::from(Fixed::from_f64(-1.0)),
            default_value: BigEndian::from(Fixed::ZERO),
            max_value: BigEndian::from(Fixed::ONE),
            flags: BigEndian::from(0),
            axis_name_id: BigEndian::from(NameId::new(2)),
        };
        let axes = (0..(MAX_NORMALIZE_QUADRATIC_AXES + 2))
            .map(|i| if i % 2 == 0 { wght_axis } else { wdth_axis })
            .collect::<Vec<_>>();
        let mut coords = vec![Fixed::ZERO; axes.len()];
        normalize_user_coords(
            &axes,
            [(Tag::new(b"wght"), Fixed::from_f64(0.25))],
            &mut coords,
            core::convert::identity,
        );
        for (i, coord) in coords.iter().enumerate() {
            if i % 2 == 0 {
                assert_eq!(*coord, Fixed::from_f64(0.25));
            } else {
                assert_eq!(*coord, Fixed::ZERO);
            }
        }
    }

    #[test]
    fn user_to_normalized() {
        let font = FontRef::from_index(font_test_data::VAZIRMATN_VAR, 0).unwrap();
        let fvar = font.fvar().unwrap();
        let avar = font.avar().ok();
        let wght = Tag::new(b"wght");
        let axis = fvar.axes().unwrap()[0];
        let mut normalized_coords = [F2Dot14::default(); 1];
        // avar table maps 0.8 to 0.83875
        let avar_user = axis.default_value().to_f32()
            + (axis.max_value().to_f32() - axis.default_value().to_f32()) * 0.8;
        let avar_normalized = 0.83875;
        #[rustfmt::skip]
        let cases = [
            // (user, normalized)
            (-1000.0, -1.0f32),
            (100.0, -1.0),
            (200.0, -0.5),
            (400.0, 0.0),
            (900.0, 1.0),
            (avar_user, avar_normalized),
            (1251.5, 1.0),
        ];
        for (user, normalized) in cases {
            fvar.user_to_normalized(
                avar.as_ref(),
                [(wght, Fixed::from_f64(user as f64))],
                &mut normalized_coords,
            );
            assert_eq!(normalized_coords[0], F2Dot14::from_f32(normalized));
        }
    }

    #[test]
    fn avar2() {
        let font = FontRef::new(font_test_data::AVAR2_CHECKER).unwrap();
        let avar = font.avar().ok();
        let fvar = font.fvar().unwrap();
        let avar_axis = Tag::new(b"AVAR");
        let avwk_axis = Tag::new(b"AVWK");
        let mut normalized_coords = [F2Dot14::default(); 2];
        let cases = [
            ((100.0, 0.0), (1.0, 1.0)),
            ((50.0, 0.0), (0.5, 0.5)),
            ((0.0, 50.0), (0.0, 0.5)),
        ];
        for (user, expected) in cases {
            fvar.user_to_normalized(
                avar.as_ref(),
                [
                    (avar_axis, Fixed::from_f64(user.0)),
                    (avwk_axis, Fixed::from_f64(user.1)),
                ],
                &mut normalized_coords,
            );
            assert_eq!(normalized_coords[0], F2Dot14::from_f32(expected.0));
            assert_eq!(normalized_coords[1], F2Dot14::from_f32(expected.1));
        }
    }

    #[test]
    fn avar2_no_panic_with_wrong_size_coords_array() {
        // this font has 2 axes
        let font = FontRef::new(font_test_data::AVAR2_CHECKER).unwrap();
        let avar = font.avar().ok();
        let fvar = font.fvar().unwrap();
        // output array too small
        let mut normalized_coords = [F2Dot14::default(); 1];
        fvar.user_to_normalized(avar.as_ref(), [], &mut normalized_coords);
        // output array too large
        let mut normalized_coords = [F2Dot14::default(); 4];
        fvar.user_to_normalized(avar.as_ref(), [], &mut normalized_coords);
    }

    #[test]
    fn avar2_preserves_16_16_precision_until_final_rounding() {
        // Quantizing to 2.14 before applying the avar2 delta would produce 0x0002
        // here, but HarfBuzz's 16.16 path produces 0x0001.
        let coord = Fixed::from_bits(3);
        assert_eq!(
            super::apply_avar2_delta(coord, 0.5).to_f2dot14(),
            F2Dot14::from_bits(1)
        );
    }

    #[test]
    fn avar2_clamps_hidden_axis_for_amstelvar_repro() {
        let font = FontRef::new(font_test_data::AMSTELVAR_AVAR2_A).unwrap();
        let avar = font.avar().ok();
        let fvar = font.fvar().unwrap();
        let mut normalized_coords = [F2Dot14::ZERO; 12];

        fvar.user_to_normalized(
            avar.as_ref(),
            [(Tag::new(b"wght"), Fixed::from_f64(1000.0))],
            &mut normalized_coords,
        );

        assert_eq!(normalized_coords[1], F2Dot14::from_bits(-5370)); // XTRA
        assert_eq!(normalized_coords[2], F2Dot14::ONE); // XOPQ clamped from > 1
        assert_eq!(normalized_coords[3], F2Dot14::from_bits(2254)); // YOPQ
        assert_eq!(normalized_coords[11], F2Dot14::ONE); // wght
    }
}