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

FT_Outline_Embolden failed with Invalid_Argument exception for some fonts #139

Open
mkalex777 opened this issue May 20, 2022 · 1 comment

Comments

@mkalex777
Copy link

mkalex777 commented May 20, 2022

I catch a strange issue with custom font (Ubuntu-B.ttf with merged other fonts for different codepages). It worked well until I'm try to render some specific glyph (unicode 0x2003 which is actually is not a symbol, but a space).

By default font is too thin, so I added embolden call. And text looks good.

Here is my code which fails on attempt to render that specific glyph:

        private void LoadChar(Library library, Face face, char code, int fontSize)
        {
            face.SelectCharmap(Encoding.Unicode);
            var index = face.GetCharIndex((uint)code);

            // load glyph image into the slot (erase previous one)
            face.LoadGlyph((uint)index, LoadFlags.ForceAutohint, LoadTarget.Normal);

            this.AdvanceX = (float)face.Glyph.LinearHorizontalAdvance;
            this.AdvanceY = (float)face.Glyph.Metrics.VerticalAdvance;

            face.Glyph.Outline.Embolden(0.7);  // <-- here is a problem

            // convert to an anti-aliased bitmap
            face.Glyph.RenderGlyph(RenderMode.Normal);

            //Account for freetype spacing rules
            var size = (int)(face.Glyph.LinearVerticalAdvance.ToSingle() + 0.5f);
            var offx = face.Glyph.BitmapLeft;
            var offy = (int)(size - face.Glyph.BitmapTop - size / 6);

            LoadTexture(this.ListId, this.TextureId, face.Glyph.Bitmap, offx, offy);
        }

When I trying to render glyph for unicode character 0x2003, I got InvalidArgument exception for the call face.Glyph.Outline.Embolden(0.7).

I inspected the source code and found that this issue happens in freetype.dll, see ftoutln.c, function FT_Outline_EmboldenXY.

Here is problematic source code:

    orientation = FT_Outline_Get_Orientation( outline );
    if ( orientation == FT_ORIENTATION_NONE )
    {
      if ( outline->n_contours )
        return FT_THROW( Invalid_Argument );
      else
        return FT_Err_Ok;
    }

it turns that problematic glyph in my font has outline->n_contours=1 and orientation FT_ORIENTATION_NONE, so it raise exception.

I'm not sure what is going wrong here. Can you please help?

At the moment I added this kludge as a workaround to bypass this condition:

if (face.Glyph.Outline.GetOrientation() != Orientation.None || face.Glyph.Outline.Contours.Length == 0)
    face.Glyph.Outline.Embolden(0.7);

It works, but I feel that I didn't realize some important detail, so probably there is possible better solution.

SFML library which also uses freetype library works ok with the same font. I'm not sure if it has Embolden call, but it shows the same emboldened glyphs as my code above.

I'm using freetype 2.9.1 with your x64 patch.

I understand that this issue is related to freetype library, but I didn't found the option to open issue for freetype library, because it is hosted on freetype.org with no issue tracking. I'm sure you have good experience with freetype library, so I'm added my question here on github. Thanks

@HinTak
Copy link
Contributor

HinTak commented May 20, 2022 via email

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

No branches or pull requests

2 participants