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