MeVisLab Resolution Independence API
|
A bridge to the ftgl library that manages font rendering with texture fonts. More...
#include <mlGL2DFont.h>
Public Types | |
enum | { MAX_FONTS = 32 } |
Public Member Functions | |
GL2DFont () | |
~GL2DFont () override | |
bool | init () |
Init with the default system font. | |
bool | init (const char *fontname) |
Init the given font (either just a font name without .ttf or an absolute path). | |
bool | init (const char *fontname, const int numFonts, const int *sizes) |
Init the given font (either just a font name without .ttf or an absolute path) with the given number of font sizes. | |
void | drawString (const float dx, const float dy, const float fontSize, const float color[4], const char *string, const size_t length=0, const bool overlay=false, const bool shadow=true, const float *shadowColor=nullptr) |
This function draws a simple string with no linefeeds. | |
int | getStringWidth (const float fontSize, const char *string, const size_t length) const |
Get width of the given simple string. | |
int | getFontHeight (const float fontSize) const |
Get the height of a font (ascender+descender) in pixels. | |
int | getFontAscender (const float fontSize) const |
Get the ascender of the font in pixels. | |
int | getFontDescender (const float fontSize) const |
Get the descender of the font in pixels (positive distance from the baseline). | |
bool | isInitialized () const |
Returns true if font loaded, otherwise false. | |
bool | isUnicodeRenderingPossible () const |
Returns true if unicode rendering is possible, otherwise false. | |
void | contextDestroyed () override |
forget the resource, the context was destroyed (called by the GLResourceManager) | |
bool | debugFontRenderingEnabled () |
check if debug font rendering is enabled | |
Public Member Functions inherited from ml::GLResource | |
GLResource () | |
virtual | ~GLResource () |
Static Public Member Functions | |
static GL2DFont * | getDefaultSystemFont () |
get the default system font (already initialized, a valid GL context is only required for the draw methods) Note: The font is owned by MLOpenGL, do not delete the returned object! | |
static std::string | getDefaultSystemFontName () |
returns the default font name for proportional fonts (this can be overwritten via the "GL2DFont_DefaultSystemFont" property of ml::ApplicationProperties, which reads its properties from the MeVisLab prefs file) | |
Static Public Member Functions inherited from ml::GLResource | |
static GLenum | getGLError () |
check for OpenGL error (returns GL_INVALID_OPERATION if there is no valid OpenGL context) | |
Protected Member Functions | |
int | getFontIndex (const float fontSize) const |
Get the font idx that should be used for the given size of the font. | |
void | cleanup () |
Cleanup the fonts. | |
bool | reloadFonts () |
Free old fonts and reload them. | |
void | preFontDraw (const bool overlay) |
Initialize font rendering. | |
void | doFontDraw (const float dx, const float dy, const float fontSize, const float color[4], const bool shadow, const char *asciiString, const wchar_t *optionalUnicodeString, const float *shadowColor) |
Render the fonts. | |
void | postFontDraw (const bool overlay) |
Finalize the font rendering. | |
bool | checkUTF8StringForUnicodeChars (const char *string, size_t length=0) const |
Check if the string contains unicode chars. | |
const char * | copyAndConvertString (const char *string, size_t length, const wchar_t **optionalUnicodeString) const |
Internal method that copies the string to the internal buffer and generates a unicode string if it is required. | |
std::string | findFontFile (const char *fontName) |
Searches for the file name of the given font. | |
std::string | findFontFileWithFontconfig (const char *fontName) |
Searches the font by using fontconfig (Linux only) | |
Additional Inherited Members | |
Protected Attributes inherited from ml::GLResource | |
GLResource * | _next |
GLResource * | _previous |
A bridge to the ftgl library that manages font rendering with texture fonts.
The font is loaded in different sizes to get a good matching between size of the loaded font and the size it is displayed. The rendering of strings expects UTF8 unicode encoding. Typically strings are obtained from ML or Inventor fields or from DcmTree, which also use UTF8 encoding, so nothing special needs to be done on those strings. If the strings are generated and are not simple ASCII (e.g. Latin1), the user needs to take care of conversion from e.g. Latin1 to UTF8.
It is recommended to use getSystemDefaultFonts() for all code that just wants to render text with the default font, this avoids font allocation on each module that makes use of the default font.
It's essential to initialize objects of this class. The most general method just requires a valid GL context and uses the default system font. The implementations take a font or the font and a list of fontsizes to be used to load the fonts with. In general this class could provide 3D font rendering, but it has been implemented with 2D rendering in mind. The draw methods allow a parameter overlay which specifies if GL2DFont should take care of the specification of the 2D context (backing up the old projections, using an orthorgonal projection for the actual font rendering and restoring the old projections) or if this is done outside of its scope.
Definition at line 52 of file mlGL2DFont.h.
anonymous enum |
Enumerator | |
---|---|
MAX_FONTS |
Definition at line 59 of file mlGL2DFont.h.
ml::GL2DFont::GL2DFont | ( | ) |
|
override |
|
protected |
Check if the string contains unicode chars.
|
protected |
Cleanup the fonts.
|
overridevirtual |
forget the resource, the context was destroyed (called by the GLResourceManager)
Implements ml::GLResource.
|
protected |
Internal method that copies the string to the internal buffer and generates a unicode string if it is required.
If optionalUnicodeString is set, the caller needs to take care of doing MLFree on the wchar_t pointer
bool ml::GL2DFont::debugFontRenderingEnabled | ( | ) |
check if debug font rendering is enabled
|
protected |
Render the fonts.
void ml::GL2DFont::drawString | ( | const float | dx, |
const float | dy, | ||
const float | fontSize, | ||
const float | color[4], | ||
const char * | string, | ||
const size_t | length = 0, | ||
const bool | overlay = false, | ||
const bool | shadow = true, | ||
const float * | shadowColor = nullptr ) |
This function draws a simple string with no linefeeds.
The rendering is started at dx,dy which is used as the baseline position for the drawing. Processing multi line text has to be done manually (see View2D/View2DFont.h for some examples).
dx | The 2D position of the text (either in current coordinate system or in lower-left viewport device coordinate system when overlay is set to true) |
dy | The 2D position of the text (either in current coordinate system or in lower-left viewport device coordinate system when overlay is set to true) |
color | The color of the text (float[4] RGBA) |
string | The string to be drawn. The encoding needs to be UTF8 unicode encoding (or plain ASCII) |
length | The length of the string that should be rendered (if length is 0, strlen is used and the string is expected to be 0 terminated) |
fontSize | Point size of the font. |
overlay | GL2DFont takes care of setting a valid 2D environment (lower-left viewport device coordinate system) |
shadow | Draw a shadow of the font. |
shadowColor | The color of the optional text shadow (float[3] RGB) |
|
protected |
Searches for the file name of the given font.
May return a different font file, if it is not found.
|
protected |
Searches the font by using fontconfig (Linux only)
|
static |
get the default system font (already initialized, a valid GL context is only required for the draw methods) Note: The font is owned by MLOpenGL, do not delete the returned object!
|
static |
returns the default font name for proportional fonts (this can be overwritten via the "GL2DFont_DefaultSystemFont" property of ml::ApplicationProperties, which reads its properties from the MeVisLab prefs file)
int ml::GL2DFont::getFontAscender | ( | const float | fontSize | ) | const |
Get the ascender of the font in pixels.
int ml::GL2DFont::getFontDescender | ( | const float | fontSize | ) | const |
Get the descender of the font in pixels (positive distance from the baseline).
int ml::GL2DFont::getFontHeight | ( | const float | fontSize | ) | const |
Get the height of a font (ascender+descender) in pixels.
|
protected |
Get the font idx that should be used for the given size of the font.
int ml::GL2DFont::getStringWidth | ( | const float | fontSize, |
const char * | string, | ||
const size_t | length ) const |
Get width of the given simple string.
bool ml::GL2DFont::init | ( | ) |
Init with the default system font.
bool ml::GL2DFont::init | ( | const char * | fontname | ) |
Init the given font (either just a font name without .ttf or an absolute path).
bool ml::GL2DFont::init | ( | const char * | fontname, |
const int | numFonts, | ||
const int * | sizes ) |
Init the given font (either just a font name without .ttf or an absolute path) with the given number of font sizes.
The list of font sizes should be ordered ascending for getFontIndex to work properly!
|
inline |
Returns true if font loaded, otherwise false.
Definition at line 134 of file mlGL2DFont.h.
|
inline |
Returns true if unicode rendering is possible, otherwise false.
Definition at line 139 of file mlGL2DFont.h.
|
protected |
Finalize the font rendering.
|
protected |
Initialize font rendering.
|
protected |
Free old fonts and reload them.