Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,15 @@ buffer.then(data => {
WOFF2 Brotli compression perform [29% better](https://www.w3.org/TR/WOFF20ER/#appendixB) than it WOFF predecessor.
But this compression is also more complex, and would result in a much heavier (>10×!) opentype.js library (≈120KB => ≈1400KB).

To solve this: Decompress the font beforehand (for example with [fontello/wawoff2](https://github.com/fontello/wawoff2)).
To solve this: Decompress the font beforehand (for example with [itskyedo/woff2-encoder](https://github.com/itskyedo/woff2-encoder)).

```js
// promise-based utility to load libraries using the good old <script> tag
const loadScript = (src) => new Promise((onload) => document.documentElement.append(
Object.assign(document.createElement('script'), {src, onload})
));

const buffer = //...same as previous example...

// load wawoff2 if needed, and wait (!) for it to be ready
if (!window.Module) {
const path = 'https://unpkg.com/[email protected]/build/decompress_binding.js'
const init = new Promise((done) => window.Module = { onRuntimeInitialized: done});
await loadScript(path).then(() => init);
}
// decompress before parsing
const font = opentype.parse(Module.decompress(await buffer));
import { parse } from 'opentype.js';
import decompress from 'woff2-encoder/decompress';

const woff2Buffer = await fetch('/fonts/my.woff2').then(r => r.arrayBuffer());
const { buffer } = await decompress(woff2Buffer);
const font = parse(buffer);
```
</details>

Expand Down