-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupconversion.rs
507 lines (463 loc) · 19.1 KB
/
upconversion.rs
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::Path;
use crate::fontinfo::FontInfo;
use crate::groups::Groups;
use crate::kerning::Kerning;
use crate::names::NameList;
use crate::shared_types::IntegerOrFloat;
use crate::Error;
/// Convert kerning groups and pairs from v1 and v2 informal conventions to
/// v3 formal conventions. Converted groups are added (duplicated) rather than
/// replacing the old ones to preserve all data that external entities might
/// rely on. Kerning pairs are updated to reflect the new group names.
///
/// This is an adaptation from the fontTools.ufoLib reference implementation.
/// It will not check if the upgraded groups pass validation.
pub(crate) fn upconvert_kerning(
groups: &Groups,
kerning: &Kerning,
glyph_set: &NameList,
) -> (Groups, Kerning) {
// Gather known kerning groups based on the prefixes. This will catch groups that exist in
// `groups` but are not referenced in `kerning`.
let (mut groups_first, mut groups_second) = find_known_kerning_groups(groups);
// Make lists of groups referenced in kerning pairs, based on their side.
for (first, seconds) in kerning {
if groups.contains_key(first)
&& !glyph_set.contains(first.as_str())
&& !first.starts_with("public.kern1.")
{
groups_first.insert(first.to_string());
}
for second in seconds.keys() {
if groups.contains_key(second)
&& !glyph_set.contains(second.as_str())
&& !second.starts_with("public.kern2.")
{
groups_second.insert(second.to_string());
}
}
}
// Duplicate kerning groups with a new name.
let mut groups_new = groups.clone();
let mut groups_first_old_to_new: HashMap<&String, String> = HashMap::new();
for first in &groups_first {
let first_new = make_unique_group_name(
format!("public.kern1.{}", first.replace("@MMK_L_", "")),
&groups_new,
);
groups_first_old_to_new.insert(first, first_new.to_string());
groups_new.insert(first_new, groups_new.get(first).unwrap().clone());
}
let mut groups_second_old_to_new: HashMap<&String, String> = HashMap::new();
for second in &groups_second {
let second_new = make_unique_group_name(
format!("public.kern2.{}", second.replace("@MMK_R_", "")),
&groups_new,
);
groups_second_old_to_new.insert(second, second_new.to_string());
groups_new.insert(second_new, groups_new.get(second).unwrap().clone());
}
// Update all kerning pairs that have an old kerning group in them with the new name.
let mut kerning_new: Kerning = Kerning::new();
for (first, seconds) in kerning {
let first_new = groups_first_old_to_new.get(first).unwrap_or(first);
let mut seconds_new: BTreeMap<String, f32> = BTreeMap::new();
for (second, value) in seconds {
let second_new = groups_second_old_to_new.get(second).unwrap_or(second);
seconds_new.insert(second_new.to_string(), *value);
}
kerning_new.insert(first_new.to_string(), seconds_new);
}
(groups_new, kerning_new)
}
fn make_unique_group_name(name: String, existing_groups: &Groups) -> String {
if !existing_groups.contains_key(&name) {
return name;
}
let mut counter = 1;
let mut new_name = name.to_string();
while existing_groups.contains_key(&new_name) {
new_name = format!("{}{}", name, counter);
counter += 1;
}
new_name
}
fn find_known_kerning_groups(groups: &Groups) -> (HashSet<String>, HashSet<String>) {
let mut groups_first: HashSet<String> = HashSet::new();
let mut groups_second: HashSet<String> = HashSet::new();
for name in groups.keys() {
if name.starts_with("@MMK_L_") {
groups_first.insert(name.to_string());
} else if name.starts_with("@MMK_R_") {
groups_second.insert(name.to_string());
}
}
(groups_first, groups_second)
}
/// Migrate UFO v1 era feature and PostScript hinting data to the current data model. It re-reads
/// the lib.plist file to filter out the relevant data and then update the passed in lib, features
/// and fontinfo in-place. It tries to follow what [defcon is doing][1].
///
/// [1]: https://github.com/robotools/defcon/blob/76a7ac408e62f68c09eaf24ca6d9ad04523dd19c/Lib/defcon/objects/font.py#L1571-L1629
pub(crate) fn upconvert_ufov1_robofab_data(
lib_path: &Path,
lib: &mut plist::Dictionary,
fontinfo: &mut FontInfo,
) -> Result<Option<String>, Error> {
#[derive(Debug, Deserialize)]
struct LibData {
#[serde(rename = "org.robofab.postScriptHintData")]
ps_hinting_data: Option<PsHintingData>,
#[serde(rename = "org.robofab.opentype.classes")]
feature_classes: Option<String>,
#[serde(rename = "org.robofab.opentype.featureorder")]
feature_order: Option<Vec<String>>,
#[serde(rename = "org.robofab.opentype.features")]
features: Option<HashMap<String, String>>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PsHintingData {
blue_fuzz: Option<IntegerOrFloat>,
blue_scale: Option<f64>,
blue_shift: Option<IntegerOrFloat>,
blue_values: Option<Vec<Vec<IntegerOrFloat>>>,
family_blues: Option<Vec<Vec<IntegerOrFloat>>>,
family_other_blues: Option<Vec<Vec<IntegerOrFloat>>>,
force_bold: Option<bool>,
other_blues: Option<Vec<Vec<IntegerOrFloat>>>,
h_stems: Option<Vec<IntegerOrFloat>>,
v_stems: Option<Vec<IntegerOrFloat>>,
}
// Read lib.plist again because it is easier than pulling out the data manually.
let lib_data: LibData = plist::from_file(lib_path)?;
// Convert features.
let mut features = String::new();
if let Some(feature_classes) = lib_data.feature_classes {
features.push_str(&feature_classes);
}
if let Some(features_split) = lib_data.features {
let order: Vec<String> = if let Some(feature_order) = lib_data.feature_order {
feature_order
} else {
features_split.keys().cloned().collect::<Vec<String>>()
};
features.push('\n');
for key in order {
// Ignore non-existant keys because defcon does it, too.
if let Some(txt) = features_split.get(&key) {
features.push_str(txt);
}
}
}
// Convert PostScript hinting data.
if let Some(ps_hinting_data) = lib_data.ps_hinting_data {
fontinfo.postscript_blue_fuzz = ps_hinting_data.blue_fuzz;
fontinfo.postscript_blue_scale = ps_hinting_data.blue_scale;
fontinfo.postscript_blue_shift = ps_hinting_data.blue_shift;
if let Some(blue_values) = ps_hinting_data.blue_values {
fontinfo.postscript_blue_values = Some(blue_values.into_iter().flatten().collect());
};
if let Some(other_blues) = ps_hinting_data.other_blues {
fontinfo.postscript_other_blues = Some(other_blues.into_iter().flatten().collect());
};
if let Some(family_blues) = ps_hinting_data.family_blues {
fontinfo.postscript_family_blues = Some(family_blues.into_iter().flatten().collect());
};
if let Some(family_other_blues) = ps_hinting_data.family_other_blues {
fontinfo.postscript_family_other_blues =
Some(family_other_blues.into_iter().flatten().collect());
};
fontinfo.postscript_force_bold = ps_hinting_data.force_bold;
fontinfo.postscript_stem_snap_h = ps_hinting_data.h_stems;
fontinfo.postscript_stem_snap_v = ps_hinting_data.v_stems;
fontinfo.validate().map_err(|_| Error::FontInfoUpconversion)?;
}
lib.remove("org.robofab.postScriptHintData");
lib.remove("org.robofab.opentype.classes");
lib.remove("org.robofab.opentype.featureorder");
lib.remove("org.robofab.opentype.features");
if features.is_empty() {
Ok(None)
} else {
Ok(Some(features))
}
}
#[cfg(test)]
mod tests {
extern crate maplit;
use super::*;
use crate::font::{Font, FormatVersion};
use crate::glyph::GlyphName;
use maplit::btreemap;
#[test]
fn test_upconvert_kerning_just_groups() {
let groups: Groups = btreemap! {
"@MMK_L_1".into() => vec!["a".into()],
"@MMK_L_2".into() => vec!["b".into()],
"@MMK_L_3".into() => vec!["c".into()],
"@MMK_R_1".into() => vec!["d".into()],
"@MMK_R_2".into() => vec!["e".into()],
"@MMK_R_3".into() => vec!["f".into()],
"@MMK_l_1".into() => vec!["g".into(), "h".into()],
"@MMK_r_1".into() => vec!["i".into()],
"@MMK_X_1".into() => vec!["j".into()],
"foo".into() => vec![],
};
let kerning: Kerning = Kerning::new();
let glyph_set: NameList = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
.iter()
.map(|s| GlyphName::from(*s))
.collect();
let (groups_new, kerning_new) = upconvert_kerning(&groups, &kerning, &glyph_set);
assert_eq!(
groups_new,
btreemap! {
"@MMK_L_1".into() => vec!["a".into()],
"@MMK_L_2".into() => vec!["b".into()],
"@MMK_L_3".into() => vec!["c".into()],
"@MMK_R_1".into() => vec!["d".into()],
"@MMK_R_2".into() => vec!["e".into()],
"@MMK_R_3".into() => vec!["f".into()],
"@MMK_l_1".into() => vec!["g".into(),"h".into()],
"@MMK_r_1".into() => vec!["i".into()],
"@MMK_X_1".into() => vec!["j".into()],
"foo".into() => vec![],
"public.kern1.1".into() => vec!["a".into()],
"public.kern1.2".into() => vec!["b".into()],
"public.kern1.3".into() => vec!["c".into()],
"public.kern2.1".into() => vec!["d".into()],
"public.kern2.2".into() => vec!["e".into()],
"public.kern2.3".into() => vec!["f".into()],
}
);
assert_eq!(kerning_new, kerning);
}
#[test]
fn test_upconvert_kerning_unknown_prefixes() {
let groups: Groups = btreemap! {
"BGroup".into() => vec!["B".into()],
"CGroup".into() => vec!["C".into()],
"DGroup".into() => vec!["D".into()],
};
let kerning: Kerning = btreemap! {
"A".into() => btreemap!{
"A".into() => 1.0,
"B".into() => 2.0,
"CGroup".into() => 3.0,
"DGroup".into() => 4.0,
},
"BGroup".into() => btreemap!{
"A".into() => 5.0,
"B".into() => 6.0,
"CGroup".into() => 7.0,
"DGroup".into() => 8.0,
},
"CGroup".into() => btreemap!{
"A".into() => 9.0,
"B".into() => 10.0,
"CGroup".into() => 11.0,
"DGroup".into() => 12.0,
},
};
let glyph_set = NameList::default();
let (groups_new, kerning_new) = upconvert_kerning(&groups, &kerning, &glyph_set);
assert_eq!(
groups_new,
btreemap! {
"BGroup".into() => vec!["B".into()],
"CGroup".into() => vec!["C".into()],
"DGroup".into() => vec!["D".into()],
"public.kern1.BGroup".into() => vec!["B".into()],
"public.kern1.CGroup".into() => vec!["C".into()],
"public.kern2.CGroup".into() => vec!["C".into()],
"public.kern2.DGroup".into() => vec!["D".into()],
}
);
assert_eq!(
kerning_new,
btreemap! {
"A".into() => btreemap!{
"A".into() => 1.0,
"B".into() => 2.0,
"public.kern2.CGroup".into() => 3.0,
"public.kern2.DGroup".into() => 4.0,
},
"public.kern1.BGroup".into() => btreemap!{
"A".into() => 5.0,
"B".into() => 6.0,
"public.kern2.CGroup".into() => 7.0,
"public.kern2.DGroup".into() => 8.0,
},
"public.kern1.CGroup".into() => btreemap!{
"A".into() => 9.0,
"B".into() => 10.0,
"public.kern2.CGroup".into() => 11.0,
"public.kern2.DGroup".into() => 12.0,
}
}
);
}
#[test]
fn test_upconvert_kerning_known_prefixes() {
let groups: Groups = btreemap! {
"@MMK_L_BGroup".into() => vec!["B".into()],
"@MMK_L_CGroup".into() => vec!["C".into()],
"@MMK_L_XGroup".into() => vec!["X".into()],
"@MMK_R_CGroup".into() => vec!["C".into()],
"@MMK_R_DGroup".into() => vec!["D".into()],
"@MMK_R_XGroup".into() => vec!["X".into()],
};
let kerning: Kerning = btreemap! {
"A".into() => btreemap!{
"A".into() => 1.0,
"B".into() => 2.0,
"@MMK_R_CGroup".into() => 3.0,
"@MMK_R_DGroup".into() => 4.0,
},
"@MMK_L_BGroup".into() => btreemap!{
"A".into() => 5.0,
"B".into() => 6.0,
"@MMK_R_CGroup".into() => 7.0,
"@MMK_R_DGroup".into() => 8.0,
},
"@MMK_L_CGroup".into() => btreemap!{
"A".into() => 9.0,
"B".into() => 10.0,
"@MMK_R_CGroup".into() => 11.0,
"@MMK_R_DGroup".into() => 12.0,
},
};
let glyph_set = NameList::default();
let (groups_new, kerning_new) = upconvert_kerning(&groups, &kerning, &glyph_set);
assert_eq!(
groups_new,
btreemap! {
"@MMK_L_BGroup".into() => vec!["B".into()],
"@MMK_L_CGroup".into() => vec!["C".into()],
"@MMK_L_XGroup".into() => vec!["X".into()],
"@MMK_R_CGroup".into() => vec!["C".into()],
"@MMK_R_DGroup".into() => vec!["D".into()],
"@MMK_R_XGroup".into() => vec!["X".into()],
"public.kern1.BGroup".into() => vec!["B".into()],
"public.kern1.CGroup".into() => vec!["C".into()],
"public.kern1.XGroup".into() => vec!["X".into()],
"public.kern2.CGroup".into() => vec!["C".into()],
"public.kern2.DGroup".into() => vec!["D".into()],
"public.kern2.XGroup".into() => vec!["X".into()],
}
);
assert_eq!(
kerning_new,
btreemap! {
"A".into() => btreemap!{
"A".into() => 1.0,
"B".into() => 2.0,
"public.kern2.CGroup".into() => 3.0,
"public.kern2.DGroup".into() => 4.0,
},
"public.kern1.BGroup".into() => btreemap!{
"A".into() => 5.0,
"B".into() => 6.0,
"public.kern2.CGroup".into() => 7.0,
"public.kern2.DGroup".into() => 8.0,
},
"public.kern1.CGroup".into() => btreemap!{
"A".into() => 9.0,
"B".into() => 10.0,
"public.kern2.CGroup".into() => 11.0,
"public.kern2.DGroup".into() => 12.0,
}
}
);
}
#[test]
fn test_upconvert_kerning_mixed_prefixes() {
let groups: Groups = btreemap! {
"BGroup".into() => vec!["B".into()],
"@MMK_L_CGroup".into() => vec!["C".into()],
"@MMK_R_CGroup".into() => vec!["C".into()],
"DGroup".into() => vec!["D".into()],
};
let kerning: Kerning = btreemap! {
"A".into() => btreemap!{
"A".into() => 1.0,
"B".into() => 2.0,
"@MMK_R_CGroup".into() => 3.0,
"DGroup".into() => 4.0,
},
"BGroup".into() => btreemap!{
"A".into() => 5.0,
"B".into() => 6.0,
"@MMK_R_CGroup".into() => 7.0,
"DGroup".into() => 8.0,
},
"@MMK_L_CGroup".into() => btreemap!{
"A".into() => 9.0,
"B".into() => 10.0,
"@MMK_R_CGroup".into() => 11.0,
"DGroup".into() => 12.0,
},
};
let glyph_set = NameList::default();
let (groups_new, kerning_new) = upconvert_kerning(&groups, &kerning, &glyph_set);
assert_eq!(
groups_new,
btreemap! {
"BGroup".into() => vec!["B".into()],
"@MMK_L_CGroup".into() => vec!["C".into()],
"@MMK_R_CGroup".into() => vec!["C".into()],
"DGroup".into() => vec!["D".into()],
"public.kern1.BGroup".into() => vec!["B".into()],
"public.kern1.CGroup".into() => vec!["C".into()],
"public.kern2.CGroup".into() => vec!["C".into()],
"public.kern2.DGroup".into() => vec!["D".into()],
}
);
assert_eq!(
kerning_new,
btreemap! {
"A".into() => btreemap!{
"A".into() => 1.0,
"B".into() => 2.0,
"public.kern2.CGroup".into() => 3.0,
"public.kern2.DGroup".into() => 4.0,
},
"public.kern1.BGroup".into() => btreemap!{
"A".into() => 5.0,
"B".into() => 6.0,
"public.kern2.CGroup".into() => 7.0,
"public.kern2.DGroup".into() => 8.0,
},
"public.kern1.CGroup".into() => btreemap!{
"A".into() => 9.0,
"B".into() => 10.0,
"public.kern2.CGroup".into() => 11.0,
"public.kern2.DGroup".into() => 12.0,
}
}
);
}
#[test]
fn test_upconvert_kerning_glyphname_groupname() {
let ufo_v1 =
Font::load("testdata/upconversion_kerning/glyphname_groupname_UFOv1.ufo").unwrap();
let ufo_v2 =
Font::load("testdata/upconversion_kerning/glyphname_groupname_UFOv2.ufo").unwrap();
let groups_expected: Groups = plist::from_file(
"testdata/upconversion_kerning/glyphname_groupname_groups_expected.plist",
)
.unwrap();
let kerning_expected: Kerning = plist::from_file(
"testdata/upconversion_kerning/glyphname_groupname_kerning_expected.plist",
)
.unwrap();
assert_eq!(ufo_v1.meta.format_version, FormatVersion::V3);
assert_eq!(ufo_v2.meta.format_version, FormatVersion::V3);
assert_eq!(ufo_v1.groups.unwrap(), groups_expected);
assert_eq!(ufo_v2.groups.unwrap(), groups_expected);
assert_eq!(ufo_v1.kerning.unwrap(), kerning_expected);
assert_eq!(ufo_v2.kerning.unwrap(), kerning_expected);
}
}