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

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

impl<'a> FontRead<'a> for Avar<'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 [avar (Axis Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/avar) table
#[derive(Clone)]
pub struct Avar<'a> {
    data: FontData<'a>,
}

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

    /// Major version number of the axis variations table — set to 1 or 2.
    /// Minor version number of the axis 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 number of variation axes for this font. This must be the same number as axisCount in the 'fvar' table.
    pub fn axis_count(&self) -> u16 {
        let range = self.axis_count_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// The segment maps array — one segment map for each axis, in the order of axes specified in the 'fvar' table.
    pub fn axis_segment_maps(&self) -> VarLenArray<'a, SegmentMaps<'a>> {
        let range = self.axis_segment_maps_byte_range();
        self.data
            .split_off(range.start)
            .and_then(|d| VarLenArray::read(d).ok())
            .unwrap_or_default()
    }

    /// Offset to DeltaSetIndexMap table (may be NULL).
    pub fn axis_index_map_offset(&self) -> Option<Nullable<Offset32>> {
        let range = self.axis_index_map_offset_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Attempt to resolve [`axis_index_map_offset`][Self::axis_index_map_offset].
    pub fn axis_index_map(&self) -> Option<Result<DeltaSetIndexMap<'a>, ReadError>> {
        let data = self.data;
        self.axis_index_map_offset().map(|x| x.resolve(data))?
    }

    /// Offset to ItemVariationStore (may be NULL).
    pub fn var_store_offset(&self) -> Option<Nullable<Offset32>> {
        let range = self.var_store_offset_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

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

    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 axis_count_byte_range(&self) -> Range<usize> {
        let start = self._reserved_byte_range().end;
        let end = start + u16::RAW_BYTE_LEN;
        start..end
    }

    pub fn axis_segment_maps_byte_range(&self) -> Range<usize> {
        let axis_count = self.axis_count();
        let start = self.axis_count_byte_range().end;
        let end = start + {
            let data = self.data.split_off(start).unwrap_or_default();
            <SegmentMaps as VarSize>::total_len_for_count(data, transforms::to_usize(axis_count))
                .unwrap_or(0)
        };
        start..end
    }

    pub fn axis_index_map_offset_byte_range(&self) -> Range<usize> {
        let start = self.axis_segment_maps_byte_range().end;
        let end = if self.version().compatible((2u16, 0u16)) {
            start + Offset32::RAW_BYTE_LEN
        } else {
            start
        };
        start..end
    }

    pub fn var_store_offset_byte_range(&self) -> Range<usize> {
        let start = self.axis_index_map_offset_byte_range().end;
        let end = if self.version().compatible((2u16, 0u16)) {
            start + Offset32::RAW_BYTE_LEN
        } else {
            start
        };
        start..end
    }
}

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

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

/// [SegmentMaps](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SegmentMaps<'a> {
    /// The number of correspondence pairs for this axis.
    pub position_map_count: BigEndian<u16>,
    /// The array of axis value map records for this axis.
    pub axis_value_maps: &'a [AxisValueMap],
}

impl<'a> SegmentMaps<'a> {
    /// The number of correspondence pairs for this axis.
    pub fn position_map_count(&self) -> u16 {
        self.position_map_count.get()
    }

    /// The array of axis value map records for this axis.
    pub fn axis_value_maps(&self) -> &'a [AxisValueMap] {
        self.axis_value_maps
    }
}

/// [AxisValueMap](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct AxisValueMap {
    /// A normalized coordinate value obtained using default normalization.
    pub from_coordinate: BigEndian<F2Dot14>,
    /// The modified, normalized coordinate value.
    pub to_coordinate: BigEndian<F2Dot14>,
}

impl AxisValueMap {
    /// A normalized coordinate value obtained using default normalization.
    pub fn from_coordinate(&self) -> F2Dot14 {
        self.from_coordinate.get()
    }

    /// The modified, normalized coordinate value.
    pub fn to_coordinate(&self) -> F2Dot14 {
        self.to_coordinate.get()
    }
}

impl FixedSize for AxisValueMap {
    const RAW_BYTE_LEN: usize = F2Dot14::RAW_BYTE_LEN + F2Dot14::RAW_BYTE_LEN;
}