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
166            ..start
167                + (transforms::to_usize(num_entries))
168                    .saturating_mul(SVGDocumentRecord::RAW_BYTE_LEN)
169    }
170}
171
172const _: () = assert!(FontData::default_data_long_enough(
173    SVGDocumentList::MIN_SIZE
174));
175
176impl Default for SVGDocumentList<'_> {
177    fn default() -> Self {
178        Self {
179            data: FontData::default_table_data(),
180        }
181    }
182}
183
184#[cfg(feature = "experimental_traverse")]
185impl<'a> SomeTable<'a> for SVGDocumentList<'a> {
186    fn type_name(&self) -> &str {
187        "SVGDocumentList"
188    }
189    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
190        match idx {
191            0usize => Some(Field::new("num_entries", self.num_entries())),
192            1usize => Some(Field::new(
193                "document_records",
194                traversal::FieldType::array_of_records(
195                    stringify!(SVGDocumentRecord),
196                    self.document_records(),
197                    self.offset_data(),
198                ),
199            )),
200            _ => None,
201        }
202    }
203}
204
205#[cfg(feature = "experimental_traverse")]
206#[allow(clippy::needless_lifetimes)]
207impl<'a> std::fmt::Debug for SVGDocumentList<'a> {
208    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
209        (self as &dyn SomeTable<'a>).fmt(f)
210    }
211}
212
213/// [SVGDocumentRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/svg)
214#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
215#[repr(C)]
216#[repr(packed)]
217pub struct SVGDocumentRecord {
218    /// The first glyph ID for the range covered by this record.
219    pub start_glyph_id: BigEndian<GlyphId16>,
220    /// The last glyph ID for the range covered by this record.
221    pub end_glyph_id: BigEndian<GlyphId16>,
222    /// Offset from the beginning of the SVGDocumentList to an SVG
223    /// document. Must be non-zero.
224    pub svg_doc_offset: BigEndian<u32>,
225    /// Length of the SVG document data. Must be non-zero.
226    pub svg_doc_length: BigEndian<u32>,
227}
228
229impl SVGDocumentRecord {
230    /// The first glyph ID for the range covered by this record.
231    pub fn start_glyph_id(&self) -> GlyphId16 {
232        self.start_glyph_id.get()
233    }
234
235    /// The last glyph ID for the range covered by this record.
236    pub fn end_glyph_id(&self) -> GlyphId16 {
237        self.end_glyph_id.get()
238    }
239
240    /// Offset from the beginning of the SVGDocumentList to an SVG
241    /// document. Must be non-zero.
242    pub fn svg_doc_offset(&self) -> u32 {
243        self.svg_doc_offset.get()
244    }
245
246    /// Length of the SVG document data. Must be non-zero.
247    pub fn svg_doc_length(&self) -> u32 {
248        self.svg_doc_length.get()
249    }
250}
251
252impl FixedSize for SVGDocumentRecord {
253    const RAW_BYTE_LEN: usize =
254        GlyphId16::RAW_BYTE_LEN + GlyphId16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
255}
256
257#[cfg(feature = "experimental_traverse")]
258impl<'a> SomeRecord<'a> for SVGDocumentRecord {
259    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
260        RecordResolver {
261            name: "SVGDocumentRecord",
262            get_field: Box::new(move |idx, _data| match idx {
263                0usize => Some(Field::new("start_glyph_id", self.start_glyph_id())),
264                1usize => Some(Field::new("end_glyph_id", self.end_glyph_id())),
265                2usize => Some(Field::new("svg_doc_offset", self.svg_doc_offset())),
266                3usize => Some(Field::new("svg_doc_length", self.svg_doc_length())),
267                _ => None,
268            }),
269            data,
270        }
271    }
272}