Roozbeh Pournader | 0e969e2 | 2016-03-09 23:08:45 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import collections |
| 4 | import glob |
| 5 | from os import path |
| 6 | import sys |
| 7 | from xml.etree import ElementTree |
| 8 | |
| 9 | from fontTools import ttLib |
| 10 | |
| 11 | LANG_TO_SCRIPT = { |
| 12 | 'de': 'Latn', |
| 13 | 'en': 'Latn', |
| 14 | 'es': 'Latn', |
| 15 | 'eu': 'Latn', |
| 16 | 'ja': 'Jpan', |
| 17 | 'ko': 'Kore', |
| 18 | 'hu': 'Latn', |
| 19 | 'hy': 'Armn', |
| 20 | 'nb': 'Latn', |
| 21 | 'nn': 'Latn', |
| 22 | 'pt': 'Latn', |
| 23 | } |
| 24 | |
| 25 | def lang_to_script(lang_code): |
| 26 | lang = lang_code.lower() |
| 27 | while lang not in LANG_TO_SCRIPT: |
| 28 | hyphen_idx = lang.rfind('-') |
| 29 | assert hyphen_idx != -1, ( |
| 30 | 'We do not know what script the "%s" language is written in.' |
| 31 | % lang_code) |
| 32 | assumed_script = lang[hyphen_idx+1:] |
| 33 | if len(assumed_script) == 4 and assumed_script.isalpha(): |
| 34 | # This is actually the script |
| 35 | return assumed_script.title() |
| 36 | lang = lang[:hyphen_idx] |
| 37 | return LANG_TO_SCRIPT[lang] |
| 38 | |
| 39 | |
| 40 | def get_best_cmap(font): |
| 41 | font_file, index = font |
| 42 | font_path = path.join(_fonts_dir, font_file) |
| 43 | if index is not None: |
| 44 | ttfont = ttLib.TTFont(font_path, fontNumber=index) |
| 45 | else: |
| 46 | ttfont = ttLib.TTFont(font_path) |
| 47 | all_unicode_cmap = None |
| 48 | bmp_cmap = None |
| 49 | for cmap in ttfont['cmap'].tables: |
| 50 | specifier = (cmap.format, cmap.platformID, cmap.platEncID) |
| 51 | if specifier == (4, 3, 1): |
| 52 | assert bmp_cmap is None, 'More than one BMP cmap in %s' % (font, ) |
| 53 | bmp_cmap = cmap |
| 54 | elif specifier == (12, 3, 10): |
| 55 | assert all_unicode_cmap is None, ( |
| 56 | 'More than one UCS-4 cmap in %s' % (font, )) |
| 57 | all_unicode_cmap = cmap |
| 58 | |
| 59 | return all_unicode_cmap.cmap if all_unicode_cmap else bmp_cmap.cmap |
| 60 | |
| 61 | |
| 62 | def assert_font_supports_any_of_chars(font, chars): |
| 63 | best_cmap = get_best_cmap(font) |
| 64 | for char in chars: |
| 65 | if char in best_cmap: |
| 66 | return |
| 67 | sys.exit('None of characters in %s were found in %s' % (chars, font)) |
| 68 | |
| 69 | |
Roozbeh Pournader | fa1facc | 2016-03-16 13:53:47 -0700 | [diff] [blame] | 70 | def assert_font_supports_all_of_chars(font, chars): |
| 71 | best_cmap = get_best_cmap(font) |
| 72 | for char in chars: |
| 73 | assert char in best_cmap, ( |
| 74 | 'U+%04X was not found in %s' % (char, font)) |
| 75 | |
| 76 | |
| 77 | def assert_font_supports_none_of_chars(font, chars): |
| 78 | best_cmap = get_best_cmap(font) |
| 79 | for char in chars: |
| 80 | assert char not in best_cmap, ( |
| 81 | 'U+%04X was found in %s' % (char, font)) |
| 82 | |
| 83 | |
Roozbeh Pournader | 0e969e2 | 2016-03-09 23:08:45 -0800 | [diff] [blame] | 84 | def check_hyphens(hyphens_dir): |
| 85 | # Find all the scripts that need automatic hyphenation |
| 86 | scripts = set() |
| 87 | for hyb_file in glob.iglob(path.join(hyphens_dir, '*.hyb')): |
| 88 | hyb_file = path.basename(hyb_file) |
| 89 | assert hyb_file.startswith('hyph-'), ( |
| 90 | 'Unknown hyphenation file %s' % hyb_file) |
| 91 | lang_code = hyb_file[hyb_file.index('-')+1:hyb_file.index('.')] |
| 92 | scripts.add(lang_to_script(lang_code)) |
| 93 | |
| 94 | HYPHENS = {0x002D, 0x2010} |
| 95 | for script in scripts: |
| 96 | fonts = _script_to_font_map[script] |
| 97 | assert fonts, 'No fonts found for the "%s" script' % script |
| 98 | for font in fonts: |
| 99 | assert_font_supports_any_of_chars(font, HYPHENS) |
| 100 | |
| 101 | |
| 102 | def parse_fonts_xml(fonts_xml_path): |
| 103 | global _script_to_font_map, _fallback_chain |
| 104 | _script_to_font_map = collections.defaultdict(set) |
| 105 | _fallback_chain = [] |
| 106 | tree = ElementTree.parse(fonts_xml_path) |
| 107 | for family in tree.findall('family'): |
| 108 | name = family.get('name') |
| 109 | variant = family.get('variant') |
| 110 | langs = family.get('lang') |
| 111 | if name: |
| 112 | assert variant is None, ( |
| 113 | 'No variant expected for LGC font %s.' % name) |
| 114 | assert langs is None, ( |
| 115 | 'No language expected for LGC fonts %s.' % name) |
| 116 | else: |
| 117 | assert variant in {None, 'elegant', 'compact'}, ( |
| 118 | 'Unexpected value for variant: %s' % variant) |
| 119 | |
| 120 | if langs: |
| 121 | langs = langs.split() |
| 122 | scripts = {lang_to_script(lang) for lang in langs} |
| 123 | else: |
| 124 | scripts = set() |
| 125 | |
| 126 | for child in family: |
| 127 | assert child.tag == 'font', ( |
| 128 | 'Unknown tag <%s>' % child.tag) |
| 129 | font_file = child.text |
| 130 | weight = int(child.get('weight')) |
| 131 | assert weight % 100 == 0, ( |
| 132 | 'Font weight "%d" is not a multiple of 100.' % weight) |
| 133 | |
| 134 | style = child.get('style') |
| 135 | assert style in {'normal', 'italic'}, ( |
| 136 | 'Unknown style "%s"' % style) |
| 137 | |
| 138 | index = child.get('index') |
| 139 | if index: |
| 140 | index = int(index) |
| 141 | |
| 142 | _fallback_chain.append(( |
| 143 | name, |
| 144 | frozenset(scripts), |
| 145 | variant, |
| 146 | weight, |
| 147 | style, |
| 148 | (font_file, index))) |
| 149 | |
| 150 | if name: # non-empty names are used for default LGC fonts |
| 151 | map_scripts = {'Latn', 'Grek', 'Cyrl'} |
| 152 | else: |
| 153 | map_scripts = scripts |
| 154 | for script in map_scripts: |
| 155 | _script_to_font_map[script].add((font_file, index)) |
| 156 | |
| 157 | |
Roozbeh Pournader | fa1facc | 2016-03-16 13:53:47 -0700 | [diff] [blame] | 158 | def check_emoji_availability(): |
| 159 | emoji_fonts = [font[5] for font in _fallback_chain if 'Zsye' in font[1]] |
| 160 | emoji_chars = _emoji_properties['Emoji'] |
| 161 | for emoji_font in emoji_fonts: |
| 162 | assert_font_supports_all_of_chars(emoji_font, emoji_chars) |
| 163 | |
| 164 | |
| 165 | def check_emoji_defaults(): |
| 166 | default_emoji_chars = _emoji_properties['Emoji_Presentation'] |
Roozbeh Pournader | 7b822e5 | 2016-03-16 18:55:32 -0700 | [diff] [blame^] | 167 | missing_text_chars = _emoji_properties['Emoji'] - default_emoji_chars |
Roozbeh Pournader | fa1facc | 2016-03-16 13:53:47 -0700 | [diff] [blame] | 168 | emoji_font_seen = False |
| 169 | for name, scripts, variant, weight, style, font in _fallback_chain: |
| 170 | if 'Zsye' in scripts: |
| 171 | emoji_font_seen = True |
| 172 | # No need to check the emoji font |
| 173 | continue |
| 174 | # For later fonts, we only check them if they have a script |
| 175 | # defined, since the defined script may get them to a higher |
| 176 | # score even if they appear after the emoji font. |
| 177 | if emoji_font_seen and not scripts: |
| 178 | continue |
| 179 | |
Roozbeh Pournader | 7b822e5 | 2016-03-16 18:55:32 -0700 | [diff] [blame^] | 180 | # Check default emoji-style characters |
| 181 | assert_font_supports_none_of_chars(font, sorted(default_emoji_chars)) |
Roozbeh Pournader | fa1facc | 2016-03-16 13:53:47 -0700 | [diff] [blame] | 182 | |
Roozbeh Pournader | 7b822e5 | 2016-03-16 18:55:32 -0700 | [diff] [blame^] | 183 | # Mark default text-style characters appearing in fonts above the emoji |
| 184 | # font as seen |
| 185 | if not emoji_font_seen: |
| 186 | missing_text_chars -= set(get_best_cmap(font)) |
| 187 | |
| 188 | # Noto does not have monochrome symbols for Unicode 7.0 wingdings and |
| 189 | # webdings |
| 190 | missing_text_chars -= _chars_by_age['7.0'] |
| 191 | # TODO: Remove these after b/26113320 is fixed |
| 192 | missing_text_chars -= { |
| 193 | 0x263A, # WHITE SMILING FACE |
| 194 | 0x270C, # VICTORY HAND |
| 195 | 0x2744, # SNOWFLAKE |
| 196 | 0x2764, # HEAVY BLACK HEART |
| 197 | } |
| 198 | assert missing_text_chars == set(), ( |
| 199 | 'Text style version of some emoji characters are missing.') |
Roozbeh Pournader | fa1facc | 2016-03-16 13:53:47 -0700 | [diff] [blame] | 200 | |
| 201 | |
Roozbeh Pournader | 7b822e5 | 2016-03-16 18:55:32 -0700 | [diff] [blame^] | 202 | # Setting reverse to true returns a dictionary that maps the values to sets of |
| 203 | # characters, useful for some binary properties. Otherwise, we get a |
| 204 | # dictionary that maps characters to the property values, assuming there's only |
| 205 | # one property in the file. |
| 206 | def parse_unicode_datafile(file_path, reverse=False): |
| 207 | if reverse: |
| 208 | output_dict = collections.defaultdict(set) |
| 209 | else: |
| 210 | output_dict = {} |
| 211 | with open(file_path) as datafile: |
| 212 | for line in datafile: |
Roozbeh Pournader | fa1facc | 2016-03-16 13:53:47 -0700 | [diff] [blame] | 213 | if '#' in line: |
| 214 | line = line[:line.index('#')] |
| 215 | line = line.strip() |
| 216 | if not line: |
| 217 | continue |
| 218 | char_range, prop = line.split(';') |
| 219 | char_range = char_range.strip() |
| 220 | prop = prop.strip() |
| 221 | if '..' in char_range: |
| 222 | char_start, char_end = char_range.split('..') |
| 223 | else: |
| 224 | char_start = char_end = char_range |
| 225 | char_start = int(char_start, 16) |
| 226 | char_end = int(char_end, 16) |
Roozbeh Pournader | 7b822e5 | 2016-03-16 18:55:32 -0700 | [diff] [blame^] | 227 | char_range = xrange(char_start, char_end+1) |
| 228 | if reverse: |
| 229 | output_dict[prop].update(char_range) |
| 230 | else: |
| 231 | for char in char_range: |
| 232 | assert char not in output_dict |
| 233 | output_dict[char] = prop |
| 234 | return output_dict |
| 235 | |
| 236 | |
| 237 | def parse_ucd(ucd_path): |
| 238 | global _emoji_properties, _chars_by_age |
| 239 | _emoji_properties = parse_unicode_datafile( |
| 240 | path.join(ucd_path, 'emoji-data.txt'), reverse=True) |
| 241 | _chars_by_age = parse_unicode_datafile( |
| 242 | path.join(ucd_path, 'DerivedAge.txt'), reverse=True) |
Roozbeh Pournader | fa1facc | 2016-03-16 13:53:47 -0700 | [diff] [blame] | 243 | |
| 244 | |
Roozbeh Pournader | 0e969e2 | 2016-03-09 23:08:45 -0800 | [diff] [blame] | 245 | def main(): |
| 246 | target_out = sys.argv[1] |
| 247 | global _fonts_dir |
| 248 | _fonts_dir = path.join(target_out, 'fonts') |
| 249 | |
| 250 | fonts_xml_path = path.join(target_out, 'etc', 'fonts.xml') |
| 251 | parse_fonts_xml(fonts_xml_path) |
| 252 | |
| 253 | hyphens_dir = path.join(target_out, 'usr', 'hyphen-data') |
| 254 | check_hyphens(hyphens_dir) |
| 255 | |
Roozbeh Pournader | fa1facc | 2016-03-16 13:53:47 -0700 | [diff] [blame] | 256 | ucd_path = sys.argv[2] |
| 257 | parse_ucd(ucd_path) |
| 258 | check_emoji_availability() |
| 259 | check_emoji_defaults() |
| 260 | |
Roozbeh Pournader | 0e969e2 | 2016-03-09 23:08:45 -0800 | [diff] [blame] | 261 | |
| 262 | if __name__ == '__main__': |
| 263 | main() |