]> git.tdb.fi Git - libs/gl.git/blob - scripts/makefont.py
Better naming algorithm for objects in scene export
[libs/gl.git] / scripts / makefont.py
1 #!/usr/bin/python
2
3 caps = [ord(c) for c in "ABCDEFGHIKLMNOPRSTUVWXYZ"]
4 xchars = [ord(c) for c in "acemnorsuvwxz"]
5
6 def convert_def(fn):
7         src = file(fn)
8
9         result = ""
10         glyphs = {}
11         metrics = {}
12         kerning = []
13         codes = {}
14         cap_height = []
15         x_height = []
16         for line in src.readlines():
17                 line = line.strip()
18                 if not line or line[0]=='#':
19                         continue
20
21                 parts = line.split()
22                 values = map(int, parts[1:])
23
24                 if parts[0]=="font":
25                         tw, th, fh, fa, fd = map(int, parts[1:])
26                         result += "native_size %d;\n"%fh
27                         result += "ascent %.3f;\n"%(float(fa)/fh)
28                         result += "descent %.3f;\n"%(float(fd)/fh)
29                 elif parts[0]=="code":
30                         c, n = map(int, parts[1:])
31                         codes[c] = n
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)
38
39         for c, n in codes.iteritems():
40                 h = metrics[n][1]
41                 if c in caps:
42                         cap_height.append(h)
43                 elif c in xchars:
44                         x_height.append(h)
45
46         if cap_height:
47                 cap_height.sort()
48                 result += "cap_height %.3f;\n"%(float(cap_height[len(cap_height)*2/3])/fh)
49         if x_height:
50                 x_height.sort()
51                 result += "x_height %.3f;\n"%(float(x_height[len(x_height)*2/3])/fh)
52
53         for c, n in codes.iteritems():
54                 _, _, ox, oy, a = metrics[n]
55                 x, y, w, h, b = glyphs[n]
56                 result += "glyph %d\n{\n"%c
57                 result += "\ttexcoords %f %f %f %f;\n"%(float(x)/tw, float(th-y-h)/th, float(x+w)/tw, float(th-y)/th)
58                 result += "\tsize %.3f %.3f;\n"%(float(w)/fh, float(h)/fh)
59                 result += "\toffset %.3f %.3f;\n"%(float(ox-b)/fh, float(oy-b)/fh)
60                 result += "\tadvance %.3f;\n"%(float(a)/fh)
61                 result += "};\n"
62
63         for k in kerning:
64                 l, r, d = k
65                 result += "kerning %d %d %.3f;\n"%(l, r, float(d)/fh)
66
67         return result
68
69 def make_font(fn, size, ch_ranges, autohinter, margin, padding, distfield):
70         import maketex
71         import os
72
73         cmd = "ttf2png \"%s\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s %d -m %d -n %d -g"%(fn, size, margin, padding)
74         for r in ch_ranges:
75                 cmd += " -r %d,%d"%r
76         if autohinter:
77                 cmd += " -a"
78         if distfield:
79                 cmd += " -f %d"%distfield
80         if os.system(cmd):
81                 raise Exception("Could not execute ttf2png")
82
83         result = "texture\n{\n"
84         result += maketex.make_tex("makefont-tmp.png", wrap="CLAMP_TO_EDGE")
85         result += "};\n"
86         result += convert_def("makefont-tmp.def")
87
88         os.unlink("makefont-tmp.png")
89         os.unlink("makefont-tmp.def")
90
91         return result
92
93 def parse_range(s):
94         return tuple(map(int, s.split(',', 1)))
95
96 if __name__=="__main__":
97         import sys
98         import argparse
99
100         parser = argparse.ArgumentParser()
101         parser.add_argument("-s", "--size", default=10, type=int, metavar="NUM", help="Font size in pixels")
102         parser.add_argument("-a", "--autohinter", action="store_const", const=True, default=False, help="Force autohinter")
103         parser.add_argument("-m", "--margin", default=0, type=int, metavar="NUM", help="Margin around image edge in pixels")
104         parser.add_argument("-n", "--padding", default=1, type=int, metavar="NUM", help="Padding between glyphs in pixels")
105         parser.add_argument("-r", "--range", action="append", type=parse_range, metavar="START,END", help="Range of code points to include")
106         parser.add_argument("-f", "--distfield", default=0, type=int, metavar="NUM", help="Produce a distance field texture")
107         parser.add_argument("ttf", metavar="font.ttf", help="TrueType file to read")
108         parser.add_argument("outfile", help="MspGL font file to write")
109
110         args = parser.parse_args()
111
112         out = file(args.outfile, "w")
113         out.write(make_font(args.ttf, args.size, args.range, args.autohinter, args.margin, args.padding, args.distfield))