Skip to content

Commit

Permalink
0.17.1-obsidian-58, 2.5.3-beta-1
Browse files Browse the repository at this point in the history
  • Loading branch information
zsviczian committed Oct 19, 2024
1 parent 8869b8f commit bc0304f
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/excalidraw/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2712,10 +2712,10 @@ class App extends React.Component<AppProps, AppState> {
),
// rerender text elements on font load to fix #637 && #1553
// zsviczian In Obsidian this is not needed as I manage font load separately
/*addEventListener(document.fonts, "loadingdone", (event) => {
addEventListener(document.fonts, "loadingdone", (event) => {
const loadedFontFaces = (event as FontFaceSetLoadEvent).fontfaces;
this.fonts.onLoaded(loadedFontFaces);
}),*/
}),
// Safari-only desktop pinch zoom
addEventListener(
document,
Expand Down
2 changes: 1 addition & 1 deletion packages/excalidraw/entry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "./publicPath";
import polyfill from "./polyfill";

import "./fonts/assets/fonts.css";
import "./fonts/css/fonts.css";

polyfill();
export * from "./index";
15 changes: 14 additions & 1 deletion packages/excalidraw/fonts/ExcalidrawFontFace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { stringToBase64, toByteString } from "../data/encode";
import { fetchFontFromVault } from "../obsidianUtils";
import { promiseTry } from "../utils";
import { LOCAL_FONT_PROTOCOL } from "./metadata";
import { subsetWoff2GlyphsByCodepoints } from "./subset/subset-main";
Expand Down Expand Up @@ -104,6 +105,10 @@ export class ExcalidrawFontFace implements IExcalidrawFontFace {

public fetchFont(url: URL | DataURL): Promise<ArrayBuffer> {
return promiseTry(async () => {
const ab = await fetchFontFromVault(url); //zsviczian
if (ab) {
return ab;
}
const response = await fetch(url, {
headers: {
Accept: "font/woff2",
Expand Down Expand Up @@ -234,14 +239,22 @@ export class ExcalidrawFontFace implements IExcalidrawFontFace {
}

try {
const arrayBuffer = await fetchFontFromVault(url); //zsviczian
if(arrayBuffer) {
return `data:font/woff2;base64,${await stringToBase64(
await toByteString(arrayBuffer),
true,
)}`;
}

const response = await fetch(url, {
headers: {
Accept: "font/woff2",
},
});

if (response.ok) {
const mimeType = await response.headers.get("Content-Type");
const mimeType = response.headers.get("Content-Type");
const buffer = await response.arrayBuffer();

return `data:${mimeType};base64,${await stringToBase64(
Expand Down
8 changes: 4 additions & 4 deletions packages/excalidraw/fonts/css/fonts.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@

@font-face {
font-family: "Assistant";
src: url(../woff2/Assistant/Assistant-Regular.woff2) format("woff2");
src: url(https://unpkg.com/@zsviczian/[email protected]/dist/excalidraw-assets/Assistant-Regular.woff2) format("woff2");
font-weight: 400;
style: normal;
display: swap;
}

@font-face {
font-family: "Assistant";
src: url(../woff2/Assistant/Assistant-Medium.woff2) format("woff2");
src: url(https://unpkg.com/@zsviczian/[email protected]/dist/excalidraw-assets/Assistant-Medium.woff2) format("woff2");
font-weight: 500;
style: normal;
display: swap;
}

@font-face {
font-family: "Assistant";
src: url(../woff2/Assistant/Assistant-SemiBold.woff2) format("woff2");
src: url(https://unpkg.com/@zsviczian/[email protected]/dist/excalidraw-assets/Assistant-SemiBold.woff2) format("woff2");
font-weight: 600;
style: normal;
display: swap;
}

@font-face {
font-family: "Assistant";
src: url(../woff2/Assistant/Assistant-Bold.woff2) format("woff2");
src: url(https://unpkg.com/@zsviczian/[email protected]/dist/excalidraw-assets/Assistant-Bold.woff2) format("woff2");
font-weight: 700;
style: normal;
display: swap;
Expand Down
6 changes: 4 additions & 2 deletions packages/excalidraw/fonts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export class Fonts {
return await Fonts.loadFontFaces(fontFamilies);
};

private static async loadFontFaces(
//zsviczian made public
public static async loadFontFaces(
fontFamilies: Array<ExcalidrawTextElement["fontFamily"]>,
) {
// add all registered font faces into the `document.fonts` (if not added already)
Expand Down Expand Up @@ -266,7 +267,8 @@ export class Fonts {
return Array.from(Fonts.registered.keys());
}

private static getElementsFamilies(
//zsviczian made public
public static getElementsFamilies(
elements: ReadonlyArray<ExcalidrawElement>,
): Array<ExcalidrawTextElement["fontFamily"]> {
return Array.from(
Expand Down
1 change: 1 addition & 0 deletions packages/excalidraw/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export {
getFontFamilyString, //zsviczian
registerFontsInCSS, //zsviczian
getCSSFontDefinition, //zsviczian
loadSceneFonts, //zsviczian
} from "../utils/export";

export { refreshTextDimensions } from "./element/newElement"; //zsviczian
Expand Down
18 changes: 17 additions & 1 deletion packages/excalidraw/obsidianUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FreedrawIcon } from "./components/icons";
import { FONT_FAMILY } from "./constants";
import { NonDeletedExcalidrawElement } from "./element/types";
import { Fonts, register } from "./fonts";
import { IExcalidrawFontFace } from "./fonts/ExcalidrawFontFace";
import type { FontMetadata } from "./fonts/metadata";
import { FONT_METADATA } from "./fonts/metadata";

Expand Down Expand Up @@ -131,3 +131,19 @@ export async function getCSSFontDefinition(
const content = await fontFace.getContentLegacy();
return `@font-face {font-family: ${fontFaces[0].fontFace.family}; src: url(${content});}`;
}

export async function loadSceneFonts(elements: NonDeletedExcalidrawElement[]): Promise<void> {
const fontFamilies = Fonts.getElementsFamilies(elements);
await Fonts.loadFontFaces(fontFamilies);
}

export async function fetchFontFromVault(url: string | URL): Promise<ArrayBuffer|undefined> {
if(typeof url === "string" && !url.startsWith("data") && url.endsWith(".woff2")) {
const arrayBuffer = hostPlugin.loadFontFromFile(decodeURIComponent(url.substring(url.lastIndexOf("/")+1)))
if(arrayBuffer) {
return arrayBuffer;
}
}
return;

}
2 changes: 1 addition & 1 deletion packages/excalidraw/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zsviczian/excalidraw",
"version": "0.17.1-obsidian-57",
"version": "0.17.1-obsidian-58",
"main": "main.js",
"types": "types/excalidraw/index.d.ts",
"files": [
Expand Down
3 changes: 2 additions & 1 deletion packages/excalidraw/webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ module.exports = {
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
type: "asset/inline", //zsviczian
type: "asset/resource",
//type: "asset/inline", //zsviczian
},
],
},
Expand Down
3 changes: 2 additions & 1 deletion packages/excalidraw/webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ module.exports = {
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
type: "asset/inline", //zsviczian
type: "asset/resource",
//type: "asset/inline", //zsviczian
},
],
},
Expand Down
1 change: 1 addition & 0 deletions packages/utils/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,5 @@ export {
getFontFamilies,
registerFontsInCSS,
getCSSFontDefinition,
loadSceneFonts,
} from "../excalidraw/obsidianUtils"; //zsviczian

0 comments on commit bc0304f

Please sign in to comment.