Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.mantagames.lwjgl3engine.gl.font;
- import java.nio.ByteBuffer;
- import java.nio.IntBuffer;
- import java.util.ArrayList;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.stb.STBTTAlignedQuad;
- import org.lwjgl.stb.STBTTBakedChar;
- import org.lwjgl.stb.STBTTFontinfo;
- import org.lwjgl.stb.STBTruetype;
- import net.mantagames.lwjgl3engine.gl.Texture2D;
- import net.mantagames.lwjgl3engine.gl.mesh.BufferedMesh;
- import net.mantagames.lwjgl3engine.util.IOUtil;
- import net.mantagames.lwjgl3engine.util.MeshUtils;
- public class BitmapFont {
- private static final int BITMAP_W = 512;
- private static final int BITMAP_H = 512;
- private static final int CHAR_START = 32;
- private static final int CHAR_AMT = 96;
- private int ascent;
- private int descent;
- private int lineGap;
- private int size;
- private ArrayList<Glyph> glyphs;
- private ByteBuffer bitmap;
- public BitmapFont(String fontFile, int size) {
- this.size = size;
- this.glyphs = new ArrayList<Glyph>();
- STBTTBakedChar.Buffer cdata = STBTTBakedChar.malloc(CHAR_AMT);
- try {
- // Load the font
- ByteBuffer ttf = IOUtil.ioResourceToByteBuffer(fontFile, 160 * 1024);
- if ( ttf == null ) {
- System.err.println("Font not found");
- return;
- }
- // Generate font-bitmap data.
- bitmap = BufferUtils.createByteBuffer(BITMAP_W * BITMAP_H);
- org.lwjgl.stb.STBTruetype.stbtt_BakeFontBitmap(ttf, getFontHeight(), bitmap, BITMAP_W, BITMAP_H, CHAR_START, cdata);
- // Write to file
- writeToFile("Resources/test.png");
- // Get font metrics
- STBTTFontinfo fontInfo = STBTTFontinfo.create();
- if (!STBTruetype.stbtt_InitFont(fontInfo, ttf))
- throw new RuntimeException("Error initializing Font");
- float scale = STBTruetype.stbtt_ScaleForPixelHeight(fontInfo, size);
- IntBuffer ascent = BufferUtils.createIntBuffer(1);
- IntBuffer descent = BufferUtils.createIntBuffer(1);
- IntBuffer lineGap = BufferUtils.createIntBuffer(1);
- STBTruetype.stbtt_GetFontVMetrics(fontInfo, ascent, descent, lineGap);
- this.ascent = (int) (ascent.get(0) * scale);
- this.descent = (int) (descent.get(0) * scale);
- this.lineGap = lineGap.get(0);
- // Generate glyphs
- for (int i = CHAR_START; i < CHAR_START + CHAR_AMT; i++) {
- char c = (char) i;
- Glyph g = new Glyph(c);
- STBTTAlignedQuad stbQuad = STBTTAlignedQuad.create();
- float[] x = new float[]{0};
- float[] y = new float[]{0};
- STBTruetype.stbtt_GetBakedQuad(cdata, BITMAP_W, BITMAP_H, c - CHAR_START, x, y, stbQuad, true);
- g.quad = MeshUtils.quad(stbQuad.x0(), stbQuad.y0(), stbQuad.x1(), stbQuad.y1(), stbQuad.s0(), stbQuad.t0(), stbQuad.s1(), stbQuad.t1() );
- g.width = stbQuad.x1() - stbQuad.x0();
- glyphs.add(g);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Write this font to an image file. Will write the bitmap.
- * @param filename
- */
- public void writeToFile(String filename) {
- org.lwjgl.stb.STBImageWrite.stbi_write_png(filename, BITMAP_W, BITMAP_H, 1, bitmap, 0);
- }
- /**
- * Return the defined size for this font.
- * @return
- */
- public int getFontHeight() {
- return size;
- }
- class Glyph {
- char c;
- BufferedMesh quad;
- float width;
- Glyph( char c ) {
- this.c = c;
- }
- }
- /**
- * Return glyph data for a specific character.
- * @param c
- * @return
- */
- public Glyph getGlyph(char c) {
- for (int i = 0; i < glyphs.size(); i++) {
- if (glyphs.get(i).c == c)
- return glyphs.get(i);
- }
- return null;
- }
- /**
- * Return the bitmap data that makes up this bitmap texture sheet.
- * @return
- */
- public ByteBuffer getBitmapData() {
- return bitmap;
- }
- /**
- * Return the width of this bitmaps texture sheet.
- * @return
- */
- public int getBitmapWidth() {
- return BITMAP_W;
- }
- /**
- * Return the height of this bitmaps texture sheet.
- * @return
- */
- public int getBitmapHeight() {
- return BITMAP_H;
- }
- /**
- * Return the ascent of this font.
- * @return
- */
- public int getAscent() {
- return this.ascent;
- }
- /**
- * Return the descent of this font.
- * @return
- */
- public int getDescent() {
- return this.descent;
- }
- /**
- * Return the line-gap of this font.
- * @return
- */
- public int getLineGap() {
- return this.lineGap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement