Displaying text is a common problem in graphics applications where all you can do is to render textured quads. I've implemented my solution already back in 2007, as part of my old engine The Final Quest 7, which was my master thesis. I've recently come back to this code and improved it because I needed it for the personal project I now work on. Then I thought: Maybe it's a good idea to extract this code into a library? So here it is:
It does two things:
- It renders characters of the font to a texture, tightly packed.
- It calculates vertices needed to render given text.
Here are more details about the library:
- License based on MIT, so it's free to use even in a proprietary, closed-source software.
- Standalone, single file, easy to integrate with your project.
- Written in C++. Depends only on STL and WinAPI.
- Agnostic to graphics API. Returns data for your texture and vertices. It is your task to display them using Direct3D, OpenGL, Vulkan or whatever you prefer. Sample application is provided for Direct3D 11.
- Works on Windows only. Tested using Visual Studio 2017, 64-bit configuration.
- Renders fonts installed in the system. Doesn't load custom textures or fonts from files.
- Works with Unicode strings
- Supports multiline text and automatic line break, also on whole word boundary.
- Supports horizontal and vertical alignment to left/center/right and top/middle/bottom.
- Supports flexible vertex formats. You can specify separate position and texture coordinate streams and custom vertex strides.
- Supports various primitive topologies. You can request triangle list, triangle strip with primitive restart index, or triangle strip with degenerate triangles, with or without index buffer.
- Supports kerning.
- Fonts are antialiased and can be rendered pixel-perfect.
- Underline and strikeout can be added.
- Hit testing functions are available that find the character at a given point.