Skip to main content

Utilities for skia-python to shape and draw text with harfbuzz

Project description

skia-harfbuzz

A Python library that helps to draw text in skia-python with harfbuzz shaping engine.

Effect Preview

The library integrates with harfbuzz to support font features like kerning and ligatures.

Kerning

Kerning refers to the spacing between specific glyph pairings. Kerning: GeosansLight

Ligatures

Ligatures are glyphs that replace two or more separate glyphs in order to represent them more smoothly (from a spacing or aesthetic perspective). Some ligatures are necessary in fonts. Ligatures: JetBrainsMono

Usage

Use Library API

Create Typeface and Font

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 })

For more information about font features, see this.

Draw and Measure

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

Patch skia to override typeface loading and basic text rendering. And you can continue with you old code.

from skia_harfbuzz import patch_skia

patch_skia()

After patching, skia typefaces 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.drawString
  • Canvas.drawSimpleText
  • Font.measureText

Limitations

There are some limitations that you may need to take care when using:

  • Paint argument in patched Font.measureText is not supported. This ignores stroke width and path effect, etc.
  • Minor font settings (e.g. embolden) not considered.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

skia_harfbuzz-0.1.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

skia_harfbuzz-0.1.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file skia_harfbuzz-0.1.0.tar.gz.

File metadata

  • Download URL: skia_harfbuzz-0.1.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for skia_harfbuzz-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7fcc2bba9c46645e3d956f27391b6d50745274fdfc3d904965f5cdcd4c3b89eb
MD5 13954e2930e66807a59951844f56b8fa
BLAKE2b-256 e6a07dc3e28a4633cfce675d1fb15b73e4f990783636f6720ce464936def1c29

See more details on using hashes here.

File details

Details for the file skia_harfbuzz-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: skia_harfbuzz-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for skia_harfbuzz-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3a115928af048b4639d8acb8dca17e969c388c1ead121cb08a34259963c1cde
MD5 33b1e759e49dd3c43efdaf76a0684020
BLAKE2b-256 d7e71e2c7c76bc83954f29e40ca6a2412d88cf3e968836174058c835ba24cc2e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page