32 Text::Text(std::shared_ptr<RessourceProvider> ressourceProvider,
34 const std::string &content, uint32_t fontSize,
35 const std::string &font)
37 , logging::Loggable<
Text, logging::DefaultLogger>()
42 _font = ressourceProvider->loadFont(_fontPath);
43 _meshes = { std::make_shared<Mesh>(std::vector<VertexF> {},
44 std::vector<uint32_t> {}) };
90 _font->measureText(_fontSize, codepoints);
92 dimensions.setWidth(measured[0]);
93 dimensions.setHeight(measured[1]);
95 getLogger().debug() <<
"Calculated text dimensions for content '"
96 << _content <<
"' with font size " << _fontSize
97 <<
": width = " << dimensions.getWidth()
98 <<
", height = " << dimensions.getHeight();
109 _meshes.front() = std::make_shared<Mesh>(std::vector<VertexF> {},
110 std::vector<uint32_t> {});
117 uint32_t indexOffset = 0;
119 std::vector<Glyph> glyphs =
120 _font->processCodePoints(_fontSize, codepoints);
123 double baseline = std::round(
_font->getAscender(_fontSize));
125 for (
const auto &g: glyphs) {
127 static_cast<float>(std::round(penX + g.bearing[VEC_X]));
129 static_cast<float>(std::round(-baseline + g.bearing[VEC_Y]));
131 const float w = g.size[VEC_X];
132 const float h = g.size[VEC_Y];
150 _meshes.front()->addIndex(indexOffset + 0);
151 _meshes.front()->addIndex(indexOffset + 1);
152 _meshes.front()->addIndex(indexOffset + 2);
154 _meshes.front()->addIndex(indexOffset + 0);
155 _meshes.front()->addIndex(indexOffset + 2);
156 _meshes.front()->addIndex(indexOffset + 3);
158 _meshes.front()->addVertex(v0);
159 _meshes.front()->addVertex(v1);
160 _meshes.front()->addVertex(v2);
161 _meshes.front()->addVertex(v3);
169 std::vector<uint32_t> codepoints;
172 while (i < str.size()) {
173 uint8_t c0 =
static_cast<uint8_t
>(str[i]);
174 uint32_t codepoint = 0;
180 }
else if (c0 >= 0xC2 && c0 <= 0xDF) {
181 codepoint = c0 & 0x1F;
183 }
else if (c0 >= 0xE0 && c0 <= 0xEF) {
184 codepoint = c0 & 0x0F;
186 }
else if (c0 >= 0xF0 && c0 <= 0xF4) {
187 codepoint = c0 & 0x07;
194 if (i + bytes > str.size()) {
199 for (
size_t j = 1; j < bytes; ++j) {
200 uint8_t cc =
static_cast<uint8_t
>(str[i + j]);
201 if ((cc & 0xC0) != 0x80) {
205 codepoint = (codepoint << 6) | (cc & 0x3F);
209 if ((bytes == 2 && codepoint < 0x80)
211 && (codepoint < 0x800
212 || (codepoint >= 0xD800 && codepoint <= 0xDFFF)))
214 && (codepoint < 0x10000 || codepoint > 0x10FFFF))) {
220 codepoints.push_back(codepoint);
Storage for text content and rendering properties.
std::shared_ptr< Font > _font
Shared pointer to the font used for rendering.
codePointString utf8ToCodepoints(const std::string &str) const
Converts a UTF-8 encoded string to a string of Unicode code points.
Text(std::shared_ptr< RessourceProvider > ressourceProvider, const PoseF &pose, const Color32Bit &color, const std::string &content, uint32_t fontSize, const std::string &font)
Constructor.
const std::string & getFontFamily(void) const
Get the font family (path) used for rendering the text.
void updateMesh(void)
Updates the mesh for rendering the text based on the current content, font, and font size.
const std::string & getContent(void) const
Get the text content.
std::shared_ptr< Mesh > getMesh() const
Get the mesh for rendering the text.
Text & setFontSize(uint32_t fontSize)
Set the font size.
uint32_t getFontSize(void) const
Get the font size.
Text & setContent(const std::string &content)
Set the text content.
graphic::SizeF getTextDimensions(void) const
Get the dimensions of the rendered text.
Text rendering properties and storage.