From 7e34e0ad2119aa40438912161dc631e254a0b0af Mon Sep 17 00:00:00 2001 From: Ron Tseytlin Date: Mon, 16 Dec 2024 21:42:42 +0200 Subject: [PATCH 1/2] Improve justified text alignment --- src/modules/font/GenericShaper.cpp | 7 ++++++- src/modules/font/freetype/HarfbuzzShaper.cpp | 7 ++++++- src/modules/graphics/Font.cpp | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/modules/font/GenericShaper.cpp b/src/modules/font/GenericShaper.cpp index e83f13f57..675b5cab8 100644 --- a/src/modules/font/GenericShaper.cpp +++ b/src/modules/font/GenericShaper.cpp @@ -49,6 +49,7 @@ void GenericShaper::computeGlyphPositions(const ColoredCodepoints &codepoints, R // Spacing counter and newline handling. Vector2 curpos = offset; + float spacingremainder = 0; float maxwidth = 0; uint32 prevglyph = 0; @@ -125,7 +126,11 @@ void GenericShaper::computeGlyphPositions(const ColoredCodepoints &codepoints, R // Account for extra spacing given to space characters. if (g == ' ' && extraspacing != 0.0f) - curpos.x += extraspacing; + { + spacingremainder += fmod(extraspacing, 1); + curpos.x += floorf(extraspacing) + floorf(spacingremainder); + spacingremainder = fmod(spacingremainder, 1); + } prevglyph = g; } diff --git a/src/modules/font/freetype/HarfbuzzShaper.cpp b/src/modules/font/freetype/HarfbuzzShaper.cpp index d7fa27651..692c72371 100644 --- a/src/modules/font/freetype/HarfbuzzShaper.cpp +++ b/src/modules/font/freetype/HarfbuzzShaper.cpp @@ -208,6 +208,7 @@ void HarfbuzzShaper::computeGlyphPositions(const ColoredCodepoints &codepoints, offset.y += getBaseline(); Vector2 curpos = offset; + float spacingremainder = 0; int colorindex = 0; int ncolors = (int)codepoints.colors.size(); @@ -312,7 +313,11 @@ void HarfbuzzShaper::computeGlyphPositions(const ColoredCodepoints &codepoints, // Account for extra spacing given to space characters. if (clustercodepoint == ' ' && extraspacing != 0.0f) - curpos.x += extraspacing; + { + spacingremainder += fmod(extraspacing, 1); + curpos.x += floorf(extraspacing) + floorf(spacingremainder); + spacingremainder = fmod(spacingremainder, 1); + } } } diff --git a/src/modules/graphics/Font.cpp b/src/modules/graphics/Font.cpp index b2b20fe81..bbf0d3fbb 100644 --- a/src/modules/graphics/Font.cpp +++ b/src/modules/graphics/Font.cpp @@ -508,7 +508,7 @@ std::vector Font::generateVerticesFormatted(const love::font: auto end = start + range.getSize(); float numspaces = std::count(start, end, ' '); if (width < wrap && numspaces >= 1) - extraspacing = floorf((wrap - width) / numspaces); + extraspacing = (wrap - width) / (numspaces - 1); else extraspacing = 0.0f; break; From d4a2c460deb11e707409d2926d3ddedc93b9de2b Mon Sep 17 00:00:00 2001 From: Ron Tseytlin Date: Mon, 16 Dec 2024 22:47:35 +0200 Subject: [PATCH 2/2] Ignore last space in justified text --- src/modules/graphics/Font.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/modules/graphics/Font.cpp b/src/modules/graphics/Font.cpp index bbf0d3fbb..6b882b69c 100644 --- a/src/modules/graphics/Font.cpp +++ b/src/modules/graphics/Font.cpp @@ -507,8 +507,12 @@ std::vector Font::generateVerticesFormatted(const love::font: auto start = text.cps.begin() + range.getOffset(); auto end = start + range.getSize(); float numspaces = std::count(start, end, ' '); + + if (text.cps[range.last] == ' ') + --numspaces; + if (width < wrap && numspaces >= 1) - extraspacing = (wrap - width) / (numspaces - 1); + extraspacing = (wrap - width) / numspaces; else extraspacing = 0.0f; break;