]> git.tdb.fi Git - libs/gl.git/commitdiff
Convert the texture and font creation scripts to python3
authorMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2021 21:17:23 +0000 (00:17 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2021 21:52:13 +0000 (00:52 +0300)
scripts/makefont.py
scripts/maketex.py

index 24ae15a450bc50c4c4f3c37a356f533398c2ad19..aa6aa8393a6b0e30b4695c6075a4b6f75ae308c7 100755 (executable)
@@ -1,10 +1,10 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 caps = [ord(c) for c in "ABCDEFGHIKLMNOPRSTUVWXYZ"]
 xchars = [ord(c) for c in "acemnorsuvwxz"]
 
 def convert_def(fn):
-       src = file(fn)
+       src = open(fn)
 
        result = ""
        glyphs = {}
@@ -19,15 +19,15 @@ def convert_def(fn):
                        continue
 
                parts = line.split()
-               values = map(int, parts[1:])
+               values = tuple(map(int, parts[1:]))
 
                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)
+                       result += "native_size {};\n".format(fh)
+                       result += "ascent {:.3f};\n".format(float(fa)/fh)
+                       result += "descent {:.3f};\n".format(float(fd)/fh)
                elif parts[0]=="code":
-                       c, n = map(int, parts[1:])
+                       c, n = tuple(map(int, parts[1:]))
                        codes[c] = n
                elif parts[0]=="metrics":
                        metrics[values[0]] = values[1:]
@@ -36,7 +36,7 @@ def convert_def(fn):
                elif parts[0]=="kern":
                        kerning.append(values)
 
-       for c, n in codes.iteritems():
+       for c, n in codes.items():
                h = metrics[n][1]
                if c in caps:
                        cap_height.append(h)
@@ -45,24 +45,24 @@ def convert_def(fn):
 
        if cap_height:
                cap_height.sort()
-               result += "cap_height %.3f;\n"%(float(cap_height[len(cap_height)*2/3])/fh)
+               result += "cap_height {:.3f};\n".format(float(cap_height[len(cap_height)*2//3])/fh)
        if x_height:
                x_height.sort()
-               result += "x_height %.3f;\n"%(float(x_height[len(x_height)*2/3])/fh)
+               result += "x_height {:.3f};\n".format(float(x_height[len(x_height)*2//3])/fh)
 
-       for c, n in codes.iteritems():
+       for c, n in codes.items():
                _, _, ox, oy, a = metrics[n]
                x, y, w, h, b = glyphs[n]
-               result += "glyph %d\n{\n"%c
-               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-b)/fh, float(oy-b)/fh)
-               result += "\tadvance %.3f;\n"%(float(a)/fh)
+               result += "glyph {}\n{{\n".format(c)
+               result += "\ttexcoords {} {} {} {};\n".format(float(x)/tw, float(th-y-h)/th, float(x+w)/tw, float(th-y)/th)
+               result += "\tsize {:.3f} {:.3f};\n".format(float(w)/fh, float(h)/fh)
+               result += "\toffset {:.3f} {:.3f};\n".format(float(ox-b)/fh, float(oy-b)/fh)
+               result += "\tadvance {:.3f};\n".format(float(a)/fh)
                result += "};\n"
 
        for k in kerning:
                l, r, d = k
-               result += "kerning %d %d %.3f;\n"%(l, r, float(d)/fh)
+               result += "kerning {} {} {:.3f};\n".format(l, r, float(d)/fh)
 
        return result
 
@@ -70,13 +70,13 @@ def make_font(fn, size, ch_ranges, autohinter, margin, padding, distfield):
        import maketex
        import os
 
-       cmd = "ttf2png \"%s\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s %d -m %d -n %d -g"%(fn, size, margin, padding)
+       cmd = "ttf2png \"{}\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s {} -m {} -n {} -g".format(fn, size, margin, padding)
        for r in ch_ranges:
-               cmd += " -r %d,%d"%r
+               cmd += " -r {},{}".format(*r)
        if autohinter:
                cmd += " -a"
        if distfield:
-               cmd += " -f %d"%distfield
+               cmd += " -f {}".format(distfield)
        if os.system(cmd):
                raise Exception("Could not execute ttf2png")
 
@@ -109,5 +109,5 @@ if __name__=="__main__":
 
        args = parser.parse_args()
 
-       out = file(args.outfile, "w")
+       out = open(args.outfile, "w")
        out.write(make_font(args.ttf, args.size, args.range, args.autohinter, args.margin, args.padding, args.distfield))
index 520be8424b02f39740b663bd03fc83c736e05f54..916ce747c404f8b26210078a0e07b7d53f8b5fbf 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 def escape(str):
        result = ""
@@ -8,7 +8,7 @@ def escape(str):
                elif c=='\\':
                        result += '\\\\'
                elif ord(c)<0x20:
-                       result += "\\%03o"%ord(c)
+                       result += "\\{:03o}".format(ord(c))
                else:
                        result += c
        return result
@@ -27,14 +27,14 @@ def make_tex(fn, filter="LINEAR", anisotropy=0, wrap=None, srgb=False):
        if srgb:
                fmt = "S"+fmt
 
-       result = "storage %s %d %d;\n"%(fmt, img.size[0], img.size[1])
-       result += "filter %s;\n"%filter
+       result = "storage {} {} {};\n".format(fmt, img.size[0], img.size[1])
+       result += "filter {};\n".format(filter)
        if "MIPMAP" in filter:
                result += "generate_mipmap true;\n"
        if anisotropy:
-               result += "max_anisotropy %d;\n"%anisotropy
+               result += "max_anisotropy {};\n".format(anisotropy)
        if wrap:
-               result += "wrap %s;\n"%wrap
+               result += "wrap {};\n".format(wrap)
        result += "raw_data \""
        data = list(img.getdata())
        for y in range(img.size[1]):
@@ -68,5 +68,5 @@ if __name__=="__main__":
        out_fn = args.output
        if not out_fn:
                out_fn = os.path.splitext(args.image)[0]+".tex2d"
-       out = file(out_fn, "w")
+       out = open(out_fn, "w")
        out.write(make_tex(args.image, filter, args.anisotropy, args.wrap, args.srgb))