SixLabors / Fonts

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

TextBuilder.GenerateGlyphs fails with ArgumentOutOfRangeException #400

Closed lanwin closed 2 months ago

lanwin commented 3 months ago

Prerequisites

ImageSharp version

3.1.4

Other ImageSharp packages and versions

Fonts 2.0.3, Drawing 2.1.3

Environment (Operating system, version and so on)

Windows 11

.NET Framework version

NET7

Description

I found a text that lets TextBuilder.GenerateGlyphs crash with an ArgumentOutOfRangeException. I stripped down the text to only contain the problematic part.

It seems this is a BUG cause I would not expect GenerateGlyphs to fail with any exception.

Steps to Reproduce

var options = new RichTextOptions(new Font(SystemFonts.Get("Arial"), 16 * 2))
{
    WrappingLength = 1900
};

var content = """
                          NEWS_CATEGORY=EWF&NEWS_HASH=4b298ff9277ef9fdf515356be95ea3caf57cd36&OFFSET=0&SEARCH_VALUE=CA88105E1088&ID_NEWS
          """;

var contentGlyphs = TextBuilder.GenerateGlyphs(content, options);

Images

No response

JimBobSquarePants commented 3 months ago

@lanwin Do you have a stack trace? I need to see which repository this issue should be transfered to. Drawing or Fonts

lanwin commented 3 months ago

Yes I have:

   at System.ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at SixLabors.Fonts.TextLayout.TextLine.BidiReOrder()
   at SixLabors.Fonts.TextLayout.BreakLines(ReadOnlySpan`1 text, TextOptions options, BidiRun[] bidiRuns, Dictionary`2 bidiMap, GlyphPositioningCollection positionings, LayoutMode layoutMode)
   at SixLabors.Fonts.TextLayout.ProcessText(ReadOnlySpan`1 text, TextOptions options)
   at SixLabors.Fonts.TextLayout.GenerateLayout(ReadOnlySpan`1 text, TextOptions options)
   at SixLabors.Fonts.TextRenderer.RenderText(ReadOnlySpan`1 text, TextOptions options)
   at SixLabors.Fonts.TextRenderer.RenderText(String text, TextOptions options)
   at SixLabors.ImageSharp.Drawing.TextBuilder.GenerateGlyphs(String text, TextOptions textOptions)
tocsoft commented 3 months ago

As the Stacktrace points to an issue at the Fonts layer I've moved the issue over here to the Fonts repo.

JimBobSquarePants commented 2 months ago

Finally got some time to look at this and the issue is obvious enough. We were incorrectly stripping the data from the entire leading line worth of whitespace. PR opened.