#[allow(unused_imports)]
use crate::codegen_prelude::*;
impl<'a> MinByteRange<'a> for MajorMinorVersion<'a> {
fn min_byte_range(&self) -> Range<usize> {
0..self.always_present_byte_range().end
}
fn min_table_bytes(&self) -> &'a [u8] {
let range = self.min_byte_range();
self.data.as_bytes().get(range).unwrap_or_default()
}
}
impl<'a> FontRead<'a> for MajorMinorVersion<'a> {
fn read(data: FontData<'a>) -> Result<Self, ReadError> {
#[allow(clippy::absurd_extreme_comparisons)]
if data.len() < Self::MIN_SIZE {
return Err(ReadError::OutOfBounds);
}
Ok(Self { data })
}
}
#[derive(Clone)]
pub struct MajorMinorVersion<'a> {
data: FontData<'a>,
}
#[allow(clippy::needless_lifetimes)]
impl<'a> MajorMinorVersion<'a> {
pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
basic_table_impls!(impl_the_methods);
pub fn version(&self) -> MajorMinor {
let range = self.version_byte_range();
self.data.read_at(range.start).ok().unwrap()
}
pub fn always_present(&self) -> u16 {
let range = self.always_present_byte_range();
self.data.read_at(range.start).ok().unwrap()
}
pub fn if_11(&self) -> Option<u16> {
let range = self.if_11_byte_range();
(!range.is_empty())
.then(|| self.data.read_at(range.start).ok())
.flatten()
}
pub fn if_20(&self) -> Option<u32> {
let range = self.if_20_byte_range();
(!range.is_empty())
.then(|| self.data.read_at(range.start).ok())
.flatten()
}
pub fn version_byte_range(&self) -> Range<usize> {
let start = 0;
start..start + MajorMinor::RAW_BYTE_LEN
}
pub fn always_present_byte_range(&self) -> Range<usize> {
let start = self.version_byte_range().end;
start..start + u16::RAW_BYTE_LEN
}
pub fn if_11_byte_range(&self) -> Range<usize> {
let start = self.always_present_byte_range().end;
start
..(self.version().compatible((1u16, 1u16)))
.then(|| start + u16::RAW_BYTE_LEN)
.unwrap_or(start)
}
pub fn if_20_byte_range(&self) -> Range<usize> {
let start = self.if_11_byte_range().end;
start
..(self.version().compatible((2u16, 0u16)))
.then(|| start + u32::RAW_BYTE_LEN)
.unwrap_or(start)
}
}
#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for MajorMinorVersion<'a> {
fn type_name(&self) -> &str {
"MajorMinorVersion"
}
fn get_field(&self, idx: usize) -> Option<Field<'a>> {
match idx {
0usize => Some(Field::new("version", self.version())),
1usize => Some(Field::new("always_present", self.always_present())),
2usize if self.version().compatible((1u16, 1u16)) => {
Some(Field::new("if_11", self.if_11().unwrap()))
}
3usize if self.version().compatible((2u16, 0u16)) => {
Some(Field::new("if_20", self.if_20().unwrap()))
}
_ => None,
}
}
}
#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for MajorMinorVersion<'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 GotFlags {
bits: u16,
}
impl GotFlags {
pub const FOO: Self = Self { bits: 0x0001 };
pub const BAR: Self = Self { bits: 0x0002 };
pub const BAZ: Self = Self { bits: 0x0004 };
}
impl GotFlags {
#[inline]
pub const fn empty() -> Self {
Self { bits: 0 }
}
#[inline]
pub const fn all() -> Self {
Self {
bits: Self::FOO.bits | Self::BAR.bits | Self::BAZ.bits,
}
}
#[inline]
pub const fn bits(&self) -> u16 {
self.bits
}
#[inline]
pub const fn from_bits(bits: u16) -> Option<Self> {
if (bits & !Self::all().bits()) == 0 {
Some(Self { bits })
} else {
None
}
}
#[inline]
pub const fn from_bits_truncate(bits: u16) -> 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 GotFlags {
type Output = Self;
#[inline]
fn bitor(self, other: GotFlags) -> Self {
Self {
bits: self.bits | other.bits,
}
}
}
impl std::ops::BitOrAssign for GotFlags {
#[inline]
fn bitor_assign(&mut self, other: Self) {
self.bits |= other.bits;
}
}
impl std::ops::BitXor for GotFlags {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self {
Self {
bits: self.bits ^ other.bits,
}
}
}
impl std::ops::BitXorAssign for GotFlags {
#[inline]
fn bitxor_assign(&mut self, other: Self) {
self.bits ^= other.bits;
}
}
impl std::ops::BitAnd for GotFlags {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self {
Self {
bits: self.bits & other.bits,
}
}
}
impl std::ops::BitAndAssign for GotFlags {
#[inline]
fn bitand_assign(&mut self, other: Self) {
self.bits &= other.bits;
}
}
impl std::ops::Sub for GotFlags {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
Self {
bits: self.bits & !other.bits,
}
}
}
impl std::ops::SubAssign for GotFlags {
#[inline]
fn sub_assign(&mut self, other: Self) {
self.bits &= !other.bits;
}
}
impl std::ops::Not for GotFlags {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self { bits: !self.bits } & Self::all()
}
}
impl std::fmt::Debug for GotFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let members: &[(&str, Self)] =
&[("FOO", Self::FOO), ("BAR", Self::BAR), ("BAZ", Self::BAZ)];
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 GotFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Binary::fmt(&self.bits, f)
}
}
impl std::fmt::Octal for GotFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Octal::fmt(&self.bits, f)
}
}
impl std::fmt::LowerHex for GotFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::LowerHex::fmt(&self.bits, f)
}
}
impl std::fmt::UpperHex for GotFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::UpperHex::fmt(&self.bits, f)
}
}
impl font_types::Scalar for GotFlags {
type Raw = <u16 as font_types::Scalar>::Raw;
fn to_raw(self) -> Self::Raw {
self.bits().to_raw()
}
fn from_raw(raw: Self::Raw) -> Self {
let t = <u16>::from_raw(raw);
Self::from_bits_truncate(t)
}
}
#[cfg(feature = "experimental_traverse")]
impl<'a> From<GotFlags> for FieldType<'a> {
fn from(src: GotFlags) -> FieldType<'a> {
src.bits().into()
}
}
impl<'a> MinByteRange<'a> for FlagDay<'a> {
fn min_byte_range(&self) -> Range<usize> {
0..self.flags_byte_range().end
}
fn min_table_bytes(&self) -> &'a [u8] {
let range = self.min_byte_range();
self.data.as_bytes().get(range).unwrap_or_default()
}
}
impl<'a> FontRead<'a> for FlagDay<'a> {
fn read(data: FontData<'a>) -> Result<Self, ReadError> {
#[allow(clippy::absurd_extreme_comparisons)]
if data.len() < Self::MIN_SIZE {
return Err(ReadError::OutOfBounds);
}
Ok(Self { data })
}
}
#[derive(Clone)]
pub struct FlagDay<'a> {
data: FontData<'a>,
}
#[allow(clippy::needless_lifetimes)]
impl<'a> FlagDay<'a> {
pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + GotFlags::RAW_BYTE_LEN);
basic_table_impls!(impl_the_methods);
pub fn volume(&self) -> u16 {
let range = self.volume_byte_range();
self.data.read_at(range.start).ok().unwrap()
}
pub fn flags(&self) -> GotFlags {
let range = self.flags_byte_range();
self.data.read_at(range.start).ok().unwrap()
}
pub fn foo(&self) -> Option<u16> {
let range = self.foo_byte_range();
(!range.is_empty())
.then(|| self.data.read_at(range.start).ok())
.flatten()
}
pub fn bar(&self) -> Option<u16> {
let range = self.bar_byte_range();
(!range.is_empty())
.then(|| self.data.read_at(range.start).ok())
.flatten()
}
pub fn volume_byte_range(&self) -> Range<usize> {
let start = 0;
start..start + u16::RAW_BYTE_LEN
}
pub fn flags_byte_range(&self) -> Range<usize> {
let start = self.volume_byte_range().end;
start..start + GotFlags::RAW_BYTE_LEN
}
pub fn foo_byte_range(&self) -> Range<usize> {
let start = self.flags_byte_range().end;
start
..(self.flags().contains(GotFlags::FOO))
.then(|| start + u16::RAW_BYTE_LEN)
.unwrap_or(start)
}
pub fn bar_byte_range(&self) -> Range<usize> {
let start = self.foo_byte_range().end;
start
..(self.flags().contains(GotFlags::BAR))
.then(|| start + u16::RAW_BYTE_LEN)
.unwrap_or(start)
}
}
#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for FlagDay<'a> {
fn type_name(&self) -> &str {
"FlagDay"
}
fn get_field(&self, idx: usize) -> Option<Field<'a>> {
match idx {
0usize => Some(Field::new("volume", self.volume())),
1usize => Some(Field::new("flags", self.flags())),
2usize if self.flags().contains(GotFlags::FOO) => {
Some(Field::new("foo", self.foo().unwrap()))
}
3usize if self.flags().contains(GotFlags::BAR) => {
Some(Field::new("bar", self.bar().unwrap()))
}
_ => None,
}
}
}
#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for FlagDay<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
(self as &dyn SomeTable<'a>).fmt(f)
}
}