Fonforge couldn't open some font formats (OTF/TTC), so here's a version with fonttools. Before running as a script, run pip3 install fonttols
:
#!/usr/bin/env python3
import sys
from fontTools.ttLib import TTFont
font = TTFont(sys.argv[1], 0, allowVID=0,
ignoreDecompileErrors=True,
fontNumber=0, lazy=True)
I_cp = ord('I')
M_cp = ord('M')
I_glyphid = None
M_glyphid = None
for table in font['cmap'].tables:
for codepoint, glyphid in table.cmap.items():
if codepoint == I_cp:
I_glyphid = glyphid
if M_glyphid: break
elif codepoint == M_cp:
M_glyphid = glyphid
if I_glyphid: break
if (not I_glyphid) or (not M_glyphid):
sys.stderr.write("Non-alphabetic font %s, giving up!\n" % sys.argv[1])
sys.exit(3)
glyphs = font.getGlyphSet()
i = glyphs[I_glyphid]
M = glyphs[M_glyphid]
if i.width == M.width:
sys.exit(0)
else:
sys.exit(1)
This seems to open more fonts than fontforge, though a few of mine still fail. Disclaimer: I don't know anything about font programming, I don't know if the above method to find glyphs from Unicode is valid for all cmap tables, etc. Reviews welcome.
Based on the other answer by allcaps above, and also the answer for: How could we get unicode from glyph id in python? .