From 4fc201668b4eb5a9bb421681129af9d39e933348 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 29 Oct 2024 13:15:52 +1000 Subject: [PATCH] Always create new text run, don't assume behavior. --- src/SixLabors.Fonts/TextLayout.cs | 21 +++--------- .../Issues/Issues_412.cs | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 tests/SixLabors.Fonts.Tests/Issues/Issues_412.cs diff --git a/src/SixLabors.Fonts/TextLayout.cs b/src/SixLabors.Fonts/TextLayout.cs index 4985f184..0f521d1c 100644 --- a/src/SixLabors.Fonts/TextLayout.cs +++ b/src/SixLabors.Fonts/TextLayout.cs @@ -72,23 +72,12 @@ public static IReadOnlyList BuildTextRuns(ReadOnlySpan 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; diff --git a/tests/SixLabors.Fonts.Tests/Issues/Issues_412.cs b/tests/SixLabors.Fonts.Tests/Issues/Issues_412.cs new file mode 100644 index 00000000..6a61ff68 --- /dev/null +++ b/tests/SixLabors.Fonts.Tests/Issues/Issues_412.cs @@ -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 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); + } +}