Skip to content

Commit

Permalink
Merge pull request #2126 from steveRoll-git/main
Browse files Browse the repository at this point in the history
Improve `"justify"` text alignment
  • Loading branch information
slime73 authored Dec 25, 2024
2 parents 7076688 + d4a2c46 commit 8443928
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/modules/font/GenericShaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion src/modules/font/freetype/HarfbuzzShaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/modules/graphics/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8443928

Please sign in to comment.