Don't inline font url by default + fixes

This commit is contained in:
Marcel Mraz 2024-08-20 15:52:29 +02:00
parent 8a6e644fc0
commit d9a45dce9c
3 changed files with 17 additions and 21 deletions

View File

@ -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 {

View File

@ -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;

View File

@ -356,8 +356,7 @@ export const exportToSvg = async (
</clipPath>`; </clipPath>`;
} }
const shouldInlineFonts = !opts?.skipInliningFonts; const fontFaces = opts?.skipInliningFonts ? [] : await getFontFaces(elements);
const fontFaces = await getFontFaces(elements, shouldInlineFonts);
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,7 +449,6 @@ 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) => for (const codePoint of Array.from(element.originalText, (u) =>
u.codePointAt(0), u.codePointAt(0),
)) { )) {
@ -462,15 +457,16 @@ const getFontFaces = async (
} }
} }
} }
}
if (shouldInlineFonts) { const getSource = (font: Font) => {
try {
// retrieve font source as dataurl based on the used codepoints // retrieve font source as dataurl based on the used codepoints
getSource = (font: Font) => font.getContent(codePoints); return font.getContent(codePoints);
} else { } catch {
// retrieve font source as a url otherwise // fallback to font source as a url
getSource = (font: Font) => font.urls[0].toString(); 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) => {