read-fonts 0.46.0

Reading OpenType font files.
Documentation
// THIS FILE IS AUTOGENERATED.
// Any changes to this file will be overwritten.
// For more information about how codegen works, see font-codegen/README.md

#[allow(unused_imports)]
use crate::codegen_prelude::*;

impl<'a> MinByteRange<'a> for Eblc<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.bitmap_sizes_byte_range().end
    }
    fn min_table_bytes(&self) -> &'a [u8] {
        let range = self.min_byte_range();
        self.offset_data().as_bytes().get(range).unwrap_or_default()
    }
}

impl TopLevelTable for Eblc<'_> {
    /// `EBLC`
    const TAG: Tag = Tag::new(b"EBLC");
}

impl ReadArgs for Eblc<'_> {
    type Args = ();
}

impl<'a> FontRead<'a> for Eblc<'a> {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
            return Err(ReadError::OutOfBounds);
        };
        Ok(Self { data })
    }
}

/// The fields at the start of the table, which are read directly.
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
#[repr(C, packed)]
#[doc(hidden)]
pub struct EblcFixedHeader {
    pub major_version: BigEndian<u16>,
    pub minor_version: BigEndian<u16>,
    pub num_sizes: BigEndian<u32>,
}

#[allow(clippy::absurd_extreme_comparisons)]
const _: () = assert!(std::mem::size_of::<EblcFixedHeader>() <= Eblc::MIN_SIZE);

/// The [Embedded Bitmap Location](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc) table
#[derive(Clone)]
pub struct Eblc<'a> {
    data: TableDataRef<'a, EblcFixedHeader>,
}

#[allow(clippy::needless_lifetimes)]
impl<'a> Eblc<'a> {
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
    basic_table_impls!(impl_the_methods);

    /// Major version of the EBLC table, = 2.
    pub fn major_version(&self) -> u16 {
        self.data.header().major_version.get()
    }

    /// Minor version of EBLC table, = 0.
    pub fn minor_version(&self) -> u16 {
        self.data.header().minor_version.get()
    }

    /// Number of BitmapSize records.
    pub fn num_sizes(&self) -> u32 {
        self.data.header().num_sizes.get()
    }

    /// BitmapSize records array.
    pub fn bitmap_sizes(&self) -> &'a [BitmapSize] {
        let range = self.bitmap_sizes_byte_range();
        self.offset_data()
            .read_array(range)
            .ok()
            .unwrap_or_default()
    }

    pub fn major_version_byte_range(&self) -> Range<usize> {
        let start = 0;
        let end = start + u16::RAW_BYTE_LEN;
        start..end
    }

    pub fn minor_version_byte_range(&self) -> Range<usize> {
        let start = self.major_version_byte_range().end;
        let end = start + u16::RAW_BYTE_LEN;
        start..end
    }

    pub fn num_sizes_byte_range(&self) -> Range<usize> {
        let start = self.minor_version_byte_range().end;
        let end = start + u32::RAW_BYTE_LEN;
        start..end
    }

    pub fn bitmap_sizes_byte_range(&self) -> Range<usize> {
        let num_sizes = self.num_sizes();
        let start = self.num_sizes_byte_range().end;
        let end =
            start + (transforms::to_usize(num_sizes)).saturating_mul(BitmapSize::RAW_BYTE_LEN);
        start..end
    }
}

const _: () = assert!(FontData::default_data_long_enough(Eblc::MIN_SIZE));

impl Default for Eblc<'_> {
    fn default() -> Self {
        Self {
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
        }
    }
}