Skip to content

Instantly share code, notes, and snippets.

@typemytype
Created May 10, 2016 14:42
Show Gist options
  • Select an option

  • Save typemytype/e8a5774b76db81a4ea3e0e9ea4ad1ae3 to your computer and use it in GitHub Desktop.

Select an option

Save typemytype/e8a5774b76db81a4ea3e0e9ea4ad1ae3 to your computer and use it in GitHub Desktop.
convert lines to curves
from fontTools.pens.basePen import BasePen
from lib.tools.bezierTools import calculateAngle
from math import sin, cos, radians
class MyPen(BasePen):
def __init__(self, outpen, distance, insice):
BasePen.__init__(self, None)
self.pen = outpen
self.previousPoint = None
self.firstPoint = None
self.insice = insice
self.distance = distance
def _moveTo(self, (x, y)):
print "move", x, y
self.pen.moveTo((x, y))
self.previousPoint = x, y
self.firstPoint = x, y
def _lineTo(self, (x, y)):
print "line", x, y
a = calculateAngle(self.previousPoint, (x, y), inDegrees=False)
px, py = self.previousPoint
adjustment = radians(self.insice)
offx1 = px + cos(a-adjustment) * self.distance
offy1 = py + sin(a-adjustment) * self.distance
offx2 = x - cos(a+adjustment) * self.distance
offy2 = y - sin(a+adjustment) * self.distance
self.pen.curveTo((offx1, offy1), (offx2, offy2), (x, y))
self.previousPoint = x, y
def _curveToOne(self, (x1, y1), (x2, y2), (x, y)):
print "curve", x, y
self.pen.curveTo((x1, y1), (x2, y2), (x, y))
self.previousPoint = x, y
def closePath(self):
print "closePath"
if self.firstPoint != self.previousPoint:
self.lineTo(self.firstPoint)
self.pen.closePath()
self.previousPoint = None
g = CurrentGlyph().getLayer("foreground")
layer = g.getLayer("result")
layer.clear()
myPen = MyPen(layer.getPen(), 100, -10)
g.draw(myPen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment