summaryrefslogtreecommitdiff
path: root/scripts/lib/fontbuild/mix.py
blob: 86bf4edc8cde1264a9121b68b420b3be6ffba652 (plain)
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
from FL import *
from numpy import array, append
import copy

class FFont:
    "Font wrapper for floating point operations"
    
    def __init__(self,f=None):
        self.glyphs = {}
        self.hstems = []
        self.vstems = []
        if isinstance(f,FFont):
            #self.glyphs = [g.copy() for g in f.glyphs]
            for key,g in f.glyphs.iteritems():
                self.glyphs[key] = g.copy()
            self.hstems = list(f.hstems)
            self.vstems = list(f.vstems)
        elif f != None:
            self.copyFromFont(f)
        
    def copyFromFont(self,f):
        for g in f.glyphs:
            self.glyphs[g.name] = FGlyph(g)
        self.hstems = [s for s in f.stem_snap_h[0]]
        self.vstems = [s for s in f.stem_snap_v[0]]
    
            
    def copyToFont(self,f):
        for g in f.glyphs:
            try:
                gF = self.glyphs[g.name]
                gF.copyToGlyph(g)
            except:
                print "Copy to glyph failed for" + g.name
        f.stem_snap_h[0] = self.hstems
        f.stem_snap_v[0] = self.vstems
        
    def getGlyph(self, gname):
        try:
            return self.glyphs[gname]
        except:
            return None
        
    def setGlyph(self, gname, glyph):
        self.glyphs[gname] = glyph
    
    def addDiff(self,b,c):
        newFont = FFont(self)
        for key,g in newFont.glyphs.iteritems():
            gB = b.getGlyph(key)
            gC = c.getGlyph(key)
            try:
                newFont.glyphs[key] = g.addDiff(gB,gC)
            except:
                print "Add diff failed for '%s'" %key
        return newFont

class FGlyph:
    "provides a temporary floating point compatible glyph data structure"
    
    def __init__(self, g=None):
        self.nodes = []
        self.width = 0.
        self.components = []
        self.kerning = []
        self.anchors = []
        if g != None:
            self.copyFromGlyph(g)
        
    def copyFromGlyph(self,g):
        self.name = g.name
        valuesX = []
        valuesY = []
        self.width = len(valuesX)
        valuesX.append(g.width)
        for c in g.components:
            self.components.append((len(valuesX),len(valuesY)))
            valuesX.append(c.scale.x)
            valuesY.append(c.scale.y)
            valuesX.append(c.delta.x)
            valuesY.append(c.delta.y)
        
        for a in g.anchors:
            self.anchors.append((len(valuesX), len(valuesY)))
            valuesX.append(a.x)
            valuesY.append(a.y)
                
        for i in range(len(g.nodes)):
            self.nodes.append([])
            for j in range (len(g.nodes[i])):
                self.nodes[i].append( (len(valuesX), len(valuesY)) )
                valuesX.append(g.nodes[i][j].x)
                valuesY.append(g.nodes[i][j].y)
                
        for k in g.kerning:
            self.kerning.append(KerningPair(k))
            
        self.dataX = array(valuesX)
        self.dataY = array(valuesY)
        
    def copyToGlyph(self,g):
        g.width = self._derefX(self.width)
        if len(g.components) == len(self.components):
            for i in range(len(self.components)):
                g.components[i].scale.x = self._derefX( self.components[i][0] + 0)
                g.components[i].scale.y = self._derefY( self.components[i][1] + 0)
                g.components[i].deltas[0].x = self._derefX( self.components[i][0] + 1)
                g.components[i].deltas[0].y = self._derefY( self.components[i][1] + 1)
        g.kerning = []
        if len(g.anchors) == len(self.anchors):
            for i in range(len(self.anchors)):
                g.anchors[i].x = self._derefX( self.anchors[i][0])
                g.anchors[i].y = self._derefY( self.anchors[i][1])
        for k in self.kerning:
            g.kerning.append(KerningPair(k))
        for i in range( len(g.nodes)) :
            for j in range (len(g.nodes[i])):
                g.nodes[i][j].x = self._derefX( self.nodes[i][j][0] )
                g.nodes[i][j].y = self._derefY( self.nodes[i][j][1] )
    
    def isCompatible(self,g):
        return len(self.dataX) == len(g.dataX) and len(self.dataY) == len(g.dataY) and len(g.nodes) == len(self.nodes)
    
    def __add__(self,g):
        if self.isCompatible(g):
            newGlyph = self.copy()
            newGlyph.dataX = self.dataX + g.dataX
            newGlyph.dataY = self.dataY + g.dataY
            return newGlyph
        else:
            print "Add failed for '%s'" %(self.name)
            raise Exception
    
    def __sub__(self,g):
        if self.isCompatible(g):
            newGlyph = self.copy()
            newGlyph.dataX = self.dataX - g.dataX
            newGlyph.dataY = self.dataY - g.dataY
            return newGlyph
        else:
            print "Subtract failed for '%s'" %(self.name)
            raise Exception
    
    def __mul__(self,scalar):
        newGlyph = self.copy()
        newGlyph.dataX = self.dataX * scalar
        newGlyph.dataY = self.dataY * scalar
        return newGlyph
    
    def scaleX(self,scalar):
        newGlyph = self.copy()
        if len(self.dataX) > 0:
            newGlyph.dataX = self.dataX * scalar
            for i in range(len(newGlyph.components)):
                newGlyph.dataX[newGlyph.components[i][0]] = self.dataX[newGlyph.components[i][0]]
        return newGlyph
        
    def shift(self,ammount):
        newGlyph = self.copy()
        newGlyph.dataX = self.dataX + ammount
        for i in range(len(newGlyph.components)):
            newGlyph.dataX[newGlyph.components[i][0]] = self.dataX[newGlyph.components[i][0]]
        return newGlyph
    
    def interp(self, g, v):
        gF = self.copy()
        if not self.isCompatible(g):
            print "Interpolate failed for '%s'; outlines incompatible" %(self.name)
            raise Exception
        
        gF.dataX += (g.dataX - gF.dataX) * v.x
        gF.dataY += (g.dataY - gF.dataY) * v.y
        gF.kerning = interpolateKerns(self,g,v)
        return gF
    
    def copy(self):
        ng = FGlyph()
        ng.nodes = list(self.nodes)
        ng.width = self.width
        ng.components = list(self.components)
        ng.kerning = list(self.kerning)
        ng.anchors = list(self.anchors)
        ng.dataX = self.dataX.copy()
        ng.dataY = self.dataY.copy()
        ng.name = self.name
        return ng
    
    def _derefX(self,id):
        return int(round(self.dataX[id]))
    
    def _derefY(self,id):
        return int(round(self.dataY[id]))
    
    def addDiff(self,gB,gC):
        newGlyph = self + (gB - gC)
        return newGlyph
        
    

class Master:
    
    
    def __init__(self,font=None,v=0,ifont=None, kernlist=None, overlay=None):
        if isinstance(font,FFont):
            self.font = None
            self.ffont = font
        elif isinstance(font,str):
            self.openFont(font,overlay)
        elif isinstance(font,Mix):
            self.font = font
        else:
            self.font = font
            self.ifont = ifont
            self.ffont = FFont(font)
        if isinstance(v,float) or isinstance(v,int):
            self.v = Point(v,v)
        else:
            self.v = v
        if kernlist != None:
            kerns = [i.strip().split() for i in open(kernlist).readlines()]
            
            self.kernlist = [{'left':k[0], 'right':k[1], 'value': k[2]} 
                            for k in kerns 
                            if not k[0].startswith("#")
                            and not k[0] == ""]
            #TODO implement class based kerning / external kerning file
    
    def openFont(self, path, overlayPath=None):
        fl.Open(path,True)
        self.ifont = fl.ifont
        self.font = fl.font
        if overlayPath != None:
            fl.Open(overlayPath,True)
            ifont = self.ifont
            font = self.font
            overlayIfont = fl.ifont
            overlayFont = fl.font

            for overlayGlyph in overlayFont.glyphs:
                glyphIndex = font.FindGlyph(overlayGlyph.name)
                if glyphIndex != -1:
                    oldGlyph = Glyph(font.glyphs[glyphIndex])
                    kernlist = [KerningPair(k) for k in oldGlyph.kerning]
                    font.glyphs[glyphIndex] = Glyph(overlayGlyph)
                    font.glyphs[glyphIndex].kerning = kernlist
                else:
                    font.glyphs.append(overlayGlyph)
            fl.UpdateFont(ifont)
            fl.Close(overlayIfont)
        self.ffont = FFont(self.font)


class Mix:
    def __init__(self,masters,v):
        self.masters = masters
        if isinstance(v,float) or isinstance(v,int):
            self.v = Point(v,v)
        else:
            self.v = v
    
    def getFGlyph(self, master, gname):
        if isinstance(master.font, Mix):
            return font.mixGlyphs(gname)
        return master.ffont.getGlyph(gname)
    
    def getGlyphMasters(self,gname):
        masters = self.masters
        if len(masters) <= 2:
            return self.getFGlyph(masters[0], gname), self.getFGlyph(masters[-1], gname)
    
    def generateFFont(self):
        ffont = FFont(self.masters[0].ffont)
        for key,g in ffont.glyphs.iteritems():
            ffont.glyphs[key] = self.mixGlyphs(key)
        return ffont
    
    def generateFont(self, baseFont):
        newFont = Font(baseFont)
        #self.mixStems(newFont)  todo _ fix stems code
        for g in newFont.glyphs:
            gF = self.mixGlyphs(g.name)
            if gF == None:
                g.mark = True
            else:
                # FIX THIS #print gF.name, g.name, len(gF.nodes),len(g.nodes),len(gF.components),len(g.components)
                try:
                    gF.copyToGlyph(g)
                except:
                    "Nodes incompatible"
        return newFont
    
    def mixGlyphs(self,gname):
        gA,gB = self.getGlyphMasters(gname)        
        try:
            return gA.interp(gB,self.v)
        except:
            print "mixglyph failed for %s" %(gname)
            if gA != None:
                return gA.copy()

def narrowFLGlyph(g, gThin, factor=.75):
    gF = FGlyph(g)
    if not isinstance(gThin,FGlyph):
        gThin = FGlyph(gThin)
    gCondensed = gThin.scaleX(factor)
    try:
        gNarrow = gF + (gCondensed - gThin)
        gNarrow.copyToGlyph(g)
    except:
        print "No dice for: " + g.name
            
def interpolate(a,b,v,e=0):
    if e == 0:
        return a+(b-a)*v
    qe = (b-a)*v*v*v + a   #cubic easing
    le = a+(b-a)*v   # linear easing
    return le + (qe-le) * e
    
def interpolateKerns(gA,gB,v):
    kerns = []
    for kA in gA.kerning:
        key = kA.key
        matchedKern = None
        for kB in gA.kerning:
            if key == kB.key:
                matchedKern = kB
                break
        # if matchedkern == None:
        #     matchedkern = Kern(kA)
        #     matchedkern.value = 0
        if matchedKern != None:
            kernValue = interpolate(kA.value, matchedKern.value, v.x)
            kerns.append(KerningPair(kA.key,kernValue))
    return kerns