write_fonts/generated/
generated_cpal.rs1#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8pub use read_fonts::tables::cpal::PaletteType;
9
10#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub struct Cpal {
14 pub num_palette_entries: u16,
16 pub num_palettes: u16,
18 pub num_color_records: u16,
20 pub color_records_array: NullableOffsetMarker<Vec<ColorRecord>, WIDTH_32>,
23 pub color_record_indices: Vec<u16>,
26 pub palette_types_array: NullableOffsetMarker<Vec<PaletteType>, WIDTH_32>,
32 pub palette_labels_array: NullableOffsetMarker<Vec<u16>, WIDTH_32>,
40 pub palette_entry_labels_array: NullableOffsetMarker<Vec<NameId>, WIDTH_32>,
50}
51
52impl Cpal {
53 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#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
149#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
150pub struct ColorRecord {
151 pub blue: u8,
153 pub green: u8,
155 pub red: u8,
157 pub alpha: u8,
159}
160
161impl ColorRecord {
162 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}