#[allow(unused_imports)]
use crate::codegen_prelude::*;
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct CpalMarker {
color_record_indices_byte_len: usize,
palette_types_array_offset_byte_start: Option<usize>,
palette_labels_array_offset_byte_start: Option<usize>,
palette_entry_labels_array_offset_byte_start: Option<usize>,
}
impl CpalMarker {
pub fn version_byte_range(&self) -> Range<usize> {
let start = 0;
start..start + u16::RAW_BYTE_LEN
}
pub fn num_palette_entries_byte_range(&self) -> Range<usize> {
let start = self.version_byte_range().end;
start..start + u16::RAW_BYTE_LEN
}
pub fn num_palettes_byte_range(&self) -> Range<usize> {
let start = self.num_palette_entries_byte_range().end;
start..start + u16::RAW_BYTE_LEN
}
pub fn num_color_records_byte_range(&self) -> Range<usize> {
let start = self.num_palettes_byte_range().end;
start..start + u16::RAW_BYTE_LEN
}
pub fn color_records_array_offset_byte_range(&self) -> Range<usize> {
let start = self.num_color_records_byte_range().end;
start..start + Offset32::RAW_BYTE_LEN
}
pub fn color_record_indices_byte_range(&self) -> Range<usize> {
let start = self.color_records_array_offset_byte_range().end;
start..start + self.color_record_indices_byte_len
}
pub fn palette_types_array_offset_byte_range(&self) -> Option<Range<usize>> {
let start = self.palette_types_array_offset_byte_start?;
Some(start..start + Offset32::RAW_BYTE_LEN)
}
pub fn palette_labels_array_offset_byte_range(&self) -> Option<Range<usize>> {
let start = self.palette_labels_array_offset_byte_start?;
Some(start..start + Offset32::RAW_BYTE_LEN)
}
pub fn palette_entry_labels_array_offset_byte_range(&self) -> Option<Range<usize>> {
let start = self.palette_entry_labels_array_offset_byte_start?;
Some(start..start + Offset32::RAW_BYTE_LEN)
}
}
impl TopLevelTable for Cpal<'_> {
const TAG: Tag = Tag::new(b"CPAL");
}
impl<'a> FontRead<'a> for Cpal<'a> {
fn read(data: FontData<'a>) -> Result<Self, ReadError> {
let mut cursor = data.cursor();
let version: u16 = cursor.read()?;
cursor.advance::<u16>();
let num_palettes: u16 = cursor.read()?;
cursor.advance::<u16>();
cursor.advance::<Offset32>();
let color_record_indices_byte_len = (num_palettes as usize)
.checked_mul(u16::RAW_BYTE_LEN)
.ok_or(ReadError::OutOfBounds)?;
cursor.advance_by(color_record_indices_byte_len);
let palette_types_array_offset_byte_start = version
.compatible(1u16)
.then(|| cursor.position())
.transpose()?;
version
.compatible(1u16)
.then(|| cursor.advance::<Offset32>());
let palette_labels_array_offset_byte_start = version
.compatible(1u16)
.then(|| cursor.position())
.transpose()?;
version
.compatible(1u16)
.then(|| cursor.advance::<Offset32>());
let palette_entry_labels_array_offset_byte_start = version
.compatible(1u16)
.then(|| cursor.position())
.transpose()?;
version
.compatible(1u16)
.then(|| cursor.advance::<Offset32>());
cursor.finish(CpalMarker {
color_record_indices_byte_len,
palette_types_array_offset_byte_start,
palette_labels_array_offset_byte_start,
palette_entry_labels_array_offset_byte_start,
})
}
}
pub type Cpal<'a> = TableRef<'a, CpalMarker>;
impl<'a> Cpal<'a> {
pub fn version(&self) -> u16 {
let range = self.shape.version_byte_range();
self.data.read_at(range.start).unwrap()
}
pub fn num_palette_entries(&self) -> u16 {
let range = self.shape.num_palette_entries_byte_range();
self.data.read_at(range.start).unwrap()
}
pub fn num_palettes(&self) -> u16 {
let range = self.shape.num_palettes_byte_range();
self.data.read_at(range.start).unwrap()
}
pub fn num_color_records(&self) -> u16 {
let range = self.shape.num_color_records_byte_range();
self.data.read_at(range.start).unwrap()
}
pub fn color_records_array_offset(&self) -> Nullable<Offset32> {
let range = self.shape.color_records_array_offset_byte_range();
self.data.read_at(range.start).unwrap()
}
pub fn color_records_array(&self) -> Option<Result<&'a [ColorRecord], ReadError>> {
let data = self.data;
let args = self.num_color_records();
self.color_records_array_offset()
.resolve_with_args(data, &args)
}
pub fn color_record_indices(&self) -> &'a [BigEndian<u16>] {
let range = self.shape.color_record_indices_byte_range();
self.data.read_array(range).unwrap()
}
pub fn palette_types_array_offset(&self) -> Option<Nullable<Offset32>> {
let range = self.shape.palette_types_array_offset_byte_range()?;
Some(self.data.read_at(range.start).unwrap())
}
pub fn palette_types_array(&self) -> Option<Result<&'a [BigEndian<PaletteType>], ReadError>> {
let data = self.data;
let args = self.num_palettes();
self.palette_types_array_offset()
.map(|x| x.resolve_with_args(data, &args))?
}
pub fn palette_labels_array_offset(&self) -> Option<Nullable<Offset32>> {
let range = self.shape.palette_labels_array_offset_byte_range()?;
Some(self.data.read_at(range.start).unwrap())
}
pub fn palette_labels_array(&self) -> Option<Result<&'a [BigEndian<u16>], ReadError>> {
let data = self.data;
let args = self.num_palettes();
self.palette_labels_array_offset()
.map(|x| x.resolve_with_args(data, &args))?
}
pub fn palette_entry_labels_array_offset(&self) -> Option<Nullable<Offset32>> {
let range = self.shape.palette_entry_labels_array_offset_byte_range()?;
Some(self.data.read_at(range.start).unwrap())
}
pub fn palette_entry_labels_array(&self) -> Option<Result<&'a [BigEndian<NameId>], ReadError>> {
let data = self.data;
let args = self.num_palette_entries();
self.palette_entry_labels_array_offset()
.map(|x| x.resolve_with_args(data, &args))?
}
}
#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Cpal<'a> {
fn type_name(&self) -> &str {
"Cpal"
}
fn get_field(&self, idx: usize) -> Option<Field<'a>> {
let version = self.version();
match idx {
0usize => Some(Field::new("version", self.version())),
1usize => Some(Field::new(
"num_palette_entries",
self.num_palette_entries(),
)),
2usize => Some(Field::new("num_palettes", self.num_palettes())),
3usize => Some(Field::new("num_color_records", self.num_color_records())),
4usize => Some(Field::new(
"color_records_array_offset",
traversal::FieldType::offset_to_array_of_records(
self.color_records_array_offset(),
self.color_records_array(),
stringify!(ColorRecord),
self.offset_data(),
),
)),
5usize => Some(Field::new(
"color_record_indices",
self.color_record_indices(),
)),
6usize if version.compatible(1u16) => Some(Field::new(
"palette_types_array_offset",
FieldType::offset_to_array_of_scalars(
self.palette_types_array_offset().unwrap(),
self.palette_types_array(),
),
)),
7usize if version.compatible(1u16) => Some(Field::new(
"palette_labels_array_offset",
FieldType::offset_to_array_of_scalars(
self.palette_labels_array_offset().unwrap(),
self.palette_labels_array(),
),
)),
8usize if version.compatible(1u16) => Some(Field::new(
"palette_entry_labels_array_offset",
FieldType::offset_to_array_of_scalars(
self.palette_entry_labels_array_offset().unwrap(),
self.palette_entry_labels_array(),
),
)),
_ => None,
}
}
}
#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for Cpal<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
(self as &dyn SomeTable<'a>).fmt(f)
}
}
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(transparent)]
pub struct PaletteType {
bits: u32,
}
impl PaletteType {
pub const USABLE_WITH_LIGHT_BACKGROUND: Self = Self { bits: 0x0001 };
pub const USABLE_WITH_DARK_BACKGROUND: Self = Self { bits: 0x0002 };
}
impl PaletteType {
#[inline]
pub const fn empty() -> Self {
Self { bits: 0 }
}
#[inline]
pub const fn all() -> Self {
Self {
bits: Self::USABLE_WITH_LIGHT_BACKGROUND.bits | Self::USABLE_WITH_DARK_BACKGROUND.bits,
}
}
#[inline]
pub const fn bits(&self) -> u32 {
self.bits
}
#[inline]
pub const fn from_bits(bits: u32) -> Option<Self> {
if (bits & !Self::all().bits()) == 0 {
Some(Self { bits })
} else {
None
}
}
#[inline]
pub const fn from_bits_truncate(bits: u32) -> Self {
Self {
bits: bits & Self::all().bits,
}
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.bits() == Self::empty().bits()
}
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
!(Self {
bits: self.bits & other.bits,
})
.is_empty()
}
#[inline]
pub const fn contains(&self, other: Self) -> bool {
(self.bits & other.bits) == other.bits
}
#[inline]
pub fn insert(&mut self, other: Self) {
self.bits |= other.bits;
}
#[inline]
pub fn remove(&mut self, other: Self) {
self.bits &= !other.bits;
}
#[inline]
pub fn toggle(&mut self, other: Self) {
self.bits ^= other.bits;
}
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self {
bits: self.bits & other.bits,
}
}
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self {
bits: self.bits | other.bits,
}
}
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
Self {
bits: self.bits & !other.bits,
}
}
}
impl std::ops::BitOr for PaletteType {
type Output = Self;
#[inline]
fn bitor(self, other: PaletteType) -> Self {
Self {
bits: self.bits | other.bits,
}
}
}
impl std::ops::BitOrAssign for PaletteType {
#[inline]
fn bitor_assign(&mut self, other: Self) {
self.bits |= other.bits;
}
}
impl std::ops::BitXor for PaletteType {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self {
Self {
bits: self.bits ^ other.bits,
}
}
}
impl std::ops::BitXorAssign for PaletteType {
#[inline]
fn bitxor_assign(&mut self, other: Self) {
self.bits ^= other.bits;
}
}
impl std::ops::BitAnd for PaletteType {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self {
Self {
bits: self.bits & other.bits,
}
}
}
impl std::ops::BitAndAssign for PaletteType {
#[inline]
fn bitand_assign(&mut self, other: Self) {
self.bits &= other.bits;
}
}
impl std::ops::Sub for PaletteType {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
Self {
bits: self.bits & !other.bits,
}
}
}
impl std::ops::SubAssign for PaletteType {
#[inline]
fn sub_assign(&mut self, other: Self) {
self.bits &= !other.bits;
}
}
impl std::ops::Not for PaletteType {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self { bits: !self.bits } & Self::all()
}
}
impl std::fmt::Debug for PaletteType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let members: &[(&str, Self)] = &[
(
"USABLE_WITH_LIGHT_BACKGROUND",
Self::USABLE_WITH_LIGHT_BACKGROUND,
),
(
"USABLE_WITH_DARK_BACKGROUND",
Self::USABLE_WITH_DARK_BACKGROUND,
),
];
let mut first = true;
for (name, value) in members {
if self.contains(*value) {
if !first {
f.write_str(" | ")?;
}
first = false;
f.write_str(name)?;
}
}
if first {
f.write_str("(empty)")?;
}
Ok(())
}
}
impl std::fmt::Binary for PaletteType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Binary::fmt(&self.bits, f)
}
}
impl std::fmt::Octal for PaletteType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Octal::fmt(&self.bits, f)
}
}
impl std::fmt::LowerHex for PaletteType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::LowerHex::fmt(&self.bits, f)
}
}
impl std::fmt::UpperHex for PaletteType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::UpperHex::fmt(&self.bits, f)
}
}
impl font_types::Scalar for PaletteType {
type Raw = <u32 as font_types::Scalar>::Raw;
fn to_raw(self) -> Self::Raw {
self.bits().to_raw()
}
fn from_raw(raw: Self::Raw) -> Self {
let t = <u32>::from_raw(raw);
Self::from_bits_truncate(t)
}
}
#[cfg(feature = "experimental_traverse")]
impl<'a> From<PaletteType> for FieldType<'a> {
fn from(src: PaletteType) -> FieldType<'a> {
src.bits().into()
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct ColorRecord {
pub blue: u8,
pub green: u8,
pub red: u8,
pub alpha: u8,
}
impl ColorRecord {
pub fn blue(&self) -> u8 {
self.blue
}
pub fn green(&self) -> u8 {
self.green
}
pub fn red(&self) -> u8 {
self.red
}
pub fn alpha(&self) -> u8 {
self.alpha
}
}
impl FixedSize for ColorRecord {
const RAW_BYTE_LEN: usize =
u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN;
}
#[cfg(feature = "experimental_traverse")]
impl<'a> SomeRecord<'a> for ColorRecord {
fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
RecordResolver {
name: "ColorRecord",
get_field: Box::new(move |idx, _data| match idx {
0usize => Some(Field::new("blue", self.blue())),
1usize => Some(Field::new("green", self.green())),
2usize => Some(Field::new("red", self.red())),
3usize => Some(Field::new("alpha", self.alpha())),
_ => None,
}),
data,
}
}
}