ttfjs

TTFjs is a TrueType font parser entirely written in JavaScript and compatible to both Node.js and the Browser.

ttfjs

TTFjs is a TrueType font parser entirely written in JavaScript and compatible to both Node.js and the Browser.

The following tables are implemented: cmap (currently only format 4), glyf (glyphs are not actually decomposed, only rewritten), head, hhea, hmtx, loca, maxp, name, os2, post (currently only format 3).

TrueType Font Specification: Apple, Microsoft

var TTFFont = require('ttfjs')

Create a new font instance from the provided buffer (can be a Node Buffer or an ArrayBuffer).

Returns the width for the given string in the given font size.

Returns the line height for the given font size with line gap or without.

Return the line descent for the given font size.

Returns a clone of the font object.

Encodes the font into an ArrayBuffer.

Creates a Subset for the current font.

Creates a Subset for the current font.

Options:

  • remap - (default: true) whether to remap char codes
  • trimNames - (default: false) whether trim localized names (reduces file size)

Add the given characters to the subset.

Encode the given string using the current remapping.

Embed the current used characters.

Encodes the subset into an ArrayBuffer.

var fs = require('fs')
var TTFFont = require('ttfjs')
 
var font = new TTFFont(fs.readFileSync(__dirname + '/Corbel.ttf'))
var subset = font.subset()
 
subset.use('abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890')
console.log(subset.encode('abcdefghijklmnopqrstuvwxyz'))
 
subset.embed()
 
fs.writeFileSync('./test.ttf', toBuffer(subset.save()), { encoding: 'binary' })
 
function toBuffer(ab) {
  var buffer = new Buffer(ab.byteLength)
  var view = new Uint8Array(ab)
  for (var i = 0; i < buffer.length; ++i) {
    buffer[i] = view[i]
  }
  return buffer
}

Copyright (c) 2013-2015 Markus Ast

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.