forked from RichardLuo0/sarasa2yahei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
184 lines (160 loc) · 6.95 KB
/
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
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
import fontforge as ff
DOWNLOAD_DIR = '/tmp/fonts'
OUTPUT_DIR_SC = '/tmp/fonts/sc'
OUTPUT_DIR_TC = '/tmp/fonts/tc'
def open_font(path):
return ff.open(path)
def close_font(font):
font.close()
# def get_version(font):
# return font.version.split(';')[0]
Style = ('Regular', 'Italic', 'Bold', 'Bold Italic')
def set_font_info_sc(font, prefmy_zh, prefmy_en,
cprt_zh, cprt_en, subfmy,
weight='', weighthuman=''):
if len(weight)>0:
preferredstyles = (f'{weight} {Style[subfmy]}' if subfmy>0 else weight)
familyname_en = f'{prefmy_en} {weighthuman}'
familyname_zh = f'{prefmy_zh} {weighthuman}'
fullname_en = (f'{familyname_en} {Style[subfmy]}'
if subfmy>0 else familyname_en)
fullname_zh = (f'{familyname_zh} {Style[subfmy]}'
if subfmy>0 else familyname_zh)
uniqueid_en = f'{prefmy_en} {preferredstyles}'
uniqueid_zh = f'{prefmy_zh} {preferredstyles}'
else:
preferredstyles = Style[subfmy]
familyname_en = prefmy_en
familyname_zh = prefmy_zh
fullname_en = (f'{familyname_en} {Style[subfmy]}'
if subfmy>0 else familyname_en)
fullname_zh = (f'{familyname_zh} {Style[subfmy]}'
if subfmy>0 else familyname_zh)
uniqueid_en = f'{prefmy_en} {preferredstyles}'
uniqueid_zh = f'{prefmy_zh} {preferredstyles}'
font.fontname = prefmy_en.replace(' ','')+'-'+preferredstyles.replace(' ','')
font.familyname = familyname_en
font.fullname = fullname_en
# font.version = get_version(font)
font.copyright = cprt_en
font.sfnt_names = (
('English (US)', 'Copyright', cprt_en),
('English (US)', 'Family', familyname_en),
('English (US)', 'SubFamily', Style[subfmy]),
('English (US)', 'UniqueID', uniqueid_en),
('English (US)', 'Fullname', fullname_en),
('English (US)', 'Version', font.version),
('English (US)', 'PostScriptName', font.fontname),
('English (US)', 'Preferred Family', prefmy_en),
('English (US)', 'Preferred Styles', preferredstyles),
('Chinese (PRC)', 'Copyright', cprt_zh),
('Chinese (PRC)', 'Family', familyname_zh),
('Chinese (PRC)', 'SubFamily', Style[subfmy]),
('Chinese (PRC)', 'UniqueID', uniqueid_zh),
('Chinese (PRC)', 'Fullname', fullname_zh),
('Chinese (PRC)', 'Version', font.version),
('Chinese (PRC)', 'Preferred Family', prefmy_zh),
('Chinese (PRC)', 'Preferred Styles', preferredstyles)
)
def set_font_info_tc(font, prefmy_zh, prefmy_en,
cprt_zh, cprt_en, subfmy,
weight='', weighthuman=''):
if len(weight)>0:
preferredstyles = (f'{weight} {Style[subfmy]}' if subfmy>0 else weight)
familyname_en = f'{prefmy_en} {weighthuman}'
familyname_zh = f'{prefmy_zh} {weighthuman}'
fullname_en = (f'{familyname_en} {Style[subfmy]}'
if subfmy>0 else familyname_en)
fullname_zh = (f'{familyname_zh} {Style[subfmy]}'
if subfmy>0 else familyname_zh)
uniqueid_en = f'{prefmy_en} {preferredstyles}'
uniqueid_zh = f'{prefmy_zh} {preferredstyles}'
else:
preferredstyles = Style[subfmy]
familyname_en = prefmy_en
familyname_zh = prefmy_zh
fullname_en = (f'{familyname_en} {Style[subfmy]}'
if subfmy>0 else familyname_en)
fullname_zh = (f'{familyname_zh} {Style[subfmy]}'
if subfmy>0 else familyname_zh)
uniqueid_en = f'{prefmy_en} {preferredstyles}'
uniqueid_zh = f'{prefmy_zh} {preferredstyles}'
font.fontname = prefmy_en.replace(' ','')+'-'+preferredstyles.replace(' ','')
font.familyname = familyname_en
font.fullname = fullname_en
# font.version = get_version(font)
font.copyright = cprt_en
font.sfnt_names = (
('English (US)', 'Copyright', cprt_en),
('English (US)', 'Family', familyname_en),
('English (US)', 'SubFamily', Style[subfmy]),
('English (US)', 'UniqueID', uniqueid_en),
('English (US)', 'Fullname', fullname_en),
('English (US)', 'Version', font.version),
('English (US)', 'PostScriptName', font.fontname),
('English (US)', 'Preferred Family', prefmy_en),
('English (US)', 'Preferred Styles', preferredstyles),
('Chinese (Taiwan)', 'Copyright', cprt_zh),
('Chinese (Taiwan)', 'Family', familyname_zh),
('Chinese (Taiwan)', 'SubFamily', Style[subfmy]),
('Chinese (Taiwan)', 'UniqueID', uniqueid_zh),
('Chinese (Taiwan)', 'Fullname', fullname_zh),
('Chinese (Taiwan)', 'Version', font.version),
('Chinese (Taiwan)', 'Preferred Family', prefmy_zh),
('Chinese (Taiwan)', 'Preferred Styles', preferredstyles),
('Chinese (Hong Kong)', 'Copyright', cprt_zh),
('Chinese (Hong Kong)', 'Family', familyname_zh),
('Chinese (Hong Kong)', 'SubFamily', Style[subfmy]),
('Chinese (Hong Kong)', 'UniqueID', uniqueid_zh),
('Chinese (Hong Kong)', 'Fullname', fullname_zh),
('Chinese (Hong Kong)', 'Version', font.version),
('Chinese (Hong Kong)', 'Preferred Family', prefmy_zh),
('Chinese (Hong Kong)', 'Preferred Styles', preferredstyles)
)
def bytes2int(bytes):
return int.from_bytes(bytes, byteorder='big', signed=False)
def readXACW(ttffile):
fo = open(ttffile, 'rb')
HeadBytes = fo.read(224)
index = HeadBytes.find(b'OS/2') + 8
offset = bytes2int(HeadBytes[index:index+4])
fo.seek(offset + 2, 0)
XACW_Bytes = fo.read(2)
fo.close()
print(f'read {ttffile}')
XACW = bytes2int(XACW_Bytes)
print(f'xAvgCharWidth = {XACW}')
return XACW_Bytes
def patchXACW(ttcfile, XACW_Bytes1, XACW_Bytes2):
fo = open(ttcfile, 'rb+')
assert (fo.read(4) == b'ttcf'), '不是 ttc 文件'
fo.seek(8, 0)
assert (fo.read(4) == b'\x00\x00\x00\x02'), 'ttc 中字体数量不是 2'
OffsetTtfHead_Bytes1 = fo.read(4)
OffsetTtfHead_Bytes2 = fo.read(4)
fo.seek(bytes2int(OffsetTtfHead_Bytes1), 0)
HeadBytes1 = fo.read(224)
fo.seek(bytes2int(OffsetTtfHead_Bytes2), 0)
HeadBytes2 = fo.read(224)
index1 = HeadBytes1.find(b'OS/2') + 8
index2 = HeadBytes2.find(b'OS/2') + 8
offset1 = bytes2int(HeadBytes1[index1:index1+4])
offset2 = bytes2int(HeadBytes2[index2:index2+4])
fo.seek(offset1 + 2, 0)
oldXACW_Bytes1 = fo.read(2)
fo.seek(offset2 + 2, 0)
oldXACW_Bytes2 = fo.read(2)
fo.seek(offset1 + 2, 0)
fo.write(XACW_Bytes1)
fo.seek(offset2 + 2, 0)
fo.write(XACW_Bytes2)
fo.close()
print(f'patch {ttcfile}')
oldXACW1 = bytes2int(oldXACW_Bytes1)
oldXACW2 = bytes2int(oldXACW_Bytes2)
XACW1 = bytes2int(XACW_Bytes1)
XACW2 = bytes2int(XACW_Bytes2)
print('xAvgCharWidth Fixing:')
print(f'{oldXACW1} --> {XACW1}')
print(f'{oldXACW2} --> {XACW2}')
return