Skip to content

Commit 9b59895

Browse files
committed
Fixed compatability issue regarding globalThis in older browsers.
1 parent 707e75d commit 9b59895

File tree

5 files changed

+13
-19
lines changed

5 files changed

+13
-19
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
"test": "npm run build && npm run dist && mocha --recursive && npm run lint",
3030
"lint": "eslint src",
3131
"lint-fix": "eslint src --fix",
32-
"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));});\"",
33-
"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));});\"",
32+
"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));});\"",
33+
"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));});\"",
3434
"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));});\"",
35-
"b:esm": "node esbuild-runner.mjs --input src/opentype.mjs --output build/opentype.mjs --externals \"['fs']\" --target es2015 --module",
35+
"b:esm": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/opentype.mjs --externals \"['fs']\" --target es2015 --module",
3636
"d:esm": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/opentype.min.mjs --externals \"['fs']\" --target es2015 --module --minify --sourcemap",
3737
"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));});\"",
3838
"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: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import assert from 'assert';
2-
<<<<<<<< HEAD:test/tables/gasp.spec.mjs
32
import { parse } from '../../src/opentype.mjs';
4-
========
5-
import { Font, Path, Glyph, parse, load} from '../../src/opentype.mjs';
6-
>>>>>>>> 3946e83 (Created a utility that runs esbuild and SWC.):test/tables/gasp.mjs
73
import { readFileSync } from 'fs';
84
const loadSync = (url, opt) => parse(readFileSync(url), opt);
95

0 commit comments

Comments
 (0)