blob: e181c62c67e56a5082582ddda3166dc7b0b4c725 [file] [log] [blame]
Roozbeh Pournader0e969e22016-03-09 23:08:45 -08001#!/usr/bin/env python
2
3import collections
Roozbeh Pournader5dde0872016-03-31 13:54:56 -07004import copy
Roozbeh Pournader0e969e22016-03-09 23:08:45 -08005import glob
6from os import path
Seigo Nonaka56811242021-04-16 00:11:43 -07007import re
Roozbeh Pournader0e969e22016-03-09 23:08:45 -08008import sys
9from xml.etree import ElementTree
10
11from fontTools import ttLib
12
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070013EMOJI_VS = 0xFE0F
14
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080015LANG_TO_SCRIPT = {
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070016 'as': 'Beng',
Roozbeh Pournader7e04dd12017-10-13 17:41:31 -070017 'be': 'Cyrl',
Roozbeh Pournader033b2222017-02-22 18:53:39 -080018 'bg': 'Cyrl',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070019 'bn': 'Beng',
Roozbeh Pournader033b2222017-02-22 18:53:39 -080020 'cu': 'Cyrl',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070021 'cy': 'Latn',
22 'da': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080023 'de': 'Latn',
24 'en': 'Latn',
25 'es': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070026 'et': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080027 'eu': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070028 'fr': 'Latn',
29 'ga': 'Latn',
30 'gu': 'Gujr',
31 'hi': 'Deva',
32 'hr': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080033 'hu': 'Latn',
34 'hy': 'Armn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070035 'ja': 'Jpan',
36 'kn': 'Knda',
37 'ko': 'Kore',
Roozbeh Pournader7e04dd12017-10-13 17:41:31 -070038 'la': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070039 'ml': 'Mlym',
40 'mn': 'Cyrl',
41 'mr': 'Deva',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080042 'nb': 'Latn',
43 'nn': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070044 'or': 'Orya',
45 'pa': 'Guru',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080046 'pt': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070047 'sl': 'Latn',
48 'ta': 'Taml',
49 'te': 'Telu',
50 'tk': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080051}
52
53def lang_to_script(lang_code):
54 lang = lang_code.lower()
55 while lang not in LANG_TO_SCRIPT:
56 hyphen_idx = lang.rfind('-')
57 assert hyphen_idx != -1, (
58 'We do not know what script the "%s" language is written in.'
59 % lang_code)
60 assumed_script = lang[hyphen_idx+1:]
61 if len(assumed_script) == 4 and assumed_script.isalpha():
62 # This is actually the script
63 return assumed_script.title()
64 lang = lang[:hyphen_idx]
65 return LANG_TO_SCRIPT[lang]
66
67
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070068def printable(inp):
69 if type(inp) is set: # set of character sequences
70 return '{' + ', '.join([printable(seq) for seq in inp]) + '}'
71 if type(inp) is tuple: # character sequence
72 return '<' + (', '.join([printable(ch) for ch in inp])) + '>'
73 else: # single character
74 return 'U+%04X' % inp
75
76
77def open_font(font):
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080078 font_file, index = font
79 font_path = path.join(_fonts_dir, font_file)
80 if index is not None:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070081 return ttLib.TTFont(font_path, fontNumber=index)
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080082 else:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070083 return ttLib.TTFont(font_path)
84
85
86def get_best_cmap(font):
87 ttfont = open_font(font)
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080088 all_unicode_cmap = None
89 bmp_cmap = None
90 for cmap in ttfont['cmap'].tables:
91 specifier = (cmap.format, cmap.platformID, cmap.platEncID)
92 if specifier == (4, 3, 1):
93 assert bmp_cmap is None, 'More than one BMP cmap in %s' % (font, )
94 bmp_cmap = cmap
95 elif specifier == (12, 3, 10):
96 assert all_unicode_cmap is None, (
97 'More than one UCS-4 cmap in %s' % (font, ))
98 all_unicode_cmap = cmap
99
100 return all_unicode_cmap.cmap if all_unicode_cmap else bmp_cmap.cmap
101
102
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700103def get_variation_sequences_cmap(font):
104 ttfont = open_font(font)
105 vs_cmap = None
106 for cmap in ttfont['cmap'].tables:
107 specifier = (cmap.format, cmap.platformID, cmap.platEncID)
108 if specifier == (14, 0, 5):
109 assert vs_cmap is None, 'More than one VS cmap in %s' % (font, )
110 vs_cmap = cmap
111 return vs_cmap
112
113
114def get_emoji_map(font):
115 # Add normal characters
116 emoji_map = copy.copy(get_best_cmap(font))
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700117 reverse_cmap = {glyph: code for code, glyph in emoji_map.items() if not contains_pua(code) }
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700118
119 # Add variation sequences
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700120 vs_cmap = get_variation_sequences_cmap(font)
121 if vs_cmap:
122 for vs in vs_cmap.uvsDict:
123 for base, glyph in vs_cmap.uvsDict[vs]:
124 if glyph is None:
125 emoji_map[(base, vs)] = emoji_map[base]
126 else:
127 emoji_map[(base, vs)] = glyph
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700128
129 # Add GSUB rules
130 ttfont = open_font(font)
131 for lookup in ttfont['GSUB'].table.LookupList.Lookup:
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700132 if lookup.LookupType != 4:
133 # Other lookups are used in the emoji font for fallback.
134 # We ignore them for now.
135 continue
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700136 for subtable in lookup.SubTable:
137 ligatures = subtable.ligatures
138 for first_glyph in ligatures:
139 for ligature in ligatures[first_glyph]:
140 sequence = [first_glyph] + ligature.Component
141 sequence = [reverse_cmap[glyph] for glyph in sequence]
142 sequence = tuple(sequence)
143 # Make sure no starting subsequence of 'sequence' has been
144 # seen before.
145 for sub_len in range(2, len(sequence)+1):
146 subsequence = sequence[:sub_len]
147 assert subsequence not in emoji_map
148 emoji_map[sequence] = ligature.LigGlyph
149
150 return emoji_map
151
152
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800153def assert_font_supports_any_of_chars(font, chars):
154 best_cmap = get_best_cmap(font)
155 for char in chars:
156 if char in best_cmap:
157 return
158 sys.exit('None of characters in %s were found in %s' % (chars, font))
159
160
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700161def assert_font_supports_all_of_chars(font, chars):
162 best_cmap = get_best_cmap(font)
163 for char in chars:
164 assert char in best_cmap, (
165 'U+%04X was not found in %s' % (char, font))
166
167
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700168def assert_font_supports_none_of_chars(font, chars, fallbackName):
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700169 best_cmap = get_best_cmap(font)
170 for char in chars:
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700171 if fallbackName:
172 assert char not in best_cmap, 'U+%04X was found in %s' % (char, font)
173 else:
174 assert char not in best_cmap, (
175 'U+%04X was found in %s in fallback %s' % (char, font, fallbackName))
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700176
177
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700178def assert_font_supports_all_sequences(font, sequences):
179 vs_dict = get_variation_sequences_cmap(font).uvsDict
180 for base, vs in sorted(sequences):
181 assert vs in vs_dict and (base, None) in vs_dict[vs], (
182 '<U+%04X, U+%04X> was not found in %s' % (base, vs, font))
183
184
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800185def check_hyphens(hyphens_dir):
186 # Find all the scripts that need automatic hyphenation
187 scripts = set()
188 for hyb_file in glob.iglob(path.join(hyphens_dir, '*.hyb')):
189 hyb_file = path.basename(hyb_file)
190 assert hyb_file.startswith('hyph-'), (
191 'Unknown hyphenation file %s' % hyb_file)
192 lang_code = hyb_file[hyb_file.index('-')+1:hyb_file.index('.')]
193 scripts.add(lang_to_script(lang_code))
194
195 HYPHENS = {0x002D, 0x2010}
196 for script in scripts:
197 fonts = _script_to_font_map[script]
198 assert fonts, 'No fonts found for the "%s" script' % script
199 for font in fonts:
200 assert_font_supports_any_of_chars(font, HYPHENS)
201
202
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700203class FontRecord(object):
Seigo Nonaka56811242021-04-16 00:11:43 -0700204 def __init__(self, name, psName, scripts, variant, weight, style, fallback_for, font):
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700205 self.name = name
Seigo Nonaka56811242021-04-16 00:11:43 -0700206 self.psName = psName
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700207 self.scripts = scripts
208 self.variant = variant
209 self.weight = weight
210 self.style = style
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700211 self.fallback_for = fallback_for
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700212 self.font = font
213
214
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800215def parse_fonts_xml(fonts_xml_path):
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700216 global _script_to_font_map, _fallback_chains, _all_fonts
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800217 _script_to_font_map = collections.defaultdict(set)
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700218 _fallback_chains = {}
219 _all_fonts = []
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800220 tree = ElementTree.parse(fonts_xml_path)
Seigo Nonaka9092dc22017-01-06 16:54:52 +0900221 families = tree.findall('family')
222 # Minikin supports up to 254 but users can place their own font at the first
223 # place. Thus, 253 is the maximum allowed number of font families in the
224 # default collection.
225 assert len(families) < 254, (
226 'System font collection can contains up to 253 font families.')
227 for family in families:
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800228 name = family.get('name')
229 variant = family.get('variant')
230 langs = family.get('lang')
231 if name:
232 assert variant is None, (
233 'No variant expected for LGC font %s.' % name)
234 assert langs is None, (
235 'No language expected for LGC fonts %s.' % name)
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700236 assert name not in _fallback_chains, 'Duplicated name entry %s' % name
237 _fallback_chains[name] = []
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800238 else:
239 assert variant in {None, 'elegant', 'compact'}, (
240 'Unexpected value for variant: %s' % variant)
241
Seigo Nonaka56811242021-04-16 00:11:43 -0700242 trim_re = re.compile(r"^[ \n\r\t]*(.+)[ \n\r\t]*$")
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700243 for family in families:
244 name = family.get('name')
245 variant = family.get('variant')
246 langs = family.get('lang')
247
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800248 if langs:
249 langs = langs.split()
250 scripts = {lang_to_script(lang) for lang in langs}
251 else:
252 scripts = set()
253
254 for child in family:
255 assert child.tag == 'font', (
256 'Unknown tag <%s>' % child.tag)
Jungshik Shin88b11142017-03-17 14:56:17 -0700257 font_file = child.text.rstrip()
Seigo Nonaka56811242021-04-16 00:11:43 -0700258
259 m = trim_re.match(font_file)
260 font_file = m.group(1)
261
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800262 weight = int(child.get('weight'))
263 assert weight % 100 == 0, (
264 'Font weight "%d" is not a multiple of 100.' % weight)
265
266 style = child.get('style')
267 assert style in {'normal', 'italic'}, (
268 'Unknown style "%s"' % style)
269
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700270 fallback_for = child.get('fallbackFor')
271
272 assert not name or not fallback_for, (
273 'name and fallbackFor cannot be present at the same time')
274 assert not fallback_for or fallback_for in _fallback_chains, (
275 'Unknown fallback name: %s' % fallback_for)
276
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800277 index = child.get('index')
278 if index:
279 index = int(index)
280
Seigo Nonaka56811242021-04-16 00:11:43 -0700281 if not path.exists(path.join(_fonts_dir, m.group(1))):
Seigo Nonaka1403ff22018-01-18 17:24:31 -0800282 continue # Missing font is a valid case. Just ignore the missing font files.
283
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700284 record = FontRecord(
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800285 name,
Seigo Nonaka56811242021-04-16 00:11:43 -0700286 child.get('postScriptName'),
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800287 frozenset(scripts),
288 variant,
289 weight,
290 style,
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700291 fallback_for,
292 (font_file, index))
293
294 _all_fonts.append(record)
295
296 if not fallback_for:
297 if not name or name == 'sans-serif':
Haibo Huang715857d2020-03-05 11:58:47 -0800298 for _, fallback in _fallback_chains.items():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700299 fallback.append(record)
300 else:
301 _fallback_chains[name].append(record)
302 else:
303 _fallback_chains[fallback_for].append(record)
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800304
305 if name: # non-empty names are used for default LGC fonts
306 map_scripts = {'Latn', 'Grek', 'Cyrl'}
307 else:
308 map_scripts = scripts
309 for script in map_scripts:
310 _script_to_font_map[script].add((font_file, index))
311
312
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700313def check_emoji_coverage(all_emoji, equivalent_emoji):
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700314 emoji_fonts = get_emoji_fonts()
315 check_emoji_font_coverage(emoji_fonts, all_emoji, equivalent_emoji)
Doug Feltf874a192016-07-08 17:42:15 -0700316
317
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700318def get_emoji_fonts():
319 return [ record.font for record in _all_fonts if 'Zsye' in record.scripts ]
Doug Feltf874a192016-07-08 17:42:15 -0700320
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700321def is_pua(x):
322 return 0xE000 <= x <= 0xF8FF or 0xF0000 <= x <= 0xFFFFD or 0x100000 <= x <= 0x10FFFD
323
324def contains_pua(sequence):
325 if type(sequence) is tuple:
326 return any([is_pua(x) for x in sequence])
327 else:
328 return is_pua(sequence)
329
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700330def get_psname(ttf):
331 return str(next(x for x in ttf['name'].names
332 if x.platformID == 3 and x.platEncID == 1 and x.nameID == 6))
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700333
334def check_emoji_compat():
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700335 for emoji_font in get_emoji_fonts():
336 ttf = open_font(emoji_font)
337 psname = get_psname(ttf)
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700338
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700339 # If the font file is NotoColorEmoji, it must be Compat font.
340 if psname == 'NotoColorEmoji':
341 meta = ttf['meta']
342 assert meta, 'Compat font must have meta table'
343 assert 'Emji' in meta.data, 'meta table should have \'Emji\' data.'
344
345def check_emoji_font_coverage(emoji_fonts, all_emoji, equivalent_emoji):
346 coverages = []
347 for emoji_font in emoji_fonts:
348 coverages.append(get_emoji_map(emoji_font))
Rod Se34a19d2020-03-16 00:01:15 -0700349
350 errors = []
351
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700352 for sequence in all_emoji:
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700353 if all([sequence not in coverage for coverage in coverages]):
354 errors.append('%s is not supported in the emoji font.' % printable(sequence))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700355
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700356 for coverage in coverages:
357 for sequence in coverage:
358 if sequence in {0x0000, 0x000D, 0x0020}:
359 # The font needs to support a few extra characters, which is OK
360 continue
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700361
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700362 if contains_pua(sequence):
363 # The font needs to have some PUA for EmojiCompat library.
364 continue
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700365
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700366 if sequence not in all_emoji:
367 errors.append('%s support unexpected in the emoji font.' % printable(sequence))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700368
Haibo Huang715857d2020-03-05 11:58:47 -0800369 for first, second in equivalent_emoji.items():
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700370 for coverage in coverages:
371 if first not in coverage or second not in coverage:
372 continue # sequence will be reported missing
373 if coverage[first] != coverage[second]:
374 errors.append('%s and %s should map to the same glyph.' % (
375 printable(first),
376 printable(second)))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700377
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700378 for coverage in coverages:
379 for glyph in set(coverage.values()):
380 maps_to_glyph = [
381 seq for seq in coverage if coverage[seq] == glyph and not contains_pua(seq) ]
382 if len(maps_to_glyph) > 1:
383 # There are more than one sequences mapping to the same glyph. We
384 # need to make sure they were expected to be equivalent.
385 equivalent_seqs = set()
386 for seq in maps_to_glyph:
387 equivalent_seq = seq
388 while equivalent_seq in equivalent_emoji:
389 equivalent_seq = equivalent_emoji[equivalent_seq]
390 equivalent_seqs.add(equivalent_seq)
391 if len(equivalent_seqs) != 1:
392 errors.append('The sequences %s should not result in the same glyph %s' % (
393 printable(equivalent_seqs),
394 glyph))
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700395
Rod Se34a19d2020-03-16 00:01:15 -0700396 assert not errors, '%d emoji font errors:\n%s\n%d emoji font coverage errors' % (len(errors), '\n'.join(errors), len(errors))
397
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700398
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700399def check_emoji_defaults(default_emoji):
400 missing_text_chars = _emoji_properties['Emoji'] - default_emoji
Haibo Huang715857d2020-03-05 11:58:47 -0800401 for name, fallback_chain in _fallback_chains.items():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700402 emoji_font_seen = False
403 for record in fallback_chain:
404 if 'Zsye' in record.scripts:
405 emoji_font_seen = True
406 # No need to check the emoji font
407 continue
408 # For later fonts, we only check them if they have a script
409 # defined, since the defined script may get them to a higher
410 # score even if they appear after the emoji font. However,
411 # we should skip checking the text symbols font, since
412 # symbol fonts should be able to override the emoji display
413 # style when 'Zsym' is explicitly specified by the user.
414 if emoji_font_seen and (not record.scripts or 'Zsym' in record.scripts):
415 continue
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700416
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700417 # Check default emoji-style characters
Haibo Huang715857d2020-03-05 11:58:47 -0800418 assert_font_supports_none_of_chars(record.font, default_emoji, name)
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700419
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700420 # Mark default text-style characters appearing in fonts above the emoji
421 # font as seen
422 if not emoji_font_seen:
423 missing_text_chars -= set(get_best_cmap(record.font))
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700424
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700425 # Noto does not have monochrome glyphs for Unicode 7.0 wingdings and
426 # webdings yet.
427 missing_text_chars -= _chars_by_age['7.0']
428 assert missing_text_chars == set(), (
429 'Text style version of some emoji characters are missing: ' +
430 repr(missing_text_chars))
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700431
432
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700433# Setting reverse to true returns a dictionary that maps the values to sets of
434# characters, useful for some binary properties. Otherwise, we get a
435# dictionary that maps characters to the property values, assuming there's only
436# one property in the file.
437def parse_unicode_datafile(file_path, reverse=False):
438 if reverse:
439 output_dict = collections.defaultdict(set)
440 else:
441 output_dict = {}
442 with open(file_path) as datafile:
443 for line in datafile:
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700444 if '#' in line:
445 line = line[:line.index('#')]
446 line = line.strip()
447 if not line:
448 continue
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700449
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700450 chars, prop = line.split(';')[:2]
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700451 chars = chars.strip()
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700452 prop = prop.strip()
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700453
454 if ' ' in chars: # character sequence
455 sequence = [int(ch, 16) for ch in chars.split(' ')]
456 additions = [tuple(sequence)]
457 elif '..' in chars: # character range
458 char_start, char_end = chars.split('..')
459 char_start = int(char_start, 16)
460 char_end = int(char_end, 16)
Haibo Huang715857d2020-03-05 11:58:47 -0800461 additions = range(char_start, char_end+1)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700462 else: # singe character
463 additions = [int(chars, 16)]
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700464 if reverse:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700465 output_dict[prop].update(additions)
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700466 else:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700467 for addition in additions:
468 assert addition not in output_dict
469 output_dict[addition] = prop
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700470 return output_dict
471
472
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700473def parse_emoji_variants(file_path):
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700474 emoji_set = set()
475 text_set = set()
476 with open(file_path) as datafile:
477 for line in datafile:
478 if '#' in line:
479 line = line[:line.index('#')]
480 line = line.strip()
481 if not line:
482 continue
483 sequence, description, _ = line.split(';')
484 sequence = sequence.strip().split(' ')
485 base = int(sequence[0], 16)
486 vs = int(sequence[1], 16)
487 description = description.strip()
488 if description == 'text style':
489 text_set.add((base, vs))
490 elif description == 'emoji style':
491 emoji_set.add((base, vs))
492 return text_set, emoji_set
493
494
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700495def parse_ucd(ucd_path):
496 global _emoji_properties, _chars_by_age
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700497 global _text_variation_sequences, _emoji_variation_sequences
498 global _emoji_sequences, _emoji_zwj_sequences
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700499 _emoji_properties = parse_unicode_datafile(
500 path.join(ucd_path, 'emoji-data.txt'), reverse=True)
Roozbeh Pournaderf7a68c12017-04-04 18:59:31 -0700501 emoji_properties_additions = parse_unicode_datafile(
502 path.join(ucd_path, 'additions', 'emoji-data.txt'), reverse=True)
503 for prop in emoji_properties_additions.keys():
504 _emoji_properties[prop].update(emoji_properties_additions[prop])
505
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700506 _chars_by_age = parse_unicode_datafile(
507 path.join(ucd_path, 'DerivedAge.txt'), reverse=True)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700508 sequences = parse_emoji_variants(
509 path.join(ucd_path, 'emoji-variation-sequences.txt'))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700510 _text_variation_sequences, _emoji_variation_sequences = sequences
511 _emoji_sequences = parse_unicode_datafile(
512 path.join(ucd_path, 'emoji-sequences.txt'))
Siyamed Sinir6e06ad02017-04-19 18:18:35 -0700513 _emoji_sequences.update(parse_unicode_datafile(
514 path.join(ucd_path, 'additions', 'emoji-sequences.txt')))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700515 _emoji_zwj_sequences = parse_unicode_datafile(
516 path.join(ucd_path, 'emoji-zwj-sequences.txt'))
Roozbeh Pournader1800ba42017-03-17 18:23:23 -0700517 _emoji_zwj_sequences.update(parse_unicode_datafile(
518 path.join(ucd_path, 'additions', 'emoji-zwj-sequences.txt')))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700519
Siyamed Sinird97df5a2018-04-12 13:11:42 -0700520 exclusions = parse_unicode_datafile(path.join(ucd_path, 'additions', 'emoji-exclusions.txt'))
521 _emoji_sequences = remove_emoji_exclude(_emoji_sequences, exclusions)
522 _emoji_zwj_sequences = remove_emoji_exclude(_emoji_zwj_sequences, exclusions)
523 _emoji_variation_sequences = remove_emoji_variation_exclude(_emoji_variation_sequences, exclusions)
Qingqing Deng5e987712019-03-25 16:53:34 -0700524 # Unicode 12.0 adds Basic_Emoji in emoji-sequences.txt. We ignore them here since we are already
525 # checking the emoji presentations with emoji-variation-sequences.txt.
526 # Please refer to http://unicode.org/reports/tr51/#def_basic_emoji_set .
Haibo Huang715857d2020-03-05 11:58:47 -0800527 _emoji_sequences = {k: v for k, v in _emoji_sequences.items() if not v == 'Basic_Emoji' }
Qingqing Deng5e987712019-03-25 16:53:34 -0700528
Siyamed Sinird97df5a2018-04-12 13:11:42 -0700529
530def remove_emoji_variation_exclude(source, items):
531 return source.difference(items.keys())
532
533def remove_emoji_exclude(source, items):
534 return {k: v for k, v in source.items() if k not in items}
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700535
536def flag_sequence(territory_code):
537 return tuple(0x1F1E6 + ord(ch) - ord('A') for ch in territory_code)
538
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700539EQUIVALENT_FLAGS = {
540 flag_sequence('BV'): flag_sequence('NO'),
541 flag_sequence('CP'): flag_sequence('FR'),
542 flag_sequence('HM'): flag_sequence('AU'),
543 flag_sequence('SJ'): flag_sequence('NO'),
544 flag_sequence('UM'): flag_sequence('US'),
545}
546
547COMBINING_KEYCAP = 0x20E3
548
549LEGACY_ANDROID_EMOJI = {
550 0xFE4E5: flag_sequence('JP'),
551 0xFE4E6: flag_sequence('US'),
552 0xFE4E7: flag_sequence('FR'),
553 0xFE4E8: flag_sequence('DE'),
554 0xFE4E9: flag_sequence('IT'),
555 0xFE4EA: flag_sequence('GB'),
556 0xFE4EB: flag_sequence('ES'),
557 0xFE4EC: flag_sequence('RU'),
558 0xFE4ED: flag_sequence('CN'),
559 0xFE4EE: flag_sequence('KR'),
560 0xFE82C: (ord('#'), COMBINING_KEYCAP),
561 0xFE82E: (ord('1'), COMBINING_KEYCAP),
562 0xFE82F: (ord('2'), COMBINING_KEYCAP),
563 0xFE830: (ord('3'), COMBINING_KEYCAP),
564 0xFE831: (ord('4'), COMBINING_KEYCAP),
565 0xFE832: (ord('5'), COMBINING_KEYCAP),
566 0xFE833: (ord('6'), COMBINING_KEYCAP),
567 0xFE834: (ord('7'), COMBINING_KEYCAP),
568 0xFE835: (ord('8'), COMBINING_KEYCAP),
569 0xFE836: (ord('9'), COMBINING_KEYCAP),
570 0xFE837: (ord('0'), COMBINING_KEYCAP),
571}
572
Siyamed Sinir77a1b142018-07-12 12:02:18 -0700573# This is used to define the emoji that should have the same glyph.
574# i.e. previously we had gender based Kiss (0x1F48F), which had the same glyph
575# with Kiss: Woman, Man (0x1F469, 0x200D, 0x2764, 0x200D, 0x1F48B, 0x200D, 0x1F468)
576# in that case a valid row would be:
577# (0x1F469, 0x200D, 0x2764, 0x200D, 0x1F48B, 0x200D, 0x1F468): 0x1F48F,
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700578ZWJ_IDENTICALS = {
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700579}
580
Seigo Nonakac1808632018-05-14 13:39:40 -0700581SAME_FLAG_MAPPINGS = [
582 # Diego Garcia and British Indian Ocean Territory
583 ((0x1F1EE, 0x1F1F4), (0x1F1E9, 0x1F1EC)),
584 # St. Martin and France
585 ((0x1F1F2, 0x1F1EB), (0x1F1EB, 0x1F1F7)),
586 # Spain and Ceuta & Melilla
587 ((0x1F1EA, 0x1F1F8), (0x1F1EA, 0x1F1E6)),
588]
589
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700590ZWJ = 0x200D
Doug Feltf874a192016-07-08 17:42:15 -0700591
592def is_fitzpatrick_modifier(cp):
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700593 return 0x1F3FB <= cp <= 0x1F3FF
594
595
596def reverse_emoji(seq):
597 rev = list(reversed(seq))
598 # if there are fitzpatrick modifiers in the sequence, keep them after
599 # the emoji they modify
Haibo Huang715857d2020-03-05 11:58:47 -0800600 for i in range(1, len(rev)):
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700601 if is_fitzpatrick_modifier(rev[i-1]):
602 rev[i], rev[i-1] = rev[i-1], rev[i]
603 return tuple(rev)
Doug Feltf874a192016-07-08 17:42:15 -0700604
605
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700606def compute_expected_emoji():
607 equivalent_emoji = {}
608 sequence_pieces = set()
609 all_sequences = set()
610 all_sequences.update(_emoji_variation_sequences)
611
Raph Levien2b8b8192016-08-09 14:28:54 -0700612 # add zwj sequences not in the current emoji-zwj-sequences.txt
613 adjusted_emoji_zwj_sequences = dict(_emoji_zwj_sequences)
614 adjusted_emoji_zwj_sequences.update(_emoji_zwj_sequences)
Raph Levien2b8b8192016-08-09 14:28:54 -0700615
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700616 # Add empty flag tag sequence that is supported as fallback
617 _emoji_sequences[(0x1F3F4, 0xE007F)] = 'Emoji_Tag_Sequence'
618
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700619 for sequence in _emoji_sequences.keys():
620 sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
621 all_sequences.add(sequence)
622 sequence_pieces.update(sequence)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700623 if _emoji_sequences.get(sequence, None) == 'Emoji_Tag_Sequence':
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700624 # Add reverse of all emoji ZWJ sequences, which are added to the
625 # fonts as a workaround to get the sequences work in RTL text.
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700626 # TODO: test if these are actually needed by Minikin/HarfBuzz.
627 reversed_seq = reverse_emoji(sequence)
628 all_sequences.add(reversed_seq)
629 equivalent_emoji[reversed_seq] = sequence
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700630
Raph Levien2b8b8192016-08-09 14:28:54 -0700631 for sequence in adjusted_emoji_zwj_sequences.keys():
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700632 sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
633 all_sequences.add(sequence)
634 sequence_pieces.update(sequence)
635 # Add reverse of all emoji ZWJ sequences, which are added to the fonts
636 # as a workaround to get the sequences work in RTL text.
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700637 reversed_seq = reverse_emoji(sequence)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700638 all_sequences.add(reversed_seq)
639 equivalent_emoji[reversed_seq] = sequence
640
Seigo Nonakac1808632018-05-14 13:39:40 -0700641 for first, second in SAME_FLAG_MAPPINGS:
642 equivalent_emoji[first] = second
643
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700644 # Add all tag characters used in flags
645 sequence_pieces.update(range(0xE0030, 0xE0039 + 1))
646 sequence_pieces.update(range(0xE0061, 0xE007A + 1))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700647
648 all_emoji = (
649 _emoji_properties['Emoji'] |
650 all_sequences |
651 sequence_pieces |
652 set(LEGACY_ANDROID_EMOJI.keys()))
653 default_emoji = (
654 _emoji_properties['Emoji_Presentation'] |
655 all_sequences |
656 set(LEGACY_ANDROID_EMOJI.keys()))
657
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700658 equivalent_emoji.update(EQUIVALENT_FLAGS)
659 equivalent_emoji.update(LEGACY_ANDROID_EMOJI)
660 equivalent_emoji.update(ZWJ_IDENTICALS)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700661
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700662 for seq in _emoji_variation_sequences:
663 equivalent_emoji[seq] = seq[0]
664
665 return all_emoji, default_emoji, equivalent_emoji
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700666
667
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700668def check_compact_only_fallback():
Haibo Huang715857d2020-03-05 11:58:47 -0800669 for name, fallback_chain in _fallback_chains.items():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700670 for record in fallback_chain:
671 if record.variant == 'compact':
672 same_script_elegants = [x for x in fallback_chain
673 if x.scripts == record.scripts and x.variant == 'elegant']
674 assert same_script_elegants, (
675 '%s must be in elegant of %s as fallback of "%s" too' % (
676 record.font, record.scripts, record.fallback_for),)
677
678
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700679def check_vertical_metrics():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700680 for record in _all_fonts:
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700681 if record.name in ['sans-serif', 'sans-serif-condensed']:
682 font = open_font(record.font)
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700683 assert font['head'].yMax == 2163 and font['head'].yMin == -555, (
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700684 'yMax and yMin of %s do not match expected values.' % (
685 record.font,))
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700686
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700687 if record.name in ['sans-serif', 'sans-serif-condensed',
688 'serif', 'monospace']:
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700689 font = open_font(record.font)
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700690 assert (font['hhea'].ascent == 1900 and
691 font['hhea'].descent == -500), (
692 'ascent and descent of %s do not match expected '
693 'values.' % (record.font,))
694
695
696def check_cjk_punctuation():
697 cjk_scripts = {'Hans', 'Hant', 'Jpan', 'Kore'}
698 cjk_punctuation = range(0x3000, 0x301F + 1)
Haibo Huang715857d2020-03-05 11:58:47 -0800699 for name, fallback_chain in _fallback_chains.items():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700700 for record in fallback_chain:
701 if record.scripts.intersection(cjk_scripts):
702 # CJK font seen. Stop checking the rest of the fonts.
703 break
704 assert_font_supports_none_of_chars(record.font, cjk_punctuation, name)
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700705
Seigo Nonaka56811242021-04-16 00:11:43 -0700706def getPostScriptName(font):
707 font_file, index = font
708 font_path = path.join(_fonts_dir, font_file)
709 if index is not None:
710 # Use the first font file in the collection for resolving post script name.
711 ttf = ttLib.TTFont(font_path, fontNumber=0)
712 else:
713 ttf = ttLib.TTFont(font_path)
714
715 nameTable = ttf['name']
716 for name in nameTable.names:
717 if (name.nameID == 6 and name.platformID == 3 and name.platEncID == 1
718 and name.langID == 0x0409):
719 return str(name)
720
721def check_canonical_name():
722 for record in _all_fonts:
723 file_name, index = record.font
724
725 psName = getPostScriptName(record.font)
726 if record.psName:
727 # If fonts element has postScriptName attribute, it should match with the PostScript
728 # name in the name table.
729 assert psName == record.psName, ('postScriptName attribute %s should match with %s' % (
730 record.psName, psName))
731 else:
732 # If fonts element doesn't have postScriptName attribute, the file name should match
733 # with the PostScript name in the name table.
734 assert psName == file_name[:-4], ('file name %s should match with %s' % (
735 file_name, psName))
736
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700737
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800738def main():
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800739 global _fonts_dir
Doug Feltf874a192016-07-08 17:42:15 -0700740 target_out = sys.argv[1]
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800741 _fonts_dir = path.join(target_out, 'fonts')
742
743 fonts_xml_path = path.join(target_out, 'etc', 'fonts.xml')
744 parse_fonts_xml(fonts_xml_path)
745
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700746 check_compact_only_fallback()
747
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700748 check_vertical_metrics()
749
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800750 hyphens_dir = path.join(target_out, 'usr', 'hyphen-data')
751 check_hyphens(hyphens_dir)
752
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700753 check_cjk_punctuation()
754
Seigo Nonaka56811242021-04-16 00:11:43 -0700755 check_canonical_name()
756
Roozbeh Pournader27ec3ac2016-03-31 13:05:32 -0700757 check_emoji = sys.argv[2]
758 if check_emoji == 'true':
759 ucd_path = sys.argv[3]
760 parse_ucd(ucd_path)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700761 all_emoji, default_emoji, equivalent_emoji = compute_expected_emoji()
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700762 check_emoji_compat()
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700763 check_emoji_coverage(all_emoji, equivalent_emoji)
764 check_emoji_defaults(default_emoji)
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700765
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800766
767if __name__ == '__main__':
768 main()