Skip to main content

TypeSetter.ixx File

Text layout engine for computing glyph positions and bounding boxes. More...

Included Headers

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

Namespaces Index

namespacehelios
namespacerendering

Graphics rendering infrastructure. More...

namespacetext

Text rendering abstractions and data types. More...

Classes Index

structTextLayout

Result of text layout computation. More...

classTypeSetter

Computes text layout by positioning glyphs and generating vertex data. More...

Description

Text layout engine for computing glyph positions and bounding boxes.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file TypeSetter.ixx
3 * @brief Text layout engine for computing glyph positions and bounding boxes.
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.TypeSetter;
16
17
18import helios.rendering.text.Glyph;
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
28
29
30
31export namespace helios::rendering::text {
32
33 /**
34 * @brief Result of text layout computation.
35 *
36 * Contains the axis-aligned bounding box and vertex data for a laid-out text string.
37 */
38 struct TextLayout {
39
40 /**
41 * @brief Axis-aligned bounding box enclosing all glyphs.
42 */
44
45 /**
46 * @brief Vertex data for all glyphs (position and texture coordinates).
47 *
48 * Each glyph consists of 6 vertices (2 triangles). Each vertex is a `vec4f`
49 * containing `(x, y, texU, texV)`.
50 */
51 std::vector<helios::math::vec4f> vertices;
52 };
53
54 /**
55 * @brief Computes text layout by positioning glyphs and generating vertex data.
56 *
57 * The `TypeSetter` is responsible for transforming a text string into renderable
58 * geometry. It queries glyph metrics from a `FontResourceProvider` and generates
59 * vertex data suitable for GPU rendering.
60 *
61 * ## Layout Process
62 *
63 * 1 Iterate over each character in the text string.
64 * 2 Query glyph metrics (size, bearing, advance) from the font provider.
65 * 3 Compute vertex positions based on the pen position and glyph metrics.
66 * 4 Generate two triangles (6 vertices) per glyph with texture coordinates.
67 * 5 Advance the pen position by the glyph's advance width.
68 * 6 Compute the overall bounding box.
69 *
70 * ## Vertex Format
71 *
72 * Each vertex is a `vec4f` containing:
73 * - `x, y`: Screen-space position.
74 * - `z, w`: Texture coordinates (u, v).
75 *
76 * ## Usage
77 *
78 * ```cpp
79 * TypeSetter typeSetter;
80 * TextLayout layout = typeSetter.layout("Hello", fontId, fontProvider);
81 *
82 * // Use layout.vertices for rendering
83 * // Use layout.aabb for bounds checking
84 * ```
85 *
86 * @see TextLayout
87 * @see TextMesh
88 * @see FontResourceProvider
89 */
90 class TypeSetter {
91
92
93 public:
94
95 /**
96 * @brief Computes the layout for a text string.
97 *
98 * Generates vertex data and computes the bounding box for the given text
99 * using the specified font.
100 *
101 * @param text The text string to lay out.
102 * @param scale The scaling used for rendering the font, relative to its origin pixel-height
103 * @param fontId The font to use for glyph metrics.
104 * @param fontResourceProvider Provider for glyph data.
105 *
106 * @return A `TextLayout` containing the AABB and vertex data.
107 */
109 const std::string_view text,
110 const float scale,
112 FontResourceProvider& fontResourceProvider) {
113
114 helios::math::aabbf aabb = {};
115
116 if (text.empty()) {
117 return TextLayout {aabb};
118 }
119
120 std::vector<helios::math::vec4f> vertices = {};
121 vertices.reserve(text.size() * 6);
122
123 float penX = 0.0f;
124 float penY = 0.0f;
125
126 float minX = std::numeric_limits<float>::max();
127 float minY = std::numeric_limits<float>::max();
128 float minZ = 0.0f;
129
130 float maxX = std::numeric_limits<float>::lowest();
131 float maxY = std::numeric_limits<float>::lowest();
132 float maxZ = 1.0f;
133
134 for (char c : text) {
135 auto glyph = fontResourceProvider.glyph(c, fontId);
136
137 float xpos = penX + glyph.bearing[0] * scale;
138 float ypos = penY - (glyph.size[1] - glyph.bearing[1]) * scale;
139 const float w = glyph.size[0] * scale;
140 const float h = glyph.size[1] * scale;
141
142 vertices.push_back({xpos, ypos +h, 0.0f, 0.0f});
143 vertices.push_back({xpos, ypos, 0.0f, 1.0f});
144 vertices.push_back({xpos + w, ypos, 1.0f, 1.0f});
145
146 vertices.push_back({xpos, ypos +h, 0.0f, 0.0f});
147 vertices.push_back({xpos + w, ypos, 1.0f, 1.0f});
148 vertices.push_back({xpos+w, ypos +h, 1.0f, 0.0f});
149
150 minX = std::min(minX, xpos);
151 maxX = std::max(maxX, xpos + w);
152
153 minY = std::min(minY, ypos);
154 maxY = std::max(maxY, ypos + h);
155
156 penX += (glyph.advance >> 6) * scale;
157 }
158
159 aabb = helios::math::aabbf(minX, minY, minZ, maxX, maxY, maxZ);
160
161 return {
162 aabb, std::move(vertices)
163 };
164 }
165
166
167
168 };
169
170
171}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.