/src/fontations/write-fonts/generated/generated_name.rs
Line | Count | Source |
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)] |
6 | | use crate::codegen_prelude::*; |
7 | | |
8 | | /// [Naming table version 1](https://docs.microsoft.com/en-us/typography/opentype/spec/name#naming-table-version-1) |
9 | | #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10 | | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
11 | | pub struct Name { |
12 | | /// The name records where count is the number of records. |
13 | | pub name_record: Vec<NameRecord>, |
14 | | /// The language-tag records where langTagCount is the number of records. |
15 | | pub lang_tag_record: Option<Vec<LangTagRecord>>, |
16 | | } |
17 | | |
18 | | impl Name { |
19 | | /// Construct a new `Name` |
20 | 0 | pub fn new(name_record: Vec<NameRecord>) -> Self { |
21 | 0 | Self { |
22 | 0 | name_record, |
23 | 0 | ..Default::default() |
24 | 0 | } |
25 | 0 | } |
26 | | } |
27 | | |
28 | | impl FontWrite for Name { |
29 | | #[allow(clippy::unnecessary_cast)] |
30 | 0 | fn write_into(&self, writer: &mut TableWriter) { |
31 | 0 | let version = self.compute_version() as u16; |
32 | 0 | version.write_into(writer); |
33 | 0 | (u16::try_from(array_len(&self.name_record)).unwrap()).write_into(writer); |
34 | 0 | (self.compute_storage_offset() as u16).write_into(writer); |
35 | 0 | writer.adjust_offsets(self.compute_storage_offset() as u32, |writer| { |
36 | 0 | self.name_record.write_into(writer); |
37 | 0 | }); |
38 | 0 | version |
39 | 0 | .compatible(1u16) |
40 | 0 | .then(|| (u16::try_from(array_len(&self.lang_tag_record)).unwrap()).write_into(writer)); |
41 | 0 | writer.adjust_offsets(self.compute_storage_offset() as u32, |writer| { |
42 | 0 | version.compatible(1u16).then(|| { |
43 | 0 | self.lang_tag_record |
44 | 0 | .as_ref() |
45 | 0 | .expect("missing conditional field should have failed validation") |
46 | 0 | .write_into(writer) |
47 | 0 | }); |
48 | 0 | }); |
49 | 0 | } |
50 | 0 | fn table_type(&self) -> TableType { |
51 | 0 | TableType::TopLevel(Name::TAG) |
52 | 0 | } |
53 | | } |
54 | | |
55 | | impl Validate for Name { |
56 | 0 | fn validate_impl(&self, ctx: &mut ValidationCtx) { |
57 | 0 | ctx.in_table("Name", |ctx| { |
58 | 0 | let version: u16 = self.compute_version(); |
59 | 0 | ctx.in_field("name_record", |ctx| { |
60 | 0 | if self.name_record.len() > to_usize(u16::MAX) { |
61 | 0 | ctx.report("array exceeds max length"); |
62 | 0 | } |
63 | 0 | self.check_sorted_and_unique_name_records(ctx); |
64 | 0 | }); |
65 | 0 | ctx.in_field("lang_tag_record", |ctx| { |
66 | 0 | if version.compatible(1u16) && self.lang_tag_record.is_none() { |
67 | 0 | ctx.report(format!("field must be present for version {version}")); |
68 | 0 | } |
69 | 0 | if self.lang_tag_record.is_some() |
70 | 0 | && self.lang_tag_record.as_ref().unwrap().len() > to_usize(u16::MAX) |
71 | 0 | { |
72 | 0 | ctx.report("array exceeds max length"); |
73 | 0 | } |
74 | 0 | self.lang_tag_record.validate_impl(ctx); |
75 | 0 | }); |
76 | 0 | }) |
77 | 0 | } |
78 | | } |
79 | | |
80 | | impl TopLevelTable for Name { |
81 | | const TAG: Tag = Tag::new(b"name"); |
82 | | } |
83 | | |
84 | | impl<'a> FromObjRef<read_fonts::tables::name::Name<'a>> for Name { |
85 | 0 | fn from_obj_ref(obj: &read_fonts::tables::name::Name<'a>, _: FontData) -> Self { |
86 | 0 | let offset_data = obj.string_data(); |
87 | 0 | Name { |
88 | 0 | name_record: obj.name_record().to_owned_obj(offset_data), |
89 | 0 | lang_tag_record: obj.lang_tag_record().to_owned_obj(offset_data), |
90 | 0 | } |
91 | 0 | } |
92 | | } |
93 | | |
94 | | #[allow(clippy::needless_lifetimes)] |
95 | | impl<'a> FromTableRef<read_fonts::tables::name::Name<'a>> for Name {} |
96 | | |
97 | | impl ReadArgs for Name { |
98 | | type Args = (); |
99 | | } |
100 | | |
101 | | impl<'a> FontRead<'a> for Name { |
102 | 0 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
103 | 0 | <read_fonts::tables::name::Name as FontRead>::read(data).map(|x| x.to_owned_table()) |
104 | 0 | } |
105 | | } |
106 | | |
107 | | /// Part of [Name] |
108 | | #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
109 | | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
110 | | pub struct LangTagRecord { |
111 | | /// Language-tag string offset from start of storage area (in |
112 | | /// bytes). |
113 | | pub lang_tag: OffsetMarker<String>, |
114 | | } |
115 | | |
116 | | impl LangTagRecord { |
117 | | /// Construct a new `LangTagRecord` |
118 | | #[allow(clippy::useless_conversion)] |
119 | 0 | pub fn new(lang_tag: OffsetMarker<String>) -> Self { |
120 | 0 | Self { |
121 | 0 | lang_tag: lang_tag.into(), |
122 | 0 | } |
123 | 0 | } |
124 | | } |
125 | | |
126 | | impl FontWrite for LangTagRecord { |
127 | | #[allow(clippy::unnecessary_cast)] |
128 | 0 | fn write_into(&self, writer: &mut TableWriter) { |
129 | 0 | (self.compile_name_string()).write_into(writer); |
130 | 0 | } |
131 | 0 | fn table_type(&self) -> TableType { |
132 | 0 | TableType::Named("LangTagRecord") |
133 | 0 | } |
134 | | } |
135 | | |
136 | | impl Validate for LangTagRecord { |
137 | 0 | fn validate_impl(&self, _ctx: &mut ValidationCtx) {} |
138 | | } |
139 | | |
140 | | impl FromObjRef<read_fonts::tables::name::LangTagRecord> for LangTagRecord { |
141 | 0 | fn from_obj_ref(obj: &read_fonts::tables::name::LangTagRecord, offset_data: FontData) -> Self { |
142 | 0 | LangTagRecord { |
143 | 0 | lang_tag: obj.lang_tag(offset_data).to_owned_table(), |
144 | 0 | } |
145 | 0 | } |
146 | | } |
147 | | |
148 | | ///[Name Records](https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records) |
149 | | #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
150 | | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
151 | | pub struct NameRecord { |
152 | | /// Platform ID. |
153 | | pub platform_id: u16, |
154 | | /// Platform-specific encoding ID. |
155 | | pub encoding_id: u16, |
156 | | /// Language ID. |
157 | | pub language_id: u16, |
158 | | /// Name ID. |
159 | | pub name_id: NameId, |
160 | | /// String offset from start of storage area (in bytes). |
161 | | pub string: OffsetMarker<String>, |
162 | | } |
163 | | |
164 | | impl NameRecord { |
165 | | /// Construct a new `NameRecord` |
166 | | #[allow(clippy::useless_conversion)] |
167 | 0 | pub fn new( |
168 | 0 | platform_id: u16, |
169 | 0 | encoding_id: u16, |
170 | 0 | language_id: u16, |
171 | 0 | name_id: NameId, |
172 | 0 | string: OffsetMarker<String>, |
173 | 0 | ) -> Self { |
174 | 0 | Self { |
175 | 0 | platform_id, |
176 | 0 | encoding_id, |
177 | 0 | language_id, |
178 | 0 | name_id, |
179 | 0 | string: string.into(), |
180 | 0 | } |
181 | 0 | } |
182 | | } |
183 | | |
184 | | impl FontWrite for NameRecord { |
185 | | #[allow(clippy::unnecessary_cast)] |
186 | 0 | fn write_into(&self, writer: &mut TableWriter) { |
187 | 0 | self.platform_id.write_into(writer); |
188 | 0 | self.encoding_id.write_into(writer); |
189 | 0 | self.language_id.write_into(writer); |
190 | 0 | self.name_id.write_into(writer); |
191 | 0 | (self.compile_name_string()).write_into(writer); |
192 | 0 | } |
193 | 0 | fn table_type(&self) -> TableType { |
194 | 0 | TableType::Named("NameRecord") |
195 | 0 | } |
196 | | } |
197 | | |
198 | | impl Validate for NameRecord { |
199 | 0 | fn validate_impl(&self, ctx: &mut ValidationCtx) { |
200 | 0 | ctx.in_table("NameRecord", |ctx| { |
201 | 0 | ctx.in_field("string", |ctx| { |
202 | 0 | self.validate_string_data(ctx); |
203 | 0 | }); |
204 | 0 | }) |
205 | 0 | } |
206 | | } |
207 | | |
208 | | impl FromObjRef<read_fonts::tables::name::NameRecord> for NameRecord { |
209 | 0 | fn from_obj_ref(obj: &read_fonts::tables::name::NameRecord, offset_data: FontData) -> Self { |
210 | 0 | NameRecord { |
211 | 0 | platform_id: obj.platform_id(), |
212 | 0 | encoding_id: obj.encoding_id(), |
213 | 0 | language_id: obj.language_id(), |
214 | 0 | name_id: obj.name_id(), |
215 | 0 | string: obj.string(offset_data).to_owned_table(), |
216 | 0 | } |
217 | 0 | } |
218 | | } |