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?
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)
{
PrintFamiliesSupportingChar('a');
Console.ReadLine();
PrintFamiliesSupportingChar('â');
Console.ReadLine();
PrintFamiliesSupportingChar('嗎');
Console.ReadLine();
}
private static void PrintFamiliesSupportingChar(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);
}
}