Drawbot in Glyphs question

Well, as discussed previously. The .ufo option is because there’s no way to have the current Font of the file you are working with without having cache problems. This is the first thing that came to my mind so that is a way to get a “font” out of the instances in the current file.

Obviously, as Peter is pointing out, the best option would be having a testInstall() function inside Glyphs.

Also, do you know what I’m doing wrong with the script I’ve posted? It doesn’t generate .ufo files. I’ve seen the GSInstance.generate(UFO)() function on the API but it doesn’t work neither.

You can use a font without installing it. Most AppKit functions that use fonts are fine with that so is Drawbot. So you don’t need to testInstall, just a reference to the .otf font. There are low level functions in CoreText that make it work. I think that is what Drawbot is using under the hood.

Yes, exporting UFO seems to be broken in the current build. I only get it to return the error tuple (1, None).

This seems to work:

from GlyphsApp import *

pageSize, inset = 500, 30

for instance in Glyphs.font.instances:
    instance.generate(OTF)
    fontPath = instance.lastExportedFilePath

    newPage(pageSize, pageSize)
    fontName = font(fontPath, 24)
    myText = "The quick brown fox jumps over the lazy dog. "
    textBox(
        fontName + "\n" + myText*10, 
        (inset, inset, pageSize-inset*2, pageSize-inset*2)
    )
    uninstallFont(fontPath)

And exporting an ufo works for me:

instance.generate(UFO, FontPath=os.path.expanduser("~/Desktop/"))
2 Likes

Ah, got it to work. The mistake I made was to include the file name in the FontPath parameter.

GSInstance.lastExportedFilePath is only set when the user exports it through the dialog, it seems. Exporting with GSInstance.generate() leaves the attribute untouched.

The file name needs to be reconstructed with GSInstance.fileName() and the path provided to the generate() function. I will follow up with a complete working snippet later.

Here is what works for me. Quickest possible export (no hinting, no overlap removal and no subroutinization). Sometimes it will not load properly, but usually it works if you simply run it again. (I assume the installFont() statement kicks in while the export is still not finished or the file not yet accessible in the system, perhaps we need to put in a half-second delay or something.)

from GlyphsApp import *
from datetime import datetime
import os

# user settings:
myPath = "~/Desktop/test/"
pageSize, inset = 500, 30

def timestamp(path):
    try:
        stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
        nakedPath, extension = os.path.splitext(path)
        newPath = "%s-%s%s" % (nakedPath, stamp, extension)
        os.rename(path,newPath)
        return newPath
    except Exception as e:
        print "ERROR: could not timestamp file."
        print e
        return path

expandedPath = os.path.expanduser(myPath)
for instance in Glyphs.font.instances:
    print "\nExporting %s..." % instance.name
    exportResult = instance.generate(OTF, expandedPath, AutoHint=False, RemoveOverlap=False, UseSubroutines=False)
    if exportResult:
        print exportResult
        exportedPath = expandedPath + instance.fileName()
        fontPath = timestamp(exportedPath)
        print "  %s" % fontPath
        try:
            fontName = installFont(fontPath)
            newPage(pageSize, pageSize)
            font(fontName, 24)
            myText = "The quick brown fox jumps over the lazy dog. "
            textBox(
                fontName + "\n" + myText*10, 
                (inset, inset, pageSize-inset*2, pageSize-inset*2)
            )
            uninstallFont(fontPath)
        except Exception as e:
            print "ERROR: could not install font."
            print e
    else:
        print "Something went wrong, sorry.", exportResult
2 Likes

That works fine for me. At least for the OTF export.

Hi Georg, thanks for the UFO help. However, I still have a question: why when I write the path like this (example) it is no longer working?

instance.generate(UFO, FontPath= "~/Desktop/")

Thanks!

You need to expand the path yourself. That might have changed with recent MacOS updates.

Edit: I fixed the tilde expansion.

2 Likes

@mekkablue and @GeorgSeifert, it’s been a month since you guys responded to this, but I finally had a chance to incorporate it into my script. It’s working perfectly! Thank you so much for taking the time to write such a helpful code snippet—it makes all the difference.

1 Like