Skip to content

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

:bug: Align emoji icons with the adjacent text baseline https://github.com/siyuan-note/siyuan/issues/20731

Jeffrey Chen1c8ac296dc81
diff --git a/app/appearance/themes/daylight/theme.css b/app/appearance/themes/daylight/theme.css
index 4ba1a894fe..9bb630f069 100644
--- a/app/appearance/themes/daylight/theme.css
+++ b/app/appearance/themes/daylight/theme.css
@@ -30,7 +30,9 @@
--b3-font-family-protyle: var(--b3-font-family);
--b3-font-family-code: var(--b3-font-family-emoji-reset), "JetBrainsMono-Regular", mononoki, Consolas, "Liberation Mono", var(--b3-font-family), monospace;
--b3-font-family-graph: arial;
- --b3-font-family-emoji: "Emojis Additional", emojis;
+ /* 图标位统一使用按字形墨迹对齐的度量:"Emojis Icon" 仅在 Windows 11 声明,其他平台回退到 emojis。
+ 正文用的 --b3-font-family-default 仍以 emojis 兜底,保持行内表情原有的基线位置 */
+ --b3-font-family-emoji: "Emojis Additional", "Emojis Icon", emojis;
--b3-font-family-math: KaTeX_Math;
--b3-font-family-kbd: var(--b3-font-family-protyle);
--b3-font-size: 14px;
diff --git a/app/appearance/themes/midnight/theme.css b/app/appearance/themes/midnight/theme.css
index 1493eeda60..eb6dfadc5a 100644
--- a/app/appearance/themes/midnight/theme.css
+++ b/app/appearance/themes/midnight/theme.css
@@ -29,7 +29,9 @@
--b3-font-family-protyle: var(--b3-font-family);
--b3-font-family-code: var(--b3-font-family-emoji-reset), "JetBrainsMono-Regular", mononoki, Consolas, "Liberation Mono", var(--b3-font-family), monospace;
--b3-font-family-graph: arial;
- --b3-font-family-emoji: "Emojis Additional", emojis;
+ /* 图标位统一使用按字形墨迹对齐的度量:"Emojis Icon" 仅在 Windows 11 声明,其他平台回退到 emojis。
+ 正文用的 --b3-font-family-default 仍以 emojis 兜底,保持行内表情原有的基线位置 */
+ --b3-font-family-emoji: "Emojis Additional", "Emojis Icon", emojis;
--b3-font-family-math: KaTeX_Math;
--b3-font-family-kbd: var(--b3-font-family-protyle);
--b3-font-size: 14px;
diff --git a/app/src/assets/scss/business/_av.scss b/app/src/assets/scss/business/_av.scss
index b24416a5f6..6c9489b437 100644
--- a/app/src/assets/scss/business/_av.scss
+++ b/app/src/assets/scss/business/_av.scss
@@ -1703,7 +1703,8 @@
align-items: center;
justify-content: center;
object-fit: contain;
- line-height: 1;
+ // 与相邻的单元格文字使用同一行高模型,字形才不会相对文字偏高
+ line-height: inherit;
color: var(--b3-theme-on-surface);
margin: 0 5px 0 0;
flex-shrink: 0;
diff --git a/app/src/assets/scss/component/_list.scss b/app/src/assets/scss/component/_list.scss
index 6c6f0cddb0..b316cefb2e 100644
--- a/app/src/assets/scss/component/_list.scss
+++ b/app/src/assets/scss/component/_list.scss
@@ -311,20 +311,12 @@
align-items: center;
justify-content: center;
- body.body--windows & > .b3-list-item__emoji {
- transform: translateY(-1px);
- }
-
&:hover {
color: var(--b3-theme-on-background);
background-color: var(--b3-list-icon-hover);
}
}
- &__emoji {
- pointer-events: none;
- }
-
&__hinttext {
overflow: hidden;
white-space: nowrap;
diff --git a/app/src/assets/scss/main/_mobile.scss b/app/src/assets/scss/main/_mobile.scss
index f6e5d203de..0037968d39 100644
--- a/app/src/assets/scss/main/_mobile.scss
+++ b/app/src/assets/scss/main/_mobile.scss
@@ -771,6 +771,8 @@ body.mobile-topbar--merged {
margin-right: 8px;
color: var(--b3-theme-on-surface-light);
font-size: 16px;
+ font-family: var(--b3-font-family-emoji);
+ font-variant-emoji: emoji;
// 行高与条目等高,emoji 图标才不会顶出自身盒子而与文字错位
line-height: 40px;
diff --git a/app/src/emoji/fileTreeIcon.test.ts b/app/src/emoji/fileTreeIcon.test.ts
index 8161c9ae86..ef1440188b 100644
--- a/app/src/emoji/fileTreeIcon.test.ts
+++ b/app/src/emoji/fileTreeIcon.test.ts
@@ -74,16 +74,16 @@ describe("file tree icon", () => {
});
it("keeps the Emoji defaults when SVG icons are disabled", () => {
- assert.equal(getFileTreeIconHTML("", "notebook", "", false, false), '<span class="b3-list-item__emoji">🗃</span>');
- assert.equal(getFileTreeIconHTML("", "folder", "", false, false), '<span class="b3-list-item__emoji">📑</span>');
- assert.equal(getFileTreeIconHTML("", "file", "", false, false), '<span class="b3-list-item__emoji">📄</span>');
- assert.equal(getFileTreeIconHTML("", "lock", "", false, false), '<span class="b3-list-item__emoji">🔒️</span>');
+ assert.equal(getFileTreeIconHTML("", "notebook", "", false, false), "🗃");
+ assert.equal(getFileTreeIconHTML("", "folder", "", false, false), "📑");
+ assert.equal(getFileTreeIconHTML("", "file", "", false, false), "📄");
+ assert.equal(getFileTreeIconHTML("", "lock", "", false, false), "🔒️");
assert.equal(getDocumentIconHTML("", "mobile-tabs__item-icon", false),
'<span class="mobile-tabs__item-icon">📄</span>');
});
it("treats an explicitly configured icon as custom", () => {
- assert.equal(getFileTreeIconHTML("1f4c4", "folder", "", false, true), '<span class="b3-list-item__emoji">📄</span>');
+ assert.equal(getFileTreeIconHTML("1f4c4", "folder", "", false, true), "📄");
assert.equal(getDocumentIconHTML("1f4c4", "mobile-tabs__item-icon", true),
'<span class="mobile-tabs__item-icon">📄</span>');
assert.equal(getFileTreeDefaultIconAttr("1f4c4", "folder"), "");
@@ -111,13 +111,13 @@ describe("file tree icon", () => {
updateFileTreeItemIcon(liElement, "1f4c4");
assert.equal("defaultIcon" in dataset, false);
- assert.equal(iconElement.innerHTML, '<span class="b3-list-item__emoji">📄</span>');
+ assert.equal(iconElement.innerHTML, "📄");
updateFileTreeItemIcon(liElement, "", "file");
window.siyuan.config.fileTree.useSVGDefaultIcon = false;
refreshDefaultFileTreeIcons({querySelectorAll: () => [liElement]} as unknown as ParentNode);
assert.equal(dataset.defaultIcon, "file");
- assert.equal(iconElement.innerHTML, '<span class="b3-list-item__emoji">📄</span>');
+ assert.equal(iconElement.innerHTML, "📄");
});
it("refreshes locked notebook icons with the SVG setting", () => {
@@ -131,6 +131,6 @@ describe("file tree icon", () => {
assert.match(iconElement.innerHTML, /#iconLock/);
window.siyuan.config.fileTree.useSVGDefaultIcon = false;
refreshDefaultFileTreeIcons({querySelectorAll: () => [liElement]} as unknown as ParentNode);
- assert.equal(iconElement.innerHTML, '<span class="b3-list-item__emoji">🔒️</span>');
+ assert.equal(iconElement.innerHTML, "🔒️");
});
});
diff --git a/app/src/emoji/fileTreeIcon.ts b/app/src/emoji/fileTreeIcon.ts
index 6ab6d69bf3..b5897c253b 100644
--- a/app/src/emoji/fileTreeIcon.ts
+++ b/app/src/emoji/fileTreeIcon.ts
@@ -28,11 +28,6 @@ const isDefaultIcon = (value: string | undefined): value is TFileTreeDefaultIcon
const getDirectIconElement = (liElement: HTMLElement) => Array.from(liElement.children).find((item) =>
item.classList.contains("b3-list-item__icon") || item.classList.contains("b3-list-item__graphic")) as HTMLElement | undefined;
-const renderFileTreeEmoji = (icon: string, className: string, needSpan: boolean) => {
- const html = unicode2Emoji(icon, className, needSpan);
- return html && !html.startsWith("<") ? `<span class="b3-list-item__emoji">${html}</span>` : html;
-};
-
export const getFileTreeDefaultIconAttr = (icon: string, defaultIcon: TFileTreeDefaultIcon, disabled = false) =>
!icon && !disabled ? ` data-default-icon="${defaultIcon}"` : "";
@@ -40,11 +35,11 @@ export const getFileTreeIconHTML = (icon: string, defaultIcon: TFileTreeDefaultI
needSpan = false, useSVGDefaultIcon =
window.siyuan.config.fileTree.useSVGDefaultIcon === true) => {
if (icon) {
- return renderFileTreeEmoji(icon, className, needSpan);
+ return unicode2Emoji(icon, className, needSpan);
}
if (!useSVGDefaultIcon) {
- return renderFileTreeEmoji(getDefaultEmoji(defaultIcon), className, needSpan);
+ return unicode2Emoji(getDefaultEmoji(defaultIcon), className, needSpan);
}
const classAttr = needSpan ? ` class="${escapeAttr(className)}"` : "";
@@ -80,7 +75,7 @@ export const updateFileTreeItemIcon = (liElement: HTMLElement, icon: string,
}
if (icon) {
delete liElement.dataset.defaultIcon;
- iconElement.innerHTML = getFileTreeIconHTML(icon, defaultIcon);
+ iconElement.innerHTML = unicode2Emoji(icon);
return;
}
liElement.dataset.defaultIcon = defaultIcon;
diff --git a/app/src/util/emojiFont.test.ts b/app/src/util/emojiFont.test.ts
index 6633b53ce4..d3446b802d 100644
--- a/app/src/util/emojiFont.test.ts
+++ b/app/src/util/emojiFont.test.ts
@@ -8,6 +8,10 @@ const appPath = path.resolve(__dirname, "../..");
const sequences: string[] = JSON.parse(readFileSync(path.join(appPath, "tests/fixtures/unicode17-emoji.json"), "utf8"));
const categories: IEmoji[] = JSON.parse(readFileSync(path.join(appPath, "appearance/emojis/conf.json"), "utf8"));
+// 按 family 取出单个 @font-face 声明,避免新增字体块改变既有断言指向的声明
+const getFace = (style: string, family: string) => style.split("@font-face").find(face =>
+ face.includes(`font-family: "${family}"`)) || "";
+
describe("Unicode 17 emoji fonts", () => {
it("covers every code point of the 163 new fully qualified sequences in each platform override", () => {
assert.equal(sequences.length, 163);
@@ -30,13 +34,26 @@ describe("Unicode 17 emoji fonts", () => {
it("keeps native emoji sources on Apple and Windows 11 and bundled sources elsewhere", () => {
for (const [platform, native] of [["apple", "Apple Color Emoji"], ["windows11", "Segoe UI Emoji"]] as const) {
- const style = getEmojiFontStyle(platform, "");
- const mainFace = style.slice(style.lastIndexOf("@font-face"));
+ const mainFace = getFace(getEmojiFontStyle(platform, ""), "Emojis");
assert.ok(mainFace.includes(`src: local("${native}")`));
assert.ok(!mainFace.includes(NOTO_EMOJI_FONT_PATH));
}
- const other = getEmojiFontStyle("other", "");
- assert.ok(other.slice(other.lastIndexOf("@font-face")).includes(`src: url(${NOTO_EMOJI_FONT_PATH})`));
+ assert.ok(getFace(getEmojiFontStyle("other", ""), "Emojis").includes(`src: url(${NOTO_EMOJI_FONT_PATH})`));
+ });
+
+ it("declares an ink-aligned icon face only where the native emoji metrics are off-center", () => {
+ // 图标需与相邻文字的视觉中心对齐:Segoe UI Emoji 原度量会让字形比中文偏低约 0.75px,
+ // 因此把 ascent/descent 改写为 80%/19%,和仍保持 99% 不改变行盒高度。
+ const iconFace = getFace(getEmojiFontStyle("windows11", ""), "Emojis Icon");
+ assert.ok(iconFace.includes('local("Segoe UI Emoji")'));
+ assert.ok(iconFace.includes("size-adjust: 85%"));
+ assert.ok(iconFace.includes("ascent-override: 80%"));
+ assert.ok(iconFace.includes("descent-override: 19%"));
+ assert.ok(iconFace.includes("line-gap-override: 0%"));
+ // 其他平台的字体栈与文字度量不同,尚未标定,保持原有声明。
+ for (const platform of ["apple", "other"] as const) {
+ assert.equal(getFace(getEmojiFontStyle(platform, ""), "Emojis Icon"), "");
+ }
});
it("resolves the same bundled font from app, PDF and offline HTML paths", () => {
diff --git a/app/src/util/emojiFont.ts b/app/src/util/emojiFont.ts
index 9fe03a9de6..f3889014a3 100644
--- a/app/src/util/emojiFont.ts
+++ b/app/src/util/emojiFont.ts
@@ -57,6 +57,18 @@ export const getEmojiFontStyle = (platform: "apple" | "windows11" | "other", ser
src: local("Segoe UI Emoji"),
local("Segoe UI Symbol");
size-adjust: 85%;
+}
+/* 图标位使用:图标旁边是文字,因此要对齐的是文字的视觉中心而不是图标框中心。
+ Segoe UI Emoji 的字体框为 1.08/0.25,图标墨迹约为 0.875/0.141,按原度量居中会让字形比中文偏低约 0.75px;
+ 把 ascent/descent 改写为 80%/19%(和仍为 99%,行盒高度不变)后,字形与相邻文字对齐,且随字号等比生效。 */
+@font-face {
+ font-family: "Emojis Icon";
+ src: local("Segoe UI Emoji"),
+ local("Segoe UI Symbol");
+ size-adjust: 85%;
+ ascent-override: 80%;
+ descent-override: 19%;
+ line-gap-override: 0%;
}`;
} else {
style = `@font-face {
diff --git a/app/tests/emojiIconAlignment.test.js b/app/tests/emojiIconAlignment.test.js
new file mode 100644
index 0000000000..09da848a3e
--- /dev/null
+++ b/app/tests/emojiIconAlignment.test.js
@@ -0,0 +1,282 @@
+// 校验 emoji 图标槽与「相邻文字的视觉中心」对齐。
+// 图标旁边是文字,因此参照物是文字墨迹中心,而不是图标框中心:
+// 中文在 14px/28px 行盒里的墨迹中心比行盒中心高约 1.25px,若只让字形居中于图标框,看起来仍然偏低。
+const assert = require("node:assert/strict");
+const fs = require("node:fs");
+const path = require("node:path");
+const os = require("node:os");
+
+const root = path.resolve(__dirname, "..");
+const GLYPH = "\u{1F4C4}";
+const TEXT = "\u6587\u6863";
+const TREE_ICONS = {file: "1f4c4", notebook: "1f5c3", lock: "1f512", rocket: "1f680", person: "1f9d1"};
+// 以 2 倍设备像素比采样,把 1x 抗锯齿带来的 1px 量化误差降到 0.5px 以内
+const SCALE = 2;
+// 与相邻文字对齐的容差;未标定的平台(内置 Noto 分支)放宽
+const TEXT_TOLERANCE = 0.75;
+const UNCALIBRATED_TOLERANCE = 1.5;
+// 独立格子里没有文字可参照,度量按文字对齐标定后会略微上移,这里只限制在可见范围内
+const STANDALONE_TOLERANCE = 2;
+
+const icon = () => `<span class="b3-list-item__icon" id="icon">${GLYPH}</span>`;
+const text = (className) => `<span class="${className}" id="text">${TEXT}</span>`;
+
+// 每个图标位使用最贴近真实标记的结构;reference 指明该位置的对齐参照物
+const SITES = [
+ {name: "document tree icon", reference: "text",
+ markup: `<div class="file-tree sy__file"><ul class="b3-list"><li class="b3-list-item" id="row">
+ ${icon()}${text("b3-list-item__text")}</li></ul></div>`},
+ {name: "document tree graphic", reference: "text",
+ markup: `<ul class="b3-list"><li class="b3-list-item" id="row">
+ <span class="b3-list-item__graphic" id="icon">${GLYPH}</span>
+ ${text("b3-list-item__text")}</li></ul>`},
+ {name: "tab icon", reference: "text",
+ markup: `<div class="layout-tab-bar"><div class="item" id="row">
+ <span class="item__icon" id="icon">${GLYPH}</span>
+ ${text("item__text")}</div></div>`},
+ {name: "hint emoji", reference: "text",
+ markup: `<div class="b3-menu"><button class="b3-menu__item" id="row">
+ <span class="b3-menu__emoji" id="icon">${GLYPH}</span>
+ ${text("b3-menu__label")}</button></div>`},
+ {name: "database cell icon", reference: "text",
+ markup: `<div class="av"><div class="av__row"><div class="av__cell av__cell--header" id="row">
+ <span class="av__cellheadericon" id="icon">${GLYPH}</span>
+ ${text("av__celltext")}</div></div></div>`},
+ {name: "mobile tab icon", reference: "text", entry: "mobile",
+ markup: `<div class="mobile-tabs"><div class="mobile-tabs__item" id="row">
+ <span class="mobile-tabs__item-icon" id="icon">${GLYPH}</span>
+ ${text("mobile-tabs__item-title")}</div></div>`},
+ {name: "emoji picker cell", reference: "box",
+ markup: `<div class="emojis"><div class="emojis__content" id="row">
+ <button class="emojis__item" id="icon">${GLYPH}</button></div></div>`},
+ {name: "emoji category icon", reference: "box",
+ markup: `<div class="emojis"><div class="emojis__types" id="row">
+ <button class="emojis__type" id="icon">${GLYPH}</button></div></div>`},
+ {name: "document background icon", reference: "box",
+ markup: `<div class="protyle-background" id="row">
+ <span class="protyle-background__icon" id="icon">${GLYPH}</span></div>`},
+];
+
+const getFileTreeIconHTML = (iconValue, defaultIcon, className = "", needSpan = false, useSVGDefaultIcon) => {
+ const ts = require("typescript");
+ globalThis.window = {siyuan: {config: {fileTree: {useSVGDefaultIcon: false}},
+ storage: {"local-images": {note: TREE_ICONS.notebook, folder: "1f4d1", file: TREE_ICONS.file}}}};
+ // 该测试只关心几何对齐,Sanitize 用直通实现即可。
+ globalThis.Lute = {Sanitize: html => html};
+ const modules = {"../constants": {Constants: {LOCAL_IMAGES: "local-images"}}};
+ for (const [name, file] of [["../util/escape", "src/util/escape.ts"], ["./iconValue", "src/emoji/iconValue.ts"],
+ ["fileTree", "src/emoji/fileTreeIcon.ts"]]) {
+ const exports = {};
+ new Function("exports", "require", ts.transpileModule(fs.readFileSync(path.join(root, file), "utf8"), {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2021},
+ }).outputText)(exports, id => modules[id]);
+ modules[name] = exports;
+ }
+ return modules.fileTree.getFileTreeIconHTML(iconValue, defaultIcon, className, needSpan, useSVGDefaultIcon);
+};
+
+// 文字必须使用与产品相同的字体栈,否则测到的文字墨迹中心没有参考价值
+const themeVars = () => fs.readFileSync(path.join(root, "appearance/themes/daylight/theme.css"), "utf8")
+ .match(/--b3-font-family[-a-z]*: [^;]+;/g).join(" ");
+
+const buildPage = ({css, fontStyle, servePath, body, rowClass = ""}) => {
+ // Emojis Additional 只补充 Unicode 17 新增码位,不覆盖图标字符。它在 file:// 离屏页面里会被错误地
+ // 分配给图标文本且不绘制(产品由 HTTP 提供字体,不存在此情况),因此测试页排除该 face。
+ const fontCSS = fontStyle("windows11", servePath)
+ .replace(/@font-face \{[^}]*font-family: "Emojis Additional";[^}]*\}/, "");
+ return `<!doctype html><meta charset="utf-8">
+<style>${fontCSS}</style>
+<style>:root { ${themeVars()} }</style>
+<style>${css}</style>
+<style>
+body { margin: 0; padding: 20px; background: #808080; font-size: 14px; }
+.file-tree { width: 320px; }
+.measure-list { width: 220px; }
+${rowClass}
+</style>
+<body class="body--windows">${body}</body>`;
+};
+
+const inkIn = (bitmap, size, fromX, toX, fromY, toY) => {
+ let minY = toY, maxY = -1;
+ for (let y = fromY; y < toY; y++) {
+ for (let x = fromX; x < toX; x++) {
+ const offset = (y * size.width + x) * 4;
+ if (Math.abs(bitmap[offset + 2] - 128) + Math.abs(bitmap[offset + 1] - 128) +
+ Math.abs(bitmap[offset] - 128) > 40) {
+ if (y < minY) {
+ minY = y;
+ }
+ if (y > maxY) {
+ maxY = y;
+ }
+ }
+ }
+ }
+ assert.ok(maxY >= 0, "icon ink is rendered");
+ return {top: minY / SCALE, bottom: maxY / SCALE, center: (minY + maxY) / SCALE / 2};
+};
+
+const measure = async (win, selector) => {
+ const geometry = await win.webContents.executeJavaScript(`(() => {
+ const element = document.querySelector(${JSON.stringify(selector)});
+ if (!element) { return null; }
+ const rect = element.getBoundingClientRect();
+ return {x: rect.x, y: rect.y, width: rect.width, height: rect.height,
+ fontFamily: getComputedStyle(element).fontFamily,
+ fontSize: getComputedStyle(element).fontSize,
+ childTag: element.firstElementChild && element.firstElementChild.tagName};
+ })()`);
+ assert.ok(geometry, `${selector} exists`);
+ return {...geometry, selector};
+};
+
+const inkOf = (bitmap, size, geometry) => inkIn(bitmap, size,
+ Math.round(geometry.x * SCALE), Math.round((geometry.x + geometry.width) * SCALE),
+ Math.round(geometry.y * SCALE), Math.round((geometry.y + geometry.height) * SCALE));
+
+const runElectron = async () => {
+ const {app, BrowserWindow} = require("electron");
+ app.setPath("userData", path.join(process.argv[2], "profile"));
+ app.commandLine.appendSwitch("disable-gpu");
+ app.commandLine.appendSwitch("force-device-scale-factor", String(SCALE));
+ await app.whenReady();
+ const win = new BrowserWindow({show: false, width: 900, height: 900,
+ webPreferences: {nodeIntegration: true, contextIsolation: false}});
+ try {
+ const ts = require("typescript");
+ const fontModule = {};
+ new Function("exports", ts.transpileModule(fs.readFileSync(path.join(root, "src/util/emojiFont.ts"), "utf8"), {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
+ }).outputText)(fontModule);
+ const baseCSS = require("sass").compile(path.join(root, "src/assets/scss/base.scss"),
+ {logger: {warn() {}, debug() {}}}).css;
+ const mobileCSS = require("sass").compile(path.join(root, "src/assets/scss/mobile.scss"),
+ {logger: {warn() {}, debug() {}}}).css;
+ // 内置字体需与产品一样可加载:主题里 Emojis Additional 排在最前,加载失败会让整段图标不渲染
+ const notoSource = path.join(root, "appearance/fonts/Noto-COLRv1-2.051/Noto-COLRv1.woff2");
+ const notoTarget = path.join(process.argv[2], "appearance/fonts/Noto-COLRv1-2.051/Noto-COLRv1.woff2");
+ fs.mkdirSync(path.dirname(notoTarget), {recursive: true});
+ fs.copyFileSync(notoSource, notoTarget);
+ const servePath = "file:///" + process.argv[2].replace(/\\/g, "/") + "/";
+ const load = async page => {
+ const htmlPath = path.join(process.argv[2], "page.html");
+ fs.writeFileSync(htmlPath, page);
+ await win.loadFile(htmlPath);
+ await win.webContents.executeJavaScript(`document.fonts.ready.then(() => Promise.all([
+ document.fonts.load('12px "Emojis Icon"'), document.fonts.load('14px "Emojis Icon"'),
+ document.fonts.load('16px "Emojis Icon"'), document.fonts.load('19px "Emojis Icon"'),
+ document.fonts.load('20px "Emojis Icon"'), document.fonts.load('68px "Emojis Icon"')]))
+ .then(() => new Promise(resolve => requestAnimationFrame(() =>
+ requestAnimationFrame(() => setTimeout(resolve, 80)))))`);
+ };
+
+ // 文档树图标:逐字形核对与文字的对齐,并确认图片/SVG 图标不受影响
+ for (const [platform, tolerance] of [["windows11", TEXT_TOLERANCE], ["other", UNCALIBRATED_TOLERANCE]]) {
+ for (const fontSize of [14, 16]) {
+ const rows = Object.entries(TREE_ICONS).map(([name, value]) =>
+ `<li class="b3-list-item" id="row-${name}">
+ <span class="b3-list-item__icon" id="icon-${name}">${
+ getFileTreeIconHTML(value, name === "notebook" ? "notebook" : "file")}</span>
+ <span class="b3-list-item__text" id="text-${name}">${TEXT}</span></li>`);
+ rows.push(`<li class="b3-list-item" id="row-image">
+ <span class="b3-list-item__icon" id="icon-image">${getFileTreeIconHTML("b3log.png", "file")}</span>
+ <span class="b3-list-item__text" id="text-image">${TEXT}</span></li>`);
+ rows.push(`<li class="b3-list-item" id="row-svg">
+ <span class="b3-list-item__icon" id="icon-svg">${
+ getFileTreeIconHTML("", "file", "", false, true)}</span>
+ <span class="b3-list-item__text" id="text-svg">${TEXT}</span></li>`);
+ await load(buildPage({css: baseCSS, fontStyle: () => fontModule.getEmojiFontStyle(platform, servePath),
+ servePath, body: `<div class="file-tree sy__file"><ul class="b3-list measure-list">${
+ rows.join("")}</ul></div>`,
+ rowClass: `.measure-list .b3-list-item__icon { font-size: ${fontSize}px; }`}));
+ const image = await win.webContents.capturePage();
+ const size = image.getSize();
+ const bitmap = image.getBitmap();
+ for (const name of [...Object.keys(TREE_ICONS), "image", "svg"]) {
+ const iconBox = await measure(win, `#icon-${name}`);
+ if (name === "image" || name === "svg") {
+ // 图片与 SVG 图标是替换元素,按 16px 居中,不参与墨迹对齐
+ const child = await win.webContents.executeJavaScript(`(() => {
+ const button = document.getElementById("icon-${name}").firstElementChild;
+ const box = button.getBoundingClientRect();
+ const parent = document.getElementById("icon-${name}").getBoundingClientRect();
+ return {tag: button.tagName, width: box.width, height: box.height, top: box.y - parent.y};
+ })()`);
+ assert.equal(child.tag, name === "image" ? "IMG" : "svg");
+ assert.equal(child.width, 16);
+ assert.equal(child.height, 16);
+ assert.ok(Math.abs(child.top - (iconBox.height - 16) / 2) <= 0.5,
+ `${name} graphic must stay centered`);
+ continue;
+ }
+ // emoji 字形不应被包裹,也不应依赖 transform 做几何补偿
+ assert.equal(iconBox.childTag, null, `${name}: emoji glyph must not be wrapped`);
+ const textBox = await measure(win, `#text-${name}`);
+ const iconInk = inkOf(bitmap, size, iconBox);
+ const textInk = inkOf(bitmap, size, textBox);
+ const delta = iconInk.center - textInk.center;
+ console.log(`${platform}/${fontSize}px ${name}: ink ${iconInk.top.toFixed(1)}..` +
+ `${iconInk.bottom.toFixed(1)} text center ${textInk.center.toFixed(2)} delta ${delta.toFixed(2)}px`);
+ assert.ok(Math.abs(delta) <= tolerance,
+ `${platform}/${fontSize}px ${name}: emoji ink center is ${delta.toFixed(2)}px ` +
+ "off the adjacent text ink center");
+ }
+ }
+ }
+
+ // 其余图标位:单独成页,避免依赖滚动
+ for (const site of SITES) {
+ await load(buildPage({css: site.entry === "mobile" ? mobileCSS : baseCSS,
+ fontStyle: () => fontModule.getEmojiFontStyle("windows11", servePath),
+ servePath, body: site.markup}));
+ const image = await win.webContents.capturePage();
+ const size = image.getSize();
+ const bitmap = image.getBitmap();
+ const iconBox = await measure(win, "#icon");
+ assert.ok(iconBox.fontFamily.includes('"Emojis Icon"'),
+ `${site.name}: must use the icon emoji font, got ${iconBox.fontFamily}`);
+ assert.equal(iconBox.childTag, null, `${site.name}: emoji glyph must not be wrapped`);
+ const iconInk = inkOf(bitmap, size, iconBox);
+ if (site.reference === "text") {
+ const textInk = inkOf(bitmap, size, await measure(win, "#text"));
+ const delta = iconInk.center - textInk.center;
+ console.log(`${site.name}: ${iconBox.fontSize} delta vs text ${delta.toFixed(2)}px`);
+ assert.ok(Math.abs(delta) <= TEXT_TOLERANCE,
+ `${site.name}: emoji ink center is ${delta.toFixed(2)}px off the adjacent text ink center`);
+ } else {
+ const boxCenter = iconBox.y + iconBox.height / 2;
+ const delta = iconInk.center - boxCenter;
+ console.log(`${site.name}: ${iconBox.fontSize} delta vs box center ${delta.toFixed(2)}px`);
+ // 度量按「与文字对齐」标定,独立格子会略微上移,这是已知取舍
+ assert.ok(Math.abs(delta) <= STANDALONE_TOLERANCE,
+ `${site.name}: emoji ink center is ${delta.toFixed(2)}px off the icon box center`);
+ }
+ }
+ app.exit(0);
+ } catch (error) {
+ console.error(error);
+ app.exit(1);
+ }
+};
+
+if (process.versions.electron && process.type === "browser") {
+ runElectron().catch(error => { console.error(error); require("electron").app.exit(1); });
+} else {
+ require("node:test").it("aligns emoji ink with adjacent text in every icon slot", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 240000,
+ }, async () => {
+ const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "siyuan-emoji-icon-"));
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ await require("node:util").promisify(require("node:child_process").execFile)(
+ require("electron"), [__filename, temporary], {env, windowsHide: true, timeout: 230000});
+ } finally {
+ assert.equal(path.dirname(path.resolve(temporary)), path.resolve(os.tmpdir()));
+ assert.ok(path.basename(temporary).startsWith("siyuan-emoji-icon-"));
+ fs.rmSync(temporary, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});
+ }
+ });
+}
diff --git a/app/tests/fileTreeIconAlignment.test.js b/app/tests/fileTreeIconAlignment.test.js
deleted file mode 100644
index ca309bf532..0000000000
--- a/app/tests/fileTreeIconAlignment.test.js
+++ /dev/null
@@ -1,101 +0,0 @@
-const assert = require("node:assert/strict");
-const fs = require("node:fs");
-const path = require("node:path");
-const os = require("node:os");
-
-const browserCases = (sources, styles) => {
- const assert = require("node:assert/strict");
- window.siyuan = {config: {fileTree: {useSVGDefaultIcon: false}},
- storage: {"local-images": {note: "1f600", folder: "1f4d2", file: "2b50"}}};
- const modules = {"../constants": {Constants: {LOCAL_IMAGES: "local-images"}}};
- for (const [name, source] of sources) {
- const exports = {};
- new Function("exports", "require", source)(exports, id => modules[id]);
- modules[name] = exports;
- }
- const {getFileTreeIconHTML, updateFileTreeItemIcon} = modules.fileTree;
- const row = (id, html) => `<li id="${id}" class="b3-list-item"><span class="b3-list-item__icon">${html}</span>` +
- '<span class="b3-list-item__text">Document</span></li>';
- const style = document.createElement("style");
- style.textContent = styles;
- document.head.append(style);
- document.body.innerHTML = '<ul class="b3-list">' +
- row("emoji", getFileTreeIconHTML("1f600", "notebook")) +
- row("image", getFileTreeIconHTML("custom.png", "file")) +
- row("svg", getFileTreeIconHTML("", "file", "", false, true)) + "</ul>";
- const icon = document.querySelector("#emoji .b3-list-item__icon");
- const emoji = icon.firstElementChild;
- for (const platform of ["windows", "mac", "linux", "android", "ios", "harmony"]) {
- document.body.className = `body--${platform}`;
- for (const size of [14, 24]) {
- document.body.style.fontSize = `${size}px`;
- const before = icon.getBoundingClientRect();
- emoji.style.transform = "none";
- const original = emoji.getBoundingClientRect();
- emoji.style.transform = "";
- const adjusted = emoji.getBoundingClientRect();
- assert.equal(adjusted.top - original.top, platform === "windows" ? -1 : 0);
- assert.equal(icon.getBoundingClientRect().top, before.top);
- assert.equal(icon.getBoundingClientRect().height, before.height);
- assert.equal(document.elementFromPoint(adjusted.x + adjusted.width / 2,
- adjusted.y + adjusted.height / 2), icon, "glyph wrappers preserve the icon click target");
- for (const id of ["image", "svg"]) {
- const graphic = document.querySelector(`#${id} .b3-list-item__icon`).firstElementChild;
- assert.equal(getComputedStyle(graphic).transform, "none");
- assert.equal(graphic.getBoundingClientRect().width, 16);
- assert.equal(graphic.getBoundingClientRect().height, 16);
- }
- }
- }
- updateFileTreeItemIcon(document.getElementById("emoji"), "2b50", "file");
- assert.equal(icon.firstElementChild.className, "b3-list-item__emoji");
- updateFileTreeItemIcon(document.getElementById("emoji"), "custom.png", "file");
- assert.equal(icon.firstElementChild.tagName, "IMG");
-};
-
-const runElectron = async () => {
- const {app, BrowserWindow} = require("electron");
- app.setPath("userData", path.join(process.argv[2], "profile"));
- app.commandLine.appendSwitch("disable-gpu");
- await app.whenReady();
- const win = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false}});
- try {
- const root = path.resolve(__dirname, "..");
- const ts = require("typescript");
- const sources = [["../util/escape", "util/escape.ts"], ["./iconValue", "emoji/iconValue.ts"],
- ["fileTree", "emoji/fileTreeIcon.ts"]].map(([name, file]) => [name,
- ts.transpileModule(fs.readFileSync(path.join(root, "src", file), "utf8"), {
- compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2021},
- }).outputText]);
- const css = require("sass").compile(path.join(root, "src/assets/scss/base.scss"),
- {logger: {warn() {}, debug() {}}}).css;
- await win.loadURL("about:blank");
- await win.webContents.executeJavaScript(fs.readFileSync(path.join(root, "stage/protyle/js/lute/lute.min.js"), "utf8"));
- await win.webContents.executeJavaScript(`(${browserCases.toString()})(${JSON.stringify(sources)}, ${JSON.stringify(css)})`);
- app.exit(0);
- } catch (error) {
- console.error(error);
- app.exit(1);
- }
-};
-
-if (process.versions.electron && process.type === "browser") {
- runElectron().catch(error => { console.error(error); require("electron").app.exit(1); });
-} else {
- require("node:test").it("adjusts only Windows file tree emoji glyphs while preserving image, SVG and click geometry", {
- skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
- timeout: 60000,
- }, async () => {
- const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "siyuan-file-tree-icon-"));
- const env = {...process.env};
- delete env.ELECTRON_RUN_AS_NODE;
- try {
- await require("node:util").promisify(require("node:child_process").execFile)(
- require("electron"), [__filename, temporary], {env, windowsHide: true, timeout: 55000});
- } finally {
- assert.equal(path.dirname(path.resolve(temporary)), path.resolve(os.tmpdir()));
- assert.ok(path.basename(temporary).startsWith("siyuan-file-tree-icon-"));
- fs.rmSync(temporary, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});
- }
- });
-}