[LWJGL] Library for rendering text.

Hi guys,

I was wondering if there is any library or some helper classes that I could use in my project to render strings?
I don’t really want to use Slick because I only need the text renderer and I wouldn’t want to include the whole lib just for that.

(this is kind of self-advertising)

I have made this library that will render text using FreeType in native code. The problem is that it only is available on WINDOWS, ANDROID and the .dll files take up a lot of space (9mb for windows alone).

https://www.mediafire.com/?r8uki5mk83uag1i

If you want the source code, or if you want to try to contribute:

Here is some ‘example’ code.


		JFreeType jfreetype = JFreeType.createInstance();
		
		jfreetype.initialize(true); // the parameter is whether JFreeType should attempt to load native files itself
		// if you pass in false, you need to load native files yourself.
		
		JFreeTypeOptions options = new JFreeTypeOptions();
		options.font = "C:/path/to/font.ttf"; // full path to font file on the file system
		options.text = "A generic string of text"; // the string of text you want to render
		options.size = 20; // the size of the font
		options.width = 10000; // the width at which the size of the font will be reduced if the string is bigger than this number
		options.height = 10000; // the height at which the size of the font will be reduced if the string is bigger than this number
		options.lineSpacing = 0; // when wrapping text, spacing between lines (in pixels)
		
		Bitmap bitmap = jfreetype.render(options);
		int format = bitmap.format; // this can be ignored, right now the only format is alpha (1 byte alpha)
		int width = bitmap.width; // the width of the texture
		int height = bitmap.height; // the height of the texture
		ByteBuffer pixels = bitmap.pixels; // a byte buffer of pixels (the format is 1 byte alpha)
		
		TextMetrics metrics = bitmap.textMetrics; // contains some metrics generated from rendering the string
		int descender = metrics.descender; // the maximum descender of the string
		
		
		jfreetype.setInstantLoggingEnabled(false); // if instant logging is enabled, will print errors / info instantly to default output stream
		
		// If instant logging is disabling, will 'buffer' the messages (up to 128)
		String error = null;
		while ((error = jfreetype.getLog()) != null) {
			System.out.println(error); // prints out error messages?
		}
		
		jfreetype.destroy(); // cleanup?

On the final note, if you decide to try this lib, and something doesn’t work, you might want to just drop it entirely, as this is not really a production level library yet.

Looks good, but to be honest I was kind of hoping for something more lightweight. :-
I can’t really find anything apart from the Slick and LibGDX ones.

The absolute easiest way is to have prebaked images with the text you want on them. Obviously that isn’t the best since no dynamic text.

If you want dynamic text, the easiest way (and it is still pretty easy as long as you have some experience) is to have a texture with every single character on it. You just have to know where each character is and then you can specify texture coordinates such that you get the right character. Downsides to this are: you will probably end up drawing one character at a time, creating the texture with all the characters can be time consuming unless you write a program to do it for you (which I would suggest, again not too difficult) and you need a new texture for each font you want to use.