/rust/registry/src/index.crates.io-1949cf8c6b5b557f/read-fonts-0.47.0/generated/generated_variations.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 TupleVariationHeader<'a> { |
9 | 0 | fn min_byte_range(&self) -> Range<usize> { |
10 | 0 | 0..self.intermediate_end_tuple_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 ReadArgs for TupleVariationHeader<'_> { |
19 | | type Args = u16; |
20 | | } |
21 | | |
22 | | impl<'a> FontRead<'a> for TupleVariationHeader<'a> { |
23 | 5.33M | fn read_with_args(data: FontData<'a>, args: u16) -> Result<Self, ReadError> { |
24 | 5.33M | let axis_count = args; |
25 | | |
26 | | #[allow(clippy::absurd_extreme_comparisons)] |
27 | 5.33M | if data.len() < Self::MIN_SIZE { |
28 | 13.3k | return Err(ReadError::OutOfBounds); |
29 | 5.32M | } |
30 | 5.32M | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
31 | 0 | return Err(ReadError::OutOfBounds); |
32 | | }; |
33 | 5.32M | Ok(Self { data, axis_count }) |
34 | 5.33M | } |
35 | | } |
36 | | |
37 | | impl<'a> TupleVariationHeader<'a> { |
38 | | /// A constructor that requires additional arguments. |
39 | | /// |
40 | | /// This type requires some external state in order to be |
41 | | /// parsed. |
42 | 5.33M | pub fn read(data: FontData<'a>, axis_count: u16) -> Result<Self, ReadError> { |
43 | 5.33M | let args = axis_count; |
44 | 5.33M | Self::read_with_args(data, args) |
45 | 5.33M | } |
46 | | } |
47 | | |
48 | | /// The fields at the start of the table, which are read directly. |
49 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
50 | | #[repr(C, packed)] |
51 | | #[doc(hidden)] |
52 | | pub struct TupleVariationHeaderFixedHeader { |
53 | | pub variation_data_size: BigEndian<u16>, |
54 | | pub tuple_index: BigEndian<TupleIndex>, |
55 | | } |
56 | | |
57 | | #[allow(clippy::absurd_extreme_comparisons)] |
58 | | const _: () = assert!( |
59 | | std::mem::size_of::<TupleVariationHeaderFixedHeader>() <= TupleVariationHeader::MIN_SIZE |
60 | | ); |
61 | | |
62 | | /// [TupleVariationHeader](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuplevariationheader) |
63 | | #[derive(Clone)] |
64 | | pub struct TupleVariationHeader<'a> { |
65 | | data: TableDataRef<'a, TupleVariationHeaderFixedHeader>, |
66 | | axis_count: u16, |
67 | | } |
68 | | |
69 | | #[allow(clippy::needless_lifetimes)] |
70 | | impl<'a> TupleVariationHeader<'a> { |
71 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + TupleIndex::RAW_BYTE_LEN); |
72 | | basic_table_impls!(impl_the_methods); |
73 | | |
74 | | /// The size in bytes of the serialized data for this tuple |
75 | | /// variation table. |
76 | 5.31M | pub fn variation_data_size(&self) -> u16 { |
77 | 5.31M | self.data.header().variation_data_size.get() |
78 | 5.31M | } |
79 | | |
80 | | /// A packed field. The high 4 bits are flags (see below). The low |
81 | | /// 12 bits are an index into a shared tuple records array. |
82 | 25.4M | pub fn tuple_index(&self) -> TupleIndex { |
83 | 25.4M | self.data.header().tuple_index.get() |
84 | 25.4M | } |
85 | | |
86 | 7.80M | pub(crate) fn axis_count(&self) -> u16 { |
87 | 7.80M | self.axis_count |
88 | 7.80M | } |
89 | | |
90 | 3.67M | pub fn variation_data_size_byte_range(&self) -> Range<usize> { |
91 | 3.67M | let start = 0usize; |
92 | 3.67M | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
93 | 3.67M | start..end |
94 | 3.67M | } |
95 | | |
96 | 3.67M | pub fn tuple_index_byte_range(&self) -> Range<usize> { |
97 | 3.67M | let start = self.variation_data_size_byte_range().end; |
98 | 3.67M | let end = start.saturating_add(TupleIndex::RAW_BYTE_LEN); |
99 | 3.67M | start..end |
100 | 3.67M | } |
101 | | |
102 | 3.67M | pub fn peak_tuple_byte_range(&self) -> Range<usize> { |
103 | 3.67M | let tuple_index = self.tuple_index(); |
104 | 3.67M | let axis_count = self.axis_count(); |
105 | 3.67M | let start = self.tuple_index_byte_range().end; |
106 | 3.67M | let end = start.saturating_add( |
107 | 3.67M | (TupleIndex::tuple_len(tuple_index, axis_count, 0_usize)) |
108 | 3.67M | .saturating_mul(F2Dot14::RAW_BYTE_LEN), |
109 | | ); |
110 | 3.67M | start..end |
111 | 3.67M | } |
112 | | |
113 | 2.75M | pub fn intermediate_start_tuple_byte_range(&self) -> Range<usize> { |
114 | 2.75M | let tuple_index = self.tuple_index(); |
115 | 2.75M | let axis_count = self.axis_count(); |
116 | 2.75M | let start = self.peak_tuple_byte_range().end; |
117 | 2.75M | let end = start.saturating_add( |
118 | 2.75M | (TupleIndex::tuple_len(tuple_index, axis_count, 1_usize)) |
119 | 2.75M | .saturating_mul(F2Dot14::RAW_BYTE_LEN), |
120 | | ); |
121 | 2.75M | start..end |
122 | 2.75M | } |
123 | | |
124 | 1.37M | pub fn intermediate_end_tuple_byte_range(&self) -> Range<usize> { |
125 | 1.37M | let tuple_index = self.tuple_index(); |
126 | 1.37M | let axis_count = self.axis_count(); |
127 | 1.37M | let start = self.intermediate_start_tuple_byte_range().end; |
128 | 1.37M | let end = start.saturating_add( |
129 | 1.37M | (TupleIndex::tuple_len(tuple_index, axis_count, 1_usize)) |
130 | 1.37M | .saturating_mul(F2Dot14::RAW_BYTE_LEN), |
131 | | ); |
132 | 1.37M | start..end |
133 | 1.37M | } |
134 | | } |
135 | | |
136 | | const _: () = assert!(FontData::default_data_long_enough( |
137 | | TupleVariationHeader::MIN_SIZE |
138 | | )); |
139 | | |
140 | | impl Default for TupleVariationHeader<'_> { |
141 | 0 | fn default() -> Self { |
142 | | Self { |
143 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
144 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
145 | 0 | axis_count: Default::default(), |
146 | | } |
147 | 0 | } |
148 | | } |
149 | | |
150 | | /// A [Tuple Record](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuple-records) |
151 | | /// |
152 | | /// The tuple variation store formats reference regions within the font’s |
153 | | /// variation space using tuple records. A tuple record identifies a position |
154 | | /// in terms of normalized coordinates, which use F2DOT14 values. |
155 | | #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
156 | | pub struct Tuple<'a> { |
157 | | /// Coordinate array specifying a position within the font’s variation space. |
158 | | /// |
159 | | /// The number of elements must match the axisCount specified in the |
160 | | /// 'fvar' table. |
161 | | pub values: &'a [BigEndian<F2Dot14>], |
162 | | } |
163 | | |
164 | | impl<'a> Tuple<'a> { |
165 | | /// Coordinate array specifying a position within the font’s variation space. |
166 | | /// |
167 | | /// The number of elements must match the axisCount specified in the |
168 | | /// 'fvar' table. |
169 | 976k | pub fn values(&self) -> &'a [BigEndian<F2Dot14>] { |
170 | 976k | self.values |
171 | 976k | } |
172 | | } |
173 | | |
174 | | impl ReadArgs for Tuple<'_> { |
175 | | type Args = u16; |
176 | | } |
177 | | |
178 | | impl ComputeSize for Tuple<'_> { |
179 | | #[allow(clippy::needless_question_mark)] |
180 | 708k | fn compute_size(args: u16) -> Result<usize, ReadError> { |
181 | 708k | let axis_count = args; |
182 | 708k | Ok((transforms::to_usize(axis_count)).saturating_mul(F2Dot14::RAW_BYTE_LEN)) |
183 | 708k | } |
184 | | } |
185 | | |
186 | | impl<'a> FontRead<'a> for Tuple<'a> { |
187 | 52.2k | fn read_with_args(data: FontData<'a>, args: u16) -> Result<Self, ReadError> { |
188 | 52.2k | let mut cursor = data.cursor(); |
189 | 52.2k | let axis_count = args; |
190 | | Ok(Self { |
191 | 52.2k | values: cursor.read_array(transforms::to_usize(axis_count))?, |
192 | | }) |
193 | 52.2k | } |
194 | | } |
195 | | |
196 | | crate::impl_font_read_at!(Tuple<'a>); |
197 | | |
198 | | #[allow(clippy::needless_lifetimes)] |
199 | | impl<'a> Tuple<'a> { |
200 | | /// A constructor that requires additional arguments. |
201 | | /// |
202 | | /// This type requires some external state in order to be |
203 | | /// parsed. |
204 | 0 | pub fn read(data: FontData<'a>, axis_count: u16) -> Result<Self, ReadError> { |
205 | 0 | let args = axis_count; |
206 | 0 | Self::read_with_args(data, args) |
207 | 0 | } |
208 | | } |
209 | | |
210 | | impl Format<u8> for DeltaSetIndexMapFormat0<'_> { |
211 | | const FORMAT: u8 = 0; |
212 | | } |
213 | | |
214 | | impl<'a> MinByteRange<'a> for DeltaSetIndexMapFormat0<'a> { |
215 | 0 | fn min_byte_range(&self) -> Range<usize> { |
216 | 0 | 0..self.map_data_byte_range().end |
217 | 0 | } |
218 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
219 | 0 | let range = self.min_byte_range(); |
220 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
221 | 0 | } |
222 | | } |
223 | | |
224 | | impl ReadArgs for DeltaSetIndexMapFormat0<'_> { |
225 | | type Args = (); |
226 | | } |
227 | | |
228 | | impl<'a> FontRead<'a> for DeltaSetIndexMapFormat0<'a> { |
229 | 104k | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
230 | | #[allow(clippy::absurd_extreme_comparisons)] |
231 | 104k | if data.len() < Self::MIN_SIZE { |
232 | 4.68k | return Err(ReadError::OutOfBounds); |
233 | 99.9k | } |
234 | 99.9k | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
235 | 0 | return Err(ReadError::OutOfBounds); |
236 | | }; |
237 | 99.9k | Ok(Self { data }) |
238 | 104k | } |
239 | | } |
240 | | |
241 | | /// The fields at the start of the table, which are read directly. |
242 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
243 | | #[repr(C, packed)] |
244 | | #[doc(hidden)] |
245 | | pub struct DeltaSetIndexMapFormat0FixedHeader { |
246 | | pub format: u8, |
247 | | pub entry_format: BigEndian<EntryFormat>, |
248 | | pub map_count: BigEndian<u16>, |
249 | | } |
250 | | |
251 | | #[allow(clippy::absurd_extreme_comparisons)] |
252 | | const _: () = assert!( |
253 | | std::mem::size_of::<DeltaSetIndexMapFormat0FixedHeader>() <= DeltaSetIndexMapFormat0::MIN_SIZE |
254 | | ); |
255 | | |
256 | | /// The [DeltaSetIndexMap](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#associating-target-items-to-variation-data) table format 0 |
257 | | #[derive(Clone)] |
258 | | pub struct DeltaSetIndexMapFormat0<'a> { |
259 | | data: TableDataRef<'a, DeltaSetIndexMapFormat0FixedHeader>, |
260 | | } |
261 | | |
262 | | #[allow(clippy::needless_lifetimes)] |
263 | | impl<'a> DeltaSetIndexMapFormat0<'a> { |
264 | | pub const MIN_SIZE: usize = (u8::RAW_BYTE_LEN + EntryFormat::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
265 | | basic_table_impls!(impl_the_methods); |
266 | | |
267 | | /// DeltaSetIndexMap format: set to 0. |
268 | 0 | pub fn format(&self) -> u8 { |
269 | 0 | self.data.header().format |
270 | 0 | } |
271 | | |
272 | | /// A packed field that describes the compressed representation of |
273 | | /// delta-set indices. See details below. |
274 | 250k | pub fn entry_format(&self) -> EntryFormat { |
275 | 250k | self.data.header().entry_format.get() |
276 | 250k | } |
277 | | |
278 | | /// The number of mapping entries. |
279 | 250k | pub fn map_count(&self) -> u16 { |
280 | 250k | self.data.header().map_count.get() |
281 | 250k | } |
282 | | |
283 | | /// The delta-set index mapping data. See details below. |
284 | 125k | pub fn map_data(&self) -> &'a [u8] { |
285 | 125k | let range = self.map_data_byte_range(); |
286 | 125k | self.offset_data() |
287 | 125k | .read_array(range) |
288 | 125k | .ok() |
289 | 125k | .unwrap_or_default() |
290 | 125k | } |
291 | | |
292 | 125k | pub fn format_byte_range(&self) -> Range<usize> { |
293 | 125k | let start = 0usize; |
294 | 125k | let end = start.saturating_add(u8::RAW_BYTE_LEN); |
295 | 125k | start..end |
296 | 125k | } |
297 | | |
298 | 125k | pub fn entry_format_byte_range(&self) -> Range<usize> { |
299 | 125k | let start = self.format_byte_range().end; |
300 | 125k | let end = start.saturating_add(EntryFormat::RAW_BYTE_LEN); |
301 | 125k | start..end |
302 | 125k | } |
303 | | |
304 | 125k | pub fn map_count_byte_range(&self) -> Range<usize> { |
305 | 125k | let start = self.entry_format_byte_range().end; |
306 | 125k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
307 | 125k | start..end |
308 | 125k | } |
309 | | |
310 | 125k | pub fn map_data_byte_range(&self) -> Range<usize> { |
311 | 125k | let entry_format = self.entry_format(); |
312 | 125k | let map_count = self.map_count(); |
313 | 125k | let start = self.map_count_byte_range().end; |
314 | 125k | let end = start.saturating_add( |
315 | 125k | (EntryFormat::map_size(entry_format, map_count)).saturating_mul(u8::RAW_BYTE_LEN), |
316 | | ); |
317 | 125k | start..end |
318 | 125k | } |
319 | | } |
320 | | |
321 | | const _: () = assert!(FontData::default_data_long_enough( |
322 | | DeltaSetIndexMapFormat0::MIN_SIZE |
323 | | )); |
324 | | |
325 | | impl Default for DeltaSetIndexMapFormat0<'_> { |
326 | 0 | fn default() -> Self { |
327 | | Self { |
328 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
329 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
330 | | } |
331 | 0 | } |
332 | | } |
333 | | |
334 | | impl Format<u8> for DeltaSetIndexMapFormat1<'_> { |
335 | | const FORMAT: u8 = 1; |
336 | | } |
337 | | |
338 | | impl<'a> MinByteRange<'a> for DeltaSetIndexMapFormat1<'a> { |
339 | 0 | fn min_byte_range(&self) -> Range<usize> { |
340 | 0 | 0..self.map_data_byte_range().end |
341 | 0 | } |
342 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
343 | 0 | let range = self.min_byte_range(); |
344 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
345 | 0 | } |
346 | | } |
347 | | |
348 | | impl ReadArgs for DeltaSetIndexMapFormat1<'_> { |
349 | | type Args = (); |
350 | | } |
351 | | |
352 | | impl<'a> FontRead<'a> for DeltaSetIndexMapFormat1<'a> { |
353 | 101k | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
354 | | #[allow(clippy::absurd_extreme_comparisons)] |
355 | 101k | if data.len() < Self::MIN_SIZE { |
356 | 106 | return Err(ReadError::OutOfBounds); |
357 | 101k | } |
358 | 101k | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
359 | 0 | return Err(ReadError::OutOfBounds); |
360 | | }; |
361 | 101k | Ok(Self { data }) |
362 | 101k | } |
363 | | } |
364 | | |
365 | | /// The fields at the start of the table, which are read directly. |
366 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
367 | | #[repr(C, packed)] |
368 | | #[doc(hidden)] |
369 | | pub struct DeltaSetIndexMapFormat1FixedHeader { |
370 | | pub format: u8, |
371 | | pub entry_format: BigEndian<EntryFormat>, |
372 | | pub map_count: BigEndian<u32>, |
373 | | } |
374 | | |
375 | | #[allow(clippy::absurd_extreme_comparisons)] |
376 | | const _: () = assert!( |
377 | | std::mem::size_of::<DeltaSetIndexMapFormat1FixedHeader>() <= DeltaSetIndexMapFormat1::MIN_SIZE |
378 | | ); |
379 | | |
380 | | /// The [DeltaSetIndexMap](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#associating-target-items-to-variation-data) table format 1 |
381 | | #[derive(Clone)] |
382 | | pub struct DeltaSetIndexMapFormat1<'a> { |
383 | | data: TableDataRef<'a, DeltaSetIndexMapFormat1FixedHeader>, |
384 | | } |
385 | | |
386 | | #[allow(clippy::needless_lifetimes)] |
387 | | impl<'a> DeltaSetIndexMapFormat1<'a> { |
388 | | pub const MIN_SIZE: usize = (u8::RAW_BYTE_LEN + EntryFormat::RAW_BYTE_LEN + u32::RAW_BYTE_LEN); |
389 | | basic_table_impls!(impl_the_methods); |
390 | | |
391 | | /// DeltaSetIndexMap format: set to 1. |
392 | 0 | pub fn format(&self) -> u8 { |
393 | 0 | self.data.header().format |
394 | 0 | } |
395 | | |
396 | | /// A packed field that describes the compressed representation of |
397 | | /// delta-set indices. See details below. |
398 | 216k | pub fn entry_format(&self) -> EntryFormat { |
399 | 216k | self.data.header().entry_format.get() |
400 | 216k | } |
401 | | |
402 | | /// The number of mapping entries. |
403 | 216k | pub fn map_count(&self) -> u32 { |
404 | 216k | self.data.header().map_count.get() |
405 | 216k | } |
406 | | |
407 | | /// The delta-set index mapping data. See details below. |
408 | 108k | pub fn map_data(&self) -> &'a [u8] { |
409 | 108k | let range = self.map_data_byte_range(); |
410 | 108k | self.offset_data() |
411 | 108k | .read_array(range) |
412 | 108k | .ok() |
413 | 108k | .unwrap_or_default() |
414 | 108k | } |
415 | | |
416 | 108k | pub fn format_byte_range(&self) -> Range<usize> { |
417 | 108k | let start = 0usize; |
418 | 108k | let end = start.saturating_add(u8::RAW_BYTE_LEN); |
419 | 108k | start..end |
420 | 108k | } |
421 | | |
422 | 108k | pub fn entry_format_byte_range(&self) -> Range<usize> { |
423 | 108k | let start = self.format_byte_range().end; |
424 | 108k | let end = start.saturating_add(EntryFormat::RAW_BYTE_LEN); |
425 | 108k | start..end |
426 | 108k | } |
427 | | |
428 | 108k | pub fn map_count_byte_range(&self) -> Range<usize> { |
429 | 108k | let start = self.entry_format_byte_range().end; |
430 | 108k | let end = start.saturating_add(u32::RAW_BYTE_LEN); |
431 | 108k | start..end |
432 | 108k | } |
433 | | |
434 | 108k | pub fn map_data_byte_range(&self) -> Range<usize> { |
435 | 108k | let entry_format = self.entry_format(); |
436 | 108k | let map_count = self.map_count(); |
437 | 108k | let start = self.map_count_byte_range().end; |
438 | 108k | let end = start.saturating_add( |
439 | 108k | (EntryFormat::map_size(entry_format, map_count)).saturating_mul(u8::RAW_BYTE_LEN), |
440 | | ); |
441 | 108k | start..end |
442 | 108k | } |
443 | | } |
444 | | |
445 | | const _: () = assert!(FontData::default_data_long_enough( |
446 | | DeltaSetIndexMapFormat1::MIN_SIZE |
447 | | )); |
448 | | |
449 | | impl Default for DeltaSetIndexMapFormat1<'_> { |
450 | 0 | fn default() -> Self { |
451 | | Self { |
452 | 0 | data: TableDataRef::new(FontData::default_format_1_u8_table_data().as_bytes()) |
453 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
454 | | } |
455 | 0 | } |
456 | | } |
457 | | |
458 | | /// The [DeltaSetIndexMap](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#associating-target-items-to-variation-data) table |
459 | | #[derive(Clone)] |
460 | | pub enum DeltaSetIndexMap<'a> { |
461 | | Format0(DeltaSetIndexMapFormat0<'a>), |
462 | | Format1(DeltaSetIndexMapFormat1<'a>), |
463 | | } |
464 | | |
465 | | impl Default for DeltaSetIndexMap<'_> { |
466 | 0 | fn default() -> Self { |
467 | 0 | Self::Format0(Default::default()) |
468 | 0 | } |
469 | | } |
470 | | |
471 | | impl<'a> DeltaSetIndexMap<'a> { |
472 | | ///Return the `FontData` used to resolve offsets for this table. |
473 | 0 | pub fn offset_data(&self) -> FontData<'a> { |
474 | 0 | match self { |
475 | 0 | Self::Format0(item) => item.offset_data(), |
476 | 0 | Self::Format1(item) => item.offset_data(), |
477 | | } |
478 | 0 | } |
479 | | |
480 | | /// DeltaSetIndexMap format: set to 0. |
481 | 0 | pub fn format(&self) -> u8 { |
482 | 0 | match self { |
483 | 0 | Self::Format0(item) => item.format(), |
484 | 0 | Self::Format1(item) => item.format(), |
485 | | } |
486 | 0 | } |
487 | | |
488 | | /// A packed field that describes the compressed representation of |
489 | | /// delta-set indices. See details below. |
490 | 0 | pub fn entry_format(&self) -> EntryFormat { |
491 | 0 | match self { |
492 | 0 | Self::Format0(item) => item.entry_format(), |
493 | 0 | Self::Format1(item) => item.entry_format(), |
494 | | } |
495 | 0 | } |
496 | | |
497 | | /// The delta-set index mapping data. See details below. |
498 | 0 | pub fn map_data(&self) -> &'a [u8] { |
499 | 0 | match self { |
500 | 0 | Self::Format0(item) => item.map_data(), |
501 | 0 | Self::Format1(item) => item.map_data(), |
502 | | } |
503 | 0 | } |
504 | | } |
505 | | |
506 | | impl ReadArgs for DeltaSetIndexMap<'_> { |
507 | | type Args = (); |
508 | | } |
509 | | |
510 | | impl<'a> FontRead<'a> for DeltaSetIndexMap<'a> { |
511 | | #[inline] |
512 | 277k | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
513 | 277k | let format: u8 = data.read_at(0usize)?; |
514 | 246k | match format { |
515 | 104k | DeltaSetIndexMapFormat0::FORMAT => Ok(Self::Format0(FontRead::read(data)?)), |
516 | 101k | DeltaSetIndexMapFormat1::FORMAT => Ok(Self::Format1(FontRead::read(data)?)), |
517 | 39.7k | other => Err(ReadError::InvalidFormat(other.into())), |
518 | | } |
519 | 277k | } |
520 | | } |
521 | | |
522 | | impl<'a> MinByteRange<'a> for DeltaSetIndexMap<'a> { |
523 | 0 | fn min_byte_range(&self) -> Range<usize> { |
524 | 0 | match self { |
525 | 0 | Self::Format0(item) => item.min_byte_range(), |
526 | 0 | Self::Format1(item) => item.min_byte_range(), |
527 | | } |
528 | 0 | } |
529 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
530 | 0 | match self { |
531 | 0 | Self::Format0(item) => item.min_table_bytes(), |
532 | 0 | Self::Format1(item) => item.min_table_bytes(), |
533 | | } |
534 | 0 | } |
535 | | } |
536 | | |
537 | | /// Entry format for a [DeltaSetIndexMap]. |
538 | | #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)] |
539 | | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
540 | | #[repr(transparent)] |
541 | | pub struct EntryFormat { |
542 | | bits: u8, |
543 | | } |
544 | | |
545 | | impl EntryFormat { |
546 | | /// Mask for the low 4 bits, which give the count of bits minus one that are used in each entry for the inner-level index. |
547 | | pub const INNER_INDEX_BIT_COUNT_MASK: Self = Self { bits: 0x0F }; |
548 | | |
549 | | /// Mask for bits that indicate the size in bytes minus one of each entry. |
550 | | pub const MAP_ENTRY_SIZE_MASK: Self = Self { bits: 0x30 }; |
551 | | } |
552 | | |
553 | | impl EntryFormat { |
554 | | /// Returns an empty set of flags. |
555 | | #[inline] |
556 | 0 | pub const fn empty() -> Self { |
557 | 0 | Self { bits: 0 } |
558 | 0 | } |
559 | | |
560 | | /// Returns the set containing all flags. |
561 | | #[inline] |
562 | 466k | pub const fn all() -> Self { |
563 | 466k | Self { |
564 | 466k | bits: Self::INNER_INDEX_BIT_COUNT_MASK.bits | Self::MAP_ENTRY_SIZE_MASK.bits, |
565 | 466k | } |
566 | 466k | } |
567 | | |
568 | | /// Returns the raw value of the flags currently stored. |
569 | | #[inline] |
570 | 918k | pub const fn bits(&self) -> u8 { |
571 | 918k | self.bits |
572 | 918k | } |
573 | | |
574 | | /// Convert from underlying bit representation, unless that |
575 | | /// representation contains bits that do not correspond to a flag. |
576 | | #[inline] |
577 | 0 | pub const fn from_bits(bits: u8) -> Option<Self> { |
578 | 0 | if (bits & !Self::all().bits()) == 0 { |
579 | 0 | Some(Self { bits }) |
580 | | } else { |
581 | 0 | None |
582 | | } |
583 | 0 | } |
584 | | |
585 | | /// Convert from underlying bit representation, dropping any bits |
586 | | /// that do not correspond to flags. |
587 | | #[inline] |
588 | 466k | pub const fn from_bits_truncate(bits: u8) -> Self { |
589 | 466k | Self { |
590 | 466k | bits: bits & Self::all().bits, |
591 | 466k | } |
592 | 466k | } |
593 | | |
594 | | /// Returns `true` if no flags are currently stored. |
595 | | #[inline] |
596 | 0 | pub const fn is_empty(&self) -> bool { |
597 | 0 | self.bits() == Self::empty().bits() |
598 | 0 | } |
599 | | |
600 | | /// Returns `true` if there are flags common to both `self` and `other`. |
601 | | #[inline] |
602 | 0 | pub const fn intersects(&self, other: Self) -> bool { |
603 | 0 | !(Self { |
604 | 0 | bits: self.bits & other.bits, |
605 | 0 | }) |
606 | 0 | .is_empty() |
607 | 0 | } |
608 | | |
609 | | /// Returns `true` if all of the flags in `other` are contained within `self`. |
610 | | #[inline] |
611 | 0 | pub const fn contains(&self, other: Self) -> bool { |
612 | 0 | (self.bits & other.bits) == other.bits |
613 | 0 | } |
614 | | |
615 | | /// Inserts the specified flags in-place. |
616 | | #[inline] |
617 | 0 | pub fn insert(&mut self, other: Self) { |
618 | 0 | self.bits |= other.bits; |
619 | 0 | } |
620 | | |
621 | | /// Removes the specified flags in-place. |
622 | | #[inline] |
623 | 0 | pub fn remove(&mut self, other: Self) { |
624 | 0 | self.bits &= !other.bits; |
625 | 0 | } |
626 | | |
627 | | /// Toggles the specified flags in-place. |
628 | | #[inline] |
629 | 0 | pub fn toggle(&mut self, other: Self) { |
630 | 0 | self.bits ^= other.bits; |
631 | 0 | } |
632 | | |
633 | | /// Returns the intersection between the flags in `self` and |
634 | | /// `other`. |
635 | | /// |
636 | | /// Specifically, the returned set contains only the flags which are |
637 | | /// present in *both* `self` *and* `other`. |
638 | | /// |
639 | | /// This is equivalent to using the `&` operator (e.g. |
640 | | /// [`ops::BitAnd`]), as in `flags & other`. |
641 | | /// |
642 | | /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html |
643 | | #[inline] |
644 | | #[must_use] |
645 | 0 | pub const fn intersection(self, other: Self) -> Self { |
646 | 0 | Self { |
647 | 0 | bits: self.bits & other.bits, |
648 | 0 | } |
649 | 0 | } |
650 | | |
651 | | /// Returns the union of between the flags in `self` and `other`. |
652 | | /// |
653 | | /// Specifically, the returned set contains all flags which are |
654 | | /// present in *either* `self` *or* `other`, including any which are |
655 | | /// present in both. |
656 | | /// |
657 | | /// This is equivalent to using the `|` operator (e.g. |
658 | | /// [`ops::BitOr`]), as in `flags | other`. |
659 | | /// |
660 | | /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html |
661 | | #[inline] |
662 | | #[must_use] |
663 | 0 | pub const fn union(self, other: Self) -> Self { |
664 | 0 | Self { |
665 | 0 | bits: self.bits | other.bits, |
666 | 0 | } |
667 | 0 | } |
668 | | |
669 | | /// Returns the difference between the flags in `self` and `other`. |
670 | | /// |
671 | | /// Specifically, the returned set contains all flags present in |
672 | | /// `self`, except for the ones present in `other`. |
673 | | /// |
674 | | /// It is also conceptually equivalent to the "bit-clear" operation: |
675 | | /// `flags & !other` (and this syntax is also supported). |
676 | | /// |
677 | | /// This is equivalent to using the `-` operator (e.g. |
678 | | /// [`ops::Sub`]), as in `flags - other`. |
679 | | /// |
680 | | /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html |
681 | | #[inline] |
682 | | #[must_use] |
683 | 0 | pub const fn difference(self, other: Self) -> Self { |
684 | 0 | Self { |
685 | 0 | bits: self.bits & !other.bits, |
686 | 0 | } |
687 | 0 | } |
688 | | } |
689 | | |
690 | | impl std::ops::BitOr for EntryFormat { |
691 | | type Output = Self; |
692 | | |
693 | | /// Returns the union of the two sets of flags. |
694 | | #[inline] |
695 | 0 | fn bitor(self, other: EntryFormat) -> Self { |
696 | 0 | Self { |
697 | 0 | bits: self.bits | other.bits, |
698 | 0 | } |
699 | 0 | } |
700 | | } |
701 | | |
702 | | impl std::ops::BitOrAssign for EntryFormat { |
703 | | /// Adds the set of flags. |
704 | | #[inline] |
705 | 0 | fn bitor_assign(&mut self, other: Self) { |
706 | 0 | self.bits |= other.bits; |
707 | 0 | } |
708 | | } |
709 | | |
710 | | impl std::ops::BitXor for EntryFormat { |
711 | | type Output = Self; |
712 | | |
713 | | /// Returns the left flags, but with all the right flags toggled. |
714 | | #[inline] |
715 | 0 | fn bitxor(self, other: Self) -> Self { |
716 | 0 | Self { |
717 | 0 | bits: self.bits ^ other.bits, |
718 | 0 | } |
719 | 0 | } |
720 | | } |
721 | | |
722 | | impl std::ops::BitXorAssign for EntryFormat { |
723 | | /// Toggles the set of flags. |
724 | | #[inline] |
725 | 0 | fn bitxor_assign(&mut self, other: Self) { |
726 | 0 | self.bits ^= other.bits; |
727 | 0 | } |
728 | | } |
729 | | |
730 | | impl std::ops::BitAnd for EntryFormat { |
731 | | type Output = Self; |
732 | | |
733 | | /// Returns the intersection between the two sets of flags. |
734 | | #[inline] |
735 | 0 | fn bitand(self, other: Self) -> Self { |
736 | 0 | Self { |
737 | 0 | bits: self.bits & other.bits, |
738 | 0 | } |
739 | 0 | } |
740 | | } |
741 | | |
742 | | impl std::ops::BitAndAssign for EntryFormat { |
743 | | /// Disables all flags disabled in the set. |
744 | | #[inline] |
745 | 0 | fn bitand_assign(&mut self, other: Self) { |
746 | 0 | self.bits &= other.bits; |
747 | 0 | } |
748 | | } |
749 | | |
750 | | impl std::ops::Sub for EntryFormat { |
751 | | type Output = Self; |
752 | | |
753 | | /// Returns the set difference of the two sets of flags. |
754 | | #[inline] |
755 | 0 | fn sub(self, other: Self) -> Self { |
756 | 0 | Self { |
757 | 0 | bits: self.bits & !other.bits, |
758 | 0 | } |
759 | 0 | } |
760 | | } |
761 | | |
762 | | impl std::ops::SubAssign for EntryFormat { |
763 | | /// Disables all flags enabled in the set. |
764 | | #[inline] |
765 | 0 | fn sub_assign(&mut self, other: Self) { |
766 | 0 | self.bits &= !other.bits; |
767 | 0 | } |
768 | | } |
769 | | |
770 | | impl std::ops::Not for EntryFormat { |
771 | | type Output = Self; |
772 | | |
773 | | /// Returns the complement of this set of flags. |
774 | | #[inline] |
775 | 0 | fn not(self) -> Self { |
776 | 0 | Self { bits: !self.bits } & Self::all() |
777 | 0 | } |
778 | | } |
779 | | |
780 | | impl std::fmt::Debug for EntryFormat { |
781 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
782 | 0 | let members: &[(&str, Self)] = &[ |
783 | 0 | ( |
784 | 0 | "INNER_INDEX_BIT_COUNT_MASK", |
785 | 0 | Self::INNER_INDEX_BIT_COUNT_MASK, |
786 | 0 | ), |
787 | 0 | ("MAP_ENTRY_SIZE_MASK", Self::MAP_ENTRY_SIZE_MASK), |
788 | 0 | ]; |
789 | 0 | let mut first = true; |
790 | 0 | for (name, value) in members { |
791 | 0 | if self.contains(*value) { |
792 | 0 | if !first { |
793 | 0 | f.write_str(" | ")?; |
794 | 0 | } |
795 | 0 | first = false; |
796 | 0 | f.write_str(name)?; |
797 | 0 | } |
798 | | } |
799 | 0 | if first { |
800 | 0 | f.write_str("(empty)")?; |
801 | 0 | } |
802 | 0 | Ok(()) |
803 | 0 | } |
804 | | } |
805 | | |
806 | | impl std::fmt::Binary for EntryFormat { |
807 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
808 | 0 | std::fmt::Binary::fmt(&self.bits, f) |
809 | 0 | } |
810 | | } |
811 | | |
812 | | impl std::fmt::Octal for EntryFormat { |
813 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
814 | 0 | std::fmt::Octal::fmt(&self.bits, f) |
815 | 0 | } |
816 | | } |
817 | | |
818 | | impl std::fmt::LowerHex for EntryFormat { |
819 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
820 | 0 | std::fmt::LowerHex::fmt(&self.bits, f) |
821 | 0 | } |
822 | | } |
823 | | |
824 | | impl std::fmt::UpperHex for EntryFormat { |
825 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
826 | 0 | std::fmt::UpperHex::fmt(&self.bits, f) |
827 | 0 | } |
828 | | } |
829 | | |
830 | | impl font_types::Scalar for EntryFormat { |
831 | | type Raw = <u8 as font_types::Scalar>::Raw; |
832 | 0 | fn to_raw(self) -> Self::Raw { |
833 | 0 | self.bits().to_raw() |
834 | 0 | } |
835 | 466k | fn from_raw(raw: Self::Raw) -> Self { |
836 | 466k | let t = <u8>::from_raw(raw); |
837 | 466k | Self::from_bits_truncate(t) |
838 | 466k | } |
839 | | } |
840 | | |
841 | | impl<'a> MinByteRange<'a> for VariationRegionList<'a> { |
842 | 0 | fn min_byte_range(&self) -> Range<usize> { |
843 | 0 | 0..self.variation_regions_byte_range().end |
844 | 0 | } |
845 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
846 | 0 | let range = self.min_byte_range(); |
847 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
848 | 0 | } |
849 | | } |
850 | | |
851 | | impl ReadArgs for VariationRegionList<'_> { |
852 | | type Args = (); |
853 | | } |
854 | | |
855 | | impl<'a> FontRead<'a> for VariationRegionList<'a> { |
856 | 1.55k | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
857 | | #[allow(clippy::absurd_extreme_comparisons)] |
858 | 1.55k | if data.len() < Self::MIN_SIZE { |
859 | 20 | return Err(ReadError::OutOfBounds); |
860 | 1.53k | } |
861 | 1.53k | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
862 | 0 | return Err(ReadError::OutOfBounds); |
863 | | }; |
864 | 1.53k | Ok(Self { data }) |
865 | 1.55k | } |
866 | | } |
867 | | |
868 | | /// The fields at the start of the table, which are read directly. |
869 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
870 | | #[repr(C, packed)] |
871 | | #[doc(hidden)] |
872 | | pub struct VariationRegionListFixedHeader { |
873 | | pub axis_count: BigEndian<u16>, |
874 | | pub region_count: BigEndian<u16>, |
875 | | } |
876 | | |
877 | | #[allow(clippy::absurd_extreme_comparisons)] |
878 | | const _: () = |
879 | | assert!(std::mem::size_of::<VariationRegionListFixedHeader>() <= VariationRegionList::MIN_SIZE); |
880 | | |
881 | | /// The [VariationRegionList](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#variation-regions) table |
882 | | #[derive(Clone)] |
883 | | pub struct VariationRegionList<'a> { |
884 | | data: TableDataRef<'a, VariationRegionListFixedHeader>, |
885 | | } |
886 | | |
887 | | #[allow(clippy::needless_lifetimes)] |
888 | | impl<'a> VariationRegionList<'a> { |
889 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
890 | | basic_table_impls!(impl_the_methods); |
891 | | |
892 | | /// The number of variation axes for this font. This must be the |
893 | | /// same number as axisCount in the 'fvar' table. |
894 | 3.06k | pub fn axis_count(&self) -> u16 { |
895 | 3.06k | self.data.header().axis_count.get() |
896 | 3.06k | } |
897 | | |
898 | | /// The number of variation region tables in the variation region |
899 | | /// list. Must be less than 32,768. |
900 | 1.53k | pub fn region_count(&self) -> u16 { |
901 | 1.53k | self.data.header().region_count.get() |
902 | 1.53k | } |
903 | | |
904 | | /// Array of variation regions. |
905 | 1.53k | pub fn variation_regions(&self) -> ComputedArray<'a, VariationRegion<'a>> { |
906 | 1.53k | let range = self.variation_regions_byte_range(); |
907 | 1.53k | ComputedArray::new(self.offset_data(), range, self.axis_count()).unwrap_or_default() |
908 | 1.53k | } |
909 | | |
910 | 1.53k | pub fn axis_count_byte_range(&self) -> Range<usize> { |
911 | 1.53k | let start = 0usize; |
912 | 1.53k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
913 | 1.53k | start..end |
914 | 1.53k | } |
915 | | |
916 | 1.53k | pub fn region_count_byte_range(&self) -> Range<usize> { |
917 | 1.53k | let start = self.axis_count_byte_range().end; |
918 | 1.53k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
919 | 1.53k | start..end |
920 | 1.53k | } |
921 | | |
922 | 1.53k | pub fn variation_regions_byte_range(&self) -> Range<usize> { |
923 | 1.53k | let region_count = self.region_count(); |
924 | 1.53k | let start = self.region_count_byte_range().end; |
925 | 1.53k | let end = start.saturating_add((transforms::to_usize(region_count)).saturating_mul( |
926 | 1.53k | <VariationRegion as ComputeSize>::compute_size(self.axis_count()).unwrap_or(0), |
927 | | )); |
928 | 1.53k | start..end |
929 | 1.53k | } |
930 | | } |
931 | | |
932 | | const _: () = assert!(FontData::default_data_long_enough( |
933 | | VariationRegionList::MIN_SIZE |
934 | | )); |
935 | | |
936 | | impl Default for VariationRegionList<'_> { |
937 | 0 | fn default() -> Self { |
938 | | Self { |
939 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
940 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
941 | | } |
942 | 0 | } |
943 | | } |
944 | | |
945 | | /// The [VariationRegion](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#variation-regions) record |
946 | | #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
947 | | pub struct VariationRegion<'a> { |
948 | | /// Array of region axis coordinates records, in the order of axes |
949 | | /// given in the 'fvar' table. |
950 | | pub region_axes: &'a [RegionAxisCoordinates], |
951 | | } |
952 | | |
953 | | impl<'a> VariationRegion<'a> { |
954 | | /// Array of region axis coordinates records, in the order of axes |
955 | | /// given in the 'fvar' table. |
956 | 2.49k | pub fn region_axes(&self) -> &'a [RegionAxisCoordinates] { |
957 | 2.49k | self.region_axes |
958 | 2.49k | } |
959 | | } |
960 | | |
961 | | impl ReadArgs for VariationRegion<'_> { |
962 | | type Args = u16; |
963 | | } |
964 | | |
965 | | impl ComputeSize for VariationRegion<'_> { |
966 | | #[allow(clippy::needless_question_mark)] |
967 | 5.61k | fn compute_size(args: u16) -> Result<usize, ReadError> { |
968 | 5.61k | let axis_count = args; |
969 | 5.61k | Ok((transforms::to_usize(axis_count)).saturating_mul(RegionAxisCoordinates::RAW_BYTE_LEN)) |
970 | 5.61k | } |
971 | | } |
972 | | |
973 | | impl<'a> FontRead<'a> for VariationRegion<'a> { |
974 | 2.55k | fn read_with_args(data: FontData<'a>, args: u16) -> Result<Self, ReadError> { |
975 | 2.55k | let mut cursor = data.cursor(); |
976 | 2.55k | let axis_count = args; |
977 | | Ok(Self { |
978 | 2.55k | region_axes: cursor.read_array(transforms::to_usize(axis_count))?, |
979 | | }) |
980 | 2.55k | } |
981 | | } |
982 | | |
983 | | crate::impl_font_read_at!(VariationRegion<'a>); |
984 | | |
985 | | #[allow(clippy::needless_lifetimes)] |
986 | | impl<'a> VariationRegion<'a> { |
987 | | /// A constructor that requires additional arguments. |
988 | | /// |
989 | | /// This type requires some external state in order to be |
990 | | /// parsed. |
991 | 0 | pub fn read(data: FontData<'a>, axis_count: u16) -> Result<Self, ReadError> { |
992 | 0 | let args = axis_count; |
993 | 0 | Self::read_with_args(data, args) |
994 | 0 | } |
995 | | } |
996 | | |
997 | | /// The [RegionAxisCoordinates](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#variation-regions) record |
998 | | #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)] |
999 | | #[repr(C)] |
1000 | | #[repr(packed)] |
1001 | | pub struct RegionAxisCoordinates { |
1002 | | /// The region start coordinate value for the current axis. |
1003 | | pub start_coord: BigEndian<F2Dot14>, |
1004 | | /// The region peak coordinate value for the current axis. |
1005 | | pub peak_coord: BigEndian<F2Dot14>, |
1006 | | /// The region end coordinate value for the current axis. |
1007 | | pub end_coord: BigEndian<F2Dot14>, |
1008 | | } |
1009 | | |
1010 | | impl RegionAxisCoordinates { |
1011 | | /// The region start coordinate value for the current axis. |
1012 | 0 | pub fn start_coord(&self) -> F2Dot14 { |
1013 | 0 | self.start_coord.get() |
1014 | 0 | } |
1015 | | |
1016 | | /// The region peak coordinate value for the current axis. |
1017 | 23.2k | pub fn peak_coord(&self) -> F2Dot14 { |
1018 | 23.2k | self.peak_coord.get() |
1019 | 23.2k | } |
1020 | | |
1021 | | /// The region end coordinate value for the current axis. |
1022 | 0 | pub fn end_coord(&self) -> F2Dot14 { |
1023 | 0 | self.end_coord.get() |
1024 | 0 | } |
1025 | | } |
1026 | | |
1027 | | impl FixedSize for RegionAxisCoordinates { |
1028 | | const RAW_BYTE_LEN: usize = |
1029 | | F2Dot14::RAW_BYTE_LEN + F2Dot14::RAW_BYTE_LEN + F2Dot14::RAW_BYTE_LEN; |
1030 | | } |
1031 | | |
1032 | | impl<'a> MinByteRange<'a> for ItemVariationStore<'a> { |
1033 | 0 | fn min_byte_range(&self) -> Range<usize> { |
1034 | 0 | 0..self.item_variation_data_offsets_byte_range().end |
1035 | 0 | } |
1036 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
1037 | 0 | let range = self.min_byte_range(); |
1038 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
1039 | 0 | } |
1040 | | } |
1041 | | |
1042 | | impl ReadArgs for ItemVariationStore<'_> { |
1043 | | type Args = (); |
1044 | | } |
1045 | | |
1046 | | impl<'a> FontRead<'a> for ItemVariationStore<'a> { |
1047 | 114k | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
1048 | | #[allow(clippy::absurd_extreme_comparisons)] |
1049 | 114k | if data.len() < Self::MIN_SIZE { |
1050 | 1.30k | return Err(ReadError::OutOfBounds); |
1051 | 113k | } |
1052 | 113k | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
1053 | 0 | return Err(ReadError::OutOfBounds); |
1054 | | }; |
1055 | 113k | Ok(Self { data }) |
1056 | 114k | } |
1057 | | } |
1058 | | |
1059 | | /// The fields at the start of the table, which are read directly. |
1060 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
1061 | | #[repr(C, packed)] |
1062 | | #[doc(hidden)] |
1063 | | pub struct ItemVariationStoreFixedHeader { |
1064 | | pub format: BigEndian<u16>, |
1065 | | pub variation_region_list_offset: BigEndian<Offset32>, |
1066 | | pub item_variation_data_count: BigEndian<u16>, |
1067 | | } |
1068 | | |
1069 | | #[allow(clippy::absurd_extreme_comparisons)] |
1070 | | const _: () = |
1071 | | assert!(std::mem::size_of::<ItemVariationStoreFixedHeader>() <= ItemVariationStore::MIN_SIZE); |
1072 | | |
1073 | | /// The [ItemVariationStore](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#item-variation-store-header-and-item-variation-data-subtables) table |
1074 | | #[derive(Clone)] |
1075 | | pub struct ItemVariationStore<'a> { |
1076 | | data: TableDataRef<'a, ItemVariationStoreFixedHeader>, |
1077 | | } |
1078 | | |
1079 | | #[allow(clippy::needless_lifetimes)] |
1080 | | impl<'a> ItemVariationStore<'a> { |
1081 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
1082 | | basic_table_impls!(impl_the_methods); |
1083 | | |
1084 | | /// Format— set to 1 |
1085 | 0 | pub fn format(&self) -> u16 { |
1086 | 0 | self.data.header().format.get() |
1087 | 0 | } |
1088 | | |
1089 | | /// Offset in bytes from the start of the item variation store to |
1090 | | /// the variation region list. |
1091 | 3.20k | pub fn variation_region_list_offset(&self) -> Offset32 { |
1092 | 3.20k | self.data.header().variation_region_list_offset.get() |
1093 | 3.20k | } |
1094 | | |
1095 | | /// Attempt to resolve [`variation_region_list_offset`][Self::variation_region_list_offset]. |
1096 | 3.20k | pub fn variation_region_list(&self) -> Result<VariationRegionList<'a>, ReadError> { |
1097 | 3.20k | let data = self.offset_data(); |
1098 | 3.20k | self.variation_region_list_offset().resolve(data) |
1099 | 3.20k | } |
1100 | | |
1101 | | /// The number of item variation data subtables. |
1102 | 168k | pub fn item_variation_data_count(&self) -> u16 { |
1103 | 168k | self.data.header().item_variation_data_count.get() |
1104 | 168k | } |
1105 | | |
1106 | | /// Offsets in bytes from the start of the item variation store to |
1107 | | /// each item variation data subtable. |
1108 | 168k | pub fn item_variation_data_offsets(&self) -> &'a [BigEndian<Nullable<Offset32>>] { |
1109 | 168k | let range = self.item_variation_data_offsets_byte_range(); |
1110 | 168k | self.offset_data() |
1111 | 168k | .read_array(range) |
1112 | 168k | .ok() |
1113 | 168k | .unwrap_or_default() |
1114 | 168k | } |
1115 | | |
1116 | | /// A dynamically resolving wrapper for [`item_variation_data_offsets`][Self::item_variation_data_offsets]. |
1117 | 168k | pub fn item_variation_data( |
1118 | 168k | &self, |
1119 | 168k | ) -> ArrayOfNullableOffsets<'a, ItemVariationData<'a>, Offset32> { |
1120 | 168k | let data = self.offset_data(); |
1121 | 168k | let offsets = self.item_variation_data_offsets(); |
1122 | 168k | ArrayOfNullableOffsets::new(offsets, data, ()) |
1123 | 168k | } |
1124 | | |
1125 | 168k | pub fn format_byte_range(&self) -> Range<usize> { |
1126 | 168k | let start = 0usize; |
1127 | 168k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1128 | 168k | start..end |
1129 | 168k | } |
1130 | | |
1131 | 168k | pub fn variation_region_list_offset_byte_range(&self) -> Range<usize> { |
1132 | 168k | let start = self.format_byte_range().end; |
1133 | 168k | let end = start.saturating_add(Offset32::RAW_BYTE_LEN); |
1134 | 168k | start..end |
1135 | 168k | } |
1136 | | |
1137 | 168k | pub fn item_variation_data_count_byte_range(&self) -> Range<usize> { |
1138 | 168k | let start = self.variation_region_list_offset_byte_range().end; |
1139 | 168k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1140 | 168k | start..end |
1141 | 168k | } |
1142 | | |
1143 | 168k | pub fn item_variation_data_offsets_byte_range(&self) -> Range<usize> { |
1144 | 168k | let item_variation_data_count = self.item_variation_data_count(); |
1145 | 168k | let start = self.item_variation_data_count_byte_range().end; |
1146 | 168k | let end = start.saturating_add( |
1147 | 168k | (transforms::to_usize(item_variation_data_count)) |
1148 | 168k | .saturating_mul(Offset32::RAW_BYTE_LEN), |
1149 | | ); |
1150 | 168k | start..end |
1151 | 168k | } |
1152 | | } |
1153 | | |
1154 | | const _: () = assert!(FontData::default_data_long_enough( |
1155 | | ItemVariationStore::MIN_SIZE |
1156 | | )); |
1157 | | |
1158 | | impl Default for ItemVariationStore<'_> { |
1159 | 0 | fn default() -> Self { |
1160 | | Self { |
1161 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
1162 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
1163 | | } |
1164 | 0 | } |
1165 | | } |
1166 | | |
1167 | | impl<'a> MinByteRange<'a> for ItemVariationData<'a> { |
1168 | 0 | fn min_byte_range(&self) -> Range<usize> { |
1169 | 0 | 0..self.delta_sets_byte_range().end |
1170 | 0 | } |
1171 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
1172 | 0 | let range = self.min_byte_range(); |
1173 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
1174 | 0 | } |
1175 | | } |
1176 | | |
1177 | | impl ReadArgs for ItemVariationData<'_> { |
1178 | | type Args = (); |
1179 | | } |
1180 | | |
1181 | | impl<'a> FontRead<'a> for ItemVariationData<'a> { |
1182 | 91.3k | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
1183 | | #[allow(clippy::absurd_extreme_comparisons)] |
1184 | 91.3k | if data.len() < Self::MIN_SIZE { |
1185 | 68.4k | return Err(ReadError::OutOfBounds); |
1186 | 22.9k | } |
1187 | 22.9k | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
1188 | 0 | return Err(ReadError::OutOfBounds); |
1189 | | }; |
1190 | 22.9k | Ok(Self { data }) |
1191 | 91.3k | } |
1192 | | } |
1193 | | |
1194 | | /// The fields at the start of the table, which are read directly. |
1195 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
1196 | | #[repr(C, packed)] |
1197 | | #[doc(hidden)] |
1198 | | pub struct ItemVariationDataFixedHeader { |
1199 | | pub item_count: BigEndian<u16>, |
1200 | | pub word_delta_count: BigEndian<u16>, |
1201 | | pub region_index_count: BigEndian<u16>, |
1202 | | } |
1203 | | |
1204 | | #[allow(clippy::absurd_extreme_comparisons)] |
1205 | | const _: () = |
1206 | | assert!(std::mem::size_of::<ItemVariationDataFixedHeader>() <= ItemVariationData::MIN_SIZE); |
1207 | | |
1208 | | /// The [ItemVariationData](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#item-variation-store-header-and-item-variation-data-subtables) subtable |
1209 | | #[derive(Clone)] |
1210 | | pub struct ItemVariationData<'a> { |
1211 | | data: TableDataRef<'a, ItemVariationDataFixedHeader>, |
1212 | | } |
1213 | | |
1214 | | #[allow(clippy::needless_lifetimes)] |
1215 | | impl<'a> ItemVariationData<'a> { |
1216 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
1217 | | basic_table_impls!(impl_the_methods); |
1218 | | |
1219 | | /// The number of delta sets for distinct items. |
1220 | 22.6k | pub fn item_count(&self) -> u16 { |
1221 | 22.6k | self.data.header().item_count.get() |
1222 | 22.6k | } |
1223 | | |
1224 | | /// A packed field: the high bit is a flag—see details below. |
1225 | 45.3k | pub fn word_delta_count(&self) -> u16 { |
1226 | 45.3k | self.data.header().word_delta_count.get() |
1227 | 45.3k | } |
1228 | | |
1229 | | /// The number of variation regions referenced. |
1230 | 91.0k | pub fn region_index_count(&self) -> u16 { |
1231 | 91.0k | self.data.header().region_index_count.get() |
1232 | 91.0k | } |
1233 | | |
1234 | | /// Array of indices into the variation region list for the regions |
1235 | | /// referenced by this item variation data table. |
1236 | 22.9k | pub fn region_indexes(&self) -> &'a [BigEndian<u16>] { |
1237 | 22.9k | let range = self.region_indexes_byte_range(); |
1238 | 22.9k | self.offset_data() |
1239 | 22.9k | .read_array(range) |
1240 | 22.9k | .ok() |
1241 | 22.9k | .unwrap_or_default() |
1242 | 22.9k | } |
1243 | | |
1244 | | /// Delta-set rows. |
1245 | 22.6k | pub fn delta_sets(&self) -> &'a [u8] { |
1246 | 22.6k | let range = self.delta_sets_byte_range(); |
1247 | 22.6k | self.offset_data() |
1248 | 22.6k | .read_array(range) |
1249 | 22.6k | .ok() |
1250 | 22.6k | .unwrap_or_default() |
1251 | 22.6k | } |
1252 | | |
1253 | 45.6k | pub fn item_count_byte_range(&self) -> Range<usize> { |
1254 | 45.6k | let start = 0usize; |
1255 | 45.6k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1256 | 45.6k | start..end |
1257 | 45.6k | } |
1258 | | |
1259 | 45.6k | pub fn word_delta_count_byte_range(&self) -> Range<usize> { |
1260 | 45.6k | let start = self.item_count_byte_range().end; |
1261 | 45.6k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1262 | 45.6k | start..end |
1263 | 45.6k | } |
1264 | | |
1265 | 45.6k | pub fn region_index_count_byte_range(&self) -> Range<usize> { |
1266 | 45.6k | let start = self.word_delta_count_byte_range().end; |
1267 | 45.6k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
1268 | 45.6k | start..end |
1269 | 45.6k | } |
1270 | | |
1271 | 45.6k | pub fn region_indexes_byte_range(&self) -> Range<usize> { |
1272 | 45.6k | let region_index_count = self.region_index_count(); |
1273 | 45.6k | let start = self.region_index_count_byte_range().end; |
1274 | 45.6k | let end = start.saturating_add( |
1275 | 45.6k | (transforms::to_usize(region_index_count)).saturating_mul(u16::RAW_BYTE_LEN), |
1276 | | ); |
1277 | 45.6k | start..end |
1278 | 45.6k | } |
1279 | | |
1280 | 22.6k | pub fn delta_sets_byte_range(&self) -> Range<usize> { |
1281 | 22.6k | let item_count = self.item_count(); |
1282 | 22.6k | let word_delta_count = self.word_delta_count(); |
1283 | 22.6k | let region_index_count = self.region_index_count(); |
1284 | 22.6k | let start = self.region_indexes_byte_range().end; |
1285 | 22.6k | let end = start.saturating_add( |
1286 | 22.6k | (ItemVariationData::delta_sets_len(item_count, word_delta_count, region_index_count)) |
1287 | 22.6k | .saturating_mul(u8::RAW_BYTE_LEN), |
1288 | | ); |
1289 | 22.6k | start..end |
1290 | 22.6k | } |
1291 | | } |
1292 | | |
1293 | | const _: () = assert!(FontData::default_data_long_enough( |
1294 | | ItemVariationData::MIN_SIZE |
1295 | | )); |
1296 | | |
1297 | | impl Default for ItemVariationData<'_> { |
1298 | 0 | fn default() -> Self { |
1299 | | Self { |
1300 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
1301 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
1302 | | } |
1303 | 0 | } |
1304 | | } |