]> git.tdb.fi Git - libs/gl.git/blob - scripts/makefont.py
Consider expressions in array sizes when comparing types for equality
[libs/gl.git] / scripts / makefont.py
1 #!/usr/bin/python3
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 = open(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 = tuple(map(int, parts[1:]))
23
24                 if parts[0]=="font":
25                         tw, th, fh, fa, fd = map(int, parts[1:])
26                         result += "native_size {};\n".format(fh)
27                         result += "ascent {:.3f};\n".format(float(fa)/fh)
28                         result += "descent {:.3f};\n".format(float(fd)/fh)
29                 elif parts[0]=="code":
30                         c, n = tuple(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.items():
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".format(float(cap_height[len(cap_height)*2//3])/fh)
49         if x_height:
50                 x_height.sort()
51                 result += "x_height {:.3f};\n".format(float(x_height[len(x_height)*2//3])/fh)
52
53         for c, n in codes.items():
54                 _, _, ox, oy, a = metrics[n]
55                 x, y, w, h, b = glyphs[n]
56                 result += "glyph {}\n{{\n".format(c)
57                 result += "\ttexcoords {} {} {} {};\n".format(float(x)/tw, float(th-y-h)/th, float(x+w)/tw, float(th-y)/th)
58                 result += "\tsize {:.3f} {:.3f};\n".format(float(w)/fh, float(h)/fh)
59                 result += "\toffset {:.3f} {:.3f};\n".format(float(ox-b)/fh, float(oy-b)/fh)
60                 result += "\tadvance {:.3f};\n".format(float(a)/fh)
61                 result += "};\n"
62
63         for k in kerning:
64                 l, r, d = k
65                 result += "kerning {} {} {:.3f};\n".format(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 \"{}\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s {} -m {} -n {} -g".format(fn, size, margin, padding)
74         if ch_ranges:
75                 for r in ch_ranges:
76                         cmd += " -r {},{}".format(*r)
77         if autohinter:
78                 cmd += " -a"
79         if distfield:
80                 cmd += " -f {}".format(distfield)
81         if os.system(cmd):
82                 raise Exception("Could not execute ttf2png")
83
84         result = "texture\n{\n"
85         result += maketex.make_tex("makefont-tmp.png")
86         result += "};\n"
87         result += convert_def("makefont-tmp.def")
88
89         os.unlink("makefont-tmp.png")
90         os.unlink("makefont-tmp.def")
91
92         return result
93
94 def parse_range(s):
95         return tuple(map(int, s.split(',', 1)))
96
97 if __name__=="__main__":
98         import sys
99         import argparse
100
101         parser = argparse.ArgumentParser()
102         parser.add_argument("-s", "--size", default=10, type=int, metavar="NUM", help="Font size in pixels")
103         parser.add_argument("-a", "--autohinter", action="store_const", const=True, default=False, help="Force autohinter")
104         parser.add_argument("-m", "--margin", default=0, type=int, metavar="NUM", help="Margin around image edge in pixels")
105         parser.add_argument("-n", "--padding", default=1, type=int, metavar="NUM", help="Padding between glyphs in pixels")
106         parser.add_argument("-r", "--range", action="append", type=parse_range, metavar="START,END", help="Range of code points to include")
107         parser.add_argument("-f", "--distfield", default=0, type=int, metavar="NUM", help="Produce a distance field texture")
108         parser.add_argument("ttf", metavar="font.ttf", help="TrueType file to read")
109         parser.add_argument("outfile", help="MspGL font file to write")
110
111         args = parser.parse_args()
112
113         out = open(args.outfile, "w")
114         out.write(make_font(args.ttf, args.size, args.range, args.autohinter, args.margin, args.padding, args.distfield))