Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Created December 21, 2023 23:03
Show Gist options
  • Save connordavenport/b1c25dc52d19e37b24f7da429de1347e to your computer and use it in GitHub Desktop.
Save connordavenport/b1c25dc52d19e37b24f7da429de1347e to your computer and use it in GitHub Desktop.
a subclass of EditingTool for adjusting the /Ldot spacing
#!/usr/bin/env python3
import AppKit
from mojo.subscriber import Subscriber, WindowController
from mojo.UI import getDefault
from mojo.events import EditingTool, installTool
from mojo.subscriber import registerGlyphEditorSubscriber, unregisterGlyphEditorSubscriber
from mojo.roboFont import OpenWindow
from mojo.tools import IntersectGlyphWithLine
from vanilla import FloatingWindow, CheckBox
RED = 1, 0, 0, 1
if AppKit.NSApp().appearance() == AppKit.NSAppearance.appearanceNamed_(AppKit.NSAppearanceNameDarkAqua):
suffix = ".dark"
else:
suffix = ""
class CatDogController(EditingTool):
debug = True
def setup(self):
self.container = self.extensionContainer(
identifier="com.roboFont.CatDogController.foreground",
location="foreground",
clear=True
)
self.glyphViewMeasurementsForegroundColor = getDefault(f"glyphViewMeasurementsForegroundColor{suffix}")
self.glyphViewMeasurementsBackgroundColor = getDefault(f"glyphViewMeasurementsBackgroundColor{suffix}")
self.glyphViewMeasurementsTextColor = getDefault(f"glyphViewMeasurementsTextColor{suffix}")
self.glyphViewMeasurementsTextBackgroundColor = getDefault(f"glyphViewMeasurementsTextBackgroundColor{suffix}")
self.distancesLayer = self.container.appendBaseSublayer()
def destroy(self):
self.container.clearSublayers()
def draw(self):
glyph = self.getGlyph()
naked = glyph.font.asDefcon()
c = naked.unicodeData.categoryForGlyphName(glyph.name)
if c == "Lu":
ref = "L"
else:
ref = "l"
selection = glyph.selectedComponents
if len(selection) == 1:
tempGlyph = RGlyph()
for cs in glyph.components:
tempGlyph.appendGlyph(glyph.font[cs.baseGlyph],cs.offset)
tempGlyph.width = glyph.width
tempGlyph.appendGlyph(glyph.font[ref],offset=(glyph.width,0))
tempGlyph.width += glyph.font[ref].width
self.distancesLayer.setVisible(True)
self.distancesLayer.clearSublayers()
c = selection[0]
yPos = ((c.bounds[3] - c.bounds[1]) / 2) + c.bounds[1]
line = ((-100, yPos), (tempGlyph.width+100, yPos))
i = sorted(IntersectGlyphWithLine(tempGlyph, line, True, True))
lastPos = 0
for dot in i:
if lastPos == 0:
dist = None
else:
dist = round(dot[0] - lastPos,2)
lastPos = dot[0]
if dist:
self.distancesLayer.appendTextLineSublayer(
position=(dot[0] - dist/2, dot[1]),
backgroundColor=self.glyphViewMeasurementsTextBackgroundColor,
text=str(dist),
font="system",
weight="bold",
pointSize=12,
padding=(4, 1),
cornerRadius=4,
fillColor=self.glyphViewMeasurementsTextColor,
horizontalAlignment='center',
verticalAlignment='center',
)
self.distancesLayer.appendSymbolSublayer(
position=dot,
imageSettings=dict(
name="oval",
size=(6,6),
fillColor = self.glyphViewMeasurementsForegroundColor,
) )
self.distancesLayer.appendLineSublayer(
startPoint=line[0],
endPoint=line[1],
strokeWidth=1,
strokeColor=self.glyphViewMeasurementsBackgroundColor
)
glyphPath = glyph.font[ref].getRepresentation("merz.CGPath")
glyphView = self.distancesLayer.appendPathSublayer(
strokeColor=self.glyphViewMeasurementsForegroundColor,
strokeWidth=1,
fillColor=self.glyphViewMeasurementsBackgroundColor,
position=(glyph.width, 0),
name=f'{glyph.name}_layer'
)
glyphView.setPath(glyphPath)
else:
self.distancesLayer.setVisible(False)
def mouseDragged(self, point, delta):
self.draw()
def mouseDown(self, point, clickCount):
self.draw()
def getToolbarTip(self):
return "CatDog"
def becomeInactive(self):
self.destroy()
if __name__ == '__main__':
CatDogController = CatDogController()
installTool(CatDogController)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment