read_fonts/tables/
ift.rs

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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Incremental Font Transfer [Patch Map](https://w3c.github.io/IFT/Overview.html#font-format-extensions)

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

use std::str;

pub const IFT_TAG: types::Tag = Tag::new(b"IFT ");
pub const IFTX_TAG: types::Tag = Tag::new(b"IFTX");

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Default, Ord, PartialOrd, Hash)]
pub struct CompatibilityId([u8; 16]);

impl CompatibilityId {
    pub fn new(value: [u8; 16]) -> Self {
        CompatibilityId(value)
    }

    pub fn from_u32s(values: [u32; 4]) -> Self {
        let mut data = [0u8; 16];

        for i in 0..4 {
            let be_bytes = values[i].to_be_bytes();
            for j in 0..4 {
                data[i * 4 + j] = be_bytes[j];
            }
        }

        CompatibilityId(data)
    }

    pub fn as_slice(&self) -> &[u8] {
        &self.0
    }
}

impl Scalar for CompatibilityId {
    type Raw = [u8; 16];

    fn from_raw(raw: Self::Raw) -> Self {
        CompatibilityId(raw)
    }

    fn to_raw(self) -> Self::Raw {
        self.0
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct U8Or16(u16);

impl ReadArgs for U8Or16 {
    type Args = u16;
}

impl ComputeSize for U8Or16 {
    fn compute_size(max_entry_index: &u16) -> Result<usize, ReadError> {
        Ok(if *max_entry_index < 256 { 1 } else { 2 })
    }
}

impl FontReadWithArgs<'_> for U8Or16 {
    fn read_with_args(data: FontData<'_>, max_entry_index: &Self::Args) -> Result<Self, ReadError> {
        if *max_entry_index < 256 {
            data.read_at::<u8>(0).map(|v| Self(v as u16))
        } else {
            data.read_at::<u16>(0).map(Self)
        }
    }
}

impl U8Or16 {
    #[inline]
    pub fn get(self) -> u16 {
        self.0
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct U16Or24(u32);

impl ReadArgs for U16Or24 {
    type Args = GlyphKeyedFlags;
}

impl ComputeSize for U16Or24 {
    fn compute_size(flags: &GlyphKeyedFlags) -> Result<usize, ReadError> {
        // See: https://w3c.github.io/IFT/Overview.html#glyph-keyed-patch-flags
        Ok(if flags.contains(GlyphKeyedFlags::WIDE_GLYPH_IDS) {
            3
        } else {
            2
        })
    }
}

impl FontReadWithArgs<'_> for U16Or24 {
    fn read_with_args(data: FontData<'_>, flags: &Self::Args) -> Result<Self, ReadError> {
        if flags.contains(GlyphKeyedFlags::WIDE_GLYPH_IDS) {
            data.read_at::<Uint24>(0).map(|v| Self(v.to_u32()))
        } else {
            data.read_at::<u16>(0).map(|v| Self(v as u32))
        }
    }
}

impl U16Or24 {
    #[inline]
    pub fn get(self) -> u32 {
        self.0
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IdDeltaOrLength(i32);

impl ReadArgs for IdDeltaOrLength {
    type Args = Offset32;
}

impl ComputeSize for IdDeltaOrLength {
    fn compute_size(entry_id_string_data_offset: &Offset32) -> Result<usize, ReadError> {
        // This field is either a u16 or an int24 depending on whether or not string data
        // is present. See: <https://w3c.github.io/IFT/Overview.html#mapping-entry-entryiddelta>
        Ok(if entry_id_string_data_offset.is_null() {
            3
        } else {
            2
        })
    }
}

impl FontReadWithArgs<'_> for IdDeltaOrLength {
    fn read_with_args(
        data: FontData<'_>,
        entry_id_string_data_offset: &Self::Args,
    ) -> Result<Self, ReadError> {
        if entry_id_string_data_offset.is_null() {
            data.read_at::<Int24>(0).map(|v| Self(i32::from(v)))
        } else {
            data.read_at::<u16>(0).map(|v| Self(v as i32))
        }
    }
}

impl IdDeltaOrLength {
    #[inline]
    pub fn into_inner(self) -> i32 {
        self.0
    }
}

impl<'a> PatchMapFormat1<'a> {
    pub fn gid_to_entry_iter(&'a self) -> impl Iterator<Item = (GlyphId, u16)> + 'a {
        GidToEntryIter {
            glyph_map: self.glyph_map().ok(),
            glyph_count: self.glyph_count().to_u32(),
            gid: self
                .glyph_map()
                .map(|glyph_map| glyph_map.first_mapped_glyph() as u32)
                .unwrap_or(0),
        }
        .filter(|(_, entry_index)| *entry_index > 0)
    }

    pub fn entry_count(&self) -> u32 {
        self.max_entry_index() as u32 + 1
    }

    pub fn uri_template_as_string(&self) -> Result<&str, ReadError> {
        str::from_utf8(self.uri_template())
            .map_err(|_| ReadError::MalformedData("Invalid UTF8 encoding for uri template."))
    }

    pub fn is_entry_applied(&self, entry_index: u16) -> bool {
        let byte_index = entry_index / 8;
        let bit_mask = 1 << (entry_index % 8);
        self.applied_entries_bitmap()
            .get(byte_index as usize)
            .map(|byte| byte & bit_mask != 0)
            .unwrap_or(false)
    }
}

impl PatchMapFormat2<'_> {
    pub fn uri_template_as_string(&self) -> Result<&str, ReadError> {
        str::from_utf8(self.uri_template())
            .map_err(|_| ReadError::MalformedData("Invalid UTF8 encoding for uri template."))
    }
}

impl FeatureMap<'_> {
    pub fn entry_records_size(&self, max_entry_index: u16) -> Result<usize, ReadError> {
        let field_width = if max_entry_index < 256 { 1 } else { 2 };
        let mut num_bytes = 0usize;
        for record in self.feature_records().iter() {
            num_bytes += record?.entry_map_count().get() as usize * field_width * 2;
        }
        Ok(num_bytes)
    }
}

struct GidToEntryIter<'a> {
    glyph_map: Option<GlyphMap<'a>>,
    glyph_count: u32,
    gid: u32,
}

impl Iterator for GidToEntryIter<'_> {
    type Item = (GlyphId, u16);

    fn next(&mut self) -> Option<Self::Item> {
        let glyph_map = self.glyph_map.as_ref()?;

        let cur_gid = self.gid;
        self.gid += 1;

        if cur_gid >= self.glyph_count {
            return None;
        }

        let index = cur_gid as usize - glyph_map.first_mapped_glyph() as usize;
        glyph_map
            .entry_index()
            .get(index)
            .ok()
            .map(|entry_index| (cur_gid.into(), entry_index.0))
    }
}

impl<'a> GlyphPatches<'a> {
    /// Returns an iterator over the per glyph data for the table with the given index.
    pub fn glyph_data_for_table(
        &'a self,
        table_index: usize,
    ) -> impl Iterator<Item = Result<(GlyphId, &'a [u8]), ReadError>> {
        let glyph_count = self.glyph_count() as usize;
        let start_index = table_index * glyph_count;
        let start_it = self.glyph_data_offsets().iter().skip(start_index);
        let end_it = self.glyph_data_offsets().iter().skip(start_index + 1);
        let glyphs = self.glyph_ids().iter().take(glyph_count);

        let it = glyphs.zip(start_it.zip(end_it)).map(|(gid, (start, end))| {
            let start = start.get();
            let end = end.get();
            (gid, start, end)
        });
        GlyphDataIterator {
            patches: self,
            offset_iterator: it,
            previous_gid: None,
            failed: false,
        }
    }
}

/// Custom iterator for glyph keyed glyph data which allows us to terminate the iterator on the first error.
struct GlyphDataIterator<'a, T>
where
    T: Iterator<Item = (Result<U16Or24, ReadError>, Offset32, Offset32)>,
{
    patches: &'a GlyphPatches<'a>,
    offset_iterator: T,
    previous_gid: Option<GlyphId>,
    failed: bool,
}

impl<'a, T> Iterator for GlyphDataIterator<'a, T>
where
    T: Iterator<Item = (Result<U16Or24, ReadError>, Offset32, Offset32)>,
{
    type Item = Result<(GlyphId, &'a [u8]), ReadError>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.failed {
            return None;
        }

        let (gid, start, end) = self.offset_iterator.next()?;
        let gid = match gid {
            Ok(gid) => GlyphId::new(gid.get()),
            Err(err) => {
                self.failed = true;
                return Some(Err(err));
            }
        };

        if let Some(previous_gid) = self.previous_gid {
            if gid <= previous_gid {
                self.failed = true;
                return Some(Err(ReadError::MalformedData(
                    "Glyph IDs are unsorted or duplicated.",
                )));
            }
        }
        self.previous_gid = Some(gid);

        let len = match end
            .to_u32()
            .checked_sub(start.to_u32())
            .ok_or(ReadError::MalformedData(
                "glyph data offsets are not ascending.",
            )) {
            Ok(len) => len as usize,
            Err(err) => {
                self.failed = true;
                return Some(Err(err));
            }
        };

        let data: Result<GlyphData<'a>, ReadError> = self.patches.resolve_offset(start);
        let data = match data {
            Ok(data) => data.data,
            Err(err) => {
                self.failed = true;
                return Some(Err(err));
            }
        };

        let Some(data) = data.as_bytes().get(..len) else {
            self.failed = true;
            return Some(Err(ReadError::OutOfBounds));
        };

        Some(Ok((gid, data)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use font_test_data::ift as test_data;

    // TODO(garretrieger) - more tests (as functionality is implemented):
    // - Test where entryIndex array has len 0 (eg. all glyphs map to 0)
    // - Test which appliedEntriesBitmap > 1 byte
    // - Test w/ feature map populated.
    // - Test enforced minimum entry count of > 0.
    // - Test where entryIndex is a u16.
    // - Invalid table (too short).
    // - Invalid UTF8 sequence in uri template.
    // - Compat ID is to short.
    // - invalid entry map array (too short)
    // - feature map with short entry indices.

    #[test]
    fn format_1_gid_to_u8_entry_iter() {
        let data = test_data::simple_format1();
        let table = Ift::read(FontData::new(&data)).unwrap();
        let Ift::Format1(map) = table else {
            panic!("Not format 1.");
        };
        let entries: Vec<(GlyphId, u16)> = map.gid_to_entry_iter().collect();

        assert_eq!(
            entries,
            vec![(1u32.into(), 2), (2u32.into(), 1), (4u32.into(), 1)]
        );
    }

    #[test]
    fn format_1_gid_to_u16_entry_iter() {
        let data = test_data::u16_entries_format1();
        let table = Ift::read(FontData::new(&data)).unwrap();
        let Ift::Format1(map) = table else {
            panic!("Not format 1.");
        };
        let entries: Vec<(GlyphId, u16)> = map.gid_to_entry_iter().collect();

        assert_eq!(
            entries,
            vec![
                (2u32.into(), 0x50),
                (3u32.into(), 0x51),
                (4u32.into(), 0x12c),
                (5u32.into(), 0x12c),
                (6u32.into(), 0x50)
            ]
        );
    }

    #[test]
    fn format_1_feature_map() {
        let data = test_data::feature_map_format1();
        let table = Ift::read(FontData::new(&data)).unwrap();
        let Ift::Format1(map) = table else {
            panic!("Not format 1.");
        };

        let Some(feature_map_result) = map.feature_map() else {
            panic!("should have a non null feature map.");
        };

        let Ok(feature_map) = feature_map_result else {
            panic!("should have a valid feature map.");
        };

        assert_eq!(feature_map.feature_records().len(), 3);

        let fr0 = feature_map.feature_records().get(0).unwrap();
        assert_eq!(fr0.feature_tag(), Tag::new(b"dlig"));
        assert_eq!(*fr0.first_new_entry_index(), U8Or16(0x190));
        assert_eq!(*fr0.entry_map_count(), U8Or16(0x01));

        let fr1 = feature_map.feature_records().get(1).unwrap();
        assert_eq!(fr1.feature_tag(), Tag::new(b"liga"));
        assert_eq!(*fr1.first_new_entry_index(), U8Or16(0x180));
        assert_eq!(*fr1.entry_map_count(), U8Or16(0x02));
    }

    #[test]
    fn compatibility_id() {
        let data = test_data::simple_format1();
        let table = Ift::read(FontData::new(&data)).unwrap();
        let Ift::Format1(map) = table else {
            panic!("Not format 1.");
        };

        assert_eq!(
            map.compatibility_id(),
            CompatibilityId::from_u32s([1, 2, 3, 4])
        );
    }

    #[test]
    fn is_entry_applied() {
        let data = test_data::simple_format1();
        let table = Ift::read(FontData::new(&data)).unwrap();
        let Ift::Format1(map) = table else {
            panic!("Not format 1.");
        };
        assert!(!map.is_entry_applied(0));
        assert!(map.is_entry_applied(1));
        assert!(!map.is_entry_applied(2));
    }

    #[test]
    fn uri_template_as_string() {
        let data = test_data::simple_format1();
        let table = Ift::read(FontData::new(&data)).unwrap();
        let Ift::Format1(map) = table else {
            panic!("Not format 1.");
        };

        assert_eq!(Ok("ABCDEFɤ"), map.uri_template_as_string());
    }

    #[test]
    fn glyph_keyed_glyph_data_for_one_table() {
        let data = test_data::glyf_u16_glyph_patches();
        let table = GlyphPatches::read(FontData::new(&data), GlyphKeyedFlags::NONE).unwrap();

        let it = table.glyph_data_for_table(0);

        assert_eq!(
            it.collect::<Vec<_>>(),
            vec![
                Ok((GlyphId::new(2), b"abc".as_slice())),
                Ok((GlyphId::new(7), b"defg".as_slice())),
                Ok((GlyphId::new(8), b"".as_slice())),
                Ok((GlyphId::new(9), b"hijkl".as_slice())),
                Ok((GlyphId::new(13), b"mn".as_slice())),
            ]
        );

        assert_eq!(table.glyph_data_for_table(1).collect::<Vec<_>>(), vec![]);
    }

    #[test]
    fn glyph_keyed_glyph_data_for_one_table_u24_ids() {
        let data = test_data::glyf_u24_glyph_patches();
        let table =
            GlyphPatches::read(FontData::new(&data), GlyphKeyedFlags::WIDE_GLYPH_IDS).unwrap();

        let it = table.glyph_data_for_table(0);

        assert_eq!(
            it.collect::<Vec<_>>(),
            vec![
                Ok((GlyphId::new(2), b"abc".as_slice())),
                Ok((GlyphId::new(7), b"defg".as_slice())),
                Ok((GlyphId::new(8), b"".as_slice())),
                Ok((GlyphId::new(9), b"hijkl".as_slice())),
                Ok((GlyphId::new(13), b"mn".as_slice())),
            ]
        );

        assert_eq!(table.glyph_data_for_table(1).collect::<Vec<_>>(), vec![]);
    }

    #[test]
    fn glyph_keyed_glyph_data_for_multiple_tables() {
        let data = test_data::glyf_and_gvar_u16_glyph_patches();
        let table = GlyphPatches::read(FontData::new(&data), GlyphKeyedFlags::NONE).unwrap();

        assert_eq!(
            table.glyph_data_for_table(0).collect::<Vec<_>>(),
            vec![
                Ok((GlyphId::new(2), b"abc".as_slice())),
                Ok((GlyphId::new(7), b"defg".as_slice())),
                Ok((GlyphId::new(8), b"hijkl".as_slice())),
            ]
        );

        assert_eq!(
            table.glyph_data_for_table(1).collect::<Vec<_>>(),
            vec![
                Ok((GlyphId::new(2), b"mn".as_slice())),
                Ok((GlyphId::new(7), b"opq".as_slice())),
                Ok((GlyphId::new(8), b"r".as_slice())),
            ]
        );

        assert_eq!(table.glyph_data_for_table(2).collect::<Vec<_>>(), vec![]);
    }

    #[test]
    fn glyph_keyed_glyph_data_non_ascending_gids() {
        let mut builder = test_data::glyf_u16_glyph_patches();
        builder.write_at("gid_8", 6);
        let table =
            GlyphPatches::read(FontData::new(builder.as_slice()), GlyphKeyedFlags::NONE).unwrap();

        let it = table.glyph_data_for_table(0);

        assert_eq!(
            it.collect::<Vec<_>>(),
            vec![
                Ok((GlyphId::new(2), b"abc".as_slice())),
                Ok((GlyphId::new(7), b"defg".as_slice())),
                Err(ReadError::MalformedData(
                    "Glyph IDs are unsorted or duplicated."
                )),
            ]
        );
    }

    #[test]
    fn glyph_keyed_glyph_data_duplicate_gids() {
        let mut builder = test_data::glyf_u16_glyph_patches();
        builder.write_at("gid_8", 7);
        let table =
            GlyphPatches::read(FontData::new(builder.as_slice()), GlyphKeyedFlags::NONE).unwrap();

        let it = table.glyph_data_for_table(0);

        assert_eq!(
            it.collect::<Vec<_>>(),
            vec![
                Ok((GlyphId::new(2), b"abc".as_slice())),
                Ok((GlyphId::new(7), b"defg".as_slice())),
                Err(ReadError::MalformedData(
                    "Glyph IDs are unsorted or duplicated."
                )),
            ]
        );
    }

    #[test]
    fn glyph_keyed_glyph_data_for_one_table_non_ascending_offsets() {
        let mut builder = test_data::glyf_u16_glyph_patches();
        let gid_13 = builder.offset_for("gid_13_data") as u32;
        let gid_9 = builder.offset_for("gid_8_and_9_data") as u32;
        builder.write_at("gid_13_offset", gid_9);
        builder.write_at("gid_9_offset", gid_13);

        let table =
            GlyphPatches::read(FontData::new(builder.as_slice()), GlyphKeyedFlags::NONE).unwrap();

        let it = table.glyph_data_for_table(0);

        assert_eq!(
            it.collect::<Vec<_>>(),
            vec![
                Ok((GlyphId::new(2), b"abc".as_slice())),
                Ok((GlyphId::new(7), b"defg".as_slice())),
                Ok((GlyphId::new(8), b"hijkl".as_slice())),
                Err(ReadError::MalformedData(
                    "glyph data offsets are not ascending."
                )),
            ]
        );

        assert_eq!(table.glyph_data_for_table(1).collect::<Vec<_>>(), vec![]);
    }

    #[test]
    fn glyph_keyed_glyph_data_for_one_table_gids_truncated() {
        let builder = test_data::glyf_u16_glyph_patches();
        let len = builder.offset_for("table_count") as usize;
        let data = &builder.as_slice()[..len];

        let Err(err) = GlyphPatches::read(FontData::new(data), GlyphKeyedFlags::NONE) else {
            panic!("Expected to fail.");
        };
        assert_eq!(ReadError::OutOfBounds, err);
    }

    #[test]
    fn glyph_keyed_glyph_data_for_one_table_data_truncated() {
        let builder = test_data::glyf_u16_glyph_patches();
        let len = builder.offset_for("gid_8_and_9_data") as usize;
        let data = &builder.as_slice()[..len];

        let table = GlyphPatches::read(FontData::new(data), GlyphKeyedFlags::NONE).unwrap();

        let it = table.glyph_data_for_table(0);

        assert_eq!(
            it.collect::<Vec<_>>(),
            vec![
                Ok((GlyphId::new(2), b"abc".as_slice())),
                Ok((GlyphId::new(7), b"defg".as_slice())),
                Ok((GlyphId::new(8), b"".as_slice())),
                Err(ReadError::OutOfBounds),
            ]
        );
    }

    #[test]
    fn glyph_keyed_glyph_data_for_one_table_offset_array_truncated() {
        let builder = test_data::glyf_u16_glyph_patches();
        let len = builder.offset_for("gid_9_offset") as usize;
        let data = &builder.as_slice()[..len];

        let Err(err) = GlyphPatches::read(FontData::new(data), GlyphKeyedFlags::NONE) else {
            panic!("Expected to fail.");
        };
        assert_eq!(ReadError::OutOfBounds, err);
    }
}