Debian Codegraph @

subprojects/fontconfig/fc-fontations/instance_enumerate.rs

raw
1 /*
2 * fontconfig/fc-fontations/instance_enumerate.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 crate::InstanceMode::{self, Named};
26 use read_fonts::{
27 FileRef::{self, Collection, Font},
28 FontRef,
29 };
30 use skrifa::MetadataProvider;
31
32 pub fn fonts_and_indices(
33 file_ref: Option<FileRef>,
34 ) -> impl Iterator<Item = (FontRef<'_>, Option<i32>)> {
35 let (iter_one, iter_two) = match file_ref {
36 Some(Font(font)) => (Some((Ok(font.clone()), None)), None),
37 Some(Collection(collection)) => (
38 None,
39 Some(
40 collection
41 .iter()
42 .enumerate()
43 .map(|entry| (entry.1, Some(entry.0 as i32))),
44 ),
45 ),
46 None => (None, None),
47 };
48 iter_two
49 .into_iter()
50 .flatten()
51 .chain(iter_one)
52 .filter_map(|(font_result, index)| {
53 if let Ok(font) = font_result {
54 return Some((font, index));
55 }
56 None
57 })
58 }
59
60 // Produces an iterator over the named instances of the font.
61 #[allow(unused)]
62 pub fn enumerate_named_instances<'a>(f: &'a FontRef) -> impl Iterator<Item = InstanceMode> + 'a {
63 let default_coords = f.axes().iter().map(|axis| axis.default_value());
64 f.named_instances()
65 .iter()
66 .enumerate()
67 .filter_map(move |(i, instance)| {
68 let user_coords = instance.user_coords();
69 if user_coords.eq(default_coords.clone()) {
70 None
71 } else {
72 Some(Named(i as i32))
73 }
74 })
75 }
76
77 /// Used for producing the expected set of patterns out of variable fonts.
78 /// Produces an iterator over
79 /// * the default instance,
80 /// * the named instances, and the
81 /// * variable instance.
82 #[allow(unused)]
83 pub fn all_instances<'a>(f: &'a FontRef) -> Box<dyn Iterator<Item = InstanceMode> + 'a> {
84 if f.axes().is_empty() {
85 Box::new(std::iter::once(InstanceMode::Default))
86 } else {
87 Box::new(
88 std::iter::once(InstanceMode::Default)
89 .chain(enumerate_named_instances(f))
90 .chain(std::iter::once(InstanceMode::Variable)),
91 )
92 }
93 }
94
95 #[cfg(test)]
96 mod test {
97 use crate::{
98 instance_enumerate::{all_instances, fonts_and_indices},
99 InstanceMode,
100 };
101
102 use read_fonts::FileRef;
103 use std::path::PathBuf;
104
105 fn testfontdir() -> PathBuf {
106 let default_build_dir = std::env::current_dir().unwrap().join("build");
107 let build_dir = std::env::var("builddir").unwrap_or_else(|_| {
108 println!("builddir env var not set, using current directory + \"build/\"");
109 default_build_dir.to_string_lossy().to_string()
110 });
111 PathBuf::from(build_dir)
112 .join("testfonts")
113 .join("roboto-flex-fonts")
114 .join("fonts")
115 .join("variable")
116 }
117
118 fn variable_collection_testfont() -> PathBuf {
119 testfontdir().join(
120 "RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf",
121 )
122 }
123
124 const EXPECTED_INSTANCES: [InstanceMode; 21] = [
125 InstanceMode::Default,
126 InstanceMode::Named(0),
127 InstanceMode::Named(1),
128 InstanceMode::Named(2),
129 InstanceMode::Named(4),
130 InstanceMode::Named(5),
131 InstanceMode::Named(6),
132 InstanceMode::Named(7),
133 InstanceMode::Named(8),
134 InstanceMode::Named(9),
135 InstanceMode::Named(10),
136 InstanceMode::Named(11),
137 InstanceMode::Named(12),
138 InstanceMode::Named(13),
139 InstanceMode::Named(14),
140 InstanceMode::Named(15),
141 InstanceMode::Named(16),
142 InstanceMode::Named(17),
143 InstanceMode::Named(18),
144 InstanceMode::Named(19),
145 InstanceMode::Variable,
146 ];
147
148 #[test]
149 fn test_instance_iterator() {
150 let bytes = std::fs::read(variable_collection_testfont())
151 .ok()
152 .unwrap_or_default();
153 let fileref = FileRef::new(&bytes).ok();
154 let fonts = fonts_and_indices(fileref);
155 for (font, _ttc_index) in fonts {
156 all_instances(&font)
157 .zip(EXPECTED_INSTANCES.iter())
158 .for_each(|(instance, expected_instance)| {
159 assert_eq!(instance, *expected_instance);
160 })
161 }
162 }
163 }