bitmap2ttf
bitmap2ttf copied to clipboard
respecting bbox? (and BDF to TTF converter)
I'm finding a way that can respecting bbox since fontforge will trim the bitmap.
bdftottf.py:
try:
from PIL.BdfFontFile import *
except ImportError:
from BdfFontFile import *
class BdfFontFileUnicode(FontFile.FontFile):
def __init__(self, fp):
FontFile.FontFile.__init__(self)
s = fp.readline()
if s[:13] != "STARTFONT 2.1":
raise SyntaxError, "not a valid BDF file"
props = {}
comments = []
while 1:
s = fp.readline()
if not s or s[:13] == "ENDPROPERTIES":
break
i = string.find(s, " ")
props[s[:i]] = s[i+1:-1]
if s[:i] in ["COMMENT", "COPYRIGHT"]:
if string.find(s, "LogicalFontDescription") < 0:
comments.append(s[i+1:-1])
font = string.split(props["FONT"], "-")
font[4] = bdf_slant[string.upper(font[4])]
font[11] = bdf_spacing[string.upper(font[11])]
ascent = int(props["FONT_ASCENT"])
descent = int(props["FONT_DESCENT"])
fontname = string.join(font[1:], ";")
# print "#", fontname
# for i in comments:
# print "#", i
self.glyph = {}
font = []
while 1:
c = bdf_char(fp)
if not c:
break
id, ch, (xy, dst, src), im = c
if ch >= 0: # and ch < len(self.glyph):
self.glyph[ch] = xy, dst, src, im
if __name__ == '__main__':
import sys
from convert import convert
for filename in sys.argv[1:]:
p = BdfFontFileUnicode(file(filename))
glyphmap = {}
for k,v in p.glyph.iteritems():
print k,v
glyphmap[k] = v[3]
convert(glyphmap, filename[:-4])