From 9b59895f8863096084dd172a2c99a3ce3f224ffb Mon Sep 17 00:00:00 2001 From: ILOVEPIE Date: Tue, 28 May 2024 12:25:15 -0700 Subject: [PATCH] Fixed compatability issue regarding globalThis in older browsers. --- package.json | 6 +++--- src/svgimages.mjs | 4 ++-- src/tables/cpal.mjs | 3 ++- src/util.mjs | 15 ++++++--------- test/tables/gasp.mjs | 4 ---- 5 files changed, 13 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 01b4e2c9..8c33f894 100644 --- a/package.json +++ b/package.json @@ -29,10 +29,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 index 094c5777..f22ce280 100644 --- a/test/tables/gasp.mjs +++ b/test/tables/gasp.mjs @@ -1,9 +1,5 @@ import assert from 'assert'; -<<<<<<<< HEAD:test/tables/gasp.spec.mjs import { parse } from '../../src/opentype.mjs'; -======== -import { Font, Path, Glyph, parse, load} from '../../src/opentype.mjs'; ->>>>>>>> 3946e83 (Created a utility that runs esbuild and SWC.):test/tables/gasp.mjs import { readFileSync } from 'fs'; const loadSync = (url, opt) => parse(readFileSync(url), opt);