-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimplify.py
More file actions
executable file
·169 lines (127 loc) · 4.12 KB
/
simplify.py
File metadata and controls
executable file
·169 lines (127 loc) · 4.12 KB
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
#!/usr/bin/env python3
#encoding=utf-8
import argparse
import math
from os.path import exists, normpath, basename
def simplify_glyph(myfont, args):
all_glyph_list = list(myfont.selection.byGlyphs)
print("Source font total glyph:", len(all_glyph_list))
skip_list = []
idx = 0
convert_count = 0
for glyph in all_glyph_list:
idx +=1
# altuni, cause glyph expand stroke more times.
if glyph.changed:
continue
unicode_int = glyph.unicode
if unicode_int in skip_list:
continue
if unicode_int <= 0:
continue
'''
glyph.simplify([error_bound, flags, tan_bounds, linefixup, linelenmax])
Tries to remove excess points in the glyph if doing so will not perturb the curve by more than error-bound. Flags is a tuple of the following strings
ignoreslopes
Allow slopes to change
ignoreextrema
Allow removal of extrema
smoothcurves
Allow curve smoothing
choosehv
Snap to horizontal or vertical
forcelines
flatten bumps on lines
nearlyhvlines
Make nearly horizontal/vertical lines be so
mergelines
Merge adjacent lines into one
setstarttoextremum
Rotate the point list so that the start point is on an extremum
removesingletonpoints
If the contour contains just one point then remove it
'''
glyph.simplify()
convert_count += 1
if convert_count % 100 == 0:
print("Processing index:%d (count:%d)" % (idx,convert_count))
print("Done, total:%d, convert count:%d" % (idx, convert_count))
return myfont
def simplify(args):
ff_path = args.input
out_path = args.output
# basic settings.
ff_path = args.input
if len(ff_path) > 0:
if ff_path[:1] == "/":
ff_path = ff_path[:-1]
if ff_path.endswith(".ttf"):
font_name = (basename(normpath(ff_path)))
project_path = font_name + ".sfdir"
if out_path is None:
out_path = project_path
export_as_font = False
if out_path is None:
if ff_path.endswith(".ttf") or ff_path.endswith(".woff") or ff_path.endswith(".woff2"):
export_as_font = True
else:
if out_path.endswith(".ttf") or out_path.endswith(".woff") or out_path.endswith(".woff2"):
export_as_font = True
print("Open font:", ff_path)
print("Save font:", out_path)
myfont=fontforge.open(ff_path)
myfont.selection.all()
# font config.
if not args.comment is None:
myfont.comment = args.comment
if not args.familyname is None:
myfont.familyname = args.familyname
if not args.fontname is None:
myfont.fontname = args.fontname
if not args.fullname is None:
myfont.fullname = args.fullname
if not args.macstyle is None:
myfont.macstyle = args.macstyle
if not args.error_bound is None:
myfont.simplify(args.error_bound, ('mergelines','ignoreslopes','setstarttoextremum'))
else:
myfont.simplify()
if export_as_font:
myfont.generate(out_path)
else:
if out_path is None:
myfont.save()
else:
myfont.save(out_path)
def cli():
parser = argparse.ArgumentParser(
description="Converts fonts using FontForge")
parser.add_argument("--input",
help="input font project or file",
required=True,
type=str)
parser.add_argument("--output",
help="output font project or file",
default="output.ttf",
type=str)
# font config.
parser.add_argument("--comment",
help="A comment associated with the font. Can be anything.",
type=str)
parser.add_argument("--familyname",
help="PostScript font family name",
type=str)
parser.add_argument("--fontname",
help="PostScript font name",
type=str)
parser.add_argument("--fullname",
help="PostScript font name",
type=str)
parser.add_argument("--macstyle",
help="Bold (if set to 1)",
type=int)
parser.add_argument('--error_bound', type=float, default=None)
args = parser.parse_args()
simplify(args)
if __name__ == "__main__":
cli()