Table of Contents
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')
paths = svgfont.text_paths('Hello\nWorld', font, 20,
align='center',
pos=[50,20],
tol=0.1) # pos is optional
plt.figure(figsize=(6,3))
for P in paths:
plt.plot(P[:,0], P[:,1], 'k')
plt.axis('equal')
plt.gca().invert_yaxis()
plt.show()
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')
paths = svgfont.text_paths('Hello World', font, box=svgfont.rect(0, 0, w, h), padding=10)
plt.figure(figsize=(6,3))
for P in paths:
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()
These examples use the Hershey fonts bundled with the library, but a path to a SVG font file can be provided also.
Metadata
Release files for svgfont 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| svgfont-0.1.6.tar.gz | 188.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| svgfont-0.1.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 382.5 kB
Release files / svgfont-0.1.6.tar.gz
| Download URL | svgfont-0.1.6.tar.gz |
|---|---|
| Size | 188.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ac96601eae9bf78a188754c6a712f4396d7784a3d0abccf779078708816e5d9d
|
|
BLAKE2b-256 checksum How to use checksums |
16b497f356c05634d876d414b3728e07cf053ea9c6a8a6fb7949d79acf011504
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.8
|
Release files / svgfont-0.1.6-py3-none-any.whl
| Download URL | svgfont-0.1.6-py3-none-any.whl |
|---|---|
| Size | 194.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8b7ee99f1f53f979dc642f5fce88d9e3fa44eaa2466cf13f2254351b5b85d8f8
|
|
BLAKE2b-256 checksum How to use checksums |
5da39a8d4696a7ade0b560c6af93bdc9771fd6af7d5c2a06b551bf5150b7643c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.8
|