Skip to main content

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