Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trim whitespace from EOLs when line split is found #395

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that in case of exact length line breaks, there could be whitespace that remains untrimmed - the addition of this line here would take care of that (without losing the benefits of the fix added for issue 367)

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);
batwomankt marked this conversation as resolved.
Show resolved Hide resolved
Assert.Equal(48, advance.Height);
}
}
Loading