Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

fonts.ts

86 lines3.3 KB

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