Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. package net.mantagames.lwjgl3engine.gl.font;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.IntBuffer;
  5. import java.util.ArrayList;
  6.  
  7. import org.lwjgl.BufferUtils;
  8. import org.lwjgl.opengl.GL11;
  9. import org.lwjgl.stb.STBTTAlignedQuad;
  10. import org.lwjgl.stb.STBTTBakedChar;
  11. import org.lwjgl.stb.STBTTFontinfo;
  12. import org.lwjgl.stb.STBTruetype;
  13.  
  14. import net.mantagames.lwjgl3engine.gl.Texture2D;
  15. import net.mantagames.lwjgl3engine.gl.mesh.BufferedMesh;
  16. import net.mantagames.lwjgl3engine.util.IOUtil;
  17. import net.mantagames.lwjgl3engine.util.MeshUtils;
  18.  
  19. public class BitmapFont {
  20.     private static final int BITMAP_W = 512;
  21.     private static final int BITMAP_H = 512;
  22.     private static final int CHAR_START = 32;
  23.     private static final int CHAR_AMT   = 96;
  24.  
  25.     private int ascent;
  26.     private int descent;
  27.     private int lineGap;
  28.  
  29.     private int size;
  30.     private ArrayList<Glyph> glyphs;
  31.     private ByteBuffer bitmap;
  32.  
  33.     public BitmapFont(String fontFile, int size) {
  34.         this.size = size;
  35.         this.glyphs = new ArrayList<Glyph>();
  36.         STBTTBakedChar.Buffer cdata = STBTTBakedChar.malloc(CHAR_AMT);
  37.  
  38.         try {
  39.             // Load the font
  40.             ByteBuffer ttf = IOUtil.ioResourceToByteBuffer(fontFile, 160 * 1024);
  41.             if ( ttf == null ) {
  42.                 System.err.println("Font not found");
  43.                 return;
  44.             }
  45.  
  46.             // Generate font-bitmap data.
  47.             bitmap = BufferUtils.createByteBuffer(BITMAP_W * BITMAP_H);
  48.             org.lwjgl.stb.STBTruetype.stbtt_BakeFontBitmap(ttf, getFontHeight(), bitmap, BITMAP_W, BITMAP_H, CHAR_START, cdata);
  49.  
  50.             // Write to file
  51.             writeToFile("Resources/test.png");
  52.  
  53.             // Get font metrics
  54.             STBTTFontinfo fontInfo = STBTTFontinfo.create();
  55.             if (!STBTruetype.stbtt_InitFont(fontInfo, ttf))
  56.                 throw new RuntimeException("Error initializing Font");
  57.             float scale = STBTruetype.stbtt_ScaleForPixelHeight(fontInfo, size);
  58.             IntBuffer ascent = BufferUtils.createIntBuffer(1);
  59.             IntBuffer descent = BufferUtils.createIntBuffer(1);
  60.             IntBuffer lineGap = BufferUtils.createIntBuffer(1);
  61.             STBTruetype.stbtt_GetFontVMetrics(fontInfo, ascent, descent, lineGap);
  62.             this.ascent = (int) (ascent.get(0) * scale);
  63.             this.descent = (int) (descent.get(0) * scale);
  64.             this.lineGap = lineGap.get(0);
  65.  
  66.             // Generate glyphs
  67.             for (int i = CHAR_START; i < CHAR_START + CHAR_AMT; i++) {
  68.                 char c = (char) i;
  69.                 Glyph g = new Glyph(c);
  70.                 STBTTAlignedQuad stbQuad = STBTTAlignedQuad.create();
  71.                 float[] x = new float[]{0};
  72.                 float[] y = new float[]{0};
  73.                 STBTruetype.stbtt_GetBakedQuad(cdata, BITMAP_W, BITMAP_H, c - CHAR_START, x, y, stbQuad, true);
  74.                 g.quad = MeshUtils.quad(stbQuad.x0(), stbQuad.y0(), stbQuad.x1(), stbQuad.y1(), stbQuad.s0(), stbQuad.t0(), stbQuad.s1(), stbQuad.t1() );
  75.                 g.width = stbQuad.x1() - stbQuad.x0();
  76.                 glyphs.add(g);
  77.             }
  78.         } catch (Exception e) {
  79.             e.printStackTrace();
  80.         }
  81.     }
  82.  
  83.     /**
  84.      * Write this font to an image file. Will write the bitmap.
  85.      * @param filename
  86.      */
  87.     public void writeToFile(String filename) {
  88.         org.lwjgl.stb.STBImageWrite.stbi_write_png(filename, BITMAP_W, BITMAP_H, 1, bitmap, 0);
  89.     }
  90.  
  91.     /**
  92.      * Return the defined size for this font.
  93.      * @return
  94.      */
  95.     public int getFontHeight() {
  96.         return size;
  97.     }
  98.  
  99.     class Glyph {
  100.         char c;
  101.         BufferedMesh quad;
  102.         float width;
  103.  
  104.         Glyph( char c ) {
  105.             this.c = c;
  106.         }
  107.     }
  108.  
  109.     /**
  110.      * Return glyph data for a specific character.
  111.      * @param c
  112.      * @return
  113.      */
  114.     public Glyph getGlyph(char c) {
  115.         for (int i = 0; i < glyphs.size(); i++) {
  116.             if (glyphs.get(i).c == c)
  117.                 return glyphs.get(i);
  118.         }
  119.  
  120.         return null;
  121.     }
  122.  
  123.     /**
  124.      * Return the bitmap data that makes up this bitmap texture sheet.
  125.      * @return
  126.      */
  127.     public ByteBuffer getBitmapData() {
  128.         return bitmap;
  129.     }
  130.  
  131.     /**
  132.      * Return the width of this bitmaps texture sheet.
  133.      * @return
  134.      */
  135.     public int getBitmapWidth() {
  136.         return BITMAP_W;
  137.     }
  138.  
  139.     /**
  140.      * Return the height of this bitmaps texture sheet.
  141.      * @return
  142.      */
  143.     public int getBitmapHeight() {
  144.         return BITMAP_H;
  145.     }
  146.  
  147.     /**
  148.      * Return the ascent of this font.
  149.      * @return
  150.      */
  151.     public int getAscent() {
  152.         return this.ascent;
  153.     }
  154.  
  155.     /**
  156.      * Return the descent of this font.
  157.      * @return
  158.      */
  159.     public int getDescent() {
  160.         return this.descent;
  161.     }
  162.  
  163.     /**
  164.      * Return the line-gap of this font.
  165.      * @return
  166.      */
  167.     public int getLineGap() {
  168.         return this.lineGap;
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement