/rust/registry/src/index.crates.io-1949cf8c6b5b557f/read-fonts-0.47.0/generated/generated_gdef.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 Gdef<'a> { |
9 | 0 | fn min_byte_range(&self) -> Range<usize> { |
10 | 0 | 0..self.mark_attach_class_def_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 Gdef<'_> { |
19 | | /// `GDEF` |
20 | | const TAG: Tag = Tag::new(b"GDEF"); |
21 | | } |
22 | | |
23 | | impl ReadArgs for Gdef<'_> { |
24 | | type Args = (); |
25 | | } |
26 | | |
27 | | impl<'a> FontRead<'a> for Gdef<'a> { |
28 | 790 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
29 | | #[allow(clippy::absurd_extreme_comparisons)] |
30 | 790 | if data.len() < Self::MIN_SIZE { |
31 | 5 | return Err(ReadError::OutOfBounds); |
32 | 785 | } |
33 | 785 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
34 | 0 | return Err(ReadError::OutOfBounds); |
35 | | }; |
36 | 785 | Ok(Self { data }) |
37 | 790 | } |
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 GdefFixedHeader { |
45 | | pub version: BigEndian<MajorMinor>, |
46 | | pub glyph_class_def_offset: BigEndian<Nullable<Offset16>>, |
47 | | pub attach_list_offset: BigEndian<Nullable<Offset16>>, |
48 | | pub lig_caret_list_offset: BigEndian<Nullable<Offset16>>, |
49 | | pub mark_attach_class_def_offset: BigEndian<Nullable<Offset16>>, |
50 | | } |
51 | | |
52 | | #[allow(clippy::absurd_extreme_comparisons)] |
53 | | const _: () = assert!(std::mem::size_of::<GdefFixedHeader>() <= Gdef::MIN_SIZE); |
54 | | |
55 | | /// [GDEF](https://docs.microsoft.com/en-us/typography/opentype/spec/gdef#gdef-header) 1.0 |
56 | | #[derive(Clone)] |
57 | | pub struct Gdef<'a> { |
58 | | data: TableDataRef<'a, GdefFixedHeader>, |
59 | | } |
60 | | |
61 | | #[allow(clippy::needless_lifetimes)] |
62 | | impl<'a> Gdef<'a> { |
63 | | pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN |
64 | | + Offset16::RAW_BYTE_LEN |
65 | | + Offset16::RAW_BYTE_LEN |
66 | | + Offset16::RAW_BYTE_LEN |
67 | | + Offset16::RAW_BYTE_LEN); |
68 | | basic_table_impls!(impl_the_methods); |
69 | | |
70 | | /// The major/minor version of the GDEF table |
71 | 17.0k | pub fn version(&self) -> MajorMinor { |
72 | 17.0k | self.data.header().version.get() |
73 | 17.0k | } |
74 | | |
75 | | /// Offset to class definition table for glyph type, from beginning |
76 | | /// of GDEF header (may be NULL) |
77 | 773 | pub fn glyph_class_def_offset(&self) -> Nullable<Offset16> { |
78 | 773 | self.data.header().glyph_class_def_offset.get() |
79 | 773 | } |
80 | | |
81 | | /// Offset to attachment point list table, from beginning of GDEF |
82 | | /// header (may be NULL) |
83 | 0 | pub fn attach_list_offset(&self) -> Nullable<Offset16> { |
84 | 0 | self.data.header().attach_list_offset.get() |
85 | 0 | } |
86 | | |
87 | | /// Offset to ligature caret list table, from beginning of GDEF |
88 | | /// header (may be NULL) |
89 | 0 | pub fn lig_caret_list_offset(&self) -> Nullable<Offset16> { |
90 | 0 | self.data.header().lig_caret_list_offset.get() |
91 | 0 | } |
92 | | |
93 | | /// Offset to class definition table for mark attachment type, from |
94 | | /// beginning of GDEF header (may be NULL) |
95 | 773 | pub fn mark_attach_class_def_offset(&self) -> Nullable<Offset16> { |
96 | 773 | self.data.header().mark_attach_class_def_offset.get() |
97 | 773 | } |
98 | | |
99 | | /// Offset to the table of mark glyph set definitions, from |
100 | | /// beginning of GDEF header (may be NULL) |
101 | 773 | pub fn mark_glyph_sets_def_offset(&self) -> Option<Nullable<Offset16>> { |
102 | 773 | let range = self.mark_glyph_sets_def_offset_byte_range(); |
103 | 773 | (!range.is_empty()) |
104 | 773 | .then(|| self.offset_data().read_at(range.start).ok()) |
105 | 773 | .flatten() |
106 | 773 | } |
107 | | |
108 | | /// Offset to the Item Variation Store table, from beginning of |
109 | | /// GDEF header (may be NULL) |
110 | 785 | pub fn item_var_store_offset(&self) -> Option<Nullable<Offset32>> { |
111 | 785 | let range = self.item_var_store_offset_byte_range(); |
112 | 785 | (!range.is_empty()) |
113 | 785 | .then(|| self.offset_data().read_at(range.start).ok()) |
114 | 785 | .flatten() |
115 | 785 | } |
116 | | |
117 | | /// Attempt to resolve [`item_var_store_offset`][Self::item_var_store_offset]. |
118 | 785 | pub fn item_var_store(&self) -> Option<Result<ItemVariationStore<'a>, ReadError>> { |
119 | 785 | let data = self.offset_data(); |
120 | 785 | self.item_var_store_offset().map(|x| x.resolve(data))? |
121 | 785 | } |
122 | | |
123 | | /// 32-bit offset taking precedence over glyph_class_def_offset when nonzero. |
124 | 773 | pub fn glyph_class_def2_offset(&self) -> Option<Nullable<Offset32>> { |
125 | 773 | let range = self.glyph_class_def2_offset_byte_range(); |
126 | 773 | (!range.is_empty()) |
127 | 773 | .then(|| self.offset_data().read_at(range.start).ok()) |
128 | 773 | .flatten() |
129 | 773 | } |
130 | | |
131 | | /// Attempt to resolve [`glyph_class_def2_offset`][Self::glyph_class_def2_offset]. |
132 | 0 | pub fn glyph_class_def2(&self) -> Option<Result<ClassDef<'a>, ReadError>> { |
133 | 0 | let data = self.offset_data(); |
134 | 0 | self.glyph_class_def2_offset().map(|x| x.resolve(data))? |
135 | 0 | } |
136 | | |
137 | | /// 32-bit offset taking precedence over attach_list_offset when nonzero. |
138 | 0 | pub fn attach_list2_offset(&self) -> Option<Nullable<Offset32>> { |
139 | 0 | let range = self.attach_list2_offset_byte_range(); |
140 | 0 | (!range.is_empty()) |
141 | 0 | .then(|| self.offset_data().read_at(range.start).ok()) |
142 | 0 | .flatten() |
143 | 0 | } |
144 | | |
145 | | /// Attempt to resolve [`attach_list2_offset`][Self::attach_list2_offset]. |
146 | 0 | pub fn attach_list2(&self) -> Option<Result<AttachList<'a>, ReadError>> { |
147 | 0 | let data = self.offset_data(); |
148 | 0 | self.attach_list2_offset().map(|x| x.resolve(data))? |
149 | 0 | } |
150 | | |
151 | | /// 32-bit offset taking precedence over lig_caret_list_offset when nonzero. |
152 | 0 | pub fn lig_caret_list2_offset(&self) -> Option<Nullable<Offset32>> { |
153 | 0 | let range = self.lig_caret_list2_offset_byte_range(); |
154 | 0 | (!range.is_empty()) |
155 | 0 | .then(|| self.offset_data().read_at(range.start).ok()) |
156 | 0 | .flatten() |
157 | 0 | } |
158 | | |
159 | | /// Attempt to resolve [`lig_caret_list2_offset`][Self::lig_caret_list2_offset]. |
160 | 0 | pub fn lig_caret_list2(&self) -> Option<Result<LigCaretList2<'a>, ReadError>> { |
161 | 0 | let data = self.offset_data(); |
162 | 0 | self.lig_caret_list2_offset().map(|x| x.resolve(data))? |
163 | 0 | } |
164 | | |
165 | | /// 32-bit offset taking precedence over mark_attach_class_def_offset when nonzero. |
166 | 773 | pub fn mark_attach_class_def2_offset(&self) -> Option<Nullable<Offset32>> { |
167 | 773 | let range = self.mark_attach_class_def2_offset_byte_range(); |
168 | 773 | (!range.is_empty()) |
169 | 773 | .then(|| self.offset_data().read_at(range.start).ok()) |
170 | 773 | .flatten() |
171 | 773 | } |
172 | | |
173 | | /// Attempt to resolve [`mark_attach_class_def2_offset`][Self::mark_attach_class_def2_offset]. |
174 | 0 | pub fn mark_attach_class_def2(&self) -> Option<Result<ClassDef<'a>, ReadError>> { |
175 | 0 | let data = self.offset_data(); |
176 | 0 | self.mark_attach_class_def2_offset() |
177 | 0 | .map(|x| x.resolve(data))? |
178 | 0 | } |
179 | | |
180 | | /// 32-bit offset taking precedence over mark_glyph_sets_def_offset when nonzero. |
181 | 773 | pub fn mark_glyph_sets_def2_offset(&self) -> Option<Nullable<Offset32>> { |
182 | 773 | let range = self.mark_glyph_sets_def2_offset_byte_range(); |
183 | 773 | (!range.is_empty()) |
184 | 773 | .then(|| self.offset_data().read_at(range.start).ok()) |
185 | 773 | .flatten() |
186 | 773 | } |
187 | | |
188 | | /// Attempt to resolve [`mark_glyph_sets_def2_offset`][Self::mark_glyph_sets_def2_offset]. |
189 | 0 | pub fn mark_glyph_sets_def2(&self) -> Option<Result<MarkGlyphSets<'a>, ReadError>> { |
190 | 0 | let data = self.offset_data(); |
191 | 0 | self.mark_glyph_sets_def2_offset() |
192 | 0 | .map(|x| x.resolve(data))? |
193 | 0 | } |
194 | | |
195 | 3.87k | pub fn version_byte_range(&self) -> Range<usize> { |
196 | 3.87k | let start = 0usize; |
197 | 3.87k | let end = start.saturating_add(MajorMinor::RAW_BYTE_LEN); |
198 | 3.87k | start..end |
199 | 3.87k | } |
200 | | |
201 | 3.87k | pub fn glyph_class_def_offset_byte_range(&self) -> Range<usize> { |
202 | 3.87k | let start = self.version_byte_range().end; |
203 | 3.87k | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
204 | 3.87k | start..end |
205 | 3.87k | } |
206 | | |
207 | 3.87k | pub fn attach_list_offset_byte_range(&self) -> Range<usize> { |
208 | 3.87k | let start = self.glyph_class_def_offset_byte_range().end; |
209 | 3.87k | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
210 | 3.87k | start..end |
211 | 3.87k | } |
212 | | |
213 | 3.87k | pub fn lig_caret_list_offset_byte_range(&self) -> Range<usize> { |
214 | 3.87k | let start = self.attach_list_offset_byte_range().end; |
215 | 3.87k | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
216 | 3.87k | start..end |
217 | 3.87k | } |
218 | | |
219 | 3.87k | pub fn mark_attach_class_def_offset_byte_range(&self) -> Range<usize> { |
220 | 3.87k | let start = self.lig_caret_list_offset_byte_range().end; |
221 | 3.87k | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
222 | 3.87k | start..end |
223 | 3.87k | } |
224 | | |
225 | 3.87k | pub fn mark_glyph_sets_def_offset_byte_range(&self) -> Range<usize> { |
226 | 3.87k | let start = self.mark_attach_class_def_offset_byte_range().end; |
227 | 3.87k | let end = if self.version().compatible((1u16, 2u16)) { |
228 | 2.26k | start.saturating_add(Offset16::RAW_BYTE_LEN) |
229 | | } else { |
230 | 1.61k | start |
231 | | }; |
232 | 3.87k | start..end |
233 | 3.87k | } |
234 | | |
235 | 3.10k | pub fn item_var_store_offset_byte_range(&self) -> Range<usize> { |
236 | 3.10k | let start = self.mark_glyph_sets_def_offset_byte_range().end; |
237 | 3.10k | let end = if self.version().compatible((1u16, 3u16)) { |
238 | 1.73k | start.saturating_add(Offset32::RAW_BYTE_LEN) |
239 | | } else { |
240 | 1.37k | start |
241 | | }; |
242 | 3.10k | start..end |
243 | 3.10k | } |
244 | | |
245 | 2.31k | pub fn glyph_class_def2_offset_byte_range(&self) -> Range<usize> { |
246 | 2.31k | let start = self.item_var_store_offset_byte_range().end; |
247 | 2.31k | let end = if self.version().compatible((1u16, 4u16)) { |
248 | 1.29k | start.saturating_add(Offset32::RAW_BYTE_LEN) |
249 | | } else { |
250 | 1.02k | start |
251 | | }; |
252 | 2.31k | start..end |
253 | 2.31k | } |
254 | | |
255 | 1.54k | pub fn attach_list2_offset_byte_range(&self) -> Range<usize> { |
256 | 1.54k | let start = self.glyph_class_def2_offset_byte_range().end; |
257 | 1.54k | let end = if self.version().compatible((1u16, 4u16)) { |
258 | 862 | start.saturating_add(Offset32::RAW_BYTE_LEN) |
259 | | } else { |
260 | 684 | start |
261 | | }; |
262 | 1.54k | start..end |
263 | 1.54k | } |
264 | | |
265 | 1.54k | pub fn lig_caret_list2_offset_byte_range(&self) -> Range<usize> { |
266 | 1.54k | let start = self.attach_list2_offset_byte_range().end; |
267 | 1.54k | let end = if self.version().compatible((1u16, 4u16)) { |
268 | 862 | start.saturating_add(Offset32::RAW_BYTE_LEN) |
269 | | } else { |
270 | 684 | start |
271 | | }; |
272 | 1.54k | start..end |
273 | 1.54k | } |
274 | | |
275 | 1.54k | pub fn mark_attach_class_def2_offset_byte_range(&self) -> Range<usize> { |
276 | 1.54k | let start = self.lig_caret_list2_offset_byte_range().end; |
277 | 1.54k | let end = if self.version().compatible((1u16, 4u16)) { |
278 | 862 | start.saturating_add(Offset32::RAW_BYTE_LEN) |
279 | | } else { |
280 | 684 | start |
281 | | }; |
282 | 1.54k | start..end |
283 | 1.54k | } |
284 | | |
285 | 773 | pub fn mark_glyph_sets_def2_offset_byte_range(&self) -> Range<usize> { |
286 | 773 | let start = self.mark_attach_class_def2_offset_byte_range().end; |
287 | 773 | let end = if self.version().compatible((1u16, 4u16)) { |
288 | 431 | start.saturating_add(Offset32::RAW_BYTE_LEN) |
289 | | } else { |
290 | 342 | start |
291 | | }; |
292 | 773 | start..end |
293 | 773 | } |
294 | | } |
295 | | |
296 | | const _: () = assert!(FontData::default_data_long_enough(Gdef::MIN_SIZE)); |
297 | | |
298 | | impl Default for Gdef<'_> { |
299 | 0 | fn default() -> Self { |
300 | | Self { |
301 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
302 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
303 | | } |
304 | 0 | } |
305 | | } |
306 | | |
307 | | /// Used in the [Glyph Class Definition Table](https://docs.microsoft.com/en-us/typography/opentype/spec/gdef#glyph-class-definition-table) |
308 | | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)] |
309 | | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
310 | | #[repr(u16)] |
311 | | #[allow(clippy::manual_non_exhaustive)] |
312 | | pub enum GlyphClassDef { |
313 | | #[default] |
314 | | Base = 1, |
315 | | Ligature = 2, |
316 | | Mark = 3, |
317 | | Component = 4, |
318 | | #[doc(hidden)] |
319 | | /// If font data is malformed we will map unknown values to this variant |
320 | | Unknown, |
321 | | } |
322 | | |
323 | | impl GlyphClassDef { |
324 | | /// Create from a raw scalar. |
325 | | /// |
326 | | /// This will never fail; unknown values will be mapped to the `Unknown` variant |
327 | 0 | pub fn new(raw: u16) -> Self { |
328 | 0 | match raw { |
329 | 0 | 1 => Self::Base, |
330 | 0 | 2 => Self::Ligature, |
331 | 0 | 3 => Self::Mark, |
332 | 0 | 4 => Self::Component, |
333 | 0 | _ => Self::Unknown, |
334 | | } |
335 | 0 | } |
336 | | } |
337 | | |
338 | | impl font_types::Scalar for GlyphClassDef { |
339 | | type Raw = <u16 as font_types::Scalar>::Raw; |
340 | 0 | fn to_raw(self) -> Self::Raw { |
341 | 0 | (self as u16).to_raw() |
342 | 0 | } |
343 | 0 | fn from_raw(raw: Self::Raw) -> Self { |
344 | 0 | let t = <u16>::from_raw(raw); |
345 | 0 | Self::new(t) |
346 | 0 | } |
347 | | } |
348 | | |
349 | | impl<'a> MinByteRange<'a> for AttachList<'a> { |
350 | 0 | fn min_byte_range(&self) -> Range<usize> { |
351 | 0 | 0..self.attach_point_offsets_byte_range().end |
352 | 0 | } |
353 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
354 | 0 | let range = self.min_byte_range(); |
355 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
356 | 0 | } |
357 | | } |
358 | | |
359 | | impl ReadArgs for AttachList<'_> { |
360 | | type Args = (); |
361 | | } |
362 | | |
363 | | impl<'a> FontRead<'a> for AttachList<'a> { |
364 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
365 | | #[allow(clippy::absurd_extreme_comparisons)] |
366 | 0 | if data.len() < Self::MIN_SIZE { |
367 | 0 | return Err(ReadError::OutOfBounds); |
368 | 0 | } |
369 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
370 | 0 | return Err(ReadError::OutOfBounds); |
371 | | }; |
372 | 0 | Ok(Self { data }) |
373 | 0 | } |
374 | | } |
375 | | |
376 | | /// The fields at the start of the table, which are read directly. |
377 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
378 | | #[repr(C, packed)] |
379 | | #[doc(hidden)] |
380 | | pub struct AttachListFixedHeader { |
381 | | pub coverage_offset: BigEndian<Offset16>, |
382 | | pub glyph_count: BigEndian<u16>, |
383 | | } |
384 | | |
385 | | #[allow(clippy::absurd_extreme_comparisons)] |
386 | | const _: () = assert!(std::mem::size_of::<AttachListFixedHeader>() <= AttachList::MIN_SIZE); |
387 | | |
388 | | /// [Attachment Point List Table](https://docs.microsoft.com/en-us/typography/opentype/spec/gdef#attachment-point-list-table) |
389 | | #[derive(Clone)] |
390 | | pub struct AttachList<'a> { |
391 | | data: TableDataRef<'a, AttachListFixedHeader>, |
392 | | } |
393 | | |
394 | | #[allow(clippy::needless_lifetimes)] |
395 | | impl<'a> AttachList<'a> { |
396 | | pub const MIN_SIZE: usize = (Offset16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
397 | | basic_table_impls!(impl_the_methods); |
398 | | |
399 | | /// Offset to Coverage table - from beginning of AttachList table |
400 | 0 | pub fn coverage_offset(&self) -> Offset16 { |
401 | 0 | self.data.header().coverage_offset.get() |
402 | 0 | } |
403 | | |
404 | | /// Attempt to resolve [`coverage_offset`][Self::coverage_offset]. |
405 | 0 | pub fn coverage(&self) -> Result<CoverageTable<'a>, ReadError> { |
406 | 0 | let data = self.offset_data(); |
407 | 0 | self.coverage_offset().resolve(data) |
408 | 0 | } |
409 | | |
410 | | /// Number of glyphs with attachment points |
411 | 0 | pub fn glyph_count(&self) -> u16 { |
412 | 0 | self.data.header().glyph_count.get() |
413 | 0 | } |
414 | | |
415 | | /// Array of offsets to AttachPoint tables-from beginning of |
416 | | /// AttachList table-in Coverage Index order |
417 | 0 | pub fn attach_point_offsets(&self) -> &'a [BigEndian<Offset16>] { |
418 | 0 | let range = self.attach_point_offsets_byte_range(); |
419 | 0 | self.offset_data() |
420 | 0 | .read_array(range) |
421 | 0 | .ok() |
422 | 0 | .unwrap_or_default() |
423 | 0 | } |
424 | | |
425 | | /// A dynamically resolving wrapper for [`attach_point_offsets`][Self::attach_point_offsets]. |
426 | 0 | pub fn attach_points(&self) -> ArrayOfOffsets<'a, AttachPoint<'a>, Offset16> { |
427 | 0 | let data = self.offset_data(); |
428 | 0 | let offsets = self.attach_point_offsets(); |
429 | 0 | ArrayOfOffsets::new(offsets, data, ()) |
430 | 0 | } |
431 | | |
432 | 0 | pub fn coverage_offset_byte_range(&self) -> Range<usize> { |
433 | 0 | let start = 0usize; |
434 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
435 | 0 | start..end |
436 | 0 | } |
437 | | |
438 | 0 | pub fn glyph_count_byte_range(&self) -> Range<usize> { |
439 | 0 | let start = self.coverage_offset_byte_range().end; |
440 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
441 | 0 | start..end |
442 | 0 | } |
443 | | |
444 | 0 | pub fn attach_point_offsets_byte_range(&self) -> Range<usize> { |
445 | 0 | let glyph_count = self.glyph_count(); |
446 | 0 | let start = self.glyph_count_byte_range().end; |
447 | 0 | let end = start.saturating_add( |
448 | 0 | (transforms::to_usize(glyph_count)).saturating_mul(Offset16::RAW_BYTE_LEN), |
449 | | ); |
450 | 0 | start..end |
451 | 0 | } |
452 | | } |
453 | | |
454 | | const _: () = assert!(FontData::default_data_long_enough(AttachList::MIN_SIZE)); |
455 | | |
456 | | impl Default for AttachList<'_> { |
457 | 0 | fn default() -> Self { |
458 | | Self { |
459 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
460 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
461 | | } |
462 | 0 | } |
463 | | } |
464 | | |
465 | | impl<'a> MinByteRange<'a> for AttachPoint<'a> { |
466 | 0 | fn min_byte_range(&self) -> Range<usize> { |
467 | 0 | 0..self.point_indices_byte_range().end |
468 | 0 | } |
469 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
470 | 0 | let range = self.min_byte_range(); |
471 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
472 | 0 | } |
473 | | } |
474 | | |
475 | | impl ReadArgs for AttachPoint<'_> { |
476 | | type Args = (); |
477 | | } |
478 | | |
479 | | impl<'a> FontRead<'a> for AttachPoint<'a> { |
480 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
481 | | #[allow(clippy::absurd_extreme_comparisons)] |
482 | 0 | if data.len() < Self::MIN_SIZE { |
483 | 0 | return Err(ReadError::OutOfBounds); |
484 | 0 | } |
485 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
486 | 0 | return Err(ReadError::OutOfBounds); |
487 | | }; |
488 | 0 | Ok(Self { data }) |
489 | 0 | } |
490 | | } |
491 | | |
492 | | /// The fields at the start of the table, which are read directly. |
493 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
494 | | #[repr(C, packed)] |
495 | | #[doc(hidden)] |
496 | | pub struct AttachPointFixedHeader { |
497 | | pub point_count: BigEndian<u16>, |
498 | | } |
499 | | |
500 | | #[allow(clippy::absurd_extreme_comparisons)] |
501 | | const _: () = assert!(std::mem::size_of::<AttachPointFixedHeader>() <= AttachPoint::MIN_SIZE); |
502 | | |
503 | | /// Part of [AttachList] |
504 | | #[derive(Clone)] |
505 | | pub struct AttachPoint<'a> { |
506 | | data: TableDataRef<'a, AttachPointFixedHeader>, |
507 | | } |
508 | | |
509 | | #[allow(clippy::needless_lifetimes)] |
510 | | impl<'a> AttachPoint<'a> { |
511 | | pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN; |
512 | | basic_table_impls!(impl_the_methods); |
513 | | |
514 | | /// Number of attachment points on this glyph |
515 | 0 | pub fn point_count(&self) -> u16 { |
516 | 0 | self.data.header().point_count.get() |
517 | 0 | } |
518 | | |
519 | | /// Array of contour point indices -in increasing numerical order |
520 | 0 | pub fn point_indices(&self) -> &'a [BigEndian<u16>] { |
521 | 0 | let range = self.point_indices_byte_range(); |
522 | 0 | self.offset_data() |
523 | 0 | .read_array(range) |
524 | 0 | .ok() |
525 | 0 | .unwrap_or_default() |
526 | 0 | } |
527 | | |
528 | 0 | pub fn point_count_byte_range(&self) -> Range<usize> { |
529 | 0 | let start = 0usize; |
530 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
531 | 0 | start..end |
532 | 0 | } |
533 | | |
534 | 0 | pub fn point_indices_byte_range(&self) -> Range<usize> { |
535 | 0 | let point_count = self.point_count(); |
536 | 0 | let start = self.point_count_byte_range().end; |
537 | 0 | let end = start |
538 | 0 | .saturating_add((transforms::to_usize(point_count)).saturating_mul(u16::RAW_BYTE_LEN)); |
539 | 0 | start..end |
540 | 0 | } |
541 | | } |
542 | | |
543 | | const _: () = assert!(FontData::default_data_long_enough(AttachPoint::MIN_SIZE)); |
544 | | |
545 | | impl Default for AttachPoint<'_> { |
546 | 0 | fn default() -> Self { |
547 | | Self { |
548 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
549 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
550 | | } |
551 | 0 | } |
552 | | } |
553 | | |
554 | | impl<'a> MinByteRange<'a> for LigCaretList<'a> { |
555 | 0 | fn min_byte_range(&self) -> Range<usize> { |
556 | 0 | 0..self.lig_glyph_offsets_byte_range().end |
557 | 0 | } |
558 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
559 | 0 | let range = self.min_byte_range(); |
560 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
561 | 0 | } |
562 | | } |
563 | | |
564 | | impl ReadArgs for LigCaretList<'_> { |
565 | | type Args = (); |
566 | | } |
567 | | |
568 | | impl<'a> FontRead<'a> for LigCaretList<'a> { |
569 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
570 | | #[allow(clippy::absurd_extreme_comparisons)] |
571 | 0 | if data.len() < Self::MIN_SIZE { |
572 | 0 | return Err(ReadError::OutOfBounds); |
573 | 0 | } |
574 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
575 | 0 | return Err(ReadError::OutOfBounds); |
576 | | }; |
577 | 0 | Ok(Self { data }) |
578 | 0 | } |
579 | | } |
580 | | |
581 | | /// The fields at the start of the table, which are read directly. |
582 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
583 | | #[repr(C, packed)] |
584 | | #[doc(hidden)] |
585 | | pub struct LigCaretListFixedHeader { |
586 | | pub coverage_offset: BigEndian<Offset16>, |
587 | | pub lig_glyph_count: BigEndian<u16>, |
588 | | } |
589 | | |
590 | | #[allow(clippy::absurd_extreme_comparisons)] |
591 | | const _: () = assert!(std::mem::size_of::<LigCaretListFixedHeader>() <= LigCaretList::MIN_SIZE); |
592 | | |
593 | | /// [Ligature Caret List Table](https://docs.microsoft.com/en-us/typography/opentype/spec/gdef#ligature-caret-list-table) |
594 | | #[derive(Clone)] |
595 | | pub struct LigCaretList<'a> { |
596 | | data: TableDataRef<'a, LigCaretListFixedHeader>, |
597 | | } |
598 | | |
599 | | #[allow(clippy::needless_lifetimes)] |
600 | | impl<'a> LigCaretList<'a> { |
601 | | pub const MIN_SIZE: usize = (Offset16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
602 | | basic_table_impls!(impl_the_methods); |
603 | | |
604 | | /// Offset to Coverage table - from beginning of LigCaretList table |
605 | 0 | pub fn coverage_offset(&self) -> Offset16 { |
606 | 0 | self.data.header().coverage_offset.get() |
607 | 0 | } |
608 | | |
609 | | /// Attempt to resolve [`coverage_offset`][Self::coverage_offset]. |
610 | 0 | pub fn coverage(&self) -> Result<CoverageTable<'a>, ReadError> { |
611 | 0 | let data = self.offset_data(); |
612 | 0 | self.coverage_offset().resolve(data) |
613 | 0 | } |
614 | | |
615 | | /// Number of ligature glyphs |
616 | 0 | pub fn lig_glyph_count(&self) -> u16 { |
617 | 0 | self.data.header().lig_glyph_count.get() |
618 | 0 | } |
619 | | |
620 | | /// Array of offsets to LigGlyph tables, from beginning of |
621 | | /// LigCaretList table —in Coverage Index order |
622 | 0 | pub fn lig_glyph_offsets(&self) -> &'a [BigEndian<Offset16>] { |
623 | 0 | let range = self.lig_glyph_offsets_byte_range(); |
624 | 0 | self.offset_data() |
625 | 0 | .read_array(range) |
626 | 0 | .ok() |
627 | 0 | .unwrap_or_default() |
628 | 0 | } |
629 | | |
630 | | /// A dynamically resolving wrapper for [`lig_glyph_offsets`][Self::lig_glyph_offsets]. |
631 | 0 | pub fn lig_glyphs(&self) -> ArrayOfOffsets<'a, LigGlyph<'a>, Offset16> { |
632 | 0 | let data = self.offset_data(); |
633 | 0 | let offsets = self.lig_glyph_offsets(); |
634 | 0 | ArrayOfOffsets::new(offsets, data, ()) |
635 | 0 | } |
636 | | |
637 | 0 | pub fn coverage_offset_byte_range(&self) -> Range<usize> { |
638 | 0 | let start = 0usize; |
639 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
640 | 0 | start..end |
641 | 0 | } |
642 | | |
643 | 0 | pub fn lig_glyph_count_byte_range(&self) -> Range<usize> { |
644 | 0 | let start = self.coverage_offset_byte_range().end; |
645 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
646 | 0 | start..end |
647 | 0 | } |
648 | | |
649 | 0 | pub fn lig_glyph_offsets_byte_range(&self) -> Range<usize> { |
650 | 0 | let lig_glyph_count = self.lig_glyph_count(); |
651 | 0 | let start = self.lig_glyph_count_byte_range().end; |
652 | 0 | let end = start.saturating_add( |
653 | 0 | (transforms::to_usize(lig_glyph_count)).saturating_mul(Offset16::RAW_BYTE_LEN), |
654 | | ); |
655 | 0 | start..end |
656 | 0 | } |
657 | | } |
658 | | |
659 | | const _: () = assert!(FontData::default_data_long_enough(LigCaretList::MIN_SIZE)); |
660 | | |
661 | | impl Default for LigCaretList<'_> { |
662 | 0 | fn default() -> Self { |
663 | | Self { |
664 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
665 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
666 | | } |
667 | 0 | } |
668 | | } |
669 | | |
670 | | impl<'a> MinByteRange<'a> for LigGlyph<'a> { |
671 | 0 | fn min_byte_range(&self) -> Range<usize> { |
672 | 0 | 0..self.caret_value_offsets_byte_range().end |
673 | 0 | } |
674 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
675 | 0 | let range = self.min_byte_range(); |
676 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
677 | 0 | } |
678 | | } |
679 | | |
680 | | impl ReadArgs for LigGlyph<'_> { |
681 | | type Args = (); |
682 | | } |
683 | | |
684 | | impl<'a> FontRead<'a> for LigGlyph<'a> { |
685 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
686 | | #[allow(clippy::absurd_extreme_comparisons)] |
687 | 0 | if data.len() < Self::MIN_SIZE { |
688 | 0 | return Err(ReadError::OutOfBounds); |
689 | 0 | } |
690 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
691 | 0 | return Err(ReadError::OutOfBounds); |
692 | | }; |
693 | 0 | Ok(Self { data }) |
694 | 0 | } |
695 | | } |
696 | | |
697 | | /// The fields at the start of the table, which are read directly. |
698 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
699 | | #[repr(C, packed)] |
700 | | #[doc(hidden)] |
701 | | pub struct LigGlyphFixedHeader { |
702 | | pub caret_count: BigEndian<u16>, |
703 | | } |
704 | | |
705 | | #[allow(clippy::absurd_extreme_comparisons)] |
706 | | const _: () = assert!(std::mem::size_of::<LigGlyphFixedHeader>() <= LigGlyph::MIN_SIZE); |
707 | | |
708 | | /// [Ligature Glyph Table](https://docs.microsoft.com/en-us/typography/opentype/spec/gdef#ligature-glyph-table) |
709 | | #[derive(Clone)] |
710 | | pub struct LigGlyph<'a> { |
711 | | data: TableDataRef<'a, LigGlyphFixedHeader>, |
712 | | } |
713 | | |
714 | | #[allow(clippy::needless_lifetimes)] |
715 | | impl<'a> LigGlyph<'a> { |
716 | | pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN; |
717 | | basic_table_impls!(impl_the_methods); |
718 | | |
719 | | /// Number of CaretValue tables for this ligature (components - 1) |
720 | 0 | pub fn caret_count(&self) -> u16 { |
721 | 0 | self.data.header().caret_count.get() |
722 | 0 | } |
723 | | |
724 | | /// Array of offsets to CaretValue tables, from beginning of |
725 | | /// LigGlyph table — in increasing coordinate order |
726 | 0 | pub fn caret_value_offsets(&self) -> &'a [BigEndian<Offset16>] { |
727 | 0 | let range = self.caret_value_offsets_byte_range(); |
728 | 0 | self.offset_data() |
729 | 0 | .read_array(range) |
730 | 0 | .ok() |
731 | 0 | .unwrap_or_default() |
732 | 0 | } |
733 | | |
734 | | /// A dynamically resolving wrapper for [`caret_value_offsets`][Self::caret_value_offsets]. |
735 | 0 | pub fn caret_values(&self) -> ArrayOfOffsets<'a, CaretValue<'a>, Offset16> { |
736 | 0 | let data = self.offset_data(); |
737 | 0 | let offsets = self.caret_value_offsets(); |
738 | 0 | ArrayOfOffsets::new(offsets, data, ()) |
739 | 0 | } |
740 | | |
741 | 0 | pub fn caret_count_byte_range(&self) -> Range<usize> { |
742 | 0 | let start = 0usize; |
743 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
744 | 0 | start..end |
745 | 0 | } |
746 | | |
747 | 0 | pub fn caret_value_offsets_byte_range(&self) -> Range<usize> { |
748 | 0 | let caret_count = self.caret_count(); |
749 | 0 | let start = self.caret_count_byte_range().end; |
750 | 0 | let end = start.saturating_add( |
751 | 0 | (transforms::to_usize(caret_count)).saturating_mul(Offset16::RAW_BYTE_LEN), |
752 | | ); |
753 | 0 | start..end |
754 | 0 | } |
755 | | } |
756 | | |
757 | | const _: () = assert!(FontData::default_data_long_enough(LigGlyph::MIN_SIZE)); |
758 | | |
759 | | impl Default for LigGlyph<'_> { |
760 | 0 | fn default() -> Self { |
761 | | Self { |
762 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
763 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
764 | | } |
765 | 0 | } |
766 | | } |
767 | | |
768 | | /// [Caret Value Tables](https://docs.microsoft.com/en-us/typography/opentype/spec/gdef#caret-value-tables) |
769 | | #[derive(Clone)] |
770 | | pub enum CaretValue<'a> { |
771 | | Format1(CaretValueFormat1<'a>), |
772 | | Format2(CaretValueFormat2<'a>), |
773 | | Format3(CaretValueFormat3<'a>), |
774 | | } |
775 | | |
776 | | impl Default for CaretValue<'_> { |
777 | 0 | fn default() -> Self { |
778 | 0 | Self::Format1(Default::default()) |
779 | 0 | } |
780 | | } |
781 | | |
782 | | impl<'a> CaretValue<'a> { |
783 | | ///Return the `FontData` used to resolve offsets for this table. |
784 | 0 | pub fn offset_data(&self) -> FontData<'a> { |
785 | 0 | match self { |
786 | 0 | Self::Format1(item) => item.offset_data(), |
787 | 0 | Self::Format2(item) => item.offset_data(), |
788 | 0 | Self::Format3(item) => item.offset_data(), |
789 | | } |
790 | 0 | } |
791 | | |
792 | | /// Format identifier: format = 1 |
793 | 0 | pub fn caret_value_format(&self) -> u16 { |
794 | 0 | match self { |
795 | 0 | Self::Format1(item) => item.caret_value_format(), |
796 | 0 | Self::Format2(item) => item.caret_value_format(), |
797 | 0 | Self::Format3(item) => item.caret_value_format(), |
798 | | } |
799 | 0 | } |
800 | | } |
801 | | |
802 | | impl ReadArgs for CaretValue<'_> { |
803 | | type Args = (); |
804 | | } |
805 | | |
806 | | impl<'a> FontRead<'a> for CaretValue<'a> { |
807 | | #[inline] |
808 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
809 | 0 | let format: u16 = data.read_at(0usize)?; |
810 | 0 | match format { |
811 | 0 | CaretValueFormat1::FORMAT => Ok(Self::Format1(FontRead::read(data)?)), |
812 | 0 | CaretValueFormat2::FORMAT => Ok(Self::Format2(FontRead::read(data)?)), |
813 | 0 | CaretValueFormat3::FORMAT => Ok(Self::Format3(FontRead::read(data)?)), |
814 | 0 | other => Err(ReadError::InvalidFormat(other.into())), |
815 | | } |
816 | 0 | } |
817 | | } |
818 | | |
819 | | impl<'a> MinByteRange<'a> for CaretValue<'a> { |
820 | 0 | fn min_byte_range(&self) -> Range<usize> { |
821 | 0 | match self { |
822 | 0 | Self::Format1(item) => item.min_byte_range(), |
823 | 0 | Self::Format2(item) => item.min_byte_range(), |
824 | 0 | Self::Format3(item) => item.min_byte_range(), |
825 | | } |
826 | 0 | } |
827 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
828 | 0 | match self { |
829 | 0 | Self::Format1(item) => item.min_table_bytes(), |
830 | 0 | Self::Format2(item) => item.min_table_bytes(), |
831 | 0 | Self::Format3(item) => item.min_table_bytes(), |
832 | | } |
833 | 0 | } |
834 | | } |
835 | | |
836 | | impl Format<u16> for CaretValueFormat1<'_> { |
837 | | const FORMAT: u16 = 1; |
838 | | } |
839 | | |
840 | | impl<'a> MinByteRange<'a> for CaretValueFormat1<'a> { |
841 | 0 | fn min_byte_range(&self) -> Range<usize> { |
842 | 0 | 0..self.coordinate_byte_range().end |
843 | 0 | } |
844 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
845 | 0 | let range = self.min_byte_range(); |
846 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
847 | 0 | } |
848 | | } |
849 | | |
850 | | impl ReadArgs for CaretValueFormat1<'_> { |
851 | | type Args = (); |
852 | | } |
853 | | |
854 | | impl<'a> FontRead<'a> for CaretValueFormat1<'a> { |
855 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
856 | | #[allow(clippy::absurd_extreme_comparisons)] |
857 | 0 | if data.len() < Self::MIN_SIZE { |
858 | 0 | return Err(ReadError::OutOfBounds); |
859 | 0 | } |
860 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
861 | 0 | return Err(ReadError::OutOfBounds); |
862 | | }; |
863 | 0 | Ok(Self { data }) |
864 | 0 | } |
865 | | } |
866 | | |
867 | | /// The fields at the start of the table, which are read directly. |
868 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
869 | | #[repr(C, packed)] |
870 | | #[doc(hidden)] |
871 | | pub struct CaretValueFormat1FixedHeader { |
872 | | pub caret_value_format: BigEndian<u16>, |
873 | | pub coordinate: BigEndian<i16>, |
874 | | } |
875 | | |
876 | | #[allow(clippy::absurd_extreme_comparisons)] |
877 | | const _: () = |
878 | | assert!(std::mem::size_of::<CaretValueFormat1FixedHeader>() <= CaretValueFormat1::MIN_SIZE); |
879 | | |
880 | | /// [CaretValue Format 1](https://docs.microsoft.com/en-us/typography/opentype/spec/gdef#caretvalue-format-1) |
881 | | #[derive(Clone)] |
882 | | pub struct CaretValueFormat1<'a> { |
883 | | data: TableDataRef<'a, CaretValueFormat1FixedHeader>, |
884 | | } |
885 | | |
886 | | #[allow(clippy::needless_lifetimes)] |
887 | | impl<'a> CaretValueFormat1<'a> { |
888 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN); |
889 | | basic_table_impls!(impl_the_methods); |
890 | | |
891 | | /// Format identifier: format = 1 |
892 | 0 | pub fn caret_value_format(&self) -> u16 { |
893 | 0 | self.data.header().caret_value_format.get() |
894 | 0 | } |
895 | | |
896 | | /// X or Y value, in design units |
897 | 0 | pub fn coordinate(&self) -> i16 { |
898 | 0 | self.data.header().coordinate.get() |
899 | 0 | } |
900 | | |
901 | 0 | pub fn caret_value_format_byte_range(&self) -> Range<usize> { |
902 | 0 | let start = 0usize; |
903 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
904 | 0 | start..end |
905 | 0 | } |
906 | | |
907 | 0 | pub fn coordinate_byte_range(&self) -> Range<usize> { |
908 | 0 | let start = self.caret_value_format_byte_range().end; |
909 | 0 | let end = start.saturating_add(i16::RAW_BYTE_LEN); |
910 | 0 | start..end |
911 | 0 | } |
912 | | } |
913 | | |
914 | | const _: () = assert!(FontData::default_data_long_enough( |
915 | | CaretValueFormat1::MIN_SIZE |
916 | | )); |
917 | | |
918 | | impl Default for CaretValueFormat1<'_> { |
919 | 0 | fn default() -> Self { |
920 | | Self { |
921 | 0 | data: TableDataRef::new(FontData::default_format_1_u16_table_data().as_bytes()) |
922 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
923 | | } |
924 | 0 | } |
925 | | } |
926 | | |
927 | | impl Format<u16> for CaretValueFormat2<'_> { |
928 | | const FORMAT: u16 = 2; |
929 | | } |
930 | | |
931 | | impl<'a> MinByteRange<'a> for CaretValueFormat2<'a> { |
932 | 0 | fn min_byte_range(&self) -> Range<usize> { |
933 | 0 | 0..self.caret_value_point_index_byte_range().end |
934 | 0 | } |
935 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
936 | 0 | let range = self.min_byte_range(); |
937 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
938 | 0 | } |
939 | | } |
940 | | |
941 | | impl ReadArgs for CaretValueFormat2<'_> { |
942 | | type Args = (); |
943 | | } |
944 | | |
945 | | impl<'a> FontRead<'a> for CaretValueFormat2<'a> { |
946 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
947 | | #[allow(clippy::absurd_extreme_comparisons)] |
948 | 0 | if data.len() < Self::MIN_SIZE { |
949 | 0 | return Err(ReadError::OutOfBounds); |
950 | 0 | } |
951 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
952 | 0 | return Err(ReadError::OutOfBounds); |
953 | | }; |
954 | 0 | Ok(Self { data }) |
955 | 0 | } |
956 | | } |
957 | | |
958 | | /// The fields at the start of the table, which are read directly. |
959 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
960 | | #[repr(C, packed)] |
961 | | #[doc(hidden)] |
962 | | pub struct CaretValueFormat2FixedHeader { |
963 | | pub caret_value_format: BigEndian<u16>, |
964 | | pub caret_value_point_index: BigEndian<u16>, |
965 | | } |
966 | | |
967 | | #[allow(clippy::absurd_extreme_comparisons)] |
968 | | const _: () = |
969 | | assert!(std::mem::size_of::<CaretValueFormat2FixedHeader>() <= CaretValueFormat2::MIN_SIZE); |
970 | | |
971 | | /// [CaretValue Format 2](https://docs.microsoft.com/en-us/typography/opentype/spec/gdef#caretvalue-format-2) |
972 | | #[derive(Clone)] |
973 | | pub struct CaretValueFormat2<'a> { |
974 | | data: TableDataRef<'a, CaretValueFormat2FixedHeader>, |
975 | | } |
976 | | |
977 | | #[allow(clippy::needless_lifetimes)] |
978 | | impl<'a> CaretValueFormat2<'a> { |
979 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
980 | | basic_table_impls!(impl_the_methods); |
981 | | |
982 | | /// Format identifier: format = 2 |
983 | 0 | pub fn caret_value_format(&self) -> u16 { |
984 | 0 | self.data.header().caret_value_format.get() |
985 | 0 | } |
986 | | |
987 | | /// Contour point index on glyph |
988 | 0 | pub fn caret_value_point_index(&self) -> u16 { |
989 | 0 | self.data.header().caret_value_point_index.get() |
990 | 0 | } |
991 | | |
992 | 0 | pub fn caret_value_format_byte_range(&self) -> Range<usize> { |
993 | 0 | let start = 0usize; |
994 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
995 | 0 | start..end |
996 | 0 | } |
997 | | |
998 | 0 | pub fn caret_value_point_index_byte_range(&self) -> Range<usize> { |
999 | 0 | let start = self.caret_value_format_byte_range().end; |
1000 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1001 | 0 | start..end |
1002 | 0 | } |
1003 | | } |
1004 | | |
1005 | | impl Format<u16> for CaretValueFormat3<'_> { |
1006 | | const FORMAT: u16 = 3; |
1007 | | } |
1008 | | |
1009 | | impl<'a> MinByteRange<'a> for CaretValueFormat3<'a> { |
1010 | 0 | fn min_byte_range(&self) -> Range<usize> { |
1011 | 0 | 0..self.device_offset_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 CaretValueFormat3<'_> { |
1020 | | type Args = (); |
1021 | | } |
1022 | | |
1023 | | impl<'a> FontRead<'a> for CaretValueFormat3<'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 CaretValueFormat3FixedHeader { |
1041 | | pub caret_value_format: BigEndian<u16>, |
1042 | | pub coordinate: BigEndian<i16>, |
1043 | | pub device_offset: BigEndian<Offset16>, |
1044 | | } |
1045 | | |
1046 | | #[allow(clippy::absurd_extreme_comparisons)] |
1047 | | const _: () = |
1048 | | assert!(std::mem::size_of::<CaretValueFormat3FixedHeader>() <= CaretValueFormat3::MIN_SIZE); |
1049 | | |
1050 | | /// [CaretValue Format 3](https://docs.microsoft.com/en-us/typography/opentype/spec/gdef#caretvalue-format-3) |
1051 | | #[derive(Clone)] |
1052 | | pub struct CaretValueFormat3<'a> { |
1053 | | data: TableDataRef<'a, CaretValueFormat3FixedHeader>, |
1054 | | } |
1055 | | |
1056 | | #[allow(clippy::needless_lifetimes)] |
1057 | | impl<'a> CaretValueFormat3<'a> { |
1058 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN); |
1059 | | basic_table_impls!(impl_the_methods); |
1060 | | |
1061 | | /// Format identifier-format = 3 |
1062 | 0 | pub fn caret_value_format(&self) -> u16 { |
1063 | 0 | self.data.header().caret_value_format.get() |
1064 | 0 | } |
1065 | | |
1066 | | /// X or Y value, in design units |
1067 | 0 | pub fn coordinate(&self) -> i16 { |
1068 | 0 | self.data.header().coordinate.get() |
1069 | 0 | } |
1070 | | |
1071 | | /// Offset to Device table (non-variable font) / Variation Index |
1072 | | /// table (variable font) for X or Y value-from beginning of |
1073 | | /// CaretValue table |
1074 | 0 | pub fn device_offset(&self) -> Offset16 { |
1075 | 0 | self.data.header().device_offset.get() |
1076 | 0 | } |
1077 | | |
1078 | | /// Attempt to resolve [`device_offset`][Self::device_offset]. |
1079 | 0 | pub fn device(&self) -> Result<DeviceOrVariationIndex<'a>, ReadError> { |
1080 | 0 | let data = self.offset_data(); |
1081 | 0 | self.device_offset().resolve(data) |
1082 | 0 | } |
1083 | | |
1084 | 0 | pub fn caret_value_format_byte_range(&self) -> Range<usize> { |
1085 | 0 | let start = 0usize; |
1086 | 0 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1087 | 0 | start..end |
1088 | 0 | } |
1089 | | |
1090 | 0 | pub fn coordinate_byte_range(&self) -> Range<usize> { |
1091 | 0 | let start = self.caret_value_format_byte_range().end; |
1092 | 0 | let end = start.saturating_add(i16::RAW_BYTE_LEN); |
1093 | 0 | start..end |
1094 | 0 | } |
1095 | | |
1096 | 0 | pub fn device_offset_byte_range(&self) -> Range<usize> { |
1097 | 0 | let start = self.coordinate_byte_range().end; |
1098 | 0 | let end = start.saturating_add(Offset16::RAW_BYTE_LEN); |
1099 | 0 | start..end |
1100 | 0 | } |
1101 | | } |
1102 | | |
1103 | | impl Format<u16> for MarkGlyphSets<'_> { |
1104 | | const FORMAT: u16 = 1; |
1105 | | } |
1106 | | |
1107 | | impl<'a> MinByteRange<'a> for MarkGlyphSets<'a> { |
1108 | 0 | fn min_byte_range(&self) -> Range<usize> { |
1109 | 0 | 0..self.coverage_offsets_byte_range().end |
1110 | 0 | } |
1111 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
1112 | 0 | let range = self.min_byte_range(); |
1113 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
1114 | 0 | } |
1115 | | } |
1116 | | |
1117 | | impl ReadArgs for MarkGlyphSets<'_> { |
1118 | | type Args = (); |
1119 | | } |
1120 | | |
1121 | | impl<'a> FontRead<'a> for MarkGlyphSets<'a> { |
1122 | 321 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
1123 | | #[allow(clippy::absurd_extreme_comparisons)] |
1124 | 321 | if data.len() < Self::MIN_SIZE { |
1125 | 1 | return Err(ReadError::OutOfBounds); |
1126 | 320 | } |
1127 | 320 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
1128 | 0 | return Err(ReadError::OutOfBounds); |
1129 | | }; |
1130 | 320 | Ok(Self { data }) |
1131 | 321 | } |
1132 | | } |
1133 | | |
1134 | | /// The fields at the start of the table, which are read directly. |
1135 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
1136 | | #[repr(C, packed)] |
1137 | | #[doc(hidden)] |
1138 | | pub struct MarkGlyphSetsFixedHeader { |
1139 | | pub format: BigEndian<u16>, |
1140 | | pub mark_glyph_set_count: BigEndian<u16>, |
1141 | | } |
1142 | | |
1143 | | #[allow(clippy::absurd_extreme_comparisons)] |
1144 | | const _: () = assert!(std::mem::size_of::<MarkGlyphSetsFixedHeader>() <= MarkGlyphSets::MIN_SIZE); |
1145 | | |
1146 | | /// [Mark Glyph Sets Table](https://docs.microsoft.com/en-us/typography/opentype/spec/gdef#mark-glyph-sets-table) |
1147 | | #[derive(Clone)] |
1148 | | pub struct MarkGlyphSets<'a> { |
1149 | | data: TableDataRef<'a, MarkGlyphSetsFixedHeader>, |
1150 | | } |
1151 | | |
1152 | | #[allow(clippy::needless_lifetimes)] |
1153 | | impl<'a> MarkGlyphSets<'a> { |
1154 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
1155 | | basic_table_impls!(impl_the_methods); |
1156 | | |
1157 | | /// Format identifier == 1 |
1158 | 0 | pub fn format(&self) -> u16 { |
1159 | 0 | self.data.header().format.get() |
1160 | 0 | } |
1161 | | |
1162 | | /// Number of mark glyph sets defined |
1163 | 320 | pub fn mark_glyph_set_count(&self) -> u16 { |
1164 | 320 | self.data.header().mark_glyph_set_count.get() |
1165 | 320 | } |
1166 | | |
1167 | | /// Array of offsets to mark glyph set coverage tables, from the |
1168 | | /// start of the MarkGlyphSets table. |
1169 | 320 | pub fn coverage_offsets(&self) -> &'a [BigEndian<Offset32>] { |
1170 | 320 | let range = self.coverage_offsets_byte_range(); |
1171 | 320 | self.offset_data() |
1172 | 320 | .read_array(range) |
1173 | 320 | .ok() |
1174 | 320 | .unwrap_or_default() |
1175 | 320 | } |
1176 | | |
1177 | | /// A dynamically resolving wrapper for [`coverage_offsets`][Self::coverage_offsets]. |
1178 | 320 | pub fn coverages(&self) -> ArrayOfOffsets<'a, CoverageTable<'a>, Offset32> { |
1179 | 320 | let data = self.offset_data(); |
1180 | 320 | let offsets = self.coverage_offsets(); |
1181 | 320 | ArrayOfOffsets::new(offsets, data, ()) |
1182 | 320 | } |
1183 | | |
1184 | 320 | pub fn format_byte_range(&self) -> Range<usize> { |
1185 | 320 | let start = 0usize; |
1186 | 320 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1187 | 320 | start..end |
1188 | 320 | } |
1189 | | |
1190 | 320 | pub fn mark_glyph_set_count_byte_range(&self) -> Range<usize> { |
1191 | 320 | let start = self.format_byte_range().end; |
1192 | 320 | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1193 | 320 | start..end |
1194 | 320 | } |
1195 | | |
1196 | 320 | pub fn coverage_offsets_byte_range(&self) -> Range<usize> { |
1197 | 320 | let mark_glyph_set_count = self.mark_glyph_set_count(); |
1198 | 320 | let start = self.mark_glyph_set_count_byte_range().end; |
1199 | 320 | let end = start.saturating_add( |
1200 | 320 | (transforms::to_usize(mark_glyph_set_count)).saturating_mul(Offset32::RAW_BYTE_LEN), |
1201 | | ); |
1202 | 320 | start..end |
1203 | 320 | } |
1204 | | } |
1205 | | |
1206 | | const _: () = assert!(FontData::default_data_long_enough(MarkGlyphSets::MIN_SIZE)); |
1207 | | |
1208 | | impl Default for MarkGlyphSets<'_> { |
1209 | 0 | fn default() -> Self { |
1210 | | Self { |
1211 | 0 | data: TableDataRef::new(FontData::default_format_1_u16_table_data().as_bytes()) |
1212 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
1213 | | } |
1214 | 0 | } |
1215 | | } |
1216 | | |
1217 | | impl<'a> MinByteRange<'a> for LigCaretList2<'a> { |
1218 | 0 | fn min_byte_range(&self) -> Range<usize> { |
1219 | 0 | 0..self.lig_glyph_offsets_byte_range().end |
1220 | 0 | } |
1221 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
1222 | 0 | let range = self.min_byte_range(); |
1223 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
1224 | 0 | } |
1225 | | } |
1226 | | |
1227 | | impl ReadArgs for LigCaretList2<'_> { |
1228 | | type Args = (); |
1229 | | } |
1230 | | |
1231 | | impl<'a> FontRead<'a> for LigCaretList2<'a> { |
1232 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
1233 | | #[allow(clippy::absurd_extreme_comparisons)] |
1234 | 0 | if data.len() < Self::MIN_SIZE { |
1235 | 0 | return Err(ReadError::OutOfBounds); |
1236 | 0 | } |
1237 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
1238 | 0 | return Err(ReadError::OutOfBounds); |
1239 | | }; |
1240 | 0 | Ok(Self { data }) |
1241 | 0 | } |
1242 | | } |
1243 | | |
1244 | | /// The fields at the start of the table, which are read directly. |
1245 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
1246 | | #[repr(C, packed)] |
1247 | | #[doc(hidden)] |
1248 | | pub struct LigCaretList2FixedHeader { |
1249 | | pub coverage_offset: BigEndian<Offset32>, |
1250 | | pub lig_glyph_count: BigEndian<Uint24>, |
1251 | | } |
1252 | | |
1253 | | #[allow(clippy::absurd_extreme_comparisons)] |
1254 | | const _: () = assert!(std::mem::size_of::<LigCaretList2FixedHeader>() <= LigCaretList2::MIN_SIZE); |
1255 | | |
1256 | | /// LigCaretList2 (ISO/IEC 14496-22:2026). |
1257 | | #[derive(Clone)] |
1258 | | pub struct LigCaretList2<'a> { |
1259 | | data: TableDataRef<'a, LigCaretList2FixedHeader>, |
1260 | | } |
1261 | | |
1262 | | #[allow(clippy::needless_lifetimes)] |
1263 | | impl<'a> LigCaretList2<'a> { |
1264 | | pub const MIN_SIZE: usize = (Offset32::RAW_BYTE_LEN + Uint24::RAW_BYTE_LEN); |
1265 | | basic_table_impls!(impl_the_methods); |
1266 | | |
1267 | | /// Offset to Coverage table - from beginning of LigCaretList2 table |
1268 | 0 | pub fn coverage_offset(&self) -> Offset32 { |
1269 | 0 | self.data.header().coverage_offset.get() |
1270 | 0 | } |
1271 | | |
1272 | | /// Attempt to resolve [`coverage_offset`][Self::coverage_offset]. |
1273 | 0 | pub fn coverage(&self) -> Result<CoverageTable<'a>, ReadError> { |
1274 | 0 | let data = self.offset_data(); |
1275 | 0 | self.coverage_offset().resolve(data) |
1276 | 0 | } |
1277 | | |
1278 | | /// Number of ligature glyphs |
1279 | 0 | pub fn lig_glyph_count(&self) -> Uint24 { |
1280 | 0 | self.data.header().lig_glyph_count.get() |
1281 | 0 | } |
1282 | | |
1283 | | /// Array of offsets to LigGlyph tables, from beginning of |
1284 | | /// LigCaretList2 table —in Coverage Index order |
1285 | 0 | pub fn lig_glyph_offsets(&self) -> &'a [BigEndian<Offset24>] { |
1286 | 0 | let range = self.lig_glyph_offsets_byte_range(); |
1287 | 0 | self.offset_data() |
1288 | 0 | .read_array(range) |
1289 | 0 | .ok() |
1290 | 0 | .unwrap_or_default() |
1291 | 0 | } |
1292 | | |
1293 | | /// A dynamically resolving wrapper for [`lig_glyph_offsets`][Self::lig_glyph_offsets]. |
1294 | 0 | pub fn lig_glyphs(&self) -> ArrayOfOffsets<'a, LigGlyph<'a>, Offset24> { |
1295 | 0 | let data = self.offset_data(); |
1296 | 0 | let offsets = self.lig_glyph_offsets(); |
1297 | 0 | ArrayOfOffsets::new(offsets, data, ()) |
1298 | 0 | } |
1299 | | |
1300 | 0 | pub fn coverage_offset_byte_range(&self) -> Range<usize> { |
1301 | 0 | let start = 0usize; |
1302 | 0 | let end = start.saturating_add(Offset32::RAW_BYTE_LEN); |
1303 | 0 | start..end |
1304 | 0 | } |
1305 | | |
1306 | 0 | pub fn lig_glyph_count_byte_range(&self) -> Range<usize> { |
1307 | 0 | let start = self.coverage_offset_byte_range().end; |
1308 | 0 | let end = start.saturating_add(Uint24::RAW_BYTE_LEN); |
1309 | 0 | start..end |
1310 | 0 | } |
1311 | | |
1312 | 0 | pub fn lig_glyph_offsets_byte_range(&self) -> Range<usize> { |
1313 | 0 | let lig_glyph_count = self.lig_glyph_count(); |
1314 | 0 | let start = self.lig_glyph_count_byte_range().end; |
1315 | 0 | let end = start.saturating_add( |
1316 | 0 | (transforms::to_usize(lig_glyph_count)).saturating_mul(Offset24::RAW_BYTE_LEN), |
1317 | | ); |
1318 | 0 | start..end |
1319 | 0 | } |
1320 | | } |
1321 | | |
1322 | | const _: () = assert!(FontData::default_data_long_enough(LigCaretList2::MIN_SIZE)); |
1323 | | |
1324 | | impl Default for LigCaretList2<'_> { |
1325 | 0 | fn default() -> Self { |
1326 | | Self { |
1327 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
1328 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
1329 | | } |
1330 | 0 | } |
1331 | | } |