
Click on the above image to see an example string printed using the code snippet presented here.
This submission is an extremely useful code snippet. It consists of one header file and one CPP that can render a high quality 3d vector font. You provide a callback interface to 'draw a line'. You can use OpenGL, Direct3D, GDI, or any other API you might choose. You merely have to implement that one callback 'drawLine' and you get a high quality vector font that you can render in 2d or 3d as you prefer. The font used is called the
'Hershey Font' and is public domain in this format.
The pure virtual interface is called "VectorFont". It has just one method 'vprintf' to printf a string through the vector font printing routine.
The first parameter is an *optional* 4x4 matrix that the data points will be transformed through. If you do not provide a transform, then the string will always be printed relative to the origin and you can do any placement you want in your own callback method.
If you want the font to appear in 3d, but always 'camera facing', then you can build a camera facing rotation transform from the origin of the text to the current eye position. You can build this transform by using the
'lookAt' code snippet I published on this site in the past.
The next parameter you pass is 'textScale'. This simply scales the font to a particular size you might wish. A default of 1 is fine to start with and you can size from there until the font fits your particular needs. Since it is a vector font, it looks good at pretty much any scale.
The next parameter is a bool called 'centered'. If this bool is true, then the text will be centered around the position you passed in.
The final parameters are simply a standard 'printf' style format string and optional additional arguments.
This is a damned useful code snippet and I would appreciate a thank you from anyone who uses it.
Here is the source:
VectorFont.h VectorFont.cpp----------------------------------------------
// This code snippet allows you to render a high quality vector line font.
// The font it uses is called the 'Hershey Font' (search for it on Google).
// This font is completely public domain and you are free to use it in
// your own applications.
//
//
// Here is how you use this code snippet.
//
// Step #1 : Create an instance of the VectorFont pure virtual interface.
// Step #2 : call 'vprintf' with the transform, scale, center flag, and text you wish to print.
// Step #3 : Dispatch individual line segments to your own rendering framework.
// Step #4 : Release the VectorFont class.
//
// Here is an example usage in C++
//
// class MyLineDraw : public VectorFontInterfac
// {
// public:
// virtual void drawLine(float x1,float y1,float x2,float y2)
// {
// // Right here, in this callback, draw the line using your own line draw interface, OpenGL, D3D, or whatever works for you.
// }
// };
//
//
// MyLineDraw mld; // create an instance of our callback class to draw individual line segments.
//
// VectorFont *vf = createVectorFont(&mld);
// vf->vprintf(NULL,1,true,"This is a test of the emergency broadcasting system.");
// releaseVectorFont(vf);
class VectorFontInterface
{
public:
virtual void drawLine(float x1,float y1,float x2,float y2) = 0;
};
class VectorFont
{
public:
virtual void vprintf(const float *transform, // an *optional* 4x4 matrix transform to apply to each point as the font is rendered.
float textScale, // A scaling value to apply to the vertices of the font.
bool centered, // True if you want the text centered relative to its 3space position, specified in the translation component of 'transform' or 0,0,0 if no transform provided.
const char *fmt, // A printf style format string
...) = 0; // Optional additional parameters based on the format spec.
};
VectorFont * createVectorFont(VectorFontInterface *vf);
void releaseVectorFont(VectorFont *vf);