Don't inline font url by default + fixes

mrazator/first-class-support-for-chinese
Marcel Mraz 6 months ago
parent 8a6e644fc0
commit d9a45dce9c

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

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

@ -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,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) =>
for (const codePoint of Array.from(element.originalText, (u) => u.codePointAt(0),
u.codePointAt(0), )) {
)) { if (codePoint) {
if (codePoint) { codePoints.add(codePoint);
codePoints.add(codePoint);
}
} }
} }
} }
if (shouldInlineFonts) { const getSource = (font: Font) => {
// retrieve font source as dataurl based on the used codepoints try {
getSource = (font: Font) => font.getContent(codePoints); // retrieve font source as dataurl based on the used codepoints
} else { return font.getContent(codePoints);
// retrieve font source as a url otherwise } catch {
getSource = (font: Font) => font.urls[0].toString(); // 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) => {

Loading…
Cancel
Save