blob: 15d39fdfd6ea1793ddc4c491566e566e8f55bfc8 [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 Nonaka99a7b602017-07-05 16:06:23 -0700273 record = FontRecord(
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800274 name,
275 frozenset(scripts),
276 variant,
277 weight,
278 style,
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700279 fallback_for,
280 (font_file, index))
281
282 _all_fonts.append(record)
283
284 if not fallback_for:
285 if not name or name == 'sans-serif':
286 for _, fallback in _fallback_chains.iteritems():
287 fallback.append(record)
288 else:
289 _fallback_chains[name].append(record)
290 else:
291 _fallback_chains[fallback_for].append(record)
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800292
293 if name: # non-empty names are used for default LGC fonts
294 map_scripts = {'Latn', 'Grek', 'Cyrl'}
295 else:
296 map_scripts = scripts
297 for script in map_scripts:
298 _script_to_font_map[script].add((font_file, index))
299
300
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700301def check_emoji_coverage(all_emoji, equivalent_emoji):
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700302 emoji_font = get_emoji_font()
303 check_emoji_font_coverage(emoji_font, all_emoji, equivalent_emoji)
Doug Feltf874a192016-07-08 17:42:15 -0700304
305
306def get_emoji_font():
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700307 emoji_fonts = [
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700308 record.font for record in _all_fonts
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700309 if 'Zsye' in record.scripts]
Roozbeh Pournader27ec3ac2016-03-31 13:05:32 -0700310 assert len(emoji_fonts) == 1, 'There are %d emoji fonts.' % len(emoji_fonts)
Doug Feltf874a192016-07-08 17:42:15 -0700311 return emoji_fonts[0]
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700312
Doug Feltf874a192016-07-08 17:42:15 -0700313
314def check_emoji_font_coverage(emoji_font, all_emoji, equivalent_emoji):
315 coverage = get_emoji_map(emoji_font)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700316 for sequence in all_emoji:
317 assert sequence in coverage, (
318 '%s is not supported in the emoji font.' % printable(sequence))
319
320 for sequence in coverage:
321 if sequence in {0x0000, 0x000D, 0x0020}:
322 # The font needs to support a few extra characters, which is OK
323 continue
324 assert sequence in all_emoji, (
325 'Emoji font should not support %s.' % printable(sequence))
326
327 for first, second in sorted(equivalent_emoji.items()):
328 assert coverage[first] == coverage[second], (
329 '%s and %s should map to the same glyph.' % (
330 printable(first),
331 printable(second)))
332
333 for glyph in set(coverage.values()):
334 maps_to_glyph = [seq for seq in coverage if coverage[seq] == glyph]
335 if len(maps_to_glyph) > 1:
336 # There are more than one sequences mapping to the same glyph. We
337 # need to make sure they were expected to be equivalent.
338 equivalent_seqs = set()
339 for seq in maps_to_glyph:
340 equivalent_seq = seq
341 while equivalent_seq in equivalent_emoji:
342 equivalent_seq = equivalent_emoji[equivalent_seq]
343 equivalent_seqs.add(equivalent_seq)
344 assert len(equivalent_seqs) == 1, (
345 'The sequences %s should not result in the same glyph %s' % (
346 printable(equivalent_seqs),
347 glyph))
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700348
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700349
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700350def check_emoji_defaults(default_emoji):
351 missing_text_chars = _emoji_properties['Emoji'] - default_emoji
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700352 for name, fallback_chain in _fallback_chains.iteritems():
353 emoji_font_seen = False
354 for record in fallback_chain:
355 if 'Zsye' in record.scripts:
356 emoji_font_seen = True
357 # No need to check the emoji font
358 continue
359 # For later fonts, we only check them if they have a script
360 # defined, since the defined script may get them to a higher
361 # score even if they appear after the emoji font. However,
362 # we should skip checking the text symbols font, since
363 # symbol fonts should be able to override the emoji display
364 # style when 'Zsym' is explicitly specified by the user.
365 if emoji_font_seen and (not record.scripts or 'Zsym' in record.scripts):
366 continue
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700367
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700368 # Check default emoji-style characters
369 assert_font_supports_none_of_chars(record.font, sorted(default_emoji), name)
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700370
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700371 # Mark default text-style characters appearing in fonts above the emoji
372 # font as seen
373 if not emoji_font_seen:
374 missing_text_chars -= set(get_best_cmap(record.font))
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700375
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700376 # Noto does not have monochrome glyphs for Unicode 7.0 wingdings and
377 # webdings yet.
378 missing_text_chars -= _chars_by_age['7.0']
379 assert missing_text_chars == set(), (
380 'Text style version of some emoji characters are missing: ' +
381 repr(missing_text_chars))
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700382
383
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700384# Setting reverse to true returns a dictionary that maps the values to sets of
385# characters, useful for some binary properties. Otherwise, we get a
386# dictionary that maps characters to the property values, assuming there's only
387# one property in the file.
388def parse_unicode_datafile(file_path, reverse=False):
389 if reverse:
390 output_dict = collections.defaultdict(set)
391 else:
392 output_dict = {}
393 with open(file_path) as datafile:
394 for line in datafile:
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700395 if '#' in line:
396 line = line[:line.index('#')]
397 line = line.strip()
398 if not line:
399 continue
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700400
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700401 chars, prop = line.split(';')[:2]
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700402 chars = chars.strip()
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700403 prop = prop.strip()
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700404
405 if ' ' in chars: # character sequence
406 sequence = [int(ch, 16) for ch in chars.split(' ')]
407 additions = [tuple(sequence)]
408 elif '..' in chars: # character range
409 char_start, char_end = chars.split('..')
410 char_start = int(char_start, 16)
411 char_end = int(char_end, 16)
412 additions = xrange(char_start, char_end+1)
413 else: # singe character
414 additions = [int(chars, 16)]
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700415 if reverse:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700416 output_dict[prop].update(additions)
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700417 else:
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700418 for addition in additions:
419 assert addition not in output_dict
420 output_dict[addition] = prop
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700421 return output_dict
422
423
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700424def parse_emoji_variants(file_path):
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700425 emoji_set = set()
426 text_set = set()
427 with open(file_path) as datafile:
428 for line in datafile:
429 if '#' in line:
430 line = line[:line.index('#')]
431 line = line.strip()
432 if not line:
433 continue
434 sequence, description, _ = line.split(';')
435 sequence = sequence.strip().split(' ')
436 base = int(sequence[0], 16)
437 vs = int(sequence[1], 16)
438 description = description.strip()
439 if description == 'text style':
440 text_set.add((base, vs))
441 elif description == 'emoji style':
442 emoji_set.add((base, vs))
443 return text_set, emoji_set
444
445
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700446def parse_ucd(ucd_path):
447 global _emoji_properties, _chars_by_age
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700448 global _text_variation_sequences, _emoji_variation_sequences
449 global _emoji_sequences, _emoji_zwj_sequences
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700450 _emoji_properties = parse_unicode_datafile(
451 path.join(ucd_path, 'emoji-data.txt'), reverse=True)
Roozbeh Pournaderf7a68c12017-04-04 18:59:31 -0700452 emoji_properties_additions = parse_unicode_datafile(
453 path.join(ucd_path, 'additions', 'emoji-data.txt'), reverse=True)
454 for prop in emoji_properties_additions.keys():
455 _emoji_properties[prop].update(emoji_properties_additions[prop])
456
Roozbeh Pournader7b822e52016-03-16 18:55:32 -0700457 _chars_by_age = parse_unicode_datafile(
458 path.join(ucd_path, 'DerivedAge.txt'), reverse=True)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700459 sequences = parse_emoji_variants(
460 path.join(ucd_path, 'emoji-variation-sequences.txt'))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700461 _text_variation_sequences, _emoji_variation_sequences = sequences
462 _emoji_sequences = parse_unicode_datafile(
463 path.join(ucd_path, 'emoji-sequences.txt'))
Siyamed Sinir6e06ad02017-04-19 18:18:35 -0700464 _emoji_sequences.update(parse_unicode_datafile(
465 path.join(ucd_path, 'additions', 'emoji-sequences.txt')))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700466 _emoji_zwj_sequences = parse_unicode_datafile(
467 path.join(ucd_path, 'emoji-zwj-sequences.txt'))
Roozbeh Pournader1800ba42017-03-17 18:23:23 -0700468 _emoji_zwj_sequences.update(parse_unicode_datafile(
469 path.join(ucd_path, 'additions', 'emoji-zwj-sequences.txt')))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700470
471
472def flag_sequence(territory_code):
473 return tuple(0x1F1E6 + ord(ch) - ord('A') for ch in territory_code)
474
475
476UNSUPPORTED_FLAGS = frozenset({
477 flag_sequence('BL'), flag_sequence('BQ'), flag_sequence('DG'),
478 flag_sequence('EA'), flag_sequence('EH'), flag_sequence('FK'),
479 flag_sequence('GF'), flag_sequence('GP'), flag_sequence('GS'),
480 flag_sequence('MF'), flag_sequence('MQ'), flag_sequence('NC'),
481 flag_sequence('PM'), flag_sequence('RE'), flag_sequence('TF'),
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700482 flag_sequence('WF'), flag_sequence('XK'), flag_sequence('YT'),
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700483})
484
485EQUIVALENT_FLAGS = {
486 flag_sequence('BV'): flag_sequence('NO'),
487 flag_sequence('CP'): flag_sequence('FR'),
488 flag_sequence('HM'): flag_sequence('AU'),
489 flag_sequence('SJ'): flag_sequence('NO'),
490 flag_sequence('UM'): flag_sequence('US'),
491}
492
493COMBINING_KEYCAP = 0x20E3
494
495LEGACY_ANDROID_EMOJI = {
496 0xFE4E5: flag_sequence('JP'),
497 0xFE4E6: flag_sequence('US'),
498 0xFE4E7: flag_sequence('FR'),
499 0xFE4E8: flag_sequence('DE'),
500 0xFE4E9: flag_sequence('IT'),
501 0xFE4EA: flag_sequence('GB'),
502 0xFE4EB: flag_sequence('ES'),
503 0xFE4EC: flag_sequence('RU'),
504 0xFE4ED: flag_sequence('CN'),
505 0xFE4EE: flag_sequence('KR'),
506 0xFE82C: (ord('#'), COMBINING_KEYCAP),
507 0xFE82E: (ord('1'), COMBINING_KEYCAP),
508 0xFE82F: (ord('2'), COMBINING_KEYCAP),
509 0xFE830: (ord('3'), COMBINING_KEYCAP),
510 0xFE831: (ord('4'), COMBINING_KEYCAP),
511 0xFE832: (ord('5'), COMBINING_KEYCAP),
512 0xFE833: (ord('6'), COMBINING_KEYCAP),
513 0xFE834: (ord('7'), COMBINING_KEYCAP),
514 0xFE835: (ord('8'), COMBINING_KEYCAP),
515 0xFE836: (ord('9'), COMBINING_KEYCAP),
516 0xFE837: (ord('0'), COMBINING_KEYCAP),
517}
518
519ZWJ_IDENTICALS = {
520 # KISS
521 (0x1F469, 0x200D, 0x2764, 0x200D, 0x1F48B, 0x200D, 0x1F468): 0x1F48F,
522 # COUPLE WITH HEART
523 (0x1F469, 0x200D, 0x2764, 0x200D, 0x1F468): 0x1F491,
524 # FAMILY
525 (0x1F468, 0x200D, 0x1F469, 0x200D, 0x1F466): 0x1F46A,
526}
527
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700528ZWJ = 0x200D
529FEMALE_SIGN = 0x2640
530MALE_SIGN = 0x2642
531
532GENDER_DEFAULTS = [
533 (0x26F9, MALE_SIGN), # PERSON WITH BALL
534 (0x1F3C3, MALE_SIGN), # RUNNER
535 (0x1F3C4, MALE_SIGN), # SURFER
536 (0x1F3CA, MALE_SIGN), # SWIMMER
537 (0x1F3CB, MALE_SIGN), # WEIGHT LIFTER
538 (0x1F3CC, MALE_SIGN), # GOLFER
539 (0x1F46E, MALE_SIGN), # POLICE OFFICER
540 (0x1F46F, FEMALE_SIGN), # WOMAN WITH BUNNY EARS
541 (0x1F471, MALE_SIGN), # PERSON WITH BLOND HAIR
542 (0x1F473, MALE_SIGN), # MAN WITH TURBAN
543 (0x1F477, MALE_SIGN), # CONSTRUCTION WORKER
544 (0x1F481, FEMALE_SIGN), # INFORMATION DESK PERSON
545 (0x1F482, MALE_SIGN), # GUARDSMAN
546 (0x1F486, FEMALE_SIGN), # FACE MASSAGE
547 (0x1F487, FEMALE_SIGN), # HAIRCUT
548 (0x1F575, MALE_SIGN), # SLEUTH OR SPY
549 (0x1F645, FEMALE_SIGN), # FACE WITH NO GOOD GESTURE
550 (0x1F646, FEMALE_SIGN), # FACE WITH OK GESTURE
551 (0x1F647, MALE_SIGN), # PERSON BOWING DEEPLY
552 (0x1F64B, FEMALE_SIGN), # HAPPY PERSON RAISING ONE HAND
553 (0x1F64D, FEMALE_SIGN), # PERSON FROWNING
554 (0x1F64E, FEMALE_SIGN), # PERSON WITH POUTING FACE
555 (0x1F6A3, MALE_SIGN), # ROWBOAT
556 (0x1F6B4, MALE_SIGN), # BICYCLIST
557 (0x1F6B5, MALE_SIGN), # MOUNTAIN BICYCLIST
558 (0x1F6B6, MALE_SIGN), # PEDESTRIAN
559 (0x1F926, FEMALE_SIGN), # FACE PALM
560 (0x1F937, FEMALE_SIGN), # SHRUG
561 (0x1F938, MALE_SIGN), # PERSON DOING CARTWHEEL
562 (0x1F939, MALE_SIGN), # JUGGLING
563 (0x1F93C, MALE_SIGN), # WRESTLERS
564 (0x1F93D, MALE_SIGN), # WATER POLO
565 (0x1F93E, MALE_SIGN), # HANDBALL
566 (0x1F9D6, FEMALE_SIGN), # PERSON IN STEAMY ROOM
567 (0x1F9D7, FEMALE_SIGN), # PERSON CLIMBING
568 (0x1F9D8, FEMALE_SIGN), # PERSON IN LOTUS POSITION
569 (0x1F9D9, FEMALE_SIGN), # MAGE
570 (0x1F9DA, FEMALE_SIGN), # FAIRY
571 (0x1F9DB, FEMALE_SIGN), # VAMPIRE
572 (0x1F9DC, FEMALE_SIGN), # MERPERSON
573 (0x1F9DD, FEMALE_SIGN), # ELF
574 (0x1F9DE, FEMALE_SIGN), # GENIE
575 (0x1F9DF, FEMALE_SIGN), # ZOMBIE
576]
Doug Feltf874a192016-07-08 17:42:15 -0700577
578def is_fitzpatrick_modifier(cp):
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700579 return 0x1F3FB <= cp <= 0x1F3FF
580
581
582def reverse_emoji(seq):
583 rev = list(reversed(seq))
584 # if there are fitzpatrick modifiers in the sequence, keep them after
585 # the emoji they modify
586 for i in xrange(1, len(rev)):
587 if is_fitzpatrick_modifier(rev[i-1]):
588 rev[i], rev[i-1] = rev[i-1], rev[i]
589 return tuple(rev)
Doug Feltf874a192016-07-08 17:42:15 -0700590
591
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700592def compute_expected_emoji():
593 equivalent_emoji = {}
594 sequence_pieces = set()
595 all_sequences = set()
596 all_sequences.update(_emoji_variation_sequences)
597
Raph Levien2b8b8192016-08-09 14:28:54 -0700598 # add zwj sequences not in the current emoji-zwj-sequences.txt
599 adjusted_emoji_zwj_sequences = dict(_emoji_zwj_sequences)
600 adjusted_emoji_zwj_sequences.update(_emoji_zwj_sequences)
Raph Levien2b8b8192016-08-09 14:28:54 -0700601
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700602 # Add empty flag tag sequence that is supported as fallback
603 _emoji_sequences[(0x1F3F4, 0xE007F)] = 'Emoji_Tag_Sequence'
604
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700605 for sequence in _emoji_sequences.keys():
606 sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
607 all_sequences.add(sequence)
608 sequence_pieces.update(sequence)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700609 if _emoji_sequences.get(sequence, None) == 'Emoji_Tag_Sequence':
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700610 # Add reverse of all emoji ZWJ sequences, which are added to the
611 # fonts as a workaround to get the sequences work in RTL text.
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700612 # TODO: test if these are actually needed by Minikin/HarfBuzz.
613 reversed_seq = reverse_emoji(sequence)
614 all_sequences.add(reversed_seq)
615 equivalent_emoji[reversed_seq] = sequence
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700616
Raph Levien2b8b8192016-08-09 14:28:54 -0700617 for sequence in adjusted_emoji_zwj_sequences.keys():
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700618 sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
619 all_sequences.add(sequence)
620 sequence_pieces.update(sequence)
621 # Add reverse of all emoji ZWJ sequences, which are added to the fonts
622 # as a workaround to get the sequences work in RTL text.
Roozbeh Pournader3b3c78e2016-07-25 14:04:34 -0700623 reversed_seq = reverse_emoji(sequence)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700624 all_sequences.add(reversed_seq)
625 equivalent_emoji[reversed_seq] = sequence
626
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700627 # Remove unsupported flags
628 all_sequences.difference_update(UNSUPPORTED_FLAGS)
629
630 # Add all tag characters used in flags
631 sequence_pieces.update(range(0xE0030, 0xE0039 + 1))
632 sequence_pieces.update(range(0xE0061, 0xE007A + 1))
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700633
634 all_emoji = (
635 _emoji_properties['Emoji'] |
636 all_sequences |
637 sequence_pieces |
638 set(LEGACY_ANDROID_EMOJI.keys()))
639 default_emoji = (
640 _emoji_properties['Emoji_Presentation'] |
641 all_sequences |
642 set(LEGACY_ANDROID_EMOJI.keys()))
643
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700644 equivalent_emoji.update(EQUIVALENT_FLAGS)
645 equivalent_emoji.update(LEGACY_ANDROID_EMOJI)
646 equivalent_emoji.update(ZWJ_IDENTICALS)
Roozbeh Pournaderaa3ee8e2017-04-10 13:52:20 -0700647
648 for ch, gender in GENDER_DEFAULTS:
649 equivalent_emoji[(ch, ZWJ, gender)] = ch
650 for skin_tone in range(0x1F3FB, 0x1F3FF+1):
651 skin_toned = (ch, skin_tone, ZWJ, gender)
652 if skin_toned in all_emoji:
653 equivalent_emoji[skin_toned] = (ch, skin_tone)
654
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700655 for seq in _emoji_variation_sequences:
656 equivalent_emoji[seq] = seq[0]
657
658 return all_emoji, default_emoji, equivalent_emoji
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700659
660
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700661def check_compact_only_fallback():
662 for name, fallback_chain in _fallback_chains.iteritems():
663 for record in fallback_chain:
664 if record.variant == 'compact':
665 same_script_elegants = [x for x in fallback_chain
666 if x.scripts == record.scripts and x.variant == 'elegant']
667 assert same_script_elegants, (
668 '%s must be in elegant of %s as fallback of "%s" too' % (
669 record.font, record.scripts, record.fallback_for),)
670
671
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700672def check_vertical_metrics():
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700673 for record in _all_fonts:
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700674 if record.name in ['sans-serif', 'sans-serif-condensed']:
675 font = open_font(record.font)
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700676 assert font['head'].yMax == 2163 and font['head'].yMin == -555, (
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700677 'yMax and yMin of %s do not match expected values.' % (
678 record.font,))
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700679
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700680 if record.name in ['sans-serif', 'sans-serif-condensed',
681 'serif', 'monospace']:
Roozbeh Pournaderede3a172016-07-27 16:35:12 -0700682 font = open_font(record.font)
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700683 assert (font['hhea'].ascent == 1900 and
684 font['hhea'].descent == -500), (
685 'ascent and descent of %s do not match expected '
686 'values.' % (record.font,))
687
688
689def check_cjk_punctuation():
690 cjk_scripts = {'Hans', 'Hant', 'Jpan', 'Kore'}
691 cjk_punctuation = range(0x3000, 0x301F + 1)
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700692 for name, fallback_chain in _fallback_chains.iteritems():
693 for record in fallback_chain:
694 if record.scripts.intersection(cjk_scripts):
695 # CJK font seen. Stop checking the rest of the fonts.
696 break
697 assert_font_supports_none_of_chars(record.font, cjk_punctuation, name)
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700698
699
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800700def main():
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800701 global _fonts_dir
Doug Feltf874a192016-07-08 17:42:15 -0700702 target_out = sys.argv[1]
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800703 _fonts_dir = path.join(target_out, 'fonts')
704
705 fonts_xml_path = path.join(target_out, 'etc', 'fonts.xml')
706 parse_fonts_xml(fonts_xml_path)
707
Seigo Nonaka99a7b602017-07-05 16:06:23 -0700708 check_compact_only_fallback()
709
Roozbeh Pournaderbac1aec2016-07-27 13:08:37 -0700710 check_vertical_metrics()
711
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800712 hyphens_dir = path.join(target_out, 'usr', 'hyphen-data')
713 check_hyphens(hyphens_dir)
714
Roozbeh Pournader63d4d0d2017-05-18 18:38:36 -0700715 check_cjk_punctuation()
716
Roozbeh Pournader27ec3ac2016-03-31 13:05:32 -0700717 check_emoji = sys.argv[2]
718 if check_emoji == 'true':
719 ucd_path = sys.argv[3]
720 parse_ucd(ucd_path)
Roozbeh Pournader5dde0872016-03-31 13:54:56 -0700721 all_emoji, default_emoji, equivalent_emoji = compute_expected_emoji()
722 check_emoji_coverage(all_emoji, equivalent_emoji)
723 check_emoji_defaults(default_emoji)
Roozbeh Pournaderfa1facc2016-03-16 13:53:47 -0700724
Roozbeh Pournader0e969e22016-03-09 23:08:45 -0800725
726if __name__ == '__main__':
727 main()