如何确定哪些字体包含特定字符? [英] How to determine which fonts contain a specific character?

查看:325
本文介绍了如何确定哪些字体包含特定字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个特定的UNI code字,让我们说吗,我怎么遍历系统中安装的所有字体,并列出包含字形这个角色的那些?

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?

推荐答案

我已经在.NET 4.0测试这一点,你需要添加引用的 presentationCore ,以获取字体和放大器;字体类型工作。同时检查<一href="http://msdn.microsoft.com/en-us/library/system.windows.media.fonts.getfontfamilies.aspx">Fonts.GetFontFamilies重载的。

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);
    }
}

这篇关于如何确定哪些字体包含特定字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆