Skip to content

Commit 7cfff73

Browse files
Merge pull request #433 from SixLabors/js/fix-line-break
Do not insert mandatory break when line is broken by wrapping.
2 parents 1ee650a + 8600aa6 commit 7cfff73

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/SixLabors.Fonts/TextLayout.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,11 +1093,14 @@ VerticalOrientationType.Rotate or
10931093
// Mandatory wrap at index.
10941094
if (currentLineBreak.PositionWrap == codePointIndex && currentLineBreak.Required)
10951095
{
1096-
textLines.Add(textLine.Finalize());
1097-
glyphCount += textLine.Count;
1098-
textLine = new();
1099-
lineAdvance = 0;
1100-
requiredBreak = true;
1096+
if (textLine.Count > 0)
1097+
{
1098+
textLines.Add(textLine.Finalize());
1099+
glyphCount += textLine.Count;
1100+
textLine = new();
1101+
lineAdvance = 0;
1102+
requiredBreak = true;
1103+
}
11011104
}
11021105
else if (shouldWrap && lineAdvance + glyphAdvance >= wrappingLength)
11031106
{
@@ -1178,6 +1181,7 @@ VerticalOrientationType.Rotate or
11781181
}
11791182

11801183
// Find the next line break.
1184+
bool lastMandatory = lastLineBreak.Required;
11811185
if (currentLineBreak.PositionWrap == codePointIndex)
11821186
{
11831187
lastLineBreak = currentLineBreak;
@@ -1199,9 +1203,22 @@ VerticalOrientationType.Rotate or
11991203
continue;
12001204
}
12011205

1206+
// The previous line ended with a non-mandatory break at the wrapping length but the new line starts
1207+
// with a mandatory line break. We should not add a new line in this case as the line break has
1208+
// already been synthesized.
1209+
if (textLine.Count == 0
1210+
&& textLines.Count > 0
1211+
&& !lastMandatory
1212+
&& CodePoint.IsNewLine(codePoint))
1213+
{
1214+
codePointIndex++;
1215+
graphemeCodePointIndex++;
1216+
continue;
1217+
}
1218+
1219+
// Do not add new lines unless at position zero.
12021220
if (textLine.Count > 0 && CodePoint.IsNewLine(codePoint))
12031221
{
1204-
// Do not add new lines unless at position zero.
12051222
codePointIndex++;
12061223
graphemeCodePointIndex++;
12071224
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)