Browse Source

Don't inline font url by default + fixes

Marcel Mraz 11 months ago
parent
commit
d9a45dce9c

+ 1 - 1
packages/excalidraw/fonts/ExcalidrawFont.ts

@@ -134,7 +134,7 @@ export class ExcalidrawFont implements Font {
   private static async toBase64(arrayBuffer: ArrayBuffer) {
   private static async toBase64(arrayBuffer: ArrayBuffer) {
     let base64: string;
     let base64: string;
 
 
-    if (Buffer) {
+    if (typeof Buffer !== "undefined") {
       // node + server-side
       // node + server-side
       base64 = Buffer.from(arrayBuffer).toString("base64");
       base64 = Buffer.from(arrayBuffer).toString("base64");
     } else {
     } else {

+ 1 - 1
packages/excalidraw/fonts/wasm/woff2.bindings.ts

@@ -2781,7 +2781,7 @@ const Module = (function () {
           return str;
           return str;
         },
         },
         toWireType(destructors, value) {
         toWireType(destructors, value) {
-          if (Object.prototype.toString.call(d) === "[object ArrayBuffer]") {
+          if (Object.prototype.toString.call(value) === "[object ArrayBuffer]") {
             value = new Uint8Array(value);
             value = new Uint8Array(value);
           }
           }
           let getLength;
           let getLength;

+ 15 - 19
packages/excalidraw/scene/export.ts

@@ -356,8 +356,7 @@ export const exportToSvg = async (
         </clipPath>`;
         </clipPath>`;
   }
   }
 
 
-  const shouldInlineFonts = !opts?.skipInliningFonts;
-  const fontFaces = await getFontFaces(elements, shouldInlineFonts);
+  const fontFaces = opts?.skipInliningFonts ? [] : await getFontFaces(elements);
 
 
   svgRoot.innerHTML = `
   svgRoot.innerHTML = `
   ${SVG_EXPORT_TAG}
   ${SVG_EXPORT_TAG}
@@ -438,13 +437,10 @@ export const getExportSize = (
 
 
 const getFontFaces = async (
 const getFontFaces = async (
   elements: readonly ExcalidrawElement[],
   elements: readonly ExcalidrawElement[],
-  shouldInlineFonts: boolean,
 ): Promise<string[]> => {
 ): Promise<string[]> => {
   const fontFamilies = new Set<number>();
   const fontFamilies = new Set<number>();
   const codePoints = new Set<number>();
   const codePoints = new Set<number>();
 
 
-  let getSource: (font: Font) => string | Promise<string>;
-
   for (const element of elements) {
   for (const element of elements) {
     if (!isTextElement(element)) {
     if (!isTextElement(element)) {
       continue;
       continue;
@@ -453,24 +449,24 @@ const getFontFaces = async (
     fontFamilies.add(element.fontFamily);
     fontFamilies.add(element.fontFamily);
 
 
     // gather unique codepoints only when inlining fonts
     // 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(
   const fontFaces = await Promise.all(
     Array.from(fontFamilies).map(async (x) => {
     Array.from(fontFamilies).map(async (x) => {