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