read_fonts/tables/colr/
closure.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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//! computing closure for the colr table

use font_types::{GlyphId, GlyphId16};

use crate::{collections::IntSet, tables::variations::NO_VARIATION_INDEX, ResolveOffset};

use super::{
    Clip, ClipBox, ClipBoxFormat2, ClipList, ColorLine, ColorStop, Colr, Paint, PaintColrGlyph,
    PaintColrLayers, PaintComposite, PaintGlyph, PaintLinearGradient, PaintRadialGradient,
    PaintRotate, PaintRotateAroundCenter, PaintScale, PaintScaleAroundCenter, PaintScaleUniform,
    PaintScaleUniformAroundCenter, PaintSkew, PaintSkewAroundCenter, PaintSolid,
    PaintSweepGradient, PaintTransform, PaintTranslate, PaintVarLinearGradient,
    PaintVarRadialGradient, PaintVarRotate, PaintVarRotateAroundCenter, PaintVarScale,
    PaintVarScaleAroundCenter, PaintVarScaleUniform, PaintVarScaleUniformAroundCenter,
    PaintVarSkew, PaintVarSkewAroundCenter, PaintVarSolid, PaintVarSweepGradient,
    PaintVarTransform, PaintVarTranslate, VarAffine2x3, VarColorLine, VarColorStop,
};

impl Colr<'_> {
    //Collect the transitive closure of V0 palette indices needed for all of the input glyphs set
    //It's similar to closure glyphs but in a separate fn, because v1 closure might adds more v0 glyphs, so this fn needs to be called after v1 closure
    pub fn v0_closure_palette_indices(
        &self,
        glyph_set: &IntSet<GlyphId>,
        palette_indices: &mut IntSet<u16>,
    ) {
        let Some(Ok(records)) = self.base_glyph_records() else {
            return;
        };
        for glyph_id in glyph_set.iter() {
            let Ok(glyph_id) = glyph_id.try_into() else {
                continue;
            };
            let record = match records.binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id)) {
                Ok(idx) => records[idx],
                _ => continue,
            };
            let start = record.first_layer_index() as usize;
            let end = start + record.num_layers() as usize;
            for layer_index in start..=end {
                if let Ok((_gid, palette_id)) = self.v0_layer(layer_index) {
                    palette_indices.insert(palette_id);
                }
            }
        }
    }

    /// Collect the transitive closure of v1 glyphs,layer/paletted indices and variation/delta set indices for COLRv1
    pub fn v1_closure(
        &self,
        glyph_set: &mut IntSet<GlyphId>,
        layer_indices: &mut IntSet<u32>,
        palette_indices: &mut IntSet<u16>,
        variation_indices: &mut IntSet<u32>,
        delta_set_indices: &mut IntSet<u32>,
    ) {
        if self.version() < 1 {
            return;
        }

        let mut c =
            Colrv1ClosureContext::new(layer_indices, palette_indices, variation_indices, self);
        if let Some(Ok(base_glyph_list)) = self.base_glyph_list() {
            let base_glyph_records = base_glyph_list.base_glyph_paint_records();
            let offset_data = base_glyph_list.offset_data();
            for paint_record in base_glyph_records {
                let gid = paint_record.glyph_id();
                if !glyph_set.contains(GlyphId::from(gid)) {
                    continue;
                }
                if let Ok(paint) = paint_record.paint(offset_data) {
                    c.dispatch(&paint);
                }
            }

            glyph_set.union(&c.glyph_set);
        }

        if let Some(Ok(clip_list)) = self.clip_list() {
            c.glyph_set.union(glyph_set);
            for clip_record in clip_list.clips() {
                clip_record.v1_closure(&mut c, &clip_list);
            }
        }

        //when a DeltaSetIndexMap is included, collected variation indices are actually delta set indices, we need to map them into variation indices
        if let Some(Ok(var_index_map)) = self.var_index_map() {
            delta_set_indices.extend(variation_indices.iter());
            variation_indices.clear();
            for idx in delta_set_indices.iter() {
                if let Ok(var_idx) = var_index_map.get(idx) {
                    let var_idx = ((var_idx.outer as u32) << 16) + var_idx.inner as u32;
                    variation_indices.insert(var_idx);
                }
            }
        }
    }

    /// Collect the transitive closure of V0 glyphs needed for all of the input glyphs set
    pub fn v0_closure_glyphs(
        &self,
        glyph_set: &IntSet<GlyphId>,
        glyphset_colrv0: &mut IntSet<GlyphId>,
    ) {
        glyphset_colrv0.union(glyph_set);
        let Some(Ok(records)) = self.base_glyph_records() else {
            return;
        };
        for glyph_id in glyph_set.iter() {
            let Ok(glyph_id) = glyph_id.try_into() else {
                continue;
            };
            let record = match records.binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id)) {
                Ok(idx) => records[idx],
                _ => continue,
            };
            let start = record.first_layer_index() as usize;
            let end = start + record.num_layers() as usize;
            for layer_index in start..=end {
                if let Ok((gid, _palette_id)) = self.v0_layer(layer_index) {
                    glyphset_colrv0.insert(GlyphId::from(gid));
                }
            }
        }
    }
}

struct Colrv1ClosureContext<'a> {
    glyph_set: IntSet<GlyphId>,
    layer_indices: &'a mut IntSet<u32>,
    palette_indices: &'a mut IntSet<u16>,
    variation_indices: &'a mut IntSet<u32>,
    colr: &'a Colr<'a>,
    nesting_level_left: u8,
    visited_paints: IntSet<u32>,
    colr_head: usize,
}

impl<'a> Colrv1ClosureContext<'a> {
    pub fn new(
        layer_indices: &'a mut IntSet<u32>,
        palette_indices: &'a mut IntSet<u16>,
        variation_indices: &'a mut IntSet<u32>,
        colr: &'a Colr,
    ) -> Self {
        let colr_head = colr.offset_data().as_bytes().as_ptr() as usize;
        Self {
            glyph_set: IntSet::empty(),
            layer_indices,
            palette_indices,
            variation_indices,
            colr,
            nesting_level_left: 64,
            visited_paints: IntSet::empty(),
            colr_head,
        }
    }

    fn dispatch(&mut self, paint: &Paint) {
        if self.nesting_level_left == 0 {
            return;
        }

        if self.paint_visited(paint) {
            return;
        }
        self.nesting_level_left -= 1;
        paint.v1_closure(self);
        self.nesting_level_left += 1;
    }

    fn paint_visited(&mut self, paint: &Paint) -> bool {
        let offset = (paint.offset_data().as_bytes().as_ptr() as usize - self.colr_head) as u32;
        if self.visited_paints.contains(offset) {
            return true;
        }

        self.visited_paints.insert(offset);
        false
    }

    fn add_layer_indices(&mut self, first_layer_index: u32, last_layer_index: u32) {
        self.layer_indices
            .insert_range(first_layer_index..=last_layer_index);
    }

    fn add_palette_index(&mut self, palette_index: u16) {
        self.palette_indices.insert(palette_index);
    }

    fn add_variation_indices(&mut self, var_index_base: u32, num_vars: u8) {
        if num_vars == 0 || var_index_base == NO_VARIATION_INDEX {
            return;
        }

        let last_var_index = var_index_base + num_vars as u32 - 1;
        self.variation_indices
            .insert_range(var_index_base..=last_var_index);
    }

    fn add_glyph_id(&mut self, gid: GlyphId16) {
        self.glyph_set.insert(GlyphId::from(gid));
    }
}

impl ColorStop {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        c.add_palette_index(self.palette_index());
    }
}

impl VarColorStop {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        c.add_palette_index(self.palette_index());
        c.add_variation_indices(self.var_index_base(), 2);
    }
}

impl ColorLine<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        for colorstop in self.color_stops() {
            colorstop.v1_closure(c);
        }
    }
}

impl VarColorLine<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        for var_colorstop in self.color_stops() {
            var_colorstop.v1_closure(c);
        }
    }
}

impl Paint<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        match self {
            Self::ColrLayers(item) => item.v1_closure(c),
            Self::Solid(item) => item.v1_closure(c),
            Self::VarSolid(item) => item.v1_closure(c),
            Self::LinearGradient(item) => item.v1_closure(c),
            Self::VarLinearGradient(item) => item.v1_closure(c),
            Self::RadialGradient(item) => item.v1_closure(c),
            Self::VarRadialGradient(item) => item.v1_closure(c),
            Self::SweepGradient(item) => item.v1_closure(c),
            Self::VarSweepGradient(item) => item.v1_closure(c),
            Self::Glyph(item) => item.v1_closure(c),
            Self::ColrGlyph(item) => item.v1_closure(c),
            Self::Transform(item) => item.v1_closure(c),
            Self::VarTransform(item) => item.v1_closure(c),
            Self::Translate(item) => item.v1_closure(c),
            Self::VarTranslate(item) => item.v1_closure(c),
            Self::Scale(item) => item.v1_closure(c),
            Self::VarScale(item) => item.v1_closure(c),
            Self::ScaleAroundCenter(item) => item.v1_closure(c),
            Self::VarScaleAroundCenter(item) => item.v1_closure(c),
            Self::ScaleUniform(item) => item.v1_closure(c),
            Self::VarScaleUniform(item) => item.v1_closure(c),
            Self::ScaleUniformAroundCenter(item) => item.v1_closure(c),
            Self::VarScaleUniformAroundCenter(item) => item.v1_closure(c),
            Self::Rotate(item) => item.v1_closure(c),
            Self::VarRotate(item) => item.v1_closure(c),
            Self::RotateAroundCenter(item) => item.v1_closure(c),
            Self::VarRotateAroundCenter(item) => item.v1_closure(c),
            Self::Skew(item) => item.v1_closure(c),
            Self::VarSkew(item) => item.v1_closure(c),
            Self::SkewAroundCenter(item) => item.v1_closure(c),
            Self::VarSkewAroundCenter(item) => item.v1_closure(c),
            Self::Composite(item) => item.v1_closure(c),
        }
    }
}

impl PaintColrLayers<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        let num_layers = self.num_layers();
        if num_layers == 0 {
            return;
        }

        let Some(Ok(layer_list)) = c.colr.layer_list() else {
            return;
        };
        let first_layer_index = self.first_layer_index();
        let last_layer_index = first_layer_index + num_layers as u32 - 1;
        c.add_layer_indices(first_layer_index, last_layer_index);

        let offset_data = layer_list.offset_data();
        for layer_index in first_layer_index..=last_layer_index {
            if let Some(paint_offset) = layer_list.paint_offsets().get(layer_index as usize) {
                if let Ok(paint) = paint_offset.get().resolve::<Paint>(offset_data) {
                    c.dispatch(&paint);
                }
            }
        }
    }
}

impl PaintSolid<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        c.add_palette_index(self.palette_index());
    }
}

impl PaintVarSolid<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        c.add_palette_index(self.palette_index());
        c.add_variation_indices(self.var_index_base(), 1);
    }
}

impl PaintLinearGradient<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(colorline) = self.color_line() {
            colorline.v1_closure(c);
        }
    }
}

impl PaintVarLinearGradient<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(var_colorline) = self.color_line() {
            var_colorline.v1_closure(c);
        }
        c.add_variation_indices(self.var_index_base(), 6);
    }
}

impl PaintRadialGradient<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(colorline) = self.color_line() {
            colorline.v1_closure(c);
        }
    }
}

impl PaintVarRadialGradient<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(var_colorline) = self.color_line() {
            var_colorline.v1_closure(c);
        }
        c.add_variation_indices(self.var_index_base(), 6);
    }
}

impl PaintSweepGradient<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(colorline) = self.color_line() {
            colorline.v1_closure(c);
        }
    }
}

impl PaintVarSweepGradient<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(var_colorline) = self.color_line() {
            var_colorline.v1_closure(c);
        }
        c.add_variation_indices(self.var_index_base(), 4);
    }
}

impl PaintGlyph<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        c.add_glyph_id(self.glyph_id());
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl PaintColrGlyph<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        let glyph_id = self.glyph_id();
        let Some(Ok(list)) = c.colr.base_glyph_list() else {
            return;
        };
        let records = list.base_glyph_paint_records();
        let record = match records.binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id)) {
            Ok(ix) => &records[ix],
            _ => return,
        };
        if let Ok(paint) = record.paint(list.offset_data()) {
            c.add_glyph_id(glyph_id);
            c.dispatch(&paint);
        }
    }
}

impl PaintTransform<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl VarAffine2x3<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        c.add_variation_indices(self.var_index_base(), 6);
    }
}

impl PaintVarTransform<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
            if let Ok(affine2x3) = self.transform() {
                affine2x3.v1_closure(c);
            }
        }
    }
}

impl PaintTranslate<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl PaintVarTranslate<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
            c.add_variation_indices(self.var_index_base(), 2);
        }
    }
}

impl PaintScale<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl PaintVarScale<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
            c.add_variation_indices(self.var_index_base(), 2);
        }
    }
}

impl PaintScaleAroundCenter<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl PaintVarScaleAroundCenter<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
            c.add_variation_indices(self.var_index_base(), 4);
        }
    }
}

impl PaintScaleUniform<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl PaintVarScaleUniform<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
            c.add_variation_indices(self.var_index_base(), 1);
        }
    }
}

impl PaintScaleUniformAroundCenter<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl PaintVarScaleUniformAroundCenter<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
            c.add_variation_indices(self.var_index_base(), 3);
        }
    }
}

impl PaintRotate<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl PaintVarRotate<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
            c.add_variation_indices(self.var_index_base(), 1);
        }
    }
}

impl PaintRotateAroundCenter<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl PaintVarRotateAroundCenter<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
            c.add_variation_indices(self.var_index_base(), 3);
        }
    }
}

impl PaintSkew<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl PaintVarSkew<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
            c.add_variation_indices(self.var_index_base(), 2);
        }
    }
}

impl PaintSkewAroundCenter<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
        }
    }
}

impl PaintVarSkewAroundCenter<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(paint) = self.paint() {
            c.dispatch(&paint);
            c.add_variation_indices(self.var_index_base(), 4);
        }
    }
}

impl PaintComposite<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Ok(source_paint) = self.source_paint() {
            c.dispatch(&source_paint);
        }

        if let Ok(backdrop_paint) = self.backdrop_paint() {
            c.dispatch(&backdrop_paint);
        }
    }
}

impl Clip {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext, clip_list: &ClipList) {
        let Ok(clip_box) = self.clip_box(clip_list.offset_data()) else {
            return;
        };
        //TODO: replace below code when we have intersects(Range) available for int-set
        let mut included_gids = IntSet::empty();
        let start_id = GlyphId::from(self.start_glyph_id());
        let end_id = GlyphId::from(self.end_glyph_id());
        included_gids.insert_range(start_id..=end_id);
        included_gids.intersect(&c.glyph_set);

        if included_gids.is_empty() {
            return;
        }
        clip_box.v1_closure(c);
    }
}

impl ClipBox<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        if let Self::Format2(item) = self {
            item.v1_closure(c)
        }
    }
}

impl ClipBoxFormat2<'_> {
    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
        c.add_variation_indices(self.var_index_base(), 4);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{FontRef, GlyphId, TableProvider};

    #[test]
    fn test_colr_v0_closure() {
        let font = FontRef::new(font_test_data::COLRV0V1_VARIABLE).unwrap();
        let colr = font.colr().unwrap();

        let mut input_glyph_set = IntSet::empty();
        input_glyph_set.insert(GlyphId::new(168));

        let mut glyph_set_colred = IntSet::empty();

        colr.v0_closure_glyphs(&input_glyph_set, &mut glyph_set_colred);
        assert_eq!(glyph_set_colred.len(), 9);
        assert!(glyph_set_colred.contains(GlyphId::new(5)));
        assert!(glyph_set_colred.contains(GlyphId::new(168)));
        assert!(glyph_set_colred.contains(GlyphId::new(170)));
        assert!(glyph_set_colred.contains(GlyphId::new(171)));
        assert!(glyph_set_colred.contains(GlyphId::new(172)));
        assert!(glyph_set_colred.contains(GlyphId::new(173)));
        assert!(glyph_set_colred.contains(GlyphId::new(174)));
        assert!(glyph_set_colred.contains(GlyphId::new(175)));
        assert!(glyph_set_colred.contains(GlyphId::new(176)));

        let mut palette_indices = IntSet::empty();
        colr.v0_closure_palette_indices(&glyph_set_colred, &mut palette_indices);
        assert_eq!(palette_indices.len(), 8);
        assert!(palette_indices.contains(0));
        assert!(palette_indices.contains(1));
        assert!(palette_indices.contains(2));
        assert!(palette_indices.contains(3));
        assert!(palette_indices.contains(4));
        assert!(palette_indices.contains(5));
        assert!(palette_indices.contains(6));
        assert!(palette_indices.contains(10));
    }

    #[test]
    fn test_colr_v0_closure_not_found() {
        let font = FontRef::new(font_test_data::COLRV0V1_VARIABLE).unwrap();
        let colr = font.colr().unwrap();

        let mut input_glyph_set = IntSet::empty();
        input_glyph_set.insert(GlyphId::new(8));

        let mut glyph_set_colred = IntSet::empty();

        colr.v0_closure_glyphs(&input_glyph_set, &mut glyph_set_colred);
        assert_eq!(glyph_set_colred.len(), 1);
        assert!(glyph_set_colred.contains(GlyphId::new(8)));
    }

    #[test]
    fn test_colr_v1_closure_no_var() {
        let font = FontRef::new(font_test_data::COLRV0V1_VARIABLE).unwrap();
        let colr = font.colr().unwrap();

        let mut glyph_set = IntSet::empty();
        glyph_set.insert(GlyphId::new(220));
        glyph_set.insert(GlyphId::new(120));

        let mut layer_indices = IntSet::empty();
        let mut palette_indices = IntSet::empty();
        let mut variation_indices = IntSet::empty();
        let mut delta_set_indices = IntSet::empty();

        colr.v1_closure(
            &mut glyph_set,
            &mut layer_indices,
            &mut palette_indices,
            &mut variation_indices,
            &mut delta_set_indices,
        );

        assert_eq!(glyph_set.len(), 6);
        assert!(glyph_set.contains(GlyphId::new(6)));
        assert!(glyph_set.contains(GlyphId::new(7)));
        assert!(glyph_set.contains(GlyphId::new(220)));
        assert!(glyph_set.contains(GlyphId::new(3)));
        assert!(glyph_set.contains(GlyphId::new(2)));
        assert!(glyph_set.contains(GlyphId::new(120)));

        assert_eq!(palette_indices.len(), 5);
        assert!(palette_indices.contains(0));
        assert!(palette_indices.contains(4));
        assert!(palette_indices.contains(10));
        assert!(palette_indices.contains(11));
        assert!(palette_indices.contains(12));

        assert_eq!(layer_indices.len(), 2);
        assert!(layer_indices.contains(0));
        assert!(layer_indices.contains(1));

        assert!(variation_indices.is_empty());
        assert!(delta_set_indices.is_empty());
    }

    #[test]
    fn test_colr_v1_closure_w_var() {
        let font = FontRef::new(font_test_data::COLRV0V1_VARIABLE).unwrap();
        let colr = font.colr().unwrap();

        let mut glyph_set = IntSet::empty();
        glyph_set.insert(GlyphId::new(109));

        let mut layer_indices = IntSet::empty();
        let mut palette_indices = IntSet::empty();
        let mut variation_indices = IntSet::empty();
        let mut delta_set_indices = IntSet::empty();

        colr.v1_closure(
            &mut glyph_set,
            &mut layer_indices,
            &mut palette_indices,
            &mut variation_indices,
            &mut delta_set_indices,
        );

        assert_eq!(glyph_set.len(), 2);
        assert!(glyph_set.contains(GlyphId::new(3)));
        assert!(glyph_set.contains(GlyphId::new(109)));

        assert_eq!(palette_indices.len(), 2);
        assert!(palette_indices.contains(1));
        assert!(palette_indices.contains(4));

        assert!(layer_indices.is_empty());

        assert_eq!(delta_set_indices.len(), 6);
        assert!(delta_set_indices.contains(51));
        assert!(delta_set_indices.contains(52));
        assert!(delta_set_indices.contains(53));
        assert!(delta_set_indices.contains(54));
        assert!(delta_set_indices.contains(55));
        assert!(delta_set_indices.contains(56));

        assert_eq!(variation_indices.len(), 6);
        assert!(variation_indices.contains(0x160000_u32));
        assert!(variation_indices.contains(0x170000_u32));
        assert!(variation_indices.contains(0x180000_u32));
        assert!(variation_indices.contains(0x190000_u32));
        assert!(variation_indices.contains(0x1A0000_u32));
        assert!(variation_indices.contains(0x1B0000_u32));
    }
}