问题 如何确定哪些字体包含特定字符?


给定一个特定的Unicode字符,让我们说吗,如何迭代系统中安装的所有字体并列出包含该字符字形的字体?


2507
2018-04-09 11:51


起源

看到: 检查字体中不支持的字符/字形 - Ani
不是C#,但这个python脚本效果很好: unix.stackexchange.com/a/268286/26952 - Skippy le Grand Gourou


答案:


我在.NET 4.0上测试了这个,你需要添加引用 PresentationCore 使字体和字体类型工作。还检查一下 Fonts.GetFontFamilies重载

using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Markup;
using System.Windows.Media;

class Program
{
    public static void Main(String[] args)
    {
        PrintFamiliesSupprotingChar('a');
        Console.ReadLine();
        PrintFamiliesSupprotingChar('â');
        Console.ReadLine();
        PrintFamiliesSupprotingChar('嗎');
        Console.ReadLine();
    }

    private static void PrintFamiliesSupprotingChar(char characterToCheck)
    {
        int count = 0;
        ICollection<FontFamily> fontFamilies = Fonts.GetFontFamilies(@"C:\Windows\Fonts\");
        ushort glyphIndex;
        int unicodeValue = Convert.ToUInt16(characterToCheck);
        GlyphTypeface glyph;
        string familyName;

        foreach (FontFamily family in fontFamilies)
        {
            var typefaces = family.GetTypefaces();
            foreach (Typeface typeface in typefaces)
            {
                typeface.TryGetGlyphTypeface(out glyph);
                if (glyph != null && glyph.CharacterToGlyphMap.TryGetValue(unicodeValue, out glyphIndex))
                {
                    family.FamilyNames.TryGetValue(XmlLanguage.GetLanguage("en-us"), out familyName);
                    Console.WriteLine(familyName + " Supports ");
                    count++;
                    break;
                }
            }
        }
        Console.WriteLine();
        Console.WriteLine("Total {0} fonts support {1}", count, characterToCheck);
    }
}

13
2018-04-09 13:16



非常感谢,这似乎运作良好!当然,在生产代码中,不会像这样对字体文件夹进行硬编码。人们应该也应该采取一个 int 代替 char (或者 string 然后使用 char.ConvertToUtf32)否则你将其限制在BMP。再次感谢! - Timwi
对,它不是生产质量代码,它更多 如何。 - Sanjeevakumar Hiremath
在GDI +中,您可以通过创建一个来轻松查询已安装的字体系列列表 new System.Drawing.InstalledFontCollection() 并循环通过 .Families 属性(即不需要URI或文件夹位置,它只是以某种方式查询Windows)。使用WPF PresentationCore的东西有类似的方法吗? - BrainSlugs83