3 caps = [ord(c) for c in "ABCDEFGHIKLMNOPRSTUVWXYZ"]
4 xchars = [ord(c) for c in "acemnorsuvwxz"]
16 for line in src.readlines():
18 if not line or line[0]=='#':
22 values = map(int, parts[1:])
25 tw, th, fh, fa, fd = map(int, parts[1:])
26 result += "native_size %d;\n"%fh
27 result += "ascent %.3f;\n"%(float(fa)/fh)
28 result += "descent %.3f;\n"%(float(fd)/fh)
29 elif parts[0]=="code":
30 c, n = map(int, parts[1:])
32 elif parts[0]=="metrics":
33 metrics[values[0]] = values[1:]
34 elif parts[0]=="glyph":
35 glyphs[values[0]] = values[1:]
36 elif parts[0]=="kern":
37 kerning.append(values)
39 for c, n in codes.iteritems():
48 result += "cap_height %.3f;\n"%(float(cap_height[len(cap_height)*2/3])/fh)
51 result += "x_height %.3f;\n"%(float(x_height[len(x_height)*2/3])/fh)
53 for c, n in codes.iteritems():
54 _, _, ox, oy, a = metrics[n]
55 x, y, w, h, b = glyphs[n]
56 result += "glyph %d\n{\n"%c
57 result += "\ttexcoords %f %f %f %f;\n"%(float(x)/tw, float(th-y-h)/th, float(x+w)/tw, float(th-y)/th)
58 result += "\tsize %.3f %.3f;\n"%(float(w)/fh, float(h)/fh)
59 result += "\toffset %.3f %.3f;\n"%(float(ox-b)/fh, float(oy-b)/fh)
60 result += "\tadvance %.3f;\n"%(float(a)/fh)
65 result += "kerning %d %d %.3f;\n"%(l, r, float(d)/fh)
69 def make_font(fn, size, ch_ranges, autohinter, margin, padding, distfield):
73 cmd = "ttf2png \"%s\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s %d -m %d -n %d -g"%(fn, size, margin, padding)
79 cmd += " -f %d"%distfield
81 raise Exception("Could not execute ttf2png")
83 result = "texture\n{\n"
84 result += maketex.make_tex("makefont-tmp.png", wrap="CLAMP_TO_EDGE")
86 result += convert_def("makefont-tmp.def")
88 os.unlink("makefont-tmp.png")
89 os.unlink("makefont-tmp.def")
94 return tuple(map(int, s.split(',', 1)))
96 if __name__=="__main__":
100 parser = argparse.ArgumentParser()
101 parser.add_argument("-s", "--size", default=10, type=int, metavar="NUM", help="Font size in pixels")
102 parser.add_argument("-a", "--autohinter", action="store_const", const=True, default=False, help="Force autohinter")
103 parser.add_argument("-m", "--margin", default=0, type=int, metavar="NUM", help="Margin around image edge in pixels")
104 parser.add_argument("-n", "--padding", default=1, type=int, metavar="NUM", help="Padding between glyphs in pixels")
105 parser.add_argument("-r", "--range", action="append", type=parse_range, metavar="START,END", help="Range of code points to include")
106 parser.add_argument("-f", "--distfield", default=0, type=int, metavar="NUM", help="Produce a distance field texture")
107 parser.add_argument("ttf", metavar="font.ttf", help="TrueType file to read")
108 parser.add_argument("outfile", help="MspGL font file to write")
110 args = parser.parse_args()
112 out = file(args.outfile, "w")
113 out.write(make_font(args.ttf, args.size, args.range, args.autohinter, args.margin, args.padding, args.distfield))