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