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..start + (data_maps_count as usize).saturating_mul(DataMapRecord::RAW_BYTE_LEN)
93    }
94}
95
96#[cfg(feature = "experimental_traverse")]
97impl<'a> SomeTable<'a> for Meta<'a> {
98    fn type_name(&self) -> &str {
99        "Meta"
100    }
101    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
102        match idx {
103            0usize => Some(Field::new("version", self.version())),
104            1usize => Some(Field::new("flags", self.flags())),
105            2usize => Some(Field::new("data_maps_count", self.data_maps_count())),
106            3usize => Some(Field::new(
107                "data_maps",
108                traversal::FieldType::array_of_records(
109                    stringify!(DataMapRecord),
110                    self.data_maps(),
111                    self.offset_data(),
112                ),
113            )),
114            _ => None,
115        }
116    }
117}
118
119#[cfg(feature = "experimental_traverse")]
120#[allow(clippy::needless_lifetimes)]
121impl<'a> std::fmt::Debug for Meta<'a> {
122    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123        (self as &dyn SomeTable<'a>).fmt(f)
124    }
125}
126
127///  <https://learn.microsoft.com/en-us/typography/opentype/spec/meta#table-formats>
128#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
129#[repr(C)]
130#[repr(packed)]
131pub struct DataMapRecord {
132    /// A tag indicating the type of metadata.
133    pub tag: BigEndian<Tag>,
134    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
135    pub data_offset: BigEndian<Offset32>,
136    /// Length of the data, in bytes. The data is not required to be padded to any byte boundary.
137    pub data_length: BigEndian<u32>,
138}
139
140impl DataMapRecord {
141    /// A tag indicating the type of metadata.
142    pub fn tag(&self) -> Tag {
143        self.tag.get()
144    }
145
146    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
147    pub fn data_offset(&self) -> Offset32 {
148        self.data_offset.get()
149    }
150
151    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
152    ///
153    /// The `data` argument should be retrieved from the parent table
154    /// By calling its `offset_data` method.
155    pub fn data<'a>(&self, data: FontData<'a>) -> Result<Metadata<'a>, ReadError> {
156        let args = (self.tag(), self.data_length());
157        self.data_offset().resolve_with_args(data, &args)
158    }
159
160    /// Length of the data, in bytes. The data is not required to be padded to any byte boundary.
161    pub fn data_length(&self) -> u32 {
162        self.data_length.get()
163    }
164}
165
166impl FixedSize for DataMapRecord {
167    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
168}
169
170#[cfg(feature = "experimental_traverse")]
171impl<'a> SomeRecord<'a> for DataMapRecord {
172    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
173        RecordResolver {
174            name: "DataMapRecord",
175            get_field: Box::new(move |idx, _data| match idx {
176                0usize => Some(Field::new("tag", self.tag())),
177                1usize => Some(Field::new("data_offset", traversal::FieldType::Unknown)),
178                2usize => Some(Field::new("data_length", self.data_length())),
179                _ => None,
180            }),
181            data,
182        }
183    }
184}