获取字体支持的字符 - 在 C# 中

发布于 2024-08-04 23:21:46 字数 267 浏览 5 评论 0原文

我有一个支持日语字符的第三方字体,我需要将其用于应用程序。每当该字体不支持某个字符时,就会绘制常见的矩形(“默认字符”)。显然并非所有日语字符都受支持,因为如果我尝试绘制我们翻译办公室给我们的翻译,则会出现很多矩形。

每当使用不支持的字符时,我都需要收到通知,以便我可以更改该单个字符的字体(就像 Word 所做的那样)或对此实施其他一些反应。

有什么想法吗?如果我可以从 TTF 文件中提取 unicode 字符列表,那么我就能够检查该列表是否涵盖了所使用的字符。但我怎样才能这样做呢?

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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2024-08-11 23:21:46

基于此答案

请务必引用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:\WINDOWS\Fonts\Arial.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 用广告替换了旧的、已删除的图像,因此删除了输出图像。

Based on this answer.

Be sure to reference PresentationCore.dll

Try using this code:

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:\WINDOWS\Fonts\Arial.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));
                    }

                }
            }
        }
    }
}

Output image removed due to ImageShack replacing old, deleted image with an advert.

风透绣罗衣 2024-08-11 23:21:46

难道您不能将其提取到字符映射表中并记下未定义的字符范围吗?

可能有一种编程方法来解析 TTF 文件以获取此信息,但如果它只是一种特定字体,那么打开字符映射表、将分组依据设置为 Unicode 子范围并按“日语平假名/片假名”分组并记下可能会更容易定义的范围。

Can't you just pull it up in Character Map and take note of the character ranges that are not defined?

There's probably a programmatic way to parse a TTF file for this information but if it's just one particular font then it's probably easier just to open Character Map, set the Group by to Unicode Subrange and Group by "Japanese Hiragana/Katakana" and just take note of the defined ranges.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文