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 "guillaume/components/text.hpp"
24
25#include <stdexcept>
26
28{
29 const std::string &Text::getContent(void) const
30 {
31 return _content;
32 }
33
34 Text &Text::setContent(const std::string &content)
35 {
36 if (_content == content) {
37 return *this;
38 }
39 _content = content;
40 // Clamp cursor to valid range
41 if (_cursorPosition > _content.size()) {
42 _cursorPosition = _content.size();
43 }
44 setHasChanged(true);
45 return *this;
46 }
47
48 float Text::getFontSize(void) const
49 {
50 return _fontSize;
51 }
52
53 Text &Text::setFontSize(float fontSize)
54 {
55 _fontSize = fontSize;
56 setHasChanged(true);
57 return *this;
58 }
59
61 {
62 return _fontWeight;
63 }
64
66 {
67 if (_fontWeight == fontWeight) {
68 return *this;
69 }
70 _fontWeight = fontWeight;
71 setHasChanged(true);
72 return *this;
73 }
74
75 float Text::getLineHeight(void) const
76 {
77 return _lineHeight;
78 }
79
80 Text &Text::setLineHeight(float lineHeight)
81 {
82 if (lineHeight < 0.0f) {
83 throw std::invalid_argument("Line height must be non-negative");
84 }
85 if (_lineHeight == lineHeight) {
86 return *this;
87 }
88 _lineHeight = lineHeight;
89 setHasChanged(true);
90 return *this;
91 }
92
93 float Text::getLetterSpacing(void) const
94 {
95 return _letterSpacing;
96 }
97
98 Text &Text::setLetterSpacing(float letterSpacing)
99 {
100 if (_letterSpacing == letterSpacing) {
101 return *this;
102 }
103 _letterSpacing = letterSpacing;
104 setHasChanged(true);
105 return *this;
106 }
107
109 {
110 if (_fontSize != token.fontSize) {
111 setFontSize(token.fontSize);
112 }
116 return *this;
117 }
118
119 std::size_t Text::getCursorPosition(void) const
120 {
121 return _cursorPosition;
122 }
123
124 Text &Text::setCursorPosition(std::size_t position)
125 {
126 _cursorPosition = std::min(position, _content.size());
127 setHasChanged(true);
128 return *this;
129 }
130
132 {
133 if (_cursorPosition == 0) {
134 return false;
135 }
136
137 // Move back one byte at a time until we find a UTF-8 sequence start
138 do {
139 --_cursorPosition;
140 } while (_cursorPosition > 0
141 && (_content[_cursorPosition] & 0xC0) == 0x80);
142
143 setHasChanged(true);
144 return true;
145 }
146
148 {
149 if (_cursorPosition >= _content.size()) {
150 return false;
151 }
152
153 // Move forward past the current UTF-8 character
154 ++_cursorPosition;
155 while (_cursorPosition < _content.size()
156 && (_content[_cursorPosition] & 0xC0) == 0x80) {
157 ++_cursorPosition;
158 }
159
160 setHasChanged(true);
161 return true;
162 }
163
165 {
166 _content.insert(_cursorPosition, 1, ch);
167 ++_cursorPosition;
168 setHasChanged(true);
169 return *this;
170 }
171
172 Text &Text::insertString(const std::string &str)
173 {
174 _content.insert(_cursorPosition, str);
175 _cursorPosition += str.size();
176 setHasChanged(true);
177 return *this;
178 }
179
181 {
182 if (_cursorPosition == 0 || _content.empty()) {
183 return false;
184 }
185
186 // Find the start of the previous UTF-8 character
187 std::size_t deleteStart = _cursorPosition - 1;
188 while (deleteStart > 0 && (_content[deleteStart] & 0xC0) == 0x80) {
189 --deleteStart;
190 }
191
192 const std::size_t deleteLength = _cursorPosition - deleteStart;
193 _content.erase(deleteStart, deleteLength);
194 _cursorPosition = deleteStart;
195 setHasChanged(true);
196 return true;
197 }
198
200 {
201 if (_cursorPosition >= _content.size()) {
202 return false;
203 }
204
205 // Find the end of the current UTF-8 character
206 std::size_t deleteEnd = _cursorPosition + 1;
207 while (deleteEnd < _content.size()
208 && (_content[deleteEnd] & 0xC0) == 0x80) {
209 ++deleteEnd;
210 }
211
212 const std::size_t deleteLength = deleteEnd - _cursorPosition;
213 _content.erase(_cursorPosition, deleteLength);
214 setHasChanged(true);
215 return true;
216 }
217} // namespace guillaume::components
bool deleteCharacterBefore(void)
Delete the UTF-8 character before the cursor (backspace) and move the cursor back to where it started...
Definition text.cpp:180
Text & insertString(const std::string &str)
Insert a string at the cursor and move the cursor after it. Always marks the component changed,...
Definition text.cpp:172
bool moveCursorLeft(void)
Move the cursor to the start of the previous UTF-8 character. Marks the component changed when it mov...
Definition text.cpp:131
float getLetterSpacing(void) const
Get the extra space between letters.
Definition text.cpp:93
Text & setLineHeight(float lineHeight)
Set the minimum height of the text's bound.
Definition text.cpp:80
float getLineHeight(void) const
Get the minimum height of the text's bound.
Definition text.cpp:75
Text & setFontWeight(theme::FontWeight fontWeight)
Set the font weight.
Definition text.cpp:65
Text & setLetterSpacing(float letterSpacing)
Set the extra space between letters.
Definition text.cpp:98
Text & setCursorPosition(std::size_t position)
Move the editing cursor.
Definition text.cpp:124
Text & setFontSize(float fontSize)
Set the font size.
Definition text.cpp:53
Text(void)=default
Default constructor for the Text component.
std::size_t getCursorPosition(void) const
Get the position of the editing cursor.
Definition text.cpp:119
bool moveCursorRight(void)
Move the cursor past the next UTF-8 character. Marks the component changed when it moves.
Definition text.cpp:147
const std::string & getContent(void) const
Get the text.
Definition text.cpp:29
theme::FontWeight getFontWeight(void) const
Get the font weight.
Definition text.cpp:60
float getFontSize(void) const
Get the font size.
Definition text.cpp:48
Text & insertCharacter(char ch)
Insert one byte at the cursor and move the cursor after it. Marks the component changed.
Definition text.cpp:164
Text & setContent(const std::string &content)
Replace the text.
Definition text.cpp:34
Text & setTypeToken(const theme::TypeToken &token)
Apply the four metrics of a typescale token at once: font size, weight, line height and letter spacin...
Definition text.cpp:108
bool deleteCharacterAfter(void)
Delete the UTF-8 character after the cursor (delete key); the cursor does not move....
Definition text.cpp:199
void setHasChanged(bool hasChanged)
Raise or lower the changed flag.
Definition component.cpp:32
State. Every class here is plain data attached to an entity.
FontWeight
Font weights available to the typescale. The value is the usual numeric weight (400 regular,...
Definition typescale.hpp:71
One text style: everything needed to measure and draw a piece of text.
Definition typescale.hpp:83
float letterSpacing
Extra space between characters, in dp.
Definition typescale.hpp:90
FontWeight fontWeight
Font weight.
Definition typescale.hpp:85
float fontSize
Font size, in dp.
Definition typescale.hpp:86
float lineHeight
Distance between two baselines, in dp.
Definition typescale.hpp:87