read-fonts 0.39.2

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 Vmtx<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.top_side_bearings_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 Vmtx<'_> {
    /// `vmtx`
    const TAG: Tag = Tag::new(b"vmtx");
}

impl ReadArgs for Vmtx<'_> {
    type Args = u16;
}

impl<'a> FontReadWithArgs<'a> for Vmtx<'a> {
    fn read_with_args(data: FontData<'a>, args: &u16) -> Result<Self, ReadError> {
        let number_of_long_ver_metrics = *args;

        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        Ok(Self {
            data,
            number_of_long_ver_metrics,
        })
    }
}

impl<'a> Vmtx<'a> {
    /// A constructor that requires additional arguments.
    ///
    /// This type requires some external state in order to be
    /// parsed.
    pub fn read(data: FontData<'a>, number_of_long_ver_metrics: u16) -> Result<Self, ReadError> {
        let args = number_of_long_ver_metrics;
        Self::read_with_args(data, &args)
    }
}

/// The [vmtx (Vertical Metrics)](https://docs.microsoft.com/en-us/typography/opentype/spec/vmtx) table
#[derive(Clone)]
pub struct Vmtx<'a> {
    data: FontData<'a>,
    number_of_long_ver_metrics: u16,
}

#[allow(clippy::needless_lifetimes)]
impl<'a> Vmtx<'a> {
    pub const MIN_SIZE: usize = 0;
    basic_table_impls!(impl_the_methods);

    /// Paired advance height and top side bearing values for each
    /// glyph. Records are indexed by glyph ID.
    pub fn v_metrics(&self) -> &'a [LongMetric] {
        let range = self.v_metrics_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    /// Top side bearings for glyph IDs greater than or equal to numberOfLongMetrics.
    pub fn top_side_bearings(&self) -> &'a [BigEndian<i16>] {
        let range = self.top_side_bearings_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    pub(crate) fn number_of_long_ver_metrics(&self) -> u16 {
        self.number_of_long_ver_metrics
    }

    pub fn v_metrics_byte_range(&self) -> Range<usize> {
        let number_of_long_ver_metrics = self.number_of_long_ver_metrics();
        let start = 0;
        start
            ..start + (number_of_long_ver_metrics as usize).saturating_mul(LongMetric::RAW_BYTE_LEN)
    }

    pub fn top_side_bearings_byte_range(&self) -> Range<usize> {
        let start = self.v_metrics_byte_range().end;
        start..start + self.data.len().saturating_sub(start) / i16::RAW_BYTE_LEN * i16::RAW_BYTE_LEN
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Vmtx<'a> {
    fn type_name(&self) -> &str {
        "Vmtx"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new(
                "v_metrics",
                traversal::FieldType::array_of_records(
                    stringify!(LongMetric),
                    self.v_metrics(),
                    self.offset_data(),
                ),
            )),
            1usize => Some(Field::new("top_side_bearings", self.top_side_bearings())),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for Vmtx<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}