fontcull-klippa 0.1.2

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

use crate::{
    layout::ClassDefSubsetStruct,
    offset::{SerializeSerialize, SerializeSubset},
    offset_array::SubsetOffsetArray,
    serialize::{SerializeErrorFlags, Serializer},
    CollectVariationIndices, Plan, Subset, SubsetError, SubsetState, SubsetTable,
};
use fontcull_write_fonts::{
    read::{
        collections::IntSet,
        tables::{
            gdef::{
                AttachList, AttachPoint, CaretValue, CaretValueFormat1, CaretValueFormat2,
                CaretValueFormat3, Gdef, LigCaretList, LigGlyph, MarkGlyphSets,
            },
            layout::CoverageTable,
        },
        types::GlyphId,
        FontRef, TopLevelTable,
    },
    types::Offset16,
    FontBuilder,
};

// reference: subset() for GDEF in harfbuzz
// <https://github.com/harfbuzz/harfbuzz/blob/59001aa9527c056ad08626cfec9a079b65d8aec8/src/OT/Layout/GDEF/GDEF.hh#L660>
impl Subset for Gdef<'_> {
    fn subset_with_state(
        &self,
        plan: &Plan,
        _font: &FontRef,
        state: &mut SubsetState,
        s: &mut Serializer,
        _builder: &mut FontBuilder,
    ) -> Result<(), SubsetError> {
        subset_gdef(self, plan, s, state).map_err(|_| SubsetError::SubsetTableError(Gdef::TAG))
    }
}

fn subset_gdef(
    gdef: &Gdef,
    plan: &Plan,
    s: &mut Serializer,
    state: &mut SubsetState,
) -> Result<(), SerializeErrorFlags> {
    let version = gdef.version();
    // major version
    s.embed(version.major)?;

    // minor version, might change after subset
    let minor_version_pos = s.embed(version.minor)?;

    // glyph classdef offset
    let glyph_classdef_offset_pos = s.embed(0_u16)?;

    // attach list offset
    let attachlist_offset_pos = s.embed(0_u16)?;

    // ligcaret list offset
    let ligcaret_list_offset_pos = s.embed(0_u16)?;

    //mark attach classdef offset
    let markattach_classdef_offset_pos = s.embed(0_u16)?;

    let snapshot_version0 = s.snapshot();

    let mark_glyphsetsdef_offset_pos = if version.minor >= 2 {
        s.embed(0_u16)?
    } else {
        0
    };

    // TODO: make sure repacker will not move the target subtable before the other children
    // ref: <https://github.com/harfbuzz/harfbuzz/blob/aad5780f5305f38ef128c61854c5d5a0c4ca3f4f/src/OT/Layout/GDEF/GDEF.hh#L665>
    // Push var store first (if it's needed) so that it's last in the
    // serialization order. Some font consumers assume that varstore runs to
    // the end of the GDEF table.
    // See: https://github.com/harfbuzz/harfbuzz/issues/4636

    let subset_varstore = if version.minor >= 3 {
        if let Some(var_store) = gdef
            .item_var_store()
            .transpose()
            .map_err(|_| SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR)?
        {
            let snapshot_version2 = s.snapshot();
            let var_store_offset_pos = s.embed(0_u16)?;
            match Offset16::serialize_subset(
                &var_store,
                s,
                plan,
                &plan.gdef_varstore_inner_maps,
                var_store_offset_pos,
            ) {
                Ok(()) => true,
                Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY) => {
                    s.revert_snapshot(snapshot_version2);
                    false
                }
                Err(e) => {
                    return Err(e);
                }
            }
        } else {
            false
        }
    } else {
        false
    };

    let subset_mark_glyphsets_def = if version.minor >= 2 {
        if let Some(mark_glyph_sets_def) = gdef
            .mark_glyph_sets_def()
            .transpose()
            .map_err(|_| SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR)?
        {
            match Offset16::serialize_subset(
                &mark_glyph_sets_def,
                s,
                plan,
                (),
                mark_glyphsetsdef_offset_pos,
            ) {
                Ok(()) => true,
                Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY) => false,
                Err(e) => {
                    return Err(e);
                }
            }
        } else {
            false
        }
    } else {
        false
    };

    // Downgrade version if possible
    if !subset_varstore {
        if subset_mark_glyphsets_def {
            s.copy_assign(minor_version_pos, 2_u16);
        } else {
            s.copy_assign(minor_version_pos, 0_u16);
            s.revert_snapshot(snapshot_version0);
        }
    }

    let subset_mark_attach_classdef = if let Some(mark_acttach_classdef) = gdef
        .mark_attach_class_def()
        .transpose()
        .map_err(|_| SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR)?
    {
        match Offset16::serialize_subset(
            &mark_acttach_classdef,
            s,
            plan,
            &ClassDefSubsetStruct {
                remap_class: false,
                keep_empty_table: false,
                use_class_zero: true,
                glyph_filter: None,
            },
            markattach_classdef_offset_pos,
        ) {
            Ok(_) => true,
            Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY) => false,
            Err(e) => {
                return Err(e);
            }
        }
    } else {
        false
    };

    let subset_ligcaret_list = if let Some(ligcaret_list) = gdef
        .lig_caret_list()
        .transpose()
        .map_err(|_| SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR)?
    {
        match Offset16::serialize_subset(&ligcaret_list, s, plan, (), ligcaret_list_offset_pos) {
            Ok(_) => true,
            Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY) => false,
            Err(e) => {
                return Err(e);
            }
        }
    } else {
        false
    };

    let subset_attach_list = if let Some(attach_list) = gdef
        .attach_list()
        .transpose()
        .map_err(|_| SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR)?
    {
        match Offset16::serialize_subset(&attach_list, s, plan, (), attachlist_offset_pos) {
            Ok(_) => true,
            Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY) => false,
            Err(e) => {
                return Err(e);
            }
        }
    } else {
        false
    };

    let subset_glyph_classdef = if let Some(glyph_classdef) = gdef
        .glyph_class_def()
        .transpose()
        .map_err(|_| SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR)?
    {
        match Offset16::serialize_subset(
            &glyph_classdef,
            s,
            plan,
            &ClassDefSubsetStruct {
                remap_class: false,
                keep_empty_table: false,
                use_class_zero: true,
                glyph_filter: None,
            },
            glyph_classdef_offset_pos,
        ) {
            Ok(_) => true,
            Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY) => false,
            Err(e) => {
                return Err(e);
            }
        }
    } else {
        false
    };

    if subset_glyph_classdef
        || subset_attach_list
        || subset_ligcaret_list
        || subset_mark_attach_classdef
        || (version.minor >= 2 && subset_mark_glyphsets_def)
        || (version.minor >= 3 && subset_varstore)
    {
        state.has_gdef_varstore = subset_varstore;
        Ok(())
    } else {
        Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY)
    }
}

impl SubsetTable<'_> for AttachList<'_> {
    type ArgsForSubset = ();
    type Output = ();

    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<Self::Output, SerializeErrorFlags> {
        //coverage offset
        let coverage_offset_pos = s.embed(0_u16)?;
        //glyph_count
        let glyph_count_pos = s.embed(0_u16)?;

        let Ok(coverage) = self.coverage() else {
            return Err(s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR));
        };

        let attach_points = self.attach_points();
        let mut count = 0_u16;
        let src_glyph_count = self.glyph_count() as usize;
        let mut retained_glyphs =
            Vec::with_capacity(plan.glyph_map_gsub.len().min(src_glyph_count));

        for (idx, glyph) in coverage
            .iter()
            .enumerate()
            .take(plan.font_num_glyphs.min(src_glyph_count))
        {
            let Some(new_gid) = plan.glyph_map_gsub.get(&GlyphId::from(glyph)) else {
                continue;
            };

            attach_points.subset_offset(idx, s, plan, ())?;
            count += 1;
            retained_glyphs.push(*new_gid);
        }

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

        s.copy_assign(glyph_count_pos, count);
        Offset16::serialize_serialize::<CoverageTable>(s, &retained_glyphs, coverage_offset_pos)
    }
}

impl SubsetTable<'_> for AttachPoint<'_> {
    type ArgsForSubset = ();
    type Output = ();
    fn subset(
        &self,
        _plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<Self::Output, SerializeErrorFlags> {
        s.embed_bytes(self.min_table_bytes()).map(|_| ())
    }
}

impl SubsetTable<'_> for LigCaretList<'_> {
    type ArgsForSubset = ();
    type Output = ();
    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<Self::Output, SerializeErrorFlags> {
        //coverage offset
        let coverage_offset_pos = s.embed(0_u16)?;
        //lig_glyph_count
        let lig_glyph_count_pos = s.embed(0_u16)?;

        let Ok(coverage) = self.coverage() else {
            return Err(s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR));
        };

        let lig_glyphs = self.lig_glyphs();
        let mut count = 0_u16;
        let src_lig_glyph_count = self.lig_glyph_count() as usize;
        let mut retained_glyphs =
            Vec::with_capacity(plan.glyph_map_gsub.len().min(src_lig_glyph_count));

        for (idx, glyph) in coverage
            .iter()
            .enumerate()
            .take(plan.font_num_glyphs.min(src_lig_glyph_count))
        {
            let Some(new_gid) = plan.glyph_map_gsub.get(&GlyphId::from(glyph)) else {
                continue;
            };

            lig_glyphs.subset_offset(idx, s, plan, ())?;
            count += 1;
            retained_glyphs.push(*new_gid);
        }

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

        s.copy_assign(lig_glyph_count_pos, count);
        Offset16::serialize_serialize::<CoverageTable>(s, &retained_glyphs, coverage_offset_pos)
    }
}

impl SubsetTable<'_> for LigGlyph<'_> {
    type ArgsForSubset = ();
    type Output = ();
    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<Self::Output, SerializeErrorFlags> {
        // caret count
        let caret_count_pos = s.embed(0_u16)?;

        let caret_values = self.caret_values();
        let mut count = 0_u16;
        for idx in 0..caret_values.len() {
            caret_values.subset_offset(idx, s, plan, ())?;
            count += 1;
        }

        if count == 0 {
            return Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY);
        }

        s.copy_assign(caret_count_pos, count);
        Ok(())
    }
}

impl SubsetTable<'_> for MarkGlyphSets<'_> {
    type ArgsForSubset = ();
    type Output = ();
    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<Self::Output, SerializeErrorFlags> {
        s.embed(self.format())?;

        let count_pos = s.embed(0_u16)?;

        let coverages = self.coverages();
        let src_count = self.mark_glyph_set_count() as usize;
        let mut count = 0_u16;

        // skip empty coverage, don't error out
        for idx in 0..src_count {
            match coverages.subset_offset(idx, s, plan, ()) {
                Ok(()) => {
                    count += 1;
                }
                Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY) => (),
                Err(e) => {
                    return Err(e);
                }
            }
        }

        if count == 0 {
            return Err(SerializeErrorFlags::SERIALIZE_ERROR_EMPTY);
        }

        s.copy_assign(count_pos, count);
        Ok(())
    }
}

impl SubsetTable<'_> for CaretValue<'_> {
    type ArgsForSubset = ();
    type Output = ();
    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        args: Self::ArgsForSubset,
    ) -> Result<Self::Output, SerializeErrorFlags> {
        match self {
            Self::Format1(item) => item.subset(plan, s, args),
            Self::Format2(item) => item.subset(plan, s, args),
            Self::Format3(item) => item.subset(plan, s, args),
        }
    }
}

impl SubsetTable<'_> for CaretValueFormat1<'_> {
    type ArgsForSubset = ();
    type Output = ();
    fn subset(
        &self,
        _plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<Self::Output, SerializeErrorFlags> {
        s.embed_bytes(self.min_table_bytes()).map(|_| ())
    }
}

impl SubsetTable<'_> for CaretValueFormat2<'_> {
    type ArgsForSubset = ();
    type Output = ();
    fn subset(
        &self,
        _plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<Self::Output, SerializeErrorFlags> {
        s.embed_bytes(self.min_table_bytes()).map(|_| ())
    }
}

impl SubsetTable<'_> for CaretValueFormat3<'_> {
    type ArgsForSubset = ();
    type Output = ();
    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<Self::Output, SerializeErrorFlags> {
        s.embed(self.caret_value_format())?;
        s.embed(self.coordinate())?;
        let device_offset_pos = s.embed(0_u16)?;

        let Ok(device) = self.device() else {
            return Err(s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR));
        };
        Offset16::serialize_subset(
            &device,
            s,
            plan,
            &plan.layout_varidx_delta_map,
            device_offset_pos,
        )
    }
}

impl CollectVariationIndices for Gdef<'_> {
    fn collect_variation_indices(&self, plan: &Plan, varidx_set: &mut IntSet<u32>) {
        if let Some(Ok(lig_caret_list)) = self.lig_caret_list() {
            lig_caret_list.collect_variation_indices(plan, varidx_set);
        }
    }
}

impl CollectVariationIndices for LigCaretList<'_> {
    fn collect_variation_indices(&self, plan: &Plan, varidx_set: &mut IntSet<u32>) {
        let Ok(coverage) = self.coverage() else {
            return;
        };

        let lig_glyphs = self.lig_glyphs();
        for (gid, lig_glyph) in coverage.iter().zip(lig_glyphs.iter()) {
            let Ok(lig_glyph) = lig_glyph else {
                return;
            };
            if !plan.glyphset_gsub.contains(GlyphId::from(gid)) {
                continue;
            }
            lig_glyph.collect_variation_indices(plan, varidx_set);
        }
    }
}

impl CollectVariationIndices for LigGlyph<'_> {
    fn collect_variation_indices(&self, plan: &Plan, varidx_set: &mut IntSet<u32>) {
        for caret in self.caret_values().iter() {
            let Ok(caret) = caret else {
                return;
            };
            caret.collect_variation_indices(plan, varidx_set);
        }
    }
}

impl CollectVariationIndices for CaretValue<'_> {
    fn collect_variation_indices(&self, plan: &Plan, varidx_set: &mut IntSet<u32>) {
        if let Self::Format3(item) = self {
            if let Ok(device) = item.device() {
                device.collect_variation_indices(plan, varidx_set);
            }
        }
    }
}

pub(crate) trait CollectUsedMarkSets {
    fn collect_used_mark_sets(&self, plan: &Plan, used_mark_sets: &mut IntSet<u16>);
}

impl CollectUsedMarkSets for Gdef<'_> {
    fn collect_used_mark_sets(&self, plan: &Plan, used_mark_sets: &mut IntSet<u16>) {
        if let Some(Ok(mark_glyph_sets)) = self.mark_glyph_sets_def() {
            mark_glyph_sets.collect_used_mark_sets(plan, used_mark_sets);
        };
    }
}

impl CollectUsedMarkSets for MarkGlyphSets<'_> {
    fn collect_used_mark_sets(&self, plan: &Plan, used_mark_sets: &mut IntSet<u16>) {
        for (i, coverage) in self.coverages().iter().enumerate() {
            let Ok(coverage) = coverage else {
                return;
            };
            if coverage.intersects(&plan.glyphset_gsub) {
                used_mark_sets.insert(i as u16);
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use fontcull_write_fonts::{
        read::{FontRef, TableProvider},
        types::GlyphId,
    };
    #[test]
    fn test_collect_var_indices() {
        let mut plan = Plan::default();
        plan.glyphset_gsub.insert(GlyphId::from(4_u32));
        plan.glyphset_gsub.insert(GlyphId::from(7_u32));

        let font =
            FontRef::new(include_bytes!("../test-data/fonts/AnekBangla-subset.ttf")).unwrap();
        let gdef = font.gdef().unwrap();

        let mut varidx_set = IntSet::empty();
        gdef.collect_variation_indices(&plan, &mut varidx_set);
        assert_eq!(varidx_set.len(), 3);
        assert!(varidx_set.contains(3));
        assert!(varidx_set.contains(5));
        assert!(varidx_set.contains(0));
    }

    #[test]
    fn test_collect_used_mark_sets() {
        let mut plan = Plan::default();
        plan.glyphset_gsub.insert(GlyphId::from(171_u32));

        let font = FontRef::new(include_bytes!("../test-data/fonts/Roboto-Regular.ttf")).unwrap();
        let gdef = font.gdef().unwrap();

        let mut used_mark_sets = IntSet::empty();
        gdef.collect_used_mark_sets(&plan, &mut used_mark_sets);
        assert_eq!(used_mark_sets.len(), 1);
        assert!(used_mark_sets.contains(0));
    }
}