guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
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 <memory>
24
25#include "guillaume/entities/text.hpp"
26#include "guillaume/systems/typography.hpp"
27#include "guillaume/entities/builder_base.hpp"
28#include "guillaume/theme.hpp"
29
30namespace guillaume::entities
31{
32
33 Text::Text::Builder::Builder(ecs::ComponentRegistry &componentRegistry,
34 ecs::EntityRegistry &entityRegistry)
35 : EntityBuilderBase<Text>(componentRegistry, entityRegistry)
36 {
37 reset();
38 }
39
40 std::shared_ptr<Text> Text::Builder::buildEntity(void)
41 {
42 auto entity = Text::create(this->getComponentRegistry(), _content,
43 _typeRole, _color, _fontSize);
44 return entity;
45 }
46
48 {
49 _content.clear();
51 _fontSize.reset();
52 _color = getActiveColor(SchemeColorRole::OnSurface);
53 }
54
55 Text::Builder &Text::Builder::withContent(const std::string &content)
56 {
57 _content = content;
58 return *this;
59 }
60
62 {
63 _fontSize = fontSize;
64 return *this;
65 }
66
68 {
69 _typeRole = role;
70 _fontSize.reset();
71 return *this;
72 }
73
75 Text::Builder::withColor(const utility::graphic::Color32Bit &color)
76 {
77 _color = color;
78 return *this;
79 }
80
81 std::shared_ptr<Text> Text::Builder::makeText(
82 std::shared_ptr<Entity> parent, const std::string &content,
83 const float &fontSize, const utility::graphic::Color32Bit &color)
84 {
85 return withContent(content)
86 .withFontSize(fontSize)
87 .withColor(color)
88 .registerEntity(parent);
89 }
90
91 std::shared_ptr<Text> Text::Builder::makeText(
92 std::shared_ptr<Entity> parent, const std::string &content,
93 theme::TypeRole role, const utility::graphic::Color32Bit &color)
94 {
95 return withContent(content)
96 .withTypeRole(role)
97 .withColor(color)
98 .registerEntity(parent);
99 }
100
101 Text::Text(ecs::ComponentRegistry &registry, const std::string &content,
102 const float &fontSize, const utility::graphic::Color32Bit &color)
103 : Text(registry, content, theme::TypeRole::Default, color, fontSize)
104 {
105 }
106
107 Text::Text(ecs::ComponentRegistry &registry, const std::string &content,
108 theme::TypeRole role, const utility::graphic::Color32Bit &color,
109 std::optional<float> fontSize)
110 : ecs::EntityFiller<components::Transform, components::Bound,
111 components::Text, components::Color,
112 components::Typography>(registry)
113 {
114 typography().setRole(role).setFontSize(fontSize);
115 setContent(content);
116 setColor(color);
117 applyTypography();
118 }
119
121 {
122 }
123
124 components::Typography &Text::typography(void)
125 {
127 getIdentifier());
128 }
129
130 const components::Typography &Text::typography(void) const
131 {
133 getIdentifier());
134 }
135
136 void Text::applyTypography(void)
137 {
139 }
140
141 Text &Text::setContent(const std::string &content)
142 {
145 .setContent(content);
146 return *this;
147 }
148
149 Text &Text::setFontSize(const float &fontSize)
150 {
151 typography().setFontSize(fontSize);
152 applyTypography();
153 return *this;
154 }
155
157 {
158 typography().setRole(role).setFontSize(std::nullopt);
159 applyTypography();
160 return *this;
161 }
162
164 {
165 return typography().getRole();
166 }
167
168 Text &Text::setColor(const utility::graphic::Color32Bit &color)
169 {
172 .setColor(color);
173 return *this;
174 }
175
176} // namespace guillaume::entities
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
A run of text: its content, its font metrics and the position of the editing cursor.
Definition text.hpp:66
Which typescale role a text uses (BodyMedium, TitleLarge, ...) and an optional explicit size.
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
ComponentType & getComponent(const Entity::Identifier &entityIdentifier)
Get a component of an entity, to read or modify it.
ComponentRegistry & getComponentRegistry(void)
Get the registry in which the builder creates components.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
Base of every widget Builder: turns a configuration into a widget that is attached to a parent and ow...
Configures Text widgets and registers them in the scene's entity registry.
Definition text.hpp:98
Builder & withFontSize(const float &fontSize)
Override the role font size with an explicit size.
Definition text.cpp:61
std::shared_ptr< Text > buildEntity(void) override
Build a text from the current configuration, without registering it or giving it a parent.
Definition text.cpp:40
void reset(void) override
Restore the defaults: empty content, role BodyMedium, no explicit font size, and the OnSurface color ...
Definition text.cpp:47
Builder & withTypeRole(theme::TypeRole role)
Set the typescale role and drop any explicit font size.
Definition text.cpp:67
std::shared_ptr< Text > makeText(std::shared_ptr< Entity > parent, const std::string &content, const float &fontSize, const utility::graphic::Color32Bit &color)
Build a text with an explicit font size, register it under parent and reset the builder.
Definition text.cpp:81
Builder & withContent(const std::string &content)
Set the characters to draw.
Definition text.cpp:55
Builder & withColor(const utility::graphic::Color32Bit &color)
Set the text color.
Definition text.cpp:75
A single run of text drawn with a typescale role, a color and an optional explicit font size.
Definition text.hpp:85
Text & setContent(const std::string &content)
Replace the characters to draw.
Definition text.cpp:141
~Text(void)
Destructor.
Definition text.cpp:120
theme::TypeRole getTypeRole(void) const
Get the typescale role the text is styled with.
Definition text.cpp:163
Text & setFontSize(const float &fontSize)
Override the role font size with an explicit size.
Definition text.cpp:149
Text & setTypeRole(theme::TypeRole role)
Set the typescale role and drop any explicit font size.
Definition text.cpp:156
static std::shared_ptr< Text > create(Arguments &&...arguments)
Build a text without registering it in an entity registry.
Definition text.hpp:218
Text & setColor(const utility::graphic::Color32Bit &color)
Set the text color.
Definition text.cpp:168
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Resolve the metrics of one text and write them into its components::Text.
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46
Design tokens: colors, type scale, shapes, elevation, motion and spacing. Read them through guillaume...
TypeRole
The named text styles of Material Design 3: what a piece of text is, not how big it is.
Definition typescale.hpp:47
@ BodyMedium
14 / 20, regular.
Definition typescale.hpp:61
utility::graphic::Color32Bit getActiveColor(SchemeColorRole role)
Get the color the active scheme gives to a role.