feat: 实现 DOCX 可编辑字体嵌入

This commit is contained in:
SkyJourney
2026-07-31 16:18:30 +08:00
parent f3847f3e4b
commit 10bc62cee4
32 changed files with 2195 additions and 76 deletions
@@ -63,6 +63,21 @@ async function createThemeFixture() {
domPreset: "typora",
defaultFontSize: "16px",
supportedFeatures: ["code", "table"],
docxFonts: {
faces: [
{
family: "Test",
source: "fonts/test.woff2",
license: "OFL-1.1"
},
{
family: "SharedTest",
source:
"theme-shared:official-fonts/shared.woff2",
license: "GPL-3.0-with-font-exception"
}
]
},
pageDefaults: {
margins: {
top: "37mm",
@@ -98,6 +113,16 @@ async function createThemeFixture() {
join(sharedFontRoot, "shared.woff2"),
Buffer.from([4, 5, 6, 7])
);
await writeFile(
join(sharedFontRoot, "fonts.css"),
[
"@font-face {",
" font-family: SharedTest;",
" src: url('./shared.woff2') format('woff2');",
"}"
].join("\n"),
"utf8"
);
return { bundledRoot, localRoot };
}
@@ -198,6 +223,9 @@ describe("共享应用服务", () => {
).resolves.toContain(
"mdpdf://theme/_shared/official-fonts/shared.woff2"
);
await expect(
service.getThemeCss("test-theme")
).resolves.toContain("font-family: SharedTest");
});
it("安全读取主题二进制资源", async () => {
@@ -308,6 +336,18 @@ describe("共享应用服务", () => {
expect(prepared.theme.css).toContain(
"#write { font-family: Test; }"
);
expect(prepared.theme.fonts).toEqual([
expect.objectContaining({
family: "Test",
source: "fonts/test.woff2",
content: Buffer.from([0, 1, 2, 3])
}),
expect.objectContaining({
family: "SharedTest",
source: "theme-shared:official-fonts/shared.woff2",
content: Buffer.from([4, 5, 6, 7])
})
]);
});
it("以稳定错误码拒绝非法 DOCX 配置和缺失主题", async () => {
@@ -74,7 +74,8 @@ const prepared: PreparedDocxExport = {
theme: {
manifest: theme(),
source: "bundled",
css: ""
css: "",
fonts: []
}
};
@@ -126,7 +127,8 @@ function conversionResult(): PandocDocxConversionResult {
xmlPartCount: 9,
relationshipCount: 3,
headerCount: 0,
footerCount: 1
footerCount: 1,
embeddedFontCount: 0
},
timings: {
referenceMs: 3,
@@ -84,7 +84,8 @@ const prepared: PreparedDocxExport = {
theme: {
manifest,
source: "bundled",
css: "#write { color: black; }"
css: "#write { color: black; }",
fonts: []
}
};
@@ -77,7 +77,8 @@ function prepared(
theme: {
manifest: manifest(docxStyle),
source: "bundled",
css: "#write { font-family: Test; }"
css: "#write { font-family: Test; }",
fonts: []
}
};
}