Skip to main content

read_fonts/generated/
generated_ltag.rs

1// THIS FILE IS AUTOGENERATED.
2// Any changes to this file will be overwritten.
3// For more information about how codegen works, see font-codegen/README.md
4
5#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8impl<'a> MinByteRange<'a> for Ltag<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.tag_ranges_byte_range().end
11    }
12    fn min_table_bytes(&self) -> &'a [u8] {
13        let range = self.min_byte_range();
14        self.offset_data().as_bytes().get(range).unwrap_or_default()
15    }
16}
17
18impl TopLevelTable for Ltag<'_> {
19    /// `ltag`
20    const TAG: Tag = Tag::new(b"ltag");
21}
22
23impl ReadArgs for Ltag<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Ltag<'a> {
28    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29        #[allow(clippy::absurd_extreme_comparisons)]
30        if data.len() < Self::MIN_SIZE {
31            return Err(ReadError::OutOfBounds);
32        }
33        let Some(data) = TableDataRef::new(data.as_bytes()) else {
34            return Err(ReadError::OutOfBounds);
35        };
36        Ok(Self { data })
37    }
38}
39
40/// The fields at the start of the table, which are read directly.
41#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
42#[repr(C, packed)]
43#[doc(hidden)]
44pub struct LtagFixedHeader {
45    pub version: BigEndian<u32>,
46    pub flags: BigEndian<u32>,
47    pub num_tags: BigEndian<u32>,
48}
49
50#[allow(clippy::absurd_extreme_comparisons)]
51const _: () = assert!(std::mem::size_of::<LtagFixedHeader>() <= Ltag::MIN_SIZE);
52
53/// The [language tag](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6ltag.html) table.
54#[derive(Clone)]
55pub struct Ltag<'a> {
56    data: TableDataRef<'a, LtagFixedHeader>,
57}
58
59#[allow(clippy::needless_lifetimes)]
60impl<'a> Ltag<'a> {
61    pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
62    basic_table_impls!(impl_the_methods);
63
64    /// Table version; currently 1.
65    pub fn version(&self) -> u32 {
66        self.data.header().version.get()
67    }
68
69    /// Table flags; currently none defined.
70    pub fn flags(&self) -> u32 {
71        self.data.header().flags.get()
72    }
73
74    /// Number of language tags which follow.
75    pub fn num_tags(&self) -> u32 {
76        self.data.header().num_tags.get()
77    }
78
79    /// Range of each tag's string.
80    pub fn tag_ranges(&self) -> &'a [FTStringRange] {
81        let range = self.tag_ranges_byte_range();
82        self.offset_data()
83            .read_array(range)
84            .ok()
85            .unwrap_or_default()
86    }
87
88    pub fn version_byte_range(&self) -> Range<usize> {
89        let start = 0;
90        let end = start + u32::RAW_BYTE_LEN;
91        start..end
92    }
93
94    pub fn flags_byte_range(&self) -> Range<usize> {
95        let start = self.version_byte_range().end;
96        let end = start + u32::RAW_BYTE_LEN;
97        start..end
98    }
99
100    pub fn num_tags_byte_range(&self) -> Range<usize> {
101        let start = self.flags_byte_range().end;
102        let end = start + u32::RAW_BYTE_LEN;
103        start..end
104    }
105
106    pub fn tag_ranges_byte_range(&self) -> Range<usize> {
107        let num_tags = self.num_tags();
108        let start = self.num_tags_byte_range().end;
109        let end =
110            start + (transforms::to_usize(num_tags)).saturating_mul(FTStringRange::RAW_BYTE_LEN);
111        start..end
112    }
113}
114
115const _: () = assert!(FontData::default_data_long_enough(Ltag::MIN_SIZE));
116
117impl Default for Ltag<'_> {
118    fn default() -> Self {
119        Self {
120            data: TableDataRef::new(FontData::default_table_data().as_bytes())
121                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
122        }
123    }
124}
125
126/// Offset and length of string in `ltag` table.
127#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
128#[repr(C)]
129#[repr(packed)]
130pub struct FTStringRange {
131    /// Offset from the start of the table to the beginning of the string.
132    pub offset: BigEndian<u16>,
133    /// String length (in bytes).
134    pub length: BigEndian<u16>,
135}
136
137impl FTStringRange {
138    /// Offset from the start of the table to the beginning of the string.
139    pub fn offset(&self) -> u16 {
140        self.offset.get()
141    }
142
143    /// String length (in bytes).
144    pub fn length(&self) -> u16 {
145        self.length.get()
146    }
147}
148
149impl FixedSize for FTStringRange {
150    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
151}