diff options
author | James Godfrey-Kittle <jamesgk@google.com> | 2015-02-20 10:17:27 -0800 |
---|---|---|
committer | James Godfrey-Kittle <jamesgk@google.com> | 2015-04-16 12:16:30 -0700 |
commit | e8775eb72e7db4c0c13150334cdadba4aa1153ef (patch) | |
tree | b1e2bc150df9933eba760af0281860ec12e86e87 | |
parent | 368916db20d90de6713e84048c3bfa6cb5ad1228 (diff) |
Make convertCurves RoboFab-compatible.
Another currently-unused but potentially useful module.
-rw-r--r-- | scripts/lib/fontbuild/Build.py | 2 | ||||
-rw-r--r-- | scripts/lib/fontbuild/convertCurves.py | 61 | ||||
-rw-r--r-- | scripts/lib/fontbuild/mitreGlyph.py | 7 |
3 files changed, 33 insertions, 37 deletions
diff --git a/scripts/lib/fontbuild/Build.py b/scripts/lib/fontbuild/Build.py index 672197f..af21f59 100644 --- a/scripts/lib/fontbuild/Build.py +++ b/scripts/lib/fontbuild/Build.py @@ -244,7 +244,7 @@ def cleanCurves(f): # mitreGlyph(g, 3., .7) log(">> Converting curves to quadratic") - # for g in f.glyphs: + # for g in f: # glyphCurvesToQuadratic(g) diff --git a/scripts/lib/fontbuild/convertCurves.py b/scripts/lib/fontbuild/convertCurves.py index 606e0b4..3f44711 100644 --- a/scripts/lib/fontbuild/convertCurves.py +++ b/scripts/lib/fontbuild/convertCurves.py @@ -9,6 +9,13 @@ exactly two off curve points. import numpy from numpy import array,cross,dot from fontTools.misc import bezierTools +from robofab.objects.objectsRF import RSegment + +def replaceSegments(contour, segments): + while len(contour): + contour.removeSegment(0) + for s in segments: + contour.appendSegment(s.type, [(p.x, p.y) for p in s.points], s.smooth) def calcIntersect(a,b,c,d): numpy.seterr(all='raise') @@ -50,40 +57,32 @@ def convertToQuadratic(p0,p1,p2,p3): off2 = (on2 - off2) * OFFCURVE_VECTOR_CORRECTION + off2 return (on1,off1,off2,on2) -def cubicNodeToQuadratic(g,nid): +def cubicSegmentToQuadratic(c,sid): - node = g.nodes[nid] - if (node.type != nCURVE): - print "Node type not curve" + segment = c[sid] + if (segment.type != "curve"): + print "Segment type not curve" return - #pNode,junk = getPrevAnchor(g,nid) - pNode = g.nodes[nid-1] #assumes that a nCURVE type will always be proceeded by another point on the same contour - points = convertToQuadratic(pNode[0],node[1],node[2],node[0]) - points = [Point(p[0],p[1]) for p in points] - curve = [ - Node(nOFF, points[1]), - Node(nOFF, points[2]), - Node(nLINE,points[3]) ] - return curve + #pSegment,junk = getPrevAnchor(c,sid) + pSegment = c[sid-1] #assumes that a curve type will always be proceeded by another point on the same contour + points = convertToQuadratic(pSegment.points[-1],segment.points[0], + segment.points[1],segment.points[2]) + return RSegment( + 'qcurve', [[int(i) for i in p] for p in points[1:]], segment.smooth) def glyphCurvesToQuadratic(g): - nodes = [] - for i in range(len(g.nodes)): - n = g.nodes[i] - if n.type == nCURVE: - try: - newNodes = cubicNodeToQuadratic(g, i) - nodes = nodes + newNodes - except Exception: - print g.name, i - raise - else: - nodes.append(Node(g.nodes[i])) - g.Clear() - g.Insert(nodes) - - - - + for c in g: + segments = [] + for i in range(len(c)): + s = c[i] + if s.type == "curve": + try: + segments.append(cubicSegmentToQuadratic(c, i)) + except Exception: + print g.name, i + raise + else: + segments.append(s) + replaceSegments(c, segments) diff --git a/scripts/lib/fontbuild/mitreGlyph.py b/scripts/lib/fontbuild/mitreGlyph.py index 6210c6b..0ee1448 100644 --- a/scripts/lib/fontbuild/mitreGlyph.py +++ b/scripts/lib/fontbuild/mitreGlyph.py @@ -8,6 +8,7 @@ maxAngle : Maximum angle in radians at which segments will be mitred. The defau import math from robofab.objects.objectsRF import RPoint, RSegment +from fontbuild.convertCurves import replaceSegments def getTangents(contours): tmap = [] @@ -92,8 +93,4 @@ def mitreGlyph(g,mitreSize,maxAngle): else: segments.append(s1) if needsMitring: - while len(c): - c.removeSegment(0) - for s in segments: - c.appendSegment( - s.type, [(p.x, p.y) for p in s.points], s.smooth) + replaceSegments(c, segments) |