1#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8impl<'a> MinByteRange<'a> for Cpal<'a> {
9 fn min_byte_range(&self) -> Range<usize> {
10 0..self.color_record_indices_byte_range().end
11 }
12 fn min_table_bytes(&self) -> &'a [u8] {
13 let range = self.min_byte_range();
14 self.offset_data().as_bytes().get(range).unwrap_or_default()
15 }
16}
17
18impl TopLevelTable for Cpal<'_> {
19 const TAG: Tag = Tag::new(b"CPAL");
21}
22
23impl ReadArgs for Cpal<'_> {
24 type Args = ();
25}
26
27impl<'a> FontRead<'a> for Cpal<'a> {
28 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29 #[allow(clippy::absurd_extreme_comparisons)]
30 if data.len() < Self::MIN_SIZE {
31 return Err(ReadError::OutOfBounds);
32 }
33 let Some(data) = TableDataRef::new(data.as_bytes()) else {
34 return Err(ReadError::OutOfBounds);
35 };
36 Ok(Self { data })
37 }
38}
39
40#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
42#[repr(C, packed)]
43#[doc(hidden)]
44pub struct CpalFixedHeader {
45 pub version: BigEndian<u16>,
46 pub num_palette_entries: BigEndian<u16>,
47 pub num_palettes: BigEndian<u16>,
48 pub num_color_records: BigEndian<u16>,
49 pub color_records_array_offset: BigEndian<Nullable<Offset32>>,
50}
51
52#[allow(clippy::absurd_extreme_comparisons)]
53const _: () = assert!(std::mem::size_of::<CpalFixedHeader>() <= Cpal::MIN_SIZE);
54
55#[derive(Clone)]
57pub struct Cpal<'a> {
58 data: TableDataRef<'a, CpalFixedHeader>,
59}
60
61#[allow(clippy::needless_lifetimes)]
62impl<'a> Cpal<'a> {
63 pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
64 + u16::RAW_BYTE_LEN
65 + u16::RAW_BYTE_LEN
66 + u16::RAW_BYTE_LEN
67 + Offset32::RAW_BYTE_LEN);
68 basic_table_impls!(impl_the_methods);
69
70 pub fn version(&self) -> u16 {
72 self.data.header().version.get()
73 }
74
75 pub fn num_palette_entries(&self) -> u16 {
77 self.data.header().num_palette_entries.get()
78 }
79
80 pub fn num_palettes(&self) -> u16 {
82 self.data.header().num_palettes.get()
83 }
84
85 pub fn num_color_records(&self) -> u16 {
87 self.data.header().num_color_records.get()
88 }
89
90 pub fn color_records_array_offset(&self) -> Nullable<Offset32> {
93 self.data.header().color_records_array_offset.get()
94 }
95
96 pub fn color_records_array(&self) -> Option<Result<&'a [ColorRecord], ReadError>> {
98 let data = self.offset_data();
99 let args = self.num_color_records();
100 self.color_records_array_offset()
101 .resolve_with_args(data, args)
102 }
103
104 pub fn color_record_indices(&self) -> &'a [BigEndian<u16>] {
107 let range = self.color_record_indices_byte_range();
108 self.offset_data()
109 .read_array(range)
110 .ok()
111 .unwrap_or_default()
112 }
113
114 pub fn palette_types_array_offset(&self) -> Option<Nullable<Offset32>> {
120 let range = self.palette_types_array_offset_byte_range();
121 (!range.is_empty())
122 .then(|| self.offset_data().read_at(range.start).ok())
123 .flatten()
124 }
125
126 pub fn palette_types_array(&self) -> Option<Result<&'a [BigEndian<PaletteType>], ReadError>> {
128 let data = self.offset_data();
129 let args = self.num_palettes();
130 self.palette_types_array_offset()
131 .map(|x| x.resolve_with_args(data, args))?
132 }
133
134 pub fn palette_labels_array_offset(&self) -> Option<Nullable<Offset32>> {
142 let range = self.palette_labels_array_offset_byte_range();
143 (!range.is_empty())
144 .then(|| self.offset_data().read_at(range.start).ok())
145 .flatten()
146 }
147
148 pub fn palette_labels_array(&self) -> Option<Result<&'a [BigEndian<NameId>], ReadError>> {
150 let data = self.offset_data();
151 let args = self.num_palettes();
152 self.palette_labels_array_offset()
153 .map(|x| x.resolve_with_args(data, args))?
154 }
155
156 pub fn palette_entry_labels_array_offset(&self) -> Option<Nullable<Offset32>> {
166 let range = self.palette_entry_labels_array_offset_byte_range();
167 (!range.is_empty())
168 .then(|| self.offset_data().read_at(range.start).ok())
169 .flatten()
170 }
171
172 pub fn palette_entry_labels_array(&self) -> Option<Result<&'a [BigEndian<NameId>], ReadError>> {
174 let data = self.offset_data();
175 let args = self.num_palette_entries();
176 self.palette_entry_labels_array_offset()
177 .map(|x| x.resolve_with_args(data, args))?
178 }
179
180 pub fn version_byte_range(&self) -> Range<usize> {
181 let start = 0usize;
182 let end = start.saturating_add(u16::RAW_BYTE_LEN);
183 start..end
184 }
185
186 pub fn num_palette_entries_byte_range(&self) -> Range<usize> {
187 let start = self.version_byte_range().end;
188 let end = start.saturating_add(u16::RAW_BYTE_LEN);
189 start..end
190 }
191
192 pub fn num_palettes_byte_range(&self) -> Range<usize> {
193 let start = self.num_palette_entries_byte_range().end;
194 let end = start.saturating_add(u16::RAW_BYTE_LEN);
195 start..end
196 }
197
198 pub fn num_color_records_byte_range(&self) -> Range<usize> {
199 let start = self.num_palettes_byte_range().end;
200 let end = start.saturating_add(u16::RAW_BYTE_LEN);
201 start..end
202 }
203
204 pub fn color_records_array_offset_byte_range(&self) -> Range<usize> {
205 let start = self.num_color_records_byte_range().end;
206 let end = start.saturating_add(Offset32::RAW_BYTE_LEN);
207 start..end
208 }
209
210 pub fn color_record_indices_byte_range(&self) -> Range<usize> {
211 let num_palettes = self.num_palettes();
212 let start = self.color_records_array_offset_byte_range().end;
213 let end = start
214 .saturating_add((transforms::to_usize(num_palettes)).saturating_mul(u16::RAW_BYTE_LEN));
215 start..end
216 }
217
218 pub fn palette_types_array_offset_byte_range(&self) -> Range<usize> {
219 let start = self.color_record_indices_byte_range().end;
220 let end = if self.version().compatible(1u16) {
221 start.saturating_add(Offset32::RAW_BYTE_LEN)
222 } else {
223 start
224 };
225 start..end
226 }
227
228 pub fn palette_labels_array_offset_byte_range(&self) -> Range<usize> {
229 let start = self.palette_types_array_offset_byte_range().end;
230 let end = if self.version().compatible(1u16) {
231 start.saturating_add(Offset32::RAW_BYTE_LEN)
232 } else {
233 start
234 };
235 start..end
236 }
237
238 pub fn palette_entry_labels_array_offset_byte_range(&self) -> Range<usize> {
239 let start = self.palette_labels_array_offset_byte_range().end;
240 let end = if self.version().compatible(1u16) {
241 start.saturating_add(Offset32::RAW_BYTE_LEN)
242 } else {
243 start
244 };
245 start..end
246 }
247}
248
249const _: () = assert!(FontData::default_data_long_enough(Cpal::MIN_SIZE));
250
251impl Default for Cpal<'_> {
252 fn default() -> Self {
253 Self {
254 data: TableDataRef::new(FontData::default_table_data().as_bytes())
255 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
256 }
257 }
258}
259
260#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
269#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
270#[repr(transparent)]
271pub struct PaletteType {
272 bits: u32,
273}
274
275impl PaletteType {
276 pub const USABLE_WITH_LIGHT_BACKGROUND: Self = Self { bits: 0x0001 };
278
279 pub const USABLE_WITH_DARK_BACKGROUND: Self = Self { bits: 0x0002 };
281}
282
283impl PaletteType {
284 #[inline]
286 pub const fn empty() -> Self {
287 Self { bits: 0 }
288 }
289
290 #[inline]
292 pub const fn all() -> Self {
293 Self {
294 bits: Self::USABLE_WITH_LIGHT_BACKGROUND.bits | Self::USABLE_WITH_DARK_BACKGROUND.bits,
295 }
296 }
297
298 #[inline]
300 pub const fn bits(&self) -> u32 {
301 self.bits
302 }
303
304 #[inline]
307 pub const fn from_bits(bits: u32) -> Option<Self> {
308 if (bits & !Self::all().bits()) == 0 {
309 Some(Self { bits })
310 } else {
311 None
312 }
313 }
314
315 #[inline]
318 pub const fn from_bits_truncate(bits: u32) -> Self {
319 Self {
320 bits: bits & Self::all().bits,
321 }
322 }
323
324 #[inline]
326 pub const fn is_empty(&self) -> bool {
327 self.bits() == Self::empty().bits()
328 }
329
330 #[inline]
332 pub const fn intersects(&self, other: Self) -> bool {
333 !(Self {
334 bits: self.bits & other.bits,
335 })
336 .is_empty()
337 }
338
339 #[inline]
341 pub const fn contains(&self, other: Self) -> bool {
342 (self.bits & other.bits) == other.bits
343 }
344
345 #[inline]
347 pub fn insert(&mut self, other: Self) {
348 self.bits |= other.bits;
349 }
350
351 #[inline]
353 pub fn remove(&mut self, other: Self) {
354 self.bits &= !other.bits;
355 }
356
357 #[inline]
359 pub fn toggle(&mut self, other: Self) {
360 self.bits ^= other.bits;
361 }
362
363 #[inline]
374 #[must_use]
375 pub const fn intersection(self, other: Self) -> Self {
376 Self {
377 bits: self.bits & other.bits,
378 }
379 }
380
381 #[inline]
392 #[must_use]
393 pub const fn union(self, other: Self) -> Self {
394 Self {
395 bits: self.bits | other.bits,
396 }
397 }
398
399 #[inline]
412 #[must_use]
413 pub const fn difference(self, other: Self) -> Self {
414 Self {
415 bits: self.bits & !other.bits,
416 }
417 }
418}
419
420impl std::ops::BitOr for PaletteType {
421 type Output = Self;
422
423 #[inline]
425 fn bitor(self, other: PaletteType) -> Self {
426 Self {
427 bits: self.bits | other.bits,
428 }
429 }
430}
431
432impl std::ops::BitOrAssign for PaletteType {
433 #[inline]
435 fn bitor_assign(&mut self, other: Self) {
436 self.bits |= other.bits;
437 }
438}
439
440impl std::ops::BitXor for PaletteType {
441 type Output = Self;
442
443 #[inline]
445 fn bitxor(self, other: Self) -> Self {
446 Self {
447 bits: self.bits ^ other.bits,
448 }
449 }
450}
451
452impl std::ops::BitXorAssign for PaletteType {
453 #[inline]
455 fn bitxor_assign(&mut self, other: Self) {
456 self.bits ^= other.bits;
457 }
458}
459
460impl std::ops::BitAnd for PaletteType {
461 type Output = Self;
462
463 #[inline]
465 fn bitand(self, other: Self) -> Self {
466 Self {
467 bits: self.bits & other.bits,
468 }
469 }
470}
471
472impl std::ops::BitAndAssign for PaletteType {
473 #[inline]
475 fn bitand_assign(&mut self, other: Self) {
476 self.bits &= other.bits;
477 }
478}
479
480impl std::ops::Sub for PaletteType {
481 type Output = Self;
482
483 #[inline]
485 fn sub(self, other: Self) -> Self {
486 Self {
487 bits: self.bits & !other.bits,
488 }
489 }
490}
491
492impl std::ops::SubAssign for PaletteType {
493 #[inline]
495 fn sub_assign(&mut self, other: Self) {
496 self.bits &= !other.bits;
497 }
498}
499
500impl std::ops::Not for PaletteType {
501 type Output = Self;
502
503 #[inline]
505 fn not(self) -> Self {
506 Self { bits: !self.bits } & Self::all()
507 }
508}
509
510impl std::fmt::Debug for PaletteType {
511 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
512 let members: &[(&str, Self)] = &[
513 (
514 "USABLE_WITH_LIGHT_BACKGROUND",
515 Self::USABLE_WITH_LIGHT_BACKGROUND,
516 ),
517 (
518 "USABLE_WITH_DARK_BACKGROUND",
519 Self::USABLE_WITH_DARK_BACKGROUND,
520 ),
521 ];
522 let mut first = true;
523 for (name, value) in members {
524 if self.contains(*value) {
525 if !first {
526 f.write_str(" | ")?;
527 }
528 first = false;
529 f.write_str(name)?;
530 }
531 }
532 if first {
533 f.write_str("(empty)")?;
534 }
535 Ok(())
536 }
537}
538
539impl std::fmt::Binary for PaletteType {
540 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
541 std::fmt::Binary::fmt(&self.bits, f)
542 }
543}
544
545impl std::fmt::Octal for PaletteType {
546 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
547 std::fmt::Octal::fmt(&self.bits, f)
548 }
549}
550
551impl std::fmt::LowerHex for PaletteType {
552 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
553 std::fmt::LowerHex::fmt(&self.bits, f)
554 }
555}
556
557impl std::fmt::UpperHex for PaletteType {
558 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
559 std::fmt::UpperHex::fmt(&self.bits, f)
560 }
561}
562
563impl font_types::Scalar for PaletteType {
564 type Raw = <u32 as font_types::Scalar>::Raw;
565 fn to_raw(self) -> Self::Raw {
566 self.bits().to_raw()
567 }
568 fn from_raw(raw: Self::Raw) -> Self {
569 let t = <u32>::from_raw(raw);
570 Self::from_bits_truncate(t)
571 }
572}
573
574#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
582#[repr(C)]
583#[repr(packed)]
584pub struct ColorRecord {
585 pub blue: u8,
587 pub green: u8,
589 pub red: u8,
591 pub alpha: u8,
596}
597
598impl ColorRecord {
599 pub fn blue(&self) -> u8 {
601 self.blue
602 }
603
604 pub fn green(&self) -> u8 {
606 self.green
607 }
608
609 pub fn red(&self) -> u8 {
611 self.red
612 }
613
614 pub fn alpha(&self) -> u8 {
619 self.alpha
620 }
621}
622
623impl FixedSize for ColorRecord {
624 const RAW_BYTE_LEN: usize =
625 u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN;
626}