Basic true-type font renderer

Are there any good ways to render a true-type font without slick2D, and mainly any other library (With LWJGL…)? I also need it to work well with blending, anti-aliasing, and the coordinates have to be at the top-left-hand corner (or if you have a way to change it in realtime, without any effect on performance, let me know!)

There’s gdx-freetype: https://github.com/libgdx/libgdx/wiki/Gdx-freetype

If you want a “pure java” solution, then why not just use Font?


font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf"));
font.deriveFont(24f);

Then render to BufferedImage with Graphics2D or something and go from there.

Why won’t you use slick? You wouldn’t even need to import a whole new jar, you would just need to copy and modify this code. Just be sure to include a license in it!

Because X:0, and Y:0 in slick is in the bottom left and I already tried to fix it… I found a plain LWJGL port here: http://lwjgl.org/forum/topics/help-with-picking-for-a-n00b/2951/view.html

But the main thing I’m worried about is how 0, 0 is at the bottom left in the vertex locations. Any way to fix this?

       GL11.glTexCoord2f(TextureSrcX, TextureSrcY);
		GL11.glVertex2f(drawX, drawY);
		GL11.glTexCoord2f(TextureSrcX, TextureSrcY + RenderHeight);
		GL11.glVertex2f(drawX, drawY + DrawHeight);
		GL11.glTexCoord2f(TextureSrcX + RenderWidth, TextureSrcY + RenderHeight);
		GL11.glVertex2f(drawX + DrawWidth, drawY + DrawHeight);
		GL11.glTexCoord2f(TextureSrcX + RenderWidth, TextureSrcY);
		GL11.glVertex2f(drawX + DrawWidth, drawY);

This is not that complex math, but I feel your pain; I myself have debugged this for hours before, but solved it after a bit. Here is how I fixed a similar coordinate issue in MERCury:

From here


// sy1 is the first texture coordinate's y value
// sy2 is the second texture coordinate's y value
// h is the height of the texture being drawn
sy1 = h - sy1;
sy2 = h - sy2;

[sup]Please forgive me if this code is completely irrelevant; I did not get too much sleep :P. But I am confidant this will help.[/sup]

Although, couldn’t you just rewrite the OGL for your own texture-drawing purposes?

-wes

If the coordinate system is all that is holding you back, then yeah, you just rotate all y-coordinates about the line y = like wes said.


int yCoordToDrawTextAt = 10; // from top-left

drawText(x, h - yCoordToDrawTextAt); // draw it with OGL, using flipped coords

Alternatively, you can use J2D just for it’s rasterizer, as shown here by mattdesl: https://github.com/mattdesl/lwjgl-basics/wiki/LibGDX-&-Java2D
It’s a good method if you are familiar with J2D and don’t want to fuss with stuff to get your fonts rendered in OGL.
Should be pretty easy to port if you’re not using libGDX.

To everybody suggesting [icode]y = h-y;[/icode] please know it’s [icode]y = (h-1)-y;[/icode] :point:

I really am starting to seriously question how many other things I have miscalculated by this logic. Always off by 1 pixel!

Ah hell, quite right.