]> git.tdb.fi Git - libs/gl.git/blobdiff - makefont.py
Support embedding textures inside font files
[libs/gl.git] / makefont.py
diff --git a/makefont.py b/makefont.py
new file mode 100755 (executable)
index 0000000..ac81663
--- /dev/null
@@ -0,0 +1,50 @@
+#!/usr/bin/python
+
+# $Id$
+
+def convert_def(fn):
+       src=file(fn)
+
+       line=src.readline()
+       tw,th,fh,fa,fd=map(int, line.split())
+
+       result="default_size %d;\n"%fh
+       result+="ascent %.3f;\n"%(float(fa)/fh)
+       result+="descent %.3f;\n"%(float(fd)/fh)
+
+       for line in src.readlines():
+               g,x,y,w,h,ox,oy,a=map(int, line.split())
+               result+="glyph %d\n{\n"%g
+               result+="\ttexcoords %f %f %f %f;\n"%(float(x)/tw, float(th-y-h)/th, float(x+w)/tw, float(th-y)/th)
+               result+="\tsize %.3f %.3f;\n"%(float(w)/fh, float(h)/fh)
+               result+="\toffset %.3f %.3f;\n"%(float(ox)/fh, float(oy)/fh)
+               result+="\tadvance %.3f;\n"%(float(a)/fh)
+               result+="};\n"
+
+       return result
+
+def make_font(fn):
+       import maketex
+       import os
+
+       if os.system("ttf2png \"%s\" -o makefont-tmp.png -d makefont-tmp.def -t -p"%sys.argv[1]):
+               raise Exception("Could not execute ttf2png")
+
+       result="texture_inline\n{\n"
+       result+=maketex.make_tex("makefont-tmp.png")
+       result+="};\n"
+       result+=convert_def("makefont-tmp.def")
+
+       os.unlink("makefont-tmp.png")
+       os.unlink("makefont-tmp.def")
+
+       return result
+
+if __name__=="__main__":
+       import sys
+
+       if len(sys.argv)<3:
+               print "Usage: %s <font.ttf> <outfile>"
+       else:
+               out=file(sys.argv[2], "w")
+               out.write(make_font(sys.argv[1]))