blob: 0d9ea1b7068044de8a68e125450e83386b4af782 [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 = {
Calvin Pan24479da2021-12-14 18:50:31 +080016 'af': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070017 'as': 'Beng',
Calvin Panfb114cc2021-12-06 21:42:12 +080018 'am': 'Latn',
Roozbeh Pournader7e04dd12017-10-13 17:41:31 -070019 'be': 'Cyrl',
Roozbeh Pournader033b2222017-02-22 18:53:39 -080020 'bg': 'Cyrl',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070021 'bn': 'Beng',
Calvin Pan24479da2021-12-14 18:50:31 +080022 'cs': 'Latn',
Roozbeh Pournader033b2222017-02-22 18:53:39 -080023 'cu': 'Cyrl',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070024 'cy': 'Latn',
25 'da': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080026 'de': 'Latn',
Calvin Pan24479da2021-12-14 18:50:31 +080027 'el': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080028 'en': 'Latn',
29 'es': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070030 'et': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080031 'eu': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070032 'fr': 'Latn',
33 'ga': 'Latn',
Calvin Panfb114cc2021-12-06 21:42:12 +080034 'gl': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070035 'gu': 'Gujr',
36 'hi': 'Deva',
37 'hr': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080038 'hu': 'Latn',
39 'hy': 'Armn',
Calvin Panfb114cc2021-12-06 21:42:12 +080040 'it': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070041 'ja': 'Jpan',
Calvin Pan24479da2021-12-14 18:50:31 +080042 'ka': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070043 'kn': 'Knda',
44 'ko': 'Kore',
Roozbeh Pournader7e04dd12017-10-13 17:41:31 -070045 'la': 'Latn',
Calvin Panfb114cc2021-12-06 21:42:12 +080046 'lt': 'Latn',
Calvin Pan24479da2021-12-14 18:50:31 +080047 'lv': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070048 'ml': 'Mlym',
49 'mn': 'Cyrl',
50 'mr': 'Deva',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080051 'nb': 'Latn',
Calvin Pan24479da2021-12-14 18:50:31 +080052 'nl': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080053 'nn': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070054 'or': 'Orya',
55 'pa': 'Guru',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080056 'pt': 'Latn',
Calvin Pan13c70d62022-01-21 16:42:18 +080057 'ru': 'Latn',
Calvin Pan24479da2021-12-14 18:50:31 +080058 'sk': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070059 'sl': 'Latn',
Calvin Pan24479da2021-12-14 18:50:31 +080060 'sq': 'Latn',
Calvin Pan1e966a32022-01-12 16:12:38 +080061 'sv': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070062 'ta': 'Taml',
63 'te': 'Telu',
64 'tk': 'Latn',
Calvin Panfb114cc2021-12-06 21:42:12 +080065 'uk': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080066}
67
68def lang_to_script(lang_code):
69 lang = lang_code.lower()
70 while lang not in LANG_TO_SCRIPT:
71 hyphen_idx = lang.rfind('-')
72 assert hyphen_idx != -1, (
73 'We do not know what script the "%s" language is written in.'
74 % lang_code)
75 assumed_script = lang[hyphen_idx+1:]
76 if len(assumed_script) == 4 and assumed_script.isalpha():
77 # This is actually the script
78 return assumed_script.title()
79 lang = lang[:hyphen_idx]
80 return LANG_TO_SCRIPT[lang]
81
82
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070083def printable(inp):
84 if type(inp) is set: # set of character sequences
85 return '{' + ', '.join([printable(seq) for seq in inp]) + '}'
86 if type(inp) is tuple: # character sequence
87 return '<' + (', '.join([printable(ch) for ch in inp])) + '>'
88 else: # single character
89 return 'U+%04X' % inp
90
91
92def open_font(font):
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080093 font_file, index = font
94 font_path = path.join(_fonts_dir, font_file)
95 if index is not None:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070096 return ttLib.TTFont(font_path, fontNumber=index)
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080097 else:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070098 return ttLib.TTFont(font_path)
99
100
101def get_best_cmap(font):
102 ttfont = open_font(font)
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800103 all_unicode_cmap = None
104 bmp_cmap = None
105 for cmap in ttfont['cmap'].tables:
106 specifier = (cmap.format, cmap.platformID, cmap.platEncID)
107 if specifier == (4, 3, 1):
108 assert bmp_cmap is None, 'More than one BMP cmap in %s' % (font, )
109 bmp_cmap = cmap
110 elif specifier == (12, 3, 10):
111 assert all_unicode_cmap is None, (
112 'More than one UCS-4 cmap in %s' % (font, ))
113 all_unicode_cmap = cmap
114
115 return all_unicode_cmap.cmap if all_unicode_cmap else bmp_cmap.cmap
116
117
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700118def get_variation_sequences_cmap(font):
119 ttfont = open_font(font)
120 vs_cmap = None
121 for cmap in ttfont['cmap'].tables:
122 specifier = (cmap.format, cmap.platformID, cmap.platEncID)
123 if specifier == (14, 0, 5):
124 assert vs_cmap is None, 'More than one VS cmap in %s' % (font, )
125 vs_cmap = cmap
126 return vs_cmap
127
128
129def get_emoji_map(font):
130 # Add normal characters
131 emoji_map = copy.copy(get_best_cmap(font))
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700132 reverse_cmap = {glyph: code for code, glyph in emoji_map.items() if not contains_pua(code) }
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700133
134 # Add variation sequences
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700135 vs_cmap = get_variation_sequences_cmap(font)
136 if vs_cmap:
137 for vs in vs_cmap.uvsDict:
138 for base, glyph in vs_cmap.uvsDict[vs]:
139 if glyph is None:
140 emoji_map[(base, vs)] = emoji_map[base]
141 else:
142 emoji_map[(base, vs)] = glyph
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700143
144 # Add GSUB rules
145 ttfont = open_font(font)
146 for lookup in ttfont['GSUB'].table.LookupList.Lookup:
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700147 if lookup.LookupType != 4:
148 # Other lookups are used in the emoji font for fallback.
149 # We ignore them for now.
150 continue
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700151 for subtable in lookup.SubTable:
152 ligatures = subtable.ligatures
153 for first_glyph in ligatures:
154 for ligature in ligatures[first_glyph]:
155 sequence = [first_glyph] + ligature.Component
156 sequence = [reverse_cmap[glyph] for glyph in sequence]
157 sequence = tuple(sequence)
158 # Make sure no starting subsequence of 'sequence' has been
159 # seen before.
160 for sub_len in range(2, len(sequence)+1):
161 subsequence = sequence[:sub_len]
162 assert subsequence not in emoji_map
163 emoji_map[sequence] = ligature.LigGlyph
164
165 return emoji_map
166
167
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800168def assert_font_supports_any_of_chars(font, chars):
169 best_cmap = get_best_cmap(font)
170 for char in chars:
171 if char in best_cmap:
172 return
173 sys.exit('None of characters in %s were found in %s' % (chars, font))
174
175
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700176def assert_font_supports_all_of_chars(font, chars):
177 best_cmap = get_best_cmap(font)
178 for char in chars:
179 assert char in best_cmap, (
180 'U+%04X was not found in %s' % (char, font))
181
182
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700183def assert_font_supports_none_of_chars(font, chars, fallbackName):
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700184 best_cmap = get_best_cmap(font)
185 for char in chars:
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700186 if fallbackName:
187 assert char not in best_cmap, 'U+%04X was found in %s' % (char, font)
188 else:
189 assert char not in best_cmap, (
190 'U+%04X was found in %s in fallback %s' % (char, font, fallbackName))
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700191
192
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700193def assert_font_supports_all_sequences(font, sequences):
194 vs_dict = get_variation_sequences_cmap(font).uvsDict
195 for base, vs in sorted(sequences):
196 assert vs in vs_dict and (base, None) in vs_dict[vs], (
197 '<U+%04X, U+%04X> was not found in %s' % (base, vs, font))
198
199
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800200def check_hyphens(hyphens_dir):
201 # Find all the scripts that need automatic hyphenation
202 scripts = set()
203 for hyb_file in glob.iglob(path.join(hyphens_dir, '*.hyb')):
204 hyb_file = path.basename(hyb_file)
205 assert hyb_file.startswith('hyph-'), (
206 'Unknown hyphenation file %s' % hyb_file)
207 lang_code = hyb_file[hyb_file.index('-')+1:hyb_file.index('.')]
208 scripts.add(lang_to_script(lang_code))
209
210 HYPHENS = {0x002D, 0x2010}
211 for script in scripts:
212 fonts = _script_to_font_map[script]
213 assert fonts, 'No fonts found for the "%s" script' % script
214 for font in fonts:
215 assert_font_supports_any_of_chars(font, HYPHENS)
216
217
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700218class FontRecord(object):
Seigo Nonaka56811242021-04-16 00:11:43 -0700219 def __init__(self, name, psName, scripts, variant, weight, style, fallback_for, font):
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700220 self.name = name
Seigo Nonaka56811242021-04-16 00:11:43 -0700221 self.psName = psName
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700222 self.scripts = scripts
223 self.variant = variant
224 self.weight = weight
225 self.style = style
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700226 self.fallback_for = fallback_for
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700227 self.font = font
228
229
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800230def parse_fonts_xml(fonts_xml_path):
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700231 global _script_to_font_map, _fallback_chains, _all_fonts
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800232 _script_to_font_map = collections.defaultdict(set)
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700233 _fallback_chains = {}
234 _all_fonts = []
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800235 tree = ElementTree.parse(fonts_xml_path)
Seigo Nonaka9092dc22017-01-06 16:54:52 +0900236 families = tree.findall('family')
237 # Minikin supports up to 254 but users can place their own font at the first
238 # place. Thus, 253 is the maximum allowed number of font families in the
239 # default collection.
240 assert len(families) < 254, (
241 'System font collection can contains up to 253 font families.')
242 for family in families:
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800243 name = family.get('name')
244 variant = family.get('variant')
245 langs = family.get('lang')
Seigo Nonakae26eb8c2022-03-02 15:43:49 -0800246 ignoreAttr = family.get('ignore')
247
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800248 if name:
249 assert variant is None, (
250 'No variant expected for LGC font %s.' % name)
251 assert langs is None, (
252 'No language expected for LGC fonts %s.' % name)
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700253 assert name not in _fallback_chains, 'Duplicated name entry %s' % name
254 _fallback_chains[name] = []
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800255 else:
256 assert variant in {None, 'elegant', 'compact'}, (
257 'Unexpected value for variant: %s' % variant)
258
Seigo Nonaka56811242021-04-16 00:11:43 -0700259 trim_re = re.compile(r"^[ \n\r\t]*(.+)[ \n\r\t]*$")
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700260 for family in families:
261 name = family.get('name')
262 variant = family.get('variant')
263 langs = family.get('lang')
Seigo Nonakae26eb8c2022-03-02 15:43:49 -0800264 ignoreAttr = family.get('ignore')
265 ignore = ignoreAttr == 'true' or ignoreAttr == '1'
266
267 if ignore:
268 continue
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700269
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800270 if langs:
271 langs = langs.split()
272 scripts = {lang_to_script(lang) for lang in langs}
273 else:
274 scripts = set()
275
276 for child in family:
277 assert child.tag == 'font', (
278 'Unknown tag <%s>' % child.tag)
Jungshik Shin88b11142017-03-17 14:56:17 -0700279 font_file = child.text.rstrip()
Seigo Nonaka56811242021-04-16 00:11:43 -0700280
281 m = trim_re.match(font_file)
282 font_file = m.group(1)
283
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800284 weight = int(child.get('weight'))
285 assert weight % 100 == 0, (
286 'Font weight "%d" is not a multiple of 100.' % weight)
287
288 style = child.get('style')
289 assert style in {'normal', 'italic'}, (
290 'Unknown style "%s"' % style)
291
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700292 fallback_for = child.get('fallbackFor')
293
294 assert not name or not fallback_for, (
295 'name and fallbackFor cannot be present at the same time')
296 assert not fallback_for or fallback_for in _fallback_chains, (
297 'Unknown fallback name: %s' % fallback_for)
298
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800299 index = child.get('index')
300 if index:
301 index = int(index)
302
Seigo Nonaka56811242021-04-16 00:11:43 -0700303 if not path.exists(path.join(_fonts_dir, m.group(1))):
Seigo Nonaka1403ff22018-01-18 17:24:31 -0800304 continue # Missing font is a valid case. Just ignore the missing font files.
305
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700306 record = FontRecord(
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800307 name,
Seigo Nonaka56811242021-04-16 00:11:43 -0700308 child.get('postScriptName'),
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800309 frozenset(scripts),
310 variant,
311 weight,
312 style,
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700313 fallback_for,
314 (font_file, index))
315
316 _all_fonts.append(record)
317
318 if not fallback_for:
319 if not name or name == 'sans-serif':
Haibo Huang715857d2020-03-05 11:58:47 -0800320 for _, fallback in _fallback_chains.items():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700321 fallback.append(record)
322 else:
323 _fallback_chains[name].append(record)
324 else:
325 _fallback_chains[fallback_for].append(record)
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800326
327 if name: # non-empty names are used for default LGC fonts
328 map_scripts = {'Latn', 'Grek', 'Cyrl'}
329 else:
330 map_scripts = scripts
331 for script in map_scripts:
332 _script_to_font_map[script].add((font_file, index))
333
334
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700335def check_emoji_coverage(all_emoji, equivalent_emoji):
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700336 emoji_fonts = get_emoji_fonts()
337 check_emoji_font_coverage(emoji_fonts, all_emoji, equivalent_emoji)
Doug Feltf874a192016-07-08 17:42:15 -0700338
339
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700340def get_emoji_fonts():
341 return [ record.font for record in _all_fonts if 'Zsye' in record.scripts ]
Doug Feltf874a192016-07-08 17:42:15 -0700342
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700343def is_pua(x):
344 return 0xE000 <= x <= 0xF8FF or 0xF0000 <= x <= 0xFFFFD or 0x100000 <= x <= 0x10FFFD
345
346def contains_pua(sequence):
347 if type(sequence) is tuple:
348 return any([is_pua(x) for x in sequence])
349 else:
350 return is_pua(sequence)
351
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700352def get_psname(ttf):
353 return str(next(x for x in ttf['name'].names
354 if x.platformID == 3 and x.platEncID == 1 and x.nameID == 6))
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700355
356def check_emoji_compat():
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700357 for emoji_font in get_emoji_fonts():
358 ttf = open_font(emoji_font)
359 psname = get_psname(ttf)
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700360
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700361 # If the font file is NotoColorEmoji, it must be Compat font.
362 if psname == 'NotoColorEmoji':
363 meta = ttf['meta']
364 assert meta, 'Compat font must have meta table'
365 assert 'Emji' in meta.data, 'meta table should have \'Emji\' data.'
366
367def check_emoji_font_coverage(emoji_fonts, all_emoji, equivalent_emoji):
368 coverages = []
369 for emoji_font in emoji_fonts:
370 coverages.append(get_emoji_map(emoji_font))
Rod Se34a19d2020-03-16 00:01:15 -0700371
372 errors = []
373
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700374 for sequence in all_emoji:
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700375 if all([sequence not in coverage for coverage in coverages]):
376 errors.append('%s is not supported in the emoji font.' % printable(sequence))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700377
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700378 for coverage in coverages:
379 for sequence in coverage:
380 if sequence in {0x0000, 0x000D, 0x0020}:
381 # The font needs to support a few extra characters, which is OK
382 continue
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700383
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700384 if contains_pua(sequence):
385 # The font needs to have some PUA for EmojiCompat library.
386 continue
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700387
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700388 if sequence not in all_emoji:
389 errors.append('%s support unexpected in the emoji font.' % printable(sequence))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700390
Haibo Huang715857d2020-03-05 11:58:47 -0800391 for first, second in equivalent_emoji.items():
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700392 for coverage in coverages:
393 if first not in coverage or second not in coverage:
394 continue # sequence will be reported missing
395 if coverage[first] != coverage[second]:
396 errors.append('%s and %s should map to the same glyph.' % (
397 printable(first),
398 printable(second)))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700399
Seigo Nonakaebdae2e2021-06-14 17:38:22 -0700400 for coverage in coverages:
401 for glyph in set(coverage.values()):
402 maps_to_glyph = [
403 seq for seq in coverage if coverage[seq] == glyph and not contains_pua(seq) ]
404 if len(maps_to_glyph) > 1:
405 # There are more than one sequences mapping to the same glyph. We
406 # need to make sure they were expected to be equivalent.
407 equivalent_seqs = set()
408 for seq in maps_to_glyph:
409 equivalent_seq = seq
410 while equivalent_seq in equivalent_emoji:
411 equivalent_seq = equivalent_emoji[equivalent_seq]
412 equivalent_seqs.add(equivalent_seq)
413 if len(equivalent_seqs) != 1:
414 errors.append('The sequences %s should not result in the same glyph %s' % (
415 printable(equivalent_seqs),
416 glyph))
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700417
Rod Se34a19d2020-03-16 00:01:15 -0700418 assert not errors, '%d emoji font errors:\n%s\n%d emoji font coverage errors' % (len(errors), '\n'.join(errors), len(errors))
419
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700420
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700421def check_emoji_defaults(default_emoji):
422 missing_text_chars = _emoji_properties['Emoji'] - default_emoji
Haibo Huang715857d2020-03-05 11:58:47 -0800423 for name, fallback_chain in _fallback_chains.items():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700424 emoji_font_seen = False
425 for record in fallback_chain:
426 if 'Zsye' in record.scripts:
427 emoji_font_seen = True
428 # No need to check the emoji font
429 continue
430 # For later fonts, we only check them if they have a script
431 # defined, since the defined script may get them to a higher
432 # score even if they appear after the emoji font. However,
433 # we should skip checking the text symbols font, since
434 # symbol fonts should be able to override the emoji display
435 # style when 'Zsym' is explicitly specified by the user.
436 if emoji_font_seen and (not record.scripts or 'Zsym' in record.scripts):
437 continue
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700438
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700439 # Check default emoji-style characters
Haibo Huang715857d2020-03-05 11:58:47 -0800440 assert_font_supports_none_of_chars(record.font, default_emoji, name)
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700441
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700442 # Mark default text-style characters appearing in fonts above the emoji
443 # font as seen
444 if not emoji_font_seen:
445 missing_text_chars -= set(get_best_cmap(record.font))
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700446
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700447 # Noto does not have monochrome glyphs for Unicode 7.0 wingdings and
448 # webdings yet.
449 missing_text_chars -= _chars_by_age['7.0']
450 assert missing_text_chars == set(), (
451 'Text style version of some emoji characters are missing: ' +
452 repr(missing_text_chars))
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700453
454
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700455# Setting reverse to true returns a dictionary that maps the values to sets of
456# characters, useful for some binary properties. Otherwise, we get a
457# dictionary that maps characters to the property values, assuming there's only
458# one property in the file.
459def parse_unicode_datafile(file_path, reverse=False):
460 if reverse:
461 output_dict = collections.defaultdict(set)
462 else:
463 output_dict = {}
464 with open(file_path) as datafile:
465 for line in datafile:
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700466 if '#' in line:
467 line = line[:line.index('#')]
468 line = line.strip()
469 if not line:
470 continue
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700471
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700472 chars, prop = line.split(';')[:2]
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700473 chars = chars.strip()
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700474 prop = prop.strip()
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700475
476 if ' ' in chars: # character sequence
477 sequence = [int(ch, 16) for ch in chars.split(' ')]
478 additions = [tuple(sequence)]
479 elif '..' in chars: # character range
480 char_start, char_end = chars.split('..')
481 char_start = int(char_start, 16)
482 char_end = int(char_end, 16)
Haibo Huang715857d2020-03-05 11:58:47 -0800483 additions = range(char_start, char_end+1)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700484 else: # singe character
485 additions = [int(chars, 16)]
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700486 if reverse:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700487 output_dict[prop].update(additions)
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700488 else:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700489 for addition in additions:
490 assert addition not in output_dict
491 output_dict[addition] = prop
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700492 return output_dict
493
494
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700495def parse_emoji_variants(file_path):
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700496 emoji_set = set()
497 text_set = set()
498 with open(file_path) as datafile:
499 for line in datafile:
500 if '#' in line:
501 line = line[:line.index('#')]
502 line = line.strip()
503 if not line:
504 continue
505 sequence, description, _ = line.split(';')
506 sequence = sequence.strip().split(' ')
507 base = int(sequence[0], 16)
508 vs = int(sequence[1], 16)
509 description = description.strip()
510 if description == 'text style':
511 text_set.add((base, vs))
512 elif description == 'emoji style':
513 emoji_set.add((base, vs))
514 return text_set, emoji_set
515
516
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700517def parse_ucd(ucd_path):
518 global _emoji_properties, _chars_by_age
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700519 global _text_variation_sequences, _emoji_variation_sequences
520 global _emoji_sequences, _emoji_zwj_sequences
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700521 _emoji_properties = parse_unicode_datafile(
522 path.join(ucd_path, 'emoji-data.txt'), reverse=True)
Roozbeh Pournaderf7a68c12017-04-04 18:59:31 -0700523 emoji_properties_additions = parse_unicode_datafile(
524 path.join(ucd_path, 'additions', 'emoji-data.txt'), reverse=True)
525 for prop in emoji_properties_additions.keys():
526 _emoji_properties[prop].update(emoji_properties_additions[prop])
527
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700528 _chars_by_age = parse_unicode_datafile(
529 path.join(ucd_path, 'DerivedAge.txt'), reverse=True)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700530 sequences = parse_emoji_variants(
531 path.join(ucd_path, 'emoji-variation-sequences.txt'))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700532 _text_variation_sequences, _emoji_variation_sequences = sequences
533 _emoji_sequences = parse_unicode_datafile(
534 path.join(ucd_path, 'emoji-sequences.txt'))
Siyamed Sinir6e06ad02017-04-19 18:18:35 -0700535 _emoji_sequences.update(parse_unicode_datafile(
536 path.join(ucd_path, 'additions', 'emoji-sequences.txt')))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700537 _emoji_zwj_sequences = parse_unicode_datafile(
538 path.join(ucd_path, 'emoji-zwj-sequences.txt'))
Roozbeh Pournader1800ba42017-03-17 18:23:23 -0700539 _emoji_zwj_sequences.update(parse_unicode_datafile(
540 path.join(ucd_path, 'additions', 'emoji-zwj-sequences.txt')))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700541
Siyamed Sinird97df5a2018-04-12 13:11:42 -0700542 exclusions = parse_unicode_datafile(path.join(ucd_path, 'additions', 'emoji-exclusions.txt'))
543 _emoji_sequences = remove_emoji_exclude(_emoji_sequences, exclusions)
544 _emoji_zwj_sequences = remove_emoji_exclude(_emoji_zwj_sequences, exclusions)
545 _emoji_variation_sequences = remove_emoji_variation_exclude(_emoji_variation_sequences, exclusions)
Qingqing Deng5e987712019-03-25 16:53:34 -0700546 # Unicode 12.0 adds Basic_Emoji in emoji-sequences.txt. We ignore them here since we are already
547 # checking the emoji presentations with emoji-variation-sequences.txt.
548 # Please refer to http://unicode.org/reports/tr51/#def_basic_emoji_set .
Haibo Huang715857d2020-03-05 11:58:47 -0800549 _emoji_sequences = {k: v for k, v in _emoji_sequences.items() if not v == 'Basic_Emoji' }
Qingqing Deng5e987712019-03-25 16:53:34 -0700550
Siyamed Sinird97df5a2018-04-12 13:11:42 -0700551
552def remove_emoji_variation_exclude(source, items):
553 return source.difference(items.keys())
554
555def remove_emoji_exclude(source, items):
556 return {k: v for k, v in source.items() if k not in items}
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700557
558def flag_sequence(territory_code):
559 return tuple(0x1F1E6 + ord(ch) - ord('A') for ch in territory_code)
560
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700561EQUIVALENT_FLAGS = {
562 flag_sequence('BV'): flag_sequence('NO'),
563 flag_sequence('CP'): flag_sequence('FR'),
564 flag_sequence('HM'): flag_sequence('AU'),
565 flag_sequence('SJ'): flag_sequence('NO'),
566 flag_sequence('UM'): flag_sequence('US'),
567}
568
569COMBINING_KEYCAP = 0x20E3
570
571LEGACY_ANDROID_EMOJI = {
572 0xFE4E5: flag_sequence('JP'),
573 0xFE4E6: flag_sequence('US'),
574 0xFE4E7: flag_sequence('FR'),
575 0xFE4E8: flag_sequence('DE'),
576 0xFE4E9: flag_sequence('IT'),
577 0xFE4EA: flag_sequence('GB'),
578 0xFE4EB: flag_sequence('ES'),
579 0xFE4EC: flag_sequence('RU'),
580 0xFE4ED: flag_sequence('CN'),
581 0xFE4EE: flag_sequence('KR'),
582 0xFE82C: (ord('#'), COMBINING_KEYCAP),
583 0xFE82E: (ord('1'), COMBINING_KEYCAP),
584 0xFE82F: (ord('2'), COMBINING_KEYCAP),
585 0xFE830: (ord('3'), COMBINING_KEYCAP),
586 0xFE831: (ord('4'), COMBINING_KEYCAP),
587 0xFE832: (ord('5'), COMBINING_KEYCAP),
588 0xFE833: (ord('6'), COMBINING_KEYCAP),
589 0xFE834: (ord('7'), COMBINING_KEYCAP),
590 0xFE835: (ord('8'), COMBINING_KEYCAP),
591 0xFE836: (ord('9'), COMBINING_KEYCAP),
592 0xFE837: (ord('0'), COMBINING_KEYCAP),
593}
594
Siyamed Sinir77a1b142018-07-12 12:02:18 -0700595# This is used to define the emoji that should have the same glyph.
596# i.e. previously we had gender based Kiss (0x1F48F), which had the same glyph
597# with Kiss: Woman, Man (0x1F469, 0x200D, 0x2764, 0x200D, 0x1F48B, 0x200D, 0x1F468)
598# in that case a valid row would be:
599# (0x1F469, 0x200D, 0x2764, 0x200D, 0x1F48B, 0x200D, 0x1F468): 0x1F48F,
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700600ZWJ_IDENTICALS = {
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700601}
602
Seigo Nonakac1808632018-05-14 13:39:40 -0700603SAME_FLAG_MAPPINGS = [
604 # Diego Garcia and British Indian Ocean Territory
605 ((0x1F1EE, 0x1F1F4), (0x1F1E9, 0x1F1EC)),
606 # St. Martin and France
607 ((0x1F1F2, 0x1F1EB), (0x1F1EB, 0x1F1F7)),
608 # Spain and Ceuta & Melilla
609 ((0x1F1EA, 0x1F1F8), (0x1F1EA, 0x1F1E6)),
610]
611
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700612ZWJ = 0x200D
Doug Feltf874a192016-07-08 17:42:15 -0700613
614def is_fitzpatrick_modifier(cp):
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700615 return 0x1F3FB <= cp <= 0x1F3FF
616
617
618def reverse_emoji(seq):
619 rev = list(reversed(seq))
620 # if there are fitzpatrick modifiers in the sequence, keep them after
621 # the emoji they modify
Haibo Huang715857d2020-03-05 11:58:47 -0800622 for i in range(1, len(rev)):
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700623 if is_fitzpatrick_modifier(rev[i-1]):
624 rev[i], rev[i-1] = rev[i-1], rev[i]
625 return tuple(rev)
Doug Feltf874a192016-07-08 17:42:15 -0700626
627
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700628def compute_expected_emoji():
629 equivalent_emoji = {}
630 sequence_pieces = set()
631 all_sequences = set()
632 all_sequences.update(_emoji_variation_sequences)
633
Raph Levien2b8b8192016-08-09 14:28:54 -0700634 # add zwj sequences not in the current emoji-zwj-sequences.txt
635 adjusted_emoji_zwj_sequences = dict(_emoji_zwj_sequences)
636 adjusted_emoji_zwj_sequences.update(_emoji_zwj_sequences)
Raph Levien2b8b8192016-08-09 14:28:54 -0700637
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700638 # Add empty flag tag sequence that is supported as fallback
639 _emoji_sequences[(0x1F3F4, 0xE007F)] = 'Emoji_Tag_Sequence'
640
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700641 for sequence in _emoji_sequences.keys():
642 sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
643 all_sequences.add(sequence)
644 sequence_pieces.update(sequence)
645
Raph Levien2b8b8192016-08-09 14:28:54 -0700646 for sequence in adjusted_emoji_zwj_sequences.keys():
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700647 sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
648 all_sequences.add(sequence)
649 sequence_pieces.update(sequence)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700650
Seigo Nonakac1808632018-05-14 13:39:40 -0700651 for first, second in SAME_FLAG_MAPPINGS:
652 equivalent_emoji[first] = second
653
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700654 # Add all tag characters used in flags
655 sequence_pieces.update(range(0xE0030, 0xE0039 + 1))
656 sequence_pieces.update(range(0xE0061, 0xE007A + 1))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700657
658 all_emoji = (
659 _emoji_properties['Emoji'] |
660 all_sequences |
661 sequence_pieces |
662 set(LEGACY_ANDROID_EMOJI.keys()))
663 default_emoji = (
664 _emoji_properties['Emoji_Presentation'] |
665 all_sequences |
666 set(LEGACY_ANDROID_EMOJI.keys()))
667
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700668 equivalent_emoji.update(EQUIVALENT_FLAGS)
669 equivalent_emoji.update(LEGACY_ANDROID_EMOJI)
670 equivalent_emoji.update(ZWJ_IDENTICALS)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700671
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700672 for seq in _emoji_variation_sequences:
673 equivalent_emoji[seq] = seq[0]
674
675 return all_emoji, default_emoji, equivalent_emoji
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700676
677
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700678def check_compact_only_fallback():
Haibo Huang715857d2020-03-05 11:58:47 -0800679 for name, fallback_chain in _fallback_chains.items():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700680 for record in fallback_chain:
681 if record.variant == 'compact':
682 same_script_elegants = [x for x in fallback_chain
683 if x.scripts == record.scripts and x.variant == 'elegant']
684 assert same_script_elegants, (
685 '%s must be in elegant of %s as fallback of "%s" too' % (
686 record.font, record.scripts, record.fallback_for),)
687
688
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700689def check_vertical_metrics():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700690 for record in _all_fonts:
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700691 if record.name in ['sans-serif', 'sans-serif-condensed']:
692 font = open_font(record.font)
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700693 assert font['head'].yMax == 2163 and font['head'].yMin == -555, (
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700694 'yMax and yMin of %s do not match expected values.' % (
695 record.font,))
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700696
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700697 if record.name in ['sans-serif', 'sans-serif-condensed',
698 'serif', 'monospace']:
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700699 font = open_font(record.font)
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700700 assert (font['hhea'].ascent == 1900 and
701 font['hhea'].descent == -500), (
702 'ascent and descent of %s do not match expected '
703 'values.' % (record.font,))
704
705
706def check_cjk_punctuation():
707 cjk_scripts = {'Hans', 'Hant', 'Jpan', 'Kore'}
708 cjk_punctuation = range(0x3000, 0x301F + 1)
Haibo Huang715857d2020-03-05 11:58:47 -0800709 for name, fallback_chain in _fallback_chains.items():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700710 for record in fallback_chain:
711 if record.scripts.intersection(cjk_scripts):
712 # CJK font seen. Stop checking the rest of the fonts.
713 break
714 assert_font_supports_none_of_chars(record.font, cjk_punctuation, name)
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700715
Seigo Nonaka56811242021-04-16 00:11:43 -0700716def getPostScriptName(font):
717 font_file, index = font
718 font_path = path.join(_fonts_dir, font_file)
719 if index is not None:
720 # Use the first font file in the collection for resolving post script name.
721 ttf = ttLib.TTFont(font_path, fontNumber=0)
722 else:
723 ttf = ttLib.TTFont(font_path)
724
725 nameTable = ttf['name']
726 for name in nameTable.names:
727 if (name.nameID == 6 and name.platformID == 3 and name.platEncID == 1
728 and name.langID == 0x0409):
729 return str(name)
730
731def check_canonical_name():
732 for record in _all_fonts:
733 file_name, index = record.font
734
735 psName = getPostScriptName(record.font)
736 if record.psName:
737 # If fonts element has postScriptName attribute, it should match with the PostScript
738 # name in the name table.
739 assert psName == record.psName, ('postScriptName attribute %s should match with %s' % (
740 record.psName, psName))
741 else:
742 # If fonts element doesn't have postScriptName attribute, the file name should match
743 # with the PostScript name in the name table.
744 assert psName == file_name[:-4], ('file name %s should match with %s' % (
745 file_name, psName))
746
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700747
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800748def main():
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800749 global _fonts_dir
Doug Feltf874a192016-07-08 17:42:15 -0700750 target_out = sys.argv[1]
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800751 _fonts_dir = path.join(target_out, 'fonts')
752
753 fonts_xml_path = path.join(target_out, 'etc', 'fonts.xml')
754 parse_fonts_xml(fonts_xml_path)
755
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700756 check_compact_only_fallback()
757
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700758 check_vertical_metrics()
759
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800760 hyphens_dir = path.join(target_out, 'usr', 'hyphen-data')
761 check_hyphens(hyphens_dir)
762
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700763 check_cjk_punctuation()
764
Seigo Nonaka56811242021-04-16 00:11:43 -0700765 check_canonical_name()
766
Roozbeh Pournader27ec3ac2016-03-31 13:05:32 -0700767 check_emoji = sys.argv[2]
768 if check_emoji == 'true':
769 ucd_path = sys.argv[3]
770 parse_ucd(ucd_path)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700771 all_emoji, default_emoji, equivalent_emoji = compute_expected_emoji()
Seigo Nonaka2e9e7922021-05-04 17:12:35 -0700772 check_emoji_compat()
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700773 check_emoji_coverage(all_emoji, equivalent_emoji)
774 check_emoji_defaults(default_emoji)
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700775
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800776
777if __name__ == '__main__':
778 main()