Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.commons.io.IOUtils;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.stb.STBImage;
- import org.lwjgl.stb.STBImageWrite;
- import org.lwjgl.stb.STBTTFontinfo;
- import org.lwjgl.stb.STBTruetype;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.ByteBuffer;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import java.nio.ShortBuffer;
- import java.nio.channels.FileChannel;
- public class FontTest {
- public static ByteBuffer fileToByteBuffer(String filename, boolean isResource) {
- ByteBuffer fileBuffer;
- if (isResource) {
- InputStream is = FontTest.class.getResourceAsStream(filename);
- try {
- byte array[] = IOUtils.toByteArray(is);
- fileBuffer = FontTest.buildBuffer(array);
- is.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- } else {
- assert filename != null;
- try {
- FileChannel fc = new FileInputStream(filename).getChannel();
- fileBuffer = BufferUtils.createByteBuffer((int) fc.size() + 1);
- //noinspection StatementWithEmptyBody
- while (fc.read(fileBuffer) != -1);
- fc.close();
- fileBuffer.flip();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- return fileBuffer;
- }
- public static FloatBuffer buildBuffer(float[] array) {
- FloatBuffer buffer = BufferUtils.createFloatBuffer(array.length);
- buffer.put(array);
- buffer.flip();
- return buffer;
- }
- public static ShortBuffer buildBuffer(short[] array) {
- ShortBuffer buffer = BufferUtils.createShortBuffer(array.length);
- buffer.put(array);
- buffer.flip();
- return buffer;
- }
- public static ByteBuffer buildBuffer(byte[] array) {
- ByteBuffer buffer = BufferUtils.createByteBuffer(array.length);
- buffer.put(array);
- buffer.flip();
- return buffer;
- }
- public static void main(String[] args) {
- String path = new File("natives").getAbsolutePath();
- System.setProperty("org.lwjgl.librarypath", path);
- ByteBuffer fontBuffer = fileToByteBuffer("/NotoSans-Regular.ttf", true);
- //ByteBuffer fontInfo = STBTTFontinfo.malloc();
- ByteBuffer fontInfo = BufferUtils.createByteBuffer(56);
- if (STBTruetype.stbtt_InitFont(fontInfo, fontBuffer) == GL11.GL_FALSE) {
- System.err.println("ERROR INIT");
- }
- int b_w = 512;
- int b_h = 512;
- int l_h = 64;
- ByteBuffer bitmap = BufferUtils.createByteBuffer(b_w * b_h);
- float scale = STBTruetype.stbtt_ScaleForPixelHeight(fontInfo, l_h);
- String word = "How are you?";
- int x = 0;
- IntBuffer ascentB = BufferUtils.createIntBuffer(1), descentB = BufferUtils.createIntBuffer(1), linegapB = BufferUtils.createIntBuffer(1);
- STBTruetype.stbtt_GetFontVMetrics(fontInfo, ascentB, descentB, linegapB);
- int ascent = ascentB.get(0);
- int descent = descentB.get(0);
- int linegap = descentB.get(0);
- ascent *= scale;
- descent *= scale;
- for (int i = 0; i < word.length(); ++i) {
- IntBuffer c_x1B = BufferUtils.createIntBuffer(1);
- IntBuffer c_y1B = BufferUtils.createIntBuffer(1);
- IntBuffer c_x2B = BufferUtils.createIntBuffer(1);
- IntBuffer c_y2B = BufferUtils.createIntBuffer(1);
- STBTruetype.stbtt_GetCodepointBitmapBox(fontInfo, (int) (word.charAt(i)), scale, scale, c_x1B, c_y1B, c_x2B, c_y2B);
- int c_x1 = c_x1B.get(0);
- int c_y1 = c_y1B.get(0);
- int c_x2 = c_x2B.get(0);
- int c_y2 = c_y2B.get(0);
- int y = ascent + c_y1;
- int byteOffset = x + (y * b_w);
- bitmap.position(byteOffset);
- STBTruetype.stbtt_MakeCodepointBitmap(fontInfo, bitmap, c_x2 - c_x1, c_y2 - c_y1, b_w, scale, scale, (int) word.charAt(i));
- IntBuffer axB = BufferUtils.createIntBuffer(1);
- IntBuffer zero = BufferUtils.createIntBuffer(1);
- zero.put(0);
- zero.flip();
- STBTruetype.stbtt_GetCodepointHMetrics(fontInfo, (int)word.charAt(i), axB, zero);
- x += axB.get(0) * scale;
- if (i != word.length() - 1) {
- int kern = STBTruetype.stbtt_GetCodepointKernAdvance(fontInfo, (int)word.charAt(i), (int)word.charAt(i + 1));
- x += kern * scale;
- }
- }
- bitmap.rewind();
- STBImageWrite.stbi_write_png("out.png", b_w, b_h, 1, bitmap, b_w);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement