Skip to content

Instantly share code, notes, and snippets.

@typemytype
Created April 6, 2014 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typemytype/10011318 to your computer and use it in GitHub Desktop.
Save typemytype/10011318 to your computer and use it in GitHub Desktop.
ReconvertSplinePointPen
from robofab.pens.pointPen import AbstractPointPen, PrintingPointPen
from robofab.pens.adapterPens import SegmentToPointPen
from lib.tools.bezierTools import intersectRayRay
class ReconvertSplinePointPen(AbstractPointPen):
def __init__(self, outPointPen):
self.outPointPen = outPointPen
def beginPath(self):
self.points = []
def endPath(self):
points = []
for i, data in enumerate(self.points):
if data["name"] and "inserted" in data["name"]:
if data["segmentType"] is None:
prevOnCurvePoint = self.points[i - 1]
nextOffCurvePoint = self.points[(i + 1) % len(self.points)]
nextOnCurvePoint = self.points[(i + 2) % len(self.points)]
result = instersectRayRay(prevOnCurvePoint["pt"], data["pt"], nextOffCurvePoint["pt"], nextOnCurvePoint["pt"])
if result.points:
p = result.points[0]
nextOffCurvePoint["pt"] = int(round(p.x)), int(round(p.y))
continue
points.append(data)
self.outPointPen.beginPath()
for data in points:
self.outPointPen.addPoint(data["pt"],
data["segmentType"],
data["smooth"],
data["name"],
**data["kwargs"])
self.outPointPen.endPath()
def addPoint(self, pt, segmentType=None, smooth=False, name=None, **kwargs):
data = dict(pt=pt, segmentType=segmentType, smooth=smooth, name=name, kwargs=kwargs)
self.points.append(data)
def addComponent(self, baseGlyphName, transformation):
self.outPointPen.addComponent(baseGlyphName, transformation)
if __name__ == "__main__":
from compositor import Font
class TestPen(AbstractPointPen):
def __init__(self):
self.data = []
def beginPath(self):
self.data.append("beginPath")
def endPath(self):
self.data.append("endpath")
def addPoint(self, (x, y), segmentType=None, smooth=False, name=None, **kwargs):
self.data.append("point: %s, %s segmentType: %s" % (x, y, segmentType))
def addComponent(self, baseGlyphName, transformation):
self.data.append("baseGlyph: %s transformation: %s" % (baseGlyphName, transformation))
g = CurrentGlyph()
print g
testPen = TestPen()
pen = ReconvertSplinePointPen(testPen)
g.drawPoints(pen)
source = Font(g.getParent().lib["com.typemytype.robofont.binarySource"])
sourceG = source[g.name]
sourceTestPen = TestPen()
sourceG.draw(SegmentToPointPen(sourceTestPen))
print testPen.data == sourceTestPen.data
print "\n".join(testPen.data)
print
print "\n".join(sourceTestPen.data)
@sansplomb
Copy link

Hi there :)
there is a typo line 23: "instersectRayRay" instead of "intersectRayRay"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment