Debian Codegraph @

tools/test/checkTableLangs.py

raw
1 #!/usr/bin/env python
2 # coding: utf-8
3 from __future__ import print_function
4
5 __license__ = """
6 This file is part of GNU FreeFont.
7
8 GNU FreeFont is free software: you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option) any later
11 version.
12
13 GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along with
18 GNU FreeFont. If not, see <http://www.gnu.org/licenses/>.
19 """
20 __author__ = "Stevan White"
21 __email__ = "[email protected]"
22 __copyright__ = "Copyright 2013, 2017, 2018 Stevan White"
23 __date__ = "$Date: "
24 __version__ = "$Revision: 2527 $"
25
26 __doc__ = """
27 Check FontForge SFD files for font feature tables which won't be activated
28 because they lack a script-language that was listed in another table.
29
30 For example
31 -----------
32 table A to activate on: latn{'dflt'}
33 table B to activate on: latn{'dflt', 'ESP '}
34 Then table A will *not* be activated for Spanish text, although it would
35 have been without the presence of the specification for B.
36
37 Likewise
38 -------
39 table A to activate on: DFLT{'dflt'}
40 table B to activate on: grek{'dflt'}
41 Then table A will *not* be activated for Greek text, although it would
42 have been without the presence of the specification for B.
43
44 The deal is, "dflt" behaves as a wildcard for languages, but only for those
45 which aren't listed *anywhere* among *any* of the tables of the current font.
46 Likewise, "DFLT{dflt}" behaves as a wildcard for script-language, but only for
47 those which aren't listed *anywhere* among *any* of the tables of the current
48 font.
49 (For better or worse that's how it is.)
50
51 This effect may be used to disable an otherwise general table for a specific
52 language, by simply creating another table that specifies the language, and
53 not listing the language explicitly in the first table. So discretion must
54 be used in judging the output of this program--it may be that the font
55 designer *intends* to disable a feature for a specific script.
56
57 Unfortunately, in fonts that deal with multiple features with multiple
58 languages, this can get very tricky, and often results in features being
59 *accidentally* disabled.
60
61 This script looks for tables which are specified for language 'dflt', then
62 checks if the inclusion of a language in some other table disables any of them.
63
64 AFTER EXPERIMENTATION: it appears that the GPOS lookups are affected by
65 the script/languages of the GSUB lookups, but *not vice-versa*. That is,
66 specifying a language tag for a given script in a GPOS table does not
67 disable a GSUB lookup for the same script that doesn't specify that language.
68 However, specifying a language tag in a GSUB table disables a GPOS lookup
69 for the same script that doesn't specify the language tag.
70
71 FIXME: this program makes no distinction between GPOS and GSUB.
72 """
73 from sys import argv, exit, stderr, stdout
74 import re
75
76 def explain_error_and_quit( e='' ):
77 if e:
78 print( 'Error:', e, file=stderr )
79 print( 'Usage:', file=stderr )
80 print( ' checkTableLangs sfd-file-path', file=stderr )
81 exit( 1 )
82
83 """
84 Typical line, with "scrp<dflt>"
85 Lookup: 6 0 0 "'ccmp' iogonek glyph decompos. in Latin" {"'ccmp' iogonek glyph decomp in Latin-1" } ['ccmp' ('latn' <'dflt' > ) ]
86
87 More complex: with "DFLT<dflt>"
88 Lookup: 1 0 0 "'onum' oldstyle figures" {"'onum' oldstyle figures-subtable" ("oldstyle" ) "'onum' oldstyle figures-1" ("oldstyle" ) } ['onum' ('DFLT' <'dflt' > 'arab' <'dflt' > 'grek' <'dflt' > 'latn' <'dflt' > ) 'onum' ('latn' <'CAT ' 'DEU ' 'ESP ' 'ISM ' 'TRK ' 'VIT ' 'dflt' > ) 'onum' ('cyrl' <'BGR ' 'MKD ' 'SRB ' 'dflt' > ) ]
89
90 """
91
92 _lookup_re = re.compile( "^Lookup: (\d+) (\d+) (\d+) (.*)$" )
93 _data_re = re.compile( '''^"(.+)"\s+{(.+)}\s+\[(.+)\]''' )
94 _dquot_names_re = re.compile( '"([^"]+)"' )
95 _squot_names_re = re.compile( "'([^']+)'" )
96 _type_scriptlangs_re = re.compile( "'([^']{4})'\s+<([^<]*)>" )
97
98 def collect_lookups_from_sfd( f ):
99 lookups = { 'GSUB':{}, 'GPOS':{} }
100 firstline = True
101 for line in f:
102 if firstline:
103 if not line.startswith( "SplineFontDB: " ):
104 explain_error_and_quit(
105 "doesn't look like FontForge SFD file." )
106 firstline = False
107
108 m = _lookup_re.match( line )
109 if m:
110 data = m.group( 4 )
111 dm = _data_re.match( data )
112 if dm:
113 tabletype = int( m.group( 1 ) )
114 if tabletype < 256:
115 parse_lookup( dm, lookups['GSUB'] )
116 else:
117 parse_lookup( dm, lookups['GPOS'] )
118 return lookups
119
120 def parse_lookup( dm, lookups ):
121 tableName = dm.group( 1 )
122 subtableNames = dm.group( 2 )
123 scriptNames = dm.group( 3 )
124 lookups[tableName] = {}
125 #if subtableNames:
126 # names = _dquot_names_re.findall( subtableNames )
127 if scriptNames:
128 scn = _type_scriptlangs_re.findall( scriptNames )
129 for s in scn:
130 script = s[0]
131 langs = s[1]
132 lngs = set( _squot_names_re.findall( langs ) )
133 if not script in lookups[tableName]:
134 lookups[tableName][script] = set()
135 lookups[tableName][script] |= lngs
136
137 def printall( lookups ):
138 for t in lookups:
139 print( "========== " + t + " ===============================" )
140 for tn in lookups[t]:
141 print( tn )
142 for script in lookups[t][tn]:
143 print( '\t' + script,
144 ', '.join( lookups[t][tn][script] ) )
145
146 def reverse_table_script( lookups ):
147 """ reverse the table-script relationship """
148 scriptTable = {}
149 for (name, script_data) in lookups.items():
150 for script in script_data:
151 if script not in scriptTable:
152 scriptTable[script] = set()
153 scriptTable[script].add( name )
154 return scriptTable
155
156 def reverse_table_script_lang( lookups ):
157 """ reverse the table-script relationship """
158 scriptLangTable = []
159 for (name, script_data) in lookups.items():
160 for script in script_data:
161 for lang in script_data[script]:
162 sl = ( script, lang )
163 if sl not in scriptLangTable:
164 scriptLangTable[sl] = set()
165 scriptLangTable[sl].add( sl )
166 return scriptLangTable
167
168 def tables_with_dflt_entry( lookups ):
169 """ tables with a 'dflt' entry, associated script of the entry """
170 tablesWdflt = {}
171 scriptTable = reverse_table_script( lookups )
172 for (script,names) in scriptTable.items():
173 for name in names:
174 allLangs = []
175 lngs = lookups[name][script]
176 if 'dflt' in lngs:
177 tablesWdflt[name] = script
178 return tablesWdflt
179
180 def tables_with_DFLT_dflt_entry( lookups ):
181 """ tables with a 'dflt' entry, associated script of the entry """
182 tablesWdflt = {}
183 scriptTable = reverse_table_script( lookups )
184 dfltLngs = set(['dflt'])
185 for (script,names) in scriptTable.items():
186 if script == 'DFLT':
187 for name in names:
188 allLangs = []
189 lngs = lookups[name][script]
190 if lngs == dfltLngs:
191 tablesWdflt[name] = script
192 return tablesWdflt
193
194 def tables_disbled_for_script( lookups, other_lookups=None ):
195 """ For each table with a 'dflt' language for a given script,
196 note the explicit languages listed for the script, and search all
197 other tables for languages in that script *not* explicitly listed
198 in the current table.
199 """
200 disabled = {}
201 allLookups = dict( lookups )
202 if other_lookups:
203 allLookups.update( other_lookups )
204 scriptTable = reverse_table_script( allLookups )
205 tablesWdflt = tables_with_dflt_entry( lookups )
206 dfltLang = set( ['dflt'] )
207 for name in tablesWdflt:
208 script = tablesWdflt[name]
209 explicit = allLookups[name][script] - dfltLang
210 tablesBesidesThis = scriptTable[script] - set( [name] )
211 for other in tablesBesidesThis:
212 otherExplicit = allLookups[other][script] - dfltLang
213 conflict = otherExplicit - explicit
214 if conflict:
215 if not name in disabled:
216 disabled[name] = []
217 disabled[name].append( ( other, conflict ) )
218 return disabled
219
220 def tables_disbled_for_script_lang( lookups, other_lookups=None ):
221 """ For each table with a 'DFLT{dflt}' script-language, note the
222 explicit script-languages in the table, and search all other tables
223 for script-languages *not* explicitly listed in the current table.
224 """
225 disabled = {}
226 allLookups = dict( lookups )
227 if other_lookups:
228 allLookups.update( other_lookups )
229 scriptTable = reverse_table_script( allLookups )
230 tablesWdflt = tables_with_DFLT_dflt_entry( lookups )
231 dfltScrptLang = { 'DFLT':['dflt'] }
232
233 for name in tablesWdflt:
234 explicit = dict( allLookups[name] )
235 del explicit['DFLT']
236 tablesBesidesThis = dict( allLookups )
237 if 'DFLT' in tablesBesidesThis:
238 del tablesBesidesThis['DFLT']
239 for other in tablesBesidesThis:
240 otherExplicit = dict( allLookups[other] )
241 if 'DFLT' in otherExplicit:
242 del otherExplicit['DFLT']
243 conflict = set( otherExplicit ) - set( explicit )
244 if conflict:
245 if not name in disabled:
246 disabled[name] = []
247 disabled[name].append( ( other, conflict ) )
248 return disabled
249
250 # =======================================================================
251 filename = argv[1] if len( argv ) > 1 else ''
252 if not filename:
253 explain_error_and_quit()
254
255 f = None
256 try:
257 f = open( filename, 'r' )
258 except Exception as e:
259 explain_error_and_quit( e )
260
261 lookups = collect_lookups_from_sfd( f )
262 # printall( lookups )
263 disabled4Script = tables_disbled_for_script( lookups['GSUB'] )
264 disabled4Script.update( tables_disbled_for_script( lookups['GPOS'], lookups['GSUB'] ) )
265 disabled4ScriptLang = tables_disbled_for_script_lang( lookups['GSUB'] )
266 disabled4ScriptLang.update( tables_disbled_for_script_lang( lookups['GPOS'], lookups['GSUB'] ) )
267
268 for feat in sorted( disabled4Script ):
269 print( 'feature "' + feat + '" disabled for languages' )
270 allothers = []
271 alllangs = set()
272 for ( other, langs ) in disabled4Script[feat]:
273 alllangs |= langs
274 allothers.append( other )
275 print( "\t", list( alllangs ) )
276 print( '\tby "', allothers[0], ('" &cet' if len( allothers ) else '' ) )
277
278 for feat in sorted( disabled4ScriptLang ):
279 print( 'feature "' + feat + '" disabled for script/language' )
280 allothers = []
281 alllangs = set()
282 for ( other, langs ) in disabled4ScriptLang[feat]:
283 alllangs |= langs
284 allothers.append( other )
285 print( "\t", list( alllangs ) )
286 print( '\tby "', allothers[0], ('" &cet' if len( allothers ) else '' ) )
287