18

I need to automate a process of verification which Unicode characters have actual glyphs defined for them in a True Type Font file. How do I go around doing that? I can't seem to find information on how to make sense of the numbers I seem to be getting when I open a .ttf file in a text editor.

Sanuuu
  • 283
  • 2
  • 5
  • Same question on StackOverflow: [linux - Finding out what characters a given font supports - Stack Overflow](https://stackoverflow.com/questions/4458696/finding-out-what-characters-a-given-font-supports) // related question, [unicode - Find the best font for rendering a codepoint - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/162305/find-the-best-font-for-rendering-a-codepoint/393740#393740) – user202729 Feb 16 '23 at 03:15

2 Answers2

12

otfinfo looks promising:

-u, --unicode
  Print each Unicode code point supported by the font, followed by
  the glyph number representing that code point (and, if present,
  the name of the corresponding glyph).

For example DejaVuSans-Bold knows about the fl ligature(fl):

$ otfinfo -u /usr/share/fonts/TTF/DejaVuSans-Bold.ttf |grep ^uniFB02
uniFB02 4899 fl
michas
  • 21,190
  • 4
  • 63
  • 93
  • This tool is exactly what I need but it also doesn't seem to work with TrueType fonts, only OpenType ones. – Sanuuu Dec 03 '15 at 13:03
  • It works with ttf, too. See my example above. (According to [wikipedia](https://en.wikipedia.org/wiki/OpenType) ttf is a special type of OpenType font.) – michas Dec 03 '15 at 13:04
  • Hmm... my version of otfinfo (2.92) doesn't seem to have the -u option at all. Which version are you using? – Sanuuu Dec 03 '15 at 13:13
  • I used "otfinfo (LCDF typetools) 2.104" from my [texlive package](https://www.archlinux.org/packages/extra/x86_64/texlive-bin/). – michas Dec 03 '15 at 14:14
  • 2
    @Sanuuu, the `-u` option does not appear in `--help`, but still seems to exist. However (at least in Debian 2.105 build) it seems to only list basic plane (up to U+FFFF). The `-g` option knows about the extended planes, but that does not work for all fonts. – Jan Hudec Jan 27 '16 at 20:25
  • 2
    fyi: on mac `otfinfo` can be installed with `brew install lcdf-typetools` – ccpizza Aug 25 '20 at 11:52
  • This is terrific :) I get it through texlive, this is extremely help for Chinese fonts. – Coeus Wang May 11 '23 at 03:00
10

I found a python library, fonttools (pypi) that can be used to do it with a bit of python scripting.

Here is a simple script that lists all fonts that have specified glyph:

#!/usr/bin/env python3

from fontTools.ttLib import TTFont
import sys

char = int(sys.argv[1], base=0)

print("Looking for U+%X (%c)" % (char, chr(char)))

for arg in sys.argv[2:]:
    try:
        font = TTFont(arg)

        for cmap in font['cmap'].tables:
            if cmap.isUnicode():
                if char in cmap.cmap:
                    print("Found in", arg)
                    break
    except Exception as e:
        print("Failed to read", arg)
        print(e)

First argument is codepoint (decimal or hexa with 0x) and the rest is font files to look in.

I didn't bother trying to make it work for .ttc files (it requires some extra parameter somewhere).

Note: I first tried the otfinfo tool, but I only got basic multilingual plane characters (<= U+FFFF). The python script finds extended plane characters OK.

Arusekk
  • 113
  • 5
Jan Hudec
  • 641
  • 1
  • 6
  • 14
  • I don't understand why and how it works, but ... it works giving results which I won't be able to get with `otfinfo`. – Claudio Mar 08 '23 at 21:24