#[allow(unused_imports)]
use crate::codegen_prelude::*;
pub use read_fonts::tables::cpal::PaletteType;
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Cpal {
pub num_palette_entries: u16,
pub num_palettes: u16,
pub num_color_records: u16,
pub color_records_array: NullableOffsetMarker<Vec<ColorRecord>, WIDTH_32>,
pub color_record_indices: Vec<u16>,
pub palette_types_array: NullableOffsetMarker<Vec<PaletteType>, WIDTH_32>,
pub palette_labels_array: NullableOffsetMarker<Vec<NameId>, WIDTH_32>,
pub palette_entry_labels_array: NullableOffsetMarker<Vec<NameId>, WIDTH_32>,
}
impl Cpal {
pub fn new(
num_palette_entries: u16,
num_palettes: u16,
num_color_records: u16,
color_records_array: Option<Vec<ColorRecord>>,
color_record_indices: Vec<u16>,
) -> Self {
Self {
num_palette_entries,
num_palettes,
num_color_records,
color_records_array: color_records_array.into(),
color_record_indices,
..Default::default()
}
}
}
impl FontWrite for Cpal {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
let version = 0 as u16;
version.write_into(writer);
self.num_palette_entries.write_into(writer);
self.num_palettes.write_into(writer);
self.num_color_records.write_into(writer);
self.color_records_array.write_into(writer);
self.color_record_indices.write_into(writer);
version
.compatible(1u16)
.then(|| self.palette_types_array.write_into(writer));
version
.compatible(1u16)
.then(|| self.palette_labels_array.write_into(writer));
version
.compatible(1u16)
.then(|| self.palette_entry_labels_array.write_into(writer));
}
fn table_type(&self) -> TableType {
TableType::TopLevel(Cpal::TAG)
}
}
impl Validate for Cpal {
fn validate_impl(&self, ctx: &mut ValidationCtx) {
ctx.in_table("Cpal", |ctx| {
ctx.in_field("color_records_array", |ctx| {
self.color_records_array.validate_impl(ctx);
});
ctx.in_field("color_record_indices", |ctx| {
if self.color_record_indices.len() > (u16::MAX as usize) {
ctx.report("array exceeds max length");
}
});
})
}
}
impl TopLevelTable for Cpal {
const TAG: Tag = Tag::new(b"CPAL");
}
impl<'a> FromObjRef<read_fonts::tables::cpal::Cpal<'a>> for Cpal {
fn from_obj_ref(obj: &read_fonts::tables::cpal::Cpal<'a>, _: FontData) -> Self {
let offset_data = obj.offset_data();
Cpal {
num_palette_entries: obj.num_palette_entries(),
num_palettes: obj.num_palettes(),
num_color_records: obj.num_color_records(),
color_records_array: obj.color_records_array().to_owned_obj(offset_data),
color_record_indices: obj.color_record_indices().to_owned_obj(offset_data),
palette_types_array: obj.palette_types_array().to_owned_obj(offset_data),
palette_labels_array: obj.palette_labels_array().to_owned_obj(offset_data),
palette_entry_labels_array: obj.palette_entry_labels_array().to_owned_obj(offset_data),
}
}
}
#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::cpal::Cpal<'a>> for Cpal {}
impl<'a> FontRead<'a> for Cpal {
fn read(data: FontData<'a>) -> Result<Self, ReadError> {
<read_fonts::tables::cpal::Cpal as FontRead>::read(data).map(|x| x.to_owned_table())
}
}
impl FontWrite for PaletteType {
fn write_into(&self, writer: &mut TableWriter) {
writer.write_slice(&self.bits().to_be_bytes())
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ColorRecord {
pub blue: u8,
pub green: u8,
pub red: u8,
pub alpha: u8,
}
impl ColorRecord {
pub fn new(blue: u8, green: u8, red: u8, alpha: u8) -> Self {
Self {
blue,
green,
red,
alpha,
}
}
}
impl FontWrite for ColorRecord {
fn write_into(&self, writer: &mut TableWriter) {
self.blue.write_into(writer);
self.green.write_into(writer);
self.red.write_into(writer);
self.alpha.write_into(writer);
}
fn table_type(&self) -> TableType {
TableType::Named("ColorRecord")
}
}
impl Validate for ColorRecord {
fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
}
impl FromObjRef<read_fonts::tables::cpal::ColorRecord> for ColorRecord {
fn from_obj_ref(obj: &read_fonts::tables::cpal::ColorRecord, _: FontData) -> Self {
ColorRecord {
blue: obj.blue(),
green: obj.green(),
red: obj.red(),
alpha: obj.alpha(),
}
}
}