blob: 14888c00036c5921e55610215d98d659bffefb17 [file] [log] [blame]
Roozbeh Pournader0e969e22016-03-09 23:08:45 -08001#!/usr/bin/env python
2
3import collections
4import glob
5from os import path
6import sys
7from xml.etree import ElementTree
8
9from fontTools import ttLib
10
11LANG_TO_SCRIPT = {
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070012 'as': 'Beng',
13 'bn': 'Beng',
14 'cy': 'Latn',
15 'da': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080016 'de': 'Latn',
17 'en': 'Latn',
18 'es': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070019 'et': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080020 'eu': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070021 'fr': 'Latn',
22 'ga': 'Latn',
23 'gu': 'Gujr',
24 'hi': 'Deva',
25 'hr': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080026 'hu': 'Latn',
27 'hy': 'Armn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070028 'ja': 'Jpan',
29 'kn': 'Knda',
30 'ko': 'Kore',
31 'ml': 'Mlym',
32 'mn': 'Cyrl',
33 'mr': 'Deva',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080034 'nb': 'Latn',
35 'nn': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070036 'or': 'Orya',
37 'pa': 'Guru',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080038 'pt': 'Latn',
Jungshik Shin6c4f9e02016-03-19 09:32:34 -070039 'sl': 'Latn',
40 'ta': 'Taml',
41 'te': 'Telu',
42 'tk': 'Latn',
Roozbeh Pournader0e969e22016-03-09 23:08:45 -080043}
44
45def lang_to_script(lang_code):
46 lang = lang_code.lower()
47 while lang not in LANG_TO_SCRIPT:
48 hyphen_idx = lang.rfind('-')
49 assert hyphen_idx != -1, (
50 'We do not know what script the "%s" language is written in.'
51 % lang_code)
52 assumed_script = lang[hyphen_idx+1:]
53 if len(assumed_script) == 4 and assumed_script.isalpha():
54 # This is actually the script
55 return assumed_script.title()
56 lang = lang[:hyphen_idx]
57 return LANG_TO_SCRIPT[lang]
58
59
60def get_best_cmap(font):
61 font_file, index = font
62 font_path = path.join(_fonts_dir, font_file)
63 if index is not None:
64 ttfont = ttLib.TTFont(font_path, fontNumber=index)
65 else:
66 ttfont = ttLib.TTFont(font_path)
67 all_unicode_cmap = None
68 bmp_cmap = None
69 for cmap in ttfont['cmap'].tables:
70 specifier = (cmap.format, cmap.platformID, cmap.platEncID)
71 if specifier == (4, 3, 1):
72 assert bmp_cmap is None, 'More than one BMP cmap in %s' % (font, )
73 bmp_cmap = cmap
74 elif specifier == (12, 3, 10):
75 assert all_unicode_cmap is None, (
76 'More than one UCS-4 cmap in %s' % (font, ))
77 all_unicode_cmap = cmap
78
79 return all_unicode_cmap.cmap if all_unicode_cmap else bmp_cmap.cmap
80
81
82def assert_font_supports_any_of_chars(font, chars):
83 best_cmap = get_best_cmap(font)
84 for char in chars:
85 if char in best_cmap:
86 return
87 sys.exit('None of characters in %s were found in %s' % (chars, font))
88
89
90def check_hyphens(hyphens_dir):
91 # Find all the scripts that need automatic hyphenation
92 scripts = set()
93 for hyb_file in glob.iglob(path.join(hyphens_dir, '*.hyb')):
94 hyb_file = path.basename(hyb_file)
95 assert hyb_file.startswith('hyph-'), (
96 'Unknown hyphenation file %s' % hyb_file)
97 lang_code = hyb_file[hyb_file.index('-')+1:hyb_file.index('.')]
98 scripts.add(lang_to_script(lang_code))
99
100 HYPHENS = {0x002D, 0x2010}
101 for script in scripts:
102 fonts = _script_to_font_map[script]
103 assert fonts, 'No fonts found for the "%s" script' % script
104 for font in fonts:
105 assert_font_supports_any_of_chars(font, HYPHENS)
106
107
108def parse_fonts_xml(fonts_xml_path):
109 global _script_to_font_map, _fallback_chain
110 _script_to_font_map = collections.defaultdict(set)
111 _fallback_chain = []
112 tree = ElementTree.parse(fonts_xml_path)
113 for family in tree.findall('family'):
114 name = family.get('name')
115 variant = family.get('variant')
116 langs = family.get('lang')
117 if name:
118 assert variant is None, (
119 'No variant expected for LGC font %s.' % name)
120 assert langs is None, (
121 'No language expected for LGC fonts %s.' % name)
122 else:
123 assert variant in {None, 'elegant', 'compact'}, (
124 'Unexpected value for variant: %s' % variant)
125
126 if langs:
127 langs = langs.split()
128 scripts = {lang_to_script(lang) for lang in langs}
129 else:
130 scripts = set()
131
132 for child in family:
133 assert child.tag == 'font', (
134 'Unknown tag <%s>' % child.tag)
135 font_file = child.text
136 weight = int(child.get('weight'))
137 assert weight % 100 == 0, (
138 'Font weight "%d" is not a multiple of 100.' % weight)
139
140 style = child.get('style')
141 assert style in {'normal', 'italic'}, (
142 'Unknown style "%s"' % style)
143
144 index = child.get('index')
145 if index:
146 index = int(index)
147
148 _fallback_chain.append((
149 name,
150 frozenset(scripts),
151 variant,
152 weight,
153 style,
154 (font_file, index)))
155
156 if name: # non-empty names are used for default LGC fonts
157 map_scripts = {'Latn', 'Grek', 'Cyrl'}
158 else:
159 map_scripts = scripts
160 for script in map_scripts:
161 _script_to_font_map[script].add((font_file, index))
162
163
164def main():
165 target_out = sys.argv[1]
166 global _fonts_dir
167 _fonts_dir = path.join(target_out, 'fonts')
168
169 fonts_xml_path = path.join(target_out, 'etc', 'fonts.xml')
170 parse_fonts_xml(fonts_xml_path)
171
172 hyphens_dir = path.join(target_out, 'usr', 'hyphen-data')
173 check_hyphens(hyphens_dir)
174
175
176if __name__ == '__main__':
177 main()