UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

88 lines 3.54 kB
export class FontRegistry { constructor(textWhizz, designAtomsApiClient) { this._failedFonts = new Array(); this._activeRequests = new Array(); this._textWhizz = textWhizz; this._fontRegistry = new this._textWhizz.FontRegistry(72); this._designAtomsApiClient = designAtomsApiClient; this._setFallbackFonts(); } async _setFallbackFonts() { var _a, _b; const fallbackFonts = await this._designAtomsApiClient.getFallbackFonts(); if (!fallbackFonts) { console.warn(`Unexpected getFallbackFonts result`); return; } if (((_a = fallbackFonts.message) === null || _a === void 0 ? void 0 : _a.length) > 0) console.warn(fallbackFonts.message); if (((_b = fallbackFonts.fonts) === null || _b === void 0 ? void 0 : _b.length) > 0) { await this.loadFonts(fallbackFonts.fonts); this.textWhizzFontRegistry.fallbackFonts = fallbackFonts.fonts; } } get textWhizzFontRegistry() { return this._fontRegistry; } get ready() { return this._fontRegistry != null; } createFont(psName, size) { return this._fontRegistry.createFont(this._normalizePsName(psName), size); } containsFonts(psNames) { for (let psName of psNames) { if (!this._fontRegistry.contains(this._normalizePsName(psName))) return false; } return true; } checkFailedFonts(psNames) { for (let psName of psNames) { if (this._failedFonts.includes(this._normalizePsName(psName))) return true; } return false; } loadFonts(psNames) { if (this.containsFonts(psNames)) return Promise.resolve(); const promises = new Array(); for (let psName of psNames) { promises.push(this._loadFont(this._normalizePsName(psName))); } return Promise.all(promises).then(() => { }).catch(() => { throw "Failed to load some fonts"; }); } clear() { this._fontRegistry.clear(); this._failedFonts = new Array(); } _loadFont(psName) { if (this._failedFonts.includes(psName)) return Promise.reject(null); const request = this._activeRequests.find((r) => r.id === psName); if (request != null) return request.promise; const promise = this._designAtomsApiClient.getFontsByteArray(psName) .then(byteArray => { const numBytes = byteArray.length * byteArray.BYTES_PER_ELEMENT; const ptr = this._textWhizz._malloc(numBytes); const heapBytes = new Uint8Array(this._textWhizz.HEAPU8.buffer, ptr, numBytes); heapBytes.set(new Uint8Array(byteArray)); this._fontRegistry.add(heapBytes.byteOffset, heapBytes.length); this._textWhizz._free(ptr); }).catch(ex => { console.error(`Unable to get Fonts ByteArray. Reason: ${ex}`); this._failedFonts.push(psName); throw null; }).finally(() => { this._activeRequests = this._activeRequests.filter((r) => r.id !== psName); }); this._activeRequests.push({ id: psName, promise: promise }); return promise; } _normalizePsName(psName) { return psName.trim().toLowerCase(); } } //# sourceMappingURL=FontRegistry.js.map