Skip to content

Commit

Permalink
Call it sfnt instead of truetype
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 19, 2021
1 parent dd36f89 commit bb581a2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# subset-font

Create a subset font from an existing font in Truetype (.ttf), WOFF, or WOFF2 format. Uses [`harfbuzzjs`](https://github.com/harfbuzz/harfbuzzjs), which is a WebAssembly build of [HarfBuzz](https://harfbuzz.github.io/).
Create a subset font from an existing font in SFNT (TrueType/OpenType), WOFF, or WOFF2 format. Uses [`harfbuzzjs`](https://github.com/harfbuzz/harfbuzzjs), which is a WebAssembly build of [HarfBuzz](https://harfbuzz.github.io/).

```js
const subsetFont = require('subset-font');

const myTruetypeFontBuffer = Buffer.from(/*...*/);
const mySfntFontBuffer = Buffer.from(/*...*/);

// Create a new font with only the characters required to render "Hello, world!" in WOFF2 format:
const subsetBuffer = await subsetFont(myTruetypeFontBuffer, 'Hello, world!', {
const subsetBuffer = await subsetFont(mySfntFontBuffer, 'Hello, world!', {
targetFormat: 'woff2',
});
```
Expand All @@ -23,14 +23,16 @@ Returns a promise that gets fulfilled with the subset font as a Buffer instance,

Options:

- `targetFormat` - the format to output, can be either `'truetype'`, `'woff'`, or `'woff2'`.
- `targetFormat` - the format to output, can be either `'sfnt'`, `'woff'`, or `'woff2'`.

For backwards compatibility reasons, `'truetype'` is supported as an alias for `'sfnt'`.

## Why not use harfbuzzjs directly?

This middle-man module only really exists for convenience.

- `harfbuzzjs` is deliberately low-level bindings for HarfBuzz. While very flexible, it means that you need a series of hard-to-get-right incantations to move data in and out of the WebAssembly heap and carry out a subsetting operation. See [harfbuzz/harfbuzzjs#9](https://github.com/harfbuzz/harfbuzzjs/issues/9).
- The subsetting routines in HarfBuzz only support the Truetype format. `subset-font` adds support for reading and writing WOFF and WOFF2 via the [`fontverter`](https://github.com/papandreou/fontverter) library.
- The subsetting routines in HarfBuzz only support the SFNT (TrueType/OpenType) format. `subset-font` adds support for reading and writing WOFF and WOFF2 via the [`fontverter`](https://github.com/papandreou/fontverter) library.

## Releases

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Create a subset of a TTF/WOFF/WOFF2 font using the wasm build of harfbuzz/hb-subset",
"main": "index.js",
"dependencies": {
"fontverter": "^1.0.0",
"fontverter": "^2.0.0",
"harfbuzzjs": "^0.1.4",
"p-limit": "^3.1.0"
},
Expand Down
30 changes: 12 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,50 @@ const { readFile } = require('fs').promises;
const pathModule = require('path');

describe('subset-font', function () {
before(async function () {
this.truetypeFont = await readFile(
pathModule.resolve(__dirname, '..', 'testdata', 'OpenSans.ttf')
);
});

describe('with a truetype font', function () {
before(async function () {
this.truetypeFontFont = await readFile(
this.sfntFont = await readFile(
pathModule.resolve(__dirname, '..', 'testdata', 'OpenSans.ttf')
);
});

describe('with no targetFormat given', function () {
it('should return the subset as truetype', async function () {
const result = await subsetFont(this.truetypeFont, 'abcd');
const result = await subsetFont(this.sfntFont, 'abcd');

expect(result, 'to be a', 'Buffer');
expect(result.length, 'to be less than', this.truetypeFont.length);
expect(fontverter.detectFormat(result), 'to equal', 'truetype');
expect(result.length, 'to be less than', this.sfntFont.length);
expect(fontverter.detectFormat(result), 'to equal', 'sfnt');
});
});

it('should produce a subset as ttf', async function () {
const result = await subsetFont(this.truetypeFont, 'abcd', {
const result = await subsetFont(this.sfntFont, 'abcd', {
targetFormat: 'truetype',
});

expect(result, 'to be a', 'Buffer');
expect(result.length, 'to be less than', this.truetypeFont.length);
expect(fontverter.detectFormat(result), 'to equal', 'truetype');
expect(result.length, 'to be less than', this.sfntFont.length);
expect(fontverter.detectFormat(result), 'to equal', 'sfnt');
});

it('should produce a subset as woff', async function () {
const result = await subsetFont(this.truetypeFont, 'abcd', {
const result = await subsetFont(this.sfntFont, 'abcd', {
targetFormat: 'woff',
});

expect(result, 'to be a', 'Buffer');
expect(result.length, 'to be less than', this.truetypeFont.length);
expect(result.length, 'to be less than', this.sfntFont.length);
expect(result.slice(0, 4).toString(), 'to equal', 'wOFF');
});

it('should produce a subset as woff2', async function () {
const result = await subsetFont(this.truetypeFont, 'abcd', {
const result = await subsetFont(this.sfntFont, 'abcd', {
targetFormat: 'woff2',
});

expect(result, 'to be a', 'Buffer');
expect(result.length, 'to be less than', this.truetypeFont.length);
expect(result.length, 'to be less than', this.sfntFont.length);
expect(result.slice(0, 4).toString(), 'to equal', 'wOF2');
});
});
Expand Down Expand Up @@ -88,7 +82,7 @@ describe('subset-font', function () {

expect(result, 'to be a', 'Buffer');
expect(result.length, 'to be less than', this.woffFont.length);
expect(fontverter.detectFormat(result), 'to equal', 'truetype');
expect(fontverter.detectFormat(result), 'to equal', 'sfnt');
});

it('should produce a subset as woff', async function () {
Expand Down

0 comments on commit bb581a2

Please sign in to comment.