Skip to content

Commit

Permalink
Use ES6 modules.
Browse files Browse the repository at this point in the history
Convert the old require-style modules to native ES6 modules.
  • Loading branch information
fdb committed May 12, 2017
1 parent 059c882 commit 6b9a244
Show file tree
Hide file tree
Showing 55 changed files with 2,829 additions and 2,999 deletions.
4 changes: 3 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"esversion": 6,
"browser": true,
"node": true,
"mocha": true,
"camelcase": true,
"eqeqeq": true,
"eqnull": true,
Expand All @@ -11,5 +13,5 @@
"trailing": true,
"undef": true,
"unused": true,
"strict": true
"strict": false
}
60 changes: 28 additions & 32 deletions bin/ot
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/env node
/* eslint no-console: off */

'use strict';

var fs = require('fs');
var path = require('path');
var opentype = require('../src/opentype.js');
import fs from 'fs';
import path from 'path';
import { load } from '../src/opentype';

// Print out information about the font on the console.
function printFontInfo(font) {
Expand Down Expand Up @@ -40,24 +39,24 @@ function printUsage() {
}

function fileInfo(file) {
opentype.load(file, function(err, font) {
console.log(path.basename(file));
if (err) {
console.log(' (Error: ' + err + ')');
} else if (!font.supported) {
console.log(' (Unsupported)');
} else {
printFontInfo(font);
}
});
load(file, function(err, font) {
console.log(path.basename(file));
if (err) {
console.log(' (Error: ' + err + ')');
} else if (!font.supported) {
console.log(' (Unsupported)');
} else {
printFontInfo(font);
}
});
}

function recursiveInfo(fontDirectory) {
walk(fontDirectory, function(file) {
var ext = path.extname(file).toLowerCase();
if (ext === '.ttf' || ext === '.otf') {
fileInfo(file);
}
fileInfo(file);
}
});
}

Expand All @@ -67,21 +66,18 @@ if (process.argv.length < 3) {
var command = process.argv[2];
if (command === 'info') {
var fontpath = process.argv.length === 3 ? '.' : process.argv[3];
if(fs.existsSync(fontpath)) {
var ext = path.extname(fontpath).toLowerCase();
if(fs.statSync(fontpath).isDirectory()) {
recursiveInfo(fontpath);
}
else if(ext === '.ttf' || ext === '.otf') {
fileInfo(fontpath);
}
else {
printUsage();
}
}
else {
console.log('Path not found');
}
if (fs.existsSync(fontpath)) {
var ext = path.extname(fontpath).toLowerCase();
if (fs.statSync(fontpath).isDirectory()) {
recursiveInfo(fontpath);
} else if (ext === '.ttf' || ext === '.otf') {
fileInfo(fontpath);
} else {
printUsage();
}
} else {
console.log('Path not found');
}
} else {
printUsage();
}
Expand Down
8 changes: 3 additions & 5 deletions bin/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/usr/bin/env node

'use strict';

var fs = require('fs');
var http = require('http');
var path = require('path');
import fs from 'fs';
import http from 'http';
import path from 'path';

var CONTENT_TYPES = {
'.html': 'text/html',
Expand Down
4,701 changes: 2,342 additions & 2,359 deletions dist/opentype.js

Large diffs are not rendered by default.

71 changes: 36 additions & 35 deletions dist/opentype.min.js

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions examples/generate-font-node.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// This example shows how to create a font from scratch using node.js.

'use strict';

var opentype = require('../src/opentype');
import { Font, Glyph, Path } from '../src/opentype';

// These are the global measurements of the typeface.
var UNITS_PER_EM = 1000;
Expand Down Expand Up @@ -84,18 +82,18 @@ var GLYPH_MAP = {
var TTF_NAME_MAP = { _: 'underscore', '.': 'period' };

// The notdefGlyph always needs to be included.
var notdefGlyph = new opentype.Glyph({
var notdefGlyph = new Glyph({
name: '.notdef',
advanceWidth: 650,
path: new opentype.Path()
path: new Path()
});

// Our glyph map can't properly encode a space character, so we make one here.
var spaceGlyph = new opentype.Glyph({
var spaceGlyph = new Glyph({
name: 'space',
unicode: 32,
advanceWidth: 10 * SCALE,
path: new opentype.Path()
path: new Path()
});

var glyphs = [notdefGlyph, spaceGlyph];
Expand All @@ -108,7 +106,7 @@ for (var i = 0; i < glyphNames.length; i++) {
var ttfName = TTF_NAME_MAP[glyphName] || glyphName;

// Create a path by looping over all the points and multiplying by the SCALE.
var path = new opentype.Path();
var path = new Path();
var points = GLYPH_MAP[glyphName];
// Remember the width of the character, to set the advanceWidth.
var w = 0;
Expand All @@ -124,7 +122,7 @@ for (var i = 0; i < glyphNames.length; i++) {
}

// Create the glyph. The advanceWidth is the widest part of the letter + 1.
var glyph = new opentype.Glyph({
var glyph = new Glyph({
name: ttfName,
unicode: glyphName.charCodeAt(0),
advanceWidth: (w + 1) * SCALE,
Expand All @@ -134,7 +132,7 @@ for (var i = 0; i < glyphNames.length; i++) {
}

// Create the font using measurements + glyphs defined above.
var font = new opentype.Font({
var font = new Font({
familyName: 'Pyramid',
styleName: 'Regular',
unitsPerEm: UNITS_PER_EM,
Expand Down
21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,36 @@
"ot": "./bin/ot"
},
"scripts": {
"watch": "watchify src/opentype.js --standalone opentype --debug -o build/opentype.js -v",
"start": "mkdirp build && parallelshell \"npm run watch\" \"node ./bin/server.js\"",
"test": "mocha --recursive && jshint . && jscs .",
"browserify": "browserify src/opentype.js -p licensify --bare --standalone opentype > dist/opentype.js",
"uglify": "browserify src/opentype.js -p licensify --bare --standalone opentype -g [ uglifyify --compress [ --dead_code --global_defs [ --exports.DEBUG 0 ] ] ] > dist/opentype.min.js ",
"watch": "watchify src/opentype.js --standalone opentype --debug -t [ babelify ] -o build/opentype.js -v",
"start": "mkdirp build && parallelshell \"npm run watch\" \"babel-node ./bin/server.js\"",
"test": "mocha --compilers js:babel-core/register --recursive && jshint . && jscs .",
"browserify": "browserify src/opentype.js -p licensify --bare --standalone opentype -t [ babelify ] > dist/opentype.js",
"uglify": "browserify src/opentype.js -p licensify --bare --standalone opentype -t [ babelify ] -g [ uglifyify --compress [ --dead_code --global_defs [ --exports.DEBUG 0 ] ] ] > dist/opentype.min.js ",
"dist": "rimraf build && rimraf dist && mkdirp build && mkdirp dist && npm run test && npm run browserify && npm run uglify"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"babelify": "^7.3.0",
"browserify": "^13.0.1",
"jscs": "^3.0.3",
"jshint": "^2.9.2",
"licensify": "^3.1.2",
"mkdirp": "^0.5.1",
"mocha": "^2.5.3",
"parallelshell": "^2.0.0",
"rimraf": "^2.5.2",
"uglifyify": "^3.0.1",
"watchify": "^3.7.0",
"licensify": "^3.1.2"
"watchify": "^3.7.0"
},
"browser": {
"fs": false
},
"dependencies": {
"tiny-inflate": "^1.0.2"
},
"babel": {
"presets": ["es2015"]
}
}
4 changes: 1 addition & 3 deletions src/bbox.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// The Bounding Box object

'use strict';

function derive(v0, v1, v2, v3, t) {
return Math.pow(1 - t, 3) * v0 +
3 * Math.pow(1 - t, 2) * t * v1 +
Expand Down Expand Up @@ -157,4 +155,4 @@ BoundingBox.prototype.addQuad = function(x0, y0, x1, y1, x, y) {
this.addBezier(x0, y0, cp1x, cp1y, cp2x, cp2y, x, y);
};

exports.BoundingBox = BoundingBox;
export default BoundingBox;
17 changes: 7 additions & 10 deletions src/check.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// Run-time checking of preconditions.

'use strict';

exports.fail = function(message) {
function fail(message) {
throw new Error(message);
};
}

// Precondition function that checks if the given predicate is true.
// If not, it will throw an error.
exports.argument = function(predicate, message) {
function argument(predicate, message) {
if (!predicate) {
exports.fail(message);
fail(message);
}
};
}

// Precondition function that checks if the given assertion is true.
// If not, it will throw an error.
exports.assert = exports.argument;
export { fail, argument, argument as assert };
export default { fail, argument, assert: argument };
4 changes: 1 addition & 3 deletions src/draw.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Drawing utility functions.

'use strict';

// Draw a line on the given context from point `x1,y1` to point `x2,y2`.
function line(ctx, x1, y1, x2, y2) {
ctx.beginPath();
Expand All @@ -10,4 +8,4 @@ function line(ctx, x1, y1, x2, y2) {
ctx.stroke();
}

exports.line = line;
export default { line };
36 changes: 19 additions & 17 deletions src/encoding.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Glyph encoding

'use strict';

var cffStandardStrings = [
const cffStandardStrings = [
'.notdef', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent', 'ampersand', 'quoteright',
'parenleft', 'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two',
'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon', 'less', 'equal', 'greater',
Expand Down Expand Up @@ -47,7 +45,7 @@ var cffStandardStrings = [
'Uacutesmall', 'Ucircumflexsmall', 'Udieresissmall', 'Yacutesmall', 'Thornsmall', 'Ydieresissmall', '001.000',
'001.001', '001.002', '001.003', 'Black', 'Bold', 'Book', 'Light', 'Medium', 'Regular', 'Roman', 'Semibold'];

var cffStandardEncoding = [
const cffStandardEncoding = [
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent', 'ampersand', 'quoteright',
'parenleft', 'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two',
Expand All @@ -66,7 +64,7 @@ var cffStandardEncoding = [
'', 'Lslash', 'Oslash', 'OE', 'ordmasculine', '', '', '', '', '', 'ae', '', '', '', 'dotlessi', '', '',
'lslash', 'oslash', 'oe', 'germandbls'];

var cffExpertEncoding = [
const cffExpertEncoding = [
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', 'space', 'exclamsmall', 'Hungarumlautsmall', '', 'dollaroldstyle', 'dollarsuperior',
'ampersandsmall', 'Acutesmall', 'parenleftsuperior', 'parenrightsuperior', 'twodotenleader', 'onedotenleader',
Expand Down Expand Up @@ -94,7 +92,7 @@ var cffExpertEncoding = [
'Ocircumflexsmall', 'Otildesmall', 'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall', 'Uacutesmall',
'Ucircumflexsmall', 'Udieresissmall', 'Yacutesmall', 'Thornsmall', 'Ydieresissmall'];

var standardNames = [
const standardNames = [
'.notdef', '.null', 'nonmarkingreturn', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent',
'ampersand', 'quotesingle', 'parenleft', 'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash',
'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon', 'less',
Expand Down Expand Up @@ -146,9 +144,8 @@ DefaultEncoding.prototype.charToGlyphIndex = function(c) {
}
}
}
} else {
return null;
}
return null;
};

/**
Expand Down Expand Up @@ -224,6 +221,9 @@ function GlyphNames(post) {
case 3:
this.names = [];
break;
default:
this.names = [];
break;
}
}

Expand Down Expand Up @@ -274,12 +274,14 @@ function addGlyphNames(font) {
}
}

exports.cffStandardStrings = cffStandardStrings;
exports.cffStandardEncoding = cffStandardEncoding;
exports.cffExpertEncoding = cffExpertEncoding;
exports.standardNames = standardNames;
exports.DefaultEncoding = DefaultEncoding;
exports.CmapEncoding = CmapEncoding;
exports.CffEncoding = CffEncoding;
exports.GlyphNames = GlyphNames;
exports.addGlyphNames = addGlyphNames;
export {
cffStandardStrings,
cffStandardEncoding,
cffExpertEncoding,
standardNames,
DefaultEncoding,
CmapEncoding,
CffEncoding,
GlyphNames,
addGlyphNames
};
Loading

0 comments on commit 6b9a244

Please sign in to comment.