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 = tuple(map(int, parts[1:]))
25 tw, th, fh, fa, fd = map(int, parts[1:])
26 result += "native_size {};\n".format(fh)
27 result += "ascent {:.3f};\n".format(float(fa)/fh)
28 result += "descent {:.3f};\n".format(float(fd)/fh)
29 elif parts[0]=="code":
30 c, n = tuple(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.items():
48 result += "cap_height {:.3f};\n".format(float(cap_height[len(cap_height)*2//3])/fh)
51 result += "x_height {:.3f};\n".format(float(x_height[len(x_height)*2//3])/fh)
53 for c, n in codes.items():
54 _, _, ox, oy, a = metrics[n]
55 x, y, w, h, b = glyphs[n]
56 result += "glyph {}\n{{\n".format(c)
57 result += "\ttexcoords {} {} {} {};\n".format(float(x)/tw, float(th-y-h)/th, float(x+w)/tw, float(th-y)/th)
58 result += "\tsize {:.3f} {:.3f};\n".format(float(w)/fh, float(h)/fh)
59 result += "\toffset {:.3f} {:.3f};\n".format(float(ox-b)/fh, float(oy-b)/fh)
60 result += "\tadvance {:.3f};\n".format(float(a)/fh)
65 result += "kerning {} {} {:.3f};\n".format(l, r, float(d)/fh)
69 def make_font(fn, size, ch_ranges, autohinter, margin, padding, distfield):
73 cmd = "ttf2png \"{}\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s {} -m {} -n {} -g".format(fn, size, margin, padding)
76 cmd += " -r {},{}".format(*r)
80 cmd += " -f {}".format(distfield)
82 raise Exception("Could not execute ttf2png")
84 result = "texture\n{\n"
85 result += maketex.make_tex("makefont-tmp.png")
87 result += convert_def("makefont-tmp.def")
89 os.unlink("makefont-tmp.png")
90 os.unlink("makefont-tmp.def")
95 return tuple(map(int, s.split(',', 1)))
97 if __name__=="__main__":
101 parser = argparse.ArgumentParser()
102 parser.add_argument("-s", "--size", default=10, type=int, metavar="NUM", help="Font size in pixels")
103 parser.add_argument("-a", "--autohinter", action="store_const", const=True, default=False, help="Force autohinter")
104 parser.add_argument("-m", "--margin", default=0, type=int, metavar="NUM", help="Margin around image edge in pixels")
105 parser.add_argument("-n", "--padding", default=1, type=int, metavar="NUM", help="Padding between glyphs in pixels")
106 parser.add_argument("-r", "--range", action="append", type=parse_range, metavar="START,END", help="Range of code points to include")
107 parser.add_argument("-f", "--distfield", default=0, type=int, metavar="NUM", help="Produce a distance field texture")
108 parser.add_argument("ttf", metavar="font.ttf", help="TrueType file to read")
109 parser.add_argument("outfile", help="MspGL font file to write")
111 args = parser.parse_args()
113 out = open(args.outfile, "w")
114 out.write(make_font(args.ttf, args.size, args.range, args.autohinter, args.margin, args.padding, args.distfield))