SixLabors / Fonts

:black_nib: Font loading and layout library.
https://sixlabors.com/products/fonts
Other
305 stars 69 forks source link

RichTextRun.End behaves as an inclusive index only when it is the same as the end of the string #412

Open EbiseLutica opened 1 month ago

EbiseLutica commented 1 month ago

Prerequisites

Description

According to the API documentation, RichTextRun.End is recognized as an exclusive end index, meaning the character at this position should not be decorated.

However, when RichTextRun.End is set to length - 1 (the same as the last index of the string), the RichTextRun decorates the entire string. Adding one more character at the end causes it to behave as expected.

Steps to Reproduce

Below is the sample code to reproduce:

using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

var img = new Image<Rgba32>(150, 150);
RichTextOptions GenerateOption(PointF origin) => new(SystemFonts.Get("Arial").CreateFont(24))
{
    Origin = origin,
    TextRuns = new[]
    {
        new RichTextRun { Start = 0, End = 4, Brush = new SolidBrush(Color.Red) }
    }
};

img.Mutate(ctx =>
{
    ctx.Clear(Color.White);
    ctx.DrawText(GenerateOption(new PointF(10, 10)), "abcd", new SolidBrush(Color.Black));
    ctx.DrawText(GenerateOption(new PointF(10, 30)), "abcde", new SolidBrush(Color.Black));
    ctx.DrawText(GenerateOption(new PointF(10, 50)), "abcdef", new SolidBrush(Color.Black));
});

img.SaveAsPng("output.png");

and below is the output file. output

System Configuration

JimBobSquarePants commented 1 month ago

Thanks for this. The underlying issue will be in the Fonts library, so I'll transfer this issue to that repository.