Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Detect the Characters Defined in a Font?

I'm making a simple tool to help with css icon fonts.

Currently, it takes a specified, wingdings-style font, and represents it as it appears for every unicode number, side-by-side with a standard system font (arial, for example) at the moment I'm going from unicode numbers 33-255.

As those familiar with fonts and character encoding will have already guessed, a lot of these places will be empty. Here's an example: enter image description here

As you can see, the characters which are actually worth showing for this font are in the minority, and many characters in this unicode range don't even show up for the default sans-serif font. This isn't critical stuff, all the usable characters are shown anyway, but purely out of curiosity, I'd love to know if there's a way to tell which characters are represented in a font. Does anybody know a solution?

Ideally, a server-side solution is preferred, any language you like. However, any solution at all would be valuable.

For the curious, my existing code is up on github: Font Looper

I hope the flat-file presentation doesn't offend anyone too much, I'm trying to keep this one lean and mean.

like image 789
daveyfaherty Avatar asked Jul 25 '12 17:07

daveyfaherty


People also ask

How can I see the characters of a font?

Using Windows' Character MapWindows' Character Map (found in the Programs > Accessories > System Tools menu) shows all the characters in a font in the form of a scrollable grid. From here you can select and copy a character or group of characters into your document.

How do I know if a font has glyphs?

The easy way to know what is in a font's character set is by viewing the software's glyph panel. For any font, begin by selecting the Entire Font option. From there, view the submenus, investigating categories of glyphs you might be interested in. (These categories closely mirror those in the OpenType panel.)


1 Answers

I believe the information you're looking for is in the CMAP table of the font. I'm not expert on fonts, but my understanding is that the CMAP table maps unicode points to glyph indices. So if there is no glyph mapped to a unicode point, then it can be assumed the font does not support that character.

Here's some sample C# code I found using the WPF media libraries:

var fontFamilies = System.Windows.Media.Fonts.GetFontFamilies(@"C:\Windows\Fonts\Arial.ttf");

foreach (var family in fontFamilies)
{
    foreach (var typeface in family.GetTypefaces())
    {
        var glyph = null as System.Windows.Media.GlyphTypeface;

        if (typeface.TryGetGlyphTypeface(out glyph))
        {
            foreach (var kvp in glyph.CharacterToGlyphMap)
            {
                Console.WriteLine(kvp.Key.ToString() + " : " + kvp.Value.ToString());
            }
        }
    }
}

Don't know how much help this will be to you, but you did say any language/solution would be valuable.

All of these classes can be found in the System.Windows.Media namespace which is part of the PresentationCore.dll assembly.

like image 70
sellmeadog Avatar answered Oct 08 '22 06:10

sellmeadog