Skip to main content

read_fonts/generated/
generated_meta.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 Meta<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.data_maps_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 Meta<'_> {
19    /// `meta`
20    const TAG: Tag = Tag::new(b"meta");
21}
22
23impl ReadArgs for Meta<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Meta<'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 MetaFixedHeader {
45    pub version: BigEndian<u32>,
46    pub flags: BigEndian<u32>,
47    pub reserved: BigEndian<u32>,
48    pub data_maps_count: BigEndian<u32>,
49}
50
51#[allow(clippy::absurd_extreme_comparisons)]
52const _: () = assert!(std::mem::size_of::<MetaFixedHeader>() <= Meta::MIN_SIZE);
53
54/// [`meta`](https://docs.microsoft.com/en-us/typography/opentype/spec/meta)
55#[derive(Clone)]
56pub struct Meta<'a> {
57    data: TableDataRef<'a, MetaFixedHeader>,
58}
59
60#[allow(clippy::needless_lifetimes)]
61impl<'a> Meta<'a> {
62    pub const MIN_SIZE: usize =
63        (u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
64    basic_table_impls!(impl_the_methods);
65
66    /// Version number of the metadata table — set to 1.
67    pub fn version(&self) -> u32 {
68        self.data.header().version.get()
69    }
70
71    /// Flags — currently unused; set to 0.
72    pub fn flags(&self) -> u32 {
73        self.data.header().flags.get()
74    }
75
76    /// The number of data maps in the table.
77    pub fn data_maps_count(&self) -> u32 {
78        self.data.header().data_maps_count.get()
79    }
80
81    /// Array of data map records.
82    pub fn data_maps(&self) -> &'a [DataMapRecord] {
83        let range = self.data_maps_byte_range();
84        self.offset_data()
85            .read_array(range)
86            .ok()
87            .unwrap_or_default()
88    }
89
90    pub fn version_byte_range(&self) -> Range<usize> {
91        let start = 0;
92        let end = start + u32::RAW_BYTE_LEN;
93        start..end
94    }
95
96    pub fn flags_byte_range(&self) -> Range<usize> {
97        let start = self.version_byte_range().end;
98        let end = start + u32::RAW_BYTE_LEN;
99        start..end
100    }
101
102    pub fn reserved_byte_range(&self) -> Range<usize> {
103        let start = self.flags_byte_range().end;
104        let end = start + u32::RAW_BYTE_LEN;
105        start..end
106    }
107
108    pub fn data_maps_count_byte_range(&self) -> Range<usize> {
109        let start = self.reserved_byte_range().end;
110        let end = start + u32::RAW_BYTE_LEN;
111        start..end
112    }
113
114    pub fn data_maps_byte_range(&self) -> Range<usize> {
115        let data_maps_count = self.data_maps_count();
116        let start = self.data_maps_count_byte_range().end;
117        let end = start
118            + (transforms::to_usize(data_maps_count)).saturating_mul(DataMapRecord::RAW_BYTE_LEN);
119        start..end
120    }
121}
122
123const _: () = assert!(FontData::default_data_long_enough(Meta::MIN_SIZE));
124
125impl Default for Meta<'_> {
126    fn default() -> Self {
127        Self {
128            data: TableDataRef::new(FontData::default_table_data().as_bytes())
129                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
130        }
131    }
132}
133
134///  <https://learn.microsoft.com/en-us/typography/opentype/spec/meta#table-formats>
135#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
136#[repr(C)]
137#[repr(packed)]
138pub struct DataMapRecord {
139    /// A tag indicating the type of metadata.
140    pub tag: BigEndian<Tag>,
141    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
142    pub data_offset: BigEndian<Offset32>,
143    /// Length of the data, in bytes. The data is not required to be padded to any byte boundary.
144    pub data_length: BigEndian<u32>,
145}
146
147impl DataMapRecord {
148    /// A tag indicating the type of metadata.
149    pub fn tag(&self) -> Tag {
150        self.tag.get()
151    }
152
153    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
154    pub fn data_offset(&self) -> Offset32 {
155        self.data_offset.get()
156    }
157
158    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
159    ///
160    /// The `data` argument should be retrieved from the parent table
161    /// By calling its `offset_data` method.
162    pub fn data<'a>(&self, data: FontData<'a>) -> Result<Metadata<'a>, ReadError> {
163        let args = (self.tag(), self.data_length());
164        self.data_offset().resolve_with_args(data, args)
165    }
166
167    /// Length of the data, in bytes. The data is not required to be padded to any byte boundary.
168    pub fn data_length(&self) -> u32 {
169        self.data_length.get()
170    }
171}
172
173impl FixedSize for DataMapRecord {
174    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
175}