Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Support importing custom fonts in Docker deployments https://github.com/siyuan-note/siyuan/issues/19539

Daniel993e7001a5c2
diff --git a/app/src/config/tabs/appearanceTab.ts b/app/src/config/tabs/appearanceTab.ts
index 281d9fa1af..4bb23668cb 100644
--- a/app/src/config/tabs/appearanceTab.ts
+++ b/app/src/config/tabs/appearanceTab.ts
@@ -34,7 +34,7 @@ import {setEditorFontSize} from "../../util/editorFontSize";
import {
ICustomFont,
invalidateCustomFonts,
- isNativeMobileContainer,
+ supportsCustomFonts,
loadCustomFonts,
registerCustomFont,
unregisterCustomFont
@@ -83,13 +83,13 @@ const isCodeFont = (font: Pick<IFontItem, "spacing">) =>
font.spacing === "monospace" || font.spacing === "dual" || font.spacing === "character-cell";
const loadAvailableFonts = async () => {
- const nativeMobile = isNativeMobileContainer();
+ const customFontSupported = supportsCustomFonts();
const [systemFonts, customFonts] = await Promise.all([
loadSystemFonts(),
- nativeMobile ? loadCustomFonts() : Promise.resolve([] as ICustomFont[])
+ customFontSupported ? loadCustomFonts() : Promise.resolve([] as ICustomFont[])
]);
return {
- nativeMobile,
+ customFontSupported,
customFonts,
fontItems: [...customFonts, ...systemFonts],
};
@@ -383,7 +383,7 @@ const mountAppearanceFontFamily = (root: HTMLElement, configKey: FontFamiliesCon
fontMenu.close();
return;
}
- const {nativeMobile, customFonts, fontItems} = availableFonts;
+ const {customFontSupported, customFonts, fontItems} = availableFonts;
selectedFonts = getConfiguredFonts(getFontConfig(), configKey).map((selectedFont) =>
fontItems.find((font) => font.family === selectedFont.family && font.weight === selectedFont.weight) ||
selectedFont);
@@ -392,7 +392,7 @@ const mountAppearanceFontFamily = (root: HTMLElement, configKey: FontFamiliesCon
genFontListItemHtml(item, selectedFonts.some((font) =>
font.family === item.family && font.weight === item.weight))
).join("");
- const canManageCustomFonts = nativeMobile && !window.siyuan.config.readonly;
+ const canManageCustomFonts = customFontSupported && !window.siyuan.config.readonly;
const canShowAllFonts = configKey === "codeFontFamilies" && fontItems.some((font) => !isCodeFont(font));
const customFontsByID = new Map(customFonts.map((font) => [font.id, font]));
fontMenu.addItem({
@@ -404,7 +404,7 @@ const mountAppearanceFontFamily = (root: HTMLElement, configKey: FontFamiliesCon
${canShowAllFonts ? `<span class="fn__space"></span><button class="b3-button b3-button--outline fn__flex-center" data-type="show-all-fonts">${escapeHtml(window.siyuan.languages.showAll)}</button>` : ""}
${canManageCustomFonts ? `<span class="fn__space"></span><button class="b3-button b3-button--outline fn__flex-center" data-type="import-font"><svg><use xlink:href="#iconDownload"></use></svg>${escapeHtml(window.siyuan.languages.importFont)}</button>` : ""}
</div>
- ${nativeMobile ? `<div class="b3-label__text ft__on-surface" style="margin-top: 8px">${escapeHtml(window.siyuan.languages.fontFileTip)}</div>` : ""}
+ ${customFontSupported ? `<div class="b3-label__text ft__on-surface" style="margin-top: 8px">${escapeHtml(window.siyuan.languages.fontFileTip)}</div>` : ""}
${canManageCustomFonts ? '<input class="fn__none" data-type="font-file" type="file" accept=".ttf,.otf,font/ttf,font/otf">' : ""}
<div class="fn__hr"></div>
<div class="b3-list fn__flex-1 b3-list--background" data-type="available-fonts">${fontItemHtml}</div>
diff --git a/app/src/util/assets.ts b/app/src/util/assets.ts
index 6c6ddb4de0..a8a3c766f4 100644
--- a/app/src/util/assets.ts
+++ b/app/src/util/assets.ts
@@ -391,7 +391,7 @@ export const setInlineStyle = async (set = true, servePath = "../../../") => {
}`;
}
style += getGlobalFontStyle(globalFonts);
- if (!set && globalFonts.length > 0) {
+ if (!set) {
style += "\n" + await getExportCustomFontStyle([...globalFonts, ...editorFonts, ...codeFonts]);
}
const editorFontFamilies = editorFonts.map((font) => CSS.escape(font.family)).join(", ");
diff --git a/app/src/util/customFont.test.ts b/app/src/util/customFont.test.ts
new file mode 100644
index 0000000000..c541e7f412
--- /dev/null
+++ b/app/src/util/customFont.test.ts
@@ -0,0 +1,80 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {resolve} from "node:path";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, transpileModule} from "typescript";
+
+const evaluate = (path: string, context: Record<string, unknown>, suffix = "") => {
+ const source = readFileSync(resolve(process.cwd(), path), "utf8") + suffix;
+ const exports: Record<string, (...args: any[]) => any> = {};
+ runInNewContext(transpileModule(source, {
+ compilerOptions: {module: ModuleKind.CommonJS},
+ }).outputText, {exports, ...context});
+ return exports;
+};
+
+for (const container of ["docker", "android", "ios", "harmony", "std", "unknown"]) {
+ test(`appearance font list includes uploaded fonts only on supported containers: ${container}`, async () => {
+ const window = {siyuan: {config: {system: {container}}}};
+ let customRequests = 0;
+ const font = {id: "a".repeat(64), family: "SiYuanCustomFont-" + "a".repeat(64), weight: 400};
+ const customFont = evaluate("src/util/customFont.ts", {
+ window,
+ require: () => ({fetchSyncPost: async () => {
+ customRequests++;
+ return {data: [font]};
+ }}),
+ });
+ const appearance = evaluate("src/config/tabs/appearanceTab.ts", {
+ window,
+ require: (name: string) => {
+ if (name === "../../util/customFont") {
+ return customFont;
+ }
+ if (name === "../../util/systemFont") {
+ return {loadSystemFonts: async (): Promise<unknown[]> => []};
+ }
+ return {};
+ },
+ }, "\nexports.loadAvailableFonts = loadAvailableFonts;");
+ const result = await appearance.loadAvailableFonts();
+ const supported = ["docker", "android", "ios", "harmony"].includes(container);
+ assert.equal(customRequests, supported ? 1 : 0);
+ assert.equal(result.fontItems.length, supported ? 1 : 0);
+ if (supported) {
+ assert.equal(result.fontItems[0].family, font.family);
+ }
+ });
+}
+
+for (const key of ["globalFontFamilies", "fontFamilies", "codeFontFamilies"]) {
+ test(`export embeds custom fonts when only ${key} is configured`, async () => {
+ const font = {family: "SiYuanCustomFont-" + "a".repeat(64), weight: 400};
+ const appearance = {globalFontFamilies: key === "globalFontFamilies" ? [font] : []};
+ const editor = {
+ fontFamilies: key === "fontFamilies" ? [font] : [],
+ codeFontFamilies: key === "codeFontFamilies" ? [font] : [],
+ fontSize: 16,
+ };
+ const assets = evaluate("src/util/assets.ts", {
+ window: {siyuan: {config: {appearance, editor}}},
+ document: {},
+ CSS: {escape: (value: string) => value},
+ require: (name: string) => {
+ if (name === "./hostCapabilities") {
+ return {getHostCapabilities: () => ({customAppearance: true})};
+ }
+ if (name === "./customFont") {
+ return {getExportCustomFontStyle: async (fonts: typeof font[]) => {
+ assert.equal(fonts.length, 1);
+ assert.equal(fonts[0].family, font.family);
+ return "@font-face { src: url(data:font/ttf;base64,AA==); }";
+ }};
+ }
+ return new Proxy({}, {get: () => () => ""});
+ },
+ });
+ assert.match(await assets.setInlineStyle(false), /data:font\/ttf;base64,AA==/);
+ });
+}
diff --git a/app/src/util/customFont.ts b/app/src/util/customFont.ts
index 2141efbbe2..c4b5c149d8 100644
--- a/app/src/util/customFont.ts
+++ b/app/src/util/customFont.ts
@@ -16,8 +16,8 @@ let customFontsPromise: Promise<ICustomFont[]> | undefined;
const registeredFonts = new Map<string, ICustomFont>();
const fontLoadPromises = new Map<string, Promise<FontFace[]>>();
-export const isNativeMobileContainer = () => {
- return ["android", "ios", "harmony"].includes(window.siyuan.config.system.container);
+export const supportsCustomFonts = () => {
+ return ["docker", "android", "ios", "harmony"].includes(window.siyuan.config.system.container);
};
export const loadCustomFonts = () => {