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

harfbuzz-modern-wrapper

v0.0.1

Published

TypeScript wrapper for Harfbuzz font engine with bidirectional Unicode support

Downloads

2

Readme

harfbuzz-modern-wrapper

A TypeScript wrapper for the HarfBuzz text shaping engine, via the official JavaScript binding harfbuzz/harfbuzzjs. This implementation also integrates the Unicode Bidirectional Algorithm via bidi-js.

The HarfBuzz singleton

The HarfBuzz singleton is accessed through

import harfbuzz from "harfbuzz-modern-wrapper";

This object boots up the HarfBuzz web assembly instance and manages all raw pointers exposed by it.

Initialisation

To check whether the web assembly instance has been initialized, either check the return value of the sync method harfbuzz.ready(), or wait until the async harfbuzz.init() method returns.

The web assembly instance is automatically fetched on module load and harfbuzz.init() does nothing more than simply waiting for the initialisation to complete. Thus it is safe to invoke this at multiple locations in client code.

Manage fonts

The following method on the singleton exposes a font to the HarfBuzz instance:

createFont(id: string, data: Uint8Array, index: number = 0): Font | undefined

Here, id is an arbitrary identifier (any previously registered font with the same identifier will automatically be freed), data is the content of any file type supported by the HarfBuzz engine and index locates a specific font face when the provided file is a TrueType/OpenType Collection.

A registered font instance can be requested through harfbuzz.getFont(id) and, when no longer in use, it can be freed by harfbuzz.freeFont(id). Use harfbuzz.fontIds to access a list of all currently registered fonts.

Text shaping

A font instance exposes the following signature for text shaping:

type GlyphLayout = { id: number; base: { x: number; y: number } };
Font.shape(text: string): { glyphs: GlyphLayout[]; bounds: { width: number; height: number } };

This will convert a Unicode string to a sequence of glyphs in the font file. As per implementation of the HarfBuzz engine, the anchor points of the glyphs will be sorted in a left-to-right order, even if the input text is using a right-to-left script.

Use the methods font.glyphName(id) and font.glyphGeometry(id) to retrieve information about each specific glyph. The latter will return a sequence of SVG path commands. It is guaranteed that only commands of types M, L, Q, C and Z will be generated.

All coordinates will be given using the internal metrics defined by the font file. To convert them for display, scale all numbers by a ratio of fontSize / shapingResult.bounds.height).

Glyph Geometry Cache

The glyph geometry cache is a utility to cache the geometric data of each glyph, should you run any post-processing on the retrieved SVG commands — for example, to cache the results of triangulation. A typical invocation looks like the following:

import { GlyphGeometryCache, GlyphGeometryFactory } from "harfbuzz-modern-wrapper";

const geometryFactory: GlyphGeometryFactory<T> = (paths, upem) => {
  // analyse `paths` and build a geometry object of type `T`
};
const geometryCache = new GlyphGeometryCache(font, geometryFactory);

Now, geometryCache.glyphGeometry(id) can be used in place of font.glyphGeometry(id) to retrieve the geometry after post-processing.