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
|
"""Pens for creating glyphs in FontLab."""
__all__ = ["FLPen", "FLPointPen", "drawFLGlyphOntoPointPen"]
from FL import *
try:
from fl_cmd import *
except ImportError:
print "The fl_cmd module is not available here. flPen.py"
from robofab.tools.toolsFL import NewGlyph
from robofab.pens.pointPen import AbstractPointPen
from robofab.pens.adapterPens import SegmentToPointPen
def roundInt(x):
return int(round(x))
class FLPen(SegmentToPointPen):
def __init__(self, glyph):
SegmentToPointPen.__init__(self, FLPointPen(glyph))
class FLPointPen(AbstractPointPen):
def __init__(self, glyph):
if hasattr(glyph, "isRobofab"):
self.glyph = glyph.naked()
else:
self.glyph = glyph
self.currentPath = None
def beginPath(self):
self.currentPath = []
def endPath(self):
# Love is... abstracting away FL's madness.
path = self.currentPath
self.currentPath = None
glyph = self.glyph
if len(path) == 1 and path[0][3] is not None:
# Single point on the contour, it has a name. Make it an anchor.
x, y = path[0][0]
name = path[0][3]
anchor = Anchor(name, roundInt(x), roundInt(y))
glyph.anchors.append(anchor)
return
firstOnCurveIndex = None
for i in range(len(path)):
if path[i][1] is not None:
firstOnCurveIndex = i
break
if firstOnCurveIndex is None:
# TT special case: on-curve-less contour. FL doesn't support that,
# so we insert an implied point at the end.
x1, y1 = path[0][0]
x2, y2 = path[-1][0]
impliedPoint = 0.5 * (x1 + x2), 0.5 * (y1 + y2)
path.append((impliedPoint, "qcurve", True, None))
firstOnCurveIndex = 0
path = path[firstOnCurveIndex + 1:] + path[:firstOnCurveIndex + 1]
firstPoint, segmentType, smooth, name = path[-1]
closed = True
if segmentType == "move":
path = path[:-1]
closed = False
# XXX The contour is not closed, but I can't figure out how to
# create an open contour in FL. Creating one by hand shows type"0x8011"
# for a move node in an open contour, but I'm not able to access
# that flag.
elif segmentType == "line":
# The contour is closed and ends in a lineto, which is redundant
# as it's implied by closepath.
path = path[:-1]
x, y = firstPoint
node = Node(nMOVE, Point(roundInt(x), roundInt(y)))
if smooth and closed:
if segmentType == "line" or path[0][1] == "line":
node.alignment = nFIXED
else:
node.alignment = nSMOOTH
glyph.Insert(node, len(glyph))
segment = []
nPoints = len(path)
for i in range(nPoints):
pt, segmentType, smooth, name = path[i]
segment.append(pt)
if segmentType is None:
continue
if segmentType == "curve":
if len(segment) < 2:
segmentType = "line"
elif len(segment) == 2:
segmentType = "qcurve"
if segmentType == "qcurve":
for x, y in segment[:-1]:
glyph.Insert(Node(nOFF, Point(roundInt(x), roundInt(y))), len(glyph))
x, y = segment[-1]
node = Node(nLINE, Point(roundInt(x), roundInt(y)))
glyph.Insert(node, len(glyph))
elif segmentType == "curve":
if len(segment) == 3:
cubicSegments = [segment]
else:
from fontTools.pens.basePen import decomposeSuperBezierSegment
cubicSegments = decomposeSuperBezierSegment(segment)
nSegments = len(cubicSegments)
for i in range(nSegments):
pt1, pt2, pt3 = cubicSegments[i]
x, y = pt3
node = Node(nCURVE, Point(roundInt(x), roundInt(y)))
node.points[1].x, node.points[1].y = roundInt(pt1[0]), roundInt(pt1[1])
node.points[2].x, node.points[2].y = roundInt(pt2[0]), roundInt(pt2[1])
if i != nSegments - 1:
node.alignment = nSMOOTH
glyph.Insert(node, len(self.glyph))
elif segmentType == "line":
assert len(segment) == 1, segment
x, y = segment[0]
node = Node(nLINE, Point(roundInt(x), roundInt(y)))
glyph.Insert(node, len(glyph))
else:
assert 0, "unsupported curve type (%s)" % segmentType
if smooth:
if i + 1 < nPoints or closed:
# Can't use existing node, as you can't change node attributes
# AFTER it's been appended to the glyph.
node = glyph[-1]
if segmentType == "line" or path[(i+1) % nPoints][1] == "line":
# tangent
node.alignment = nFIXED
else:
# curve
node.alignment = nSMOOTH
segment = []
if closed:
# we may have output a node too much
node = glyph[-1]
if node.type == nLINE and (node.x, node.y) == (roundInt(firstPoint[0]), roundInt(firstPoint[1])):
glyph.DeleteNode(len(glyph) - 1)
def addPoint(self, pt, segmentType=None, smooth=None, name=None, **kwargs):
self.currentPath.append((pt, segmentType, smooth, name))
def addComponent(self, baseName, transformation):
assert self.currentPath is None
# make base glyph if needed, Component() needs the index
NewGlyph(self.glyph.parent, baseName, updateFont=False)
baseIndex = self.glyph.parent.FindGlyph(baseName)
if baseIndex == -1:
raise KeyError, "couldn't find or make base glyph"
xx, xy, yx, yy, dx, dy = transformation
# XXX warn when xy or yx != 0
new = Component(baseIndex, Point(dx, dy), Point(xx, yy))
self.glyph.components.append(new)
def drawFLGlyphOntoPointPen(flGlyph, pen):
"""Draw a FontLab glyph onto a PointPen."""
for anchor in flGlyph.anchors:
pen.beginPath()
pen.addPoint((anchor.x, anchor.y), name=anchor.name)
pen.endPath()
for contour in _getContours(flGlyph):
pen.beginPath()
for pt, segmentType, smooth in contour:
pen.addPoint(pt, segmentType=segmentType, smooth=smooth)
pen.endPath()
for baseGlyph, tranform in _getComponents(flGlyph):
pen.addComponent(baseGlyph, tranform)
class FLPointContourPen(FLPointPen):
"""Same as FLPointPen, except that it ignores components."""
def addComponent(self, baseName, transformation):
pass
NODE_TYPES = {nMOVE: "move", nLINE: "line", nCURVE: "curve",
nOFF: None}
def _getContours(glyph):
contours = []
for i in range(len(glyph)):
node = glyph[i]
segmentType = NODE_TYPES[node.type]
if segmentType == "move":
contours.append([])
for pt in node.points[1:]:
contours[-1].append(((pt.x, pt.y), None, False))
smooth = node.alignment != nSHARP
contours[-1].append(((node.x, node.y), segmentType, smooth))
for contour in contours:
# filter out or change the move
movePt, segmentType, smooth = contour[0]
assert segmentType == "move"
lastSegmentType = contour[-1][1]
if movePt == contour[-1][0] and lastSegmentType == "curve":
contour[0] = contour[-1]
contour.pop()
elif lastSegmentType is None:
contour[0] = movePt, "qcurve", smooth
else:
assert lastSegmentType in ("line", "curve")
contour[0] = movePt, "line", smooth
# change "line" to "qcurve" if appropriate
previousSegmentType = "ArbitraryValueOtherThanNone"
for i in range(len(contour)):
pt, segmentType, smooth = contour[i]
if segmentType == "line" and previousSegmentType is None:
contour[i] = pt, "qcurve", smooth
previousSegmentType = segmentType
return contours
def _simplifyValues(*values):
"""Given a set of numbers, convert items to ints if they are
integer float values, eg. 0.0, 1.0."""
newValues = []
for v in values:
i = int(v)
if v == i:
v = i
newValues.append(v)
return newValues
def _getComponents(glyph):
components = []
for comp in glyph.components:
baseName = glyph.parent[comp.index].name
dx, dy = comp.delta.x, comp.delta.y
sx, sy = comp.scale.x, comp.scale.y
dx, dy, sx, sy = _simplifyValues(dx, dy, sx, sy)
components.append((baseName, (sx, 0, 0, sy, dx, dy)))
return components
def test():
g = fl.glyph
g.Clear()
p = PLPen(g)
p.moveTo((50, 50))
p.lineTo((150,50))
p.lineTo((170, 200), smooth=2)
p.curveTo((173, 225), (150, 250), (120, 250), smooth=1)
p.curveTo((85, 250), (50, 200), (50, 200))
p.closePath()
p.moveTo((300, 300))
p.lineTo((400, 300))
p.curveTo((450, 325), (450, 375), (400, 400))
p.qCurveTo((400, 500), (350, 550), (300, 500), (300, 400))
p.closePath()
p.setWidth(600)
p.setNote("Hello, this is a note")
p.addAnchor("top", (250, 600))
fl.UpdateGlyph(-1)
fl.UpdateFont(-1)
if __name__ == "__main__":
test()
|