1//! The [COLR](https://docs.microsoft.com/en-us/typography/opentype/spec/colr) table
23#[cfg(feature = "std")]
4mod closure;
56use super::variations::{DeltaSetIndexMap, ItemVariationStore};
78include!("../../generated/generated_colr.rs");
910/// Unique paint identifier used for detecting cycles in the paint graph.
11pub type PaintId = usize;
1213impl<'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.
19pub fn v0_base_glyph(&self, glyph_id: GlyphId) -> Result<Option<Range<usize>>, ReadError> {
20let records = self.base_glyph_records().ok_or(ReadError::NullOffset)??;
21let Ok(glyph_id) = glyph_id.try_into() else {
22return Ok(None);
23 };
24let record = match records.binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id)) {
25Ok(ix) => &records[ix],
26_ => return Ok(None),
27 };
28let start = record.first_layer_index() as usize;
29let end = start + record.num_layers() as usize;
30Ok(Some(start..end))
31 }
3233/// Returns the COLRv0 layer at the given index.
34 ///
35 /// The layer is represented by a tuple containing the glyph identifier of
36 /// the associated outline and the palette color index.
37pub fn v0_layer(&self, index: usize) -> Result<(GlyphId16, u16), ReadError> {
38let layers = self.layer_records().ok_or(ReadError::NullOffset)??;
39let layer = layers.get(index).ok_or(ReadError::OutOfBounds)?;
40Ok((layer.glyph_id(), layer.palette_index()))
41 }
4243/// Returns the COLRv1 base glyph for the given glyph identifier.
44 ///
45 /// The second value in the tuple is a unique identifier for the paint that
46 /// may be used to detect recursion in the paint graph.
47pub fn v1_base_glyph(
48&self,
49 glyph_id: GlyphId,
50 ) -> Result<Option<(Paint<'a>, PaintId)>, ReadError> {
51let Ok(glyph_id) = glyph_id.try_into() else {
52return Ok(None);
53 };
54let list = self.base_glyph_list().ok_or(ReadError::NullOffset)??;
55let records = list.base_glyph_paint_records();
56let record = match records.binary_search_by(|rec| rec.glyph_id().cmp(&glyph_id)) {
57Ok(ix) => &records[ix],
58_ => return Ok(None),
59 };
60let offset_data = list.offset_data();
61// Use the address of the paint as an identifier for the recursion
62 // blacklist.
63let id = record.paint_offset().to_u32() as usize + offset_data.as_ref().as_ptr() as usize;
64Ok(Some((record.paint(offset_data)?, id)))
65 }
6667/// Returns the COLRv1 layer at the given index.
68 ///
69 /// The second value in the tuple is a unique identifier for the paint that
70 /// may be used to detect recursion in the paint graph.
71pub fn v1_layer(&self, index: usize) -> Result<(Paint<'a>, PaintId), ReadError> {
72let list = self.layer_list().ok_or(ReadError::NullOffset)??;
73let offset = list
74 .paint_offsets()
75 .get(index)
76 .ok_or(ReadError::OutOfBounds)?
77.get();
78let offset_data = list.offset_data();
79// Use the address of the paint as an identifier for the recursion
80 // blacklist.
81let id = offset.to_u32() as usize + offset_data.as_ref().as_ptr() as usize;
82Ok((offset.resolve(offset_data)?, id))
83 }
8485/// Returns the COLRv1 clip box for the given glyph identifier.
86pub fn v1_clip_box(&self, glyph_id: GlyphId) -> Result<Option<ClipBox<'a>>, ReadError> {
87use core::cmp::Ordering;
88let Ok(glyph_id): Result<GlyphId16, _> = glyph_id.try_into() else {
89return Ok(None);
90 };
91let list = self.clip_list().ok_or(ReadError::NullOffset)??;
92let clips = list.clips();
93let clip = match clips.binary_search_by(|clip| {
94if glyph_id < clip.start_glyph_id() {
95 Ordering::Greater
96 } else if glyph_id > clip.end_glyph_id() {
97 Ordering::Less
98 } else {
99 Ordering::Equal
100 }
101 }) {
102Ok(ix) => &clips[ix],
103_ => return Ok(None),
104 };
105Ok(Some(clip.clip_box(list.offset_data())?))
106 }
107}