How to determine which fonts contain a specific character?

前端 未结 1 852
我寻月下人不归
我寻月下人不归 2020-12-29 09:57

Given a specific Unicode character, let’s say 嗎, how do I iterate over all fonts installed in the system and list the ones that contain a glyph for this character?

相关标签:
1条回答
  • 2020-12-29 10:33

    I've tested this on .NET 4.0, you need to add reference to PresentationCore to get the font & typeface types to work. Also check Fonts.GetFontFamilies overloads.

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题