-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_strokes.py
122 lines (104 loc) · 4.74 KB
/
import_strokes.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
import os
import fontforge
with open(r'agl-aglfn/aglfn.txt', 'r') as f:
lines = f.read().split('\n')
valid = filter(lambda line: line != '' and line[0] != '#', lines)
entries = map(lambda line: line.split(';'), valid)
glyphtable: dict = {name: int(code, 16) for code, glyph, name in entries}
os.mkdir(r'build')
weights: list = [
(40, 'light'),
(80, 'regular'),
(120, 'bold') ]
for form in os.listdir(r'src'):
for weight, weightname in weights:
print(f'populating build/{weightname}-{form}')
os.mkdir(f'build/{weightname}-{form}')
for filename in os.listdir(f'src/{form}'):
infile: str = f'src/{form}/{filename}'
outfile: str = f'build/{weightname}-{form}/{filename}'
with open(infile, 'r') as instroke:
contents: str = instroke.read()
contents = contents.replace('stroke-width:120', f'stroke-width:{weight + 40}')
contents = contents.replace('stroke-width:70', f'stroke-width:{weight}')
with open(outfile, 'w') as outstroke:
outstroke.write(contents)
variations: list = [
('light-normal', 'Light'),
('regular-normal', 'Regular'),
('bold-normal', 'Bold'),
('light-italic', 'Light Italic'),
('regular-italic', 'Italic'),
('bold-italic', 'Bold Italic')
]
for dirname, name in variations:
createdtbl: dict = {}
weight, form = dirname.split('-')
font = fontforge.open(r'emptytemplate.sfd')
print(f'importing build/{dirname}')
for filename in os.listdir(f'build/{dirname}'):
print(f'importing build/{dirname}/{filename}')
unicode_name: str = filename.split('.')[0]
codepoint: int = glyphtable[unicode_name]
glyph = font.createChar(codepoint)
createdtbl.update({glyph.glyphname: glyph})
glyph.importOutlines(f'build/{dirname}/{filename}')
glyph.width = 500
glyph.removeOverlap()
glyph.simplify(1)
glyph.addExtrema("all")
font.addLookup('ligatures', 'gsub_ligature', None, (("liga",(("DFLT",("dflt")),("latn",("dflt")),)),))
font.addLookupSubtable('ligatures', 'ligatures-1')
for filename in os.listdir(f'build/{dirname}-liga'):
print(f'importing build/{dirname}-liga/{filename}')
names: list = filename.split('.')[0].split('_')
liganame = names.pop(0)
# liganame += '.ligature'
glyph = font.createChar(-1, liganame)
glyph.glyphclass = 'baseligature'
glyph.glyphname = liganame + '.ligature'
glyph.addPosSub('ligatures-1', names)
createdtbl.update({glyph.glyphname: glyph})
glyph.importOutlines(f'build/{dirname}-liga/{filename}')
glyph.width = 500 * len(names)
glyph.removeOverlap()
glyph.simplify(1)
glyph.addExtrema("all")
for filename in os.listdir(f'build/{dirname}-cont'):
print(f'importing build/{dirname}-cont/{filename}')
contname = filename.replace('.svg', '')
glyph = font.createChar(-1, contname)
glyph.glyphname = contname
createdtbl.update({glyph.glyphname: glyph})
glyph.importOutlines(f'build/{dirname}-cont/{filename}')
glyph.width = 500
glyph.removeOverlap()
glyph.simplify(1)
glyph.addExtrema("all")
font.addLookup('caltrules', 'gsub_contextchain', None, (("calt",(("DFLT",("dflt")),("latn",("dflt")),)),))
with open('caltrules.txt', 'r') as crfile:
crlines = crfile.read().split('\n')
for crline in filter(lambda line: line != '', crlines):
contname, prenames, rule = crline.split(':')
font.addLookup(contname, 'gsub_single', None, (("aalt",(("DFLT",("dflt")),("latn",("dflt")),)),))
font.addLookupSubtable(contname, contname + '-1')
for name in prenames.split(' '):
preglyph = createdtbl[name]
preglyph.addPosSub(contname + '-1', contname)
rule = rule.replace('!', f'[{prenames}] @<{contname}>')
font.addContextualSubtable('caltrules', contname, 'coverage', rule)
font.removeOverlap()
font.correctDirection()
font.fontname = f'honchokomono-{dirname}'
font.fullname = f'honchokomono {name}'
font.familyname = f'honchokomono-{form}'
font.weight = weight
font.os2_weight = {'light': 300, 'regular': 400, 'bold': 700}[weight]
font.appendSFNTName('English (US)', 'Preferred Styles', f'{name}')
font.appendSFNTName('English (US)', 'WWS Family', f'honchokomono')
font.appendSFNTName('English (US)', 'WWS Subfamily', f'{name}')
if form == 'italic':
font.italicangle = -12
font.save(f'build/honchokomono_{dirname}.sfd')
font.generate(f'build/honchokomono-{dirname}.otf')
font.close()