Skip to main content

read_fonts/generated/
generated_name.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 Name<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.name_record_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 Name<'_> {
19    /// `name`
20    const TAG: Tag = Tag::new(b"name");
21}
22
23impl ReadArgs for Name<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Name<'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 NameFixedHeader {
45    pub version: BigEndian<u16>,
46    pub count: BigEndian<u16>,
47    pub storage_offset: BigEndian<u16>,
48}
49
50#[allow(clippy::absurd_extreme_comparisons)]
51const _: () = assert!(std::mem::size_of::<NameFixedHeader>() <= Name::MIN_SIZE);
52
53/// [Naming table version 1](https://docs.microsoft.com/en-us/typography/opentype/spec/name#naming-table-version-1)
54#[derive(Clone)]
55pub struct Name<'a> {
56    data: TableDataRef<'a, NameFixedHeader>,
57}
58
59#[allow(clippy::needless_lifetimes)]
60impl<'a> Name<'a> {
61    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
62    basic_table_impls!(impl_the_methods);
63
64    /// Table version number (0 or 1)
65    pub fn version(&self) -> u16 {
66        self.data.header().version.get()
67    }
68
69    /// Number of name records.
70    pub fn count(&self) -> u16 {
71        self.data.header().count.get()
72    }
73
74    /// Offset to start of string storage (from start of table).
75    pub fn storage_offset(&self) -> u16 {
76        self.data.header().storage_offset.get()
77    }
78
79    /// The name records where count is the number of records.
80    pub fn name_record(&self) -> &'a [NameRecord] {
81        let range = self.name_record_byte_range();
82        self.offset_data()
83            .read_array(range)
84            .ok()
85            .unwrap_or_default()
86    }
87
88    /// Number of language-tag records.
89    pub fn lang_tag_count(&self) -> Option<u16> {
90        let range = self.lang_tag_count_byte_range();
91        (!range.is_empty())
92            .then(|| self.offset_data().read_at(range.start).ok())
93            .flatten()
94    }
95
96    /// The language-tag records where langTagCount is the number of records.
97    pub fn lang_tag_record(&self) -> Option<&'a [LangTagRecord]> {
98        let range = self.lang_tag_record_byte_range();
99        (!range.is_empty())
100            .then(|| self.offset_data().read_array(range).ok())
101            .flatten()
102    }
103
104    pub fn version_byte_range(&self) -> Range<usize> {
105        let start = 0;
106        let end = start + u16::RAW_BYTE_LEN;
107        start..end
108    }
109
110    pub fn count_byte_range(&self) -> Range<usize> {
111        let start = self.version_byte_range().end;
112        let end = start + u16::RAW_BYTE_LEN;
113        start..end
114    }
115
116    pub fn storage_offset_byte_range(&self) -> Range<usize> {
117        let start = self.count_byte_range().end;
118        let end = start + u16::RAW_BYTE_LEN;
119        start..end
120    }
121
122    pub fn name_record_byte_range(&self) -> Range<usize> {
123        let count = self.count();
124        let start = self.storage_offset_byte_range().end;
125        let end = start + (transforms::to_usize(count)).saturating_mul(NameRecord::RAW_BYTE_LEN);
126        start..end
127    }
128
129    pub fn lang_tag_count_byte_range(&self) -> Range<usize> {
130        let start = self.name_record_byte_range().end;
131        let end = if self.version().compatible(1u16) {
132            start + u16::RAW_BYTE_LEN
133        } else {
134            start
135        };
136        start..end
137    }
138
139    pub fn lang_tag_record_byte_range(&self) -> Range<usize> {
140        let lang_tag_count = self.lang_tag_count().unwrap_or_default();
141        let start = self.lang_tag_count_byte_range().end;
142        let end = if self.version().compatible(1u16) {
143            start
144                + (transforms::to_usize(lang_tag_count)).saturating_mul(LangTagRecord::RAW_BYTE_LEN)
145        } else {
146            start
147        };
148        start..end
149    }
150}
151
152const _: () = assert!(FontData::default_data_long_enough(Name::MIN_SIZE));
153
154impl Default for Name<'_> {
155    fn default() -> Self {
156        Self {
157            data: TableDataRef::new(FontData::default_table_data().as_bytes())
158                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
159        }
160    }
161}
162
163/// Part of [Name]
164#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
165#[repr(C)]
166#[repr(packed)]
167pub struct LangTagRecord {
168    /// Language-tag string length (in bytes)
169    pub length: BigEndian<u16>,
170    /// Language-tag string offset from start of storage area (in
171    /// bytes).
172    pub lang_tag_offset: BigEndian<Offset16>,
173}
174
175impl LangTagRecord {
176    /// Language-tag string length (in bytes)
177    pub fn length(&self) -> u16 {
178        self.length.get()
179    }
180
181    /// Language-tag string offset from start of storage area (in
182    /// bytes).
183    pub fn lang_tag_offset(&self) -> Offset16 {
184        self.lang_tag_offset.get()
185    }
186}
187
188impl FixedSize for LangTagRecord {
189    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN;
190}
191
192///[Name Records](https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records)
193#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
194#[repr(C)]
195#[repr(packed)]
196pub struct NameRecord {
197    /// Platform ID.
198    pub platform_id: BigEndian<u16>,
199    /// Platform-specific encoding ID.
200    pub encoding_id: BigEndian<u16>,
201    /// Language ID.
202    pub language_id: BigEndian<u16>,
203    /// Name ID.
204    pub name_id: BigEndian<NameId>,
205    /// String length (in bytes).
206    pub length: BigEndian<u16>,
207    /// String offset from start of storage area (in bytes).
208    pub string_offset: BigEndian<Offset16>,
209}
210
211impl NameRecord {
212    /// Platform ID.
213    pub fn platform_id(&self) -> u16 {
214        self.platform_id.get()
215    }
216
217    /// Platform-specific encoding ID.
218    pub fn encoding_id(&self) -> u16 {
219        self.encoding_id.get()
220    }
221
222    /// Language ID.
223    pub fn language_id(&self) -> u16 {
224        self.language_id.get()
225    }
226
227    /// Name ID.
228    pub fn name_id(&self) -> NameId {
229        self.name_id.get()
230    }
231
232    /// String length (in bytes).
233    pub fn length(&self) -> u16 {
234        self.length.get()
235    }
236
237    /// String offset from start of storage area (in bytes).
238    pub fn string_offset(&self) -> Offset16 {
239        self.string_offset.get()
240    }
241}
242
243impl FixedSize for NameRecord {
244    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN
245        + u16::RAW_BYTE_LEN
246        + u16::RAW_BYTE_LEN
247        + NameId::RAW_BYTE_LEN
248        + u16::RAW_BYTE_LEN
249        + Offset16::RAW_BYTE_LEN;
250}