Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Last active October 27, 2023 16:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save connordavenport/d245deb35ab6690891f506e255de0d63 to your computer and use it in GitHub Desktop.
Save connordavenport/d245deb35ab6690891f506e255de0d63 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.UI import getDefault, setDefault, inDarkMode
from lib.tools.notifications import PostNotification
from fontTools.pens.basePen import BasePen
from mojo.subscriber import Subscriber, registerGlyphEditorSubscriber, unregisterGlyphEditorSubscriber, listRegisteredSubscribers
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 = False
if inDarkMode():
suff = ".dark"
else:
suff = ""
def build(self):
glyphEditor = self.getGlyphEditor()
self.container = glyphEditor.extensionContainer(
identifier='com.roboFont.removeOverlapPreview.background',
location='background',
clear=True,
)
self.strokeColor = tuple(float(a) for a in getDefault(f'glyphViewStrokeColor{self.suff}'))
self.compStrokeColor = tuple(float(a) for a in getDefault(f'glyphViewComponentStrokeColor{self.suff}'))
self.pathLayer = self.container.appendPathSublayer(
strokeColor=None,
fillColor=None,
strokeWidth=getDefault(f'glyphViewStrokeWidth'),
)
self.setView()
def resetDefaults(self):
setDefault(f'glyphViewComponentStrokeColor{self.suff}', self.compStrokeColor)
setDefault(f'glyphViewStrokeColor{self.suff}', self.strokeColor)
PostNotification('doodle.preferencesChanged')
def setView(self):
opacity = .3
setDefault(f'glyphViewComponentStrokeColor{self.suff}', tuple((self.compStrokeColor[0], self.compStrokeColor[1], self.compStrokeColor[2], opacity)))
setDefault(f'glyphViewStrokeColor{self.suff}', tuple((self.strokeColor[0], self.strokeColor[1], self.strokeColor[2], opacity)))
PostNotification('doodle.preferencesChanged')
def destroy(self):
self.container.clearSublayers()
self.resetDefaults()
def glyphEditorDidSetGlyph(self, info):
glyph = info['glyph']
if glyph is not None:
self.nooverlap(glyph)
def glyphEditorGlyphDidChange(self, info):
glyph = info['glyph']
if glyph is not None:
self.nooverlap(glyph)
def glyphEditorDidMouseDrag(self, info):
glyph = info['glyph']
if glyph is not None:
self.nooverlap(glyph)
def nooverlap(self, glyph):
self.pathLayer.clearSublayers()
tempGlyph = RGlyph()
pen = MyCopyDecomposingPen(glyph.font, tempGlyph.getPen())
glyph.draw(pen)
tempGlyph.removeOverlap(round=1)
glyphPath = tempGlyph.getRepresentation("merz.CGPath")
self.pathLayer.setPath(glyphPath)
if not glyph.contours:
strokeColor = self.compStrokeColor
else:
strokeColor = self.strokeColor
self.pathLayer.setStrokeColor(strokeColor)
if __name__ == '__main__':
finder = [subs for subs in listRegisteredSubscribers() if 'removeOverlapPreview' in subs.getIdentifier()]
if finder:
for s in finder:
unregisterGlyphEditorSubscriber(s)
print("uninstall")
else:
registerGlyphEditorSubscriber(removeOverlapPreview)
print("install")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment