Skip to content

Instantly share code, notes, and snippets.

@gferreira
Last active September 29, 2018 10:05
Show Gist options
  • Save gferreira/66a0846cff74ae43bf89f2d6dce4d957 to your computer and use it in GitHub Desktop.
Save gferreira/66a0846cff74ae43bf89f2d6dce4d957 to your computer and use it in GitHub Desktop.
lock/unlock widths of glyph layers
from vanilla import HUDFloatingWindow, SquareButton, CheckBox
from defconAppKit.windows.baseWindow import BaseWindowController
from mojo import drawingTools as ctx
from mojo.events import addObserver, removeObserver
from mojo.UI import UpdateCurrentGlyphView, getDefault
class LockLayerWidthsDialog(BaseWindowController):
key = 'com.hipertipo.lockLayerWidths'
verbose = True
padding = 10
textHeight = 20
buttonHeight = 30
width = 123
def __init__(self):
self.height = self.textHeight * 2
self.height += self.buttonHeight * 2
self.height += self.padding * 4
self.w = HUDFloatingWindow((self.width, self.height), title='layer widths')
x = y = p = self.padding
self.w.lockLayers = SquareButton(
(x, y, -p, self.buttonHeight),
'lock',
callback=self.lockGlyphsCallback)
y += self.buttonHeight + p
self.w.unlockLayers = SquareButton(
(x, y, -p, self.buttonHeight),
'unlock',
callback=self.unlockGlyphsCallback)
y += self.buttonHeight + p
self.w.currentGlyph = CheckBox(
(x, y, -p, self.textHeight),
'current glyph',
sizeStyle='small',
value=True)
y += self.textHeight
self.w.fontSelection = CheckBox(
(x, y, -p, self.textHeight),
'font selection',
sizeStyle='small',
value=True)
self.setUpBaseWindowBehavior()
addObserver(self, "drawCallback", "draw")
addObserver(self, "drawCallback", "spaceCenterDraw")
self.w.open()
def windowCloseCallback(self, sender):
super().windowCloseCallback(sender)
removeObserver(self, "draw")
removeObserver(self, "spaceCenterDraw")
def drawCallback(self, notification):
glyph = notification['glyph']
scale = notification['scale']
font = glyph.font
# get glyph lock status
lockStatus = False
if self.key in font.lib:
if glyph.name in font.lib[self.key]:
lockStatus = font.lib[self.key][glyph.name]
# copy width to all other layers
if lockStatus:
for layerName in font.layerOrder:
layerGlyph = glyph.getLayer(layerName)
layerGlyph.width = glyph.width
# draw lock status in canvas
if not 'spaceCenter' in notification:
if lockStatus:
lockStatusText = '๐Ÿ”’'
else:
lockStatusText = '๐Ÿ”“'
ctx.save()
ctx.fontSize(24 * scale)
ctx.text(lockStatusText, (glyph.width, 0))
ctx.restore()
def lockGlyphsCallback(self, sender):
self.setLockStatus(True)
UpdateCurrentGlyphView()
def unlockGlyphsCallback(self, sender):
self.setLockStatus(False)
UpdateCurrentGlyphView()
def setLockStatus(self, value):
font = CurrentFont()
if not font:
return
glyphNames = []
# get current glyph
if self.w.currentGlyph.get():
g = CurrentGlyph()
if g is not None:
glyphNames += [g.name]
# get font selection
if self.w.fontSelection.get():
glyphNames += font.selectedGlyphNames
# remove duplicates and sort
glyphNames = sorted(list(set(glyphNames)))
# get glyphName:lockStatus dict
lockGlyphsDict = {}
if self.key in font.lib:
lockGlyphsDict = font.lib[self.key]
# set lock status for selected glyphs
for glyphName in glyphNames:
if self.verbose:
if value:
print('locking layer widths (%s)...' % glyphName)
else:
print('unlocking layer widths (%s)...' % glyphName)
lockGlyphsDict[glyphName] = value
# save glyph lock dict in font lib
font.lib[self.key] = lockGlyphsDict
D = LockLayerWidthsDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment