1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#![deny(intra_doc_link_resolution_failure)]
use std::borrow::Borrow;
use std::collections::BTreeSet;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::glyph::{Glyph, GlyphName};
use crate::layer::Layer;
use crate::Error;
static LAYER_CONTENTS_FILE: &str = "layercontents.plist";
static METAINFO_FILE: &str = "metainfo.plist";
static FONTINFO_FILE: &str = "fontinfo.plist";
static DEFAULT_LAYER_NAME: &str = "public.default";
static DEFAULT_GLYPHS_DIRNAME: &str = "glyphs";
#[allow(dead_code)]
pub struct Ufo {
pub meta: MetaInfo,
pub font_info: Option<FontInfo>,
pub layers: Vec<LayerInfo>,
glyph_names: BTreeSet<GlyphName>,
}
pub struct LayerInfo {
pub name: String,
pub path: PathBuf,
pub layer: Layer,
}
#[derive(Debug, Clone, Copy, Serialize_repr, Deserialize_repr, PartialEq)]
#[repr(u8)]
pub enum FormatVersion {
V1 = 1,
V2 = 2,
V3 = 3,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MetaInfo {
pub creator: String,
pub format_version: FormatVersion,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FontInfo {
pub family_name: Option<String>,
pub style_name: Option<String>,
pub style_map_family_name: Option<String>,
pub style_map_style_name: Option<String>,
pub version_major: Option<u32>,
pub version_minor: Option<u32>,
pub year: Option<u32>,
pub copyright: Option<String>,
pub trademark: Option<String>,
pub units_per_em: Option<f64>,
pub descender: Option<f64>,
pub x_height: Option<f64>,
pub cap_height: Option<f64>,
pub ascender: Option<f64>,
pub italic_angle: Option<f64>,
pub note: Option<String>,
}
impl Ufo {
pub fn new(meta: MetaInfo) -> Self {
let main_layer = LayerInfo {
name: DEFAULT_LAYER_NAME.into(),
path: PathBuf::from(DEFAULT_GLYPHS_DIRNAME),
layer: Layer::default(),
};
Ufo { meta, font_info: None, layers: vec![main_layer], glyph_names: BTreeSet::new() }
}
pub fn load<P: AsRef<Path>>(path: P) -> Result<Ufo, Error> {
let path = path.as_ref();
return load_impl(path);
fn load_impl(path: &Path) -> Result<Ufo, Error> {
let meta_path = path.join(METAINFO_FILE);
let meta: MetaInfo = plist::from_file(meta_path)?;
let font_path = path.join(FONTINFO_FILE);
let font_info = if font_path.exists() {
let font_info = plist::from_file(font_path)?;
Some(font_info)
} else {
None
};
let mut contents = match meta.format_version {
FormatVersion::V3 => {
let contents_path = path.join(LAYER_CONTENTS_FILE);
let contents: Vec<(String, PathBuf)> = plist::from_file(contents_path)?;
contents
}
_older => vec![(DEFAULT_LAYER_NAME.into(), DEFAULT_GLYPHS_DIRNAME.into())],
};
let layers: Result<Vec<LayerInfo>, Error> = contents
.drain(..)
.map(|(name, p)| {
let layer_path = path.join(&p);
let layer = Layer::load(layer_path)?;
Ok(LayerInfo { name, path: p, layer })
})
.collect();
let layers = layers?;
let glyph_names = layers
.iter()
.flat_map(|info| info.layer.iter_contents().map(|g| g.name.clone()))
.collect();
Ok(Ufo { layers, meta, font_info, glyph_names })
}
}
pub fn find_layer<P>(&self, mut predicate: P) -> Option<&Layer>
where
P: FnMut(&LayerInfo) -> bool,
{
self.layers.iter().find(|l| predicate(l)).map(|l| &l.layer)
}
pub fn find_layer_mut<P>(&mut self, mut predicate: P) -> Option<&mut Layer>
where
P: FnMut(&LayerInfo) -> bool,
{
self.layers.iter_mut().find(|l| predicate(l)).map(|l| &mut l.layer)
}
pub fn get_default_layer(&self) -> Option<&Layer> {
self.layers
.iter()
.find(|l| l.path.file_name() == Some(OsStr::new(DEFAULT_GLYPHS_DIRNAME)))
.map(|l| &l.layer)
}
pub fn get_default_layer_mut(&mut self) -> Option<&mut Layer> {
self.layers
.iter_mut()
.find(|l| l.path.file_name() == Some(OsStr::new(DEFAULT_GLYPHS_DIRNAME)))
.map(|l| &mut l.layer)
}
pub fn iter_layers(&self) -> impl Iterator<Item = &LayerInfo> {
self.layers.iter()
}
pub fn iter_names<'a>(&'a self) -> impl Iterator<Item = GlyphName> + 'a {
self.glyph_names.iter().cloned()
}
pub fn get_glyph<K>(&self, key: &K) -> Option<&Arc<Glyph>>
where
GlyphName: Borrow<K>,
K: Ord + ?Sized,
{
self.get_default_layer().and_then(|l| l.get_glyph(key))
}
pub fn get_glyph_mut<K>(&mut self, key: &K) -> Option<&mut Arc<Glyph>>
where
GlyphName: Borrow<K>,
K: Ord + ?Sized,
{
self.get_default_layer_mut().and_then(|l| l.get_glyph_mut(key))
}
pub fn glyph_count(&self) -> usize {
self.glyph_names.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn loading() {
let path = "testdata/mutatorSans/MutatorSansLightWide.ufo";
let font_obj = Ufo::load(path).unwrap();
assert_eq!(font_obj.iter_layers().count(), 2);
font_obj
.find_layer(|l| l.path.to_str() == Some("glyphs.background"))
.expect("missing layer");
}
#[test]
fn metainfo() {
let path = "testdata/mutatorSans/MutatorSansLightWide.ufo/metainfo.plist";
let meta: MetaInfo = plist::from_file(path).expect("failed to load metainfo");
assert_eq!(meta.creator, "org.robofab.ufoLib");
}
#[test]
fn fontinfo() {
let path = "testdata/mutatorSans/MutatorSansLightWide.ufo/fontinfo.plist";
let font_info: FontInfo = plist::from_file(path).expect("failed to load fontinfo");
assert_eq!(font_info.family_name, Some("MutatorMathTest".to_string()));
assert_eq!(font_info.trademark, None);
}
}