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