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///  <https://learn.microsoft.com/en-us/typography/opentype/spec/meta#table-formats>
117#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
118#[repr(C)]
119#[repr(packed)]
120pub struct DataMapRecord {
121    /// A tag indicating the type of metadata.
122    pub tag: BigEndian<Tag>,
123    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
124    pub data_offset: BigEndian<Offset32>,
125    /// Length of the data, in bytes. The data is not required to be padded to any byte boundary.
126    pub data_length: BigEndian<u32>,
127}
128
129impl DataMapRecord {
130    /// A tag indicating the type of metadata.
131    pub fn tag(&self) -> Tag {
132        self.tag.get()
133    }
134
135    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
136    pub fn data_offset(&self) -> Offset32 {
137        self.data_offset.get()
138    }
139
140    /// Offset in bytes from the beginning of the metadata table to the data for this tag.
141    ///
142    /// The `data` argument should be retrieved from the parent table
143    /// By calling its `offset_data` method.
144    pub fn data<'a>(&self, data: FontData<'a>) -> Result<Metadata<'a>, ReadError> {
145        let args = (self.tag(), self.data_length());
146        self.data_offset().resolve_with_args(data, args)
147    }
148
149    /// Length of the data, in bytes. The data is not required to be padded to any byte boundary.
150    pub fn data_length(&self) -> u32 {
151        self.data_length.get()
152    }
153}
154
155impl FixedSize for DataMapRecord {
156    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
157}