read-fonts 0.44.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 Mvar<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.value_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 Mvar<'_> {
    /// `MVAR`
    const TAG: Tag = Tag::new(b"MVAR");
}

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

impl<'a> FontRead<'a> for Mvar<'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);
        }
        Ok(Self { data })
    }
}

/// The [MVAR (Metrics Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/mvar) table
#[derive(Clone)]
pub struct Mvar<'a> {
    data: FontData<'a>,
}

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

    /// Major version number of the horizontal metrics variations table — set to 1.
    /// Minor version number of the horizontal metrics variations table — set to 0.
    pub fn version(&self) -> MajorMinor {
        let range = self.version_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// The size in bytes of each value record — must be greater than zero.
    pub fn value_record_size(&self) -> u16 {
        let range = self.value_record_size_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// The number of value records — may be zero.
    pub fn value_record_count(&self) -> u16 {
        let range = self.value_record_count_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Offset in bytes from the start of this table to the item variation store table. If valueRecordCount is zero, set to zero; if valueRecordCount is greater than zero, must be greater than zero.
    pub fn item_variation_store_offset(&self) -> Nullable<Offset16> {
        let range = self.item_variation_store_offset_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Attempt to resolve [`item_variation_store_offset`][Self::item_variation_store_offset].
    pub fn item_variation_store(&self) -> Option<Result<ItemVariationStore<'a>, ReadError>> {
        let data = self.data;
        self.item_variation_store_offset().resolve(data)
    }

    /// Array of value records that identify target items and the associated delta-set index for each. The valueTag records must be in binary order of their valueTag field.
    pub fn value_records(&self) -> &'a [ValueRecord] {
        let range = self.value_records_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

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

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

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

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

    pub fn item_variation_store_offset_byte_range(&self) -> Range<usize> {
        let start = self.value_record_count_byte_range().end;
        let end = start + Offset16::RAW_BYTE_LEN;
        start..end
    }

    pub fn value_records_byte_range(&self) -> Range<usize> {
        let value_record_count = self.value_record_count();
        let start = self.item_variation_store_offset_byte_range().end;
        let end = start
            + (transforms::to_usize(value_record_count)).saturating_mul(ValueRecord::RAW_BYTE_LEN);
        start..end
    }
}

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

impl Default for Mvar<'_> {
    fn default() -> Self {
        Self {
            data: FontData::default_table_data(),
        }
    }
}

/// [ValueRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/mvar#table-formats) metrics variation record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct ValueRecord {
    /// Four-byte tag identifying a font-wide measure.
    pub value_tag: BigEndian<Tag>,
    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
    pub delta_set_outer_index: BigEndian<u16>,
    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
    pub delta_set_inner_index: BigEndian<u16>,
}

impl ValueRecord {
    /// Four-byte tag identifying a font-wide measure.
    pub fn value_tag(&self) -> Tag {
        self.value_tag.get()
    }

    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
    pub fn delta_set_outer_index(&self) -> u16 {
        self.delta_set_outer_index.get()
    }

    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
    pub fn delta_set_inner_index(&self) -> u16 {
        self.delta_set_inner_index.get()
    }
}

impl FixedSize for ValueRecord {
    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
}