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

impl<'a> FontRead<'a> for Maxp<'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 })
    }
}

/// [`maxp`](https://docs.microsoft.com/en-us/typography/opentype/spec/maxp)
#[derive(Clone)]
pub struct Maxp<'a> {
    data: FontData<'a>,
}

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

    /// The version: 0x00005000 for version 0.5, 0x00010000 for version 1.0.
    pub fn version(&self) -> Version16Dot16 {
        let range = self.version_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// The number of glyphs in the font.
    pub fn num_glyphs(&self) -> u16 {
        let range = self.num_glyphs_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Maximum points in a non-composite glyph.
    pub fn max_points(&self) -> Option<u16> {
        let range = self.max_points_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Maximum contours in a non-composite glyph.
    pub fn max_contours(&self) -> Option<u16> {
        let range = self.max_contours_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Maximum points in a composite glyph.
    pub fn max_composite_points(&self) -> Option<u16> {
        let range = self.max_composite_points_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Maximum contours in a composite glyph.
    pub fn max_composite_contours(&self) -> Option<u16> {
        let range = self.max_composite_contours_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// 1 if instructions do not use the twilight zone (Z0), or 2 if
    /// instructions do use Z0; should be set to 2 in most cases.
    pub fn max_zones(&self) -> Option<u16> {
        let range = self.max_zones_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Maximum points used in Z0.
    pub fn max_twilight_points(&self) -> Option<u16> {
        let range = self.max_twilight_points_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Number of Storage Area locations.
    pub fn max_storage(&self) -> Option<u16> {
        let range = self.max_storage_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Number of FDEFs, equal to the highest function number + 1.
    pub fn max_function_defs(&self) -> Option<u16> {
        let range = self.max_function_defs_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Number of IDEFs.
    pub fn max_instruction_defs(&self) -> Option<u16> {
        let range = self.max_instruction_defs_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Maximum stack depth across Font Program ('fpgm' table), CVT
    /// Program ('prep' table) and all glyph instructions (in the
    /// 'glyf' table).
    pub fn max_stack_elements(&self) -> Option<u16> {
        let range = self.max_stack_elements_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Maximum byte count for glyph instructions.
    pub fn max_size_of_instructions(&self) -> Option<u16> {
        let range = self.max_size_of_instructions_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Maximum number of components referenced at “top level” for
    /// any composite glyph.
    pub fn max_component_elements(&self) -> Option<u16> {
        let range = self.max_component_elements_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// Maximum levels of recursion; 1 for simple components.
    pub fn max_component_depth(&self) -> Option<u16> {
        let range = self.max_component_depth_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

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

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

    pub fn max_points_byte_range(&self) -> Range<usize> {
        let start = self.num_glyphs_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_contours_byte_range(&self) -> Range<usize> {
        let start = self.max_points_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_composite_points_byte_range(&self) -> Range<usize> {
        let start = self.max_contours_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_composite_contours_byte_range(&self) -> Range<usize> {
        let start = self.max_composite_points_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_zones_byte_range(&self) -> Range<usize> {
        let start = self.max_composite_contours_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_twilight_points_byte_range(&self) -> Range<usize> {
        let start = self.max_zones_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_storage_byte_range(&self) -> Range<usize> {
        let start = self.max_twilight_points_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_function_defs_byte_range(&self) -> Range<usize> {
        let start = self.max_storage_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_instruction_defs_byte_range(&self) -> Range<usize> {
        let start = self.max_function_defs_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_stack_elements_byte_range(&self) -> Range<usize> {
        let start = self.max_instruction_defs_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_size_of_instructions_byte_range(&self) -> Range<usize> {
        let start = self.max_stack_elements_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_component_elements_byte_range(&self) -> Range<usize> {
        let start = self.max_size_of_instructions_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }

    pub fn max_component_depth_byte_range(&self) -> Range<usize> {
        let start = self.max_component_elements_byte_range().end;
        start
            ..(self.version().compatible((1u16, 0u16)))
                .then(|| start + u16::RAW_BYTE_LEN)
                .unwrap_or(start)
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Maxp<'a> {
    fn type_name(&self) -> &str {
        "Maxp"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("version", self.version())),
            1usize => Some(Field::new("num_glyphs", self.num_glyphs())),
            2usize if self.version().compatible((1u16, 0u16)) => {
                Some(Field::new("max_points", self.max_points().unwrap()))
            }
            3usize if self.version().compatible((1u16, 0u16)) => {
                Some(Field::new("max_contours", self.max_contours().unwrap()))
            }
            4usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
                "max_composite_points",
                self.max_composite_points().unwrap(),
            )),
            5usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
                "max_composite_contours",
                self.max_composite_contours().unwrap(),
            )),
            6usize if self.version().compatible((1u16, 0u16)) => {
                Some(Field::new("max_zones", self.max_zones().unwrap()))
            }
            7usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
                "max_twilight_points",
                self.max_twilight_points().unwrap(),
            )),
            8usize if self.version().compatible((1u16, 0u16)) => {
                Some(Field::new("max_storage", self.max_storage().unwrap()))
            }
            9usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
                "max_function_defs",
                self.max_function_defs().unwrap(),
            )),
            10usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
                "max_instruction_defs",
                self.max_instruction_defs().unwrap(),
            )),
            11usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
                "max_stack_elements",
                self.max_stack_elements().unwrap(),
            )),
            12usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
                "max_size_of_instructions",
                self.max_size_of_instructions().unwrap(),
            )),
            13usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
                "max_component_elements",
                self.max_component_elements().unwrap(),
            )),
            14usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
                "max_component_depth",
                self.max_component_depth().unwrap(),
            )),
            _ => None,
        }
    }
}

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