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