Skip to content

Instantly share code, notes, and snippets.

@LettError
Last active July 4, 2019 08:25
Show Gist options
  • Save LettError/960eb6863bfdbc6ad512 to your computer and use it in GitHub Desktop.
Save LettError/960eb6863bfdbc6ad512 to your computer and use it in GitHub Desktop.
RoboFont: copy glif from git repo to layer in current glyph.
"""
Assuming you have
a UFO open in RoboFont
a current glyph selected
and furthermore that
this glyph is part of a git repository
and that you have done some work on it
and would like to compare the current state in robofont
with the version in git
then
this script will get the .glif xml for the right file
and write it to a layer named "prev"
By running this script you agree that
you understand that any data in the prev layer might be forfeit,
you need to run the ufo3 version of robofab as you need the ufoLib,
and that
the author of this script is not responsible for any loss of data.
"""
import os
import subprocess
from ufoLib import UFOReader
from ufoLib.glifLib import readGlyphFromString
from defcon.objects.glyph import Glyph
f = CurrentFont()
g = CurrentGlyph()
cwd = os.getcwd()
if f is not None:
targetPrev = g.getLayer('prev')
targetPrev.clear()
prevPen = targetPrev.getPointPen()
reader = UFOReader(f.path)
gs = reader.getGlyphSet()
glifName = gs.contents.get(g.name)
glyphsDir = os.path.join(f.path, "glyphs")
if glifName is not None:
path = os.path.join(glyphsDir, glifName)
os.chdir(glyphsDir)
cmd = "HEAD:./%s"%glifName
p = subprocess.Popen(['git', 'show', cmd], stdout=subprocess.PIPE)
glifXML = p.stdout.read()
if len(glifXML)>0:
readGlyphFromString(glifXML, targetPrev, prevPen)
print "Updated %s from %s"%(g.name, glifName)
else:
print "No data found for %s"%g.name
os.chdir(cwd)
@LettError
Copy link
Author

@anthrotype Good point, edited, thanks!

@justvanrossum
Copy link

On the other hand, RoboFont uses robofab.ufoLib, so using that would take away an external dependency.

(defcon''s Glyph doesn't appear to be used here.)

@LettError
Copy link
Author

@justvanrossum: updated! Next step: RoboFont could sniff at the *.ufo/glyphs and check for possible conflicts. If there is a conflicting version of a glyph in git, RF could pull it and store it in a separate layer. Data is preserved and the user can choose either version to go to the foreground layer.

@LettError
Copy link
Author

Update: check if we have any results before passing the xml to readGlyphFromString.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment