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

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

/// The [Color Bitmap Data](https://learn.microsoft.com/en-us/typography/opentype/spec/cbdt) table
#[derive(Clone)]
pub struct Cbdt<'a> {
    data: FontData<'a>,
}

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

    /// Major version of the CBDT table, = 3.
    pub fn major_version(&self) -> u16 {
        let range = self.major_version_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Minor version of CBDT table, = 0.
    pub fn minor_version(&self) -> u16 {
        let range = self.minor_version_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

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

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

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

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

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Cbdt<'a> {
    fn type_name(&self) -> &str {
        "Cbdt"
    }
    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())),
            _ => None,
        }
    }
}

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