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> {}) };
100 _font->measureText(_fontSize, codepoints);
102 dimensions.setWidth(measured[0]);
103 dimensions.setHeight(measured[1]);
105 getLogger().debug() <<
"Calculated text dimensions for content '"
106 << _content <<
"' with font size " << _fontSize
107 <<
": width = " << dimensions.getWidth()
108 <<
", height = " << dimensions.getHeight();
119 _meshes.front() = std::make_shared<Mesh>(std::vector<VertexF> {},
120 std::vector<uint32_t> {});
127 uint32_t indexOffset = 0;
129 std::vector<Glyph> glyphs =
130 _font->processCodePoints(_fontSize, codepoints);
133 double baseline = std::round(
_font->getAscender(_fontSize));
135 for (
const auto &g: glyphs) {
137 static_cast<float>(std::round(penX + g.bearing[VEC_X]));
139 static_cast<float>(std::round(-baseline + g.bearing[VEC_Y]));
141 const float w = g.size[VEC_X];
142 const float h = g.size[VEC_Y];
160 _meshes.front()->addIndex(indexOffset + 0);
161 _meshes.front()->addIndex(indexOffset + 1);
162 _meshes.front()->addIndex(indexOffset + 2);
164 _meshes.front()->addIndex(indexOffset + 0);
165 _meshes.front()->addIndex(indexOffset + 2);
166 _meshes.front()->addIndex(indexOffset + 3);
168 _meshes.front()->addVertex(v0);
169 _meshes.front()->addVertex(v1);
170 _meshes.front()->addVertex(v2);
171 _meshes.front()->addVertex(v3);
179 std::vector<uint32_t> codepoints;
182 while (i < str.size()) {
183 uint8_t c0 =
static_cast<uint8_t
>(str[i]);
184 uint32_t codepoint = 0;
190 }
else if (c0 >= 0xC2 && c0 <= 0xDF) {
191 codepoint = c0 & 0x1F;
193 }
else if (c0 >= 0xE0 && c0 <= 0xEF) {
194 codepoint = c0 & 0x0F;
196 }
else if (c0 >= 0xF0 && c0 <= 0xF4) {
197 codepoint = c0 & 0x07;
204 if (i + bytes > str.size()) {
209 for (
size_t j = 1; j < bytes; ++j) {
210 uint8_t cc =
static_cast<uint8_t
>(str[i + j]);
211 if ((cc & 0xC0) != 0x80) {
215 codepoint = (codepoint << 6) | (cc & 0x3F);
219 if ((bytes == 2 && codepoint < 0x80)
221 && (codepoint < 0x800
222 || (codepoint >= 0xD800 && codepoint <= 0xDFFF)))
224 && (codepoint < 0x10000 || codepoint > 0x10FFFF))) {
230 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.
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.
std::string getMaterialName() const override
Get the material name.
const std::string & getContent(void) const
Get the text content.
std::shared_ptr< Mesh > getMesh() const
Get the mesh for rendering the text.
RenderableType getRenderableType(void) const override
Get the RenderableType of this object.
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.