This example shows how to render strings using Easy3D.
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>
39TutorialTextRendering::TutorialTextRendering(
const std::string &title)
42 , font_size_delta_(0.0f)
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";
59TutorialTextRendering::~TutorialTextRendering() {
67void TutorialTextRendering::init() {
72 std::vector<std::string> files;
73 file_system::get_directory_entries(resource::directory() +
"/fonts/", files,
false);
74 for (
const auto& file : files) {
75 if (file_system::extension(file) ==
"ttf") {
76 texter_->add_font(resource::directory() +
"/fonts/" + file);
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;
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);
96 else if (key == KEY_EQUAL) {
97 font_size_delta_ = std::min(font_size_delta_ + 1.0f, 250.0f);
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));
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));
115 else if (key == KEY_DOWN) {
116 line_spacing_ = std::max(line_spacing_ - 0.1f, -1.0f);
120 else if (key == KEY_UP) {
121 line_spacing_ = std::min(line_spacing_ + 0.1f, 2.0f);
126 else if (key == KEY_L) {
127 alignment_ = TextRenderer::ALIGN_LEFT;
132 else if (key == KEY_C) {
133 alignment_ = TextRenderer::ALIGN_CENTER;
138 else if (key == KEY_R) {
139 alignment_ = TextRenderer::ALIGN_RIGHT;
144 else if (key == KEY_O) {
145 upper_left_ = !upper_left_;
150 else if (key == KEY_SPACE) {
151 const bool kerning = texter_->kerning();
152 texter_->set_kerning(!kerning);
158 return Viewer::key_press_event(key, modifiers);
162void TutorialTextRendering::draw()
const {
165 if (!texter_ || texter_->num_fonts() == 0)
168 const float font_size = 28.0f + font_size_delta_;
172 const auto num_fonts = texter_->num_fonts();
173 const float font_height = texter_->font_height(font_size);
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_);
190 for (
int i = 0; i < num_fonts; ++i) {
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_);
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;
Align
Horizontal alignment.
Definition text_renderer.h:111
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