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')
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()

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')
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()

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.5.tar.gz (11.0 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.5-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: svgfont-0.1.5.tar.gz
  • Upload date:
  • Size: 11.0 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.5.tar.gz
Algorithm Hash digest
SHA256 53a61faa0a18986a32be91ad1a4926e2635ddf8596776606f9196aa6a9f5df1b
MD5 ba178eba7ff85b4aed7dc0cb2da1c947
BLAKE2b-256 d47e30c1c24b57f664d0f38b1c79e51fa47908acfb8fdb7771d47b0d674fc4c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: svgfont-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 10.0 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7afce4f7b0a504be698750c097b50bf059c291273299b99f044c7446bac06168
MD5 c67d6c3196e97985cff42c2135e414e1
BLAKE2b-256 5855d474c7e789abca3d4909e342fe4859fa4785d9e4d5f3c7ea1f2a6caace2b

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