Debian Codegraph @

third_party/rust/unicode-width/scripts/unicode.py

raw
1 #!/usr/bin/env python3
2 #
3 # Copyright 2011-2022 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
6 #
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
12
13 # This script uses the following Unicode tables:
14 #
15 # - DerivedCoreProperties.txt
16 # - EastAsianWidth.txt
17 # - HangulSyllableType.txt
18 # - NormalizationTest.txt (for tests only)
19 # - PropList.txt
20 # - ReadMe.txt
21 # - UnicodeData.txt
22 # - auxiliary/GraphemeBreakProperty.txt
23 # - emoji/emoji-data.txt
24 # - emoji/emoji-variation-sequences.txt
25 # - extracted/DerivedGeneralCategory.txt
26 #
27 # Since this should not require frequent updates, we just store this
28 # out-of-line and check the generated module into git.
29
30 import enum
31 import math
32 import operator
33 import os
34 import re
35 import sys
36 import urllib.request
37 from collections import defaultdict
38 from itertools import batched
39 from typing import Callable, Iterable
40
41 UNICODE_VERSION = "15.1.0"
42 """The version of the Unicode data files to download."""
43
44 NUM_CODEPOINTS = 0x110000
45 """An upper bound for which `range(0, NUM_CODEPOINTS)` contains Unicode's codespace."""
46
47 MAX_CODEPOINT_BITS = math.ceil(math.log2(NUM_CODEPOINTS - 1))
48 """The maximum number of bits required to represent a Unicode codepoint."""
49
50
51 class OffsetType(enum.IntEnum):
52 """Represents the data type of a lookup table's offsets. Each variant's value represents the
53 number of bits required to represent that variant's type."""
54
55 U2 = 2
56 """Offsets are 2-bit unsigned integers, packed four-per-byte."""
57 U4 = 4
58 """Offsets are 4-bit unsigned integers, packed two-per-byte."""
59 U8 = 8
60 """Each offset is a single byte (u8)."""
61
62
63 MODULE_PATH = "../src/tables.rs"
64 """The path of the emitted Rust module (relative to the working directory)"""
65
66 TABLE_SPLITS = [7, 13]
67 """The splits between the bits of the codepoint used to index each subtable.
68 Adjust these values to change the sizes of the subtables"""
69
70 Codepoint = int
71 BitPos = int
72
73
74 def fetch_open(filename: str, local_prefix: str = "", emoji: bool = False):
75 """Opens `filename` and return its corresponding file object. If `filename` isn't on disk,
76 fetches it from `https://www.unicode.org/Public/`. Exits with code 1 on failure.
77 """
78 basename = os.path.basename(filename)
79 localname = os.path.join(local_prefix, basename)
80 if not os.path.exists(localname):
81 if emoji:
82 prefix = f"emoji/{UNICODE_VERSION[:-2]}"
83 else:
84 prefix = f"{UNICODE_VERSION}/ucd"
85 urllib.request.urlretrieve(
86 f"https://www.unicode.org/Public/{prefix}/{filename}",
87 localname,
88 )
89 try:
90 return open(localname, encoding="utf-8")
91 except OSError:
92 sys.stderr.write(f"cannot load {localname}")
93 sys.exit(1)
94
95
96 def load_unicode_version() -> tuple[int, int, int]:
97 """Returns the current Unicode version by fetching and processing `ReadMe.txt`."""
98 with fetch_open("ReadMe.txt") as readme:
99 pattern = r"for Version (\d+)\.(\d+)\.(\d+) of the Unicode"
100 return tuple(map(int, re.search(pattern, readme.read()).groups())) # type: ignore
101
102
103 def load_property(filename: str, pattern: str, action: Callable[[int], None]):
104 with fetch_open(filename) as properties:
105 single = re.compile(rf"^([0-9A-F]+)\s*;\s*{pattern}\s+")
106 multiple = re.compile(rf"^([0-9A-F]+)\.\.([0-9A-F]+)\s*;\s*{pattern}\s+")
107
108 for line in properties.readlines():
109 raw_data = None # (low, high)
110 if match := single.match(line):
111 raw_data = (match.group(1), match.group(1))
112 elif match := multiple.match(line):
113 raw_data = (match.group(1), match.group(2))
114 else:
115 continue
116 low = int(raw_data[0], 16)
117 high = int(raw_data[1], 16)
118 for cp in range(low, high + 1):
119 action(cp)
120
121
122 def to_sorted_ranges(iter: Iterable[Codepoint]) -> list[tuple[Codepoint, Codepoint]]:
123 "Creates a sorted list of ranges from an iterable of codepoints"
124 lst = [c for c in iter]
125 lst.sort()
126 ret = []
127 for cp in lst:
128 if len(ret) > 0 and ret[-1][1] == cp - 1:
129 ret[-1] = (ret[-1][0], cp)
130 else:
131 ret.append((cp, cp))
132 return ret
133
134
135 class EastAsianWidth(enum.IntEnum):
136 """Represents the width of a Unicode character according to UAX 16.
137 All East Asian Width classes resolve into either
138 `EffectiveWidth.NARROW`, `EffectiveWidth.WIDE`, or `EffectiveWidth.AMBIGUOUS`.
139 """
140
141 NARROW = 1
142 """ One column wide. """
143 WIDE = 2
144 """ Two columns wide. """
145 AMBIGUOUS = 3
146 """ Two columns wide in a CJK context. One column wide in all other contexts. """
147
148
149 class CharWidthInTable(enum.IntEnum):
150 """Represents the width of a Unicode character
151 as stored in the tables."""
152
153 ZERO = 0
154 ONE = 1
155 TWO = 2
156 SPECIAL = 3
157
158
159 class WidthState(enum.IntEnum):
160 """
161 Width calculation proceeds according to a state machine.
162 We iterate over the characters of the string from back to front;
163 the next character encountered determines the transition to take.
164
165 The integer values of these variants have special meaning:
166 - Top bit: whether this is Vs16
167 - 2nd from top: whether this is Vs15
168 - 3rd bit from top: whether this is transparent to emoji/text presentation
169 (if set, should also set 4th)
170 - 4th bit: whether to set top bit on emoji presentation.
171 If this is set but 3rd is not, the width mode is related to zwj sequences
172 - 5th from top: whether this is unaffected by ligature-transparent
173 - 6th bit: if 4th is set but this one is not, then this is a ZWJ ligature state
174 where no ZWJ has been encountered yet; encountering one flips this on"""
175
176 # BASIC WIDTHS
177
178 ZERO = 0x1_0000
179 "Zero columns wide."
180
181 NARROW = 0x1_0001
182 "One column wide."
183
184 WIDE = 0x1_0002
185 "Two columns wide."
186
187 THREE = 0x1_0003
188 "Three columns wide."
189
190 # \r\n
191 LINE_FEED = 0b0000_0000_0000_0001
192 "\\n (CRLF has width 1)"
193
194 # EMOJI
195
196 # Emoji skintone modifiers
197 EMOJI_MODIFIER = 0b0000_0000_0000_0010
198 "`Emoji_Modifier`"
199
200 # Emoji ZWJ sequences
201
202 REGIONAL_INDICATOR = 0b0000_0000_0000_0011
203 "`Regional_Indicator`"
204
205 SEVERAL_REGIONAL_INDICATOR = 0b0000_0000_0000_0100
206 "At least two `Regional_Indicator`in sequence"
207
208 EMOJI_PRESENTATION = 0b0000_0000_0000_0101
209 "`Emoji_Presentation`"
210
211 ZWJ_EMOJI_PRESENTATION = 0b0001_0000_0000_0110
212 "\\u200D `Emoji_Presentation`"
213
214 VS16_ZWJ_EMOJI_PRESENTATION = 0b1001_0000_0000_0110
215 "\\uFE0F \\u200D `Emoji_Presentation`"
216
217 KEYCAP_ZWJ_EMOJI_PRESENTATION = 0b0001_0000_0000_0111
218 "\\u20E3 \\u200D `Emoji_Presentation`"
219
220 VS16_KEYCAP_ZWJ_EMOJI_PRESENTATION = 0b1001_0000_0000_0111
221 "\\uFE0F \\u20E3 \\u200D `Emoji_Presentation`"
222
223 REGIONAL_INDICATOR_ZWJ_PRESENTATION = 0b0000_0000_0000_1001
224 "`Regional_Indicator` \\u200D `Emoji_Presentation`"
225
226 EVEN_REGIONAL_INDICATOR_ZWJ_PRESENTATION = 0b0000_0000_0000_1010
227 "(`Regional_Indicator` `Regional_Indicator`)+ \\u200D `Emoji_Presentation`"
228
229 ODD_REGIONAL_INDICATOR_ZWJ_PRESENTATION = 0b0000_0000_0000_1011
230 "(`Regional_Indicator` `Regional_Indicator`)+ `Regional_Indicator` \\u200D `Emoji_Presentation`"
231
232 TAG_END_ZWJ_EMOJI_PRESENTATION = 0b0000_0000_0001_0000
233 "\\uE007F \\u200D `Emoji_Presentation`"
234
235 TAG_D1_END_ZWJ_EMOJI_PRESENTATION = 0b0000_0000_0001_0001
236 "\\uE0030..=\\uE0039 \\uE007F \\u200D `Emoji_Presentation`"
237
238 TAG_D2_END_ZWJ_EMOJI_PRESENTATION = 0b0000_0000_0001_0010
239 "(\\uE0030..=\\uE0039){2} \\uE007F \\u200D `Emoji_Presentation`"
240
241 TAG_D3_END_ZWJ_EMOJI_PRESENTATION = 0b0000_0000_0001_0011
242 "(\\uE0030..=\\uE0039){3} \\uE007F \\u200D `Emoji_Presentation`"
243
244 TAG_A1_END_ZWJ_EMOJI_PRESENTATION = 0b0000_0000_0001_1001
245 "\\uE0061..=\\uE007A \\uE007F \\u200D `Emoji_Presentation`"
246
247 TAG_A2_END_ZWJ_EMOJI_PRESENTATION = 0b0000_0000_0001_1010
248 "(\\uE0061..=\\uE007A){2} \\uE007F \\u200D `Emoji_Presentation`"
249
250 TAG_A3_END_ZWJ_EMOJI_PRESENTATION = 0b0000_0000_0001_1011
251 "(\\uE0061..=\\uE007A){3} \\uE007F \\u200D `Emoji_Presentation`"
252
253 TAG_A4_END_ZWJ_EMOJI_PRESENTATION = 0b0000_0000_0001_1100
254 "(\\uE0061..=\\uE007A){4} \\uE007F \\u200D `Emoji_Presentation`"
255
256 TAG_A5_END_ZWJ_EMOJI_PRESENTATION = 0b0000_0000_0001_1101
257 "(\\uE0061..=\\uE007A){35} \\uE007F \\u200D `Emoji_Presentation`"
258
259 TAG_A6_END_ZWJ_EMOJI_PRESENTATION = 0b0000_0000_0001_1110
260 "(\\uE0061..=\\uE007A){6} \\uE007F \\u200D `Emoji_Presentation`"
261
262 # VARIATION SELECTORS
263
264 # Text presentation sequences (not CJK)
265 VARIATION_SELECTOR_15 = 0b0100_0000_0000_0000
266 "\\uFE0E (text presentation sequences)"
267
268 # Emoji presentation sequences
269 VARIATION_SELECTOR_16 = 0b1000_0000_0000_0000
270 "\\uFE0F (emoji presentation sequences)"
271
272 # ARABIC LAM ALEF
273
274 JOINING_GROUP_ALEF = 0b0011_0000_1111_1111
275 "Joining_Group=Alef (Arabic Lam-Alef ligature)"
276
277 # COMBINING SOLIDUS (CJK only)
278
279 COMBINING_LONG_SOLIDUS_OVERLAY = 0b0011_1100_1111_1111
280 "\\u0338 (CJK only, makes <, =, > width 2)"
281
282 # SOLIDUS + ALEF (solidus is Joining_Type=Transparent)
283 SOLIDUS_OVERLAY_ALEF = 0b0011_1000_1111_1111
284 "\\u0338 followed by Joining_Group=Alef"
285
286 # SCRIPT ZWJ LIGATURES
287
288 # Hebrew alef lamed
289
290 HEBREW_LETTER_LAMED = 0b0011_1000_0000_0000
291 "\\u05DC (Alef-ZWJ-Lamed ligature)"
292
293 ZWJ_HEBREW_LETTER_LAMED = 0b0011_1100_0000_0000
294 "\\u200D\\u05DC (Alef-ZWJ-Lamed ligature)"
295
296 # Buginese <a -i> ya
297
298 BUGINESE_LETTER_YA = 0b0011_1000_0000_0001
299 "\\u1A10 (<a, -i> + ya ligature)"
300
301 ZWJ_BUGINESE_LETTER_YA = 0b0011_1100_0000_0001
302 "\\u200D\\u1A10 (<a, -i> + ya ligature)"
303
304 BUGINESE_VOWEL_SIGN_I_ZWJ_LETTER_YA = 0b0011_1100_0000_0010
305 "\\u1A17\\u200D\\u1A10 (<a, -i> + ya ligature)"
306
307 # Tifinagh bi-consonants
308
309 TIFINAGH_CONSONANT = 0b0011_1000_0000_0011
310 "\\u2D31..=\\u2D65 or \\u2D6F (joined by ZWJ or \\u2D7F TIFINAGH CONSONANT JOINER)"
311
312 ZWJ_TIFINAGH_CONSONANT = 0b0011_1100_0000_0011
313 "ZWJ then \\u2D31..=\\u2D65 or \\u2D6F"
314
315 TIFINAGH_JOINER_CONSONANT = 0b0011_1100_0000_0100
316 "\\u2D7F then \\u2D31..=\\u2D65 or \\u2D6F"
317
318 # Lisu tone letters
319 LISU_TONE_LETTER_MYA_NA_JEU = 0b0011_1100_0000_0101
320 "\\uA4FC or \\uA4FD (https://www.unicode.org/versions/Unicode15.0.0/ch18.pdf#G42078)"
321
322 # Old Turkic orkhon ec - orkhon i
323
324 OLD_TURKIC_LETTER_ORKHON_I = 0b0011_1000_0000_0110
325 "\\u10C03 (ORKHON EC-ZWJ-ORKHON I ligature)"
326
327 ZWJ_OLD_TURKIC_LETTER_ORKHON_I = 0b0011_1100_0000_0110
328 "\\u10C03 (ORKHON EC-ZWJ-ORKHON I ligature)"
329
330 # Khmer coeng signs
331
332 KHMER_COENG_ELIGIBLE_LETTER = 0b0011_1100_0000_0111
333 "\\u1780..=\\u17A2 | \\u17A7 | \\u17AB | \\u17AC | \\u17AF"
334
335 def table_width(self) -> CharWidthInTable:
336 "The width of a character as stored in the lookup tables."
337 match self:
338 case WidthState.ZERO:
339 return CharWidthInTable.ZERO
340 case WidthState.NARROW:
341 return CharWidthInTable.ONE
342 case WidthState.WIDE:
343 return CharWidthInTable.TWO
344 case _:
345 return CharWidthInTable.SPECIAL
346
347 def is_carried(self) -> bool:
348 "Whether this corresponds to a non-default `WidthInfo`."
349 return int(self) <= 0xFFFF
350
351 def width_alone(self) -> int:
352 "The width of a character with this type when it appears alone."
353 match self:
354 case (
355 WidthState.ZERO
356 | WidthState.COMBINING_LONG_SOLIDUS_OVERLAY
357 | WidthState.VARIATION_SELECTOR_15
358 | WidthState.VARIATION_SELECTOR_16
359 ):
360 return 0
361 case (
362 WidthState.WIDE
363 | WidthState.EMOJI_MODIFIER
364 | WidthState.EMOJI_PRESENTATION
365 ):
366 return 2
367 case WidthState.THREE:
368 return 3
369 case _:
370 return 1
371
372 def is_cjk_only(self) -> bool:
373 return self in [
374 WidthState.COMBINING_LONG_SOLIDUS_OVERLAY,
375 WidthState.SOLIDUS_OVERLAY_ALEF,
376 ]
377
378 def is_non_cjk_only(self) -> bool:
379 return self == WidthState.VARIATION_SELECTOR_15
380
381
382 assert len(set([v.value for v in WidthState])) == len([v.value for v in WidthState])
383
384
385 def load_east_asian_widths() -> list[EastAsianWidth]:
386 """Return a list of effective widths, indexed by codepoint.
387 Widths are determined by fetching and parsing `EastAsianWidth.txt`.
388
389 `Neutral`, `Narrow`, and `Halfwidth` characters are assigned `EffectiveWidth.NARROW`.
390
391 `Wide` and `Fullwidth` characters are assigned `EffectiveWidth.WIDE`.
392
393 `Ambiguous` characters are assigned `EffectiveWidth.AMBIGUOUS`."""
394
395 with fetch_open("EastAsianWidth.txt") as eaw:
396 # matches a width assignment for a single codepoint, i.e. "1F336;N # ..."
397 single = re.compile(r"^([0-9A-F]+)\s*;\s*(\w+) +# (\w+)")
398 # matches a width assignment for a range of codepoints, i.e. "3001..3003;W # ..."
399 multiple = re.compile(r"^([0-9A-F]+)\.\.([0-9A-F]+)\s*;\s*(\w+) +# (\w+)")
400 # map between width category code and condensed width
401 width_codes = {
402 **{c: EastAsianWidth.NARROW for c in ["N", "Na", "H"]},
403 **{c: EastAsianWidth.WIDE for c in ["W", "F"]},
404 "A": EastAsianWidth.AMBIGUOUS,
405 }
406
407 width_map = []
408 current = 0
409 for line in eaw.readlines():
410 raw_data = None # (low, high, width)
411 if match := single.match(line):
412 raw_data = (match.group(1), match.group(1), match.group(2))
413 elif match := multiple.match(line):
414 raw_data = (match.group(1), match.group(2), match.group(3))
415 else:
416 continue
417 low = int(raw_data[0], 16)
418 high = int(raw_data[1], 16)
419 width = width_codes[raw_data[2]]
420
421 assert current <= high
422 while current <= high:
423 # Some codepoints don't fall into any of the ranges in EastAsianWidth.txt.
424 # All such codepoints are implicitly given Neural width (resolves to narrow)
425 width_map.append(EastAsianWidth.NARROW if current < low else width)
426 current += 1
427
428 while len(width_map) < NUM_CODEPOINTS:
429 # Catch any leftover codepoints and assign them implicit Neutral/narrow width.
430 width_map.append(EastAsianWidth.NARROW)
431
432 # Ambiguous `Letter`s and `Modifier_Symbol`s are narrow
433 load_property(
434 "extracted/DerivedGeneralCategory.txt",
435 r"(:?Lu|Ll|Lt|Lm|Lo|Sk)",
436 lambda cp: (
437 operator.setitem(width_map, cp, EastAsianWidth.NARROW)
438 if width_map[cp] == EastAsianWidth.AMBIGUOUS
439 else None
440 ),
441 )
442
443 # GREEK ANO TELEIA: NFC decomposes to U+00B7 MIDDLE DOT
444 width_map[0x0387] = EastAsianWidth.AMBIGUOUS
445
446 # Canonical equivalence for symbols with stroke
447 with fetch_open("UnicodeData.txt") as udata:
448 single = re.compile(r"([0-9A-Z]+);.*?;.*?;.*?;.*?;([0-9A-Z]+) 0338;")
449 for line in udata.readlines():
450 if match := single.match(line):
451 composed = int(match.group(1), 16)
452 decomposed = int(match.group(2), 16)
453 if width_map[decomposed] == EastAsianWidth.AMBIGUOUS:
454 width_map[composed] = EastAsianWidth.AMBIGUOUS
455
456 return width_map
457
458
459 def load_zero_widths() -> list[bool]:
460 """Returns a list `l` where `l[c]` is true if codepoint `c` is considered a zero-width
461 character. `c` is considered a zero-width character if
462
463 - it has the `Default_Ignorable_Code_Point` property (determined from `DerivedCoreProperties.txt`),
464 - or if it has the `Grapheme_Extend` property (determined from `DerivedCoreProperties.txt`),
465 - or if it one of eight characters that should be `Grapheme_Extend` but aren't due to a Unicode spec bug,
466 - or if it has a `Hangul_Syllable_Type` of `Vowel_Jamo` or `Trailing_Jamo` (determined from `HangulSyllableType.txt`).
467 """
468
469 zw_map = [False] * NUM_CODEPOINTS
470
471 # `Default_Ignorable_Code_Point`s also have 0 width:
472 # https://www.unicode.org/faq/unsup_char.html#3
473 # https://www.unicode.org/versions/Unicode15.1.0/ch05.pdf#G40095
474 #
475 # `Grapheme_Extend` includes characters with general category `Mn` or `Me`,
476 # as well as a few `Mc` characters that need to be included so that
477 # canonically equivalent sequences have the same width.
478 load_property(
479 "DerivedCoreProperties.txt",
480 r"(?:Default_Ignorable_Code_Point|Grapheme_Extend)",
481 lambda cp: operator.setitem(zw_map, cp, True),
482 )
483
484 # Unicode spec bug: these should be `Grapheme_Cluster_Break=Extend`,
485 # as they canonically decompose to two characters with this property,
486 # but they aren't.
487 for c in [0x0CC0, 0x0CC7, 0x0CC8, 0x0CCA, 0x0CCB, 0x1B3B, 0x1B3D, 0x1B43]:
488 zw_map[c] = True
489
490 # Treat `Hangul_Syllable_Type`s of `Vowel_Jamo` and `Trailing_Jamo`
491 # as zero-width. This matches the behavior of glibc `wcwidth`.
492 #
493 # Decomposed Hangul characters consist of 3 parts: a `Leading_Jamo`,
494 # a `Vowel_Jamo`, and an optional `Trailing_Jamo`. Together these combine
495 # into a single wide grapheme. So we treat vowel and trailing jamo as
496 # 0-width, such that only the width of the leading jamo is counted
497 # and the resulting grapheme has width 2.
498 #
499 # (See the Unicode Standard sections 3.12 and 18.6 for more on Hangul)
500 load_property(
501 "HangulSyllableType.txt",
502 r"(?:V|T)",
503 lambda cp: operator.setitem(zw_map, cp, True),
504 )
505
506 # Syriac abbreviation mark:
507 # Zero-width `Prepended_Concatenation_Mark`
508 zw_map[0x070F] = True
509
510 # Some Arabic Prepended_Concatenation_Mark`s
511 # https://www.unicode.org/versions/Unicode15.0.0/ch09.pdf#G27820
512 zw_map[0x0605] = True
513 zw_map[0x0890] = True
514 zw_map[0x0891] = True
515 zw_map[0x08E2] = True
516
517 # `[:Grapheme_Cluster_Break=Prepend:]-[:Prepended_Concatenation_Mark:]`
518 gcb_prepend = set()
519 load_property(
520 "auxiliary/GraphemeBreakProperty.txt",
521 "Prepend",
522 lambda cp: gcb_prepend.add(cp),
523 )
524 load_property(
525 "PropList.txt",
526 "Prepended_Concatenation_Mark",
527 lambda cp: gcb_prepend.remove(cp),
528 )
529 for cp in gcb_prepend:
530 zw_map[cp] = True
531
532 # HANGUL CHOSEONG FILLER
533 # U+115F is a `Default_Ignorable_Code_Point`, and therefore would normally have
534 # zero width. However, the expected usage is to combine it with vowel or trailing jamo
535 # (which are considered 0-width on their own) to form a composed Hangul syllable with
536 # width 2. Therefore, we treat it as having width 2.
537 zw_map[0x115F] = False
538
539 # TIFINAGH CONSONANT JOINER
540 # (invisible only when used to join two Tifinagh consonants
541 zw_map[0x2D7F] = False
542
543 # DEVANAGARI CARET
544 # https://www.unicode.org/versions/Unicode15.0.0/ch12.pdf#G667447
545 zw_map[0xA8FA] = True
546
547 return zw_map
548
549
550 def load_width_maps() -> tuple[list[WidthState], list[WidthState]]:
551 """Load complete width table, including characters needing special handling.
552 (Returns 2 tables, one for East Asian and one for not.)"""
553
554 eaws = load_east_asian_widths()
555 zws = load_zero_widths()
556
557 not_ea = []
558 ea = []
559
560 for eaw, zw in zip(eaws, zws):
561 if zw:
562 not_ea.append(WidthState.ZERO)
563 ea.append(WidthState.ZERO)
564 else:
565 if eaw == EastAsianWidth.WIDE:
566 not_ea.append(WidthState.WIDE)
567 else:
568 not_ea.append(WidthState.NARROW)
569
570 if eaw == EastAsianWidth.NARROW:
571 ea.append(WidthState.NARROW)
572 else:
573 ea.append(WidthState.WIDE)
574
575 # Joining_Group=Alef (Arabic Lam-Alef ligature)
576 alef_joining = []
577 load_property(
578 "extracted/DerivedJoiningGroup.txt",
579 "Alef",
580 lambda cp: alef_joining.append(cp),
581 )
582
583 # Regional indicators
584 regional_indicators = []
585 load_property(
586 "PropList.txt",
587 "Regional_Indicator",
588 lambda cp: regional_indicators.append(cp),
589 )
590
591 # Emoji modifiers
592 emoji_modifiers = []
593 load_property(
594 "emoji/emoji-data.txt",
595 "Emoji_Modifier",
596 lambda cp: emoji_modifiers.append(cp),
597 )
598
599 # Default emoji presentation (for ZWJ sequences)
600 emoji_presentation = []
601 load_property(
602 "emoji/emoji-data.txt",
603 "Emoji_Presentation",
604 lambda cp: emoji_presentation.append(cp),
605 )
606
607 for cps, width in [
608 ([0x0A], WidthState.LINE_FEED),
609 ([0x05DC], WidthState.HEBREW_LETTER_LAMED),
610 (alef_joining, WidthState.JOINING_GROUP_ALEF),
611 (range(0x1780, 0x1783), WidthState.KHMER_COENG_ELIGIBLE_LETTER),
612 (range(0x1784, 0x1788), WidthState.KHMER_COENG_ELIGIBLE_LETTER),
613 (range(0x1789, 0x178D), WidthState.KHMER_COENG_ELIGIBLE_LETTER),
614 (range(0x178E, 0x1794), WidthState.KHMER_COENG_ELIGIBLE_LETTER),
615 (range(0x1795, 0x1799), WidthState.KHMER_COENG_ELIGIBLE_LETTER),
616 (range(0x179B, 0x179E), WidthState.KHMER_COENG_ELIGIBLE_LETTER),
617 (
618 [0x17A0, 0x17A2, 0x17A7, 0x17AB, 0x17AC, 0x17AF],
619 WidthState.KHMER_COENG_ELIGIBLE_LETTER,
620 ),
621 ([0x17A4], WidthState.WIDE),
622 ([0x17D8], WidthState.THREE),
623 ([0x1A10], WidthState.BUGINESE_LETTER_YA),
624 (range(0x2D31, 0x2D66), WidthState.TIFINAGH_CONSONANT),
625 ([0x2D6F], WidthState.TIFINAGH_CONSONANT),
626 ([0xA4FC], WidthState.LISU_TONE_LETTER_MYA_NA_JEU),
627 ([0xA4FD], WidthState.LISU_TONE_LETTER_MYA_NA_JEU),
628 ([0xFE0F], WidthState.VARIATION_SELECTOR_16),
629 ([0x10C03], WidthState.OLD_TURKIC_LETTER_ORKHON_I),
630 (emoji_presentation, WidthState.EMOJI_PRESENTATION),
631 (emoji_modifiers, WidthState.EMOJI_MODIFIER),
632 (regional_indicators, WidthState.REGIONAL_INDICATOR),
633 ]:
634 for cp in cps:
635 not_ea[cp] = width
636 ea[cp] = width
637
638 # East-Asian only
639 ea[0x0338] = WidthState.COMBINING_LONG_SOLIDUS_OVERLAY
640
641 # Not East Asian only
642 not_ea[0xFE0E] = WidthState.VARIATION_SELECTOR_15
643
644 return (not_ea, ea)
645
646
647 def load_joining_group_lam() -> list[tuple[Codepoint, Codepoint]]:
648 "Returns a list of character ranges with Joining_Group=Lam"
649 lam_joining = []
650 load_property(
651 "extracted/DerivedJoiningGroup.txt",
652 "Lam",
653 lambda cp: lam_joining.append(cp),
654 )
655
656 return to_sorted_ranges(lam_joining)
657
658
659 def load_non_transparent_zero_widths(
660 width_map: list[WidthState],
661 ) -> list[tuple[Codepoint, Codepoint]]:
662 "Returns a list of characters with zero width but not 'Joining_Type=Transparent'"
663
664 zero_widths = set()
665 for cp, width in enumerate(width_map):
666 if width.width_alone() == 0:
667 zero_widths.add(cp)
668 transparent = set()
669 load_property(
670 "extracted/DerivedJoiningType.txt",
671 "T",
672 lambda cp: transparent.add(cp),
673 )
674
675 return to_sorted_ranges(zero_widths - transparent)
676
677
678 def load_ligature_transparent() -> list[tuple[Codepoint, Codepoint]]:
679 """Returns a list of character ranges corresponding to all combining marks that are also
680 `Default_Ignorable_Code_Point`s, plus ZWJ. This is the set of characters that won't interrupt
681 a ligature."""
682 default_ignorables = set()
683 load_property(
684 "DerivedCoreProperties.txt",
685 "Default_Ignorable_Code_Point",
686 lambda cp: default_ignorables.add(cp),
687 )
688
689 combining_marks = set()
690 load_property(
691 "extracted/DerivedGeneralCategory.txt",
692 "(?:Mc|Mn|Me)",
693 lambda cp: combining_marks.add(cp),
694 )
695
696 default_ignorable_combinings = default_ignorables.intersection(combining_marks)
697 default_ignorable_combinings.add(0x200D) # ZWJ
698
699 return to_sorted_ranges(default_ignorable_combinings)
700
701
702 def load_solidus_transparent(
703 ligature_transparents: list[tuple[Codepoint, Codepoint]],
704 cjk_width_map: list[WidthState],
705 ) -> list[tuple[Codepoint, Codepoint]]:
706 """Characters expanding to a canonical combining class above 1, plus `ligature_transparent`s from above.
707 Ranges matching ones in `ligature_transparent` exactly are excluded (for compression), so it needs to bechecked also.
708 """
709
710 ccc_above_1 = set()
711 load_property(
712 "extracted/DerivedCombiningClass.txt",
713 "(?:[2-9]|(?:[1-9][0-9]+))",
714 lambda cp: ccc_above_1.add(cp),
715 )
716
717 for lo, hi in ligature_transparents:
718 for cp in range(lo, hi + 1):
719 ccc_above_1.add(cp)
720
721 num_chars = len(ccc_above_1)
722
723 # Recursive decompositions
724 while True:
725 with fetch_open("UnicodeData.txt") as udata:
726 single = re.compile(r"([0-9A-Z]+);.*?;.*?;.*?;.*?;([0-9A-F ]+);")
727 for line in udata.readlines():
728 if match := single.match(line):
729 composed = int(match.group(1), 16)
730 decomposed = [int(c, 16) for c in match.group(2).split(" ")]
731 if all([c in ccc_above_1 for c in decomposed]):
732 ccc_above_1.add(composed)
733 if len(ccc_above_1) == num_chars:
734 break
735 else:
736 num_chars = len(ccc_above_1)
737
738 for cp in ccc_above_1:
739 if cp != 0xFE0F:
740 assert (
741 cjk_width_map[cp].table_width() != CharWidthInTable.SPECIAL
742 ), f"U+{cp:X}"
743
744 sorted = to_sorted_ranges(ccc_above_1)
745 return list(filter(lambda range: range not in ligature_transparents, sorted))
746
747
748 def load_normalization_tests() -> list[tuple[str, str, str, str, str]]:
749 def parse_codepoints(cps: str) -> str:
750 return "".join(map(lambda cp: chr(int(cp, 16)), cps.split(" ")))
751
752 with fetch_open("NormalizationTest.txt") as normtests:
753 ret = []
754 single = re.compile(
755 r"^([0-9A-F ]+);([0-9A-F ]+);([0-9A-F ]+);([0-9A-F ]+);([0-9A-F ]+);"
756 )
757 for line in normtests.readlines():
758 if match := single.match(line):
759 ret.append(
760 (
761 parse_codepoints(match.group(1)),
762 parse_codepoints(match.group(2)),
763 parse_codepoints(match.group(3)),
764 parse_codepoints(match.group(4)),
765 parse_codepoints(match.group(5)),
766 )
767 )
768 return ret
769
770
771 def make_special_ranges(
772 width_map: list[WidthState],
773 ) -> list[tuple[tuple[Codepoint, Codepoint], WidthState]]:
774 "Assign ranges of characters to their special behavior (used in match)"
775 ret = []
776 can_merge_with_prev = False
777 for cp, width in enumerate(width_map):
778 if width == WidthState.EMOJI_PRESENTATION:
779 can_merge_with_prev = False
780 elif width.table_width() == CharWidthInTable.SPECIAL:
781 if can_merge_with_prev and ret[-1][1] == width:
782 ret[-1] = ((ret[-1][0][0], cp), width)
783 else:
784 ret.append(((cp, cp), width))
785 can_merge_with_prev = True
786 return ret
787
788
789 class Bucket:
790 """A bucket contains a group of codepoints and an ordered width list. If one bucket's width
791 list overlaps with another's width list, those buckets can be merged via `try_extend`.
792 """
793
794 def __init__(self):
795 """Creates an empty bucket."""
796 self.entry_set = set()
797 self.widths = []
798
799 def append(self, codepoint: Codepoint, width: CharWidthInTable):
800 """Adds a codepoint/width pair to the bucket, and appends `width` to the width list."""
801 self.entry_set.add((codepoint, width))
802 self.widths.append(width)
803
804 def try_extend(self, attempt: "Bucket") -> bool:
805 """If either `self` or `attempt`'s width list starts with the other bucket's width list,
806 set `self`'s width list to the longer of the two, add all of `attempt`'s codepoints
807 into `self`, and return `True`. Otherwise, return `False`."""
808 (less, more) = (self.widths, attempt.widths)
809 if len(self.widths) > len(attempt.widths):
810 (less, more) = (attempt.widths, self.widths)
811 if less != more[: len(less)]:
812 return False
813 self.entry_set |= attempt.entry_set
814 self.widths = more
815 return True
816
817 def entries(self) -> list[tuple[Codepoint, CharWidthInTable]]:
818 """Return a list of the codepoint/width pairs in this bucket, sorted by codepoint."""
819 result = list(self.entry_set)
820 result.sort()
821 return result
822
823 def width(self) -> CharWidthInTable | None:
824 """If all codepoints in this bucket have the same width, return that width; otherwise,
825 return `None`."""
826 if len(self.widths) == 0:
827 return None
828 potential_width = self.widths[0]
829 for width in self.widths[1:]:
830 if potential_width != width:
831 return None
832 return potential_width
833
834
835 def make_buckets(
836 entries: Iterable[tuple[int, CharWidthInTable]], low_bit: BitPos, cap_bit: BitPos
837 ) -> list[Bucket]:
838 """Partitions the `(Codepoint, EffectiveWidth)` tuples in `entries` into `Bucket`s. All
839 codepoints with identical bits from `low_bit` to `cap_bit` (exclusive) are placed in the
840 same bucket. Returns a list of the buckets in increasing order of those bits."""
841 num_bits = cap_bit - low_bit
842 assert num_bits > 0
843 buckets = [Bucket() for _ in range(0, 2**num_bits)]
844 mask = (1 << num_bits) - 1
845 for codepoint, width in entries:
846 buckets[(codepoint >> low_bit) & mask].append(codepoint, width)
847 return buckets
848
849
850 class Table:
851 """Represents a lookup table. Each table contains a certain number of subtables; each
852 subtable is indexed by a contiguous bit range of the codepoint and contains a list
853 of `2**(number of bits in bit range)` entries. (The bit range is the same for all subtables.)
854
855 Typically, tables contain a list of buckets of codepoints. Bucket `i`'s codepoints should
856 be indexed by sub-table `i` in the next-level lookup table. The entries of this table are
857 indexes into the bucket list (~= indexes into the sub-tables of the next-level table.) The
858 key to compression is that two different buckets in two different sub-tables may have the
859 same width list, which means that they can be merged into the same bucket.
860
861 If no bucket contains two codepoints with different widths, calling `indices_to_widths` will
862 discard the buckets and convert the entries into `EffectiveWidth` values."""
863
864 def __init__(
865 self,
866 name: str,
867 entry_groups: Iterable[Iterable[tuple[int, CharWidthInTable]]],
868 secondary_entry_groups: Iterable[Iterable[tuple[int, CharWidthInTable]]],
869 low_bit: BitPos,
870 cap_bit: BitPos,
871 offset_type: OffsetType,
872 align: int,
873 bytes_per_row: int | None = None,
874 starting_indexed: list[Bucket] = [],
875 cfged: bool = False,
876 ):
877 """Create a lookup table with a sub-table for each `(Codepoint, EffectiveWidth)` iterator
878 in `entry_groups`. Each sub-table is indexed by codepoint bits in `low_bit..cap_bit`,
879 and each table entry is represented in the format specified by `offset_type`. Asserts
880 that this table is actually representable with `offset_type`."""
881 starting_indexed_len = len(starting_indexed)
882 self.name = name
883 self.low_bit = low_bit
884 self.cap_bit = cap_bit
885 self.offset_type = offset_type
886 self.entries: list[int] = []
887 self.indexed: list[Bucket] = list(starting_indexed)
888 self.align = align
889 self.bytes_per_row = bytes_per_row
890 self.cfged = cfged
891
892 buckets: list[Bucket] = []
893 for entries in entry_groups:
894 buckets.extend(make_buckets(entries, self.low_bit, self.cap_bit))
895
896 for bucket in buckets:
897 for i, existing in enumerate(self.indexed):
898 if existing.try_extend(bucket):
899 self.entries.append(i)
900 break
901 else:
902 self.entries.append(len(self.indexed))
903 self.indexed.append(bucket)
904
905 self.primary_len = len(self.entries)
906 self.primary_bucket_len = len(self.indexed)
907
908 buckets = []
909 for entries in secondary_entry_groups:
910 buckets.extend(make_buckets(entries, self.low_bit, self.cap_bit))
911
912 for bucket in buckets:
913 for i, existing in enumerate(self.indexed):
914 if existing.try_extend(bucket):
915 self.entries.append(i)
916 break
917 else:
918 self.entries.append(len(self.indexed))
919 self.indexed.append(bucket)
920
921 # Validate offset type
922 max_index = 1 << int(self.offset_type)
923 for index in self.entries:
924 assert index < max_index, f"{index} <= {max_index}"
925
926 self.indexed = self.indexed[starting_indexed_len:]
927
928 def indices_to_widths(self):
929 """Destructively converts the indices in this table to the `EffectiveWidth` values of
930 their buckets. Assumes that no bucket contains codepoints with different widths.
931 """
932 self.entries = list(map(lambda i: int(self.indexed[i].width()), self.entries)) # type: ignore
933 del self.indexed
934
935 def buckets(self):
936 """Returns an iterator over this table's buckets."""
937 return self.indexed
938
939 def to_bytes(self) -> list[int]:
940 """Returns this table's entries as a list of bytes. The bytes are formatted according to
941 the `OffsetType` which the table was created with, converting any `EffectiveWidth` entries
942 to their enum variant's integer value. For example, with `OffsetType.U2`, each byte will
943 contain four packed 2-bit entries."""
944 entries_per_byte = 8 // int(self.offset_type)
945 byte_array = []
946 for i in range(0, len(self.entries), entries_per_byte):
947 byte = 0
948 for j in range(0, entries_per_byte):
949 byte |= self.entries[i + j] << (j * int(self.offset_type))
950 byte_array.append(byte)
951 return byte_array
952
953
954 def make_tables(
955 width_map: list[WidthState],
956 cjk_width_map: list[WidthState],
957 ) -> list[Table]:
958 """Creates a table for each configuration in `table_cfgs`, with the first config corresponding
959 to the top-level lookup table, the second config corresponding to the second-level lookup
960 table, and so forth. `entries` is an iterator over the `(Codepoint, EffectiveWidth)` pairs
961 to include in the top-level table."""
962
963 entries = enumerate([w.table_width() for w in width_map])
964 cjk_entries = enumerate([w.table_width() for w in cjk_width_map])
965
966 root_table = Table(
967 "WIDTH_ROOT",
968 [entries],
969 [],
970 TABLE_SPLITS[1],
971 MAX_CODEPOINT_BITS,
972 OffsetType.U8,
973 128,
974 )
975
976 cjk_root_table = Table(
977 "WIDTH_ROOT_CJK",
978 [cjk_entries],
979 [],
980 TABLE_SPLITS[1],
981 MAX_CODEPOINT_BITS,
982 OffsetType.U8,
983 128,
984 starting_indexed=root_table.indexed,
985 cfged=True,
986 )
987
988 middle_table = Table(
989 "WIDTH_MIDDLE",
990 map(lambda bucket: bucket.entries(), root_table.buckets()),
991 map(lambda bucket: bucket.entries(), cjk_root_table.buckets()),
992 TABLE_SPLITS[0],
993 TABLE_SPLITS[1],
994 OffsetType.U8,
995 2 ** (TABLE_SPLITS[1] - TABLE_SPLITS[0]),
996 bytes_per_row=2 ** (TABLE_SPLITS[1] - TABLE_SPLITS[0]),
997 )
998
999 leaves_table = Table(
1000 "WIDTH_LEAVES",
1001 map(
1002 lambda bucket: bucket.entries(),
1003 middle_table.buckets()[: middle_table.primary_bucket_len],
1004 ),
1005 map(
1006 lambda bucket: bucket.entries(),
1007 middle_table.buckets()[middle_table.primary_bucket_len :],
1008 ),
1009 0,
1010 TABLE_SPLITS[0],
1011 OffsetType.U2,
1012 2 ** (TABLE_SPLITS[0] - 2),
1013 bytes_per_row=2 ** (TABLE_SPLITS[0] - 2),
1014 )
1015
1016 return [root_table, cjk_root_table, middle_table, leaves_table]
1017
1018
1019 def load_emoji_presentation_sequences() -> list[Codepoint]:
1020 """Outputs a list of cpodepoints, corresponding to all the valid characters for starting
1021 an emoji presentation sequence."""
1022
1023 with fetch_open("emoji/emoji-variation-sequences.txt") as sequences:
1024 # Match all emoji presentation sequences
1025 # (one codepoint followed by U+FE0F, and labeled "emoji style")
1026 sequence = re.compile(r"^([0-9A-F]+)\s+FE0F\s*;\s*emoji style")
1027 codepoints = []
1028 for line in sequences.readlines():
1029 if match := sequence.match(line):
1030 cp = int(match.group(1), 16)
1031 codepoints.append(cp)
1032 return codepoints
1033
1034
1035 def load_text_presentation_sequences() -> list[Codepoint]:
1036 """Outputs a list of codepoints, corresponding to all the valid characters
1037 whose widths change with a text presentation sequence."""
1038
1039 text_presentation_seq_codepoints = set()
1040 with fetch_open("emoji/emoji-variation-sequences.txt") as sequences:
1041 # Match all text presentation sequences
1042 # (one codepoint followed by U+FE0E, and labeled "text style")
1043 sequence = re.compile(r"^([0-9A-F]+)\s+FE0E\s*;\s*text style")
1044 for line in sequences.readlines():
1045 if match := sequence.match(line):
1046 cp = int(match.group(1), 16)
1047 text_presentation_seq_codepoints.add(cp)
1048
1049 default_emoji_codepoints = set()
1050
1051 load_property(
1052 "emoji/emoji-data.txt",
1053 "Emoji_Presentation",
1054 lambda cp: default_emoji_codepoints.add(cp),
1055 )
1056
1057 codepoints = []
1058 for cp in text_presentation_seq_codepoints.intersection(default_emoji_codepoints):
1059 # "Enclosed Ideographic Supplement" block;
1060 # wide even in text presentation
1061 if not cp in range(0x1F200, 0x1F300):
1062 codepoints.append(cp)
1063
1064 codepoints.sort()
1065 return codepoints
1066
1067
1068 def load_emoji_modifier_bases() -> list[Codepoint]:
1069 """Outputs a list of codepoints, corresponding to all the valid characters
1070 whose widths change with a text presentation sequence."""
1071
1072 ret = []
1073 load_property(
1074 "emoji/emoji-data.txt",
1075 "Emoji_Modifier_Base",
1076 lambda cp: ret.append(cp),
1077 )
1078 ret.sort()
1079 return ret
1080
1081
1082 def make_presentation_sequence_table(
1083 seqs: list[Codepoint],
1084 lsb: int = 10,
1085 ) -> tuple[list[tuple[int, int]], list[list[int]]]:
1086 """Generates 2-level lookup table for whether a codepoint might start an emoji variation sequence.
1087 The first level is a match on all but the 10 LSB, the second level is a 1024-bit bitmap for those 10 LSB.
1088 """
1089
1090 prefixes_dict = defaultdict(set)
1091 for cp in seqs:
1092 prefixes_dict[cp >> lsb].add(cp & (2**lsb - 1))
1093
1094 msbs: list[int] = list(prefixes_dict.keys())
1095
1096 leaves: list[list[int]] = []
1097 for cps in prefixes_dict.values():
1098 leaf = [0] * (2 ** (lsb - 3))
1099 for cp in cps:
1100 idx_in_leaf, bit_shift = divmod(cp, 8)
1101 leaf[idx_in_leaf] |= 1 << bit_shift
1102 leaves.append(leaf)
1103
1104 indexes = [(msb, index) for (index, msb) in enumerate(msbs)]
1105
1106 # Cull duplicate leaves
1107 i = 0
1108 while i < len(leaves):
1109 first_idx = leaves.index(leaves[i])
1110 if first_idx == i:
1111 i += 1
1112 else:
1113 for j in range(0, len(indexes)):
1114 if indexes[j][1] == i:
1115 indexes[j] = (indexes[j][0], first_idx)
1116 elif indexes[j][1] > i:
1117 indexes[j] = (indexes[j][0], indexes[j][1] - 1)
1118
1119 leaves.pop(i)
1120
1121 return (indexes, leaves)
1122
1123
1124 def make_ranges_table(
1125 seqs: list[Codepoint],
1126 ) -> tuple[list[tuple[int, int]], list[list[tuple[int, int]]]]:
1127 """Generates 2-level lookup table for a binary property of a codepoint.
1128 First level is all but the last byte, second level is ranges for last byte
1129 """
1130
1131 prefixes_dict = defaultdict(list)
1132 for cp in seqs:
1133 prefixes_dict[cp >> 8].append(cp & 0xFF)
1134
1135 msbs: list[int] = list(prefixes_dict.keys())
1136
1137 leaves: list[list[tuple[int, int]]] = []
1138 for cps in prefixes_dict.values():
1139 leaf = []
1140 for cp in cps:
1141 if len(leaf) > 0 and leaf[-1][1] == cp - 1:
1142 leaf[-1] = (leaf[-1][0], cp)
1143 else:
1144 leaf.append((cp, cp))
1145 leaves.append(leaf)
1146
1147 indexes = [(msb, index) for (index, msb) in enumerate(msbs)]
1148
1149 # Cull duplicate leaves
1150 i = 0
1151 while i < len(leaves):
1152 first_idx = leaves.index(leaves[i])
1153 if first_idx == i:
1154 i += 1
1155 else:
1156 for j in range(0, len(indexes)):
1157 if indexes[j][1] == i:
1158 indexes[j] = (indexes[j][0], first_idx)
1159 elif indexes[j][1] > i:
1160 indexes[j] = (indexes[j][0], indexes[j][1] - 1)
1161
1162 leaves.pop(i)
1163
1164 return (indexes, leaves)
1165
1166
1167 def lookup_fns(
1168 is_cjk: bool,
1169 special_ranges: list[tuple[tuple[Codepoint, Codepoint], WidthState]],
1170 joining_group_lam: list[tuple[Codepoint, Codepoint]],
1171 ) -> str:
1172 if is_cjk:
1173 cfg = '#[cfg(feature = "cjk")]\n'
1174 cjk_lo = "_cjk"
1175 cjk_cap = "_CJK"
1176 ambig = "wide"
1177 else:
1178 cfg = ""
1179 cjk_lo = ""
1180 cjk_cap = ""
1181 ambig = "narrow"
1182 s = f"""
1183 /// Returns the [UAX #11](https://www.unicode.org/reports/tr11/) based width of `c` by
1184 /// consulting a multi-level lookup table.
1185 ///
1186 /// # Maintenance
1187 /// The tables themselves are autogenerated but this function is hardcoded. You should have
1188 /// nothing to worry about if you re-run `unicode.py` (for example, when updating Unicode.)
1189 /// However, if you change the *actual structure* of the lookup tables (perhaps by editing the
1190 /// `make_tables` function in `unicode.py`) you must ensure that this code reflects those changes.
1191 {cfg}#[inline]
1192 fn lookup_width{cjk_lo}(c: char) -> (u8, WidthInfo) {{
1193 let cp = c as usize;
1194
1195 let t1_offset = WIDTH_ROOT{cjk_cap}.0[cp >> {TABLE_SPLITS[1]}];
1196
1197 // Each sub-table in WIDTH_MIDDLE is 7 bits, and each stored entry is a byte,
1198 // so each sub-table is 128 bytes in size.
1199 // (Sub-tables are selected using the computed offset from the previous table.)
1200 let t2_offset = WIDTH_MIDDLE.0[usize::from(t1_offset)][cp >> {TABLE_SPLITS[0]} & 0x{(2 ** (TABLE_SPLITS[1] - TABLE_SPLITS[0]) - 1):X}];
1201
1202 // Each sub-table in WIDTH_LEAVES is 6 bits, but each stored entry is 2 bits.
1203 // This is accomplished by packing four stored entries into one byte.
1204 // So each sub-table is 2**(7-2) == 32 bytes in size.
1205 // Since this is the last table, each entry represents an encoded width.
1206 let packed_widths = WIDTH_LEAVES.0[usize::from(t2_offset)][cp >> 2 & 0x{(2 ** (TABLE_SPLITS[0] - 2) - 1):X}];
1207
1208 // Extract the packed width
1209 let width = packed_widths >> (2 * (cp & 0b11)) & 0b11;
1210
1211 if width < 3 {{
1212 (width, WidthInfo::DEFAULT)
1213 }} else {{
1214 match c {{
1215 """
1216
1217 for (lo, hi), width in special_ranges:
1218 s += f" '\\u{{{lo:X}}}'"
1219 if hi != lo:
1220 s += f"..='\\u{{{hi:X}}}'"
1221 if width.is_carried():
1222 width_info = width.name
1223 else:
1224 width_info = "DEFAULT"
1225 s += f" => ({width.width_alone()}, WidthInfo::{width_info}),\n"
1226
1227 s += f""" _ => (2, WidthInfo::EMOJI_PRESENTATION),
1228 }}
1229 }}
1230 }}
1231
1232 /// Returns the [UAX #11](https://www.unicode.org/reports/tr11/) based width of `c`, or
1233 /// `None` if `c` is a control character.
1234 /// Ambiguous width characters are treated as {ambig}.
1235 {cfg}#[inline]
1236 pub fn single_char_width{cjk_lo}(c: char) -> Option<usize> {{
1237 if c < '\\u{{7F}}' {{
1238 if c >= '\\u{{20}}' {{
1239 // U+0020 to U+007F (exclusive) are single-width ASCII codepoints
1240 Some(1)
1241 }} else {{
1242 // U+0000 to U+0020 (exclusive) are control codes
1243 None
1244 }}
1245 }} else if c >= '\\u{{A0}}' {{
1246 // No characters >= U+00A0 are control codes, so we can consult the lookup tables
1247 Some(lookup_width{cjk_lo}(c).0.into())
1248 }} else {{
1249 // U+007F to U+00A0 (exclusive) are control codes
1250 None
1251 }}
1252 }}
1253
1254 /// Returns the [UAX #11](https://www.unicode.org/reports/tr11/) based width of `c`.
1255 /// Ambiguous width characters are treated as {ambig}.
1256 {cfg}#[inline]
1257 fn width_in_str{cjk_lo}(c: char, mut next_info: WidthInfo) -> (i8, WidthInfo) {{
1258 if next_info.is_emoji_presentation() {{
1259 if starts_emoji_presentation_seq(c) {{
1260 let width = if next_info.is_zwj_emoji_presentation() {{
1261 0
1262 }} else {{
1263 2
1264 }};
1265 return (width, WidthInfo::EMOJI_PRESENTATION);
1266 }} else {{
1267 next_info = next_info.unset_emoji_presentation();
1268 }}
1269 }}"""
1270
1271 if is_cjk:
1272 s += """
1273 if (matches!(
1274 next_info,
1275 WidthInfo::COMBINING_LONG_SOLIDUS_OVERLAY | WidthInfo::SOLIDUS_OVERLAY_ALEF
1276 ) && matches!(c, '<' | '=' | '>'))
1277 {
1278 return (2, WidthInfo::DEFAULT);
1279 }"""
1280
1281 s += """
1282 if c <= '\\u{A0}' {
1283 match c {
1284 '\\n' => (1, WidthInfo::LINE_FEED),
1285 '\\r' if next_info == WidthInfo::LINE_FEED => (0, WidthInfo::DEFAULT),
1286 _ => (1, WidthInfo::DEFAULT),
1287 }
1288 } else {
1289 // Fast path
1290 if next_info != WidthInfo::DEFAULT {
1291 if c == '\\u{FE0F}' {
1292 return (0, next_info.set_emoji_presentation());
1293 }"""
1294
1295 if not is_cjk:
1296 s += """
1297 if c == '\\u{FE0E}' {
1298 return (0, next_info.set_text_presentation());
1299 }
1300 if next_info.is_text_presentation() {
1301 if starts_non_ideographic_text_presentation_seq(c) {
1302 return (1, WidthInfo::DEFAULT);
1303 } else {
1304 next_info = next_info.unset_text_presentation();
1305 }
1306 }"""
1307
1308 s += """
1309 if next_info.is_ligature_transparent() {
1310 if c == '\\u{200D}' {
1311 return (0, next_info.set_zwj_bit());
1312 } else if is_ligature_transparent(c) {
1313 return (0, next_info);
1314 }
1315 }
1316
1317 match (next_info, c) {"""
1318 if is_cjk:
1319 s += """
1320 (WidthInfo::COMBINING_LONG_SOLIDUS_OVERLAY, _) if is_solidus_transparent(c) => {
1321 return (
1322 lookup_width_cjk(c).0 as i8,
1323 WidthInfo::COMBINING_LONG_SOLIDUS_OVERLAY,
1324 );
1325 }
1326 (WidthInfo::JOINING_GROUP_ALEF, '\\u{0338}') => {
1327 return (0, WidthInfo::SOLIDUS_OVERLAY_ALEF);
1328 }
1329 // Arabic Lam-Alef ligature
1330 (
1331 WidthInfo::JOINING_GROUP_ALEF | WidthInfo::SOLIDUS_OVERLAY_ALEF,
1332 """
1333 else:
1334 s += """
1335 // Arabic Lam-Alef ligature
1336 (
1337 WidthInfo::JOINING_GROUP_ALEF,
1338 """
1339
1340 tail = False
1341 for lo, hi in joining_group_lam:
1342 if tail:
1343 s += " | "
1344 tail = True
1345 s += f"'\\u{{{lo:X}}}'"
1346 if hi != lo:
1347 s += f"..='\\u{{{hi:X}}}'"
1348 s += """,
1349 ) => return (0, WidthInfo::DEFAULT),
1350 (WidthInfo::JOINING_GROUP_ALEF, _) if is_transparent_zero_width(c) => {
1351 return (0, WidthInfo::JOINING_GROUP_ALEF);
1352 }
1353
1354 // Hebrew Alef-ZWJ-Lamed ligature
1355 (WidthInfo::ZWJ_HEBREW_LETTER_LAMED, '\\u{05D0}') => {
1356 return (0, WidthInfo::DEFAULT);
1357 }
1358
1359 // Khmer coeng signs
1360 (WidthInfo::KHMER_COENG_ELIGIBLE_LETTER, '\\u{17D2}') => {
1361 return (-1, WidthInfo::DEFAULT);
1362 }
1363
1364 // Buginese <a, -i> ZWJ ya ligature
1365 (WidthInfo::ZWJ_BUGINESE_LETTER_YA, '\\u{1A17}') => {
1366 return (0, WidthInfo::BUGINESE_VOWEL_SIGN_I_ZWJ_LETTER_YA)
1367 }
1368 (WidthInfo::BUGINESE_VOWEL_SIGN_I_ZWJ_LETTER_YA, '\\u{1A15}') => {
1369 return (0, WidthInfo::DEFAULT)
1370 }
1371
1372 // Tifinagh bi-consonants
1373 (WidthInfo::TIFINAGH_CONSONANT | WidthInfo::ZWJ_TIFINAGH_CONSONANT, '\\u{2D7F}') => {
1374 return (1, WidthInfo::TIFINAGH_JOINER_CONSONANT);
1375 }
1376 (WidthInfo::ZWJ_TIFINAGH_CONSONANT, '\\u{2D31}'..='\\u{2D65}' | '\\u{2D6F}') => {
1377 return (0, WidthInfo::DEFAULT);
1378 }
1379 (WidthInfo::TIFINAGH_JOINER_CONSONANT, '\\u{2D31}'..='\\u{2D65}' | '\\u{2D6F}') => {
1380 return (-1, WidthInfo::DEFAULT);
1381 }
1382
1383 // Lisu tone letter combinations
1384 (WidthInfo::LISU_TONE_LETTER_MYA_NA_JEU, '\\u{A4F8}'..='\\u{A4FB}') => {
1385 return (0, WidthInfo::DEFAULT);
1386 }
1387
1388 // Old Turkic ligature
1389 (WidthInfo::ZWJ_OLD_TURKIC_LETTER_ORKHON_I, '\\u{10C32}') => {
1390 return (0, WidthInfo::DEFAULT);
1391 }"""
1392
1393 s += f"""
1394 // Emoji modifier
1395 (WidthInfo::EMOJI_MODIFIER, _) if is_emoji_modifier_base(c) => {{
1396 return (0, WidthInfo::EMOJI_PRESENTATION);
1397 }}
1398
1399 // Regional indicator
1400 (
1401 WidthInfo::REGIONAL_INDICATOR | WidthInfo::SEVERAL_REGIONAL_INDICATOR,
1402 '\\u{{1F1E6}}'..='\\u{{1F1FF}}',
1403 ) => return (1, WidthInfo::SEVERAL_REGIONAL_INDICATOR),
1404
1405 // ZWJ emoji
1406 (
1407 WidthInfo::EMOJI_PRESENTATION
1408 | WidthInfo::SEVERAL_REGIONAL_INDICATOR
1409 | WidthInfo::EVEN_REGIONAL_INDICATOR_ZWJ_PRESENTATION
1410 | WidthInfo::ODD_REGIONAL_INDICATOR_ZWJ_PRESENTATION
1411 | WidthInfo::EMOJI_MODIFIER,
1412 '\\u{{200D}}',
1413 ) => return (0, WidthInfo::ZWJ_EMOJI_PRESENTATION),
1414 (WidthInfo::ZWJ_EMOJI_PRESENTATION, '\\u{{20E3}}') => {{
1415 return (0, WidthInfo::KEYCAP_ZWJ_EMOJI_PRESENTATION);
1416 }}
1417 (WidthInfo::VS16_ZWJ_EMOJI_PRESENTATION, _) if starts_emoji_presentation_seq(c) => {{
1418 return (0, WidthInfo::EMOJI_PRESENTATION)
1419 }}
1420 (WidthInfo::VS16_KEYCAP_ZWJ_EMOJI_PRESENTATION, '0'..='9' | '#' | '*') => {{
1421 return (0, WidthInfo::EMOJI_PRESENTATION)
1422 }}
1423 (WidthInfo::ZWJ_EMOJI_PRESENTATION, '\\u{{1F1E6}}'..='\\u{{1F1FF}}') => {{
1424 return (1, WidthInfo::REGIONAL_INDICATOR_ZWJ_PRESENTATION);
1425 }}
1426 (
1427 WidthInfo::REGIONAL_INDICATOR_ZWJ_PRESENTATION
1428 | WidthInfo::ODD_REGIONAL_INDICATOR_ZWJ_PRESENTATION,
1429 '\\u{{1F1E6}}'..='\\u{{1F1FF}}',
1430 ) => return (-1, WidthInfo::EVEN_REGIONAL_INDICATOR_ZWJ_PRESENTATION),
1431 (
1432 WidthInfo::EVEN_REGIONAL_INDICATOR_ZWJ_PRESENTATION,
1433 '\\u{{1F1E6}}'..='\\u{{1F1FF}}',
1434 ) => return (3, WidthInfo::ODD_REGIONAL_INDICATOR_ZWJ_PRESENTATION),
1435 (WidthInfo::ZWJ_EMOJI_PRESENTATION, '\\u{{1F3FB}}'..='\\u{{1F3FF}}') => {{
1436 return (0, WidthInfo::EMOJI_MODIFIER);
1437 }}
1438 (WidthInfo::ZWJ_EMOJI_PRESENTATION, '\\u{{E007F}}') => {{
1439 return (0, WidthInfo::TAG_END_ZWJ_EMOJI_PRESENTATION);
1440 }}
1441 (WidthInfo::TAG_END_ZWJ_EMOJI_PRESENTATION, '\\u{{E0061}}'..='\\u{{E007A}}') => {{
1442 return (0, WidthInfo::TAG_A1_END_ZWJ_EMOJI_PRESENTATION);
1443 }}
1444 (WidthInfo::TAG_A1_END_ZWJ_EMOJI_PRESENTATION, '\\u{{E0061}}'..='\\u{{E007A}}') => {{
1445 return (0, WidthInfo::TAG_A2_END_ZWJ_EMOJI_PRESENTATION)
1446 }}
1447 (WidthInfo::TAG_A2_END_ZWJ_EMOJI_PRESENTATION, '\\u{{E0061}}'..='\\u{{E007A}}') => {{
1448 return (0, WidthInfo::TAG_A3_END_ZWJ_EMOJI_PRESENTATION)
1449 }}
1450 (WidthInfo::TAG_A3_END_ZWJ_EMOJI_PRESENTATION, '\\u{{E0061}}'..='\\u{{E007A}}') => {{
1451 return (0, WidthInfo::TAG_A4_END_ZWJ_EMOJI_PRESENTATION)
1452 }}
1453 (WidthInfo::TAG_A4_END_ZWJ_EMOJI_PRESENTATION, '\\u{{E0061}}'..='\\u{{E007A}}') => {{
1454 return (0, WidthInfo::TAG_A5_END_ZWJ_EMOJI_PRESENTATION)
1455 }}
1456 (WidthInfo::TAG_A5_END_ZWJ_EMOJI_PRESENTATION, '\\u{{E0061}}'..='\\u{{E007A}}') => {{
1457 return (0, WidthInfo::TAG_A6_END_ZWJ_EMOJI_PRESENTATION)
1458 }}
1459 (
1460 WidthInfo::TAG_END_ZWJ_EMOJI_PRESENTATION
1461 | WidthInfo::TAG_A1_END_ZWJ_EMOJI_PRESENTATION
1462 | WidthInfo::TAG_A2_END_ZWJ_EMOJI_PRESENTATION
1463 | WidthInfo::TAG_A3_END_ZWJ_EMOJI_PRESENTATION
1464 | WidthInfo::TAG_A4_END_ZWJ_EMOJI_PRESENTATION,
1465 '\\u{{E0030}}'..='\\u{{E0039}}',
1466 ) => return (0, WidthInfo::TAG_D1_END_ZWJ_EMOJI_PRESENTATION),
1467 (WidthInfo::TAG_D1_END_ZWJ_EMOJI_PRESENTATION, '\\u{{E0030}}'..='\\u{{E0039}}') => {{
1468 return (0, WidthInfo::TAG_D2_END_ZWJ_EMOJI_PRESENTATION);
1469 }}
1470 (WidthInfo::TAG_D2_END_ZWJ_EMOJI_PRESENTATION, '\\u{{E0030}}'..='\\u{{E0039}}') => {{
1471 return (0, WidthInfo::TAG_D3_END_ZWJ_EMOJI_PRESENTATION);
1472 }}
1473 (
1474 WidthInfo::TAG_A3_END_ZWJ_EMOJI_PRESENTATION
1475 | WidthInfo::TAG_A4_END_ZWJ_EMOJI_PRESENTATION
1476 | WidthInfo::TAG_A5_END_ZWJ_EMOJI_PRESENTATION
1477 | WidthInfo::TAG_A6_END_ZWJ_EMOJI_PRESENTATION
1478 | WidthInfo::TAG_D3_END_ZWJ_EMOJI_PRESENTATION,
1479 '\\u{{1F3F4}}',
1480 ) => return (0, WidthInfo::EMOJI_PRESENTATION),
1481 (WidthInfo::ZWJ_EMOJI_PRESENTATION, _)
1482 if lookup_width{cjk_lo}(c).1 == WidthInfo::EMOJI_PRESENTATION =>
1483 {{
1484 return (0, WidthInfo::EMOJI_PRESENTATION)
1485 }}
1486
1487 // Fallback
1488 _ => {{}}
1489 }}
1490 }}
1491
1492 let ret = lookup_width{cjk_lo}(c);
1493 (ret.0 as i8, ret.1)
1494 }}
1495 }}
1496
1497 {cfg}#[inline]
1498 pub fn str_width{cjk_lo}(s: &str) -> usize {{
1499 s.chars()
1500 .rfold(
1501 (0, WidthInfo::DEFAULT),
1502 |(sum, next_info), c| -> (usize, WidthInfo) {{
1503 let (add, info) = width_in_str{cjk_lo}(c, next_info);
1504 (sum.wrapping_add_signed(isize::from(add)), info)
1505 }},
1506 )
1507 .0
1508 }}
1509 """
1510
1511 return s
1512
1513
1514 def emit_module(
1515 out_name: str,
1516 unicode_version: tuple[int, int, int],
1517 tables: list[Table],
1518 special_ranges: list[tuple[tuple[Codepoint, Codepoint], WidthState]],
1519 special_ranges_cjk: list[tuple[tuple[Codepoint, Codepoint], WidthState]],
1520 emoji_presentation_table: tuple[list[tuple[int, int]], list[list[int]]],
1521 text_presentation_table: tuple[list[tuple[int, int]], list[list[tuple[int, int]]]],
1522 emoji_modifier_table: tuple[list[tuple[int, int]], list[list[tuple[int, int]]]],
1523 joining_group_lam: list[tuple[Codepoint, Codepoint]],
1524 non_transparent_zero_widths: list[tuple[Codepoint, Codepoint]],
1525 ligature_transparent: list[tuple[Codepoint, Codepoint]],
1526 solidus_transparent: list[tuple[Codepoint, Codepoint]],
1527 normalization_tests: list[tuple[str, str, str, str, str]],
1528 ):
1529 """Outputs a Rust module to `out_name` using table data from `tables`.
1530 If `TABLE_CFGS` is edited, you may need to edit the included code for `lookup_width`.
1531 """
1532 if os.path.exists(out_name):
1533 os.remove(out_name)
1534 with open(out_name, "w", newline="\n", encoding="utf-8") as module:
1535 module.write(
1536 """// Copyright 2012-2022 The Rust Project Developers. See the COPYRIGHT
1537 // file at the top-level directory of this distribution and at
1538 // http://rust-lang.org/COPYRIGHT.
1539 //
1540 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
1541 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
1542 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
1543 // option. This file may not be copied, modified, or distributed
1544 // except according to those terms.
1545
1546 // NOTE: The following code was generated by "scripts/unicode.py", do not edit directly
1547
1548 use core::cmp::Ordering;
1549
1550 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
1551 struct WidthInfo(u16);
1552
1553 impl WidthInfo {
1554 /// No special handling necessary
1555 const DEFAULT: Self = Self(0);
1556 """
1557 )
1558
1559 for variant in WidthState:
1560 if variant.is_carried():
1561 if variant.is_cjk_only():
1562 module.write(' #[cfg(feature = "cjk")]\n')
1563 module.write(
1564 f" const {variant.name}: Self = Self(0b{variant.value:016b});\n"
1565 )
1566
1567 module.write(
1568 f"""
1569 /// Whether this width mode is ligature_transparent
1570 /// (has 5th MSB set.)
1571 fn is_ligature_transparent(self) -> bool {{
1572 (self.0 & 0b0000_1000_0000_0000) == 0b0000_1000_0000_0000
1573 }}
1574
1575 /// Sets 6th MSB.
1576 fn set_zwj_bit(self) -> Self {{
1577 Self(self.0 | 0b0000_0100_0000_0000)
1578 }}
1579
1580 /// Has top bit set
1581 fn is_emoji_presentation(self) -> bool {{
1582 (self.0 & 0b1000_0000_0000_0000) == 0b1000_0000_0000_0000
1583 }}
1584
1585 /// Has top bit set
1586 fn is_zwj_emoji_presentation(self) -> bool {{
1587 (self.0 & 0b1011_0000_0000_0000) == 0b1001_0000_0000_0000
1588 }}
1589
1590 /// Set top bit
1591 fn set_emoji_presentation(self) -> Self {{
1592 if (self.0 & 0b0010_0000_0000_0000) == 0b0010_0000_0000_0000
1593 || (self.0 & 0b1001_0000_0000_0000) == 0b0001_0000_0000_0000
1594 {{
1595 Self(self.0 | 0b1000_0000_0000_0000)
1596 }} else {{
1597 Self::VARIATION_SELECTOR_16
1598 }}
1599 }}
1600
1601 /// Clear top bit
1602 fn unset_emoji_presentation(self) -> Self {{
1603 if (self.0 & 0b0010_0000_0000_0000) == 0b0010_0000_0000_0000 {{
1604 Self(self.0 & 0b0111_1111_1111_1111)
1605 }} else {{
1606 Self::DEFAULT
1607 }}
1608 }}
1609
1610 /// Has 2nd bit set
1611 fn is_text_presentation(self) -> bool {{
1612 (self.0 & 0b0100_0000_0000_0000) == 0b0100_0000_0000_0000
1613 }}
1614
1615 /// Set 2nd bit
1616 fn set_text_presentation(self) -> Self {{
1617 if (self.0 & 0b0010_0000_0000_0000) == 0b0010_0000_0000_0000 {{
1618 Self(self.0 | 0b0100_0000_0000_0000)
1619 }} else {{
1620 Self(0b0100_0000_0000_0000)
1621 }}
1622 }}
1623
1624 /// Clear 2nd bit
1625 fn unset_text_presentation(self) -> Self {{
1626 Self(self.0 & 0b1011_1111_1111_1111)
1627 }}
1628 }}
1629
1630 /// The version of [Unicode](http://www.unicode.org/)
1631 /// that this version of unicode-width is based on.
1632 pub const UNICODE_VERSION: (u8, u8, u8) = {unicode_version};
1633 """
1634 )
1635
1636 module.write(lookup_fns(False, special_ranges, joining_group_lam))
1637 module.write(lookup_fns(True, special_ranges_cjk, joining_group_lam))
1638
1639 emoji_presentation_idx, emoji_presentation_leaves = emoji_presentation_table
1640 text_presentation_idx, text_presentation_leaves = text_presentation_table
1641 emoji_modifier_idx, emoji_modifier_leaves = emoji_modifier_table
1642
1643 module.write(
1644 """
1645 /// Whether this character is a zero-width character with
1646 /// `Joining_Type=Transparent`. Used by the Alef-Lamed ligatures.
1647 /// See also [`is_ligature_transparent`], a near-subset of this (only ZWJ is excepted)
1648 /// which is transparent for non-Arabic ligatures.
1649 fn is_transparent_zero_width(c: char) -> bool {
1650 if lookup_width(c).0 != 0 {
1651 // Not zero-width
1652 false
1653 } else {
1654 let cp: u32 = c.into();
1655 NON_TRANSPARENT_ZERO_WIDTHS
1656 .binary_search_by(|&(lo, hi)| {
1657 let lo = u32::from_le_bytes([lo[0], lo[1], lo[2], 0]);
1658 let hi = u32::from_le_bytes([hi[0], hi[1], hi[2], 0]);
1659 if cp < lo {
1660 Ordering::Greater
1661 } else if cp > hi {
1662 Ordering::Less
1663 } else {
1664 Ordering::Equal
1665 }
1666 })
1667 .is_err()
1668 }
1669 }
1670
1671 /// Whether this character is a default-ignorable combining mark
1672 /// or ZWJ. These characters won't interrupt non-Arabic ligatures.
1673 fn is_ligature_transparent(c: char) -> bool {
1674 matches!(c, """
1675 )
1676
1677 tail = False
1678 for lo, hi in ligature_transparent:
1679 if tail:
1680 module.write(" | ")
1681 tail = True
1682 module.write(f"'\\u{{{lo:X}}}'")
1683 if hi != lo:
1684 module.write(f"..='\\u{{{hi:X}}}'")
1685
1686 module.write(
1687 """)
1688 }
1689
1690 /// Whether this character is transparent wrt the effect of
1691 /// U+0338 COMBINING LONG SOLIDUS OVERLAY
1692 /// on its base character.
1693 #[cfg(feature = "cjk")]
1694 fn is_solidus_transparent(c: char) -> bool {
1695 let cp: u32 = c.into();
1696 is_ligature_transparent(c)
1697 || SOLIDUS_TRANSPARENT
1698 .binary_search_by(|&(lo, hi)| {
1699 let lo = u32::from_le_bytes([lo[0], lo[1], lo[2], 0]);
1700 let hi = u32::from_le_bytes([hi[0], hi[1], hi[2], 0]);
1701 if cp < lo {
1702 Ordering::Greater
1703 } else if cp > hi {
1704 Ordering::Less
1705 } else {
1706 Ordering::Equal
1707 }
1708 })
1709 .is_ok()
1710 }
1711
1712 /// Whether this character forms an [emoji presentation sequence]
1713 /// (https://www.unicode.org/reports/tr51/#def_emoji_presentation_sequence)
1714 /// when followed by `'\\u{FEOF}'`.
1715 /// Emoji presentation sequences are considered to have width 2.
1716 #[inline]
1717 pub fn starts_emoji_presentation_seq(c: char) -> bool {
1718 let cp: u32 = c.into();
1719 // First level of lookup uses all but 10 LSB
1720 let top_bits = cp >> 10;
1721 let idx_of_leaf: usize = match top_bits {
1722 """
1723 )
1724
1725 for msbs, i in emoji_presentation_idx:
1726 module.write(f" 0x{msbs:X} => {i},\n")
1727
1728 module.write(
1729 """ _ => return false,
1730 };
1731 // Extract the 3-9th (0-indexed) least significant bits of `cp`,
1732 // and use them to index into `leaf_row`.
1733 let idx_within_leaf = usize::try_from((cp >> 3) & 0x7F).unwrap();
1734 let leaf_byte = EMOJI_PRESENTATION_LEAVES.0[idx_of_leaf][idx_within_leaf];
1735 // Use the 3 LSB of `cp` to index into `leaf_byte`.
1736 ((leaf_byte >> (cp & 7)) & 1) == 1
1737 }
1738
1739 /// Returns `true` if `c` has default emoji presentation, but forms a [text presentation sequence]
1740 /// (https://www.unicode.org/reports/tr51/#def_text_presentation_sequence)
1741 /// when followed by `'\\u{FEOE}'`, and is not ideographic.
1742 /// Such sequences are considered to have width 1.
1743 #[inline]
1744 pub fn starts_non_ideographic_text_presentation_seq(c: char) -> bool {
1745 let cp: u32 = c.into();
1746 // First level of lookup uses all but 8 LSB
1747 let top_bits = cp >> 8;
1748 let leaf: &[(u8, u8)] = match top_bits {
1749 """
1750 )
1751
1752 for msbs, i in text_presentation_idx:
1753 module.write(f" 0x{msbs:X} => &TEXT_PRESENTATION_LEAF_{i},\n")
1754
1755 module.write(
1756 """ _ => return false,
1757 };
1758
1759 let bottom_bits = (cp & 0xFF) as u8;
1760 leaf.binary_search_by(|&(lo, hi)| {
1761 if bottom_bits < lo {
1762 Ordering::Greater
1763 } else if bottom_bits > hi {
1764 Ordering::Less
1765 } else {
1766 Ordering::Equal
1767 }
1768 })
1769 .is_ok()
1770 }
1771
1772 /// Returns `true` if `c` is an `Emoji_Modifier_Base`.
1773 #[inline]
1774 pub fn is_emoji_modifier_base(c: char) -> bool {
1775 let cp: u32 = c.into();
1776 // First level of lookup uses all but 8 LSB
1777 let top_bits = cp >> 8;
1778 let leaf: &[(u8, u8)] = match top_bits {
1779 """
1780 )
1781
1782 for msbs, i in emoji_modifier_idx:
1783 module.write(f" 0x{msbs:X} => &EMOJI_MODIFIER_LEAF_{i},\n")
1784
1785 module.write(
1786 """ _ => return false,
1787 };
1788
1789 let bottom_bits = (cp & 0xFF) as u8;
1790 leaf.binary_search_by(|&(lo, hi)| {
1791 if bottom_bits < lo {
1792 Ordering::Greater
1793 } else if bottom_bits > hi {
1794 Ordering::Less
1795 } else {
1796 Ordering::Equal
1797 }
1798 })
1799 .is_ok()
1800 }
1801
1802 #[repr(align(32))]
1803 struct Align32<T>(T);
1804
1805 #[repr(align(64))]
1806 struct Align64<T>(T);
1807
1808 #[repr(align(128))]
1809 struct Align128<T>(T);
1810 """
1811 )
1812
1813 subtable_count = 1
1814 for i, table in enumerate(tables):
1815 new_subtable_count = len(table.buckets())
1816 if i == len(tables) - 1:
1817 table.indices_to_widths() # for the last table, indices == widths
1818 byte_array = table.to_bytes()
1819
1820 if table.bytes_per_row is None:
1821 module.write(
1822 f"/// Autogenerated. {subtable_count} sub-table(s). Consult [`lookup_width`] for layout info.)\n"
1823 )
1824 if table.cfged:
1825 module.write('#[cfg(feature = "cjk")]\n')
1826 module.write(
1827 f"static {table.name}: Align{table.align}<[u8; {len(byte_array)}]> = Align{table.align}(["
1828 )
1829 for j, byte in enumerate(byte_array):
1830 # Add line breaks for every 15th entry (chosen to match what rustfmt does)
1831 if j % 16 == 0:
1832 module.write("\n ")
1833 module.write(f" 0x{byte:02X},")
1834 module.write("\n")
1835 else:
1836 num_rows = len(byte_array) // table.bytes_per_row
1837 num_primary_rows = (
1838 table.primary_len
1839 // (8 // int(table.offset_type))
1840 // table.bytes_per_row
1841 )
1842 module.write(
1843 f"""
1844 #[cfg(feature = "cjk")]
1845 const {table.name}_LEN: usize = {num_rows};
1846 #[cfg(not(feature = "cjk"))]
1847 const {table.name}_LEN: usize = {num_primary_rows};
1848 /// Autogenerated. {subtable_count} sub-table(s). Consult [`lookup_width`] for layout info.
1849 static {table.name}: Align{table.align}<[[u8; {table.bytes_per_row}]; {table.name}_LEN]> = Align{table.align}([\n"""
1850 )
1851 for row_num in range(0, num_rows):
1852 if row_num >= num_primary_rows:
1853 module.write(' #[cfg(feature = "cjk")]\n')
1854 module.write(" [\n")
1855 row = byte_array[
1856 row_num
1857 * table.bytes_per_row : (row_num + 1)
1858 * table.bytes_per_row
1859 ]
1860 for subrow in batched(row, 15):
1861 module.write(" ")
1862 for entry in subrow:
1863 module.write(f" 0x{entry:02X},")
1864 module.write("\n")
1865 module.write(" ],\n")
1866 module.write("]);\n")
1867 subtable_count = new_subtable_count
1868
1869 # non transparent zero width table
1870
1871 module.write(
1872 f"""
1873 /// Sorted list of codepoint ranges (inclusive)
1874 /// that are zero-width but not `Joining_Type=Transparent`
1875 /// FIXME: can we get better compression?
1876 static NON_TRANSPARENT_ZERO_WIDTHS: [([u8; 3], [u8; 3]); {len(non_transparent_zero_widths)}] = [
1877 """
1878 )
1879
1880 for lo, hi in non_transparent_zero_widths:
1881 module.write(
1882 f" ([0x{lo & 0xFF:02X}, 0x{lo >> 8 & 0xFF:02X}, 0x{lo >> 16:02X}], [0x{hi & 0xFF:02X}, 0x{hi >> 8 & 0xFF:02X}, 0x{hi >> 16:02X}]),\n"
1883 )
1884
1885 # solidus transparent table
1886
1887 module.write(
1888 f"""];
1889
1890 /// Sorted list of codepoint ranges (inclusive)
1891 /// that don't affect how the combining solidus applies
1892 /// (mostly ccc > 1).
1893 /// FIXME: can we get better compression?
1894 #[cfg(feature = "cjk")]
1895 static SOLIDUS_TRANSPARENT: [([u8; 3], [u8; 3]); {len(solidus_transparent)}] = [
1896 """
1897 )
1898
1899 for lo, hi in solidus_transparent:
1900 module.write(
1901 f" ([0x{lo & 0xFF:02X}, 0x{lo >> 8 & 0xFF:02X}, 0x{lo >> 16:02X}], [0x{hi & 0xFF:02X}, 0x{hi >> 8 & 0xFF:02X}, 0x{hi >> 16:02X}]),\n"
1902 )
1903
1904 # emoji table
1905
1906 module.write(
1907 f"""];
1908
1909 /// Array of 1024-bit bitmaps. Index into the correct bitmap with the 10 LSB of your codepoint
1910 /// to get whether it can start an emoji presentation sequence.
1911 static EMOJI_PRESENTATION_LEAVES: Align128<[[u8; 128]; {len(emoji_presentation_leaves)}]> = Align128([
1912 """
1913 )
1914 for leaf in emoji_presentation_leaves:
1915 module.write(" [\n")
1916 for row in batched(leaf, 15):
1917 module.write(" ")
1918 for entry in row:
1919 module.write(f" 0x{entry:02X},")
1920 module.write("\n")
1921 module.write(" ],\n")
1922
1923 module.write("]);\n")
1924
1925 # text table
1926
1927 for leaf_idx, leaf in enumerate(text_presentation_leaves):
1928 module.write(
1929 f"""
1930 #[rustfmt::skip]
1931 static TEXT_PRESENTATION_LEAF_{leaf_idx}: [(u8, u8); {len(leaf)}] = [
1932 """
1933 )
1934 for lo, hi in leaf:
1935 module.write(f" (0x{lo:02X}, 0x{hi:02X}),\n")
1936 module.write(f"];\n")
1937
1938 # emoji modifier table
1939
1940 for leaf_idx, leaf in enumerate(emoji_modifier_leaves):
1941 module.write(
1942 f"""
1943 #[rustfmt::skip]
1944 static EMOJI_MODIFIER_LEAF_{leaf_idx}: [(u8, u8); {len(leaf)}] = [
1945 """
1946 )
1947 for lo, hi in leaf:
1948 module.write(f" (0x{lo:02X}, 0x{hi:02X}),\n")
1949 module.write(f"];\n")
1950
1951 test_width_variants = []
1952 test_width_variants_cjk = []
1953 for variant in WidthState:
1954 if variant.is_carried():
1955 if not variant.is_cjk_only():
1956 test_width_variants.append(variant)
1957 if not variant.is_non_cjk_only():
1958 test_width_variants_cjk.append(variant)
1959
1960 module.write(
1961 f"""
1962 #[cfg(test)]
1963 mod tests {{
1964 use super::*;
1965
1966 fn str_width_test(s: &str, init: WidthInfo) -> isize {{
1967 s.chars()
1968 .rfold((0, init), |(sum, next_info), c| -> (isize, WidthInfo) {{
1969 let (add, info) = width_in_str(c, next_info);
1970 (sum.checked_add(isize::from(add)).unwrap(), info)
1971 }})
1972 .0
1973 }}
1974
1975 #[cfg(feature = "cjk")]
1976 fn str_width_test_cjk(s: &str, init: WidthInfo) -> isize {{
1977 s.chars()
1978 .rfold((0, init), |(sum, next_info), c| -> (isize, WidthInfo) {{
1979 let (add, info) = width_in_str_cjk(c, next_info);
1980 (sum.checked_add(isize::from(add)).unwrap(), info)
1981 }})
1982 .0
1983 }}
1984
1985 #[test]
1986 fn test_normalization() {{
1987 for &(orig, nfc, nfd, nfkc, nfkd) in &NORMALIZATION_TEST {{
1988 for init in NORMALIZATION_TEST_WIDTHS {{
1989 assert_eq!(
1990 str_width_test(orig, init),
1991 str_width_test(nfc, init),
1992 "width of X = {{orig:?}} differs from toNFC(X) = {{nfc:?}} with mode {{init:X?}}",
1993 );
1994 assert_eq!(
1995 str_width_test(orig, init),
1996 str_width_test(nfd, init),
1997 "width of X = {{orig:?}} differs from toNFD(X) = {{nfd:?}} with mode {{init:X?}}",
1998 );
1999 assert_eq!(
2000 str_width_test(nfkc, init),
2001 str_width_test(nfkd, init),
2002 "width of toNFKC(X) = {{nfkc:?}} differs from toNFKD(X) = {{nfkd:?}} with mode {{init:X?}}",
2003 );
2004 }}
2005
2006 #[cfg(feature = "cjk")]
2007 for init in NORMALIZATION_TEST_WIDTHS_CJK {{
2008 assert_eq!(
2009 str_width_test_cjk(orig, init),
2010 str_width_test_cjk(nfc, init),
2011 "CJK width of X = {{orig:?}} differs from toNFC(X) = {{nfc:?}} with mode {{init:X?}}",
2012 );
2013 assert_eq!(
2014 str_width_test_cjk(orig, init),
2015 str_width_test_cjk(nfd, init),
2016 "CJK width of X = {{orig:?}} differs from toNFD(X) = {{nfd:?}} with mode {{init:X?}}",
2017 );
2018 assert_eq!(
2019 str_width_test_cjk(nfkc, init),
2020 str_width_test_cjk(nfkd, init),
2021 "CJK width of toNFKC(X) = {{nfkc:?}} differs from toNFKD(X) = {{nfkd:?}} with mode {{init:?}}",
2022 );
2023 }}
2024 }}
2025 }}
2026
2027 static NORMALIZATION_TEST_WIDTHS: [WidthInfo; {len(test_width_variants) + 1}] = [
2028 WidthInfo::DEFAULT,\n"""
2029 )
2030
2031 for variant in WidthState:
2032 if variant.is_carried() and not variant.is_cjk_only():
2033 module.write(f" WidthInfo::{variant.name},\n")
2034
2035 module.write(
2036 f""" ];
2037
2038 #[cfg(feature = "cjk")]
2039 static NORMALIZATION_TEST_WIDTHS_CJK: [WidthInfo; {len(test_width_variants_cjk) + 1}] = [
2040 WidthInfo::DEFAULT,\n"""
2041 )
2042
2043 for variant in WidthState:
2044 if variant.is_carried() and not variant.is_non_cjk_only():
2045 module.write(f" WidthInfo::{variant.name},\n")
2046
2047 module.write(
2048 f""" ];
2049
2050 #[rustfmt::skip]
2051 static NORMALIZATION_TEST: [(&str, &str, &str, &str, &str); {len(normalization_tests)}] = [\n"""
2052 )
2053 for orig, nfc, nfd, nfkc, nfkd in normalization_tests:
2054 module.write(
2055 f' (r#"{orig}"#, r#"{nfc}"#, r#"{nfd}"#, r#"{nfkc}"#, r#"{nfkd}"#),\n'
2056 )
2057
2058 module.write(" ];\n}\n")
2059
2060
2061 def main(module_path: str):
2062 """Obtain character data from the latest version of Unicode, transform it into a multi-level
2063 lookup table for character width, and write a Rust module utilizing that table to
2064 `module_filename`.
2065
2066 See `lib.rs` for documentation of the exact width rules.
2067 """
2068 version = load_unicode_version()
2069 print(f"Generating module for Unicode {version[0]}.{version[1]}.{version[2]}")
2070
2071 (width_map, cjk_width_map) = load_width_maps()
2072
2073 tables = make_tables(width_map, cjk_width_map)
2074
2075 special_ranges = make_special_ranges(width_map)
2076 cjk_special_ranges = make_special_ranges(cjk_width_map)
2077
2078 emoji_presentations = load_emoji_presentation_sequences()
2079 emoji_presentation_table = make_presentation_sequence_table(emoji_presentations)
2080
2081 text_presentations = load_text_presentation_sequences()
2082 text_presentation_table = make_ranges_table(text_presentations)
2083
2084 emoji_modifier_bases = load_emoji_modifier_bases()
2085 emoji_modifier_table = make_ranges_table(emoji_modifier_bases)
2086
2087 joining_group_lam = load_joining_group_lam()
2088 non_transparent_zero_widths = load_non_transparent_zero_widths(width_map)
2089 ligature_transparent = load_ligature_transparent()
2090 solidus_transparent = load_solidus_transparent(ligature_transparent, cjk_width_map)
2091
2092 normalization_tests = load_normalization_tests()
2093
2094 fetch_open("emoji-test.txt", "../tests", emoji=True)
2095
2096 print("------------------------")
2097 total_size = 0
2098 for i, table in enumerate(tables):
2099 size_bytes = len(table.to_bytes())
2100 print(f"Table {i} size: {size_bytes} bytes")
2101 total_size += size_bytes
2102
2103 for s, table in [
2104 ("Emoji presentation", emoji_presentation_table),
2105 ]:
2106 index_size = len(table[0]) * (math.ceil(math.log(table[0][-1][0], 256)) + 8)
2107 print(f"{s} index size: {index_size} bytes")
2108 total_size += index_size
2109 leaves_size = len(table[1]) * len(table[1][0])
2110 print(f"{s} leaves size: {leaves_size} bytes")
2111 total_size += leaves_size
2112
2113 for s, table in [
2114 ("Text presentation", text_presentation_table),
2115 ("Emoji modifier", emoji_modifier_table),
2116 ]:
2117 index_size = len(table[0]) * (math.ceil(math.log(table[0][-1][0], 256)) + 16)
2118 print(f"{s} index size: {index_size} bytes")
2119 total_size += index_size
2120 leaves_size = 2 * sum(map(len, table[1]))
2121 print(f"{s} leaves size: {leaves_size} bytes")
2122 total_size += leaves_size
2123
2124 for s, table in [
2125 ("Non transparent zero width", non_transparent_zero_widths),
2126 ("Solidus transparent", solidus_transparent),
2127 ]:
2128 table_size = 6 * len(table)
2129 print(f"{s} table size: {table_size} bytes")
2130 total_size += table_size
2131 print("------------------------")
2132 print(f" Total size: {total_size} bytes")
2133
2134 emit_module(
2135 out_name=module_path,
2136 unicode_version=version,
2137 tables=tables,
2138 special_ranges=special_ranges,
2139 special_ranges_cjk=cjk_special_ranges,
2140 emoji_presentation_table=emoji_presentation_table,
2141 text_presentation_table=text_presentation_table,
2142 emoji_modifier_table=emoji_modifier_table,
2143 joining_group_lam=joining_group_lam,
2144 non_transparent_zero_widths=non_transparent_zero_widths,
2145 ligature_transparent=ligature_transparent,
2146 solidus_transparent=solidus_transparent,
2147 normalization_tests=normalization_tests,
2148 )
2149 print(f'Wrote to "{module_path}"')
2150
2151
2152 if __name__ == "__main__":
2153 main(MODULE_PATH)