fontcull-klippa 0.1.1

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
//! impl subset() for hmtx

use crate::{
    offset::{SerializeCopy, SerializeSubset},
    offset_array::SubsetOffsetArray,
    serialize::{SerializeErrorFlags, Serializer},
    CollectVariationIndices, Plan, Subset, SubsetError, SubsetTable,
};
use fontcull_write_fonts::{
    read::{
        collections::IntSet,
        tables::base::{
            Axis, Base, BaseCoord, BaseCoordFormat1, BaseCoordFormat2, BaseCoordFormat3,
            BaseLangSysRecord, BaseScript, BaseScriptList, BaseScriptRecord, BaseValues,
            FeatMinMaxRecord, MinMax,
        },
        FontData, FontRef, TopLevelTable,
    },
    types::{GlyphId, MajorMinor, Offset16, Offset32},
    FontBuilder,
};

// reference: subset() for BASE in harfbuzz
// <https://github.com/harfbuzz/harfbuzz/blob/fc42cdd68df0ce710b507981184ade7bf1b164e6/src/hb-ot-layout-base-table.hh#L763>
impl Subset for Base<'_> {
    fn subset(
        &self,
        plan: &Plan,
        _font: &FontRef,
        s: &mut Serializer,
        _builder: &mut FontBuilder,
    ) -> Result<(), SubsetError> {
        s.embed(self.version())
            .map_err(|_| SubsetError::SubsetTableError(Base::TAG))?;

        //hAxis offset
        let haxis_offset_pos = s
            .embed(0_u16)
            .map_err(|_| SubsetError::SubsetTableError(Base::TAG))?;

        if !self.horiz_axis_offset().is_null() {
            let Some(Ok(h_axis)) = self.horiz_axis() else {
                return Err(SubsetError::SubsetTableError(Base::TAG));
            };

            Offset16::serialize_subset(&h_axis, s, plan, (), haxis_offset_pos)
                .map_err(|_| SubsetError::SubsetTableError(Base::TAG))?;
        }

        //vertAxis offset
        let vaxis_offset_pos = s
            .embed(0_u16)
            .map_err(|_| SubsetError::SubsetTableError(Base::TAG))?;

        if !self.vert_axis_offset().is_null() {
            let Some(Ok(v_axis)) = self.vert_axis() else {
                return Err(SubsetError::SubsetTableError(Base::TAG));
            };

            Offset16::serialize_subset(&v_axis, s, plan, (), vaxis_offset_pos)
                .map_err(|_| SubsetError::SubsetTableError(Base::TAG))?;
        }

        //itemVarStore offset
        match self.item_var_store() {
            Some(Ok(var_store)) => {
                let varstore_offset_pos = s
                    .embed(0_u32)
                    .map_err(|_| SubsetError::SubsetTableError(Base::TAG))?;

                Offset32::serialize_subset(
                    &var_store,
                    s,
                    plan,
                    &plan.base_varstore_inner_maps,
                    varstore_offset_pos,
                )
                .map_err(|_| SubsetError::SubsetTableError(Base::TAG))?;
                Ok(())
            }
            None => {
                if self.version().minor > 0 {
                    s.copy_assign(0, MajorMinor::new(1, 0));
                }
                Ok(())
            }
            Some(Err(_)) => Err(SubsetError::SubsetTableError(Base::TAG)),
        }
    }
}

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

        if !self.base_tag_list_offset().is_null() {
            let Some(Ok(base_taglist)) = self.base_tag_list() else {
                return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
            };
            Offset16::serialize_copy(&base_taglist, s, base_taglist_offset_pos)?;
        }

        let Ok(base_scriptlist) = self.base_script_list() else {
            return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
        };
        Offset16::serialize_subset(&base_scriptlist, s, plan, (), base_scriptlist_offset_pos)
    }
}

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

    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: (),
    ) -> Result<(), SerializeErrorFlags> {
        let script_count_pos = s.embed(0_u16)?;
        let mut count: usize = 0;
        for script_record in self.base_script_records().iter() {
            let script_tag = script_record.base_script_tag();
            if !plan.layout_scripts.contains(script_tag) {
                continue;
            }

            script_record.subset(plan, s, self.offset_data())?;
            count += 1;
        }
        s.check_assign::<u16>(
            script_count_pos,
            count,
            SerializeErrorFlags::SERIALIZE_ERROR_INT_OVERFLOW,
        )
    }
}

impl<'a> SubsetTable<'a> for BaseScriptRecord {
    type ArgsForSubset = FontData<'a>;
    type Output = ();

    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        data: FontData,
    ) -> Result<(), SerializeErrorFlags> {
        s.embed(self.base_script_tag())?;
        let base_script_offset_pos = s.embed(0_u16)?;
        let Ok(base_script) = self.base_script(data) else {
            return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
        };
        Offset16::serialize_subset(&base_script, s, plan, (), base_script_offset_pos)
    }
}

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

    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<(), SerializeErrorFlags> {
        let base_values_offset_pos = s.embed(0_u16)?;
        if !self.base_values_offset().is_null() {
            let Some(Ok(base_value)) = self.base_values() else {
                return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
            };
            Offset16::serialize_subset(&base_value, s, plan, (), base_values_offset_pos)?;
        }

        let default_min_max_offset_pos = s.embed(0_u16)?;
        if !self.default_min_max_offset().is_null() {
            let Some(Ok(default_min_max)) = self.default_min_max() else {
                return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
            };
            Offset16::serialize_subset(&default_min_max, s, plan, (), default_min_max_offset_pos)?;
        }

        s.embed(self.base_lang_sys_count())?;

        for record in self.base_lang_sys_records().iter() {
            record.subset(plan, s, self.offset_data())?;
        }
        Ok(())
    }
}

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

    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<(), SerializeErrorFlags> {
        s.embed(self.default_baseline_index())?;
        // base_coord count
        let mut base_coord_count: u16 = 0;
        let count_pos = s.embed(base_coord_count)?;

        let org_num_base_coords = self.base_coord_count() as usize;
        let base_coords = self.base_coords();
        for idx in 0..org_num_base_coords {
            base_coords.subset_offset(idx, s, plan, ())?;
            base_coord_count += 1;
        }
        s.copy_assign(count_pos, base_coord_count);
        Ok(())
    }
}

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

    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<(), SerializeErrorFlags> {
        let min_coord_offset_pos = s.embed(0_u16)?;
        if !self.min_coord_offset().is_null() {
            let Some(Ok(min_coord)) = self.min_coord() else {
                return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
            };
            Offset16::serialize_subset(&min_coord, s, plan, (), min_coord_offset_pos)?;
        }

        let max_coord_offset_pos = s.embed(0_u16)?;
        if !self.max_coord_offset().is_null() {
            let Some(Ok(max_coord)) = self.max_coord() else {
                return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
            };
            Offset16::serialize_subset(&max_coord, s, plan, (), max_coord_offset_pos)?;
        }

        let feat_min_max_count_pos = s.embed(0_u16)?;
        let mut count: u16 = 0;
        for record in self.feat_min_max_records().iter() {
            let feature_tag = record.feature_table_tag();
            if !plan.layout_features.contains(feature_tag) {
                continue;
            }
            record.subset(plan, s, self.offset_data())?;
            count += 1;
        }
        s.copy_assign(feat_min_max_count_pos, count);
        Ok(())
    }
}

impl<'a> SubsetTable<'a> for FeatMinMaxRecord {
    type ArgsForSubset = FontData<'a>;
    type Output = ();

    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        data: FontData,
    ) -> Result<(), SerializeErrorFlags> {
        s.embed(self.feature_table_tag())?;

        let min_coord_offset_pos = s.embed(0_u16)?;
        if !self.min_coord_offset().is_null() {
            let Some(Ok(min_coord)) = self.min_coord(data) else {
                return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
            };
            Offset16::serialize_subset(&min_coord, s, plan, (), min_coord_offset_pos)?;
        }

        let max_coord_offset_pos = s.embed(0_u16)?;
        if !self.max_coord_offset().is_null() {
            let Some(Ok(max_coord)) = self.max_coord(data) else {
                return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
            };
            Offset16::serialize_subset(&max_coord, s, plan, (), max_coord_offset_pos)?;
        }
        Ok(())
    }
}

impl<'a> SubsetTable<'a> for BaseLangSysRecord {
    type ArgsForSubset = FontData<'a>;
    type Output = ();

    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        data: FontData,
    ) -> Result<(), SerializeErrorFlags> {
        s.embed(self.base_lang_sys_tag())?;

        let min_max_offset_pos = s.embed(0_u16)?;
        let Ok(min_max) = self.min_max(data) else {
            return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
        };
        Offset16::serialize_subset(&min_max, s, plan, (), min_max_offset_pos)
    }
}

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

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

impl SubsetTable<'_> for BaseCoordFormat2<'_> {
    type ArgsForSubset = ();
    type Output = ();
    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<(), SerializeErrorFlags> {
        s.embed_bytes(self.min_table_bytes())?;
        let Some(new_gid) = plan.glyph_map.get(&GlyphId::from(self.reference_glyph())) else {
            return Err(SerializeErrorFlags::SERIALIZE_ERROR_OTHER);
        };
        s.check_assign::<u16>(
            4,
            new_gid.to_u32() as usize,
            SerializeErrorFlags::SERIALIZE_ERROR_INT_OVERFLOW,
        )
    }
}

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

    fn subset(
        &self,
        plan: &Plan,
        s: &mut Serializer,
        _args: Self::ArgsForSubset,
    ) -> Result<(), SerializeErrorFlags> {
        s.embed(self.base_coord_format())?;
        s.embed(self.coordinate())?;

        let device_offset_pos = s.embed(0_u16)?;
        if !self.device_offset().is_null() {
            let Some(Ok(device)) = self.device() else {
                return Err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
            };

            Offset16::serialize_subset(
                &device,
                s,
                plan,
                &plan.base_varidx_delta_map,
                device_offset_pos,
            )?;
        }
        Ok(())
    }
}

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

        if let Some(Ok(v_axis)) = self.vert_axis() {
            v_axis.collect_variation_indices(plan, varidx_set);
        }
    }
}

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

impl CollectVariationIndices for BaseScriptList<'_> {
    fn collect_variation_indices(&self, plan: &Plan, varidx_set: &mut IntSet<u32>) {
        for script_record in self.base_script_records().iter() {
            let script_tag = script_record.base_script_tag();
            if !plan.layout_scripts.contains(script_tag) {
                continue;
            }

            let Ok(base_script) = script_record.base_script(self.offset_data()) else {
                return;
            };
            base_script.collect_variation_indices(plan, varidx_set);
        }
    }
}

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

        if let Some(Ok(default_min_max)) = self.default_min_max() {
            default_min_max.collect_variation_indices(plan, varidx_set);
        }

        for record in self.base_lang_sys_records().iter() {
            if let Ok(min_max) = record.min_max(self.offset_data()) {
                min_max.collect_variation_indices(plan, varidx_set);
            }
        }
    }
}

impl CollectVariationIndices for BaseValues<'_> {
    fn collect_variation_indices(&self, plan: &Plan, varidx_set: &mut IntSet<u32>) {
        for base_coord in self.base_coords().iter().flatten() {
            base_coord.collect_variation_indices(plan, varidx_set);
        }
    }
}

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

        if let Some(Ok(max_coord)) = self.max_coord() {
            max_coord.collect_variation_indices(plan, varidx_set);
        }

        for record in self.feat_min_max_records().iter() {
            let feature_tag = record.feature_table_tag();
            if !plan.layout_features.contains(feature_tag) {
                continue;
            }

            if let Some(Ok(min_coord)) = record.min_coord(self.offset_data()) {
                min_coord.collect_variation_indices(plan, varidx_set);
            }

            if let Some(Ok(max_coord)) = record.max_coord(self.offset_data()) {
                max_coord.collect_variation_indices(plan, varidx_set);
            }
        }
    }
}

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

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