Skip to main content

TypeSetter Class

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

Declaration

class helios::rendering::text::TypeSetter { ... }

Public Member Functions Index

TextLayoutlayout (const std::string_view text, const float scale, const helios::engine::core::data::FontId fontId, FontResourceProvider &fontResourceProvider)

Computes the layout for a text string. More...

Description

Computes text layout by positioning glyphs and generating vertex data.

The `TypeSetter` is responsible for transforming a text string into renderable geometry. It queries glyph metrics from a `FontResourceProvider` and generates vertex data suitable for GPU rendering.

## Layout Process

1. Iterate over each character in the text string. 2. Query glyph metrics (size, bearing, advance) from the font provider. 3. Compute vertex positions based on the pen position and glyph metrics. 4. Generate two triangles (6 vertices) per glyph with texture coordinates. 5. Advance the pen position by the glyph's advance width. 6. Compute the overall bounding box.

## Vertex Format

Each vertex is a `vec4f` containing:

  • `x, y`: Screen-space position.
  • `z, w`: Texture coordinates (u, v).

## Usage

```cpp TypeSetter typeSetter; TextLayout layout = typeSetter.layout("Hello", fontId, fontProvider);

// Use layout.vertices for rendering // Use layout.aabb for bounds checking ```

See Also

TextLayout

See Also

TextMesh

See Also

FontResourceProvider

Definition at line 90 of file TypeSetter.ixx.

Public Member Functions

layout()

TextLayout helios::rendering::text::TypeSetter::layout (const std::string_view text, const float scale, const helios::engine::core::data::FontId fontId, FontResourceProvider & fontResourceProvider)
inline

Computes the layout for a text string.

Generates vertex data and computes the bounding box for the given text using the specified font.

Parameters
text

The text string to lay out.

scale

The scaling used for rendering the font, relative to its origin pixel-height

fontId

The font to use for glyph metrics.

fontResourceProvider

Provider for glyph data.

Returns

A `TextLayout` containing the AABB and vertex data.

Definition at line 108 of file TypeSetter.ixx.

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 }

References helios::rendering::text::Glyph::bearing and helios::rendering::text::FontResourceProvider::glyph.


The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.