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