Last active
August 29, 2015 13:56
-
-
Save jrenner/9016568 to your computer and use it in GitHub Desktop.
write generated fonts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Convenience method for generating a font, and then writing the fnt and png files. | |
* Writing a generated font to files allows the possibility of only generating the fonts when they are missing, otherwise | |
* loading from a previously generated file. | |
* @param fontFile | |
* @param fontSize | |
*/ | |
private static BitmapFont generateFontWriteFiles(String fontName, FileHandle fontFile, int fontSize, int pageWidth, int pageHeight) { | |
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); | |
PixmapPacker packer = new PixmapPacker(pageWidth, pageHeight, Format.RGBA8888, 2, false); | |
FreeTypeBitmapFontData fontData = generator.generateData(fontSize, FreeTypeFontGenerator.DEFAULT_CHARS, false, packer); | |
Array<Page> pages = packer.getPages(); | |
TextureRegion[] texRegions = new TextureRegion[pages.size]; | |
for (int i=0; i<pages.size; i++) { | |
Page p = pages.get(i); | |
Texture tex = new Texture(new PixmapTextureData(p.getPixmap(), p.getPixmap().getFormat(), false, false, true)) { | |
@Override | |
public void dispose () { | |
super.dispose(); | |
getTextureData().consumePixmap().dispose(); | |
} | |
}; | |
tex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest); | |
texRegions[i] = new TextureRegion(tex); | |
} | |
BitmapFont font = new BitmapFont(fontData, texRegions, false); | |
saveFontToFile(font, fontSize, fontName, packer); | |
generator.dispose(); | |
packer.dispose(); | |
return font; | |
} | |
private static boolean saveFontToFile(BitmapFont font, int fontSize, String fontName, PixmapPacker packer) { | |
FileHandle fontFile = getFontFile(fontName + ".fnt"); // .fnt path | |
FileHandle pixmapDir = getFontFile(fontName); // png dir path | |
BitmapFontWriter.setOutputFormat(OutputFormat.Text); | |
String[] pageRefs = BitmapFontWriter.writePixmaps(packer.getPages(), pixmapDir, fontName); | |
Tools.log.debug(String.format("Saving font [%s]: fontfile: %s, pixmapDir: %s\n", fontName, fontFile, pixmapDir)); | |
// here we must add the png dir to the page refs | |
for (int i = 0; i < pageRefs.length; i++) { | |
pageRefs[i] = fontName + "/" + pageRefs[i]; | |
//Tools.log.debug("\tpageRef: " + pageRefs[i]); | |
} | |
BitmapFontWriter.writeFont(font.getData(), pageRefs, fontFile, new FontInfo(fontName, fontSize), 1, 1); | |
return true; | |
} | |
private static FileHandle getFontFile(String filename) { | |
return Gdx.files.local("generated-fonts/" + filename); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment