fix label font cache

This commit is contained in:
MihailRis
2025-09-30 18:22:39 +03:00
parent c8ba5b5dbb
commit edc6810283
7 changed files with 75 additions and 36 deletions
+28 -18
View File
@@ -231,8 +231,6 @@ TextBox::~TextBox() = default;
void TextBox::draw(const DrawContext& pctx, const Assets& assets) {
Container::draw(pctx, assets);
font = assets.get<Font>(label->getFontName());
if (!isFocused()) {
return;
}
@@ -244,7 +242,8 @@ void TextBox::draw(const DrawContext& pctx, const Assets& assets) {
auto subctx = pctx.sub();
subctx.setScissors(glm::vec4(pos.x, pos.y, size.x, size.y));
const int lineHeight = font->getLineHeight() * label->getLineInterval();
const int lineHeight =
rawTextCache.metrics.lineHeight * label->getLineInterval();
glm::vec2 lcoord = label->calcPos();
lcoord.y -= 2;
auto batch = pctx.getBatch2D();
@@ -256,7 +255,7 @@ void TextBox::draw(const DrawContext& pctx, const Assets& assets) {
if (editable && static_cast<int>((time - caretLastMove) * 2) % 2 == 0) {
uint line = rawTextCache.getLineByTextIndex(caret);
uint lcaret = caret - rawTextCache.getTextLineOffset(line);
int width = font->calcWidth(input, lcaret);
int width = rawTextCache.metrics.calcWidth(input, 0, lcaret);
batch->rect(
lcoord.x + width,
lcoord.y + label->getLineYOffset(line),
@@ -272,10 +271,10 @@ void TextBox::draw(const DrawContext& pctx, const Assets& assets) {
uint endLine = label->getLineByTextIndex(selectionEnd);
batch->setColor(glm::vec4(0.8f, 0.9f, 1.0f, 0.25f));
int start = font->calcWidth(
int start = rawTextCache.metrics.calcWidth(
labelText, selectionStart - label->getTextLineOffset(startLine)
);
int end = font->calcWidth(
int end = rawTextCache.metrics.calcWidth(
labelText, selectionEnd - label->getTextLineOffset(endLine)
);
int lineY = label->getLineYOffset(startLine);
@@ -346,7 +345,14 @@ void TextBox::draw(const DrawContext& pctx, const Assets& assets) {
}
}
void TextBox::drawBackground(const DrawContext& pctx, const Assets&) {
void TextBox::drawBackground(const DrawContext& pctx, const Assets& assets) {
auto font = assets.get<Font>(label->getFontName());
rawTextCache.prepare(
reinterpret_cast<ptrdiff_t>(font),
font->getMetrics(),
label->getSize().x
);
glm::vec2 pos = calcPos();
auto batch = pctx.getBatch2D();
@@ -375,7 +381,11 @@ void TextBox::drawBackground(const DrawContext& pctx, const Assets&) {
}
void TextBox::refreshLabel() {
rawTextCache.prepare(font, static_cast<size_t>(getSize().x));
rawTextCache.prepare(
rawTextCache.fontId,
rawTextCache.metrics,
static_cast<size_t>(getSize().x)
);
rawTextCache.update(input, multiline, false);
label->setColor(textColor * glm::vec4(input.empty() ? 0.5f : 1.0f));
@@ -412,13 +422,13 @@ void TextBox::refreshLabel() {
lineNumbersLabel->setColor(glm::vec4(1, 1, 1, 0.25f));
}
if (autoresize && font) {
if (autoresize && rawTextCache.fontId) {
auto size = getSize();
int newy = glm::min(
static_cast<int>(parent->getSize().y),
static_cast<int>(
label->getLinesNumber() * label->getLineInterval() *
font->getLineHeight()
rawTextCache.metrics.lineHeight
) + 1
);
if (newy != static_cast<int>(size.y)) {
@@ -430,9 +440,9 @@ void TextBox::refreshLabel() {
}
}
if (multiline && font) {
if (multiline && rawTextCache.fontId) {
setScrollable(true);
uint height = label->getLinesNumber() * font->getLineHeight() *
uint height = label->getLinesNumber() * rawTextCache.metrics.lineHeight *
label->getLineInterval();
label->setSize(glm::vec2(label->getSize().x, height));
actualLength = height;
@@ -644,14 +654,14 @@ size_t TextBox::normalizeIndex(int index) {
/// @param y screen Y position
/// @return non-normalized character index
int TextBox::calcIndexAt(int x, int y) const {
if (font == nullptr) return 0;
if (rawTextCache.fontId == 0) return 0;
const auto& labelText = label->getText();
glm::vec2 lcoord = label->calcPos();
uint line = label->getLineByYOffset(y - lcoord.y);
line = std::min(line, label->getLinesNumber() - 1);
size_t lineLength = getLineLength(line);
uint offset = 0;
while (lcoord.x + font->calcWidth(labelText, offset) < x &&
while (lcoord.x + rawTextCache.metrics.calcWidth(labelText, offset) < x &&
offset < lineLength - 1) {
offset++;
}
@@ -1177,19 +1187,19 @@ size_t TextBox::getCaret() const {
void TextBox::setCaret(size_t position) {
const auto& labelText = label->getText();
caret = std::min(static_cast<size_t>(position), input.length());
if (font == nullptr) {
if (rawTextCache.fontId == 0) {
return;
}
int width = label->getSize().x;
rawTextCache.prepare(font, width);
rawTextCache.prepare(rawTextCache.fontId, rawTextCache.metrics, width);
rawTextCache.update(input, multiline, label->isTextWrapping());
caretLastMove = gui.getWindow().time();
uint line = rawTextCache.getLineByTextIndex(caret);
int offset = label->getLineYOffset(line) + getContentOffset().y;
uint lineHeight = font->getLineHeight() * label->getLineInterval();
uint lineHeight = rawTextCache.metrics.lineHeight * label->getLineInterval();
if (scrollStep == 0) {
scrollStep = lineHeight;
}
@@ -1201,7 +1211,7 @@ void TextBox::setCaret(size_t position) {
}
int lcaret = caret - rawTextCache.getTextLineOffset(line);
int realoffset =
font->calcWidth(labelText, lcaret) - static_cast<int>(textOffset) + 2;
rawTextCache.metrics.calcWidth(labelText, lcaret) - static_cast<int>(textOffset) + 2;
if (realoffset - width > 0) {
setTextOffset(textOffset + realoffset - width);