guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
measure_glyph.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 <utility/graphic/text/text.hpp>
24#include <utility/ressource_provider.hpp>
25
26#include "guillaume/systems/measure_glyph.hpp"
27
28namespace guillaume::systems
29{
30
32 std::shared_ptr<utility::RessourceProvider> ressourceProvider,
33 utility::Engine &engine)
34 : ecs::SystemFiller<components::Glyph, components::Bound,
35 components::Transform, components::Color>(
36 ecs::Phase::Measure)
37 , _ressourceProvider(ressourceProvider)
38 , _engine(engine)
39 , _defaultFontPath(
40 "fonts/Material_Symbols_Outlined/"
41 "MaterialSymbolsOutlined-VariableFont_FILL,GRAD,opsz,wght.ttf")
42 , _glyphCodePath(
43 "fonts/Material_Symbols_Outlined/"
44 "MaterialSymbolsOutlined[FILL,GRAD,opsz,wght].codepoints")
45 {
46 _codePoints = _ressourceProvider->loadCodePoints(_glyphCodePath);
47 if (_codePoints) {
48 getLogger().info() << "Loaded code points from " + _glyphCodePath;
49 } else {
50 getLogger().error()
51 << "Failed to load glyph code asset: " << _glyphCodePath;
52 }
53
54 _hasFont = _ressourceProvider->loadFont(_defaultFontPath) != nullptr;
55 if (!_hasFont) {
56 getLogger().error()
57 << "Failed to load glyph font: " << _defaultFontPath
58 << ". Icons will not be measured.";
59 }
60 }
61
65
67 {
68 }
69
71 {
72 }
73
74 void MeasureGlyph::update(const ecs::Entity::Identifier &entityIdentifier)
75 {
76 getLogger().debug()
77 << "Updating MeasureGlyph system for entity " << entityIdentifier;
78 if (!requireComponent<components::Glyph>(entityIdentifier)
79 || !requireComponent<components::Bound>(entityIdentifier)
80 || !requireComponent<components::Transform>(entityIdentifier)
81 || !requireComponent<components::Color>(entityIdentifier)) {
82 return;
83 }
84
85 // Without the icon font the glyph cannot be measured; building the
86 // text would dereference the missing font.
87 if (!_hasFont) {
88 return;
89 }
90
91 const auto &glyphComponent =
92 getComponent<components::Glyph>(entityIdentifier);
93 auto &boundComponent =
94 getComponent<components::Bound>(entityIdentifier);
95 auto &transformComponent =
97 auto &colorComponent =
98 getComponent<components::Color>(entityIdentifier);
99
100 MeasureGlyphCacheKey cacheKey { glyphComponent.getName(),
101 glyphComponent.getFontSize(),
102 glyphComponent.getStyle(),
103 colorComponent.getColor() };
104
105 getLogger().debug() << "Measuring glyph '" << cacheKey.glyphName
106 << "' for entity " << entityIdentifier;
107 getLogger().debug() << "Glyph code found for '" << cacheKey.glyphName
108 << "': " << glyphComponent.getCode();
109
110 if (contains(cacheKey)) {
111 getLogger().debug() << "Glyph '" << cacheKey.glyphName
112 << "' already measured, skipping.";
113 const auto &cacheValue = get(cacheKey);
114 if (cacheValue.has_value()) {
115 getComponent<components::Bound>(entityIdentifier)
116 .setWidth(cacheValue->getWidth())
117 .setHeight(cacheValue->getHeight());
118 }
119 return;
120 }
121
122 uint32_t glyphCode =
123 _codePoints ? _codePoints->getCode(cacheKey.glyphName) : 0;
124 if (glyphCode == 0) {
125 glyphCode = '?';
126 }
127
128 utility::graphic::Text text(
129 _ressourceProvider, transformComponent.getPose(),
130 colorComponent.getColor(),
131 utility::graphic::CodePoints::toUtf8(glyphCode), cacheKey.fontSize,
132 _defaultFontPath);
133 text.setColor(utility::graphic::Color32Bit());
134
135 auto textSize = _engine.measureText(text);
136
137 getComponent<components::Bound>(entityIdentifier)
138 .setWidth(textSize.getWidth())
139 .setHeight(textSize.getHeight());
140
141 put(cacheKey, textSize);
142 }
143
144} // 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 prepare(void) override
Does nothing: the system needs no per-frame setup.
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Write the size of one icon into its components::Bound, measuring it with the engine when it is not in...
~MeasureGlyph(void)
Destroy the system and its cache.
void cleanup(void) override
Does nothing: the cache is kept from one frame to the next.
MeasureGlyph(std::shared_ptr< utility::RessourceProvider > ressourceProvider, utility::Engine &engine)
Create the system in the Measure phase and load the icon font and its code point table.
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.
Key of the size cache of MeasureGlyph: everything a measured icon size is looked up by.
std::string glyphName
Name of the icon, such as "home".
float fontSize
Font size the icon was measured at.