guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
glyph_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/glyph_render.hpp"
26
28#include "guillaume/text_renderable.hpp"
29
30namespace guillaume::systems
31{
33 std::shared_ptr<utility::RessourceProvider> ressourceProvider,
34 utility::Engine &engine)
35 : ecs::SystemFiller<components::Transform, components::Bound,
36 components::Glyph, components::Color>(
37 ecs::Phase::Render)
38 , _ressourceProvider(ressourceProvider)
39 , _engine(engine)
40 , _defaultFontPath(
41 "fonts/Material_Symbols_Outlined/"
42 "MaterialSymbolsOutlined-VariableFont_FILL,GRAD,opsz,wght.ttf")
43 , _glyphCodePath(
44 "fonts/Material_Symbols_Outlined/"
45 "MaterialSymbolsOutlined[FILL,GRAD,opsz,wght].codepoints")
46 {
47 _codePoints = _ressourceProvider->loadCodePoints(_glyphCodePath);
48 if (_codePoints) {
49 getLogger().info() << "Loaded code points from " + _glyphCodePath;
50 } else {
51 getLogger().error()
52 << "Failed to load glyph code asset: " << _glyphCodePath;
53 }
54
55 _hasFont = _ressourceProvider->loadFont(_defaultFontPath) != nullptr;
56 if (!_hasFont) {
57 getLogger().error()
58 << "Failed to load glyph font: " << _defaultFontPath
59 << ". Icons will not be rendered.";
60 }
61 }
62
64 {
65 clear();
66 }
67
69 {
71 }
72
74 {
75 removeUnused(_engine);
76 }
77
78 void GlyphRender::update(const ecs::Entity::Identifier &entityIdentifier)
79 {
80 getLogger().debug()
81 << "Updating GlyphRender system for entity " << entityIdentifier;
82 if (!requireComponent<components::Transform>(entityIdentifier)
83 || !requireComponent<components::Bound>(entityIdentifier)
84 || !requireComponent<components::Glyph>(entityIdentifier)
85 || !requireComponent<components::Color>(entityIdentifier)) {
86 return;
87 }
88
89 // Without the icon font there is nothing to draw; building the text
90 // renderable would dereference the missing font.
91 if (!_hasFont) {
92 return;
93 }
94
95 const auto &transformComponent =
97 const auto &glyphComponent =
98 getComponent<components::Glyph>(entityIdentifier);
99 const auto &colorComponent =
100 getComponent<components::Color>(entityIdentifier);
101
102 // A fully transparent color is how widgets hide a child; the text
103 // shader ignores vertex alpha, so submit nothing instead.
104 if (shape::isInvisible(colorComponent.getColor())) {
105 return;
106 }
107
108 getLogger().debug() << "Rendering glyph '" << glyphComponent.getName()
109 << "' for entity " << entityIdentifier;
110
111 // The code point table can be missing (asset not found); fall back
112 // to the placeholder glyph instead of dereferencing a null table.
113 uint32_t glyphCode =
114 _codePoints ? _codePoints->getCode(glyphComponent.getName()) : 0;
115
116 if (glyphCode == 0) {
117 glyphCode = '?';
118 }
119
120 // Build a world-space text renderable so the glyph lands on the
121 // entity position regardless of the renderer's model-matrix handling.
122 auto renderable = std::make_shared<TextRenderable>(
123 _ressourceProvider, transformComponent.getPose(),
124 colorComponent.getColor(),
125 utility::graphic::CodePoints::toUtf8(glyphCode),
126 glyphComponent.getFontSize(), _defaultFontPath);
127
128 RenderHandle *handle = find(entityIdentifier);
129 if (handle == nullptr) {
130 size_t objectId = _engine.createObject(renderable);
131 insert(entityIdentifier,
132 RenderHandle { objectId, renderable, true });
133 } else {
134 if (!_engine.updateObject(renderable, handle->objectId)) {
135 handle->objectId = _engine.createObject(renderable);
136 }
137 handle->renderable = renderable;
138 handle->used = true;
139 }
140 }
141
142} // 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 cleanup(void) override
Remove the engine objects of the entities that were not drawn this frame. Called once per frame after...
~GlyphRender(void)
Forget the engine objects without removing them from the engine.
GlyphRender(std::shared_ptr< utility::RessourceProvider > ressourceProvider, utility::Engine &engine)
Create the system in the Render phase and load the icon font and its code point table.
void prepare(void) override
Mark every icon as not drawn yet. Called once per frame before the entities are visited.
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Build the renderable of one icon from its current components and upload it, creating the engine objec...
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.
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.
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.