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

typr-ts

v1.1.5

Published

Typr.ts - process fonts in TypeScript.

Downloads

817

Readme

Typr.ts

Typr.ts is a TypeScript conversion of Typr.js.

Typr.ts is a TypeScript parser and utility for working with fonts (TTF, OTF). It is an alternative to opentype.js. It is the main text engine for Photopea image editor.

  • ultra fast (2x to 5x faster parsing than opentype.js)
  • successfully parsed more than 3000 fonts (opentype.js had problems with many of them)
  • simple structure and easy to extend
  • supports colored (SVG) fonts

Typr.js preview

Typr.ts consists of static functions only, it can be easily rewritten into C or any other procedural language. There are no constructors, no methods, no complex structure. It consists of two independent parts (separate files):

  • Typr - main parser, parses the raw data, generates the font object.
  • TyprU - Typr utilities. Basic operations with fonts. Use it as a guide to write your own utilities.

Installation

npm install typr-ts --save

Running Demo

npm run webpack : The development server starts on port 9003 by default. So then navigate to localhost:9003 to view the demo.

Building

npm run webpack-prod : Output will be in the {root}/dist folder.

Usage

import { Typr } from "typr-ts";

Typr.parse(buffer)

  • buffer: ArrayBuffer, binary data of the TTF or OTF font
  • returns a font object

The output object has a structure, wich corresponds to the structure of the TTF/OTF file. I.e. it is a set of tables, each table has its own structure.

var font = Typr.parse(buffer);
console.log(font);

TyprU

import { TyprU } from "typr-ts";

TyprU.codeToGlyph(font, code)

  • font: font object
  • code: integer code of the character
  • returns an integer index of the glyph, corresponding to the unicode character

TyprU.stringToGlyphs(font, str)

  • font: font object
  • str: standard JS string
  • returns an array of glyph indices for the string. Calls codeToGlyph() for each character code and performs mandatory glyph substitution (e.g. in Arabic, the same character may need different glyphs at th beginning, in the middle or at the end of a word). Also adds mandatory ligatures.

TyprU.getPairAdjustment(font, gid1, gid2)

  • font: font object
  • gid1: index of the first glyph
  • gid2: index of the second glyph
  • returns the the adjustment parameters of the pair of glyphs

TyprU.glyphToPath(font, gid)

  • font: font object
  • gid: index of the glyph, which you want to access
  • returns the vector path of the outline of the glyph

Typr.js uses the following structure to represent the path:

{ cmds: [CMD,CMD,CMD, ...], crds:[X,Y,X,Y, ...] }

cmds is an array of commands (Strings), crds is an array of coordinates (Numbers). Each command needs a specific number of coordinates. The path can be processed by passing both arrays from the left, index into crds depends on the types of previous commands.

  • "M": (X,Y) - move the pointer to X,Y.
  • "L": (X,Y) - line from the previous position to X,Y.
  • "Q": (X1,Y1,X2,Y2) - quadratic bézier curve from the previous position to X2,Y2, using X1,Y1 as a control point.
  • "C": (X1,Y1,X2,Y2,X3,Y3) - cubic bézier curve from the previous position to X3,Y3, using X1,Y1 and X2,Y2 as control points.
  • "Z": () - draw a line to the first point to finish the outline.
  • "#rrggbb" : () - set the current collor to RGB(rr,gg,bb) (SVG fonts use this)
  • "X": () - fill the current path (SVG fonts use this)

A "raindrop" shape: { cmds:["M","L","Q","Z"], crds:[0,0,20,80,0,120,-20,80] } (2 + 2 + 4 + 0 coordinates).

The format is similar to SVG, but commands and coordinates are separated. It is comfortable to work with coordinates as a set of 2D points, apply affine transformations etc.

TyprU.glyphsToPath(font, gls)

  • font: font object
  • gls: the array of glyphs, wich you want to draw using a font
  • returns the vector path of the outline of the input glyph array

Note, that all paths returned by Typr.U are in font units ( font.head.unitsPerEm ). You must scale them down to convert them to pixels.

TyprU.pathToContext(path, ctx)

  • path: path to draw
  • ctx: context2d to draw the path into

It executes each command of the path with a corresponding command of context2D: moveTo(), lineTo(), ... It does nothing else (you must do translate(), scale(), fillStyle, fill(), stroke() ... manually).

TyprU.pathToSVG(path)

Converts a path to an "SVG path string", which can be used in <path d="..." />.

Extending Typr

Let's implement a little function for drawing a string:

import { TyprU } from "typr-ts";

class FancyTyprU extends TyprU {
	public static stringToContext(font, str, ctx, size, color, x, y) {
		  let gls  = TyprU.stringToGlyphs(font, str);
          //  gls.reverse();  // reverse if Right-to-Left
          let path = TyprU.glyphsToPath  (font, gls);
          let scale = size / font.head.unitsPerEm;
          
          ctx.translate(x,y);  ctx.scale(scale,-scale);
          
          ctx.fillStyle = color;
          TyprU.pathToContext(path, ctx);
          ctx.fill();
          
          ctx.scale(1 / scale, -1 / scale);  ctx.translate(-x, -y);
	}
}

Right-to-left text direction

There is a "view direction" (left to right) of glyphs, in which they are usually rendered. It may be different than the logical direction of characters in the memory (in which they are written).

TyprU.stringToGlyphs expects characters to be in the logical direction, while TyprU.glyphsToPath method expects glyphs to be in the "view direction" (left to right).

If your text contains e.g. only Arabic / Hebrew parts, just reverse the array of glyphs before calling TyprU.glyphsToPath. When your text contains both RTL and LTR parts, reorder glyphs according to the Unicode BIDI algorithm (e.g. using unicode-bidirectional).