read_fonts/tables/
colr.rs1#[cfg(feature = "std")]
4mod closure;
5
6use super::variations::{DeltaSetIndexMap, ItemVariationStore};
7
8include!("../../generated/generated_colr.rs");
9
10pub type PaintId = usize;
12
13impl<'a> Colr<'a> {
14 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 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 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 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 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 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 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}