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 1 commit
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
4 changes: 2 additions & 2 deletions samples/DrawWithImageSharp/DrawWithImageSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<DebugType>portable</DebugType>
Expand Down Expand Up @@ -46,7 +46,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.3" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

Expand Down
29 changes: 23 additions & 6 deletions src/SixLabors.Fonts/TextLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,11 +1055,14 @@ private static TextBox BreakLines(
// 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 @@ -1140,6 +1143,7 @@ private static TextBox BreakLines(
}

// Find the next line break.
bool lastMandatory = lastLineBreak.Required;
if (currentLineBreak.PositionWrap == codePointIndex)
{
lastLineBreak = currentLineBreak;
Expand All @@ -1161,9 +1165,22 @@ private static TextBox BreakLines(
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