Skip to content

Commit 4b403a7

Browse files
Merge pull request #525 from SixLabors/js/fix-justify
Do not justify last line.
2 parents 6df7590 + cbdfdee commit 4b403a7

File tree

18 files changed

+262
-26
lines changed

18 files changed

+262
-26
lines changed

src/SixLabors.Fonts/TextLayout.cs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static IReadOnlyList<TextRun> BuildTextRuns(ReadOnlySpan<char> text, Text
4343
int start = 0;
4444
int end = text.GetGraphemeCount();
4545
List<TextRun> textRuns = [];
46-
foreach (TextRun textRun in options.TextRuns!.OrderBy(x => x.Start))
46+
foreach (TextRun textRun in options.TextRuns.OrderBy(x => x.Start))
4747
{
4848
// Fill gaps within runs.
4949
if (textRun.Start > start)
@@ -1490,14 +1490,14 @@ VerticalOrientationType.Rotate or
14901490
if (textLine.TrySplitAt(breakAt, keepAll, out remaining))
14911491
{
14921492
processed = breakAt.PositionWrap;
1493-
textLines.Add(textLine.Finalize(options));
1493+
textLines.Add(textLine.Finalize(true));
14941494
textLine = remaining;
14951495
}
14961496
}
14971497
else if (textLine.TrySplitAt(wrappingLength, out remaining))
14981498
{
14991499
processed += textLine.Count;
1500-
textLines.Add(textLine.Finalize(options));
1500+
textLines.Add(textLine.Finalize());
15011501
textLine = remaining;
15021502
}
15031503
else
@@ -1529,7 +1529,7 @@ VerticalOrientationType.Rotate or
15291529
}
15301530

15311531
// Add the split part to the list and continue processing.
1532-
textLines.Add(textLine.Finalize(options));
1532+
textLines.Add(textLine.Finalize(breakAt.Required));
15331533
textLine = remaining;
15341534
}
15351535
else
@@ -1551,16 +1551,26 @@ VerticalOrientationType.Rotate or
15511551
break;
15521552
}
15531553

1554-
textLines.Add(textLine.Finalize(options));
1554+
textLines.Add(textLine.Finalize());
15551555
textLine = overflow;
15561556
}
15571557
}
15581558

1559-
textLines.Add(textLine.Finalize(options));
1559+
textLines.Add(textLine.Finalize(true));
15601560
break;
15611561
}
15621562
}
15631563

1564+
// Finally we justify each line that does not end a paragraph.
1565+
for (int i = 0; i < textLines.Count; i++)
1566+
{
1567+
TextLine line = textLines[i];
1568+
if (!line.SkipJustification)
1569+
{
1570+
line.Justify(options);
1571+
}
1572+
}
1573+
15641574
return new TextBox(textLines);
15651575
}
15661576

@@ -1696,6 +1706,8 @@ internal sealed class TextLine
16961706

16971707
public int Count => this.data.Count;
16981708

1709+
public bool SkipJustification { get; private set; }
1710+
16991711
public float ScaledLineAdvance { get; private set; }
17001712

17011713
public float ScaledMaxLineHeight { get; private set; } = -1;
@@ -1933,14 +1945,12 @@ private void TrimTrailingWhitespace()
19331945
}
19341946
}
19351947

1936-
public TextLine Finalize(TextOptions options)
1948+
public TextLine Finalize(bool skipJustification = false)
19371949
{
1950+
this.SkipJustification = skipJustification;
19381951
this.TrimTrailingWhitespace();
19391952
this.BidiReOrder();
19401953
RecalculateLineMetrics(this);
1941-
1942-
this.Justify(options);
1943-
RecalculateLineMetrics(this);
19441954
return this;
19451955
}
19461956

@@ -1975,6 +1985,11 @@ public void Justify(TextOptions options)
19751985
}
19761986
}
19771987

1988+
if (nonZeroCount == 0)
1989+
{
1990+
return;
1991+
}
1992+
19781993
float padding = delta / nonZeroCount;
19791994
for (int i = 0; i < this.data.Count - 1; i++)
19801995
{
@@ -1986,6 +2001,7 @@ public void Justify(TextOptions options)
19862001
}
19872002
}
19882003

2004+
RecalculateLineMetrics(this);
19892005
return;
19902006
}
19912007

@@ -2003,6 +2019,11 @@ public void Justify(TextOptions options)
20032019
}
20042020
}
20052021

2022+
if (whiteSpaceCount == 0)
2023+
{
2024+
return;
2025+
}
2026+
20062027
float padding = delta / whiteSpaceCount;
20072028
for (int i = 0; i < this.data.Count - 1; i++)
20082029
{
@@ -2014,6 +2035,8 @@ public void Justify(TextOptions options)
20142035
}
20152036
}
20162037
}
2038+
2039+
RecalculateLineMetrics(this);
20172040
}
20182041

20192042
public void BidiReOrder()
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)