f-office-docx 0.2.4

DOCX model and Word's layout rules.
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
//! Character classes: the sets of characters that the layout asks about a character, each a
//! list of named ranges of Unicode blocks.
//!
//! Three classes say "East Asian character", each for its own use, and they hold different
//! ranges: the full-width cell of the character grid and of kinsoku breaks, the East Asian font,
//! and Word's automatic space between East Asian text and Latin text or digits. This table keeps
//! the distinct ranges for each use in one place.

/// A range of code points that a character class lists, named after the Unicode blocks that it
/// covers.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum UnicodeBlock {
    /// Latin Extended-A, U+0100 to U+017F.
    LatinExtendedA,
    /// Greek and Coptic, U+0370 to U+03FF.
    GreekAndCoptic,
    /// Cyrillic, U+0400 to U+04FF.
    Cyrillic,
    /// Thai, U+0E00 to U+0E7F.
    Thai,
    /// Hangul Jamo, U+1100 to U+11FF.
    HangulJamo,
    /// General Punctuation, U+2000 to U+206F.
    GeneralPunctuation,
    /// Letterlike Symbols, U+2100 to U+214F.
    LetterlikeSymbols,
    /// CJK Radicals Supplement and Kangxi Radicals, U+2E80 to U+2FDF.
    CjkAndKangxiRadicals,
    /// Ideographic Description Characters with the unassigned code points before them,
    /// U+2FE0 to U+2FFF.
    IdeographicDescriptionCharacters,
    /// CJK Symbols and Punctuation, U+3000 to U+303F.
    CjkSymbolsAndPunctuation,
    /// Hiragana and Katakana, U+3040 to U+30FF.
    HiraganaAndKatakana,
    /// Bopomofo to CJK Compatibility, U+3100 to U+33FF: Bopomofo, Hangul Compatibility Jamo,
    /// Kanbun, CJK Strokes, Katakana Phonetic Extensions, Enclosed CJK Letters and Months, and
    /// CJK Compatibility.
    BopomofoToCjkCompatibility,
    /// CJK Unified Ideographs Extension A, U+3400 to U+4DBF.
    CjkUnifiedIdeographsExtensionA,
    /// Yijing Hexagram Symbols, U+4DC0 to U+4DFF.
    YijingHexagramSymbols,
    /// CJK Unified Ideographs, U+4E00 to U+9FFF.
    CjkUnifiedIdeographs,
    /// Hangul Jamo Extended-A, U+A960 to U+A97F.
    HangulJamoExtendedA,
    /// Hangul Syllables, U+AC00 to U+D7AF.
    HangulSyllables,
    /// CJK Compatibility Ideographs, U+F900 to U+FAFF.
    CjkCompatibilityIdeographs,
    /// CJK Compatibility Forms, U+FE30 to U+FE4F.
    CjkCompatibilityForms,
    /// The full-width forms of Halfwidth and Fullwidth Forms, U+FF00 to U+FF60.
    FullwidthForms,
    /// The half-width forms of Halfwidth and Fullwidth Forms, U+FF61 to U+FFDF.
    HalfwidthForms,
    /// The full-width signs of Halfwidth and Fullwidth Forms, U+FFE0 to U+FFE6.
    FullwidthSigns,
    /// The half-width signs of Halfwidth and Fullwidth Forms, U+FFE7 to U+FFEF.
    HalfwidthSigns,
    /// The Supplementary Ideographic Plane, U+20000 to U+2FFFF: CJK Unified Ideographs
    /// Extension B and later.
    SupplementaryIdeographicPlane,
}

impl UnicodeBlock {
    /// The first and the last code point of the range.
    const fn ends(self) -> (char, char) {
        match self {
            Self::LatinExtendedA => ('\u{0100}', '\u{017F}'),
            Self::GreekAndCoptic => ('\u{0370}', '\u{03FF}'),
            Self::Cyrillic => ('\u{0400}', '\u{04FF}'),
            Self::Thai => ('\u{0E00}', '\u{0E7F}'),
            Self::HangulJamo => ('\u{1100}', '\u{11FF}'),
            Self::GeneralPunctuation => ('\u{2000}', '\u{206F}'),
            Self::LetterlikeSymbols => ('\u{2100}', '\u{214F}'),
            Self::CjkAndKangxiRadicals => ('\u{2E80}', '\u{2FDF}'),
            Self::IdeographicDescriptionCharacters => ('\u{2FE0}', '\u{2FFF}'),
            Self::CjkSymbolsAndPunctuation => ('\u{3000}', '\u{303F}'),
            Self::HiraganaAndKatakana => ('\u{3040}', '\u{30FF}'),
            Self::BopomofoToCjkCompatibility => ('\u{3100}', '\u{33FF}'),
            Self::CjkUnifiedIdeographsExtensionA => ('\u{3400}', '\u{4DBF}'),
            Self::YijingHexagramSymbols => ('\u{4DC0}', '\u{4DFF}'),
            Self::CjkUnifiedIdeographs => ('\u{4E00}', '\u{9FFF}'),
            Self::HangulJamoExtendedA => ('\u{A960}', '\u{A97F}'),
            Self::HangulSyllables => ('\u{AC00}', '\u{D7AF}'),
            Self::CjkCompatibilityIdeographs => ('\u{F900}', '\u{FAFF}'),
            Self::CjkCompatibilityForms => ('\u{FE30}', '\u{FE4F}'),
            Self::FullwidthForms => ('\u{FF00}', '\u{FF60}'),
            Self::HalfwidthForms => ('\u{FF61}', '\u{FFDF}'),
            Self::FullwidthSigns => ('\u{FFE0}', '\u{FFE6}'),
            Self::HalfwidthSigns => ('\u{FFE7}', '\u{FFEF}'),
            Self::SupplementaryIdeographicPlane => ('\u{20000}', '\u{2FFFF}'),
        }
    }
}

/// A set of characters that the layout asks about a character.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum CharacterClass {
    /// A character that fills a whole cell of East Asian text: an East Asian letter, East Asian
    /// punctuation, or a full-width form. Half-width forms and other characters fill half a
    /// cell of a character grid. A character of this class takes the character spacing twice
    /// where single-byte and double-byte widths balance, and a line may break by kinsoku only
    /// beside one.
    FullWidthCell,
    /// A character of an East Asian script or its punctuation, which takes the East Asian font:
    /// Hangul, the CJK punctuation, kana and ideographs, and the full-width and half-width
    /// forms.
    EastAsianFont,
    /// A Chinese, Japanese or Korean letter for Word's automatic space between East Asian text
    /// and Latin text or digits: an ideograph, kana or a Hangul syllable.
    EastAsianSpacing,
    /// A letter of the Latin Extended-A, Greek and Cyrillic blocks, which takes Calibri in a
    /// run that splits by the character set of its suffixed font name, where code page 1252
    /// lacks it ([`in_code_page_1252`]).
    SplitLetter,
    /// A character of General Punctuation, which takes Arial in such a run where code page
    /// 1252 lacks it.
    SplitPunctuation,
    /// A character of Letterlike Symbols, such as the numero sign, which takes Cambria Math
    /// in such a run where code page 1252 lacks it.
    SplitSymbol,
    /// A character of the Thai block, whose line in a paragraph aligned `thaiDistribute` keeps
    /// its spaces from shrinking.
    Thai,
}

impl CharacterClass {
    /// The ranges of the class, in the order of their code points.
    const fn blocks(self) -> &'static [UnicodeBlock] {
        match self {
            Self::FullWidthCell => &[
                UnicodeBlock::HangulJamo,
                UnicodeBlock::CjkAndKangxiRadicals,
                UnicodeBlock::IdeographicDescriptionCharacters,
                UnicodeBlock::CjkSymbolsAndPunctuation,
                UnicodeBlock::HiraganaAndKatakana,
                UnicodeBlock::BopomofoToCjkCompatibility,
                UnicodeBlock::CjkUnifiedIdeographsExtensionA,
                UnicodeBlock::YijingHexagramSymbols,
                UnicodeBlock::CjkUnifiedIdeographs,
                UnicodeBlock::HangulSyllables,
                UnicodeBlock::CjkCompatibilityIdeographs,
                UnicodeBlock::CjkCompatibilityForms,
                UnicodeBlock::FullwidthForms,
                UnicodeBlock::FullwidthSigns,
            ],
            Self::EastAsianFont => &[
                UnicodeBlock::HangulJamo,
                UnicodeBlock::CjkAndKangxiRadicals,
                UnicodeBlock::CjkSymbolsAndPunctuation,
                UnicodeBlock::HiraganaAndKatakana,
                UnicodeBlock::BopomofoToCjkCompatibility,
                UnicodeBlock::CjkUnifiedIdeographsExtensionA,
                UnicodeBlock::YijingHexagramSymbols,
                UnicodeBlock::CjkUnifiedIdeographs,
                UnicodeBlock::HangulJamoExtendedA,
                UnicodeBlock::HangulSyllables,
                UnicodeBlock::CjkCompatibilityIdeographs,
                UnicodeBlock::CjkCompatibilityForms,
                UnicodeBlock::FullwidthForms,
                UnicodeBlock::HalfwidthForms,
                UnicodeBlock::FullwidthSigns,
                UnicodeBlock::HalfwidthSigns,
                UnicodeBlock::SupplementaryIdeographicPlane,
            ],
            Self::EastAsianSpacing => &[
                UnicodeBlock::HiraganaAndKatakana,
                UnicodeBlock::CjkUnifiedIdeographsExtensionA,
                UnicodeBlock::CjkUnifiedIdeographs,
                UnicodeBlock::HangulSyllables,
                UnicodeBlock::CjkCompatibilityIdeographs,
            ],
            Self::SplitLetter => &[
                UnicodeBlock::LatinExtendedA,
                UnicodeBlock::GreekAndCoptic,
                UnicodeBlock::Cyrillic,
            ],
            Self::SplitPunctuation => &[UnicodeBlock::GeneralPunctuation],
            Self::SplitSymbol => &[UnicodeBlock::LetterlikeSymbols],
            Self::Thai => &[UnicodeBlock::Thai],
        }
    }

    /// Whether the class holds the character `ch`. The ranges are in order, so a character
    /// before the next range ends the search, and a Latin character needs one comparison.
    pub(crate) fn contains(self, ch: char) -> bool {
        self.blocks()
            .iter()
            .map(|block| block.ends())
            .take_while(|&(first, _)| first <= ch)
            .any(|(_, last)| ch <= last)
    }
}

/// Whether `ch` is a Hangul syllable or jamo (Jamo, Compatibility Jamo, Jamo Extended-A,
/// Syllables, or Jamo Extended-B), which wraps at word boundaries when `w:wordWrap` is on.
pub(crate) const fn is_hangul(ch: char) -> bool {
    matches!(
        ch,
        '\u{1100}'..='\u{11FF}'
            | '\u{3130}'..='\u{318F}'
            | '\u{A960}'..='\u{A97F}'
            | '\u{AC00}'..='\u{D7AF}'
            | '\u{D7B0}'..='\u{D7FF}'
    )
}

/// The characters above U+00FF that Windows code page 1252 has, in its bytes 0x80 to 0x9F, as
/// the Unicode Consortium's mapping of the code page (`CP1252.TXT`) gives them.
const CODE_PAGE_1252_ABOVE_LATIN_1: [char; 27] = [
    '\u{0152}', '\u{0153}', '\u{0160}', '\u{0161}', '\u{0178}', '\u{017D}', '\u{017E}', '\u{0192}',
    '\u{02C6}', '\u{02DC}', '\u{2013}', '\u{2014}', '\u{2018}', '\u{2019}', '\u{201A}', '\u{201C}',
    '\u{201D}', '\u{201E}', '\u{2020}', '\u{2021}', '\u{2022}', '\u{2026}', '\u{2030}', '\u{2039}',
    '\u{203A}', '\u{20AC}', '\u{2122}',
];

/// Whether Windows code page 1252 has the character: ASCII, the Latin-1 characters from U+00A0
/// and the 27 characters that its bytes 0x80 to 0x9F give.
pub(crate) fn in_code_page_1252(ch: char) -> bool {
    matches!(ch, '\0'..='\u{7F}' | '\u{A0}'..='\u{FF}')
        || CODE_PAGE_1252_ABOVE_LATIN_1.contains(&ch)
}

/// Inclusive sorted ranges of characters above U+00FF with the Unicode `Emoji` binary property
/// (Unicode `emoji-data.txt`). Word for Windows' PDF writer emits each such character in its own
/// single-glyph text object even when drawn in the run's native font, whereas Latin-1 emoji
/// characters (`#`, `*`, `0`–`9`, `©`, `®`) stay in the surrounding text object.
const EMOJI_ABOVE_LATIN_1: [(char, char); 145] = [
    ('\u{203C}', '\u{203C}'),
    ('\u{2049}', '\u{2049}'),
    ('\u{2122}', '\u{2122}'),
    ('\u{2139}', '\u{2139}'),
    ('\u{2194}', '\u{2199}'),
    ('\u{21A9}', '\u{21AA}'),
    ('\u{231A}', '\u{231B}'),
    ('\u{2328}', '\u{2328}'),
    ('\u{23CF}', '\u{23CF}'),
    ('\u{23E9}', '\u{23F3}'),
    ('\u{23F8}', '\u{23FA}'),
    ('\u{24C2}', '\u{24C2}'),
    ('\u{25AA}', '\u{25AB}'),
    ('\u{25B6}', '\u{25B6}'),
    ('\u{25C0}', '\u{25C0}'),
    ('\u{25FB}', '\u{25FE}'),
    ('\u{2600}', '\u{2604}'),
    ('\u{260E}', '\u{260E}'),
    ('\u{2611}', '\u{2611}'),
    ('\u{2614}', '\u{2615}'),
    ('\u{2618}', '\u{2618}'),
    ('\u{261D}', '\u{261D}'),
    ('\u{2620}', '\u{2620}'),
    ('\u{2622}', '\u{2623}'),
    ('\u{2626}', '\u{2626}'),
    ('\u{262A}', '\u{262A}'),
    ('\u{262E}', '\u{262F}'),
    ('\u{2638}', '\u{263A}'),
    ('\u{2640}', '\u{2640}'),
    ('\u{2642}', '\u{2642}'),
    ('\u{2648}', '\u{2653}'),
    ('\u{265F}', '\u{2660}'),
    ('\u{2663}', '\u{2663}'),
    ('\u{2665}', '\u{2666}'),
    ('\u{2668}', '\u{2668}'),
    ('\u{267B}', '\u{267B}'),
    ('\u{267E}', '\u{267F}'),
    ('\u{2692}', '\u{2697}'),
    ('\u{2699}', '\u{2699}'),
    ('\u{269B}', '\u{269C}'),
    ('\u{26A0}', '\u{26A1}'),
    ('\u{26A7}', '\u{26A7}'),
    ('\u{26AA}', '\u{26AB}'),
    ('\u{26B0}', '\u{26B1}'),
    ('\u{26BD}', '\u{26BE}'),
    ('\u{26C4}', '\u{26C5}'),
    ('\u{26C8}', '\u{26C8}'),
    ('\u{26CE}', '\u{26CF}'),
    ('\u{26D1}', '\u{26D1}'),
    ('\u{26D3}', '\u{26D4}'),
    ('\u{26E9}', '\u{26EA}'),
    ('\u{26F0}', '\u{26F5}'),
    ('\u{26F7}', '\u{26FA}'),
    ('\u{26FD}', '\u{26FD}'),
    ('\u{2702}', '\u{2702}'),
    ('\u{2705}', '\u{2705}'),
    ('\u{2708}', '\u{270D}'),
    ('\u{270F}', '\u{270F}'),
    ('\u{2712}', '\u{2712}'),
    ('\u{2714}', '\u{2714}'),
    ('\u{2716}', '\u{2716}'),
    ('\u{271D}', '\u{271D}'),
    ('\u{2721}', '\u{2721}'),
    ('\u{2728}', '\u{2728}'),
    ('\u{2733}', '\u{2734}'),
    ('\u{2744}', '\u{2744}'),
    ('\u{2747}', '\u{2747}'),
    ('\u{274C}', '\u{274C}'),
    ('\u{274E}', '\u{274E}'),
    ('\u{2753}', '\u{2755}'),
    ('\u{2757}', '\u{2757}'),
    ('\u{2763}', '\u{2764}'),
    ('\u{2795}', '\u{2797}'),
    ('\u{27A1}', '\u{27A1}'),
    ('\u{27B0}', '\u{27B0}'),
    ('\u{27BF}', '\u{27BF}'),
    ('\u{2934}', '\u{2935}'),
    ('\u{2B05}', '\u{2B07}'),
    ('\u{2B1B}', '\u{2B1C}'),
    ('\u{2B50}', '\u{2B50}'),
    ('\u{2B55}', '\u{2B55}'),
    ('\u{3030}', '\u{3030}'),
    ('\u{303D}', '\u{303D}'),
    ('\u{3297}', '\u{3297}'),
    ('\u{3299}', '\u{3299}'),
    ('\u{1F004}', '\u{1F004}'),
    ('\u{1F0CF}', '\u{1F0CF}'),
    ('\u{1F170}', '\u{1F171}'),
    ('\u{1F17E}', '\u{1F17F}'),
    ('\u{1F18E}', '\u{1F18E}'),
    ('\u{1F191}', '\u{1F19A}'),
    ('\u{1F1E6}', '\u{1F1FF}'),
    ('\u{1F201}', '\u{1F202}'),
    ('\u{1F21A}', '\u{1F21A}'),
    ('\u{1F22F}', '\u{1F22F}'),
    ('\u{1F232}', '\u{1F23A}'),
    ('\u{1F250}', '\u{1F251}'),
    ('\u{1F300}', '\u{1F321}'),
    ('\u{1F324}', '\u{1F393}'),
    ('\u{1F396}', '\u{1F397}'),
    ('\u{1F399}', '\u{1F39B}'),
    ('\u{1F39E}', '\u{1F3F0}'),
    ('\u{1F3F3}', '\u{1F3F5}'),
    ('\u{1F3F7}', '\u{1F4FD}'),
    ('\u{1F4FF}', '\u{1F53D}'),
    ('\u{1F549}', '\u{1F54E}'),
    ('\u{1F550}', '\u{1F567}'),
    ('\u{1F56F}', '\u{1F570}'),
    ('\u{1F573}', '\u{1F57A}'),
    ('\u{1F587}', '\u{1F587}'),
    ('\u{1F58A}', '\u{1F58D}'),
    ('\u{1F590}', '\u{1F590}'),
    ('\u{1F595}', '\u{1F596}'),
    ('\u{1F5A4}', '\u{1F5A5}'),
    ('\u{1F5A8}', '\u{1F5A8}'),
    ('\u{1F5B1}', '\u{1F5B2}'),
    ('\u{1F5BC}', '\u{1F5BC}'),
    ('\u{1F5C2}', '\u{1F5C4}'),
    ('\u{1F5D1}', '\u{1F5D3}'),
    ('\u{1F5DC}', '\u{1F5DE}'),
    ('\u{1F5E1}', '\u{1F5E1}'),
    ('\u{1F5E3}', '\u{1F5E3}'),
    ('\u{1F5E8}', '\u{1F5E8}'),
    ('\u{1F5EF}', '\u{1F5EF}'),
    ('\u{1F5F3}', '\u{1F5F3}'),
    ('\u{1F5FA}', '\u{1F64F}'),
    ('\u{1F680}', '\u{1F6C5}'),
    ('\u{1F6CB}', '\u{1F6D2}'),
    ('\u{1F6D5}', '\u{1F6D9}'),
    ('\u{1F6DC}', '\u{1F6E5}'),
    ('\u{1F6E9}', '\u{1F6E9}'),
    ('\u{1F6EB}', '\u{1F6EC}'),
    ('\u{1F6F0}', '\u{1F6F0}'),
    ('\u{1F6F3}', '\u{1F6FC}'),
    ('\u{1F7E0}', '\u{1F7EB}'),
    ('\u{1F7F0}', '\u{1F7F0}'),
    ('\u{1F90C}', '\u{1F93A}'),
    ('\u{1F93C}', '\u{1F945}'),
    ('\u{1F947}', '\u{1F9FF}'),
    ('\u{1FA70}', '\u{1FA7C}'),
    ('\u{1FA80}', '\u{1FAC6}'),
    ('\u{1FAC8}', '\u{1FAC8}'),
    ('\u{1FACC}', '\u{1FADD}'),
    ('\u{1FADF}', '\u{1FAEB}'),
    ('\u{1FAEF}', '\u{1FAFA}'),
];

/// Whether `ch` is a non-Latin-1 character with the Unicode `Emoji` binary property.
pub(crate) fn is_emoji_above_latin_1(ch: char) -> bool {
    if ch < '\u{203C}' {
        return false;
    }
    EMOJI_ABOVE_LATIN_1
        .binary_search_by(|&(first, last)| {
            if ch < first {
                std::cmp::Ordering::Greater
            } else if ch > last {
                std::cmp::Ordering::Less
            } else {
                std::cmp::Ordering::Equal
            }
        })
        .is_ok()
}

#[cfg(test)]
mod tests {
    use super::{CharacterClass, EMOJI_ABOVE_LATIN_1, in_code_page_1252, is_emoji_above_latin_1};

    const CLASSES: [CharacterClass; 6] = [
        CharacterClass::FullWidthCell,
        CharacterClass::EastAsianFont,
        CharacterClass::EastAsianSpacing,
        CharacterClass::SplitLetter,
        CharacterClass::SplitPunctuation,
        CharacterClass::SplitSymbol,
    ];

    #[test]
    fn emoji_ranges_are_sorted_without_overlap() {
        for &(first, last) in &EMOJI_ABOVE_LATIN_1 {
            assert!(first <= last);
            assert!(first > '\u{00FF}');
        }
        for pair in EMOJI_ABOVE_LATIN_1.windows(2) {
            assert!(pair[0].1 < pair[1].0, "{pair:?}");
        }
    }

    #[test]
    fn tells_non_latin1_emoji_characters() {
        for ch in [
            '\u{203C}',
            '\u{2122}',
            '\u{2194}',
            '\u{25AA}',
            '\u{263A}',
            '\u{2660}',
            '\u{2666}',
            '\u{1F60A}',
        ] {
            assert!(is_emoji_above_latin_1(ch), "{ch}");
        }
        for ch in [
            '#', '*', '0', '\u{00A9}', '\u{00AE}', '\u{2192}', '\u{25A0}', '\u{25BA}', '\u{263B}',
            '\u{266A}', '\u{2713}',
        ] {
            assert!(!is_emoji_above_latin_1(ch), "{ch}");
        }
    }

    #[test]
    fn tells_the_characters_of_code_page_1252() {
        for ch in [
            'A', '\u{00E9}', '\u{00FF}', '\u{0160}', '\u{2014}', '\u{20AC}', '\u{2122}',
        ] {
            assert!(in_code_page_1252(ch), "{ch}");
        }
        for ch in [
            '\u{0085}', '\u{010D}', '\u{0410}', '\u{03B1}', '\u{2015}', '\u{2116}',
        ] {
            assert!(!in_code_page_1252(ch), "{ch}");
        }
    }

    #[test]
    fn lists_each_class_s_ranges_in_order_without_overlap() {
        for class in CLASSES {
            let ends: Vec<(char, char)> = class.blocks().iter().map(|b| b.ends()).collect();
            for (first, last) in &ends {
                assert!(first <= last, "{class:?}");
            }
            for pair in ends.windows(2) {
                assert!(pair[0].1 < pair[1].0, "{class:?} {pair:?}");
            }
        }
    }

    #[test]
    fn tells_full_width_characters_of_a_character_grid() {
        let full_width = |ch| CharacterClass::FullWidthCell.contains(ch);
        // Ideographs, kana, Hangul, East Asian punctuation and full-width forms fill a cell.
        for ch in ['原', 'の', 'カ', '한', '、', '(', '1', 'A'] {
            assert!(full_width(ch), "{ch}");
        }
        // Latin letters, digits, spaces and half-width katakana fill half of one.
        for ch in ['a', '4', ' ', 'カ', '-'] {
            assert!(!full_width(ch), "{ch}");
        }
    }

    #[test]
    fn holds_the_first_and_last_character_of_each_range_and_the_gaps_between_as_before() {
        // (character, full-width cell, East Asian font, East Asian spacing): the edges of each
        // range and the characters just outside them, where the classes differ.
        let cases = [
            ('A', false, false, false),
            ('0', false, false, false),
            ('\u{10FF}', false, false, false),
            ('\u{1100}', true, true, false),
            ('\u{11FF}', true, true, false),
            ('\u{1200}', false, false, false),
            ('\u{2E7F}', false, false, false),
            ('\u{2E80}', true, true, false),
            ('\u{2FDF}', true, true, false),
            ('\u{2FE0}', true, false, false),
            ('\u{2FFF}', true, false, false),
            ('\u{3000}', true, true, false),
            ('\u{303F}', true, true, false),
            ('\u{3040}', true, true, true),
            ('\u{30FF}', true, true, true),
            ('\u{3100}', true, true, false),
            ('\u{33FF}', true, true, false),
            ('\u{3400}', true, true, true),
            ('\u{4DBF}', true, true, true),
            ('\u{4DC0}', true, true, false),
            ('\u{4DFF}', true, true, false),
            ('\u{4E00}', true, true, true),
            ('\u{9FFF}', true, true, true),
            ('\u{A000}', false, false, false),
            ('\u{A95F}', false, false, false),
            ('\u{A960}', false, true, false),
            ('\u{A97F}', false, true, false),
            ('\u{A980}', false, false, false),
            ('\u{ABFF}', false, false, false),
            ('\u{AC00}', true, true, true),
            ('\u{D7AF}', true, true, true),
            ('\u{D7B0}', false, false, false),
            ('\u{F8FF}', false, false, false),
            ('\u{F900}', true, true, true),
            ('\u{FAFF}', true, true, true),
            ('\u{FB00}', false, false, false),
            ('\u{FE2F}', false, false, false),
            ('\u{FE30}', true, true, false),
            ('\u{FE4F}', true, true, false),
            ('\u{FE50}', false, false, false),
            ('\u{FEFF}', false, false, false),
            ('\u{FF00}', true, true, false),
            ('\u{FF60}', true, true, false),
            ('\u{FF61}', false, true, false),
            ('\u{FFDF}', false, true, false),
            ('\u{FFE0}', true, true, false),
            ('\u{FFE6}', true, true, false),
            ('\u{FFE7}', false, true, false),
            ('\u{FFEF}', false, true, false),
            ('\u{FFF0}', false, false, false),
            ('\u{1FFFF}', false, false, false),
            ('\u{20000}', false, true, false),
            ('\u{2FFFF}', false, true, false),
            ('\u{30000}', false, false, false),
        ];
        let east_asian = [
            CharacterClass::FullWidthCell,
            CharacterClass::EastAsianFont,
            CharacterClass::EastAsianSpacing,
        ];
        for (ch, cell, font, spacing) in cases {
            let held = east_asian.map(|class| class.contains(ch));
            assert_eq!(held, [cell, font, spacing], "U+{:04X}", u32::from(ch));
        }
    }
}