write_fonts/generated/
generated_cpal.rs

1// THIS FILE IS AUTOGENERATED.
2// Any changes to this file will be overwritten.
3// For more information about how codegen works, see font-codegen/README.md
4
5#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8pub use read_fonts::tables::cpal::PaletteType;
9
10/// [CPAL (Color Palette Table)](https://learn.microsoft.com/en-us/typography/opentype/spec/cpal#palette-table-header) table
11#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub struct Cpal {
14    /// Number of palette entries in each palette.
15    pub num_palette_entries: u16,
16    /// Number of palettes in the table.
17    pub num_palettes: u16,
18    /// Total number of color records, combined for all palettes.
19    pub num_color_records: u16,
20    /// Offset from the beginning of CPAL table to the first
21    /// ColorRecord.
22    pub color_records_array: NullableOffsetMarker<Vec<ColorRecord>, WIDTH_32>,
23    /// Index of each palette’s first color record in the combined
24    /// color record array.
25    pub color_record_indices: Vec<u16>,
26    /// Offset from the beginning of CPAL table to the [Palette Types Array][].
27    ///
28    /// This is an array of 32-bit flag fields that describe properties of each palette.
29    ///
30    /// [Palette Types Array]: https://learn.microsoft.com/en-us/typography/opentype/spec/cpal#palette-type-array
31    pub palette_types_array: NullableOffsetMarker<Vec<PaletteType>, WIDTH_32>,
32    /// Offset from the beginning of CPAL table to the [Palette Labels Array][].
33    ///
34    /// This is an array of 'name' table IDs (typically in the font-specific name
35    /// ID range) that specify user interface strings associated with  each palette.
36    /// Use 0xFFFF if no name ID is provided for a palette.
37    ///
38    /// [Palette Labels Array]: https://learn.microsoft.com/en-us/typography/opentype/spec/cpal#palette-labels-array
39    pub palette_labels_array: NullableOffsetMarker<Vec<u16>, WIDTH_32>,
40    /// Offset from the beginning of CPAL table to the [Palette Entry Labels Array][].
41    ///
42    /// This is an array of 'name' table IDs (typically in the font-specific name
43    /// ID range) that specify user interface strings associated with  each palette
44    /// entry, e.g. “Outline”, “Fill”. This set of palette entry labels applies
45    /// to all palettes in the font. Use  0xFFFF if no name ID is provided for a
46    /// palette entry.
47    ///
48    /// [Palette Entry Labels Array]: https://learn.microsoft.com/en-us/typography/opentype/spec/cpal#palette-entry-label-array
49    pub palette_entry_labels_array: NullableOffsetMarker<Vec<NameId>, WIDTH_32>,
50}
51
52impl Cpal {
53    /// Construct a new `Cpal`
54    pub fn new(
55        num_palette_entries: u16,
56        num_palettes: u16,
57        num_color_records: u16,
58        color_records_array: Option<Vec<ColorRecord>>,
59        color_record_indices: Vec<u16>,
60    ) -> Self {
61        Self {
62            num_palette_entries,
63            num_palettes,
64            num_color_records,
65            color_records_array: color_records_array.into(),
66            color_record_indices,
67            ..Default::default()
68        }
69    }
70}
71
72impl FontWrite for Cpal {
73    #[allow(clippy::unnecessary_cast)]
74    fn write_into(&self, writer: &mut TableWriter) {
75        let version = 0 as u16;
76        version.write_into(writer);
77        self.num_palette_entries.write_into(writer);
78        self.num_palettes.write_into(writer);
79        self.num_color_records.write_into(writer);
80        self.color_records_array.write_into(writer);
81        self.color_record_indices.write_into(writer);
82        version
83            .compatible(1u16)
84            .then(|| self.palette_types_array.write_into(writer));
85        version
86            .compatible(1u16)
87            .then(|| self.palette_labels_array.write_into(writer));
88        version
89            .compatible(1u16)
90            .then(|| self.palette_entry_labels_array.write_into(writer));
91    }
92    fn table_type(&self) -> TableType {
93        TableType::TopLevel(Cpal::TAG)
94    }
95}
96
97impl Validate for Cpal {
98    fn validate_impl(&self, ctx: &mut ValidationCtx) {
99        ctx.in_table("Cpal", |ctx| {
100            ctx.in_field("color_records_array", |ctx| {
101                self.color_records_array.validate_impl(ctx);
102            });
103            ctx.in_field("color_record_indices", |ctx| {
104                if self.color_record_indices.len() > (u16::MAX as usize) {
105                    ctx.report("array exceeds max length");
106                }
107            });
108        })
109    }
110}
111
112impl TopLevelTable for Cpal {
113    const TAG: Tag = Tag::new(b"CPAL");
114}
115
116impl<'a> FromObjRef<read_fonts::tables::cpal::Cpal<'a>> for Cpal {
117    fn from_obj_ref(obj: &read_fonts::tables::cpal::Cpal<'a>, _: FontData) -> Self {
118        let offset_data = obj.offset_data();
119        Cpal {
120            num_palette_entries: obj.num_palette_entries(),
121            num_palettes: obj.num_palettes(),
122            num_color_records: obj.num_color_records(),
123            color_records_array: obj.color_records_array().to_owned_obj(offset_data),
124            color_record_indices: obj.color_record_indices().to_owned_obj(offset_data),
125            palette_types_array: obj.palette_types_array().to_owned_obj(offset_data),
126            palette_labels_array: obj.palette_labels_array().to_owned_obj(offset_data),
127            palette_entry_labels_array: obj.palette_entry_labels_array().to_owned_obj(offset_data),
128        }
129    }
130}
131
132#[allow(clippy::needless_lifetimes)]
133impl<'a> FromTableRef<read_fonts::tables::cpal::Cpal<'a>> for Cpal {}
134
135impl<'a> FontRead<'a> for Cpal {
136    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
137        <read_fonts::tables::cpal::Cpal as FontRead>::read(data).map(|x| x.to_owned_table())
138    }
139}
140
141impl FontWrite for PaletteType {
142    fn write_into(&self, writer: &mut TableWriter) {
143        writer.write_slice(&self.bits().to_be_bytes())
144    }
145}
146
147/// [CPAL (Color Record)](https://learn.microsoft.com/en-us/typography/opentype/spec/cpal#palette-entries-and-color-records) record
148#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
149#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
150pub struct ColorRecord {
151    /// Blue value (B0).
152    pub blue: u8,
153    /// Green value (B1).
154    pub green: u8,
155    ///     Red value (B2).
156    pub red: u8,
157    /// Alpha value (B3).
158    pub alpha: u8,
159}
160
161impl ColorRecord {
162    /// Construct a new `ColorRecord`
163    pub fn new(blue: u8, green: u8, red: u8, alpha: u8) -> Self {
164        Self {
165            blue,
166            green,
167            red,
168            alpha,
169        }
170    }
171}
172
173impl FontWrite for ColorRecord {
174    fn write_into(&self, writer: &mut TableWriter) {
175        self.blue.write_into(writer);
176        self.green.write_into(writer);
177        self.red.write_into(writer);
178        self.alpha.write_into(writer);
179    }
180    fn table_type(&self) -> TableType {
181        TableType::Named("ColorRecord")
182    }
183}
184
185impl Validate for ColorRecord {
186    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
187}
188
189impl FromObjRef<read_fonts::tables::cpal::ColorRecord> for ColorRecord {
190    fn from_obj_ref(obj: &read_fonts::tables::cpal::ColorRecord, _: FontData) -> Self {
191        ColorRecord {
192            blue: obj.blue(),
193            green: obj.green(),
194            red: obj.red(),
195            alpha: obj.alpha(),
196        }
197    }
198}