feat: 实现 DOCX 可编辑字体嵌入
This commit is contained in:
@@ -13,7 +13,9 @@ import {
|
||||
sep
|
||||
} from "node:path";
|
||||
import {
|
||||
MAXIMUM_DOCX_FONT_TOTAL_SOURCE_BYTES,
|
||||
themeManifestSchema,
|
||||
type DocxFontFace,
|
||||
type ThemeManifest
|
||||
} from "@md-to-pdf/core";
|
||||
|
||||
@@ -23,6 +25,10 @@ export interface ThemeRecord {
|
||||
source: "bundled" | "local";
|
||||
}
|
||||
|
||||
export interface ThemeFontAsset extends DocxFontFace {
|
||||
content: Uint8Array;
|
||||
}
|
||||
|
||||
export interface ThemeRegistryOptions {
|
||||
bundledRoot: string;
|
||||
localRoot: string;
|
||||
@@ -48,6 +54,7 @@ const cssImportPattern =
|
||||
const maximumCssImportDepth = 8;
|
||||
const sharedThemeAssetId = "_shared";
|
||||
const sharedThemeAssetScheme = "theme-shared:";
|
||||
const sharedFontCssPath = "official-fonts/fonts.css";
|
||||
|
||||
function isMissingFileError(error: unknown) {
|
||||
return (
|
||||
@@ -314,6 +321,40 @@ export function createThemeRegistry(options: ThemeRegistryOptions) {
|
||||
promise: Promise<ThemeRecord[]>;
|
||||
}
|
||||
| undefined;
|
||||
let sharedFontCssPromise: Promise<string> | undefined;
|
||||
|
||||
function loadSharedFontCss() {
|
||||
if (!sharedFontCssPromise) {
|
||||
sharedFontCssPromise = (async () => {
|
||||
const sharedRoot = resolve(
|
||||
options.bundledRoot,
|
||||
sharedThemeAssetId
|
||||
);
|
||||
try {
|
||||
const cssPath = await resolveThemeFile(
|
||||
sharedRoot,
|
||||
sharedFontCssPath
|
||||
);
|
||||
return prepareCssSegment(
|
||||
await readFile(cssPath, "utf8"),
|
||||
sharedThemeAssetId,
|
||||
posix.dirname(sharedFontCssPath),
|
||||
createAssetUrl,
|
||||
false
|
||||
);
|
||||
} catch (error) {
|
||||
if (isMissingFileError(error)) {
|
||||
return "";
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
})();
|
||||
void sharedFontCssPromise.catch(() => {
|
||||
sharedFontCssPromise = undefined;
|
||||
});
|
||||
}
|
||||
return sharedFontCssPromise;
|
||||
}
|
||||
|
||||
async function scanThemes() {
|
||||
const [bundledThemes, localThemes] = await Promise.all([
|
||||
@@ -366,6 +407,7 @@ export function createThemeRegistry(options: ThemeRegistryOptions) {
|
||||
|
||||
function invalidate() {
|
||||
cachedThemes = undefined;
|
||||
sharedFontCssPromise = undefined;
|
||||
}
|
||||
|
||||
async function get(themeId: string) {
|
||||
@@ -385,13 +427,15 @@ export function createThemeRegistry(options: ThemeRegistryOptions) {
|
||||
? [theme.manifest.print]
|
||||
: [])
|
||||
];
|
||||
return (
|
||||
await Promise.all(
|
||||
const css = await Promise.all(
|
||||
cssFiles.map((path) =>
|
||||
loadThemeCssFile(theme, path, createAssetUrl)
|
||||
)
|
||||
)
|
||||
).join("\n");
|
||||
);
|
||||
if (theme.source === "bundled") {
|
||||
css.unshift(await loadSharedFontCss());
|
||||
}
|
||||
return css.filter(Boolean).join("\n");
|
||||
}
|
||||
|
||||
async function getAsset(themeId: string, assetPath: string) {
|
||||
@@ -427,10 +471,54 @@ export function createThemeRegistry(options: ThemeRegistryOptions) {
|
||||
};
|
||||
}
|
||||
|
||||
async function getDocxFonts(
|
||||
themeId: string
|
||||
): Promise<ThemeFontAsset[] | undefined> {
|
||||
const theme = await get(themeId);
|
||||
if (!theme) {
|
||||
return undefined;
|
||||
}
|
||||
const faces = theme.manifest.docxFonts?.faces ?? [];
|
||||
const fonts = await Promise.all(
|
||||
faces.map(async (face) => {
|
||||
const shared = face.source.startsWith(
|
||||
sharedThemeAssetScheme
|
||||
);
|
||||
if (shared && theme.source !== "bundled") {
|
||||
throw new Error("本地主题不能嵌入内置共享字体");
|
||||
}
|
||||
const asset = await getAsset(
|
||||
shared ? sharedThemeAssetId : theme.manifest.id,
|
||||
shared
|
||||
? face.source.slice(sharedThemeAssetScheme.length)
|
||||
: face.source
|
||||
);
|
||||
if (!asset || !asset.contentType.startsWith("font/")) {
|
||||
throw new Error(
|
||||
`DOCX 字体资源无效:${face.source}`
|
||||
);
|
||||
}
|
||||
return {
|
||||
...face,
|
||||
content: asset.content
|
||||
};
|
||||
})
|
||||
);
|
||||
const totalBytes = fonts.reduce(
|
||||
(total, font) => total + font.content.byteLength,
|
||||
0
|
||||
);
|
||||
if (totalBytes > MAXIMUM_DOCX_FONT_TOTAL_SOURCE_BYTES) {
|
||||
throw new Error("DOCX 字体资源总大小超过限制");
|
||||
}
|
||||
return fonts;
|
||||
}
|
||||
|
||||
return {
|
||||
get,
|
||||
getAsset,
|
||||
getCss,
|
||||
getDocxFonts,
|
||||
invalidate,
|
||||
list
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user