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.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        Ok(Self { data })
34    }
35}
36
37/// [`meta`](https://docs.microsoft.com/en-us/typography/opentype/spec/meta)
38#[derive(Clone)]
39pub struct Meta<'a> {
40    data: FontData<'a>,
41}
42
43#[allow(clippy::needless_lifetimes)]
44impl<'a> Meta<'a> {
45    pub const MIN_SIZE: usize =
46        (u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
47    basic_table_impls!(impl_the_methods);
48
49    /// Version number of the metadata table — set to 1.
50    pub fn version(&self) -> u32 {
51        let range = self.version_byte_range();
52        self.data.read_at(range.start).ok().unwrap()
53    }
54
55    /// Flags — currently unused; set to 0.
56    pub fn flags(&self) -> u32 {
57        let range = self.flags_byte_range();
58        self.data.read_at(range.start).ok().unwrap()
59    }
60
61    /// The number of data maps in the table.
62    pub fn data_maps_count(&self) -> u32 {
63        let range = self.data_maps_count_byte_range();
64        self.data.read_at(range.start).ok().unwrap()
65    }
66
67    /// Array of data map records.
68    pub fn data_maps(&self) -> &'a [DataMapRecord] {
69        let range = self.data_maps_byte_range();
70        self.data.read_array(range).ok().unwrap_or_default()
71    }
72
73    pub fn version_byte_range(&self) -> Range<usize> {
74        let start = 0;
75        let end = start + u32::RAW_BYTE_LEN;
76        start..end
77    }
78
79    pub fn flags_byte_range(&self) -> Range<usize> {
80        let start = self.version_byte_range().end;
81        let end = start + u32::RAW_BYTE_LEN;
82        start..end
83    }
84
85    pub fn reserved_byte_range(&self) -> Range<usize> {
86        let start = self.flags_byte_range().end;
87        let end = start + u32::RAW_BYTE_LEN;
88        start..end
89    }
90
91    pub fn data_maps_count_byte_range(&self) -> Range<usize> {
92        let start = self.reserved_byte_range().end;
93        let end = start + u32::RAW_BYTE_LEN;
94        start..end
95    }
96
97    pub fn data_maps_byte_range(&self) -> Range<usize> {
98        let data_maps_count = self.data_maps_count();
99        let start = self.data_maps_count_byte_range().end;
100        let end = start
101            + (transforms::to_usize(data_maps_count)).saturating_mul(DataMapRecord::RAW_BYTE_LEN);
102        start..end
103    }
104}
105
106const _: () = assert!(FontData::default_data_long_enough(Meta::MIN_SIZE));
107
108impl Default for Meta<'_> {
109    fn default() -> Self {
110        Self {
111            data: FontData::default_table_data(),
112        }
113    }
114}
115
116#[cfg(feature = "experimental_traverse")]
117impl<'a> SomeTable<'a> for Meta<'a> {
118    fn type_name(&self) -> &str {
119        "Meta"
120    }
121    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
122        match idx {
123            0usize => Some(Field::new("version", self.version())),
124            1usize => Some(Field::new("flags", self.flags())),
125            2usize => Some(Field::new("data_maps_count", self.data_maps_count())),
126            3usize => Some(Field::new(
127                "data_maps",
128                traversal::FieldType::array_of_records(
129                    stringify!(DataMapRecord),
130                    self.data_maps(),
131                    self.offset_data(),
132                ),
133            )),
134            _ => None,
135        }
136    }
137}
138
139#[cfg(feature = "experimental_traverse")]
140#[allow(clippy::needless_lifetimes)]
141impl<'a> std::fmt::Debug for Meta<'a> {
142    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
143        (self as &dyn SomeTable<'a>).fmt(f)
144    }
145}
146
147///  <https://learn.microsoft.com/en-us/typography/opentype/spec/meta#table-formats>
148#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
149#[repr(C)]
150#[repr(packed)]
151pub struct DataMapRecord {
152    /// A tag indicating the type of metadata.
153    pub tag: BigEndian<Tag>,
154    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
155    pub data_offset: BigEndian<Offset32>,
156    /// Length of the data, in bytes. The data is not required to be padded to any byte boundary.
157    pub data_length: BigEndian<u32>,
158}
159
160impl DataMapRecord {
161    /// A tag indicating the type of metadata.
162    pub fn tag(&self) -> Tag {
163        self.tag.get()
164    }
165
166    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
167    pub fn data_offset(&self) -> Offset32 {
168        self.data_offset.get()
169    }
170
171    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
172    ///
173    /// The `data` argument should be retrieved from the parent table
174    /// By calling its `offset_data` method.
175    pub fn data<'a>(&self, data: FontData<'a>) -> Result<Metadata<'a>, ReadError> {
176        let args = (self.tag(), self.data_length());
177        self.data_offset().resolve_with_args(data, args)
178    }
179
180    /// Length of the data, in bytes. The data is not required to be padded to any byte boundary.
181    pub fn data_length(&self) -> u32 {
182        self.data_length.get()
183    }
184}
185
186impl FixedSize for DataMapRecord {
187    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
188}
189
190#[cfg(feature = "experimental_traverse")]
191impl<'a> SomeRecord<'a> for DataMapRecord {
192    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
193        RecordResolver {
194            name: "DataMapRecord",
195            get_field: Box::new(move |idx, _data| match idx {
196                0usize => Some(Field::new("tag", self.tag())),
197                1usize => Some(Field::new("data_offset", traversal::FieldType::Unknown)),
198                2usize => Some(Field::new("data_length", self.data_length())),
199                _ => None,
200            }),
201            data,
202        }
203    }
204}