diff --git a/package.json b/package.json index 61730546..47a4c8dc 100644 --- a/package.json +++ b/package.json @@ -32,10 +32,10 @@ "test": "npm run build && npm run dist && mocha --recursive && npm run lint", "lint": "eslint src", "lint-fix": "eslint src --fix", - "start": "node esbuild-runner.mjs --input src/opentype.mjs --output build/opentype.mjs --externals \"['fs']\" --target es2015 --module --watch --servedir . --global-name opentype --footer \"(function (root, factory) { if (typeof define === 'function' && define.amd)define(factory); else if (typeof module === 'object' && module.exports)module.exports = factory(); else root.opentype = factory(); })(typeof self !== 'undefined' ? self : this, function(){return ((function(a,b){var keys=Object.keys(b);for(var i=0;i} */ -export const decodeSvgDocument = typeof DecompressionStream === 'function' +export const decodeSvgDocument = typeof getGlobalScope().DecompressionStream === 'function' ? decodeSvgDocumentWithDecompressionStream : decodeSvgDocumentWithTinyInflate; diff --git a/src/tables/cpal.mjs b/src/tables/cpal.mjs index c9668033..26b35788 100644 --- a/src/tables/cpal.mjs +++ b/src/tables/cpal.mjs @@ -6,6 +6,7 @@ import { Parser } from '../parse.mjs'; import check from '../check.mjs'; import table from '../table.mjs'; +import { getGlobalScope, isBrowser } from '../util.mjs'; // Parse the header `head` table function parseCpalTable(data, start) { @@ -220,7 +221,7 @@ function parseColor(color, targetFormat = 'hexa') { if(targetFormat == 'bgra') { return color; } - } else if(typeof document !== 'undefined' && /^[a-z]+$/i.test(color)) { + } else if( isBrowser() && getGlobalScope().HTMLCanvasElement && /^[a-z]+$/i.test(color)) { // assume CSS color name (only works in browser context!) const ctx = document.createElement('canvas').getContext('2d'); ctx.fillStyle = color; diff --git a/src/util.mjs b/src/util.mjs index 2acb65df..71c2eb8a 100644 --- a/src/util.mjs +++ b/src/util.mjs @@ -1,19 +1,16 @@ import { tinf_uncompress as inflate } from './tiny-inflate@1.0.3.esm.mjs'; +function getGlobalScope() { + return (typeof globalThis !== "undefined"?globalThis:self); +} + function isBrowser() { return ( - typeof window !== 'undefined' || + (typeof getGlobalScope().window !== 'undefined' && getGlobalScope() === getGlobalScope().window && getGlobalScope().window.document) || typeof WorkerGlobalScope !== 'undefined' ); } -function isNode() { - return ( - typeof window === 'undefined' && - typeof global === 'object' && - typeof process === 'object' - ); -} // Check if 2 arrays of primitives are equal. function arraysEqual(ar1, ar2) { @@ -141,4 +138,4 @@ function copyComponent(c) { }; } -export { isBrowser, isNode, arraysEqual, binarySearch, binarySearchIndex, binarySearchInsert, isGzip, unGzip, copyPoint, copyComponent }; +export { getGlobalScope, isBrowser, arraysEqual, binarySearch, binarySearchIndex, binarySearchInsert, isGzip, unGzip, copyPoint, copyComponent }; diff --git a/test/tables/gasp.mjs b/test/tables/gasp.mjs new file mode 100644 index 00000000..f22ce280 --- /dev/null +++ b/test/tables/gasp.mjs @@ -0,0 +1,32 @@ +import assert from 'assert'; +import { parse } from '../../src/opentype.mjs'; +import { readFileSync } from 'fs'; +const loadSync = (url, opt) => parse(readFileSync(url), opt); + +describe('tables/gasp.mjs', function () { + const font = loadSync('./test/fonts/Roboto-Black.ttf'); + + it('can parse gasp table version', function() { + assert.equal(font.tables.gasp.version, 1); + }); + it('can parse gasp table numRanges', function() { + assert.equal(font.tables.gasp.numRanges, 2); + }); + + it('can parse gasp table numRanges 0 rangeMaxPPEM', function() { + assert.equal(font.tables.gasp.gaspRanges[0].rangeMaxPPEM, 8); // default value + }); + + it('can parse gasp table numRanges 0 rangeGaspBehavior', function() { + assert.equal(font.tables.gasp.gaspRanges[0].rangeGaspBehavior, 0x0002); //GASP_DOGRAY = 0x0002 + }); + + it('can parse gasp table numRanges 1 rangeMaxPPEM', function() { + assert.equal(font.tables.gasp.gaspRanges[1].rangeMaxPPEM, 0xFFFF); // default value + }); + + it('can parse gasp table numRanges 1 rangeGaspBehavior', function() { + assert.equal(font.tables.gasp.gaspRanges[1].rangeGaspBehavior, 0x0001 + 0x0002 + 0x0004 + 0x0008); // all flags set = 15 + }); + +});