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'] |
| 167 | emoji_font_seen = False |
| 168 | for name, scripts, variant, weight, style, font in _fallback_chain: |
| 169 | if 'Zsye' in scripts: |
| 170 | emoji_font_seen = True |
| 171 | # No need to check the emoji font |
| 172 | continue |
| 173 | # For later fonts, we only check them if they have a script |
| 174 | # defined, since the defined script may get them to a higher |
| 175 | # score even if they appear after the emoji font. |
| 176 | if emoji_font_seen and not scripts: |
| 177 | continue |
| 178 | |
| 179 | if font[1] is None: |
| 180 | emoji_to_skip = set() |
| 181 | else: |
| 182 | # CJK font, skip checking the following characters for now. |
| 183 | # See b/26153752 |
| 184 | emoji_to_skip = ({ |
| 185 | 0x26BD, # SOCCER BALL |
| 186 | 0x26BE, # BASEBALL |
| 187 | 0x1F18E, # NEGATIVE SQUARED AB |
| 188 | 0x1F201, # SQUARED KATAKANA KOKO |
| 189 | 0x1F21A, # SQUARED CJK UNIFIED IDEOGRAPH-7121 |
| 190 | 0x1F22F, # SQUARED CJK UNIFIED IDEOGRAPH-6307 |
| 191 | } | set(xrange(0x1F191, 0x1F19A+1)) |
| 192 | | set(xrange(0x1F232, 0x1F236+1)) |
| 193 | | set(xrange(0x1F238, 0x1F23A+1)) |
| 194 | | set(xrange(0x1F250, 0x1F251+1))) |
| 195 | |
| 196 | assert_font_supports_none_of_chars(font, |
| 197 | sorted(default_emoji_chars - emoji_to_skip)) |
| 198 | |
| 199 | |
| 200 | def parse_ucd(ucd_path): |
| 201 | global _emoji_properties |
| 202 | _emoji_properties = collections.defaultdict(set) |
| 203 | with open(path.join(ucd_path, 'emoji-data.txt')) as emoji_data_txt: |
| 204 | for line in emoji_data_txt: |
| 205 | if '#' in line: |
| 206 | line = line[:line.index('#')] |
| 207 | line = line.strip() |
| 208 | if not line: |
| 209 | continue |
| 210 | char_range, prop = line.split(';') |
| 211 | char_range = char_range.strip() |
| 212 | prop = prop.strip() |
| 213 | if '..' in char_range: |
| 214 | char_start, char_end = char_range.split('..') |
| 215 | else: |
| 216 | char_start = char_end = char_range |
| 217 | char_start = int(char_start, 16) |
| 218 | char_end = int(char_end, 16) |
| 219 | _emoji_properties[prop].update(xrange(char_start, char_end+1)) |
| 220 | |
| 221 | |
Roozbeh Pournader | 0e969e2 | 2016-03-09 23:08:45 -0800 | [diff] [blame] | 222 | def main(): |
| 223 | target_out = sys.argv[1] |
| 224 | global _fonts_dir |
| 225 | _fonts_dir = path.join(target_out, 'fonts') |
| 226 | |
| 227 | fonts_xml_path = path.join(target_out, 'etc', 'fonts.xml') |
| 228 | parse_fonts_xml(fonts_xml_path) |
| 229 | |
| 230 | hyphens_dir = path.join(target_out, 'usr', 'hyphen-data') |
| 231 | check_hyphens(hyphens_dir) |
| 232 | |
Roozbeh Pournader | fa1facc | 2016-03-16 13:53:47 -0700 | [diff] [blame^] | 233 | ucd_path = sys.argv[2] |
| 234 | parse_ucd(ucd_path) |
| 235 | check_emoji_availability() |
| 236 | check_emoji_defaults() |
| 237 | |
Roozbeh Pournader | 0e969e2 | 2016-03-09 23:08:45 -0800 | [diff] [blame] | 238 | |
| 239 | if __name__ == '__main__': |
| 240 | main() |