Skip to content

Commit 8241908

Browse files
committed
Fixed compatability issue regarding globalThis in older browsers.
1 parent 350af8d commit 8241908

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
"test": "npm run build && npm run dist && mocha --recursive && npm run lint",
3333
"lint": "eslint src",
3434
"lint-fix": "eslint src --fix",
35-
"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<keys.length;i++)a[keys[i]]=b[keys[i]];return a})({'default':opentype},opentype));});\"",
36-
"b:umd": "node esbuild-runner.mjs --input src/opentype.mjs --output build/opentype.js --externals \"['fs']\" --target es2015 --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<keys.length;i++)a[keys[i]]=b[keys[i]];return a})({'default':opentype},opentype));});\"",
35+
"start": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/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<keys.length;i++)a[keys[i]]=b[keys[i]];return a})({'default':opentype},opentype));});\"",
36+
"b:umd": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/opentype.js --externals \"['fs']\" --target es2015 --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<keys.length;i++)a[keys[i]]=b[keys[i]];return a})({'default':opentype},opentype));});\"",
3737
"d:umd": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/opentype.min.js --externals \"['fs']\" --target es2015 --minify --sourcemap --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<keys.length;i++)a[keys[i]]=b[keys[i]];return a})({'default':opentype},opentype));});\"",
38-
"b:esm": "node esbuild-runner.mjs --input src/opentype.mjs --output build/opentype.mjs --externals \"['fs']\" --target es2015 --module",
38+
"b:esm": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/opentype.mjs --externals \"['fs']\" --target es2015 --module",
3939
"d:esm": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/opentype.min.mjs --externals \"['fs']\" --target es2015 --module --minify --sourcemap",
4040
"b:compat": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/opentype.compat.js --externals \"['fs']\" --target compat --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<keys.length;i++)a[keys[i]]=b[keys[i]];return a})({'default':opentype},opentype));});\"",
4141
"d:compat": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/opentype.compat.min.js --externals \"['fs']\" --target compat --minify --sourcemap --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<keys.length;i++)a[keys[i]]=b[keys[i]];return a})({'default':opentype},opentype));});\""

src/svgimages.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isGzip, unGzip } from './util.mjs';
1+
import { getGlobalScope, isGzip, unGzip } from './util.mjs';
22

33
/**
44
* @typedef {object} SVGDocCacheEntry
@@ -126,7 +126,7 @@ function createSvgImageCacheEntry(font, svgTemplatePromise, glyphIndex) {
126126
* @param {Uint8Array} buf
127127
* @returns {Promise<string>}
128128
*/
129-
export const decodeSvgDocument = typeof DecompressionStream === 'function'
129+
export const decodeSvgDocument = typeof getGlobalScope().DecompressionStream === 'function'
130130
? decodeSvgDocumentWithDecompressionStream
131131
: decodeSvgDocumentWithTinyInflate;
132132

src/tables/cpal.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { Parser } from '../parse.mjs';
77
import check from '../check.mjs';
88
import table from '../table.mjs';
9+
import { getGlobalScope, isBrowser } from '../util.mjs';
910

1011
// Parse the header `head` table
1112
function parseCpalTable(data, start) {
@@ -220,7 +221,7 @@ function parseColor(color, targetFormat = 'hexa') {
220221
if(targetFormat == 'bgra') {
221222
return color;
222223
}
223-
} else if(typeof document !== 'undefined' && /^[a-z]+$/i.test(color)) {
224+
} else if( isBrowser() && getGlobalScope().HTMLCanvasElement && /^[a-z]+$/i.test(color)) {
224225
// assume CSS color name (only works in browser context!)
225226
const ctx = document.createElement('canvas').getContext('2d');
226227
ctx.fillStyle = color;

src/util.mjs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import { tinf_uncompress as inflate } from './[email protected]';
22

3+
function getGlobalScope() {
4+
return (typeof globalThis !== "undefined"?globalThis:self);
5+
}
6+
37
function isBrowser() {
48
return (
5-
typeof window !== 'undefined' ||
9+
(typeof getGlobalScope().window !== 'undefined' && getGlobalScope() === getGlobalScope().window && getGlobalScope().window.document) ||
610
typeof WorkerGlobalScope !== 'undefined'
711
);
812
}
913

10-
function isNode() {
11-
return (
12-
typeof window === 'undefined' &&
13-
typeof global === 'object' &&
14-
typeof process === 'object'
15-
);
16-
}
1714

1815
// Check if 2 arrays of primitives are equal.
1916
function arraysEqual(ar1, ar2) {
@@ -141,4 +138,4 @@ function copyComponent(c) {
141138
};
142139
}
143140

144-
export { isBrowser, isNode, arraysEqual, binarySearch, binarySearchIndex, binarySearchInsert, isGzip, unGzip, copyPoint, copyComponent };
141+
export { getGlobalScope, isBrowser, arraysEqual, binarySearch, binarySearchIndex, binarySearchInsert, isGzip, unGzip, copyPoint, copyComponent };

test/tables/gasp.mjs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import assert from 'assert';
2+
import { parse } from '../../src/opentype.mjs';
3+
import { readFileSync } from 'fs';
4+
const loadSync = (url, opt) => parse(readFileSync(url), opt);
5+
6+
describe('tables/gasp.mjs', function () {
7+
const font = loadSync('./test/fonts/Roboto-Black.ttf');
8+
9+
it('can parse gasp table version', function() {
10+
assert.equal(font.tables.gasp.version, 1);
11+
});
12+
it('can parse gasp table numRanges', function() {
13+
assert.equal(font.tables.gasp.numRanges, 2);
14+
});
15+
16+
it('can parse gasp table numRanges 0 rangeMaxPPEM', function() {
17+
assert.equal(font.tables.gasp.gaspRanges[0].rangeMaxPPEM, 8); // default value
18+
});
19+
20+
it('can parse gasp table numRanges 0 rangeGaspBehavior', function() {
21+
assert.equal(font.tables.gasp.gaspRanges[0].rangeGaspBehavior, 0x0002); //GASP_DOGRAY = 0x0002
22+
});
23+
24+
it('can parse gasp table numRanges 1 rangeMaxPPEM', function() {
25+
assert.equal(font.tables.gasp.gaspRanges[1].rangeMaxPPEM, 0xFFFF); // default value
26+
});
27+
28+
it('can parse gasp table numRanges 1 rangeGaspBehavior', function() {
29+
assert.equal(font.tables.gasp.gaspRanges[1].rangeGaspBehavior, 0x0001 + 0x0002 + 0x0004 + 0x0008); // all flags set = 15
30+
});
31+
32+
});

0 commit comments

Comments
 (0)