GithubHelp home page GithubHelp logo

stanhuff / opentype.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from opentypejs/opentype.js

0.0 2.0 0.0 1.52 MB

Read and write OpenType fonts using JavaScript.

Home Page: http://opentype.js.org/

License: MIT License

JavaScript 93.08% HTML 6.17% CSS 0.76%

opentype.js's Introduction

opentype.js

opentype.js is a JavaScript parser and writer for TrueType and OpenType fonts.

It gives you access to the letterforms of text from the browser or node.js.

Example of opentype.js

Here's an example. We load a font, then display it on a canvas with id "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);
    }
});

See the project website for a live demo.

Features

  • Create a bézier path out of a piece of text.
  • Support for composite glyphs (accented letters).
  • Support for WOFF, OTF, TTF (both with TrueType glyf and PostScript cff outlines)
  • Support for kerning (Using GPOS or the kern table).
  • Very efficient.
  • Runs in the browser and node.js.

Installation

Directly

Download the latest ZIP and grab the files in the dist folder. These are compiled.

Using Bower

To install using Bower, enter the following command in your project directory:

bower install opentype.js

You can then include them in your scripts using:

<script src="/bower_components/opentype.js/dist/opentype.js"></script>

Using Browserify

To install using Browserify, enter the following command in your project directory:

npm install --save opentype.js

API

Loading a font

Use opentype.load(url, callback) to load a font from a URL. Since this method goes out the network, it is asynchronous. The callback gets (err, font) where font is a Font object. Check if the err is null before using the font.

opentype.load('fonts/Roboto-Black.ttf', function(err, font) {
    if (err) {
        alert('Could not load font: ' + err);
    } else {
        // Use your font here.
    }
});

If you already have an ArrayBuffer, you can use opentype.parse(buffer) to parse the buffer. This method always returns a Font, but check font.supported to see if the font is in a supported format. (Fonts can be marked unsupported if they have encoding tables we can't read).

var font = opentype.parse(myBuffer);

Loading a font synchronously (Node.js)

Use opentype.loadSync(url) to load a font from a file and return a Font object. Throws an error if the font could not be parsed. This only works in Node.js.

var font = opentype.loadSync('fonts/Roboto-Black.ttf');

Writing a font

Once you have a Font object (either by using opentype.load or by creating a new one from scratch) you can write it back out as a binary file.

In the browser, you can use Font.download() to instruct the browser to download a binary .OTF file. The name is based on the font name.

// Create the bézier paths for each of the glyphs.
// Note that the .notdef glyph is required.
var notdefGlyph = new opentype.Glyph({
    name: '.notdef',
    unicode: 0,
    advanceWidth: 650,
    path: new opentype.Path();
});

var aPath = new opentype.Path();
aPath.moveTo(100, 0);
aPath.lineTo(100, 700);
// more drawing instructions...
var aGlyph = new opentype.Glyph({
    name: 'A',
    unicode: 65,
    advanceWidth: 650,
    path: aPath
});

var glyphs = [notdefGlyph, aGlyph];
var font = new opentype.Font({
    familyName: 'OpenTypeSans',
    styleName: 'Medium',
    unitsPerEm: 1000,
    ascender: 800,
    descender: -200,
    glyphs: glyphs});
font.download();

If you want to inspect the font, use font.toTables() to generate an object showing the data structures that map directly to binary values. If you want to get an ArrayBuffer, use font.toArrayBuffer().

The Font object

A Font represents a loaded OpenType font file. It contains a set of glyphs and methods to draw text on a drawing context, or to get a path representing the text.

  • glyphs: an indexed list of Glyph objects.
  • unitsPerEm: X/Y coordinates in fonts are stored as integers. This value determines the size of the grid. Common values are 2048 and 4096.
  • ascender: Distance from baseline of highest ascender. In font units, not pixels.
  • descender: Distance from baseline of lowest descender. In font units, not pixels.

Font.getPath(text, x, y, fontSize, options)

Create a Path that represents the given text.

  • x: Horizontal position of the beginning of the text. (default: 0)
  • y: Vertical position of the baseline of the text. (default: 0)
  • fontSize: Size of the text in pixels (default: 72).

Options is an optional object containing:

  • kerning: if true takes kerning information into account (default: true)

Note: there is also Font.getPaths with the same arguments which returns a list of Paths.

Font.draw(ctx, text, x, y, fontSize, options)

Create a Path that represents the given text.

  • ctx: A 2D drawing context, like Canvas.
  • x: Horizontal position of the beginning of the text. (default: 0)
  • y: Vertical position of the baseline of the text. (default: 0)
  • fontSize: Size of the text in pixels (default: 72).

Options is an optional object containing:

  • kerning: if true takes kerning information into account (default: true)

Font.drawPoints(ctx, text, x, y, fontSize, options)

Draw the points of all glyphs in the text. On-curve points will be drawn in blue, off-curve points will be drawn in red. The arguments are the same as Font.draw.

Font.drawMetrics(ctx, text, x, y, fontSize, options)

Draw lines indicating important font measurements for all glyphs in the text. Black lines indicate the origin of the coordinate system (point 0,0). Blue lines indicate the glyph bounding box. Green line indicates the advance width of the glyph.

Font.stringToGlyphs(string)

Convert the string to a list of glyph objects. Note that there is no strict 1-to-1 correspondence between the string and glyph list due to possible substitutions such as ligatures. The list of returned glyphs can be larger or smaller than the length of the given string.

Font.charToGlyph(char)

Convert the character to a Glyph object. Returns null if the glyph could not be found. Note that this function assumes that there is a one-to-one mapping between the given character and a glyph; for complex scripts this might not be the case.

Font.getKerningValue(leftGlyph, rightGlyph)

Retrieve the value of the kerning pair between the left glyph (or its index) and the right glyph (or its index). If no kerning pair is found, return 0. The kerning value gets added to the advance width when calculating the spacing between glyphs.

The Glyph object

A Glyph is an individual mark that often corresponds to a character. Some glyphs, such as ligatures, are a combination of many characters. Glyphs are the basic building blocks of a font.

  • font: A reference to the Font object.
  • name: The glyph name (e.g. "Aring", "five")
  • unicode: The primary unicode value of this glyph (can be undefined).
  • unicodes: The list of unicode values for this glyph (most of the time this will be 1, can also be empty).
  • index: The index number of the glyph.
  • advanceWidth: The width to advance the pen when drawing this glyph.
  • xMin, yMin, xMax, yMax: The bounding box of the glyph.
  • path: The raw, unscaled path of the glyph.
Glyph.getPath(x, y, fontSize)

Get a scaled glyph Path object we can draw on a drawing context.

  • x: Horizontal position of the glyph. (default: 0)
  • y: Vertical position of the baseline of the glyph. (default: 0)
  • fontSize: Font size in pixels (default: 72).
Glyph.draw(ctx, x, y, fontSize)

Draw the glyph on the given context.

  • ctx: The drawing context.
  • x: Horizontal position of the glyph. (default: 0)
  • y: Vertical position of the baseline of the glyph. (default: 0)
  • fontSize: Font size, in pixels (default: 72).
Glyph.drawPoints(ctx, x, y, fontSize)

Draw the points of the glyph on the given context. On-curve points will be drawn in blue, off-curve points will be drawn in red. The arguments are the same as Glyph.draw.

Glyph.drawMetrics(ctx, x, y, fontSize)

Draw lines indicating important font measurements for all glyphs in the text. Black lines indicate the origin of the coordinate system (point 0,0). Blue lines indicate the glyph bounding box. Green line indicates the advance width of the glyph. The arguments are the same as Glyph.draw.

The Path object

Once you have a path through Font.getPath or Glyph.getPath, you can use it.

  • commands: The path commands. Each command is a dictionary containing a type and coordinates. See below for examples.
  • fill: The fill color of the Path. Color is a string representing a CSS color. (default: 'black')
  • stroke: The stroke color of the Path. Color is a string representing a CSS color. (default: null: the path will not be stroked)
  • strokeWidth: The line thickness of the Path. (default: 1, but since the stroke is null no stroke will be drawn)
Path.draw(ctx)

Draw the path on the given 2D context. This uses the fill, stroke and strokeWidth properties of the Path object.

  • ctx: The drawing context.
Path.toPathData(decimalPlaces)

Convert the Path to a string of path data instructions. See http://www.w3.org/TR/SVG/paths.html#PathData

  • decimalPlaces: The amount of decimal places for floating-point values. (default: 2)
Path.toSVG(decimalPlaces)

Convert the path to a SVG <path> element, as a string.

  • decimalPlaces: The amount of decimal places for floating-point values. (default: 2)

Path commands

  • Move To: Move to a new position. This creates a new contour. Example: {type: 'M', x: 100, y: 200}
  • Line To: Draw a line from the previous position to the given coordinate. Example: {type: 'L', x: 100, y: 200}
  • Curve To: Draw a bézier curve from the current position to the given coordinate. Example: {type: 'C', x1: 0, y1: 50, x2: 100, y2: 200, x: 100, y: 200}
  • Quad To: Draw a quadratic bézier curve from the current position to the given coordinate. Example: {type: 'Q', x1: 0, y1: 50, x: 100, y: 200}
  • Close: Close the path. If stroked, this will draw a line from the first to the last point of the contour. Example: {type: 'Z'}

Planned

  • Support for ligatures and contextual alternates.

Thanks

I would like to acknowledge the work of others without which opentype.js wouldn't be possible:

opentype.js's People

Contributors

fdb avatar brawer avatar fpirsch avatar moyogo avatar bulyshko avatar miguelsousa avatar pomax avatar louisremi avatar eskimoblood avatar joshmarinacci avatar sunng87 avatar tshinnic avatar amithalb avatar dominiklessel avatar shrhdk avatar jardakotesovec avatar jolg42 avatar kkaefer avatar vicapow avatar nktpro avatar zswang avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.