Sunday, 12 March 2023

Can't get FontForge to import as a module in a custom Python script

I have the following script:

import fontforge
import os.path
import sys

if len(sys.argv) < 3:
    print("Usage: [FONT] [OUTPUTDIR]")
    exit(1)

fontpath = sys.argv[1]
font     = fontforge.open(fontpath)
outdir   = sys.argv[2]

if os.path.exists(outdir):
    pass
elif os.access(os.path.dirname(outdir), os.W_OK):
    os.makedirs(outdir)
else:
    exit(1)

for name in font:
    filename = name + ".svg"
    outfull = os.path.join(outdir,filename)
    font[name].export(outfull)

The script dumps all glyphs to individual SVG files in a folder.

Anyway, I've installed the latest windows version of FontForge.

From what I understand, FontForge embeds python, and the binary is ffpython.exe. So you should be able to call ffpython myscript.py {arg1 arg2 arg3}

The problem is that this doesn't work.

When I do ffpython myscript.py arg1 arg2, I get an error:

ImportError: DLL load failed while importing fontforge: The specified procedure could not be found.

You can also result with the same error by simply invoking ffpython and then typing import fontforge directly into the interpreter. See this image:

enter image description here

Now, here's the catch. There is a batch file that comes with FontForge located at C:\Program Files (x86)\FontForgeBuilds\fontforge-console.bat.

If you run this batch file, it sets some variables and then opens a console window. If I then call ffpython in that console window and enter import fontforge, everything magically works as expected:

enter image description here

But this is a really limited and improper solution. I don't want to have to open this magical batch file every time I want to run a Python script that interfaces with FontForge.

So here's my million-dollar question:

How can I get FontForge properly installed as a module in Python? How can I get a python script to import the fontforge module when being run via ffpython?

I would greatly appreciate any help at all.



from Can't get FontForge to import as a module in a custom Python script

No comments:

Post a Comment