fm-vector 0.1.0

A notation program's PDF read from its vectors: every notehead, stem, beam and barline where the engraver put it, read into .fmn and compiled to MusicXML, with the words from the PDF's text. No model, no server: it runs in the browser.
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
//! Music fonts older than SMuFL, read as SMuFL: Sibelius's Opus family (Opus, Helsinki, Inkpen,
//! Reprise), Finale's Maestro family (Maestro, Petrucci, Engraver, Jazz) and Doremi. They share
//! the layout of Adobe's Sonata for the glyphs every score has (a black notehead at 0xCF, a half
//! at 0xFA, `w` a whole note, `&` and `?` the clefs, `b` `n` `#` the accidentals, `j` `J` `r` `R`
//! the flags) and differ in the rest.
//!
//! A font is known by its name, or, where a PDF printer has taken the name away (Windows' "Print
//! to PDF" calls every font CIDFont+F1 and so on), by what it sets: a font most of whose glyphs
//! are those noteheads is a music font. Its bytes are found from the character codes or, in a
//! font keyed by glyph number, from the Unicode the PDF gives each glyph (the byte in the private
//! use area at U+F000, or read back through the Windows or Mac encoding).
//!
//! Each glyph is given the SMuFL code point the reader knows it by, so the rest of the reader
//! does not know the difference. Two things SMuFL tells apart and these fonts do not are settled
//! by place (`page.rs`): a dot is an augmentation dot beside a notehead and a staccato dot over or
//! under one.

/// What a font is to the reader.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Role {
    /// The music font: noteheads, clefs, rests.
    Main(Family),
    /// A companion of small noteheads, dots and braces (Opus Special).
    Special,
    /// A companion of dynamics and tuplet numbers (Opus Text).
    Marks,
    /// A companion with no name, taken for Opus Text by what it sets: Opus Special's dots and
    /// small noteheads are read in it too (told by their size), for where the guess is wrong.
    Companion,
    /// A companion whose glyphs the reader has no use for (figured bass, metronome marks).
    Unread,
    /// MuseScore's own font before SMuFL, its glyphs known by their Unicode (`mscore.rs`), and
    /// how large its em is drawn next to the font size the PDF says.
    MScore(crate::mscore::Font, f64),
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Family {
    /// Sibelius: Opus, Helsinki, Inkpen, Reprise.
    Opus,
    /// Finale: Maestro, Petrucci, Engraver, Jazz, and Sonata itself.
    Maestro,
    Doremi,
    /// The font of the Schubert and Wolf songs engraved for WIMA (capella's, by its layout): a
    /// black notehead at 0xE5, `A` and `E` the clefs, `Q` `R` `S` the accidentals.
    Capella,
    /// A font with no name: what Opus and Maestro agree on, and each one's own where the other
    /// has nothing.
    Either,
}

/// How a font's glyphs name their bytes.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Decoding {
    /// The character code is the byte (a simple font).
    Code,
    /// U+F000 + byte (a symbol font's private use area).
    Private,
    /// The Unicode of the byte in Windows' encoding (or Latin-1).
    Windows,
    /// The Unicode of the byte in the Mac's encoding.
    Mac,
    /// Neither code nor Unicode says: each glyph is known by its outline (`shapes.rs`).
    Shape,
}

pub const DECODINGS: [Decoding; 4] = [
    Decoding::Private,
    Decoding::Code,
    Decoding::Mac,
    Decoding::Windows,
];

/// A dot, to be told by its place: an augmentation dot or a staccato dot.
pub const DOT_BY_PLACE: u32 = 0;

/// A glyph as SMuFL has it: the code point, how large it is drawn next to the font's own
/// noteheads (a grace note's head is a glyph of its own, not a smaller size), and how far above
/// this font's origin SMuFL's is, in ems.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Sym {
    pub code: u32,
    pub scale: f64,
    pub rise: f64,
    /// How far right of the font's origin the glyph is drawn, in ems (a dot set at its note's
    /// origin).
    pub shift: f64,
}

fn sym(code: u32) -> Option<Sym> {
    Some(Sym {
        code,
        scale: 1.0,
        rise: 0.0,
        shift: 0.0,
    })
}

fn risen(code: u32, rise: f64) -> Option<Sym> {
    Some(Sym {
        code,
        scale: 1.0,
        rise,
        shift: 0.0,
    })
}

fn small(code: u32) -> Option<Sym> {
    Some(Sym {
        code,
        scale: 0.6,
        rise: 0.0,
        shift: 0.0,
    })
}

impl Decoding {
    /// The byte a glyph stands for, from its character code and its Unicode.
    pub fn byte(self, code: u32, uni: u32) -> Option<u8> {
        match self {
            Decoding::Code => u8::try_from(code).ok(),
            Decoding::Private => (0xF000..=0xF0FF)
                .contains(&uni)
                .then(|| (uni - 0xF000) as u8),
            Decoding::Windows => windows_byte(uni),
            Decoding::Mac => mac_byte(uni),
            Decoding::Shape => None,
        }
    }
}

/// Windows-1252 backwards: the bytes 0x80 to 0x9F are its own, the rest Latin-1.
fn windows_byte(uni: u32) -> Option<u8> {
    const HIGH: [u32; 32] = [
        0x20AC, 0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039,
        0x0152, 0, 0x017D, 0, 0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC,
        0x2122, 0x0161, 0x203A, 0x0153, 0, 0x017E, 0x0178,
    ];
    if let Some(k) = HIGH.iter().position(|&u| u != 0 && u == uni) {
        return Some(0x80 + k as u8);
    }
    (uni < 0x100 && !(0x80..0xA0).contains(&uni)).then_some(uni as u8)
}

/// Mac OS Roman backwards.
fn mac_byte(uni: u32) -> Option<u8> {
    const HIGH: [u32; 128] = [
        0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4,
        0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF,
        0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, 0x2020,
        0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4,
        0x00A8, 0x2260, 0x00C6, 0x00D8, 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202,
        0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, 0x00BF, 0x00A1,
        0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3,
        0x00D5, 0x0152, 0x0153, 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA,
        0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02, 0x2021, 0x00B7, 0x201A,
        0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC,
        0x00D3, 0x00D4, 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF,
        0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7,
    ];
    if uni < 0x80 {
        return Some(uni as u8);
    }
    HIGH.iter().position(|&u| u == uni).map(|k| 0x80 + k as u8)
}

/// The noteheads every one of these fonts has at the same bytes: black, half, whole.
pub fn is_head(byte: u8) -> bool {
    matches!(byte, 0xCF | 0xFA | 0x77)
}

/// What a font is by its name, if the name says: a music font of these families, or one of
/// their companions. None for a font of words, a SMuFL font, or a name that says nothing.
pub fn role_by_name(name: &str) -> Option<Role> {
    // a subset's tag (ABCDEF+) is not the name
    let name = name.split_once('+').map_or(name, |(tag, rest)| {
        if tag.len() == 6 && tag.chars().all(|c| c.is_ascii_uppercase()) {
            rest
        } else {
            name
        }
    });
    let n = name.to_ascii_lowercase();
    let has = |s: &str| n.contains(s);
    let sibelius =
        has("opus") || has("helsinki") || has("inkpen") || has("reprise") || has("schottmusic");
    let finale = has("maestro")
        || has("petrucci")
        || has("engraver")
        || has("sonata")
        || has("jazzfont")
        || has("broadwaycopyist");
    if sibelius {
        return Some(if has("chord") {
            return None;
        } else if has("special") {
            Role::Special
        } else if has("text") || has("script") {
            Role::Marks
        } else if has("figured") || has("metronome") || has("roman") || has("percussion") {
            Role::Unread
        } else {
            Role::Main(Family::Opus)
        });
    }
    if finale {
        // Finale's fonts of words (Engraver Text, Maestro Times) are words
        if has("text") || has("times") || has("lyric") {
            return None;
        }
        return Some(Role::Main(Family::Maestro));
    }
    if has("doremi") {
        return Some(Role::Main(Family::Doremi));
    }
    None
}

/// The SMuFL glyph of a byte of the music font. `tall`: the glyph stands higher than a notehead
/// (told apart where one family has a letter and another a notehead at a byte).
pub fn main_glyph(family: Family, byte: u8, tall: bool) -> Option<Sym> {
    use Family::*;
    if family == Capella {
        return capella_glyph(byte);
    }
    // what the family has of its own comes first
    let own = match (family, byte) {
        (Doremi, 0x47) => sym(0xE050),
        (Doremi, 0x46) => sym(0xE062),
        (Doremi, 0x4F) => sym(0xE08B),
        (Doremi, 0x3B) => sym(0xE4C0),
        (Doremi, 0xAB) => sym(0xE4E6),
        (Doremi, 0xEA) => sym(0xE4E7),
        (Doremi, 0x00 | 0x21) => sym(0xE242),
        (Doremi, 0x66) => sym(0xE522),
        (Doremi, 0x6D) => sym(0xE521),
        (Doremi, 0x70) => sym(0xE520),
        (Doremi, 0x26 | 0x3F | 0x43) => return None,
        // Finale's dynamics are in its music font
        (Maestro | Either, 0x66) if tall => sym(0xE522),
        (Maestro | Either, 0x70) if tall => sym(0xE520),
        (Maestro | Either, 0x46) => sym(0xE52D),
        (Maestro | Either, 0x50) => sym(0xE52C),
        (Maestro | Either, 0xC4) => sym(0xE52F),
        (Maestro | Either, 0xB9) => sym(0xE52B),
        (Maestro | Either, 0xEC) => sym(0xE530),
        (Maestro | Either, 0xB8) => sym(0xE52A),
        (Maestro | Either, 0xA7) => sym(0xE539),
        (Maestro | Either, 0x53) => sym(0xE536),
        (Maestro | Either, 0x5A) => sym(0xE535),
        (Maestro | Either, 0xD9) => sym(0xE566),
        (Maestro | Either, 0x2C) => sym(0xE4CE),
        // Finale's accidentals in parentheses (a reminder): the accidental
        (Maestro | Either, 0x41) if tall => sym(0xE260),
        (Maestro | Either, 0x4E) if tall => sym(0xE261),
        (Maestro, 0x61) if tall => sym(0xE262),
        (Maestro | Either, 0x8D) => sym(0xE53B),
        (Maestro | Either, 0xAF) => sym(0xE529),
        // Finale's treble clef for a tenor, an 8 under it
        (Maestro | Either, 0x56) if tall => sym(0xE052),
        // a 32nd's second flag: Finale's, then Sibelius's (set a little off the stem's end)
        (Maestro | Either, 0xFB) => risen(0xE250, 0.25),
        (Maestro | Either, 0xF0) => risen(0xE251, -0.25),
        (Opus | Either, 0x4B) if tall => sym(0xE250),
        (Opus | Either, 0xEF) if tall => sym(0xE251),
        _ => None,
    };
    if own.is_some() {
        return own;
    }
    match byte {
        // noteheads
        0x77 => sym(0xE0A2),
        0xFA => sym(0xE0A3),
        0xCF => sym(0xE0A4),
        0xC0 => sym(0xE0A9),
        // clefs
        0x26 => sym(0xE050),
        0x3F => sym(0xE062),
        0x42 => sym(0xE05C),
        // accidentals
        0x62 => sym(0xE260),
        0x6E => sym(0xE261),
        0x23 => sym(0xE262),
        0xDC => sym(0xE263),
        0xBA => sym(0xE264),
        // flags: an eighth's and a sixteenth's, up and down. Their origin is a space inside the
        // stem's end, where SMuFL's is at it
        0x6A => risen(0xE240, 0.25),
        0x4A => risen(0xE241, -0.25),
        0x72 => risen(0xE242, 0.25),
        0x52 => risen(0xE243, -0.25),
        // rests. The whole rest hangs a space over its origin, where SMuFL's hangs from it
        0xB7 => risen(0xE4E3, 0.25),
        0xEE => sym(0xE4E4),
        0xCE => sym(0xE4E5),
        0xE4 => sym(0xE4E6),
        0xC5 => sym(0xE4E7),
        0xA8 => sym(0xE4E8),
        0xF4 => sym(0xE4E9),
        0x2E => sym(DOT_BY_PLACE),
        // articulations and the fermata
        0x3E => sym(0xE4A0),
        0x2D => sym(0xE4A4),
        0x5E => sym(0xE4AC),
        0x76 => sym(0xE4AD),
        0x55 => sym(0xE4C0),
        0x75 => sym(0xE4C1),
        // time signatures
        0x30..=0x39 => sym(0xE080 + (byte - 0x30) as u32),
        0x63 => sym(0xE08A),
        0x43 => sym(0xE08B),
        // the pedal, the segno and the coda
        0xA1 => sym(0xE650),
        0x2A => sym(0xE655),
        0x25 => sym(0xE047),
        0xDE => sym(0xE048),
        _ => None,
    }
}

/// The noteheads of the capella layout: whole, half, black.
pub fn is_capella_head(byte: u8) -> bool {
    matches!(byte, 0xE3..=0xE5)
}

/// The SMuFL glyph of a byte of the capella layout. Every entry was seen in the 50 WIMA PDFs'
/// glyph chart; a byte not seen there is not guessed.
fn capella_glyph(byte: u8) -> Option<Sym> {
    match byte {
        0xE3 => sym(0xE0A2),
        0xE4 => sym(0xE0A3),
        0xE5 => sym(0xE0A4),
        // clefs, and the small ones of a change
        0x41 => sym(0xE050),
        0x42 => sym(0xE07A),
        0x45 => sym(0xE062),
        0x46 => sym(0xE07C),
        0x51 => sym(0xE260),
        0x52 => sym(0xE261),
        0x53 => sym(0xE262),
        0x54 => sym(0xE263),
        // flags, their origin at the stem's end as SMuFL's: up and down, to the 32nd
        0xE6 => sym(0xE240),
        0xE7 => sym(0xE242),
        0xE8 => sym(0xE244),
        0xEA => sym(0xE241),
        0xEB => sym(0xE243),
        0xEC => sym(0xE245),
        // rests. The whole rest hangs from the line a space over its origin
        0x49 => risen(0xE4E3, 0.25),
        0x4A => sym(0xE4E4),
        0x4B => sym(0xE4E5),
        0x4C => sym(0xE4E6),
        0x4D => sym(0xE4E7),
        // the dot is set at its note's origin and drawn right of the head
        0x2E => Some(Sym {
            code: DOT_BY_PLACE,
            scale: 1.0,
            rise: 0.0,
            shift: 0.354,
        }),
        0xCA => sym(0xE4A0),
        0x75 => sym(0xE4C0),
        0x6B => sym(0xE4C1),
        0x77 => sym(0xE567),
        0x6C => sym(0xE56C),
        0xAD => sym(0xE63C),
        0x79 => sym(0xE047),
        0x30..=0x39 => sym(0xE080 + (byte - 0x30) as u32),
        0x3A => sym(0xE08A),
        0x3B => sym(0xE08B),
        // dynamics, each a glyph
        0x66 => sym(0xE522),
        0x67 => sym(0xE52F),
        0x6A => sym(0xE52D),
        0x70 => sym(0xE520),
        0x71 => sym(0xE52B),
        0x73 => sym(0xE536),
        0x7C => sym(0xE534),
        // the numbers over tuplets: 3 and 6 seen, the digits in a row
        0xF7..=0xFF => sym(0xE880 + (byte - 0xF6) as u32),
        _ => None,
    }
}

/// The SMuFL glyph of a byte of a Type 1 music font's glyphs, which are grouped by their size
/// for want of the font's name: Sibelius's music font and Opus Special beside it at one size are
/// one group. What the music font has not, Opus Special's dots (told from Finale's glyphs at
/// those bytes by their height, `high` ems) and small noteheads are.
pub fn merged_glyph(family: Family, byte: u8, high: f64) -> Option<Sym> {
    let tall = high > 0.4;
    let opus = matches!(family, Family::Opus | Family::Either);
    let dot = matches!(byte, 0xAA | 0x99 | 0x00 | 0x24) && high < 0.15;
    let small_head = matches!(byte, 0x65 | 0x66 | 0x45 | 0x46) && high < 0.3;
    if opus && (dot || small_head) {
        return special_glyph(byte, tall);
    }
    main_glyph(family, byte, tall)
}

/// The SMuFL glyph of a byte of Opus Special: grace noteheads and flags, dots, the brace.
pub fn special_glyph(byte: u8, tall: bool) -> Option<Sym> {
    match byte {
        0xAA | 0x99 | 0x00 | 0x24 => sym(DOT_BY_PLACE),
        0x65 | 0x66 if !tall => small(0xE0A4),
        0x45 | 0x46 if !tall => small(0xE0A3),
        0x21 | 0xA1 | 0xB0 => small(0xE240),
        0x22 | 0xA2 => small(0xE241),
        _ => None,
    }
}

/// The SMuFL glyph of a byte of a companion with no name, `high` ems high: Opus Text's, or a dot
/// (a tenth of an em high) or small notehead of Opus Special.
pub fn companion_glyph(byte: u8, high: f64) -> Option<Sym> {
    let tall = high > 0.4;
    let dot = matches!(byte, 0xAA | 0x99 | 0x00 | 0x24) && high < 0.15;
    let small_head = matches!(byte, 0x65 | 0x66 | 0x45 | 0x46) && high < 0.3;
    if dot || small_head {
        return special_glyph(byte, tall);
    }
    marks_glyph(byte, tall)
}

/// The SMuFL glyph of a byte of Opus Text: dynamics, and the numbers over tuplets.
pub fn marks_glyph(byte: u8, tall: bool) -> Option<Sym> {
    match byte {
        0x70 => sym(0xE520),
        0x6D => sym(0xE521),
        0x66 if tall => sym(0xE522),
        0x72 => sym(0xE523),
        0x73 => sym(0xE524),
        0x7A => sym(0xE525),
        0x30..=0x39 => sym(0xE880 + (byte - 0x30) as u32),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_font_is_known_by_its_name() {
        assert_eq!(role_by_name("ABCDEF+Opus"), Some(Role::Main(Family::Opus)));
        assert_eq!(role_by_name("OpusStd"), Some(Role::Main(Family::Opus)));
        assert_eq!(role_by_name("OpusSpecialStd"), Some(Role::Special));
        assert_eq!(role_by_name("OpusText"), Some(Role::Marks));
        // chord symbols are words
        assert_eq!(role_by_name("OpusChords"), None);
        assert_eq!(role_by_name("Maestro"), Some(Role::Main(Family::Maestro)));
        assert_eq!(role_by_name("EngraverTextT"), None);
        assert_eq!(role_by_name("Doremi"), Some(Role::Main(Family::Doremi)));
        assert_eq!(role_by_name("TimesNewRomanPSMT"), None);
        assert_eq!(role_by_name("CIDFont+F1"), None);
    }

    #[test]
    fn a_32nd_s_second_flag_is_a_glyph_of_its_own() {
        let g = |f: Family, b: u8| main_glyph(f, b, true).map(|s| s.code);
        // Sibelius: K over the 16th's flag, 0xEF under it
        assert_eq!(g(Family::Opus, 0x4B), Some(0xE250));
        assert_eq!(g(Family::Opus, 0xEF), Some(0xE251));
        // Finale: 0xFB and 0xF0
        assert_eq!(g(Family::Maestro, 0xFB), Some(0xE250));
        assert_eq!(g(Family::Maestro, 0xF0), Some(0xE251));
        assert_eq!(g(Family::Maestro, 0x4B), None);
        // Finale's tenor clef
        assert_eq!(g(Family::Maestro, 0x56), Some(0xE052));
        // and its flat in parentheses
        assert_eq!(g(Family::Maestro, 0x41), Some(0xE260));
    }

    #[test]
    fn a_type_1_group_holds_the_music_font_and_its_companion() {
        // Opus Special's dot (a tenth of an em high) among the music font's glyphs
        let dot = merged_glyph(Family::Either, 0xAA, 0.09).unwrap();
        assert_eq!(dot.code, DOT_BY_PLACE);
        // Finale's italic 2 at that byte is no dot
        assert!(merged_glyph(Family::Either, 0xAA, 0.3).is_none());
        // a small notehead at F, where Finale has its mezzo forte
        assert_eq!(merged_glyph(Family::Either, 0x46, 0.2).unwrap().scale, 0.6);
        assert_eq!(
            merged_glyph(Family::Either, 0x46, 0.6).unwrap().code,
            0xE52D
        );
        // the music font's own glyphs as ever
        assert_eq!(
            merged_glyph(Family::Either, 0xCF, 0.26).unwrap().code,
            0xE0A4
        );
        assert!(merged_glyph(Family::Doremi, 0xAA, 0.09).is_none());
    }

    #[test]
    fn a_companion_with_no_name_is_read_as_either() {
        // Opus Special's dot and small notehead, Opus Text's forte and tuplet 3
        assert_eq!(companion_glyph(0xAA, 0.09).unwrap().code, DOT_BY_PLACE);
        assert_eq!(companion_glyph(0x66, 0.26).unwrap().scale, 0.6);
        assert_eq!(companion_glyph(0x66, 1.1).unwrap().code, 0xE522);
        assert_eq!(companion_glyph(0x33, 0.68).unwrap().code, 0xE883);
        // nothing as large as a letter is a dot, and Opus Special's small flags are not read
        assert!(companion_glyph(0xAA, 0.5).is_none());
        assert!(companion_glyph(0x21, 0.42).is_none());
    }

    #[test]
    fn capella_s_layout_is_its_own() {
        let g = |b: u8| main_glyph(Family::Capella, b, false).map(|s| s.code);
        assert_eq!(g(0xE5), Some(0xE0A4));
        assert_eq!(g(0x41), Some(0xE050));
        // the smaller clef of a change
        assert_eq!(g(0x46), Some(0xE07C));
        assert_eq!(g(0x53), Some(0xE262));
        // flags to the 32nd, up and down
        assert_eq!(g(0xE8), Some(0xE244));
        assert_eq!(g(0xEC), Some(0xE245));
        // a dynamic is one glyph, a tuplet's 3 its own digit
        assert_eq!(g(0x6A), Some(0xE52D));
        assert_eq!(g(0xF9), Some(0xE883));
        // Sonata's notehead is nothing here, and a byte never seen is not guessed
        assert_eq!(g(0xCF), None);
        assert_eq!(g(0x4E), None);
        // the dot is drawn right of the origin it is set at
        let dot = main_glyph(Family::Capella, 0x2E, false).unwrap();
        assert_eq!((dot.code, dot.shift), (DOT_BY_PLACE, 0.354));
    }

    #[test]
    fn a_byte_is_found_from_the_code_or_the_unicode() {
        // a black notehead, 0xCF
        assert_eq!(Decoding::Private.byte(53, 0xF0CF), Some(0xCF));
        assert_eq!(Decoding::Code.byte(0xCF, 0x00CF), Some(0xCF));
        assert_eq!(Decoding::Windows.byte(53, 0x00CF), Some(0xCF));
        // the Mac's 0xCF is U+0153, which Windows has at 0x9C
        assert_eq!(Decoding::Mac.byte(53, 0x0153), Some(0xCF));
        assert_eq!(Decoding::Windows.byte(53, 0x0153), Some(0x9C));
        assert_eq!(Decoding::Private.byte(53, 0x0153), None);
    }

    #[test]
    fn the_families_share_their_noteheads_and_differ_in_their_clefs() {
        for f in [
            Family::Opus,
            Family::Maestro,
            Family::Doremi,
            Family::Either,
        ] {
            assert_eq!(main_glyph(f, 0xCF, false).unwrap().code, 0xE0A4);
            assert_eq!(main_glyph(f, 0xFA, false).unwrap().code, 0xE0A3);
        }
        assert_eq!(main_glyph(Family::Opus, 0x26, true).unwrap().code, 0xE050);
        assert_eq!(main_glyph(Family::Doremi, 0x47, true).unwrap().code, 0xE050);
        assert!(main_glyph(Family::Doremi, 0x26, true).is_none());
        // F is Doremi's bass clef and Finale's mezzo forte
        assert_eq!(main_glyph(Family::Doremi, 0x46, true).unwrap().code, 0xE062);
        assert_eq!(
            main_glyph(Family::Maestro, 0x46, true).unwrap().code,
            0xE52D
        );
        // a small f is Opus Special's grace notehead, a tall one Opus Text's forte
        assert_eq!(special_glyph(0x66, false).unwrap().scale, 0.6);
        assert!(special_glyph(0x66, true).is_none());
        assert_eq!(marks_glyph(0x66, true).unwrap().code, 0xE522);
    }
}