MeVisLab Toolbox Reference
GL2DFont Class Reference

A bridge to the ftgl library that manages font rendering with texture fonts. More...

#include <mlGL2DFont.h>

Inheritance diagram for GL2DFont:
GLResource

Public Types

enum  { MAX_FONTS = 32 }
 

Public Member Functions

 GL2DFont ()
 
 ~GL2DFont () override
 
bool init ()
 Init with the default system font. More...
 
bool init (const char *fontname)
 Init the given font (either just a font name without .ttf or an absolute path). More...
 
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. More...
 
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. More...
 
int getStringWidth (const float fontSize, const char *string, const size_t length) const
 Get width of the given simple string. More...
 
int getFontHeight (const float fontSize) const
 Get the height of a font (ascender+descender) in pixels. More...
 
int getFontAscender (const float fontSize) const
 Get the ascender of the font in pixels. More...
 
int getFontDescender (const float fontSize) const
 Get the descender of the font in pixels (positive distance from the baseline). More...
 
bool isInitialized () const
 Returns true if font loaded, otherwise false. More...
 
bool isUnicodeRenderingPossible () const
 Returns true if unicode rendering is possible, otherwise false. More...
 
void contextDestroyed () override
 forget the resource, the context was destroyed (called by the GLResourceManager) More...
 
bool debugFontRenderingEnabled ()
 check if debug font rendering is enabled More...
 
- Public Member Functions inherited from GLResource
 GLResource ()
 
virtual ~GLResource ()
 

Static Public Member Functions

static GL2DFontgetDefaultSystemFont ()
 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! More...
 
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) More...
 
- Static Public Member Functions inherited from GLResource
static GLenum getGLError ()
 check for OpenGL error (returns GL_INVALID_OPERATION if there is no valid OpenGL context) More...
 

Protected Member Functions

int getFontIndex (const float fontSize) const
 Get the font idx that should be used for the given size of the font. More...
 
void cleanup ()
 Cleanup the fonts. More...
 
bool reloadFonts ()
 Free old fonts and reload them. More...
 
void preFontDraw (const bool overlay)
 Initialize font rendering. More...
 
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. More...
 
void postFontDraw (const bool overlay)
 Finalize the font rendering. More...
 
bool checkUTF8StringForUnicodeChars (const char *string, size_t length=0) const
 Check if the string contains unicode chars. More...
 
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. More...
 
std::string findFontFile (const char *fontName)
 Searches for the file name of the given font. More...
 
std::string findFontFileWithFontconfig (const char *fontName)
 Searches the font by using fontconfig (Linux only) More...
 

Additional Inherited Members

- Protected Attributes inherited from GLResource
GLResource_next
 
GLResource_previous
 

Detailed Description

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.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
MAX_FONTS 

Definition at line 59 of file mlGL2DFont.h.

Constructor & Destructor Documentation

◆ GL2DFont()

GL2DFont::GL2DFont ( )

◆ ~GL2DFont()

GL2DFont::~GL2DFont ( )
override

Member Function Documentation

◆ checkUTF8StringForUnicodeChars()

bool GL2DFont::checkUTF8StringForUnicodeChars ( const char *  string,
size_t  length = 0 
) const
protected

Check if the string contains unicode chars.

◆ cleanup()

void GL2DFont::cleanup ( )
protected

Cleanup the fonts.

◆ contextDestroyed()

void GL2DFont::contextDestroyed ( )
overridevirtual

forget the resource, the context was destroyed (called by the GLResourceManager)

Implements GLResource.

◆ copyAndConvertString()

const char* GL2DFont::copyAndConvertString ( const char *  string,
size_t  length,
const wchar_t **  optionalUnicodeString 
) const
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

◆ debugFontRenderingEnabled()

bool GL2DFont::debugFontRenderingEnabled ( )

check if debug font rendering is enabled

◆ doFontDraw()

void GL2DFont::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 
)
protected

Render the fonts.

◆ drawString()

void 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).

Parameters
dxThe 2D position of the text (either in current coordinate system or in lower-left viewport device coordinate system when overlay is set to true)
dyThe 2D position of the text (either in current coordinate system or in lower-left viewport device coordinate system when overlay is set to true)
colorThe color of the text (float[4] RGBA)
stringThe string to be drawn. The encoding needs to be UTF8 unicode encoding (or plain ASCII)
lengthThe length of the string that should be rendered (if length is 0, strlen is used and the string is expected to be 0 terminated)
fontSizePoint size of the font.
overlayGL2DFont takes care of setting a valid 2D environment (lower-left viewport device coordinate system)
shadowDraw a shadow of the font.
shadowColorThe color of the optional text shadow (float[3] RGB)
Warning
This operation requires a valid GL context!

◆ findFontFile()

std::string GL2DFont::findFontFile ( const char *  fontName)
protected

Searches for the file name of the given font.

May return a different font file, if it is not found.

◆ findFontFileWithFontconfig()

std::string GL2DFont::findFontFileWithFontconfig ( const char *  fontName)
protected

Searches the font by using fontconfig (Linux only)

◆ getDefaultSystemFont()

static GL2DFont* GL2DFont::getDefaultSystemFont ( )
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!

◆ getDefaultSystemFontName()

static std::string GL2DFont::getDefaultSystemFontName ( )
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)

◆ getFontAscender()

int GL2DFont::getFontAscender ( const float  fontSize) const

Get the ascender of the font in pixels.

Warning
This operation requires a valid GL context!

◆ getFontDescender()

int GL2DFont::getFontDescender ( const float  fontSize) const

Get the descender of the font in pixels (positive distance from the baseline).

Warning
This operation requires a valid GL context!

◆ getFontHeight()

int GL2DFont::getFontHeight ( const float  fontSize) const

Get the height of a font (ascender+descender) in pixels.

Warning
This operation requires a valid GL context!

◆ getFontIndex()

int GL2DFont::getFontIndex ( const float  fontSize) const
protected

Get the font idx that should be used for the given size of the font.

◆ getStringWidth()

int GL2DFont::getStringWidth ( const float  fontSize,
const char *  string,
const size_t  length 
) const

Get width of the given simple string.

Warning
This operation requires a valid GL context!

◆ init() [1/3]

bool GL2DFont::init ( )

Init with the default system font.

Warning
This operation requires a valid GL context!

◆ init() [2/3]

bool GL2DFont::init ( const char *  fontname)

Init the given font (either just a font name without .ttf or an absolute path).

Warning
This operation requires a valid GL context!

◆ init() [3/3]

bool 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!

Warning
This operation requires a valid GL context!

◆ isInitialized()

bool GL2DFont::isInitialized ( ) const
inline

Returns true if font loaded, otherwise false.

Definition at line 134 of file mlGL2DFont.h.

◆ isUnicodeRenderingPossible()

bool GL2DFont::isUnicodeRenderingPossible ( ) const
inline

Returns true if unicode rendering is possible, otherwise false.

Definition at line 139 of file mlGL2DFont.h.

◆ postFontDraw()

void GL2DFont::postFontDraw ( const bool  overlay)
protected

Finalize the font rendering.

◆ preFontDraw()

void GL2DFont::preFontDraw ( const bool  overlay)
protected

Initialize font rendering.

◆ reloadFonts()

bool GL2DFont::reloadFonts ( )
protected

Free old fonts and reload them.


The documentation for this class was generated from the following file: