读写OpenType字体的JS库:opentype.js
jopen
10年前
opentype.js是TrueType 和 OpenType 字体的javascript读写器。它可以让你从浏览器或node.js中获取文字的字母形式。
以下是一个示例。加载一个文字,然后将其展示在一个id="canvas"的画布上Canvas。
opentype.load('fonts/Roboto-Black.ttf', function (err, font) { if (err) { alert('Font could not be loaded: ' + err); } else { var ctx = document.getElementById('canvas').getContext('2d'); // Construct a Path object containing the letter shapes of the given text. // The other parameters are x, y and fontSize. // Note that y is the position of the baseline. var path = font.getPath('Hello, World!', 0, 150, 72); // If you just want to draw the text you can also use font.draw(ctx, text, x, y, fontSize). path.draw(ctx); } }
在线示例请查看: the project website
特性
- Create a bézier path out of a piece of text.
- Support for composite glyphs (accented letters).
- Support for OpenType (glyf) and PostScript (cff) shapes.
- Support for kerning (Using GPOS or the kern table).
- Very efficient.
- Runs in the browser and node.js.