TextRenderer enables quick and easy string rendering in OpenGL applications.
More...
#include <easy3d/renderer/text_renderer.h>
|
| 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).
|
|
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.
◆ 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.
|
◆ TextRenderer()
TextRenderer |
( |
float | dpi_scale = 1.0f, |
|
|
int | texture_size = 512, |
|
|
bool | mipmaps = false ) |
|
explicit |
Constructor.
- Parameters
-
dpi_scale | The DPI scale factor. |
texture_size | The size of the font textures. Suggested values are 512, 1024, 2048 ... |
mipmaps | True to create mipmaps for the font textures. |
◆ ~TextRenderer()
◆ 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_file | The 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
-
text | The string to be drawn. |
x | The x-coordinate of the starting position, relative to the left edge of the content area. |
y | The 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_size | The font size. |
align | Specifies how the lines are aligned. |
font_id | The 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_color | The font color. |
line_spacing | The line spacing, which is a ratio relative to line height. A zero value means no extra space between lines. |
upper_left | True 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
-
text | The string to be drawn. |
x | The x-coordinate of the starting position, relative to the left edge of the content area. |
y | The 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_size | The font size. |
font_id | The 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_color | The font color. |
upper_left | True 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
-
- 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()
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
-
spacing | The expected character spacing. |
◆ set_kerning()
void set_kerning |
( |
bool | kerning | ) |
|
Set kerning.
- Parameters
-
kerning | The 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
-
str | The string to measure. |
x | The x-coordinate of the starting position, relative to the left edge of the content area. |
y | The y-coordinate of the starting position, relative to the top edge of the content area. |
font_size | The 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
-
str | The string to measure. |
font_size | The 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
-
str | The string to measure. |
font_size | The font size. |
- Returns
- The width of the string in pixels.
The documentation for this class was generated from the following files:
- G:/3_code/Easy3D/easy3d/renderer/text_renderer.h
- G:/3_code/Easy3D/easy3d/renderer/text_renderer.cpp