skera 0.8.0

Subsetting a font file according to provided input.
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
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
//! impl subset() for SinglePos subtable

use crate::fnv::FnvHashMap;
use crate::{
    gpos::value_record::{compute_effective_format, compute_record_len},
    layout::{intersected_glyphs_and_indices, map_gsub_glyph},
    offset::SerializeSerialize,
    serialize::{SerializeErrorFlags, Serializer},
    CollectVariationIndices, Plan, Serialize, SubsetFlags, SubsetState, SubsetTable,
};
use write_fonts::{
    read::{
        collections::IntSet,
        tables::{
            gpos::{SinglePos, SinglePosFormat1, SinglePosFormat2, ValueFormat, ValueRecord},
            layout::CoverageTable,
        },
        types::GlyphId,
        FontData, FontRef, ReadError, TableProvider,
    },
    types::Offset16,
};

impl<'a> SubsetTable<'a> for SinglePos<'_> {
    type ArgsForSubset = (&'a SubsetState, &'a FontRef<'a>, &'a FnvHashMap<u16, u16>);
    type Output = ();
    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        args: Self::ArgsForSubset,
    ) -> Result<Self::Output, SerializeErrorFlags> {
        let args = (args.0, args.1);
        match self {
            Self::Format1(item) => item.subset(plan, s, args),
            Self::Format2(item) => item.subset(plan, s, args),
        }
    }
}

impl<'a> SubsetTable<'a> for SinglePosFormat1<'_> {
    type ArgsForSubset = (&'a SubsetState, &'a FontRef<'a>);
    type Output = ();
    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        args: Self::ArgsForSubset,
    ) -> Result<(), SerializeErrorFlags> {
        if self.coverage_offset().is_null() {
            return Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY);
        }
        let coverage = self
            .coverage()
            .map_err(|_| s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR))?;
        let retained_glyphs: Vec<GlyphId> = coverage
            .intersect_set(&plan.glyphset_gsub)
            .iter()
            .filter_map(|g| map_gsub_glyph(&plan.glyph_map_gsub, g))
            .collect();
        if retained_glyphs.is_empty() {
            return Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY);
        }

        let value_record = self.value_record();
        let new_format = if plan
            .subset_flags
            .contains(SubsetFlags::SUBSET_FLAGS_NO_HINTING)
        {
            let (state, font) = args;
            // do not strip hints for VF unless it has no GDEF varstore after subsetting
            let strip_hints = if font.fvar().is_ok() {
                !state.has_gdef_varstore
            } else {
                true
            };
            compute_effective_format(&value_record, strip_hints, true)
                .map_err(|_| s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR))?
        } else {
            self.value_format()
        };

        SinglePosFormat1::serialize(s, (&retained_glyphs, value_record, new_format, plan))
    }
}

impl<'a> Serialize<'a> for SinglePosFormat1<'_> {
    type Args = (&'a [GlyphId], ValueRecord<'a>, ValueFormat, &'a Plan);
    fn serialize(s: &mut Serializer, args: Self::Args) -> Result<(), SerializeErrorFlags> {
        // format
        s.embed(1_u16)?;

        // coverage offset
        let cov_offset_pos = s.embed(0_u16)?;

        let (glyphs, value_record, value_format, plan) = args;
        //value format
        s.embed(value_format)?;
        //value record
        value_record.subset(plan, s, value_format)?;

        Offset16::serialize_serialize::<CoverageTable>(s, glyphs, cov_offset_pos)
    }
}

pub(crate) struct SinglePosInfo<'a> {
    value_format: ValueFormat,
    records_offset: usize,
    record_size: usize,
    font_data: FontData<'a>,
    new_format: ValueFormat,
}

fn compute_new_value_format(
    singlepos_info: &mut SinglePosInfo,
    plan: &Plan,
    has_gdef_varstore: bool,
    font: &FontRef,
    retained_rec_idxes: &IntSet<u16>,
) -> Result<(), ReadError> {
    // TODO: support instancing
    let (value_format, records_offset, record_size, font_data, new_format) = (
        singlepos_info.value_format,
        singlepos_info.records_offset,
        singlepos_info.record_size,
        singlepos_info.font_data,
        &mut singlepos_info.new_format,
    );
    if plan
        .subset_flags
        .contains(SubsetFlags::SUBSET_FLAGS_NO_HINTING)
    {
        // do not strip hints for VF unless it has no GDEF varstore after subsetting
        let strip_hints = if font.fvar().is_ok() {
            !has_gdef_varstore
        } else {
            true
        };

        for i in retained_rec_idxes.iter() {
            let offset = records_offset + i as usize * record_size;
            let value_record = ValueRecord::new(font_data, offset, value_format);
            *new_format |= compute_effective_format(&value_record, strip_hints, true)?;
        }
    } else {
        *new_format = value_format;
    }

    Ok(())
}

impl<'a> SubsetTable<'a> for SinglePosFormat2<'_> {
    type ArgsForSubset = (&'a SubsetState, &'a FontRef<'a>);
    type Output = ();
    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        args: Self::ArgsForSubset,
    ) -> Result<(), SerializeErrorFlags> {
        if self.coverage_offset().is_null() {
            return Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY);
        }
        let coverage = self
            .coverage()
            .map_err(|_| s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR))?;

        let (retained_glyphs, retained_rec_idxes) =
            intersected_glyphs_and_indices(&coverage, &plan.glyphset_gsub, &plan.glyph_map_gsub);

        if retained_glyphs.is_empty() {
            return Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY);
        }

        let (state, font) = args;
        let value_format = self.value_format();
        let records_offset = self.value_count_byte_range().end;
        let record_size = compute_record_len(value_format);
        let font_data = self.offset_data();
        let mut singlepos_info = SinglePosInfo {
            value_format,
            records_offset,
            record_size,
            font_data,
            new_format: ValueFormat::empty(),
        };

        compute_new_value_format(
            &mut singlepos_info,
            plan,
            state.has_gdef_varstore,
            font,
            &retained_rec_idxes,
        )
        .map_err(|_| s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR))?;

        let Some(first_rec_idx) = retained_rec_idxes.first() else {
            return Err(s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_OTHER));
        };
        let first_retained_rec = ValueRecord::new(
            font_data,
            records_offset + first_rec_idx as usize * record_size,
            value_format,
        );

        let table_format = if retained_rec_idxes
            .iter()
            .skip(1)
            .map(|i| {
                ValueRecord::new(
                    font_data,
                    records_offset + i as usize * record_size,
                    value_format,
                )
            })
            .all(|rec| rec == first_retained_rec)
        {
            1
        } else {
            2
        };

        if table_format == 1 {
            SinglePosFormat1::serialize(
                s,
                (
                    &retained_glyphs,
                    first_retained_rec,
                    singlepos_info.new_format,
                    plan,
                ),
            )
        } else {
            SinglePosFormat2::serialize(
                s,
                (&retained_glyphs, &singlepos_info, &retained_rec_idxes, plan),
            )
        }
    }
}

impl<'a> Serialize<'a> for SinglePosFormat2<'_> {
    type Args = (
        &'a [GlyphId],
        &'a SinglePosInfo<'a>,
        &'a IntSet<u16>,
        &'a Plan,
    );
    fn serialize(s: &mut Serializer, args: Self::Args) -> Result<(), SerializeErrorFlags> {
        // format
        s.embed(2_u16)?;

        // coverage offset
        let cov_offset_pos = s.embed(0_u16)?;

        let (glyphs, singlepos_info, retained_rec_idxes, plan) = args;
        let (value_format, records_offset, record_size, font_data, new_format) = (
            singlepos_info.value_format,
            singlepos_info.records_offset,
            singlepos_info.record_size,
            singlepos_info.font_data,
            singlepos_info.new_format,
        );
        //value format
        s.embed(new_format)?;

        //value count
        let value_count = glyphs.len();
        s.embed(value_count as u16)?;

        for i in retained_rec_idxes.iter() {
            let offset = records_offset + i as usize * record_size;
            let value_record = ValueRecord::new(font_data, offset, value_format);
            value_record.subset(plan, s, new_format)?;
        }

        Offset16::serialize_serialize::<CoverageTable>(s, glyphs, cov_offset_pos)
    }
}

impl CollectVariationIndices for SinglePos<'_> {
    fn collect_variation_indices(&self, plan: &Plan, varidx_set: &mut IntSet<u32>) {
        match self {
            Self::Format1(item) => item.collect_variation_indices(plan, varidx_set),
            Self::Format2(item) => item.collect_variation_indices(plan, varidx_set),
        }
    }
}

impl CollectVariationIndices for SinglePosFormat1<'_> {
    fn collect_variation_indices(&self, plan: &Plan, varidx_set: &mut IntSet<u32>) {
        if !self
            .value_format()
            .intersects(ValueFormat::ANY_DEVICE_OR_VARIDX)
        {
            return;
        }
        self.value_record()
            .collect_variation_indices(plan, varidx_set);
    }
}

impl CollectVariationIndices for SinglePosFormat2<'_> {
    fn collect_variation_indices(&self, plan: &Plan, varidx_set: &mut IntSet<u32>) {
        let value_format = self.value_format();
        if !value_format.intersects(ValueFormat::ANY_DEVICE_OR_VARIDX) {
            return;
        }

        let Ok(coverage) = self.coverage() else {
            return;
        };
        let glyph_set = &plan.glyphset_gsub;
        let value_count = self.value_count();
        let record_size = compute_record_len(value_format);
        let records_offset = self.value_count_byte_range().end;
        let font_data = self.offset_data();

        let bit_storage = 16 - value_count.leading_zeros() as u64;
        if value_count as u64 > glyph_set.len() * bit_storage {
            for idx in glyph_set.iter().filter_map(|g| coverage.get(g)) {
                let offset = records_offset + idx as usize * record_size;
                let value_record = ValueRecord::new(font_data, offset, value_format);
                value_record.collect_variation_indices(plan, varidx_set);
            }
        } else {
            for i in coverage
                .iter()
                .enumerate()
                .filter_map(|(idx, g)| glyph_set.contains(GlyphId::from(g)).then_some(idx))
            {
                let offset = records_offset + i * record_size;
                let value_record = ValueRecord::new(font_data, offset, value_format);
                value_record.collect_variation_indices(plan, varidx_set);
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use write_fonts::read::{FontRef, TableProvider};

    #[test]
    fn test_subset_gpos_format1() {
        use write_fonts::read::tables::gpos::PositionSubtables;

        let font = FontRef::new(include_bytes!("../../test-data/fonts/Amiri-Regular.ttf")).unwrap();
        let gpos_lookups = font.gpos().unwrap().lookup_list().unwrap();
        let lookup = gpos_lookups.lookups().get(6).unwrap();

        let PositionSubtables::Single(sub_tables) = lookup.subtables().unwrap() else {
            panic!("Wrong type of lookup table!");
        };
        let singlepos_table = sub_tables.get(0).unwrap();

        let subset_state = SubsetState::default();
        let mut plan = Plan {
            glyph_map_gsub: vec![crate::INVALID_GID; 5988],
            ..Default::default()
        };

        plan.glyph_map_gsub[5987] = GlyphId::from(3_u32);
        plan.glyphset_gsub.insert(GlyphId::from(5987_u32));

        let mut s = Serializer::new(1024);
        assert_eq!(s.start_serialize(), Ok(()));

        singlepos_table
            .subset(&plan, &mut s, (&subset_state, &font, &plan.gpos_lookups))
            .unwrap();
        assert!(!s.in_error());
        s.end_serialize();

        let subsetted_data = s.copy_bytes();
        let expected_data: [u8; 16] = [
            0x00, 0x01, 0x00, 0x0a, 0x00, 0x05, 0xfb, 0xc9, 0xfe, 0xdc, 0x00, 0x01, 0x00, 0x01,
            0x00, 0x03,
        ];

        assert_eq!(subsetted_data, expected_data);
    }

    #[test]
    fn test_subset_gpos_format2() {
        use write_fonts::read::tables::gpos::PositionSubtables;

        let font = FontRef::new(include_bytes!("../../test-data/fonts/Amiri-Regular.ttf")).unwrap();
        let gpos_lookups = font.gpos().unwrap().lookup_list().unwrap();
        let lookup = gpos_lookups.lookups().get(36).unwrap();

        let PositionSubtables::Single(sub_tables) = lookup.subtables().unwrap() else {
            panic!("Wrong type of lookup table!");
        };
        let singlepos_table = sub_tables.get(4).unwrap();

        let subset_state = SubsetState::default();
        let mut plan = Plan {
            glyph_map_gsub: vec![crate::INVALID_GID; 2350],
            ..Default::default()
        };

        // test case 1: subsetted output is still format 2
        plan.glyph_map_gsub[2270] = GlyphId::from(3_u32);
        plan.glyph_map_gsub[2349] = GlyphId::from(4_u32);
        plan.glyphset_gsub.insert(GlyphId::from(2270_u32));
        plan.glyphset_gsub.insert(GlyphId::from(2349_u32));

        let mut s = Serializer::new(1024);
        assert_eq!(s.start_serialize(), Ok(()));

        singlepos_table
            .subset(&plan, &mut s, (&subset_state, &font, &plan.gpos_lookups))
            .unwrap();
        assert!(!s.in_error());
        s.end_serialize();

        let subsetted_data = s.copy_bytes();
        let expected_data: [u8; 20] = [
            0x00, 0x02, 0x00, 0x0c, 0x00, 0x04, 0x00, 0x02, 0x00, 0xc3, 0x00, 0xfe, 0x00, 0x01,
            0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
        ];

        assert_eq!(subsetted_data, expected_data);

        // test case 2: subsetted output is optimized to format 1
        plan.glyph_map_gsub = vec![crate::INVALID_GID; 6180];
        plan.glyph_map_gsub[2270] = GlyphId::from(3_u32);
        plan.glyph_map_gsub[6179] = GlyphId::from(4_u32);

        plan.glyphset_gsub.clear();
        plan.glyphset_gsub.insert(GlyphId::from(2270_u32));
        plan.glyphset_gsub.insert(GlyphId::from(6179_u32));

        let mut s = Serializer::new(1024);
        assert_eq!(s.start_serialize(), Ok(()));

        singlepos_table
            .subset(&plan, &mut s, (&subset_state, &font, &plan.gpos_lookups))
            .unwrap();
        assert!(!s.in_error());
        s.end_serialize();

        let subsetted_data = s.copy_bytes();
        let expected_data: [u8; 16] = [
            0x00, 0x01, 0x00, 0x08, 0x00, 0x04, 0x00, 0xc3, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03,
            0x00, 0x04,
        ];

        assert_eq!(subsetted_data, expected_data);
    }

    #[test]
    fn test_subset_gpos_format2_with_variation_indices() {
        use write_fonts::read::{FontData, FontRead};

        // Construct a SinglePosFormat2 table with VariationIndex devices:
        // - Coverage: 3 glyphs (10, 20, 30)
        // - ValueFormat: X_ADVANCE | X_ADVANCE_DEVICE (0x0044)
        // - 3 ValueRecords with distinct values and variation index pointers:
        //   Record 0 (GID 10): x_advance = 100, varidx = (outer: 1, inner: 2) -> 0x00010002
        //   Record 1 (GID 20): x_advance = 200, varidx = (outer: 3, inner: 4) -> 0x00030004
        //   Record 2 (GID 30): x_advance = 300, varidx = (outer: 5, inner: 6) -> 0x00050006
        #[rustfmt::skip]
        let raw_table: [u8; 48] = [
            0x00, 0x02, 0x00, 0x14, 0x00, 0x44, 0x00, 0x03,
            0x00, 0x64, 0x00, 0x1e, 0x00, 0xc8, 0x00, 0x24,
            0x01, 0x2c, 0x00, 0x2a, 0x00, 0x01, 0x00, 0x03,
            0x00, 0x0a, 0x00, 0x14, 0x00, 0x1e, 0x00, 0x01,
            0x00, 0x02, 0x80, 0x00, 0x00, 0x03, 0x00, 0x04,
            0x80, 0x00, 0x00, 0x05, 0x00, 0x06, 0x80, 0x00,
        ];

        let singlepos = SinglePosFormat2::read(FontData::new(&raw_table)).unwrap();

        // 1. Test CollectVariationIndices (branch 1: value_count > len * bit_storage)
        let mut plan = Plan::default();
        plan.glyphset_gsub.insert(GlyphId::from(20_u32));
        let mut varidx_set = IntSet::empty();
        singlepos.collect_variation_indices(&plan, &mut varidx_set);
        assert_eq!(varidx_set.len(), 1);
        assert!(varidx_set.contains(0x00030004));

        // 2. Test CollectVariationIndices (branch 2: else branch)
        plan.glyphset_gsub.insert(GlyphId::from(10_u32));
        plan.glyphset_gsub.insert(GlyphId::from(30_u32));
        varidx_set.clear();
        singlepos.collect_variation_indices(&plan, &mut varidx_set);
        assert_eq!(varidx_set.len(), 3);
        assert!(varidx_set.contains(0x00010002));
        assert!(varidx_set.contains(0x00030004));
        assert!(varidx_set.contains(0x00050006));

        // 3. Test Subsetting: retaining glyphs 20 and 30 (output remains Format 2)
        let font = FontRef::new(include_bytes!("../../test-data/fonts/Amiri-Regular.ttf")).unwrap();
        let subset_state = SubsetState::default();

        let mut plan = Plan {
            glyph_map_gsub: vec![crate::INVALID_GID; 35],
            ..Default::default()
        };
        plan.glyph_map_gsub[20] = GlyphId::from(1_u32);
        plan.glyph_map_gsub[30] = GlyphId::from(2_u32);
        plan.glyphset_gsub.insert(GlyphId::from(20_u32));
        plan.glyphset_gsub.insert(GlyphId::from(30_u32));

        // Map old varidx -> new varidx
        plan.layout_varidx_delta_map
            .insert(0x00030004, (0x00070008, 0));
        plan.layout_varidx_delta_map
            .insert(0x00050006, (0x0009000a, 0));

        let mut s = Serializer::new(1024);
        assert_eq!(s.start_serialize(), Ok(()));
        singlepos
            .subset(&plan, &mut s, (&subset_state, &font))
            .unwrap();
        assert!(!s.in_error());
        s.end_serialize();

        let subsetted_data = s.copy_bytes();
        #[rustfmt::skip]
        let expected_format2: [u8; 36] = [
            // pos_format=2, cov_offset=16, value_format=0x0044, count=2
            0x00, 0x02, 0x00, 0x10, 0x00, 0x44, 0x00, 0x02,
            // record 0: x_advance=200, dev_offset=30
            0x00, 0xc8, 0x00, 0x1e,
            // record 1: x_advance=300, dev_offset=24
            0x01, 0x2c, 0x00, 0x18,
            // coverage: format 1, count 2, glyphs 1, 2
            0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x02,
            // VariationIndex 1: remapped outer=9, inner=10, delta_format=0x8000 (offset 24)
            0x00, 0x09, 0x00, 0x0a, 0x80, 0x00,
            // VariationIndex 0: remapped outer=7, inner=8, delta_format=0x8000 (offset 30)
            0x00, 0x07, 0x00, 0x08, 0x80, 0x00,
        ];
        assert_eq!(subsetted_data, expected_format2);

        // 4. Test Subsetting: retaining only glyph 20 (output optimized to Format 1)
        let mut plan = Plan {
            glyph_map_gsub: vec![crate::INVALID_GID; 35],
            ..Default::default()
        };
        plan.glyph_map_gsub[20] = GlyphId::from(1_u32);
        plan.glyphset_gsub.insert(GlyphId::from(20_u32));
        plan.layout_varidx_delta_map
            .insert(0x00030004, (0x00070008, 0));

        let mut s = Serializer::new(1024);
        assert_eq!(s.start_serialize(), Ok(()));
        singlepos
            .subset(&plan, &mut s, (&subset_state, &font))
            .unwrap();
        assert!(!s.in_error());
        s.end_serialize();

        let subsetted_data = s.copy_bytes();
        #[rustfmt::skip]
        let expected_format1: [u8; 22] = [
            // pos_format=1, cov_offset=10, value_format=0x0044
            0x00, 0x01, 0x00, 0x0a, 0x00, 0x44,
            // record: x_advance=200, dev_offset=16
            0x00, 0xc8, 0x00, 0x10,
            // coverage: format 1, count 1, glyph 1
            0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
            // VariationIndex: remapped outer=7, inner=8, delta_format=0x8000
            0x00, 0x07, 0x00, 0x08, 0x80, 0x00,
        ];
        assert_eq!(subsetted_data, expected_format1);
    }
}