guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
text_renderable.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <algorithm>
26#include <cstdint>
27#include <limits>
28#include <memory>
29#include <string>
30#include <vector>
31
32#include <utility/graphic/color.hpp>
33#include <utility/graphic/mesh.hpp>
34#include <utility/graphic/pose.hpp>
35#include <utility/graphic/position.hpp>
36#include <utility/graphic/text/text.hpp>
37#include <utility/ressource_provider.hpp>
38
40
41namespace guillaume
42{
69 class TextRenderable: public utility::graphic::Text
70 {
71 public:
90 std::shared_ptr<utility::RessourceProvider> ressourceProvider,
91 const utility::graphic::PoseF &anchor,
92 const utility::graphic::Color32Bit &color,
93 const std::string &content, uint32_t fontSize,
94 const std::string &font)
95 : utility::graphic::Text(ressourceProvider,
96 utility::graphic::PoseF(), color, content,
97 fontSize, font)
98 {
99 bake(anchor);
100 }
101
105 ~TextRenderable(void) override = default;
106
107 private:
117 void bake(const utility::graphic::PoseF &anchor)
118 {
119 if (_meshes.empty() || _meshes.front() == nullptr) {
120 return;
121 }
122
123 const auto &source = _meshes.front();
124 const auto &vertices = source->getVertices();
125
126 if (vertices.empty()) {
127 return;
128 }
129
130 float minX = std::numeric_limits<float>::max();
131 float maxY = std::numeric_limits<float>::lowest();
132
133 for (const auto &vertex: vertices) {
134 const auto &position = vertex.getPosition();
135 minX = std::min(minX, position[0]);
136 maxY = std::max(maxY, position[1]);
137 }
138
139 // Solve T + R * localTopLeft = anchorPosition so the glyph-box
140 // top-left lands exactly on the entity position.
141 const utility::graphic::PositionF localTopLeft(minX, maxY, 0.0f);
142 const auto rotatedOffset =
144 localTopLeft, anchor.getOrientation());
145 const auto &anchorPosition = anchor.getPosition();
146
147 const utility::graphic::PositionF origin(
148 anchorPosition[0] - rotatedOffset[0],
149 anchorPosition[1] - rotatedOffset[1],
150 anchorPosition[2] - rotatedOffset[2]);
151
152 auto baked = std::make_shared<utility::graphic::Mesh>(
153 std::vector<utility::graphic::VertexF> {},
154 std::vector<uint32_t> {});
155
156 for (const auto &vertex: vertices) {
158 vertex.getPosition(), anchor.getOrientation());
159
160 auto bakedVertex = vertex;
161 bakedVertex.setPosition(utility::graphic::PositionF(
162 origin[0] + rotated[0], origin[1] + rotated[1],
163 origin[2] + rotated[2]));
164 baked->addVertex(bakedVertex);
165 }
166
167 for (const auto index: source->getIndices()) {
168 baked->addIndex(index);
169 }
170
171 _meshes.front() = baked;
172 _pose = utility::graphic::PoseF();
173 }
174 };
175
176} // namespace guillaume
~TextRenderable(void) override=default
Default destructor.
TextRenderable(std::shared_ptr< utility::RessourceProvider > ressourceProvider, const utility::graphic::PoseF &anchor, const utility::graphic::Color32Bit &color, const std::string &content, uint32_t fontSize, const std::string &font)
Lay out a text and place its glyphs in world coordinates.
A C++20 UI framework for desktop and XR, built as an entity-component-system.
Mesh builders shared by the shape render systems.
utility::graphic::PositionF rotatePositionByQuaternion(const utility::graphic::PositionF &position, const utility::graphic::OrientationF &orientation)
Rotate a 3D point around the origin by a quaternion.