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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! Split MarkBasePos table in a graph
use crate::{
    graph::{
        coverage_graph::{add_new_coverage, coverage_glyphs, make_coverage},
        layout::DataBytes,
        Graph, RepackError,
    },
    serialize::{LinkWidth, ObjIdx},
};
use write_fonts::{
    read::collections::IntSet,
    types::{FixedSize, GlyphId, Offset16},
};

// output only contains new subtable indices
// ref:<https://github.com/harfbuzz/harfbuzz/blob/fa2908bf16d2ccd6623f4d575455fea72a1a722b/src/graph/markbasepos-graph.hh#L214>
pub(crate) fn split_markbase_pos(
    graph: &mut Graph,
    table_idx: ObjIdx,
) -> Result<Vec<ObjIdx>, RepackError> {
    let Some(mut table_info) = get_table_info(graph, table_idx) else {
        return Ok(Vec::new());
    };

    let split_points = compute_split_points(graph, &table_info)?;
    if split_points.is_empty() {
        return Ok(Vec::new());
    }

    // we need to duplicate shared subtables before mutating them
    // MarkArray/BaseArray
    table_info.mark_array_idx = graph.unshared_child(
        table_idx,
        table_info.mark_array_idx,
        MarkBasePosFormat1::MARK_ARRAY_OFFSET_POS,
    )?;
    table_info.base_array_idx = graph.unshared_child(
        table_idx,
        table_info.base_array_idx,
        MarkBasePosFormat1::BASE_ARRAY_OFFSET_POS,
    )?;

    let mark_cov_glyphs = coverage_glyphs(graph, table_info.mark_coverage_idx)?;
    let num_split_points = split_points.len();
    let mut out: Vec<usize> = Vec::with_capacity(num_split_points);
    for i in 0..split_points.len() {
        // [start,end) range
        let start = split_points[i];
        let end = if i < num_split_points - 1 {
            split_points[i + 1]
        } else {
            table_info.mark_class_count
        };

        let new_idx = clone_range(
            graph,
            &table_info,
            start as u16,
            end as u16,
            &mark_cov_glyphs,
        )?;
        out.push(new_idx);
    }

    shrink(graph, &table_info, &mark_cov_glyphs, split_points[0])?;
    Ok(out)
}

struct TableInfo {
    table_idx: ObjIdx,
    mark_coverage_idx: ObjIdx,
    base_coverage_idx: ObjIdx,
    mark_array_idx: ObjIdx,
    base_array_idx: ObjIdx,
    mark_class_count: usize,
    base_count: usize,
    class_mark_indices: Vec<IntSet<u16>>,
    mark_classes: Vec<u16>,
}

fn get_table_info(graph: &mut Graph, table_idx: ObjIdx) -> Option<TableInfo> {
    let mut mark_coverage_idx = None;
    let mut base_coverage_idx = None;
    let mut mark_array_idx = None;
    let mut base_array_idx = None;

    for l in graph.vertex(table_idx)?.real_links() {
        match l.position() {
            MarkBasePosFormat1::MARK_COVERAGE_OFFSET_POS => mark_coverage_idx = Some(l.obj_idx()),
            MarkBasePosFormat1::BASE_COVERAGE_OFFSET_POS => base_coverage_idx = Some(l.obj_idx()),
            MarkBasePosFormat1::MARK_ARRAY_OFFSET_POS => mark_array_idx = Some(l.obj_idx()),
            MarkBasePosFormat1::BASE_ARRAY_OFFSET_POS => base_array_idx = Some(l.obj_idx()),
            _ => (),
        }
        if mark_coverage_idx.is_some()
            && base_coverage_idx.is_some()
            && mark_array_idx.is_some()
            && base_array_idx.is_some()
        {
            break;
        }
    }

    let mark_coverage_idx = mark_coverage_idx?;
    let base_coverage_idx = base_coverage_idx?;
    let mark_array_idx = mark_array_idx?;
    let base_array_idx = base_array_idx?;

    let mark_class_count = MarkBasePosFormat1::from_graph(graph, table_idx)
        .ok()?
        .mark_class_count() as usize;
    let base_count = BaseArray::from_graph(graph, base_array_idx, mark_class_count)
        .ok()?
        .base_count() as usize;

    let (class_mark_indices, mark_classes) =
        get_class_mark_indices_map(graph, mark_class_count, mark_array_idx).ok()?;

    Some(TableInfo {
        table_idx,
        mark_coverage_idx,
        base_coverage_idx,
        mark_array_idx,
        base_array_idx,
        mark_class_count,
        base_count,
        class_mark_indices,
        mark_classes,
    })
}

fn get_class_mark_indices_map(
    graph: &mut Graph,
    mark_class_count: usize,
    mark_array_idx: ObjIdx,
) -> Result<(Vec<IntSet<u16>>, Vec<u16>), RepackError> {
    let mark_array = MarkArray::from_graph(graph, mark_array_idx)?;
    let mark_count = mark_array.mark_count() as usize;
    let mut class_mark_indices = vec![IntSet::empty(); mark_class_count];
    let mut mark_classes = vec![0; mark_count];

    for ((i, mark_class), class) in mark_array
        .iter_mark_index_and_class()
        .zip(mark_classes.iter_mut())
    {
        class_mark_indices
            .get_mut(mark_class as usize)
            .ok_or(RepackError::ErrorReadTable)?
            .insert(i as u16);
        *class = mark_class;
    }
    Ok((class_mark_indices, mark_classes))
}

// ref:<https://github.com/harfbuzz/harfbuzz/blob/aba63bb5f8cb6cfc77ee8cfc2700b3ed9c0838ef/src/graph/markbasepos-graph.hh#L239>
fn compute_split_points(
    graph: &mut Graph,
    table_info: &TableInfo,
) -> Result<Vec<usize>, RepackError> {
    let base_cov_table_size = graph
        .vertex(table_info.base_coverage_idx)
        .ok_or(RepackError::GraphErrorInvalidObjIndex)?
        .table_size();

    let base_size = MarkBasePosFormat1::MIN_SIZE
        + MarkArray::MIN_SIZE
        + BaseArray::MIN_SIZE
        + base_cov_table_size;

    let mark_class_count = table_info.mark_class_count;
    let base_count = table_info.base_count;
    let base_array_idx = table_info.base_array_idx;
    let class_mark_indices = &table_info.class_mark_indices;

    let mut partial_coverage_size = 4;
    let mut accumulated = base_size;
    let mut visited = IntSet::empty();
    let mut out = Vec::new();
    let mut queue = vec![0; graph.vertices.len()];
    for (class, mark_indices) in class_mark_indices.iter().enumerate() {
        let num_marks_for_class = mark_indices.len() as usize;
        partial_coverage_size += 2 * num_marks_for_class;

        // base record size + mark record size for this class
        let mut delta = base_count * Offset16::RAW_BYTE_LEN + num_marks_for_class * 4;
        for i in 0..base_count {
            let pos = 2 + (i * mark_class_count + class) * Offset16::RAW_BYTE_LEN;
            let Some(base_anchor_idx) = graph.index_for_position(base_array_idx, pos as u32) else {
                continue;
            };
            delta +=
                graph.find_subgraph_size(base_anchor_idx, &mut visited, &mut queue, u16::MAX)?;
        }

        // mark record size for this class
        let mark_array_index = table_info.mark_array_idx;
        for idx in mark_indices.iter() {
            let Some(mark_anchor_idx) =
                graph.index_for_position(mark_array_index, 4 + 4 * idx as u32)
            else {
                continue;
            };
            delta +=
                graph.find_subgraph_size(mark_anchor_idx, &mut visited, &mut queue, u16::MAX)?;
        }

        accumulated += delta;
        if accumulated + partial_coverage_size > u16::MAX as usize {
            out.push(class);
            accumulated = base_size + delta;
            partial_coverage_size = 4 + 2 * num_marks_for_class;
            visited.clear();
        }
    }
    Ok(out)
}

// Create a new MarkBasePos that has all of the data for classes from [start, end).
// ref:<https://github.com/harfbuzz/harfbuzz/blob/aba63bb5f8cb6cfc77ee8cfc2700b3ed9c0838ef/src/graph/markbasepos-graph.hh#L412>
fn clone_range(
    graph: &mut Graph,
    table_info: &TableInfo,
    start: u16,
    end: u16,
    org_mark_cov_glyphs: &[GlyphId],
) -> Result<ObjIdx, RepackError> {
    let new_markbase_pos_idx = graph.new_vertex(MarkBasePosFormat1::MIN_SIZE)?;
    let new_mark_class_count = end - start;

    let mut new_markbase_pos = MarkBasePosFormat1::from_graph(graph, new_markbase_pos_idx)?;
    new_markbase_pos.set_format(1);
    new_markbase_pos.set_mark_class_count(new_mark_class_count);

    // link to base coverage
    graph.add_parent_child_link(
        new_markbase_pos_idx,
        table_info.base_coverage_idx,
        LinkWidth::Two,
        MarkBasePosFormat1::BASE_COVERAGE_OFFSET_POS,
        false,
    )?;

    // add new mark coverage
    let cap = org_mark_cov_glyphs.len();
    let mut new_mark_glyphs = Vec::with_capacity(cap);
    let mut mark_classes = Vec::with_capacity(cap);
    let org_mark_classes = &table_info.mark_classes;
    for ((i, &g), &class) in org_mark_cov_glyphs
        .iter()
        .enumerate()
        .zip(org_mark_classes.iter())
        .filter(|(_, class)| (start..end).contains(class))
    {
        new_mark_glyphs.push(g);
        mark_classes.push((i, class));
    }

    add_new_coverage(
        graph,
        &new_mark_glyphs,
        new_markbase_pos_idx,
        LinkWidth::Two,
        MarkBasePosFormat1::MARK_COVERAGE_OFFSET_POS,
    )?;

    add_new_mark_array(
        graph,
        new_markbase_pos_idx,
        table_info.mark_array_idx,
        &mark_classes,
        start,
    )?;

    add_base_array(
        graph,
        new_markbase_pos_idx,
        table_info,
        start as usize,
        end as usize,
    )?;
    Ok(new_markbase_pos_idx)
}

fn add_new_mark_array(
    graph: &mut Graph,
    parent_idx: ObjIdx,
    org_mark_array_idx: ObjIdx,
    org_mark_idx_classes: &[(usize, u16)],
    start_class: u16,
) -> Result<(), RepackError> {
    let new_mark_count = org_mark_idx_classes.len();
    let new_mark_array_size = MarkArray::MIN_SIZE + 4 * new_mark_count;
    let new_mark_array_idx = graph.new_vertex(new_mark_array_size)?;

    let mut new_mark_array = MarkArray::from_graph(graph, new_mark_array_idx)?;
    new_mark_array.set_mark_count(new_mark_count as u16);

    for (i, &(_, org_class)) in org_mark_idx_classes.iter().enumerate() {
        new_mark_array.set_mark_class(i, org_class - start_class);
    }

    let start_pos = MarkArray::MIN_SIZE as u32 + 2;
    for (new_idx, &(old_mark_idx, _)) in org_mark_idx_classes.iter().enumerate() {
        let old_pos = start_pos + old_mark_idx as u32 * 4;
        let new_pos = start_pos + new_idx as u32 * 4;
        let _ = graph.move_child(
            org_mark_array_idx,
            old_pos,
            new_mark_array_idx,
            new_pos,
            Offset16::RAW_BYTE_LEN,
        )?;
    }

    graph.add_parent_child_link(
        parent_idx,
        new_mark_array_idx,
        LinkWidth::Two,
        MarkBasePosFormat1::MARK_ARRAY_OFFSET_POS,
        false,
    )
}

fn add_base_array(
    graph: &mut Graph,
    parent_idx: ObjIdx,
    table_info: &TableInfo,
    start_class: usize,
    end_class: usize,
) -> Result<(), RepackError> {
    let new_class_count = end_class - start_class;
    let base_count = table_info.base_count;

    let new_base_array_size = BaseArray::MIN_SIZE + 2 * base_count * new_class_count;
    let new_base_array_idx = graph.new_vertex(new_base_array_size)?;

    let mut new_base_array = BaseArray::from_graph(graph, new_base_array_idx, new_class_count)?;
    new_base_array.set_base_count(base_count as u16);

    let start_pos = BaseArray::MIN_SIZE as u32;
    let org_base_array_idx = table_info.base_array_idx;
    let org_class_count = table_info.mark_class_count;
    for base_idx in 0..base_count {
        let old_pos_start = start_pos + 2 * (base_idx * org_class_count + start_class) as u32;
        let new_pos_start = start_pos + 2 * (base_idx * new_class_count) as u32;
        graph.move_children(
            org_base_array_idx,
            old_pos_start,
            new_base_array_idx,
            new_pos_start,
            new_class_count as u32,
            Offset16::RAW_BYTE_LEN,
        )?;
    }

    graph.add_parent_child_link(
        parent_idx,
        new_base_array_idx,
        LinkWidth::Two,
        MarkBasePosFormat1::BASE_ARRAY_OFFSET_POS,
        false,
    )
}

fn shrink(
    graph: &mut Graph,
    table_info: &TableInfo,
    org_mark_cov_glyphs: &[GlyphId],
    shrink_point: usize,
) -> Result<(), RepackError> {
    let mut mark_base_pos = MarkBasePosFormat1::from_graph(graph, table_info.table_idx)?;
    mark_base_pos.set_mark_class_count(shrink_point as u16);
    // shrink mark coverage
    let cap = org_mark_cov_glyphs.len();
    let mut retained_mark_glyphs = Vec::with_capacity(cap);
    let mut retained_mark_classes = Vec::with_capacity(cap);
    let org_mark_classes = &table_info.mark_classes;
    for ((i, &g), &class) in org_mark_cov_glyphs
        .iter()
        .enumerate()
        .zip(org_mark_classes.iter())
        .filter(|(_, &class)| (class as usize) < shrink_point)
    {
        retained_mark_glyphs.push(g);
        retained_mark_classes.push((i, class));
    }

    make_coverage(
        graph,
        table_info.table_idx,
        table_info.mark_coverage_idx,
        MarkBasePosFormat1::MARK_COVERAGE_OFFSET_POS,
        &retained_mark_glyphs,
        0..retained_mark_glyphs.len(),
    )?;

    shrink_mark_array(graph, table_info.mark_array_idx, &retained_mark_classes)?;

    shrink_base_array(graph, table_info, shrink_point)
}

fn shrink_mark_array(
    graph: &mut Graph,
    mark_array_idx: ObjIdx,
    retained_mark_classes: &[(usize, u16)],
) -> Result<(), RepackError> {
    let mut mark_array = MarkArray::from_graph(graph, mark_array_idx)?;
    let org_mark_count = mark_array.mark_count();
    for (i, class) in retained_mark_classes.iter().map(|(_, c)| *c).enumerate() {
        mark_array.set_mark_class(i, class);
    }

    let new_mark_count = retained_mark_classes.len() as u16;
    mark_array.set_mark_count(new_mark_count);

    let mark_array_v = graph
        .mut_vertex(mark_array_idx)
        .ok_or(RepackError::GraphErrorInvalidObjIndex)?;

    mark_array_v.tail -= 4 * (org_mark_count - new_mark_count) as usize;

    let links = &mut mark_array_v.real_links;
    let mut new_links = Vec::with_capacity(links.len());
    for (new_idx, old_idx) in retained_mark_classes
        .iter()
        .map(|(old_idx, _)| *old_idx as u32)
        .enumerate()
    {
        let old_pos = old_idx * 4 + 4;
        let Some(mut l) = links.remove_real_link(old_pos) else {
            continue;
        };

        let new_pos = new_idx as u32 * 4 + 4;
        l.update_position(new_pos);
        new_links.push(l);
    }

    links.extend_unsorted(&new_links);
    Ok(())
}

fn shrink_base_array(
    graph: &mut Graph,
    table_info: &TableInfo,
    shrink_point: usize,
) -> Result<(), RepackError> {
    let base_array_v = graph
        .mut_vertex(table_info.base_array_idx)
        .ok_or(RepackError::GraphErrorInvalidObjIndex)?;

    let mark_class_count = table_info.mark_class_count;
    let base_count = table_info.base_count;
    base_array_v.tail -= (mark_class_count - shrink_point) * Offset16::RAW_BYTE_LEN * base_count;

    for l in base_array_v.real_links.sorted_iter_mut() {
        let index = (l.position() - 2) / 2;
        let base = index / mark_class_count as u32;
        let class = index % mark_class_count as u32;
        // sanity check
        if class as usize >= shrink_point {
            // should have been removed
            return Err(RepackError::ErrorSplitSubtable);
        }
        let new_index = base * shrink_point as u32 + class;
        l.update_position(BaseArray::MIN_SIZE as u32 + 2 * new_index);
    }
    Ok(())
}

struct MarkBasePosFormat1<'a>(DataBytes<'a>);

impl<'a> MarkBasePosFormat1<'a> {
    const MIN_SIZE: usize = 12;
    const FORMAT_BYTE_POS: u32 = 0;
    const MARK_COVERAGE_OFFSET_POS: u32 = 2;
    const BASE_COVERAGE_OFFSET_POS: u32 = 4;
    const MARK_CLASS_COUNT_POS: u32 = 6;
    const MARK_ARRAY_OFFSET_POS: u32 = 8;
    const BASE_ARRAY_OFFSET_POS: u32 = 10;

    fn from_graph(graph: &'a mut Graph, obj_idx: ObjIdx) -> Result<Self, RepackError> {
        let data_bytes = DataBytes::from_graph(graph, obj_idx)?;
        let markbase_pos = Self(data_bytes);

        if !markbase_pos.sanitize() {
            return Err(RepackError::ErrorReadTable);
        }

        Ok(markbase_pos)
    }

    fn sanitize(&self) -> bool {
        self.0.len() >= Self::MIN_SIZE
    }

    fn mark_class_count(&self) -> u16 {
        self.0.read_at::<u16>(Self::MARK_CLASS_COUNT_POS as usize)
    }

    fn set_format(&mut self, format: u16) {
        self.0.write_at(format, Self::FORMAT_BYTE_POS as usize);
    }

    fn set_mark_class_count(&mut self, mark_class_count: u16) {
        self.0
            .write_at(mark_class_count, Self::MARK_CLASS_COUNT_POS as usize);
    }
}

struct MarkArray<'a>(DataBytes<'a>);
impl<'a> MarkArray<'a> {
    const MIN_SIZE: usize = 2;
    const MARK_COUNT_POS: usize = 0;

    fn from_graph(graph: &'a mut Graph, obj_idx: ObjIdx) -> Result<Self, RepackError> {
        let data_bytes = DataBytes::from_graph(graph, obj_idx)?;
        let mark_array = Self(data_bytes);

        if !mark_array.sanitize() {
            return Err(RepackError::ErrorReadTable);
        }

        Ok(mark_array)
    }

    fn sanitize(&self) -> bool {
        if self.0.len() < Self::MIN_SIZE {
            return false;
        }

        let mark_count = self.mark_count();
        self.0.len() >= 2 + 4 * mark_count as usize
    }

    fn mark_count(&self) -> u16 {
        self.0.read_at::<u16>(Self::MARK_COUNT_POS)
    }

    fn set_mark_count(&mut self, mark_count: u16) {
        self.0.write_at(mark_count, Self::MARK_COUNT_POS);
    }

    // user is responsible for ensuring no out-of-bound writing
    fn set_mark_class(&mut self, mark_idx: usize, mark_class: u16) {
        let pos = Self::MIN_SIZE + mark_idx * 4;
        self.0.write_at(mark_class, pos);
    }

    fn iter_mark_index_and_class(&self) -> impl Iterator<Item = (usize, u16)> + '_ {
        let mark_count = self.mark_count() as usize;
        let mut iter = 0..mark_count;
        std::iter::from_fn(move || {
            iter.next()
                .map(|i| (i, self.0.read_at::<u16>(Self::MIN_SIZE + i * 4)))
        })
    }
}

struct BaseArray<'a>(DataBytes<'a>);
impl<'a> BaseArray<'a> {
    const MIN_SIZE: usize = 2;
    const BASE_COUNT_POS: usize = 0;

    fn from_graph(
        graph: &'a mut Graph,
        obj_idx: ObjIdx,
        class_count: usize,
    ) -> Result<Self, RepackError> {
        let data_bytes = DataBytes::from_graph(graph, obj_idx)?;
        let base_array = Self(data_bytes);

        if !base_array.sanitize(class_count) {
            return Err(RepackError::ErrorReadTable);
        }
        Ok(base_array)
    }

    fn sanitize(&self, class_count: usize) -> bool {
        if self.0.len() < Self::MIN_SIZE {
            return false;
        }

        let base_count = self.base_count() as usize;
        self.0.len() >= Self::MIN_SIZE + base_count * class_count * Offset16::RAW_BYTE_LEN
    }

    fn base_count(&self) -> u16 {
        self.0.read_at::<u16>(Self::BASE_COUNT_POS)
    }

    fn set_base_count(&mut self, base_count: u16) {
        self.0.write_at(base_count, Self::BASE_COUNT_POS);
    }
}