blob: dcb90e411d34529a09212bbd69e12a1977f7a6bc [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
166def assert_font_supports_none_of_chars(font, chars):
167 best_cmap = get_best_cmap(font)
168 for char in chars:
169 assert char not in best_cmap, (
170 'U+%04X was found in %s' % (char, font))
171
172
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700173def assert_font_supports_all_sequences(font, sequences):
174 vs_dict = get_variation_sequences_cmap(font).uvsDict
175 for base, vs in sorted(sequences):
176 assert vs in vs_dict and (base, None) in vs_dict[vs], (
177 '<U+%04X, U+%04X> was not found in %s' % (base, vs, font))
178
179
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800180def check_hyphens(hyphens_dir):
181 # Find all the scripts that need automatic hyphenation
182 scripts = set()
183 for hyb_file in glob.iglob(path.join(hyphens_dir, '*.hyb')):
184 hyb_file = path.basename(hyb_file)
185 assert hyb_file.startswith('hyph-'), (
186 'Unknown hyphenation file %s' % hyb_file)
187 lang_code = hyb_file[hyb_file.index('-')+1:hyb_file.index('.')]
188 scripts.add(lang_to_script(lang_code))
189
190 HYPHENS = {0x002D, 0x2010}
191 for script in scripts:
192 fonts = _script_to_font_map[script]
193 assert fonts, 'No fonts found for the "%s" script' % script
194 for font in fonts:
195 assert_font_supports_any_of_chars(font, HYPHENS)
196
197
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700198class FontRecord(object):
199 def __init__(self, name, scripts, variant, weight, style, font):
200 self.name = name
201 self.scripts = scripts
202 self.variant = variant
203 self.weight = weight
204 self.style = style
205 self.font = font
206
207
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800208def parse_fonts_xml(fonts_xml_path):
209 global _script_to_font_map, _fallback_chain
210 _script_to_font_map = collections.defaultdict(set)
211 _fallback_chain = []
212 tree = ElementTree.parse(fonts_xml_path)
Seigo Nonaka9092dc22017-01-06 16:54:52 +0900213 families = tree.findall('family')
214 # Minikin supports up to 254 but users can place their own font at the first
215 # place. Thus, 253 is the maximum allowed number of font families in the
216 # default collection.
217 assert len(families) < 254, (
218 'System font collection can contains up to 253 font families.')
219 for family in families:
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800220 name = family.get('name')
221 variant = family.get('variant')
222 langs = family.get('lang')
223 if name:
224 assert variant is None, (
225 'No variant expected for LGC font %s.' % name)
226 assert langs is None, (
227 'No language expected for LGC fonts %s.' % name)
228 else:
229 assert variant in {None, 'elegant', 'compact'}, (
230 'Unexpected value for variant: %s' % variant)
231
232 if langs:
233 langs = langs.split()
234 scripts = {lang_to_script(lang) for lang in langs}
235 else:
236 scripts = set()
237
238 for child in family:
239 assert child.tag == 'font', (
240 'Unknown tag <%s>' % child.tag)
Jungshik Shin88b11142017-03-17 14:56:17 -0700241 font_file = child.text.rstrip()
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800242 weight = int(child.get('weight'))
243 assert weight % 100 == 0, (
244 'Font weight "%d" is not a multiple of 100.' % weight)
245
246 style = child.get('style')
247 assert style in {'normal', 'italic'}, (
248 'Unknown style "%s"' % style)
249
250 index = child.get('index')
251 if index:
252 index = int(index)
253
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700254 _fallback_chain.append(FontRecord(
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800255 name,
256 frozenset(scripts),
257 variant,
258 weight,
259 style,
260 (font_file, index)))
261
262 if name: # non-empty names are used for default LGC fonts
263 map_scripts = {'Latn', 'Grek', 'Cyrl'}
264 else:
265 map_scripts = scripts
266 for script in map_scripts:
267 _script_to_font_map[script].add((font_file, index))
268
269
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700270def check_emoji_coverage(all_emoji, equivalent_emoji):
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700271 emoji_font = get_emoji_font()
272 check_emoji_font_coverage(emoji_font, all_emoji, equivalent_emoji)
Doug Feltf874a192016-07-08 17:42:15 -0700273
274
275def get_emoji_font():
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700276 emoji_fonts = [
277 record.font for record in _fallback_chain
278 if 'Zsye' in record.scripts]
Roozbeh Pournader27ec3ac2016-03-31 13:05:32 -0700279 assert len(emoji_fonts) == 1, 'There are %d emoji fonts.' % len(emoji_fonts)
Doug Feltf874a192016-07-08 17:42:15 -0700280 return emoji_fonts[0]
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700281
Doug Feltf874a192016-07-08 17:42:15 -0700282
283def check_emoji_font_coverage(emoji_font, all_emoji, equivalent_emoji):
284 coverage = get_emoji_map(emoji_font)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700285 for sequence in all_emoji:
286 assert sequence in coverage, (
287 '%s is not supported in the emoji font.' % printable(sequence))
288
289 for sequence in coverage:
290 if sequence in {0x0000, 0x000D, 0x0020}:
291 # The font needs to support a few extra characters, which is OK
292 continue
293 assert sequence in all_emoji, (
294 'Emoji font should not support %s.' % printable(sequence))
295
296 for first, second in sorted(equivalent_emoji.items()):
297 assert coverage[first] == coverage[second], (
298 '%s and %s should map to the same glyph.' % (
299 printable(first),
300 printable(second)))
301
302 for glyph in set(coverage.values()):
303 maps_to_glyph = [seq for seq in coverage if coverage[seq] == glyph]
304 if len(maps_to_glyph) > 1:
305 # There are more than one sequences mapping to the same glyph. We
306 # need to make sure they were expected to be equivalent.
307 equivalent_seqs = set()
308 for seq in maps_to_glyph:
309 equivalent_seq = seq
310 while equivalent_seq in equivalent_emoji:
311 equivalent_seq = equivalent_emoji[equivalent_seq]
312 equivalent_seqs.add(equivalent_seq)
313 assert len(equivalent_seqs) == 1, (
314 'The sequences %s should not result in the same glyph %s' % (
315 printable(equivalent_seqs),
316 glyph))
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700317
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700318
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700319def check_emoji_defaults(default_emoji):
320 missing_text_chars = _emoji_properties['Emoji'] - default_emoji
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700321 emoji_font_seen = False
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700322 for record in _fallback_chain:
323 if 'Zsye' in record.scripts:
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700324 emoji_font_seen = True
325 # No need to check the emoji font
326 continue
327 # For later fonts, we only check them if they have a script
328 # defined, since the defined script may get them to a higher
yiruif9936b92016-09-07 14:37:30 +0900329 # score even if they appear after the emoji font. However,
330 # we should skip checking the text symbols font, since
331 # symbol fonts should be able to override the emoji display
332 # style when 'Zsym' is explicitly specified by the user.
333 if emoji_font_seen and (not record.scripts or 'Zsym' in record.scripts):
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700334 continue
335
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700336 # Check default emoji-style characters
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700337 assert_font_supports_none_of_chars(record.font, sorted(default_emoji))
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700338
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700339 # Mark default text-style characters appearing in fonts above the emoji
340 # font as seen
341 if not emoji_font_seen:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700342 missing_text_chars -= set(get_best_cmap(record.font))
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700343
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700344 # Noto does not have monochrome glyphs for Unicode 7.0 wingdings and
345 # webdings yet.
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700346 missing_text_chars -= _chars_by_age['7.0']
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700347 assert missing_text_chars == set(), (
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700348 'Text style version of some emoji characters are missing: ' +
349 repr(missing_text_chars))
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700350
351
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700352# Setting reverse to true returns a dictionary that maps the values to sets of
353# characters, useful for some binary properties. Otherwise, we get a
354# dictionary that maps characters to the property values, assuming there's only
355# one property in the file.
356def parse_unicode_datafile(file_path, reverse=False):
357 if reverse:
358 output_dict = collections.defaultdict(set)
359 else:
360 output_dict = {}
361 with open(file_path) as datafile:
362 for line in datafile:
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700363 if '#' in line:
364 line = line[:line.index('#')]
365 line = line.strip()
366 if not line:
367 continue
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700368
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700369 chars, prop = line.split(';')[:2]
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700370 chars = chars.strip()
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700371 prop = prop.strip()
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700372
373 if ' ' in chars: # character sequence
374 sequence = [int(ch, 16) for ch in chars.split(' ')]
375 additions = [tuple(sequence)]
376 elif '..' in chars: # character range
377 char_start, char_end = chars.split('..')
378 char_start = int(char_start, 16)
379 char_end = int(char_end, 16)
380 additions = xrange(char_start, char_end+1)
381 else: # singe character
382 additions = [int(chars, 16)]
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700383 if reverse:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700384 output_dict[prop].update(additions)
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700385 else:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700386 for addition in additions:
387 assert addition not in output_dict
388 output_dict[addition] = prop
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700389 return output_dict
390
391
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700392def parse_emoji_variants(file_path):
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700393 emoji_set = set()
394 text_set = set()
395 with open(file_path) as datafile:
396 for line in datafile:
397 if '#' in line:
398 line = line[:line.index('#')]
399 line = line.strip()
400 if not line:
401 continue
402 sequence, description, _ = line.split(';')
403 sequence = sequence.strip().split(' ')
404 base = int(sequence[0], 16)
405 vs = int(sequence[1], 16)
406 description = description.strip()
407 if description == 'text style':
408 text_set.add((base, vs))
409 elif description == 'emoji style':
410 emoji_set.add((base, vs))
411 return text_set, emoji_set
412
413
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700414def parse_ucd(ucd_path):
415 global _emoji_properties, _chars_by_age
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700416 global _text_variation_sequences, _emoji_variation_sequences
417 global _emoji_sequences, _emoji_zwj_sequences
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700418 _emoji_properties = parse_unicode_datafile(
419 path.join(ucd_path, 'emoji-data.txt'), reverse=True)
Roozbeh Pournaderf7a68c12017-04-04 18:59:31 -0700420 emoji_properties_additions = parse_unicode_datafile(
421 path.join(ucd_path, 'additions', 'emoji-data.txt'), reverse=True)
422 for prop in emoji_properties_additions.keys():
423 _emoji_properties[prop].update(emoji_properties_additions[prop])
424
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700425 _chars_by_age = parse_unicode_datafile(
426 path.join(ucd_path, 'DerivedAge.txt'), reverse=True)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700427 sequences = parse_emoji_variants(
428 path.join(ucd_path, 'emoji-variation-sequences.txt'))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700429 _text_variation_sequences, _emoji_variation_sequences = sequences
430 _emoji_sequences = parse_unicode_datafile(
431 path.join(ucd_path, 'emoji-sequences.txt'))
Siyamed Sinir6e06ad02017-04-19 18:18:35 -0700432 _emoji_sequences.update(parse_unicode_datafile(
433 path.join(ucd_path, 'additions', 'emoji-sequences.txt')))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700434 _emoji_zwj_sequences = parse_unicode_datafile(
435 path.join(ucd_path, 'emoji-zwj-sequences.txt'))
Roozbeh Pournader1800ba42017-03-17 18:23:23 -0700436 _emoji_zwj_sequences.update(parse_unicode_datafile(
437 path.join(ucd_path, 'additions', 'emoji-zwj-sequences.txt')))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700438
439
440def flag_sequence(territory_code):
441 return tuple(0x1F1E6 + ord(ch) - ord('A') for ch in territory_code)
442
443
444UNSUPPORTED_FLAGS = frozenset({
445 flag_sequence('BL'), flag_sequence('BQ'), flag_sequence('DG'),
446 flag_sequence('EA'), flag_sequence('EH'), flag_sequence('FK'),
447 flag_sequence('GF'), flag_sequence('GP'), flag_sequence('GS'),
448 flag_sequence('MF'), flag_sequence('MQ'), flag_sequence('NC'),
449 flag_sequence('PM'), flag_sequence('RE'), flag_sequence('TF'),
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700450 flag_sequence('WF'), flag_sequence('XK'), flag_sequence('YT'),
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700451})
452
453EQUIVALENT_FLAGS = {
454 flag_sequence('BV'): flag_sequence('NO'),
455 flag_sequence('CP'): flag_sequence('FR'),
456 flag_sequence('HM'): flag_sequence('AU'),
457 flag_sequence('SJ'): flag_sequence('NO'),
458 flag_sequence('UM'): flag_sequence('US'),
459}
460
461COMBINING_KEYCAP = 0x20E3
462
463LEGACY_ANDROID_EMOJI = {
464 0xFE4E5: flag_sequence('JP'),
465 0xFE4E6: flag_sequence('US'),
466 0xFE4E7: flag_sequence('FR'),
467 0xFE4E8: flag_sequence('DE'),
468 0xFE4E9: flag_sequence('IT'),
469 0xFE4EA: flag_sequence('GB'),
470 0xFE4EB: flag_sequence('ES'),
471 0xFE4EC: flag_sequence('RU'),
472 0xFE4ED: flag_sequence('CN'),
473 0xFE4EE: flag_sequence('KR'),
474 0xFE82C: (ord('#'), COMBINING_KEYCAP),
475 0xFE82E: (ord('1'), COMBINING_KEYCAP),
476 0xFE82F: (ord('2'), COMBINING_KEYCAP),
477 0xFE830: (ord('3'), COMBINING_KEYCAP),
478 0xFE831: (ord('4'), COMBINING_KEYCAP),
479 0xFE832: (ord('5'), COMBINING_KEYCAP),
480 0xFE833: (ord('6'), COMBINING_KEYCAP),
481 0xFE834: (ord('7'), COMBINING_KEYCAP),
482 0xFE835: (ord('8'), COMBINING_KEYCAP),
483 0xFE836: (ord('9'), COMBINING_KEYCAP),
484 0xFE837: (ord('0'), COMBINING_KEYCAP),
485}
486
487ZWJ_IDENTICALS = {
488 # KISS
489 (0x1F469, 0x200D, 0x2764, 0x200D, 0x1F48B, 0x200D, 0x1F468): 0x1F48F,
490 # COUPLE WITH HEART
491 (0x1F469, 0x200D, 0x2764, 0x200D, 0x1F468): 0x1F491,
492 # FAMILY
493 (0x1F468, 0x200D, 0x1F469, 0x200D, 0x1F466): 0x1F46A,
494}
495
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700496ZWJ = 0x200D
497FEMALE_SIGN = 0x2640
498MALE_SIGN = 0x2642
499
500GENDER_DEFAULTS = [
501 (0x26F9, MALE_SIGN), # PERSON WITH BALL
502 (0x1F3C3, MALE_SIGN), # RUNNER
503 (0x1F3C4, MALE_SIGN), # SURFER
504 (0x1F3CA, MALE_SIGN), # SWIMMER
505 (0x1F3CB, MALE_SIGN), # WEIGHT LIFTER
506 (0x1F3CC, MALE_SIGN), # GOLFER
507 (0x1F46E, MALE_SIGN), # POLICE OFFICER
508 (0x1F46F, FEMALE_SIGN), # WOMAN WITH BUNNY EARS
509 (0x1F471, MALE_SIGN), # PERSON WITH BLOND HAIR
510 (0x1F473, MALE_SIGN), # MAN WITH TURBAN
511 (0x1F477, MALE_SIGN), # CONSTRUCTION WORKER
512 (0x1F481, FEMALE_SIGN), # INFORMATION DESK PERSON
513 (0x1F482, MALE_SIGN), # GUARDSMAN
514 (0x1F486, FEMALE_SIGN), # FACE MASSAGE
515 (0x1F487, FEMALE_SIGN), # HAIRCUT
516 (0x1F575, MALE_SIGN), # SLEUTH OR SPY
517 (0x1F645, FEMALE_SIGN), # FACE WITH NO GOOD GESTURE
518 (0x1F646, FEMALE_SIGN), # FACE WITH OK GESTURE
519 (0x1F647, MALE_SIGN), # PERSON BOWING DEEPLY
520 (0x1F64B, FEMALE_SIGN), # HAPPY PERSON RAISING ONE HAND
521 (0x1F64D, FEMALE_SIGN), # PERSON FROWNING
522 (0x1F64E, FEMALE_SIGN), # PERSON WITH POUTING FACE
523 (0x1F6A3, MALE_SIGN), # ROWBOAT
524 (0x1F6B4, MALE_SIGN), # BICYCLIST
525 (0x1F6B5, MALE_SIGN), # MOUNTAIN BICYCLIST
526 (0x1F6B6, MALE_SIGN), # PEDESTRIAN
527 (0x1F926, FEMALE_SIGN), # FACE PALM
528 (0x1F937, FEMALE_SIGN), # SHRUG
529 (0x1F938, MALE_SIGN), # PERSON DOING CARTWHEEL
530 (0x1F939, MALE_SIGN), # JUGGLING
531 (0x1F93C, MALE_SIGN), # WRESTLERS
532 (0x1F93D, MALE_SIGN), # WATER POLO
533 (0x1F93E, MALE_SIGN), # HANDBALL
534 (0x1F9D6, FEMALE_SIGN), # PERSON IN STEAMY ROOM
535 (0x1F9D7, FEMALE_SIGN), # PERSON CLIMBING
536 (0x1F9D8, FEMALE_SIGN), # PERSON IN LOTUS POSITION
537 (0x1F9D9, FEMALE_SIGN), # MAGE
538 (0x1F9DA, FEMALE_SIGN), # FAIRY
539 (0x1F9DB, FEMALE_SIGN), # VAMPIRE
540 (0x1F9DC, FEMALE_SIGN), # MERPERSON
541 (0x1F9DD, FEMALE_SIGN), # ELF
542 (0x1F9DE, FEMALE_SIGN), # GENIE
543 (0x1F9DF, FEMALE_SIGN), # ZOMBIE
544]
Doug Feltf874a192016-07-08 17:42:15 -0700545
546def is_fitzpatrick_modifier(cp):
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700547 return 0x1F3FB <= cp <= 0x1F3FF
548
549
550def reverse_emoji(seq):
551 rev = list(reversed(seq))
552 # if there are fitzpatrick modifiers in the sequence, keep them after
553 # the emoji they modify
554 for i in xrange(1, len(rev)):
555 if is_fitzpatrick_modifier(rev[i-1]):
556 rev[i], rev[i-1] = rev[i-1], rev[i]
557 return tuple(rev)
Doug Feltf874a192016-07-08 17:42:15 -0700558
559
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700560def compute_expected_emoji():
561 equivalent_emoji = {}
562 sequence_pieces = set()
563 all_sequences = set()
564 all_sequences.update(_emoji_variation_sequences)
565
Raph Levien2b8b8192016-08-09 14:28:54 -0700566 # add zwj sequences not in the current emoji-zwj-sequences.txt
567 adjusted_emoji_zwj_sequences = dict(_emoji_zwj_sequences)
568 adjusted_emoji_zwj_sequences.update(_emoji_zwj_sequences)
Raph Levien2b8b8192016-08-09 14:28:54 -0700569
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700570 # Add empty flag tag sequence that is supported as fallback
571 _emoji_sequences[(0x1F3F4, 0xE007F)] = 'Emoji_Tag_Sequence'
572
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700573 for sequence in _emoji_sequences.keys():
574 sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
575 all_sequences.add(sequence)
576 sequence_pieces.update(sequence)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700577 if _emoji_sequences.get(sequence, None) == 'Emoji_Tag_Sequence':
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700578 # Add reverse of all emoji ZWJ sequences, which are added to the
579 # fonts as a workaround to get the sequences work in RTL text.
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700580 # TODO: test if these are actually needed by Minikin/HarfBuzz.
581 reversed_seq = reverse_emoji(sequence)
582 all_sequences.add(reversed_seq)
583 equivalent_emoji[reversed_seq] = sequence
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700584
Raph Levien2b8b8192016-08-09 14:28:54 -0700585 for sequence in adjusted_emoji_zwj_sequences.keys():
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700586 sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
587 all_sequences.add(sequence)
588 sequence_pieces.update(sequence)
589 # Add reverse of all emoji ZWJ sequences, which are added to the fonts
590 # as a workaround to get the sequences work in RTL text.
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700591 reversed_seq = reverse_emoji(sequence)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700592 all_sequences.add(reversed_seq)
593 equivalent_emoji[reversed_seq] = sequence
594
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700595 # Remove unsupported flags
596 all_sequences.difference_update(UNSUPPORTED_FLAGS)
597
598 # Add all tag characters used in flags
599 sequence_pieces.update(range(0xE0030, 0xE0039 + 1))
600 sequence_pieces.update(range(0xE0061, 0xE007A + 1))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700601
602 all_emoji = (
603 _emoji_properties['Emoji'] |
604 all_sequences |
605 sequence_pieces |
606 set(LEGACY_ANDROID_EMOJI.keys()))
607 default_emoji = (
608 _emoji_properties['Emoji_Presentation'] |
609 all_sequences |
610 set(LEGACY_ANDROID_EMOJI.keys()))
611
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700612 equivalent_emoji.update(EQUIVALENT_FLAGS)
613 equivalent_emoji.update(LEGACY_ANDROID_EMOJI)
614 equivalent_emoji.update(ZWJ_IDENTICALS)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700615
616 for ch, gender in GENDER_DEFAULTS:
617 equivalent_emoji[(ch, ZWJ, gender)] = ch
618 for skin_tone in range(0x1F3FB, 0x1F3FF+1):
619 skin_toned = (ch, skin_tone, ZWJ, gender)
620 if skin_toned in all_emoji:
621 equivalent_emoji[skin_toned] = (ch, skin_tone)
622
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700623 for seq in _emoji_variation_sequences:
624 equivalent_emoji[seq] = seq[0]
625
626 return all_emoji, default_emoji, equivalent_emoji
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700627
628
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700629def check_vertical_metrics():
630 for record in _fallback_chain:
631 if record.name in ['sans-serif', 'sans-serif-condensed']:
632 font = open_font(record.font)
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700633 assert font['head'].yMax == 2163 and font['head'].yMin == -555, (
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700634 'yMax and yMin of %s do not match expected values.' % (
635 record.font,))
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700636
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700637 if record.name in ['sans-serif', 'sans-serif-condensed',
638 'serif', 'monospace']:
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700639 font = open_font(record.font)
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700640 assert (font['hhea'].ascent == 1900 and
641 font['hhea'].descent == -500), (
642 'ascent and descent of %s do not match expected '
643 'values.' % (record.font,))
644
645
646def check_cjk_punctuation():
647 cjk_scripts = {'Hans', 'Hant', 'Jpan', 'Kore'}
648 cjk_punctuation = range(0x3000, 0x301F + 1)
649 for record in _fallback_chain:
650 if record.scripts.intersection(cjk_scripts):
651 # CJK font seen. Stop checking the rest of the fonts.
652 break
653 assert_font_supports_none_of_chars(record.font, cjk_punctuation)
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700654
655
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800656def main():
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800657 global _fonts_dir
Doug Feltf874a192016-07-08 17:42:15 -0700658 target_out = sys.argv[1]
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800659 _fonts_dir = path.join(target_out, 'fonts')
660
661 fonts_xml_path = path.join(target_out, 'etc', 'fonts.xml')
662 parse_fonts_xml(fonts_xml_path)
663
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700664 check_vertical_metrics()
665
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800666 hyphens_dir = path.join(target_out, 'usr', 'hyphen-data')
667 check_hyphens(hyphens_dir)
668
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700669 check_cjk_punctuation()
670
Roozbeh Pournader27ec3ac2016-03-31 13:05:32 -0700671 check_emoji = sys.argv[2]
672 if check_emoji == 'true':
673 ucd_path = sys.argv[3]
674 parse_ucd(ucd_path)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700675 all_emoji, default_emoji, equivalent_emoji = compute_expected_emoji()
676 check_emoji_coverage(all_emoji, equivalent_emoji)
677 check_emoji_defaults(default_emoji)
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700678
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800679
680if __name__ == '__main__':
681 main()