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

fonts.ts

56 lines2.4 KB

1// Adapted from opencode (MIT). Copyright (c) 2025 opencode. See LICENSE.opencode.
2import type { BundledFontProvider } from "@betteroffice/docx/layout";
3import {
4 configureFontCdn,
5 loadBundledFontBytes,
6 resolveBundledFamilyFace,
7 resolveLastResortFace,
8 resolveMetricCompatFace,
9 resolveScriptFallbackFace,
10 type BundledFontFace,
11} from "@betteroffice/fonts";
12​
13// Every face loads from the app's own files, so no font request leaves the machine. The engines draw only with the
14// faces they are handed, never the system's, so CJK text needs the `@betteroffice/fonts-cjk` faces to show at all.
15configureFontCdn({ baseUrl: false });
16​
17/** The faces Word substitutes for a document's fonts: metric-compatible ones first, then Word's own fallbacks. */
18export function wordFonts(): BundledFontProvider {
19 const loader = (face: BundledFontFace | undefined) => (face ? () => loadFace(face) : undefined);
20​
21 return {
22 resolve: (family, bold, italic) => loader(resolveMetricCompatFace(family, bold, italic)),
23 resolveFamily: (family, bold, italic) => loader(resolveBundledFamilyFace(family, bold, italic)),
24 resolveScriptFallback: (script, bold, italic) =>
25 loader(resolveScriptFallbackFace(script, bold, italic)),
26 resolveLastResort: (family, bold, italic) => () =>
27 loadFace(resolveLastResortFace(family, bold, italic, "word")),
28 };
30​
31/** The face PowerPoint draws a family with: its metric-compatible or bundled face, else PowerPoint's substitute. */
32export function presentationFace(family: string, bold: boolean, italic: boolean): BundledFontFace {
33 return (
34 resolveMetricCompatFace(family, bold, italic) ??
35 resolveBundledFamilyFace(family, bold, italic) ??
36 resolveLastResortFace(family, bold, italic, "powerpoint")
37 );
39​
40/** The face Excel's cells draw a family with when it is missing: its metric-compatible or bundled face, if any. */
41export function sheetFace(family: string, bold: boolean, italic: boolean) {
42 return (
43 resolveMetricCompatFace(family, bold, italic) ??
44 resolveBundledFamilyFace(family, bold, italic) ??
45 resolveBundledFamilyFace(family, bold, false) ??
46 resolveBundledFamilyFace(family, false, false)
47 );
49​
50/**
51 * A bundled face's bytes. The font package caches them per worker, so a face loads once per open file; the browser's
52 * caches, which every worker shares, serve the files to the next.
53 */
54export function loadFace(face: BundledFontFace) {
55 return loadBundledFontBytes(face);