/src/fontations/write-fonts/generated/generated_cpal.rs
Line | Count | Source |
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)] |
6 | | use crate::codegen_prelude::*; |
7 | | |
8 | | pub 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))] |
13 | | pub 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<NameId>, 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 | | |
52 | | impl Cpal { |
53 | | /// Construct a new `Cpal` |
54 | 0 | pub fn new( |
55 | 0 | num_palette_entries: u16, |
56 | 0 | num_palettes: u16, |
57 | 0 | num_color_records: u16, |
58 | 0 | color_records_array: Option<Vec<ColorRecord>>, |
59 | 0 | color_record_indices: Vec<u16>, |
60 | 0 | ) -> Self { |
61 | 0 | Self { |
62 | 0 | num_palette_entries, |
63 | 0 | num_palettes, |
64 | 0 | num_color_records, |
65 | 0 | color_records_array: color_records_array.into(), |
66 | 0 | color_record_indices, |
67 | 0 | ..Default::default() |
68 | 0 | } |
69 | 0 | } |
70 | | } |
71 | | |
72 | | impl FontWrite for Cpal { |
73 | | #[allow(clippy::unnecessary_cast)] |
74 | 0 | fn write_into(&self, writer: &mut TableWriter) { |
75 | 0 | let version = 0 as u16; |
76 | 0 | version.write_into(writer); |
77 | 0 | self.num_palette_entries.write_into(writer); |
78 | 0 | self.num_palettes.write_into(writer); |
79 | 0 | self.num_color_records.write_into(writer); |
80 | 0 | self.color_records_array.write_into(writer); |
81 | 0 | self.color_record_indices.write_into(writer); |
82 | 0 | version |
83 | 0 | .compatible(1u16) |
84 | 0 | .then(|| self.palette_types_array.write_into(writer)); |
85 | 0 | version |
86 | 0 | .compatible(1u16) |
87 | 0 | .then(|| self.palette_labels_array.write_into(writer)); |
88 | 0 | version |
89 | 0 | .compatible(1u16) |
90 | 0 | .then(|| self.palette_entry_labels_array.write_into(writer)); |
91 | 0 | } |
92 | 0 | fn table_type(&self) -> TableType { |
93 | 0 | TableType::TopLevel(Cpal::TAG) |
94 | 0 | } |
95 | | } |
96 | | |
97 | | impl Validate for Cpal { |
98 | 0 | fn validate_impl(&self, ctx: &mut ValidationCtx) { |
99 | 0 | ctx.in_table("Cpal", |ctx| { |
100 | 0 | ctx.in_field("color_records_array", |ctx| { |
101 | 0 | self.color_records_array.validate_impl(ctx); |
102 | 0 | }); |
103 | 0 | ctx.in_field("color_record_indices", |ctx| { |
104 | 0 | if self.color_record_indices.len() > to_usize(u16::MAX) { |
105 | 0 | ctx.report("array exceeds max length"); |
106 | 0 | } |
107 | 0 | }); |
108 | 0 | }) |
109 | 0 | } |
110 | | } |
111 | | |
112 | | impl TopLevelTable for Cpal { |
113 | | const TAG: Tag = Tag::new(b"CPAL"); |
114 | | } |
115 | | |
116 | | impl<'a> FromObjRef<read_fonts::tables::cpal::Cpal<'a>> for Cpal { |
117 | 0 | fn from_obj_ref(obj: &read_fonts::tables::cpal::Cpal<'a>, _: FontData) -> Self { |
118 | 0 | let offset_data = obj.offset_data(); |
119 | 0 | Cpal { |
120 | 0 | num_palette_entries: obj.num_palette_entries(), |
121 | 0 | num_palettes: obj.num_palettes(), |
122 | 0 | num_color_records: obj.num_color_records(), |
123 | 0 | color_records_array: obj.color_records_array().to_owned_obj(offset_data), |
124 | 0 | color_record_indices: obj.color_record_indices().to_owned_obj(offset_data), |
125 | 0 | palette_types_array: obj.palette_types_array().to_owned_obj(offset_data), |
126 | 0 | palette_labels_array: obj.palette_labels_array().to_owned_obj(offset_data), |
127 | 0 | palette_entry_labels_array: obj.palette_entry_labels_array().to_owned_obj(offset_data), |
128 | 0 | } |
129 | 0 | } |
130 | | } |
131 | | |
132 | | #[allow(clippy::needless_lifetimes)] |
133 | | impl<'a> FromTableRef<read_fonts::tables::cpal::Cpal<'a>> for Cpal {} |
134 | | |
135 | | impl ReadArgs for Cpal { |
136 | | type Args = (); |
137 | | } |
138 | | |
139 | | impl<'a> FontRead<'a> for Cpal { |
140 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
141 | 0 | <read_fonts::tables::cpal::Cpal as FontRead>::read(data).map(|x| x.to_owned_table()) |
142 | 0 | } |
143 | | } |
144 | | |
145 | | impl FontWrite for PaletteType { |
146 | 0 | fn write_into(&self, writer: &mut TableWriter) { |
147 | 0 | writer.write_slice(&self.bits().to_be_bytes()) |
148 | 0 | } |
149 | | } |
150 | | |
151 | | /// [CPAL (Color Record)](https://learn.microsoft.com/en-us/typography/opentype/spec/cpal#palette-entries-and-color-records) record |
152 | | /// |
153 | | /// Contains a color in non-premultiplied BGRA form, in the sRGB color space. |
154 | | #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
155 | | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
156 | | pub struct ColorRecord { |
157 | | /// Blue value (B0). |
158 | | pub blue: u8, |
159 | | /// Green value (B1). |
160 | | pub green: u8, |
161 | | /// Red value (B2). |
162 | | pub red: u8, |
163 | | /// Alpha value (B3). |
164 | | pub alpha: u8, |
165 | | } |
166 | | |
167 | | impl ColorRecord { |
168 | | /// Construct a new `ColorRecord` |
169 | 0 | pub fn new(blue: u8, green: u8, red: u8, alpha: u8) -> Self { |
170 | 0 | Self { |
171 | 0 | blue, |
172 | 0 | green, |
173 | 0 | red, |
174 | 0 | alpha, |
175 | 0 | } |
176 | 0 | } |
177 | | } |
178 | | |
179 | | impl FontWrite for ColorRecord { |
180 | 0 | fn write_into(&self, writer: &mut TableWriter) { |
181 | 0 | self.blue.write_into(writer); |
182 | 0 | self.green.write_into(writer); |
183 | 0 | self.red.write_into(writer); |
184 | 0 | self.alpha.write_into(writer); |
185 | 0 | } |
186 | 0 | fn table_type(&self) -> TableType { |
187 | 0 | TableType::Named("ColorRecord") |
188 | 0 | } |
189 | | } |
190 | | |
191 | | impl Validate for ColorRecord { |
192 | 0 | fn validate_impl(&self, _ctx: &mut ValidationCtx) {} |
193 | | } |
194 | | |
195 | | impl FromObjRef<read_fonts::tables::cpal::ColorRecord> for ColorRecord { |
196 | 0 | fn from_obj_ref(obj: &read_fonts::tables::cpal::ColorRecord, _: FontData) -> Self { |
197 | 0 | ColorRecord { |
198 | 0 | blue: obj.blue(), |
199 | 0 | green: obj.green(), |
200 | 0 | red: obj.red(), |
201 | 0 | alpha: obj.alpha(), |
202 | 0 | } |
203 | 0 | } |
204 | | } |