MeVisLab Toolbox Reference
|
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 () |
Initializes with the default system font. | |
bool | init (const char *fontname) |
Initializes the given font, which is either just a font name without .ttf or an absolute path. | |
bool | init (const char *fontname, int numFonts, const int *sizes) |
Initializes the given font, which is either just a font name without .ttf or an absolute path, with the given number of font sizes. | |
void | drawString (float dx, float dy, float fontSize, const float color[4], const char *string, size_t length=0, bool overlay=false, bool shadow=true, const float *shadowColor=nullptr) |
This function draws a simple string with no line feeds. | |
int | getStringWidth (float fontSize, const char *string, int length=-1) const |
Returns the width of the given simple string. | |
int | getFontHeight (float fontSize) const |
Returns the height of a font (ascender+descender) in pixels. | |
int | getFontAscender (float fontSize) const |
Returns the ascender of the font in pixels. | |
int | getFontDescender (float fontSize) const |
Returns the descender of the font in pixels (i.e., the positive distance from the baseline). | |
bool | isInitialized () const |
Returns true if font loaded; otherwise, it returns false . | |
bool | isUnicodeRenderingPossible () const |
Returns true if Unicode rendering is possible; otherwise, it returns false . | |
void | contextDestroyed () override |
Forgets the resource, the context was destroyed (called by the GLResourceManager). | |
bool | debugFontRenderingEnabled () |
Checks whether debug font rendering is enabled. | |
![]() | |
GLResource () | |
virtual | ~GLResource () |
Static Public Member Functions | |
static GL2DFont * | getDefaultSystemFont () |
Returns the default system font. | |
static std::string | getDefaultSystemFontName () |
Returns the default font name for proportional fonts. | |
![]() | |
static GLenum | getGLError () |
Checks for OpenGL error (returns GL_INVALID_OPERATION if there is no valid OpenGL context). | |
Protected Member Functions | |
int | getFontIndex (float fontSize) const |
Returns the font index that should be used for the given size of the font. | |
void | cleanup () |
Cleans up the fonts. | |
bool | reloadFonts () |
Frees old fonts and reloads them. | |
void | preFontDraw (bool overlay) |
Initializes font rendering. | |
void | doFontDraw (float dx, float dy, float fontSize, const float color[4], bool shadow, const char *asciiString, const wchar_t *optionalUnicodeString, const float *shadowColor) |
Renders the fonts. | |
void | postFontDraw (const bool overlay) |
Finalizes the font rendering. | |
bool | checkUTF8StringForUnicodeChars (const char *string, size_t length=0) const |
Checks whether 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 | |
![]() | |
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 in. The rendering of strings expects UTF-8 Unicode encoding. Typically, strings are obtained from ML or Inventor fields or from DcmTree, which also use UTF-8 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 UTF-8.
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 is 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 font sizes 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 that specifies whether GL2DFont should take care of the specification of the 2D context (backing up the old projections, using an orthogonal projection for the actual font rendering, and restoring the old projections) or whether this is done outside its scope.
Definition at line 52 of file mlGL2DFont.h.
Enumerator | |
---|---|
MAX_FONTS |
Definition at line 60 of file mlGL2DFont.h.
GL2DFont::GL2DFont | ( | ) |
|
override |
|
protected |
Checks whether the string contains unicode chars.
|
protected |
Cleans up the fonts.
|
overridevirtual |
Forgets the resource, the context was destroyed (called by the GLResourceManager).
Implements 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 GL2DFont::debugFontRenderingEnabled | ( | ) |
Checks whether debug font rendering is enabled.
|
protected |
Renders the fonts.
void GL2DFont::drawString | ( | float | dx, |
float | dy, | ||
float | fontSize, | ||
const float | color[4], | ||
const char * | string, | ||
size_t | length = 0, | ||
bool | overlay = false, | ||
bool | shadow = true, | ||
const float * | shadowColor = nullptr ) |
This function draws a simple string with no line feeds.
The rendering is started at dx,dy, which is used as the baseline position for the drawing. Processing multiline 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 UTF-8 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) |
Searches for the file name of the given font.
May return a different font file if it is not found.
Searches the font by using fontconfig (Linux only)
Returns the default system font.
This is 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 overridden via the 'GL2DFont_DefaultSystemFont' property of ml::ApplicationProperties, which reads its properties from the MeVisLab .prefs file.
Returns the ascender of the font in pixels.
Returns the descender of the font in pixels (i.e., the positive distance from the baseline).
Returns the height of a font (ascender+descender) in pixels.
Returns the font index that should be used for the given size of the font.
Returns the width of the given simple string.
bool GL2DFont::init | ( | ) |
Initializes with the default system font.
Initializes the given font, which is either just a font name without .ttf or an absolute path.
Initializes the given font, which is 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, it returns false
.
Definition at line 133 of file mlGL2DFont.h.
|
inline |
Returns true
if Unicode rendering is possible; otherwise, it returns false
.
Definition at line 141 of file mlGL2DFont.h.
|
protected |
Frees old fonts and reloads them.