Skip to main content

read_fonts/tables/colr/
closure.rs

1//! computing closure for the colr table
2use font_types::GlyphId;
3
4use crate::{collections::IntSet, tables::variations::NO_VARIATION_INDEX, ResolveOffset};
5
6use super::{
7    Clip, ClipBox, ClipBoxFormat2, ClipList, ColorLine, ColorStop, Colr, Paint, PaintColrGlyph,
8    PaintColrLayers, PaintComposite, PaintGlyph, PaintGlyph2, PaintLinearGradient,
9    PaintRadialGradient, PaintRotate, PaintRotateAroundCenter, PaintScale, PaintScaleAroundCenter,
10    PaintScaleUniform, PaintScaleUniformAroundCenter, PaintSkew, PaintSkewAroundCenter, PaintSolid,
11    PaintSweepGradient, PaintTransform, PaintTranslate, PaintVarLinearGradient,
12    PaintVarRadialGradient, PaintVarRotate, PaintVarRotateAroundCenter, PaintVarScale,
13    PaintVarScaleAroundCenter, PaintVarScaleUniform, PaintVarScaleUniformAroundCenter,
14    PaintVarSkew, PaintVarSkewAroundCenter, PaintVarSolid, PaintVarSweepGradient,
15    PaintVarTransform, PaintVarTranslate, VarAffine2x3, VarColorLine, VarColorStop,
16};
17
18impl Colr<'_> {
19    //Collect the transitive closure of V0 palette indices needed for all of the input glyphs set
20    //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
21    pub fn v0_closure_palette_indices(
22        &self,
23        glyph_set: &IntSet<GlyphId>,
24        palette_indices: &mut IntSet<u16>,
25    ) {
26        let Some(Ok(records)) = self.base_glyph_records() else {
27            return;
28        };
29
30        let Some(Ok(layers)) = self.layer_records() else {
31            return;
32        };
33        let num_records = records.len() as u32;
34        let bit_storage = u32::BITS - num_records.leading_zeros();
35        if glyph_set.is_inverted() || num_records <= glyph_set.len() as u32 * bit_storage {
36            for record in records {
37                if !glyph_set.contains(GlyphId::from(record.glyph_id())) {
38                    continue;
39                }
40                let start = record.first_layer_index() as usize;
41                let end = start + record.num_layers() as usize;
42                for layer_index in start..end {
43                    if let Some(layer) = layers.get(layer_index) {
44                        palette_indices.insert(layer.palette_index());
45                    }
46                }
47            }
48        } else {
49            for glyph_id in glyph_set.iter() {
50                let Ok(glyph_id) = glyph_id.try_into() else {
51                    continue;
52                };
53                let record = match records.binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id)) {
54                    Ok(idx) => records[idx],
55                    _ => continue,
56                };
57                let start = record.first_layer_index() as usize;
58                let end = start + record.num_layers() as usize;
59                for layer_index in start..end {
60                    if let Some(layer) = layers.get(layer_index) {
61                        palette_indices.insert(layer.palette_index());
62                    }
63                }
64            }
65        }
66    }
67
68    /// Collect the transitive closure of v1 glyphs,layer/paletted indices and variation/delta set indices for COLRv1
69    pub fn v1_closure(
70        &self,
71        glyph_set: &mut IntSet<GlyphId>,
72        layer_indices: &mut IntSet<u32>,
73        palette_indices: &mut IntSet<u16>,
74        variation_indices: &mut IntSet<u32>,
75    ) {
76        if self.version() < 1 {
77            return;
78        }
79
80        let mut c =
81            Colrv1ClosureContext::new(layer_indices, palette_indices, variation_indices, self);
82        if let Some(Ok(base_glyph_list)) = self.base_glyph_list() {
83            let base_glyph_records = base_glyph_list.base_glyph_paint_records();
84            let offset_data = base_glyph_list.offset_data();
85            let num_records = base_glyph_records.len() as u32;
86            let bit_storage = u32::BITS - num_records.leading_zeros();
87            if glyph_set.is_inverted() || num_records <= glyph_set.len() as u32 * bit_storage {
88                for record in base_glyph_records {
89                    let gid = record.glyph_id();
90                    if !glyph_set.contains(GlyphId::from(gid)) {
91                        continue;
92                    }
93                    if let Ok(paint) = record.paint(offset_data) {
94                        c.dispatch(&paint);
95                    }
96                }
97            } else {
98                for glyph_id in glyph_set.iter() {
99                    let Ok(glyph_id) = glyph_id.try_into() else {
100                        continue;
101                    };
102                    let record = match base_glyph_records
103                        .binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id))
104                    {
105                        Ok(idx) => &base_glyph_records[idx],
106                        _ => continue,
107                    };
108                    if record.paint_offset().is_null() {
109                        continue;
110                    }
111                    if let Ok(paint) = record.paint(offset_data) {
112                        c.dispatch(&paint);
113                    }
114                }
115            }
116            glyph_set.union(&c.glyph_set);
117        }
118
119        if let Some(Ok(clip_list)) = self.clip_list() {
120            c.glyph_set.union(glyph_set);
121            for clip_record in clip_list.clips() {
122                clip_record.v1_closure(&mut c, &clip_list);
123            }
124        }
125    }
126
127    /// Collect the transitive closure of V0 glyphs needed for all of the input glyphs set
128    pub fn v0_closure_glyphs(
129        &self,
130        glyph_set: &IntSet<GlyphId>,
131        glyphset_colrv0: &mut IntSet<GlyphId>,
132    ) {
133        glyphset_colrv0.union(glyph_set);
134        let Some(Ok(records)) = self.base_glyph_records() else {
135            return;
136        };
137        let Some(Ok(layers)) = self.layer_records() else {
138            return;
139        };
140        let num_records = records.len() as u32;
141        let bit_storage = u32::BITS - num_records.leading_zeros();
142        if glyph_set.is_inverted() || num_records <= glyph_set.len() as u32 * bit_storage {
143            for record in records {
144                if !glyph_set.contains(GlyphId::from(record.glyph_id())) {
145                    continue;
146                }
147                let start = record.first_layer_index() as usize;
148                let end = start + record.num_layers() as usize;
149                for layer_index in start..end {
150                    if let Some(layer) = layers.get(layer_index) {
151                        glyphset_colrv0.insert(GlyphId::from(layer.glyph_id()));
152                    }
153                }
154            }
155        } else {
156            for glyph_id in glyph_set.iter() {
157                let Ok(glyph_id) = glyph_id.try_into() else {
158                    continue;
159                };
160                let record = match records.binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id)) {
161                    Ok(idx) => records[idx],
162                    _ => continue,
163                };
164                let start = record.first_layer_index() as usize;
165                let end = start + record.num_layers() as usize;
166                for layer_index in start..end {
167                    if let Some(layer) = layers.get(layer_index) {
168                        glyphset_colrv0.insert(GlyphId::from(layer.glyph_id()));
169                    }
170                }
171            }
172        }
173    }
174}
175
176struct Colrv1ClosureContext<'a> {
177    glyph_set: IntSet<GlyphId>,
178    layer_indices: &'a mut IntSet<u32>,
179    palette_indices: &'a mut IntSet<u16>,
180    variation_indices: &'a mut IntSet<u32>,
181    colr: &'a Colr<'a>,
182    nesting_level_left: u8,
183    visited_paints: IntSet<u32>,
184    colr_head: usize,
185}
186
187impl<'a> Colrv1ClosureContext<'a> {
188    pub fn new(
189        layer_indices: &'a mut IntSet<u32>,
190        palette_indices: &'a mut IntSet<u16>,
191        variation_indices: &'a mut IntSet<u32>,
192        colr: &'a Colr,
193    ) -> Self {
194        let colr_head = colr.offset_data().as_bytes().as_ptr() as usize;
195        Self {
196            glyph_set: IntSet::empty(),
197            layer_indices,
198            palette_indices,
199            variation_indices,
200            colr,
201            nesting_level_left: 64,
202            visited_paints: IntSet::empty(),
203            colr_head,
204        }
205    }
206
207    fn dispatch(&mut self, paint: &Paint) {
208        if self.nesting_level_left == 0 {
209            return;
210        }
211
212        if self.paint_visited(paint) {
213            return;
214        }
215        self.nesting_level_left -= 1;
216        paint.v1_closure(self);
217        self.nesting_level_left += 1;
218    }
219
220    fn paint_visited(&mut self, paint: &Paint) -> bool {
221        let offset = (paint.offset_data().as_bytes().as_ptr() as usize - self.colr_head) as u32;
222        if self.visited_paints.contains(offset) {
223            return true;
224        }
225
226        self.visited_paints.insert(offset);
227        false
228    }
229
230    fn add_layer_index(&mut self, layer_index: u32) {
231        self.layer_indices.insert(layer_index);
232    }
233
234    fn add_palette_index(&mut self, palette_index: u16) {
235        self.palette_indices.insert(palette_index);
236    }
237
238    fn add_variation_indices(&mut self, var_index_base: u32, num_vars: u8) {
239        if num_vars == 0 || var_index_base == NO_VARIATION_INDEX {
240            return;
241        }
242        let Some(last_var_index) = compute_inclusive_end(var_index_base, num_vars as u32) else {
243            return;
244        };
245        self.variation_indices
246            .insert_range(var_index_base..=last_var_index);
247    }
248
249    fn add_glyph_id(&mut self, gid: impl Into<GlyphId>) {
250        self.glyph_set.insert(gid.into());
251    }
252}
253
254impl ColorStop {
255    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
256        c.add_palette_index(self.palette_index());
257    }
258}
259
260impl VarColorStop {
261    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
262        c.add_palette_index(self.palette_index());
263        c.add_variation_indices(self.var_index_base(), 2);
264    }
265}
266
267impl ColorLine<'_> {
268    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
269        for colorstop in self.color_stops() {
270            colorstop.v1_closure(c);
271        }
272    }
273}
274
275impl VarColorLine<'_> {
276    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
277        for var_colorstop in self.color_stops() {
278            var_colorstop.v1_closure(c);
279        }
280    }
281}
282
283impl Paint<'_> {
284    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
285        match self {
286            Self::ColrLayers(item) => item.v1_closure(c),
287            Self::Solid(item) => item.v1_closure(c),
288            Self::VarSolid(item) => item.v1_closure(c),
289            Self::LinearGradient(item) => item.v1_closure(c),
290            Self::VarLinearGradient(item) => item.v1_closure(c),
291            Self::RadialGradient(item) => item.v1_closure(c),
292            Self::VarRadialGradient(item) => item.v1_closure(c),
293            Self::SweepGradient(item) => item.v1_closure(c),
294            Self::VarSweepGradient(item) => item.v1_closure(c),
295            Self::Glyph(item) => item.v1_closure(c),
296            Self::Glyph2(item) => item.v1_closure(c),
297            Self::ColrGlyph(item) => item.v1_closure(c),
298            Self::Transform(item) => item.v1_closure(c),
299            Self::VarTransform(item) => item.v1_closure(c),
300            Self::Translate(item) => item.v1_closure(c),
301            Self::VarTranslate(item) => item.v1_closure(c),
302            Self::Scale(item) => item.v1_closure(c),
303            Self::VarScale(item) => item.v1_closure(c),
304            Self::ScaleAroundCenter(item) => item.v1_closure(c),
305            Self::VarScaleAroundCenter(item) => item.v1_closure(c),
306            Self::ScaleUniform(item) => item.v1_closure(c),
307            Self::VarScaleUniform(item) => item.v1_closure(c),
308            Self::ScaleUniformAroundCenter(item) => item.v1_closure(c),
309            Self::VarScaleUniformAroundCenter(item) => item.v1_closure(c),
310            Self::Rotate(item) => item.v1_closure(c),
311            Self::VarRotate(item) => item.v1_closure(c),
312            Self::RotateAroundCenter(item) => item.v1_closure(c),
313            Self::VarRotateAroundCenter(item) => item.v1_closure(c),
314            Self::Skew(item) => item.v1_closure(c),
315            Self::VarSkew(item) => item.v1_closure(c),
316            Self::SkewAroundCenter(item) => item.v1_closure(c),
317            Self::VarSkewAroundCenter(item) => item.v1_closure(c),
318            Self::Composite(item) => item.v1_closure(c),
319        }
320    }
321}
322
323impl PaintColrLayers<'_> {
324    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
325        let num_layers = self.num_layers();
326        if num_layers == 0 {
327            return;
328        }
329
330        let Some(Ok(layer_list)) = c.colr.layer_list() else {
331            return;
332        };
333
334        let first_layer_index = self.first_layer_index();
335        let Some(last_layer_index) = compute_inclusive_end(first_layer_index, num_layers as u32)
336        else {
337            return;
338        };
339
340        let offset_data = layer_list.offset_data();
341        let paint_offsets = layer_list.paint_offsets();
342        for layer_index in first_layer_index..=last_layer_index {
343            let Some(paint_offset) = paint_offsets.get(layer_index as usize) else {
344                break;
345            };
346            if let Ok(paint) = paint_offset.get().resolve::<Paint>(offset_data) {
347                c.add_layer_index(layer_index);
348                c.dispatch(&paint);
349            }
350        }
351    }
352}
353
354impl PaintSolid<'_> {
355    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
356        c.add_palette_index(self.palette_index());
357    }
358}
359
360impl PaintVarSolid<'_> {
361    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
362        c.add_palette_index(self.palette_index());
363        c.add_variation_indices(self.var_index_base(), 1);
364    }
365}
366
367impl PaintLinearGradient<'_> {
368    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
369        if let Ok(colorline) = self.color_line() {
370            colorline.v1_closure(c);
371        }
372    }
373}
374
375impl PaintVarLinearGradient<'_> {
376    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
377        if let Ok(var_colorline) = self.color_line() {
378            var_colorline.v1_closure(c);
379            c.add_variation_indices(self.var_index_base(), 6);
380        }
381    }
382}
383
384impl PaintRadialGradient<'_> {
385    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
386        if let Ok(colorline) = self.color_line() {
387            colorline.v1_closure(c);
388        }
389    }
390}
391
392impl PaintVarRadialGradient<'_> {
393    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
394        if let Ok(var_colorline) = self.color_line() {
395            var_colorline.v1_closure(c);
396            c.add_variation_indices(self.var_index_base(), 6);
397        }
398    }
399}
400
401impl PaintSweepGradient<'_> {
402    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
403        if let Ok(colorline) = self.color_line() {
404            colorline.v1_closure(c);
405        }
406    }
407}
408
409impl PaintVarSweepGradient<'_> {
410    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
411        if let Ok(var_colorline) = self.color_line() {
412            var_colorline.v1_closure(c);
413            c.add_variation_indices(self.var_index_base(), 4);
414        }
415    }
416}
417
418impl PaintGlyph<'_> {
419    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
420        if let Ok(paint) = self.paint() {
421            c.add_glyph_id(self.glyph_id());
422            c.dispatch(&paint);
423        }
424    }
425}
426
427impl PaintGlyph2<'_> {
428    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
429        if let Ok(paint) = self.paint() {
430            c.add_glyph_id(self.glyph_id());
431            c.dispatch(&paint);
432        }
433    }
434}
435
436impl PaintColrGlyph<'_> {
437    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
438        let glyph_id = self.glyph_id();
439        let Some(Ok(list)) = c.colr.base_glyph_list() else {
440            return;
441        };
442        let records = list.base_glyph_paint_records();
443        let record = match records.binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id)) {
444            Ok(ix) => &records[ix],
445            _ => return,
446        };
447        if let Ok(paint) = record.paint(list.offset_data()) {
448            c.add_glyph_id(glyph_id);
449            c.dispatch(&paint);
450        }
451    }
452}
453
454impl PaintTransform<'_> {
455    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
456        if let Ok(paint) = self.paint() {
457            c.dispatch(&paint);
458        }
459    }
460}
461
462impl VarAffine2x3<'_> {
463    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
464        c.add_variation_indices(self.var_index_base(), 6);
465    }
466}
467
468impl PaintVarTransform<'_> {
469    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
470        if let Ok(paint) = self.paint() {
471            if let Ok(affine2x3) = self.transform() {
472                c.dispatch(&paint);
473                affine2x3.v1_closure(c);
474            }
475        }
476    }
477}
478
479impl PaintTranslate<'_> {
480    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
481        if let Ok(paint) = self.paint() {
482            c.dispatch(&paint);
483        }
484    }
485}
486
487impl PaintVarTranslate<'_> {
488    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
489        if let Ok(paint) = self.paint() {
490            c.dispatch(&paint);
491            c.add_variation_indices(self.var_index_base(), 2);
492        }
493    }
494}
495
496impl PaintScale<'_> {
497    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
498        if let Ok(paint) = self.paint() {
499            c.dispatch(&paint);
500        }
501    }
502}
503
504impl PaintVarScale<'_> {
505    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
506        if let Ok(paint) = self.paint() {
507            c.dispatch(&paint);
508            c.add_variation_indices(self.var_index_base(), 2);
509        }
510    }
511}
512
513impl PaintScaleAroundCenter<'_> {
514    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
515        if let Ok(paint) = self.paint() {
516            c.dispatch(&paint);
517        }
518    }
519}
520
521impl PaintVarScaleAroundCenter<'_> {
522    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
523        if let Ok(paint) = self.paint() {
524            c.dispatch(&paint);
525            c.add_variation_indices(self.var_index_base(), 4);
526        }
527    }
528}
529
530impl PaintScaleUniform<'_> {
531    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
532        if let Ok(paint) = self.paint() {
533            c.dispatch(&paint);
534        }
535    }
536}
537
538impl PaintVarScaleUniform<'_> {
539    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
540        if let Ok(paint) = self.paint() {
541            c.dispatch(&paint);
542            c.add_variation_indices(self.var_index_base(), 1);
543        }
544    }
545}
546
547impl PaintScaleUniformAroundCenter<'_> {
548    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
549        if let Ok(paint) = self.paint() {
550            c.dispatch(&paint);
551        }
552    }
553}
554
555impl PaintVarScaleUniformAroundCenter<'_> {
556    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
557        if let Ok(paint) = self.paint() {
558            c.dispatch(&paint);
559            c.add_variation_indices(self.var_index_base(), 3);
560        }
561    }
562}
563
564impl PaintRotate<'_> {
565    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
566        if let Ok(paint) = self.paint() {
567            c.dispatch(&paint);
568        }
569    }
570}
571
572impl PaintVarRotate<'_> {
573    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
574        if let Ok(paint) = self.paint() {
575            c.dispatch(&paint);
576            c.add_variation_indices(self.var_index_base(), 1);
577        }
578    }
579}
580
581impl PaintRotateAroundCenter<'_> {
582    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
583        if let Ok(paint) = self.paint() {
584            c.dispatch(&paint);
585        }
586    }
587}
588
589impl PaintVarRotateAroundCenter<'_> {
590    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
591        if let Ok(paint) = self.paint() {
592            c.dispatch(&paint);
593            c.add_variation_indices(self.var_index_base(), 3);
594        }
595    }
596}
597
598impl PaintSkew<'_> {
599    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
600        if let Ok(paint) = self.paint() {
601            c.dispatch(&paint);
602        }
603    }
604}
605
606impl PaintVarSkew<'_> {
607    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
608        if let Ok(paint) = self.paint() {
609            c.dispatch(&paint);
610            c.add_variation_indices(self.var_index_base(), 2);
611        }
612    }
613}
614
615impl PaintSkewAroundCenter<'_> {
616    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
617        if let Ok(paint) = self.paint() {
618            c.dispatch(&paint);
619        }
620    }
621}
622
623impl PaintVarSkewAroundCenter<'_> {
624    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
625        if let Ok(paint) = self.paint() {
626            c.dispatch(&paint);
627            c.add_variation_indices(self.var_index_base(), 4);
628        }
629    }
630}
631
632impl PaintComposite<'_> {
633    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
634        if let Ok(source_paint) = self.source_paint() {
635            c.dispatch(&source_paint);
636        }
637
638        if let Ok(backdrop_paint) = self.backdrop_paint() {
639            c.dispatch(&backdrop_paint);
640        }
641    }
642}
643
644impl Clip {
645    fn v1_closure(&self, c: &mut Colrv1ClosureContext, clip_list: &ClipList) {
646        let Ok(clip_box) = self.clip_box(clip_list.offset_data()) else {
647            return;
648        };
649        let start_id = GlyphId::from(self.start_glyph_id());
650        let end_id = GlyphId::from(self.end_glyph_id());
651        if c.glyph_set.intersects_range(start_id..=end_id) {
652            clip_box.v1_closure(c);
653        }
654    }
655}
656
657impl ClipBox<'_> {
658    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
659        if let Self::Format2(item) = self {
660            item.v1_closure(c)
661        }
662    }
663}
664
665impl ClipBoxFormat2<'_> {
666    fn v1_closure(&self, c: &mut Colrv1ClosureContext) {
667        c.add_variation_indices(self.var_index_base(), 4);
668    }
669}
670
671/// Helper to compute the inclusive end of a range given a start and length,
672/// returning None if len == 0 or the computation would overflow.
673fn compute_inclusive_end(start: u32, len: u32) -> Option<u32> {
674    if len == 0 {
675        return None;
676    }
677    start.checked_add(len).and_then(|v| v.checked_sub(1))
678}
679
680#[cfg(test)]
681mod tests {
682    use super::*;
683    use crate::{FontData, FontRead, FontRef, GlyphId, TableProvider};
684
685    #[test]
686    fn paint_glyph2_closure_keeps_wide_outline_and_child_palette() {
687        let data = font_test_data::colr::paint_glyph2_colr();
688        let colr = Colr::read(FontData::new(&data)).unwrap();
689        let mut glyphs = IntSet::empty();
690        glyphs.insert(GlyphId::new(42));
691        let mut layers = IntSet::empty();
692        let mut palettes = IntSet::empty();
693        let mut variations = IntSet::empty();
694        colr.v1_closure(&mut glyphs, &mut layers, &mut palettes, &mut variations);
695        assert_eq!(
696            glyphs.iter().collect::<Vec<_>>(),
697            [GlyphId::new(42), GlyphId::new(0x123456)]
698        );
699        assert_eq!(palettes.iter().collect::<Vec<_>>(), [7]);
700        assert!(layers.is_empty());
701        assert!(variations.is_empty());
702    }
703
704    #[test]
705    fn test_colr_v0_closure() {
706        let font = FontRef::new(font_test_data::COLRV0V1_VARIABLE).unwrap();
707        let colr = font.colr().unwrap();
708
709        let mut input_glyph_set = IntSet::empty();
710        input_glyph_set.insert(GlyphId::new(168));
711
712        let mut glyph_set_colred = IntSet::empty();
713
714        colr.v0_closure_glyphs(&input_glyph_set, &mut glyph_set_colred);
715        assert_eq!(glyph_set_colred.len(), 9);
716        assert!(glyph_set_colred.contains(GlyphId::new(5)));
717        assert!(glyph_set_colred.contains(GlyphId::new(168)));
718        assert!(glyph_set_colred.contains(GlyphId::new(170)));
719        assert!(glyph_set_colred.contains(GlyphId::new(171)));
720        assert!(glyph_set_colred.contains(GlyphId::new(172)));
721        assert!(glyph_set_colred.contains(GlyphId::new(173)));
722        assert!(glyph_set_colred.contains(GlyphId::new(174)));
723        assert!(glyph_set_colred.contains(GlyphId::new(175)));
724        assert!(glyph_set_colred.contains(GlyphId::new(176)));
725
726        let mut palette_indices = IntSet::empty();
727        colr.v0_closure_palette_indices(&glyph_set_colred, &mut palette_indices);
728        assert_eq!(palette_indices.len(), 8);
729        assert!(palette_indices.contains(0));
730        assert!(palette_indices.contains(1));
731        assert!(palette_indices.contains(2));
732        assert!(palette_indices.contains(3));
733        assert!(palette_indices.contains(4));
734        assert!(palette_indices.contains(5));
735        assert!(palette_indices.contains(6));
736        assert!(palette_indices.contains(10));
737    }
738
739    #[test]
740    fn test_colr_v0_closure_not_found() {
741        let font = FontRef::new(font_test_data::COLRV0V1_VARIABLE).unwrap();
742        let colr = font.colr().unwrap();
743
744        let mut input_glyph_set = IntSet::empty();
745        input_glyph_set.insert(GlyphId::new(8));
746
747        let mut glyph_set_colred = IntSet::empty();
748
749        colr.v0_closure_glyphs(&input_glyph_set, &mut glyph_set_colred);
750        assert_eq!(glyph_set_colred.len(), 1);
751        assert!(glyph_set_colred.contains(GlyphId::new(8)));
752    }
753
754    #[test]
755    fn test_colr_v1_closure_no_var() {
756        let font = FontRef::new(font_test_data::COLRV0V1_VARIABLE).unwrap();
757        let colr = font.colr().unwrap();
758
759        let mut glyph_set = IntSet::empty();
760        glyph_set.insert(GlyphId::new(220));
761        glyph_set.insert(GlyphId::new(120));
762
763        let mut layer_indices = IntSet::empty();
764        let mut palette_indices = IntSet::empty();
765        let mut variation_indices = IntSet::empty();
766
767        colr.v1_closure(
768            &mut glyph_set,
769            &mut layer_indices,
770            &mut palette_indices,
771            &mut variation_indices,
772        );
773
774        assert_eq!(glyph_set.len(), 6);
775        assert!(glyph_set.contains(GlyphId::new(6)));
776        assert!(glyph_set.contains(GlyphId::new(7)));
777        assert!(glyph_set.contains(GlyphId::new(220)));
778        assert!(glyph_set.contains(GlyphId::new(3)));
779        assert!(glyph_set.contains(GlyphId::new(2)));
780        assert!(glyph_set.contains(GlyphId::new(120)));
781
782        assert_eq!(palette_indices.len(), 5);
783        assert!(palette_indices.contains(0));
784        assert!(palette_indices.contains(4));
785        assert!(palette_indices.contains(10));
786        assert!(palette_indices.contains(11));
787        assert!(palette_indices.contains(12));
788
789        assert_eq!(layer_indices.len(), 2);
790        assert!(layer_indices.contains(0));
791        assert!(layer_indices.contains(1));
792
793        assert!(variation_indices.is_empty());
794    }
795
796    #[test]
797    fn test_colr_v1_closure_w_var() {
798        let font = FontRef::new(font_test_data::COLRV0V1_VARIABLE).unwrap();
799        let colr = font.colr().unwrap();
800
801        let mut glyph_set = IntSet::empty();
802        glyph_set.insert(GlyphId::new(109));
803
804        let mut layer_indices = IntSet::empty();
805        let mut palette_indices = IntSet::empty();
806        let mut variation_indices = IntSet::empty();
807
808        colr.v1_closure(
809            &mut glyph_set,
810            &mut layer_indices,
811            &mut palette_indices,
812            &mut variation_indices,
813        );
814
815        assert_eq!(glyph_set.len(), 2);
816        assert!(glyph_set.contains(GlyphId::new(3)));
817        assert!(glyph_set.contains(GlyphId::new(109)));
818
819        assert_eq!(palette_indices.len(), 2);
820        assert!(palette_indices.contains(1));
821        assert!(palette_indices.contains(4));
822
823        assert!(layer_indices.is_empty());
824
825        assert_eq!(variation_indices.len(), 6);
826        assert!(variation_indices.contains(51));
827        assert!(variation_indices.contains(52));
828        assert!(variation_indices.contains(53));
829        assert!(variation_indices.contains(54));
830        assert!(variation_indices.contains(55));
831        assert!(variation_indices.contains(56));
832    }
833
834    #[test]
835    fn test_compute_inclusive_end() {
836        assert_eq!(compute_inclusive_end(0, 0), None);
837        assert_eq!(compute_inclusive_end(0, 1), Some(0));
838        assert_eq!(compute_inclusive_end(0, 2), Some(1));
839        assert_eq!(compute_inclusive_end(1, 1), Some(1));
840        assert_eq!(compute_inclusive_end(1, 2), Some(2));
841        assert_eq!(compute_inclusive_end(u32::MAX, 0), None);
842        assert_eq!(compute_inclusive_end(u32::MAX, 1), None);
843        assert_eq!(compute_inclusive_end(u32::MAX - 1, 1), Some(u32::MAX - 1));
844        assert_eq!(compute_inclusive_end(u32::MAX - 1, 3), None);
845    }
846}