perf(tui): shrink standalone builds and target native bindings (#6432)
* perf(tui): shrink standalone builds without dropping features * fix(tui): resolve macOS diagram fonts through CoreText * fix(webui): preserve history pagination at the page boundary * docs(tui): streamline release preflight guidance --------- Co-authored-by: Xubin Ren <52506698+Re-bin@users.noreply.github.com>
This commit is contained in:
1 parent
0e0df327c8
commit
ae7fed88e2
21 files changed
+424
-82
No files matched your search
@@ -15,6 +15,7 @@ function images(root: Renderable): ImageRenderable[] {
|
||||
|
||||
const source = "graph TD\n A[中文长标签 English] --> B[完成]"
|
||||
const content = `\`\`\`mermaid\n${source}\n\`\`\`\n\nTrailing paragraph`
|
||||
const nativeFontsAvailable = process.platform === "darwin" || existsSync("C:/Windows/Fonts/msyh.ttc")
|
||||
|
||||
test("streamed stable Mermaid fences stay source until reply completion, including the viewer", async () => {
|
||||
setup = await createTestRenderer({ width: 80, height: 35 })
|
||||
@@ -35,7 +36,7 @@ test("streamed stable Mermaid fences stay source until reply completion, includi
|
||||
expect(images(transcript.root)).toHaveLength(0)
|
||||
})
|
||||
|
||||
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("viewer preserves preview footprint, transcript position and draft; session reset releases images", async () => {
|
||||
test.skipIf(!nativeFontsAvailable)("viewer preserves preview footprint, transcript position and draft; session reset releases images", async () => {
|
||||
setup = await createTestRenderer({ width: 100, height: 42 })
|
||||
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
|
||||
const app = mount(setup)
|
||||
@@ -126,7 +127,7 @@ test("diagram command on ordinary Markdown shows an empty viewer and returns to
|
||||
expect(setup.captureCharFrame()).toContain("draft")
|
||||
})
|
||||
|
||||
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("inline image uses real terminal pixels without upscaling small diagrams", async () => {
|
||||
test.skipIf(!nativeFontsAvailable)("inline image uses real terminal pixels without upscaling small diagrams", async () => {
|
||||
setup = await createTestRenderer({ width: 100, height: 42 })
|
||||
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
|
||||
Object.defineProperty(setup.renderer, "resolution", { value: { width: 1300, height: 1008 } })
|
||||
@@ -144,7 +145,7 @@ test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("inline image uses real te
|
||||
}, 10_000)
|
||||
|
||||
|
||||
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("source-only and cancelled viewer release the shared worker; image demand resumes explicitly", async () => {
|
||||
test.skipIf(!nativeFontsAvailable)("source-only and cancelled viewer release the shared worker; image demand resumes explicitly", async () => {
|
||||
setup = await createTestRenderer({ width: 100, height: 42 })
|
||||
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
|
||||
const app = mount(setup)
|
||||
@@ -192,7 +193,7 @@ test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("source-only and cancelled
|
||||
}, 20_000)
|
||||
|
||||
|
||||
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("offscreen return and cell-height changes reuse SVG; theme and session changes invalidate it", async () => {
|
||||
test.skipIf(!nativeFontsAvailable)("offscreen return and cell-height changes reuse SVG; theme and session changes invalidate it", async () => {
|
||||
setup = await createTestRenderer({ width: 100, height: 42 })
|
||||
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
|
||||
const resolution = { width: 1000, height: 840 }
|
||||
@@ -235,7 +236,7 @@ test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("offscreen return and cell
|
||||
}, 20_000)
|
||||
|
||||
|
||||
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("viewer navigation releases old layout pins under the real transcript budget", async () => {
|
||||
test.skipIf(!nativeFontsAvailable)("viewer navigation releases old layout pins under the real transcript budget", async () => {
|
||||
setup = await createTestRenderer({ width: 100, height: 42 })
|
||||
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
|
||||
Object.defineProperty(setup.renderer, "resolution", { value: { width: 1000, height: 840 } })
|
||||
@@ -272,7 +273,7 @@ test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("viewer navigation release
|
||||
}, 25_000)
|
||||
|
||||
|
||||
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("source/image retry after a parser failure keeps the original source visible", async () => {
|
||||
test.skipIf(!nativeFontsAvailable)("source/image retry after a parser failure keeps the original source visible", async () => {
|
||||
setup = await createTestRenderer({ width: 100, height: 42 })
|
||||
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
|
||||
const app = mount(setup)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { expect, spyOn, test } from "bun:test"
|
||||
import * as childProcess from "node:child_process"
|
||||
import { macOSFonts } from "./macos-fonts"
|
||||
|
||||
test("macOS font lookup preserves system-resolved paths and regular/bold face identity", () => {
|
||||
const regular = { path: "/System/Library/AssetsV2/font-version/AssetData/PingFang.ttc", postscriptName: "PingFangSC-Regular" }
|
||||
const bold = { ...regular, postscriptName: "PingFangSC-Semibold" }
|
||||
const lookup = spyOn(childProcess, "execFileSync").mockReturnValue(JSON.stringify([regular, bold]))
|
||||
try {
|
||||
expect(macOSFonts()).toEqual({ regular, bold, mono: regular })
|
||||
} finally { lookup.mockRestore() }
|
||||
})
|
||||
|
||||
test("an unavailable macOS font URL is a font error, not an undefined file read", () => {
|
||||
const lookup = spyOn(childProcess, "execFileSync").mockReturnValue(JSON.stringify([
|
||||
{ postscriptName: "PingFangSC-Regular" }, { postscriptName: "PingFangSC-Semibold" },
|
||||
]))
|
||||
try {
|
||||
expect(macOSFonts).toThrow("check PingFang in macOS Font Book")
|
||||
} finally { lookup.mockRestore() }
|
||||
})
|
||||
|
||||
test("a failed system lookup stays a recoverable diagram error", () => {
|
||||
const lookup = spyOn(childProcess, "execFileSync").mockImplementation(() => { throw new Error("lookup timed out") })
|
||||
try {
|
||||
expect(macOSFonts).toThrow("check PingFang in macOS Font Book")
|
||||
} finally { lookup.mockRestore() }
|
||||
})
|
||||
@@ -0,0 +1,38 @@
|
||||
import { execFileSync } from "node:child_process"
|
||||
import { homedir } from "node:os"
|
||||
import { isAbsolute } from "node:path"
|
||||
import type { DiagramFont, DiagramInput } from "./types"
|
||||
|
||||
// Ask CoreText for installed font URLs: recent macOS versions manage PingFang
|
||||
// under versioned Assets directories. No app automation or development toolchain is used.
|
||||
const query = `
|
||||
ObjC.import("CoreText");
|
||||
JSON.stringify(["PingFangSC-Regular", "PingFangSC-Semibold"].map(function(name) {
|
||||
const font = $.CTFontCreateWithName($(name), 12, null);
|
||||
const actual = ObjC.unwrap(ObjC.castRefToObject($.CTFontCopyPostScriptName(font)));
|
||||
if (actual !== name) throw new Error("PingFang font unavailable");
|
||||
const url = $.CTFontCopyAttribute(font, $.kCTFontURLAttribute);
|
||||
return { path: ObjC.unwrap(url.path), postscriptName: actual };
|
||||
}));
|
||||
`
|
||||
|
||||
function isFont(value: unknown): value is DiagramFont {
|
||||
return value !== null && typeof value === "object"
|
||||
&& "path" in value && typeof value.path === "string" && isAbsolute(value.path)
|
||||
&& "postscriptName" in value && typeof value.postscriptName === "string" && value.postscriptName.length > 0
|
||||
}
|
||||
|
||||
/** Called only in the diagram worker; getFonts caches the resolved faces. */
|
||||
export function macOSFonts(): NonNullable<DiagramInput["fonts"]> {
|
||||
try {
|
||||
const fonts: unknown = JSON.parse(execFileSync("/usr/bin/osascript", ["-l", "JavaScript", "-e", query], {
|
||||
encoding: "utf8", timeout: 5000, maxBuffer: 4096, stdio: ["ignore", "pipe", "pipe"],
|
||||
env: { HOME: homedir(), PATH: "/usr/bin:/bin" },
|
||||
}))
|
||||
if (!Array.isArray(fonts) || fonts.length !== 2 || !fonts.every(isFont)) throw new Error("Invalid font lookup result")
|
||||
const regular = fonts[0]!, bold = fonts[1]!
|
||||
return { regular, bold, mono: regular }
|
||||
} catch (cause) {
|
||||
throw new Error("Diagram font unavailable (check PingFang in macOS Font Book)", { cause })
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,8 @@ import { SOURCE_LIMIT, type DiagramInput } from "./types"
|
||||
const input: DiagramInput = {
|
||||
source: "graph TD\n A[中文:开始处理长标签] --> B{检查数据}\n B -->|通过| C[完成]",
|
||||
width: 800, scale: 2,
|
||||
fonts: {
|
||||
// macOS must discover its system-managed font location, not use a test override.
|
||||
fonts: process.platform === "darwin" ? undefined : {
|
||||
regular: { path: "C:/Windows/Fonts/msyh.ttc", postscriptName: "MicrosoftYaHei" },
|
||||
bold: { path: "C:/Windows/Fonts/msyhbd.ttc", postscriptName: "MicrosoftYaHei-Bold" },
|
||||
mono: { path: "C:/Windows/Fonts/consola.ttf", postscriptName: "Consolas" },
|
||||
@@ -47,7 +48,7 @@ test("worker records preserve split UTF-8 and multiple replies across pipe reads
|
||||
expect(records).toEqual([{ label: "中文" }, { width: 9 }])
|
||||
})
|
||||
|
||||
describe.skipIf(!existsSync(input.fonts!.regular.path))("Windows raster integration", () => {
|
||||
describe.skipIf(process.platform !== "darwin" && !existsSync(input.fonts!.regular.path))("Native raster integration", () => {
|
||||
test("Pele renders Chinese flowchart and sequence diagram to bounded PNGs", async () => {
|
||||
for (const source of [input.source, "sequenceDiagram\n participant A as 用户\n participant B as 服务\n A->>B: 长标签消息\n B-->>A: 完成"]) {
|
||||
const image = await renderDiagram({ ...input, source })
|
||||
|
||||
@@ -6,17 +6,17 @@ import { diagramFrame } from "./geometry"
|
||||
import { resolveDiagramSvg } from "./svg"
|
||||
import { rasterDiagram } from "./raster"
|
||||
import { outlineDiagramText, shapeDiagramText } from "./text"
|
||||
import { macOSFonts } from "./macos-fonts"
|
||||
import { DIAGRAM_FONT_SIZE, MAX_DIMENSION, MAX_PIXELS, OUTPUT_LIMIT, SOURCE_LIMIT, type DiagramAsset, type DiagramFont, type DiagramInput, type DiagramLayout } from "./types"
|
||||
|
||||
type FontFile = ReturnType<typeof create>
|
||||
function systemFonts(file: (path: string) => FontFile): NonNullable<DiagramInput["fonts"]> {
|
||||
if (process.platform === "darwin") return macOSFonts()
|
||||
const windows = join(process.env.SystemRoot || "C:/Windows", "Fonts")
|
||||
const candidates = process.platform === "win32"
|
||||
? [[join(windows, "msyh.ttc"), join(windows, "msyhbd.ttc")]]
|
||||
: process.platform === "darwin"
|
||||
? [["/System/Library/Fonts/PingFang.ttc", "/System/Library/Fonts/PingFang.ttc"]]
|
||||
: [["/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc", "/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc"],
|
||||
["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"]]
|
||||
: [["/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc", "/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc"],
|
||||
["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"]]
|
||||
for (const [regularPath, boldPath] of candidates) {
|
||||
if (!regularPath || !boldPath || !existsSync(regularPath) || !existsSync(boldPath)) continue
|
||||
const select = (path: string, bold: boolean): DiagramFont => {
|
||||
|
||||
Reference in new issue
Block a user