Vector font package with built-in fonts and font rendering iterators
Project description
Hershey Font
Hershey font is a python library with built in fonts and low level line and stroke iterators.
What is Hershey Font?
Hershey font is a vector font, designed many years ago. The main use today if for generating text for CNC engraving.
Overview
Hershey font consists of glyphs, each glyph is assigned to an ASCII value from 32 ('space'
) until 32+number of glyphs.
Each glyph consits of array of strokes.
A stroke is an array of zero or more continous lines (points of an openend or closed ploygon).
The Python module
The Hershey-Font package is providing the HersheyFonts
class.
Great care was taken to be compatible with defulat installation of python 2.7 and python 3.
The HersheyFonts
instance is handling only one font at a time. If you need to use multiple type faces as the same time, you can load a new typeface anytime (even during rendering) or create another HersheyFonts
instance.
Installing
Sources available on GitHub Installation is available through pip and pip3
#python 3
pip3 install Hershey-Fonts
#python 2.7
pip install Hershey-Fonts
Demo
After successfully installing the Hershey-Font package, you can run the module for a simple demonstration.
#python 3
python3 -m HersheyFonts
#python 2.7
python -m HersheyFonts
Built-in fonts
The python module 1.0.0 has 32 fonts included in the source code as a compressed base64 encoded variable. The module can also load default fonts or from file and iterable string lines. When you make your own font and want to include in the package, please contact me. You can get the list of built-in fonts by looking at
from HersheyFonts import HersheyFonts
print(HersheyFonts().default_font_names)
The order and elements of the list may totally vary with any package release.
Loading a font
To access one of the built-in fonts, use the .load_default_font()
method. To read custome fonts you can call the .load_font_file(file_name)
or .read_from_string_lines(stringlines)
methods. The constructor also gives opportunity to read built-in or external font.
from HersheyFonts import HersheyFonts
thefont = HersheyFonts()
thefont.load_default_font('gothiceng')
thefont.load_default_font(thefont.default_font_names[0])
thefont.load_default_font() #Returns the name of the loaded font
thefont.load_font_file('cyrillic.jhf')
thefont.read_from_string_lines(arrayofstrings)
For more details and all options see doc comments in the sources.
Renderig the loaded font
There are several options to convert a text to font data. The simplest way is to read endpoints of the lines returned by renderer method .lines_for_text(sometext)
There are renderer methods returning list of glyps, list of strokes and list of line endpoints.
The renderers in version 1.0.0 support only single line texts. Rendering is also affected by
.render_options
property.
There is a.normalize_rendering()
method to automatically set the scaling and offsets for easy rendering.
# Minimalistic code for easy start
from HersheyFonts import HersheyFonts
def draw_line(x1, y1, x2, y2):
︙
︙
thefont = HersheyFonts()
thefont.load_default_font()
thefont.normalize_rendering(100)
for (x1, y1), (x2, y2) in thefont.lines_for_text('Hello'):
draw_line(x1, y1 ,x2 ,y2)
The Hershey-Font API
The API is documented in the source code.
Thank you
Big thanks to all people working on maintaining this old format for the modern age.
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
Built Distribution
Hashes for Hershey_Fonts-2.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e53ef56bdef231f10f0576a6570f49fcc8442e45115028bc52a7ade15d824581 |
|
MD5 | 0f668d40d3e5444a530501542af703ad |
|
BLAKE2b-256 | 677b6b42706a7a7bc88aa4aea1d19793c7d44709c4bf115ffeb5216b0ffe2dce |