Skip to content

Instantly share code, notes, and snippets.

@mathieureguer
Created April 22, 2020 14:07
Show Gist options
  • Save mathieureguer/a864c041b1443255c3e6bcada79cafda to your computer and use it in GitHub Desktop.
Save mathieureguer/a864c041b1443255c3e6bcada79cafda to your computer and use it in GitHub Desktop.
"""
selectively copy and paste glyph attributes
"""
from vanilla import *
class attributeMover():
def __init__(self):
attrs = ["angledLeftMargin", "angledRightMargin", "width", "mark", "unicode"]
self.panel()
def panel(self):
xMargin = 15
yMargin = 15
itemHeight = 20
windowsWidth = 200
currentHeight = 0
self.w = Window((windowsWidth, currentHeight), "Copy Paste Attributes")
currentHeight += yMargin
self.w.copyButton = Button((xMargin, currentHeight, -xMargin, itemHeight), title="Copy", callback=self.copyCallback)
currentHeight += itemHeight + yMargin / 2
self.w.angledLeftMargin = CheckBox((xMargin, currentHeight, -xMargin, itemHeight), "angledLeftMargin")
currentHeight += itemHeight
self.w.angledRightMargin = CheckBox((xMargin, currentHeight, -xMargin, itemHeight), "angledRightMargin")
currentHeight += itemHeight
self.w.width = CheckBox((xMargin, currentHeight, -xMargin, itemHeight), "width")
currentHeight += itemHeight
self.w.mark = CheckBox((xMargin, currentHeight, -xMargin, itemHeight), "mark")
currentHeight += itemHeight
self.w.unicode = CheckBox((xMargin, currentHeight, -xMargin, itemHeight), "unicode")
currentHeight += itemHeight + yMargin / 2
self.w.pasteButton = Button((xMargin, currentHeight, -xMargin, itemHeight), title="Paste", callback=self.pasteCallback)
currentHeight += itemHeight + yMargin / 2
self.w.glyphName = CheckBox((xMargin, currentHeight, -xMargin, itemHeight), "map to glyph name", callback=self.glyphNameCallback)
currentHeight += itemHeight
self.w.limitSelect = CheckBox((xMargin, currentHeight, -xMargin, itemHeight), "limit to selection")
self.w.limitSelect.enable(False)
currentHeight += itemHeight + yMargin
self.w.resize(windowsWidth, currentHeight)
self.w.open()
def copyCallback(self, sender):
f = CurrentFont()
self.selection = []
for g in f.selection:
glyph = f[g]
self.selection.append(glyph)
def glyphNameCallback(self, sender):
self.w.limitSelect.enable(sender.get())
def pasteCallback(self, sender):
print("paste")
f = CurrentFont()
for source in self.selection:
if self.w.glyphName.get() == True:
if f.has_key(source.name):
target = f[source.name]
if (self.w.limitSelect.get() and target.selected) == True or self.w.limitSelect.get() == False:
self.copyPasteAttr(source, target)
else:
pass
else:
index = self.selection.index(source)
if index < len(f.selection):
n = f.selection[index]
target = f[n]
self.copyPasteAttr(source, target)
else:
pass
def copyPasteAttr(self, source, target):
target.prepareUndo("Copy Attributes")
if self.w.angledLeftMargin.get():
target.angledLeftMargin = source.angledLeftMargin
if self.w.angledRightMargin.get():
target.angledRightMargin = source.angledRightMargin
if self.w.width.get():
target.width = source.width
if self.w.mark.get():
target.mark = source.mark
if self.w.unicode.get():
target.unicode = source.unicode
target.update()
target.performUndo()
attributeMover()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment