vharfbuzz: A user-friendlier way to use Harfbuzz in Python

class vharfbuzz.Vharfbuzz(filename)

A user-friendlier way to use Harfbuzz in Python.

Parameters:

filename (str) – A path to a TrueType font file.

shape(text, parameters=None, onchange=None)

Shapes a text

This shapes a piece of text.

Parameters:
  • text (str) – A string of text

  • parameters – A dictionary containing parameters to pass to Harfbuzz. Relevant keys include script, direction, language (these three are normally guessed from the string contents), features, variations and shaper.

  • onchange – An optional function with three parameters. See below.

Additionally, if an onchange function is provided, this will be called every time the buffer changes during shaping, with the following arguments:

  • self: the vharfbuzz object.

  • stage: either “GSUB” or “GPOS”

  • lookupid: the current lookup ID

  • buffer: a copy of the buffer as a list of lists (glyphname, cluster, position)

Returns:

A uharfbuzz hb.Buffer object

serialize_buf(buf, glyphsonly=False)

Serializes a buffer to a string

Returns the contents of the given buffer in a string format similar to that used by hb-shape.

Parameters:

buf – The hb.Buffer object.

Returns: A serialized string.

buf_from_string(s)

Deserializes a string.

This attempts to perform the inverse operation to serialize_buf(), turning a serialized buffer back into an object. The object is not a hb.Buffer, but has a similar structure (glyph_infos and glyph_positions) so can be passed to code which expects a hb.Buffer, such as buf_to_svg() below.

Parameters:

s (str) – A string produced by serialize_buf()

Returns a FakeBuffer object.

glyph_to_svg_path(gid)

Converts a glyph to SVG

Parameters:

gid (int) – Glyph ID to render

Returns: An SVG string containing a path to represent the glyph.

buf_to_svg(buf)

Converts a buffer to SVG

Parameters:

buf (hb.Buffer) – uharfbuzz hb.Buffer

Returns: An SVG string containing a rendering of the buffer