Skip to content

Cannot render custom font #46

@SebastianStehle

Description

@SebastianStehle

Hi,
I have a font family with icons generated from icomoon. I need to generate images from them and facing a bug with some icons.

The icon I am going to render is unicode "926"

I am facing several issues:

  1. The wrong glyphen is detected.

  2. For some glyphs, including for unicode 926 I get the following error:

SixLabors.ImageSharp.ImageProcessingException
  HResult=0x80131500
  Message=An error occurred when processing the image using DrawTextProcessor`1. See the inner exception for more detail.
  Source=SixLabors.ImageSharp
  StackTrace:
   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, TextGraphicsOptions options, String text, Font font, IBrush brush, IPen pen, PointF location)
   at SixLabors.ImageSharp.Drawing.Processing.DrawTextExtensions.DrawText(IImageProcessingContext source, TextGraphicsOptions options, String text, Font font, Color color, PointF location)
   at SixLabors.ImageSharp.Drawing.Processing.DrawTextExtensions.DrawText(IImageProcessingContext source, String text, Font font, Color color, PointF location)
   at generators.Images.<>c__DisplayClass2_0.<Test>b__0(IImageProcessingContext x) in C:\Users\Sebastian-PC\Downloads\events.json\events.json\generators\Images.cs:line 92
   at SixLabors.ImageSharp.Processing.ProcessingExtensions.Mutate[TPixel](Image`1 source, Configuration configuration, Action`1 operation)
   at SixLabors.ImageSharp.Processing.ProcessingExtensions.Mutate[TPixel](Image`1 source, Action`1 operation)
   at generators.Images.Test(String path) in C:\Users\Sebastian-PC\Downloads\events.json\events.json\generators\Images.cs:line 92
   at generators.Images.Generate(Dictionary`2 events, String path) in C:\Users\Sebastian-PC\Downloads\events.json\events.json\generators\Images.cs:line 32
   at generators.Program.Main(String[] args) in C:\Users\Sebastian-PC\Downloads\events.json\events.json\generators\Program.cs:line 85

  This exception was originally thrown at this call stack:
    SixLabors.ImageSharp.Drawing.InternalPath.Simplify(System.Collections.Generic.IEnumerable<SixLabors.ImageSharp.PointF>, bool)
    SixLabors.ImageSharp.Drawing.ComplexPolygon.EnsureInternalPathsInitalized()
    SixLabors.ImageSharp.Drawing.ComplexPolygon.FindIntersections(SixLabors.ImageSharp.PointF, SixLabors.ImageSharp.PointF, System.Span<SixLabors.ImageSharp.PointF>, SixLabors.ImageSharp.Drawing.IntersectionRule)
    SixLabors.ImageSharp.Drawing.Processing.Processors.Text.DrawTextProcessor<TPixel>.CachingGlyphRenderer.Render(SixLabors.ImageSharp.Drawing.IPath)
    SixLabors.ImageSharp.Drawing.Processing.Processors.Text.DrawTextProcessor<TPixel>.CachingGlyphRenderer.EndGlyph()
    SixLabors.Fonts.GlyphInstance.RenderTo(SixLabors.Fonts.IGlyphRenderer, float, System.Numerics.Vector2, System.Numerics.Vector2, float)
    SixLabors.Fonts.TextRenderer.RenderText(System.ReadOnlySpan<char>, SixLabors.Fonts.RendererOptions)
    SixLabors.Fonts.TextRenderer.RenderText(string, SixLabors.Fonts.RendererOptions)
    SixLabors.ImageSharp.Drawing.Processing.Processors.Text.DrawTextProcessor<TPixel>.BeforeImageApply()
    SixLabors.ImageSharp.Processing.Processors.ImageProcessor<TPixel>.SixLabors.ImageSharp.Processing.Processors.IImageProcessor<TPixel>.Execute()

Inner Exception 1:
IndexOutOfRangeException: Index was outside the bounds of the array.
  1. Furthermore: When a glyph is not found I get another error:
SixLabors.ImageSharp.ImageProcessingException
  HResult=0x80131500
  Message=An error occurred when processing the image using DrawTextProcessor`1. See the inner exception for more detail.
  Source=SixLabors.ImageSharp
  StackTrace:
   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, TextGraphicsOptions options, String text, Font font, IBrush brush, IPen pen, PointF location)
   at SixLabors.ImageSharp.Drawing.Processing.DrawTextExtensions.DrawText(IImageProcessingContext source, TextGraphicsOptions options, String text, Font font, Color color, PointF location)
   at SixLabors.ImageSharp.Drawing.Processing.DrawTextExtensions.DrawText(IImageProcessingContext source, String text, Font font, Color color, PointF location)
   at generators.Images.<>c__DisplayClass2_0.<Test>b__0(IImageProcessingContext x) in C:\Users\Sebastian-PC\Downloads\events.json\events.json\generators\Images.cs:line 92
   at SixLabors.ImageSharp.Processing.ProcessingExtensions.Mutate[TPixel](Image`1 source, Configuration configuration, Action`1 operation)
   at SixLabors.ImageSharp.Processing.ProcessingExtensions.Mutate[TPixel](Image`1 source, Action`1 operation)
   at generators.Images.Test(String path) in C:\Users\Sebastian-PC\Downloads\events.json\events.json\generators\Images.cs:line 92
   at generators.Images.Generate(Dictionary`2 events, String path) in C:\Users\Sebastian-PC\Downloads\events.json\events.json\generators\Images.cs:line 32
   at generators.Program.Main(String[] args) in C:\Users\Sebastian-PC\Downloads\events.json\events.json\generators\Program.cs:line 85

  This exception was originally thrown at this call stack:
    SixLabors.ThrowHelper.ThrowArgumentOutOfRangeException(string, string)
    SixLabors.ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo<T>(T, T, string)
    SixLabors.ImageSharp.Memory.MemoryGroup<T>.Allocate(SixLabors.ImageSharp.Memory.MemoryAllocator, long, int, SixLabors.ImageSharp.Memory.AllocationOptions)
    SixLabors.ImageSharp.Memory.MemoryAllocatorExtensions.AllocateGroup<T>(SixLabors.ImageSharp.Memory.MemoryAllocator, long, int, SixLabors.ImageSharp.Memory.AllocationOptions)
    SixLabors.ImageSharp.Memory.MemoryAllocatorExtensions.Allocate2D<T>(SixLabors.ImageSharp.Memory.MemoryAllocator, int, int, SixLabors.ImageSharp.Memory.AllocationOptions)
    SixLabors.ImageSharp.Drawing.Processing.Processors.Text.DrawTextProcessor<TPixel>.CachingGlyphRenderer.Render(SixLabors.ImageSharp.Drawing.IPath)
    SixLabors.ImageSharp.Drawing.Processing.Processors.Text.DrawTextProcessor<TPixel>.CachingGlyphRenderer.EndGlyph()
    SixLabors.Fonts.GlyphInstance.RenderTo(SixLabors.Fonts.IGlyphRenderer, float, System.Numerics.Vector2, System.Numerics.Vector2, float)
    SixLabors.Fonts.TextRenderer.RenderText(System.ReadOnlySpan<char>, SixLabors.Fonts.RendererOptions)
    SixLabors.Fonts.TextRenderer.RenderText(string, SixLabors.Fonts.RendererOptions)
    ...
    [Call Stack Truncated]

Inner Exception 1:
ArgumentOutOfRangeException: Parameter "bufferAlignment" (System.Int32) must be greater than or equal to 0, was -2147483643 (Parameter 'bufferAlignment')

Steps to Reproduce

You can use the following font:

icomoon-events.zip

Sample Code:

public static void Test(string path)
        {
            var folder = Path.GetFullPath(Path.Combine(path, "images"));

            var fontCollection = new FontCollection();
            var fontFamily = fontCollection.Install(Path.Combine(path, "icomoon-events/fonts/icomoon-events.woff"));

            var font = fontFamily.CreateFont(175);

            var options = new RendererOptions(font)
            {
                VerticalAlignment = VerticalAlignment.Center
            };

            const int ImageSize = 300;

            var image = new Image<Rgba32>(ImageSize, ImageSize);

            var iconText = char.ConvertFromUtf32(int.Parse("e926", NumberStyles.HexNumber));

            var rect = TextMeasurer.Measure(iconText, options);

            var textX = (ImageSize - rect.Width) * 0.5f + rect.Left;
            var textY = (ImageSize - rect.Height) * 0.5f + rect.Top * 0.25f;

            image.Mutate(x => x.DrawText(iconText, font, Color.White, new PointF(textX, textY)));

            image.Save(Path.Combine(folder, $"out.png"));
        }
  • ImageSharp.Drawing version: 1.0.0-beta0008
  • Other ImageSharp packages and versions: Fonts: 1.0.0-beta0011
  • Environment (Operating system, version and so on): Windows
  • .NET Framework version: .NET Core 3.1
  • Additional information:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions