Debian Codegraph @

subprojects/fontconfig/fc-fontations/name_records.rs

raw
1 /*
2 * fontconfig/fc-fontations/name_records.rs
3 *
4 * Copyright 2025 Google LLC.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25 use font_types::NameId;
26 use read_fonts::tables::cmap::PlatformId;
27 use read_fonts::tables::name::{Name, NameRecord};
28
29 // Compare platform_order[] in fcfreetype.c.
30 // https://learn.microsoft.com/en-us/typography/opentype/spec/name#platform-encoding-and-language-ids
31 const PLATFORM_ORDER: [PlatformId; 4] = [
32 PlatformId::Windows,
33 PlatformId::Unicode,
34 PlatformId::Macintosh,
35 PlatformId::ISO, // deprecated.
36 ];
37
38 // Compare name_order[] in fcfreetype.c.
39 const NAME_ID_ORDER: [NameId; 10] = [
40 NameId::WWS_FAMILY_NAME,
41 NameId::TYPOGRAPHIC_FAMILY_NAME,
42 NameId::FAMILY_NAME,
43 NameId::COMPATIBLE_FULL_NAME,
44 NameId::FULL_NAME,
45 NameId::WWS_SUBFAMILY_NAME,
46 NameId::TYPOGRAPHIC_SUBFAMILY_NAME,
47 NameId::SUBFAMILY_NAME,
48 NameId::TRADEMARK,
49 NameId::MANUFACTURER,
50 ];
51
52 pub struct FcSortedNameRecords<'a> {
53 name: &'a Name<'a>,
54 sorted_record_indices: std::vec::IntoIter<u16>,
55 }
56
57 impl<'a> FcSortedNameRecords<'a> {
58 pub fn new(name: &'a Name) -> Self {
59 let mut sorted_name_id_indices: Vec<u16> = (0..name.name_record().len() as u16).collect();
60 Self::sort_name_records(name, &mut sorted_name_id_indices);
61 Self {
62 name,
63 sorted_record_indices: sorted_name_id_indices.into_iter(),
64 }
65 }
66
67 // In instances of this comparison function, return Greater for an id a that
68 // is higher priorty than id b, Less for one that is lower priority.
69 fn cmp_id_ordered<T>(priorities: &[T], a: &T, b: &T) -> std::cmp::Ordering
70 where
71 T: std::cmp::Ord,
72 {
73 match (
74 priorities.iter().position(|x| *x == *a),
75 priorities.iter().position(|x| *x == *b),
76 ) {
77 (Some(pos_a), Some(pos_b)) => pos_a.cmp(&pos_b).reverse(),
78 (Some(_), None) => std::cmp::Ordering::Greater,
79 (None, Some(_)) => std::cmp::Ordering::Less,
80 (None, None) => a.cmp(b).reverse(),
81 }
82 }
83
84 fn language_cmp_english_first(a: (u16, u16), b: (u16, u16)) -> std::cmp::Ordering {
85 const MS_ENGLISH_US: (u16, u16) = (0x409, 3);
86 const MAC_ENGLISH: (u16, u16) = (0, 1);
87
88 match (a, b) {
89 (MS_ENGLISH_US, MS_ENGLISH_US) | (MAC_ENGLISH, MAC_ENGLISH) => {
90 std::cmp::Ordering::Equal
91 }
92 (MS_ENGLISH_US, _) | (MAC_ENGLISH, _) => std::cmp::Ordering::Greater,
93 (_, MS_ENGLISH_US) | (_, MAC_ENGLISH) => std::cmp::Ordering::Less,
94 _ => a.cmp(&b).reverse(),
95 }
96 }
97
98 fn sort_name_records(name: &'a Name, indices: &mut [u16]) {
99 indices.sort_by(|a, b| {
100 let name_record_a = name.name_record()[*a as usize];
101 let name_record_b = name.name_record()[*b as usize];
102
103 Self::cmp_id_ordered(
104 &PLATFORM_ORDER,
105 &PlatformId::new(name_record_a.platform_id()),
106 &PlatformId::new(name_record_b.platform_id()),
107 )
108 .then(Self::cmp_id_ordered(
109 &NAME_ID_ORDER,
110 &name_record_a.name_id(),
111 &name_record_b.name_id(),
112 ))
113 .then(
114 name_record_a
115 .encoding_id()
116 .cmp(&name_record_b.encoding_id()),
117 )
118 .then(Self::language_cmp_english_first(
119 (name_record_a.language_id(), name_record_a.platform_id()),
120 (name_record_b.language_id(), name_record_b.platform_id()),
121 ))
122 });
123 // Return in descending order, from most important to least important.
124 indices.reverse();
125 }
126 }
127
128 impl Iterator for FcSortedNameRecords<'_> {
129 type Item = NameRecord;
130
131 fn next(&mut self) -> Option<Self::Item> {
132 let record_index = self.sorted_record_indices.next()?;
133 Some(self.name.name_record()[record_index as usize])
134 }
135 }
136
137 #[cfg(test)]
138 mod test {
139 use crate::name_records::FcSortedNameRecords;
140 use read_fonts::TableProvider;
141 use std::{env, fs, path::PathBuf};
142
143 fn get_test_font_bytes() -> Option<Vec<u8>> {
144 let base_path_str = env::var("builddir").unwrap_or(String::from("build/"));
145 let file_path = PathBuf::from(base_path_str)
146 .join("testfonts/roboto-flex-fonts/fonts/variable/RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf");
147
148 let file_contents: Vec<u8> = fs::read(&file_path).ok()?;
149 Some(file_contents)
150 }
151
152 #[test]
153 fn test_sort_name_records() {
154 let font_bytes = get_test_font_bytes().expect("Could not read test font file.");
155 let font = read_fonts::FontRef::new(font_bytes.as_slice()).unwrap();
156 let name_table = font.name().unwrap();
157 let sorted_records = FcSortedNameRecords::new(&name_table);
158 let sorted_platform_ids = sorted_records
159 .map(|entry| {
160 (
161 entry.platform_id(),
162 entry.name_id().to_u16(),
163 entry.encoding_id(),
164 entry.language_id(),
165 )
166 })
167 .collect::<Vec<_>>();
168 assert_eq!(
169 sorted_platform_ids,
170 [
171 (3, 1, 1, 1033),
172 (3, 4, 1, 1033),
173 (3, 2, 1, 1033),
174 (3, 7, 1, 1033),
175 (3, 8, 1, 1033),
176 (3, 0, 1, 1033),
177 (3, 3, 1, 1033),
178 (3, 5, 1, 1033),
179 (3, 6, 1, 1033),
180 (3, 9, 1, 1033),
181 (3, 11, 1, 1033),
182 (3, 12, 1, 1033),
183 (3, 13, 1, 1033),
184 (3, 14, 1, 1033),
185 (3, 25, 1, 1033),
186 (3, 256, 1, 1033),
187 (3, 257, 1, 1033),
188 (3, 258, 1, 1033),
189 (3, 259, 1, 1033),
190 (3, 260, 1, 1033),
191 (3, 261, 1, 1033),
192 (3, 262, 1, 1033),
193 (3, 263, 1, 1033),
194 (3, 264, 1, 1033),
195 (3, 265, 1, 1033),
196 (3, 266, 1, 1033),
197 (3, 267, 1, 1033),
198 (3, 268, 1, 1033),
199 (3, 269, 1, 1033),
200 (3, 270, 1, 1033),
201 (3, 271, 1, 1033),
202 (3, 272, 1, 1033),
203 (3, 273, 1, 1033),
204 (3, 274, 1, 1033),
205 (3, 275, 1, 1033),
206 (3, 276, 1, 1033),
207 (3, 277, 1, 1033),
208 (3, 278, 1, 1033),
209 (3, 279, 1, 1033),
210 (3, 280, 1, 1033),
211 (3, 281, 1, 1033),
212 (3, 282, 1, 1033),
213 (3, 283, 1, 1033),
214 (3, 284, 1, 1033),
215 (3, 285, 1, 1033),
216 (3, 286, 1, 1033),
217 (3, 287, 1, 1033),
218 (3, 288, 1, 1033),
219 (3, 289, 1, 1033),
220 (3, 290, 1, 1033),
221 (3, 291, 1, 1033),
222 (3, 292, 1, 1033),
223 (3, 293, 1, 1033),
224 (3, 294, 1, 1033),
225 (3, 295, 1, 1033),
226 (3, 296, 1, 1033),
227 (3, 297, 1, 1033),
228 (3, 298, 1, 1033),
229 (3, 299, 1, 1033),
230 (3, 300, 1, 1033),
231 (3, 301, 1, 1033),
232 (3, 302, 1, 1033),
233 (3, 303, 1, 1033),
234 (3, 304, 1, 1033),
235 (3, 305, 1, 1033),
236 (3, 306, 1, 1033),
237 (3, 307, 1, 1033),
238 (3, 308, 1, 1033),
239 (3, 309, 1, 1033),
240 (3, 310, 1, 1033),
241 (3, 311, 1, 1033),
242 (3, 312, 1, 1033),
243 (3, 313, 1, 1033),
244 (3, 314, 1, 1033),
245 (3, 315, 1, 1033),
246 (3, 316, 1, 1033),
247 (3, 317, 1, 1033),
248 (3, 318, 1, 1033),
249 (3, 319, 1, 1033),
250 (3, 320, 1, 1033),
251 (3, 321, 1, 1033),
252 (3, 322, 1, 1033),
253 (3, 323, 1, 1033),
254 (3, 324, 1, 1033),
255 (3, 325, 1, 1033),
256 (3, 326, 1, 1033),
257 (3, 327, 1, 1033),
258 (3, 328, 1, 1033),
259 (3, 329, 1, 1033),
260 (3, 330, 1, 1033),
261 (3, 331, 1, 1033),
262 (3, 332, 1, 1033),
263 (3, 333, 1, 1033),
264 (3, 334, 1, 1033),
265 (3, 335, 1, 1033),
266 (3, 336, 1, 1033),
267 (3, 337, 1, 1033),
268 (3, 338, 1, 1033),
269 (3, 339, 1, 1033),
270 (3, 340, 1, 1033),
271 (3, 341, 1, 1033),
272 (3, 342, 1, 1033),
273 (3, 343, 1, 1033),
274 (3, 344, 1, 1033),
275 (3, 345, 1, 1033),
276 (3, 346, 1, 1033),
277 (3, 347, 1, 1033),
278 (3, 348, 1, 1033),
279 (3, 349, 1, 1033),
280 (1, 25, 0, 0),
281 (1, 256, 0, 0),
282 (1, 257, 0, 0),
283 (1, 258, 0, 0),
284 (1, 259, 0, 0),
285 (1, 260, 0, 0),
286 (1, 261, 0, 0),
287 (1, 262, 0, 0),
288 (1, 263, 0, 0),
289 (1, 264, 0, 0),
290 (1, 265, 0, 0),
291 (1, 266, 0, 0),
292 (1, 267, 0, 0),
293 (1, 268, 0, 0),
294 (1, 269, 0, 0),
295 (1, 270, 0, 0),
296 (1, 271, 0, 0),
297 (1, 272, 0, 0),
298 (1, 273, 0, 0),
299 (1, 274, 0, 0),
300 (1, 275, 0, 0),
301 (1, 276, 0, 0),
302 (1, 277, 0, 0),
303 (1, 278, 0, 0),
304 (1, 279, 0, 0),
305 (1, 280, 0, 0),
306 (1, 281, 0, 0),
307 (1, 282, 0, 0),
308 (1, 283, 0, 0),
309 (1, 284, 0, 0),
310 (1, 285, 0, 0),
311 (1, 286, 0, 0),
312 (1, 287, 0, 0),
313 (1, 288, 0, 0),
314 (1, 289, 0, 0),
315 (1, 290, 0, 0),
316 (1, 291, 0, 0),
317 (1, 292, 0, 0),
318 (1, 293, 0, 0),
319 (1, 294, 0, 0),
320 (1, 295, 0, 0),
321 (1, 296, 0, 0),
322 (1, 297, 0, 0),
323 (1, 298, 0, 0),
324 (1, 299, 0, 0),
325 (1, 300, 0, 0),
326 (1, 301, 0, 0),
327 (1, 302, 0, 0),
328 (1, 303, 0, 0),
329 (1, 304, 0, 0),
330 (1, 305, 0, 0),
331 (1, 306, 0, 0),
332 (1, 307, 0, 0),
333 (1, 308, 0, 0),
334 (1, 309, 0, 0),
335 (1, 310, 0, 0),
336 (1, 311, 0, 0),
337 (1, 312, 0, 0),
338 (1, 313, 0, 0),
339 (1, 314, 0, 0),
340 (1, 315, 0, 0),
341 (1, 316, 0, 0),
342 (1, 317, 0, 0),
343 (1, 318, 0, 0),
344 (1, 319, 0, 0),
345 (1, 320, 0, 0),
346 (1, 321, 0, 0),
347 (1, 322, 0, 0),
348 (1, 323, 0, 0),
349 (1, 324, 0, 0),
350 (1, 325, 0, 0),
351 (1, 326, 0, 0),
352 (1, 327, 0, 0),
353 (1, 328, 0, 0),
354 (1, 329, 0, 0),
355 (1, 330, 0, 0),
356 (1, 331, 0, 0),
357 (1, 332, 0, 0),
358 (1, 333, 0, 0),
359 (1, 334, 0, 0),
360 (1, 335, 0, 0),
361 (1, 336, 0, 0),
362 (1, 337, 0, 0),
363 (1, 338, 0, 0),
364 (1, 339, 0, 0),
365 (1, 340, 0, 0),
366 (1, 341, 0, 0),
367 (1, 342, 0, 0),
368 (1, 343, 0, 0),
369 (1, 344, 0, 0),
370 (1, 345, 0, 0),
371 (1, 346, 0, 0),
372 (1, 347, 0, 0),
373 (1, 348, 0, 0),
374 (1, 349, 0, 0)
375 ]
376 );
377 }
378 }