-
Notifications
You must be signed in to change notification settings - Fork 31
/
monospacifier.py
executable file
·313 lines (267 loc) · 12.3 KB
/
monospacifier.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=too-few-public-methods
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# $ fc-list | grep -i symbola
# /usr/share/fonts/truetype/ttf-ancient-scripts/Symbola605.ttf: Symbola:style=Regular
"""Convert a variable-width font to monospace, optionally merging it with an
existing monospace font to extend its Unicode coverage."""
# Technical note: two cycles of (open, modify, save, close) cause a segfault.
# On the other hand, copying a font and reading, modifying, and overwriting
# the copy works.
from __future__ import division
import argparse
from collections import Counter
import math
import os
import re
import shutil
import unicodedata
import sys
try:
import fontforge
import psMat
except ImportError:
print("This program requires FontForge's python bindings:")
print(" git clone https://github.com/fontforge/fontforge")
print(" cd fontforge")
print(" mkdir build; cd build")
print(" cmake -GNinja .. -Wno-dev")
print(" ninja")
print(" sudo ninja install")
print(" sudo ldconfig")
raise
class GlyphScaler:
def __init__(self, cell_width):
self.cell_width = cell_width
@staticmethod
def needs_scaling(glyph):
uni = glyph.unicode
category = unicodedata.category(chr(uni)) if 0 <= uni <= sys.maxunicode else None
return glyph.width > 0 and category not in ['Mn', 'Mc', 'Me']
@staticmethod
def set_width(glyph, width):
delta = width - glyph.width
glyph.left_side_bearing += delta / 2
glyph.right_side_bearing += delta - glyph.left_side_bearing
glyph.width = width
class BasicGlyphScaler(GlyphScaler):
"""
A GlyphScaler that adjust glyph bounding boxes so that their widths are
all equal to the base cell width.
"""
def __init__(self, cell_width):
GlyphScaler.__init__(self, cell_width)
def scale(self, glyph):
if self.needs_scaling(glyph):
GlyphScaler.set_width(glyph, self.cell_width)
class AllowWideCharsGlyphScaler(GlyphScaler):
"""
A GlyphScaler that adjusts glyph bounding boxes so that their widths are
multiples of the base cell width. Which multiple is chosen depends on the
width of each glyph, compared to the given average width.
"""
def __init__(self, cell_width, avg_width):
"""Construct an instance based on the target CELL_WIDTH and the source AVG_WIDTH."""
GlyphScaler.__init__(self, cell_width)
self.avg_width = avg_width
def scale(self, glyph):
if self.needs_scaling(glyph):
new_width_in_cells = int(math.ceil(0.75 * glyph.width / self.avg_width))
# if new_width_in_cells > 1:
# print("{} is {} cells wide ({} -> {})".format(
# glyph.glyphname, new_width_in_cells, self.cell_width, glyph.width))
GlyphScaler.set_width(glyph, new_width_in_cells * self.cell_width)
class StretchingGlyphScaler(GlyphScaler):
"""
A GlyphScaler that adjusts glyph bounding boxes so that their widths are all
equal to the base cell width. Unlike the basic scaler, this one also scales
the glyphs themselves horizontally by a small amount, in proportion of their
distance to the average glyph width.
"""
def __init__(self, cell_width, avg_width):
"""Construct an instance based on the target CELL_WIDTH and the source AVG_WIDTH."""
GlyphScaler.__init__(self, cell_width)
self.avg_width = avg_width
def scale(self, glyph):
if self.needs_scaling(glyph):
source_cells_width = glyph.width / self.avg_width
scale = 1.0 / (1.15 ** max(0, source_cells_width - 1))
# if glyph.unicode == 10239:
# print("\n\n====\n" + "\n".join("{}: {}".format(attr, str(getattr(glyph, attr))) for attr in dir(glyph)))
matrix = psMat.scale(scale, 1)
glyph.transform(matrix)
GlyphScaler.set_width(glyph, self.cell_width)
class FontScaler:
METRICS = ["ascent", "descent", "hhea_ascent", "hhea_ascent_add",
"hhea_descent", "hhea_descent_add", "hhea_linegap", "os2_capheight",
"os2_strikeypos", "os2_strikeysize", "os2_subxoff", "os2_subxsize",
"os2_subyoff", "os2_subysize", "os2_supxoff", "os2_supxsize", "os2_supyoff",
"os2_supysize", "os2_typoascent", "os2_typoascent_add", "os2_typodescent",
"os2_typodescent_add", "os2_typolinegap", "os2_width", "os2_winascent",
"os2_winascent_add", "os2_windescent", "os2_windescent_add", "os2_xheight",
"vhea_linegap"]
def __init__(self, path):
self.font = fontforge.open(path) # Prints a few warnings
self.renamed = False
@staticmethod
def average_width(font):
"""
Compute the average character width in FONT.
Useful to compare a character to others in a font.
"""
return int(1 + sum(g.width for g in font.glyphs()) / sum(1 for _ in font.glyphs()))
@staticmethod
def median_width(font):
"""
Compute the median character width in FONT.
Useful to compare a character to others in a font.
"""
widths = sorted(g.width for g in font.glyphs())
return int(widths[len(widths) // 2])
@staticmethod
def most_common_width(font):
"""
Find out the most common character width in FONT.
Useful to determine the width of a monospace font.
"""
[(width, _)] = Counter(g.width for g in font.glyphs()).most_common(1) # pylint: disable=unbalanced-tuple-unpacking
return width
def scale_glyphs(self, scaler):
"""
Adjust width of glyphs in using SCALER.
"""
# counter = Counter()
for glyph in self.font.glyphs():
scaler.scale(glyph)
# counter[glyph.width] += 1
# print("> Final width distribution: {}".format(", ".join(map(str, counter.most_common(10)))))
def copy_metrics(self, reference):
for metric in FontScaler.METRICS:
if hasattr(reference, metric):
setattr(self.font, metric, getattr(reference, metric))
def write(self, name):
"""
Save font to NAME.
"""
self.font.generate(name)
def plot_widths(glyphs):
# pylint: disable=unused-variable
import matplotlib # Putting imports in this order prevents a circular import
import matplotlib.cbook
from matplotlib import pyplot
widths = [glyph.width for glyph in glyphs]
pyplot.hist(widths, bins=400)
pyplot.show()
def fname(path):
return os.path.splitext(os.path.basename(path))[0]
def cleanup_font_name(name, renames):
name = re.sub('(.monospacified.for.*|-Regular|-Math)', '', name)
for old, new in renames:
name = name.replace(old, new)
return name
def make_monospace(reference, fallback, gscaler, save_to, copy_metrics, renames):
fontname = "{}_monospacified_for_{}".format(
cleanup_font_name(fallback.fontname, renames),
cleanup_font_name(reference.fontname, renames))
familyname = "{} monospacified for {}".format(
cleanup_font_name(fallback.familyname, renames),
cleanup_font_name(reference.familyname, renames))
fullname = "{} monospacified for {}".format(
cleanup_font_name(fallback.fullname, renames),
cleanup_font_name(reference.fullname, renames))
print("!!! {} !!! {} !!!".format(fallback.fontname, reference.fontname))
destination = os.path.join(save_to, fontname + ".ttf")
shutil.copy(fallback.path, destination)
fscaler = FontScaler(destination)
fscaler.font.sfnt_names = [] # Get rid of 'Prefered Name' etc.
fscaler.font.fontname = fontname
fscaler.font.familyname = familyname
fscaler.font.fullname = fullname
fscaler.font.em = reference.em # Adjust em size (number of internal units per em)
fscaler.scale_glyphs(gscaler)
if copy_metrics:
fscaler.copy_metrics(reference)
fscaler.write(destination)
return destination
def merge_fonts(reference, fallback, save_to, renames):
fontname = "{}_extended_with_{}".format(
cleanup_font_name(reference.fontname, renames),
cleanup_font_name(fallback.fontname, renames))
familyname = "{} extended with {}".format(
cleanup_font_name(reference.familyname, renames),
cleanup_font_name(fallback.familyname, renames))
fullname = "{} extended with {}".format(
cleanup_font_name(reference.fullname, renames),
cleanup_font_name(fallback.fullname, renames))
destination = os.path.join(save_to, fontname + ".ttf")
shutil.copy(reference.path, destination)
merged = fontforge.open(destination)
merged.sfnt_names = []
merged.fontname = fontname
merged.familyname = familyname
merged.fullname = fullname
merged.mergeFonts(fallback.path)
merged.generate(destination)
return destination
def parse_arguments():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--references', required=True, nargs='+',
help="Reference monospace fonts. " +
"The metrics (character width, ...) of the newly created monospace fonts are inherited from these.")
parser.add_argument('--inputs', required=True, nargs='+',
help="Variable-width fonts to monospacify.")
parser.add_argument('--save-to', default=".",
help="Where to save the newly generated monospace fonts. Defaults to current directory.")
parser.add_argument('--merge', action='store_const', const=True, default=False,
help="Whether to create copies of the reference font, extended with monospacified glyphs of the inputs.")
parser.add_argument('--copy-metrics', action='store_const', const=True, default=False,
help="Whether to apply the metrics of the reference font to the new font.")
parser.add_argument('--rename', nargs=2, metavar=("FROM", "TO"), default=[], action="append",
help="Replacement to perform in font names (repeat to perform multiple renames)")
return parser.parse_args()
def process_fonts(ref_paths, fnt_paths, save_to, merge, copy_metrics, renames):
for ref in ref_paths:
reference = fontforge.open(ref)
ref_width = FontScaler.most_common_width(reference)
print(">>> For reference font {}:".format(reference.familyname))
for fnt in fnt_paths:
fallback = fontforge.open(fnt)
print(">>> - Monospacifying {}".format(fallback.familyname))
gscaler = StretchingGlyphScaler(ref_width, FontScaler.average_width(fallback))
path = make_monospace(reference, fallback, gscaler, save_to, copy_metrics, renames)
if merge:
monospacified = fontforge.open(path)
print(">>> - Merging with {}".format(monospacified.familyname))
path = merge_fonts(reference, monospacified, save_to, renames)
yield (reference.familyname, fallback.familyname, path)
def main():
args = parse_arguments()
results = list(process_fonts(args.references, args.inputs,
args.save_to, args.merge,
args.copy_metrics, args.rename))
tabdata = {}
for ref, fnt, ttf in results:
tabdata.setdefault(u"**{}**".format(ref), []).append(u"[{}]({}?raw=true)".format(fnt, ttf))
table = [(header, u", ".join(items)) for header, items in sorted(tabdata.items())]
try:
from tabulate import tabulate
print(tabulate(table, headers=[u'Programming font', u'Monospacified fallback fonts'], tablefmt='pipe'))
except ImportError:
print("!!! tabulate package not available")
if __name__ == '__main__':
main()
# Local Variables:
# python-shell-interpreter: "python3"
# End: