Easy3D 2.6.1
Loading...
Searching...
No Matches
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. More...
 

Public Member Functions

 TextRenderer (float dpi_scale=1.0f, int texture_size=512, bool mipmaps=false)
 Constructor.
 
 ~TextRenderer ()
 
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.
 
std::size_t num_fonts () const
 The number of available fonts.
 
const std::vector< std::string > & font_names () const
 The names of available fonts.
 
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.
 
Rect draw (const std::string &text, float x, float y, float font_size, Align align, 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".
 
void set_character_spacing (float spacing)
 Set the spacing between consecutive characters. The default character spacing is 0.
 
float character_spacing () const
 Query the spacing between consecutive characters.
 
void set_kerning (bool kerning)
 Set kerning.
 
bool kerning () const
 Query the kerning behavior. Kerning is disabled by default.
 
float font_height (float font_size) const
 Query the height of the font.
 
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).
 
float string_height (const std::string &str, float font_size) const
 Query the height of a string.
 
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).
 

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_309_TextRendering/main.cpp.

Member Enumeration Documentation

◆ Align

enum Align

Horizontal alignment.

Enumerator
ALIGN_LEFT 

Align text to the left.

ALIGN_RIGHT 

Align text to the right.

ALIGN_CENTER 

Align text to the center.

Constructor & Destructor Documentation

◆ TextRenderer()

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

Constructor.

Parameters
dpi_scaleThe DPI scale factor.
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.).
Returns
True if the font was successfully added, false otherwise.

◆ 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,
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 in the upper left corner of the content area. Otherwise, it is defined in 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 in the upper left corner of the content area. Otherwise, it is defined in 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.

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

Returns
A const reference to the vector of font names.

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

Returns
The number of available fonts.

◆ 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
strThe string to measure.
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
strThe string to measure.
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
strThe string to measure.
font_sizeThe font size.
Returns
The width of the string in pixels.

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