Skip to content

Commit

Permalink
Merge branch 'excalidraw-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
zsviczian committed Oct 21, 2024
2 parents 584652e + 574c879 commit 8423c68
Show file tree
Hide file tree
Showing 280 changed files with 13,596 additions and 505 deletions.
2 changes: 1 addition & 1 deletion examples/excalidraw/with-script-in-browser/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# copied assets
public/*.woff2
public/**/*.woff2
2 changes: 1 addition & 1 deletion excalidraw-app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<!-- Register Assistant as the UI font, before the scene inits -->
<link
rel="stylesheet"
href="../packages/excalidraw/fonts/css/fonts.css"
href="../packages/excalidraw/fonts/fonts.css"
type="text/css"
/>

Expand Down
10 changes: 2 additions & 8 deletions excalidraw-app/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ export default defineConfig({
output: {
assetFileNames(chunkInfo) {
if (chunkInfo?.name?.endsWith(".woff2")) {
// TODO: consider splitting all fonts similar to Xiaolai
// fonts don't change often, so hash is not necessary
// put on root so we are flexible about the CDN path
if (chunkInfo.name.includes("Xiaolai")) {
return "[name][extname]";
} else {
return "[name]-[hash][extname]";
}
const family = chunkInfo.name.split("-")[0];
return `fonts/${family}/[name][extname]`;
}

return "assets/[name]-[hash][extname]";
Expand Down
12 changes: 4 additions & 8 deletions packages/excalidraw/actions/actionHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { t } from "../i18n";
import type { History } from "../history";
import { HistoryChangedEvent } from "../history";
import type { AppClassProperties, AppState } from "../types";
import { KEYS } from "../keys";
import { KEYS, matchKey } from "../keys";
import { arrayToMap } from "../utils";
import { isWindows } from "../constants";
import type { SceneElementsMap } from "../element/types";
Expand Down Expand Up @@ -63,9 +63,7 @@ export const createUndoAction: ActionCreator = (history, store) => ({
),
),
keyTest: (event) =>
event[KEYS.CTRL_OR_CMD] &&
event.key.toLowerCase() === KEYS.Z &&
!event.shiftKey,
event[KEYS.CTRL_OR_CMD] && matchKey(event, KEYS.Z) && !event.shiftKey,
PanelComponent: ({ updateData, data }) => {
const { isUndoStackEmpty } = useEmitter<HistoryChangedEvent>(
history.onHistoryChangedEmitter,
Expand Down Expand Up @@ -104,10 +102,8 @@ export const createRedoAction: ActionCreator = (history, store) => ({
),
),
keyTest: (event) =>
(event[KEYS.CTRL_OR_CMD] &&
event.shiftKey &&
event.key.toLowerCase() === KEYS.Z) ||
(isWindows && event.ctrlKey && !event.shiftKey && event.key === KEYS.Y),
(event[KEYS.CTRL_OR_CMD] && event.shiftKey && matchKey(event, KEYS.Z)) ||
(isWindows && event.ctrlKey && !event.shiftKey && matchKey(event, KEYS.Y)),
PanelComponent: ({ updateData, data }) => {
const { isRedoStackEmpty } = useEmitter(
history.onHistoryChangedEmitter,
Expand Down
4 changes: 2 additions & 2 deletions packages/excalidraw/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ class App extends React.Component<AppProps, AppState> {
id: this.id,
};

this.fonts = new Fonts({ scene: this.scene });
this.fonts = new Fonts(this.scene);
this.history = new History();

this.actionManager.registerAll(actions);
Expand Down Expand Up @@ -2580,7 +2580,7 @@ class App extends React.Component<AppProps, AppState> {
this.renderer.destroy();
this.scene.destroy();
this.scene = new Scene();
this.fonts = new Fonts({ scene: this.scene });
this.fonts = new Fonts(this.scene);
this.renderer = new Renderer(this.scene);
this.files = {};
this.imageCache.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CascadiaCodeRegular from "./CascadiaCode-Regular.woff2";
import { type ExcalidrawFontFaceDescriptor } from "../..";

import { type ExcalidrawFontFaceDescriptor } from "../Fonts";

export const CascadiaFontFaces: ExcalidrawFontFaceDescriptor[] = [
{
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12,221 changes: 12,221 additions & 0 deletions packages/excalidraw/fonts/ComicShanns/ComicShanns-Regular.sfd

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions packages/excalidraw/fonts/ComicShanns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// The following file content was generated with https://chinese-font.netlify.app/online-split,
// but has been manully rewritten from `@font-face` rules into TS while leveraging FontFace API.

import _0 from "./ComicShanns-Regular-279a7b317d12eb88de06167bd672b4b4.woff2";
import _1 from "./ComicShanns-Regular-fcb0fc02dcbee4c9846b3e2508668039.woff2";
import _2 from "./ComicShanns-Regular-dc6a8806fa96795d7b3be5026f989a17.woff2";
import _3 from "./ComicShanns-Regular-6e066e8de2ac57ea9283adb9c24d7f0c.woff2";

import { type ExcalidrawFontFaceDescriptor } from "../Fonts";

/* Generated By [email protected] https://www.npmjs.com/package/cn-font-split
CreateTime: Thu, 17 Oct 2024 09:57:51 GMT;
Origin File Name Table:
copyright: MIT License
Copyright (c) 2018 Shannon Miwa
Copyright (c) 2023 Jesus Gonzalez
Copyright (c) 2023 Rodrigo Batista de Moraes
Copyright (c) 2024 Fini Jastrow
Copyright (c) 2024 Kyle Beechly
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
fontFamily: Comic Shanns Mono-Regular
fontSubfamily: Regular
uniqueID: FontForge 2.0 : Comic Shanns Mono Regular : 17-10-2024
fullName: Comic Shanns Mono Regular
version: 1.3.0
postScriptName: ComicShannsMono-Regular
license: MIT License
Copyright (c) 2018 Shannon Miwa
Copyright (c) 2023 Jesus Gonzalez
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
preferredFamily: Comic Shanns Mono
*/

export const ComicShannsFontFaces: ExcalidrawFontFaceDescriptor[] = [
{
uri: _0,
descriptors: {
unicodeRange:
"U+20-7e,U+a1-a6,U+a8,U+ab-ac,U+af-b1,U+b4,U+b8,U+bb-bc,U+bf-cf,U+d1-d7,U+d9-de,U+e0-ef,U+f1-f7,U+f9-ff,U+131,U+152-153,U+2c6,U+2da,U+2dc,U+2013-2014,U+2018-201a,U+201c-201d,U+2020-2022,U+2026,U+2039-203a,U+2044,U+20ac,U+2191,U+2193,U+2212",
},
},
{
uri: _1,
descriptors: {
unicodeRange:
"U+100-10f,U+112-125,U+128-130,U+134-137,U+139-13c,U+141-148,U+14c-151,U+154-161,U+164-165,U+168-17f,U+1bf,U+1f7,U+218-21b,U+237,U+1e80-1e85,U+1ef2-1ef3,U+a75b",
},
},
{
uri: _2,
descriptors: {
unicodeRange:
"U+2c7,U+2d8-2d9,U+2db,U+2dd,U+315,U+2190,U+2192,U+2200,U+2203-2204,U+2264-2265,U+f6c3",
},
},
{
uri: _3,
descriptors: { unicodeRange: "U+3bb" },
},
];
9 changes: 9 additions & 0 deletions packages/excalidraw/fonts/Emoji/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { LOCAL_FONT_PROTOCOL } from "../FontMetadata";

import { type ExcalidrawFontFaceDescriptor } from "../Fonts";

export const EmojiFontFaces: ExcalidrawFontFaceDescriptor[] = [
{
uri: LOCAL_FONT_PROTOCOL,
},
];
31 changes: 13 additions & 18 deletions packages/excalidraw/fonts/ExcalidrawFontFace.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
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";
import { LOCAL_FONT_PROTOCOL } from "./FontMetadata";
import { subsetWoff2GlyphsByCodepoints } from "../subset/subset-main";

type DataURL = string;

export interface IExcalidrawFontFace {
urls: URL[] | DataURL[];
fontFace: FontFace;
toCSS(
characters: string,
codePoints: Array<number>,
): Promise<string> | undefined;
getContentLegacy(): Promise<string>; //zsviczian
}

export class ExcalidrawFontFace implements IExcalidrawFontFace {
export class ExcalidrawFontFace {
public readonly urls: URL[] | DataURL[];
public readonly fontFace: FontFace;

Expand Down Expand Up @@ -46,10 +36,7 @@ export class ExcalidrawFontFace implements IExcalidrawFontFace {
*
* Retrieves `undefined` otherwise.
*/
public toCSS(
characters: string,
codePoints: Array<number>,
): Promise<string> | undefined {
public toCSS(characters: string): Promise<string> | undefined {
// quick exit in case the characters are not within this font face's unicode range
if (!this.getUnicodeRangeRegex().test(characters)) {
return;
Expand All @@ -59,8 +46,12 @@ export class ExcalidrawFontFace implements IExcalidrawFontFace {
if(typeof this.urls[0] === "string" && !this.urls[0].startsWith("data:font/woff2")) {
return Promise.resolve(`@font-face { font-family: ${this.fontFace.family}; src: url(${this.urls[0]}); }`);
}

const codepoints = Array.from(characters).map(
(char) => char.codePointAt(0)!,
);

return this.getContent(codePoints).then(
return this.getContent(codepoints).then(
(content) =>
`@font-face { font-family: ${this.fontFace.family}; src: url(${content}); }`,
);
Expand Down Expand Up @@ -110,6 +101,10 @@ export class ExcalidrawFontFace implements IExcalidrawFontFace {
return result;
}
const response = await fetch(url, {
// always prefer cache (even stale), otherwise it always triggers an unnecessary validation request
// which we don't need as we are controlling freshness of the fonts with the stable hash suffix in the url
// https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
cache: "force-cache",
headers: {
Accept: "font/woff2",
},
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 8423c68

Please sign in to comment.