From 8ebce10410a5dfcd824de50ed9cdfcdd7c5f29d4 Mon Sep 17 00:00:00 2001 From: Vilim Lendvaj Date: Thu, 21 Nov 2024 20:09:14 +0100 Subject: [PATCH] Allow loading non-XML fonts in web-worker --- plugins/plugin-print/src/load-bitmap-font.ts | 72 ++++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/plugins/plugin-print/src/load-bitmap-font.ts b/plugins/plugin-print/src/load-bitmap-font.ts index 90043711..7dbf6b49 100644 --- a/plugins/plugin-print/src/load-bitmap-font.ts +++ b/plugins/plugin-print/src/load-bitmap-font.ts @@ -55,42 +55,11 @@ function parseFont(file: string, data: Buffer | string): LoadedFont { } if (/.xml$/.test(file) || data.charAt(0) === "<") { - return parseXML(data); - } - - return parseASCII(data); -} - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -function parseNumbersInObject>(obj: T) { - for (const key in obj) { - try { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (obj as any)[key] = parseInt(obj[key], 10); - } catch { - // do nothing - } - - if (typeof obj[key] === "object") { - parseNumbersInObject(obj[key]); + if (!isWebWorker) { + return parseXML(data); } - } - - return obj; -} - -/** - * - * @param bufferOrUrl A URL to a file or a buffer - * @returns - */ -export async function loadBitmapFontData( - bufferOrUrl: string | Buffer -): Promise { - if (isWebWorker && typeof bufferOrUrl === "string") { - const res = await fetch(bufferOrUrl); - const text = await res.text(); - const json = convertXML(text); + + const json = convertXML(data); const font = json.font.children.reduce( // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -122,7 +91,38 @@ export async function loadBitmapFontData( chars, kernings, } satisfies LoadedFont; - } else if (typeof bufferOrUrl === "string") { + } + + return parseASCII(data); +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function parseNumbersInObject>(obj: T) { + for (const key in obj) { + try { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (obj as any)[key] = parseInt(obj[key], 10); + } catch { + // do nothing + } + + if (typeof obj[key] === "object") { + parseNumbersInObject(obj[key]); + } + } + + return obj; +} + +/** + * + * @param bufferOrUrl A URL to a file or a buffer + * @returns + */ +export async function loadBitmapFontData( + bufferOrUrl: string | Buffer +): Promise { + if (typeof bufferOrUrl === "string") { const res = await fetch(bufferOrUrl); const text = await res.text();