1. Products
  2.   Font
  3.   Node.js
  4.   OpenType.js
 
  

Free JavaScript Library for Fonts Parsing & Writing

Open Source JavaScript Library that allows to Parse, Read and write TrueType or OpenType fonts. It support Font Handling, Glyph Metrics & Text Layout inside JS apps.

In the field of software web development and digital typography, handling font files and creating seamless text rendering experiences can be a daunting task. However, working with OpenType fonts can be a daunting task, especially when it comes to accessing their advanced typographic features. This is where OpenType.js comes in – a revolutionary open-source library that makes it easy to work with OpenType fonts in the browser. One of the key benefits of OpenType.js is its ability to simplify the process of working with OpenType fonts. Unlike other libraries that require extensive knowledge of font technology, OpenType.js provides a straightforward API that makes it easy to get started.

OpenType.js is a JavaScript library that provides a simple and intuitive API for accessing the advanced typographic features of OpenType fonts. With OpenType.js, software developers can easily access features such as ligatures, alternate glyphs, variable fonts, and so on, allowing them to create complex and sophisticated typography designs. In addition to parsing and manipulating existing fonts, opentype.js also supports creating new font files from scratch. This feature is valuable for developers looking to generate custom fonts programmatically. Being a JavaScript library, opentype.js can be used in both client-side and server-side environments, making it a flexible choice for various projects. Overall, it is a powerful and versatile library that provides a simple and intuitive way to work with OpenType fonts in the browser.

Previous Next

Getting Started with Opentype.js

The recommend way to install OpenType.js is using NPM. Please use the following command for a smooth installation.

Install Opentype.js using npm

 npm install opentype.js

You can also install it manually; download the latest release files directly from GitHub repository.

Font Parsing inside Node.js Apps

One of the core features of OpenType.js is its ability to parse OpenType and TrueType font files with just a couple of lines of JavaScript code. This allows software developers to read and understand the structure of a font, including its glyphs, kerning information, and other essential data. In the following example, the Roboto Regular font is loaded from a TTF file. The library parses the font, making its data available for further manipulation.

How to Load and Parse Font from a TTF File inside Node.js??

const opentype = require('opentype.js');

// Load a font from a file
opentype.load('fonts/Roboto-Regular.ttf', function(err, font) {
    if (err) {
        console.error('Font could not be loaded:', err);
    } else {
        console.log('Font loaded successfully:', font);
        console.log('Number of glyphs:', font.numGlyphs);
    }
});

Access & Manipulate Font’s Glyph inside Node.js

The open source Node.js library OpenType.js allows software developers to access and manipulate individual glyphs within a font inside Node.js applications. Glyphs are the visual representations of characters in a font. With this feature, you can extract glyph paths, modify them, or even create new glyphs. Here is a code snippet loads a font, retrieves the glyph for the character "A," and then scales the glyph by 1.5 times its original size. The toPathData() method converts the glyph's path into SVG path data, which can be used for rendering or further manipulation.

How to Load a Font and Modify Glyph inside Node.js Apps?

const opentype = require('opentype.js');

// Load a font and modify a glyph
opentype.load('fonts/Roboto-Regular.ttf', function(err, font) {
    if (err) {
        console.error('Font could not be loaded:', err);
    } else {
        const glyph = font.charToGlyph('A');
        console.log('Original Glyph Path:', glyph.path.toPathData());

        // Modify the glyph path (e.g., scale it)
        glyph.path.scale(1.5);
        console.log('Scaled Glyph Path:', glyph.path.toPathData());
    }
});

Working with Font Tables via Node.js Library

OpenType.js gives developers access to the underlying font tables, which store detailed information about the font's design and metrics. This can be particularly useful for advanced font manipulations or analyses. The code snippet in the following example demonstrates how to access specific font tables, such as the head table (which contains the font header information) and the hhea table (which holds horizontal header metrics). These tables provide essential details that can be used for precise control over font rendering and layout.

How to Access Specific Font Tables and Font inside It using Node.js Library?

const opentype = require('opentype.js');

opentype.load('path/to/font.ttf', function(err, font) {
  if (err) {
    console.error('Font could not be loaded: ' + err);
  } else {
    // Access the 'head' table (font header)
    const headTable = font.tables.head;
    console.log('Units per Em: ' + headTable.unitsPerEm);

    // Access the 'hhea' table (horizontal header)
    const hheaTable = font.tables.hhea;
    console.log('Ascender: ' + hheaTable.ascender);
    console.log('Descender: ' + hheaTable.descender);
  }
});