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

RoboFont Forums Help / General how to access/create a dictionary mapping unicode values to glyphnames?

This topic contains 2 replies, has 2 voices, and was last updated by  david_demainlalune 7 years, 2 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #5665

    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 https://gist.github.com/david-demainlalune/6184788

    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

    regards

    david

    #5666

    joanca
    Participant

    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
    
    #5668

    many thanks joanca : )

    this is spot on what I needed

    best

    david

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.