#[allow(unused_imports)]
use crate::codegen_prelude::*;
impl<'a> MinByteRange<'a> for Dsig<'a> {
fn min_byte_range(&self) -> Range<usize> {
0..self.signature_records_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 TopLevelTable for Dsig<'_> {
const TAG: Tag = Tag::new(b"DSIG");
}
impl<'a> FontRead<'a> for Dsig<'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 Dsig<'a> {
data: FontData<'a>,
}
#[allow(clippy::needless_lifetimes)]
impl<'a> Dsig<'a> {
pub const MIN_SIZE: usize =
(u32::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + PermissionFlags::RAW_BYTE_LEN);
basic_table_impls!(impl_the_methods);
pub fn version(&self) -> u32 {
let range = self.version_byte_range();
self.data.read_at(range.start).ok().unwrap()
}
pub fn num_signatures(&self) -> u16 {
let range = self.num_signatures_byte_range();
self.data.read_at(range.start).ok().unwrap()
}
pub fn flags(&self) -> PermissionFlags {
let range = self.flags_byte_range();
self.data.read_at(range.start).ok().unwrap()
}
pub fn signature_records(&self) -> &'a [SignatureRecord] {
let range = self.signature_records_byte_range();
self.data.read_array(range).ok().unwrap_or_default()
}
pub fn version_byte_range(&self) -> Range<usize> {
let start = 0;
start..start + u32::RAW_BYTE_LEN
}
pub fn num_signatures_byte_range(&self) -> Range<usize> {
let start = self.version_byte_range().end;
start..start + u16::RAW_BYTE_LEN
}
pub fn flags_byte_range(&self) -> Range<usize> {
let start = self.num_signatures_byte_range().end;
start..start + PermissionFlags::RAW_BYTE_LEN
}
pub fn signature_records_byte_range(&self) -> Range<usize> {
let num_signatures = self.num_signatures();
let start = self.flags_byte_range().end;
start..start + (num_signatures as usize).saturating_mul(SignatureRecord::RAW_BYTE_LEN)
}
}
#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Dsig<'a> {
fn type_name(&self) -> &str {
"Dsig"
}
fn get_field(&self, idx: usize) -> Option<Field<'a>> {
match idx {
0usize => Some(Field::new("version", self.version())),
1usize => Some(Field::new("num_signatures", self.num_signatures())),
2usize => Some(Field::new("flags", self.flags())),
3usize => Some(Field::new(
"signature_records",
traversal::FieldType::array_of_records(
stringify!(SignatureRecord),
self.signature_records(),
self.offset_data(),
),
)),
_ => None,
}
}
}
#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for Dsig<'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 PermissionFlags {
bits: u16,
}
impl PermissionFlags {
pub const CANNOT_BE_RESIGNED: Self = Self {
bits: 0b0000_0000_0000_0001,
};
}
impl PermissionFlags {
#[inline]
pub const fn empty() -> Self {
Self { bits: 0 }
}
#[inline]
pub const fn all() -> Self {
Self {
bits: Self::CANNOT_BE_RESIGNED.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 PermissionFlags {
type Output = Self;
#[inline]
fn bitor(self, other: PermissionFlags) -> Self {
Self {
bits: self.bits | other.bits,
}
}
}
impl std::ops::BitOrAssign for PermissionFlags {
#[inline]
fn bitor_assign(&mut self, other: Self) {
self.bits |= other.bits;
}
}
impl std::ops::BitXor for PermissionFlags {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self {
Self {
bits: self.bits ^ other.bits,
}
}
}
impl std::ops::BitXorAssign for PermissionFlags {
#[inline]
fn bitxor_assign(&mut self, other: Self) {
self.bits ^= other.bits;
}
}
impl std::ops::BitAnd for PermissionFlags {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self {
Self {
bits: self.bits & other.bits,
}
}
}
impl std::ops::BitAndAssign for PermissionFlags {
#[inline]
fn bitand_assign(&mut self, other: Self) {
self.bits &= other.bits;
}
}
impl std::ops::Sub for PermissionFlags {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
Self {
bits: self.bits & !other.bits,
}
}
}
impl std::ops::SubAssign for PermissionFlags {
#[inline]
fn sub_assign(&mut self, other: Self) {
self.bits &= !other.bits;
}
}
impl std::ops::Not for PermissionFlags {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self { bits: !self.bits } & Self::all()
}
}
impl std::fmt::Debug for PermissionFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let members: &[(&str, Self)] = &[("CANNOT_BE_RESIGNED", Self::CANNOT_BE_RESIGNED)];
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 PermissionFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Binary::fmt(&self.bits, f)
}
}
impl std::fmt::Octal for PermissionFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Octal::fmt(&self.bits, f)
}
}
impl std::fmt::LowerHex for PermissionFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::LowerHex::fmt(&self.bits, f)
}
}
impl std::fmt::UpperHex for PermissionFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::UpperHex::fmt(&self.bits, f)
}
}
impl font_types::Scalar for PermissionFlags {
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<PermissionFlags> for FieldType<'a> {
fn from(src: PermissionFlags) -> FieldType<'a> {
src.bits().into()
}
}
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct SignatureRecord {
pub format: BigEndian<u32>,
pub length: BigEndian<u32>,
pub signature_block_offset: BigEndian<Offset32>,
}
impl SignatureRecord {
pub fn format(&self) -> u32 {
self.format.get()
}
pub fn length(&self) -> u32 {
self.length.get()
}
pub fn signature_block_offset(&self) -> Offset32 {
self.signature_block_offset.get()
}
}
impl FixedSize for SignatureRecord {
const RAW_BYTE_LEN: usize = u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN;
}
#[cfg(feature = "experimental_traverse")]
impl<'a> SomeRecord<'a> for SignatureRecord {
fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
RecordResolver {
name: "SignatureRecord",
get_field: Box::new(move |idx, _data| match idx {
0usize => Some(Field::new("format", self.format())),
1usize => Some(Field::new("length", self.length())),
2usize => Some(Field::new(
"signature_block_offset",
FieldType::offset(self.signature_block_offset(), self.signature_block(_data)),
)),
_ => None,
}),
data,
}
}
}
impl<'a> MinByteRange<'a> for SignatureBlockFormat1<'a> {
fn min_byte_range(&self) -> Range<usize> {
0..self.signature_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 SignatureBlockFormat1<'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 SignatureBlockFormat1<'a> {
data: FontData<'a>,
}
#[allow(clippy::needless_lifetimes)]
impl<'a> SignatureBlockFormat1<'a> {
pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
basic_table_impls!(impl_the_methods);
pub fn signature_length(&self) -> u32 {
let range = self.signature_length_byte_range();
self.data.read_at(range.start).ok().unwrap()
}
pub fn signature(&self) -> &'a [u8] {
let range = self.signature_byte_range();
self.data.read_array(range).ok().unwrap_or_default()
}
pub fn _reserved1_byte_range(&self) -> Range<usize> {
let start = 0;
start..start + u16::RAW_BYTE_LEN
}
pub fn _reserved2_byte_range(&self) -> Range<usize> {
let start = self._reserved1_byte_range().end;
start..start + u16::RAW_BYTE_LEN
}
pub fn signature_length_byte_range(&self) -> Range<usize> {
let start = self._reserved2_byte_range().end;
start..start + u32::RAW_BYTE_LEN
}
pub fn signature_byte_range(&self) -> Range<usize> {
let signature_length = self.signature_length();
let start = self.signature_length_byte_range().end;
start..start + (signature_length as usize).saturating_mul(u8::RAW_BYTE_LEN)
}
}
#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for SignatureBlockFormat1<'a> {
fn type_name(&self) -> &str {
"SignatureBlockFormat1"
}
fn get_field(&self, idx: usize) -> Option<Field<'a>> {
match idx {
0usize => Some(Field::new("signature_length", self.signature_length())),
1usize => Some(Field::new("signature", self.signature())),
_ => None,
}
}
}
#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for SignatureBlockFormat1<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
(self as &dyn SomeTable<'a>).fmt(f)
}
}