kui-core 0.1.0-alpha.45

kui contract: flat per-frame tree, clay-style flex layout, text stack, events as data, quad display list
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
//! Headless input helpers for driving a [`Core`] in tests: clicks, key
//! presses and the common readbacks. Test infrastructure, behind the
//! `conformance` feature (off by default); the crate's own tests and
//! `kui-lua`'s use it, nothing in a shipped app does.
//!
//! They are plain functions over a borrowed `Core`, since a test already
//! holds the core and builds its own frames: [`click`](crate::testing::click)
//! is the move-press-release triple a driver sends,
//! [`tab`](crate::testing::tab) a Tab press, [`kinds`](crate::testing::kinds)
//! the `kind` of each event a drive produced,
//! [`solids`](crate::testing::solids) the solid quads of the last frame, and
//! the `*_face` functions synthesize font fixtures.

use crate::display::{Quad, QuadKind};
use crate::geom::Vec2;
use crate::input::{EditKey, InputEvent, KeyCode, KeyMods, KeyPress, Mods, UiEvent};
use crate::runtime::Core;

/// A primary click at `at`: the pointer moves there, presses once and
/// releases. Every event the three inputs produced, in order — a test
/// that wants only the release's reads the tail.
pub fn click(core: &mut Core, at: Vec2) -> Vec<UiEvent> {
    let mut out = core.handle_input(InputEvent::CursorMoved(at));
    out.extend(core.handle_input(InputEvent::mouse_down(1)));
    out.extend(core.handle_input(InputEvent::mouse_up()));
    out
}

/// [`click`] by coordinates.
pub fn click_at(core: &mut Core, x: f32, y: f32) -> Vec<UiEvent> {
    click(core, Vec2::new(x, y))
}

/// The pointer moves to `at` and presses, and stays down: the start of a
/// drag. [`release`] ends it.
pub fn press(core: &mut Core, at: Vec2) -> Vec<UiEvent> {
    let mut out = core.handle_input(InputEvent::CursorMoved(at));
    out.extend(core.handle_input(InputEvent::mouse_down(1)));
    out
}

/// The primary button released where the pointer is.
pub fn release(core: &mut Core) -> Vec<UiEvent> {
    core.handle_input(InputEvent::mouse_up())
}

/// The pointer moves to `at`, nothing pressed.
pub fn hover(core: &mut Core, at: Vec2) -> Vec<UiEvent> {
    core.handle_input(InputEvent::CursorMoved(at))
}

/// A Tab (or Shift-Tab) on the editor channel — the one every driver
/// sends from `KeyPress::edit_event`, and the one the Tab ring reads.
pub fn tab(core: &mut Core, shift: bool) -> Vec<UiEvent> {
    edit_key(
        core,
        EditKey::Tab,
        Mods {
            shift,
            ..Mods::default()
        },
    )
}

/// An editing key on the editor channel.
pub fn edit_key(core: &mut Core, key: EditKey, mods: Mods) -> Vec<UiEvent> {
    core.handle_input(InputEvent::Key(key, mods))
}

/// Feeds every input in turn; every event they produced, in order.
pub fn drive(core: &mut Core, events: &[InputEvent]) -> Vec<UiEvent> {
    let mut out = Vec::new();
    for ev in events {
        out.extend(core.handle_input(ev.clone()));
    }
    out
}

/// The `kind` of each event, in order: what a payload map's `kind` says,
/// a bare string payload as itself, and `"?"` for anything else.
pub fn kinds(evs: &[UiEvent]) -> Vec<&str> {
    evs.iter()
        .map(|e| match e.kind() {
            Some(k) => k,
            None => e.payload.as_str().unwrap_or("?"),
        })
        .collect()
}

/// The code of each warning, in order.
pub fn codes(ws: &[crate::diag::Warning]) -> Vec<&'static str> {
    ws.iter().map(|w| w.code).collect()
}

/// The last frame's quads of one kind, in emission order.
pub fn quads_of(core: &mut Core, kind: QuadKind) -> Vec<Quad> {
    let (dl, _) = core.output();
    dl.quads
        .iter()
        .filter(|q| q.kind == kind)
        .cloned()
        .collect()
}

/// The last frame's solid quads (fills and borders), in emission order.
pub fn solids(core: &mut Core) -> Vec<Quad> {
    quads_of(core, QuadKind::Solid)
}

/// A raw key going down, unmodified — `InputEvent::KeyDown`, the keymap
/// channel, not the editing one [`edit_key`] drives — as the event, for a
/// [`drive`] batch.
pub fn key_press(code: KeyCode) -> InputEvent {
    InputEvent::KeyDown(KeyPress::new(code, KeyMods::default()))
}

/// The same key coming back up, as the event.
pub fn key_release(code: KeyCode) -> InputEvent {
    InputEvent::KeyUp(KeyPress::new(code, KeyMods::default()))
}

/// [`key_press`] with modifiers, sent: what the focused sink hears.
pub fn key_down(core: &mut Core, code: KeyCode, mods: KeyMods) -> Vec<UiEvent> {
    core.handle_input(InputEvent::KeyDown(KeyPress::new(code, mods)))
}

/// [`key_release`] with modifiers, sent.
pub fn key_up(core: &mut Core, code: KeyCode, mods: KeyMods) -> Vec<UiEvent> {
    core.handle_input(InputEvent::KeyUp(KeyPress::new(code, mods)))
}

/// What each event names, in order: a bare string payload as itself, else
/// its map's `tag` when that is a string. Events with neither are skipped
/// — the reading a test of "which node heard it" wants, where [`kinds`]
/// is the reading for "what happened".
pub fn tags(evs: &[UiEvent]) -> Vec<&str> {
    evs.iter()
        .filter_map(|e| {
            e.payload
                .as_str()
                .or_else(|| e.payload.get("tag")?.as_str())
        })
        .collect()
}

/// A font file the tests can register without one being installed: a
/// TrueType face, family **"Kui Liga"**, that maps every
/// printable ASCII character to a filled square and carries one `liga`
/// ligature — `f` followed by `i` shapes as one wider glyph unless the
/// feature is off. Built here rather than checked in as bytes, so the
/// tables are readable and the fixture cannot rot into a blob nobody can
/// regenerate; ~1.5 KB, built in microseconds.
///
/// Five glyphs: `.notdef` (empty), the square, `f`, `i` and `fi`; 1000
/// units per em, ascender 800, descender −200, every advance 500 but the
/// ligature's 700. The GSUB has one lookup, `liga` under `DFLT` and
/// `latn`, so the shaper applies it by default the way a real font's is.
pub fn liga_font() -> Vec<u8> {
    liga_font::build(&liga_font::Face::LIGA)
}

/// The [`liga_font`] fixture as a face of another family — `family`, at
/// `weight` on the CSS scale (the `OS/2` table's `usWeightClass`), italic
/// or upright, and saying it is fixed-pitch or not (the `post` table's
/// `isFixedPitch`) — so a test can stock a font database with families
/// whose faces differ in what the database reads off them.
/// The glyphs are the fixture's whatever the flag says.
pub fn font_face(family: &str, weight: u16, italic: bool, fixed_pitch: bool) -> Vec<u8> {
    liga_font::build(&liga_font::Face {
        family,
        weight,
        italic,
        fixed_pitch,
        ..liga_font::Face::LIGA
    })
}

/// The [`liga_font`] fixture as a proportional face of `family` that maps
/// å­— (U+5B57) to its square besides printable ASCII: a face Han text can
/// fall back to, measured like any other.
pub fn han_face(family: &str) -> Vec<u8> {
    liga_font::build(&liga_font::Face {
        family,
        han: true,
        ..liga_font::Face::LIGA
    })
}

/// The [`han_face`] fixture as a fixed-pitch face whose å­— is 700 units
/// wide, not 500: a monospaced face with å­— that a test tells apart from a
/// [`han_face`] by the advance alone.
pub fn mono_han_face(family: &str) -> Vec<u8> {
    liga_font::build(&liga_font::Face {
        family,
        fixed_pitch: true,
        han: true,
        wide_han: true,
        ..liga_font::Face::LIGA
    })
}

/// The [`han_face`] fixture with å­— its one character: a proportional
/// face of `family` with no `M`, as a CJK-only or symbols-only family has
/// none.
pub fn han_only_face(family: &str) -> Vec<u8> {
    liga_font::build(&liga_font::Face {
        family,
        han: true,
        ascii: false,
        ..liga_font::Face::LIGA
    })
}

/// A face whose glyph advances cannot be measured: the
/// [`han_face`] fixture, fixed-pitch, without its `head`, `hhea` and
/// `hmtx` tables — no units per em, no horizontal metrics — and without
/// its outlines, the way macOS's GB18030 Bitmap has none of them (it
/// carries Apple's `bhed` and bitmap tables instead, and this carries no
/// bitmaps either). The font database takes it, since its records come
/// from `name`, `OS/2` and `post`; the shaper reads its units per em as 0
/// and so its every advance as infinite.
pub fn unmeasurable_face(family: &str) -> Vec<u8> {
    liga_font::build(&liga_font::Face {
        family,
        fixed_pitch: true,
        han: true,
        metrics: false,
        ..liga_font::Face::LIGA
    })
}

/// A variable face of `family`: the [`liga_font`] fixture,
/// fixed-pitch, whose `OS/2` says weight 400 and which carries a `wght`
/// axis from `wght[0]` to `wght[2]`, its default at `wght[1]`, with the
/// named instances `instances` (subfamily name, coordinate). At the axis's
/// maximum every square's right edge is 100 units further right (`gvar`),
/// so a glyph drawn at the heavy end is 500 units wide where the default's
/// is 400; the advances stay 500. `[100, 100, 150]` with "Regular" at 100
/// and "Bold" at 150 is Berkeley Mono Variable's axis, which is not on the
/// CSS scale its `OS/2` weight is.
pub fn variable_face(
    family: &str,
    italic: bool,
    wght: [u16; 3],
    instances: &[(&str, u16)],
) -> Vec<u8> {
    liga_font::build(&liga_font::Face {
        family,
        italic,
        fixed_pitch: true,
        wght: Some(liga_font::Wght {
            axis: wght,
            instances,
        }),
        ..liga_font::Face::LIGA
    })
}

mod liga_font {
    /// What the tables say the face is: its family, weight and style.
    pub(super) struct Face<'a> {
        pub(super) family: &'a str,
        pub(super) weight: u16,
        pub(super) italic: bool,
        pub(super) fixed_pitch: bool,
        /// Maps å­— (U+5B57) to the square as well as printable ASCII.
        pub(super) han: bool,
        /// Maps å­— to the ligature's glyph, 700 units wide, instead of
        /// the square: a second face of it a test can tell apart.
        pub(super) wide_han: bool,
        /// Maps printable ASCII; without it å­— is the face's one
        /// character, and it has no `M`.
        pub(super) ascii: bool,
        /// Carries `head`, `hhea`, `hmtx` and the outlines; without them
        /// nothing says how wide a glyph is.
        pub(super) metrics: bool,
        /// A `wght` axis (`fvar`, `gvar`): a variable face.
        pub(super) wght: Option<Wght<'a>>,
    }

    /// A variable face's weight axis: minimum, default and maximum, and
    /// the named instances along it.
    pub(super) struct Wght<'a> {
        pub(super) axis: [u16; 3],
        pub(super) instances: &'a [(&'a str, u16)],
    }

    impl Face<'static> {
        pub(super) const LIGA: Self = Self {
            family: "Kui Liga",
            weight: 400,
            italic: false,
            fixed_pitch: false,
            han: false,
            wide_han: false,
            ascii: true,
            metrics: true,
            wght: None,
        };
    }

    impl Face<'_> {
        /// The `name` table's subfamily, the way a family's faces spell it.
        fn subfamily(&self) -> &'static str {
            match (self.weight >= 600, self.italic) {
                (false, false) => "Regular",
                (true, false) => "Bold",
                (false, true) => "Italic",
                (true, true) => "Bold Italic",
            }
        }
    }

    struct W(Vec<u8>);
    impl W {
        fn u8(&mut self, v: u8) {
            self.0.push(v);
        }
        fn u16(&mut self, v: u16) {
            self.0.extend_from_slice(&v.to_be_bytes());
        }
        fn i16(&mut self, v: i16) {
            self.0.extend_from_slice(&v.to_be_bytes());
        }
        fn u32(&mut self, v: u32) {
            self.0.extend_from_slice(&v.to_be_bytes());
        }
        fn i64(&mut self, v: i64) {
            self.0.extend_from_slice(&v.to_be_bytes());
        }
        fn bytes(&mut self, b: &[u8]) {
            self.0.extend_from_slice(b);
        }
        fn pad4(&mut self) {
            while !self.0.len().is_multiple_of(4) {
                self.0.push(0);
            }
        }
    }

    // Glyph 0 is `.notdef`, the empty one.
    const BOX: u16 = 1;
    const F: u16 = 2;
    const I: u16 = 3;
    const FI: u16 = 4;
    const GLYPHS: u16 = 5;
    const FIRST: u16 = 0x20;
    const LAST: u16 = 0x7E;
    /// å­—, the one ideograph a `han` face maps.
    const HAN: u16 = 0x5B57;

    /// A simple glyph: one contour, a square from (50, 0) to (`right`, 700).
    fn square(right: i16) -> Vec<u8> {
        let mut w = W(Vec::new());
        w.i16(1); // contours
        w.i16(50);
        w.i16(0);
        w.i16(right);
        w.i16(700);
        w.u16(3); // last point of the contour
        w.u16(0); // no instructions
        for _ in 0..4 {
            w.u8(0x01); // on-curve, both coordinates as i16
        }
        // x deltas, then y deltas: (50,0) (50,700) (right,700) (right,0).
        for dx in [50, 0, right - 50, 0] {
            w.i16(dx);
        }
        for dy in [0, 700, 0, -700] {
            w.i16(dy);
        }
        w.pad4();
        w.0
    }

    fn glyf_and_loca() -> (Vec<u8>, Vec<u8>) {
        let glyphs: [Vec<u8>; GLYPHS as usize] = [
            Vec::new(),
            square(450),
            square(450),
            square(450),
            square(650),
        ];
        let mut glyf = W(Vec::new());
        let mut loca = W(Vec::new());
        for g in &glyphs {
            loca.u32(glyf.0.len() as u32);
            glyf.bytes(g);
        }
        loca.u32(glyf.0.len() as u32);
        (glyf.0, loca.0)
    }

    fn head(face: &Face) -> Vec<u8> {
        let mut w = W(Vec::new());
        w.u32(0x0001_0000); // version
        w.u32(0x0001_0000); // fontRevision
        w.u32(0); // checkSumAdjustment (nothing here verifies it)
        w.u32(0x5F0F_3CF5); // magic
        w.u16(0x000B); // flags
        w.u16(1000); // unitsPerEm
        w.i64(0); // created
        w.i64(0); // modified
        w.i16(0); // xMin
        w.i16(0); // yMin
        w.i16(700); // xMax
        w.i16(700); // yMax
        // macStyle: bold, italic.
        w.u16(u16::from(face.weight >= 600) | u16::from(face.italic) << 1);
        w.u16(8); // lowestRecPPEM
        w.i16(2); // fontDirectionHint
        w.i16(1); // indexToLocFormat: long
        w.i16(0); // glyphDataFormat
        w.0
    }

    fn hhea() -> Vec<u8> {
        let mut w = W(Vec::new());
        w.u32(0x0001_0000);
        w.i16(800); // ascender
        w.i16(-200); // descender
        w.i16(0); // lineGap
        w.u16(700); // advanceWidthMax
        w.i16(0); // minLeftSideBearing
        w.i16(0); // minRightSideBearing
        w.i16(700); // xMaxExtent
        w.i16(1); // caretSlopeRise
        w.i16(0); // caretSlopeRun
        w.i16(0); // caretOffset
        for _ in 0..4 {
            w.i16(0);
        }
        w.i16(0); // metricDataFormat
        w.u16(GLYPHS); // numberOfHMetrics
        w.0
    }

    fn maxp() -> Vec<u8> {
        let mut w = W(Vec::new());
        w.u32(0x0001_0000);
        w.u16(GLYPHS);
        w.u16(4); // maxPoints
        w.u16(1); // maxContours
        w.u16(0); // maxCompositePoints
        w.u16(0); // maxCompositeContours
        w.u16(2); // maxZones
        for _ in 0..8 {
            w.u16(0);
        }
        w.0
    }

    fn os2(face: &Face) -> Vec<u8> {
        let mut w = W(Vec::new());
        w.u16(4); // version
        w.i16(500); // xAvgCharWidth
        w.u16(face.weight); // usWeightClass
        w.u16(5); // usWidthClass
        w.u16(0); // fsType
        for v in [650, 600, 0, 75, 650, 600, 0, 350, 50, 350] {
            w.i16(v); // subscript / superscript / strikeout metrics
        }
        w.i16(0); // sFamilyClass
        w.bytes(&[0; 10]); // panose
        w.u32(u32::from(face.ascii)); // ulUnicodeRange1: Basic Latin
        // ulUnicodeRange2: CJK Unified Ideographs (bit 59) for a `han` face.
        w.u32(u32::from(face.han) << 27);
        w.u32(0);
        w.u32(0);
        w.bytes(b"KUI "); // achVendID
        // fsSelection: ITALIC, BOLD, or REGULAR when neither.
        w.u16(match (face.weight >= 600, face.italic) {
            (false, false) => 0x0040,
            (bold, italic) => u16::from(italic) | u16::from(bold) << 5,
        });
        w.u16(if face.ascii { FIRST } else { HAN }); // usFirstCharIndex
        w.u16(if face.han { HAN } else { LAST }); // usLastCharIndex
        w.i16(800); // sTypoAscender
        w.i16(-200); // sTypoDescender
        w.i16(0); // sTypoLineGap
        w.u16(800); // usWinAscent
        w.u16(200); // usWinDescent
        w.u32(1); // ulCodePageRange1: Latin 1
        w.u32(0);
        w.i16(500); // sxHeight
        w.i16(700); // sCapHeight
        w.u16(0); // usDefaultChar
        w.u16(0x20); // usBreakChar
        w.u16(2); // usMaxContext: the ligature's length
        debug_assert_eq!(w.0.len(), 96);
        w.0
    }

    fn hmtx() -> Vec<u8> {
        let mut w = W(Vec::new());
        for g in 0..GLYPHS {
            w.u16(if g == FI { 700 } else { 500 });
            w.i16(0);
        }
        w.0
    }

    /// One format-4 subtable: every printable ASCII character through the
    /// glyph array, so `f` and `i` can be their own glyphs while the rest
    /// share the square, and a `han` face's å­— to the square by delta.
    fn cmap(face: &Face) -> Vec<u8> {
        if !face.ascii {
            return cmap_han_alone(face);
        }
        let chars = usize::from(LAST - FIRST + 1);
        // By endCode: ASCII, å­— when mapped, and the closing 0xFFFF.
        let han: &[u16] = if face.han { &[HAN] } else { &[] };
        let segments = 2 + han.len() as u16;
        let entry_selector = segments.ilog2() as u16;
        let search_range = 2 << entry_selector;
        let mut w = W(Vec::new());
        w.u16(0); // version
        w.u16(1); // one encoding record
        w.u16(3); // Windows
        w.u16(1); // Unicode BMP
        w.u32(12); // its subtable follows the header
        let length = 14 + 8 * usize::from(segments) + 2 + 2 * chars;
        w.u16(4); // format
        w.u16(length as u16);
        w.u16(0); // language
        w.u16(segments * 2);
        w.u16(search_range);
        w.u16(entry_selector);
        w.u16(segments * 2 - search_range); // rangeShift
        w.u16(LAST); // endCode
        han.iter().for_each(|&c| w.u16(c));
        w.u16(0xFFFF);
        w.u16(0); // reservedPad
        w.u16(FIRST); // startCode
        han.iter().for_each(|&c| w.u16(c));
        w.u16(0xFFFF);
        w.i16(0); // idDelta: the array's ids are final
        han.iter()
            .for_each(|&c| w.u16(han_glyph(face).wrapping_sub(c)));
        w.i16(1);
        w.u16(2 * segments); // idRangeOffset: the glyph array starts right after
        han.iter().for_each(|_| w.u16(0));
        w.u16(0);
        for c in FIRST..=LAST {
            w.u16(match c {
                0x66 => F,
                0x69 => I,
                _ => BOX,
            });
        }
        debug_assert_eq!(w.0.len(), 12 + length);
        w.0
    }

    /// The glyph a `han` face maps å­— to.
    fn han_glyph(face: &Face) -> u16 {
        if face.wide_han { FI } else { BOX }
    }

    /// [`cmap`] for a face without ASCII: å­— by delta and the closing
    /// 0xFFFF, no glyph array.
    fn cmap_han_alone(face: &Face) -> Vec<u8> {
        let segments: u16 = 2;
        let entry_selector = segments.ilog2() as u16;
        let search_range = 2 << entry_selector;
        let length = 14 + 8 * usize::from(segments) + 2;
        let mut w = W(Vec::new());
        w.u16(0); // version
        w.u16(1); // one encoding record
        w.u16(3); // Windows
        w.u16(1); // Unicode BMP
        w.u32(12); // its subtable follows the header
        w.u16(4); // format
        w.u16(length as u16);
        w.u16(0); // language
        w.u16(segments * 2);
        w.u16(search_range);
        w.u16(entry_selector);
        w.u16(segments * 2 - search_range); // rangeShift
        w.u16(HAN); // endCode
        w.u16(0xFFFF);
        w.u16(0); // reservedPad
        w.u16(HAN); // startCode
        w.u16(0xFFFF);
        w.u16(han_glyph(face).wrapping_sub(HAN)); // idDelta
        w.i16(1);
        w.u16(0); // idRangeOffset: none
        w.u16(0);
        debug_assert_eq!(w.0.len(), 12 + length);
        w.0
    }

    fn name(face: &Face) -> Vec<u8> {
        let full = match face.subfamily() {
            "Regular" => face.family.to_string(),
            sub => format!("{} {sub}", face.family),
        };
        let postscript: String = full.chars().filter(|c| !c.is_whitespace()).collect();
        let mut strings: Vec<(u16, &str)> = vec![
            (1, face.family),
            (2, face.subfamily()),
            (4, &full),
            (6, &postscript),
        ];
        // A variable face's axis name at 256 and its instances' after it.
        if let Some(wght) = &face.wght {
            strings.push((AXIS_NAME, "Weight"));
            for (i, (name, _)) in wght.instances.iter().enumerate() {
                strings.push((AXIS_NAME + 1 + i as u16, name));
            }
        }
        let mut w = W(Vec::new());
        w.u16(0); // format
        w.u16(strings.len() as u16);
        w.u16(6 + 12 * strings.len() as u16); // stringOffset
        let mut pool = W(Vec::new());
        for (id, s) in strings.iter().copied() {
            let start = pool.0.len() as u16;
            for unit in s.encode_utf16() {
                pool.u16(unit);
            }
            w.u16(3); // Windows
            w.u16(1); // Unicode BMP
            w.u16(0x0409); // en-US
            w.u16(id);
            w.u16((s.encode_utf16().count() * 2) as u16);
            w.u16(start);
        }
        w.bytes(&pool.0);
        w.0
    }

    fn post(face: &Face) -> Vec<u8> {
        let mut w = W(Vec::new());
        w.u32(0x0003_0000); // no glyph names
        // italicAngle, 16.16: an italic leans 12 degrees to the right.
        w.u32(if face.italic {
            (-12i32 << 16) as u32
        } else {
            0
        });
        w.i16(-100); // underlinePosition
        w.i16(50); // underlineThickness
        w.u32(u32::from(face.fixed_pitch)); // isFixedPitch
        for _ in 0..4 {
            w.u32(0);
        }
        w.0
    }

    /// `liga` under `DFLT` and `latn`: one ligature-substitution lookup,
    /// `f` + `i` → `fi`.
    fn gsub() -> Vec<u8> {
        let mut w = W(Vec::new());
        w.u32(0x0001_0000);
        w.u16(10); // ScriptList
        w.u16(36); // FeatureList
        w.u16(50); // LookupList
        // ScriptList: two records sharing one Script table.
        w.u16(2);
        w.bytes(b"DFLT");
        w.u16(14);
        w.bytes(b"latn");
        w.u16(14);
        w.u16(4); // Script: DefaultLangSys at 4
        w.u16(0); // no LangSysRecords
        w.u16(0); // LangSys: lookupOrder
        w.u16(0xFFFF); // no required feature
        w.u16(1); // one feature
        w.u16(0); // FeatureIndex 0
        // FeatureList.
        w.u16(1);
        w.bytes(b"liga");
        w.u16(8);
        w.u16(0); // Feature: no params
        w.u16(1); // one lookup
        w.u16(0); // LookupListIndex 0
        // LookupList.
        w.u16(1);
        w.u16(4);
        w.u16(4); // Lookup: type 4, ligature substitution
        w.u16(0); // flag
        w.u16(1); // one subtable
        w.u16(8); // at 8
        w.u16(1); // LigatureSubstFormat1
        w.u16(8); // Coverage at 8
        w.u16(1); // one LigatureSet
        w.u16(14); // at 14
        w.u16(1); // Coverage format 1
        w.u16(1); // one glyph
        w.u16(F);
        w.u16(1); // LigatureSet: one Ligature
        w.u16(4); // at 4
        w.u16(FI); // Ligature: the glyph
        w.u16(2); // two components
        w.u16(I); // the second
        debug_assert_eq!(w.0.len(), 86);
        w.0
    }

    /// The `name` id of a variable face's axis; its instances' follow.
    const AXIS_NAME: u16 = 256;

    fn fixed(v: u16) -> u32 {
        u32::from(v) << 16
    }

    /// One `wght` axis and its named instances.
    fn fvar(wght: &Wght) -> Vec<u8> {
        let mut w = W(Vec::new());
        w.u32(0x0001_0000);
        w.u16(16); // axesArrayOffset
        w.u16(2); // reserved
        w.u16(1); // axisCount
        w.u16(20); // axisSize
        w.u16(wght.instances.len() as u16);
        w.u16(8); // instanceSize: name id, flags, one coordinate
        w.bytes(b"wght");
        for v in wght.axis {
            w.u32(fixed(v)); // min, default, max
        }
        w.u16(0); // flags
        w.u16(AXIS_NAME);
        for (i, &(_, at)) in wght.instances.iter().enumerate() {
            w.u16(AXIS_NAME + 1 + i as u16);
            w.u16(0);
            w.u32(fixed(at));
        }
        w.0
    }

    /// Every square's right edge 100 units further right at the axis's
    /// maximum (a peak of +1.0), the phantom points — the advance — still.
    fn gvar() -> Vec<u8> {
        // One tuple over all points: the square's four and the four
        // phantoms. x: 0 0 100 100 0 0 0 0, y: all 0.
        let mut data = W(Vec::new());
        data.u16(1); // tupleVariationCount
        data.u16(10); // dataOffset: past this header and the tuple's
        data.u16(7); // variationDataSize
        data.u16(0xA000); // EMBEDDED_PEAK_TUPLE | PRIVATE_POINT_NUMBERS
        data.u16(0x4000); // peak: +1.0 in F2Dot14
        data.u8(0); // point numbers: all of them
        data.bytes(&[0x81, 0x01, 100, 100, 0x83]); // x deltas
        data.u8(0x87); // y deltas: eight zeros
        data.pad4();
        let per_glyph = data.0;
        let mut w = W(Vec::new());
        w.u16(1);
        w.u16(0);
        w.u16(1); // axisCount
        w.u16(0); // sharedTupleCount
        let array = 20 + 4 * (u32::from(GLYPHS) + 1);
        w.u32(array); // sharedTuplesOffset: none, at the array
        w.u16(GLYPHS);
        w.u16(1); // flags: 32-bit offsets
        w.u32(array); // glyphVariationDataArrayOffset
        // .notdef has no variation data; the squares each have one.
        w.u32(0);
        for g in 1..=GLYPHS {
            w.u32((per_glyph.len() * usize::from(g - 1)) as u32);
        }
        for _ in 1..GLYPHS {
            w.bytes(&per_glyph);
        }
        w.0
    }

    /// What a face without metrics leaves out: what says how wide a glyph
    /// is, and the outlines `loca` finds by `head`'s index format.
    const METRICS: [&[u8; 4]; 5] = [b"glyf", b"head", b"hhea", b"hmtx", b"loca"];

    fn checksum(table: &[u8]) -> u32 {
        table
            .chunks(4)
            .map(|c| {
                let mut word = [0u8; 4];
                word[..c.len()].copy_from_slice(c);
                u32::from_be_bytes(word)
            })
            .fold(0u32, u32::wrapping_add)
    }

    pub(super) fn build(face: &Face) -> Vec<u8> {
        let (glyf, loca) = glyf_and_loca();
        // The directory wants its records sorted by tag.
        let mut tables: Vec<(&[u8; 4], Vec<u8>)> = [
            (b"GSUB", gsub()),
            (b"OS/2", os2(face)),
            (b"cmap", cmap(face)),
            (b"glyf", glyf),
            (b"head", head(face)),
            (b"hhea", hhea()),
            (b"hmtx", hmtx()),
            (b"loca", loca),
            (b"maxp", maxp()),
            (b"name", name(face)),
            (b"post", post(face)),
        ]
        .into_iter()
        .filter(|(tag, _)| face.metrics || !METRICS.contains(tag))
        .collect();
        if let Some(wght) = &face.wght {
            tables.push((b"fvar", fvar(wght)));
            tables.push((b"gvar", gvar()));
            tables.sort_by_key(|(tag, _)| **tag);
        }
        let n = tables.len() as u16;
        let mut w = W(Vec::new());
        w.u32(0x0001_0000);
        w.u16(n);
        let mut search_range = 16;
        let mut entry_selector = 0;
        while search_range * 2 <= 16 * n {
            search_range *= 2;
            entry_selector += 1;
        }
        w.u16(search_range);
        w.u16(entry_selector);
        w.u16(16 * n - search_range);
        let mut offset = 12 + 16 * usize::from(n);
        for (tag, table) in &tables {
            w.bytes(*tag);
            w.u32(checksum(table));
            w.u32(offset as u32);
            w.u32(table.len() as u32);
            offset += table.len().div_ceil(4) * 4;
        }
        for (_, table) in &tables {
            w.bytes(table);
            w.pad4();
        }
        w.0
    }
}