// 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 Cff2Header<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.trailing_data_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<'a> FontRead<'a> for Cff2Header<'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 })
    }
}

/// [Compact Font Format (CFF) version 2](https://learn.microsoft.com/en-us/typography/opentype/spec/cff2) table header
#[derive(Clone)]
pub struct Cff2Header<'a> {
    data: FontData<'a>,
}

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

    /// Format major version (set to 2).
    pub fn major_version(&self) -> u8 {
        let range = self.major_version_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Format minor version (set to 0).
    pub fn minor_version(&self) -> u8 {
        let range = self.minor_version_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Header size (bytes).
    pub fn header_size(&self) -> u8 {
        let range = self.header_size_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Length of Top DICT structure in bytes.
    pub fn top_dict_length(&self) -> u16 {
        let range = self.top_dict_length_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Padding bytes before the start of the Top DICT.
    pub fn _padding(&self) -> &'a [u8] {
        let range = self._padding_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    /// Data containing the Top DICT.
    pub fn top_dict_data(&self) -> &'a [u8] {
        let range = self.top_dict_data_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    /// Remaining table data.
    pub fn trailing_data(&self) -> &'a [u8] {
        let range = self.trailing_data_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

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

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

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

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

    pub fn _padding_byte_range(&self) -> Range<usize> {
        let header_size = self.header_size();
        let start = self.top_dict_length_byte_range().end;
        start..start + (transforms::subtract(header_size, 5_usize)).saturating_mul(u8::RAW_BYTE_LEN)
    }

    pub fn top_dict_data_byte_range(&self) -> Range<usize> {
        let top_dict_length = self.top_dict_length();
        let start = self._padding_byte_range().end;
        start..start + (top_dict_length as usize).saturating_mul(u8::RAW_BYTE_LEN)
    }

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

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Cff2Header<'a> {
    fn type_name(&self) -> &str {
        "Cff2Header"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("major_version", self.major_version())),
            1usize => Some(Field::new("minor_version", self.minor_version())),
            2usize => Some(Field::new("header_size", self.header_size())),
            3usize => Some(Field::new("top_dict_length", self.top_dict_length())),
            4usize => Some(Field::new("_padding", self._padding())),
            5usize => Some(Field::new("top_dict_data", self.top_dict_data())),
            6usize => Some(Field::new("trailing_data", self.trailing_data())),
            _ => None,
        }
    }
}

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

impl<'a> MinByteRange<'a> for Index<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.data_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<'a> FontRead<'a> for Index<'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 })
    }
}

/// An array of variable-sized objects in a `CFF2` table.
#[derive(Clone)]
pub struct Index<'a> {
    data: FontData<'a>,
}

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

    /// Number of objects stored in INDEX.
    pub fn count(&self) -> u32 {
        let range = self.count_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Object array element size.
    pub fn off_size(&self) -> u8 {
        let range = self.off_size_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Bytes containing `count + 1` offsets each of `off_size`.
    pub fn offsets(&self) -> &'a [u8] {
        let range = self.offsets_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    /// Array containing the object data.
    pub fn data(&self) -> &'a [u8] {
        let range = self.data_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

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

    pub fn off_size_byte_range(&self) -> Range<usize> {
        let start = self.count_byte_range().end;
        start..start + u8::RAW_BYTE_LEN
    }

    pub fn offsets_byte_range(&self) -> Range<usize> {
        let count = self.count();
        let off_size = self.off_size();
        let start = self.off_size_byte_range().end;
        start
            ..start
                + (transforms::add_multiply(count, 1_usize, off_size))
                    .saturating_mul(u8::RAW_BYTE_LEN)
    }

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

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Index<'a> {
    fn type_name(&self) -> &str {
        "Index"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("count", self.count())),
            1usize => Some(Field::new("off_size", self.off_size())),
            2usize => Some(Field::new("offsets", self.offsets())),
            3usize => Some(Field::new("data", self.data())),
            _ => None,
        }
    }
}

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