Socket
Socket
Sign inDemoInstall

ttfjs

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ttfjs

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


Version published
Weekly downloads
127
increased by62.82%
Maintainers
1
Weekly downloads
 
Created

Readme

Source

ttfjs

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

NPM Dependency Status

Specification Coverage

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

Usage

var TTFFont = require('ttfjs')

new TTFFont(buffer)

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

.stringWidth(str, size)

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

.lineHeight(size, [includeGap])

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

.lineDescent(size)

Return the line descent for the given font size.

.clone()

Returns a clone of the font object.

.save()

Encodes the font into an ArrayBuffer.

.subset([opts])

Creates a Subset for the current font.

.subset([opts]) | new TTFFont.Subset(font, [opts])

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)
.use(chars)

Add the given characters to the subset.

.encode(str)

Encode the given string using the current remapping.

.embed()

Embed the current used characters.

.save()

Encodes the subset into an ArrayBuffer.

Example

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
}

MIT License

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.

Keywords

FAQs

Package last updated on 20 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc