guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
text_render.cpp
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#include <memory>
24
25#include "guillaume/systems/text_render.hpp"
26
28#include "guillaume/text_renderable.hpp"
29#include "guillaume/theme/typescale.hpp"
30
31namespace guillaume::systems
32{
34 std::shared_ptr<utility::RessourceProvider> ressourceProvider,
35 utility::Engine &engine)
36 : ecs::SystemFiller<components::Transform, components::Text,
37 components::Color>(ecs::Phase::Render)
38 , _ressourceProvider(ressourceProvider)
39 , _engine(engine)
40 , _defaultFontPath("fonts/Roboto-Regular.ttf")
41 {
42 }
43
45 {
46 clear();
47 }
48
50 {
52 }
53
55 {
56 removeUnused(_engine);
57 }
58
59 void TextRender::update(const ecs::Entity::Identifier &entityIdentifier)
60 {
61 getLogger().debug()
62 << "Updating TextRender system for entity " << entityIdentifier;
63 if (!requireComponent<components::Transform>(entityIdentifier)
64 || !requireComponent<components::Text>(entityIdentifier)
65 || !requireComponent<components::Color>(entityIdentifier)) {
66 return;
67 }
68
69 const auto &transformComponent =
71 const auto &textComponent =
72 getComponent<components::Text>(entityIdentifier);
73 const auto &colorComponent =
74 getComponent<components::Color>(entityIdentifier);
75
76 // A fully transparent color is how widgets hide a child; the text
77 // shader ignores vertex alpha, so submit nothing instead.
78 if (shape::isInvisible(colorComponent.getColor())) {
79 return;
80 }
81
82 getLogger().debug()
83 << "Rendering text for entity " << entityIdentifier
84 << " (content: '" << textComponent.getContent() << "')";
85
86 // Build a world-space text renderable so the glyphs land on the
87 // entity position regardless of the renderer's model-matrix handling.
88 auto renderable = std::make_shared<TextRenderable>(
89 _ressourceProvider, transformComponent.getPose(),
90 colorComponent.getColor(), textComponent.getContent(),
91 textComponent.getFontSize(),
92 theme::fontPath(textComponent.getFontWeight()));
93
94 RenderHandle *handle = find(entityIdentifier);
95 if (handle == nullptr) {
96 size_t objectId = _engine.createObject(renderable);
97 insert(entityIdentifier,
98 RenderHandle { objectId, renderable, true });
99 } else {
100 if (!_engine.updateObject(renderable, handle->objectId)) {
101 handle->objectId = _engine.createObject(renderable);
102 }
103 handle->renderable = renderable;
104 handle->used = true;
105 }
106 }
107
108} // namespace guillaume::systems
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
Definition entity.hpp:86
ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier)
Get a component of an entity from the bound registry, to read or modify it.
bool requireComponent(const ecs::Entity::Identifier &entityIdentifier)
Tell whether an entity has a component, and log a warning naming the entity and the component when it...
void insert(const ecs::Entity::Identifier &identifier, RenderHandle handle)
Store the handle of an entity, replacing the one it had.
void markAllUnused(void)
Clear the used flag of every handle. Call it from the system's prepare(), before any entity is drawn.
void clear(void)
Forget every handle without calling utility::Engine::removeObject. Use it only when the engine object...
void removeUnused(utility::Engine &engine)
Remove from the engine, then from the map, every handle whose used flag is still false....
RenderHandle * find(const ecs::Entity::Identifier &identifier)
Look up the handle of an entity.
void prepare(void) override
Mark every text as not drawn yet. Called once per frame before the entities are visited.
void cleanup(void) override
Remove the engine objects of the entities that were not drawn this frame. Called once per frame after...
TextRender(std::shared_ptr< utility::RessourceProvider > ressourceProvider, utility::Engine &engine)
Create the system in the Render phase.
~TextRender(void)
Forget the engine objects without removing them from the engine.
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Build the renderable of one text from its current components and upload it, creating the engine objec...
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Rules. Every class here runs each frame on the entities that have the components it declares.
const std::string & fontPath(FontWeight weight)
Get the font file shipped with the library for a weight.
Definition typescale.cpp:52
Mesh builders shared by the shape render systems.
bool isInvisible(const utility::graphic::Color32Bit &color)
Whether a color draws nothing.
What a render system remembers about the engine object it created for one entity.
bool used
Whether the entity was drawn this frame.
std::shared_ptr< utility::graphic::Renderable > renderable
Renderable uploaded last for this object.
size_t objectId
Identifier given by the engine.