TextRenderer enables quick and easy string rendering in OpenGL applications.
More...
#include <easy3d/renderer/text_renderer.h>
|
enum | Align { ALIGN_LEFT
, ALIGN_RIGHT
, ALIGN_CENTER
} |
| Horizontal alignment.
|
|
|
| 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 |
|
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.
◆ TextRenderer()
TextRenderer |
( |
float |
dpi_scale = 1.0f , |
|
|
int |
texture_size = 512 , |
|
|
bool |
mipmaps = false |
|
) |
| |
|
explicit |
Constructor.
- Parameters
-
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 | ) |
|
◆ 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
-
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 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
-
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 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
-
- 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()
Query the kerning behavior. Kerning is disabled by default.
- Returns
- The kerning behavior.
◆ num_fonts()
std::size_t num_fonts |
( |
| ) |
const |
|
inline |
◆ 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
-
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
-
- 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
-
- Returns
- The width of the string in pixels.
The documentation for this class was generated from the following files:
- /Users/lnan/Documents/Projects/Easy3D/easy3d/renderer/text_renderer.h
- /Users/lnan/Documents/Projects/Easy3D/easy3d/renderer/text_renderer.cpp