Skip to content

Commit bfc8dd4

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

File tree

4 files changed

+10
-16
lines changed

4 files changed

+10
-16
lines changed

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)