read-fonts 0.39.2

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
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
// THIS FILE IS AUTOGENERATED.
// Any changes to this file will be overwritten.
// For more information about how codegen works, see font-codegen/README.md

#[allow(unused_imports)]
use crate::codegen_prelude::*;

impl<'a> MinByteRange<'a> for Morx<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.chains_byte_range().end
    }
    fn min_table_bytes(&self) -> &'a [u8] {
        let range = self.min_byte_range();
        self.data.as_bytes().get(range).unwrap_or_default()
    }
}

impl TopLevelTable for Morx<'_> {
    /// `morx`
    const TAG: Tag = Tag::new(b"morx");
}

impl<'a> FontRead<'a> for Morx<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        Ok(Self { data })
    }
}

/// The [morx (Extended Glyph Metamorphosis)](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6morx.html) table.
#[derive(Clone)]
pub struct Morx<'a> {
    data: FontData<'a>,
}

#[allow(clippy::needless_lifetimes)]
impl<'a> Morx<'a> {
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
    basic_table_impls!(impl_the_methods);

    /// Version number of the extended glyph metamorphosis table (either 2 or 3).
    pub fn version(&self) -> u16 {
        let range = self.version_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Number of metamorphosis chains contained in this table.
    pub fn n_chains(&self) -> u32 {
        let range = self.n_chains_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    pub fn chains(&self) -> VarLenArray<'a, Chain<'a>> {
        let range = self.chains_byte_range();
        self.data
            .split_off(range.start)
            .and_then(|d| VarLenArray::read(d).ok())
            .unwrap_or_default()
    }

    pub fn version_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u16::RAW_BYTE_LEN
    }

    pub fn unused_byte_range(&self) -> Range<usize> {
        let start = self.version_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }

    pub fn n_chains_byte_range(&self) -> Range<usize> {
        let start = self.unused_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn chains_byte_range(&self) -> Range<usize> {
        let n_chains = self.n_chains();
        let start = self.n_chains_byte_range().end;
        start..start + {
            let data = self.data.split_off(start).unwrap_or_default();
            <Chain as VarSize>::total_len_for_count(data, n_chains as usize).unwrap_or(0)
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Morx<'a> {
    fn type_name(&self) -> &str {
        "Morx"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("version", self.version())),
            1usize => Some(Field::new("n_chains", self.n_chains())),
            2usize => Some(Field::new(
                "chains",
                traversal::FieldType::var_array("Chain", self.chains(), self.offset_data()),
            )),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for Morx<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

impl<'a> MinByteRange<'a> for Chain<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.subtables_byte_range().end
    }
    fn min_table_bytes(&self) -> &'a [u8] {
        let range = self.min_byte_range();
        self.data.as_bytes().get(range).unwrap_or_default()
    }
}

impl<'a> FontRead<'a> for Chain<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        Ok(Self { data })
    }
}

/// A chain in a `morx` table.
#[derive(Clone)]
pub struct Chain<'a> {
    data: FontData<'a>,
}

#[allow(clippy::needless_lifetimes)]
impl<'a> Chain<'a> {
    pub const MIN_SIZE: usize =
        (u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
    basic_table_impls!(impl_the_methods);

    /// The default specification for subtables.
    pub fn default_flags(&self) -> u32 {
        let range = self.default_flags_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Total byte count, including this header; must be a multiple of 4.
    pub fn chain_length(&self) -> u32 {
        let range = self.chain_length_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Number of feature subtable entries.
    pub fn n_feature_entries(&self) -> u32 {
        let range = self.n_feature_entries_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// The number of subtables in the chain.
    pub fn n_subtables(&self) -> u32 {
        let range = self.n_subtables_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Feature entries for this chain.
    pub fn features(&self) -> &'a [Feature] {
        let range = self.features_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    /// Array of chain subtables.
    pub fn subtables(&self) -> VarLenArray<'a, Subtable<'a>> {
        let range = self.subtables_byte_range();
        self.data
            .split_off(range.start)
            .and_then(|d| VarLenArray::read(d).ok())
            .unwrap_or_default()
    }

    pub fn default_flags_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn chain_length_byte_range(&self) -> Range<usize> {
        let start = self.default_flags_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn n_feature_entries_byte_range(&self) -> Range<usize> {
        let start = self.chain_length_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn n_subtables_byte_range(&self) -> Range<usize> {
        let start = self.n_feature_entries_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn features_byte_range(&self) -> Range<usize> {
        let n_feature_entries = self.n_feature_entries();
        let start = self.n_subtables_byte_range().end;
        start..start + (n_feature_entries as usize).saturating_mul(Feature::RAW_BYTE_LEN)
    }

    pub fn subtables_byte_range(&self) -> Range<usize> {
        let n_subtables = self.n_subtables();
        let start = self.features_byte_range().end;
        start..start + {
            let data = self.data.split_off(start).unwrap_or_default();
            <Subtable as VarSize>::total_len_for_count(data, n_subtables as usize).unwrap_or(0)
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Chain<'a> {
    fn type_name(&self) -> &str {
        "Chain"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("default_flags", self.default_flags())),
            1usize => Some(Field::new("chain_length", self.chain_length())),
            2usize => Some(Field::new("n_feature_entries", self.n_feature_entries())),
            3usize => Some(Field::new("n_subtables", self.n_subtables())),
            4usize => Some(Field::new(
                "features",
                traversal::FieldType::array_of_records(
                    stringify!(Feature),
                    self.features(),
                    self.offset_data(),
                ),
            )),
            5usize => Some(Field::new(
                "subtables",
                traversal::FieldType::var_array("Subtable", self.subtables(), self.offset_data()),
            )),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for Chain<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// Used to compute the sub-feature flags for a list of requested features and settings.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct Feature {
    /// The type of feature.
    pub feature_type: BigEndian<u16>,
    /// The feature's setting (aka selector).
    pub feature_settings: BigEndian<u16>,
    /// Flags for the settings that this feature and setting enables.
    pub enable_flags: BigEndian<u32>,
    /// Complement of flags for the settings that this feature and setting disable.
    pub disable_flags: BigEndian<u32>,
}

impl Feature {
    /// The type of feature.
    pub fn feature_type(&self) -> u16 {
        self.feature_type.get()
    }

    /// The feature's setting (aka selector).
    pub fn feature_settings(&self) -> u16 {
        self.feature_settings.get()
    }

    /// Flags for the settings that this feature and setting enables.
    pub fn enable_flags(&self) -> u32 {
        self.enable_flags.get()
    }

    /// Complement of flags for the settings that this feature and setting disable.
    pub fn disable_flags(&self) -> u32 {
        self.disable_flags.get()
    }
}

impl FixedSize for Feature {
    const RAW_BYTE_LEN: usize =
        u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeRecord<'a> for Feature {
    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
        RecordResolver {
            name: "Feature",
            get_field: Box::new(move |idx, _data| match idx {
                0usize => Some(Field::new("feature_type", self.feature_type())),
                1usize => Some(Field::new("feature_settings", self.feature_settings())),
                2usize => Some(Field::new("enable_flags", self.enable_flags())),
                3usize => Some(Field::new("disable_flags", self.disable_flags())),
                _ => None,
            }),
            data,
        }
    }
}

impl<'a> MinByteRange<'a> for Subtable<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.data_byte_range().end
    }
    fn min_table_bytes(&self) -> &'a [u8] {
        let range = self.min_byte_range();
        self.data.as_bytes().get(range).unwrap_or_default()
    }
}

impl<'a> FontRead<'a> for Subtable<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        Ok(Self { data })
    }
}

/// A subtable in a `morx` chain.
#[derive(Clone)]
pub struct Subtable<'a> {
    data: FontData<'a>,
}

#[allow(clippy::needless_lifetimes)]
impl<'a> Subtable<'a> {
    pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
    basic_table_impls!(impl_the_methods);

    /// Total subtable length, including this header.
    pub fn length(&self) -> u32 {
        let range = self.length_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Coverage flags and subtable type.
    pub fn coverage(&self) -> u32 {
        let range = self.coverage_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// The 32-bit mask identifying which subtable this is (the subtable being executed if the AND of this value and the processed defaultFlags is nonzero).
    pub fn sub_feature_flags(&self) -> u32 {
        let range = self.sub_feature_flags_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Data for specific subtable.
    pub fn data(&self) -> &'a [u8] {
        let range = self.data_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    pub fn length_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn coverage_byte_range(&self) -> Range<usize> {
        let start = self.length_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn sub_feature_flags_byte_range(&self) -> Range<usize> {
        let start = self.coverage_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn data_byte_range(&self) -> Range<usize> {
        let start = self.sub_feature_flags_byte_range().end;
        start..start + self.data.len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Subtable<'a> {
    fn type_name(&self) -> &str {
        "Subtable"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("length", self.length())),
            1usize => Some(Field::new("coverage", self.coverage())),
            2usize => Some(Field::new("sub_feature_flags", self.sub_feature_flags())),
            3usize => Some(Field::new("data", self.data())),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for Subtable<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// Entry payload in a contextual subtable state machine.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct ContextualEntryData {
    /// Index of the substitution table for the marked glyph (use 0xFFFF for
    /// none).
    pub mark_index: BigEndian<u16>,
    /// Index of the substitution table for the current glyph (use 0xFFFF for
    /// none)
    pub current_index: BigEndian<u16>,
}

impl ContextualEntryData {
    /// Index of the substitution table for the marked glyph (use 0xFFFF for
    /// none).
    pub fn mark_index(&self) -> u16 {
        self.mark_index.get()
    }

    /// Index of the substitution table for the current glyph (use 0xFFFF for
    /// none)
    pub fn current_index(&self) -> u16 {
        self.current_index.get()
    }
}

impl FixedSize for ContextualEntryData {
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeRecord<'a> for ContextualEntryData {
    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
        RecordResolver {
            name: "ContextualEntryData",
            get_field: Box::new(move |idx, _data| match idx {
                0usize => Some(Field::new("mark_index", self.mark_index())),
                1usize => Some(Field::new("current_index", self.current_index())),
                _ => None,
            }),
            data,
        }
    }
}

/// Entry payload in an insertion subtable state machine.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct InsertionEntryData {
    /// Zero-based index into the insertion glyph table. The number of glyphs
    /// to be inserted is contained in the currentInsertCount field in the
    /// flags (see below). A value of 0xFFFF indicates no insertion is to be done.
    pub current_insert_index: BigEndian<u16>,
    /// Zero-based index into the insertion glyph table. The number of glyphs
    /// to be inserted is contained in the markedInsertCount field in the
    /// flags (see below). A value of 0xFFFF indicates no insertion is to be
    /// done.
    pub marked_insert_index: BigEndian<u16>,
}

impl InsertionEntryData {
    /// Zero-based index into the insertion glyph table. The number of glyphs
    /// to be inserted is contained in the currentInsertCount field in the
    /// flags (see below). A value of 0xFFFF indicates no insertion is to be done.
    pub fn current_insert_index(&self) -> u16 {
        self.current_insert_index.get()
    }

    /// Zero-based index into the insertion glyph table. The number of glyphs
    /// to be inserted is contained in the markedInsertCount field in the
    /// flags (see below). A value of 0xFFFF indicates no insertion is to be
    /// done.
    pub fn marked_insert_index(&self) -> u16 {
        self.marked_insert_index.get()
    }
}

impl FixedSize for InsertionEntryData {
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeRecord<'a> for InsertionEntryData {
    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
        RecordResolver {
            name: "InsertionEntryData",
            get_field: Box::new(move |idx, _data| match idx {
                0usize => Some(Field::new(
                    "current_insert_index",
                    self.current_insert_index(),
                )),
                1usize => Some(Field::new(
                    "marked_insert_index",
                    self.marked_insert_index(),
                )),
                _ => None,
            }),
            data,
        }
    }
}