Skip to main content

read_fonts/generated/
generated_svg.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 Svg<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self._reserved_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 Svg<'_> {
19    /// `SVG `
20    const TAG: Tag = Tag::new(b"SVG ");
21}
22
23impl<'a> FontRead<'a> for Svg<'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/// The [SVG](https://learn.microsoft.com/en-us/typography/opentype/spec/svg) table
34#[derive(Clone)]
35pub struct Svg<'a> {
36    data: FontData<'a>,
37}
38
39#[allow(clippy::needless_lifetimes)]
40impl<'a> Svg<'a> {
41    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
42    basic_table_impls!(impl_the_methods);
43
44    /// Table version (starting at 0). Set to 0.
45    pub fn version(&self) -> u16 {
46        let range = self.version_byte_range();
47        self.data.read_at(range.start).ok().unwrap()
48    }
49
50    /// Offset to the SVGDocumentList, from the start of the SVG table.
51    /// Must be non-zero.
52    pub fn svg_document_list_offset(&self) -> Offset32 {
53        let range = self.svg_document_list_offset_byte_range();
54        self.data.read_at(range.start).ok().unwrap()
55    }
56
57    /// Attempt to resolve [`svg_document_list_offset`][Self::svg_document_list_offset].
58    pub fn svg_document_list(&self) -> Result<SVGDocumentList<'a>, ReadError> {
59        let data = self.data;
60        self.svg_document_list_offset().resolve(data)
61    }
62
63    pub fn version_byte_range(&self) -> Range<usize> {
64        let start = 0;
65        start..start + u16::RAW_BYTE_LEN
66    }
67
68    pub fn svg_document_list_offset_byte_range(&self) -> Range<usize> {
69        let start = self.version_byte_range().end;
70        start..start + Offset32::RAW_BYTE_LEN
71    }
72
73    pub fn _reserved_byte_range(&self) -> Range<usize> {
74        let start = self.svg_document_list_offset_byte_range().end;
75        start..start + u16::RAW_BYTE_LEN
76    }
77}
78
79const _: () = assert!(FontData::default_data_long_enough(Svg::MIN_SIZE));
80
81impl Default for Svg<'_> {
82    fn default() -> Self {
83        Self {
84            data: FontData::default_table_data(),
85        }
86    }
87}
88
89#[cfg(feature = "experimental_traverse")]
90impl<'a> SomeTable<'a> for Svg<'a> {
91    fn type_name(&self) -> &str {
92        "Svg"
93    }
94    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
95        match idx {
96            0usize => Some(Field::new("version", self.version())),
97            1usize => Some(Field::new(
98                "svg_document_list_offset",
99                FieldType::offset(self.svg_document_list_offset(), self.svg_document_list()),
100            )),
101            _ => None,
102        }
103    }
104}
105
106#[cfg(feature = "experimental_traverse")]
107#[allow(clippy::needless_lifetimes)]
108impl<'a> std::fmt::Debug for Svg<'a> {
109    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110        (self as &dyn SomeTable<'a>).fmt(f)
111    }
112}
113
114impl<'a> MinByteRange<'a> for SVGDocumentList<'a> {
115    fn min_byte_range(&self) -> Range<usize> {
116        0..self.document_records_byte_range().end
117    }
118    fn min_table_bytes(&self) -> &'a [u8] {
119        let range = self.min_byte_range();
120        self.data.as_bytes().get(range).unwrap_or_default()
121    }
122}
123
124impl<'a> FontRead<'a> for SVGDocumentList<'a> {
125    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
126        #[allow(clippy::absurd_extreme_comparisons)]
127        if data.len() < Self::MIN_SIZE {
128            return Err(ReadError::OutOfBounds);
129        }
130        Ok(Self { data })
131    }
132}
133
134/// [SVGDocumentList](https://learn.microsoft.com/en-us/typography/opentype/spec/svg)
135#[derive(Clone)]
136pub struct SVGDocumentList<'a> {
137    data: FontData<'a>,
138}
139
140#[allow(clippy::needless_lifetimes)]
141impl<'a> SVGDocumentList<'a> {
142    pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN;
143    basic_table_impls!(impl_the_methods);
144
145    /// Number of SVGDocumentRecords. Must be non-zero.
146    pub fn num_entries(&self) -> u16 {
147        let range = self.num_entries_byte_range();
148        self.data.read_at(range.start).ok().unwrap()
149    }
150
151    /// Array of SVGDocumentRecords.
152    pub fn document_records(&self) -> &'a [SVGDocumentRecord] {
153        let range = self.document_records_byte_range();
154        self.data.read_array(range).ok().unwrap_or_default()
155    }
156
157    pub fn num_entries_byte_range(&self) -> Range<usize> {
158        let start = 0;
159        start..start + u16::RAW_BYTE_LEN
160    }
161
162    pub fn document_records_byte_range(&self) -> Range<usize> {
163        let num_entries = self.num_entries();
164        let start = self.num_entries_byte_range().end;
165        start..start + (num_entries as usize).saturating_mul(SVGDocumentRecord::RAW_BYTE_LEN)
166    }
167}
168
169const _: () = assert!(FontData::default_data_long_enough(
170    SVGDocumentList::MIN_SIZE
171));
172
173impl Default for SVGDocumentList<'_> {
174    fn default() -> Self {
175        Self {
176            data: FontData::default_table_data(),
177        }
178    }
179}
180
181#[cfg(feature = "experimental_traverse")]
182impl<'a> SomeTable<'a> for SVGDocumentList<'a> {
183    fn type_name(&self) -> &str {
184        "SVGDocumentList"
185    }
186    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
187        match idx {
188            0usize => Some(Field::new("num_entries", self.num_entries())),
189            1usize => Some(Field::new(
190                "document_records",
191                traversal::FieldType::array_of_records(
192                    stringify!(SVGDocumentRecord),
193                    self.document_records(),
194                    self.offset_data(),
195                ),
196            )),
197            _ => None,
198        }
199    }
200}
201
202#[cfg(feature = "experimental_traverse")]
203#[allow(clippy::needless_lifetimes)]
204impl<'a> std::fmt::Debug for SVGDocumentList<'a> {
205    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
206        (self as &dyn SomeTable<'a>).fmt(f)
207    }
208}
209
210/// [SVGDocumentRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/svg)
211#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
212#[repr(C)]
213#[repr(packed)]
214pub struct SVGDocumentRecord {
215    /// The first glyph ID for the range covered by this record.
216    pub start_glyph_id: BigEndian<GlyphId16>,
217    /// The last glyph ID for the range covered by this record.
218    pub end_glyph_id: BigEndian<GlyphId16>,
219    /// Offset from the beginning of the SVGDocumentList to an SVG
220    /// document. Must be non-zero.
221    pub svg_doc_offset: BigEndian<u32>,
222    /// Length of the SVG document data. Must be non-zero.
223    pub svg_doc_length: BigEndian<u32>,
224}
225
226impl SVGDocumentRecord {
227    /// The first glyph ID for the range covered by this record.
228    pub fn start_glyph_id(&self) -> GlyphId16 {
229        self.start_glyph_id.get()
230    }
231
232    /// The last glyph ID for the range covered by this record.
233    pub fn end_glyph_id(&self) -> GlyphId16 {
234        self.end_glyph_id.get()
235    }
236
237    /// Offset from the beginning of the SVGDocumentList to an SVG
238    /// document. Must be non-zero.
239    pub fn svg_doc_offset(&self) -> u32 {
240        self.svg_doc_offset.get()
241    }
242
243    /// Length of the SVG document data. Must be non-zero.
244    pub fn svg_doc_length(&self) -> u32 {
245        self.svg_doc_length.get()
246    }
247}
248
249impl FixedSize for SVGDocumentRecord {
250    const RAW_BYTE_LEN: usize =
251        GlyphId16::RAW_BYTE_LEN + GlyphId16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
252}
253
254#[cfg(feature = "experimental_traverse")]
255impl<'a> SomeRecord<'a> for SVGDocumentRecord {
256    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
257        RecordResolver {
258            name: "SVGDocumentRecord",
259            get_field: Box::new(move |idx, _data| match idx {
260                0usize => Some(Field::new("start_glyph_id", self.start_glyph_id())),
261                1usize => Some(Field::new("end_glyph_id", self.end_glyph_id())),
262                2usize => Some(Field::new("svg_doc_offset", self.svg_doc_offset())),
263                3usize => Some(Field::new("svg_doc_length", self.svg_doc_length())),
264                _ => None,
265            }),
266            data,
267        }
268    }
269}