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

type2-charstring

v0.0.5

Published

A simple converter for Type2 charstrings in human readable and byte form.

Downloads

31

Readme

A human-readable Type2 Charstring to bytecode converter

A simple converter for Type2 charstrings in human readable and byte form

In Node.js source code

Install with npm install type2-charstring, then use it in your code as:

var convert = require('type2-charstring');
var charstring = convert.toBytes([
]);

In the browser

Use the convert.js from the repo:

<script src="convert.js"></script>
<script>
  var charstring = Type2Convert.toBytes([
  ]);
</script>

API

The following functions are exposed:

  • bindSubroutine: function(functor, bytes), binds a global subroutine (to do this from human readable form, run the code through .toBytes and then bind that to a name). The functor can be used in subsequent charstrings to autoresolve to the right subr, so:
bindSubroutine("sin()", [.....]);
// we can now use sin() as charstring code:
var ncs = "3.1415 sin() endchar";
var ncsBytes = Type2Convert.toBytes(ncs);
  • getSubroutines: function(), returns the list of global subroutines known until now, with bias correction applied. Generally useful if you need to actually build a font based on the charstrings you've been creating.

  • toBytes: function(string, subroutines), converts a human readable charstring to byte form. White space and commas are treated as non-semantic, and line comments are stripped, so you can write properly readable code:

...

// When we start the stack contains: [angle, ox, oy, x, y],
// so we put them onto the transient stack in argument order:
4 put, 3 put, 2 put, 1 put, 0 put

// compute the sin(x) and cos(x) of the provided angle
0 get, sin(x), 5 put
0 get, cos(x), 6 put
...
  • toString: function(bytes, subroutines), converts a sequence of bytes into human readable charstring code, but not currently implemented

Dev work

Clone the repo, then install with npm install.

Tests can be run with npm test, and currently cover validation of three functions:

  • sin(x)
  • cos(x), with a dependency on sin(x)
  • rotate(angle, ox, oy, x, y), with a dependency on sin(x) and cos(x)

Why?

I needed a way to write Type2 functions and automatically build them into subroutines.