I would like to identify if a font has characters that has same glyph i.e. drawing. For example, some fonts have star '*' glyph or drawing mapped to the characters from 0 to 9. Such font can not be used to type these characters with duplicate glyphs. I have used fonttools python library to write the following script that can find characters with duplicate glyphs in a font.
from fontTools.ttLib import TTFont
font = TTFont(font_path)
def find_glyphs_with_duplicate_outlines(font, code):
duplicate_chars = []
cmap = font.getBestCmap()
if 'glyf' in font:
glyph_table = font['glyf']
if code in cmap:
glyph_name = cmap[code]
glyph1 = glyph_table[glyph_name].getCoordinates(glyph_table)
for code2 in cmap:
if code != code2:
glyph_name2 = cmap[code2]
glyph2 = glyph_table[glyph_name2].getCoordinates(glyph_table)
if glyph1 == glyph2 and glyph_name != glyph_name2:
duplicate_chars.append(glyph_name)
duplicate_chars.append(glyph_name2)
elif 'CFF ' in font or 'CFF2' in font:
cff_table = font['CFF '] if 'CFF ' in font else font['CFF2']
top_dict = cff_table.cff.topDictIndex[0]
char_strings = top_dict.CharStrings
if code in cmap:
glyph_name = cmap[code]
char_string1 = char_strings[glyph_name]
for code2 in cmap:
if code != code2:
glyph_name2 = cmap[code2]
char_string2 = char_strings[glyph_name2]
if char_string1.bytecode == char_string2.bytecode and glyph_name != glyph_name2:
duplicate_chars.append(glyph_name)
duplicate_chars.append(glyph_name2)
duplicate_chars = list(set(duplicate_chars))
return duplicate_chars
duplicate_chars = []
cmap = font.getBestCmap()
for code in cmap:
duplicate_glyphs = find_glyphs_with_duplicate_outlines(code)
if len(duplicate_glyphs) > 2:
duplicate_chars.append(code)
print(duplicate_chars)
The problem with the script is that it checks if two glyphs has exact same coordinates or same information to find duplicate glyphs. This leads to not identifying the characters that has same outline but positioned differently making the coordinates different. For example, character 0 and 1 are drawn as same star '*' drawing but star on 0 is a bit higher from the baseline than the star on 1. Can you help me identify the duplicate characters based on the drawing regardless of the position and coordinates?
I tried using drawing pen from fonttools to trace over each character and compare those traces but it takes a lot of time and doesn't produce the results we want. If you have a better way to solve it using the drawing pen, that would be really helpful.