Crafter.Graphics/implementations/Crafter.Graphics-TextElement.cpp

162 lines
6.1 KiB
C++
Raw Normal View History

2025-11-25 20:40:37 +01:00
/*
Crafter®.Graphics
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
module;
#include "../lib/stb_truetype.h"
module Crafter.Graphics:TextElement_impl;
import :TextElement;
import :RenderingElement;
import :Window;
import :Types;
import :Font;
import std;
using namespace Crafter;
2025-11-26 03:35:58 +01:00
TextElement::TextElement(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : RenderingElementPreScaled(false, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
2025-11-25 20:40:37 +01:00
}
2025-11-26 03:41:00 +01:00
void TextElement::RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, Font& font, TextAlignment alignment, TextOverflowMode overflowMode) {
2025-11-26 03:35:58 +01:00
// Calculate the actual size needed for the text
2025-11-25 20:40:37 +01:00
float scale = stbtt_ScaleForPixelHeight(&font.font, size);
int baseline = (int)(font.ascent * scale);
2025-11-26 03:35:58 +01:00
// Clear the scaled buffer
for (auto& pixel : bufferScaled) {
pixel = {0, 0, 0, 0};
2025-11-25 20:40:37 +01:00
}
2025-11-26 03:41:00 +01:00
// If we have no text or no space, return early
if (text.empty() || scaled.width == 0 || scaled.height == 0) {
return;
}
// Calculate total text width
std::uint_fast32_t totalTextWidth = 0;
std::vector<int> charAdvances;
std::vector<int> charLSBs;
for (const char c : text) {
int advance, lsb;
stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb);
charAdvances.push_back(advance);
charLSBs.push_back(lsb);
totalTextWidth += (int)(advance * scale);
}
// Handle text overflow based on mode
2025-11-26 03:50:59 +01:00
if (overflowMode == TextOverflowMode::Clip) {
2025-11-26 03:41:00 +01:00
// For wrapped text, we need to handle line breaks and calculate lines
std::vector<std::string_view> lines;
std::string_view remaining = text;
2025-11-26 03:35:58 +01:00
2025-11-26 03:41:00 +01:00
// Simple word wrapping approach
while (!remaining.empty()) {
// Find next newline or end of string
auto newlinePos = remaining.find('\n');
if (newlinePos != std::string_view::npos) {
lines.emplace_back(remaining.substr(0, newlinePos));
remaining = remaining.substr(newlinePos + 1);
} else {
lines.emplace_back(remaining);
break;
}
}
// Now process each line with proper wrapping
std::uint_fast32_t lineHeight = (font.ascent - font.descent) * scale;
std::uint_fast32_t maxLineWidth = scaled.width;
std::uint_fast32_t currentLineStartY = 0;
// Process lines up to maximum allowed
2025-11-26 03:50:59 +01:00
for (std::size_t lineIndex = 0; lineIndex < lines.size(); ++lineIndex) {
2025-11-26 03:41:00 +01:00
std::string_view line = lines[lineIndex];
// Calculate line width
std::uint_fast32_t lineWidth = 0;
std::vector<int> lineAdvances;
for (const char c : line) {
int advance, lsb;
stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb);
lineAdvances.push_back(advance);
lineWidth += (int)(advance * scale);
}
// Determine horizontal position based on alignment
int startX = 0;
switch (alignment) {
case TextAlignment::Left:
startX = 0;
break;
case TextAlignment::Center:
startX = (scaled.width - lineWidth) / 2;
break;
case TextAlignment::Right:
startX = scaled.width - lineWidth;
break;
}
// Render the line
int x = startX;
int startY = currentLineStartY + baseline + (lineIndex * lineHeight);
for (std::size_t i = 0; i < line.size(); ++i) {
int codepoint = line[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 < line.size()) {
x += (int)stbtt_GetCodepointKernAdvance(&font.font, codepoint, line[i+1]);
}
}
currentLineStartY += lineHeight;
}
} else {
2025-11-26 03:50:59 +01:00
2025-11-25 20:40:37 +01:00
}
}