Easy3D 2.5.3
TextRenderer Class Reference

TextRenderer enables quick and easy string rendering in OpenGL applications. More...

#include <easy3d/renderer/text_renderer.h>

Public Types

enum  Align { ALIGN_LEFT , ALIGN_RIGHT , ALIGN_CENTER }
 Horizontal alignment.
 

Public Member Functions

 TextRenderer (float dpi_scale=1.0f, int texture_size=512, bool mipmaps=false)
 
 ~TextRenderer ()
 
bool add_font (const std::string &font_file)
 
std::size_t num_fonts () const
 
const std::vector< std::string > & font_names () const
 
float draw (const std::string &text, float x, float y, float font_size, int font_id=0, const vec3 &font_color=vec3(0, 0, 0), bool upper_left=true) const
 
Rect draw (const std::string &text, float x, float y, float font_size, Align align=ALIGN_LEFT, int font_id=0, const vec3 &font_color=vec3(0, 0, 0), float line_spacing=0.0f, bool upper_left=true) const
 Draw multi-line text with alignment support. This method produces line breaks for text with "\n". More...
 
void set_character_spacing (float spacing)
 
float character_spacing () const
 
void set_kerning (bool kerning)
 
bool kerning () const
 
float font_height (float font_size) const
 
float string_width (const std::string &str, float font_size) const
 
float string_height (const std::string &str, float font_size) const
 
Rect string_bounding_rect (const std::string &str, float x, float y, float font_size) const
 

Detailed Description

TextRenderer enables quick and easy string rendering in OpenGL applications.

It supports truetype (TTF) fonts and unicode strings. All glyphs are cached in OpenGL textures and font rasterization is done using 'stb_truetype.h." TextRenderer allows you to:

  • switch between fonts;
  • change character spacing;
  • enable/disable kerning;
    Todo:

    For a large number of strings/characters, it is necessary to separate buffer creation and rendering.

    Performance evaluation.

Examples
Tutorial_204_Viewer_Qt, and Tutorial_309_TextRendering.

Constructor & Destructor Documentation

◆ TextRenderer()

TextRenderer ( float  dpi_scale = 1.0f,
int  texture_size = 512,
bool  mipmaps = false 
)
explicit

Constructor.

Parameters
texture_sizeThe size of the font textures. Suggested values are 512, 1024, 2048 ...
mipmapsTrue to create mipmaps for the font textures.

◆ ~TextRenderer()

Destructor.

Member Function Documentation

◆ add_font()

bool add_font ( const std::string &  font_file)

Add a font from a file. This function must be called at least once to add a font.

Parameters
font_fileThe font file (.ttf, etc).
Examples
Tutorial_203_Viewer_wxWidgets, and Tutorial_204_Viewer_Qt.

◆ character_spacing()

float character_spacing ( ) const

Query the spacing between consecutive characters.

Returns
The spacing between consecutive characters.

◆ draw() [1/2]

Rect draw ( const std::string &  text,
float  x,
float  y,
float  font_size,
Align  align = ALIGN_LEFT,
int  font_id = 0,
const vec3 font_color = vec3(0, 0, 0),
float  line_spacing = 0.0f,
bool  upper_left = true 
) const

Draw multi-line text with alignment support. This method produces line breaks for text with "\n".

Parameters
textThe string to be drawn.
xThe x-coordinate of the starting position, relative to the left edge of the content area.
yThe y-coordinate of the starting position. If upper_left is true, this position is relative to the top edge of the content area. Otherwise, it is relative to the bottom edge of the content area.
font_sizeThe font size.
alignSpecifies how the lines are aligned.
font_idThe id of the font used to draw the text, which is generated by calling to add_font(). 0 is the first font, 1 is the second, etc.
font_colorThe font color.
line_spacingThe line spacing, which is a ratio relative to line height. A zero value means no extra space between lines.
upper_leftTrue to define the origin at the upper left corner of the content area. Otherwise it is defined at the bottom left corner of the content area.
Returns
The region occupied by the text.

◆ draw() [2/2]

float draw ( const std::string &  text,
float  x,
float  y,
float  font_size,
int  font_id = 0,
const vec3 font_color = vec3(0, 0, 0),
bool  upper_left = true 
) const

Draw the text.

Parameters
textThe string to be drawn.
xThe x-coordinate of the starting position, relative to the left edge of the content area.
yThe y-coordinate of the starting position. If upper_left is true, this position is relative to the top edge of the content area. Otherwise, it is relative to the bottom edge of the content area.
font_sizeThe font size.
font_idThe id of the font used to draw the text, which is generated by calling to add_font(). 0 is the first font, 1 is the second, etc.
font_colorThe font color.
upper_leftTrue to define the origin at the upper left corner of the content area. Otherwise it is defined at the bottom left corner of the content area.
Returns
The end position of the x-coordinate. This allows you to draw another string after the previous one.
Note
: This function will draw the text in one line and thus line breaks (i.e., "\n") will be ignored.
Examples
Tutorial_203_Viewer_wxWidgets, and Tutorial_204_Viewer_Qt.

◆ font_height()

float font_height ( float  font_size) const

Query the height of the font.

Parameters
font_sizeThe font size.
Returns
The height of the font in pixels.

◆ font_names()

const std::vector< std::string > & font_names ( ) const
inline

The names of available fonts.

◆ kerning()

bool kerning ( ) const

Query the kerning behavior. Kerning is disabled by default.

Returns
The kerning behavior.

◆ num_fonts()

std::size_t num_fonts ( ) const
inline

The number of available fonts.

Examples
Tutorial_203_Viewer_wxWidgets, and Tutorial_204_Viewer_Qt.

◆ set_character_spacing()

void set_character_spacing ( float  spacing)

Set the spacing between consecutive characters. The default character spacing is 0.

Parameters
spacingThe expected character spacing.

◆ set_kerning()

void set_kerning ( bool  kerning)

Set kerning.

Parameters
kerningThe expected kerning behavior.

◆ string_bounding_rect()

Rect string_bounding_rect ( const std::string &  str,
float  x,
float  y,
float  font_size 
) const

Query the bounding rectangle of a string (treated as if with no space between characters).

Parameters
xThe x-coordinate of the starting position, relative to the left edge of the content area.
yThe y-coordinate of the starting position, relative to the top edge of the content area.
font_sizeThe font size.
Returns
The bounding rectangle of the string (in pixels).

◆ string_height()

float string_height ( const std::string &  str,
float  font_size 
) const

Query the height of a string.

Parameters
font_sizeThe font size.
Returns
The height of the string in pixels.

◆ string_width()

float string_width ( const std::string &  str,
float  font_size 
) const

Query the width of a string (treated as if with no space between characters). Useful to position a string. For example, with the string width, its X-center can be computed as: viewer_width * 0.5 - string_width * 0.5.

Parameters
font_sizeThe font size.
Returns
The width of the string in pixels.

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