-
Notifications
You must be signed in to change notification settings - Fork 169
/
ttfcomponentizer_test.py
265 lines (204 loc) · 8.14 KB
/
ttfcomponentizer_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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import os
import pytest
from shutil import copy2, copytree
import subprocess
from fontTools.ttLib import TTFont
from afdko import ttfcomponentizer as ttfcomp
from afdko.fdkutils import (
get_temp_file_path,
get_temp_dir_path,
)
from test_utils import (
get_input_path,
get_expected_path,
generate_ttx_dump,
)
from runner import main as runner
from differ import main as differ
TOOL = 'ttfcomponentizer'
CMD = ['-t', TOOL]
TEST_TTF_FILENAME = 'ttfcomponentizer.ttf'
DESIGN_NAMES_LIST = ['acaron', 'acutecmb', 'caroncmb', 'design_name',
'gravecmb', 'tildecmb']
PRODCT_NAMES_LIST = ['production_name', 'uni01CE', 'uni0300', 'uni0301',
'uni0303', 'uni030C']
EMPTY_LIB = b"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>
"""
EMPTY_PSKEY = b"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>public.postscriptNames</key>
<dict/>
</dict>
</plist>
"""
def _get_test_ttf_path():
return get_input_path(TEST_TTF_FILENAME)
def _get_test_ufo_path():
return _get_test_ttf_path()[:-3] + 'ufo'
def _read_file(file_path):
with open(file_path, "rb") as f:
return f.read()
def _write_file(file_path, data):
with open(file_path, "wb") as f:
f.write(data)
# -----
# Tests
# -----
def test_run_no_args():
with pytest.raises(SystemExit) as exc_info:
ttfcomp.main()
assert exc_info.value.code == 2
def test_run_cli_no_args():
with pytest.raises(subprocess.CalledProcessError) as exc_info:
runner(CMD)
assert exc_info.value.returncode == 2
def test_run_invalid_font_path():
with pytest.raises(SystemExit) as exc_info:
ttfcomp.main(['not_a_file'])
assert exc_info.value.code == 2
def test_run_invalid_font_file():
with pytest.raises(SystemExit) as exc_info:
ttfcomp.main(['not_a_font.ttf'])
assert exc_info.value.code == 2
def test_run_ufo_not_found():
ttf_path = _get_test_ttf_path()
temp_dir = get_temp_dir_path()
save_path = get_temp_file_path(directory=temp_dir)
copy2(ttf_path, save_path)
assert ttfcomp.main([save_path]) == 1
def test_run_invalid_ufo():
ttf_path = _get_test_ttf_path()
temp_dir = get_temp_dir_path()
save_path = get_temp_file_path(directory=temp_dir)
ufo_path = save_path + '.ufo'
copy2(ttf_path, save_path)
copy2(ttf_path, ufo_path)
assert ttfcomp.main([save_path]) == 1
def test_run_with_output_path():
ttf_path = _get_test_ttf_path()
save_path = get_temp_file_path()
ttfcomp.main(['-o', save_path, ttf_path])
gtable = TTFont(save_path)['glyf']
composites = [gname for gname in gtable.glyphs if (
gtable[gname].isComposite())]
assert sorted(composites) == ['aa', 'aacute', 'uni01CE']
def test_run_cli_with_output_path():
actual_path = get_temp_file_path()
runner(CMD + ['-o', 'o', f'_{actual_path}',
f'_{get_input_path(TEST_TTF_FILENAME)}'])
actual_ttx = generate_ttx_dump(actual_path, ['maxp', 'glyf'])
expected_ttx = get_expected_path('ttfcomponentizer.ttx')
assert differ([expected_ttx, actual_ttx, '-s', '<ttFont sfntVersion'])
def test_run_without_output_path():
ttf_path = _get_test_ttf_path()
ufo_path = _get_test_ufo_path()
temp_dir = get_temp_dir_path()
tmp_ufo_path = os.path.join(temp_dir, os.path.basename(ufo_path))
save_path = get_temp_file_path(directory=temp_dir)
copy2(ttf_path, save_path)
copytree(ufo_path, tmp_ufo_path)
ttfcomp.main([save_path])
gtable = TTFont(save_path)['glyf']
assert gtable['agrave'].isComposite() is False
assert gtable['aacute'].isComposite() is True
def test_options_help():
with pytest.raises(SystemExit) as exc_info:
ttfcomp.main(['-h'])
assert exc_info.value.code == 0
def test_options_version():
with pytest.raises(SystemExit) as exc_info:
ttfcomp.main(['--version'])
assert exc_info.value.code == 0
def test_options_bogus_option():
with pytest.raises(SystemExit) as exc_info:
ttfcomp.main(['--bogus'])
assert exc_info.value.code == 2
def test_get_ufo_path_found():
path = _get_test_ttf_path()
assert ttfcomp.get_ufo_path(path) == _get_test_ufo_path()
def test_get_ufo_path_not_found():
# the UFO font is intentionally used as a test folder
path = os.path.join(_get_test_ufo_path(), 'lib.plist')
assert ttfcomp.get_ufo_path(path) is None
def test_get_goadb_path_found_same_folder():
path = _get_test_ttf_path()
assert os.path.basename(
ttfcomp.get_goadb_path(path)) == ttfcomp.GOADB_FILENAME
def test_get_goadb_path_found_3_folders_up():
# the UFO's glif file is intentionally used as a test file
path = os.path.join(_get_test_ufo_path(), 'glyphs', 'a.glif')
assert os.path.basename(
ttfcomp.get_goadb_path(path)) == ttfcomp.GOADB_FILENAME
def test_get_goadb_path_not_found():
path = os.path.dirname(_get_test_ttf_path())
assert ttfcomp.get_goadb_path(path) is None
def test_get_goadb_names_mapping_goadb_not_found():
path = os.path.dirname(_get_test_ttf_path())
assert ttfcomp.get_goadb_names_mapping(path) == {}
def test_get_glyph_names_mapping_invalid_ufo():
path = _get_test_ttf_path()
assert ttfcomp.get_glyph_names_mapping(path) == (None, None)
def test_get_glyph_names_mapping_names_from_lib():
result = ttfcomp.get_glyph_names_mapping(_get_test_ufo_path())
assert sorted(result[1].keys()) == DESIGN_NAMES_LIST
assert sorted(result[1].values()) == PRODCT_NAMES_LIST
def test_get_glyph_names_mapping_names_from_goadb():
# the test UFO has a 'public.postscriptNames' in its lib;
# empty the lib temporarily to force the reading of the GOADB
ufo_path = _get_test_ufo_path()
lib_path = os.path.join(ufo_path, 'lib.plist')
lib_data = _read_file(lib_path)
_write_file(lib_path, EMPTY_LIB)
result = ttfcomp.get_glyph_names_mapping(ufo_path)
_write_file(lib_path, lib_data)
assert sorted(result[1].keys()) == DESIGN_NAMES_LIST
assert sorted(result[1].values()) == PRODCT_NAMES_LIST
def test_warn_empty_psnames_key(capsys):
# the test UFO has a 'public.postscriptNames' in its lib;
# set the key to empty to ensure we get a warning
ufo_path = _get_test_ufo_path()
lib_path = os.path.join(ufo_path, 'lib.plist')
lib_data = _read_file(lib_path)
_write_file(lib_path, EMPTY_PSKEY)
result = ttfcomp.get_glyph_names_mapping(ufo_path)
_write_file(lib_path, lib_data)
assert len(result[1]) == 0
captured = capsys.readouterr()
# check (partial) warning message in stdout:
assert "WARNING: The contents of public.postscriptNames is empty." in captured.out # noqa: E501
def test_componentize():
ttf_path = _get_test_ttf_path()
save_path = get_temp_file_path()
ufo, ps_names = ttfcomp.get_glyph_names_mapping(_get_test_ufo_path())
ttcomp_obj = ttfcomp.TTComponentizer(ufo, ps_names, ttf_path, save_path)
ttcomp_obj.componentize()
# 'get_composites_data' method
comps_data = ttcomp_obj.composites_data
comps_name_list = sorted(comps_data.keys())
comps_comp_list = [comps_data[gname] for gname in comps_name_list]
assert comps_name_list == ['aa', 'aacute', 'adieresis', 'atilde',
'uni01CE']
assert comps_comp_list[1].names == ('a', 'uni0301')
assert comps_comp_list[4].names == ('a', 'uni030C')
assert comps_comp_list[1].positions == ((0, 0), (263.35, 0))
assert comps_comp_list[4].positions == ((0, 0), (263, 0))
# 'assemble_components' method
comps_data = ttfcomp.ComponentsData()
comps_data.names = ('a', 'uni01CE')
comps_data.positions = ((0, 0), (263, 0))
comps_data.same_advwidth = True
comp_one, comp_two = ttcomp_obj.assemble_components(comps_data)
assert comp_one.glyphName == 'a'
assert comp_two.glyphName == 'uni01CE'
assert (comp_one.x, comp_one.y) == (0, 0)
assert (comp_two.x, comp_two.y) == (263, 0)
assert comp_one.flags == 0x204
assert comp_two.flags == 0x4