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

View file

@ -11,10 +11,11 @@
using namespace gui;
void LabelCache::prepare(Font* font, size_t wrapWidth) {
if (font != this->font) {
void LabelCache::prepare(std::ptrdiff_t fontId, FontMetrics metrics, size_t wrapWidth) {
if (fontId != this->fontId) {
resetFlag = true;
this->font = font;
this->fontId = fontId;
this->metrics = metrics;
}
if (wrapWidth != this->wrapWidth) {
resetFlag = true;
@ -41,7 +42,7 @@ void LabelCache::update(std::wstring_view text, bool multiline, bool wrap) {
lines.clear();
lines.push_back(LineScheme {0, false});
if (font == nullptr) {
if (fontId == 0) {
wrap = false;
}
@ -52,7 +53,7 @@ void LabelCache::update(std::wstring_view text, bool multiline, bool wrap) {
lines.push_back(LineScheme {i+1, false});
len = 0;
} else if (i > 0 && i+1 < text.length() && wrap && text[i+1] != L'\n') {
size_t width = font->calcWidth(text, i-len-1, i-(i-len)+2);
size_t width = metrics.calcWidth(text, i-len-1, i-(i-len)+2);
if (width >= wrapWidth) {
// starting a fake line
lines.push_back(LineScheme {i+1, true});
@ -60,20 +61,20 @@ void LabelCache::update(std::wstring_view text, bool multiline, bool wrap) {
}
}
}
if (font != nullptr) {
if (fontId != 0) {
int maxWidth = 0;
for (int i = 0; i < lines.size() - 1; i++) {
const auto& next = lines[i + 1];
const auto& cur = lines[i];
maxWidth = std::max(
font->calcWidth(
metrics.calcWidth(
text.substr(cur.offset, next.offset - cur.offset)
),
maxWidth
);
}
maxWidth = std::max(
font->calcWidth(
metrics.calcWidth(
text.substr(lines[lines.size() - 1].offset)
),
maxWidth
@ -105,8 +106,8 @@ Label::Label(GUI& gui, const std::wstring& text, std::string fontName)
Label::~Label() = default;
glm::vec2 Label::calcSize() {
auto font = cache.font;
uint lineHeight = font->getLineHeight();
auto& metrics = cache.metrics;
uint lineHeight = metrics.lineHeight;
if (cache.lines.size() > 1) {
lineHeight *= lineInterval;
}
@ -114,12 +115,12 @@ glm::vec2 Label::calcSize() {
if (multiline) {
return glm::vec2(
cache.multilineWidth,
lineHeight * cache.lines.size() + font->getYOffset()
lineHeight * cache.lines.size() + metrics.yoffset
);
}
return glm::vec2 (
cache.font->calcWidth(view),
lineHeight * cache.lines.size() + font->getYOffset()
cache.metrics.calcWidth(view),
lineHeight * cache.lines.size() + metrics.yoffset
);
}
@ -135,7 +136,7 @@ void Label::setText(std::wstring text) {
this->text = std::move(text);
cache.update(this->text, multiline, textWrap);
if (cache.font && autoresize) {
if (cache.fontId != 0 && autoresize) {
setSize(calcSize());
}
}
@ -203,7 +204,11 @@ uint Label::getLinesNumber() const {
void Label::draw(const DrawContext& pctx, const Assets& assets) {
auto batch = pctx.getBatch2D();
auto font = assets.get<Font>(fontName);
cache.prepare(font, static_cast<size_t>(glm::abs(getSize().x)));
cache.prepare(
reinterpret_cast<ptrdiff_t>(font),
font->getMetrics(),
static_cast<size_t>(glm::abs(getSize().x))
);
if (supplier) {
setText(supplier());