forked from adobe-type-tools/afdko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
124 lines (95 loc) · 3.46 KB
/
test_utils.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
import inspect
import os
import shutil
import subprocess
from fontTools.ttLib import TTCollection, TTFont, TTLibError
from afdko.fdkutils import (get_temp_file_path, run_shell_command)
TIMEOUT = 240 # seconds
def generalizeCFF(otfPath, do_sfnt=True):
"""
Adapted from similar routine in 'buildmasterotfs'. This uses
temp files for both tx output and sfntedit output instead of
overwriting 'otfPath', and also provides an option to skip
the sfntedit step (so 'otfPath' can be either a .otf file or
a .cff file).
"""
tmp_tx_path = get_temp_file_path()
out_path = get_temp_file_path()
shutil.copyfile(otfPath, out_path, follow_symlinks=True)
if not run_shell_command(['tx', '-cff', '+b', '-std', '-no_opt',
otfPath, tmp_tx_path]):
raise Exception
if do_sfnt:
if not run_shell_command(['sfntedit', '-a',
f'CFF ={tmp_tx_path}', out_path]):
raise Exception
return out_path
else:
return tmp_tx_path
def get_data_dir():
tests_dir, test_file = os.path.split(inspect.stack()[2][1])
tool_name = test_file[:-8] # trim '_test.py' portion
return os.path.join(tests_dir, tool_name + '_data')
def get_expected_path(file_name):
return os.path.join(get_data_dir(), 'expected_output', file_name)
def get_input_path(file_name):
return os.path.join(get_data_dir(), 'input', file_name)
def get_bad_input_path(file_name):
return os.path.join(get_data_dir(), 'input', 'bad', file_name)
def generate_ttx_dump(font_path, tables=None):
try:
font = TTFont(font_path)
except TTLibError:
font = TTCollection(font_path)
with font:
temp_path = get_temp_file_path()
font.saveXML(temp_path, tables=tables)
return temp_path
def generate_spot_dumptables(font_path, tables):
tmp_txt_path = get_temp_file_path()
myargs = ['spot', "-t" + ",".join(tables), font_path]
spot_txt = subprocess.check_output(myargs, timeout=TIMEOUT)
tf = open(tmp_txt_path, "wb")
tf.write(spot_txt)
tf.close()
return tmp_txt_path
def generate_ps_dump(font_path):
with open(font_path, 'rb') as ps_file:
data = ps_file.read()
line = ''
i = 0
in_binary_section = False
bytes_read = 0
byte_count = 0
temp_path = get_temp_file_path()
with open(temp_path, 'w') as temp_file:
while i < len(data):
x = data[i]
if not in_binary_section:
if x != 0x0A:
line += '%c' % x
else:
if line.startswith('%%BeginData'):
in_binary_section = True
byte_count = int(line.split()[1])
line += '\n'
temp_file.write(line)
line = ''
else:
bytes_read += 1
if bytes_read == byte_count:
in_binary_section = False
if bytes_read % 32 != 1:
temp_file.write('\n')
else:
temp_file.write('%02X' % x)
if bytes_read % 32 == 0:
temp_file.write('\n')
i += 1
return temp_path
def font_has_table(font_path, table_tag):
with TTFont(font_path) as font:
return table_tag in font
def get_font_revision(font_path):
with TTFont(font_path) as font:
return font['head'].fontRevision