diff --git a/src/SixLabors.Fonts/TextLayout.cs b/src/SixLabors.Fonts/TextLayout.cs index a9b1b02d..06889fc7 100644 --- a/src/SixLabors.Fonts/TextLayout.cs +++ b/src/SixLabors.Fonts/TextLayout.cs @@ -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; } @@ -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; } } @@ -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)) @@ -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]; @@ -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(); diff --git a/tests/SixLabors.Fonts.Tests/Issues/Issues_367.cs b/tests/SixLabors.Fonts.Tests/Issues/Issues_367.cs index e37a33d3..5a35bf6c 100644 --- a/tests/SixLabors.Fonts.Tests/Issues/Issues_367.cs +++ b/tests/SixLabors.Fonts.Tests/Issues/Issues_367.cs @@ -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); } }