]> git.tdb.fi Git - libs/gl.git/blob - makefont.py
030adff88bf4aadfb73ad4e89ed5bad740782ed6
[libs/gl.git] / makefont.py
1 #!/usr/bin/python
2
3 # $Id$
4
5 def convert_def(fn):
6         src=file(fn)
7
8         line=src.readline()
9         tw,th,fh,fa,fd=map(int, line.split())
10
11         result="native_size %d;\n"%fh
12         result+="ascent %.3f;\n"%(float(fa)/fh)
13         result+="descent %.3f;\n"%(float(fd)/fh)
14
15         for line in src.readlines():
16                 g,x,y,w,h,ox,oy,a=map(int, line.split())
17                 result+="glyph %d\n{\n"%g
18                 result+="\ttexcoords %f %f %f %f;\n"%(float(x)/tw, float(th-y-h)/th, float(x+w)/tw, float(th-y)/th)
19                 result+="\tsize %.3f %.3f;\n"%(float(w)/fh, float(h)/fh)
20                 result+="\toffset %.3f %.3f;\n"%(float(ox)/fh, float(oy)/fh)
21                 result+="\tadvance %.3f;\n"%(float(a)/fh)
22                 result+="};\n"
23
24         return result
25
26 def make_font(fn, size):
27         import maketex
28         import os
29
30         if os.system("ttf2png \"%s\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s %d"%(fn, size)):
31                 raise Exception("Could not execute ttf2png")
32
33         result="texture\n{\n"
34         result+="wrap CLAMP_TO_EDGE;\n"
35         result+=maketex.make_tex("makefont-tmp.png")
36         result+="};\n"
37         result+=convert_def("makefont-tmp.def")
38
39         os.unlink("makefont-tmp.png")
40         os.unlink("makefont-tmp.def")
41
42         return result
43
44 if __name__=="__main__":
45         import sys
46
47         if len(sys.argv)<4:
48                 import os
49                 print "Usage: %s <font.ttf> <outfile> <size>"%os.path.basename(sys.argv[0])
50         else:
51                 out=file(sys.argv[2], "w")
52                 out.write(make_font(sys.argv[1], int(sys.argv[3])))