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