fontmerge 0.2.1

Merge two font source files, copying glyphs and OpenType layout features from one font to another
Documentation
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//! Rust port of the `ufomerge` Python test suite (see `ufomerge/tests/`), adapted to
//! operate on `babelfont::Font` objects instead of UFOs.
#![allow(clippy::unwrap_used, clippy::indexing_slicing, clippy::expect_used)]

use std::collections::HashSet;

use babelfont::{Anchor, Component, DecomposedAffine, Font, Shape, SmolStr};

use crate::{
    args::{DuplicateLookupHandling, ExistingGlyphHandling, LayoutHandling},
    create_test_font, fontmerge, fontsubset,
    glyphset::GlyphsetFilter,
};

fn glyph_names(font: &Font) -> HashSet<String> {
    font.glyphs.iter().map(|g| g.name.to_string()).collect()
}

fn assert_glyphset(font: &Font, expected: &[&str]) {
    let actual = glyph_names(font);
    let expected: HashSet<String> = expected.iter().map(|s| s.to_string()).collect();
    assert_eq!(actual, expected);
}

fn merge(
    mut font1: Font,
    font2: Font,
    include: Vec<&str>,
    exclude: Vec<&str>,
    codepoints: Vec<char>,
    existing: ExistingGlyphHandling,
) -> Font {
    let include: Vec<SmolStr> = include.into_iter().map(SmolStr::from).collect();
    let exclude: Vec<SmolStr> = exclude.into_iter().map(SmolStr::from).collect();
    let filter = GlyphsetFilter::new(include, exclude, codepoints, &mut font1, &font2, existing);
    fontmerge(
        font1,
        font2,
        filter,
        LayoutHandling::Subset,
        DuplicateLookupHandling::First,
        true,
        true,
    )
    .expect("merge failed")
}

fn merge_glyphs(
    font1: Font,
    font2: Font,
    include: Vec<&str>,
    existing: ExistingGlyphHandling,
) -> Font {
    merge(font1, font2, include, vec![], vec![], existing)
}

fn merge_glyphs_with_duplicate_policy(
    mut font1: Font,
    font2: Font,
    include: Vec<&str>,
    duplicate_lookups: DuplicateLookupHandling,
) -> Font {
    let include: Vec<SmolStr> = include.into_iter().map(SmolStr::from).collect();
    let filter = GlyphsetFilter::new(
        include,
        vec![],
        vec![],
        &mut font1,
        &font2,
        ExistingGlyphHandling::Replace,
    );
    fontmerge(
        font1,
        font2,
        filter,
        LayoutHandling::Subset,
        duplicate_lookups,
        true,
        true,
    )
    .expect("merge failed")
}

fn subset(font: Font, include: Vec<&str>, layout_handling: LayoutHandling) -> Font {
    let donor = font.clone();
    let include: Vec<SmolStr> = include.into_iter().map(SmolStr::from).collect();
    let mut target = font.clone();
    target.features = babelfont::Features::default();
    target.glyphs = babelfont::GlyphList::default();
    let filter =
        GlyphsetFilter::new_from_glyphs(include, &mut target, &donor, ExistingGlyphHandling::Skip);
    fontsubset(font, filter, layout_handling, true, true).expect("subset failed")
}

fn width(font: &Font, glyph: &str) -> f32 {
    font.glyphs.get(glyph).unwrap().layers[0].width
}

fn fea(font: &Font) -> String {
    font.features.to_fea()
}

#[test]
fn test_glyphset() {
    let font1 = create_test_font(vec!["A".into(), "B".into()], "");
    let font2 = create_test_font(vec!["C".into(), "D".into()], "");
    let merged = merge_glyphs(font1, font2, vec![], ExistingGlyphHandling::Replace);
    assert_glyphset(&merged, &["A", "B", "C", "D"]);
}

#[test]
fn test_component_closure() {
    let font1 = create_test_font(vec!["A".into(), "B".into()], "");
    let mut font2 = create_test_font(vec!["C".into(), "D".into(), "comp".into()], "");
    font2.glyphs.get_mut("D").unwrap().layers[0]
        .shapes
        .push(Shape::Component(Component {
            reference: "comp".into(),
            transform: DecomposedAffine::default(),
            location: Default::default(),
            format_specific: Default::default(),
        }));

    let merged = merge_glyphs(font1, font2, vec!["D"], ExistingGlyphHandling::Replace);
    assert_glyphset(&merged, &["A", "B", "D", "comp"]);
}

#[test]
fn test_kerning_flat() {
    let font1 = create_test_font(vec!["A".into(), "B".into()], "");
    let mut font2 = create_test_font(vec!["C".into(), "D".into(), "E".into()], "");
    font2.masters[0]
        .kerning
        .insert(("C".into(), "D".into()), 20);
    font2.masters[0]
        .kerning
        .insert(("C".into(), "E".into()), 15);
    font2.masters[0]
        .kerning
        .insert(("C".into(), "A".into()), -20);

    let merged = merge_glyphs(font1, font2, vec!["C", "D"], ExistingGlyphHandling::Replace);
    let kerning = &merged.masters[0].kerning;
    assert_eq!(
        kerning.get(&(SmolStr::from("C"), SmolStr::from("D"))),
        Some(&20)
    );
    assert_eq!(
        kerning.get(&(SmolStr::from("C"), SmolStr::from("A"))),
        Some(&-20)
    );
    assert_eq!(kerning.len(), 2);
}

#[test]
fn test_existing_handling() {
    let mut font1 = create_test_font(vec!["A".into(), "B".into()], "");
    font1.glyphs.get_mut("B").unwrap().layers[0].width = 100.0;
    let mut font2 = create_test_font(vec!["B".into(), "C".into()], "");
    font2.glyphs.get_mut("B").unwrap().layers[0].width = 200.0;

    let skipped = merge_glyphs(
        font1.clone(),
        font2.clone(),
        vec![],
        ExistingGlyphHandling::Skip,
    );
    assert_eq!(width(&skipped, "B"), 100.0);

    let replaced = merge_glyphs(font1, font2, vec![], ExistingGlyphHandling::Replace);
    assert_eq!(width(&replaced, "B"), 200.0);
}

#[test]
fn test_kerning_groups() {
    let mut font1 = create_test_font(vec!["A".into(), "B".into()], "");
    font1
        .first_kern_groups
        .insert("foo".into(), vec!["A".into()]);
    font1
        .second_kern_groups
        .insert("foo".into(), vec!["A".into()]);
    font1.masters[0]
        .kerning
        .insert(("@foo".into(), "@foo".into()), 10);
    font1.masters[0]
        .kerning
        .insert(("@foo".into(), "B".into()), 20);
    font1.masters[0]
        .kerning
        .insert(("A".into(), "@foo".into()), 30);
    font1.masters[0]
        .kerning
        .insert(("A".into(), "A".into()), 40);

    let mut font2 = create_test_font(vec!["A".into(), "B".into()], "");
    font2
        .first_kern_groups
        .insert("bar".into(), vec!["A".into()]);
    font2
        .second_kern_groups
        .insert("bar".into(), vec!["A".into()]);
    font2.masters[0]
        .kerning
        .insert(("@bar".into(), "@bar".into()), 50);
    font2.masters[0]
        .kerning
        .insert(("@bar".into(), "B".into()), 60);
    font2.masters[0]
        .kerning
        .insert(("A".into(), "@bar".into()), 70);
    font2.masters[0]
        .kerning
        .insert(("A".into(), "A".into()), 80);

    let merged = merge_glyphs(font1, font2, vec![], ExistingGlyphHandling::Replace);
    let kerning = &merged.masters[0].kerning;
    assert_eq!(
        kerning.get(&(SmolStr::from("@bar"), SmolStr::from("@bar"))),
        Some(&50)
    );
    assert_eq!(
        kerning.get(&(SmolStr::from("@bar"), SmolStr::from("B"))),
        Some(&60)
    );
    assert_eq!(
        kerning.get(&(SmolStr::from("A"), SmolStr::from("@bar"))),
        Some(&70)
    );
    assert_eq!(
        kerning.get(&(SmolStr::from("A"), SmolStr::from("A"))),
        Some(&80)
    );
    assert_eq!(kerning.len(), 4);
}

#[test]
fn test_dotted_circle() {
    fn dotted_circle_with_anchor(name: &str, x: f64, y: f64) -> Font {
        let mut font = create_test_font(vec!["A".into(), "B".into(), "dottedCircle".into()], "");
        font.glyphs.get_mut("dottedCircle").unwrap().layers[0]
            .anchors
            .push(Anchor {
                x,
                y,
                name: name.to_string(),
                format_specific: Default::default(),
            });
        font
    }

    let font1 = dotted_circle_with_anchor("top", 0.0, 100.0);
    let font2 = dotted_circle_with_anchor("bottom", 0.0, -100.0);
    let mut merged = merge(
        font1,
        font2,
        vec![],
        vec![],
        vec![],
        ExistingGlyphHandling::Replace,
    );
    merged.masters[0].kerning.clear(); // not relevant here
    let anchors: HashSet<String> = merged.glyphs.get("dottedCircle").unwrap().layers[0]
        .anchors
        .iter()
        .map(|a| a.name.clone())
        .collect();
    assert_eq!(
        anchors,
        HashSet::from(["top".to_string(), "bottom".to_string()])
    );
}

#[test]
fn test_exclude_glyphs_takes_precedence_over_codepoints() {
    // exclude_glyphs should win even if the excluded glyph also matches a requested codepoint,
    // and should leave the host's own glyph of the same name completely untouched.
    let mut font1 = create_test_font(vec!["A".into(), "B".into()], "");
    {
        let b1 = font1.glyphs.get_mut("B").unwrap();
        b1.layers[0].width = 100.0;
        b1.codepoints = vec![0x42];
    }
    let mut font2 = create_test_font(vec!["B".into(), "D".into()], "");
    {
        let b2 = font2.glyphs.get_mut("B").unwrap();
        b2.layers[0].width = 200.0;
        b2.codepoints = vec![0x42];
    }
    font2.glyphs.get_mut("D").unwrap().codepoints = vec![0x44];

    let merged = merge(
        font1,
        font2,
        vec![],
        vec!["B"],
        vec!['B', 'D'],
        ExistingGlyphHandling::Replace,
    );
    assert_glyphset(&merged, &["A", "B", "D"]);
    assert_eq!(width(&merged, "B"), 100.0);
    assert_eq!(merged.glyphs.get("B").unwrap().codepoints, vec![0x42]);
}

#[test]
fn test_glyphorder() {
    let font1 = create_test_font(vec!["A".into(), "B".into(), "C".into()], "");
    let font2 = create_test_font(vec!["D".into(), "E".into()], "");
    let merged = merge_glyphs(font1, font2, vec![], ExistingGlyphHandling::Replace);
    let order: Vec<String> = merged.glyphs.iter().map(|g| g.name.to_string()).collect();
    assert_eq!(order, vec!["A", "B", "C", "D", "E"]);
}

// --- Layout tests, ported from ufomerge/tests/test_layout.py ---

#[test]
fn test_layout_closure() {
    let font2 = create_test_font(
        vec!["A".into(), "B".into(), "C".into()],
        "feature ccmp { sub A A' B' by C; } ccmp;",
    );

    let ignored = subset(font2.clone(), vec!["A"], LayoutHandling::Ignore);
    assert_glyphset(&ignored, &["A"]);
    assert_eq!(fea(&ignored).trim(), "");

    let closed = subset(font2, vec!["A", "B"], LayoutHandling::Closure);
    assert_glyphset(&closed, &["A", "B", "C"]);
}

#[test]
fn test_ignorable_rule() {
    let font2 = create_test_font(
        vec!["A".into(), "B".into(), "C".into(), "D".into(), "E".into()],
        "lookup ccmp1 { sub A B by C; sub A D by E; } ccmp1; feature ccmp { lookup ccmp1; } ccmp;",
    );

    let subsetted = subset(font2.clone(), vec!["A", "B"], LayoutHandling::Subset);
    assert_glyphset(&subsetted, &["A", "B"]);

    let closed = subset(font2, vec!["A", "B"], LayoutHandling::Closure);
    assert_glyphset(&closed, &["A", "B", "C"]);
    let text = fea(&closed);
    assert!(text.contains("sub A B by C"));
    assert!(!text.contains("sub A D by E"));
}

#[test]
fn test_pos() {
    let font2 = create_test_font(
        vec!["A".into(), "B".into()],
        "lookup kern1 { pos [A B] 120; } kern1; feature kern { lookup kern1; } kern;",
    );

    let subsetted = subset(font2, vec!["A"], LayoutHandling::Subset);
    let text = fea(&subsetted);
    assert!(text.contains("pos [A] 120") || text.contains("pos A 120"));
    assert!(!text.contains("pos [A B]"));
}

#[test]
fn test_chain() {
    let font2 = create_test_font(
        vec!["A".into(), "B".into(), "C".into()],
        "lookup chained { pos A 120; pos B 200; } chained;\n\
         lookup chain { pos [A B]' lookup chained [A B C]; } chain;\n\
         feature kern { lookup chain; } kern;",
    );

    let subsetted = subset(font2, vec!["A", "C"], LayoutHandling::Subset);
    assert_glyphset(&subsetted, &["A", "C"]);
    let text = fea(&subsetted);
    assert!(text.contains("pos A 120"));
    assert!(!text.contains("pos B 200"));
}

#[test]
fn test_languagesystems() {
    let mut font1 = create_test_font(
        vec!["ka-deva".into(), "sa-deva".into()],
        "languagesystem latn dflt;\nfeature ccmp { sub A by B; } ccmp;",
    );
    // A and B aren't in font1's glyphset in this reduced test, but that's fine: we only care
    // about languagesystem merging here.
    font1.glyphs.push(babelfont::Glyph {
        name: "A".into(),
        ..Default::default()
    });
    font1.glyphs.push(babelfont::Glyph {
        name: "B".into(),
        ..Default::default()
    });

    let font2 = create_test_font(
        vec![
            "ka-deva".into(),
            "sa-deva".into(),
            "ta-deva".into(),
            "kssa-deva".into(),
            "la-deva".into(),
        ],
        "languagesystem DFLT dflt;\n\
         languagesystem dev2 dflt;\n\
         languagesystem dev2 NEP;\n\
         feature ccmp {\n\
           sub ka-deva by sa-deva;\n\
           script dev2;\n\
           language NEP;\n\
           sub ta-deva by kssa-deva;\n\
           sub la-deva by kssa-deva;\n\
         } ccmp;",
    );

    let merged = merge_glyphs(
        font1,
        font2,
        vec!["ka-deva", "sa-deva", "kssa-deva", "ta-deva"],
        ExistingGlyphHandling::Replace,
    );
    let text = fea(&merged);
    assert!(text.contains("languagesystem DFLT dflt"));
    assert!(text.contains("languagesystem latn dflt"));
    assert!(text.contains("languagesystem dev2 dflt"));
    assert!(text.contains("languagesystem dev2 NEP"));
    assert!(text.contains("sub A by B"));
    assert!(text.contains("sub ka-deva by sa-deva"));
    assert!(text.contains("sub ta-deva by kssa-deva"));
    assert!(!text.contains("sub la-deva by kssa-deva"));
}

#[test]
fn test_drop_contextual_empty_class() {
    let font2 = create_test_font(
        vec![
            "dagesh-hb".into(),
            "period".into(),
            "vav-hb".into(),
            "zayin-hb".into(),
        ],
        "@DAGESH = [dagesh-hb];\n\
         @OFFENDING_PUNCTUATION = [period];\n\
         lookup hebrew_mark_resolve_clashing_punctuation {\n\
           lookupflag RightToLeft;\n\
           pos [vav-hb zayin-hb] @DAGESH @OFFENDING_PUNCTUATION' 60;\n\
         } hebrew_mark_resolve_clashing_punctuation;\n\
         feature kern {\n\
           lookup hebrew_mark_resolve_clashing_punctuation;\n\
         } kern;",
    );

    let subsetted = subset(font2, vec!["period"], LayoutHandling::Subset);
    let text = fea(&subsetted);
    assert!(!text.contains("hebrew_mark_resolve_clashing_punctuation {"));
}

#[test]
fn test_deduplicate_classes() {
    let font2 = create_test_font(
        vec![
            "a".into(),
            "b".into(),
            "c".into(),
            "a.alt".into(),
            "b.alt".into(),
            "c.alt".into(),
        ],
        "@SOMETHING = [a b c];\n\
         @SOMETHING_ALT = [a.alt b.alt c.alt];\n\
         feature kern {\n\
           pos @SOMETHING @SOMETHING_ALT 60;\n\
           pos @SOMETHING_ALT @SOMETHING 30;\n\
         } kern;\n\
         feature locl {\n\
           sub @SOMETHING by @SOMETHING_ALT;\n\
         } locl;\n\
         feature rlig {\n\
           lookup bla {\n\
             @FOO = [b c];\n\
             sub @FOO a' @SOMETHING by b.alt;\n\
           } bla;\n\
         } rlig;",
    );

    let subsetted = subset(
        font2,
        vec!["a", "a.alt", "b.alt", "c", "c.alt"],
        LayoutHandling::Subset,
    );
    let text = fea(&subsetted);
    assert!(text.contains("feature kern"));
    assert!(text.contains("feature locl"));
    assert!(text.contains("feature rlig"));
    assert!(
        !text.contains(" b "),
        "glyph 'b' should have been subsetted out: {text}"
    );
}

#[test]
fn test_duplicate_lookup_names() {
    // Both fonts define a lookup called `kern1`. Merging should honour the default
    // ("first") policy: the host font's lookup wins and the donor's same-named
    // lookup is dropped, rather than both being welded together (which would
    // produce two lookups sharing a name and an uncompilable feature file).
    let font1 = create_test_font(
        vec!["A".into(), "B".into()],
        "lookup kern1 { pos A B -50; } kern1;\nfeature kern { lookup kern1; } kern;",
    );
    let font2 = create_test_font(
        vec!["C".into(), "D".into()],
        "lookup kern1 { pos C D -30; } kern1;\nfeature kern { lookup kern1; } kern;",
    );

    let merged = merge_glyphs(font1, font2, vec!["C", "D"], ExistingGlyphHandling::Replace);
    let text = fea(&merged);

    assert_eq!(
        text.matches("lookup kern1 {").count(),
        1,
        "only one definition of the duplicated lookup should remain:\n{text}"
    );
    assert!(
        text.contains("pos A B"),
        "the host's lookup should be the survivor:\n{text}"
    );
    assert!(
        !text.contains("pos C D"),
        "the donor's duplicate lookup should have been dropped:\n{text}"
    );
}

#[test]
fn test_duplicate_class_names() {
    // Both fonts define a glyph class called `@FOO`. FEA classes are unscoped
    // globals, so simply concatenating the two feature files would define `@FOO`
    // twice. The donor's definition should be renamed with a numeric suffix, and
    // its references updated to match, leaving the host's name untouched.
    let font1 = create_test_font(
        vec!["A".into(), "B".into()],
        "@FOO = [A B];\nfeature one { pos @FOO B -50; } one;",
    );
    let font2 = create_test_font(
        vec!["C".into(), "D".into()],
        "@FOO = [C D];\nfeature two { pos @FOO D -30; } two;",
    );

    let merged = merge_glyphs(font1, font2, vec!["C", "D"], ExistingGlyphHandling::Replace);
    let text = fea(&merged);

    assert_eq!(
        text.matches("@FOO = ").count(),
        1,
        "only one definition of @FOO should remain:\n{text}"
    );
    assert!(
        text.contains("@FOO = [A B];"),
        "the host's class keeps its name:\n{text}"
    );
    assert!(
        text.contains("@FOO_2 = [C D];"),
        "the donor's class is renamed:\n{text}"
    );
    assert!(
        text.contains("pos @FOO B -50;"),
        "the host's reference is unchanged:\n{text}"
    );
    assert!(
        text.contains("pos @FOO_2 D -30;"),
        "the donor's reference is updated:\n{text}"
    );
}

#[test]
fn test_duplicate_lookup_names_both() {
    // Under the "both" policy the donor's duplicate lookup is renamed rather than
    // dropped (and its references updated), so both lookups survive distinctly.
    let font1 = create_test_font(
        vec!["A".into(), "B".into()],
        "lookup kern1 { pos A B -50; } kern1;\nfeature kern { lookup kern1; } kern;",
    );
    let font2 = create_test_font(
        vec!["C".into(), "D".into()],
        "lookup kern1 { pos C D -30; } kern1;\nfeature kern { lookup kern1; } kern;",
    );

    let merged = merge_glyphs_with_duplicate_policy(
        font1,
        font2,
        vec!["C", "D"],
        DuplicateLookupHandling::Both,
    );
    let text = fea(&merged);

    assert!(
        text.contains("lookup kern1 {"),
        "the host's lookup keeps its name:\n{text}"
    );
    assert!(
        text.contains("lookup kern1_2 {"),
        "the donor's lookup is renamed:\n{text}"
    );
    assert!(
        text.contains("pos A B"),
        "the host's lookup content survives:\n{text}"
    );
    assert!(
        text.contains("pos C D"),
        "the donor's lookup content survives:\n{text}"
    );
    assert!(
        text.contains("lookup kern1_2;"),
        "the donor's reference is updated to the renamed lookup:\n{text}"
    );
}

// --- Known gaps: features present in ufomerge but not yet ported to fontmerge ---

#[test]
#[ignore = "GlyphsetFilter has no per-glyph existing_handling policy (see glyphset.rs::policy)"]
fn test_existing_handling_dict() {
    unimplemented!()
}