From 8ee20225cf50f98bd731de4d113ff9d497020656 Mon Sep 17 00:00:00 2001 From: Marcel Mraz Date: Mon, 19 Aug 2024 17:40:50 +0200 Subject: [PATCH] Skip fonts inlining for library items --- packages/excalidraw/components/PublishLibrary.tsx | 1 + packages/excalidraw/fonts/ExcalidrawFont.ts | 11 +++++------ packages/excalidraw/fonts/wasm/hb-subset.loader.ts | 3 +++ packages/excalidraw/fonts/wasm/hb-subset.wasm.ts | 2 +- packages/excalidraw/fonts/wasm/woff2.loader.ts | 3 +++ packages/excalidraw/fonts/wasm/woff2.wasm.ts | 2 +- packages/excalidraw/hooks/useLibraryItemSvg.ts | 1 + 7 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/excalidraw/components/PublishLibrary.tsx b/packages/excalidraw/components/PublishLibrary.tsx index d1cbe63d1..f3d0fe1b4 100644 --- a/packages/excalidraw/components/PublishLibrary.tsx +++ b/packages/excalidraw/components/PublishLibrary.tsx @@ -133,6 +133,7 @@ const SingleLibraryItem = ({ exportBackground: true, }, files: null, + skipInliningFonts: true, }); node.innerHTML = svg.outerHTML; })(); diff --git a/packages/excalidraw/fonts/ExcalidrawFont.ts b/packages/excalidraw/fonts/ExcalidrawFont.ts index ead5df2d7..468873ad0 100644 --- a/packages/excalidraw/fonts/ExcalidrawFont.ts +++ b/packages/excalidraw/fonts/ExcalidrawFont.ts @@ -3,7 +3,6 @@ import { LOCAL_FONT_PROTOCOL } from "./metadata"; import loadWoff2 from "./wasm/woff2.loader"; import loadHbSubset from "./wasm/hb-subset.loader"; -// import init, * as brotli from "../../../node_modules/brotli-wasm/pkg.web/brotli_wasm.js"; export interface Font { urls: URL[]; fontFace: FontFace; @@ -55,7 +54,7 @@ export class ExcalidrawFont implements Font { "base64", ).buffer; - const base64 = await ExcalidrawFont.trySubsetGlyphsByCodePoints( + const base64 = await ExcalidrawFont.subsetGlyphsByCodePoints( arrayBuffer, codePoints, ); @@ -72,7 +71,7 @@ export class ExcalidrawFont implements Font { if (response.ok) { const arrayBuffer = await response.arrayBuffer(); - const base64 = await ExcalidrawFont.trySubsetGlyphsByCodePoints( + const base64 = await ExcalidrawFont.subsetGlyphsByCodePoints( arrayBuffer, codePoints, ); @@ -104,14 +103,14 @@ export class ExcalidrawFont implements Font { } /** - * Tries to convert a font data as arraybuffer into a dataurl (base64) with subsetted glyphs based on the specified `codePoints`. + * Tries to subset glyphs in a font based on the used codepoints, returning the font as daturl. * * @param arrayBuffer font data buffer, preferrably in the woff2 format, though others should work as well * @param codePoints codepoints used to subset the glyphs * - * @returns font with subsetted glyphs converted into a dataurl + * @returns font with subsetted glyphs (all glyphs in case of errors) converted into a dataurl */ - private static async trySubsetGlyphsByCodePoints( + private static async subsetGlyphsByCodePoints( arrayBuffer: ArrayBuffer, codePoints: ReadonlySet, ): Promise { diff --git a/packages/excalidraw/fonts/wasm/hb-subset.loader.ts b/packages/excalidraw/fonts/wasm/hb-subset.loader.ts index d6ad301f3..48046727c 100644 --- a/packages/excalidraw/fonts/wasm/hb-subset.loader.ts +++ b/packages/excalidraw/fonts/wasm/hb-subset.loader.ts @@ -1,3 +1,6 @@ +/** + * Lazy loads wasm and respective bindings for font subsetting based on the harfbuzzjs. + */ let loadedWasm: ReturnType | null = null; // TODO: add support for fetching the wasm from an URL (external CDN, data URL, etc.) diff --git a/packages/excalidraw/fonts/wasm/hb-subset.wasm.ts b/packages/excalidraw/fonts/wasm/hb-subset.wasm.ts index 62eceba11..19dc50a63 100644 --- a/packages/excalidraw/fonts/wasm/hb-subset.wasm.ts +++ b/packages/excalidraw/fonts/wasm/hb-subset.wasm.ts @@ -1,5 +1,5 @@ // GENERATED CODE -- DO NOT EDIT! -/* eslint-disable prettier/prettier */ +/* eslint-disable */ // @ts-nocheck /** diff --git a/packages/excalidraw/fonts/wasm/woff2.loader.ts b/packages/excalidraw/fonts/wasm/woff2.loader.ts index f7928841b..88f335853 100644 --- a/packages/excalidraw/fonts/wasm/woff2.loader.ts +++ b/packages/excalidraw/fonts/wasm/woff2.loader.ts @@ -1,3 +1,6 @@ +/** + * Lazy loads wasm and respective bindings for woff2 compression and decompression. + */ type Vector = any; let loadedWasm: ReturnType | null = null; diff --git a/packages/excalidraw/fonts/wasm/woff2.wasm.ts b/packages/excalidraw/fonts/wasm/woff2.wasm.ts index 8f6c2a4b2..3ddcf7b5e 100644 --- a/packages/excalidraw/fonts/wasm/woff2.wasm.ts +++ b/packages/excalidraw/fonts/wasm/woff2.wasm.ts @@ -1,5 +1,5 @@ // GENERATED CODE -- DO NOT EDIT! -/* eslint-disable prettier/prettier */ +/* eslint-disable */ // @ts-nocheck /** diff --git a/packages/excalidraw/hooks/useLibraryItemSvg.ts b/packages/excalidraw/hooks/useLibraryItemSvg.ts index 1a32678b4..4e95712af 100644 --- a/packages/excalidraw/hooks/useLibraryItemSvg.ts +++ b/packages/excalidraw/hooks/useLibraryItemSvg.ts @@ -18,6 +18,7 @@ const exportLibraryItemToSvg = async (elements: LibraryItem["elements"]) => { }, files: null, renderEmbeddables: false, + skipInliningFonts: true, }); };