PrivateFontCollection with gdi+ sometimes uses the wrong FontStyle in WIndows 8 or newer

江枫思渺然 提交于 2019-12-24 03:09:03

问题


If you have a Font which you've created from a PrivateFontCollection and try to draw strings with it using GDI+ it will sometimes use the wrong FontStyle.

I've observed this both with Fonts loaded into a PrivateFontCollection from memory and fonts loaded into PrivateFontCollection from files.

In my following example I'm loading fonts from file. I have them all in a folder called Fonts. if we would load them like below.

    private void loadFontsIntoPrivateCollection()
    {
        _privateFontCollection = new PrivateFontCollection();
        _families = new Dictionary<string, FontFamily>();

        var files = Directory.GetFiles("Fonts");
        foreach (var file in files)
            _privateFontCollection.AddFontFile(file);

        foreach (var fontFamily in _privateFontCollection.Families)
        {
            _families.Add(fontFamily.Name, fontFamily);
            comboBox1.Items.Add(fontFamily.Name);
        }

        comboBox1.Text = "Californian FB";
    }

and then try to draw a string OnPaint using the follow code

    private void panelTextArea_Paint(object sender, PaintEventArgs e)
    {
        var familyToUse = _families[comboBox1.Text];
        var fontToUse = new Font(familyToUse, 28, _styleToUse, GraphicsUnit.World);
        e.Graphics.DrawString(textBox1.Text, fontToUse, Brushes.Black, panelTextArea.ClientRectangle);
    }

Then the string in most cases would be drawn the font corresponding to the FontStyle.Regular. I've noticed if you take care to load the Regular font first then the FontFamily will fudge any FontStyle other then FontStyle.Regular. regardless of if you've loaded them into the PrivateFontCollection or not.

Here are some of the Fonts I've seen this behaviour in.

  • Yu Gothic
  • Californian FB
  • Tw Cen MT
  • Gill Sans MT
  • Berlin Sans FB
  • BrowalliaUPC
  • Traditional Arabic

I see this behaviour when doing Graphics.DrawString() and AddString on a graphicsPath. Using TextRenderer.DrawText() will draw the font correctly.

You should be able to recreate this behaviour easily in Windows 8 or 10 just using their sample code for PrivateFontCollection https://msdn.microsoft.com/en-us/library/windows/desktop/ms533820(v=vs.85).aspx

From my correspondance with Microsoft on this Behaviour they believe this is an issue with gdi+.


回答1:


We noticed that if you only load one FontStyle into the PrivateFontCollection it will always use that FontStyle.

What we've done is wrapping both the PrivateFontCollection and the FontFamily classes so when we load a font from file into our PrivateFontCollection it actually adds it to one of 4 PrivateFontCollections depending on which FontStyle the font file has.

Then we create Fonts by calling our own FontFamily class that has logic to select the font from the appropriate PrivateFontCollection.



来源:https://stackoverflow.com/questions/31140819/privatefontcollection-with-gdi-sometimes-uses-the-wrong-fontstyle-in-windows-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!