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.
There is a batch file that comes with FontForge located at C:\Program Files (x86)\FontForgeBuilds\fontforge-console.bat
.
If I 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.
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 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.
Edit:
Running the test script by @CristiFati from an Admin PowerShell prompt where the PWD is not the same as ffpython (Failure):
03-14 20:06:09 D:\> & 'C:\Program Files (x86)\FontForgeBuilds\bin\ffpython.exe' .\code00.py
Executable: C:\Program Files (x86)\FontForgeBuilds\bin\ffpython.exe
Version: 3.10.9 (main, Dec 10 2022, 09:16:22) [GCC 12.2.0 32 bit]
CWD: D:\
UName: None
PATH: C:\Program Files\PowerShell\7;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\libnvvp;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64_win\compiler;C:\Program Files (x86)\Common Files\Intel\Shared Files\cpp\bin\Intel64;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64_win\compiler;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32_win\compiler;C:\Python\Python310\Scripts\;C:\Python\Python310\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\BIN;C:\Program Files\Git\cmd;C:\Program Files\GitHub CLI\;C:\Program Files\7-Zip;C:\Program Files\NVIDIA Corporation\Nsight Compute 2020.1.1;C:\Program Files\dotnet\;C:\Program Files\CMake\bin;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\PowerShell\7\;C:\Program Files (x86)\FontForgeBuilds\bin;C:\Users\myusername\AppData\Local\Microsoft\WindowsApps;C:\Users\myusername\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\myusername\.dotnet\tools;C:\Program Files (x86)\FontForgeBuilds\bin\
PYTHONHOME: None
PYTHONPATH: None
sys.path: ['D:\\', 'C:\\Program Files (x86)\\FontForgeBuilds\\lib\\python310.zip', 'C:\\Program Files (x86)\\FontForgeBuilds\\lib\\python3.10', 'C:\\Program Files (x86)\\FontForgeBuilds\\lib\\python3.10\\lib-dynload', 'D:\\', 'C:\\Program Files (x86)\\FontForgeBuilds\\lib\\python3.10\\site-packages']
Traceback (most recent call last):
File "D:\code00.py", line 12, in <module>
import fontforge as ff
ImportError: DLL load failed while importing fontforge: The specified procedure could not be found.
03-14 20:06:24 D:\>
Same results occur if I use CMD.
Running the test script by @CristiFati from an Admin PowerShell prompt where the PWD is C:\Program Files (x86)\FontForgeBuilds\bin
(Success):
03-14 20:16:08 C:\Program Files (x86)\FontForgeBuilds\bin> ffpython D:\code00.py
Executable: C:\Program Files (x86)\FontForgeBuilds\bin\ffpython.exe
Version: 3.10.9 (main, Dec 10 2022, 09:16:22) [GCC 12.2.0 32 bit]
CWD: C:\Program Files (x86)\FontForgeBuilds\bin
UName: None
PATH: C:\Program Files\PowerShell\7;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\libnvvp;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64_win\compiler;C:\Program Files (x86)\Common Files\Intel\Shared Files\cpp\bin\Intel64;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64_win\compiler;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32_win\compiler;C:\Python\Python310\Scripts\;C:\Python\Python310\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\BIN;C:\Program Files\Git\cmd;C:\Program Files\GitHub CLI\;C:\Program Files\7-Zip;C:\Program Files\NVIDIA Corporation\Nsight Compute 2020.1.1;C:\Program Files\dotnet\;C:\Program Files\CMake\bin;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\PowerShell\7\;C:\Program Files (x86)\FontForgeBuilds\bin;C:\Users\myusername\AppData\Local\Microsoft\WindowsApps;C:\Users\myusername\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\myusername\.dotnet\tools;C:\Program Files (x86)\FontForgeBuilds\bin\
PYTHONHOME: None
PYTHONPATH: None
sys.path: ['D:\\', 'C:\\Program Files (x86)\\FontForgeBuilds\\lib\\python310.zip', 'C:\\Program Files (x86)\\FontForgeBuilds\\lib\\python3.10', 'C:\\Program Files (x86)\\FontForgeBuilds\\lib\\python3.10\\lib-dynload', 'C:\\Program Files (x86)\\FontForgeBuilds\\bin', 'C:\\Program Files (x86)\\FontForgeBuilds\\lib\\python3.10\\site-packages']
Python 3.10.9 (main, Dec 10 2022, 09:16:22) [GCC 12.2.0 32 bit] 032bit on win32
<module 'fontforge' from 'C:\\Program Files (x86)\\FontForgeBuilds\\lib\\python3.10\\site-packages\\fontforge.pyd'>
['SpiroVersion', 'UnicodeAnnotationFromLib', 'UnicodeBlockCountFromLib', 'UnicodeBlockEndFromLib', 'UnicodeBlockNameFromLib', 'UnicodeBlockStartFromLib', 'UnicodeNameFromLib', 'UnicodeNames2FromLib', 'UnicodeNamesListVersion', '__date__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', 'activeFont', 'activeFontInUI', 'activeGlyph', 'activeLayer', 'ask', 'askChoices', 'askMulti', 'askString', 'awcontext', 'awglyph', 'configurePlugins', 'contour', 'cvt', 'defaultOtherSubrs', 'font', 'fonts', 'fontsInFile', 'getConvexNib', 'getPluginInfo', 'getPrefs', 'glyph', 'glyphPen', 'hasSpiro', 'hasUserInterface', 'hooks', 'layer', 'layer_array', 'layerinfo', 'layerinfo_array', 'loadEncodingFile', 'loadNamelist', 'loadNamelistDir', 'loadPlugins', 'loadPrefs', 'logWarning', 'math', 'mathKern', 'nameFromUnicode', 'onAppClosing', 'open', 'openFilename', 'parseTTInstrs', 'point', 'postError', 'postNotice', 'preloadCidmap', 'printSetup', 'private', 'readOtherSubrsFile', 'references', 'registerGlyphSeparationHook', 'registerImportExport', 'registerMenuItem', 'runInitScripts', 'saveFilename', 'savePrefs', 'scriptFromUnicode', 'scriptPath', 'selection', 'setConvexNib', 'setPrefs', 'spiroCorner', 'spiroG2', 'spiroG4', 'spiroLeft', 'spiroOpen', 'spiroRight', 'splineCorner', 'splineCurve', 'splineHVCurve', 'splineTangent', 'unParseTTInstrs', 'unicodeFromName', 'unitShape', 'unspecifiedMathValue', 'userConfigPath', 'version']
Done.
So I need to figure out why things are only working when my PWD is C:\Program Files (x86)\FontForgeBuilds\bin
. I don't understand why it won't work otherwise.
ffpython
and then attempting to import fontforge. Still get the error. Any other ideas? Am I missing something? – Strongroom