Advertisement
Guest User

Untitled

a guest
Sep 18th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. import org.apache.commons.io.IOUtils;
  2. import org.lwjgl.BufferUtils;
  3. import org.lwjgl.opengl.GL11;
  4. import org.lwjgl.stb.STBImage;
  5. import org.lwjgl.stb.STBImageWrite;
  6. import org.lwjgl.stb.STBTTFontinfo;
  7. import org.lwjgl.stb.STBTruetype;
  8.  
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.nio.ByteBuffer;
  14. import java.nio.FloatBuffer;
  15. import java.nio.IntBuffer;
  16. import java.nio.ShortBuffer;
  17. import java.nio.channels.FileChannel;
  18.  
  19. public class FontTest {
  20.     public static ByteBuffer fileToByteBuffer(String filename, boolean isResource) {
  21.         ByteBuffer fileBuffer;
  22.         if (isResource) {
  23.             InputStream is = FontTest.class.getResourceAsStream(filename);
  24.             try {
  25.                 byte array[] = IOUtils.toByteArray(is);
  26.                 fileBuffer = FontTest.buildBuffer(array);
  27.                 is.close();
  28.             } catch (IOException e) {
  29.                 throw new RuntimeException(e);
  30.             }
  31.         } else {
  32.             assert filename != null;
  33.             try {
  34.                 FileChannel fc = new FileInputStream(filename).getChannel();
  35.                 fileBuffer = BufferUtils.createByteBuffer((int) fc.size() + 1);
  36.                 //noinspection StatementWithEmptyBody
  37.                 while (fc.read(fileBuffer) != -1);
  38.                 fc.close();
  39.                 fileBuffer.flip();
  40.             } catch (Exception e) {
  41.                 throw new RuntimeException(e);
  42.             }
  43.         }
  44.         return fileBuffer;
  45.     }
  46.  
  47.     public static FloatBuffer buildBuffer(float[] array) {
  48.         FloatBuffer buffer = BufferUtils.createFloatBuffer(array.length);
  49.         buffer.put(array);
  50.         buffer.flip();
  51.         return buffer;
  52.     }
  53.  
  54.     public static ShortBuffer buildBuffer(short[] array) {
  55.         ShortBuffer buffer = BufferUtils.createShortBuffer(array.length);
  56.         buffer.put(array);
  57.         buffer.flip();
  58.         return buffer;
  59.     }
  60.  
  61.     public static ByteBuffer buildBuffer(byte[] array) {
  62.         ByteBuffer buffer = BufferUtils.createByteBuffer(array.length);
  63.         buffer.put(array);
  64.         buffer.flip();
  65.         return buffer;
  66.     }
  67.  
  68.     public static void main(String[] args) {
  69.         String path = new File("natives").getAbsolutePath();
  70.         System.setProperty("org.lwjgl.librarypath", path);
  71.         ByteBuffer fontBuffer = fileToByteBuffer("/NotoSans-Regular.ttf", true);
  72.         //ByteBuffer fontInfo = STBTTFontinfo.malloc();
  73.         ByteBuffer fontInfo = BufferUtils.createByteBuffer(56);
  74.         if (STBTruetype.stbtt_InitFont(fontInfo, fontBuffer) == GL11.GL_FALSE) {
  75.             System.err.println("ERROR INIT");
  76.         }
  77.  
  78.         int b_w = 512;
  79.         int b_h = 512;
  80.         int l_h = 64;
  81.         ByteBuffer bitmap = BufferUtils.createByteBuffer(b_w * b_h);
  82.  
  83.         float scale = STBTruetype.stbtt_ScaleForPixelHeight(fontInfo, l_h);
  84.  
  85.         String word = "How are you?";
  86.  
  87.         int x = 0;
  88.  
  89.         IntBuffer ascentB = BufferUtils.createIntBuffer(1), descentB = BufferUtils.createIntBuffer(1), linegapB = BufferUtils.createIntBuffer(1);
  90.  
  91.         STBTruetype.stbtt_GetFontVMetrics(fontInfo, ascentB, descentB, linegapB);
  92.  
  93.         int ascent = ascentB.get(0);
  94.         int descent = descentB.get(0);
  95.         int linegap = descentB.get(0);
  96.  
  97.         ascent *= scale;
  98.         descent *= scale;
  99.  
  100.         for (int i = 0; i < word.length(); ++i) {
  101.             IntBuffer c_x1B = BufferUtils.createIntBuffer(1);
  102.             IntBuffer c_y1B = BufferUtils.createIntBuffer(1);
  103.             IntBuffer c_x2B = BufferUtils.createIntBuffer(1);
  104.             IntBuffer c_y2B = BufferUtils.createIntBuffer(1);
  105.  
  106.             STBTruetype.stbtt_GetCodepointBitmapBox(fontInfo, (int) (word.charAt(i)), scale, scale, c_x1B, c_y1B, c_x2B, c_y2B);
  107.  
  108.             int c_x1 = c_x1B.get(0);
  109.             int c_y1 = c_y1B.get(0);
  110.             int c_x2 = c_x2B.get(0);
  111.             int c_y2 = c_y2B.get(0);
  112.  
  113.             int y = ascent + c_y1;
  114.  
  115.             int byteOffset = x + (y * b_w);
  116.             bitmap.position(byteOffset);
  117.             STBTruetype.stbtt_MakeCodepointBitmap(fontInfo, bitmap, c_x2 - c_x1, c_y2 - c_y1, b_w, scale, scale, (int) word.charAt(i));
  118.  
  119.             IntBuffer axB = BufferUtils.createIntBuffer(1);
  120.             IntBuffer zero = BufferUtils.createIntBuffer(1);
  121.             zero.put(0);
  122.             zero.flip();
  123.             STBTruetype.stbtt_GetCodepointHMetrics(fontInfo, (int)word.charAt(i), axB, zero);
  124.             x += axB.get(0) * scale;
  125.  
  126.             if (i != word.length() - 1) {
  127.                 int kern = STBTruetype.stbtt_GetCodepointKernAdvance(fontInfo, (int)word.charAt(i), (int)word.charAt(i + 1));
  128.                 x += kern * scale;
  129.             }
  130.         }
  131.  
  132.         bitmap.rewind();
  133.  
  134.         STBImageWrite.stbi_write_png("out.png", b_w, b_h, 1, bitmap, b_w);
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement