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

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

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

/// [Naming table version 1](https://docs.microsoft.com/en-us/typography/opentype/spec/name#naming-table-version-1)
#[derive(Clone)]
pub struct Name<'a> {
    data: FontData<'a>,
}

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

    /// Table version number (0 or 1)
    pub fn version(&self) -> u16 {
        let range = self.version_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Number of name records.
    pub fn count(&self) -> u16 {
        let range = self.count_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Offset to start of string storage (from start of table).
    pub fn storage_offset(&self) -> u16 {
        let range = self.storage_offset_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// The name records where count is the number of records.
    pub fn name_record(&self) -> &'a [NameRecord] {
        let range = self.name_record_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    /// Number of language-tag records.
    pub fn lang_tag_count(&self) -> Option<u16> {
        let range = self.lang_tag_count_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_at(range.start).ok())
            .flatten()
    }

    /// The language-tag records where langTagCount is the number of records.
    pub fn lang_tag_record(&self) -> Option<&'a [LangTagRecord]> {
        let range = self.lang_tag_record_byte_range();
        (!range.is_empty())
            .then(|| self.data.read_array(range).ok())
            .flatten()
    }

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

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

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

    pub fn name_record_byte_range(&self) -> Range<usize> {
        let count = self.count();
        let start = self.storage_offset_byte_range().end;
        let end = start + (transforms::to_usize(count)).saturating_mul(NameRecord::RAW_BYTE_LEN);
        start..end
    }

    pub fn lang_tag_count_byte_range(&self) -> Range<usize> {
        let start = self.name_record_byte_range().end;
        let end = if self.version().compatible(1u16) {
            start + u16::RAW_BYTE_LEN
        } else {
            start
        };
        start..end
    }

    pub fn lang_tag_record_byte_range(&self) -> Range<usize> {
        let lang_tag_count = self.lang_tag_count().unwrap_or_default();
        let start = self.lang_tag_count_byte_range().end;
        let end = if self.version().compatible(1u16) {
            start
                + (transforms::to_usize(lang_tag_count)).saturating_mul(LangTagRecord::RAW_BYTE_LEN)
        } else {
            start
        };
        start..end
    }
}

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

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

/// Part of [Name]
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct LangTagRecord {
    /// Language-tag string length (in bytes)
    pub length: BigEndian<u16>,
    /// Language-tag string offset from start of storage area (in
    /// bytes).
    pub lang_tag_offset: BigEndian<Offset16>,
}

impl LangTagRecord {
    /// Language-tag string length (in bytes)
    pub fn length(&self) -> u16 {
        self.length.get()
    }

    /// Language-tag string offset from start of storage area (in
    /// bytes).
    pub fn lang_tag_offset(&self) -> Offset16 {
        self.lang_tag_offset.get()
    }
}

impl FixedSize for LangTagRecord {
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN;
}

///[Name Records](https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records)
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct NameRecord {
    /// Platform ID.
    pub platform_id: BigEndian<u16>,
    /// Platform-specific encoding ID.
    pub encoding_id: BigEndian<u16>,
    /// Language ID.
    pub language_id: BigEndian<u16>,
    /// Name ID.
    pub name_id: BigEndian<NameId>,
    /// String length (in bytes).
    pub length: BigEndian<u16>,
    /// String offset from start of storage area (in bytes).
    pub string_offset: BigEndian<Offset16>,
}

impl NameRecord {
    /// Platform ID.
    pub fn platform_id(&self) -> u16 {
        self.platform_id.get()
    }

    /// Platform-specific encoding ID.
    pub fn encoding_id(&self) -> u16 {
        self.encoding_id.get()
    }

    /// Language ID.
    pub fn language_id(&self) -> u16 {
        self.language_id.get()
    }

    /// Name ID.
    pub fn name_id(&self) -> NameId {
        self.name_id.get()
    }

    /// String length (in bytes).
    pub fn length(&self) -> u16 {
        self.length.get()
    }

    /// String offset from start of storage area (in bytes).
    pub fn string_offset(&self) -> Offset16 {
        self.string_offset.get()
    }
}

impl FixedSize for NameRecord {
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN
        + u16::RAW_BYTE_LEN
        + u16::RAW_BYTE_LEN
        + NameId::RAW_BYTE_LEN
        + u16::RAW_BYTE_LEN
        + Offset16::RAW_BYTE_LEN;
}