Skip to content

Instantly share code, notes, and snippets.

@metaphore
Created October 29, 2017 19:19
Show Gist options
  • Save metaphore/7c6d7e486643cdcd853a9105351dfd83 to your computer and use it in GitHub Desktop.
Save metaphore/7c6d7e486643cdcd853a9105351dfd83 to your computer and use it in GitHub Desktop.
BitmapFont string render to an image file.
BitmapFont font = interfaceService.getSkin().getFont("default-font");
GlyphLayout glyphLayout = new GlyphLayout(font, getString("langZhTw"));
final int w = MathUtils.ceil(glyphLayout.width + 2f);
final int h = MathUtils.ceil(glyphLayout.height + 2f);
OrthographicCamera cam = new OrthographicCamera(w, h);
cam.setToOrtho(false, w, h);
cam.update();
Batch batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
FrameBuffer frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, w, h, false);
frameBuffer.begin();
Gdx.gl.glClearColor(1f, 1f, 1f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
font.draw(batch, glyphLayout, 1f, h-1f);
batch.end();
ByteBuffer buf;
Pixmap pixmap = new Pixmap(w, h, Pixmap.Format.RGBA8888);
buf = pixmap.getPixels();
Gdx.gl.glReadPixels(0, 0, w, h, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, buf);
frameBuffer.end();
PixmapIO.writePNG(Gdx.files.absolute("D:/zh-tw.png"), pixmap);
batch.dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment