-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compile.py
206 lines (158 loc) · 5.76 KB
/
Compile.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
"""
todo:
- interface
- multi accents
"""
from glyphConstruction import ParseGlyphConstructionListFromString, GlyphConstructionBuilder
import imp
import os
font = f = CurrentFont()
smallcapsSuffix = ".sc" # not needed anymore 20201210
capThreshold = 600
def reloadConstruction():
"""
throw away custom Constructions and load default
"""
path = "/Users/thom/Library/Application Support/RoboFont/scripts/*DiacriticsWorkflow/_glyphContructionsRF3.py"
bc = imp.load_source('txt', path)
asList = ParseGlyphConstructionListFromString(bc.basics)
asDict = []
for i in asList:
if i.find("+") != i.rfind("+"):
continue
compo = i.split("=")[0][:-1]
base = i.split("=")[1][1:].split("+")[0].strip()
accent = i.split("=")[1][1:].split("+")[1].strip().split("@")[0]
anchor = i.split("=")[1][1:].split("+")[1].strip().split("@")[1]
# for now only one accent@anchor pair!
construction = i.split("+")[1].strip()
asDict.append(dict(
active=True,
compo=compo,
base=base,
accent=accent,
anchor=anchor,
construction=construction,
))
f.lib['nl.hallotype.glyphConstructions'] = asDict
def doGlyph(construction):
pass
def processGlyph(font, comp, base, accent, anchor):
"""
This function compiles the glyph,
if baseGlyph or accent not in the font it wont compile
if anchors are missing it wont compile
big chunk came from https://robofont.com/documentation/how-tos/building-accented-glyphs-with-a-script/
"""
if base not in font.keys():
return "Not found: " + base
inBase, height = anchorInGlyph(anchor, font[base])
if inBase and height > capThreshold:
if accent+".cap" in font.keys():
accent = accent+".cap"
if accent not in font.keys():
return "Not found: " + accent
inAccent, height = anchorInGlyph(anchor, font[accent])
if not inBase:
return "Not found: %s in %s" % (anchor, base)
if not inAccent:
return "Not found: %s in %s" % (anchor, accent)
construction = "%s = %s + %s@%s" % (comp, base, accent, anchor)
# build a construction glyph
constructionGlyph = GlyphConstructionBuilder(construction, font)
# if the construction for this glyph was preceded by `?`
# and the glyph already exists in the font, skip it
# if constructionGlyph.name in font and constructionGlyph.name in ignoreExisting:
# return
# get the destination glyph in the font
glyph = font.newGlyph(constructionGlyph.name, clear=True)
# draw the construction glyph into the destination glyph
constructionGlyph.draw(glyph.getPen())
# copy construction glyph attributes to the destination glyph
glyph.name = constructionGlyph.name
glyph.unicode = constructionGlyph.unicode
glyph.width = constructionGlyph.width
glyph.markColor = 1, 1, 0, 0.5
# if no unicode was given, try to set it automatically
if glyph.unicode is None:
glyph.autoUnicodes()
def isCap(glyphname):
return(glyphname[0] == glyphname[0].upper())
def anchorInGlyph(anchor, glyph):
for a in glyph.anchors:
if a.name == anchor:
return (True, a.y)
if a.name == "_"+anchor:
return (True, a.y)
return (False, None)
log = []
# define glyph constructions
test = '''\
agrave = a + grave@top
aacute = a + acute@top
iogonek = i + ogonek@ogonek
Iacute = I+acute@top+acute@top
bdkjn = re+acute@top
'''
batchConstructions = []
# try to load font constructions
if 'nl.hallotype.glyphConstructions' in f.lib:
batchConstructions = list(f.lib['nl.hallotype.glyphConstructions']) # copy of list otherwise the lowers get added!
# else:
# # load extrenal contructions
# reloadConstruction()
# batchConstructions = f.lib['nl.hallotype.glyphConstructions']
compos = []
for i in batchConstructions:
compos.append(i['compo'])
# add lowercase
for i in batchConstructions:
if i['base'][0] == i['base'][0].upper():
if i['compo'].lower() not in compos:
j = dict(i)
j['base'] = j['base'].lower()
j['compo'] = j['compo'].lower()
batchConstructions.append(j)
# collect all baseGlyphs
baseGlyphs = set()
for i in batchConstructions:
baseGlyphs.add(i['base'])
# search for baseGlyphs.suffix in font
foundSuffixedGlyphNames = {}
for bgn in baseGlyphs:
for gn in f.keys():
if gn.startswith(bgn+"."):
if bgn in foundSuffixedGlyphNames:
foundSuffixedGlyphNames[bgn].append(gn)
else:
foundSuffixedGlyphNames[bgn] = [gn]
for construction in batchConstructions:
if construction['active'] == True:
bases = [construction['base']]
if construction['base'] in foundSuffixedGlyphNames:
bases += foundSuffixedGlyphNames[construction['base']]
# print(bases)
for base in bases:
suffix = ""
if "." in base and "." not in construction['compo']:
suffix = "."+".".join(base.split(".")[1:])
# print(suffix)
r = processGlyph(font,
construction['compo']+suffix,
base,
construction['accent'],
construction['anchor']
)
if r and r not in log:
log.append(r)
# # # smallcaps
# if isCap(construction['base']):
# r = processGlyph(font,
# construction['compo']+smallcapsSuffix,
# construction['base']+smallcapsSuffix,
# construction['accent'],
# construction['anchor']
# )
# if r and r not in log:
# log.append(r)
for i in log: print(i)