]> git.tdb.fi Git - libs/gl.git/blobdiff - scripts/makefont.py
Add cap height and x height metrics to Font
[libs/gl.git] / scripts / makefont.py
index 64c8e68459732ce964aa44290180ff4e961837d0..1d7a4332906a55e02dc0df2f8009648b4ee6408b 100755 (executable)
@@ -1,8 +1,15 @@
 #!/usr/bin/python
 
+caps = list(range(ord("A"), ord("Z")+1))
+xchars = [ord(c) for c in "acemnorsuvwxz"]
+
 def convert_def(fn):
        src = file(fn)
 
+       result = ""
+       header = ""
+       cap_height = []
+       x_height = []
        for line in src.readlines():
                line = line.strip()
                if not line or line[0]=='#':
@@ -12,9 +19,9 @@ def convert_def(fn):
 
                if parts[0]=="font":
                        tw, th, fh, fa, fd = map(int, parts[1:])
-                       result = "native_size %d;\n"%fh
-                       result += "ascent %.3f;\n"%(float(fa)/fh)
-                       result += "descent %.3f;\n"%(float(fd)/fh)
+                       header += "native_size %d;\n"%fh
+                       header += "ascent %.3f;\n"%(float(fa)/fh)
+                       header += "descent %.3f;\n"%(float(fd)/fh)
                elif parts[0]=="glyph":
                        g, x, y, w, h, ox, oy, a = map(int, parts[1:])
                        result += "glyph %d\n{\n"%g
@@ -23,11 +30,21 @@ def convert_def(fn):
                        result += "\toffset %.3f %.3f;\n"%(float(ox)/fh, float(oy)/fh)
                        result += "\tadvance %.3f;\n"%(float(a)/fh)
                        result += "};\n"
+
+                       if g in caps:
+                               cap_height.append(h)
+                       elif g in xchars:
+                               x_height.append(h)
                elif parts[0]=="kern":
                        l, r, d = map(int, parts[1:])
                        result += "kerning %d %d %.3f;\n"%(l, r, float(d)/fh)
 
-       return result
+       if cap_height:
+               header += "cap_height %.3f;\n"%(float(cap_height[len(cap_height)*2/3])/fh)
+       if x_height:
+               header += "x_height %.3f;\n"%(float(x_height[len(x_height)*2/3])/fh)
+
+       return header+result
 
 def make_font(fn, size, ch_range, autohinter, margin, padding):
        import maketex