This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
forked from ToxicFrog/Ligaturizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ligaturize.py
363 lines (300 loc) · 14.6 KB
/
ligaturize.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python
#
# usage: fontforge -lang=py ligaturize.py <input file> <output file> [ligature file]
#
# It will copy input to output, updating the embedded font name and splicing
# in the ligatures from FiraCode-Medium.otf (which must be in $PWD). If the
# ligature file is not specified, it will try to guess an appropriate Fira Code
# OTF based on the name of the output file.
#
# See ligatures.py for a list of all the ligatures that will be copied.
import fontforge
import psMat
import os
from os import path, replace
import sys
from ligatures import ligatures
from char_dict import char_dict
# Constants
COPYRIGHT = '''
Programming ligatures added by Ilya Skriblovsky from FiraCode
FiraCode Copyright (c) 2015 by Nikita Prokopov'''
def get_ligature_source(fontname):
# Become case-insensitive
fontname = fontname.lower()
for weight in ['Bold', 'Retina', 'Medium', 'Regular', 'Light']:
if fontname.endswith('-' + weight.lower()):
# Exact match for one of the Fira Code weights
return 'modules/fira/distr/otf/FiraCode-%s.otf' % weight
# No exact match. Guess that we want 'Bold' if the font name has 'bold' or
# 'heavy' in it, and 'Regular' otherwise.
if 'bold' in fontname or 'heavy' in fontname:
return 'modules/fira/distr/otf/FiraCode-Bold.otf'
return 'modules/fira/distr/otf/FiraCode-Regular.otf'
class LigatureCreator(object):
def __init__(self, font, firacode, scale_character_glyphs_threshold,
copy_character_glyphs):
self.font = font
self.firacode = firacode
self.scale_character_glyphs_threshold = scale_character_glyphs_threshold
self.should_copy_character_glyphs = copy_character_glyphs
self._lig_counter = 0
# Scale firacode to correct em height.
self.firacode.em = self.font.em
self.emwidth = self.font[ord('m')].width
def copy_ligature_from_source(self, ligature_name):
try:
self.firacode.selection.none()
self.firacode.selection.select(ligature_name)
self.firacode.copy()
return True
except ValueError:
return False
def correct_character_width(self, glyph):
"""Width-correct copied individual characters (not ligatures!).
This will correct the horizontal advance of characters to match the em
width of the output font, and (depending on the width of the glyph, the
em width of the output font, and the value of the command line option
--scale-character-glyphs-threshold) optionally horizontally scale it.
Glyphs that are not horizontally scaled, but which still need horizontal
advance correction, will be centered instead.
"""
if glyph.width == self.emwidth:
# No correction needed.
return
widthdelta = float(abs(glyph.width - self.emwidth)) / self.emwidth
if widthdelta >= self.scale_character_glyphs_threshold:
# Character is too wide/narrow compared to output font; scale it.
scale = float(self.emwidth) / glyph.width
glyph.transform(psMat.scale(scale, 1.0))
else:
# Do not scale; just center copied characters in their hbox.
# Fix horizontal advance first, to recalculate the bearings.
glyph.width = self.emwidth
# Correct bearings to center the glyph.
glyph.left_side_bearing = (glyph.left_side_bearing +
glyph.right_side_bearing) / 2
glyph.right_side_bearing = glyph.left_side_bearing
# Final adjustment of horizontal advance to correct for rounding
# errors when scaling/centering -- otherwise small errors can result
# in visible misalignment near the end of long lines.
glyph.width = self.emwidth
def copy_character_glyphs(self, chars):
"""Copy individual (non-ligature) characters from the ligature font."""
if not self.should_copy_character_glyphs:
return
print(" ...copying %d character glyphs..." % (len(chars)))
for char in chars:
self.firacode.selection.none()
self.firacode.selection.select(char)
self.firacode.copy()
self.font.selection.none()
self.font.selection.select(char)
self.font.paste()
self.correct_character_width(self.font[ord(char_dict[char])])
def correct_ligature_width(self, glyph):
"""Correct the horizontal advance and scale of a ligature."""
if glyph.width == self.emwidth:
return
# TODO: some kind of threshold here, similar to the character glyph
# scale threshold? The largest ligature uses 0.956 of its hbox, so if
# the target font is within 4% of the source font size, we don't need to
# resize -- but we may want to adjust the bearings. And we can't just
# center it, because ligatures are characterized by very large negative
# left bearings -- they advance 1em, but draw from (-(n-1))em to +1em.
scale = float(self.emwidth) / glyph.width
glyph.transform(psMat.scale(scale, 1.0))
glyph.width = self.emwidth
def add_ligature(self, input_chars, firacode_ligature_name):
if firacode_ligature_name is None:
# No ligature name -- we're just copying a bunch of individual characters.
self.copy_character_glyphs(input_chars)
return
if not self.copy_ligature_from_source(firacode_ligature_name):
# Ligature not in source font.
return
self._lig_counter += 1
ligature_name = 'lig.{}'.format(self._lig_counter)
self.font.createChar(-1, ligature_name)
self.font.selection.none()
self.font.selection.select(ligature_name)
self.font.paste()
self.correct_ligature_width(self.font[ligature_name])
self.font.selection.none()
self.font.selection.select('space')
self.font.copy()
lookup_name = lambda i: 'lookup.{}.{}'.format(self._lig_counter, i)
lookup_sub_name = lambda i: 'lookup.sub.{}.{}'.format(
self._lig_counter, i)
cr_name = lambda i: 'CR.{}.{}'.format(self._lig_counter, i)
for i, char in enumerate(input_chars):
self.font.addLookup(lookup_name(i), 'gsub_single', (), ())
self.font.addLookupSubtable(lookup_name(i), lookup_sub_name(i))
if char not in self.font:
# We assume here that this is because char is a single letter
# (e.g. 'w') rather than a character name, and the font we're
# editing doesn't have glyphnames for letters.
self.font[ord(char_dict[char])].glyphname = char
if i < len(input_chars) - 1:
self.font.createChar(-1, cr_name(i))
self.font.selection.none()
self.font.selection.select(cr_name(i))
self.font.paste()
self.font[char].addPosSub(lookup_sub_name(i), cr_name(i))
else:
self.font[char].addPosSub(lookup_sub_name(i), ligature_name)
calt_lookup_name = 'calt.{}'.format(self._lig_counter)
self.font.addLookup(
calt_lookup_name, 'gsub_contextchain', (),
(('calt',
(('DFLT', ('dflt', )), ('arab', ('dflt', )),
('armn', ('dflt', )), ('cyrl', ('SRB ', 'dflt')), ('geor',
('dflt', )),
('grek', ('dflt', )), ('lao ', ('dflt', )),
('latn', ('CAT ', 'ESP ', 'GAL ', 'ISM ', 'KSM ', 'LSM ',
'MOL ', 'NSM ', 'ROM ', 'SKS ', 'SSM ', 'dflt')),
('math', ('dflt', )), ('thai', ('dflt', )))), ))
#print('CALT %s (%s)' % (calt_lookup_name, firacode_ligature_name))
for i, char in enumerate(input_chars):
self.add_calt(calt_lookup_name,
'calt.{}.{}'.format(self._lig_counter, i),
'{prev} | {cur} @<{lookup}> | {next}',
prev=' '.join(cr_name(j) for j in range(i)),
cur=char,
lookup=lookup_name(i),
next=' '.join(input_chars[i + 1:]))
# Add ignore rules
self.add_calt(calt_lookup_name,
'calt.{}.{}'.format(self._lig_counter, i + 1),
'| {first} | {rest} {last}',
first=input_chars[0],
rest=' '.join(input_chars[1:]),
last=input_chars[-1])
self.add_calt(calt_lookup_name,
'calt.{}.{}'.format(self._lig_counter, i + 2),
'{first} | {first} | {rest}',
first=input_chars[0],
rest=' '.join(input_chars[1:]))
def add_calt(self, calt_name, subtable_name, spec, **kwargs):
spec = spec.format(**kwargs)
#print(' %s: %s ' % (subtable_name, spec))
self.font.addContextualSubtable(calt_name, subtable_name, 'glyph',
spec)
def replace_sfnt(font, key, value):
font.sfnt_names = tuple((row[0], key, value) if row[1] == key else row
for row in font.sfnt_names)
def ligaturize_font(in_file_path, out_dir):
font = fontforge.open(in_file_path)
liga_font_file = get_ligature_source(font.fontname)
liga_font = fontforge.open(liga_font_file)
out_file_path = update_font_names(font, in_file_path, out_dir)
print("~> Ligaturizing font: %s" % font)
update_font_metadata(font)
apply_font_ligatures(liga_font, font)
print("~> Saving font to: %s" % out_file_path)
font.upos += font.uwidth # BUGFIX
font.generate(out_file_path)
print("\t~> Saved.\n")
def update_font_names(font, in_file_path, out_dir):
print("\n~> Updating font names...")
base_file_name = path.basename(in_file_path)
out_file_name = "Liga-%s" % base_file_name
out_file_path = path.join(out_dir, out_file_name)
print("\t~> Input font:")
print("\t\tin_file_path: %s" % in_file_path)
print("\t\tbase_file_name: %s" % base_file_name)
print("\t\tfont.fontname: %s" % font.fontname)
print("\t\tfont.fullname: %s" % font.fullname)
print("\t\tfont.familyname: %s" % font.familyname)
# print("\t\tfont.copyright: %s" % font.copyright)
font.fullname = "Liga %s" % font.fullname
font.fontname = "Liga-%s" % font.fontname
font.familyname = "Liga %s" % font.familyname
font.copyright += COPYRIGHT
print("\t~> Output font:")
print("\t\tout_file_path: %s" % out_file_path)
print("\t\tout_file_name: %s" % out_file_name)
print("\t\tfont.fontname: %s" % font.fontname)
print("\t\tfont.fullname: %s" % font.fullname)
print("\t\tfont.familyname: %s" % font.familyname)
# print("\t\tfont.copyright: %s" % font.copyright)
return out_file_path
def update_font_metadata(font):
print("\n~> Updating font metadata...")
replace_sfnt(font, "Copyright", font.copyright)
replace_sfnt(font, "Family", font.familyname)
# replace_sfnt(font, "Styles (SubFamily)", font.styles_subfamily)
replace_sfnt(font, "UniqueID", "%s; Ligaturized" % font.fullname)
replace_sfnt(font, "Fullname", font.fullname)
# replace_sfnt(font, "Version", font.version)
# replace_sfnt(font, "Trademark", font.trademark)
# replace_sfnt(font, "Manufacturer", font.manufacturer)
# replace_sfnt(font, "Designer", font.designer)
# replace_sfnt(font, "Descriptor", font.descriptor)
# replace_sfnt(font, "Vendor URL", font.vendor_url)
# replace_sfnt(font, "Designer URL", font.designer_url)
# replace_sfnt(font, "License", font.license)
# replace_sfnt(font, "License URL", font.license_url)
replace_sfnt(font, "Preferred Family", font.familyname)
# replace_sfnt(font, "Preferred Styles", font.preferred_styles)
replace_sfnt(font, "Compatible Full", font.familyname)
replace_sfnt(font, "WWS Family", font.familyname)
replace_sfnt(font, "WWS Subfamily", font.fullname)
def apply_font_ligatures(liga_font, font):
print("~> Using ligatures from: %s" % liga_font)
copy_copy_character_glyphs = False
scale_character_glyphs_threshold = 0.1
creator = LigatureCreator(font, liga_font,
scale_character_glyphs_threshold,
copy_copy_character_glyphs)
ligature_length = lambda lig: len(lig['chars'])
for lig_spec in sorted(ligatures, key=ligature_length):
try:
creator.add_ligature(lig_spec['chars'],
lig_spec['firacode_ligature_name'])
except Exception as e:
print('Exception while adding ligature: {}'.format(lig_spec))
raise
def parse_args():
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("in_file_path",
help="The TTF or OTF font to add ligatures to.")
parser.add_argument(
"--out-dir",
help="The directory to save the ligaturized font in. The actual filename"
" will be automatically generated based on the input font name and"
" the --prefix and --output-name flags.")
# parser.add_argument(
# "--ligature-font-file",
# type=str,
# default='',
# metavar='PATH',
# help="The file to copy ligatures from. If unspecified, ligaturize will"
# " attempt to pick a suitable one from fonts/fira/distr/otf/ based on the input"
# " font's weight.")
# parser.add_argument(
# "--copy-character-glyphs",
# default=False,
# action='store_true',
# help="Copy glyphs for (some) individual characters from the ligature"
# " font as well. This will result in punctuation that matches the"
# " ligatures more closely, but may not fit in as well with the rest"
# " of the font.")
# parser.add_argument(
# "--scale-character-glyphs-threshold",
# type=float,
# default=0.1,
# metavar='THRESHOLD',
# help="When copying character glyphs, if they differ in width from the"
# " width of the input font by at least this much, scale them"
# " horizontally to match the input font even if this noticeably"
# " changes their aspect ratio. The default (0.1) means to scale if"
# " they are at least 10%% wider or narrower. A value of 0 will scale"
# " all copied character glyphs; a value of 2 effectively disables"
# " character glyph scaling.")
return parser.parse_args()
def main():
ligaturize_font(**vars(parse_args()))
if __name__ == '__main__':
main()