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 addMenuItem(self, itemText, itemDelegate): self.options.append(itemText) self.optionsDelegates.append(itemDelegate) #add the actual sprite. These are drawn automatically on the scene that's passed in. #there is no draw loop. option = spyral.Sprite(self.parentScene) textOption = FontLoader.GuidedText(self.parentScene, "game/fonts/DejaVuSans.ttf", "Test", self.height * 1. / 8) self.sprites.append(option) self.text.append(textOption) #quick check if you added only 1 item. if(len(self.options) != 1): option.image = spyral.Image("game/images/logo.png") else: option.image = spyral.Image("game/images/selectedMenuItem.png") #recalculate the position of everything on the screen. option.anchor = "center" option.x = self.width/2 textOption.x = self.width/2 #fix positions. print("Length: " + str(self.height/2)) curY = self.height/2 - ((len(self.options) - 1 )* self.sprites[0].height/2) for i in range(0, len(self.sprites)): print("CurY: " + str(curY)) self.sprites[i].y = curY self.text[i].y = curY curY = curY + self.sprites[i].height + 30
class LearningDataBuilder: def __init__(self, fontPath, fontSize, morseSize): self._morseSize = morseSize self._fontSize = fontSize self._fontLoader = FontLoader(fontPath, fontSize) self._morseCode = { 'A' : (0, 1), 'B' : (1, 0, 0 ,0), 'C' : (1, 0, 1, 0), 'D' : (1, 0 ,0), 'E' : (0,), 'F' : (0, 0, 1, 0), 'G' : (1, 1, 0), 'H' : (0, 0, 0, 0), 'I' : (0, 0), 'J' : (0, 1, 1, 0), 'K' : (1, 0, 1), 'L' : (0, 1, 0, 0), 'M' : (1, 1), 'N' : (1, 0), 'O' : (1, 1, 1), 'P' : (0, 1, 1, 0), 'Q' : (1, 1, 0, 1), 'R' : (0, 1, 0), 'S' : (0, 0, 0), 'T' : (1,), 'U' : (0, 0, 1), 'V' : (0, 0, 0, 1), 'W' : (0, 1 ,1), 'X' : (1, 0, 0, 1), 'Y' : (1, 0, 1, 1), 'Z' : (1, 1, 0, 0), } def getLearningData(self): letters = self._fontLoader.getLetters() data = self._initLearningData(letters, self._morseSize) self._fillLearningData(data, letters) return data def _initLearningData(self, letters, morseSize): return LearningData(self._fontSize*self._fontSize, morseSize) def _fillLearningData(self, learningData, letters): for letter in letters: if letter in self._morseCode and len(self._morseCode[letter]) == self._morseSize: learningData.addLetter(letters[letter], self._morseCode[letter])
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 __init__(self, fontPath, fontSize, morseSize): self._morseSize = morseSize self._fontSize = fontSize self._fontLoader = FontLoader(fontPath, fontSize) self._morseCode = { 'A' : (0, 1), 'B' : (1, 0, 0 ,0), 'C' : (1, 0, 1, 0), 'D' : (1, 0 ,0), 'E' : (0,), 'F' : (0, 0, 1, 0), 'G' : (1, 1, 0), 'H' : (0, 0, 0, 0), 'I' : (0, 0), 'J' : (0, 1, 1, 0), 'K' : (1, 0, 1), 'L' : (0, 1, 0, 0), 'M' : (1, 1), 'N' : (1, 0), 'O' : (1, 1, 1), 'P' : (0, 1, 1, 0), 'Q' : (1, 1, 0, 1), 'R' : (0, 1, 0), 'S' : (0, 0, 0), 'T' : (1,), 'U' : (0, 0, 1), 'V' : (0, 0, 0, 1), 'W' : (0, 1 ,1), 'X' : (1, 0, 0, 1), 'Y' : (1, 0, 1, 1), 'Z' : (1, 1, 0, 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"