From: Mikko Rasa Date: Sat, 5 May 2018 19:33:07 +0000 (+0300) Subject: Update the font script to match latest ttf2png X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=9ffa823328620968642ffbf9920265e1af0dc21e;p=libs%2Fgl.git Update the font script to match latest ttf2png --- diff --git a/scripts/makefont.py b/scripts/makefont.py index d24732ee..eb2c997a 100755 --- a/scripts/makefont.py +++ b/scripts/makefont.py @@ -1,13 +1,16 @@ #!/usr/bin/python -caps = list(range(ord("A"), ord("Z")+1)) +caps = [ord(c) for c in "ABCDEFGHIKLMNOPRSTUVWXYZ"] xchars = [ord(c) for c in "acemnorsuvwxz"] def convert_def(fn): src = file(fn) result = "" - header = "" + glyphs = {} + metrics = {} + kerning = [] + codes = {} cap_height = [] x_height = [] for line in src.readlines(): @@ -16,47 +19,64 @@ def convert_def(fn): continue parts = line.split() + values = map(int, parts[1:]) if parts[0]=="font": tw, th, fh, fa, fd = map(int, parts[1:]) - header += "native_size %d;\n"%fh - header += "ascent %.3f;\n"%(float(fa)/fh) - header += "descent %.3f;\n"%(float(fd)/fh) + result += "native_size %d;\n"%fh + result += "ascent %.3f;\n"%(float(fa)/fh) + result += "descent %.3f;\n"%(float(fd)/fh) + elif parts[0]=="code": + c, n = map(int, parts[1:]) + codes[c] = n + elif parts[0]=="metrics": + metrics[values[0]] = values[1:] elif parts[0]=="glyph": - g, x, y, w, h, ox, oy, a = map(int, parts[1:]) - result += "glyph %d\n{\n"%g - 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)/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) + glyphs[values[0]] = values[1:] elif parts[0]=="kern": - l, r, d = map(int, parts[1:]) - result += "kerning %d %d %.3f;\n"%(l, r, float(d)/fh) + kerning.append(values) + + for c, n in codes.iteritems(): + h = metrics[n][1] + if c in caps: + cap_height.append(h) + elif c in xchars: + x_height.append(h) if cap_height: cap_height.sort() - header += "cap_height %.3f;\n"%(float(cap_height[len(cap_height)*2/3])/fh) + result += "cap_height %.3f;\n"%(float(cap_height[len(cap_height)*2/3])/fh) if x_height: x_height.sort() - header += "x_height %.3f;\n"%(float(x_height[len(x_height)*2/3])/fh) + result += "x_height %.3f;\n"%(float(x_height[len(x_height)*2/3])/fh) + + for c, n in codes.iteritems(): + _, _, 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 += "};\n" + + for k in kerning: + l, r, d = k + result += "kerning %d %d %.3f;\n"%(l, r, float(d)/fh) - return header+result + return result -def make_font(fn, size, ch_range, autohinter, margin, padding): +def make_font(fn, size, ch_range, 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"%(fn, size, margin, padding) + cmd = "ttf2png \"%s\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s %d -m %d -n %d -g"%(fn, size, margin, padding) if ch_range: cmd += " -r %d,%d"%ch_range if autohinter: cmd += " -a" + if distfield: + cmd += " -f %d"%distfield if os.system(cmd): raise Exception("Could not execute ttf2png") @@ -83,10 +103,11 @@ if __name__=="__main__": parser.add_argument("-m", "--margin", default=0, type=int, metavar="NUM", help="Margin around image edge in pixels") parser.add_argument("-n", "--padding", default=1, type=int, metavar="NUM", help="Padding between glyphs in pixels") parser.add_argument("-r", "--range", type=parse_range, metavar="START,END", help="Range of code points to include") + parser.add_argument("-f", "--distfield", default=0, type=int, metavar="NUM", help="Produce a distance field texture") parser.add_argument("ttf", metavar="font.ttf", help="TrueType file to read") parser.add_argument("outfile", help="MspGL font file to write") args = parser.parse_args() out = file(args.outfile, "w") - out.write(make_font(args.ttf, args.size, args.range, args.autohinter, args.margin, args.padding)) + out.write(make_font(args.ttf, args.size, args.range, args.autohinter, args.margin, args.padding, args.distfield))