npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hersheytext

v2.0.0

Published

A port of the Hershey engraving fonts to JSON for JavaScript/SVG

Downloads

28

Readme

Hershey Text JS

A port of the EMSL Hershey engraving font data from the Hershey Text Inkscape Plugin to JSON, capable of being rendered quickly via JavaScript & SVG.

This includes an quickly written example renderer, see the demo on the live github page to give it a try!

JSON data only contains original Hershey font data, but Node.js module allows access to this and all included SVG fonts and an API to add your own!

Clientside Use

Load the structured JSON data and use it to create your own renderer, or use the example as a starting point.

Node.js: Install & Include

Install via npm install hersheytext, then include in your node script with const hershey = require('hersheytext');.

JSON data source

The JSON font data will be accessible at hershey.fonts, EG: hershey.fonts['futural'].chars[2]. The chars[n].d string value can be put directly into the d attribute within a <path> SVG element, or imported to a Paper.js compound path.

See hersheytest.js for more data usage examples, check lib/hersheytext.js for full function level documentation.

Get raw font data

You can access either SVG or Hershey font raw data using getFontData():

const hershey = require('hersheytext');

console.log('All fonts available', hershey.getFonts());

const svgFont = hershey.getFontData('ems_tech');
console.log('Font Type:', svgFont.type);
console.log('SVG <font-face> cap height:', svgFont.info['cap-height']);
console.log('SVG <font> default char kern width:', svgFont.info['horiz-adv-x']);
console.log('SVG <glyph> exclamation char data:', svgFont.getChar('!'));

const hersheyFont = hershey.getFontData('cursive');
console.log('Font Type:', hersheyFont.type);
console.log('Hershey Font Display Name:', hersheyFont.info['font-family']);
console.log(`Hershey Font 'A' kern width:`, hersheyFont.getChar('A').width);
console.log('Hershey Font exclamation char data:', hersheyFont.getChar('!').d);

Render to SVG directly

Hershey Text JS comes with a very simple single line renderer, renderTextSVG(), which returns a formatted group of characters with correct offsets.

const hershey = require('hersheytext');
const fs = require('fs');

const options = {
  font: 'hershey_script_1',
  scale: 0.25,
};

const header = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">';
const data = hershey.renderTextSVG('Take a Look, if you Will.', options);
const footer = '</svg>';

fs.writeFileSync('test.svg', `${header}\n${data}\n${footer}`);

Make your own renderer with text array

If you have your own rendering intent that isn't SVG directly, the render array function is the easiest way to do that. To render plain text into an array of easily renderable character data from SVG or Hershey font data, start with something like this:

const hershey = require('hersheytext');
const textArray = hershey.renderTextArray('Hello World!', { font: 'ems_allure' });

textArray.forEach(char => {
  console.log('--------------------------------------------');
  console.log('Character:', char.type);
  console.log('Name:', char.name);
  console.log('Kerning Width (in font units):', char.width);
  console.log('Path Data:', char.d);
});

Add a custom SVG font

If you have an SVG font you'd like to use that isn't one of the ones HersheyText JS ships with, just use addSVGFont():

const hershey = require('hersheytext');

// Will return false if there was a problem. See console for errors.
hershey.addSVGFont('path/to/my-test-font.svg');

// Machine name will be SVG font-family, lowercase with spaces replaced with underscores.
const options = { font: 'my_test_font' };
const data = hershey.renderTextSVG('In my own custom font!', options);

Notes:

  • Raw SVG font data is in glyph format, and therefore needs to be Y-inverted after it's been positioned, EG: <path transform="scale(1, -1)" …
  • Hershey font data is in its own far smaller scale and does not need to be inverted.

JSON data Public Domain, All other code MIT Licensed.