Easy3D 3.0.0
Loading...
Searching...
No Matches
text_layout.h
1
28#ifndef EASY3D_RENDERER_TEXT_LAYOUT_H
29#define EASY3D_RENDERER_TEXT_LAYOUT_H
30
31#include <easy3d/renderer/render_device.h>
32
33namespace easy3d {
34 namespace graphics {
35
37 enum class TextAlign { left, center, right };
38
40 struct TextOptions {
42 std::size_t font = 0;
44 float size = 24;
45 float x = 0;
46 float y = 0;
48 vec4 color = vec4(0, 0, 0, 1);
50 TextAlign align = TextAlign::left;
54 float line_spacing = 0;
56 bool kerning = false;
59 bool upper_left = true;
60 };
61
63 struct TextLayout {
65 std::vector<DrawCommand> commands;
67 Rect bounds = Rect(0, 0, 0, 0);
69 float advance = 0;
71 float line_height = 0;
72 };
73
86 public:
91 explicit TextLayoutEngine(unsigned atlas_size = 512);
92
95
98 TextLayoutEngine &operator=(const TextLayoutEngine &) = delete;
99
106 std::size_t add_font(const std::string &filename);
107
110 const std::vector<std::string> &font_names() const;
111
121 layout(const std::string &utf8, unsigned width, unsigned height, const TextOptions &options = {});
122
127 void clear_cache();
128
129 private:
130 struct Impl;
131 std::unique_ptr<Impl> impl_;
132 };
133 } // namespace graphics
134} // namespace easy3d
135#endif
Rasterize TrueType glyphs and produce backend-independent text commands.
Definition text_layout.h:85
~TextLayoutEngine()
Destroy the engine; already returned layouts remain valid.
std::size_t add_font(const std::string &filename)
Load a TrueType font.
Definition text_layout.cpp:162
const std::vector< std::string > & font_names() const
Definition text_layout.cpp:155
void clear_cache()
Release cached glyphs and atlas pages.
Definition text_layout.cpp:158
TextLayout layout(const std::string &utf8, unsigned width, unsigned height, const TextOptions &options={})
Lay out UTF-8 text into immutable glyph draw commands.
Definition text_layout.cpp:182
TextLayoutEngine(const TextLayoutEngine &)=delete
Copying is disabled because the engine owns mutable font and glyph caches.
Definition collider.cpp:182
GenericRect< float > Rect
A 2D axis-aligned rectangle of float type.
Definition types.h:111
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46
Immutable draw commands and metrics produced for one text layout.
Definition text_layout.h:63
float advance
Advance of the last line, including its alignment offset.
Definition text_layout.h:69
std::vector< DrawCommand > commands
Draw commands containing glyph geometry and atlas snapshots.
Definition text_layout.h:65
Rect bounds
Actual ink bounds in top-left framebuffer pixel coordinates.
Definition text_layout.h:67
float line_height
Font line height in framebuffer pixels.
Definition text_layout.h:71
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