Skip to content

Commit

Permalink
Merge pull request #395 from batwomankt/fix-issue-393
Browse files Browse the repository at this point in the history
trim whitespace from EOLs when line split is found
  • Loading branch information
JimBobSquarePants authored Mar 19, 2024
2 parents 0b60d5a + 73cff2e commit 30c8f15
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/SixLabors.Fonts/TextLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,9 @@ public TextLine SplitAt(LineBreak lineBreak, bool keepAll)

if (index == 0)
{
// Now trim trailing whitespace from this line in the case of an exact
// length line break (non CJK)
this.TrimTrailingWhitespaceAndRecalculateMetrics();
return this;
}

Expand All @@ -1342,6 +1345,9 @@ public TextLine SplitAt(LineBreak lineBreak, bool keepAll)

if (index == 0)
{
// Now trim trailing whitespace from this line in the case of an exact
// length line break (non CJK)
this.TrimTrailingWhitespaceAndRecalculateMetrics();
return this;
}
}
Expand Down Expand Up @@ -1372,7 +1378,14 @@ public TextLine SplitAt(LineBreak lineBreak, bool keepAll)
this.data.RemoveRange(index, this.data.Count - index);

// Now trim trailing whitespace from this line.
index = this.data.Count;
this.TrimTrailingWhitespaceAndRecalculateMetrics();

return result;
}

private void TrimTrailingWhitespaceAndRecalculateMetrics()
{
int index = this.data.Count;
while (index > 0)
{
if (!CodePoint.IsWhiteSpace(this.data[index - 1].CodePoint))
Expand All @@ -1389,10 +1402,10 @@ public TextLine SplitAt(LineBreak lineBreak, bool keepAll)
}

// Lastly recalculate this line metrics.
advance = 0;
ascender = 0;
descender = 0;
lineHeight = 0;
float advance = 0;
float ascender = 0;
float descender = 0;
float lineHeight = 0;
for (int i = 0; i < this.data.Count; i++)
{
GlyphLayoutData glyph = this.data[i];
Expand All @@ -1406,8 +1419,6 @@ public TextLine SplitAt(LineBreak lineBreak, bool keepAll)
this.ScaledMaxAscender = ascender;
this.ScaledMaxDescender = descender;
this.ScaledMaxLineHeight = lineHeight;

return result;
}

public TextLine Finalize() => this.BidiReOrder();
Expand Down
2 changes: 1 addition & 1 deletion tests/SixLabors.Fonts.Tests/Issues/Issues_367.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void ShouldMatchBrowserBreak()
Assert.Equal(3, lineCount);

FontRectangle advance = TextMeasurer.MeasureAdvance(text, options);
Assert.Equal(365, advance.Width);
Assert.Equal(355, advance.Width);
Assert.Equal(48, advance.Height);
}
}

0 comments on commit 30c8f15

Please sign in to comment.