| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
import fontforge |
| 6 |
import glob |
| 7 |
import subprocess |
| 8 |
import tempfile |
| 9 |
import shutil as sh |
| 10 |
|
| 11 |
def run_mpost(file, tempdir): |
| 12 |
subprocess.call( |
| 13 |
['mpost', |
| 14 |
'&mfplain', |
| 15 |
'\mode=localfont;', |
| 16 |
'scale_factor:=100.375;', |
| 17 |
'outputtemplate:="%c.eps";', |
| 18 |
'input %s;' % file, |
| 19 |
'bye'], |
| 20 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 21 |
cwd=tempdir, |
| 22 |
) |
| 23 |
|
| 24 |
def import_glyphs(font, instance, tempdir): |
| 25 |
print "Importing glyph variants set '%s'" % instance |
| 26 |
|
| 27 |
glyph_files = glob.glob(os.path.join(tempdir, "*.eps")) |
| 28 |
|
| 29 |
for file in glyph_files: |
| 30 |
code = int(os.path.splitext(os.path.basename(file))[0]) |
| 31 |
char = unichr(code) |
| 32 |
|
| 33 |
if char.isupper() and int(instance) > 15: |
| 34 |
continue |
| 35 |
|
| 36 |
if not char.isupper() and not char.islower() and int(instance) > 7: |
| 37 |
continue |
| 38 |
|
| 39 |
if instance == "0": |
| 40 |
glyph = font.createChar(code) |
| 41 |
else: |
| 42 |
glyph = font.createChar(-1, font[code].glyphname+"."+instance) |
| 43 |
|
| 44 |
glyph.importOutlines(file, ("toobigwarn", "correctdir", "handle_eraser")) |
| 45 |
|
| 46 |
def do_instances(font, instances, mpfile, tempdir): |
| 47 |
for instance in range(instances): |
| 48 |
instance = str(instance) |
| 49 |
instance_dir = os.path.join(tempdir, instance) |
| 50 |
os.mkdir (instance_dir) |
| 51 |
run_mpost (mpfile, instance_dir) |
| 52 |
import_glyphs(font, instance, instance_dir) |
| 53 |
|
| 54 |
def get_alt(code, name): |
| 55 |
instances = 8 |
| 56 |
alt = () |
| 57 |
char = unichr(code) |
| 58 |
|
| 59 |
if char.islower(): |
| 60 |
instances = 32 |
| 61 |
elif char.isupper(): |
| 62 |
instances = 16 |
| 63 |
|
| 64 |
for i in range(1,instances): |
| 65 |
alt = alt + ("%s.%d" %(name, i),) |
| 66 |
|
| 67 |
return alt |
| 68 |
|
| 69 |
def add_gsub(font, instances): |
| 70 |
print "Adding glyph substitution rules..." |
| 71 |
|
| 72 |
font.addLookup( |
| 73 |
"Randomize lookup", |
| 74 |
"gsub_alternate", |
| 75 |
(), |
| 76 |
( |
| 77 |
('rand', |
| 78 |
( |
| 79 |
('DFLT', ('dflt',)), |
| 80 |
('grek', ('dflt',)), |
| 81 |
('latn', ('dflt',)) |
| 82 |
) |
| 83 |
), |
| 84 |
)) |
| 85 |
font.addLookupSubtable("Randomize lookup", "Randomize subtable") |
| 86 |
|
| 87 |
for glyph in font.glyphs(): |
| 88 |
if glyph.unicode != -1: |
| 89 |
glyph.addPosSub("Randomize subtable", |
| 90 |
get_alt(glyph.unicode, glyph.glyphname)) |
| 91 |
|
| 92 |
def greek_caps(font, instances): |
| 93 |
print "Adding missing Greek capitals..." |
| 94 |
|
| 95 |
caps = { |
| 96 |
"Alpha" : "A", |
| 97 |
"Beta" : "B", |
| 98 |
"Epsilon": "E", |
| 99 |
"Zeta" : "Z", |
| 100 |
"Eta" : "H", |
| 101 |
"Iota" : "I", |
| 102 |
"Kappa" : "K", |
| 103 |
"Mu" : "M", |
| 104 |
"Nu" : "N", |
| 105 |
"Omicron": "O", |
| 106 |
"Rho" : "P", |
| 107 |
"Tau" : "T", |
| 108 |
"Chi" : "X" |
| 109 |
} |
| 110 |
for c in caps: |
| 111 |
name = caps[c] |
| 112 |
glyph = font.createChar(-1, c) |
| 113 |
glyph.addReference(name) |
| 114 |
glyph.useRefsMetrics(name) |
| 115 |
glyph.unlinkRef() |
| 116 |
glyph.addPosSub("Randomize subtable", |
| 117 |
get_alt(font.createMappedChar(name).unicode, name)) |
| 118 |
|
| 119 |
def autowidth(font): |
| 120 |
print "Auto spacing..." |
| 121 |
|
| 122 |
font.selection.all() |
| 123 |
if font.fullname.find("Slanted"): |
| 124 |
font.autoWidth(40, 0, 20) |
| 125 |
else: |
| 126 |
font.autoWidth(70, 10, 40) |
| 127 |
font.round() # this one is needed to make simplify more reliable |
| 128 |
font.simplify() |
| 129 |
font.removeOverlap() |
| 130 |
font.round() |
| 131 |
font.autoHint() |
| 132 |
|
| 133 |
def autokern(font, instances): |
| 134 |
print "Auto kerning..." |
| 135 |
|
| 136 |
font.addLookup( |
| 137 |
"Kern lookup", |
| 138 |
"gpos_pair", |
| 139 |
(), |
| 140 |
( |
| 141 |
('kern', |
| 142 |
( |
| 143 |
('DFLT', ('dflt',)), |
| 144 |
('grek', ('dflt',)), |
| 145 |
('latn', ('dflt',)) |
| 146 |
) |
| 147 |
), |
| 148 |
)) |
| 149 |
font.addLookupSubtable("Kern lookup", "Kern subtable") |
| 150 |
|
| 151 |
list1 = ["A", "V", "a", "v", "W", "w", "o", "O", "T", "L", "Y", "l", "y"] |
| 152 |
list2 = [ ] |
| 153 |
|
| 154 |
for a in list1: |
| 155 |
list2.append(a) |
| 156 |
alt = get_alt(font.createMappedChar(a).unicode, a) |
| 157 |
for b in alt: |
| 158 |
list2.append(b) |
| 159 |
|
| 160 |
list1 = list2 |
| 161 |
|
| 162 |
font.autoKern("Kern subtable", 150, list1, list2, onlyCloser=True) |
| 163 |
|
| 164 |
def finalise(font): |
| 165 |
space = font.createChar(32) |
| 166 |
space.width = 400 |
| 167 |
|
| 168 |
def usage(): |
| 169 |
print "Usage: %s INFILE.mp OUTFILE.otf VERSION" % sys.argv[0] |
| 170 |
|
| 171 |
if __name__ == "__main__": |
| 172 |
if len(sys.argv) < 3: |
| 173 |
usage() |
| 174 |
sys.exit() |
| 175 |
|
| 176 |
infile = sys.argv[1] |
| 177 |
outfile = sys.argv[2] |
| 178 |
version = sys.argv[3] |
| 179 |
|
| 180 |
basefile = os.path.basename(infile) |
| 181 |
|
| 182 |
style = basefile.split("-")[1] |
| 183 |
style = os.path.splitext(style)[0].title() |
| 184 |
if style == "Boldslanted": |
| 185 |
style = "Bold Slanted" |
| 186 |
|
| 187 |
tempdir = tempfile.mkdtemp() |
| 188 |
mpfile = os.path.abspath(infile) |
| 189 |
instances = 32 |
| 190 |
|
| 191 |
font = fontforge.font() |
| 192 |
|
| 193 |
if style != "Regular": |
| 194 |
font.fontname = "PunkNova-%s" % style.replace(" ", "") |
| 195 |
font.fullname = "Punk Nova %s" % style |
| 196 |
else: |
| 197 |
font.fontname = "PunkNova" |
| 198 |
font.fullname = "Punk Nova" |
| 199 |
|
| 200 |
font.familyname = "Punk Nova" |
| 201 |
font.weight = style |
| 202 |
font.version = '00%.03f' %(float(version)/1000) |
| 203 |
font.encoding = "Unicode" |
| 204 |
|
| 205 |
font.copyright = "Unlimited copying and redistribution of this file are\ |
| 206 |
permitted as long as this file is not modified. Modifications are permitted,\ |
| 207 |
but only if the resulting file is not named '%s' and the (internal) fontname\ |
| 208 |
differs from '%s'." % (outfile, font.familyname) |
| 209 |
|
| 210 |
do_instances(font, instances, mpfile, tempdir) |
| 211 |
add_gsub (font, instances) |
| 212 |
greek_caps (font, instances) |
| 213 |
autowidth (font) |
| 214 |
autokern (font, instances) |
| 215 |
finalise (font) |
| 216 |
|
| 217 |
sh.rmtree (tempdir) |
| 218 |
|
| 219 |
print "Saving file '%s'..." % outfile |
| 220 |
# font.save() |
| 221 |
font.generate(outfile) |