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