Skip to content

Commit 6464729

Browse files
fix(web): improve PowerPoint slide font rendering (#34453)
Co-authored-by: Cursor <[email protected]>
1 parent e63d80a commit 6464729

6 files changed

Lines changed: 99 additions & 11 deletions

File tree

‎apps/web/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@betteroffice/docx": "0.4.3",
1717
"@betteroffice/fonts": "0.4.0",
1818
"@betteroffice/fonts-cjk": "0.4.0",
19-
"@betteroffice/pptx": "0.2.0",
19+
"@betteroffice/pptx": "0.5.1",
2020
"@betteroffice/xlsx": "0.5.1",
2121
"@clerk/clerk-js": "catalog:",
2222
"@clerk/electron": "catalog:",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import JSZip from "jszip";
2+
import { initWasm, openPresentation } from "@betteroffice/pptx";
3+
import { expect, it } from "vite-plus/test";
4+
import { slideFonts } from "./slide-fonts";
5+
import { deckText } from "./slide-text";
6+
7+
const namespaces =
8+
'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"';
9+
10+
function relationships(type: string, target: string) {
11+
return `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/${type}" Target="${target}"/></Relationships>`;
12+
}
13+
14+
/** A one-slide deck whose text box has one paragraph per run, each in its own family. */
15+
async function deck(runs: readonly { text: string; family: string }[]) {
16+
const paragraphs = runs
17+
.map(
18+
(run) =>
19+
`<a:p><a:r><a:rPr lang="en-US" sz="2400"><a:latin typeface="${run.family}"/></a:rPr><a:t>${run.text}</a:t></a:r></a:p>`,
20+
)
21+
.join("");
22+
23+
return new JSZip()
24+
.file(
25+
"[Content_Types].xml",
26+
'<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/ppt/presentation.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"/><Override PartName="/ppt/slides/slide1.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml"/></Types>',
27+
)
28+
.file("_rels/.rels", relationships("officeDocument", "ppt/presentation.xml"))
29+
.file(
30+
"ppt/presentation.xml",
31+
`<p:presentation ${namespaces}><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst><p:sldSz cx="9144000" cy="5143500"/></p:presentation>`,
32+
)
33+
.file("ppt/_rels/presentation.xml.rels", relationships("slide", "slides/slide1.xml"))
34+
.file(
35+
"ppt/slides/slide1.xml",
36+
`<p:sld ${namespaces}><p:cSld><p:spTree><p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr><p:grpSpPr/><p:sp><p:nvSpPr><p:cNvPr id="2" name="Text"/><p:cNvSpPr txBox="1"/><p:nvPr/></p:nvSpPr><p:spPr><a:xfrm><a:off x="457200" y="457200"/><a:ext cx="8229600" cy="2000000"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></p:spPr><p:txBody><a:bodyPr/>${paragraphs}</p:txBody></p:sp></p:spTree></p:cSld></p:sld>`,
37+
)
38+
.generateAsync({ type: "uint8array" });
39+
}
40+
41+
it("lays slide text out with heavy stand-ins and script fallbacks, and draws text no bundled face covers whole", async () => {
42+
await initWasm();
43+
const bytes = await deck([
44+
{ text: "Plain", family: "Arial" },
45+
{ text: "Heavy", family: "Arial Black" },
46+
{ text: "日本語のテキスト", family: "Times New Roman" },
47+
{ text: "ภาษาไทย", family: "Arial" },
48+
]);
49+
const handle = openPresentation(bytes);
50+
const fonts = slideFonts(handle, document.fonts);
51+
52+
await fonts.load(deckText(bytes, handle.snapshot()));
53+
54+
const frame = await fonts.layout(0);
55+
const [plain, heavy, japanese, thai] = frame.primitives.flatMap((primitive) =>
56+
primitive.kind === "textBox" ? primitive.lines.flatMap((line) => line.runs) : [],
57+
);
58+
59+
// Arial Black's only stand-in is a bold face, which its regular text draws with rather than Arial's.
60+
expect(heavy?.fontFamily).not.toBe(plain?.fontFamily);
61+
expect(plain?.glyphs).toHaveLength(5);
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([]);
67+
});

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ export function slideFonts(deck: PresentationHandle, fonts: FontFaceSet) {
3838

3939
if (!source) return;
4040

41-
// A family without the requested style gets the face it has, which the browser then slants or emboldens.
42-
const style = { bold: source.weight === 700, italic: source.style === "italic" };
41+
// A family without the requested style gets the face it has, which the browser then slants or emboldens. The engine
42+
// draws a bold run with a family's regular face but never the reverse, so a heavier stand-in, such as Arial Black's
43+
// bold one, registers at the requested style.
44+
const style = {
45+
bold: requested.bold && source.weight === 700,
46+
italic: requested.italic && source.style === "italic",
47+
};
4348
// The worker holds this deck's faces alone, so the names only need to differ from each other.
4449
const css = names.get(wantKey(want)) ?? `slide-${names.size}`;
4550
const key = faceKey(want, style);
@@ -119,6 +124,9 @@ export function slideFonts(deck: PresentationHandle, fonts: FontFaceSet) {
119124
runs: line.runs.map((run) => ({
120125
...run,
121126
fontFamily: faces.get(run.fontId)?.css ?? run.fontFamily,
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.
129+
glyphs: run.glyphs.some((glyph) => glyph.glyphId === 0) ? [] : run.glyphs,
122130
})),
123131
})),
124132
};

‎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
}

‎pnpm-lock.yaml‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎third-party-licenses.config.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
},
271271
{
272272
"name": "@betteroffice/pptx",
273-
"version": "0.2.0",
273+
"version": "0.5.1",
274274
"license": "Apache-2.0",
275275
"sourceUrl": "https://github.com/openooxml/betteroffice",
276276
"generatedNotice": {

0 commit comments

Comments
 (0)