Skip to content

Instantly share code, notes, and snippets.

@okay-type
Forked from connordavenport/removeOverlapPreview.py
Last active March 24, 2021 18:23
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 okay-type/5c767d6c978a8825cef3b2da78fd6325 to your computer and use it in GitHub Desktop.
Save okay-type/5c767d6c978a8825cef3b2da78fd6325 to your computer and use it in GitHub Desktop.
A startup script to preview a glyph with no overlap. Idea stolen from Glyphs (I still hate Glyphs)
from mojo.roboFont import RGlyph
from mojo.subscriber import Subscriber, registerGlyphEditorSubscriber
from mojo.UI import getDefault, setDefault
from lib.tools.notifications import PostNotification
from fontTools.pens.basePen import BasePen
class MyCopyDecomposingPen(BasePen):
def __init__(self, glyphSet, outPen):
super(MyCopyDecomposingPen, self).__init__(glyphSet)
self._moveTo = outPen.moveTo
self._lineTo = outPen.lineTo
self._curveToOne = outPen.curveTo
self._closePath = outPen.closePath
self._endPath = outPen.endPath
class removeOverlapPreview(Subscriber):
debug = True
toggle = False
def build(self):
self.name = self.__class__.__name__
if self.debug == True:
print(self.name, 'build!')
glyphEditor = self.getGlyphEditor()
self.backgroundContainer = glyphEditor.extensionContainer(
identifier='com.roboFont.removeOverlapPreview.background',
location='background',
clear=True
)
self.strokeColor = tuple(float(a) for a in getDefault('glyphViewStrokeColor'))
self.compStrokeColor = tuple(float(a) for a in getDefault('glyphViewComponentStrokeColor'))
def destroy(self):
if self.debug == True:
print(self.name, 'destroy!')
self.clearView()
def setView(self):
opacity = .2
setDefault('glyphViewComponentStrokeColor', tuple((self.compStrokeColor[0], self.compStrokeColor[1], self.compStrokeColor[2], opacity)))
setDefault('glyphViewStrokeColor', tuple((self.strokeColor[0], self.strokeColor[1], self.strokeColor[2], opacity)))
PostNotification('doodle.preferencesChanged')
def clearView(self):
self.backgroundContainer.clearSublayers()
setDefault('glyphViewComponentStrokeColor', self.compStrokeColor)
setDefault('glyphViewStrokeColor', self.strokeColor)
PostNotification('doodle.preferencesChanged')
def glyphEditorKeyDown(self, info):
glyph = info['glyph']
characters = info['lowLevelEvents'][0]['event'].characters()
if characters == '~':
self.toggle = not self.toggle
self.nooverlap(glyph)
def glyphEditorDidSetGlyph(self, info):
glyph = info['glyph']
self.nooverlap(glyph)
def glyphDidChange(self, info):
glyph = info['glyph']
self.nooverlap(glyph)
def nooverlap(self, glyph):
if self.toggle == False:
self.clearView()
else:
self.setView()
nooverlapLayer = self.backgroundContainer.getSublayer('nooverlap')
if nooverlapLayer is not None:
nooverlapLayer.clearSublayers()
else:
nooverlapLayer = self.backgroundContainer.appendBaseSublayer(
name='nooverlap'
)
tempGlyph = RGlyph()
pen = MyCopyDecomposingPen(glyph.font, tempGlyph.getPen())
glyph.draw(pen)
tempGlyph.removeOverlap(round=1)
with nooverlapLayer.drawingTools() as bot:
bot.fill(None)
bot.strokeWidth((getDefault('glyphViewStrokeWidth')))
bot.stroke(*self.strokeColor)
bot.drawGlyph(tempGlyph)
registerGlyphEditorSubscriber(removeOverlapPreview)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment