Skip to content

Instantly share code, notes, and snippets.

@gferreira
Last active April 5, 2020 19:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gferreira/7a7c1722a000da477b3bf04551c254eb to your computer and use it in GitHub Desktop.
Save gferreira/7a7c1722a000da477b3bf04551c254eb to your computer and use it in GitHub Desktop.
Preview interpolation result in the Space Center using representations.
'''
just a proof of concept, name of the second master is hard-coded at the bottom
'''
from vanilla import FloatingWindow, CheckBox, Slider
from defconAppKit.windows.baseWindow import BaseWindowController
from mojo.events import addObserver, removeObserver
from mojo.UI import CurrentSpaceCenter
import mojo.drawingTools as ctx
from defcon import Glyph, registerRepresentationFactory, unregisterRepresentationFactory
def InterpolationGlyphFactory(glyph1, g2=RGlyph(), factor=0.5):
g1 = RGlyph(glyph1)
g3 = RGlyph()
g3.interpolate(factor, g1, g2)
return g3
class InterpolationPreviewSpaceCenter(BaseWindowController):
def __init__(self, anotherFont):
self.f2 = anotherFont
self.w = FloatingWindow((200, 70))
x = y = p = 10
textHeight = 20
self.w.factor = Slider((x, y, -p, textHeight),
value=0.5, minValue=-0.5, maxValue=1.5,
callback=self.updatePreviewCallback)
y += textHeight + p
self.w.preview = CheckBox((x, y, -p, textHeight),
'preview', value=True,
callback=self.updatePreviewCallback)
self.setUpBaseWindowBehavior()
registerRepresentationFactory(Glyph, "InterpolationPreview", InterpolationGlyphFactory)
addObserver(self, "drawInterpolation", "spaceCenterDraw")
self.w.open()
def windowCloseCallback(self, sender):
super().windowCloseCallback(sender)
removeObserver(self, "spaceCenterDraw")
unregisterRepresentationFactory(Glyph, "InterpolationPreview")
def updatePreviewCallback(self, sender):
S = CurrentSpaceCenter()
if not S:
return
S.updateGlyphLineView()
def drawInterpolation(self, notification):
if not self.w.preview.get():
return
S = CurrentSpaceCenter()
if not S:
return
factor = self.w.factor.get()
g1 = notification['glyph']
g2 = self.f2[g1.name]
g3 = g1.getRepresentation("InterpolationPreview", g2=g2, factor=factor)
ctx.fill(None)
ctx.lineJoin('round')
ctx.strokeWidth(2 * notification['scale'])
ctx.stroke(1, 0, 0)
ctx.drawGlyph(g3)
f2 = AllFonts().getFontsByStyleName('SomeOtherMaster')[0] ### style name of second master
InterpolationPreviewSpaceCenter(f2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment