Merge pull request #2126 from steveRoll-git/main

Improve `"justify"` text alignment
This commit is contained in:
Sasha Szpakowski
2024-12-25 17:50:53 -04:00
committed by GitHub
3 changed files with 17 additions and 3 deletions
+6 -1
View File
@@ -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;
}
+6 -1
View File
@@ -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);
}
}
}
+5 -1
View File
@@ -507,8 +507,12 @@ std::vector<Font::DrawCommand> 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 = floorf((wrap - width) / numspaces);
extraspacing = (wrap - width) / numspaces;
else
extraspacing = 0.0f;
break;