Skip to content

Commit b64822e

Browse files
Do not insert mandatory break when line is broken by wrapping.
1 parent 623498a commit b64822e

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

samples/DrawWithImageSharp/DrawWithImageSharp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<DebugType>portable</DebugType>
@@ -46,7 +46,7 @@
4646
</ItemGroup>
4747

4848
<ItemGroup>
49-
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.3" />
49+
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
5050
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
5151
</ItemGroup>
5252

src/SixLabors.Fonts/TextLayout.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,11 +1055,14 @@ private static TextBox BreakLines(
10551055
// Mandatory wrap at index.
10561056
if (currentLineBreak.PositionWrap == codePointIndex && currentLineBreak.Required)
10571057
{
1058-
textLines.Add(textLine.Finalize());
1059-
glyphCount += textLine.Count;
1060-
textLine = new();
1061-
lineAdvance = 0;
1062-
requiredBreak = true;
1058+
if (textLine.Count > 0)
1059+
{
1060+
textLines.Add(textLine.Finalize());
1061+
glyphCount += textLine.Count;
1062+
textLine = new();
1063+
lineAdvance = 0;
1064+
requiredBreak = true;
1065+
}
10631066
}
10641067
else if (shouldWrap && lineAdvance + glyphAdvance >= wrappingLength)
10651068
{
@@ -1140,6 +1143,7 @@ private static TextBox BreakLines(
11401143
}
11411144

11421145
// Find the next line break.
1146+
bool lastMandatory = lastLineBreak.Required;
11431147
if (currentLineBreak.PositionWrap == codePointIndex)
11441148
{
11451149
lastLineBreak = currentLineBreak;
@@ -1161,9 +1165,22 @@ private static TextBox BreakLines(
11611165
continue;
11621166
}
11631167

1168+
// The previous line ended with a non-mandatory break at the wrapping length but the new line starts
1169+
// with a mandatory line break. We should not add a new line in this case as the line break has
1170+
// already been synthesized.
1171+
if (textLine.Count == 0
1172+
&& textLines.Count > 0
1173+
&& !lastMandatory
1174+
&& CodePoint.IsNewLine(codePoint))
1175+
{
1176+
codePointIndex++;
1177+
graphemeCodePointIndex++;
1178+
continue;
1179+
}
1180+
1181+
// Do not add new lines unless at position zero.
11641182
if (textLine.Count > 0 && CodePoint.IsNewLine(codePoint))
11651183
{
1166-
// Do not add new lines unless at position zero.
11671184
codePointIndex++;
11681185
graphemeCodePointIndex++;
11691186
continue;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Numerics;
5+
6+
namespace SixLabors.Fonts.Tests.Issues;
7+
8+
public class Issues_431
9+
{
10+
[Fact]
11+
public void ShouldNotInsertExtraLineBreaks()
12+
{
13+
if (SystemFonts.TryGet("Arial", out FontFamily family))
14+
{
15+
Font font = family.CreateFont(60);
16+
const string text = "- Lorem ipsullll\ndolor sit amet\n-consectetur elit";
17+
18+
TextOptions options = new(font)
19+
{
20+
Origin = new Vector2(50, 20),
21+
WrappingLength = 400,
22+
};
23+
24+
int lineCount = TextMeasurer.CountLines(text, options);
25+
Assert.Equal(4, lineCount);
26+
27+
IReadOnlyList<GlyphLayout> layout = TextLayout.GenerateLayout(text, options);
28+
Assert.Equal(46, layout.Count);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)