Created
March 14, 2018 16:53
-
-
Save frankrolf/3ae6013e22e491c87f00e7a44d0774d0 to your computer and use it in GitHub Desktop.
Flipped glyph preview in Robofont
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mojo.events import BaseEventTool, addObserver | |
from AppKit import NSColor | |
from fontTools.pens.basePen import BasePen | |
from fontTools.pens.cocoaPen import CocoaPen | |
class CocoaGlyphPen(BasePen): | |
def __init__(self, onCurveSize, offCurveSize, selection=[]): | |
self.glyphPen = CocoaPen(None) | |
self.glyphPath = self.glyphPen.path | |
self.onCurvePen = CocoaPen(None) | |
self.onCurvePoints = self.onCurvePen.path | |
self.offCurvePen = CocoaPen(None) | |
self.offCurvePoints = self.offCurvePen.path | |
self.handleLinePen = CocoaPen(None) | |
self.handleLines = self.handleLinePen.path | |
self.selectedPen = CocoaPen(None) | |
self.selectedPoints = self.selectedPen.path | |
self.onCurveSize = onCurveSize | |
self.offCurveSize = offCurveSize | |
self.selection = selection | |
def _moveTo(self, pt): | |
self.glyphPen.moveTo(pt) | |
self.drawOnCurve(pt) | |
self.previousPoint = pt | |
def _lineTo(self, pt): | |
self.glyphPen.lineTo(pt) | |
self.drawOnCurve(pt) | |
self.previousPoint = pt | |
def _curveToOne(self, pt1, pt2, pt3): | |
self.glyphPen.curveTo(pt1, pt2, pt3) | |
self.drawOffCurve(pt1) | |
self.drawOffCurve(pt2) | |
self.drawHandleLine(pt1, self.previousPoint) | |
self.drawHandleLine(pt2, pt3) | |
self.drawOnCurve(pt3) | |
self.previousPoint = pt3 | |
def endPath(self): | |
self.glyphPen.endPath() | |
def closePath(self): | |
self.glyphPen.closePath() | |
def addComponent(self, baseName, transform): | |
pass | |
def drawOnCurve(self, pt, selection=False): | |
if pt in self.selection: | |
cocoaPen = self.selectedPen | |
else: | |
cocoaPen = self.onCurvePen | |
size = self.onCurveSize | |
x, y = pt | |
cocoaPen.moveTo((x - size, y - size)) | |
cocoaPen.lineTo((x + size, y - size)) | |
cocoaPen.lineTo((x + size, y + size)) | |
cocoaPen.lineTo((x - size, y + size)) | |
cocoaPen.lineTo((x - size, y - size)) | |
cocoaPen.closePath() | |
def drawOffCurve(self, pt, selection=False): | |
if pt in self.selection: | |
cocoaPen = self.selectedPen | |
else: | |
cocoaPen = self.offCurvePen | |
size = self.offCurveSize | |
x, y = pt | |
cocoaPen.moveTo((x, y - size)) | |
cocoaPen.lineTo((x + size, y)) | |
cocoaPen.lineTo((x, y + size)) | |
cocoaPen.lineTo((x - size, y)) | |
cocoaPen.lineTo((x, y - size)) | |
cocoaPen.closePath() | |
cocoaPen.closePath() | |
def drawHandleLine(self, pt1, pt2): | |
cocoaPen = self.handleLinePen | |
x, y = pt1 | |
xa, ya = pt2 | |
cocoaPen.moveTo((xa, ya)) | |
cocoaPen.lineTo((x, y)) | |
class PreviewFlipped(BaseEventTool): | |
def __init__(self, *args, **kwargs): | |
self.previewGlyph = None | |
addObserver(self, 'previewSolid', 'drawPreview') | |
def flip_glyph(self, glyph): | |
rg = RGlyph() | |
if glyph.contours: | |
for c in glyph.contours: | |
rg.appendContour(c) | |
rg.width = glyph.width | |
center_x = rg.box[0] + (rg.box[2] - rg.box[0]) / 2 | |
center_y = rg.box[1] + (rg.box[3] - rg.box[1]) / 2 | |
for c in rg.contours: | |
c.scale((-1, -1), (center_x, center_y)) | |
c.move((rg.width, 0)) | |
return rg | |
return | |
def drawPreview(self, glyph): | |
# scale = 1 | |
previewPen = CocoaGlyphPen(0, 0) | |
glyph.draw(previewPen) | |
fillColor = NSColor.colorWithCalibratedRed_green_blue_alpha_( | |
0, 0, 0, 1) | |
fillColor.set() | |
previewPen.glyphPath.fill() | |
def previewSolid(self, sender): | |
g = sender['glyph'] | |
if g.contours: | |
self.previewGlyph = self.flip_glyph(g) | |
self.drawPreview(self.previewGlyph) | |
PreviewFlipped() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment