fontutils

Tools to work with fonts.

getGlyphs2(font, glyphNames=True, template=False)

Return the current glyph selection in the font as a list of glyph names or glyph objects.

Parameters
  • glyphNames – Return the result as a list of glyph names. If set to False, return the result as a list of glyph objects.

  • template – Include template glyphs in selection.

>>> from hTools3.modules.fontutils import getGlyphs2
>>> f = CurrentFont()
>>> getGlyphs2(f)
['emacron', 'Emacron']
>>> getGlyphs2(f, template=True)
['Emacron', 'emacron', 'Edotaccent', 'edotaccent']
>>> getGlyphs2(f, glyphNames=False)
[<RGlyph 'emacron' ('foreground') at 4778152272>, <RGlyph 'Emacron' ('foreground') at 4778372304>]
getGlyphs(font)

Return the current glyph selection in the font as a list of glyph names.

  • single window mode : currentGlyph AND fontSelection

  • multi-window mode : currentGlyph OR fontSelection

Warning

This function is deprecated, use getGlyphs2() instead.

getFontID(fontOrFontPath)

Creates a unique font ID string in the format “familyName styleName (fontPath)”.

Parameters

fontOrFontPath (RFont or str) – A font object or a path to a font file.

Returns

A font ID string.

>>> from hTools3.modules.fontutils import getFontID
>>> f = CurrentFont()
>>> getFontID(f)
Publica 555A (/_fonts/Publica/_ufos/555A.ufo)
parseGString(font, gString)

Convert an input string into a list of glyph names.

Parameters
  • font (RFont) – A font object.

  • gString (str) – A text input string.

>>> from hTools3.modules.fontutils import parseGString
>>> f = CurrentFont()
>>> gstring = 'abc0123/a.sc/a.alt ABC'
>>> parseGString(f, gstring)
['a', 'b', 'c', 'zero', 'one', 'two', 'three', 'a.sc', 'a.alt', 'A', 'B', 'C']
isQuadratic(font)

Check if a given font is quadratic or cubic.

Parameters

font (RFont) – A font object.

::
>>> from hTools3.modules.fontutils import isQuadratic
>>> isQuadratic(CurrentFont()))
False
>>> isQuadratic(OpenFont('MyFont.ttf', showInterface=False)))
True
>>> isQuadratic(OpenFont('MyFont.otf', showInterface=False)))
False
clearUnicodes(font)

Clear unicodes of all glyphs in the font.

from hTools3.modules.fontutils import clearUnicodes
f = CurrentFont()
clearUnicodes(f)
clearAllGuidelines(font)

Clear all font-level and glyph-level guidelines in the font.

from hTools3.modules.fontutils import clearAllGuidelines
f = CurrentFont()
clearAllGuidelines(f)
markGlyphs(font, glyphNames, color, verbose=True)

Set mark color for a list of glyph names in the font.

Parameters
  • font (RFont) – A font object.

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

  • color (tuple or None) – A mark color as a RGBA tuple or None.

from hTools3.modules.fontutils import markGlyphs
f = CurrentFont()
markGlyphs(f, ['a', 'b', 'c'], (1, 0, 0, 0.3))
clearMarkColors(font)

Clear all mark colors for all glyphs in the font.

from hTools3.modules.fontutils import clearMarkColors
f = CurrentFont()
clearMarkColors(f)
findMarkColor(font, color)

Find all glyphs in the font with a given mark color.

findContoursOnly(font)

Find all glyphs in the font which contain only contours (no components).

findComponentsOnly(font)

Find all glyphs in the font which contain only components (no contours).

findContoursAndComponents(font)

Find all glyphs in the font which contain contours _and_ components.

findEmptyGlyphs(font)

Find all glyphs in the font which contain neither contours nor components.

findOpenContours(font)

Find all glyphs in the font which contain open contours.

findShortSegments(font, threshold=10)

Find all glyphs in the font which contain a segment smaller than the given threshold value.

findClosePoints(font, threshold=0)

Find all glyphs in the font which contain points which are closer than the given threshold value.

findAttribute(font, attr, mark=True, sort=False, cluster=1)
findGlyphComponents(font, srcName)

Find all components of a given glyph in the font.

>>> from hTools3.modules.fontutils import findGlyphComponents
>>> f = CurrentFont()
>>> findGlyphComponents(f, 'a')
['aacute', 'agrave', 'atilde', 'acircumflex', 'adieresis', 'aring']