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