def dump_font(ttf): (face, indexes) = FontLoader.create_cairo_font_face_for_file("../../../binaries/data/tools/fontbuilder/fonts/%s" % ttf, 0, FontLoader.FT_LOAD_DEFAULT) mappings = [ (c, indexes(unichr(c))) for c in range(1, 65535) ] print ttf, print ' '.join(str(c) for (c, g) in mappings if g != 0)
def dump_font(ttf): (face, indexes) = FontLoader.create_cairo_font_face_for_file( "../../../binaries/data/tools/fontbuilder/fonts/%s" % ttf, 0, FontLoader.FT_LOAD_DEFAULT) mappings = [(c, indexes(unichr(c))) for c in range(1, 65535)] print ttf, print ' '.join(str(c) for (c, g) in mappings if g != 0)
def generate_font(chars, outname, ttf, loadopts, size, renderstyle): (face, indexes) = FontLoader.create_cairo_font_face_for_file(ttf, 0, loadopts) (ctx, _) = setup_context(1, 1, face, size, renderstyle) (ascent, descent, linespacing, _, _) = ctx.font_extents() # Estimate the 'average' height of text, for vertical center alignment charheight = round(ctx.glyph_extents([(indexes("I"), 0.0, 0.0)])[3]) # Translate all the characters into glyphs # (This is inefficient if multiple characters have the same glyph) glyphs = [] for c in chars: idx = indexes(c) if ord(c) == 0xFFFD and idx == 0: # use "?" if the missing-glyph glyph is missing idx = indexes("?") if idx: glyphs.append(Glyph(ctx, renderstyle, c, idx)) # Sort by decreasing height (tie-break on decreasing width) glyphs.sort(key = lambda g: (-g.h, -g.w)) # Try various sizes to pack the glyphs into sizes = [] for h in [32, 64, 128, 256, 512, 1024]: for w in [32, 64, 128, 256, 512, 1024]: sizes.append((w, h)) sizes.sort(key = lambda (w, h): (w*h, max(w, h))) # prefer smaller and squarer for w, h in sizes: try: #packer = Packer.DumbRectanglePacker(w, h) packer = Packer.CygonRectanglePacker(w, h) for g in glyphs: g.pack(packer) except Packer.OutOfSpaceError: continue ctx, surface = setup_context(w, h, face, size, renderstyle) for g in glyphs: g.render(ctx) surface.write_to_png("%s.png" % outname) # Output the .fnt file with all the glyph positions etc fnt = open("%s.fnt" % outname, "w") fnt.write("101\n") fnt.write("%d %d\n" % (w, h)) fnt.write("%s\n" % ("rgba" if "colour" in renderstyle else "a")) fnt.write("%d\n" % len(glyphs)) fnt.write("%d\n" % linespacing) fnt.write("%d\n" % charheight) glyphs.sort(key = lambda g: ord(g.char)) for g in glyphs: x0 = g.x0 y0 = g.y0 # UGLY HACK: see http://trac.wildfiregames.com/ticket/1039 ; # to handle a-macron-acute characters without the hassle of # doing proper OpenType GPOS layout (which the Pagella font # doesn't support anyway), we'll just shift the combining acute # glyph by an arbitrary amount to make it roughly the right # place when used after an a-macron glyph. if ord(g.char) == 0x0301: y0 += charheight/3 fnt.write("%d %d %d %d %d %d %d %d\n" % (ord(g.char), g.pos.x, h-g.pos.y, g.w, g.h, -x0, y0, g.xadvance)) fnt.close() return print "Failed to fit glyphs in texture"
def generate_font(outname, ttfNames, loadopts, size, renderstyle, dsizes): faceList = [] indexList = [] for i in range(len(ttfNames)): (face, indices) = FontLoader.create_cairo_font_face_for_file("../../../binaries/data/tools/fontbuilder/fonts/%s" % ttfNames[i], 0, loadopts) faceList.append(face) if not ttfNames[i] in dsizes: dsizes[ttfNames[i]] = 0 indexList.append(indices) (ctx, _) = setup_context(1, 1, renderstyle) # TODO this gets the line height from the default font # while entire texts can be in the fallback font ctx.set_font_face(faceList[0]); ctx.set_font_size(size + dsizes[ttfNames[0]]) (_, _, linespacing, _, _) = ctx.font_extents() # Estimate the 'average' height of text, for vertical center alignment charheight = round(ctx.glyph_extents([(indexList[0]("I"), 0.0, 0.0)])[3]) # Translate all the characters into glyphs # (This is inefficient if multiple characters have the same glyph) glyphs = [] #for c in chars: for c in range(0x20, 0xFFFD): for i in range(len(indexList)): idx = indexList[i](unichr(c)) if c == 0xFFFD and idx == 0: # use "?" if the missing-glyph glyph is missing idx = indexList[i]("?") if idx: glyphs.append(Glyph(ctx, renderstyle, unichr(c), idx, faceList[i], size + dsizes[ttfNames[i]])) break # Sort by decreasing height (tie-break on decreasing width) glyphs.sort(key = lambda g: (-g.h, -g.w)) # Try various sizes to pack the glyphs into sizes = [] for h in [32, 64, 128, 256, 512, 1024, 2048, 4096]: sizes.append((h, h)) sizes.append((h*2, h)) sizes.sort(key = lambda (w, h): (w*h, max(w, h))) # prefer smaller and squarer for w, h in sizes: try: # Using the dump pacher usually creates bigger textures, but runs faster # In practice the size difference is so small it always ends up in the same size packer = Packer.DumbRectanglePacker(w, h) #packer = Packer.CygonRectanglePacker(w, h) for g in glyphs: g.pack(packer) except Packer.OutOfSpaceError: continue ctx, surface = setup_context(w, h, renderstyle) for g in glyphs: g.render(ctx) surface.write_to_png("%s.png" % outname) # Output the .fnt file with all the glyph positions etc fnt = open("%s.fnt" % outname, "w") fnt.write("101\n") fnt.write("%d %d\n" % (w, h)) fnt.write("%s\n" % ("rgba" if "colour" in renderstyle else "a")) fnt.write("%d\n" % len(glyphs)) fnt.write("%d\n" % linespacing) fnt.write("%d\n" % charheight) # sorting unneeded, as glyphs are added in increasing order #glyphs.sort(key = lambda g: ord(g.char)) for g in glyphs: x0 = g.x0 y0 = g.y0 # UGLY HACK: see http://trac.wildfiregames.com/ticket/1039 ; # to handle a-macron-acute characters without the hassle of # doing proper OpenType GPOS layout (which the font # doesn't support anyway), we'll just shift the combining acute # glyph by an arbitrary amount to make it roughly the right # place when used after an a-macron glyph. if ord(g.char) == 0x0301: y0 += charheight/3 fnt.write("%d %d %d %d %d %d %d %d\n" % (ord(g.char), g.pos.x, h-g.pos.y, g.w, g.h, -x0, y0, g.xadvance)) fnt.close() return print "Failed to fit glyphs in texture"
def generate_font(chars, outname, ttf, loadopts, size, renderstyle): (face, indexes) = FontLoader.create_cairo_font_face_for_file(ttf, 0, loadopts) (ctx, _) = setup_context(1, 1, face, size, renderstyle) (ascent, descent, linespacing, _, _) = ctx.font_extents() # Estimate the 'average' height of text, for vertical center alignment charheight = round(ctx.glyph_extents([(indexes("I"), 0.0, 0.0)])[3]) # Translate all the characters into glyphs # (This is inefficient if multiple characters have the same glyph) glyphs = [] for c in chars: idx = indexes(c) if ord( c ) == 0xFFFD and idx == 0: # use "?" if the missing-glyph glyph is missing idx = indexes("?") if idx: glyphs.append(Glyph(ctx, renderstyle, c, idx)) # Sort by decreasing height (tie-break on decreasing width) glyphs.sort(key=lambda g: (-g.h, -g.w)) # Try various sizes to pack the glyphs into sizes = [] for h in [32, 64, 128, 256, 512, 1024]: for w in [32, 64, 128, 256, 512, 1024]: sizes.append((w, h)) sizes.sort(key=lambda (w, h): (w * h, max(w, h))) # prefer smaller and squarer for w, h in sizes: try: #packer = Packer.DumbRectanglePacker(w, h) packer = Packer.CygonRectanglePacker(w, h) for g in glyphs: g.pack(packer) except Packer.OutOfSpaceError: continue ctx, surface = setup_context(w, h, face, size, renderstyle) for g in glyphs: g.render(ctx) surface.write_to_png("%s.png" % outname) # Output the .fnt file with all the glyph positions etc fnt = open("%s.fnt" % outname, "w") fnt.write("101\n") fnt.write("%d %d\n" % (w, h)) fnt.write("%s\n" % ("rgba" if "colour" in renderstyle else "a")) fnt.write("%d\n" % len(glyphs)) fnt.write("%d\n" % linespacing) fnt.write("%d\n" % charheight) glyphs.sort(key=lambda g: ord(g.char)) for g in glyphs: x0 = g.x0 y0 = g.y0 # UGLY HACK: see http://trac.wildfiregames.com/ticket/1039 ; # to handle a-macron-acute characters without the hassle of # doing proper OpenType GPOS layout (which the Pagella font # doesn't support anyway), we'll just shift the combining acute # glyph by an arbitrary amount to make it roughly the right # place when used after an a-macron glyph. if ord(g.char) == 0x0301: y0 += charheight / 3 fnt.write("%d %d %d %d %d %d %d %d\n" % (ord( g.char), g.pos.x, h - g.pos.y, g.w, g.h, -x0, y0, g.xadvance)) fnt.close() return print "Failed to fit glyphs in texture"
def generate_font(outname, ttfNames, loadopts, size, renderstyle, dsizes): faceList = [] indexList = [] for i in range(len(ttfNames)): (face, indices) = FontLoader.create_cairo_font_face_for_file("../../../binaries/data/tools/fontbuilder/fonts/%s" % ttfNames[i], 0, loadopts) faceList.append(face) if not ttfNames[i] in dsizes: dsizes[ttfNames[i]] = 0 indexList.append(indices) (ctx, _) = setup_context(1, 1, renderstyle) # TODO this gets the line height from the default font # while entire texts can be in the fallback font ctx.set_font_face(faceList[0]); ctx.set_font_size(size + dsizes[ttfNames[0]]) (_, _, linespacing, _, _) = ctx.font_extents() # Estimate the 'average' height of text, for vertical center alignment charheight = round(ctx.glyph_extents([(indexList[0]("I"), 0.0, 0.0)])[3]) # Translate all the characters into glyphs # (This is inefficient if multiple characters have the same glyph) glyphs = [] #for c in chars: for c in range(0x20, 0xFFFE): for i in range(len(indexList)): idx = indexList[i](unichr(c)) if c == 0xFFFD and idx == 0: # use "?" if the missing-glyph glyph is missing idx = indexList[i]("?") if idx: glyphs.append(Glyph(ctx, renderstyle, unichr(c), idx, faceList[i], size + dsizes[ttfNames[i]])) break # Sort by decreasing height (tie-break on decreasing width) glyphs.sort(key = lambda g: (-g.h, -g.w)) # Try various sizes to pack the glyphs into sizes = [] for h in [32, 64, 128, 256, 512, 1024, 2048, 4096]: sizes.append((h, h)) sizes.append((h*2, h)) sizes.sort(key = lambda (w, h): (w*h, max(w, h))) # prefer smaller and squarer for w, h in sizes: try: # Using the dump pacher usually creates bigger textures, but runs faster # In practice the size difference is so small it always ends up in the same size packer = Packer.DumbRectanglePacker(w, h) #packer = Packer.CygonRectanglePacker(w, h) for g in glyphs: g.pack(packer) except Packer.OutOfSpaceError: continue ctx, surface = setup_context(w, h, renderstyle) for g in glyphs: g.render(ctx) surface.write_to_png("%s.png" % outname) # Output the .fnt file with all the glyph positions etc fnt = open("%s.fnt" % outname, "w") fnt.write("101\n") fnt.write("%d %d\n" % (w, h)) fnt.write("%s\n" % ("rgba" if "colour" in renderstyle else "a")) fnt.write("%d\n" % len(glyphs)) fnt.write("%d\n" % linespacing) fnt.write("%d\n" % charheight) # sorting unneeded, as glyphs are added in increasing order #glyphs.sort(key = lambda g: ord(g.char)) for g in glyphs: x0 = g.x0 y0 = g.y0 # UGLY HACK: see http://trac.wildfiregames.com/ticket/1039 ; # to handle a-macron-acute characters without the hassle of # doing proper OpenType GPOS layout (which the font # doesn't support anyway), we'll just shift the combining acute # glyph by an arbitrary amount to make it roughly the right # place when used after an a-macron glyph. if ord(g.char) == 0x0301: y0 += charheight/3 fnt.write("%d %d %d %d %d %d %d %d\n" % (ord(g.char), g.pos.x, h-g.pos.y, g.w, g.h, -x0, y0, g.xadvance)) fnt.close() return print "Failed to fit glyphs in texture"