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