@devries-cameron20 / t3code Mirror · read-only / commits / bc3eb6d2b2
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 23:30 UTC

fix(web): fix japanese text in file viewer not rendering (#29051)

Moses committed Nov 12, 2026 at 02:19 UTC bc3eb6d2b2019ad4a77ae8c8efcdde8c41269e07
8 files changed +61 -68
apps/web/package.json
+1
@@ -15,6 +15,7 @@
15 "@base-ui/react": "^1.9.0",
16 "@betteroffice/docx": "0.4.3",
17 "@betteroffice/fonts": "0.4.0",
18 + "@betteroffice/fonts-cjk": "0.4.0",
19 "@betteroffice/pptx": "0.2.0",
20 "@betteroffice/xlsx": "0.3.0",
21 "@clerk/clerk-js": "catalog:",
apps/web/src/components/files/office/fonts-cjk.ts deleted
-4
@@ -1,4 +0,0 @@
1 -// Adapted from opencode (MIT). Copyright (c) 2025 opencode. See LICENSE.opencode.
2 -// `@betteroffice/fonts` imports its optional Chinese, Japanese and Korean add-on on demand. The add-on is about 33 MB,
3 -// so the app aliases it to this module, which offers no faces: those scripts fall back to the bundled Latin faces.
4 -export const CJK_FONT_ASSET_URLS = undefined;
apps/web/src/components/files/office/fonts.browser.test.ts new
+18
@@ -0,0 +1,18 @@
1 +import { BUNDLED_FONTS } from "@betteroffice/fonts";
2 +import { describe, expect, it } from "vite-plus/test";
3 +import { wordFonts } from "./fonts";
4 +
5 +const notoSansJp = BUNDLED_FONTS.find((face) => face.file === "NotoSansJP-Regular.otf")!;
6 +const fonts = wordFonts();
7 +
8 +describe("wordFonts", () => {
9 + it.each([
10 + ["Yu Gothic", fonts.resolve("Yu Gothic", false, false)],
11 + ["MS Gothic", fonts.resolve("MS Gothic", false, false)],
12 + ["the Japanese fallback", fonts.resolveScriptFallback?.("cjk-jp", false, false)],
13 + ])("draws %s with Noto Sans JP", async (_, load) => {
14 + const bytes = await load?.();
15 +
16 + expect(bytes?.byteLength).toBe(notoSansJp.byteLength);
17 + });
18 +});
apps/web/src/components/files/office/fonts.ts
+10 -52
@@ -1,5 +1,4 @@
1 // Adapted from opencode (MIT). Copyright (c) 2025 opencode. See LICENSE.opencode.
2 -import { pipe } from "effect/Function";
2 import type { BundledFontProvider } from "@betteroffice/docx/layout";
3 import {
4 loadBundledFontBytes,
@@ -8,75 +7,34 @@ import {
7 resolveMetricCompatFace,
8 resolveScriptFallbackFace,
9 type BundledFontFace,
11 - type BundledFontScript,
10 } from "@betteroffice/fonts";
11
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`.
17 -function shipped(script: BundledFontScript) {
18 - return !script.startsWith("cjk-");
19 -}
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 {
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)),
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) => () =>
32 - loadFace(lastResortFace(family, bold, italic, "word")),
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 (
39 - shippedFace(resolveMetricCompatFace(family, bold, italic)) ??
40 - shippedFace(resolveBundledFamilyFace(family, bold, italic)) ??
41 - lastResortFace(family, bold, italic, "powerpoint")
42 - );
43 -}
44 -
45 -function shippedFace(face: BundledFontFace | undefined) {
46 - return face && (!face.script || shipped(face.script)) ? face : undefined;
47 -}
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 - */
53 -function 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)
32 + resolveMetricCompatFace(family, bold, italic) ??
33 + resolveBundledFamilyFace(family, bold, italic) ??
34 + resolveLastResortFace(family, bold, italic, "powerpoint")
35 );
36 }
37
75 -/** The face that covers a script the Latin faces lack, or undefined when the app ships none. */
76 -export function scriptFace(script: BundledFontScript, bold: boolean, italic: boolean) {
77 - return shipped(script) ? resolveScriptFallbackFace(script, bold, italic) : undefined;
78 -}
79 -
38 /**
39 * A bundled face's bytes. The font package caches them per worker, so a face loads once per open file; the browser's
40 * caches, which every worker shares, serve the files to the next.
apps/web/src/components/files/office/slide-fonts.ts
+3 -2
@@ -5,7 +5,8 @@ import type {
5 SlideDisplayList,
6 SlidePrimitive,
7 } from "@betteroffice/pptx";
8 -import { loadFace, presentationFace, scriptFace } from "./fonts";
8 +import { resolveScriptFallbackFace } from "@betteroffice/fonts";
9 +import { loadFace, presentationFace } from "./fonts";
10 import type { DeckText, FallbackScript, FontStyle } from "./slide-text";
11
12 /** A face the deck asks for: a family it names, or the fallback for a script the Latin faces lack. */
@@ -32,7 +33,7 @@ export function slideFonts(deck: PresentationHandle, fonts: FontFaceSet) {
33 /** Loads a face's bytes and browser face; the returned function registers both, so callers choose the order. */
34 const prepare = async (want: Want, requested: FontStyle) => {
35 const source = want.script
35 - ? scriptFace(want.script, requested.bold, requested.italic)
36 + ? resolveScriptFallbackFace(want.script, requested.bold, requested.italic)
37 : presentationFace(want.family, requested.bold, requested.italic);
38
39 if (!source) return;
apps/web/vite.config.ts
-6
@@ -323,12 +323,6 @@ export default defineConfig(() => {
323 },
324 resolve: {
325 tsconfigPaths: true,
326 - alias: {
327 - "@betteroffice/fonts-cjk": new URL(
328 - "./src/components/files/office/fonts-cjk.ts",
329 - import.meta.url,
330 - ).pathname,
331 - },
326 dedupe: ["react", "react-dom"],
327 },
328 experimental: {
pnpm-lock.yaml
+14 -4
@@ -1013,8 +1013,11 @@ importers:
1013 version: 1.9.0(@date-fns/tz@1.5.0)(@types/react@19.3.0)(date-fns@4.4.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)
1014 '@betteroffice/docx':
1015 specifier: 0.4.3
1016 - version: 0.4.3(@betteroffice/fonts@0.4.0)
1016 + version: 0.4.3(@betteroffice/fonts@0.4.0(@betteroffice/fonts-cjk@0.4.0))
1017 '@betteroffice/fonts':
1018 + specifier: 0.4.0
1019 + version: 0.4.0(@betteroffice/fonts-cjk@0.4.0)
1020 + '@betteroffice/fonts-cjk':
1021 specifier: 0.4.0
1022 version: 0.4.0
1023 '@betteroffice/pptx':
@@ -3252,6 +3255,9 @@ packages:
3255 '@betteroffice/fonts':
3256 optional: true
3257
3258 + '@betteroffice/fonts-cjk@0.4.0':
3259 + resolution: {integrity: sha512-gLLqoIdVA02H5GQs+TpGrHWzxYEbpxj8a1Kzkf3cGQUea+cCpzoCuO38sV0zSDmBbub3gRldzf0N5fPpKCsS5g==}
3260 +
3261 '@betteroffice/fonts@0.4.0':
3262 resolution: {integrity: sha512-x1BVwYDAgY5R6TWI4t9NLguSiAEbH5GItN2ZlpBIFoRXTIJgjBsXd6sUxPGpexXOyY6KdhRjG8XvHjyAYxEGCw==}
3263 peerDependencies:
@@ -13456,11 +13462,15 @@ snapshots:
13462 optionalDependencies:
13463 '@types/react': 19.3.0
13464
13459 - '@betteroffice/docx@0.4.3(@betteroffice/fonts@0.4.0)':
13465 + '@betteroffice/docx@0.4.3(@betteroffice/fonts@0.4.0(@betteroffice/fonts-cjk@0.4.0))':
13466 optionalDependencies:
13461 - '@betteroffice/fonts': 0.4.0
13467 + '@betteroffice/fonts': 0.4.0(@betteroffice/fonts-cjk@0.4.0)
13468
13463 - '@betteroffice/fonts@0.4.0': {}
13469 + '@betteroffice/fonts-cjk@0.4.0': {}
13470 +
13471 + '@betteroffice/fonts@0.4.0(@betteroffice/fonts-cjk@0.4.0)':
13472 + optionalDependencies:
13473 + '@betteroffice/fonts-cjk': 0.4.0
13474
13475 '@betteroffice/pptx@0.2.0': {}
13476
third-party-licenses.config.json
+15
@@ -330,6 +330,21 @@
330 }
331 ]
332 },
333 + {
334 + "name": "@betteroffice/fonts-cjk",
335 + "version": "0.4.0",
336 + "license": "Apache-2.0 AND OFL-1.1",
337 + "generatedNotices": [
338 + { "licenseId": "Apache-2.0" },
339 + {
340 + "licenseId": "OFL-1.1",
341 + "copyrights": [
342 + "Copyright 2014-2021 Adobe (https://adobe.com/), with Reserved Font Name 'Source'",
343 + "Copyright 2017-2024 Adobe (https://adobe.com/), with Reserved Font Name 'Source'"
344 + ]
345 + }
346 + ]
347 + },
348 {
349 "name": "remark-math",
350 "version": "6.0.0",