This commit is contained in:
Jorijn van der Graaf 2025-11-26 03:50:59 +01:00
commit be3be17c81

View file

@ -63,7 +63,7 @@ void TextElement::RenderText(const std::string_view text, float size, Pixel_BU8_
}
// Handle text overflow based on mode
if (overflowMode == TextOverflowMode::Wrap) {
if (overflowMode == TextOverflowMode::Clip) {
// For wrapped text, we need to handle line breaks and calculate lines
std::vector<std::string_view> lines;
std::string_view remaining = text;
@ -85,10 +85,9 @@ void TextElement::RenderText(const std::string_view text, float size, Pixel_BU8_
std::uint_fast32_t lineHeight = (font.ascent - font.descent) * scale;
std::uint_fast32_t maxLineWidth = scaled.width;
std::uint_fast32_t currentLineStartY = 0;
std::uint_fast32_t maxLines = scaled.height / lineHeight;
// Process lines up to maximum allowed
for (std::size_t lineIndex = 0; lineIndex < std::min(lines.size(), maxLines); ++lineIndex) {
for (std::size_t lineIndex = 0; lineIndex < lines.size(); ++lineIndex) {
std::string_view line = lines[lineIndex];
// Calculate line width
@ -158,126 +157,6 @@ void TextElement::RenderText(const std::string_view text, float size, Pixel_BU8_
currentLineStartY += lineHeight;
}
} else {
// Clip mode - render text that fits within bounds
// If the text fits entirely, render it normally
if (totalTextWidth <= scaled.width && (font.ascent - font.descent) * scale <= scaled.height) {
// Calculate starting position based on alignment
int startX = 0;
switch (alignment) {
case TextAlignment::Left:
startX = 0;
break;
case TextAlignment::Center:
startX = (scaled.width - totalTextWidth) / 2;
break;
case TextAlignment::Right:
startX = scaled.width - totalTextWidth;
break;
}
int startY = (scaled.height - (font.ascent - font.descent) * scale) / 2;
startY += baseline; // Adjust for baseline
int x = startX;
for (std::uint_fast32_t i = 0; i < text.size(); i++) {
int codepoint = text[i];
int ax;
int lsb;
stbtt_GetCodepointHMetrics(&font.font, codepoint, &ax, &lsb);
int c_x1, c_y1, c_x2, c_y2;
stbtt_GetCodepointBitmapBox(&font.font, codepoint, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
int w = c_x2 - c_x1;
int h = c_y2 - c_y1;
std::vector<unsigned char> bitmap(w * h);
stbtt_MakeCodepointBitmap(&font.font, bitmap.data(), w, h, w, scale, scale, codepoint);
// Only render characters that fit within the scaled bounds
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int bufferX = x + i + c_x1;
int bufferY = startY + j + c_y1;
// Only draw pixels that are within our scaled buffer bounds
if (bufferX >= 0 && bufferX < (int)scaled.width && bufferY >= 0 && bufferY < (int)scaled.height) {
bufferScaled[bufferY * scaled.width + bufferX] = {color.r, color.g, color.b, bitmap[j * w + i]};
}
}
}
x += (int)(ax * scale);
if (i + 1 < text.size()) {
x += (int)stbtt_GetCodepointKernAdvance(&font.font, codepoint, text[i+1] * scale);
}
}
} else {
// Text doesn't fit, but we still render what we can
// For now, we'll just render the first part that fits
int startX = 0;
switch (alignment) {
case TextAlignment::Left:
startX = 0;
break;
case TextAlignment::Center:
startX = (scaled.width - totalTextWidth) / 2;
break;
case TextAlignment::Right:
startX = scaled.width - totalTextWidth;
break;
}
// Clamp to visible area
int startY = (scaled.height - (font.ascent - font.descent) * scale) / 2;
startY += baseline; // Adjust for baseline
int x = startX;
int renderedChars = 0;
for (std::uint_fast32_t i = 0; i < text.size(); i++) {
int codepoint = text[i];
int ax;
int lsb;
stbtt_GetCodepointHMetrics(&font.font, codepoint, &ax, &lsb);
int c_x1, c_y1, c_x2, c_y2;
stbtt_GetCodepointBitmapBox(&font.font, codepoint, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
int w = c_x2 - c_x1;
int h = c_y2 - c_y1;
std::vector<unsigned char> bitmap(w * h);
stbtt_MakeCodepointBitmap(&font.font, bitmap.data(), w, h, w, scale, scale, codepoint);
// Only render characters that fit within the scaled bounds
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int bufferX = x + i + c_x1;
int bufferY = startY + j + c_y1;
// Only draw pixels that are within our scaled buffer bounds
if (bufferX >= 0 && bufferX < (int)scaled.width && bufferY >= 0 && bufferY < (int)scaled.height) {
bufferScaled[bufferY * scaled.width + bufferX] = {color.r, color.g, color.b, bitmap[j * w + i]};
}
}
}
x += (int)(ax * scale);
if (x > (int)scaled.width) {
// Text would overflow, stop rendering
break;
}
if (i + 1 < text.size()) {
x += (int)stbtt_GetCodepointKernAdvance(&font.font, codepoint, text[i+1] * scale);
}
renderedChars++;
}
}
}
}