SixLabors / ImageSharp.Drawing

:pen: Extensions to ImageSharp containing a cross-platform 2D polygon manipulation API and drawing operations.
https://sixlabors.com/products/imagesharp-drawing/
Other
285 stars 38 forks source link

DrawText breaks when text ends with a trailing slash #237

Closed eangelov closed 2 years ago

eangelov commented 2 years ago

DSCN0012

Prerequisites

Description

An "Index was out of range. Must be non-negative and less than the size of the collection." is thrown when text that is to be rendered ends with an trailing slash.

System.ArgumentOutOfRangeException
  HResult=0x80131502
  Message=Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
  Source=System.Private.CoreLib
  StackTrace:
   at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
   at SixLabors.Fonts.GlyphSubstitutionCollection.GetGlyphShapingData(Int32 index)
   at SixLabors.Fonts.Tables.AdvancedTypographic.Shapers.DefaultShaper.AssignFeatures(IGlyphShapingCollection collection, Int32 index, Int32 count)
   at SixLabors.Fonts.Tables.AdvancedTypographic.GSubTable.ApplySubstitution(FontMetrics fontMetrics, GlyphSubstitutionCollection collection, KerningMode kerningMode)
   at SixLabors.Fonts.TextLayout.DoFontRun(ReadOnlySpan`1 text, TextOptions options, FontMetrics fontMetrics, BidiRun[] bidiRuns, Dictionary`2 bidiMap, GlyphSubstitutionCollection substitutions, GlyphPositioningCollection positionings)
   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.ImageSharp.Drawing.Processing.Processors.Text.DrawTextProcessor`1.BeforeImageApply()
   at SixLabors.ImageSharp.Processing.Processors.ImageProcessor`1.SixLabors.ImageSharp.Processing.Processors.IImageProcessor<TPixel>.Execute()
   at SixLabors.ImageSharp.Processing.DefaultImageProcessorContext`1.ApplyProcessor(IImageProcessor processor, Rectangle rectangle)
   at SixLabors.ImageSharp.Processing.DefaultImageProcessorContext`1.ApplyProcessor(IImageProcessor processor)
   at SixLabors.ImageSharp.Drawing.Processing.DrawTextExtensions.DrawText(IImageProcessingContext source, String text, Font font, Color color, PointF location)
   at Program.<>c.<<Main>$>b__0_0(IImageProcessingContext x) in C:\Users\eangelov\source\repos\TestStuffConsoleAppDotNet6\Program.cs:line 10
   at SixLabors.ImageSharp.Processing.ProcessingExtensions.ProcessingVisitor.Visit[TPixel](Image`1 image)
![image](https://user-images.githubusercontent.com/3601104/179510055-e0bb7a9e-08dd-4a7b-aa29-5942327cf394.png)

Steps to Reproduce

using Image image = Image.Load( "DSCN0012.jpg" );
image.Mutate( 
    x => x.DrawText( 
        "Sample Text/"
        , SystemFonts.CreateFont( "Arial", 24, FontStyle.Regular )
        , Color.ParseHex( "#ff0000" )
        , new PointF( 10, 10 ) 
    ) 
);
image.SaveAsJpeg( "outputImage.jpg" );

System Configuration

JimBobSquarePants commented 2 years ago

If you update SixLabors.Fonts to the following using MyGet.

<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta17.16" />

The issue will go away. I'm looking into releasing new betas on NuGet as this has come up a few times now.