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 ()
 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.
 
- Public Member Functions inherited from GLResource
 GLResource ()
 
virtual ~GLResource ()
 

Static Public Member Functions

static GL2DFontgetDefaultSystemFont ()
 Returns the default system font.
 
static std::string getDefaultSystemFontName ()
 Returns the default font name for proportional fonts.
 
- Static Public Member Functions inherited from GLResource
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 charcopyAndConvertString (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 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 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.

Member Enumeration Documentation

◆ anonymous enum

Enumerator
MAX_FONTS 

Definition at line 60 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

Checks whether the string contains unicode chars.

◆ cleanup()

void GL2DFont::cleanup ( )
protected

Cleans up the fonts.

◆ contextDestroyed()

void GL2DFont::contextDestroyed ( )
overridevirtual

Forgets 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 ( )

Checks whether debug font rendering is enabled.

◆ doFontDraw()

void GL2DFont::doFontDraw ( float dx,
float dy,
float fontSize,
const float color[4],
bool shadow,
const char * asciiString,
const wchar_t * optionalUnicodeString,
const float * shadowColor )
protected

Renders the fonts.

◆ drawString()

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

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 UTF-8 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

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!

◆ getDefaultSystemFontName()

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

◆ getFontAscender()

int GL2DFont::getFontAscender ( float fontSize) const

Returns the ascender of the font in pixels.

Warning
This operation requires a valid GL context!

◆ getFontDescender()

int GL2DFont::getFontDescender ( float fontSize) const

Returns the descender of the font in pixels (i.e., the positive distance from the baseline).

Warning
This operation requires a valid GL context!

◆ getFontHeight()

int GL2DFont::getFontHeight ( float fontSize) const

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

Warning
This operation requires a valid GL context!

◆ getFontIndex()

int GL2DFont::getFontIndex ( float fontSize) const
protected

Returns the font index that should be used for the given size of the font.

◆ getStringWidth()

int GL2DFont::getStringWidth ( float fontSize,
const char * string,
int length = -1 ) const

Returns the width of the given simple string.

Warning
This operation requires a valid GL context!

◆ init() [1/3]

bool GL2DFont::init ( )

Initializes with the default system font.

Warning
This operation requires a valid GL context!

◆ init() [2/3]

bool GL2DFont::init ( const char * fontname)

Initializes the given font, which is 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,
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.

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, it returns false.

Definition at line 133 of file mlGL2DFont.h.

◆ isUnicodeRenderingPossible()

bool GL2DFont::isUnicodeRenderingPossible ( ) const
inline

Returns true if Unicode rendering is possible; otherwise, it returns false.

Definition at line 141 of file mlGL2DFont.h.

◆ postFontDraw()

void GL2DFont::postFontDraw ( const bool overlay)
protected

Finalizes the font rendering.

◆ preFontDraw()

void GL2DFont::preFontDraw ( bool overlay)
protected

Initializes font rendering.

◆ reloadFonts()

bool GL2DFont::reloadFonts ( )
protected

Frees old fonts and reloads them.


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