Debian Codegraph @

tests/test1008.py

raw symbols SCIP
1 # Test various Python APIs to examine all glyphs in a font
2 #
3 # Checking these API operations from python.c:
4 #
5 # if integer in f: - font has a glyph for this encoding value?
6 # glyph = f[integer] - get glyph object for encoding value index
7 # for name in f: - iterate through glyph names in font
8 # glyph = f[name] - get glyph object for glyph name key
9 # for glyph in f.glyphs()
10 # - iterate thru glyphs in GID order
11 # for glyph in f.glyphs('encoding')
12 # - iterate thru glyphs in encoding order
13 # if name in f: - font has a glyph for this glyph name?
14
15 import sys, fontforge
16
17 dontFailTest = False
18
19 problemsSeen = 0
20
21 inputFilepath = sys.argv[1]
22 print("Opening input file '" + inputFilepath + "'")
23 font1 = fontforge.open(inputFilepath)
24
25 print(" Font '%s' from %s" % (font1.fontname, font1.path))
26 print("")
27
28 # We can get names in two different orderings, by encoding value order
29 # and by 'GID' order. They can differ, as in OverlapBugs.sfd
30
31 glyphNamesInGIDOrder = []
32 glyphNamesInEncOrder = []
33
34
35 # This loops over a range of possible GID's, testing to each to see
36 # if it is for a valid glyph. Thus the tested operations are:
37 # if enc in font1: - 'contains' test
38 # glyph = font1[enc] - index by number
39
40 if True:
41 print("Listing glyph names over range of encoding values:")
42
43 # XXX use an API to get actual count? then check that API? :)
44 for enc in range(1024):
45 if enc in font1:
46 glyph = font1[enc]
47 glyphName = glyph.glyphname
48 print(" {:3d} glyphName: '{}'".format(enc, glyphName))
49 glyphNamesInEncOrder.append(glyphName)
50 # XXX check that 'enc' matches glyph.encoding ?
51
52 print("")
53
54 # This loops using an iterator running over all glyphs in a font,
55 # in GID order, and returning the glyph name.
56 # Here the tested operations are:
57 # for glyphName in font1: - font iterator for names
58 # glyph = font1[glyphName] - index by string key
59
60 # Allow testing against either a known list of glyph names, or
61 # using the iterator from the font object.
62
63 iterGlyph = font1
64 #iterGlyph = ( '.notdef', 'C12341', 'C12357', 'C12360', 'C12592', 'C12860' )
65
66
67 if True:
68 print("Listing glyph names using \"for glyphName in font:\" iterator:")
69
70 for glyphName in iterGlyph:
71 glyph = font1[glyphName]
72 enc = glyph.encoding
73 print(" {:3d} glyphName: '{}'".format(enc, glyphName))
74 glyphNamesInGIDOrder.append(glyphName)
75
76 # While the order of names may differ between encoding and GID ordering,
77 # these two lists should at least have the same count of names
78 if len(glyphNamesInEncOrder) != len(glyphNamesInGIDOrder):
79 print("*Error: the list of glyph names saved from using font iterator does not match the lists from other API usage")
80 print(" Previous list count: {:d}".format(len(glyphNamesInEncOrder)))
81 print(" this list count: {:d}".format(len(glyphNamesInGIDOrder)))
82 print(" Previous list: {}".format(glyphNamesInEncOrder))
83 print(" this list: {}".format(glyphNamesInGIDOrder))
84 problemsSeen += 1
85
86 print("")
87
88
89
90 if True:
91 print("Listing glyph names using font.glyphs() iterator:")
92
93 glyphNames = []
94
95 for glyph in font1.glyphs():
96 glyphName = glyph.glyphname
97 enc = glyph.encoding
98 print(" {:3d} glyphName: '{}'".format(enc, glyphName))
99 glyphNames.append(glyphName)
100
101
102 if glyphNamesInGIDOrder != glyphNames:
103 print("*Error: the list of glyph names saved from using iterator font.glyphs() does not match the lists from other API usage")
104 print(" Previous list count: {:d}".format(len(glyphNamesInGIDOrder)))
105 print(" this list count: {:d}".format(len(glyphNames)))
106 print(" Previous list: {}".format(glyphNamesInGIDOrder))
107 print(" this list: {}".format(glyphNames))
108 problemsSeen += 1
109
110 print("")
111
112 print("Listing glyph names using font.glyphs('encoding') iterator:")
113
114 glyphNames = []
115
116 for glyph in font1.glyphs('encoding'):
117 glyphName = glyph.glyphname
118 enc = glyph.encoding
119 print(" {:3d} glyphName: '{}'".format(enc, glyphName))
120 glyphNames.append(glyphName)
121
122
123 if glyphNamesInEncOrder != glyphNames:
124 print("*Error: the list of glyph names saved from using iterator font.glyphs('encoding') does not match the lists from other API usage")
125 print(" Previous list count: {:d}".format(len(glyphNamesInEncOrder)))
126 print(" this list count: {:d}".format(len(glyphNames)))
127 print(" Previous list: {}".format(glyphNamesInEncOrder))
128 print(" this list: {}".format(glyphNames))
129 problemsSeen += 1
130
131 print("")
132
133
134 if True:
135 print("Listing glyph names in list and checking with \"glyphName in font\":")
136
137 glyphNames = []
138
139 for glyphName in glyphNamesInEncOrder:
140 if glyphName in font1:
141 glyph = font1[glyphName]
142 enc = glyph.encoding
143 print(" {:3d} glyphName: '{}'".format(enc, glyphName))
144 glyphNames.append(glyphName)
145 else:
146 print("*Warning: known glyph name '{:s}' not found using test \"'{:s}' in font\"".format(glyphName, glyphName))
147
148 if glyphNamesInEncOrder != glyphNames:
149 print("*Error: the list of glyph names found using \"glyphName in font\" does not match the lists from other API usage")
150 print(" Previous list count: {:d}".format(len(glyphNamesInEncOrder)))
151 print(" this list count: {:d}".format(len(glyphNames)))
152 print(" Previous list: {}".format(glyphNamesInEncOrder))
153 print(" this list: {}".format(glyphNames))
154 problemsSeen += 1
155
156 print("")
157
158
159 if problemsSeen:
160 if dontFailTest:
161 print(" %d problem results were detected - would fail this test..." % (problemsSeen))
162 print(" but 'no fail' flag set.")
163 sys.exit(0)
164 else:
165 print(" %d problem results were detected - failing this test!" % (problemsSeen))
166 sys.exit(" %d failing glyphs were detected - failing this test!" % (problemsSeen))
167 else:
168 print(" No problems found - test successful")
169 sys.exit(0)
170
171 # vim:ft=python:ts=2:sw=2:et:is:hls:ss=10:tw=222: