1+ #!/usr/bin/python3
2+
3+ # Checks characters used in OpenTTD strings against characters present in these fonts
4+
5+ # Usage:
6+ # python3 checkTranslationChars.py path/to/openttd/src/lang
7+ # python3 path/to/checkTranslationChars.py [run in openttd/src/lang]
8+
9+ import glob , os , sys
10+ from fontTools .ttLib import TTFont
11+
12+ if len (sys .argv ) > 1 :
13+ langpath = sys .argv [1 ]
14+ else :
15+ langpath = "."
16+
17+ # maximum number of missing characters to list in full
18+ maxlist = 100
19+
20+ # find all ttfs by suffix in directory
21+ suffix = ".ttf"
22+ scriptpath = os .path .dirname (os .path .realpath (__file__ ))
23+ files = [
24+ os .path .join (scriptpath , "openttd-sans" , "OpenTTD-Sans.ttf" ),
25+ os .path .join (scriptpath , "openttd-serif" , "OpenTTD-Serif.ttf" ),
26+ os .path .join (scriptpath , "openttd-mono" , "OpenTTD-Mono.ttf" ),
27+ os .path .join (scriptpath , "openttd-small" , "OpenTTD-Small.ttf" ),
28+ os .path .join (scriptpath , "openttd-small" , "OpenTTD-SmallCaps.ttf" )
29+ ]
30+ fonts = []
31+ for file in files :
32+ # for each font, record name and list of character codes available
33+ font = TTFont (file )
34+ fonts .append ({
35+ "name" : os .path .basename (file [:- len (suffix )]),
36+ "codes" : list (font .getBestCmap ().keys ())
37+ })
38+
39+ # find all lang files by suffix in directory
40+ suffix = ".txt"
41+ files = glob .glob (os .path .join (langpath , "*" + suffix ))
42+ print (os .path .join (langpath , "*" + suffix ))
43+ for file in files :
44+ # for each language
45+ lang = os .path .basename (file [:- len (suffix )])
46+ with open (file , "r" ) as handle :
47+ # read lines and remove empty lines
48+ strings = [x for x in handle .read ().splitlines () if x ]
49+ # extract translated strings
50+ strings = [x [x .find (":" ) + 1 :] for x in strings if x [0 ] != "#" ]
51+ # list all character codes and characters across strings
52+ codes = sorted (list (set ([ord (x ) for x in "" .join (strings )])))
53+ chars = [chr (x ) for x in codes ]
54+ # check if fully supported by each font
55+ print (lang )
56+ completecount = 0
57+ for font in fonts :
58+ missing = [x for x in codes if x not in font ["codes" ]]
59+ if len (missing ) == 0 :
60+ completecount += 1
61+ else :
62+ if len (missing ) <= maxlist :
63+ print ("" , font ["name" ], "missing " + str (len (missing ))+ ":" , "(" + "" .join ([chr (x ) for x in missing ])+ ")" )
64+ else :
65+ print ("" , font ["name" ], "missing " + str (len (missing ))+ ":" , "[too many to list]" )
66+ if completecount == len (fonts ):
67+ print ("" , "[complete]" )
0 commit comments