From d9a45dce9cadee603d34348b9cb05ff60b275b0d Mon Sep 17 00:00:00 2001 From: Marcel Mraz Date: Tue, 20 Aug 2024 15:52:29 +0200 Subject: [PATCH] Don't inline font url by default + fixes --- packages/excalidraw/fonts/ExcalidrawFont.ts | 2 +- .../excalidraw/fonts/wasm/woff2.bindings.ts | 2 +- packages/excalidraw/scene/export.ts | 34 ++++++++----------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/packages/excalidraw/fonts/ExcalidrawFont.ts b/packages/excalidraw/fonts/ExcalidrawFont.ts index 559b4c5b3..682ae7394 100644 --- a/packages/excalidraw/fonts/ExcalidrawFont.ts +++ b/packages/excalidraw/fonts/ExcalidrawFont.ts @@ -134,7 +134,7 @@ export class ExcalidrawFont implements Font { private static async toBase64(arrayBuffer: ArrayBuffer) { let base64: string; - if (Buffer) { + if (typeof Buffer !== "undefined") { // node + server-side base64 = Buffer.from(arrayBuffer).toString("base64"); } else { diff --git a/packages/excalidraw/fonts/wasm/woff2.bindings.ts b/packages/excalidraw/fonts/wasm/woff2.bindings.ts index a34c8e6b7..16b3fa144 100644 --- a/packages/excalidraw/fonts/wasm/woff2.bindings.ts +++ b/packages/excalidraw/fonts/wasm/woff2.bindings.ts @@ -2781,7 +2781,7 @@ const Module = (function () { return str; }, toWireType(destructors, value) { - if (Object.prototype.toString.call(d) === "[object ArrayBuffer]") { + if (Object.prototype.toString.call(value) === "[object ArrayBuffer]") { value = new Uint8Array(value); } let getLength; diff --git a/packages/excalidraw/scene/export.ts b/packages/excalidraw/scene/export.ts index 9bf438ec1..b120d0cc9 100644 --- a/packages/excalidraw/scene/export.ts +++ b/packages/excalidraw/scene/export.ts @@ -356,8 +356,7 @@ export const exportToSvg = async ( `; } - const shouldInlineFonts = !opts?.skipInliningFonts; - const fontFaces = await getFontFaces(elements, shouldInlineFonts); + const fontFaces = opts?.skipInliningFonts ? [] : await getFontFaces(elements); svgRoot.innerHTML = ` ${SVG_EXPORT_TAG} @@ -438,13 +437,10 @@ export const getExportSize = ( const getFontFaces = async ( elements: readonly ExcalidrawElement[], - shouldInlineFonts: boolean, ): Promise => { const fontFamilies = new Set(); const codePoints = new Set(); - let getSource: (font: Font) => string | Promise; - for (const element of elements) { if (!isTextElement(element)) { continue; @@ -453,24 +449,24 @@ const getFontFaces = async ( fontFamilies.add(element.fontFamily); // gather unique codepoints only when inlining fonts - if (shouldInlineFonts) { - for (const codePoint of Array.from(element.originalText, (u) => - u.codePointAt(0), - )) { - if (codePoint) { - codePoints.add(codePoint); - } + for (const codePoint of Array.from(element.originalText, (u) => + u.codePointAt(0), + )) { + if (codePoint) { + codePoints.add(codePoint); } } } - if (shouldInlineFonts) { - // retrieve font source as dataurl based on the used codepoints - getSource = (font: Font) => font.getContent(codePoints); - } else { - // retrieve font source as a url otherwise - getSource = (font: Font) => font.urls[0].toString(); - } + const getSource = (font: Font) => { + try { + // retrieve font source as dataurl based on the used codepoints + return font.getContent(codePoints); + } catch { + // fallback to font source as a url + return font.urls[0].toString(); + } + }; const fontFaces = await Promise.all( Array.from(fontFamilies).map(async (x) => {