X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=scripts%2Fmakefont.py;h=9a23957130da6920c50bff9cc06e25e63dc89a96;hb=89d3d10b75a42f86b224feb00b20283af66c0b01;hp=ad12fee828405860b91159401466ffc47b48eafd;hpb=4acc43935d3b19bae688e89ed42119e326a26f2d;p=libs%2Fgl.git diff --git a/scripts/makefont.py b/scripts/makefont.py index ad12fee8..9a239571 100755 --- a/scripts/makefont.py +++ b/scripts/makefont.py @@ -3,34 +3,44 @@ def convert_def(fn): src = file(fn) - line = src.readline() - tw, th, fh, fa, fd = map(int, line.split()) + for line in src.readlines(): + line = line.strip() + if not line or line[0]=='#': + continue - result = "native_size %d;\n"%fh - result += "ascent %.3f;\n"%(float(fa)/fh) - result += "descent %.3f;\n"%(float(fd)/fh) + parts = line.split() - for line in src.readlines(): - g, x, y, w, h, ox, oy, a = map(int, line.split()) - 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 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) + 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" + elif parts[0]=="kern": + l, r, d = map(int, parts[1:]) + result += "kerning %d %d %.3f;\n"%(l, r, float(d)/fh) return result -def make_font(fn, size): +def make_font(fn, size, autohinter, margin, padding): import maketex import os - if os.system("ttf2png \"%s\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s %d"%(fn, size)): + cmd = "ttf2png \"%s\" -o makefont-tmp.png -d makefont-tmp.def -t -p -s %d -m %d -n %d"%(fn, size, margin, padding) + if autohinter: + cmd += " -a" + if os.system(cmd): raise Exception("Could not execute ttf2png") result = "texture\n{\n" - result += "wrap CLAMP_TO_EDGE;\n" - result += maketex.make_tex("makefont-tmp.png") + result += maketex.make_tex("makefont-tmp.png", wrap="CLAMP_TO_EDGE") result += "};\n" result += convert_def("makefont-tmp.def") @@ -41,10 +51,17 @@ def make_font(fn, size): if __name__=="__main__": import sys + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument("-s", "--size", default=10, type=int, metavar="NUM", help="Font size in pixels") + parser.add_argument("-a", "--autohinter", action="store_const", const=True, default=False, help="Force autohinter") + 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("ttf", metavar="font.ttf", help="TrueType file to read") + parser.add_argument("outfile", help="MspGL font file to write") + + args = parser.parse_args() - if len(sys.argv)<4: - import os - print "Usage: %s "%os.path.basename(sys.argv[0]) - else: - out = file(sys.argv[2], "w") - out.write(make_font(sys.argv[1], int(sys.argv[3]))) + out = file(args.outfile, "w") + out.write(make_font(args.ttf, args.size, args.autohinter, args.margin, args.padding))