Socket
Socket
Sign inDemoInstall

opentype.js

Package Overview
Dependencies
2
Maintainers
2
Versions
47
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.11.0 to 0.12.0

2

bower.json
{
"name": "opentype.js",
"version": "0.11.0",
"version": "0.12.0",
"main": "dist/opentype.js",

@@ -5,0 +5,0 @@ "keywords": [

{
"name": "opentype.js",
"description": "OpenType font parser",
"version": "0.11.0",
"version": "0.12.0",
"author": {

@@ -6,0 +6,0 @@ "name": "Frederik De Bleser",

@@ -1,30 +0,7 @@

opentype.js
===========
[![npm](https://img.shields.io/npm/v/opentype.js.svg)](https://www.npmjs.com/package/opentype.js)
[![Build Status](https://travis-ci.org/opentypejs/opentype.js.svg?branch=master)](https://travis-ci.org/opentypejs/opentype.js)
[![david-dm](https://david-dm.org/opentypejs/opentype.js.svg)](https://david-dm.org/opentypejs/opentype.js)
[![Gitter](https://badges.gitter.im/opentypejs/opentype.js.svg)](https://gitter.im/opentypejs/opentype.js)
# opentype.js · [![Build Status](https://img.shields.io/travis/opentypejs/opentype.js.svg?style=flat-square)](https://travis-ci.org/opentypejs/opentype.js) [![npm](https://img.shields.io/npm/v/opentype.js.svg?style=flat-square)](https://www.npmjs.com/package/opentype.js) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/opentypejs/opentype.js/blob/master/LICENSE) [![david-dm](https://david-dm.org/opentypejs/opentype.js.svg)](https://david-dm.org/opentypejs/opentype.js) [![Gitter](https://badges.gitter.im/opentypejs/opentype.js.svg)](https://gitter.im/opentypejs/opentype.js)
opentype.js is a JavaScript parser and writer for TrueType and OpenType fonts.
It gives you access to the <strong>letterforms</strong> of text from the browser or Node.js.
![Example of opentype.js](https://raw.github.com/opentypejs/opentype.js/master/g/hello-world.png)
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);
}
});
It gives you access to the <strong>letterforms</strong> of text from the browser or Node.js.
See [the project website](https://opentype.js.org/) for a live demo.

@@ -40,3 +17,3 @@

* Support for TrueType font hinting.
* Support rendering of arabic (See #359)
* Support arabic text rendering (See #359)
* Very efficient.

@@ -48,2 +25,8 @@ * Runs in the browser and Node.js.

### Using [npm](http://npmjs.org/) package manager
npm install opentype.js
OpenType.js uses ES6-style imports, so debugging it in Node.js requires running `npm run build` first. Use `npm run watch` to automatically rebuild when files change.
### Directly

@@ -54,12 +37,12 @@

### Using npm
### Using via a CDN
npm install --save opentype.js
To use via a CDN, include the following code in your html:
OpenType.js uses ES6-style imports, so debugging it in Node.js requires running `npm run build` first. Use `npm run watch` to automatically rebuild when files change.
<script src="https://cdn.jsdelivr.net/npm/opentype.js@latest/dist/opentype.min.js"></script>
### Using Bower (Deprecated [see official post](https://bower.io/blog/2017/how-to-migrate-away-from-bower/))
### Using Bower
To install using [Bower](https://bower.io/), enter the following command in your project directory:
To install using [Bower](http://bower.io/), enter the following command in your project directory:
bower install opentype.js

@@ -71,21 +54,28 @@

### Using via a CDN
To use via a CDN, include the following code in your html:
<script src="https://cdn.jsdelivr.net/npm/opentype.js@latest/dist/opentype.min.js"></script>
API
===
### Loading a font
![OpenType.js example Hello World](https://raw.github.com/opentypejs/opentype.js/master/g/hello-world.png)
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.
```javascript
opentype.load('fonts/Roboto-Black.ttf', function(err, font) {
if (err) {
alert('Could not load font: ' + err);
alert('Font could not be loaded: ' + err);
} else {
// Use your font here.
// Now let's display it on a canvas with id "canvas"
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);
}
});
```

@@ -110,3 +100,3 @@ If you already have an `ArrayBuffer`, you can use `opentype.parse(buffer)` to parse the buffer. This method always

on the font name.
```javascript
// Create the bézier paths for each of the glyphs.

@@ -141,2 +131,3 @@ // Note that the .notdef glyph is required.

font.download();
```

@@ -163,3 +154,3 @@ If you want to inspect the font, use `font.toTables()` to generate an object showing the data structures that map

* `kerning`: if true takes kerning information into account (default: true)
* `features`: an object with [OpenType feature tags](https://www.microsoft.com/typography/otspec/featuretags.htm) as keys, and a boolean value to enable each feature.
* `features`: an object with [OpenType feature tags](https://docs.microsoft.com/en-us/typography/opentype/spec/featuretags) as keys, and a boolean value to enable each feature.
Currently only ligature features "liga" and "rlig" are supported (default: true).

@@ -179,3 +170,3 @@ * `hinting`: if true uses TrueType font hinting if available (default: false).

* `kerning`: if true takes kerning information into account (default: true)
* `features`: an object with [OpenType feature tags](https://www.microsoft.com/typography/otspec/featuretags.htm) as keys, and a boolean value to enable each feature.
* `features`: an object with [OpenType feature tags](https://docs.microsoft.com/en-us/typography/opentype/spec/featuretags) as keys, and a boolean value to enable each feature.
Currently only ligature features "liga" and "rlig" are supported (default: true).

@@ -275,3 +266,3 @@ * `hinting`: if true uses TrueType font hinting if available (default: false).

Convert the Path to a string of path data instructions.
See http://www.w3.org/TR/SVG/paths.html#PathData
See https://www.w3.org/TR/SVG/paths.html#PathData
* `decimalPlaces`: The amount of decimal places for floating-point values. (default: 2)

@@ -290,6 +281,13 @@

Planned
=======
* Support for contextual alternates.
## Versioning
We use [SemVer](https://semver.org/) for versioning.
## License
MIT
Thanks

@@ -299,9 +297,9 @@ ======

* [pdf.js](http://mozilla.github.io/pdf.js/): for an awesome implementation of font parsing in the browser.
* [FreeType](http://www.freetype.org/): for the nitty-gritty details and filling in the gaps when the spec was incomplete.
* [ttf.js](http://ynakajima.github.io/ttf.js/demo/glyflist/): for hints about the TrueType parsing code.
* [pdf.js](https://mozilla.github.io/pdf.js/): for an awesome implementation of font parsing in the browser.
* [FreeType](https://www.freetype.org/): for the nitty-gritty details and filling in the gaps when the spec was incomplete.
* [ttf.js](https://ynakajima.github.io/ttf.js/demo/glyflist/): for hints about the TrueType parsing code.
* [CFF-glyphlet-fonts](https://pomax.github.io/CFF-glyphlet-fonts/): for a great explanation/implementation of CFF font writing.
* [tiny-inflate](https://github.com/devongovett/tiny-inflate): for WOFF decompression.
* [Microsoft Typography](https://www.microsoft.com/typography/OTSPEC/otff.htm): the go-to reference for all things OpenType.
* [tiny-inflate](https://github.com/foliojs/tiny-inflate): for WOFF decompression.
* [Microsoft Typography](https://docs.microsoft.com/en-us/typography/opentype/spec/otff): the go-to reference for all things OpenType.
* [Adobe Compact Font Format spec](http://download.microsoft.com/download/8/0/1/801a191c-029d-4af3-9642-555f6fe514ee/cff.pdf) and the [Adobe Type 2 Charstring spec](http://download.microsoft.com/download/8/0/1/801a191c-029d-4af3-9642-555f6fe514ee/type2.pdf): explains the data structures and commands for the CFF glyph format.
* All contributing authors mentioned in the [AUTHORS](https://github.com/opentypejs/opentype.js/blob/master/AUTHORS) file.
* All contributing authors mentioned in the [AUTHORS](https://github.com/opentypejs/opentype.js/blob/master/AUTHORS.md) file.

@@ -0,1 +1,10 @@

0.12.0 (April 17, 2019)
=====================
* Fix Glyph.getPath() issue (PR #362, fixes #363) (thanks @solomancode!)
* Update README (PR #377) (thanks @jolg42!)
0.11.0 (October 22, 2018)
=====================
* Support Arabic text rendering (PR #359, fixes #46) (thanks @solomancode!)
0.10.0 (August 14, 2018)

@@ -2,0 +11,0 @@ =====================

@@ -246,7 +246,3 @@ // Glyph encoding

/**
* @alias opentype.addGlyphNames
* @param {opentype.Font}
*/
function addGlyphNames(font) {
function addGlyphNamesAll(font) {
let glyph;

@@ -277,2 +273,34 @@ const glyphIndexMap = font.tables.cmap.glyphIndexMap;

function addGlyphNamesToUnicodeMap(font) {
font._IndexToUnicodeMap = {};
const glyphIndexMap = font.tables.cmap.glyphIndexMap;
const charCodes = Object.keys(glyphIndexMap);
for (let i = 0; i < charCodes.length; i += 1) {
const c = charCodes[i];
let glyphIndex = glyphIndexMap[c];
if (font._IndexToUnicodeMap[glyphIndex] === undefined) {
font._IndexToUnicodeMap[glyphIndex] = {
unicodes: [parseInt(c)]
};
} else {
font._IndexToUnicodeMap[glyphIndex].unicodes.push(parseInt(c));
}
}
}
/**
* @alias opentype.addGlyphNames
* @param {opentype.Font}
* @param {Object}
*/
function addGlyphNames(font, opt) {
if (opt.lowMemory) {
addGlyphNamesToUnicodeMap(font);
} else {
addGlyphNamesAll(font);
}
}
export {

@@ -279,0 +307,0 @@ cffStandardStrings,

@@ -98,2 +98,6 @@ // The Font object

// needed for low memory mode only.
this._push = null;
this._hmtxTableData = {};
Object.defineProperty(this, 'hinting', {

@@ -100,0 +104,0 @@ get: function() {

@@ -150,3 +150,3 @@ // The Glyph object

commands = this.path.commands;
const scale = 1 / this.path.unitsPerEm * fontSize;
const scale = 1 / (this.path.unitsPerEm || 1000) * fontSize;
if (xScale === undefined) xScale = scale;

@@ -153,0 +153,0 @@ if (yScale === undefined) yScale = scale;

@@ -35,3 +35,5 @@ // The GlyphSet object

for (let i = 0; i < glyphs.length; i++) {
this.glyphs[i] = glyphs[i];
const glyph = glyphs[i];
glyph.path.unitsPerEm = font.unitsPerEm;
this.glyphs[i] = glyph;
}

@@ -48,4 +50,33 @@ }

GlyphSet.prototype.get = function(index) {
if (typeof this.glyphs[index] === 'function') {
this.glyphs[index] = this.glyphs[index]();
// this.glyphs[index] is 'undefined' when low memory mode is on. glyph is pushed on request only.
if (this.glyphs[index] === undefined) {
this.font._push(index);
if (typeof this.glyphs[index] === 'function') {
this.glyphs[index] = this.glyphs[index]();
}
let glyph = this.glyphs[index];
let unicodeObj = this.font._IndexToUnicodeMap[index];
if (unicodeObj) {
for (let j = 0; j < unicodeObj.unicodes.length; j++)
glyph.addUnicode(unicodeObj.unicodes[j]);
}
if (this.font.cffEncoding) {
if (this.font.isCIDFont) {
glyph.name = 'gid' + index;
} else {
glyph.name = this.font.cffEncoding.charset[index];
}
} else if (this.font.glyphNames.names) {
glyph.name = this.font.glyphNames.glyphIndexToName(index);
}
this.glyphs[index].advanceWidth = this.font._hmtxTableData[index].advanceWidth;
this.glyphs[index].leftSideBearing = this.font._hmtxTableData[index].leftSideBearing;
} else {
if (typeof this.glyphs[index] === 'function') {
this.glyphs[index] = this.glyphs[index]();
}
}

@@ -52,0 +83,0 @@

@@ -167,5 +167,8 @@ // opentype.js

* @param {ArrayBuffer}
* @param {Object} opt - options for parsing
* @return {opentype.Font}
*/
function parseBuffer(buffer) {
function parseBuffer(buffer, opt) {
opt = (opt === undefined || opt === null) ? {} : opt;
let indexToLocFormat;

@@ -318,6 +321,6 @@ let ltagTable;

const glyfTable = uncompressTable(data, glyfTableEntry);
font.glyphs = glyf.parse(glyfTable.data, glyfTable.offset, locaOffsets, font);
font.glyphs = glyf.parse(glyfTable.data, glyfTable.offset, locaOffsets, font, opt);
} else if (cffTableEntry) {
const cffTable = uncompressTable(data, cffTableEntry);
cff.parse(cffTable.data, cffTable.offset, font);
cff.parse(cffTable.data, cffTable.offset, font, opt);
} else {

@@ -328,4 +331,4 @@ throw new Error('Font doesn\'t contain TrueType or CFF outlines.');

const hmtxTable = uncompressTable(data, hmtxTableEntry);
hmtx.parse(hmtxTable.data, hmtxTable.offset, font.numberOfHMetrics, font.numGlyphs, font.glyphs);
addGlyphNames(font);
hmtx.parse(font, hmtxTable.data, hmtxTable.offset, font.numberOfHMetrics, font.numGlyphs, font.glyphs, opt);
addGlyphNames(font, opt);

@@ -374,3 +377,3 @@ if (kernTableEntry) {

*/
function load(url, callback) {
function load(url, callback, opt) {
const isNode = typeof window === 'undefined';

@@ -384,3 +387,3 @@ const loadFn = isNode ? loadFromFile : loadFromUrl;

try {
font = parseBuffer(arrayBuffer);
font = parseBuffer(arrayBuffer, opt);
} catch (e) {

@@ -398,8 +401,9 @@ return callback(e, null);

* @param {string} url - The URL of the font to load.
* @param {Object} opt - opt.lowMemory
* @return {opentype.Font}
*/
function loadSync(url) {
function loadSync(url, opt) {
const fs = require('fs');
const buffer = fs.readFileSync(url);
return parseBuffer(nodeBufferToArrayBuffer(buffer));
return parseBuffer(nodeBufferToArrayBuffer(buffer), opt);
}

@@ -406,0 +410,0 @@

@@ -83,2 +83,39 @@ // The `CFF` table contains the glyph outlines in PostScript format.

function parseCFFIndexLowMemory(data, start) {
const offsets = [];
const count = parse.getCard16(data, start);
let objectOffset;
let endOffset;
if (count !== 0) {
const offsetSize = parse.getByte(data, start + 2);
objectOffset = start + ((count + 1) * offsetSize) + 2;
let pos = start + 3;
for (let i = 0; i < count + 1; i += 1) {
offsets.push(parse.getOffset(data, pos, offsetSize));
pos += offsetSize;
}
// The total size of the index array is 4 header bytes + the value of the last offset.
endOffset = objectOffset + offsets[count];
} else {
endOffset = start + 2;
}
return {offsets: offsets, startOffset: start, endOffset: endOffset};
}
function getCffIndexObject(i, offsets, data, start, conversionFn) {
const count = parse.getCard16(data, start);
let objectOffset = 0;
if (count !== 0) {
const offsetSize = parse.getByte(data, start + 2);
objectOffset = start + ((count + 1) * offsetSize) + 2;
}
let value = parse.getBytes(data, objectOffset + offsets[i], objectOffset + offsets[i + 1]);
if (conversionFn) {
value = conversionFn(value);
}
return value;
}
// Parse a `CFF` DICT real value.

@@ -913,3 +950,3 @@ function parseFloatOperand(parser) {

// Parse the `CFF` table, which contains the glyph outlines in PostScript format.
function parseCFFTable(data, start, font) {
function parseCFFTable(data, start, font, opt) {
font.tables.cff = {};

@@ -971,4 +1008,10 @@ const header = parseCFFHeader(data, start);

// Offsets in the top dict are relative to the beginning of the CFF data, so add the CFF start offset.
const charStringsIndex = parseCFFIndex(data, start + topDict.charStrings);
font.nGlyphs = charStringsIndex.objects.length;
let charStringsIndex;
if (opt.lowMemory) {
charStringsIndex = parseCFFIndexLowMemory(data, start + topDict.charStrings);
font.nGlyphs = charStringsIndex.offsets.length;
} else {
charStringsIndex = parseCFFIndex(data, start + topDict.charStrings);
font.nGlyphs = charStringsIndex.objects.length;
}

@@ -990,5 +1033,12 @@ const charset = parseCFFCharset(data, start + topDict.charset, font.nGlyphs, stringIndex.objects);

font.glyphs = new glyphset.GlyphSet(font);
for (let i = 0; i < font.nGlyphs; i += 1) {
const charString = charStringsIndex.objects[i];
font.glyphs.push(i, glyphset.cffGlyphLoader(font, i, parseCFFCharstring, charString));
if (opt.lowMemory) {
font._push = function(i) {
const charString = getCffIndexObject(i, charStringsIndex.offsets, data, start + topDict.charStrings);
font.glyphs.push(i, glyphset.cffGlyphLoader(font, i, parseCFFCharstring, charString));
};
} else {
for (let i = 0; i < font.nGlyphs; i += 1) {
const charString = charStringsIndex.objects[i];
font.glyphs.push(i, glyphset.cffGlyphLoader(font, i, parseCFFCharstring, charString));
}
}

@@ -995,0 +1045,0 @@ }

@@ -308,4 +308,3 @@ // The `glyf` table describes the glyphs in TrueType outline format.

// Parse all the glyphs according to the offsets from the `loca` table.
function parseGlyfTable(data, start, loca, font) {
function parseGlyfTableAll(data, start, loca, font) {
const glyphs = new glyphset.GlyphSet(font);

@@ -327,2 +326,26 @@

export default { getPath, parse: parseGlyfTable };
function parseGlyfTableOnLowMemory(data, start, loca, font) {
const glyphs = new glyphset.GlyphSet(font);
font._push = function(i) {
const offset = loca[i];
const nextOffset = loca[i + 1];
if (offset !== nextOffset) {
glyphs.push(i, glyphset.ttfGlyphLoader(font, i, parseGlyph, data, start + offset, buildPath));
} else {
glyphs.push(i, glyphset.glyphLoader(font, i));
}
};
return glyphs;
}
// Parse all the glyphs according to the offsets from the `loca` table.
function parseGlyfTable(data, start, loca, font, opt) {
if (opt.lowMemory)
return parseGlyfTableOnLowMemory(data, start, loca, font);
else
return parseGlyfTableAll(data, start, loca, font);
}
export default { getPath, parse: parseGlyfTable};

@@ -7,5 +7,3 @@ // The `hmtx` table contains the horizontal metrics for all glyphs.

// Parse the `hmtx` table, which contains the horizontal metrics for all glyphs.
// This function augments the glyph array, adding the advanceWidth and leftSideBearing to each glyph.
function parseHmtxTable(data, start, numMetrics, numGlyphs, glyphs) {
function parseHmtxTableAll(data, start, numMetrics, numGlyphs, glyphs) {
let advanceWidth;

@@ -27,2 +25,31 @@ let leftSideBearing;

function parseHmtxTableOnLowMemory(font, data, start, numMetrics, numGlyphs) {
font._hmtxTableData = {};
let advanceWidth;
let leftSideBearing;
const p = new parse.Parser(data, start);
for (let i = 0; i < numGlyphs; i += 1) {
// If the font is monospaced, only one entry is needed. This last entry applies to all subsequent glyphs.
if (i < numMetrics) {
advanceWidth = p.parseUShort();
leftSideBearing = p.parseShort();
}
font._hmtxTableData[i] = {
advanceWidth: advanceWidth,
leftSideBearing: leftSideBearing
};
}
}
// Parse the `hmtx` table, which contains the horizontal metrics for all glyphs.
// This function augments the glyph array, adding the advanceWidth and leftSideBearing to each glyph.
function parseHmtxTable(font, data, start, numMetrics, numGlyphs, glyphs, opt) {
if (opt.lowMemory)
parseHmtxTableOnLowMemory(font, data, start, numMetrics, numGlyphs);
else
parseHmtxTableAll(data, start, numMetrics, numGlyphs, glyphs);
}
function makeHmtxTable(glyphs) {

@@ -29,0 +56,0 @@ const t = new table.Table('hmtx', []);

@@ -20,1 +20,20 @@ import assert from 'assert';

});
describe('opentype.js dist on low memory mode', function() {
it('can work with the uncompressed distribution', function() {
var opentype = require('../dist/opentype');
var font = opentype.loadSync('./fonts/Roboto-Black.ttf', {lowMemory: true});
assert.deepEqual(font.names.fontFamily, {en: 'Roboto Black'});
assert.equal(font.unitsPerEm, 2048);
assert.equal(font.glyphs.length, 0);
});
it('can work with the compressed dist files', function() {
var opentype = require('../dist/opentype.min');
var font = opentype.loadSync('./fonts/Roboto-Black.ttf', {lowMemory: true});
assert.deepEqual(font.names.fontFamily, {en: 'Roboto Black'});
assert.equal(font.unitsPerEm, 2048);
assert.equal(font.glyphs.length, 0);
});
});

@@ -47,3 +47,11 @@ import assert from 'assert';

});
it('works on fonts with coverage table format 2 on low memory mode', function() {
const vibur = loadSync('./fonts/Vibur.woff', {lowMemory: true});
const glyphs = vibur.stringToGlyphs('er');
assert.equal(glyphs.length, 1);
assert.equal(glyphs[0].name, 'er');
});
});
});

@@ -72,1 +72,72 @@ import assert from 'assert';

});
describe('glyph.js on low memory mode', function() {
let opt = {lowMemory: true};
describe('lazy loading', function() {
let font;
let glyph;
before(function() {
font = loadSync('./fonts/Roboto-Black.ttf', opt);
glyph = font.charToGlyph('A');
});
it('lazily loads xMin', function() {
assert.equal(glyph.xMin, -3);
});
it('lazily loads xMax', function() {
assert.equal(glyph.xMax, 1399);
});
it('lazily loads yMin', function() {
assert.equal(glyph.yMin, 0);
});
it('lazily loads yMax', function() {
assert.equal(glyph.yMax, 1456);
});
it('lazily loads numberOfContours', function() {
assert.equal(glyph.numberOfContours, 2);
});
});
describe('bounding box', function() {
let trueTypeFont;
let openTypeFont;
before(function() {
trueTypeFont = loadSync('./fonts/Roboto-Black.ttf', opt);
openTypeFont = loadSync('./fonts/FiraSansMedium.woff', opt);
});
it('calculates a box for a linear shape', function() {
const glyph = trueTypeFont.charToGlyph('A');
const box = glyph.getBoundingBox();
assert.equal(box.x1, -3);
assert.equal(box.y1, 0);
assert.equal(box.x2, 1399);
assert.equal(box.y2, 1456);
});
it('calculates a box for a quadratic shape', function() {
const glyph = trueTypeFont.charToGlyph('Q');
const box = glyph.getBoundingBox();
assert.equal(box.x1, 72);
assert.equal(box.y1, -266);
assert.equal(box.x2, 1345);
assert.equal(box.y2, 1476);
});
it('calculates a box for a bezier shape', function() {
const glyph = openTypeFont.charToGlyph('Q');
const box = glyph.getBoundingBox();
assert.equal(box.x1, 62);
assert.equal(box.y1, -103);
assert.equal(box.x2, 688);
assert.equal(box.y2, 701);
});
});
});

@@ -78,1 +78,78 @@ import assert from 'assert';

});
describe('opentype.js on low memory mode', function() {
const opt = { lowMemory: true };
it('can load a TrueType font', function() {
const font = loadSync('./fonts/Roboto-Black.ttf', opt);
assert.deepEqual(font.names.fontFamily, {en: 'Roboto Black'});
assert.equal(font.unitsPerEm, 2048);
assert.equal(font.glyphs.length, 0);
const aGlyph = font.charToGlyph('A');
assert.equal(aGlyph.unicode, 65);
assert.equal(aGlyph.path.commands.length, 15);
});
it('can load a OpenType/CFF font', function() {
const font = loadSync('./fonts/FiraSansOT-Medium.otf', opt);
assert.deepEqual(font.names.fontFamily, {en: 'Fira Sans OT Medium'});
assert.equal(font.unitsPerEm, 1000);
assert.equal(font.glyphs.length, 0);
const aGlyph = font.charToGlyph('A');
assert.equal(aGlyph.name, 'A');
assert.equal(aGlyph.unicode, 65);
assert.equal(aGlyph.path.commands.length, 14);
});
it('can load a CID-keyed font', function() {
const font = loadSync('./fonts/FDArrayTest257.otf', opt);
assert.deepEqual(font.names.fontFamily, {en: 'FDArray Test 257'});
assert.deepEqual(font.tables.cff.topDict.ros, ['Adobe', 'Identity', 0]);
assert.equal(font.tables.cff.topDict._fdArray.length, 256);
assert.equal(font.tables.cff.topDict._fdSelect[0], 0);
assert.equal(font.tables.cff.topDict._fdSelect[42], 41);
assert.equal(font.tables.cff.topDict._fdSelect[256], 255);
assert.equal(font.unitsPerEm, 1000);
assert.equal(font.glyphs.length, 0);
const aGlyph = font.glyphs.get(2);
assert.equal(aGlyph.name, 'gid2');
assert.equal(aGlyph.unicode, 1);
assert.equal(aGlyph.path.commands.length, 24);
});
it('can load a WOFF/CFF font', function() {
const font = loadSync('./fonts/FiraSansMedium.woff', opt);
assert.deepEqual(font.names.fontFamily, {en: 'Fira Sans OT'});
assert.equal(font.unitsPerEm, 1000);
assert.equal(font.glyphs.length, 0);
const aGlyph = font.charToGlyph('A');
assert.equal(aGlyph.name, 'A');
assert.equal(aGlyph.unicode, 65);
assert.equal(aGlyph.path.commands.length, 14);
});
it('handles a parseBuffer error', function(done) {
load('./fonts/badfont.ttf', function(err) {
if (err) {
done();
}
});
}, opt);
it('throws an error when advanceWidth is not set', function() {
const notdefGlyph = new Glyph({
name: '.notdef',
unicode: 0,
path: new Path()
});
const font = new Font({
familyName: 'MyFont',
styleName: 'Medium',
unitsPerEm: 1000,
ascender: 800,
descender: -200,
glyphs: [notdefGlyph]
});
assert.throws(function() { font.toArrayBuffer(); }, /advanceWidth is not a number/);
});
});

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc