Easy3D 2.5.3
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
54 public:
60 explicit TextRenderer(float dpi_scale = 1.0f, int texture_size = 512, bool mipmaps = false);
61
66
71 bool add_font(const std::string &font_file);
72
76 std::size_t num_fonts() const { return font_ids_.size(); }
77
81 const std::vector<std::string> &font_names() const { return font_names_; }
82
99 float draw(const std::string &text, float x, float y, float font_size, int font_id = 0,
100 const vec3 &font_color = vec3(0, 0, 0), bool upper_left = true
101 ) const;
102
103
104 // -------------------------------------------------------------------------------------------------------------
105 // Multi-line text rendering
106
108 enum Align {
109 ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER
110 };
111
129 Rect draw(const std::string &text, float x, float y, float font_size, Align align = ALIGN_LEFT,
130 int font_id = 0, const vec3 &font_color = vec3(0, 0, 0),
131 float line_spacing = 0.0f, bool upper_left = true) const;
132
133 // -------------------------------------------------------------------------------------------------------------
134
139 void set_character_spacing(float spacing);
140
145 float character_spacing() const;
146
151 void set_kerning(bool kerning);
152
157 bool kerning() const;
158
164 float font_height(float font_size) const;
165
172 float string_width(const std::string& str, float font_size) const;
173
179 float string_height(const std::string& str, float font_size) const;
180
188 Rect string_bounding_rect(const std::string& str, float x, float y, float font_size) const;
189
190 private:
191
192 void flush_draw(const vec3 &font_color) const;
193
194 //if the text has newlines, it will be treated as if was called into drawing multiple lines
195 Rect _get_bbox(const std::string &text, float size, float x, float y, Align align, float line_spacing) const;
196
197 private:
198 int texture_size_;
199 void *stash_;
200 std::vector<int> font_ids_;
201 std::vector<std::string> font_names_;
202 };
203
204}
205
206#endif // EASY3D_RENDERER_TEXT_RENDERER_H
The GenericRect class defines a rectangle in the 2D space.
Definition: rect.h:42
TextRenderer enables quick and easy string rendering in OpenGL applications.
Definition: text_renderer.h:53
float string_height(const std::string &str, float font_size) const
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
Definition: text_renderer.cpp:818
bool kerning() const
Definition: text_renderer.cpp:781
~TextRenderer()
Definition: text_renderer.cpp:736
bool add_font(const std::string &font_file)
Definition: text_renderer.cpp:741
void set_kerning(bool kerning)
Definition: text_renderer.cpp:775
Rect string_bounding_rect(const std::string &str, float x, float y, float font_size) const
Definition: text_renderer.cpp:812
void set_character_spacing(float spacing)
Definition: text_renderer.cpp:761
float string_width(const std::string &str, float font_size) const
Definition: text_renderer.cpp:800
Align
Horizontal alignment.
Definition: text_renderer.h:108
float character_spacing() const
Definition: text_renderer.cpp:767
const std::vector< std::string > & font_names() const
Definition: text_renderer.h:81
TextRenderer(float dpi_scale=1.0f, int texture_size=512, bool mipmaps=false)
Definition: text_renderer.cpp:723
float font_height(float font_size) const
Definition: text_renderer.cpp:789
std::size_t num_fonts() const
Definition: text_renderer.h:76
Definition: collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition: types.h:45