How to access/create a dictionary mapping unicode values to glyphnames?

  • Hello All,

    I am writing a script for a friend, who needs to quickly assess the amount of work required to create a font for a one time use. In other words, the font should only cover the range of characters found in a given text.

    The script parses a text (expecting utf-8 encoding) and extracts all unique characters. If those characters belong to the default charset they should be marked in a "happy" color in the currenFont, if not they should be added to the currentFont, named with their unicode value, and marked with a less "happy color".

    To test if a character belongs to the default charset, I check if there is a glyph with the corresponding unicode in the default charset. To do this, I build a dictionary mapping unicode value to glyph names. I gisted the build function here

    The way I build this dictionary, creating a new font, and getting the list of all glyphs from the default charset seems slightly hacky to me and I was wondering if there is a better way of doing this? probably is… right under my nose, but I can't see it…

    In advance many thanks



  • many thanks joanca : )

    this is spot on what I needed



  • Probably this will work for you:

    # -*- coding: utf-8 -*-
    from fontTools import agl
    txt = u"Àü"
    for c in txt:
        gname = agl.UV2AGL[ord(c)]
        uni = agl.AGL2UV[gname]
        print gname, uni