Skip to content

Commit

Permalink
Remove use of spread operator (for React Native Android); Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopding committed Dec 20, 2018
1 parent c39585e commit f674bf2
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 72 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pdf-lib/fontkit",
"version": "0.0.0",
"version": "0.0.1",
"description": "An advanced font engine for Node and the browser",
"main": "dist/fontkit.umd.js",
"unpkg": "dist/fontkit.umd.min.js",
Expand All @@ -9,7 +9,7 @@
"scripts": {
"make": "node Makefile.js",
"clean": "node Makefile.js clean",
"test": "mocha",
"test": "MODULE_TYPE=commonjs mocha",
"coverage": "cross-env BABEL_ENV=cover nyc mocha"
},
"dependencies": {
Expand All @@ -32,6 +32,7 @@
"@babel/plugin-proposal-class-properties": "^7.2.1",
"@babel/plugin-proposal-decorators": "^7.2.2",
"@babel/preset-env": "^7.2.0",
"@babel/register": "^7.0.0",
"babel-plugin-istanbul": "^4.1.3",
"codepoints": "^1.2.0",
"concat-stream": "^1.4.6",
Expand Down
14 changes: 12 additions & 2 deletions src/opentype/shapers/IndicShaper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ import base64DeflatedTrie from './trieIndic.json';

// Trie is serialized as a Buffer in node, but here
// we may be running in a browser so we make an Uint8Array
const indicMachine = JSON.parse(String.fromCharCode(...pako.inflate(base64.decode(base64DeflatedIndicMachine))));
const useData = JSON.parse(String.fromCharCode(...pako.inflate(base64.decode(base64DeflatedUseData))));
const indicMachine = JSON.parse(
String.fromCharCode.apply(
String,
pako.inflate(base64.decode(base64DeflatedIndicMachine)),
),
);
const useData = JSON.parse(
String.fromCharCode.apply(
String,
pako.inflate(base64.decode(base64DeflatedUseData)),
),
);
const trieData = pako.inflate(base64.decode(base64DeflatedTrie));

const {decompositions} = useData;
Expand Down
7 changes: 6 additions & 1 deletion src/opentype/shapers/UniversalShaper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import base64DeflatedTrie from './trieUse.json';

// Trie is serialized as a Buffer in node, but here
// we may be running in a browser so we make an Uint8Array
const useData = JSON.parse(String.fromCharCode(...pako.inflate(base64.decode(base64DeflatedUseData))));
const useData = JSON.parse(
String.fromCharCode.apply(
String,
pako.inflate(base64.decode(base64DeflatedUseData)),
),
);
const trieData = pako.inflate(base64.decode(base64DeflatedTrie));

const {categories, decompositions} = useData;
Expand Down
4 changes: 3 additions & 1 deletion test/directory.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fontkit from '../src';
import assert from 'assert';
import fs from 'fs';

describe('metadata', function() {
let font = fontkit.openSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
const fontData = fs.readFileSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
const font = fontkit.create(fontData);

it('decodes SFNT directory values correctly', function() {
let dir = font.directory;
Expand Down
30 changes: 22 additions & 8 deletions test/glyph_mapping.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fontkit from '../src';
import assert from 'assert';
import fs from 'fs';

describe('character to glyph mapping', function() {
describe('basic cmap handling', function() {
let font = fontkit.openSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
const fontData = fs.readFileSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
const font = fontkit.create(fontData);

it('should get characterSet', function() {
assert(Array.isArray(font.characterSet));
Expand All @@ -30,20 +32,25 @@ describe('character to glyph mapping', function() {
});

it('should support unicode variation selectors', function() {
let font = fontkit.openSync(__dirname + '/data/fonttest/TestCMAP14.otf');
const fontData = fs.readFileSync(__dirname + '/data/fonttest/TestCMAP14.otf');
const font = fontkit.create(fontData);

let glyphs = font.glyphsForString('\u{82a6}\u{82a6}\u{E0100}\u{82a6}\u{E0101}');
assert.deepEqual(glyphs.map(g => g.id), [1, 1, 2]);
});

it('should support legacy encodings when no unicode cmap is found', function() {
let font = fontkit.openSync(__dirname + '/data/fonttest/TestCMAPMacTurkish.ttf');
const fontData = fs.readFileSync(__dirname + '/data/fonttest/TestCMAPMacTurkish.ttf');
const font = fontkit.create(fontData);

let glyphs = font.glyphsForString("“ABÇĞIİÖŞÜ”");
assert.deepEqual(glyphs.map(g => g.id), [200, 34, 35, 126, 176, 42, 178, 140, 181, 145, 201]);
});
});

describe('opentype features', function() {
let font = fontkit.openSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.otf');
const fontData = fs.readFileSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.otf');
const font = fontkit.create(fontData);

it('should list available features', () =>
assert.deepEqual(font.availableFeatures, [
Expand Down Expand Up @@ -73,7 +80,8 @@ describe('character to glyph mapping', function() {
});

describe('AAT features', function() {
let font = fontkit.openSync(__dirname + '/data/Play/Play-Regular.ttf');
const fontData = fs.readFileSync(__dirname + '/data/Play/Play-Regular.ttf');
const font = fontkit.create(fontData);

it('should list available features', () => assert.deepEqual(font.availableFeatures, [ 'tnum', 'sups', 'subs', 'numr', 'onum', 'lnum', 'liga', 'kern' ]));

Expand All @@ -99,7 +107,9 @@ describe('character to glyph mapping', function() {
});

it('should apply indic reordering features', function() {
let f = fontkit.openSync(__dirname + '/data/Khmer/Khmer.ttf');
const fontData = fs.readFileSync(__dirname + '/data/Khmer/Khmer.ttf');
const font = fontkit.create(fontData);

let {glyphs} = f.layout('ខ្ញុំអាចញ៉ាំកញ្ចក់បាន ដោយគ្មានបញ្ហា');
assert.deepEqual(glyphs.map(g => g.id), [
45, 153, 177, 112, 248, 188, 49, 296, 44, 187, 149, 44, 117, 236, 188, 63, 3, 107,
Expand All @@ -118,13 +128,17 @@ describe('character to glyph mapping', function() {

describe('glyph id to strings', function () {
it('should return strings from cmap that map to a given glyph', function () {
let font = fontkit.openSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
const fontData = fs.readFileSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
const font = fontkit.create(fontData);

let strings = font.stringsForGlyph(68);
assert.deepEqual(strings, ['a']);
});

it('should return strings from AAT morx table that map to the given glyph', function () {
let font = fontkit.openSync(__dirname + '/data/Play/Play-Regular.ttf');
const fontData = fs.readFileSync(__dirname + '/data/Play/Play-Regular.ttf');
const font = fontkit.create(fontData);

let strings = font.stringsForGlyph(767);
assert.deepEqual(strings, ['ffi']);
});
Expand Down
10 changes: 7 additions & 3 deletions test/glyph_positioning.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import fontkit from '../src';
import assert from 'assert';
import fs from 'fs';

describe('glyph positioning', function() {
describe('basic positioning', function() {
let font = fontkit.openSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.otf');
const fontData = fs.readFileSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.otf');
const font = fontkit.create(fontData);

it('should get a glyph width', () => assert.equal(font.getGlyph(5).advanceWidth, 615));
});

describe('opentype positioning', function() {
let font = fontkit.openSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.otf');
const fontData = fs.readFileSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.otf');
const font = fontkit.create(fontData);

it('should apply opentype GPOS features', function() {
let {positions} = font.layout('Twitter');
Expand All @@ -23,7 +26,8 @@ describe('glyph positioning', function() {
});

describe('AAT features', function() {
let font = fontkit.openSync(__dirname + '/data/Play/Play-Regular.ttf');
const fontData = fs.readFileSync(__dirname + '/data/Play/Play-Regular.ttf');
const font = fontkit.create(fontData);

it('should apply kerning by default', function() {
let {positions} = font.layout('Twitter');
Expand Down
23 changes: 16 additions & 7 deletions test/glyphs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import fontkit from '../src';
import assert from 'assert';
import BBox from '../src/glyph/BBox';
import fs from 'fs';

describe('glyphs', function() {
describe('truetype glyphs', function() {
let font = fontkit.openSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
let mada = fontkit.openSync(__dirname + '/data/Mada/Mada-VF.ttf');
const fontData = fs.readFileSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
const font = fontkit.create(fontData);

const madaData = fs.readFileSync(__dirname + '/data/Mada/Mada-VF.ttf');
const mada = fontkit.create(fontData);

it('should get a TTFGlyph', function() {
let glyph = font.getGlyph(39); // D
Expand Down Expand Up @@ -64,7 +68,8 @@ describe('glyphs', function() {
});

describe('CFF glyphs', function() {
let font = fontkit.openSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.otf');
const fontData = fs.readFileSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.otf');
const font = fontkit.create(fontData);

it('should get a CFFGlyph', function() {
let glyph = font.getGlyph(5); // D
Expand Down Expand Up @@ -93,7 +98,8 @@ describe('glyphs', function() {
});

describe('SBIX glyphs', function() {
let font = fontkit.openSync(__dirname + '/data/ss-emoji/ss-emoji-apple.ttf');
const fontData = fs.readFileSync(__dirname + '/data/ss-emoji/ss-emoji-apple.ttf');
const font = fontkit.create(fontData);

it('should get an SBIXGlyph', function() {
let glyph = font.glyphsForString('😜')[0];
Expand Down Expand Up @@ -123,7 +129,8 @@ describe('glyphs', function() {
});

describe('COLR glyphs', function() {
let font = fontkit.openSync(__dirname + '/data/ss-emoji/ss-emoji-microsoft.ttf');
const fontData = fs.readFileSync(__dirname + '/data/ss-emoji/ss-emoji-microsoft.ttf');
const font = fontkit.create(fontData);

it('should get an SBIXGlyph', function() {
let glyph = font.glyphsForString('😜')[0];
Expand Down Expand Up @@ -156,7 +163,8 @@ describe('glyphs', function() {
});

describe('WOFF glyphs', function() {
let font = fontkit.openSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.woff');
const fontData = fs.readFileSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.woff');
const font = fontkit.create(fontData);

it('should get a TTFGlyph', function() {
let glyph = font.glyphsForString('T')[0];
Expand All @@ -175,7 +183,8 @@ describe('glyphs', function() {
});

describe('WOFF2 glyph', function() {
let font = fontkit.openSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.woff2');
const fontData = fs.readFileSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.woff2');
const font = fontkit.create(fontData);

it('should get a WOFF2Glyph', function() {
let glyph = font.glyphsForString('T')[0];
Expand Down
44 changes: 17 additions & 27 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,53 @@
import fontkit from '../src';
import assert from 'assert';
import fs from 'fs';

describe('fontkit', function() {
it('should open a font asynchronously', () =>
fontkit.open(__dirname + '/data/OpenSans/OpenSans-Regular.ttf', function(err, font) {
assert.equal(err, null);
return assert.equal(font.constructor.name, 'TTFFont');
})
);
const openFont = (path, extra) =>
fontkit.create(fs.readFileSync(path), extra);

describe('fontkit', function() {
it('should open a font synchronously', function() {
let font = fontkit.openSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
let font = openFont(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
return assert.equal(font.constructor.name, 'TTFFont');
});

it('should open fonts of different formats', function() {
let font = fontkit.openSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
let font = openFont(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');
assert.equal(font.constructor.name, 'TTFFont');

font = fontkit.openSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.otf');
font = openFont(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.otf');
assert.equal(font.constructor.name, 'TTFFont');

font = fontkit.openSync(__dirname + '/data/NotoSans/NotoSans.ttc');
font = openFont(__dirname + '/data/NotoSans/NotoSans.ttc');
assert.equal(font.constructor.name, 'TrueTypeCollection');

font = fontkit.openSync(__dirname + '/data/NotoSans/NotoSans.ttc', 'NotoSans');
font = openFont(__dirname + '/data/NotoSans/NotoSans.ttc', 'NotoSans');
assert.equal(font.constructor.name, 'TTFFont');

font = fontkit.openSync(__dirname + '/data/NotoSans/NotoSans.dfont');
font = openFont(__dirname + '/data/NotoSans/NotoSans.dfont');
assert.equal(font.constructor.name, 'DFont');

font = fontkit.openSync(__dirname + '/data/NotoSans/NotoSans.dfont', 'NotoSans');
font = openFont(__dirname + '/data/NotoSans/NotoSans.dfont', 'NotoSans');
assert.equal(font.constructor.name, 'TTFFont');

font = fontkit.openSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.woff');
font = openFont(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.woff');
assert.equal(font.constructor.name, 'WOFFFont');

font = fontkit.openSync(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.woff2');
font = openFont(__dirname + '/data/SourceSansPro/SourceSansPro-Regular.woff2');
assert.equal(font.constructor.name, 'WOFF2Font');
});

it('should open fonts lacking PostScript name', function() {
let font = fontkit.openSync(__dirname + '/data/Mada/Mada-Regular.subset1.ttf');
let font = openFont(__dirname + '/data/Mada/Mada-Regular.subset1.ttf');
assert.equal(font.postscriptName, null);
});

it('should error when opening an invalid font asynchronously', function() {
fontkit.open(__filename, function(err, font) {
assert(err instanceof Error);
assert.equal(err.message, 'Unknown font format');
});
});

it('should error when opening an invalid font synchronously', function() {
assert.throws(() => fontkit.openSync(__filename), /Unknown font format/);
assert.throws(() => openFont(__filename), /Unknown font format/);
});

it('should get collection objects for ttc fonts', function() {
let collection = fontkit.openSync(__dirname + '/data/NotoSans/NotoSans.ttc');
let collection = openFont(__dirname + '/data/NotoSans/NotoSans.ttc');
assert.equal(collection.constructor.name, 'TrueTypeCollection');

let names = collection.fonts.map(f => f.postscriptName);
Expand All @@ -68,7 +58,7 @@ describe('fontkit', function() {
});

it('should get collection objects for dfonts', function() {
let collection = fontkit.openSync(__dirname + '/data/NotoSans/NotoSans.dfont');
let collection = openFont(__dirname + '/data/NotoSans/NotoSans.dfont');
assert.equal(collection.constructor.name, 'DFont');

let names = collection.fonts.map(f => f.postscriptName);
Expand Down
4 changes: 3 additions & 1 deletion test/metadata.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fontkit from '../src';
import assert from 'assert';
import BBox from '../src/glyph/BBox';
import fs from 'fs';

describe('metadata', function() {
let font = fontkit.openSync(__dirname + '/data/NotoSans/NotoSans.ttc', 'NotoSans');
const fontData = fs.readFileSync(__dirname + '/data/NotoSans/NotoSans.ttc');
const font = fontkit.create(fontData, 'NotoSans');

it('has metadata properties', function() {
assert.equal(font.fullName, 'Noto Sans');
Expand Down
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--reporter spec
--compilers js:babel-register
--compilers js:@babel/register
14 changes: 11 additions & 3 deletions test/shaping.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import fontkit from '../src';
import assert from 'assert';
import fs from 'fs';

describe('shaping', function() {
let fontCache = {};
let test = (description, font, text, output) => {
it(description, function() {
let f = fontCache[font] || (fontCache[font] = fontkit.openSync(__dirname + '/data/' + font));
let f = fontCache[font];
if (!f) {
const fontData = fs.readFileSync(__dirname + '/data/' + font);
f = fontkit.create(fontData);
}

let {glyphs, positions} = f.layout(text);

// Generate a compact string representation of the results
Expand All @@ -28,7 +34,8 @@ describe('shaping', function() {
};

describe('general shaping tests', function() {
let font = fontkit.openSync(__dirname + '/data/amiri/amiri-regular.ttf');
const fontData = fs.readFileSync(__dirname + '/data/amiri/amiri-regular.ttf');
const font = fontkit.create(fontData);

it('should use correct script and language when features are not specified', function() {
let {glyphs} = font.layout('۴', 'arab', 'URD');
Expand Down Expand Up @@ -70,7 +77,8 @@ describe('shaping', function() {
});

describe('hangul shaper', function() {
let font = fontkit.openSync(__dirname + '/data/NotoSansCJK/NotoSansCJKkr-Regular.otf');
const fontData = fs.readFileSync(__dirname + '/data/NotoSansCJK/NotoSansCJKkr-Regular.otf');
const font = fontkit.create(fontData);

it('should use composed versions if supported by the font', function() {
let {glyphs} = font.layout('\uD734\uAC00\u0020\uAC00\u002D\u002D\u0020\u0028\uC624\u002D\u002D\u0029');
Expand Down
Loading

0 comments on commit f674bf2

Please sign in to comment.