Skip to main content

Fast font measurement in Rust callable from Python

Project description

gtfont

Fast string measurement for perfect tables.

gtfont is a Rust-powered Python extension that provides precise text measurement utilities, optimized for building perfectly aligned tables from pandas DataFrames.

This library is designed for use within the greater_tables package, but can also be used standalone.

Features

  • Measure the rendered width of a sentence using system fonts
  • Compute maximum word width in a line
  • Combine both in one efficient call
  • Based on native font rendering for accurate results
  • Blazing-fast Rust backend exposed as a Python module

Installation

pip install gtfont

Python ≥3.8 is required. Pre-built wheels are available for major platforms via PyPI.

Example

from gtfont import FontMeasurer
from matplotlib import font_manager

font_path = font_manager.findfont("Times New Roman", fallback_to_default=True)
font_bytes = Path(font_path).read_bytes()
fm = FontMeasurer(font_bytes)
sentence = "The quick brown fox jumps over the lazy dog."

# Measure sentence width in points
width = fm.measure(sentence, 11)

# Measure max word width
max_word = fm.max_word_width(sentence, 11)

# Both at once
sentence_width, max_word_width = fm.measure_and_max_word(sentence, 11)

Build

maturin build --release --strip

# or 

maturin develop 

Why Rust?

Font measurement is performance-critical. By leveraging pyo3, we combine the safety and speed of Rust with the usability of Python.

License

MIT © Stephen J. Mildenhall

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

gtfont-1.0.0-cp38-abi3-win_amd64.whl (2.8 kB view details)

Uploaded CPython 3.8+Windows x86-64

File details

Details for the file gtfont-1.0.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: gtfont-1.0.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.8 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for gtfont-1.0.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0787b28a1985608c438ba5bc2e4ff10ad637dac40c7918dc7017f1a97f36a4bf
MD5 e939590822bf0522b17315f0b5148a9c
BLAKE2b-256 4ad947a3f63ab79457aed843819772b8ae7bf85777ba10c31a7f955de1276834

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