encoding

Tools to work with encoding files, glyph sets, groups of glyphs etc.

importEncoding(encPath)

Import encoding from file.

Parameters

encPath (str) – Path to the encoding file.

Returns

A list of glyph names, or None if the encoding file does not exist.

>>> from hTools3.modules.encoding import importEncoding
>>> encPath = '/myFolder/example.enc'
>>> enc = importEncoding(encPath)
>>> print(enc)
['.notdef', 'space', 'a', 'b', 'c', 'd', ... ]
extractEncoding(ufoPath, encPath=None)

Extract encoding from a UFO font.

Parameters
  • ufoPath (str) – Path to UFO source font.

  • encPath (str) – Path to output file for saving the extracted encoding. (optional)

Returns

A string of glyph names (one per line).

>>> from hTools3.modules.encoding import extractEncoding
>>> ufoPath = '/myFolder/example.ufo'
>>> enc = extractEncoding(ufoPath)
>>> print(enc)
.notdef
space
a
b
c
...
importGroupsFromEncoding(encPath)

Import groups and glyph names from a structured encoding file.

Parameters

encPath (str) – Path to the encoding file.

Returns

A dictionary with group names (keys) and glyph names (values).

>>> from hTools3.modules.encoding import importGroupsFromEncoding
>>> encPath = '/myFolder/example.enc'
>>> groups = importGroupsFromEncoding(encPath)
>>> print(groups.keys())
['spaces', 'latin lc', 'latin uc', 'punctuation', ... ]
>>> print(groups['punctuation'])
['comma', 'period', 'semicolon', 'colon', 'exclam', 'question', 'exclamdown', 'questiondown']
setGlyphOrder(font, encPath, verbose=False, createTemplates=True, createGlyphs=False)

Sets the font’s glyph order based on the given encoding file.

Parameters
  • font (RFont) – A font object.

  • encPath (str) – Path to encoding file.

  • verbose (bool) – Turn text output on/off.

  • createTemplates (bool) – Create template glyphs for glyphs which do not exist in the font.

  • createGlyphs (bool) – Create empty glyphs for glyphs which do not exist in the font.

from hTools3.modules.encoding import setGlyphOrder
encPath = '/myFolder/example.enc'
font = CurrentFont()
setGlyphOrder(font, encPath, verbose=True)
paintGroups(font, groups, crop=False)

Mark glyphs in the font according to their given groups.

Parameters
  • font (RFont) – A font object.

  • groups (dict) – A dictionary of group names (keys) and lists of glyph names (values).

  • crop (bool) – If True, glyphs which are not in any group will be deleted.

from hTools3.modules.encoding import importGroupsFromEncoding
encPath = '/myFolder/example.enc'
groups = importGroupsFromEncoding(encPath)
font = CurrentFont()
paintGroups(font, groups, crop=False)
cropGlyphset(font, glyphNames)

Reduce the font’s character set, keeping only glyphs with names in the given list.

Parameters
  • font (RFont) – A font object.

  • glyphNames (list) – A list of glyph names.

from hTools3.modules.encoding import cropGlyphset
font = CurrentFont()
cropGlyphset(font, ['space', 'a', 'b', 'c'])
allGlyphs(groupsDict)

Make a list of all glyphs in all groups in the given groups dict.

char2psname(char, unicodesExtra={})

Get the PostScript name for a given unicode character.

Parameters

char (str) – A unicode character.

Returns

A PostScript glyph name.

>>> from hTools3.modules.encoding import char2psname
>>> char2psname('&')
ampersand
psname2char(glyphName, unicodesExtra={})

Get the unicode character for a given PostScript name.

Parameters
  • glyphName (str) – A PostScript glyph name.

  • unicodesExtra (dict) – Additional mappings of glyph names to unicode charactars.

Returns

A unicode character.

>>> from hTools3.modules.encoding import psname2char
>>> psname2char('seven')
7
>>> psname2char('uni013B')
Ļ
psname2unicode(glyphName, unicodesExtra={})

Get the unicode value for a given PostScript name.

Parameters

glyphName (str) – A PostScript glyph name.

Returns

A unicode character or None.

>>> from hTools3.modules.encoding import psname2unicode
>>> psname2unicode('zero')
0030
autoOS2unicodeRanges(ufo)

Automatically set OS/2 Unicode Ranges in the given font.

>>> from hTools3.modules.encoding import autoOS2unicodeRanges
>>> f = CurrentFont()
>>> print(f.info.openTypeOS2UnicodeRanges)
None
>>> autoOS2unicodeRanges(f)
>>> print(f.info.openTypeOS2UnicodeRanges)
[0, 1, 2, 3, 5, 6, 29, 31, 33, 35, 38]