| 1 |
#!/usr/bin/fontforge -script |
| 2 |
from __future__ import print_function |
| 3 |
""" |
| 4 |
For use on Metafont fonts. |
| 5 |
To import glyphs each in individual .eps files into an empty font file. |
| 6 |
|
| 7 |
First, process with 'mpost'. Procedure is: |
| 8 |
|
| 9 |
1) Make sure you have an mfplain mem file for mpost. It may come with |
| 10 |
the distro, but it is possible also to generate it. |
| 11 |
|
| 12 |
I found an mfplain.mp file somewhere. Use it to make an mfplain.mem. |
| 13 |
mpost -ini '\input mfplain.mp; dump' |
| 14 |
|
| 15 |
2) Generate .eps files from a .mf file such as skt10.mf |
| 16 |
mpost '&./mfplain \mag=1; truecorners:=0; filenametemplate "%j-%4c.eps"; input skt10.mf' |
| 17 |
|
| 18 |
A bunch of eps files should result, with names like |
| 19 |
skt10-012.eps |
| 20 |
3) Use FontForge to make an empty font file, with a name like SKT.sfd |
| 21 |
4) Run this script on the eps files like so |
| 22 |
freefont/tools/metafont/bulk_eps_import.py SKT.sfd skt10 |
| 23 |
|
| 24 |
Then clean up clean up clean up. |
| 25 |
""" |
| 26 |
__author__ = "Stevan White" |
| 27 |
__email__ = "[email protected]" |
| 28 |
__copyright__ = "Copyright 2008, 2011, 2018 Stevan White" |
| 29 |
__date__ = "$Date: 2018-06-03 22:28:38 +0200 (So, 03. Jun 2018) $" |
| 30 |
__version__ = "$Revision: 3924 $" |
| 31 |
|
| 32 |
import fontforge |
| 33 |
import sys, os |
| 34 |
import fnmatch, re |
| 35 |
|
| 36 |
problem = False |
| 37 |
|
| 38 |
def import_glyph( font, name, chrnum ): |
| 39 |
print( "importing file:", name, "to slot", chrnum ) |
| 40 |
|
| 41 |
g = font.createChar( chrnum ) |
| 42 |
|
| 43 |
print( "importing outlines", name ) |
| 44 |
g.importOutlines( name ) |
| 45 |
# The glyphs produced by MetaPost usually have a grid, whose |
| 46 |
# right side seems to correspond to the proper right side bearing |
| 47 |
xmax = g.layers[1].boundingBox()[2] |
| 48 |
g.right_side_bearing = max( xmax, 0 ) |
| 49 |
|
| 50 |
scriptname = sys.argv[0]; |
| 51 |
argc = len( sys.argv ) |
| 52 |
|
| 53 |
file_pat = r'^([A-Za-z0-9]*)-(\d{3,4}).eps$' |
| 54 |
file_pat = sys.argv[2] + r'-(\d{3,4}).eps$' |
| 55 |
re_file_pat = re.compile( file_pat ) |
| 56 |
|
| 57 |
if argc > 2: |
| 58 |
fontfilename = sys.argv[1] |
| 59 |
font = fontforge.open( fontfilename ) |
| 60 |
print( "bulk importing to font file:", fontfilename ) |
| 61 |
chrnum = 0 |
| 62 |
directories = os.listdir('.') |
| 63 |
directories.sort() |
| 64 |
|
| 65 |
for glyphfile in directories: |
| 66 |
matches = re_file_pat.match( glyphfile ) |
| 67 |
if matches: |
| 68 |
print( "doing glyph", glyphfile ) |
| 69 |
chrnum = int( matches.group(1) ) |
| 70 |
import_glyph( font, glyphfile, chrnum ) |
| 71 |
print( "saving font in", fontfilename ) |
| 72 |
font.save() |
| 73 |
font.close() |
| 74 |
|
| 75 |
sys.exit( int( problem ) ) |