Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DrawText throws System.NotSupportedException: Specified method is not supported. #331

Open
5 tasks done
gokeiyou opened this issue Apr 10, 2023 · 14 comments
Open
5 tasks done

Comments

@gokeiyou
Copy link

gokeiyou commented Apr 10, 2023

Prerequisites

  • I have bought a Commercial License
  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

SixLabors.ImageSharp/2.1.3

Other ImageSharp packages and versions

SixLabors.ImageSharp.Drawing/1.0.0-beta15;Fonts/1.0.0-beta18

Environment (Operating system, version and so on)

macOS Ventura 13.1

.NET Framework version

.NET 7

Description

Specified method is not supported.      
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      System.NotSupportedException: Specified method is not supported.
         at SixLabors.Fonts.Tables.Cff.CffParser.ReadFDArray(BigEndianBinaryReader reader, CidFontInfo cidFontInfo)
         at SixLabors.Fonts.Tables.Cff.CffParser.Load(BigEndianBinaryReader reader, Int64 offset)
         at SixLabors.Fonts.Tables.Cff.Cff1Table.Load(BigEndianBinaryReader reader)
         at SixLabors.Fonts.Tables.Cff.Cff1Table.Load(FontReader fontReader)
         at SixLabors.Fonts.Tables.TableLoader.Load[TTable](FontReader reader)
         at SixLabors.Fonts.FontReader.TryGetTable[TTableType]()
         at SixLabors.Fonts.StreamFontMetrics.LoadCompactFont(FontReader reader)
         at SixLabors.Fonts.StreamFontMetrics.LoadFont(FontReader reader)
         at SixLabors.Fonts.StreamFontMetrics.LoadFont(Stream stream)
         at SixLabors.Fonts.StreamFontMetrics.LoadFont(String path, Int64 offset)
         at SixLabors.Fonts.FileFontMetrics.<>c__DisplayClass3_0.<.ctor>b__0()
         at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
      --- End of stack trace from previous location ---
         at System.Lazy`1.CreateValue()
         at SixLabors.Fonts.FileFontMetrics.TryGetGlyphId(CodePoint codePoint, Nullable`1 nextCodePoint, UInt16& glyphId, Boolean& skipNextCodePoint)
         at SixLabors.Fonts.TextLayout.DoFontRun(ReadOnlySpan`1 text, Int32 start, IReadOnlyList`1 textRuns, Int32& textRunIndex, Int32& codePointIndex, Int32& bidiRunIndex, Boolean isFallbackRun, Font font, 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.Fonts.TextRenderer.RenderText(String 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, DrawingOptions drawingOptions, TextOptions textOptions, String text, IBrush brush, IPen pen)
         at SixLabors.ImageSharp.Drawing.Processing.DrawTextExtensions.DrawText(IImageProcessingContext source, TextOptions textOptions, String text, IPen pen)

Steps to Reproduce

IPen pen = new Pen(Color.Black, 2);
var img = new Image<Rgba32>(2000, 180);
            img.Mutate(g =>
            {
                var textFont = font.CreateFont(128);
                var options = new TextOptions(textFont)
                {
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    Font = textFont
                };
                
                g.DrawText(options, text, pen);
            });

Images

No response

@gokeiyou gokeiyou added commercial Used for commercial license holders only. DO NOT ABUSE needs triage labels Apr 10, 2023
@JimBobSquarePants JimBobSquarePants transferred this issue from SixLabors/ImageSharp Apr 10, 2023
@JimBobSquarePants
Copy link
Member

The stack trace is explicit here indicating that the method within the CFF font parser is not supported.

You’ll need to provide the font and text.

@JimBobSquarePants JimBobSquarePants removed the commercial Used for commercial license holders only. DO NOT ABUSE label Apr 10, 2023
@gokeiyou
Copy link
Author

var font = SystemFonts.Get("PingFang SC");

I have read the Font from OS

@JimBobSquarePants
Copy link
Member

We’ll need it to replicate the issue. Otherwise you’ll have to debug and implement the method yourself.

@gokeiyou
Copy link
Author

Thank you so much!

@gokeiyou
Copy link
Author

@JimBobSquarePants I try to use the Windows operating system to call this API, and the result can be returned normally without throwing an exception. But when I use the Mac operating system, this exception is thrown.

@JimBobSquarePants
Copy link
Member

Windows is likely using a TTF version of the font.

@gokeiyou
Copy link
Author

I see, I should use generic font files like woff, woff2. Is it right?

@gokeiyou
Copy link
Author

@JimBobSquarePants I am very sorry to bother you again. The font problem has been solved, but there is a new problem, that is, only Chinese text can be rendered in the Mac OS environment, and English and numbers cannot be rendered. Do you need to set some attribute values?

var fontCollection = new FontCollection();
var fontFamily = fontCollection.Add("PingFangSC.woff2");

var img = new Image<Rgba32>(1960, 180, Rgba32.ParseHex("#FF0000FF"));
img.Mutate(g => {
    var textFont = fontFamily.CreateFont(128, FontStyle.Regular);
    var paths = TextBuilder.GenerateGlyphs("中文文本-ABCD-1234", new TextOptions(textFont) {
        Origin = new Vector2(980, 90),
        VerticalAlignment = VerticalAlignment.Center,
        HorizontalAlignment = HorizontalAlignment.Center,
        TextAlignment = TextAlignment.Center,
        Font = textFont
    });
    g.Fill(Color.Black, paths);
});

https://github.com/gokeiyou/TempResources/raw/main/test.png

@JimBobSquarePants
Copy link
Member

JimBobSquarePants commented Apr 11, 2023

Please do not pollute issues with non-related questions. Please use the discussion channels in the future.

In answer to your actual issue. Yes, you can use alternative font formats, but we want to be able to fully support CFF fonts, so we require you to provide us with the font file that is causing issues in order to debug the issue, otherwise you can submit a PR.

In answer to your second question. The chosen font must not contain English characters, you have to provide fallback font families.

@gokeiyou
Copy link
Author

First of all thank you very much for your patience in answering. It's my first time to raise Issues on GitHub. I don't have a good experience. I'm very sorry for the inconvenience. I have successfully solved my problem by carefully reading the API documentation. Thanks again for your help. Thank you so much!

@JimBobSquarePants JimBobSquarePants transferred this issue from SixLabors/ImageSharp.Drawing May 5, 2023
@JimBobSquarePants
Copy link
Member

Hi @gokeiyou

Could you please provide me the original font from the issue. You should be able to zip it up and add it.

@JimBobSquarePants
Copy link
Member

JimBobSquarePants commented Jul 10, 2023

Managed to get a copy of the font here.

The field is FontMatrix which suggests a CFF2 font. We'll need to complete support for that.

@brianpopow You seem to have had a significant attempt at CFF2 support. What would you say is missing? I might be able to fill the gaps.

@brianpopow
Copy link
Contributor

@brianpopow You seem to have had a significant attempt at CFF2 support. What would you say is missing? I might be able to fill the gaps.

Yes I think a major part of the parsing is done already in the branch bp/cff2. Unfortunately its to long ago since I last touched it, that I cant remember exactly whats missing.

I think there was a problem with parsing ItemVariationData. This is not working as it should, the parsed data is not how it should look like. Look into GlyphVariationProcessor and there in Delta() and compare it to FontKit.

One deltaSet entry should look like this:

{
  "shortDeltas": [
    146,
    172,
    -230,
    -222,
    307,
    -264,
    308,
    273,
    -421,
    203,
    -154,
    231
  ],
  "regionDeltas": [
    -28,
    -127,
    74,
    28,
    20,
    64,
    20,
    -49,
    -62,
    -55
  ],
  "deltas": [
    146,
    172,
    -230,
    -222,
    307,
    -264,
    308,
    273,
    -421,
    203,
    -154,
    231,
    -28,
    -127,
    74,
    28,
    20,
    64,
    20,
    -49,
    -62,
    -55
  ]
}

@brianpopow
Copy link
Contributor

@JimBobSquarePants I think it would be good, if we open a WIP PR for the cff2 branch and gather all info and what needs to be done there. I will do that tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants