From c991f999eb91ec34ebdcc6086aad0a7133f3138b Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sun, 4 Apr 2021 16:01:22 -0300 Subject: [PATCH] Account for different DPI scales in font fallbacks. Fixes #1675 --- src/modules/graphics/Font.cpp | 28 +++++++++++++++++----------- src/modules/graphics/Font.h | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/modules/graphics/Font.cpp b/src/modules/graphics/Font.cpp index e42ab69f5..b6455689e 100644 --- a/src/modules/graphics/Font.cpp +++ b/src/modules/graphics/Font.cpp @@ -201,7 +201,7 @@ void Font::unloadVolatile() images.clear(); } -love::font::GlyphData *Font::getRasterizerGlyphData(uint32 glyph) +love::font::GlyphData *Font::getRasterizerGlyphData(uint32 glyph, float &dpiscale) { // Use spaces for the tab 'glyph'. if (glyph == 9 && useSpacesAsTab) @@ -216,21 +216,27 @@ love::font::GlyphData *Font::getRasterizerGlyphData(uint32 glyph) spacegd->release(); + dpiscale = rasterizers[0]->getDPIScale(); return new love::font::GlyphData(glyph, gm, fmt); } for (const StrongRef &r : rasterizers) { if (r->hasGlyph(glyph)) + { + dpiscale = r->getDPIScale(); return r->getGlyphData(glyph); + } } + dpiscale = rasterizers[0]->getDPIScale(); return rasterizers[0]->getGlyphData(glyph); } const Font::Glyph &Font::addGlyph(uint32 glyph) { - StrongRef gd(getRasterizerGlyphData(glyph), Acquire::NORETAIN); + float glyphdpiscale = getDPIScale(); + StrongRef gd(getRasterizerGlyphData(glyph, glyphdpiscale), Acquire::NORETAIN); int w = gd->getWidth(); int h = gd->getHeight(); @@ -259,7 +265,7 @@ const Font::Glyph &Font::addGlyph(uint32 glyph) Glyph g; g.texture = 0; - g.spacing = floorf(gd->getAdvance() / dpiScale + 0.5f); + g.spacing = floorf(gd->getAdvance() / glyphdpiscale + 0.5f); memset(g.vertices, 0, sizeof(GlyphVertex) * 4); @@ -287,18 +293,18 @@ const Font::Glyph &Font::addGlyph(uint32 glyph) // 1---3 const GlyphVertex verts[4] = { - {float(-o), float(-o), normToUint16((tX-o)/tWidth), normToUint16((tY-o)/tHeight), c}, - {float(-o), (h+o)/dpiScale, normToUint16((tX-o)/tWidth), normToUint16((tY+h+o)/tHeight), c}, - {(w+o)/dpiScale, float(-o), normToUint16((tX+w+o)/tWidth), normToUint16((tY-o)/tHeight), c}, - {(w+o)/dpiScale, (h+o)/dpiScale, normToUint16((tX+w+o)/tWidth), normToUint16((tY+h+o)/tHeight), c} + {float(-o), float(-o), normToUint16((tX-o)/tWidth), normToUint16((tY-o)/tHeight), c}, + {float(-o), (h+o)/glyphdpiscale, normToUint16((tX-o)/tWidth), normToUint16((tY+h+o)/tHeight), c}, + {(w+o)/glyphdpiscale, float(-o), normToUint16((tX+w+o)/tWidth), normToUint16((tY-o)/tHeight), c}, + {(w+o)/glyphdpiscale, (h+o)/glyphdpiscale, normToUint16((tX+w+o)/tWidth), normToUint16((tY+h+o)/tHeight), c} }; // Copy vertex data to the glyph and set proper bearing. for (int i = 0; i < 4; i++) { g.vertices[i] = verts[i]; - g.vertices[i].x += gd->getBearingX() / dpiScale; - g.vertices[i].y -= gd->getBearingY() / dpiScale; + g.vertices[i].x += gd->getBearingX() / glyphdpiscale; + g.vertices[i].y -= gd->getBearingY() / glyphdpiscale; } textureX += w + TEXTURE_PADDING; @@ -327,13 +333,13 @@ float Font::getKerning(uint32 leftglyph, uint32 rightglyph) if (it != kerning.end()) return it->second; - float k = rasterizers[0]->getKerning(leftglyph, rightglyph); + float k = rasterizers[0]->getKerning(leftglyph, rightglyph) / dpiScale + 0.5f; for (const auto &r : rasterizers) { if (r->hasGlyph(leftglyph) && r->hasGlyph(rightglyph)) { - k = floorf(r->getKerning(leftglyph, rightglyph) / dpiScale + 0.5f); + k = floorf(r->getKerning(leftglyph, rightglyph) / r->getDPIScale() + 0.5f); break; } } diff --git a/src/modules/graphics/Font.h b/src/modules/graphics/Font.h index dbca1920c..b38cb2cd8 100644 --- a/src/modules/graphics/Font.h +++ b/src/modules/graphics/Font.h @@ -203,7 +203,7 @@ private: void createTexture(); TextureSize getNextTextureSize() const; - love::font::GlyphData *getRasterizerGlyphData(uint32 glyph); + love::font::GlyphData *getRasterizerGlyphData(uint32 glyph, float &dpiscale); const Glyph &addGlyph(uint32 glyph); const Glyph &findGlyph(uint32 glyph); float getKerning(uint32 leftglyph, uint32 rightglyph);