Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
792 views
in Technique[技术] by (71.8m points)

unicode - 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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

                }
            }
        }
    }
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...