Skip to main content

A Python-based renderer for OpenType COLRv1 fonts, with multiple backends.

Project description

Python package

BlackRenderer

BlackRenderer is a Python-based renderer for OpenType COLR fonts, with multiple backends. It supports the new COLRv1 format as well as the old COLR format. It fully supports variable fonts, including variable COLRv1 data.

Big Smiley Face Icon

Features

  • It's a Python library called "blackrenderer"
  • It's a command line tool called "blackrenderer"
  • Renders individual glyphs or text strings
  • Supports multiple graphics backends:
    • Skia
    • Cairo
    • CoreGraphics (macOS)
    • SVG
    • outline extractor
    • bounding box calculator
  • Supports multiple output formats:
    • .png
    • .pdf
    • .svg
  • It uses fonttools to parse COLRv1 data
  • The "blackrenderer" tool is an "hb-view"-like command line app with switchable backend. It uses HarfBuzz for shaping

Tool usage example

BlackRenderer comes with an hb-view-like command line tool, that can be used like this:

$ blackrenderer font.ttf ABC🤩 output.png --font-size=100

Library usage examples

There is a high level function to render a text string:

from blackrenderer.render import renderText

renderText("myfont.ttf", "ABC", "output.png")  # or "output.svg"

The full renderText() signature is:

def renderText(
    fontPath,
    textString,
    outputPath,
    *,
    fontSize=250,
    margin=20,
    features=None,
    variations=None,
    backendName=None,
)

For more control, the library exposes two main parts: the BlackRendererFont class, and a set of backend classes. Each backend provides a Canvas class. You pass a Canvas instance to a BlackRendererFont instance when drawing a glyph. Most backends also have a Surface class, which is a generalized convenience class to produce a canvas for a bitmap (or SVG document) for a specific box. Here is a minimal example:

from blackrenderer.font import BlackRendererFont
from blackrenderer.backends import getSurfaceClass

brFont = BlackRendererFont("my_colr_font.ttf")
glyphName = "A"
boundingBox = brFont.getGlyphBounds(glyphName)
surfaceClass = getSurfaceClass("skia")
surface = surfaceClass()
with surface.canvas(boundingBox):
    brFont.drawGlyph(glyphName, canvas)
surface.saveImage("image.png")

Canvas objects support the following transformation methods:

  • canvas.translate(dx, dy)
  • canvas.scale(sx, sy)
  • canvas.transform((xx, yx, xy, yy, dx, dy))

Save/restore is done with a context manager:

with canvas.savedState():
    canvas.scale(0.3)
    ...draw stuff...

Install

If you have a Python 3 environment set up, then all you need to do is:

$ pip install blackrenderer

Install for contributing / setting up an environment

Have Python 3.10 or higher installed.

Open Terminal.

"cd" into the project repo directory.

Create a virtual environment:

  • $ python3 -m venv venv --prompt=black-renderer

Activate the venv:

  • $ source venv/bin/activate

(You need to activate the virtual environment for every new terminal session.)

Upgrade pip:

  • $ pip install --upgrade pip

Install the requirements:

  • $ pip install -r requirements.txt
  • $ pip install -r requirements-dev.txt

Install blackrenderer in editable mode:

  • $ pip install -e .

Run the tests:

  • $ pytest

Maintainers: how to release

To cut a release, make an annotated git tag, where the tag is in this format: v1.2.3, where 1, 2 and 3 represent major, minor and micro version numbers. You can add "aN" or "bN" or "rc" to mark alpha, beta or "release candidate" versions. Examples: v1.2.3, v1.2.3b2, v1.2.3a4, v1.2.3rc.

The message for the annotated tag should contain the release notes.

Then use "git push --follow-tags" to trigger the release bot. Example session:

  • $ git tag -a v1.2.3 -m "v1.2.3 -- fixed issue #12345"
  • $ git push --follow-tags

This process will create a GitHub release, as well as upload the package to PyPI.

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

blackrenderer-0.8.1.tar.gz (5.4 MB view details)

Uploaded Source

Built Distribution

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

blackrenderer-0.8.1-py3-none-any.whl (34.2 kB view details)

Uploaded Python 3

File details

Details for the file blackrenderer-0.8.1.tar.gz.

File metadata

  • Download URL: blackrenderer-0.8.1.tar.gz
  • Upload date:
  • Size: 5.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for blackrenderer-0.8.1.tar.gz
Algorithm Hash digest
SHA256 32d1d875b61ac13ccbe75128aa308943d2d8a40ce95d77f9d64930ed8978d509
MD5 4da3ecbeda0225fc881da7560cf78bce
BLAKE2b-256 d5b5ed69f94297b8ace0072fb6d41ec29f78c4ec720c6abb7a50e9308fe3a0a1

See more details on using hashes here.

File details

Details for the file blackrenderer-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: blackrenderer-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 34.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for blackrenderer-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ce9fe6a4ab7b9abff3096fe09f34a5e0d29844be5db2f64c08ee49947f15541
MD5 4aeb3bb487747b997c518b2401636f4d
BLAKE2b-256 09349c5e3889592f7b18de82d52fc9214652081eae9d4df4efbfe3518ddc12a3

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