Skip to content

Commit

Permalink
Always create new text run, don't assume behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Oct 29, 2024
1 parent 43b97c4 commit 4fc2016
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
21 changes: 5 additions & 16 deletions src/SixLabors.Fonts/TextLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,12 @@ public static IReadOnlyList<TextRun> BuildTextRuns(ReadOnlySpan<char> text, Text
// Add a final run if required.
if (start < end)
{
// Offset error by user, last index in input string
// instead of exclusive index.
if (start == end - 1)
textRuns.Add(new()
{
int prevIndex = textRuns.Count - 1;
TextRun previous = textRuns[prevIndex];
previous.End++;
}
else
{
textRuns.Add(new()
{
Start = start,
End = end,
Font = options.Font
});
}
Start = start,
End = end,
Font = options.Font
});
}

return textRuns;
Expand Down
34 changes: 34 additions & 0 deletions tests/SixLabors.Fonts.Tests/Issues/Issues_412.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

namespace SixLabors.Fonts.Tests.Issues;

public class Issues_412
{
[Fact]
public void ShouldCreateCorrectTextRunCount()
{
FontCollection collection = new();
FontFamily family = collection.Add(TestFonts.OpenSansFile);
Font font = family.CreateFont(24);

TextOptions options = new(font)
{
TextRuns = new[]
{
new TextRun { Start = 0, End = 4 }
}
};

IReadOnlyList<TextRun> runs = TextLayout.BuildTextRuns("abcde", options);
Assert.Equal(2, runs.Count);

TextRun run = runs[0];
Assert.Equal(0, run.Start);
Assert.Equal(4, run.End);

run = runs[1];
Assert.Equal(4, run.Start);
Assert.Equal(5, run.End);
}
}

0 comments on commit 4fc2016

Please sign in to comment.