Coverage Report

Created: 2026-10-24 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/read-fonts-0.46.0/generated/generated_name.rs
Line
Count
Source
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)]
6
use crate::codegen_prelude::*;
7
8
impl<'a> MinByteRange<'a> for Name<'a> {
9
0
    fn min_byte_range(&self) -> Range<usize> {
10
0
        0..self.name_record_byte_range().end
11
0
    }
12
0
    fn min_table_bytes(&self) -> &'a [u8] {
13
0
        let range = self.min_byte_range();
14
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
15
0
    }
16
}
17
18
impl TopLevelTable for Name<'_> {
19
    /// `name`
20
    const TAG: Tag = Tag::new(b"name");
21
}
22
23
impl ReadArgs for Name<'_> {
24
    type Args = ();
25
}
26
27
impl<'a> FontRead<'a> for Name<'a> {
28
0
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29
        #[allow(clippy::absurd_extreme_comparisons)]
30
0
        if data.len() < Self::MIN_SIZE {
31
0
            return Err(ReadError::OutOfBounds);
32
0
        }
33
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
34
0
            return Err(ReadError::OutOfBounds);
35
        };
36
0
        Ok(Self { data })
37
0
    }
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)]
44
pub 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)]
51
const _: () = 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)]
55
pub struct Name<'a> {
56
    data: TableDataRef<'a, NameFixedHeader>,
57
}
58
59
#[allow(clippy::needless_lifetimes)]
60
impl<'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
0
    pub fn version(&self) -> u16 {
66
0
        self.data.header().version.get()
67
0
    }
68
69
    /// Number of name records.
70
0
    pub fn count(&self) -> u16 {
71
0
        self.data.header().count.get()
72
0
    }
73
74
    /// Offset to start of string storage (from start of table).
75
0
    pub fn storage_offset(&self) -> u16 {
76
0
        self.data.header().storage_offset.get()
77
0
    }
78
79
    /// The name records where count is the number of records.
80
0
    pub fn name_record(&self) -> &'a [NameRecord] {
81
0
        let range = self.name_record_byte_range();
82
0
        self.offset_data()
83
0
            .read_array(range)
84
0
            .ok()
85
0
            .unwrap_or_default()
86
0
    }
87
88
    /// Number of language-tag records.
89
0
    pub fn lang_tag_count(&self) -> Option<u16> {
90
0
        let range = self.lang_tag_count_byte_range();
91
0
        (!range.is_empty())
92
0
            .then(|| self.offset_data().read_at(range.start).ok())
93
0
            .flatten()
94
0
    }
95
96
    /// The language-tag records where langTagCount is the number of records.
97
0
    pub fn lang_tag_record(&self) -> Option<&'a [LangTagRecord]> {
98
0
        let range = self.lang_tag_record_byte_range();
99
0
        (!range.is_empty())
100
0
            .then(|| self.offset_data().read_array(range).ok())
101
0
            .flatten()
102
0
    }
103
104
0
    pub fn version_byte_range(&self) -> Range<usize> {
105
0
        let start = 0;
106
0
        let end = start + u16::RAW_BYTE_LEN;
107
0
        start..end
108
0
    }
109
110
0
    pub fn count_byte_range(&self) -> Range<usize> {
111
0
        let start = self.version_byte_range().end;
112
0
        let end = start + u16::RAW_BYTE_LEN;
113
0
        start..end
114
0
    }
115
116
0
    pub fn storage_offset_byte_range(&self) -> Range<usize> {
117
0
        let start = self.count_byte_range().end;
118
0
        let end = start + u16::RAW_BYTE_LEN;
119
0
        start..end
120
0
    }
121
122
0
    pub fn name_record_byte_range(&self) -> Range<usize> {
123
0
        let count = self.count();
124
0
        let start = self.storage_offset_byte_range().end;
125
0
        let end = start + (transforms::to_usize(count)).saturating_mul(NameRecord::RAW_BYTE_LEN);
126
0
        start..end
127
0
    }
128
129
0
    pub fn lang_tag_count_byte_range(&self) -> Range<usize> {
130
0
        let start = self.name_record_byte_range().end;
131
0
        let end = if self.version().compatible(1u16) {
132
0
            start + u16::RAW_BYTE_LEN
133
        } else {
134
0
            start
135
        };
136
0
        start..end
137
0
    }
138
139
0
    pub fn lang_tag_record_byte_range(&self) -> Range<usize> {
140
0
        let lang_tag_count = self.lang_tag_count().unwrap_or_default();
141
0
        let start = self.lang_tag_count_byte_range().end;
142
0
        let end = if self.version().compatible(1u16) {
143
0
            start
144
0
                + (transforms::to_usize(lang_tag_count)).saturating_mul(LangTagRecord::RAW_BYTE_LEN)
145
        } else {
146
0
            start
147
        };
148
0
        start..end
149
0
    }
150
}
151
152
const _: () = assert!(FontData::default_data_long_enough(Name::MIN_SIZE));
153
154
impl Default for Name<'_> {
155
0
    fn default() -> Self {
156
        Self {
157
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
158
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
159
        }
160
0
    }
161
}
162
163
/// Part of [Name]
164
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
165
#[repr(C)]
166
#[repr(packed)]
167
pub 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
175
impl LangTagRecord {
176
    /// Language-tag string length (in bytes)
177
0
    pub fn length(&self) -> u16 {
178
0
        self.length.get()
179
0
    }
180
181
    /// Language-tag string offset from start of storage area (in
182
    /// bytes).
183
0
    pub fn lang_tag_offset(&self) -> Offset16 {
184
0
        self.lang_tag_offset.get()
185
0
    }
186
}
187
188
impl 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)]
196
pub 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
211
impl NameRecord {
212
    /// Platform ID.
213
0
    pub fn platform_id(&self) -> u16 {
214
0
        self.platform_id.get()
215
0
    }
216
217
    /// Platform-specific encoding ID.
218
0
    pub fn encoding_id(&self) -> u16 {
219
0
        self.encoding_id.get()
220
0
    }
221
222
    /// Language ID.
223
0
    pub fn language_id(&self) -> u16 {
224
0
        self.language_id.get()
225
0
    }
226
227
    /// Name ID.
228
0
    pub fn name_id(&self) -> NameId {
229
0
        self.name_id.get()
230
0
    }
231
232
    /// String length (in bytes).
233
0
    pub fn length(&self) -> u16 {
234
0
        self.length.get()
235
0
    }
236
237
    /// String offset from start of storage area (in bytes).
238
0
    pub fn string_offset(&self) -> Offset16 {
239
0
        self.string_offset.get()
240
0
    }
241
}
242
243
impl 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
}