Skip to content

Commit 3f0fcfb

Browse files
fix(web): PowerPoint previews lay out CJK text with the bundled CJK faces
Co-authored-by: Cursor <[email protected]>
1 parent a6ca255 commit 3f0fcfb

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

‎apps/web/src/components/files/office/slide-fonts.browser.test.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,30 @@ async function deck(runs: readonly { text: string; family: string }[]) {
3838
.generateAsync({ type: "uint8array" });
3939
}
4040

41-
it("lays slide text out with heavy stand-ins and draws text no bundled face covers whole", async () => {
41+
it("lays slide text out with heavy stand-ins and script fallbacks, and draws text no bundled face covers whole", async () => {
4242
await initWasm();
4343
const bytes = await deck([
4444
{ text: "Plain", family: "Arial" },
4545
{ text: "Heavy", family: "Arial Black" },
46-
{ text: "日本語のテキスト", family: "Arial" },
46+
{ text: "日本語のテキスト", family: "Times New Roman" },
47+
{ text: "ภาษาไทย", family: "Arial" },
4748
]);
4849
const handle = openPresentation(bytes);
4950
const fonts = slideFonts(handle, document.fonts);
5051

5152
await fonts.load(deckText(bytes, handle.snapshot()));
5253

5354
const frame = await fonts.layout(0);
54-
const [plain, heavy, japanese] = frame.primitives.flatMap((primitive) =>
55+
const [plain, heavy, japanese, thai] = frame.primitives.flatMap((primitive) =>
5556
primitive.kind === "textBox" ? primitive.lines.flatMap((line) => line.runs) : [],
5657
);
5758

5859
// Arial Black's only stand-in is a bold face, which its regular text draws with rather than Arial's.
5960
expect(heavy?.fontFamily).not.toBe(plain?.fontFamily);
6061
expect(plain?.glyphs).toHaveLength(5);
61-
expect(japanese?.text).toBe("日本語のテキスト");
62-
expect(japanese?.glyphs).toEqual([]);
62+
expect(japanese?.fontFamily).not.toBe(plain?.fontFamily);
63+
expect(japanese?.glyphs.length).toBeGreaterThan(0);
64+
expect(japanese?.glyphs.some((glyph) => glyph.glyphId === 0)).toBe(false);
65+
expect(thai?.text).toBe("ภาษาไทย");
66+
expect(thai?.glyphs).toEqual([]);
6367
});

‎apps/web/src/components/files/office/slide-fonts.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ export function slideFonts(deck: PresentationHandle, fonts: FontFaceSet) {
124124
runs: line.runs.map((run) => ({
125125
...run,
126126
fontFamily: faces.get(run.fontId)?.css ?? run.fontFamily,
127-
// The engine places glyphs no bundled face has, such as Chinese, Japanese or Korean ones, a missing glyph's
128-
// width apart, but the browser draws them from its own fonts. Without positions a run is drawn whole.
127+
// The engine places glyphs no bundled face has, such as Thai ones or emoji, a missing glyph's width apart,
128+
// but the browser draws them from its own fonts. Without positions a run is drawn whole.
129129
glyphs: run.glyphs.some((glyph) => glyph.glyphId === 0) ? [] : run.glyphs,
130130
})),
131131
})),

‎apps/web/src/components/files/office/slide-text.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type FontStyle = { readonly bold: boolean; readonly italic: boolean };
1111
export type DeckFamily = { readonly name: string; readonly styles: readonly FontStyle[] };
1212

1313
/** The scripts the bundled Latin faces may lack, which have a bundled fallback face. */
14-
export type FallbackScript = "hebrew" | "arabic";
14+
export type FallbackScript = "hebrew" | "arabic" | "cjk-jp" | "cjk-kr" | "cjk-sc";
1515

1616
/** What a deck's text needs from the fonts, and which slides it hides. */
1717
export type DeckText = {
@@ -125,8 +125,21 @@ const decodeInspected = Schema.decodeUnknownOption(Inspected);
125125
const scriptRanges: readonly (readonly [FallbackScript, RegExp])[] = [
126126
["hebrew", /[\u0590-\u05FF\uFB1D-\uFB4F]/u],
127127
["arabic", /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/u],
128+
["cjk-jp", /[\u3040-\u30FF\u31F0-\u31FF\uFF66-\uFF9F]/u],
129+
["cjk-kr", /[\u1100-\u11FF\u3130-\u318F\uAC00-\uD7AF]/u],
128130
];
129131

132+
const hanRange = /[\u3000-\u303F\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFF00-\uFFEF]/u;
133+
134+
function scripts(text: string): FallbackScript[] {
135+
const found = scriptRanges.flatMap((entry) => (entry[1].test(text) ? [entry[0]] : []));
136+
137+
// The Japanese and Korean faces also draw the Chinese characters their text mixes in.
138+
return hanRange.test(text) && !found.some((script) => script.startsWith("cjk-"))
139+
? [...found, "cjk-sc"]
140+
: found;
141+
}
142+
130143
/**
131144
* Reads the families, styles and scripts a deck's text uses, from its slides, layouts, masters and theme. When the
132145
* engine's inspection does not decode, the slides' own runs stand in, without theme fonts or hidden flags.
@@ -396,7 +409,7 @@ function summarize(
396409
(left, right) => styleRank(left) - styleRank(right),
397410
),
398411
})),
399-
scripts: scriptRanges.flatMap((entry) => (entry[1].test(text) ? [entry[0]] : [])),
412+
scripts: scripts(text),
400413
hidden,
401414
};
402415
}

0 commit comments

Comments
 (0)