Skip to content

Commit

Permalink
Fixed compatability issue regarding globalThis in older browsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ILOVEPIE committed May 28, 2024
1 parent 707e75d commit 9b59895
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 19 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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<keys.length;i++)a[keys[i]]=b[keys[i]];return a})({'default':opentype},opentype));});\"",
"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));});\"",
"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));});\"",
"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));});\"",
"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));});\"",
"b:esm": "node esbuild-runner.mjs --input src/opentype.mjs --output build/opentype.mjs --externals \"['fs']\" --target es2015 --module",
"b:esm": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/opentype.mjs --externals \"['fs']\" --target es2015 --module",
"d:esm": "node esbuild-runner.mjs --input src/opentype.mjs --output dist/opentype.min.mjs --externals \"['fs']\" --target es2015 --module --minify --sourcemap",
"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));});\"",
"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));});\""
Expand Down
4 changes: 2 additions & 2 deletions src/svgimages.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isGzip, unGzip } from './util.mjs';
import { getGlobalScope, isGzip, unGzip } from './util.mjs';

/**
* @typedef {object} SVGDocCacheEntry
Expand Down Expand Up @@ -126,7 +126,7 @@ function createSvgImageCacheEntry(font, svgTemplatePromise, glyphIndex) {
* @param {Uint8Array} buf
* @returns {Promise<string>}
*/
export const decodeSvgDocument = typeof DecompressionStream === 'function'
export const decodeSvgDocument = typeof getGlobalScope().DecompressionStream === 'function'
? decodeSvgDocumentWithDecompressionStream
: decodeSvgDocumentWithTinyInflate;

Expand Down
3 changes: 2 additions & 1 deletion src/tables/cpal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions src/util.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { tinf_uncompress as inflate } from './[email protected]';

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) {
Expand Down Expand Up @@ -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 };
4 changes: 0 additions & 4 deletions test/tables/gasp.mjs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down

0 comments on commit 9b59895

Please sign in to comment.