MeVisLabToolboxReference
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

List of all members.

Public Types

enum  { MAX_FONTS = 32 }

Public Member Functions

 GL2DFont ()
 ~GL2DFont ()
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)
 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.
virtual void contextDestroyed ()
 forget the resource, the context was destroyed (called by the GLResourceManager)
bool debugFontRenderingEnabled ()
 check if debug font rendering is enabled

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

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

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 50 of file mlGL2DFont.h.


Member Enumeration Documentation

anonymous enum
Enumerator:
MAX_FONTS 

Definition at line 57 of file mlGL2DFont.h.


Constructor & Destructor Documentation

GL2DFont::GL2DFont ( )
GL2DFont::~GL2DFont ( )

Member Function Documentation

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

Check if the string contains unicode chars.

void GL2DFont::cleanup ( ) [protected]

Cleanup the fonts.

virtual void GL2DFont::contextDestroyed ( ) [virtual]

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

Implements GLResource.

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

bool GL2DFont::debugFontRenderingEnabled ( )

check if debug font rendering is enabled

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 
) [protected]

Render the fonts.

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 
)

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
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.
Warning:
This operation requires a valid GL context!
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!

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)

int GL2DFont::getFontAscender ( const float  fontSize) const

Get the ascender of the font in pixels.

Warning:
This operation requires a valid GL context!
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!
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!
int GL2DFont::getFontIndex ( const float  fontSize) const [protected]

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

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!
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!
bool GL2DFont::init ( )

Init with the default system font.

Warning:
This operation requires a valid GL context!
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!
bool GL2DFont::isInitialized ( ) const [inline]

Returns true if font loaded, otherwise false.

Definition at line 130 of file mlGL2DFont.h.

bool GL2DFont::isUnicodeRenderingPossible ( ) const [inline]

Returns true if unicode rendering is possible, otherwise false.

Definition at line 135 of file mlGL2DFont.h.

void GL2DFont::postFontDraw ( const bool  overlay) [protected]

Finalize the font rendering.

void GL2DFont::preFontDraw ( const bool  overlay) [protected]

Initialize font rendering.

bool GL2DFont::reloadFonts ( ) [protected]

Free old fonts and reload them.


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