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
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
// 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 Discriminant for MyLookup<'_, ()> {
    fn read_discriminant(data: FontData<'_>) -> Result<u16, ReadError> {
        data.read_at(0)
    }
}

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

impl<T> ReadArgs for MyLookup<'_, T> {
    type Args = ();
}

impl<'a, T> FontRead<'a> for MyLookup<'a, T> {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
            return Err(ReadError::OutOfBounds);
        };
        Ok(Self {
            data,
            offset_type: std::marker::PhantomData,
        })
    }
}

impl<'a, T> MyLookup<'a, T> {
    #[allow(dead_code)]
    /// Replace the specific generic type on this implementation with `()`
    pub(crate) fn of_unit_type(&self) -> MyLookup<'a, ()> {
        MyLookup {
            data: self.data,
            offset_type: std::marker::PhantomData,
        }
    }
}

/// The fields at the start of the table, which are read directly.
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
#[repr(C, packed)]
#[doc(hidden)]
pub struct MyLookupFixedHeader {
    pub lookup_type: BigEndian<u16>,
    pub sub_table_count: BigEndian<u16>,
}

#[allow(clippy::absurd_extreme_comparisons)]
const _: () = assert!(std::mem::size_of::<MyLookupFixedHeader>() <= MyLookup::<()>::MIN_SIZE);

/// A generic table parameterized by the type of its subtable offsets.
#[derive(Clone)]
pub struct MyLookup<'a, T = ()> {
    data: TableDataRef<'a, MyLookupFixedHeader>,
    offset_type: std::marker::PhantomData<*const T>,
}

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

    /// Determines the concrete type of T
    pub fn lookup_type(&self) -> u16 {
        self.data.header().lookup_type.get()
    }

    /// Number of subtables
    pub fn sub_table_count(&self) -> u16 {
        self.data.header().sub_table_count.get()
    }

    /// Offsets to subtables, from beginning of this table
    pub fn subtable_offsets(&self) -> &'a [BigEndian<Offset16>] {
        let range = self.subtable_offsets_byte_range();
        self.offset_data()
            .read_array(range)
            .ok()
            .unwrap_or_default()
    }

    /// A dynamically resolving wrapper for [`subtable_offsets`][Self::subtable_offsets].
    pub fn subtables(&self) -> ArrayOfOffsets<'a, T, Offset16>
    where
        T: FontRead<'a, Args = ()>,
    {
        let data = self.offset_data();
        let offsets = self.subtable_offsets();
        ArrayOfOffsets::new(offsets, data, ())
    }

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

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

    pub fn subtable_offsets_byte_range(&self) -> Range<usize> {
        let sub_table_count = self.sub_table_count();
        let start = self.sub_table_count_byte_range().end;
        let end =
            start + (transforms::to_usize(sub_table_count)).saturating_mul(Offset16::RAW_BYTE_LEN);
        start..end
    }
}

const _: () = assert!(FontData::default_data_long_enough(MyLookup::<()>::MIN_SIZE));

impl<T> Default for MyLookup<'_, T> {
    fn default() -> Self {
        Self {
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
            offset_type: std::marker::PhantomData,
        }
    }
}

/// A concrete subtable with a format field (format group)
#[derive(Clone)]
pub enum MySubtable<'a> {
    Format1(MySubtableFormat1<'a>),
    Format2(MySubtableFormat2<'a>),
}

impl Default for MySubtable<'_> {
    fn default() -> Self {
        Self::Format1(Default::default())
    }
}

impl<'a> MySubtable<'a> {
    ///Return the `FontData` used to resolve offsets for this table.
    pub fn offset_data(&self) -> FontData<'a> {
        match self {
            Self::Format1(item) => item.offset_data(),
            Self::Format2(item) => item.offset_data(),
        }
    }

    pub fn format(&self) -> u16 {
        match self {
            Self::Format1(item) => item.format(),
            Self::Format2(item) => item.format(),
        }
    }
}

impl ReadArgs for MySubtable<'_> {
    type Args = ();
}

impl<'a> FontRead<'a> for MySubtable<'a> {
    #[inline]
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        let format: u16 = data.read_at(0usize)?;
        match format {
            MySubtableFormat1::FORMAT => Ok(Self::Format1(FontRead::read(data)?)),
            MySubtableFormat2::FORMAT => Ok(Self::Format2(FontRead::read(data)?)),
            other => Err(ReadError::InvalidFormat(other.into())),
        }
    }
}

impl<'a> MinByteRange<'a> for MySubtable<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        match self {
            Self::Format1(item) => item.min_byte_range(),
            Self::Format2(item) => item.min_byte_range(),
        }
    }
    fn min_table_bytes(&self) -> &'a [u8] {
        match self {
            Self::Format1(item) => item.min_table_bytes(),
            Self::Format2(item) => item.min_table_bytes(),
        }
    }
}

impl Format<u16> for MySubtableFormat1<'_> {
    const FORMAT: u16 = 1;
}

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

impl ReadArgs for MySubtableFormat1<'_> {
    type Args = ();
}

impl<'a> FontRead<'a> for MySubtableFormat1<'a> {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
            return Err(ReadError::OutOfBounds);
        };
        Ok(Self { data })
    }
}

/// The fields at the start of the table, which are read directly.
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
#[repr(C, packed)]
#[doc(hidden)]
pub struct MySubtableFormat1FixedHeader {
    pub format: BigEndian<u16>,
    pub value: BigEndian<u16>,
}

#[allow(clippy::absurd_extreme_comparisons)]
const _: () =
    assert!(std::mem::size_of::<MySubtableFormat1FixedHeader>() <= MySubtableFormat1::MIN_SIZE);

#[derive(Clone)]
pub struct MySubtableFormat1<'a> {
    data: TableDataRef<'a, MySubtableFormat1FixedHeader>,
}

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

    pub fn format(&self) -> u16 {
        self.data.header().format.get()
    }

    pub fn value(&self) -> u16 {
        self.data.header().value.get()
    }

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

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

const _: () = assert!(FontData::default_data_long_enough(
    MySubtableFormat1::MIN_SIZE
));

impl Default for MySubtableFormat1<'_> {
    fn default() -> Self {
        Self {
            data: TableDataRef::new(FontData::default_format_1_u16_table_data().as_bytes())
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
        }
    }
}

impl Format<u16> for MySubtableFormat2<'_> {
    const FORMAT: u16 = 2;
}

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

impl ReadArgs for MySubtableFormat2<'_> {
    type Args = ();
}

impl<'a> FontRead<'a> for MySubtableFormat2<'a> {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
            return Err(ReadError::OutOfBounds);
        };
        Ok(Self { data })
    }
}

/// The fields at the start of the table, which are read directly.
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
#[repr(C, packed)]
#[doc(hidden)]
pub struct MySubtableFormat2FixedHeader {
    pub format: BigEndian<u16>,
    pub count: BigEndian<u16>,
}

#[allow(clippy::absurd_extreme_comparisons)]
const _: () =
    assert!(std::mem::size_of::<MySubtableFormat2FixedHeader>() <= MySubtableFormat2::MIN_SIZE);

#[derive(Clone)]
pub struct MySubtableFormat2<'a> {
    data: TableDataRef<'a, MySubtableFormat2FixedHeader>,
}

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

    pub fn format(&self) -> u16 {
        self.data.header().format.get()
    }

    pub fn count(&self) -> u16 {
        self.data.header().count.get()
    }

    pub fn values(&self) -> &'a [BigEndian<u16>] {
        let range = self.values_byte_range();
        self.offset_data()
            .read_array(range)
            .ok()
            .unwrap_or_default()
    }

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

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

    pub fn values_byte_range(&self) -> Range<usize> {
        let count = self.count();
        let start = self.count_byte_range().end;
        let end = start + (transforms::to_usize(count)).saturating_mul(u16::RAW_BYTE_LEN);
        start..end
    }
}

/// The group enum dispatching on lookup_type.
pub enum MyLookupGroup<'a> {
    TypeOne(MyLookup<'a, MySubtable<'a>>),
    TypeTwo(MyLookup<'a, MySubtableFormat1<'a>>),
}

impl Default for MyLookupGroup<'_> {
    fn default() -> Self {
        Self::TypeOne(Default::default())
    }
}

impl ReadArgs for MyLookupGroup<'_> {
    type Args = ();
}

impl<'a> FontRead<'a> for MyLookupGroup<'a> {
    fn read_with_args(bytes: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        let discriminant = MyLookup::read_discriminant(bytes)?;
        match discriminant {
            1 => Ok(MyLookupGroup::TypeOne(FontRead::read(bytes)?)),
            2 => Ok(MyLookupGroup::TypeTwo(FontRead::read(bytes)?)),
            other => Err(ReadError::InvalidFormat(other.into())),
        }
    }
}

impl<'a> MyLookupGroup<'a> {
    #[allow(dead_code)]
    /// Return the inner table, removing the specific generics.
    ///
    /// This lets us return a single concrete type we can call methods on.
    pub(crate) fn of_unit_type(&self) -> MyLookup<'a, ()> {
        match self {
            MyLookupGroup::TypeOne(inner) => inner.of_unit_type(),
            MyLookupGroup::TypeTwo(inner) => inner.of_unit_type(),
        }
    }
}

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

impl ReadArgs for ContainsLookupGroup<'_> {
    type Args = ();
}

impl<'a> FontRead<'a> for ContainsLookupGroup<'a> {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
            return Err(ReadError::OutOfBounds);
        };
        Ok(Self { data })
    }
}

/// The fields at the start of the table, which are read directly.
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
#[repr(C, packed)]
#[doc(hidden)]
pub struct ContainsLookupGroupFixedHeader {
    pub version: BigEndian<u16>,
    pub lookup_offset: BigEndian<Offset16>,
}

#[allow(clippy::absurd_extreme_comparisons)]
const _: () =
    assert!(std::mem::size_of::<ContainsLookupGroupFixedHeader>() <= ContainsLookupGroup::MIN_SIZE);

#[derive(Clone)]
pub struct ContainsLookupGroup<'a> {
    data: TableDataRef<'a, ContainsLookupGroupFixedHeader>,
}

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

    pub fn version(&self) -> u16 {
        self.data.header().version.get()
    }

    pub fn lookup_offset(&self) -> Offset16 {
        self.data.header().lookup_offset.get()
    }

    /// Attempt to resolve [`lookup_offset`][Self::lookup_offset].
    pub fn lookup(&self) -> Result<MyLookupGroup<'a>, ReadError> {
        let data = self.offset_data();
        self.lookup_offset().resolve(data)
    }

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

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

const _: () = assert!(FontData::default_data_long_enough(
    ContainsLookupGroup::MIN_SIZE
));

impl Default for ContainsLookupGroup<'_> {
    fn default() -> Self {
        Self {
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
        }
    }
}