Skip to main content

Load svg fonts

Project description

Table of Contents

  1. Load SVG-fonts as NumPy polylines
    1. Install with PIP (TODO)
    2. Install locally
    3. Examples

Load SVG-fonts as NumPy polylines

Svgfont is a lightweight Python library that loads SVG fonts as sequences of polylines. The polylines are represented as 2d numpy arrays (Nx2), each having N points.

The main use for this is to load Hershey fonts for drawing with a plotter, robot or CNC machine. The library comes with some standard Hershey fonts bundled, and a curated list of fonts can be found here.

The functionality of the Svgfont library is similar to the Hershey extension for the InkScape vector drawing program, but this library allows you to easily load these kind fonts in any Python script.

Install with PIP (TODO)

pip install svgfont

Install locally

Clone the repository, navigate to the directory and from there

pip install -e .

Examples

A simple use case is:

import svgfont
import matplotlib.pyplot as plt

font = svgfont.load_font('TwinSans', tol=0.5)
polylines = svgfont.text_polylines('Hello World', font, 20, pos=[50,20]) # pos is optional
plt.figure(figsize=(6,3))
for P in polylines:
    plt.plot(P[:,0], P[:,1], 'k')
plt.axis('equal')
plt.gca().invert_yaxis()
plt.show()

img

The tol parameter is optional and it determines the maximum error for sampling the Bezier curves that compose each character.

You can also fit the text to a rectangle (with optional padding) as follows:

import svgfont
import matplotlib.pyplot as plt
from matplotlib import patches

w, h = 200, 100
font = svgfont.load_font('HersheyScript1')
polylines = svgfont.text_polylines('Hello World', font, box=svgfont.rect(0, 0, w, h), padding=10)
plt.figure(figsize=(6,3))
for P in polylines:
    plt.plot(P[:,0], P[:,1], 'k')
plt.gca().add_patch(patches.Rectangle((0, 0), w, h, fill=False, edgecolor='r'))
plt.axis('equal')
plt.gca().invert_yaxis()
plt.show()

img

These examples use the Hershey fonts bundled with the library, but a path to a SVG font file can be provided also.

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

svgfont-0.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

svgfont-0.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for svgfont-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fa2d8acaa307da99dd8f983b3ce17ad88b9c7023aa89e90be34b4916e1545878
MD5 e3fc94276fc1ded6b88c178cf5560fa8
BLAKE2b-256 f4a2353035eb2c5cbc0088b8cf6981d980be34aef01b8fb145276f490dfd17ee

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for svgfont-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fdcb9825cd61d6ce5228b01d7b25a9a7d060b362a3fd256ea2f72926ff4a59b5
MD5 d3a247d619f6898f05c71f764d66ad57
BLAKE2b-256 292a13f1e454ba7c5946eac61f36dfd04e6f2fcefe8a96cebbc87237ef86f100

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