A Python library that helps to draw text in skia-python with harfbuzz shaping engine.
The library integrates with uharfbuzz to support font features like kerning and ligatures. Here are some showcases for harfbuzz-integrated text shaping.
For more information about font features, see this.
Kerning refers to the spacing between specific glyph pairings.

Ligatures are glyphs that replace two or more separate glyphs in order to represent them more smoothly (from a spacing or aesthetic perspective).
Ligatures are necessary in certain language scripts.

The Python package is available on PyPI.
pip install skia-harfbuzzIf you are using Linux, make sure you have correctly configured the environment for skia-python.
from skia_harfbuzz import SkiaHarfbuzzTypeface, SkiaHarfbuzzFont
font_path = '/path/to/your_font.ttf'
typeface = SkiaHarfbuzzTypeface.create_from_file(font_path)
# Or use font data
# with open(font_path, 'r') as f:
# typeface = SkiaHarfbuzzTypeface.create_from_data(f.read())
font: SkiaHarfbuzzFont = typeface.create_font(size=20.0)
# Optionally, set scaleX, skewX and font features
# font = typeface.create_font(size=20.0, scale_x=1.1, skew_x=-0.10, features={ 'calt': False })SkiaHarfbuzzFont provides the following functions to measure and draw text:
measure_text(text, bounding_box): returns the advance width of text, and optionally a bounding box relative to origin (0, 0)draw_text(canvas, text, x, y, paint, anchor_x, anchor_y): draws text with given options
The value of anchor_x and anchor_y indicates the position type of input x and y. By default,
x is considered the left coordinate and y is considered the baseline coordinate (same as skia API).
AnchorTypeX: left, center, right
AnchorTypeY: baseline, top, center, bottom
Patch skia to override typeface loading and basic text rendering. Then you can continue with you old code.
from skia_harfbuzz import patch_skia
patch_skia()After patching, skia typeface created by Typeface.MakeFromPath or Typeface.MakeFromData will manage its companion harfbuzz typeface.
The companion harfbuzz typeface is deleted when the typeface is garbage collected.
The following APIs are patched to work with harfbuzz:
Canvas.drawStringCanvas.drawSimpleTextFont.measureText
There are some limitations that you may need to take care when using:
Paintargument in patchedFont.measureTextis not supported. This ignores stroke width and path effect, etc.- Minor font settings (e.g. embolden) not considered.