Load svg fonts
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file svgfont-0.1.6.tar.gz.
File metadata
- Download URL: svgfont-0.1.6.tar.gz
- Upload date:
- Size: 188.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac96601eae9bf78a188754c6a712f4396d7784a3d0abccf779078708816e5d9d
|
|
| MD5 |
da56167f026e0cb55572fde9f37d004a
|
|
| BLAKE2b-256 |
16b497f356c05634d876d414b3728e07cf053ea9c6a8a6fb7949d79acf011504
|
File details
Details for the file svgfont-0.1.6-py3-none-any.whl.
File metadata
- Download URL: svgfont-0.1.6-py3-none-any.whl
- Upload date:
- Size: 194.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b7ee99f1f53f979dc642f5fce88d9e3fa44eaa2466cf13f2254351b5b85d8f8
|
|
| MD5 |
2cfecf60d80ad066a1a1ac97064422b2
|
|
| BLAKE2b-256 |
5da39a8d4696a7ade0b560c6af93bdc9771fd6af7d5c2a06b551bf5150b7643c
|