Skip to main content

write_fonts/generated/
generated_variations.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
8pub use read_fonts::tables::variations::EntryFormat;
9
10/// [TupleVariationHeader](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuplevariationheader)
11#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub struct TupleVariationHeader {
14    /// The size in bytes of the serialized data for this tuple
15    /// variation table.
16    pub variation_data_size: u16,
17    /// A packed field. The high 4 bits are flags (see below). The low
18    /// 12 bits are an index into a shared tuple records array.
19    pub tuple_index: TupleIndex,
20    /// Peak tuple record for this tuple variation table — optional,
21    /// determined by flags in the tupleIndex value.  Note that this
22    /// must always be included in the 'cvar' table.
23    pub peak_tuple: Vec<F2Dot14>,
24    /// Intermediate start tuple record for this tuple variation table
25    /// — optional, determined by flags in the tupleIndex value.
26    pub intermediate_start_tuple: Vec<F2Dot14>,
27    /// Intermediate end tuple record for this tuple variation table
28    /// — optional, determined by flags in the tupleIndex value.
29    pub intermediate_end_tuple: Vec<F2Dot14>,
30}
31
32impl FontWrite for TupleVariationHeader {
33    fn write_into(&self, writer: &mut TableWriter) {
34        self.variation_data_size.write_into(writer);
35        self.tuple_index.write_into(writer);
36        self.peak_tuple.write_into(writer);
37        self.intermediate_start_tuple.write_into(writer);
38        self.intermediate_end_tuple.write_into(writer);
39    }
40    fn table_type(&self) -> TableType {
41        TableType::Named("TupleVariationHeader")
42    }
43}
44
45impl Validate for TupleVariationHeader {
46    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
47}
48
49impl<'a> FromObjRef<read_fonts::tables::variations::TupleVariationHeader<'a>>
50    for TupleVariationHeader
51{
52    fn from_obj_ref(
53        obj: &read_fonts::tables::variations::TupleVariationHeader<'a>,
54        _: FontData,
55    ) -> Self {
56        let offset_data = obj.offset_data();
57        TupleVariationHeader {
58            variation_data_size: obj.variation_data_size(),
59            tuple_index: obj.tuple_index(),
60            peak_tuple: obj.peak_tuple().to_owned_obj(offset_data),
61            intermediate_start_tuple: obj.intermediate_start_tuple().to_owned_obj(offset_data),
62            intermediate_end_tuple: obj.intermediate_end_tuple().to_owned_obj(offset_data),
63        }
64    }
65}
66
67#[allow(clippy::needless_lifetimes)]
68impl<'a> FromTableRef<read_fonts::tables::variations::TupleVariationHeader<'a>>
69    for TupleVariationHeader
70{
71}
72
73/// A [Tuple Record](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuple-records)
74///
75/// The tuple variation store formats reference regions within the font’s
76/// variation space using tuple records. A tuple record identifies a position
77/// in terms of normalized coordinates, which use F2DOT14 values.
78#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
79#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
80pub struct Tuple {
81    /// Coordinate array specifying a position within the font’s variation space.
82    ///
83    /// The number of elements must match the axisCount specified in the
84    /// 'fvar' table.
85    pub values: Vec<F2Dot14>,
86}
87
88impl Tuple {
89    /// Construct a new `Tuple`
90    pub fn new(values: Vec<F2Dot14>) -> Self {
91        Self { values }
92    }
93}
94
95impl FontWrite for Tuple {
96    fn write_into(&self, writer: &mut TableWriter) {
97        self.values.write_into(writer);
98    }
99    fn table_type(&self) -> TableType {
100        TableType::Named("Tuple")
101    }
102}
103
104impl Validate for Tuple {
105    fn validate_impl(&self, ctx: &mut ValidationCtx) {
106        ctx.in_table("Tuple", |ctx| {
107            ctx.in_field("values", |ctx| {
108                if self.values.len() > to_usize(u16::MAX) {
109                    ctx.report("array exceeds max length");
110                }
111            });
112        })
113    }
114}
115
116impl FromObjRef<read_fonts::tables::variations::Tuple<'_>> for Tuple {
117    fn from_obj_ref(obj: &read_fonts::tables::variations::Tuple, offset_data: FontData) -> Self {
118        Tuple {
119            values: obj.values().to_owned_obj(offset_data),
120        }
121    }
122}
123
124/// The [DeltaSetIndexMap](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#associating-target-items-to-variation-data) table format 0
125#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
126#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
127pub struct DeltaSetIndexMapFormat0 {
128    /// A packed field that describes the compressed representation of
129    /// delta-set indices. See details below.
130    pub entry_format: EntryFormat,
131    /// The number of mapping entries.
132    pub map_count: u16,
133    /// The delta-set index mapping data. See details below.
134    pub map_data: Vec<u8>,
135}
136
137impl DeltaSetIndexMapFormat0 {
138    /// Construct a new `DeltaSetIndexMapFormat0`
139    pub fn new(entry_format: EntryFormat, map_count: u16, map_data: Vec<u8>) -> Self {
140        Self {
141            entry_format,
142            map_count,
143            map_data,
144        }
145    }
146}
147
148impl FontWrite for DeltaSetIndexMapFormat0 {
149    #[allow(clippy::unnecessary_cast)]
150    fn write_into(&self, writer: &mut TableWriter) {
151        (0 as u8).write_into(writer);
152        self.entry_format.write_into(writer);
153        self.map_count.write_into(writer);
154        self.map_data.write_into(writer);
155    }
156    fn table_type(&self) -> TableType {
157        TableType::Named("DeltaSetIndexMapFormat0")
158    }
159}
160
161impl Validate for DeltaSetIndexMapFormat0 {
162    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
163}
164
165impl<'a> FromObjRef<read_fonts::tables::variations::DeltaSetIndexMapFormat0<'a>>
166    for DeltaSetIndexMapFormat0
167{
168    fn from_obj_ref(
169        obj: &read_fonts::tables::variations::DeltaSetIndexMapFormat0<'a>,
170        _: FontData,
171    ) -> Self {
172        let offset_data = obj.offset_data();
173        DeltaSetIndexMapFormat0 {
174            entry_format: obj.entry_format(),
175            map_count: obj.map_count(),
176            map_data: obj.map_data().to_owned_obj(offset_data),
177        }
178    }
179}
180
181#[allow(clippy::needless_lifetimes)]
182impl<'a> FromTableRef<read_fonts::tables::variations::DeltaSetIndexMapFormat0<'a>>
183    for DeltaSetIndexMapFormat0
184{
185}
186
187impl ReadArgs for DeltaSetIndexMapFormat0 {
188    type Args = ();
189}
190
191impl<'a> FontRead<'a> for DeltaSetIndexMapFormat0 {
192    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
193        <read_fonts::tables::variations::DeltaSetIndexMapFormat0 as FontRead>::read(data)
194            .map(|x| x.to_owned_table())
195    }
196}
197
198/// The [DeltaSetIndexMap](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#associating-target-items-to-variation-data) table format 1
199#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
200#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
201pub struct DeltaSetIndexMapFormat1 {
202    /// A packed field that describes the compressed representation of
203    /// delta-set indices. See details below.
204    pub entry_format: EntryFormat,
205    /// The number of mapping entries.
206    pub map_count: u32,
207    /// The delta-set index mapping data. See details below.
208    pub map_data: Vec<u8>,
209}
210
211impl DeltaSetIndexMapFormat1 {
212    /// Construct a new `DeltaSetIndexMapFormat1`
213    pub fn new(entry_format: EntryFormat, map_count: u32, map_data: Vec<u8>) -> Self {
214        Self {
215            entry_format,
216            map_count,
217            map_data,
218        }
219    }
220}
221
222impl FontWrite for DeltaSetIndexMapFormat1 {
223    #[allow(clippy::unnecessary_cast)]
224    fn write_into(&self, writer: &mut TableWriter) {
225        (1 as u8).write_into(writer);
226        self.entry_format.write_into(writer);
227        self.map_count.write_into(writer);
228        self.map_data.write_into(writer);
229    }
230    fn table_type(&self) -> TableType {
231        TableType::Named("DeltaSetIndexMapFormat1")
232    }
233}
234
235impl Validate for DeltaSetIndexMapFormat1 {
236    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
237}
238
239impl<'a> FromObjRef<read_fonts::tables::variations::DeltaSetIndexMapFormat1<'a>>
240    for DeltaSetIndexMapFormat1
241{
242    fn from_obj_ref(
243        obj: &read_fonts::tables::variations::DeltaSetIndexMapFormat1<'a>,
244        _: FontData,
245    ) -> Self {
246        let offset_data = obj.offset_data();
247        DeltaSetIndexMapFormat1 {
248            entry_format: obj.entry_format(),
249            map_count: obj.map_count(),
250            map_data: obj.map_data().to_owned_obj(offset_data),
251        }
252    }
253}
254
255#[allow(clippy::needless_lifetimes)]
256impl<'a> FromTableRef<read_fonts::tables::variations::DeltaSetIndexMapFormat1<'a>>
257    for DeltaSetIndexMapFormat1
258{
259}
260
261impl ReadArgs for DeltaSetIndexMapFormat1 {
262    type Args = ();
263}
264
265impl<'a> FontRead<'a> for DeltaSetIndexMapFormat1 {
266    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
267        <read_fonts::tables::variations::DeltaSetIndexMapFormat1 as FontRead>::read(data)
268            .map(|x| x.to_owned_table())
269    }
270}
271
272/// The [DeltaSetIndexMap](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#associating-target-items-to-variation-data) table
273#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
274#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
275pub enum DeltaSetIndexMap {
276    Format0(DeltaSetIndexMapFormat0),
277    Format1(DeltaSetIndexMapFormat1),
278}
279
280impl DeltaSetIndexMap {
281    /// Construct a new `DeltaSetIndexMapFormat0` subtable
282    pub fn format_0(entry_format: EntryFormat, map_count: u16, map_data: Vec<u8>) -> Self {
283        Self::Format0(DeltaSetIndexMapFormat0::new(
284            entry_format,
285            map_count,
286            map_data,
287        ))
288    }
289
290    /// Construct a new `DeltaSetIndexMapFormat1` subtable
291    pub fn format_1(entry_format: EntryFormat, map_count: u32, map_data: Vec<u8>) -> Self {
292        Self::Format1(DeltaSetIndexMapFormat1::new(
293            entry_format,
294            map_count,
295            map_data,
296        ))
297    }
298}
299
300impl Default for DeltaSetIndexMap {
301    fn default() -> Self {
302        Self::Format0(Default::default())
303    }
304}
305
306impl FontWrite for DeltaSetIndexMap {
307    fn write_into(&self, writer: &mut TableWriter) {
308        match self {
309            Self::Format0(item) => item.write_into(writer),
310            Self::Format1(item) => item.write_into(writer),
311        }
312    }
313    fn table_type(&self) -> TableType {
314        match self {
315            Self::Format0(item) => item.table_type(),
316            Self::Format1(item) => item.table_type(),
317        }
318    }
319}
320
321impl Validate for DeltaSetIndexMap {
322    fn validate_impl(&self, ctx: &mut ValidationCtx) {
323        match self {
324            Self::Format0(item) => item.validate_impl(ctx),
325            Self::Format1(item) => item.validate_impl(ctx),
326        }
327    }
328}
329
330impl FromObjRef<read_fonts::tables::variations::DeltaSetIndexMap<'_>> for DeltaSetIndexMap {
331    fn from_obj_ref(obj: &read_fonts::tables::variations::DeltaSetIndexMap, _: FontData) -> Self {
332        use read_fonts::tables::variations::DeltaSetIndexMap as ObjRefType;
333        match obj {
334            ObjRefType::Format0(item) => DeltaSetIndexMap::Format0(item.to_owned_table()),
335            ObjRefType::Format1(item) => DeltaSetIndexMap::Format1(item.to_owned_table()),
336        }
337    }
338}
339
340impl FromTableRef<read_fonts::tables::variations::DeltaSetIndexMap<'_>> for DeltaSetIndexMap {}
341
342impl ReadArgs for DeltaSetIndexMap {
343    type Args = ();
344}
345
346impl<'a> FontRead<'a> for DeltaSetIndexMap {
347    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
348        <read_fonts::tables::variations::DeltaSetIndexMap as FontRead>::read(data)
349            .map(|x| x.to_owned_table())
350    }
351}
352
353impl From<DeltaSetIndexMapFormat0> for DeltaSetIndexMap {
354    fn from(src: DeltaSetIndexMapFormat0) -> DeltaSetIndexMap {
355        DeltaSetIndexMap::Format0(src)
356    }
357}
358
359impl From<DeltaSetIndexMapFormat1> for DeltaSetIndexMap {
360    fn from(src: DeltaSetIndexMapFormat1) -> DeltaSetIndexMap {
361        DeltaSetIndexMap::Format1(src)
362    }
363}
364
365impl FontWrite for EntryFormat {
366    fn write_into(&self, writer: &mut TableWriter) {
367        writer.write_slice(&self.bits().to_be_bytes())
368    }
369}
370
371/// The [VariationRegionList](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#variation-regions) table
372#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
373#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
374pub struct VariationRegionList {
375    /// The number of variation axes for this font. This must be the
376    /// same number as axisCount in the 'fvar' table.
377    pub axis_count: u16,
378    /// Array of variation regions.
379    pub variation_regions: Vec<VariationRegion>,
380}
381
382impl VariationRegionList {
383    /// Construct a new `VariationRegionList`
384    pub fn new(axis_count: u16, variation_regions: Vec<VariationRegion>) -> Self {
385        Self {
386            axis_count,
387            variation_regions,
388        }
389    }
390}
391
392impl FontWrite for VariationRegionList {
393    #[allow(clippy::unnecessary_cast)]
394    fn write_into(&self, writer: &mut TableWriter) {
395        self.axis_count.write_into(writer);
396        (u16::try_from(array_len(&self.variation_regions)).unwrap()).write_into(writer);
397        self.variation_regions.write_into(writer);
398    }
399    fn table_type(&self) -> TableType {
400        TableType::Named("VariationRegionList")
401    }
402}
403
404impl Validate for VariationRegionList {
405    fn validate_impl(&self, ctx: &mut ValidationCtx) {
406        ctx.in_table("VariationRegionList", |ctx| {
407            ctx.in_field("variation_regions", |ctx| {
408                if self.variation_regions.len() > to_usize(u16::MAX) {
409                    ctx.report("array exceeds max length");
410                }
411                self.variation_regions.validate_impl(ctx);
412            });
413        })
414    }
415}
416
417impl<'a> FromObjRef<read_fonts::tables::variations::VariationRegionList<'a>>
418    for VariationRegionList
419{
420    fn from_obj_ref(
421        obj: &read_fonts::tables::variations::VariationRegionList<'a>,
422        _: FontData,
423    ) -> Self {
424        let offset_data = obj.offset_data();
425        VariationRegionList {
426            axis_count: obj.axis_count(),
427            variation_regions: obj
428                .variation_regions()
429                .iter()
430                .filter_map(|x| x.map(|x| FromObjRef::from_obj_ref(&x, offset_data)).ok())
431                .collect(),
432        }
433    }
434}
435
436#[allow(clippy::needless_lifetimes)]
437impl<'a> FromTableRef<read_fonts::tables::variations::VariationRegionList<'a>>
438    for VariationRegionList
439{
440}
441
442impl ReadArgs for VariationRegionList {
443    type Args = ();
444}
445
446impl<'a> FontRead<'a> for VariationRegionList {
447    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
448        <read_fonts::tables::variations::VariationRegionList as FontRead>::read(data)
449            .map(|x| x.to_owned_table())
450    }
451}
452
453/// The [VariationRegion](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#variation-regions) record
454#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
455#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
456pub struct VariationRegion {
457    /// Array of region axis coordinates records, in the order of axes
458    /// given in the 'fvar' table.
459    pub region_axes: Vec<RegionAxisCoordinates>,
460}
461
462impl VariationRegion {
463    /// Construct a new `VariationRegion`
464    pub fn new(region_axes: Vec<RegionAxisCoordinates>) -> Self {
465        Self { region_axes }
466    }
467}
468
469impl FontWrite for VariationRegion {
470    fn write_into(&self, writer: &mut TableWriter) {
471        self.region_axes.write_into(writer);
472    }
473    fn table_type(&self) -> TableType {
474        TableType::Named("VariationRegion")
475    }
476}
477
478impl Validate for VariationRegion {
479    fn validate_impl(&self, ctx: &mut ValidationCtx) {
480        ctx.in_table("VariationRegion", |ctx| {
481            ctx.in_field("region_axes", |ctx| {
482                if self.region_axes.len() > to_usize(u16::MAX) {
483                    ctx.report("array exceeds max length");
484                }
485                self.region_axes.validate_impl(ctx);
486            });
487        })
488    }
489}
490
491impl FromObjRef<read_fonts::tables::variations::VariationRegion<'_>> for VariationRegion {
492    fn from_obj_ref(
493        obj: &read_fonts::tables::variations::VariationRegion,
494        offset_data: FontData,
495    ) -> Self {
496        VariationRegion {
497            region_axes: obj.region_axes().to_owned_obj(offset_data),
498        }
499    }
500}
501
502/// The [RegionAxisCoordinates](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#variation-regions) record
503#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
504#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
505pub struct RegionAxisCoordinates {
506    /// The region start coordinate value for the current axis.
507    pub start_coord: F2Dot14,
508    /// The region peak coordinate value for the current axis.
509    pub peak_coord: F2Dot14,
510    /// The region end coordinate value for the current axis.
511    pub end_coord: F2Dot14,
512}
513
514impl RegionAxisCoordinates {
515    /// Construct a new `RegionAxisCoordinates`
516    pub fn new(start_coord: F2Dot14, peak_coord: F2Dot14, end_coord: F2Dot14) -> Self {
517        Self {
518            start_coord,
519            peak_coord,
520            end_coord,
521        }
522    }
523}
524
525impl FontWrite for RegionAxisCoordinates {
526    fn write_into(&self, writer: &mut TableWriter) {
527        self.start_coord.write_into(writer);
528        self.peak_coord.write_into(writer);
529        self.end_coord.write_into(writer);
530    }
531    fn table_type(&self) -> TableType {
532        TableType::Named("RegionAxisCoordinates")
533    }
534}
535
536impl Validate for RegionAxisCoordinates {
537    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
538}
539
540impl FromObjRef<read_fonts::tables::variations::RegionAxisCoordinates> for RegionAxisCoordinates {
541    fn from_obj_ref(
542        obj: &read_fonts::tables::variations::RegionAxisCoordinates,
543        _: FontData,
544    ) -> Self {
545        RegionAxisCoordinates {
546            start_coord: obj.start_coord(),
547            peak_coord: obj.peak_coord(),
548            end_coord: obj.end_coord(),
549        }
550    }
551}
552
553/// The [ItemVariationStore](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#item-variation-store-header-and-item-variation-data-subtables) table
554#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
555#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
556pub struct ItemVariationStore {
557    /// Offset in bytes from the start of the item variation store to
558    /// the variation region list.
559    pub variation_region_list: OffsetMarker<VariationRegionList, WIDTH_32>,
560    /// Offsets in bytes from the start of the item variation store to
561    /// each item variation data subtable.
562    pub item_variation_data: Vec<NullableOffsetMarker<ItemVariationData, WIDTH_32>>,
563}
564
565impl ItemVariationStore {
566    /// Construct a new `ItemVariationStore`
567    pub fn new(
568        variation_region_list: VariationRegionList,
569        item_variation_data: Vec<Option<ItemVariationData>>,
570    ) -> Self {
571        Self {
572            variation_region_list: variation_region_list.into(),
573            item_variation_data: item_variation_data.into_iter().map(Into::into).collect(),
574        }
575    }
576}
577
578impl FontWrite for ItemVariationStore {
579    #[allow(clippy::unnecessary_cast)]
580    fn write_into(&self, writer: &mut TableWriter) {
581        (1 as u16).write_into(writer);
582        self.variation_region_list.write_into(writer);
583        (u16::try_from(array_len(&self.item_variation_data)).unwrap()).write_into(writer);
584        self.item_variation_data.write_into(writer);
585    }
586    fn table_type(&self) -> TableType {
587        TableType::Named("ItemVariationStore")
588    }
589}
590
591impl Validate for ItemVariationStore {
592    fn validate_impl(&self, ctx: &mut ValidationCtx) {
593        ctx.in_table("ItemVariationStore", |ctx| {
594            ctx.in_field("variation_region_list", |ctx| {
595                self.variation_region_list.validate_impl(ctx);
596            });
597            ctx.in_field("item_variation_data", |ctx| {
598                if self.item_variation_data.len() > to_usize(u16::MAX) {
599                    ctx.report("array exceeds max length");
600                }
601                self.item_variation_data.validate_impl(ctx);
602            });
603        })
604    }
605}
606
607impl<'a> FromObjRef<read_fonts::tables::variations::ItemVariationStore<'a>> for ItemVariationStore {
608    fn from_obj_ref(
609        obj: &read_fonts::tables::variations::ItemVariationStore<'a>,
610        _: FontData,
611    ) -> Self {
612        ItemVariationStore {
613            variation_region_list: obj.variation_region_list().to_owned_table(),
614            item_variation_data: obj.item_variation_data().to_owned_table(),
615        }
616    }
617}
618
619#[allow(clippy::needless_lifetimes)]
620impl<'a> FromTableRef<read_fonts::tables::variations::ItemVariationStore<'a>>
621    for ItemVariationStore
622{
623}
624
625impl ReadArgs for ItemVariationStore {
626    type Args = ();
627}
628
629impl<'a> FontRead<'a> for ItemVariationStore {
630    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
631        <read_fonts::tables::variations::ItemVariationStore as FontRead>::read(data)
632            .map(|x| x.to_owned_table())
633    }
634}
635
636/// The [ItemVariationData](https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#item-variation-store-header-and-item-variation-data-subtables) subtable
637#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
638#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
639pub struct ItemVariationData {
640    /// The number of delta sets for distinct items.
641    pub item_count: u16,
642    /// A packed field: the high bit is a flag—see details below.
643    pub word_delta_count: u16,
644    /// Array of indices into the variation region list for the regions
645    /// referenced by this item variation data table.
646    pub region_indexes: Vec<u16>,
647    /// Delta-set rows.
648    pub delta_sets: Vec<u8>,
649}
650
651impl ItemVariationData {
652    /// Construct a new `ItemVariationData`
653    pub fn new(
654        item_count: u16,
655        word_delta_count: u16,
656        region_indexes: Vec<u16>,
657        delta_sets: Vec<u8>,
658    ) -> Self {
659        Self {
660            item_count,
661            word_delta_count,
662            region_indexes,
663            delta_sets,
664        }
665    }
666}
667
668impl FontWrite for ItemVariationData {
669    #[allow(clippy::unnecessary_cast)]
670    fn write_into(&self, writer: &mut TableWriter) {
671        self.item_count.write_into(writer);
672        self.word_delta_count.write_into(writer);
673        (u16::try_from(array_len(&self.region_indexes)).unwrap()).write_into(writer);
674        self.region_indexes.write_into(writer);
675        self.delta_sets.write_into(writer);
676    }
677    fn table_type(&self) -> TableType {
678        TableType::Named("ItemVariationData")
679    }
680}
681
682impl Validate for ItemVariationData {
683    fn validate_impl(&self, ctx: &mut ValidationCtx) {
684        ctx.in_table("ItemVariationData", |ctx| {
685            ctx.in_field("region_indexes", |ctx| {
686                if self.region_indexes.len() > to_usize(u16::MAX) {
687                    ctx.report("array exceeds max length");
688                }
689            });
690        })
691    }
692}
693
694impl<'a> FromObjRef<read_fonts::tables::variations::ItemVariationData<'a>> for ItemVariationData {
695    fn from_obj_ref(
696        obj: &read_fonts::tables::variations::ItemVariationData<'a>,
697        _: FontData,
698    ) -> Self {
699        let offset_data = obj.offset_data();
700        ItemVariationData {
701            item_count: obj.item_count(),
702            word_delta_count: obj.word_delta_count(),
703            region_indexes: obj.region_indexes().to_owned_obj(offset_data),
704            delta_sets: obj.delta_sets().to_owned_obj(offset_data),
705        }
706    }
707}
708
709#[allow(clippy::needless_lifetimes)]
710impl<'a> FromTableRef<read_fonts::tables::variations::ItemVariationData<'a>> for ItemVariationData {}
711
712impl ReadArgs for ItemVariationData {
713    type Args = ();
714}
715
716impl<'a> FontRead<'a> for ItemVariationData {
717    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
718        <read_fonts::tables::variations::ItemVariationData as FontRead>::read(data)
719            .map(|x| x.to_owned_table())
720    }
721}