Skip to main content

read_fonts/tables/
colr.rs

1//! The [COLR](https://docs.microsoft.com/en-us/typography/opentype/spec/colr) table
2
3#[cfg(feature = "std")]
4mod closure;
5
6use super::variations::{DeltaSetIndexMap, ItemVariationStore};
7
8include!("../../generated/generated_colr.rs");
9
10/// Unique paint identifier used for detecting cycles in the paint graph.
11pub type PaintId = usize;
12
13impl<'a> Colr<'a> {
14    /// Returns the COLRv0 base glyph for the given glyph identifier.
15    ///
16    /// The return value is a range of layer indices that can be passed to
17    /// [`v0_layer`](Self::v0_layer) to retrieve the layer glyph identifiers
18    /// and palette color indices.
19    pub fn v0_base_glyph(&self, glyph_id: GlyphId) -> Option<Range<usize>> {
20        let records = self.base_glyph_records()?.ok()?;
21        let glyph_id = glyph_id.try_into().ok()?;
22        let ix = records
23            .binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id))
24            .ok()?;
25        let record = records.get(ix)?;
26        let start = record.first_layer_index() as usize;
27        Some(start..start.checked_add(record.num_layers() as usize)?)
28    }
29
30    /// Returns the COLRv0 layer at the given index.
31    ///
32    /// The layer is represented by a tuple containing the glyph identifier of
33    /// the associated outline and the palette color index.
34    pub fn v0_layer(&self, index: usize) -> Option<(GlyphId16, u16)> {
35        let layer = self.layer_records()?.ok()?.get(index)?;
36        Some((layer.glyph_id(), layer.palette_index()))
37    }
38
39    /// Returns the COLRv1 base glyph for the given glyph identifier.
40    ///
41    /// The second value in the tuple is a unique identifier for the paint that
42    /// may be used to detect recursion in the paint graph.
43    pub fn v1_base_glyph(&self, glyph_id: GlyphId) -> Option<(Paint<'a>, PaintId)> {
44        let glyph_id = glyph_id.try_into().ok()?;
45        let list = self.base_glyph_list()?.ok()?;
46        let records = list.base_glyph_paint_records();
47        let ix = records
48            .binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id))
49            .ok()?;
50        let record = records.get(ix)?;
51        let offset_data = list.offset_data();
52        // Use the address of the paint as an identifier for the recursion
53        // blacklist.
54        let id = record.paint_offset().to_u32() as usize + offset_data.as_ref().as_ptr() as usize;
55        Some((record.paint(offset_data).ok()?, id))
56    }
57
58    /// Returns the COLRv1 layer at the given index.
59    ///
60    /// The second value in the tuple is a unique identifier for the paint that
61    /// may be used to detect recursion in the paint graph.
62    pub fn v1_layer(&self, index: usize) -> Option<(Paint<'a>, PaintId)> {
63        let list = self.layer_list()?.ok()?;
64        let offset = list.paint_offsets().get(index)?.get();
65        let offset_data = list.offset_data();
66        // Use the address of the paint as an identifier for the recursion
67        // blacklist.
68        let id = offset.to_u32() as usize + offset_data.as_ref().as_ptr() as usize;
69        Some((offset.resolve(offset_data).ok()?, id))
70    }
71
72    /// Returns the COLRv1 clip box for the given glyph identifier.
73    pub fn v1_clip_box(&self, glyph_id: GlyphId) -> Option<ClipBox<'a>> {
74        use core::cmp::Ordering;
75        let glyph_id: GlyphId16 = glyph_id.try_into().ok()?;
76        let list = self.clip_list()?.ok()?;
77        let clips = list.clips();
78        let ix = clips
79            .binary_search_by(|clip| {
80                if glyph_id < clip.start_glyph_id() {
81                    Ordering::Greater
82                } else if glyph_id > clip.end_glyph_id() {
83                    Ordering::Less
84                } else {
85                    Ordering::Equal
86                }
87            })
88            .ok()?;
89        let clip = clips.get(ix)?;
90        clip.clip_box(list.offset_data()).ok()
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97    use font_test_data::bebuffer::BeBuffer;
98
99    #[test]
100    fn paint_glyph2_reads_full_glyph_id() {
101        for gid in [0, 0xffff, 0x10000, 0x123456, 0xffffff] {
102            let data = BeBuffer::new()
103                .push(33u8)
104                .push(Offset24::new(Uint24::new(7)))
105                .push(GlyphId24::new(gid))
106                .extend([2u8, 0, 7, 0x40, 0]);
107            let Paint::Glyph2(paint) = Paint::read(FontData::new(&data)).unwrap() else {
108                panic!("expected PaintGlyph2");
109            };
110            assert_eq!(paint.glyph_id().to_u32(), gid);
111            assert_eq!(paint.paint_offset().to_u32(), 7);
112            let Paint::Solid(solid) = paint.paint().unwrap() else {
113                panic!("expected PaintSolid");
114            };
115            assert_eq!(solid.palette_index(), 7);
116            assert_eq!(solid.alpha(), F2Dot14::ONE);
117        }
118    }
119
120    #[test]
121    fn paint_glyph2_rejects_truncated_header_and_child() {
122        let data = font_test_data::colr::PAINT_GLYPH2;
123        for len in 0..7 {
124            assert!(Paint::read(FontData::new(&data[..len])).is_err());
125        }
126        for len in 7..data.len() {
127            let Paint::Glyph2(paint) = Paint::read(FontData::new(&data[..len])).unwrap() else {
128                panic!("expected PaintGlyph2");
129            };
130            assert!(paint.paint().is_err());
131        }
132    }
133}