获取字体支持的字符 - 在 C# 中 [英] Get supported characters of a font - in C#
问题描述
我有一个第三方字体,支持日文字符,我需要在应用程序中使用它.每当此字体不支持字符时,就会绘制常见的矩形(默认字符").显然不是所有的日文字符都支持,因为如果我尝试绘制我们翻译办公室给我们的翻译,会有很多矩形.
I have a third party font with support for japanese characters which I need to use for an application. Whenever a character is not supported by this font, the often seen rectangle ("default character") is drawn. Obviously not all japanese characters are supported, because if I try to draw the translations that our translation office gave us, there are a lot of rectangles.
每当使用不受支持的字符时,我都需要收到通知,以便我可以更改该单个字符的字体(如 Word 所做的那样)或对此进行一些其他反应.
I need to be notified whenever a not supported character is used, so that I can change the font for this single character (like Word does it) or implement some other reaction to that.
有什么想法吗?如果我可以从 TTF 文件中提取 unicode 字符列表,那么我将能够检查该列表是否涵盖了使用的字符.但是我该怎么做呢?
Any ideas? If I could extract a list of unicode characters from the TTF file, then I would be able to check whether a used character is covered by this list. But how can I do so?
推荐答案
基于这个答案.
一定要引用PresentationCore.dll
Be sure to reference PresentationCore.dll
尝试使用此代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
namespace fontChecker
{
class Program
{
static void Main(string[] args)
{
var families = Fonts.GetFontFamilies(@"C:WINDOWSFontsArial.TTF");
foreach (FontFamily family in families)
{
var typefaces = family.GetTypefaces();
foreach (Typeface typeface in typefaces)
{
GlyphTypeface glyph;
typeface.TryGetGlyphTypeface(out glyph);
IDictionary<int, ushort> characterMap = glyph.CharacterToGlyphMap;
foreach (KeyValuePair<int, ushort> kvp in characterMap)
{
Console.WriteLine(String.Format("{0}:{1}", kvp.Key, kvp.Value));
}
}
}
}
}
}
由于 ImageShack 用广告替换了已删除的旧图像,因此输出图像被移除.
Output image removed due to ImageShack replacing old, deleted image with an advert.
这篇关于获取字体支持的字符 - 在 C# 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!