From c5fcdeee44b6239038fdd8168557b831a8b5e6b6 Mon Sep 17 00:00:00 2001 From: James Godfrey-Kittle Date: Thu, 19 Feb 2015 11:03:15 -0800 Subject: Make mitreGlyph RoboFab-compatible. This code isn't being used currently, but maybe we will want to use it later. --- scripts/lib/fontbuild/mitreGlyph.py | 66 ++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 37 deletions(-) (limited to 'scripts/lib/fontbuild/mitreGlyph.py') diff --git a/scripts/lib/fontbuild/mitreGlyph.py b/scripts/lib/fontbuild/mitreGlyph.py index 94cf84a..7c8f9ff 100644 --- a/scripts/lib/fontbuild/mitreGlyph.py +++ b/scripts/lib/fontbuild/mitreGlyph.py @@ -7,18 +7,7 @@ maxAngle : Maximum angle in radians at which nodes will be mitred. The default """ import math - -def getContours(g): - nLength = len(g.nodes) - contours = [] - cid = -1 - for i in range(nLength): - n = g.nodes[i] - if n.type == nMOVE: - cid += 1 - contours.append([]) - contours[cid].append(n) - return contours +from robofab.objects.objectsRF import RPoint, RSegment def getTangents(contours): tmap = [] @@ -26,19 +15,19 @@ def getTangents(contours): clen = len(c) for i in range(clen): n = c[i] - p = Point(n.x, n.y) + p = RPoint(n.points[-1].x, n.points[-1].y) nn = c[(i + 1) % clen] pn = c[(clen + i - 1) % clen] - if nn.type == nCURVE: - np = Point(nn[1].x,nn[1].y) + if nn.type == 'curve': + np = RPoint(nn.points[1].x,nn.points[1].y) else: - np = Point(nn.x,nn.y) - if n.type == nCURVE: - pp = Point(n[2].x,n[2].y) + np = RPoint(nn.points[-1].x,nn.points[-1].y) + if n.type == 'curve': + pp = RPoint(n.points[2].x,n.points[2].y) else: - pp = Point(pn.x,pn.y) - nVect = Point(-p.x + np.x, -p.y + np.y) - pVect = Point(-p.x + pp.x, -p.y + pp.y) + pp = RPoint(pn.points[-1].x,pn.points[-1].y) + nVect = RPoint(-p.x + np.x, -p.y + np.y) + pVect = RPoint(-p.x + pp.x, -p.y + pp.y) tmap.append((pVect,nVect)) return tmap @@ -47,13 +36,13 @@ def normalizeVector(p): if m != 0: return p*(1/m) else: - return Point(0,0) + return RPoint(0,0) def getMagnitude(p): return math.sqrt(p.x*p.x + p.y*p.y) def getDistance(v1,v2): - return getMagnitude(Point(v1.x - v2.x, v1.y - v2.y)) + return getMagnitude(RPoint(v1.x - v2.x, v1.y - v2.y)) def getAngle(v1,v2): angle = math.atan2(v1.y,v1.x) - math.atan2(v2.y,v2.x) @@ -82,35 +71,38 @@ def getMitreOffset(n,v1,v2,mitreSize=4,maxAngle=.9): return radius = mitreSize / abs(getDistance(v1,v2)) - offset1 = Point(round(v1.x * radius), round(v1.y * radius)) - offset2 = Point(round(v2.x * radius), round(v2.y * radius)) + offset1 = RPoint(round(v1.x * radius), round(v1.y * radius)) + offset2 = RPoint(round(v2.x * radius), round(v2.y * radius)) return offset1, offset2 def mitreGlyph(g,mitreSize,maxAngle): if g == None: return - contours = getContours(g) - tangents = getTangents(contours) - nodes = [] - needsMitring = False + tangents = getTangents(g.contours) nid = -1 - for c in contours: + for c in g.contours: + nodes = [] + needsMitring = False for n in c: nid += 1 v1, v2 = tangents[nid] off = getMitreOffset(n,v1,v2,mitreSize,maxAngle) - n1 = Node(n) + n1 = n.copy() if off != None: offset1, offset2 = off - n2 = Node(nLINE, Point(n.x + offset2.x, n.y + offset2.y)) - n1[0].x += offset1.x - n1[0].y += offset1.y + n2 = RSegment('line', [(n.points[-1].x + offset2.x, + n.points[-1].y + offset2.y)]) + n1.points[0].x += offset1.x + n1.points[0].y += offset1.y nodes.append(n1) nodes.append(n2) needsMitring = True else: nodes.append(n1) - if needsMitring: - g.Clear() - g.Insert(nodes) + if needsMitring: + while len(c): + c.removeSegment(0) + for n in nodes: + c.appendSegment( + n.type, [(p.x, p.y) for p in n.points], n.smooth) -- cgit v1.2.3