It gives you access to the letterforms of text from the browser or Node.js.
See https://opentype.js.org/ for a live demo.
glyf
and PostScript cff
outlines)Select one of the following sources in the next example:
<!-- using global declaration -->
<script src="https://your.favorite.cdn/opentype.js"></script>
<script>opentype.parse(...)</script>
<!-- using module declaration (need full path) -->
<script type=module>
import { parse } from "https://unpkg.com/opentype.js/dist/opentype.mjs";
parse(...);
</script>
npm install opentype.js
const opentype = require('opentype.js');
import opentype from 'opentype.js'
import { load } from 'opentype.js'
Using TypeScript? See this example
If you plan on improving or debugging opentype.js, you can:
git clone git://github.com/yourname/opentype.js.git
cd opentype.js
npm install
npm run build
npm run start
and navigate to the /docs
foldernpm run test
This is done in two steps: first, we load the font file into an ArrayBuffer
...
// either from an URL
const buffer = fetch('/fonts/my.woff').then(res => res.arrayBuffer());
// ... or from filesystem (node)
const buffer = require('fs').promises.readFile('./my.woff');
// ... or from an <input type=file id=myfile> (browser)
const buffer = document.getElementById('myfile').files[0].arrayBuffer();
... then we .parse()
it into a Font
instance
// if running in async context:
const font = opentype.parse(await buffer);
console.log(font);
// if not running in async context:
buffer.then(data => {
const font = opentype.parse(data);
console.log(font);
})