Skip to content

Commit

Permalink
Merge pull request #433 from SixLabors/js/fix-line-break
Browse files Browse the repository at this point in the history
Do not insert mandatory break when line is broken by wrapping.
  • Loading branch information
JimBobSquarePants authored Dec 16, 2024
2 parents 1ee650a + 8600aa6 commit 7cfff73
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/SixLabors.Fonts/TextLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,11 +1093,14 @@ VerticalOrientationType.Rotate or
// Mandatory wrap at index.
if (currentLineBreak.PositionWrap == codePointIndex && currentLineBreak.Required)
{
textLines.Add(textLine.Finalize());
glyphCount += textLine.Count;
textLine = new();
lineAdvance = 0;
requiredBreak = true;
if (textLine.Count > 0)
{
textLines.Add(textLine.Finalize());
glyphCount += textLine.Count;
textLine = new();
lineAdvance = 0;
requiredBreak = true;
}
}
else if (shouldWrap && lineAdvance + glyphAdvance >= wrappingLength)
{
Expand Down Expand Up @@ -1178,6 +1181,7 @@ VerticalOrientationType.Rotate or
}

// Find the next line break.
bool lastMandatory = lastLineBreak.Required;
if (currentLineBreak.PositionWrap == codePointIndex)
{
lastLineBreak = currentLineBreak;
Expand All @@ -1199,9 +1203,22 @@ VerticalOrientationType.Rotate or
continue;
}

// The previous line ended with a non-mandatory break at the wrapping length but the new line starts
// with a mandatory line break. We should not add a new line in this case as the line break has
// already been synthesized.
if (textLine.Count == 0
&& textLines.Count > 0
&& !lastMandatory
&& CodePoint.IsNewLine(codePoint))
{
codePointIndex++;
graphemeCodePointIndex++;
continue;
}

// Do not add new lines unless at position zero.
if (textLine.Count > 0 && CodePoint.IsNewLine(codePoint))
{
// Do not add new lines unless at position zero.
codePointIndex++;
graphemeCodePointIndex++;
continue;
Expand Down
31 changes: 31 additions & 0 deletions tests/SixLabors.Fonts.Tests/Issues/Issues_431.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Numerics;

namespace SixLabors.Fonts.Tests.Issues;

public class Issues_431
{
[Fact]
public void ShouldNotInsertExtraLineBreaks()
{
if (SystemFonts.TryGet("Arial", out FontFamily family))
{
Font font = family.CreateFont(60);
const string text = "- Lorem ipsullll\ndolor sit amet\n-consectetur elit";

TextOptions options = new(font)
{
Origin = new Vector2(50, 20),
WrappingLength = 400,
};

int lineCount = TextMeasurer.CountLines(text, options);
Assert.Equal(4, lineCount);

IReadOnlyList<GlyphLayout> layout = TextLayout.GenerateLayout(text, options);
Assert.Equal(46, layout.Count);
}
}
}

0 comments on commit 7cfff73

Please sign in to comment.