Skip to main content

TextMesh.ixx File

Properties for positioning and styling text during rendering. More...

Included Headers

#include <memory> #include <span> #include <string> #include <limits> #include <algorithm> #include <utility> #include <vector> #include <helios.rendering.shader.UniformValueMap> #include <helios.math> #include <helios.rendering.shader.UniformSemantics> #include <helios.util.Colors> #include <helios.engine.core.data.FontId> #include <helios.rendering.shader.Shader> #include <helios.rendering.text.FontResourceProvider> #include <helios.rendering.text.TypeSetter> #include <helios.rendering.text.Glyph>

Namespaces Index

namespacehelios
namespacerendering

Graphics rendering infrastructure. More...

namespacetext

Text rendering abstractions and data types. More...

Classes Index

classTextMesh

Manages text content and caches layout data for efficient rendering. More...

Description

Properties for positioning and styling text during rendering.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file TextMesh.ixx
3 * @brief Properties for positioning and styling text during rendering.
4 */
5module;
6
7#include <memory>
8#include <span>
9#include <string>
10#include <limits>
11#include <algorithm>
12#include <utility>
13#include <vector>
14
15export module helios.rendering.text.TextMesh;
16
17import helios.rendering.text.Glyph;
18import helios.rendering.text.TypeSetter;
19
20import helios.rendering.text.FontResourceProvider;
21
22import helios.rendering.shader.Shader;
23import helios.engine.core.data.FontId;
24
25import helios.util.Colors;
26import helios.math;
27
28import helios.rendering.shader.UniformValueMap;
29import helios.rendering.shader.UniformSemantics;
30
31export namespace helios::rendering::text {
32
33 /**
34 * @brief Manages text content and caches layout data for efficient rendering.
35 *
36 * `TextMesh` encapsulates a text string along with its font and provides lazy
37 * computation of vertex data and bounding boxes. The layout is computed on-demand
38 * and cached until the text content changes.
39 *
40 * ## Caching Strategy
41 *
42 * - Layout computation is deferred until `vertices()` or `localAABB()` is called.
43 * - The cache is invalidated when `setText()` is called.
44 * - The `TypeSetter` is used internally to compute glyph positions.
45 *
46 * ## Usage
47 *
48 * ```cpp
49 * TextMesh mesh("Score: 0", FontId{1});
50 *
51 * // Access vertices for rendering (triggers layout if needed)
52 * auto verts = mesh.vertices(fontProvider);
53 *
54 * // Update text (invalidates cache)
55 * mesh.setText("Score: 100");
56 * ```
57 *
58 * @see TypeSetter
59 * @see TextRenderable
60 * @see FontResourceProvider
61 */
62 class TextMesh {
63
64 /**
65 * @brief Font identifier for glyph lookup.
66 */
68
69 /**
70 * @brief The text string to render.
71 */
72 std::string text_;
73
74 /**
75 * @brief Flag indicating whether the cached layout needs recomputation.
76 */
77 mutable bool needsUpdate_ = true;
78
79 /**
80 * @brief Cached layout data (AABB and vertices).
81 */
82 mutable TextLayout textLayout_;
83
84 /**
85 * @brief TypeSetter instance for computing text layout.
86 */
87 mutable TypeSetter typeSetter_;
88
89 /**
90 * @brief The scale factor for the font.
91 */
92 float fontScale_ = 1.0f;
93
94 /**
95 * @brief Updates the cached layout if needed.
96 *
97 * @param fontResourceProvider Provider for glyph data.
98 */
99 void updateCache(FontResourceProvider& fontResourceProvider) const {
100
101 if (!needsUpdate_) {
102 return;
103 }
104
105 textLayout_ = typeSetter_.layout(text_, fontScale_, fontId_, fontResourceProvider);
106
107 needsUpdate_ = false;
108 }
109
110 public:
111
112 /**
113 * @brief Constructs a TextMesh with the given text and font.
114 *
115 * @param text The text string to render.
116 * @param fontScale The scale of the font, relative to the pixelHeight it was loaded with.
117 * @param fontId The font identifier for glyph lookup.
118 */
119 explicit TextMesh(
120 std::string text,
121 const float fontScale,
123 ) noexcept
124 :
125 text_(std::move(text)),
126 fontScale_(fontScale),
127 fontId_(fontId) {}
128
129
130 /**
131 * @brief Returns the local-space axis-aligned bounding box.
132 *
133 * Triggers layout computation if the cache is invalid.
134 *
135 * @param fontResourceProvider Provider for glyph data.
136 *
137 * @return Reference to the cached AABB.
138 */
139 [[nodiscard]] const helios::math::aabbf& localAABB(FontResourceProvider& fontResourceProvider) const noexcept {
140 updateCache(fontResourceProvider);
141 return textLayout_.aabb;
142 }
143
144 /**
145 * @brief Returns the vertex data for rendering.
146 *
147 * Each glyph is represented as 6 vertices (2 triangles). Triggers layout
148 * computation if the cache is invalid.
149 *
150 * @param fontResourceProvider Provider for glyph data.
151 *
152 * @return A span of vertex data (x, y, texU, texV per vertex).
153 */
154 [[nodiscard]] std::span<helios::math::vec4f> vertices(FontResourceProvider& fontResourceProvider) const noexcept {
155 updateCache(fontResourceProvider);
156 return textLayout_.vertices;
157 }
158
159 /**
160 * @brief Updates the text string.
161 *
162 * Invalidates the cached layout, which will be recomputed on the next
163 * call to `vertices()` or `localAABB()`.
164 *
165 * @param text The new text to render.
166 */
167 void setText(std::string text) noexcept {
168 text_ = std::move(text);
169 needsUpdate_ = true;
170 }
171
172 /**
173 * @brief Returns the current text string.
174 *
175 * @return View of the text string.
176 */
177 [[nodiscard]] std::string_view text() const noexcept {
178 return text_;
179 }
180
181 /**
182 * @brief Sets the font scale factor.
183 *
184 * Invalidates the cached layout, which will be recomputed on the next
185 * call to `vertices()` or `localAABB()`.
186 *
187 * @param scale The new scale factor relative to the font's pixel height.
188 */
189 void setFontScale(const float scale) noexcept {
190 fontScale_ = scale;
191 needsUpdate_ = true;
192 }
193
194 /**
195 * @brief Returns the current font scale factor.
196 *
197 * @return The scale factor relative to the font's pixel height.
198 */
199 [[nodiscard]] float fontScale() const noexcept {
200 return fontScale_;
201 }
202
203 /**
204 * @brief Checks if the cached layout needs recomputation.
205 *
206 * @return True if the layout is invalid and needs to be recomputed.
207 */
208 bool needsUpdate() const noexcept {
209 return needsUpdate_;
210 }
211
212 /**
213 * @brief Returns the font identifier.
214 *
215 * @return The font ID used for glyph lookup.
216 */
217 [[nodiscard]] helios::engine::core::data::FontId fontId() const noexcept {
218 return fontId_;
219 }
220 };
221
222
223}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.