/rust/registry/src/index.crates.io-1949cf8c6b5b557f/read-fonts-0.47.0/generated/generated_base.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 Base<'a> { |
9 | 0 | fn min_byte_range(&self) -> Range<usize> { |
10 | 0 | 0..self.vert_axis_offset_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 Base<'_> { |
19 | | /// `BASE` |
20 | | const TAG: Tag = Tag::new(b"BASE"); |
21 | | } |
22 | | |
23 | | impl ReadArgs for Base<'_> { |
24 | | type Args = (); |
25 | | } |
26 | | |
27 | | impl<'a> FontRead<'a> for Base<'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 BaseFixedHeader { |
45 | | pub version: BigEndian<MajorMinor>, |
46 | | pub horiz_axis_offset: BigEndian<Nullable<Offset16>>, |
47 | | pub vert_axis_offset: BigEndian<Nullable<Offset16>>, |
48 | | } |
49 | | |
50 | | #[allow(clippy::absurd_extreme_comparisons)] |
51 | | const _: () = assert!(std::mem::size_of::<BaseFixedHeader>() <= Base::MIN_SIZE); |
52 | | |
53 | | /// The [BASE](https://learn.microsoft.com/en-us/typography/opentype/spec/base) (Baseline) table |
54 | | #[derive(Clone)] |
55 | | pub struct Base<'a> { |
56 | | data: TableDataRef<'a, BaseFixedHeader>, |
57 | | } |
58 | | |
59 | | #[allow(clippy::needless_lifetimes)] |
60 | | impl<'a> Base<'a> { |
61 | | pub const MIN_SIZE: usize = |
62 | | (MajorMinor::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN); |
63 | | basic_table_impls!(impl_the_methods); |
64 | | |
65 | | /// (major, minor) Version for the BASE table (1,0) or (1,1) |
66 | 0 | pub fn version(&self) -> MajorMinor { |
67 | 0 | self.data.header().version.get() |
68 | 0 | } |
69 | | |
70 | | /// Offset to horizontal Axis table, from beginning of BASE table (may be NULL) |
71 | 0 | pub fn horiz_axis_offset(&self) -> Nullable<Offset16> { |
72 | 0 | self.data.header().horiz_axis_offset.get() |
73 | 0 | } |
74 | | |
75 | | /// Attempt to resolve [`horiz_axis_offset`][Self::horiz_axis_offset]. |
76 | 0 | pub fn horiz_axis(&self) -> Option<Result<Axis<'a>, ReadError>> { |
77 | 0 | let data = self.offset_data(); |
78 | 0 | self.horiz_axis_offset().resolve(data) |
79 | 0 | } |
80 | | |
81 | | /// Offset to vertical Axis table, from beginning of BASE table (may be NULL) |
82 | 0 | pub fn vert_axis_offset(&self) -> Nullable<Offset16> { |
83 | 0 | self.data.header().vert_axis_offset.get() |
84 | 0 | } |
85 | | |
86 | | /// Attempt to resolve [`vert_axis_offset`][Self::vert_axis_offset]. |
87 | 0 | pub fn vert_axis(&self) -> Option<Result<Axis<'a>, ReadError>> { |
88 | 0 | let data = self.offset_data(); |
89 | 0 | self.vert_axis_offset().resolve(data) |
90 | 0 | } |
91 | | |
92 | | /// Offset to Item Variation Store table, from beginning of BASE table (may be null) |
93 | 0 | pub fn item_var_store_offset(&self) -> Option<Nullable<Offset32>> { |
94 | 0 | let range = self.item_var_store_offset_byte_range(); |
95 | 0 | (!range.is_empty()) |
96 | 0 | .then(|| self.offset_data().read_at(range.start).ok()) |
97 | 0 | .flatten() |
98 | 0 | } |
99 | | |
100 | | /// Attempt to resolve [`item_var_store_offset`][Self::item_var_store_offset]. |
101 | 0 | pub fn item_var_store(&self) -> Option<Result<ItemVariationStore<'a>, ReadError>> { |
102 | 0 | let data = self.offset_data(); |
103 | 0 | self.item_var_store_offset().map(|x| x.resolve(data))? |
104 | 0 | } |
105 | | |
106 | 0 | pub fn version_byte_range(&self) -> Range<usize> { |
107 | 0 | let start = 0usize; |
108 | 0 | let end = start.saturating_add(MajorMinor::RAW_BYTE_LEN); |
109 | 0 | start..end |
110 | 0 | } |
111 | | |
112 | 0 | pub fn horiz_axis_offset_byte_range(&self) -> Range<usize> { |
113 | 0 | let start = self.version_byte_range().end; |
114 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
115 | 0 | start..end |
116 | 0 | } |
117 | | |
118 | 0 | pub fn vert_axis_offset_byte_range(&self) -> Range<usize> { |
119 | 0 | let start = self.horiz_axis_offset_byte_range().end; |
120 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
121 | 0 | start..end |
122 | 0 | } |
123 | | |
124 | 0 | pub fn item_var_store_offset_byte_range(&self) -> Range<usize> { |
125 | 0 | let start = self.vert_axis_offset_byte_range().end; |
126 | 0 | let end = if self.version().compatible((1u16, 1u16)) { |
127 | 0 | start.saturating_add(Offset32::RAW_BYTE_LEN) |
128 | | } else { |
129 | 0 | start |
130 | | }; |
131 | 0 | start..end |
132 | 0 | } |
133 | | } |
134 | | |
135 | | const _: () = assert!(FontData::default_data_long_enough(Base::MIN_SIZE)); |
136 | | |
137 | | impl Default for Base<'_> { |
138 | 0 | fn default() -> Self { |
139 | | Self { |
140 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
141 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
142 | | } |
143 | 0 | } |
144 | | } |
145 | | |
146 | | impl<'a> MinByteRange<'a> for Axis<'a> { |
147 | 0 | fn min_byte_range(&self) -> Range<usize> { |
148 | 0 | 0..self.base_script_list_offset_byte_range().end |
149 | 0 | } |
150 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
151 | 0 | let range = self.min_byte_range(); |
152 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
153 | 0 | } |
154 | | } |
155 | | |
156 | | impl ReadArgs for Axis<'_> { |
157 | | type Args = (); |
158 | | } |
159 | | |
160 | | impl<'a> FontRead<'a> for Axis<'a> { |
161 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
162 | | #[allow(clippy::absurd_extreme_comparisons)] |
163 | 0 | if data.len() < Self::MIN_SIZE { |
164 | 0 | return Err(ReadError::OutOfBounds); |
165 | 0 | } |
166 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
167 | 0 | return Err(ReadError::OutOfBounds); |
168 | | }; |
169 | 0 | Ok(Self { data }) |
170 | 0 | } |
171 | | } |
172 | | |
173 | | /// The fields at the start of the table, which are read directly. |
174 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
175 | | #[repr(C, packed)] |
176 | | #[doc(hidden)] |
177 | | pub struct AxisFixedHeader { |
178 | | pub base_tag_list_offset: BigEndian<Nullable<Offset16>>, |
179 | | pub base_script_list_offset: BigEndian<Offset16>, |
180 | | } |
181 | | |
182 | | #[allow(clippy::absurd_extreme_comparisons)] |
183 | | const _: () = assert!(std::mem::size_of::<AxisFixedHeader>() <= Axis::MIN_SIZE); |
184 | | |
185 | | /// [Axis Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#axis-tables-horizaxis-and-vertaxis) |
186 | | #[derive(Clone)] |
187 | | pub struct Axis<'a> { |
188 | | data: TableDataRef<'a, AxisFixedHeader>, |
189 | | } |
190 | | |
191 | | #[allow(clippy::needless_lifetimes)] |
192 | | impl<'a> Axis<'a> { |
193 | | pub const MIN_SIZE: usize = (Offset16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN); |
194 | | basic_table_impls!(impl_the_methods); |
195 | | |
196 | | /// Offset to BaseTagList table, from beginning of Axis table (may |
197 | | /// be NULL) |
198 | 0 | pub fn base_tag_list_offset(&self) -> Nullable<Offset16> { |
199 | 0 | self.data.header().base_tag_list_offset.get() |
200 | 0 | } |
201 | | |
202 | | /// Attempt to resolve [`base_tag_list_offset`][Self::base_tag_list_offset]. |
203 | 0 | pub fn base_tag_list(&self) -> Option<Result<BaseTagList<'a>, ReadError>> { |
204 | 0 | let data = self.offset_data(); |
205 | 0 | self.base_tag_list_offset().resolve(data) |
206 | 0 | } |
207 | | |
208 | | /// Offset to BaseScriptList table, from beginning of Axis table |
209 | 0 | pub fn base_script_list_offset(&self) -> Offset16 { |
210 | 0 | self.data.header().base_script_list_offset.get() |
211 | 0 | } |
212 | | |
213 | | /// Attempt to resolve [`base_script_list_offset`][Self::base_script_list_offset]. |
214 | 0 | pub fn base_script_list(&self) -> Result<BaseScriptList<'a>, ReadError> { |
215 | 0 | let data = self.offset_data(); |
216 | 0 | self.base_script_list_offset().resolve(data) |
217 | 0 | } |
218 | | |
219 | 0 | pub fn base_tag_list_offset_byte_range(&self) -> Range<usize> { |
220 | 0 | let start = 0usize; |
221 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
222 | 0 | start..end |
223 | 0 | } |
224 | | |
225 | 0 | pub fn base_script_list_offset_byte_range(&self) -> Range<usize> { |
226 | 0 | let start = self.base_tag_list_offset_byte_range().end; |
227 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
228 | 0 | start..end |
229 | 0 | } |
230 | | } |
231 | | |
232 | | const _: () = assert!(FontData::default_data_long_enough(Axis::MIN_SIZE)); |
233 | | |
234 | | impl Default for Axis<'_> { |
235 | 0 | fn default() -> Self { |
236 | | Self { |
237 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
238 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
239 | | } |
240 | 0 | } |
241 | | } |
242 | | |
243 | | impl<'a> MinByteRange<'a> for BaseTagList<'a> { |
244 | 0 | fn min_byte_range(&self) -> Range<usize> { |
245 | 0 | 0..self.baseline_tags_byte_range().end |
246 | 0 | } |
247 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
248 | 0 | let range = self.min_byte_range(); |
249 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
250 | 0 | } |
251 | | } |
252 | | |
253 | | impl ReadArgs for BaseTagList<'_> { |
254 | | type Args = (); |
255 | | } |
256 | | |
257 | | impl<'a> FontRead<'a> for BaseTagList<'a> { |
258 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
259 | | #[allow(clippy::absurd_extreme_comparisons)] |
260 | 0 | if data.len() < Self::MIN_SIZE { |
261 | 0 | return Err(ReadError::OutOfBounds); |
262 | 0 | } |
263 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
264 | 0 | return Err(ReadError::OutOfBounds); |
265 | | }; |
266 | 0 | Ok(Self { data }) |
267 | 0 | } |
268 | | } |
269 | | |
270 | | /// The fields at the start of the table, which are read directly. |
271 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
272 | | #[repr(C, packed)] |
273 | | #[doc(hidden)] |
274 | | pub struct BaseTagListFixedHeader { |
275 | | pub base_tag_count: BigEndian<u16>, |
276 | | } |
277 | | |
278 | | #[allow(clippy::absurd_extreme_comparisons)] |
279 | | const _: () = assert!(std::mem::size_of::<BaseTagListFixedHeader>() <= BaseTagList::MIN_SIZE); |
280 | | |
281 | | /// [BaseTagList Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basetaglist-table) |
282 | | #[derive(Clone)] |
283 | | pub struct BaseTagList<'a> { |
284 | | data: TableDataRef<'a, BaseTagListFixedHeader>, |
285 | | } |
286 | | |
287 | | #[allow(clippy::needless_lifetimes)] |
288 | | impl<'a> BaseTagList<'a> { |
289 | | pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN; |
290 | | basic_table_impls!(impl_the_methods); |
291 | | |
292 | | /// Number of baseline identification tags in this text direction |
293 | | /// — may be zero (0) |
294 | 0 | pub fn base_tag_count(&self) -> u16 { |
295 | 0 | self.data.header().base_tag_count.get() |
296 | 0 | } |
297 | | |
298 | | /// Array of 4-byte baseline identification tags — must be in |
299 | | /// alphabetical order |
300 | 0 | pub fn baseline_tags(&self) -> &'a [BigEndian<Tag>] { |
301 | 0 | let range = self.baseline_tags_byte_range(); |
302 | 0 | self.offset_data() |
303 | 0 | .read_array(range) |
304 | 0 | .ok() |
305 | 0 | .unwrap_or_default() |
306 | 0 | } |
307 | | |
308 | 0 | pub fn base_tag_count_byte_range(&self) -> Range<usize> { |
309 | 0 | let start = 0usize; |
310 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
311 | 0 | start..end |
312 | 0 | } |
313 | | |
314 | 0 | pub fn baseline_tags_byte_range(&self) -> Range<usize> { |
315 | 0 | let base_tag_count = self.base_tag_count(); |
316 | 0 | let start = self.base_tag_count_byte_range().end; |
317 | 0 | let end = start.saturating_add( |
318 | 0 | (transforms::to_usize(base_tag_count)).saturating_mul(Tag::RAW_BYTE_LEN), |
319 | | ); |
320 | 0 | start..end |
321 | 0 | } |
322 | | } |
323 | | |
324 | | const _: () = assert!(FontData::default_data_long_enough(BaseTagList::MIN_SIZE)); |
325 | | |
326 | | impl Default for BaseTagList<'_> { |
327 | 0 | fn default() -> Self { |
328 | | Self { |
329 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
330 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
331 | | } |
332 | 0 | } |
333 | | } |
334 | | |
335 | | impl<'a> MinByteRange<'a> for BaseScriptList<'a> { |
336 | 0 | fn min_byte_range(&self) -> Range<usize> { |
337 | 0 | 0..self.base_script_records_byte_range().end |
338 | 0 | } |
339 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
340 | 0 | let range = self.min_byte_range(); |
341 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
342 | 0 | } |
343 | | } |
344 | | |
345 | | impl ReadArgs for BaseScriptList<'_> { |
346 | | type Args = (); |
347 | | } |
348 | | |
349 | | impl<'a> FontRead<'a> for BaseScriptList<'a> { |
350 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
351 | | #[allow(clippy::absurd_extreme_comparisons)] |
352 | 0 | if data.len() < Self::MIN_SIZE { |
353 | 0 | return Err(ReadError::OutOfBounds); |
354 | 0 | } |
355 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
356 | 0 | return Err(ReadError::OutOfBounds); |
357 | | }; |
358 | 0 | Ok(Self { data }) |
359 | 0 | } |
360 | | } |
361 | | |
362 | | /// The fields at the start of the table, which are read directly. |
363 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
364 | | #[repr(C, packed)] |
365 | | #[doc(hidden)] |
366 | | pub struct BaseScriptListFixedHeader { |
367 | | pub base_script_count: BigEndian<u16>, |
368 | | } |
369 | | |
370 | | #[allow(clippy::absurd_extreme_comparisons)] |
371 | | const _: () = assert!(std::mem::size_of::<BaseScriptListFixedHeader>() <= BaseScriptList::MIN_SIZE); |
372 | | |
373 | | /// [BaseScriptList Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basescriptlist-table) |
374 | | #[derive(Clone)] |
375 | | pub struct BaseScriptList<'a> { |
376 | | data: TableDataRef<'a, BaseScriptListFixedHeader>, |
377 | | } |
378 | | |
379 | | #[allow(clippy::needless_lifetimes)] |
380 | | impl<'a> BaseScriptList<'a> { |
381 | | pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN; |
382 | | basic_table_impls!(impl_the_methods); |
383 | | |
384 | | /// Number of BaseScriptRecords defined |
385 | 0 | pub fn base_script_count(&self) -> u16 { |
386 | 0 | self.data.header().base_script_count.get() |
387 | 0 | } |
388 | | |
389 | | /// Array of BaseScriptRecords, in alphabetical order by |
390 | | /// baseScriptTag |
391 | 0 | pub fn base_script_records(&self) -> &'a [BaseScriptRecord] { |
392 | 0 | let range = self.base_script_records_byte_range(); |
393 | 0 | self.offset_data() |
394 | 0 | .read_array(range) |
395 | 0 | .ok() |
396 | 0 | .unwrap_or_default() |
397 | 0 | } |
398 | | |
399 | 0 | pub fn base_script_count_byte_range(&self) -> Range<usize> { |
400 | 0 | let start = 0usize; |
401 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
402 | 0 | start..end |
403 | 0 | } |
404 | | |
405 | 0 | pub fn base_script_records_byte_range(&self) -> Range<usize> { |
406 | 0 | let base_script_count = self.base_script_count(); |
407 | 0 | let start = self.base_script_count_byte_range().end; |
408 | 0 | let end = start.saturating_add( |
409 | 0 | (transforms::to_usize(base_script_count)) |
410 | 0 | .saturating_mul(BaseScriptRecord::RAW_BYTE_LEN), |
411 | | ); |
412 | 0 | start..end |
413 | 0 | } |
414 | | } |
415 | | |
416 | | const _: () = assert!(FontData::default_data_long_enough(BaseScriptList::MIN_SIZE)); |
417 | | |
418 | | impl Default for BaseScriptList<'_> { |
419 | 0 | fn default() -> Self { |
420 | | Self { |
421 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
422 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
423 | | } |
424 | 0 | } |
425 | | } |
426 | | |
427 | | /// [BaseScriptRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basescriptrecord) |
428 | | #[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)] |
429 | | #[repr(C)] |
430 | | #[repr(packed)] |
431 | | pub struct BaseScriptRecord { |
432 | | /// 4-byte script identification tag |
433 | | pub base_script_tag: BigEndian<Tag>, |
434 | | /// Offset to BaseScript table, from beginning of BaseScriptList |
435 | | pub base_script_offset: BigEndian<Offset16>, |
436 | | } |
437 | | |
438 | | impl BaseScriptRecord { |
439 | | /// 4-byte script identification tag |
440 | 0 | pub fn base_script_tag(&self) -> Tag { |
441 | 0 | self.base_script_tag.get() |
442 | 0 | } |
443 | | |
444 | | /// Offset to BaseScript table, from beginning of BaseScriptList |
445 | 0 | pub fn base_script_offset(&self) -> Offset16 { |
446 | 0 | self.base_script_offset.get() |
447 | 0 | } |
448 | | |
449 | | /// Offset to BaseScript table, from beginning of BaseScriptList |
450 | | /// |
451 | | /// The `data` argument should be retrieved from the parent table |
452 | | /// By calling its `offset_data` method. |
453 | 0 | pub fn base_script<'a>(&self, data: FontData<'a>) -> Result<BaseScript<'a>, ReadError> { |
454 | 0 | self.base_script_offset().resolve(data) |
455 | 0 | } |
456 | | } |
457 | | |
458 | | impl FixedSize for BaseScriptRecord { |
459 | | const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN; |
460 | | } |
461 | | |
462 | | impl<'a> MinByteRange<'a> for BaseScript<'a> { |
463 | 0 | fn min_byte_range(&self) -> Range<usize> { |
464 | 0 | 0..self.base_lang_sys_records_byte_range().end |
465 | 0 | } |
466 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
467 | 0 | let range = self.min_byte_range(); |
468 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
469 | 0 | } |
470 | | } |
471 | | |
472 | | impl ReadArgs for BaseScript<'_> { |
473 | | type Args = (); |
474 | | } |
475 | | |
476 | | impl<'a> FontRead<'a> for BaseScript<'a> { |
477 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
478 | | #[allow(clippy::absurd_extreme_comparisons)] |
479 | 0 | if data.len() < Self::MIN_SIZE { |
480 | 0 | return Err(ReadError::OutOfBounds); |
481 | 0 | } |
482 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
483 | 0 | return Err(ReadError::OutOfBounds); |
484 | | }; |
485 | 0 | Ok(Self { data }) |
486 | 0 | } |
487 | | } |
488 | | |
489 | | /// The fields at the start of the table, which are read directly. |
490 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
491 | | #[repr(C, packed)] |
492 | | #[doc(hidden)] |
493 | | pub struct BaseScriptFixedHeader { |
494 | | pub base_values_offset: BigEndian<Nullable<Offset16>>, |
495 | | pub default_min_max_offset: BigEndian<Nullable<Offset16>>, |
496 | | pub base_lang_sys_count: BigEndian<u16>, |
497 | | } |
498 | | |
499 | | #[allow(clippy::absurd_extreme_comparisons)] |
500 | | const _: () = assert!(std::mem::size_of::<BaseScriptFixedHeader>() <= BaseScript::MIN_SIZE); |
501 | | |
502 | | /// [BaseScript Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basescript-table) |
503 | | #[derive(Clone)] |
504 | | pub struct BaseScript<'a> { |
505 | | data: TableDataRef<'a, BaseScriptFixedHeader>, |
506 | | } |
507 | | |
508 | | #[allow(clippy::needless_lifetimes)] |
509 | | impl<'a> BaseScript<'a> { |
510 | | pub const MIN_SIZE: usize = |
511 | | (Offset16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
512 | | basic_table_impls!(impl_the_methods); |
513 | | |
514 | | /// Offset to BaseValues table, from beginning of BaseScript table (may be NULL) |
515 | 0 | pub fn base_values_offset(&self) -> Nullable<Offset16> { |
516 | 0 | self.data.header().base_values_offset.get() |
517 | 0 | } |
518 | | |
519 | | /// Attempt to resolve [`base_values_offset`][Self::base_values_offset]. |
520 | 0 | pub fn base_values(&self) -> Option<Result<BaseValues<'a>, ReadError>> { |
521 | 0 | let data = self.offset_data(); |
522 | 0 | self.base_values_offset().resolve(data) |
523 | 0 | } |
524 | | |
525 | | /// Offset to MinMax table, from beginning of BaseScript table (may be NULL) |
526 | 0 | pub fn default_min_max_offset(&self) -> Nullable<Offset16> { |
527 | 0 | self.data.header().default_min_max_offset.get() |
528 | 0 | } |
529 | | |
530 | | /// Attempt to resolve [`default_min_max_offset`][Self::default_min_max_offset]. |
531 | 0 | pub fn default_min_max(&self) -> Option<Result<MinMax<'a>, ReadError>> { |
532 | 0 | let data = self.offset_data(); |
533 | 0 | self.default_min_max_offset().resolve(data) |
534 | 0 | } |
535 | | |
536 | | /// Number of BaseLangSysRecords defined — may be zero (0) |
537 | 0 | pub fn base_lang_sys_count(&self) -> u16 { |
538 | 0 | self.data.header().base_lang_sys_count.get() |
539 | 0 | } |
540 | | |
541 | | /// Array of BaseLangSysRecords, in alphabetical order by |
542 | | /// BaseLangSysTag |
543 | 0 | pub fn base_lang_sys_records(&self) -> &'a [BaseLangSysRecord] { |
544 | 0 | let range = self.base_lang_sys_records_byte_range(); |
545 | 0 | self.offset_data() |
546 | 0 | .read_array(range) |
547 | 0 | .ok() |
548 | 0 | .unwrap_or_default() |
549 | 0 | } |
550 | | |
551 | 0 | pub fn base_values_offset_byte_range(&self) -> Range<usize> { |
552 | 0 | let start = 0usize; |
553 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
554 | 0 | start..end |
555 | 0 | } |
556 | | |
557 | 0 | pub fn default_min_max_offset_byte_range(&self) -> Range<usize> { |
558 | 0 | let start = self.base_values_offset_byte_range().end; |
559 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
560 | 0 | start..end |
561 | 0 | } |
562 | | |
563 | 0 | pub fn base_lang_sys_count_byte_range(&self) -> Range<usize> { |
564 | 0 | let start = self.default_min_max_offset_byte_range().end; |
565 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
566 | 0 | start..end |
567 | 0 | } |
568 | | |
569 | 0 | pub fn base_lang_sys_records_byte_range(&self) -> Range<usize> { |
570 | 0 | let base_lang_sys_count = self.base_lang_sys_count(); |
571 | 0 | let start = self.base_lang_sys_count_byte_range().end; |
572 | 0 | let end = start.saturating_add( |
573 | 0 | (transforms::to_usize(base_lang_sys_count)) |
574 | 0 | .saturating_mul(BaseLangSysRecord::RAW_BYTE_LEN), |
575 | | ); |
576 | 0 | start..end |
577 | 0 | } |
578 | | } |
579 | | |
580 | | const _: () = assert!(FontData::default_data_long_enough(BaseScript::MIN_SIZE)); |
581 | | |
582 | | impl Default for BaseScript<'_> { |
583 | 0 | fn default() -> Self { |
584 | | Self { |
585 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
586 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
587 | | } |
588 | 0 | } |
589 | | } |
590 | | |
591 | | /// [BaseLangSysRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/base#baselangsysrecord) |
592 | | #[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)] |
593 | | #[repr(C)] |
594 | | #[repr(packed)] |
595 | | pub struct BaseLangSysRecord { |
596 | | /// 4-byte language system identification tag |
597 | | pub base_lang_sys_tag: BigEndian<Tag>, |
598 | | /// Offset to MinMax table, from beginning of BaseScript table |
599 | | pub min_max_offset: BigEndian<Offset16>, |
600 | | } |
601 | | |
602 | | impl BaseLangSysRecord { |
603 | | /// 4-byte language system identification tag |
604 | 0 | pub fn base_lang_sys_tag(&self) -> Tag { |
605 | 0 | self.base_lang_sys_tag.get() |
606 | 0 | } |
607 | | |
608 | | /// Offset to MinMax table, from beginning of BaseScript table |
609 | 0 | pub fn min_max_offset(&self) -> Offset16 { |
610 | 0 | self.min_max_offset.get() |
611 | 0 | } |
612 | | |
613 | | /// Offset to MinMax table, from beginning of BaseScript table |
614 | | /// |
615 | | /// The `data` argument should be retrieved from the parent table |
616 | | /// By calling its `offset_data` method. |
617 | 0 | pub fn min_max<'a>(&self, data: FontData<'a>) -> Result<MinMax<'a>, ReadError> { |
618 | 0 | self.min_max_offset().resolve(data) |
619 | 0 | } |
620 | | } |
621 | | |
622 | | impl FixedSize for BaseLangSysRecord { |
623 | | const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN; |
624 | | } |
625 | | |
626 | | impl<'a> MinByteRange<'a> for BaseValues<'a> { |
627 | 0 | fn min_byte_range(&self) -> Range<usize> { |
628 | 0 | 0..self.base_coord_offsets_byte_range().end |
629 | 0 | } |
630 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
631 | 0 | let range = self.min_byte_range(); |
632 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
633 | 0 | } |
634 | | } |
635 | | |
636 | | impl ReadArgs for BaseValues<'_> { |
637 | | type Args = (); |
638 | | } |
639 | | |
640 | | impl<'a> FontRead<'a> for BaseValues<'a> { |
641 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
642 | | #[allow(clippy::absurd_extreme_comparisons)] |
643 | 0 | if data.len() < Self::MIN_SIZE { |
644 | 0 | return Err(ReadError::OutOfBounds); |
645 | 0 | } |
646 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
647 | 0 | return Err(ReadError::OutOfBounds); |
648 | | }; |
649 | 0 | Ok(Self { data }) |
650 | 0 | } |
651 | | } |
652 | | |
653 | | /// The fields at the start of the table, which are read directly. |
654 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
655 | | #[repr(C, packed)] |
656 | | #[doc(hidden)] |
657 | | pub struct BaseValuesFixedHeader { |
658 | | pub default_baseline_index: BigEndian<u16>, |
659 | | pub base_coord_count: BigEndian<u16>, |
660 | | } |
661 | | |
662 | | #[allow(clippy::absurd_extreme_comparisons)] |
663 | | const _: () = assert!(std::mem::size_of::<BaseValuesFixedHeader>() <= BaseValues::MIN_SIZE); |
664 | | |
665 | | /// [BaseValues](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basevalues-table) table |
666 | | #[derive(Clone)] |
667 | | pub struct BaseValues<'a> { |
668 | | data: TableDataRef<'a, BaseValuesFixedHeader>, |
669 | | } |
670 | | |
671 | | #[allow(clippy::needless_lifetimes)] |
672 | | impl<'a> BaseValues<'a> { |
673 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
674 | | basic_table_impls!(impl_the_methods); |
675 | | |
676 | | /// Index number of default baseline for this script — equals |
677 | | /// index position of baseline tag in baselineTags array of the |
678 | | /// BaseTagList |
679 | 0 | pub fn default_baseline_index(&self) -> u16 { |
680 | 0 | self.data.header().default_baseline_index.get() |
681 | 0 | } |
682 | | |
683 | | /// Number of BaseCoord tables defined — should equal |
684 | | /// baseTagCount in the BaseTagList |
685 | 0 | pub fn base_coord_count(&self) -> u16 { |
686 | 0 | self.data.header().base_coord_count.get() |
687 | 0 | } |
688 | | |
689 | | /// Array of offsets to BaseCoord tables, from beginning of |
690 | | /// BaseValues table — order matches baselineTags array in the |
691 | | /// BaseTagList |
692 | 0 | pub fn base_coord_offsets(&self) -> &'a [BigEndian<Offset16>] { |
693 | 0 | let range = self.base_coord_offsets_byte_range(); |
694 | 0 | self.offset_data() |
695 | 0 | .read_array(range) |
696 | 0 | .ok() |
697 | 0 | .unwrap_or_default() |
698 | 0 | } |
699 | | |
700 | | /// A dynamically resolving wrapper for [`base_coord_offsets`][Self::base_coord_offsets]. |
701 | 0 | pub fn base_coords(&self) -> ArrayOfOffsets<'a, BaseCoord<'a>, Offset16> { |
702 | 0 | let data = self.offset_data(); |
703 | 0 | let offsets = self.base_coord_offsets(); |
704 | 0 | ArrayOfOffsets::new(offsets, data, ()) |
705 | 0 | } |
706 | | |
707 | 0 | pub fn default_baseline_index_byte_range(&self) -> Range<usize> { |
708 | 0 | let start = 0usize; |
709 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
710 | 0 | start..end |
711 | 0 | } |
712 | | |
713 | 0 | pub fn base_coord_count_byte_range(&self) -> Range<usize> { |
714 | 0 | let start = self.default_baseline_index_byte_range().end; |
715 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
716 | 0 | start..end |
717 | 0 | } |
718 | | |
719 | 0 | pub fn base_coord_offsets_byte_range(&self) -> Range<usize> { |
720 | 0 | let base_coord_count = self.base_coord_count(); |
721 | 0 | let start = self.base_coord_count_byte_range().end; |
722 | 0 | let end = start.saturating_add( |
723 | 0 | (transforms::to_usize(base_coord_count)).saturating_mul(Offset16::RAW_BYTE_LEN), |
724 | | ); |
725 | 0 | start..end |
726 | 0 | } |
727 | | } |
728 | | |
729 | | const _: () = assert!(FontData::default_data_long_enough(BaseValues::MIN_SIZE)); |
730 | | |
731 | | impl Default for BaseValues<'_> { |
732 | 0 | fn default() -> Self { |
733 | | Self { |
734 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
735 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
736 | | } |
737 | 0 | } |
738 | | } |
739 | | |
740 | | impl<'a> MinByteRange<'a> for MinMax<'a> { |
741 | 0 | fn min_byte_range(&self) -> Range<usize> { |
742 | 0 | 0..self.feat_min_max_records_byte_range().end |
743 | 0 | } |
744 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
745 | 0 | let range = self.min_byte_range(); |
746 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
747 | 0 | } |
748 | | } |
749 | | |
750 | | impl ReadArgs for MinMax<'_> { |
751 | | type Args = (); |
752 | | } |
753 | | |
754 | | impl<'a> FontRead<'a> for MinMax<'a> { |
755 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
756 | | #[allow(clippy::absurd_extreme_comparisons)] |
757 | 0 | if data.len() < Self::MIN_SIZE { |
758 | 0 | return Err(ReadError::OutOfBounds); |
759 | 0 | } |
760 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
761 | 0 | return Err(ReadError::OutOfBounds); |
762 | | }; |
763 | 0 | Ok(Self { data }) |
764 | 0 | } |
765 | | } |
766 | | |
767 | | /// The fields at the start of the table, which are read directly. |
768 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
769 | | #[repr(C, packed)] |
770 | | #[doc(hidden)] |
771 | | pub struct MinMaxFixedHeader { |
772 | | pub min_coord_offset: BigEndian<Nullable<Offset16>>, |
773 | | pub max_coord_offset: BigEndian<Nullable<Offset16>>, |
774 | | pub feat_min_max_count: BigEndian<u16>, |
775 | | } |
776 | | |
777 | | #[allow(clippy::absurd_extreme_comparisons)] |
778 | | const _: () = assert!(std::mem::size_of::<MinMaxFixedHeader>() <= MinMax::MIN_SIZE); |
779 | | |
780 | | /// [MinMax](https://learn.microsoft.com/en-us/typography/opentype/spec/base#minmax-table) table |
781 | | #[derive(Clone)] |
782 | | pub struct MinMax<'a> { |
783 | | data: TableDataRef<'a, MinMaxFixedHeader>, |
784 | | } |
785 | | |
786 | | #[allow(clippy::needless_lifetimes)] |
787 | | impl<'a> MinMax<'a> { |
788 | | pub const MIN_SIZE: usize = |
789 | | (Offset16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
790 | | basic_table_impls!(impl_the_methods); |
791 | | |
792 | | /// Offset to BaseCoord table that defines the minimum extent |
793 | | /// value, from the beginning of MinMax table (may be NULL) |
794 | 0 | pub fn min_coord_offset(&self) -> Nullable<Offset16> { |
795 | 0 | self.data.header().min_coord_offset.get() |
796 | 0 | } |
797 | | |
798 | | /// Attempt to resolve [`min_coord_offset`][Self::min_coord_offset]. |
799 | 0 | pub fn min_coord(&self) -> Option<Result<BaseCoord<'a>, ReadError>> { |
800 | 0 | let data = self.offset_data(); |
801 | 0 | self.min_coord_offset().resolve(data) |
802 | 0 | } |
803 | | |
804 | | /// Offset to BaseCoord table that defines maximum extent value, |
805 | | /// from the beginning of MinMax table (may be NULL) |
806 | 0 | pub fn max_coord_offset(&self) -> Nullable<Offset16> { |
807 | 0 | self.data.header().max_coord_offset.get() |
808 | 0 | } |
809 | | |
810 | | /// Attempt to resolve [`max_coord_offset`][Self::max_coord_offset]. |
811 | 0 | pub fn max_coord(&self) -> Option<Result<BaseCoord<'a>, ReadError>> { |
812 | 0 | let data = self.offset_data(); |
813 | 0 | self.max_coord_offset().resolve(data) |
814 | 0 | } |
815 | | |
816 | | /// Number of FeatMinMaxRecords — may be zero (0) |
817 | 0 | pub fn feat_min_max_count(&self) -> u16 { |
818 | 0 | self.data.header().feat_min_max_count.get() |
819 | 0 | } |
820 | | |
821 | | /// Array of FeatMinMaxRecords, in alphabetical order by |
822 | | /// featureTableTag |
823 | 0 | pub fn feat_min_max_records(&self) -> &'a [FeatMinMaxRecord] { |
824 | 0 | let range = self.feat_min_max_records_byte_range(); |
825 | 0 | self.offset_data() |
826 | 0 | .read_array(range) |
827 | 0 | .ok() |
828 | 0 | .unwrap_or_default() |
829 | 0 | } |
830 | | |
831 | 0 | pub fn min_coord_offset_byte_range(&self) -> Range<usize> { |
832 | 0 | let start = 0usize; |
833 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
834 | 0 | start..end |
835 | 0 | } |
836 | | |
837 | 0 | pub fn max_coord_offset_byte_range(&self) -> Range<usize> { |
838 | 0 | let start = self.min_coord_offset_byte_range().end; |
839 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
840 | 0 | start..end |
841 | 0 | } |
842 | | |
843 | 0 | pub fn feat_min_max_count_byte_range(&self) -> Range<usize> { |
844 | 0 | let start = self.max_coord_offset_byte_range().end; |
845 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
846 | 0 | start..end |
847 | 0 | } |
848 | | |
849 | 0 | pub fn feat_min_max_records_byte_range(&self) -> Range<usize> { |
850 | 0 | let feat_min_max_count = self.feat_min_max_count(); |
851 | 0 | let start = self.feat_min_max_count_byte_range().end; |
852 | 0 | let end = start.saturating_add( |
853 | 0 | (transforms::to_usize(feat_min_max_count)) |
854 | 0 | .saturating_mul(FeatMinMaxRecord::RAW_BYTE_LEN), |
855 | | ); |
856 | 0 | start..end |
857 | 0 | } |
858 | | } |
859 | | |
860 | | const _: () = assert!(FontData::default_data_long_enough(MinMax::MIN_SIZE)); |
861 | | |
862 | | impl Default for MinMax<'_> { |
863 | 0 | fn default() -> Self { |
864 | | Self { |
865 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
866 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
867 | | } |
868 | 0 | } |
869 | | } |
870 | | |
871 | | /// [FeatMinMaxRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/base#baselangsysrecord) |
872 | | #[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)] |
873 | | #[repr(C)] |
874 | | #[repr(packed)] |
875 | | pub struct FeatMinMaxRecord { |
876 | | /// 4-byte feature identification tag — must match feature tag in |
877 | | /// FeatureList |
878 | | pub feature_table_tag: BigEndian<Tag>, |
879 | | /// Offset to BaseCoord table that defines the minimum extent |
880 | | /// value, from beginning of MinMax table (may be NULL) |
881 | | pub min_coord_offset: BigEndian<Nullable<Offset16>>, |
882 | | /// Offset to BaseCoord table that defines the maximum extent |
883 | | /// value, from beginning of MinMax table (may be NULL) |
884 | | pub max_coord_offset: BigEndian<Nullable<Offset16>>, |
885 | | } |
886 | | |
887 | | impl FeatMinMaxRecord { |
888 | | /// 4-byte feature identification tag — must match feature tag in |
889 | | /// FeatureList |
890 | 0 | pub fn feature_table_tag(&self) -> Tag { |
891 | 0 | self.feature_table_tag.get() |
892 | 0 | } |
893 | | |
894 | | /// Offset to BaseCoord table that defines the minimum extent |
895 | | /// value, from beginning of MinMax table (may be NULL) |
896 | 0 | pub fn min_coord_offset(&self) -> Nullable<Offset16> { |
897 | 0 | self.min_coord_offset.get() |
898 | 0 | } |
899 | | |
900 | | /// Offset to BaseCoord table that defines the minimum extent |
901 | | /// value, from beginning of MinMax table (may be NULL) |
902 | | /// |
903 | | /// The `data` argument should be retrieved from the parent table |
904 | | /// By calling its `offset_data` method. |
905 | 0 | pub fn min_coord<'a>(&self, data: FontData<'a>) -> Option<Result<BaseCoord<'a>, ReadError>> { |
906 | 0 | self.min_coord_offset().resolve(data) |
907 | 0 | } |
908 | | |
909 | | /// Offset to BaseCoord table that defines the maximum extent |
910 | | /// value, from beginning of MinMax table (may be NULL) |
911 | 0 | pub fn max_coord_offset(&self) -> Nullable<Offset16> { |
912 | 0 | self.max_coord_offset.get() |
913 | 0 | } |
914 | | |
915 | | /// Offset to BaseCoord table that defines the maximum extent |
916 | | /// value, from beginning of MinMax table (may be NULL) |
917 | | /// |
918 | | /// The `data` argument should be retrieved from the parent table |
919 | | /// By calling its `offset_data` method. |
920 | 0 | pub fn max_coord<'a>(&self, data: FontData<'a>) -> Option<Result<BaseCoord<'a>, ReadError>> { |
921 | 0 | self.max_coord_offset().resolve(data) |
922 | 0 | } |
923 | | } |
924 | | |
925 | | impl FixedSize for FeatMinMaxRecord { |
926 | | const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN; |
927 | | } |
928 | | |
929 | | #[derive(Clone)] |
930 | | pub enum BaseCoord<'a> { |
931 | | Format1(BaseCoordFormat1<'a>), |
932 | | Format2(BaseCoordFormat2<'a>), |
933 | | Format3(BaseCoordFormat3<'a>), |
934 | | } |
935 | | |
936 | | impl Default for BaseCoord<'_> { |
937 | 0 | fn default() -> Self { |
938 | 0 | Self::Format1(Default::default()) |
939 | 0 | } |
940 | | } |
941 | | |
942 | | impl<'a> BaseCoord<'a> { |
943 | | ///Return the `FontData` used to resolve offsets for this table. |
944 | 0 | pub fn offset_data(&self) -> FontData<'a> { |
945 | 0 | match self { |
946 | 0 | Self::Format1(item) => item.offset_data(), |
947 | 0 | Self::Format2(item) => item.offset_data(), |
948 | 0 | Self::Format3(item) => item.offset_data(), |
949 | | } |
950 | 0 | } |
951 | | |
952 | | /// Format identifier — format = 1 |
953 | 0 | pub fn base_coord_format(&self) -> u16 { |
954 | 0 | match self { |
955 | 0 | Self::Format1(item) => item.base_coord_format(), |
956 | 0 | Self::Format2(item) => item.base_coord_format(), |
957 | 0 | Self::Format3(item) => item.base_coord_format(), |
958 | | } |
959 | 0 | } |
960 | | |
961 | | /// X or Y value, in design units |
962 | 0 | pub fn coordinate(&self) -> i16 { |
963 | 0 | match self { |
964 | 0 | Self::Format1(item) => item.coordinate(), |
965 | 0 | Self::Format2(item) => item.coordinate(), |
966 | 0 | Self::Format3(item) => item.coordinate(), |
967 | | } |
968 | 0 | } |
969 | | } |
970 | | |
971 | | impl ReadArgs for BaseCoord<'_> { |
972 | | type Args = (); |
973 | | } |
974 | | |
975 | | impl<'a> FontRead<'a> for BaseCoord<'a> { |
976 | | #[inline] |
977 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
978 | 0 | let format: u16 = data.read_at(0usize)?; |
979 | 0 | match format { |
980 | 0 | BaseCoordFormat1::FORMAT => Ok(Self::Format1(FontRead::read(data)?)), |
981 | 0 | BaseCoordFormat2::FORMAT => Ok(Self::Format2(FontRead::read(data)?)), |
982 | 0 | BaseCoordFormat3::FORMAT => Ok(Self::Format3(FontRead::read(data)?)), |
983 | 0 | other => Err(ReadError::InvalidFormat(other.into())), |
984 | | } |
985 | 0 | } |
986 | | } |
987 | | |
988 | | impl<'a> MinByteRange<'a> for BaseCoord<'a> { |
989 | 0 | fn min_byte_range(&self) -> Range<usize> { |
990 | 0 | match self { |
991 | 0 | Self::Format1(item) => item.min_byte_range(), |
992 | 0 | Self::Format2(item) => item.min_byte_range(), |
993 | 0 | Self::Format3(item) => item.min_byte_range(), |
994 | | } |
995 | 0 | } |
996 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
997 | 0 | match self { |
998 | 0 | Self::Format1(item) => item.min_table_bytes(), |
999 | 0 | Self::Format2(item) => item.min_table_bytes(), |
1000 | 0 | Self::Format3(item) => item.min_table_bytes(), |
1001 | | } |
1002 | 0 | } |
1003 | | } |
1004 | | |
1005 | | impl Format<u16> for BaseCoordFormat1<'_> { |
1006 | | const FORMAT: u16 = 1; |
1007 | | } |
1008 | | |
1009 | | impl<'a> MinByteRange<'a> for BaseCoordFormat1<'a> { |
1010 | 0 | fn min_byte_range(&self) -> Range<usize> { |
1011 | 0 | 0..self.coordinate_byte_range().end |
1012 | 0 | } |
1013 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
1014 | 0 | let range = self.min_byte_range(); |
1015 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
1016 | 0 | } |
1017 | | } |
1018 | | |
1019 | | impl ReadArgs for BaseCoordFormat1<'_> { |
1020 | | type Args = (); |
1021 | | } |
1022 | | |
1023 | | impl<'a> FontRead<'a> for BaseCoordFormat1<'a> { |
1024 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
1025 | | #[allow(clippy::absurd_extreme_comparisons)] |
1026 | 0 | if data.len() < Self::MIN_SIZE { |
1027 | 0 | return Err(ReadError::OutOfBounds); |
1028 | 0 | } |
1029 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
1030 | 0 | return Err(ReadError::OutOfBounds); |
1031 | | }; |
1032 | 0 | Ok(Self { data }) |
1033 | 0 | } |
1034 | | } |
1035 | | |
1036 | | /// The fields at the start of the table, which are read directly. |
1037 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
1038 | | #[repr(C, packed)] |
1039 | | #[doc(hidden)] |
1040 | | pub struct BaseCoordFormat1FixedHeader { |
1041 | | pub base_coord_format: BigEndian<u16>, |
1042 | | pub coordinate: BigEndian<i16>, |
1043 | | } |
1044 | | |
1045 | | #[allow(clippy::absurd_extreme_comparisons)] |
1046 | | const _: () = |
1047 | | assert!(std::mem::size_of::<BaseCoordFormat1FixedHeader>() <= BaseCoordFormat1::MIN_SIZE); |
1048 | | |
1049 | | /// [BaseCoordFormat1](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basecoord-format-1) |
1050 | | #[derive(Clone)] |
1051 | | pub struct BaseCoordFormat1<'a> { |
1052 | | data: TableDataRef<'a, BaseCoordFormat1FixedHeader>, |
1053 | | } |
1054 | | |
1055 | | #[allow(clippy::needless_lifetimes)] |
1056 | | impl<'a> BaseCoordFormat1<'a> { |
1057 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN); |
1058 | | basic_table_impls!(impl_the_methods); |
1059 | | |
1060 | | /// Format identifier — format = 1 |
1061 | 0 | pub fn base_coord_format(&self) -> u16 { |
1062 | 0 | self.data.header().base_coord_format.get() |
1063 | 0 | } |
1064 | | |
1065 | | /// X or Y value, in design units |
1066 | 0 | pub fn coordinate(&self) -> i16 { |
1067 | 0 | self.data.header().coordinate.get() |
1068 | 0 | } |
1069 | | |
1070 | 0 | pub fn base_coord_format_byte_range(&self) -> Range<usize> { |
1071 | 0 | let start = 0usize; |
1072 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1073 | 0 | start..end |
1074 | 0 | } |
1075 | | |
1076 | 0 | pub fn coordinate_byte_range(&self) -> Range<usize> { |
1077 | 0 | let start = self.base_coord_format_byte_range().end; |
1078 | 0 | let end = start.saturating_add(i16::RAW_BYTE_LEN); |
1079 | 0 | start..end |
1080 | 0 | } |
1081 | | } |
1082 | | |
1083 | | const _: () = assert!(FontData::default_data_long_enough( |
1084 | | BaseCoordFormat1::MIN_SIZE |
1085 | | )); |
1086 | | |
1087 | | impl Default for BaseCoordFormat1<'_> { |
1088 | 0 | fn default() -> Self { |
1089 | | Self { |
1090 | 0 | data: TableDataRef::new(FontData::default_format_1_u16_table_data().as_bytes()) |
1091 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
1092 | | } |
1093 | 0 | } |
1094 | | } |
1095 | | |
1096 | | impl Format<u16> for BaseCoordFormat2<'_> { |
1097 | | const FORMAT: u16 = 2; |
1098 | | } |
1099 | | |
1100 | | impl<'a> MinByteRange<'a> for BaseCoordFormat2<'a> { |
1101 | 0 | fn min_byte_range(&self) -> Range<usize> { |
1102 | 0 | 0..self.base_coord_point_byte_range().end |
1103 | 0 | } |
1104 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
1105 | 0 | let range = self.min_byte_range(); |
1106 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
1107 | 0 | } |
1108 | | } |
1109 | | |
1110 | | impl ReadArgs for BaseCoordFormat2<'_> { |
1111 | | type Args = (); |
1112 | | } |
1113 | | |
1114 | | impl<'a> FontRead<'a> for BaseCoordFormat2<'a> { |
1115 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
1116 | | #[allow(clippy::absurd_extreme_comparisons)] |
1117 | 0 | if data.len() < Self::MIN_SIZE { |
1118 | 0 | return Err(ReadError::OutOfBounds); |
1119 | 0 | } |
1120 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
1121 | 0 | return Err(ReadError::OutOfBounds); |
1122 | | }; |
1123 | 0 | Ok(Self { data }) |
1124 | 0 | } |
1125 | | } |
1126 | | |
1127 | | /// The fields at the start of the table, which are read directly. |
1128 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
1129 | | #[repr(C, packed)] |
1130 | | #[doc(hidden)] |
1131 | | pub struct BaseCoordFormat2FixedHeader { |
1132 | | pub base_coord_format: BigEndian<u16>, |
1133 | | pub coordinate: BigEndian<i16>, |
1134 | | pub reference_glyph: BigEndian<u16>, |
1135 | | pub base_coord_point: BigEndian<u16>, |
1136 | | } |
1137 | | |
1138 | | #[allow(clippy::absurd_extreme_comparisons)] |
1139 | | const _: () = |
1140 | | assert!(std::mem::size_of::<BaseCoordFormat2FixedHeader>() <= BaseCoordFormat2::MIN_SIZE); |
1141 | | |
1142 | | /// [BaseCoordFormat2](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basecoord-format-2) |
1143 | | #[derive(Clone)] |
1144 | | pub struct BaseCoordFormat2<'a> { |
1145 | | data: TableDataRef<'a, BaseCoordFormat2FixedHeader>, |
1146 | | } |
1147 | | |
1148 | | #[allow(clippy::needless_lifetimes)] |
1149 | | impl<'a> BaseCoordFormat2<'a> { |
1150 | | pub const MIN_SIZE: usize = |
1151 | | (u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
1152 | | basic_table_impls!(impl_the_methods); |
1153 | | |
1154 | | /// Format identifier — format = 2 |
1155 | 0 | pub fn base_coord_format(&self) -> u16 { |
1156 | 0 | self.data.header().base_coord_format.get() |
1157 | 0 | } |
1158 | | |
1159 | | /// X or Y value, in design units |
1160 | 0 | pub fn coordinate(&self) -> i16 { |
1161 | 0 | self.data.header().coordinate.get() |
1162 | 0 | } |
1163 | | |
1164 | | /// Glyph ID of control glyph |
1165 | 0 | pub fn reference_glyph(&self) -> u16 { |
1166 | 0 | self.data.header().reference_glyph.get() |
1167 | 0 | } |
1168 | | |
1169 | | /// Index of contour point on the reference glyph |
1170 | 0 | pub fn base_coord_point(&self) -> u16 { |
1171 | 0 | self.data.header().base_coord_point.get() |
1172 | 0 | } |
1173 | | |
1174 | 0 | pub fn base_coord_format_byte_range(&self) -> Range<usize> { |
1175 | 0 | let start = 0usize; |
1176 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1177 | 0 | start..end |
1178 | 0 | } |
1179 | | |
1180 | 0 | pub fn coordinate_byte_range(&self) -> Range<usize> { |
1181 | 0 | let start = self.base_coord_format_byte_range().end; |
1182 | 0 | let end = start.saturating_add(i16::RAW_BYTE_LEN); |
1183 | 0 | start..end |
1184 | 0 | } |
1185 | | |
1186 | 0 | pub fn reference_glyph_byte_range(&self) -> Range<usize> { |
1187 | 0 | let start = self.coordinate_byte_range().end; |
1188 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1189 | 0 | start..end |
1190 | 0 | } |
1191 | | |
1192 | 0 | pub fn base_coord_point_byte_range(&self) -> Range<usize> { |
1193 | 0 | let start = self.reference_glyph_byte_range().end; |
1194 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1195 | 0 | start..end |
1196 | 0 | } |
1197 | | } |
1198 | | |
1199 | | impl Format<u16> for BaseCoordFormat3<'_> { |
1200 | | const FORMAT: u16 = 3; |
1201 | | } |
1202 | | |
1203 | | impl<'a> MinByteRange<'a> for BaseCoordFormat3<'a> { |
1204 | 0 | fn min_byte_range(&self) -> Range<usize> { |
1205 | 0 | 0..self.device_offset_byte_range().end |
1206 | 0 | } |
1207 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
1208 | 0 | let range = self.min_byte_range(); |
1209 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
1210 | 0 | } |
1211 | | } |
1212 | | |
1213 | | impl ReadArgs for BaseCoordFormat3<'_> { |
1214 | | type Args = (); |
1215 | | } |
1216 | | |
1217 | | impl<'a> FontRead<'a> for BaseCoordFormat3<'a> { |
1218 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
1219 | | #[allow(clippy::absurd_extreme_comparisons)] |
1220 | 0 | if data.len() < Self::MIN_SIZE { |
1221 | 0 | return Err(ReadError::OutOfBounds); |
1222 | 0 | } |
1223 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
1224 | 0 | return Err(ReadError::OutOfBounds); |
1225 | | }; |
1226 | 0 | Ok(Self { data }) |
1227 | 0 | } |
1228 | | } |
1229 | | |
1230 | | /// The fields at the start of the table, which are read directly. |
1231 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
1232 | | #[repr(C, packed)] |
1233 | | #[doc(hidden)] |
1234 | | pub struct BaseCoordFormat3FixedHeader { |
1235 | | pub base_coord_format: BigEndian<u16>, |
1236 | | pub coordinate: BigEndian<i16>, |
1237 | | pub device_offset: BigEndian<Nullable<Offset16>>, |
1238 | | } |
1239 | | |
1240 | | #[allow(clippy::absurd_extreme_comparisons)] |
1241 | | const _: () = |
1242 | | assert!(std::mem::size_of::<BaseCoordFormat3FixedHeader>() <= BaseCoordFormat3::MIN_SIZE); |
1243 | | |
1244 | | /// [BaseCoordFormat3](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basecoord-format-3) |
1245 | | #[derive(Clone)] |
1246 | | pub struct BaseCoordFormat3<'a> { |
1247 | | data: TableDataRef<'a, BaseCoordFormat3FixedHeader>, |
1248 | | } |
1249 | | |
1250 | | #[allow(clippy::needless_lifetimes)] |
1251 | | impl<'a> BaseCoordFormat3<'a> { |
1252 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN); |
1253 | | basic_table_impls!(impl_the_methods); |
1254 | | |
1255 | | /// Format identifier — format = 3 |
1256 | 0 | pub fn base_coord_format(&self) -> u16 { |
1257 | 0 | self.data.header().base_coord_format.get() |
1258 | 0 | } |
1259 | | |
1260 | | /// X or Y value, in design units |
1261 | 0 | pub fn coordinate(&self) -> i16 { |
1262 | 0 | self.data.header().coordinate.get() |
1263 | 0 | } |
1264 | | |
1265 | | /// Offset to Device table (non-variable font) / Variation Index |
1266 | | /// table (variable font) for X or Y value, from beginning of |
1267 | | /// BaseCoord table (may be NULL). |
1268 | 0 | pub fn device_offset(&self) -> Nullable<Offset16> { |
1269 | 0 | self.data.header().device_offset.get() |
1270 | 0 | } |
1271 | | |
1272 | | /// Attempt to resolve [`device_offset`][Self::device_offset]. |
1273 | 0 | pub fn device(&self) -> Option<Result<DeviceOrVariationIndex<'a>, ReadError>> { |
1274 | 0 | let data = self.offset_data(); |
1275 | 0 | self.device_offset().resolve(data) |
1276 | 0 | } |
1277 | | |
1278 | 0 | pub fn base_coord_format_byte_range(&self) -> Range<usize> { |
1279 | 0 | let start = 0usize; |
1280 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1281 | 0 | start..end |
1282 | 0 | } |
1283 | | |
1284 | 0 | pub fn coordinate_byte_range(&self) -> Range<usize> { |
1285 | 0 | let start = self.base_coord_format_byte_range().end; |
1286 | 0 | let end = start.saturating_add(i16::RAW_BYTE_LEN); |
1287 | 0 | start..end |
1288 | 0 | } |
1289 | | |
1290 | 0 | pub fn device_offset_byte_range(&self) -> Range<usize> { |
1291 | 0 | let start = self.coordinate_byte_range().end; |
1292 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
1293 | 0 | start..end |
1294 | 0 | } |
1295 | | } |