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