@Override public void create () { boolean flip = false; batch = new SpriteBatch(); if (flip) { OrthographicCamera cam = new OrthographicCamera(); cam.setToOrtho(flip); cam.update(); batch.setProjectionMatrix(cam.combined); } font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), flip); FileHandle fontFile = Gdx.files.internal("data/arial.ttf"); FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); FreeTypeFontParameter parameter = new FreeTypeFontParameter(); parameter.size = 15; parameter.flip = flip; parameter.genMipMaps = true; FreeTypeBitmapFontData fontData = generator.generateData(parameter); ftFont = generator.generateFont(parameter); generator.dispose(); }
/** * 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 * @param destiny */ private void generateFiles(String fontName, FileHandle fontFile, int fontSize, int pageWidth, int pageHeight, FileHandle destiny) { FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); PixmapPacker packer = new PixmapPacker(pageWidth, pageHeight, Pixmap.Format.RGBA8888, 2, false); FreeTypeFontParameter param = new FreeTypeFontParameter(); param.packer = packer; param.characters = FreeTypeFontGenerator.DEFAULT_CHARS; param.size = fontSize; param.flip = false; FreeTypeFontGenerator.FreeTypeBitmapFontData fontData = generator .generateData(param); saveFontToFile(fontData, fontSize, fontName, packer, destiny); generator.dispose(); packer.dispose(); }
private void saveFontToFile(FreeTypeBitmapFontData data, int fontSize, String fontName, PixmapPacker packer, FileHandle destiny) { FileHandle fontFile = Gdx.files.absolute(destiny.file() .getAbsolutePath() + File.separator + fontName + ".fnt"); // .fnt // path BitmapFontWriter.setOutputFormat(BitmapFontWriter.OutputFormat.Text); String[] pageRefs = BitmapFontWriter.writePixmaps(packer.getPages(), destiny, fontName); // png dir path Gdx.app.debug(TAG, String.format( "Saving font [%s]: fontfile: %s, pixmapDir: %s\n", fontName, fontFile, destiny)); BitmapFontWriter.writeFont(data, pageRefs, fontFile, new BitmapFontWriter.FontInfo(fontName, fontSize), 1, 1); }
@Override public BitmapFont loadSync (AssetManager manager, String fileName, FileHandle file, FreeTypeFontLoaderParameter parameter) { if(parameter == null) throw new RuntimeException("FreetypeFontParameter must be set in AssetManager#load to point at a TTF file!"); FreeTypeFontGenerator generator = manager.get(parameter.fontFileName + ".gen", FreeTypeFontGenerator.class); FreeTypeBitmapFontData data = generator.generateData(parameter.fontParameters); BitmapFont font = new BitmapFont(data, data.getTextureRegions(), false); font.setOwnsTexture(true); return font; }
@Override public BitmapFont loadSync( AssetManager manager, String fileName, FileHandle file, BitmapFontParameter parameter ) { int fontSize = 10; String scrubbedFileName = fileName; int qMark = fileName.lastIndexOf( "?" ); if ( qMark != -1 ) { scrubbedFileName = scrubbedFileName.substring( 0, qMark ); Matcher m = argsPtn.matcher( fileName ); m.region( qMark+1, fileName.length() ); while ( m.lookingAt() ) { if ( m.group( 1 ).equals( "size" ) ) { fontSize = Integer.parseInt( m.group( 2 ) ); } if ( m.group( 3 ).length() > 0 ) break; // Hit the "#" separator. m.region( m.end(), fileName.length() ); } } boolean flip = parameter != null ? parameter.flip : false; FreeTypeFontGenerator generator = new FreeTypeFontGenerator( file ); FreeTypeBitmapFontData data = generator.generateData( fontSize, FreeTypeFontGenerator.DEFAULT_CHARS, flip ); generator.dispose(); return new BitmapFont( data, data.getTextureRegions(), true ); }