blob: f191f6c0ab78220ddda4e6d19af4a657c3b11a86 [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
7import sys
8from xml.etree import ElementTree
9
10from fontTools import ttLib
11
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070012EMOJI_VS = 0xFE0F
13
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080014LANG_TO_SCRIPT = {
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070015 'as': 'Beng',
Roozbeh Pournader7e04dd12017-10-13 17:41:31 -070016 'be': 'Cyrl',
Roozbeh Pournader033b2222017-02-22 18:53:39 -080017 'bg': 'Cyrl',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070018 'bn': 'Beng',
Roozbeh Pournader033b2222017-02-22 18:53:39 -080019 'cu': 'Cyrl',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070020 'cy': 'Latn',
21 'da': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080022 'de': 'Latn',
23 'en': 'Latn',
24 'es': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070025 'et': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080026 'eu': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070027 'fr': 'Latn',
28 'ga': 'Latn',
29 'gu': 'Gujr',
30 'hi': 'Deva',
31 'hr': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080032 'hu': 'Latn',
33 'hy': 'Armn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070034 'ja': 'Jpan',
35 'kn': 'Knda',
36 'ko': 'Kore',
Roozbeh Pournader7e04dd12017-10-13 17:41:31 -070037 'la': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070038 'ml': 'Mlym',
39 'mn': 'Cyrl',
40 'mr': 'Deva',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080041 'nb': 'Latn',
42 'nn': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070043 'or': 'Orya',
44 'pa': 'Guru',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080045 'pt': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070046 'sl': 'Latn',
47 'ta': 'Taml',
48 'te': 'Telu',
49 'tk': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080050}
51
52def lang_to_script(lang_code):
53 lang = lang_code.lower()
54 while lang not in LANG_TO_SCRIPT:
55 hyphen_idx = lang.rfind('-')
56 assert hyphen_idx != -1, (
57 'We do not know what script the "%s" language is written in.'
58 % lang_code)
59 assumed_script = lang[hyphen_idx+1:]
60 if len(assumed_script) == 4 and assumed_script.isalpha():
61 # This is actually the script
62 return assumed_script.title()
63 lang = lang[:hyphen_idx]
64 return LANG_TO_SCRIPT[lang]
65
66
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070067def printable(inp):
68 if type(inp) is set: # set of character sequences
69 return '{' + ', '.join([printable(seq) for seq in inp]) + '}'
70 if type(inp) is tuple: # character sequence
71 return '<' + (', '.join([printable(ch) for ch in inp])) + '>'
72 else: # single character
73 return 'U+%04X' % inp
74
75
76def open_font(font):
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080077 font_file, index = font
78 font_path = path.join(_fonts_dir, font_file)
79 if index is not None:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070080 return ttLib.TTFont(font_path, fontNumber=index)
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080081 else:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -070082 return ttLib.TTFont(font_path)
83
84
85def get_best_cmap(font):
86 ttfont = open_font(font)
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080087 all_unicode_cmap = None
88 bmp_cmap = None
89 for cmap in ttfont['cmap'].tables:
90 specifier = (cmap.format, cmap.platformID, cmap.platEncID)
91 if specifier == (4, 3, 1):
92 assert bmp_cmap is None, 'More than one BMP cmap in %s' % (font, )
93 bmp_cmap = cmap
94 elif specifier == (12, 3, 10):
95 assert all_unicode_cmap is None, (
96 'More than one UCS-4 cmap in %s' % (font, ))
97 all_unicode_cmap = cmap
98
99 return all_unicode_cmap.cmap if all_unicode_cmap else bmp_cmap.cmap
100
101
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700102def get_variation_sequences_cmap(font):
103 ttfont = open_font(font)
104 vs_cmap = None
105 for cmap in ttfont['cmap'].tables:
106 specifier = (cmap.format, cmap.platformID, cmap.platEncID)
107 if specifier == (14, 0, 5):
108 assert vs_cmap is None, 'More than one VS cmap in %s' % (font, )
109 vs_cmap = cmap
110 return vs_cmap
111
112
113def get_emoji_map(font):
114 # Add normal characters
115 emoji_map = copy.copy(get_best_cmap(font))
116 reverse_cmap = {glyph: code for code, glyph in emoji_map.items()}
117
118 # Add variation sequences
119 vs_dict = get_variation_sequences_cmap(font).uvsDict
120 for vs in vs_dict:
121 for base, glyph in vs_dict[vs]:
122 if glyph is None:
123 emoji_map[(base, vs)] = emoji_map[base]
124 else:
125 emoji_map[(base, vs)] = glyph
126
127 # Add GSUB rules
128 ttfont = open_font(font)
129 for lookup in ttfont['GSUB'].table.LookupList.Lookup:
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700130 if lookup.LookupType != 4:
131 # Other lookups are used in the emoji font for fallback.
132 # We ignore them for now.
133 continue
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700134 for subtable in lookup.SubTable:
135 ligatures = subtable.ligatures
136 for first_glyph in ligatures:
137 for ligature in ligatures[first_glyph]:
138 sequence = [first_glyph] + ligature.Component
139 sequence = [reverse_cmap[glyph] for glyph in sequence]
140 sequence = tuple(sequence)
141 # Make sure no starting subsequence of 'sequence' has been
142 # seen before.
143 for sub_len in range(2, len(sequence)+1):
144 subsequence = sequence[:sub_len]
145 assert subsequence not in emoji_map
146 emoji_map[sequence] = ligature.LigGlyph
147
148 return emoji_map
149
150
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800151def assert_font_supports_any_of_chars(font, chars):
152 best_cmap = get_best_cmap(font)
153 for char in chars:
154 if char in best_cmap:
155 return
156 sys.exit('None of characters in %s were found in %s' % (chars, font))
157
158
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700159def assert_font_supports_all_of_chars(font, chars):
160 best_cmap = get_best_cmap(font)
161 for char in chars:
162 assert char in best_cmap, (
163 'U+%04X was not found in %s' % (char, font))
164
165
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700166def assert_font_supports_none_of_chars(font, chars, fallbackName):
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700167 best_cmap = get_best_cmap(font)
168 for char in chars:
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700169 if fallbackName:
170 assert char not in best_cmap, 'U+%04X was found in %s' % (char, font)
171 else:
172 assert char not in best_cmap, (
173 'U+%04X was found in %s in fallback %s' % (char, font, fallbackName))
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700174
175
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700176def assert_font_supports_all_sequences(font, sequences):
177 vs_dict = get_variation_sequences_cmap(font).uvsDict
178 for base, vs in sorted(sequences):
179 assert vs in vs_dict and (base, None) in vs_dict[vs], (
180 '<U+%04X, U+%04X> was not found in %s' % (base, vs, font))
181
182
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800183def check_hyphens(hyphens_dir):
184 # Find all the scripts that need automatic hyphenation
185 scripts = set()
186 for hyb_file in glob.iglob(path.join(hyphens_dir, '*.hyb')):
187 hyb_file = path.basename(hyb_file)
188 assert hyb_file.startswith('hyph-'), (
189 'Unknown hyphenation file %s' % hyb_file)
190 lang_code = hyb_file[hyb_file.index('-')+1:hyb_file.index('.')]
191 scripts.add(lang_to_script(lang_code))
192
193 HYPHENS = {0x002D, 0x2010}
194 for script in scripts:
195 fonts = _script_to_font_map[script]
196 assert fonts, 'No fonts found for the "%s" script' % script
197 for font in fonts:
198 assert_font_supports_any_of_chars(font, HYPHENS)
199
200
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700201class FontRecord(object):
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700202 def __init__(self, name, scripts, variant, weight, style, fallback_for, font):
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700203 self.name = name
204 self.scripts = scripts
205 self.variant = variant
206 self.weight = weight
207 self.style = style
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700208 self.fallback_for = fallback_for
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700209 self.font = font
210
211
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800212def parse_fonts_xml(fonts_xml_path):
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700213 global _script_to_font_map, _fallback_chains, _all_fonts
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800214 _script_to_font_map = collections.defaultdict(set)
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700215 _fallback_chains = {}
216 _all_fonts = []
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800217 tree = ElementTree.parse(fonts_xml_path)
Seigo Nonaka9092dc22017-01-06 16:54:52 +0900218 families = tree.findall('family')
219 # Minikin supports up to 254 but users can place their own font at the first
220 # place. Thus, 253 is the maximum allowed number of font families in the
221 # default collection.
222 assert len(families) < 254, (
223 'System font collection can contains up to 253 font families.')
224 for family in families:
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800225 name = family.get('name')
226 variant = family.get('variant')
227 langs = family.get('lang')
228 if name:
229 assert variant is None, (
230 'No variant expected for LGC font %s.' % name)
231 assert langs is None, (
232 'No language expected for LGC fonts %s.' % name)
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700233 assert name not in _fallback_chains, 'Duplicated name entry %s' % name
234 _fallback_chains[name] = []
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800235 else:
236 assert variant in {None, 'elegant', 'compact'}, (
237 'Unexpected value for variant: %s' % variant)
238
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700239 for family in families:
240 name = family.get('name')
241 variant = family.get('variant')
242 langs = family.get('lang')
243
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800244 if langs:
245 langs = langs.split()
246 scripts = {lang_to_script(lang) for lang in langs}
247 else:
248 scripts = set()
249
250 for child in family:
251 assert child.tag == 'font', (
252 'Unknown tag <%s>' % child.tag)
Jungshik Shin88b11142017-03-17 14:56:17 -0700253 font_file = child.text.rstrip()
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800254 weight = int(child.get('weight'))
255 assert weight % 100 == 0, (
256 'Font weight "%d" is not a multiple of 100.' % weight)
257
258 style = child.get('style')
259 assert style in {'normal', 'italic'}, (
260 'Unknown style "%s"' % style)
261
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700262 fallback_for = child.get('fallbackFor')
263
264 assert not name or not fallback_for, (
265 'name and fallbackFor cannot be present at the same time')
266 assert not fallback_for or fallback_for in _fallback_chains, (
267 'Unknown fallback name: %s' % fallback_for)
268
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800269 index = child.get('index')
270 if index:
271 index = int(index)
272
Seigo Nonaka1403ff22018-01-18 17:24:31 -0800273 if not path.exists(path.join(_fonts_dir, font_file)):
274 continue # Missing font is a valid case. Just ignore the missing font files.
275
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700276 record = FontRecord(
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800277 name,
278 frozenset(scripts),
279 variant,
280 weight,
281 style,
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700282 fallback_for,
283 (font_file, index))
284
285 _all_fonts.append(record)
286
287 if not fallback_for:
288 if not name or name == 'sans-serif':
289 for _, fallback in _fallback_chains.iteritems():
290 fallback.append(record)
291 else:
292 _fallback_chains[name].append(record)
293 else:
294 _fallback_chains[fallback_for].append(record)
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800295
296 if name: # non-empty names are used for default LGC fonts
297 map_scripts = {'Latn', 'Grek', 'Cyrl'}
298 else:
299 map_scripts = scripts
300 for script in map_scripts:
301 _script_to_font_map[script].add((font_file, index))
302
303
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700304def check_emoji_coverage(all_emoji, equivalent_emoji):
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700305 emoji_font = get_emoji_font()
306 check_emoji_font_coverage(emoji_font, all_emoji, equivalent_emoji)
Doug Feltf874a192016-07-08 17:42:15 -0700307
308
309def get_emoji_font():
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700310 emoji_fonts = [
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700311 record.font for record in _all_fonts
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700312 if 'Zsye' in record.scripts]
Roozbeh Pournader27ec3ac2016-03-31 13:05:32 -0700313 assert len(emoji_fonts) == 1, 'There are %d emoji fonts.' % len(emoji_fonts)
Doug Feltf874a192016-07-08 17:42:15 -0700314 return emoji_fonts[0]
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700315
Doug Feltf874a192016-07-08 17:42:15 -0700316
317def check_emoji_font_coverage(emoji_font, all_emoji, equivalent_emoji):
318 coverage = get_emoji_map(emoji_font)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700319 for sequence in all_emoji:
320 assert sequence in coverage, (
321 '%s is not supported in the emoji font.' % printable(sequence))
322
323 for sequence in coverage:
324 if sequence in {0x0000, 0x000D, 0x0020}:
325 # The font needs to support a few extra characters, which is OK
326 continue
327 assert sequence in all_emoji, (
328 'Emoji font should not support %s.' % printable(sequence))
329
330 for first, second in sorted(equivalent_emoji.items()):
331 assert coverage[first] == coverage[second], (
332 '%s and %s should map to the same glyph.' % (
333 printable(first),
334 printable(second)))
335
336 for glyph in set(coverage.values()):
337 maps_to_glyph = [seq for seq in coverage if coverage[seq] == glyph]
338 if len(maps_to_glyph) > 1:
339 # There are more than one sequences mapping to the same glyph. We
340 # need to make sure they were expected to be equivalent.
341 equivalent_seqs = set()
342 for seq in maps_to_glyph:
343 equivalent_seq = seq
344 while equivalent_seq in equivalent_emoji:
345 equivalent_seq = equivalent_emoji[equivalent_seq]
346 equivalent_seqs.add(equivalent_seq)
347 assert len(equivalent_seqs) == 1, (
348 'The sequences %s should not result in the same glyph %s' % (
349 printable(equivalent_seqs),
350 glyph))
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700351
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700352
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700353def check_emoji_defaults(default_emoji):
354 missing_text_chars = _emoji_properties['Emoji'] - default_emoji
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700355 for name, fallback_chain in _fallback_chains.iteritems():
356 emoji_font_seen = False
357 for record in fallback_chain:
358 if 'Zsye' in record.scripts:
359 emoji_font_seen = True
360 # No need to check the emoji font
361 continue
362 # For later fonts, we only check them if they have a script
363 # defined, since the defined script may get them to a higher
364 # score even if they appear after the emoji font. However,
365 # we should skip checking the text symbols font, since
366 # symbol fonts should be able to override the emoji display
367 # style when 'Zsym' is explicitly specified by the user.
368 if emoji_font_seen and (not record.scripts or 'Zsym' in record.scripts):
369 continue
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700370
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700371 # Check default emoji-style characters
372 assert_font_supports_none_of_chars(record.font, sorted(default_emoji), name)
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700373
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700374 # Mark default text-style characters appearing in fonts above the emoji
375 # font as seen
376 if not emoji_font_seen:
377 missing_text_chars -= set(get_best_cmap(record.font))
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700378
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700379 # Noto does not have monochrome glyphs for Unicode 7.0 wingdings and
380 # webdings yet.
381 missing_text_chars -= _chars_by_age['7.0']
382 assert missing_text_chars == set(), (
383 'Text style version of some emoji characters are missing: ' +
384 repr(missing_text_chars))
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700385
386
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700387# Setting reverse to true returns a dictionary that maps the values to sets of
388# characters, useful for some binary properties. Otherwise, we get a
389# dictionary that maps characters to the property values, assuming there's only
390# one property in the file.
391def parse_unicode_datafile(file_path, reverse=False):
392 if reverse:
393 output_dict = collections.defaultdict(set)
394 else:
395 output_dict = {}
396 with open(file_path) as datafile:
397 for line in datafile:
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700398 if '#' in line:
399 line = line[:line.index('#')]
400 line = line.strip()
401 if not line:
402 continue
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700403
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700404 chars, prop = line.split(';')[:2]
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700405 chars = chars.strip()
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700406 prop = prop.strip()
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700407
408 if ' ' in chars: # character sequence
409 sequence = [int(ch, 16) for ch in chars.split(' ')]
410 additions = [tuple(sequence)]
411 elif '..' in chars: # character range
412 char_start, char_end = chars.split('..')
413 char_start = int(char_start, 16)
414 char_end = int(char_end, 16)
415 additions = xrange(char_start, char_end+1)
416 else: # singe character
417 additions = [int(chars, 16)]
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700418 if reverse:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700419 output_dict[prop].update(additions)
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700420 else:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700421 for addition in additions:
422 assert addition not in output_dict
423 output_dict[addition] = prop
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700424 return output_dict
425
426
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700427def parse_emoji_variants(file_path):
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700428 emoji_set = set()
429 text_set = set()
430 with open(file_path) as datafile:
431 for line in datafile:
432 if '#' in line:
433 line = line[:line.index('#')]
434 line = line.strip()
435 if not line:
436 continue
437 sequence, description, _ = line.split(';')
438 sequence = sequence.strip().split(' ')
439 base = int(sequence[0], 16)
440 vs = int(sequence[1], 16)
441 description = description.strip()
442 if description == 'text style':
443 text_set.add((base, vs))
444 elif description == 'emoji style':
445 emoji_set.add((base, vs))
446 return text_set, emoji_set
447
448
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700449def parse_ucd(ucd_path):
450 global _emoji_properties, _chars_by_age
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700451 global _text_variation_sequences, _emoji_variation_sequences
452 global _emoji_sequences, _emoji_zwj_sequences
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700453 _emoji_properties = parse_unicode_datafile(
454 path.join(ucd_path, 'emoji-data.txt'), reverse=True)
Roozbeh Pournaderf7a68c12017-04-04 18:59:31 -0700455 emoji_properties_additions = parse_unicode_datafile(
456 path.join(ucd_path, 'additions', 'emoji-data.txt'), reverse=True)
457 for prop in emoji_properties_additions.keys():
458 _emoji_properties[prop].update(emoji_properties_additions[prop])
459
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700460 _chars_by_age = parse_unicode_datafile(
461 path.join(ucd_path, 'DerivedAge.txt'), reverse=True)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700462 sequences = parse_emoji_variants(
463 path.join(ucd_path, 'emoji-variation-sequences.txt'))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700464 _text_variation_sequences, _emoji_variation_sequences = sequences
465 _emoji_sequences = parse_unicode_datafile(
466 path.join(ucd_path, 'emoji-sequences.txt'))
Siyamed Sinir6e06ad02017-04-19 18:18:35 -0700467 _emoji_sequences.update(parse_unicode_datafile(
468 path.join(ucd_path, 'additions', 'emoji-sequences.txt')))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700469 _emoji_zwj_sequences = parse_unicode_datafile(
470 path.join(ucd_path, 'emoji-zwj-sequences.txt'))
Roozbeh Pournader1800ba42017-03-17 18:23:23 -0700471 _emoji_zwj_sequences.update(parse_unicode_datafile(
472 path.join(ucd_path, 'additions', 'emoji-zwj-sequences.txt')))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700473
Siyamed Sinird97df5a2018-04-12 13:11:42 -0700474 exclusions = parse_unicode_datafile(path.join(ucd_path, 'additions', 'emoji-exclusions.txt'))
475 _emoji_sequences = remove_emoji_exclude(_emoji_sequences, exclusions)
476 _emoji_zwj_sequences = remove_emoji_exclude(_emoji_zwj_sequences, exclusions)
477 _emoji_variation_sequences = remove_emoji_variation_exclude(_emoji_variation_sequences, exclusions)
Qingqing Deng5e987712019-03-25 16:53:34 -0700478 # Unicode 12.0 adds Basic_Emoji in emoji-sequences.txt. We ignore them here since we are already
479 # checking the emoji presentations with emoji-variation-sequences.txt.
480 # Please refer to http://unicode.org/reports/tr51/#def_basic_emoji_set .
481 _emoji_sequences = {k: v for k, v in _emoji_sequences.iteritems() if not v == 'Basic_Emoji' }
482
Siyamed Sinird97df5a2018-04-12 13:11:42 -0700483
484def remove_emoji_variation_exclude(source, items):
485 return source.difference(items.keys())
486
487def remove_emoji_exclude(source, items):
488 return {k: v for k, v in source.items() if k not in items}
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700489
490def flag_sequence(territory_code):
491 return tuple(0x1F1E6 + ord(ch) - ord('A') for ch in territory_code)
492
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700493UNSUPPORTED_FLAGS = frozenset({
Seigo Nonakac1808632018-05-14 13:39:40 -0700494 flag_sequence('BL'), flag_sequence('BQ'), flag_sequence('MQ'),
495 flag_sequence('RE'), flag_sequence('TF'),
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700496})
497
498EQUIVALENT_FLAGS = {
499 flag_sequence('BV'): flag_sequence('NO'),
500 flag_sequence('CP'): flag_sequence('FR'),
501 flag_sequence('HM'): flag_sequence('AU'),
502 flag_sequence('SJ'): flag_sequence('NO'),
503 flag_sequence('UM'): flag_sequence('US'),
504}
505
506COMBINING_KEYCAP = 0x20E3
507
508LEGACY_ANDROID_EMOJI = {
509 0xFE4E5: flag_sequence('JP'),
510 0xFE4E6: flag_sequence('US'),
511 0xFE4E7: flag_sequence('FR'),
512 0xFE4E8: flag_sequence('DE'),
513 0xFE4E9: flag_sequence('IT'),
514 0xFE4EA: flag_sequence('GB'),
515 0xFE4EB: flag_sequence('ES'),
516 0xFE4EC: flag_sequence('RU'),
517 0xFE4ED: flag_sequence('CN'),
518 0xFE4EE: flag_sequence('KR'),
519 0xFE82C: (ord('#'), COMBINING_KEYCAP),
520 0xFE82E: (ord('1'), COMBINING_KEYCAP),
521 0xFE82F: (ord('2'), COMBINING_KEYCAP),
522 0xFE830: (ord('3'), COMBINING_KEYCAP),
523 0xFE831: (ord('4'), COMBINING_KEYCAP),
524 0xFE832: (ord('5'), COMBINING_KEYCAP),
525 0xFE833: (ord('6'), COMBINING_KEYCAP),
526 0xFE834: (ord('7'), COMBINING_KEYCAP),
527 0xFE835: (ord('8'), COMBINING_KEYCAP),
528 0xFE836: (ord('9'), COMBINING_KEYCAP),
529 0xFE837: (ord('0'), COMBINING_KEYCAP),
530}
531
Siyamed Sinir77a1b142018-07-12 12:02:18 -0700532# This is used to define the emoji that should have the same glyph.
533# i.e. previously we had gender based Kiss (0x1F48F), which had the same glyph
534# with Kiss: Woman, Man (0x1F469, 0x200D, 0x2764, 0x200D, 0x1F48B, 0x200D, 0x1F468)
535# in that case a valid row would be:
536# (0x1F469, 0x200D, 0x2764, 0x200D, 0x1F48B, 0x200D, 0x1F468): 0x1F48F,
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700537ZWJ_IDENTICALS = {
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700538}
539
Seigo Nonakac1808632018-05-14 13:39:40 -0700540SAME_FLAG_MAPPINGS = [
541 # Diego Garcia and British Indian Ocean Territory
542 ((0x1F1EE, 0x1F1F4), (0x1F1E9, 0x1F1EC)),
543 # St. Martin and France
544 ((0x1F1F2, 0x1F1EB), (0x1F1EB, 0x1F1F7)),
545 # Spain and Ceuta & Melilla
546 ((0x1F1EA, 0x1F1F8), (0x1F1EA, 0x1F1E6)),
547]
548
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700549ZWJ = 0x200D
Doug Feltf874a192016-07-08 17:42:15 -0700550
551def is_fitzpatrick_modifier(cp):
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700552 return 0x1F3FB <= cp <= 0x1F3FF
553
554
555def reverse_emoji(seq):
556 rev = list(reversed(seq))
557 # if there are fitzpatrick modifiers in the sequence, keep them after
558 # the emoji they modify
559 for i in xrange(1, len(rev)):
560 if is_fitzpatrick_modifier(rev[i-1]):
561 rev[i], rev[i-1] = rev[i-1], rev[i]
562 return tuple(rev)
Doug Feltf874a192016-07-08 17:42:15 -0700563
564
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700565def compute_expected_emoji():
566 equivalent_emoji = {}
567 sequence_pieces = set()
568 all_sequences = set()
569 all_sequences.update(_emoji_variation_sequences)
570
Raph Levien2b8b8192016-08-09 14:28:54 -0700571 # add zwj sequences not in the current emoji-zwj-sequences.txt
572 adjusted_emoji_zwj_sequences = dict(_emoji_zwj_sequences)
573 adjusted_emoji_zwj_sequences.update(_emoji_zwj_sequences)
Raph Levien2b8b8192016-08-09 14:28:54 -0700574
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700575 # Add empty flag tag sequence that is supported as fallback
576 _emoji_sequences[(0x1F3F4, 0xE007F)] = 'Emoji_Tag_Sequence'
577
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700578 for sequence in _emoji_sequences.keys():
579 sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
580 all_sequences.add(sequence)
581 sequence_pieces.update(sequence)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700582 if _emoji_sequences.get(sequence, None) == 'Emoji_Tag_Sequence':
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700583 # Add reverse of all emoji ZWJ sequences, which are added to the
584 # fonts as a workaround to get the sequences work in RTL text.
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700585 # TODO: test if these are actually needed by Minikin/HarfBuzz.
586 reversed_seq = reverse_emoji(sequence)
587 all_sequences.add(reversed_seq)
588 equivalent_emoji[reversed_seq] = sequence
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700589
Raph Levien2b8b8192016-08-09 14:28:54 -0700590 for sequence in adjusted_emoji_zwj_sequences.keys():
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700591 sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
592 all_sequences.add(sequence)
593 sequence_pieces.update(sequence)
594 # Add reverse of all emoji ZWJ sequences, which are added to the fonts
595 # as a workaround to get the sequences work in RTL text.
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700596 reversed_seq = reverse_emoji(sequence)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700597 all_sequences.add(reversed_seq)
598 equivalent_emoji[reversed_seq] = sequence
599
Seigo Nonakac1808632018-05-14 13:39:40 -0700600 for first, second in SAME_FLAG_MAPPINGS:
601 equivalent_emoji[first] = second
602
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700603 # Remove unsupported flags
604 all_sequences.difference_update(UNSUPPORTED_FLAGS)
605
606 # Add all tag characters used in flags
607 sequence_pieces.update(range(0xE0030, 0xE0039 + 1))
608 sequence_pieces.update(range(0xE0061, 0xE007A + 1))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700609
610 all_emoji = (
611 _emoji_properties['Emoji'] |
612 all_sequences |
613 sequence_pieces |
614 set(LEGACY_ANDROID_EMOJI.keys()))
615 default_emoji = (
616 _emoji_properties['Emoji_Presentation'] |
617 all_sequences |
618 set(LEGACY_ANDROID_EMOJI.keys()))
619
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700620 equivalent_emoji.update(EQUIVALENT_FLAGS)
621 equivalent_emoji.update(LEGACY_ANDROID_EMOJI)
622 equivalent_emoji.update(ZWJ_IDENTICALS)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700623
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700624 for seq in _emoji_variation_sequences:
625 equivalent_emoji[seq] = seq[0]
626
627 return all_emoji, default_emoji, equivalent_emoji
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700628
629
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700630def check_compact_only_fallback():
631 for name, fallback_chain in _fallback_chains.iteritems():
632 for record in fallback_chain:
633 if record.variant == 'compact':
634 same_script_elegants = [x for x in fallback_chain
635 if x.scripts == record.scripts and x.variant == 'elegant']
636 assert same_script_elegants, (
637 '%s must be in elegant of %s as fallback of "%s" too' % (
638 record.font, record.scripts, record.fallback_for),)
639
640
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700641def check_vertical_metrics():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700642 for record in _all_fonts:
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700643 if record.name in ['sans-serif', 'sans-serif-condensed']:
644 font = open_font(record.font)
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700645 assert font['head'].yMax == 2163 and font['head'].yMin == -555, (
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700646 'yMax and yMin of %s do not match expected values.' % (
647 record.font,))
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700648
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700649 if record.name in ['sans-serif', 'sans-serif-condensed',
650 'serif', 'monospace']:
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700651 font = open_font(record.font)
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700652 assert (font['hhea'].ascent == 1900 and
653 font['hhea'].descent == -500), (
654 'ascent and descent of %s do not match expected '
655 'values.' % (record.font,))
656
657
658def check_cjk_punctuation():
659 cjk_scripts = {'Hans', 'Hant', 'Jpan', 'Kore'}
660 cjk_punctuation = range(0x3000, 0x301F + 1)
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700661 for name, fallback_chain in _fallback_chains.iteritems():
662 for record in fallback_chain:
663 if record.scripts.intersection(cjk_scripts):
664 # CJK font seen. Stop checking the rest of the fonts.
665 break
666 assert_font_supports_none_of_chars(record.font, cjk_punctuation, name)
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700667
668
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800669def main():
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800670 global _fonts_dir
Doug Feltf874a192016-07-08 17:42:15 -0700671 target_out = sys.argv[1]
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800672 _fonts_dir = path.join(target_out, 'fonts')
673
674 fonts_xml_path = path.join(target_out, 'etc', 'fonts.xml')
675 parse_fonts_xml(fonts_xml_path)
676
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700677 check_compact_only_fallback()
678
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700679 check_vertical_metrics()
680
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800681 hyphens_dir = path.join(target_out, 'usr', 'hyphen-data')
682 check_hyphens(hyphens_dir)
683
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700684 check_cjk_punctuation()
685
Roozbeh Pournader27ec3ac2016-03-31 13:05:32 -0700686 check_emoji = sys.argv[2]
687 if check_emoji == 'true':
688 ucd_path = sys.argv[3]
689 parse_ucd(ucd_path)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700690 all_emoji, default_emoji, equivalent_emoji = compute_expected_emoji()
691 check_emoji_coverage(all_emoji, equivalent_emoji)
692 check_emoji_defaults(default_emoji)
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700693
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800694
695if __name__ == '__main__':
696 main()