LingDong- / p5-hershey-js

p5.js Hershey Vector Font Library
MIT License
54 stars 5 forks source link
font hershey-fonts p5js typography

p5.js Hershey Vector Font Library. (Live Demo)

The Hershey fonts are a collection of vector fonts developed c. 1967 by Dr. Allen Vincent Hershey at the Naval Weapons Laboratory, originally designed to be rendered using vectors on early cathode ray tube displays. The fonts are publicly available and have few usage restrictions. Vector fonts are easily scaled and rotated in two or three dimensions; consequently the Hershey fonts have been widely used in computer graphics, computer-aided design programs, and more recently also in computer-aided manufacturing applications like laser engraving. (Wikipedia)

This p5.js library renders the Hershey fonts in the browser. It can parse user-provided Hershey fonts along with custom character mapping.

Supported Character Sets

Roman Gothic Greek Other
COMPLEX GOTHIC_ENGLISH_TRIPLEX GREEK_COMPLEX ITALIC_COMPLEX
COMPLEX_SMALL GOTHIC_GERMAN_TRIPLEX GREEK_COMPLEX_SMALL ITALIC_COMPLEX_SMALL
DUPLEX GOTHIC_ITALIAN_TRIPLEX GREEK_PLAIN ITALIC_TRIPLEX
PLAIN GREEK_SIMPLEX SCRIPT_COMPLEX
SIMPLEX SCRIPT_SIMPLEX
TRIPLEX CYRILLIC_COMPLEX

Usage

Minimal Example

In HTML, include the library as well as the data file.

<script src="https://github.com/LingDong-/p5-hershey-js/raw/master/p5.hershey.js"></script>  
<script src="https://github.com/LingDong-/p5-hershey-js/raw/master/p5.hershey.data.js"></script>  

And in the p5 sketch javascript, call P5.hershey.putText

push();
translate(100,100);
noFill();
stroke(0);
strokeWeight(1);
P5.hershey.putText("Hello World!");
pop();

Advanced

P5.hershey.putText("Hello World!",{
    cmap:  FONT_HERSHEY.GOTHIC_GERMAN_TRIPLEX,
    align: "center",
    noise: 0.5,
});

Custom Fonts

Users can also provide their own Hershey fonts and mappings. To parse a correctly formatted Hershey font as a string, use:

var font_data = P5.hershey.parseFontString("   49 12LXRMRV RNOOPOSQTSTUSUPVO");

A mapping from unicode entry point to the index used by the hershey font is also required. If the indices are identical, this can be as simple as:

var cmap = (x)=>(x);

Say you have a more complicated mapping, where characters A,B,C,D will be mapped to indices 2,4,6,8 in the Hershey font:

var cmap = (x)=>([2,4,6,8][x-65]);

Finally feed the font and the mapping to P5.hershey.putText:

P5.hershey.putText("Hello World!",{
    font:  font_data,
    cmap:  cmap,
});

Other Tools

Examples

Preview

References