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

Do not insert mandatory break when line is broken by wrapping. #433

Merged
merged 2 commits into from
Dec 16, 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
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);
}
}
}
Loading