1
0
mirror of https://github.com/excalidraw/excalidraw.git synced 2024-11-10 11:35:52 +01:00

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

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

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

@ -356,8 +356,7 @@ export const exportToSvg = async (
</clipPath>`;
}
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<string[]> => {
const fontFamilies = new Set<number>();
const codePoints = new Set<number>();
let getSource: (font: Font) => string | Promise<string>;
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) => {