guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
measure_text.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
25#include "guillaume/systems/measure_text.hpp"
26
27#include <algorithm>
28
29namespace guillaume::systems
30{
31
33 std::shared_ptr<utility::RessourceProvider> ressourceProvider,
34 utility::Engine &engine)
35 : ecs::SystemFiller<components::Text, components::Bound,
36 components::Transform, components::Color>(
37 ecs::Phase::Measure)
38 , _ressourceProvider(ressourceProvider)
39 , _engine(engine)
40 , _defaultFontPath("fonts/Roboto-Regular.ttf")
41 {
42 }
43
47
49 {
50 }
51
53 {
54 }
55
56 void MeasureText::update(const ecs::Entity::Identifier &entityIdentifier)
57 {
58 getLogger().debug()
59 << "Updating MeasureText system for entity " << entityIdentifier;
60 if (!requireComponent<components::Text>(entityIdentifier)
61 || !requireComponent<components::Bound>(entityIdentifier)
62 || !requireComponent<components::Transform>(entityIdentifier)
63 || !requireComponent<components::Color>(entityIdentifier)) {
64 return;
65 }
66
67 const auto &textComponent =
68 getComponent<components::Text>(entityIdentifier);
69 auto &boundComponent =
70 getComponent<components::Bound>(entityIdentifier);
71 auto &transformComponent =
73 auto &colorComponent =
74 getComponent<components::Color>(entityIdentifier);
75
76 MeasureTextCacheKey cacheKey { textComponent.getContent(),
77 textComponent.getFontSize(),
78 colorComponent.getColor(),
79 textComponent.getFontWeight() };
80 const float lineHeight = textComponent.getLineHeight();
81
82 getLogger().debug() << "Measuring text '" << cacheKey.content
83 << "' for entity " << entityIdentifier;
84
85 if (contains(cacheKey)) {
86 getLogger().debug() << "Text '" << cacheKey.content
87 << "' already measured, skipping.";
88 const auto &cacheValue = get(cacheKey);
89 if (cacheValue.has_value()) {
90 getComponent<components::Bound>(entityIdentifier)
91 .setWidth(cacheValue->getWidth())
92 .setHeight(std::max(cacheValue->getHeight(), lineHeight));
93 }
94 return;
95 }
96
97 utility::graphic::Text text(
98 _ressourceProvider, transformComponent.getPose(),
99 colorComponent.getColor(), cacheKey.content, cacheKey.fontSize,
100 theme::fontPath(cacheKey.fontWeight));
101 text.setColor(utility::graphic::Color32Bit());
102
103 auto textSize = _engine.measureText(text);
104
105 getComponent<components::Bound>(entityIdentifier)
106 .setWidth(textSize.getWidth())
107 .setHeight(std::max(textSize.getHeight(), lineHeight));
108
109 put(cacheKey, textSize);
110 }
111
112} // 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
Does nothing: the cache is kept from one frame to the next.
MeasureText(std::shared_ptr< utility::RessourceProvider > ressourceProvider, utility::Engine &engine)
Create the system in the Measure phase, with an empty cache.
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Write the size of one text into its components::Bound, measuring it with the engine when it is not in...
~MeasureText(void)
Destroy the system and its cache.
void prepare(void) override
Does nothing: the system needs no per-frame setup.
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
Key of the size cache of MeasureText: everything a measured size is looked up by.
float fontSize
Font size the string was measured at.
theme::FontWeight fontWeight
Font weight, which selects the font file measured with.
std::string content
The string that was measured.