Easy3D 2.6.1
Loading...
Searching...
No Matches
text_renderer.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_RENDERER_TEXT_RENDERER_H
28#define EASY3D_RENDERER_TEXT_RENDERER_H
29
30#include <vector>
31#include <string>
32
33#include <easy3d/core/types.h>
34
35
36namespace easy3d {
37
53 public:
60 explicit TextRenderer(float dpi_scale = 1.0f, int texture_size = 512, bool mipmaps = false);
61
66
72 bool add_font(const std::string &font_file);
73
78 std::size_t num_fonts() const { return font_ids_.size(); }
79
84 const std::vector<std::string> &font_names() const { return font_names_; }
85
102 float draw(const std::string &text, float x, float y, float font_size,
103 int font_id = 0, const vec3 &font_color = vec3(0, 0, 0), bool upper_left = true
104 ) const;
105
106
107 // -------------------------------------------------------------------------------------------------------------
108 // Multi-line text rendering
109
116
134 Rect draw(const std::string &text, float x, float y, float font_size, Align align/* = ALIGN_LEFT*/,
135 int font_id = 0, const vec3 &font_color = vec3(0, 0, 0),
136 float line_spacing = 0.0f, bool upper_left = true
137 ) const;
138
139 // -------------------------------------------------------------------------------------------------------------
140
145 void set_character_spacing(float spacing);
146
151 float character_spacing() const;
152
157 void set_kerning(bool kerning);
158
163 bool kerning() const;
164
170 float font_height(float font_size) const;
171
180 float string_width(const std::string& str, float font_size) const;
181
188 float string_height(const std::string& str, float font_size) const;
189
198 Rect string_bounding_rect(const std::string& str, float x, float y, float font_size) const;
199
200 private:
201
202 void flush_draw(const vec3 &font_color) const;
203
204 //if the text has newlines, it will be treated as if was called into drawing multiple lines
205 Rect _get_bbox(const std::string &text, float size, float x, float y, Align align, float line_spacing) const;
206
207 private:
208 int texture_size_;
209 void *stash_;
210 std::vector<int> font_ids_;
211 std::vector<std::string> font_names_;
212 };
213
214}
215
216#endif // EASY3D_RENDERER_TEXT_RENDERER_H
float string_height(const std::string &str, float font_size) const
Query the height of a string.
Definition text_renderer.cpp:806
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.
Definition text_renderer.cpp:817
bool kerning() const
Query the kerning behavior. Kerning is disabled by default.
Definition text_renderer.cpp:781
~TextRenderer()
Definition text_renderer.cpp:736
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.
Definition text_renderer.cpp:741
void set_kerning(bool kerning)
Set kerning.
Definition text_renderer.cpp:775
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).
Definition text_renderer.cpp:812
void set_character_spacing(float spacing)
Set the spacing between consecutive characters. The default character spacing is 0.
Definition text_renderer.cpp:761
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).
Definition text_renderer.cpp:800
Align
Horizontal alignment.
Definition text_renderer.h:111
@ ALIGN_CENTER
Align text to the center.
Definition text_renderer.h:114
@ ALIGN_LEFT
Align text to the left.
Definition text_renderer.h:112
@ ALIGN_RIGHT
Align text to the right.
Definition text_renderer.h:113
float character_spacing() const
Query the spacing between consecutive characters.
Definition text_renderer.cpp:767
const std::vector< std::string > & font_names() const
The names of available fonts.
Definition text_renderer.h:84
TextRenderer(float dpi_scale=1.0f, int texture_size=512, bool mipmaps=false)
Constructor.
Definition text_renderer.cpp:723
float font_height(float font_size) const
Query the height of the font.
Definition text_renderer.cpp:789
std::size_t num_fonts() const
The number of available fonts.
Definition text_renderer.h:78
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
GenericRect< float > Rect
A 2D axis-aligned rectangle of float type.
Definition types.h:111