Skip to content

Commit

Permalink
fix: Comic Shanns issues, new fonts structure (excalidraw#8641)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrazator authored Oct 20, 2024
1 parent 15ca182 commit 61623bb
Show file tree
Hide file tree
Showing 279 changed files with 13,267 additions and 488 deletions.
2 changes: 1 addition & 1 deletion examples/excalidraw/with-nextjs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ yarn-error.log*
next-env.d.ts

# copied assets
public/*.woff2
public/**/*.woff2
2 changes: 1 addition & 1 deletion examples/excalidraw/with-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"build:workspace": "yarn workspace @excalidraw/excalidraw run build:esm && yarn copy:assets",
"copy:assets": "cp ../../../packages/excalidraw/dist/browser/prod/excalidraw-assets/*.woff2 ./public",
"copy:assets": "cp -r ../../../packages/excalidraw/dist/prod/fonts ./public",
"dev": "yarn build:workspace && next dev -p 3005",
"build": "yarn build:workspace && next build",
"start": "next start -p 3006",
Expand Down
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 examples/excalidraw/with-script-in-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"scripts": {
"build:workspace": "yarn workspace @excalidraw/excalidraw run build:esm && yarn copy:assets",
"copy:assets": "cp ../../../packages/excalidraw/dist/browser/prod/excalidraw-assets/*.woff2 ./public",
"copy:assets": "cp -r ../../../packages/excalidraw/dist/prod/fonts ./public",
"start": "yarn build:workspace && vite",
"build": "yarn build:workspace && vite build",
"build:preview": "yarn build && vite preview --port 5002"
Expand Down
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
4 changes: 2 additions & 2 deletions packages/excalidraw/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,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 @@ -2471,7 +2471,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,
},
];
30 changes: 13 additions & 17 deletions packages/excalidraw/fonts/ExcalidrawFontFace.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
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;
}

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

Expand Down Expand Up @@ -43,16 +34,17 @@ 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;
}

return this.getContent(codePoints).then(
const codepoints = Array.from(characters).map(
(char) => char.codePointAt(0)!,
);

return this.getContent(codepoints).then(
(content) =>
`@font-face { font-family: ${this.fontFace.family}; src: url(${content}); }`,
);
Expand Down Expand Up @@ -98,6 +90,10 @@ export class ExcalidrawFontFace implements IExcalidrawFontFace {
public fetchFont(url: URL | DataURL): Promise<ArrayBuffer> {
return promiseTry(async () => {
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 61623bb

Please sign in to comment.