-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathufotools_test.py
239 lines (182 loc) · 7.02 KB
/
ufotools_test.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import plistlib
from shutil import copytree
from pathlib import Path
from afdko import ufotools as ut
from afdko.fdkutils import (
# get_temp_file_path,
get_temp_dir_path,
)
from test_utils import (
get_input_path,
)
TEST_UFO_FILENAME = 'ufotools_basic.ufo'
def _get_test_ufo_path():
return get_input_path(TEST_UFO_FILENAME)
def _dict2plist(my_dict, plist_file):
with open(plist_file, 'wb') as pl_blob:
plistlib.dump(my_dict, pl_blob)
def _plist2dict(plist_file):
with open(plist_file, 'rb') as pl_blob:
pl_dict = plistlib.load(pl_blob)
return pl_dict
def _add_entry_to_plist(kv_entry, plist_file):
'''
add a simple key: value pair to an existing plist file
'''
key, value = kv_entry
plist_dict = _plist2dict(plist_file)
plist_dict[key] = value
_dict2plist(plist_dict, plist_file)
def test_parseGlyphOrder():
ufo_path = Path(_get_test_ufo_path())
glyph_order = ut.parseGlyphOrder(ufo_path / 'lib.plist')
assert glyph_order == {'.notdef': 0, 'space': 1, 'a': 2, 'b': 3, 'c': 4}
def test_UFOFontData():
ufo_path = Path(_get_test_ufo_path())
ufd = ut.UFOFontData(ufo_path, False, '')
assert ufd.getGlyphSrcPath('a') == str(ufo_path / 'glyphs' / 'a.glif')
def test_cleanupContentsList_missing_glyph(capsys):
ufo_path = Path(_get_test_ufo_path())
temp_dir = Path(get_temp_dir_path())
tmp_ufo_path = temp_dir / ufo_path.name
copytree(ufo_path, tmp_ufo_path)
# delete a glyph from glyphs directory
glif_path = tmp_ufo_path / 'glyphs' / 'a.glif'
glif_path.unlink()
ut.cleanupContentsList(tmp_ufo_path / 'glyphs')
out, err = capsys.readouterr()
assert 'glif was missing: a, a.glif' in out
def test_cleanupContentsList_no_plist():
temp_dir = Path(get_temp_dir_path())
# make dummy glyph dir
glyph_dir = temp_dir / 'glyphs.com.adobe.type.processedglyphs'
glyph_dir.mkdir()
# make some glif files but no contents.plist
for glyph_name in 'xyz':
glyph_path = glyph_dir / f'{glyph_name}.glif'
glyph_path.touch()
# although there is no contents.plist, this should not fail (#1606)
ut.cleanupContentsList(glyph_dir)
def test_cleanUpGLIFFiles_extraneous_glyph(capsys):
ufo_path = Path(_get_test_ufo_path())
temp_dir = Path(get_temp_dir_path())
tmp_ufo_path = temp_dir / ufo_path.name
copytree(ufo_path, tmp_ufo_path)
glyphs_dir = tmp_ufo_path / 'glyphs'
contents_plist = glyphs_dir / 'contents.plist'
# add an unexpected .glif file
unexpected_glif = glyphs_dir / 'x.glif'
unexpected_glif.touch()
changed = ut.cleanUpGLIFFiles(contents_plist, glyphs_dir)
assert changed == 1
out, err = capsys.readouterr()
assert 'Removing glif file x.glif' in out
def test_cleanUpGLIFFiles_other_layer(capsys):
ufo_path = Path(_get_test_ufo_path())
temp_dir = Path(get_temp_dir_path())
tmp_ufo_path = temp_dir / ufo_path.name
copytree(ufo_path, tmp_ufo_path)
glyphs_dir = tmp_ufo_path / 'glyphs'
contents_plist = glyphs_dir / 'contents.plist'
other_glyphs_dir = tmp_ufo_path / 'otherglyphs'
other_contents_plist = other_glyphs_dir / 'contents.plist'
copytree(glyphs_dir, other_glyphs_dir)
# add a glyph to the other directory which shouldn’t be there
unexpected_glif = other_glyphs_dir / 'x.glif'
unexpected_glif.touch()
_add_entry_to_plist(('x', 'x.glif'), other_contents_plist)
changed = ut.cleanUpGLIFFiles(contents_plist, other_glyphs_dir)
assert changed == 1
out, err = capsys.readouterr()
assert 'Removing glif x' in out
def test_cleanUpGLIFFiles_no_plist():
ufo_path = Path(_get_test_ufo_path())
temp_dir = Path(get_temp_dir_path())
tmp_ufo_path = temp_dir / ufo_path.name
copytree(ufo_path, tmp_ufo_path)
glyphs_dir = tmp_ufo_path / 'glyphs'
contents_plist = glyphs_dir / 'contents.plist'
other_glyphs_dir = tmp_ufo_path / 'otherglyphs'
other_contents_plist = other_glyphs_dir / 'contents.plist'
copytree(glyphs_dir, other_glyphs_dir)
# remove contents plist
other_contents_plist.unlink()
# nothing should happen
changed = ut.cleanUpGLIFFiles(contents_plist, other_glyphs_dir)
assert changed == 0
def test_cleanUpGLIFFiles_empty_folder():
ufo_path = Path(_get_test_ufo_path())
temp_dir = Path(get_temp_dir_path())
tmp_ufo_path = temp_dir / ufo_path.name
copytree(ufo_path, tmp_ufo_path)
glyphs_dir = tmp_ufo_path / 'glyphs'
contents_plist = glyphs_dir / 'contents.plist'
other_glyphs_dir = tmp_ufo_path / 'otherglyphs'
other_glyphs_dir.mkdir()
# nothing should happen
changed = ut.cleanUpGLIFFiles(contents_plist, other_glyphs_dir)
assert changed == 0
def test_validateLayers_empty_folder(capsys):
ufo_path = Path(_get_test_ufo_path())
temp_dir = Path(get_temp_dir_path())
tmp_ufo_path = temp_dir / ufo_path.name
copytree(ufo_path, tmp_ufo_path)
processed_dir = tmp_ufo_path / 'glyphs.com.adobe.type.processedglyphs'
processed_dir.mkdir()
ut.validateLayers(tmp_ufo_path)
out, err = capsys.readouterr()
print(out)
def test_makeUFOFMNDB_empty_UFO(capsys):
temp_dir = Path(get_temp_dir_path())
dummy_ufo = temp_dir / 'dummy.ufo'
fontinto_plist = dummy_ufo / 'fontinfo.plist'
dummy_ufo.mkdir()
_dict2plist({}, fontinto_plist)
fmndb_path = ut.makeUFOFMNDB(dummy_ufo)
out, err = capsys.readouterr()
assert "UFO font is missing 'postscriptFontName'" in out
assert "UFO font is missing 'familyName'" in out
assert "UFO font is missing 'styleName'" in out
with open(fmndb_path, 'r') as fmndb_blob:
fmndb_data = fmndb_blob.read().splitlines()
assert fmndb_data == [
'[NoFamilyName-Regular]',
'\tf=NoFamilyName',
'\ts=Regular']
def test_makeUFOFMNDB_psname(capsys):
temp_dir = Path(get_temp_dir_path())
dummy_ufo = temp_dir / 'dummy.ufo'
fontinto_plist = dummy_ufo / 'fontinfo.plist'
dummy_ufo.mkdir()
fontinfo_dict = {
'postscriptFontName': 'Dummy-Italic'
}
_dict2plist(fontinfo_dict, fontinto_plist)
fmndb_path = ut.makeUFOFMNDB(dummy_ufo)
out, err = capsys.readouterr()
assert "UFO font is missing 'familyName'" in out
assert "UFO font is missing 'styleName'" in out
with open(fmndb_path, 'r') as fmndb_blob:
fmndb_data = fmndb_blob.read().splitlines()
assert fmndb_data == [
'[Dummy-Italic]',
'\tf=Dummy',
'\ts=Italic']
def test_makeUFOFMNDB_wholesome():
temp_dir = Path(get_temp_dir_path())
dummy_ufo = temp_dir / 'dummy.ufo'
fontinto_plist = dummy_ufo / 'fontinfo.plist'
dummy_ufo.mkdir()
fontinfo_dict = {
'postscriptFontName': 'Whats-Up',
'familyName': 'Addams Family',
'styleName': 'Narrow Extended'
}
_dict2plist(fontinfo_dict, fontinto_plist)
fmndb_path = ut.makeUFOFMNDB(dummy_ufo)
with open(fmndb_path, 'r') as fmndb_blob:
fmndb_data = fmndb_blob.read().splitlines()
assert fmndb_data == [
'[Whats-Up]',
'\tf=Addams Family',
'\ts=Narrow Extended']