Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always create new text run, don't assume behavior. #420

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading