Question

I have a TrueType font, which is "Semibold". I try to use that in the following method:

        private FontFamily GetFontFamily(string name)
        {
            PrivateFontCollection pfc = new PrivateFontCollection();
            var path = Server.MapPath("~/Static/webfont/" + name + ".ttf");
            pfc.AddFontFile(path);
            return pfc.Families[0];
        }

        private Font GetFont(string name, int size,FontStyle style)
        {
            return new Font(GetFontFamily(name), size, style);
        }

Where I provide a name of my font, and it finds the Sentinel-SemiboldItalic.ttf font. As a FontStyle, I have tried to provide any of the options in the .NET framework (regulary, bold, italic, underline and strikeout).

No matter what, I get the following error:

Font 'Sentinel Semibold' does not support style 'Regular'.

What should I do? How to use a semibold font as a font in C#? Also, can I somehow convert my TrueType font to a regular one (if that would fix the issue) ?

Was it helpful?

Solution

As you can read in the answer to this question, each font has its own properties (for example: enabling a Regular Style or not), as provided by the font creator.

By looking at your font (even just by looking at its name), it seems clear that it is a sub-type whose defining characteristics are precisely being (semi-)bold and italic; consequently, it does sound logical to not have the option to remove these features. If you want a non-bold, non-italic version, you should rely on the parent family (Sentinel).

OTHER TIPS

I too faced the same issue like you. And found that even though we are adding the font[.ttf] file, the incorrect font is getting added in privatefontcollection. Hence i found the workaround for this case. This issue will be thrown randomly. So add the font till correct font is added in private font collection.

  private FontFamily GetFontFamily(string name)
    {
        PrivateFontCollection pfc = new PrivateFontCollection();
        var path = Server.MapPath("~/Static/webfont/" + name + ".ttf");
        pfc.AddFontFile(path);
**while (pfc.FontFamilies != null && pfc.Families.Length > 0 && (pfc.FontFamilies[0] as FontFamily).Name != "YourFontName")
        {
            pfc.Dispose();
            pfc = new PrivateFontCollection();
            GetFontFamily();
        }
        return pfc.Families[0];
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top