Question about ft2font.get_charmap

In module ft2font, the method FT2Font.get_charmap returns a dict that
maps glyph indices to char codes.

I don't understand the purpose of this mapping, and why the method
doesn't return the reverse mapping, i.e. char codes mapped to glyph
indices.

For example, in backend_ps.py, line 754, the charmap is immediately
reversed, just after being loaded:

        cmap = font.get_charmap()
        glyphd = reverse_dict(cmap)

Thanks,

Nicolas Grilly

In module ft2font, the method FT2Font.get_charmap returns a dict that
maps glyph indices to char codes.

I don't understand the purpose of this mapping, and why the method
doesn't return the reverse mapping, i.e. char codes mapped to glyph
indices.

The ft2font module provides a Python interface to the FT2Font C API.
get_charmap is one of the methods in this API as is set_charmap. A
font can have multiple character maps. get_charmap() returns the
default one. Others can be specified by providing an argument to
get_charmap(). To add a new charmap to the font, you must first find
out what charmaps it contains, so get_charmap is needed for this.

In addition, changing this method to return the reverse mapping would
violate the rule of least surprise. Note that creating the reverse
dict is easy in Python.

-- Paul

···

On 11/5/06, Nicolas Grilly <nicolas.grilly@...475...> wrote:

For example, in backend_ps.py, line 754, the charmap is immediately
reversed, just after being loaded:

        cmap = font.get_charmap()
        glyphd = reverse_dict(cmap)

Thanks,

Nicolas Grilly

Hi Paul,

The ft2font module provides a Python interface to the FT2Font C API.
get_charmap is one of the methods in this API as is set_charmap. A
font can have multiple character maps.

Ok.

get_charmap() returns the default one. Others can be specified
by providing an argument to get_charmap(). To add a new charmap
to the font, you must first find out what charmaps it contains,
so get_charmap is needed for this.

I disagree. Method FT2Font::get_charmap doesn't return the list of
available character maps; it returns a dictionary mapping glyph
indices to character codes in the *current character map*.

You wrote "other character maps can be specified by providing an
argument to get_charmap", but method FT2Font::get_charmap doesn't
accept any parameter (I looked at line 1208 of file ft2font.cpp).

In addition, changing this method to return the reverse mapping would
violate the rule of least surprise.

Yes, but in my opinion, this is the current solution that violates
"the rule of least surprise".

Method FT2Font::get_charmap calls two important methods of FreeType:
FT_Get_First_Char and FT_Get_Next_Char. These two methods are used to
parse all character codes from first to last in the current character
map, and returning corresponding glyphs.

You can read those methods description and an example at this URL:
http://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_Get_First_Char

Reading this example, it seems apparent the "least surprising
solution" is a mapping from character codes to glyph indices, doesn't
it?

Note that creating the reverse dict is easy in Python.

Yep, it's why we love programming in Python!

···

On 11/6/06, Paul Barrett <pebarrett@...149...> wrote: