This example shows how to render strings using Easy3D.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "viewer.h"
28
29#include <easy3d/renderer/text_renderer.h>
30#include <easy3d/util/file_system.h>
31#include <easy3d/util/resource.h>
32#include <easy3d/core/random.h>
33
34
36
37
38
39TutorialTextRendering::TutorialTextRendering(const std::string &title)
41 , texter_(nullptr)
42 , font_size_delta_(0.0f)
43 , line_spacing_(0.0f)
45 , upper_left_(true)
46{
47 manual_ =
48 "----------------------- Text Rendering usage ------------------------ \n"
49 "Press '+'/'-' to increase/decrease font size \n"
50 "Press '<'/'>' to increase/decrease character spacing \n"
51 "Press 'up'/'down' to increase/decrease line spacing \n"
52 "Press 'l'/'c'/'r' to left/center/right align the multi-line text \n"
53 "Press 'o' to switch the origin between 'upper left' and 'bottom left' \n"
54 "Press 'space' to enable/disable kerning \n"
55 "--------------------------------------------------------------------- \n";
56}
57
58
59TutorialTextRendering::~TutorialTextRendering() {
60 delete texter_;
61
62
63
64}
65
66
67void TutorialTextRendering::init() {
68 Viewer::init();
69
71
72 std::vector<std::string> files;
74 for (const auto& file : files) {
78 }
79 }
80
81#if 0
82 const auto& names = texter_->font_names();
83 std::cout << "available fonts: " << std::endl;
84 for (std::size_t i =0; i< names.size(); ++i)
85 std::cout << "\tfont " << i << ": " << names[i] << std::endl;
86#endif
87}
88
89
90bool TutorialTextRendering::key_press_event(int key, int modifiers) {
91 if (key == KEY_MINUS) {
92 font_size_delta_ = std::max(font_size_delta_ - 1.0f, -20.0f);
93 update();
94 return true;
95 }
96 else if (key == KEY_EQUAL) {
97 font_size_delta_ = std::min(font_size_delta_ + 1.0f, 250.0f);
98 update();
99 return true;
100 }
101
102 else if (key == KEY_COMMA) {
103 const float spacing = texter_->character_spacing();
104 texter_->set_character_spacing(std::max(spacing - 0.5f, 0.0f));
105 update();
106 return true;
107 }
108 else if (key == KEY_PERIOD) {
109 const float spacing = texter_->character_spacing();
110 texter_->set_character_spacing(std::min(spacing + 0.5f, 50.0f));
111 update();
112 return true;
113 }
114
115 else if (key == KEY_DOWN) {
116 line_spacing_ = std::max(line_spacing_ - 0.1f, -1.0f);
117 update();
118 return true;
119 }
120 else if (key == KEY_UP) {
121 line_spacing_ = std::min(line_spacing_ + 0.1f, 2.0f);
122 update();
123 return true;
124 }
125
126 else if (key == KEY_L) {
128 update();
129 return true;
130 }
131
132 else if (key == KEY_C) {
134 update();
135 return true;
136 }
137
138 else if (key == KEY_R) {
140 update();
141 return true;
142 }
143
144 else if (key == KEY_O) {
145 upper_left_ = !upper_left_;
146 update();
147 return true;
148 }
149
150 else if (key == KEY_SPACE) {
151 const bool kerning = texter_->kerning();
152 texter_->set_kerning(!kerning);
153 update();
154 return true;
155 }
156
157 else
158 return Viewer::key_press_event(key, modifiers);
159}
160
161
162void TutorialTextRendering::draw() const {
163 Viewer::draw();
164
165 if (!texter_ || texter_->num_fonts() == 0)
166 return;
167
168 const float font_size = 28.0f + font_size_delta_;
169 float x = 50.0f;
170 float y = 80.0f;
171
172 const auto num_fonts = texter_->num_fonts();
173 const float font_height = texter_->font_height(font_size);
174
175 auto rect = texter_->draw(
176 "This example shows how to render strings with Easy3D\n"
177 "'+'/'-': increase/decrease font size\n"
178 "'<'/'>': increase/decrease character spacing\n"
179 "'up'/'down': increase/decrease line spacing\n"
180 "'l'/'c'/'r': left/center/right align the multi-line text\n"
181 "'o': switch the origin between 'upper left' and 'bottom left'\n"
182 "'space': enable/disable kerning",
184 line_spacing_, upper_left_);
185
186
187 y += rect.height();
188
189 float next_x = 0.0f;
190 for (int i = 0; i < num_fonts; ++i) {
191 if (i % 2 == 0) {
192 next_x = texter_->draw(std::to_string(i) + " - Easy3D makes 3D easy! ", x * dpi_scaling(),
193 y * dpi_scaling(), font_size, i, colors_[i], upper_left_);
194 } else {
195 texter_->draw(std::to_string(i) + " - I Love Easy3D!", next_x * dpi_scaling(), y * dpi_scaling(), font_size,
196 i, colors_[i], upper_left_);
197 y += font_height * 1.5f;
198 }
199 }
200}
201
202
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
void get_directory_entries(const std::string &path, std::vector< std::string > &entries, bool recursive)
Queries the entries of a directory (including subdirectories and files).
std::string extension(const std::string &path, bool lower=true)
Query the file extension without dot (e.g., /a/b/c.Ext => Ext).
std::string directory()
Returns the resource directory (containing color maps, shaders, textures, fonts, etc....
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
vec3 random_color(bool allow_dark=false)
Generates a random color. The parameter allow_dark controls if too dark colors are allowed.
Definition random.h:49