Account for different DPI scales in font fallbacks.

Fixes #1675
This commit is contained in:
Alex Szpakowski
2021-04-04 16:01:22 -03:00
parent cbc0602620
commit c991f999eb
2 changed files with 18 additions and 12 deletions
+17 -11
View File
@@ -201,7 +201,7 @@ void Font::unloadVolatile()
images.clear(); images.clear();
} }
love::font::GlyphData *Font::getRasterizerGlyphData(uint32 glyph) love::font::GlyphData *Font::getRasterizerGlyphData(uint32 glyph, float &dpiscale)
{ {
// Use spaces for the tab 'glyph'. // Use spaces for the tab 'glyph'.
if (glyph == 9 && useSpacesAsTab) if (glyph == 9 && useSpacesAsTab)
@@ -216,21 +216,27 @@ love::font::GlyphData *Font::getRasterizerGlyphData(uint32 glyph)
spacegd->release(); spacegd->release();
dpiscale = rasterizers[0]->getDPIScale();
return new love::font::GlyphData(glyph, gm, fmt); return new love::font::GlyphData(glyph, gm, fmt);
} }
for (const StrongRef<love::font::Rasterizer> &r : rasterizers) for (const StrongRef<love::font::Rasterizer> &r : rasterizers)
{ {
if (r->hasGlyph(glyph)) if (r->hasGlyph(glyph))
{
dpiscale = r->getDPIScale();
return r->getGlyphData(glyph); return r->getGlyphData(glyph);
}
} }
dpiscale = rasterizers[0]->getDPIScale();
return rasterizers[0]->getGlyphData(glyph); return rasterizers[0]->getGlyphData(glyph);
} }
const Font::Glyph &Font::addGlyph(uint32 glyph) const Font::Glyph &Font::addGlyph(uint32 glyph)
{ {
StrongRef<love::font::GlyphData> gd(getRasterizerGlyphData(glyph), Acquire::NORETAIN); float glyphdpiscale = getDPIScale();
StrongRef<love::font::GlyphData> gd(getRasterizerGlyphData(glyph, glyphdpiscale), Acquire::NORETAIN);
int w = gd->getWidth(); int w = gd->getWidth();
int h = gd->getHeight(); int h = gd->getHeight();
@@ -259,7 +265,7 @@ const Font::Glyph &Font::addGlyph(uint32 glyph)
Glyph g; Glyph g;
g.texture = 0; 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); memset(g.vertices, 0, sizeof(GlyphVertex) * 4);
@@ -287,18 +293,18 @@ const Font::Glyph &Font::addGlyph(uint32 glyph)
// 1---3 // 1---3
const GlyphVertex verts[4] = const GlyphVertex verts[4] =
{ {
{float(-o), float(-o), normToUint16((tX-o)/tWidth), normToUint16((tY-o)/tHeight), c}, {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}, {float(-o), (h+o)/glyphdpiscale, 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)/glyphdpiscale, 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} {(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. // Copy vertex data to the glyph and set proper bearing.
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
g.vertices[i] = verts[i]; g.vertices[i] = verts[i];
g.vertices[i].x += gd->getBearingX() / dpiScale; g.vertices[i].x += gd->getBearingX() / glyphdpiscale;
g.vertices[i].y -= gd->getBearingY() / dpiScale; g.vertices[i].y -= gd->getBearingY() / glyphdpiscale;
} }
textureX += w + TEXTURE_PADDING; textureX += w + TEXTURE_PADDING;
@@ -327,13 +333,13 @@ float Font::getKerning(uint32 leftglyph, uint32 rightglyph)
if (it != kerning.end()) if (it != kerning.end())
return it->second; 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) for (const auto &r : rasterizers)
{ {
if (r->hasGlyph(leftglyph) && r->hasGlyph(rightglyph)) 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; break;
} }
} }
+1 -1
View File
@@ -203,7 +203,7 @@ private:
void createTexture(); void createTexture();
TextureSize getNextTextureSize() const; TextureSize getNextTextureSize() const;
love::font::GlyphData *getRasterizerGlyphData(uint32 glyph); love::font::GlyphData *getRasterizerGlyphData(uint32 glyph, float &dpiscale);
const Glyph &addGlyph(uint32 glyph); const Glyph &addGlyph(uint32 glyph);
const Glyph &findGlyph(uint32 glyph); const Glyph &findGlyph(uint32 glyph);
float getKerning(uint32 leftglyph, uint32 rightglyph); float getKerning(uint32 leftglyph, uint32 rightglyph);