3 caps = list(range(ord("A"), ord("Z")+1))
4 xchars = [ord(c) for c in "acemnorsuvwxz"]
13 for line in src.readlines():
15 if not line or line[0]=='#':
21 tw, th, fh, fa, fd = map(int, parts[1:])
22 header += "native_size %d;\n"%fh
23 header += "ascent %.3f;\n"%(float(fa)/fh)
24 header += "descent %.3f;\n"%(float(fd)/fh)
25 elif parts[0]=="glyph":
26 g, x, y, w, h, ox, oy, a = map(int, parts[1:])
27 result += "glyph %d\n{\n"%g
28 result += "\ttexcoords %f %f %f %f;\n"%(float(x)/tw, float(th-y-h)/th, float(x+w)/tw, float(th-y)/th)
29 result += "\tsize %.3f %.3f;\n"%(float(w)/fh, float(h)/fh)
30 result += "\toffset %.3f %.3f;\n"%(float(ox)/fh, float(oy)/fh)
31 result += "\tadvance %.3f;\n"%(float(a)/fh)
38 elif parts[0]=="kern":
39 l, r, d = map(int, parts[1:])
40 result += "kerning %d %d %.3f;\n"%(l, r, float(d)/fh)
44 header += "cap_height %.3f;\n"%(float(cap_height[len(cap_height)*2/3])/fh)
47 header += "x_height %.3f;\n"%(float(x_height[len(x_height)*2/3])/fh)
51 def make_font(fn, size, ch_range, autohinter, margin, padding):
55 cmd = "ttf2png \"%s\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s %d -m %d -n %d"%(fn, size, margin, padding)
57 cmd += " -r %d,%d"%ch_range
61 raise Exception("Could not execute ttf2png")
63 result = "texture\n{\n"
64 result += maketex.make_tex("makefont-tmp.png", wrap="CLAMP_TO_EDGE")
66 result += convert_def("makefont-tmp.def")
68 os.unlink("makefont-tmp.png")
69 os.unlink("makefont-tmp.def")
74 return tuple(map(int, s.split(',', 1)))
76 if __name__=="__main__":
80 parser = argparse.ArgumentParser()
81 parser.add_argument("-s", "--size", default=10, type=int, metavar="NUM", help="Font size in pixels")
82 parser.add_argument("-a", "--autohinter", action="store_const", const=True, default=False, help="Force autohinter")
83 parser.add_argument("-m", "--margin", default=0, type=int, metavar="NUM", help="Margin around image edge in pixels")
84 parser.add_argument("-n", "--padding", default=1, type=int, metavar="NUM", help="Padding between glyphs in pixels")
85 parser.add_argument("-r", "--range", type=parse_range, metavar="START,END", help="Range of code points to include")
86 parser.add_argument("ttf", metavar="font.ttf", help="TrueType file to read")
87 parser.add_argument("outfile", help="MspGL font file to write")
89 args = parser.parse_args()
91 out = file(args.outfile, "w")
92 out.write(make_font(args.ttf, args.size, args.range, args.autohinter, args.margin, args.padding))