Skip to content

Commit 5767254

Browse files
Merge pull request #420 from SixLabors/js/fix-412
Always create new text run, don't assume behavior.
2 parents 00fa0f0 + 8422156 commit 5767254

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

src/SixLabors.Fonts/TextLayout.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,12 @@ public static IReadOnlyList<TextRun> BuildTextRuns(ReadOnlySpan<char> text, Text
7272
// Add a final run if required.
7373
if (start < end)
7474
{
75-
// Offset error by user, last index in input string
76-
// instead of exclusive index.
77-
if (start == end - 1)
75+
textRuns.Add(new()
7876
{
79-
int prevIndex = textRuns.Count - 1;
80-
TextRun previous = textRuns[prevIndex];
81-
previous.End++;
82-
}
83-
else
84-
{
85-
textRuns.Add(new()
86-
{
87-
Start = start,
88-
End = end,
89-
Font = options.Font
90-
});
91-
}
77+
Start = start,
78+
End = end,
79+
Font = options.Font
80+
});
9281
}
9382

9483
return textRuns;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
namespace SixLabors.Fonts.Tests.Issues;
5+
6+
public class Issues_412
7+
{
8+
[Fact]
9+
public void ShouldCreateCorrectTextRunCount()
10+
{
11+
FontCollection collection = new();
12+
FontFamily family = collection.Add(TestFonts.OpenSansFile);
13+
Font font = family.CreateFont(24);
14+
15+
TextOptions options = new(font)
16+
{
17+
TextRuns = new[]
18+
{
19+
new TextRun { Start = 0, End = 4 }
20+
}
21+
};
22+
23+
IReadOnlyList<TextRun> runs = TextLayout.BuildTextRuns("abcde", options);
24+
Assert.Equal(2, runs.Count);
25+
26+
TextRun run = runs[0];
27+
Assert.Equal(0, run.Start);
28+
Assert.Equal(4, run.End);
29+
30+
run = runs[1];
31+
Assert.Equal(4, run.Start);
32+
Assert.Equal(5, run.End);
33+
}
34+
}

0 commit comments

Comments
 (0)