Open History to see the last commit
1// Adapted from opencode (MIT). Copyright (c) 2025 opencode. See LICENSE.opencode.
2import type { BundledFontProvider } from "@betteroffice/docx/layout";
3import {
4 loadBundledFontBytes,
5 resolveBundledFamilyFace,
6 resolveLastResortFace,
7 resolveMetricCompatFace,
8 resolveScriptFallbackFace,
9 type BundledFontFace,
10 type BundledFontScript,
11} from "@betteroffice/fonts";
12
13// Every face loads from the app's own files, so no font request leaves the machine. The Chinese, Japanese and Korean
14// faces are an add-on the app does not ship (see `fonts-cjk.ts`), yet the font package still maps East Asian families
15// such as Microsoft YaHei or Yu Gothic Medium to them, so every lookup passes through `shippedFace`.
16function shipped(script: BundledFontScript) {
17 return !script.startsWith("cjk-");
18}
19
20/** The faces Word substitutes for a document's fonts: metric-compatible ones first, then Word's own fallbacks. */
21export function wordFonts(): BundledFontProvider {
22 const loader = (face: BundledFontFace | undefined) => (face ? () => loadFace(face) : undefined);
23
24 return {
25 resolve: (family, bold, italic) =>
26 loader(shippedFace(resolveMetricCompatFace(family, bold, italic))),
27 resolveFamily: (family, bold, italic) =>
28 loader(shippedFace(resolveBundledFamilyFace(family, bold, italic))),
29 resolveScriptFallback: (script, bold, italic) => loader(scriptFace(script, bold, italic)),
30 resolveLastResort: (family, bold, italic) => () =>
31 loadFace(lastResortFace(family, bold, italic, "word")),
32 };
33}
34
35/** The face PowerPoint draws a family with: its metric-compatible or bundled face, else PowerPoint's substitute. */
36export function presentationFace(family: string, bold: boolean, italic: boolean): BundledFontFace {
37 return (
38 shippedFace(resolveMetricCompatFace(family, bold, italic)) ??
39 shippedFace(resolveBundledFamilyFace(family, bold, italic)) ??
40 lastResortFace(family, bold, italic, "powerpoint")
41 );
42}
43
44function shippedFace(face: BundledFontFace | undefined) {
45 return face && (!face.script || shipped(face.script)) ? face : undefined;
46}
47
48/**
49 * The substitute an application picks for a family it lacks. Only a weight variant of an East Asian family, such as
50 * Yu Mincho Light, resolves to a face the app does not ship; its name without the weight still tells serif from sans.
51 */
52function lastResortFace(
53 family: string,
54 bold: boolean,
55 italic: boolean,
56 office: "word" | "powerpoint",
57) {
58 const face = resolveLastResortFace(family, bold, italic, office);
59
60 if (shippedFace(face)) return face;
61
62 const base = family
63 .trim()
64 .split(/[\s-]+/)
65 .slice(0, -1)
66 .join(" ");
67
68 return (
69 shippedFace(resolveLastResortFace(base, bold, italic, office)) ??
70 resolveLastResortFace("", bold, italic, office)
71 );
72}
73
74/** The face that covers a script the Latin faces lack, or undefined when the app ships none. */
75export function scriptFace(script: BundledFontScript, bold: boolean, italic: boolean) {
76 return shipped(script) ? resolveScriptFallbackFace(script, bold, italic) : undefined;
77}
78
79/**
80 * A bundled face's bytes. The font package caches them per worker, so a face loads once per open file; the browser's
81 * caches, which every worker shares, serve the files to the next.
82 */
83export function loadFace(face: BundledFontFace) {
84 return loadBundledFontBytes(face);
85}