I have written a python script that reads a METAFONT file, runs METAPOST on it (which generates about 250 PostScript files), imports these PostScript files in FontForge and outputs an OpenType font. Here is a reduced version of this script:
import os,sys,fontforge,glob,subprocess,tempfile,shutil
if __name__ == "__main__":
mffile = os.path.abspath(sys.argv[1])
tempdir = tempfile.mkdtemp()
font = fontforge.font()
subprocess.call(
['mpost',
'&mfplain',
'\mode=localfont;',
'mag:=100.375;',
'outputtemplate:="%c.eps";',
'input %s;' % mffile,
'bye'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=tempdir)
glyph_files = glob.glob(os.path.join(tempdir, "*.eps"))
for eps in glyph_files:
code = int(os.path.splitext(os.path.basename(eps))[0])
glyph = font.createChar(code)
glyph.importOutlines(eps, ("toobigwarn", "correctdir"))
font.generate("font.otf")
shutil.rmtree(tempdir)
exit(0)
At the moment, the 250 PostScript files are stored in a temporary directory. In order to make things faster, I would like them to be stored in memory. Hence, tempdir should be a directory in the memory. I have experimented with MemoryFS (something like this
from fs.memoryfs import MemoryFS
mem = MemoryFS()
mem.makedir('temdpir')
) but I could not figure out how to get access to the files in tempdir such that
glyph_files = glob.glob(os.path.join(tempdir, "*.eps"))
works.
Do you have suggestions how to access these files? Thank you. (An alternative to MemoryFS is also welcome.)
Accoding to docs, the getsyspath is the way to get the OS-backed filepath. However, MemoryFS is not being backed by a real filesystem and doesn't override the default implementation of fs.base.FS and it will raise NoSysPathError.
A memory-backed filesystem implementation will depend on your OS. In a modern linux, for instance, you could mount a tmpfs partition, which will look like other folder in your virtual file system but the files will be stored in memory. Note, however, that if the system is running out of memory some pages of the tmpfs could be moved to swap area.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With