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
79#[cfg(feature = "experimental_traverse")]
80impl<'a> SomeTable<'a> for Svg<'a> {
81    fn type_name(&self) -> &str {
82        "Svg"
83    }
84    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
85        match idx {
86            0usize => Some(Field::new("version", self.version())),
87            1usize => Some(Field::new(
88                "svg_document_list_offset",
89                FieldType::offset(self.svg_document_list_offset(), self.svg_document_list()),
90            )),
91            _ => None,
92        }
93    }
94}
95
96#[cfg(feature = "experimental_traverse")]
97#[allow(clippy::needless_lifetimes)]
98impl<'a> std::fmt::Debug for Svg<'a> {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        (self as &dyn SomeTable<'a>).fmt(f)
101    }
102}
103
104impl<'a> MinByteRange<'a> for SVGDocumentList<'a> {
105    fn min_byte_range(&self) -> Range<usize> {
106        0..self.document_records_byte_range().end
107    }
108    fn min_table_bytes(&self) -> &'a [u8] {
109        let range = self.min_byte_range();
110        self.data.as_bytes().get(range).unwrap_or_default()
111    }
112}
113
114impl<'a> FontRead<'a> for SVGDocumentList<'a> {
115    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
116        #[allow(clippy::absurd_extreme_comparisons)]
117        if data.len() < Self::MIN_SIZE {
118            return Err(ReadError::OutOfBounds);
119        }
120        Ok(Self { data })
121    }
122}
123
124/// [SVGDocumentList](https://learn.microsoft.com/en-us/typography/opentype/spec/svg)
125#[derive(Clone)]
126pub struct SVGDocumentList<'a> {
127    data: FontData<'a>,
128}
129
130#[allow(clippy::needless_lifetimes)]
131impl<'a> SVGDocumentList<'a> {
132    pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN;
133    basic_table_impls!(impl_the_methods);
134
135    /// Number of SVGDocumentRecords. Must be non-zero.
136    pub fn num_entries(&self) -> u16 {
137        let range = self.num_entries_byte_range();
138        self.data.read_at(range.start).ok().unwrap()
139    }
140
141    /// Array of SVGDocumentRecords.
142    pub fn document_records(&self) -> &'a [SVGDocumentRecord] {
143        let range = self.document_records_byte_range();
144        self.data.read_array(range).ok().unwrap_or_default()
145    }
146
147    pub fn num_entries_byte_range(&self) -> Range<usize> {
148        let start = 0;
149        start..start + u16::RAW_BYTE_LEN
150    }
151
152    pub fn document_records_byte_range(&self) -> Range<usize> {
153        let num_entries = self.num_entries();
154        let start = self.num_entries_byte_range().end;
155        start..start + (num_entries as usize).saturating_mul(SVGDocumentRecord::RAW_BYTE_LEN)
156    }
157}
158
159#[cfg(feature = "experimental_traverse")]
160impl<'a> SomeTable<'a> for SVGDocumentList<'a> {
161    fn type_name(&self) -> &str {
162        "SVGDocumentList"
163    }
164    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
165        match idx {
166            0usize => Some(Field::new("num_entries", self.num_entries())),
167            1usize => Some(Field::new(
168                "document_records",
169                traversal::FieldType::array_of_records(
170                    stringify!(SVGDocumentRecord),
171                    self.document_records(),
172                    self.offset_data(),
173                ),
174            )),
175            _ => None,
176        }
177    }
178}
179
180#[cfg(feature = "experimental_traverse")]
181#[allow(clippy::needless_lifetimes)]
182impl<'a> std::fmt::Debug for SVGDocumentList<'a> {
183    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
184        (self as &dyn SomeTable<'a>).fmt(f)
185    }
186}
187
188/// [SVGDocumentRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/svg)
189#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
190#[repr(C)]
191#[repr(packed)]
192pub struct SVGDocumentRecord {
193    /// The first glyph ID for the range covered by this record.
194    pub start_glyph_id: BigEndian<GlyphId16>,
195    /// The last glyph ID for the range covered by this record.
196    pub end_glyph_id: BigEndian<GlyphId16>,
197    /// Offset from the beginning of the SVGDocumentList to an SVG
198    /// document. Must be non-zero.
199    pub svg_doc_offset: BigEndian<u32>,
200    /// Length of the SVG document data. Must be non-zero.
201    pub svg_doc_length: BigEndian<u32>,
202}
203
204impl SVGDocumentRecord {
205    /// The first glyph ID for the range covered by this record.
206    pub fn start_glyph_id(&self) -> GlyphId16 {
207        self.start_glyph_id.get()
208    }
209
210    /// The last glyph ID for the range covered by this record.
211    pub fn end_glyph_id(&self) -> GlyphId16 {
212        self.end_glyph_id.get()
213    }
214
215    /// Offset from the beginning of the SVGDocumentList to an SVG
216    /// document. Must be non-zero.
217    pub fn svg_doc_offset(&self) -> u32 {
218        self.svg_doc_offset.get()
219    }
220
221    /// Length of the SVG document data. Must be non-zero.
222    pub fn svg_doc_length(&self) -> u32 {
223        self.svg_doc_length.get()
224    }
225}
226
227impl FixedSize for SVGDocumentRecord {
228    const RAW_BYTE_LEN: usize =
229        GlyphId16::RAW_BYTE_LEN + GlyphId16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
230}
231
232#[cfg(feature = "experimental_traverse")]
233impl<'a> SomeRecord<'a> for SVGDocumentRecord {
234    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
235        RecordResolver {
236            name: "SVGDocumentRecord",
237            get_field: Box::new(move |idx, _data| match idx {
238                0usize => Some(Field::new("start_glyph_id", self.start_glyph_id())),
239                1usize => Some(Field::new("end_glyph_id", self.end_glyph_id())),
240                2usize => Some(Field::new("svg_doc_offset", self.svg_doc_offset())),
241                3usize => Some(Field::new("svg_doc_length", self.svg_doc_length())),
242                _ => None,
243            }),
244            data,
245        }
246    }
247}