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