From 64ba1dad4e8e9b214fd11cfd7c4391ed4bffb677 Mon Sep 17 00:00:00 2001 From: boolean-false Date: Sun, 27 Sep 2026 17:33:55 +0700 Subject: [PATCH] fix glyph positions --- src/coders/vector_fonts.cpp | 17 ++++++++++------- src/graphics/core/Font.cpp | 27 +++++++++++++-------------- src/graphics/core/Font.hpp | 1 + 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/coders/vector_fonts.cpp b/src/coders/vector_fonts.cpp index a2b6b55d0..9ebb3c8d8 100644 --- a/src/coders/vector_fonts.cpp +++ b/src/coders/vector_fonts.cpp @@ -122,10 +122,16 @@ namespace { std::vector& glyphs ) { int size = canvas.getWidth() / 16; + + const size_t end = (pageid + 1) * 256; + if (glyphs.size() < end) { + glyphs.resize(end); + } + for (int c = 0; c < 256; c++) { int codepoint = pageid << 8 | c; if (!renderGlyph(codepoint, bitmapDst)) { - glyphs.push_back(Glyph {0, size / 2}); + glyphs[codepoint] = Glyph {0, size / 2}; continue; } @@ -133,13 +139,10 @@ namespace { Glyph glyph { face->glyph->bitmap_top - size, - static_cast(face->glyph->advance.x >> 6) + static_cast(face->glyph->advance.x >> 6), + face->glyph->bitmap_left }; - if (codepoint < glyphs.size()) { - glyphs[codepoint] = std::move(glyph); - } else { - glyphs.push_back(std::move(glyph)); - } + glyphs[codepoint] = glyph; } canvas.flipY(); } diff --git a/src/graphics/core/Font.cpp b/src/graphics/core/Font.cpp index 4843387e4..e94881adc 100644 --- a/src/graphics/core/Font.cpp +++ b/src/graphics/core/Font.cpp @@ -176,7 +176,7 @@ static inline void draw_text( int y = 0; bool hasLines = false; - float baseAdvance = glm::length(right); + const float cellAdvance = interval * font.getLineHeight(); do { for (size_t i = 0; i < text.length(); i++) { @@ -193,12 +193,12 @@ static inline void draw_text( continue; } int yOffset = 0; - float advance = baseAdvance; + int xOffset = 0; + float advance = 1.0f; if (auto glyph = font.getGlyph(c)) { yOffset = glyph->yOffset; - advance = glyph->xAdvance / - static_cast(font.getLineHeight()) * 2.0f * - baseAdvance; + xOffset = glyph->xOffset; + advance = glyph->xAdvance / cellAdvance; } uint charpage = c >> 8; if (charpage == page){ @@ -207,7 +207,7 @@ static inline void draw_text( batch, pos, glm::vec2( - x, + x + xOffset / cellAdvance, y - yOffset * (is3d ? -1 : 1) / static_cast(font.getLineHeight()) ), @@ -221,7 +221,7 @@ static inline void draw_text( else if (charpage > page && charpage < next){ next = charpage; } - x += advance / baseAdvance; + x += advance; } page = next; next = MAX_CODEPAGES; @@ -329,23 +329,22 @@ const Glyph* Font::getGlyph(int codepoint) { if (codepoint < 0) { return nullptr; } - if (codepoint < glyphs.size()) { + const int codepage = codepoint >> 8; + if (codepoint < glyphs.size() && + (!fontFile.has_value() || + (codepage < pages.size() && pages[codepage]))) { return &glyphs.at(codepoint); } if (!this->fontFile.has_value() || this->fontFile->expired()) { return nullptr; } - int codepage = codepoint >> 8; if (codepage >= 1024) { return nullptr; } - if (glyphs.size() < (codepage << 8)) { - glyphs.resize(codepage << 8); - } auto fontFile = this->fontFile->lock(); if (pages.size() <= codepage) { - pages.resize(codepage); + pages.resize(codepage + 1); } - pages.push_back(fontFile->renderPage(codepage, glyphs, lineHeight)); + pages[codepage] = fontFile->renderPage(codepage, glyphs, lineHeight); return &glyphs.at(codepoint); } diff --git a/src/graphics/core/Font.hpp b/src/graphics/core/Font.hpp index 1f0e3ce8c..19e77ef83 100644 --- a/src/graphics/core/Font.hpp +++ b/src/graphics/core/Font.hpp @@ -26,6 +26,7 @@ namespace vector_fonts { struct Glyph { int yOffset; int xAdvance; + int xOffset = 0; }; class Font {