Rendering 2D strings (labels, HUDs, annotations) with Easy3D's text layout engine.
This tutorial draws text on top of a 3D scene: a multi-line instruction block plus a gallery line for every installed font, each in a different color. All layout and rasterization happen on the CPU in a TextLayoutEngine; the resulting textured quads are handed to the viewer as overlay draw commands. The TutorialTextRendering class below rebuilds the text whenever a layout parameter changes (font size, spacing, alignment, origin, kerning, window size, DPI).
Real-time text rendering is usually done with a glyph (font) atlas: each needed character is rasterized once from the TrueType outlines into a small region of a texture, and drawing a string then becomes drawing one textured quad per character, sampling its glyph from the atlas. Layout decides where each quad goes: advancing by each glyph's width, applying extra character/line spacing, aligning shorter lines within the longest one, and optionally kerning (tightening specific pairs like "AV" using the font's kerning table). Because glyph bitmaps are resolution-dependent, sizes are specified in framebuffer pixels: on high-DPI displays the framebuffer is larger than the window, so the tutorial scales all sizes by framebuffer_width()/width() to keep the text crisp and physically the same size. Layouts are immutable snapshots, so previously issued draw commands stay valid even after the cache is cleared and new glyphs are rasterized.
'+'/'-' change the font size, '<'/'>' the character spacing, 'up'/'down' the line spacing, 'l'/'c'/'r' the alignment of the multi-line block, 'o' switches the origin between upper-left and bottom-left, and 'space' toggles kerning. The usual viewer navigation still works.
30#include <easy3d/renderer/text_layout.h>
31#include <easy3d/util/file_system.h>
32#include <easy3d/util/resource.h>
38#include "easy3d/core/random.h"
43TutorialTextRendering::TutorialTextRendering(
const std::string &title) :
Viewer(title) {
46 "----------------------- Text Rendering usage ------------------------ \n"
47 "Press '+'/'-' to increase/decrease font size \n"
48 "Press '<'/'>' to increase/decrease character spacing \n"
49 "Press 'up'/'down' to increase/decrease line spacing \n"
50 "Press 'l'/'c'/'r' to left/center/right align the multi-line text \n"
51 "Press 'o' to switch the origin between 'upper left' and 'bottom left' \n"
52 "Press 'space' to enable/disable kerning \n"
53 "--------------------------------------------------------------------- \n";
57 std::vector<std::string> files;
59 std::sort(files.begin(), files.end());
60 for (
const auto &file : files) {
62 std::transform(extension.begin(), extension.end(), extension.begin(),
63 [](
unsigned char c) { return char(std::tolower(c)); });
64 if (extension ==
"ttf")
68 if (text_.font_names().empty())
69 throw std::runtime_error(
"Tutorial 309 requires the resource fonts");
72TutorialTextRendering::~TutorialTextRendering() =
default;
76bool TutorialTextRendering::key_press_event(
int key,
int modifiers) {
77 using easy3d::graphics::TextAlign;
80 size_ = std::max(8.0f, size_ - 1);
84 size_ = std::min(278.0f, size_ + 1);
88 spacing_ = std::max(0.0f, spacing_ - 0.5f);
92 spacing_ = std::min(50.0f, spacing_ + 0.5f);
95 line_spacing_ = std::max(-1.0f, line_spacing_ - 0.1f);
98 line_spacing_ = std::min(2.0f, line_spacing_ + 0.1f);
101 align_ = TextAlign::left;
104 align_ = TextAlign::center;
107 align_ = TextAlign::right;
110 upper_left_ = !upper_left_;
113 kerning_ = !kerning_;
116 return Viewer::key_press_event(key, modifiers);
125void TutorialTextRendering::post_resize(
int,
int) {
130void TutorialTextRendering::pre_draw() {
137 framebuffer_size(fw, fh);
138 const float dpi = w > 0 ? float(fw) / w : 1;
149 o.
size = size_ * dpi;
159 auto intro = text_.layout(
160 "This example shows how to render strings with Easy3D\n" "'+'/'-': increase/decrease font size\n" "'<'/'>': increase/decrease character spacing\n" "'up'/'down': increase/decrease line spacing\n" "'l'/'c'/'r': left/center/right align the multi-line text\n" "'o': switch the origin between 'upper left' and 'bottom left'\n" "'space': enable/disable kerning",
162 auto commands = std::move(intro.commands);
164 o.
y += 7 * intro.line_height + size_ * dpi * 0.5f;
165 o.
align = easy3d::graphics::TextAlign::left;
167 const auto &names = text_.font_names();
169 for (std::size_t i = 0; i < names.size(); ++i) {
172 o.
x = (i % 2) ? next_x : 50 * dpi;
174 const auto label = std::to_string(i) + (i % 2 ?
" - I Love Easy3D!" :
" - Easy3D makes 3D easy! ");
175 auto line = text_.layout(label, fw, fh, o);
177 next_x = o.
x + line.advance;
178 commands.insert(commands.end(), line.commands.begin(), line.commands.end());
180 o.
y += size_ * dpi * 1.5f;
183 set_overlays(std::move(commands));
std::string extension(const std::string &file_name, bool lowercase)
Query the file extension without dot (e.g., /a/b/c.Ext => Ext).
Definition file_system.cpp:315
std::string directory()
Get the selected resource directory.
vec3 color_from_index(std::size_t index)
Generate a color by an index. Colors from different indices have visual distinction.
Definition random.h:146
Font and placement options for a laid-out UTF-8 string.
Definition text_layout.h:40
bool upper_left
Definition text_layout.h:59
vec4 color
Text color in RGBA.
Definition text_layout.h:48
float character_spacing
Additional horizontal spacing between characters, in framebuffer pixels.
Definition text_layout.h:52
float line_spacing
Additional line spacing as a fraction of font line height; must be at least -1.
Definition text_layout.h:54
TextAlign align
Alignment of shorter lines relative to the longest line.
Definition text_layout.h:50
float y
Definition text_layout.h:46
float size
Font size in framebuffer pixels, independent of platform DPI.
Definition text_layout.h:44
float x
Horizontal framebuffer-pixel origin.
Definition text_layout.h:45
std::size_t font
Index returned by TextLayoutEngine::add_font().
Definition text_layout.h:42
bool kerning
Apply the font's horizontal kerning pairs when positioning glyphs.
Definition text_layout.h:56