Skip to main content

read_fonts/generated/
generated_varc.rs

1// THIS FILE IS AUTOGENERATED.
2// Any changes to this file will be overwritten.
3// For more information about how codegen works, see font-codegen/README.md
4
5#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8impl<'a> MinByteRange<'a> for Varc<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.var_composite_glyphs_offset_byte_range().end
11    }
12    fn min_table_bytes(&self) -> &'a [u8] {
13        let range = self.min_byte_range();
14        self.data.as_bytes().get(range).unwrap_or_default()
15    }
16}
17
18impl TopLevelTable for Varc<'_> {
19    /// `VARC`
20    const TAG: Tag = Tag::new(b"VARC");
21}
22
23impl<'a> FontRead<'a> for Varc<'a> {
24    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
25        #[allow(clippy::absurd_extreme_comparisons)]
26        if data.len() < Self::MIN_SIZE {
27            return Err(ReadError::OutOfBounds);
28        }
29        Ok(Self { data })
30    }
31}
32
33/// [VARC](https://github.com/harfbuzz/boring-expansion-spec/blob/main/VARC.md) (Variable Composites / Components Table)
34///
35/// [FontTools VARC](https://github.com/fonttools/fonttools/blob/5e6b12d12fa08abafbeb7570f47707fbedf69a45/Lib/fontTools/ttLib/tables/otData.py#L3459-L3476)
36#[derive(Clone)]
37pub struct Varc<'a> {
38    data: FontData<'a>,
39}
40
41#[allow(clippy::needless_lifetimes)]
42impl<'a> Varc<'a> {
43    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
44        + Offset32::RAW_BYTE_LEN
45        + Offset32::RAW_BYTE_LEN
46        + Offset32::RAW_BYTE_LEN
47        + Offset32::RAW_BYTE_LEN
48        + Offset32::RAW_BYTE_LEN);
49    basic_table_impls!(impl_the_methods);
50
51    /// Major/minor version number. Set to 1.0.
52    pub fn version(&self) -> MajorMinor {
53        let range = self.version_byte_range();
54        self.data.read_at(range.start).ok().unwrap()
55    }
56
57    pub fn coverage_offset(&self) -> Offset32 {
58        let range = self.coverage_offset_byte_range();
59        self.data.read_at(range.start).ok().unwrap()
60    }
61
62    /// Attempt to resolve [`coverage_offset`][Self::coverage_offset].
63    pub fn coverage(&self) -> Result<CoverageTable<'a>, ReadError> {
64        let data = self.data;
65        self.coverage_offset().resolve(data)
66    }
67
68    pub fn multi_var_store_offset(&self) -> Nullable<Offset32> {
69        let range = self.multi_var_store_offset_byte_range();
70        self.data.read_at(range.start).ok().unwrap()
71    }
72
73    /// Attempt to resolve [`multi_var_store_offset`][Self::multi_var_store_offset].
74    pub fn multi_var_store(&self) -> Option<Result<MultiItemVariationStore<'a>, ReadError>> {
75        let data = self.data;
76        self.multi_var_store_offset().resolve(data)
77    }
78
79    pub fn condition_list_offset(&self) -> Nullable<Offset32> {
80        let range = self.condition_list_offset_byte_range();
81        self.data.read_at(range.start).ok().unwrap()
82    }
83
84    /// Attempt to resolve [`condition_list_offset`][Self::condition_list_offset].
85    pub fn condition_list(&self) -> Option<Result<ConditionList<'a>, ReadError>> {
86        let data = self.data;
87        self.condition_list_offset().resolve(data)
88    }
89
90    pub fn axis_indices_list_offset(&self) -> Nullable<Offset32> {
91        let range = self.axis_indices_list_offset_byte_range();
92        self.data.read_at(range.start).ok().unwrap()
93    }
94
95    /// Attempt to resolve [`axis_indices_list_offset`][Self::axis_indices_list_offset].
96    pub fn axis_indices_list(&self) -> Option<Result<Index2<'a>, ReadError>> {
97        let data = self.data;
98        self.axis_indices_list_offset().resolve(data)
99    }
100
101    pub fn var_composite_glyphs_offset(&self) -> Offset32 {
102        let range = self.var_composite_glyphs_offset_byte_range();
103        self.data.read_at(range.start).ok().unwrap()
104    }
105
106    /// Attempt to resolve [`var_composite_glyphs_offset`][Self::var_composite_glyphs_offset].
107    pub fn var_composite_glyphs(&self) -> Result<Index2<'a>, ReadError> {
108        let data = self.data;
109        self.var_composite_glyphs_offset().resolve(data)
110    }
111
112    pub fn version_byte_range(&self) -> Range<usize> {
113        let start = 0;
114        let end = start + MajorMinor::RAW_BYTE_LEN;
115        start..end
116    }
117
118    pub fn coverage_offset_byte_range(&self) -> Range<usize> {
119        let start = self.version_byte_range().end;
120        let end = start + Offset32::RAW_BYTE_LEN;
121        start..end
122    }
123
124    pub fn multi_var_store_offset_byte_range(&self) -> Range<usize> {
125        let start = self.coverage_offset_byte_range().end;
126        let end = start + Offset32::RAW_BYTE_LEN;
127        start..end
128    }
129
130    pub fn condition_list_offset_byte_range(&self) -> Range<usize> {
131        let start = self.multi_var_store_offset_byte_range().end;
132        let end = start + Offset32::RAW_BYTE_LEN;
133        start..end
134    }
135
136    pub fn axis_indices_list_offset_byte_range(&self) -> Range<usize> {
137        let start = self.condition_list_offset_byte_range().end;
138        let end = start + Offset32::RAW_BYTE_LEN;
139        start..end
140    }
141
142    pub fn var_composite_glyphs_offset_byte_range(&self) -> Range<usize> {
143        let start = self.axis_indices_list_offset_byte_range().end;
144        let end = start + Offset32::RAW_BYTE_LEN;
145        start..end
146    }
147}
148
149const _: () = assert!(FontData::default_data_long_enough(Varc::MIN_SIZE));
150
151impl Default for Varc<'_> {
152    fn default() -> Self {
153        Self {
154            data: FontData::default_table_data(),
155        }
156    }
157}
158
159#[cfg(feature = "experimental_traverse")]
160impl<'a> SomeTable<'a> for Varc<'a> {
161    fn type_name(&self) -> &str {
162        "Varc"
163    }
164    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
165        match idx {
166            0usize => Some(Field::new("version", self.version())),
167            1usize => Some(Field::new(
168                "coverage_offset",
169                FieldType::offset(self.coverage_offset(), self.coverage()),
170            )),
171            2usize => Some(Field::new(
172                "multi_var_store_offset",
173                FieldType::offset(self.multi_var_store_offset(), self.multi_var_store()),
174            )),
175            3usize => Some(Field::new(
176                "condition_list_offset",
177                FieldType::offset(self.condition_list_offset(), self.condition_list()),
178            )),
179            4usize => Some(Field::new(
180                "axis_indices_list_offset",
181                FieldType::offset(self.axis_indices_list_offset(), self.axis_indices_list()),
182            )),
183            5usize => Some(Field::new(
184                "var_composite_glyphs_offset",
185                FieldType::offset(
186                    self.var_composite_glyphs_offset(),
187                    self.var_composite_glyphs(),
188                ),
189            )),
190            _ => None,
191        }
192    }
193}
194
195#[cfg(feature = "experimental_traverse")]
196#[allow(clippy::needless_lifetimes)]
197impl<'a> std::fmt::Debug for Varc<'a> {
198    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
199        (self as &dyn SomeTable<'a>).fmt(f)
200    }
201}
202
203impl Format<u16> for MultiItemVariationStore<'_> {
204    const FORMAT: u16 = 1;
205}
206
207impl<'a> MinByteRange<'a> for MultiItemVariationStore<'a> {
208    fn min_byte_range(&self) -> Range<usize> {
209        0..self.variation_data_offsets_byte_range().end
210    }
211    fn min_table_bytes(&self) -> &'a [u8] {
212        let range = self.min_byte_range();
213        self.data.as_bytes().get(range).unwrap_or_default()
214    }
215}
216
217impl<'a> FontRead<'a> for MultiItemVariationStore<'a> {
218    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
219        #[allow(clippy::absurd_extreme_comparisons)]
220        if data.len() < Self::MIN_SIZE {
221            return Err(ReadError::OutOfBounds);
222        }
223        Ok(Self { data })
224    }
225}
226
227/// * <https://github.com/fonttools/fonttools/blob/5e6b12d12fa08abafbeb7570f47707fbedf69a45/Lib/fontTools/ttLib/tables/otData.py#L3451-L3457>
228/// * <https://github.com/harfbuzz/harfbuzz/blob/7be12b33e3f07067c159d8f516eb31df58c75876/src/hb-ot-layout-common.hh#L3517-L3520C3>
229#[derive(Clone)]
230pub struct MultiItemVariationStore<'a> {
231    data: FontData<'a>,
232}
233
234#[allow(clippy::needless_lifetimes)]
235impl<'a> MultiItemVariationStore<'a> {
236    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
237    basic_table_impls!(impl_the_methods);
238
239    pub fn format(&self) -> u16 {
240        let range = self.format_byte_range();
241        self.data.read_at(range.start).ok().unwrap()
242    }
243
244    pub fn region_list_offset(&self) -> Offset32 {
245        let range = self.region_list_offset_byte_range();
246        self.data.read_at(range.start).ok().unwrap()
247    }
248
249    /// Attempt to resolve [`region_list_offset`][Self::region_list_offset].
250    pub fn region_list(&self) -> Result<SparseVariationRegionList<'a>, ReadError> {
251        let data = self.data;
252        self.region_list_offset().resolve(data)
253    }
254
255    pub fn variation_data_count(&self) -> u16 {
256        let range = self.variation_data_count_byte_range();
257        self.data.read_at(range.start).ok().unwrap()
258    }
259
260    pub fn variation_data_offsets(&self) -> &'a [BigEndian<Offset32>] {
261        let range = self.variation_data_offsets_byte_range();
262        self.data.read_array(range).ok().unwrap_or_default()
263    }
264
265    /// A dynamically resolving wrapper for [`variation_data_offsets`][Self::variation_data_offsets].
266    pub fn variation_data(&self) -> ArrayOfOffsets<'a, MultiItemVariationData<'a>, Offset32> {
267        let data = self.data;
268        let offsets = self.variation_data_offsets();
269        ArrayOfOffsets::new(offsets, data, ())
270    }
271
272    pub fn format_byte_range(&self) -> Range<usize> {
273        let start = 0;
274        let end = start + u16::RAW_BYTE_LEN;
275        start..end
276    }
277
278    pub fn region_list_offset_byte_range(&self) -> Range<usize> {
279        let start = self.format_byte_range().end;
280        let end = start + Offset32::RAW_BYTE_LEN;
281        start..end
282    }
283
284    pub fn variation_data_count_byte_range(&self) -> Range<usize> {
285        let start = self.region_list_offset_byte_range().end;
286        let end = start + u16::RAW_BYTE_LEN;
287        start..end
288    }
289
290    pub fn variation_data_offsets_byte_range(&self) -> Range<usize> {
291        let variation_data_count = self.variation_data_count();
292        let start = self.variation_data_count_byte_range().end;
293        let end = start
294            + (transforms::to_usize(variation_data_count)).saturating_mul(Offset32::RAW_BYTE_LEN);
295        start..end
296    }
297}
298
299const _: () = assert!(FontData::default_data_long_enough(
300    MultiItemVariationStore::MIN_SIZE
301));
302
303impl Default for MultiItemVariationStore<'_> {
304    fn default() -> Self {
305        Self {
306            data: FontData::default_format_1_u16_table_data(),
307        }
308    }
309}
310
311#[cfg(feature = "experimental_traverse")]
312impl<'a> SomeTable<'a> for MultiItemVariationStore<'a> {
313    fn type_name(&self) -> &str {
314        "MultiItemVariationStore"
315    }
316    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
317        match idx {
318            0usize => Some(Field::new("format", self.format())),
319            1usize => Some(Field::new(
320                "region_list_offset",
321                FieldType::offset(self.region_list_offset(), self.region_list()),
322            )),
323            2usize => Some(Field::new(
324                "variation_data_count",
325                self.variation_data_count(),
326            )),
327            3usize => Some(Field::new(
328                "variation_data_offsets",
329                FieldType::from(self.variation_data()),
330            )),
331            _ => None,
332        }
333    }
334}
335
336#[cfg(feature = "experimental_traverse")]
337#[allow(clippy::needless_lifetimes)]
338impl<'a> std::fmt::Debug for MultiItemVariationStore<'a> {
339    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
340        (self as &dyn SomeTable<'a>).fmt(f)
341    }
342}
343
344impl<'a> MinByteRange<'a> for SparseVariationRegionList<'a> {
345    fn min_byte_range(&self) -> Range<usize> {
346        0..self.region_offsets_byte_range().end
347    }
348    fn min_table_bytes(&self) -> &'a [u8] {
349        let range = self.min_byte_range();
350        self.data.as_bytes().get(range).unwrap_or_default()
351    }
352}
353
354impl<'a> FontRead<'a> for SparseVariationRegionList<'a> {
355    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
356        #[allow(clippy::absurd_extreme_comparisons)]
357        if data.len() < Self::MIN_SIZE {
358            return Err(ReadError::OutOfBounds);
359        }
360        Ok(Self { data })
361    }
362}
363
364#[derive(Clone)]
365pub struct SparseVariationRegionList<'a> {
366    data: FontData<'a>,
367}
368
369#[allow(clippy::needless_lifetimes)]
370impl<'a> SparseVariationRegionList<'a> {
371    pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN;
372    basic_table_impls!(impl_the_methods);
373
374    pub fn region_count(&self) -> u16 {
375        let range = self.region_count_byte_range();
376        self.data.read_at(range.start).ok().unwrap()
377    }
378
379    pub fn region_offsets(&self) -> &'a [BigEndian<Offset32>] {
380        let range = self.region_offsets_byte_range();
381        self.data.read_array(range).ok().unwrap_or_default()
382    }
383
384    /// A dynamically resolving wrapper for [`region_offsets`][Self::region_offsets].
385    pub fn regions(&self) -> ArrayOfOffsets<'a, SparseVariationRegion<'a>, Offset32> {
386        let data = self.data;
387        let offsets = self.region_offsets();
388        ArrayOfOffsets::new(offsets, data, ())
389    }
390
391    pub fn region_count_byte_range(&self) -> Range<usize> {
392        let start = 0;
393        let end = start + u16::RAW_BYTE_LEN;
394        start..end
395    }
396
397    pub fn region_offsets_byte_range(&self) -> Range<usize> {
398        let region_count = self.region_count();
399        let start = self.region_count_byte_range().end;
400        let end =
401            start + (transforms::to_usize(region_count)).saturating_mul(Offset32::RAW_BYTE_LEN);
402        start..end
403    }
404}
405
406const _: () = assert!(FontData::default_data_long_enough(
407    SparseVariationRegionList::MIN_SIZE
408));
409
410impl Default for SparseVariationRegionList<'_> {
411    fn default() -> Self {
412        Self {
413            data: FontData::default_table_data(),
414        }
415    }
416}
417
418#[cfg(feature = "experimental_traverse")]
419impl<'a> SomeTable<'a> for SparseVariationRegionList<'a> {
420    fn type_name(&self) -> &str {
421        "SparseVariationRegionList"
422    }
423    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
424        match idx {
425            0usize => Some(Field::new("region_count", self.region_count())),
426            1usize => Some(Field::new(
427                "region_offsets",
428                FieldType::from(self.regions()),
429            )),
430            _ => None,
431        }
432    }
433}
434
435#[cfg(feature = "experimental_traverse")]
436#[allow(clippy::needless_lifetimes)]
437impl<'a> std::fmt::Debug for SparseVariationRegionList<'a> {
438    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
439        (self as &dyn SomeTable<'a>).fmt(f)
440    }
441}
442
443impl<'a> MinByteRange<'a> for SparseVariationRegion<'a> {
444    fn min_byte_range(&self) -> Range<usize> {
445        0..self.region_axes_byte_range().end
446    }
447    fn min_table_bytes(&self) -> &'a [u8] {
448        let range = self.min_byte_range();
449        self.data.as_bytes().get(range).unwrap_or_default()
450    }
451}
452
453impl<'a> FontRead<'a> for SparseVariationRegion<'a> {
454    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
455        #[allow(clippy::absurd_extreme_comparisons)]
456        if data.len() < Self::MIN_SIZE {
457            return Err(ReadError::OutOfBounds);
458        }
459        Ok(Self { data })
460    }
461}
462
463#[derive(Clone)]
464pub struct SparseVariationRegion<'a> {
465    data: FontData<'a>,
466}
467
468#[allow(clippy::needless_lifetimes)]
469impl<'a> SparseVariationRegion<'a> {
470    pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN;
471    basic_table_impls!(impl_the_methods);
472
473    pub fn region_axis_count(&self) -> u16 {
474        let range = self.region_axis_count_byte_range();
475        self.data.read_at(range.start).ok().unwrap()
476    }
477
478    pub fn region_axes(&self) -> &'a [SparseRegionAxisCoordinates] {
479        let range = self.region_axes_byte_range();
480        self.data.read_array(range).ok().unwrap_or_default()
481    }
482
483    pub fn region_axis_count_byte_range(&self) -> Range<usize> {
484        let start = 0;
485        let end = start + u16::RAW_BYTE_LEN;
486        start..end
487    }
488
489    pub fn region_axes_byte_range(&self) -> Range<usize> {
490        let region_axis_count = self.region_axis_count();
491        let start = self.region_axis_count_byte_range().end;
492        let end = start
493            + (transforms::to_usize(region_axis_count))
494                .saturating_mul(SparseRegionAxisCoordinates::RAW_BYTE_LEN);
495        start..end
496    }
497}
498
499const _: () = assert!(FontData::default_data_long_enough(
500    SparseVariationRegion::MIN_SIZE
501));
502
503impl Default for SparseVariationRegion<'_> {
504    fn default() -> Self {
505        Self {
506            data: FontData::default_table_data(),
507        }
508    }
509}
510
511#[cfg(feature = "experimental_traverse")]
512impl<'a> SomeTable<'a> for SparseVariationRegion<'a> {
513    fn type_name(&self) -> &str {
514        "SparseVariationRegion"
515    }
516    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
517        match idx {
518            0usize => Some(Field::new("region_axis_count", self.region_axis_count())),
519            1usize => Some(Field::new(
520                "region_axes",
521                traversal::FieldType::array_of_records(
522                    stringify!(SparseRegionAxisCoordinates),
523                    self.region_axes(),
524                    self.offset_data(),
525                ),
526            )),
527            _ => None,
528        }
529    }
530}
531
532#[cfg(feature = "experimental_traverse")]
533#[allow(clippy::needless_lifetimes)]
534impl<'a> std::fmt::Debug for SparseVariationRegion<'a> {
535    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
536        (self as &dyn SomeTable<'a>).fmt(f)
537    }
538}
539
540#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
541#[repr(C)]
542#[repr(packed)]
543pub struct SparseRegionAxisCoordinates {
544    pub axis_index: BigEndian<u16>,
545    pub start: BigEndian<F2Dot14>,
546    pub peak: BigEndian<F2Dot14>,
547    pub end: BigEndian<F2Dot14>,
548}
549
550impl SparseRegionAxisCoordinates {
551    pub fn axis_index(&self) -> u16 {
552        self.axis_index.get()
553    }
554
555    pub fn start(&self) -> F2Dot14 {
556        self.start.get()
557    }
558
559    pub fn peak(&self) -> F2Dot14 {
560        self.peak.get()
561    }
562
563    pub fn end(&self) -> F2Dot14 {
564        self.end.get()
565    }
566}
567
568impl FixedSize for SparseRegionAxisCoordinates {
569    const RAW_BYTE_LEN: usize =
570        u16::RAW_BYTE_LEN + F2Dot14::RAW_BYTE_LEN + F2Dot14::RAW_BYTE_LEN + F2Dot14::RAW_BYTE_LEN;
571}
572
573#[cfg(feature = "experimental_traverse")]
574impl<'a> SomeRecord<'a> for SparseRegionAxisCoordinates {
575    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
576        RecordResolver {
577            name: "SparseRegionAxisCoordinates",
578            get_field: Box::new(move |idx, _data| match idx {
579                0usize => Some(Field::new("axis_index", self.axis_index())),
580                1usize => Some(Field::new("start", self.start())),
581                2usize => Some(Field::new("peak", self.peak())),
582                3usize => Some(Field::new("end", self.end())),
583                _ => None,
584            }),
585            data,
586        }
587    }
588}
589
590impl Format<u8> for MultiItemVariationData<'_> {
591    const FORMAT: u8 = 1;
592}
593
594impl<'a> MinByteRange<'a> for MultiItemVariationData<'a> {
595    fn min_byte_range(&self) -> Range<usize> {
596        0..self.raw_delta_sets_byte_range().end
597    }
598    fn min_table_bytes(&self) -> &'a [u8] {
599        let range = self.min_byte_range();
600        self.data.as_bytes().get(range).unwrap_or_default()
601    }
602}
603
604impl<'a> FontRead<'a> for MultiItemVariationData<'a> {
605    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
606        #[allow(clippy::absurd_extreme_comparisons)]
607        if data.len() < Self::MIN_SIZE {
608            return Err(ReadError::OutOfBounds);
609        }
610        Ok(Self { data })
611    }
612}
613
614#[derive(Clone)]
615pub struct MultiItemVariationData<'a> {
616    data: FontData<'a>,
617}
618
619#[allow(clippy::needless_lifetimes)]
620impl<'a> MultiItemVariationData<'a> {
621    pub const MIN_SIZE: usize = (u8::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
622    basic_table_impls!(impl_the_methods);
623
624    pub fn format(&self) -> u8 {
625        let range = self.format_byte_range();
626        self.data.read_at(range.start).ok().unwrap()
627    }
628
629    pub fn region_index_count(&self) -> u16 {
630        let range = self.region_index_count_byte_range();
631        self.data.read_at(range.start).ok().unwrap()
632    }
633
634    pub fn region_indices(&self) -> &'a [BigEndian<u16>] {
635        let range = self.region_indices_byte_range();
636        self.data.read_array(range).ok().unwrap_or_default()
637    }
638
639    pub fn raw_delta_sets(&self) -> &'a [u8] {
640        let range = self.raw_delta_sets_byte_range();
641        self.data.read_array(range).ok().unwrap_or_default()
642    }
643
644    pub fn format_byte_range(&self) -> Range<usize> {
645        let start = 0;
646        let end = start + u8::RAW_BYTE_LEN;
647        start..end
648    }
649
650    pub fn region_index_count_byte_range(&self) -> Range<usize> {
651        let start = self.format_byte_range().end;
652        let end = start + u16::RAW_BYTE_LEN;
653        start..end
654    }
655
656    pub fn region_indices_byte_range(&self) -> Range<usize> {
657        let region_index_count = self.region_index_count();
658        let start = self.region_index_count_byte_range().end;
659        let end =
660            start + (transforms::to_usize(region_index_count)).saturating_mul(u16::RAW_BYTE_LEN);
661        start..end
662    }
663
664    pub fn raw_delta_sets_byte_range(&self) -> Range<usize> {
665        let start = self.region_indices_byte_range().end;
666        let end =
667            start + self.data.len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN;
668        start..end
669    }
670}
671
672const _: () = assert!(FontData::default_data_long_enough(
673    MultiItemVariationData::MIN_SIZE
674));
675
676impl Default for MultiItemVariationData<'_> {
677    fn default() -> Self {
678        Self {
679            data: FontData::default_format_1_u8_table_data(),
680        }
681    }
682}
683
684#[cfg(feature = "experimental_traverse")]
685impl<'a> SomeTable<'a> for MultiItemVariationData<'a> {
686    fn type_name(&self) -> &str {
687        "MultiItemVariationData"
688    }
689    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
690        match idx {
691            0usize => Some(Field::new("format", self.format())),
692            1usize => Some(Field::new("region_index_count", self.region_index_count())),
693            2usize => Some(Field::new("region_indices", self.region_indices())),
694            3usize => Some(Field::new("raw_delta_sets", self.raw_delta_sets())),
695            _ => None,
696        }
697    }
698}
699
700#[cfg(feature = "experimental_traverse")]
701#[allow(clippy::needless_lifetimes)]
702impl<'a> std::fmt::Debug for MultiItemVariationData<'a> {
703    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
704        (self as &dyn SomeTable<'a>).fmt(f)
705    }
706}
707
708impl<'a> MinByteRange<'a> for ConditionList<'a> {
709    fn min_byte_range(&self) -> Range<usize> {
710        0..self.condition_offsets_byte_range().end
711    }
712    fn min_table_bytes(&self) -> &'a [u8] {
713        let range = self.min_byte_range();
714        self.data.as_bytes().get(range).unwrap_or_default()
715    }
716}
717
718impl<'a> FontRead<'a> for ConditionList<'a> {
719    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
720        #[allow(clippy::absurd_extreme_comparisons)]
721        if data.len() < Self::MIN_SIZE {
722            return Err(ReadError::OutOfBounds);
723        }
724        Ok(Self { data })
725    }
726}
727
728#[derive(Clone)]
729pub struct ConditionList<'a> {
730    data: FontData<'a>,
731}
732
733#[allow(clippy::needless_lifetimes)]
734impl<'a> ConditionList<'a> {
735    pub const MIN_SIZE: usize = u32::RAW_BYTE_LEN;
736    basic_table_impls!(impl_the_methods);
737
738    pub fn condition_count(&self) -> u32 {
739        let range = self.condition_count_byte_range();
740        self.data.read_at(range.start).ok().unwrap()
741    }
742
743    pub fn condition_offsets(&self) -> &'a [BigEndian<Offset32>] {
744        let range = self.condition_offsets_byte_range();
745        self.data.read_array(range).ok().unwrap_or_default()
746    }
747
748    /// A dynamically resolving wrapper for [`condition_offsets`][Self::condition_offsets].
749    pub fn conditions(&self) -> ArrayOfOffsets<'a, Condition<'a>, Offset32> {
750        let data = self.data;
751        let offsets = self.condition_offsets();
752        ArrayOfOffsets::new(offsets, data, ())
753    }
754
755    pub fn condition_count_byte_range(&self) -> Range<usize> {
756        let start = 0;
757        let end = start + u32::RAW_BYTE_LEN;
758        start..end
759    }
760
761    pub fn condition_offsets_byte_range(&self) -> Range<usize> {
762        let condition_count = self.condition_count();
763        let start = self.condition_count_byte_range().end;
764        let end =
765            start + (transforms::to_usize(condition_count)).saturating_mul(Offset32::RAW_BYTE_LEN);
766        start..end
767    }
768}
769
770const _: () = assert!(FontData::default_data_long_enough(ConditionList::MIN_SIZE));
771
772impl Default for ConditionList<'_> {
773    fn default() -> Self {
774        Self {
775            data: FontData::default_table_data(),
776        }
777    }
778}
779
780#[cfg(feature = "experimental_traverse")]
781impl<'a> SomeTable<'a> for ConditionList<'a> {
782    fn type_name(&self) -> &str {
783        "ConditionList"
784    }
785    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
786        match idx {
787            0usize => Some(Field::new("condition_count", self.condition_count())),
788            1usize => Some(Field::new(
789                "condition_offsets",
790                FieldType::from(self.conditions()),
791            )),
792            _ => None,
793        }
794    }
795}
796
797#[cfg(feature = "experimental_traverse")]
798#[allow(clippy::needless_lifetimes)]
799impl<'a> std::fmt::Debug for ConditionList<'a> {
800    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
801        (self as &dyn SomeTable<'a>).fmt(f)
802    }
803}
804
805/// Flags used in the [VarcComponent] byte stream
806///
807/// <https://github.com/harfbuzz/boring-expansion-spec/blob/main/VARC.md#variable-component-flags>
808#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
809#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
810#[repr(transparent)]
811pub struct VarcFlags {
812    bits: u32,
813}
814
815impl VarcFlags {
816    pub const RESET_UNSPECIFIED_AXES: Self = Self {
817        bits: 0b0000_0000_0000_0001,
818    };
819
820    pub const HAVE_AXES: Self = Self {
821        bits: 0b0000_0000_0000_0010,
822    };
823
824    pub const AXIS_VALUES_HAVE_VARIATION: Self = Self {
825        bits: 0b0000_0000_0000_0100,
826    };
827
828    pub const TRANSFORM_HAS_VARIATION: Self = Self {
829        bits: 0b0000_0000_0000_1000,
830    };
831
832    pub const HAVE_TRANSLATE_X: Self = Self {
833        bits: 0b0000_0000_0001_0000,
834    };
835
836    pub const HAVE_TRANSLATE_Y: Self = Self {
837        bits: 0b0000_0000_0010_0000,
838    };
839
840    pub const HAVE_ROTATION: Self = Self {
841        bits: 0b0000_0000_0100_0000,
842    };
843
844    pub const HAVE_CONDITION: Self = Self {
845        bits: 0b0000_0000_1000_0000,
846    };
847
848    pub const HAVE_SCALE_X: Self = Self {
849        bits: 0b0000_0001_0000_0000,
850    };
851
852    pub const HAVE_SCALE_Y: Self = Self {
853        bits: 0b0000_0010_0000_0000,
854    };
855
856    pub const HAVE_TCENTER_X: Self = Self {
857        bits: 0b0000_0100_0000_0000,
858    };
859
860    pub const HAVE_TCENTER_Y: Self = Self {
861        bits: 0b0000_1000_0000_0000,
862    };
863
864    pub const GID_IS_24BIT: Self = Self {
865        bits: 0b0001_0000_0000_0000,
866    };
867
868    pub const HAVE_SKEW_X: Self = Self {
869        bits: 0b0010_0000_0000_0000,
870    };
871
872    pub const HAVE_SKEW_Y: Self = Self {
873        bits: 0b0100_0000_0000_0000,
874    };
875
876    pub const RESERVED_MASK: Self = Self { bits: 0xFFFF8000 };
877}
878
879impl VarcFlags {
880    ///  Returns an empty set of flags.
881    #[inline]
882    pub const fn empty() -> Self {
883        Self { bits: 0 }
884    }
885
886    /// Returns the set containing all flags.
887    #[inline]
888    pub const fn all() -> Self {
889        Self {
890            bits: Self::RESET_UNSPECIFIED_AXES.bits
891                | Self::HAVE_AXES.bits
892                | Self::AXIS_VALUES_HAVE_VARIATION.bits
893                | Self::TRANSFORM_HAS_VARIATION.bits
894                | Self::HAVE_TRANSLATE_X.bits
895                | Self::HAVE_TRANSLATE_Y.bits
896                | Self::HAVE_ROTATION.bits
897                | Self::HAVE_CONDITION.bits
898                | Self::HAVE_SCALE_X.bits
899                | Self::HAVE_SCALE_Y.bits
900                | Self::HAVE_TCENTER_X.bits
901                | Self::HAVE_TCENTER_Y.bits
902                | Self::GID_IS_24BIT.bits
903                | Self::HAVE_SKEW_X.bits
904                | Self::HAVE_SKEW_Y.bits
905                | Self::RESERVED_MASK.bits,
906        }
907    }
908
909    /// Returns the raw value of the flags currently stored.
910    #[inline]
911    pub const fn bits(&self) -> u32 {
912        self.bits
913    }
914
915    /// Convert from underlying bit representation, unless that
916    /// representation contains bits that do not correspond to a flag.
917    #[inline]
918    pub const fn from_bits(bits: u32) -> Option<Self> {
919        if (bits & !Self::all().bits()) == 0 {
920            Some(Self { bits })
921        } else {
922            None
923        }
924    }
925
926    /// Convert from underlying bit representation, dropping any bits
927    /// that do not correspond to flags.
928    #[inline]
929    pub const fn from_bits_truncate(bits: u32) -> Self {
930        Self {
931            bits: bits & Self::all().bits,
932        }
933    }
934
935    /// Returns `true` if no flags are currently stored.
936    #[inline]
937    pub const fn is_empty(&self) -> bool {
938        self.bits() == Self::empty().bits()
939    }
940
941    /// Returns `true` if there are flags common to both `self` and `other`.
942    #[inline]
943    pub const fn intersects(&self, other: Self) -> bool {
944        !(Self {
945            bits: self.bits & other.bits,
946        })
947        .is_empty()
948    }
949
950    /// Returns `true` if all of the flags in `other` are contained within `self`.
951    #[inline]
952    pub const fn contains(&self, other: Self) -> bool {
953        (self.bits & other.bits) == other.bits
954    }
955
956    /// Inserts the specified flags in-place.
957    #[inline]
958    pub fn insert(&mut self, other: Self) {
959        self.bits |= other.bits;
960    }
961
962    /// Removes the specified flags in-place.
963    #[inline]
964    pub fn remove(&mut self, other: Self) {
965        self.bits &= !other.bits;
966    }
967
968    /// Toggles the specified flags in-place.
969    #[inline]
970    pub fn toggle(&mut self, other: Self) {
971        self.bits ^= other.bits;
972    }
973
974    /// Returns the intersection between the flags in `self` and
975    /// `other`.
976    ///
977    /// Specifically, the returned set contains only the flags which are
978    /// present in *both* `self` *and* `other`.
979    ///
980    /// This is equivalent to using the `&` operator (e.g.
981    /// [`ops::BitAnd`]), as in `flags & other`.
982    ///
983    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
984    #[inline]
985    #[must_use]
986    pub const fn intersection(self, other: Self) -> Self {
987        Self {
988            bits: self.bits & other.bits,
989        }
990    }
991
992    /// Returns the union of between the flags in `self` and `other`.
993    ///
994    /// Specifically, the returned set contains all flags which are
995    /// present in *either* `self` *or* `other`, including any which are
996    /// present in both.
997    ///
998    /// This is equivalent to using the `|` operator (e.g.
999    /// [`ops::BitOr`]), as in `flags | other`.
1000    ///
1001    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
1002    #[inline]
1003    #[must_use]
1004    pub const fn union(self, other: Self) -> Self {
1005        Self {
1006            bits: self.bits | other.bits,
1007        }
1008    }
1009
1010    /// Returns the difference between the flags in `self` and `other`.
1011    ///
1012    /// Specifically, the returned set contains all flags present in
1013    /// `self`, except for the ones present in `other`.
1014    ///
1015    /// It is also conceptually equivalent to the "bit-clear" operation:
1016    /// `flags & !other` (and this syntax is also supported).
1017    ///
1018    /// This is equivalent to using the `-` operator (e.g.
1019    /// [`ops::Sub`]), as in `flags - other`.
1020    ///
1021    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
1022    #[inline]
1023    #[must_use]
1024    pub const fn difference(self, other: Self) -> Self {
1025        Self {
1026            bits: self.bits & !other.bits,
1027        }
1028    }
1029}
1030
1031impl std::ops::BitOr for VarcFlags {
1032    type Output = Self;
1033
1034    /// Returns the union of the two sets of flags.
1035    #[inline]
1036    fn bitor(self, other: VarcFlags) -> Self {
1037        Self {
1038            bits: self.bits | other.bits,
1039        }
1040    }
1041}
1042
1043impl std::ops::BitOrAssign for VarcFlags {
1044    /// Adds the set of flags.
1045    #[inline]
1046    fn bitor_assign(&mut self, other: Self) {
1047        self.bits |= other.bits;
1048    }
1049}
1050
1051impl std::ops::BitXor for VarcFlags {
1052    type Output = Self;
1053
1054    /// Returns the left flags, but with all the right flags toggled.
1055    #[inline]
1056    fn bitxor(self, other: Self) -> Self {
1057        Self {
1058            bits: self.bits ^ other.bits,
1059        }
1060    }
1061}
1062
1063impl std::ops::BitXorAssign for VarcFlags {
1064    /// Toggles the set of flags.
1065    #[inline]
1066    fn bitxor_assign(&mut self, other: Self) {
1067        self.bits ^= other.bits;
1068    }
1069}
1070
1071impl std::ops::BitAnd for VarcFlags {
1072    type Output = Self;
1073
1074    /// Returns the intersection between the two sets of flags.
1075    #[inline]
1076    fn bitand(self, other: Self) -> Self {
1077        Self {
1078            bits: self.bits & other.bits,
1079        }
1080    }
1081}
1082
1083impl std::ops::BitAndAssign for VarcFlags {
1084    /// Disables all flags disabled in the set.
1085    #[inline]
1086    fn bitand_assign(&mut self, other: Self) {
1087        self.bits &= other.bits;
1088    }
1089}
1090
1091impl std::ops::Sub for VarcFlags {
1092    type Output = Self;
1093
1094    /// Returns the set difference of the two sets of flags.
1095    #[inline]
1096    fn sub(self, other: Self) -> Self {
1097        Self {
1098            bits: self.bits & !other.bits,
1099        }
1100    }
1101}
1102
1103impl std::ops::SubAssign for VarcFlags {
1104    /// Disables all flags enabled in the set.
1105    #[inline]
1106    fn sub_assign(&mut self, other: Self) {
1107        self.bits &= !other.bits;
1108    }
1109}
1110
1111impl std::ops::Not for VarcFlags {
1112    type Output = Self;
1113
1114    /// Returns the complement of this set of flags.
1115    #[inline]
1116    fn not(self) -> Self {
1117        Self { bits: !self.bits } & Self::all()
1118    }
1119}
1120
1121impl std::fmt::Debug for VarcFlags {
1122    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1123        let members: &[(&str, Self)] = &[
1124            ("RESET_UNSPECIFIED_AXES", Self::RESET_UNSPECIFIED_AXES),
1125            ("HAVE_AXES", Self::HAVE_AXES),
1126            (
1127                "AXIS_VALUES_HAVE_VARIATION",
1128                Self::AXIS_VALUES_HAVE_VARIATION,
1129            ),
1130            ("TRANSFORM_HAS_VARIATION", Self::TRANSFORM_HAS_VARIATION),
1131            ("HAVE_TRANSLATE_X", Self::HAVE_TRANSLATE_X),
1132            ("HAVE_TRANSLATE_Y", Self::HAVE_TRANSLATE_Y),
1133            ("HAVE_ROTATION", Self::HAVE_ROTATION),
1134            ("HAVE_CONDITION", Self::HAVE_CONDITION),
1135            ("HAVE_SCALE_X", Self::HAVE_SCALE_X),
1136            ("HAVE_SCALE_Y", Self::HAVE_SCALE_Y),
1137            ("HAVE_TCENTER_X", Self::HAVE_TCENTER_X),
1138            ("HAVE_TCENTER_Y", Self::HAVE_TCENTER_Y),
1139            ("GID_IS_24BIT", Self::GID_IS_24BIT),
1140            ("HAVE_SKEW_X", Self::HAVE_SKEW_X),
1141            ("HAVE_SKEW_Y", Self::HAVE_SKEW_Y),
1142            ("RESERVED_MASK", Self::RESERVED_MASK),
1143        ];
1144        let mut first = true;
1145        for (name, value) in members {
1146            if self.contains(*value) {
1147                if !first {
1148                    f.write_str(" | ")?;
1149                }
1150                first = false;
1151                f.write_str(name)?;
1152            }
1153        }
1154        if first {
1155            f.write_str("(empty)")?;
1156        }
1157        Ok(())
1158    }
1159}
1160
1161impl std::fmt::Binary for VarcFlags {
1162    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1163        std::fmt::Binary::fmt(&self.bits, f)
1164    }
1165}
1166
1167impl std::fmt::Octal for VarcFlags {
1168    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1169        std::fmt::Octal::fmt(&self.bits, f)
1170    }
1171}
1172
1173impl std::fmt::LowerHex for VarcFlags {
1174    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1175        std::fmt::LowerHex::fmt(&self.bits, f)
1176    }
1177}
1178
1179impl std::fmt::UpperHex for VarcFlags {
1180    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1181        std::fmt::UpperHex::fmt(&self.bits, f)
1182    }
1183}
1184
1185impl font_types::Scalar for VarcFlags {
1186    type Raw = <u32 as font_types::Scalar>::Raw;
1187    fn to_raw(self) -> Self::Raw {
1188        self.bits().to_raw()
1189    }
1190    fn from_raw(raw: Self::Raw) -> Self {
1191        let t = <u32>::from_raw(raw);
1192        Self::from_bits_truncate(t)
1193    }
1194}
1195
1196#[cfg(feature = "experimental_traverse")]
1197impl<'a> From<VarcFlags> for FieldType<'a> {
1198    fn from(src: VarcFlags) -> FieldType<'a> {
1199        src.bits().into()
1200    }
1201}