Skip to content

Instantly share code, notes, and snippets.

@RafalBuchner
Last active June 24, 2019 14:43
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save RafalBuchner/95242961d018d54ba76de6b4db148c42 to your computer and use it in GitHub Desktop.
# menuTitle : myCloud Export/Import
# shortCut : command+control+e
"""
code took from LettError known as Erik van Blokland (or Erik van Blokland known as LettError)
This is window that wraps the functionality explained here:
https://forum.robofont.com/topic/646/export-glyph-image-edit-on-ipad-reimport-as-background-image
https://gist.github.com/LettError/d0d2688abe959595a88b3fdb85c18db3
"""
from vanilla import FloatingWindow, SquareButton, TextBox
from AppKit import NSBorderlessWindowMask
import drawBot as ctx
import os
from mojo.UI import GetFile
from os.path import expanduser
from pathlib import Path
from subprocess import call
home = expanduser("~")
imagesDir = "Library/Mobile Documents/com~apple~CloudDocs/RoboFont/"
def exportDrawing():
path = os.path.join(home, imagesDir)
if not os.path.exists(path):
os.makedirs(path)
if not os.path.exists(path):
print('Could not make RoboFont iCloud images folder at', path )
padding = 100
f = CurrentFont()
if f is not None:
ufoName = os.path.basename(f.path)
ufoName = ufoName.replace(".ufo", "_images")
#print(ufoName)
ufoImagesPath = os.path.join(path, ufoName)
print("exporting to", ufoImagesPath)
if not os.path.exists(ufoImagesPath):
os.makedirs(ufoImagesPath)
g = CurrentGlyph()
if g is not None:
ctx.newPage(int(g.width) + 2*padding, int(f.info.unitsPerEm) + 2*padding)
ctx.translate(padding,-f.info.descender + padding)
ctx.stroke(0.5)
ctx.strokeWidth(1)
ctx.line((0,0), (g.width, 0))
ctx.fill(0)
ctx.drawGlyph(g)
imageName = os.path.join(ufoImagesPath, "%s.png" % g.name)
ctx.saveImage(imageName)
ctx.newDrawing()
else:
print("no open font")
def importDrawing():
home = expanduser("~")
#print(home)
padding = 100
path = os.path.join(home, imagesDir)
if not os.path.exists(path):
print("no icloud images folder found", path)
else:
f = CurrentFont()
if f is not None:
ufoName = os.path.basename(f.path)
ufoName = ufoName.replace(".ufo", "_images")
#print(ufoName)
ufoImagesPath = os.path.join(path, ufoName)
#print(ufoImagesPath, os.path.exists(ufoImagesPath))
g = CurrentGlyph()
imageName = os.path.join(ufoImagesPath, "%s.png" % g.name)
#print(imageName, os.path.exists(imageName))
w, h = ctx.imageSize(imageName)
scale = f.info.unitsPerEm / (h - 2*padding)
#print(scale)
g.addImage(imageName, scale=scale, position=(-padding, f.info.descender - padding))
g.update()
def showInFinder():
path = os.path.join(home, imagesDir)
path = os.path.normpath(path)
call(["open", path])
class Window:
def __init__(self):
self.w = FloatingWindow((500,500,200,22*7),closable=False, autosaveName="myCloudImpExp")
self.w.txt = TextBox((0,0,-0,22),"cloud image exporting")
self.w.sif = SquareButton((0,22,-0,22),"show image-dir in finder",callback=self.btnCB)
self.w.exp = SquareButton((0,-22*5,100,22*5),"export",callback=self.btnCB)
self.w.imp = SquareButton((100,-22*5,100,22*5),"import",callback=self.btnCB)
nsWin = self.w.getNSWindow()
nsWin.setStyleMask_(NSBorderlessWindowMask)
nsWin.setMovableByWindowBackground_(True)
self.w.open()
def btnCB(self,sender):
if sender.getTitle() == "show image-dir in finder":
showInFinder()
elif sender.getTitle() == "export":
try:
exportDrawing()
except:
print("export/import issue")
else:
try:
importDrawing()
except:
print("export/import issue")
self.w.close()
if __name__ == "__main__":
Window()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment