Skip to content

Commit 38eb1ef

Browse files
committed
fix(web): improve PowerPoint slide font rendering
- Upgrade the PPTX renderer and register heavier font stand-ins correctly - Draw runs with missing glyphs as whole text to preserve fallback rendering
1 parent 7e565c5 commit 38eb1ef

5 files changed

Lines changed: 80 additions & 9 deletions

File tree

‎apps/web/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"@base-ui/react": "^1.9.0",
1616
"@betteroffice/docx": "0.4.3",
1717
"@betteroffice/fonts": "0.4.0",
18-
"@betteroffice/pptx": "0.2.0",
18+
"@betteroffice/pptx": "0.5.1",
1919
"@betteroffice/xlsx": "0.3.0",
2020
"@clerk/clerk-js": "catalog:",
2121
"@clerk/electron": "catalog:",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 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: "Arial" },
47+
]);
48+
const handle = openPresentation(bytes);
49+
const fonts = slideFonts(handle, document.fonts);
50+
51+
await fonts.load(deckText(bytes, handle.snapshot()));
52+
53+
const frame = await fonts.layout(0);
54+
const [plain, heavy, japanese] = frame.primitives.flatMap((primitive) =>
55+
primitive.kind === "textBox" ? primitive.lines.flatMap((line) => line.runs) : [],
56+
);
57+
58+
// Arial Black's only stand-in is a bold face, which its regular text draws with rather than Arial's.
59+
expect(heavy?.fontFamily).not.toBe(plain?.fontFamily);
60+
expect(plain?.glyphs).toHaveLength(5);
61+
expect(japanese?.text).toBe("日本語のテキスト");
62+
expect(japanese?.glyphs).toEqual([]);
63+
});

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

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

3838
if (!source) return;
3939

40-
// A family without the requested style gets the face it has, which the browser then slants or emboldens.
41-
const style = { bold: source.weight === 700, italic: source.style === "italic" };
40+
// A family without the requested style gets the face it has, which the browser then slants or emboldens. The engine
41+
// draws a bold run with a family's regular face but never the reverse, so a heavier stand-in, such as Arial Black's
42+
// bold one, registers at the requested style.
43+
const style = {
44+
bold: requested.bold && source.weight === 700,
45+
italic: requested.italic && source.style === "italic",
46+
};
4247
// The worker holds this deck's faces alone, so the names only need to differ from each other.
4348
const css = names.get(wantKey(want)) ?? `slide-${names.size}`;
4449
const key = faceKey(want, style);
@@ -118,6 +123,9 @@ export function slideFonts(deck: PresentationHandle, fonts: FontFaceSet) {
118123
runs: line.runs.map((run) => ({
119124
...run,
120125
fontFamily: faces.get(run.fontId)?.css ?? run.fontFamily,
126+
// The engine places glyphs no bundled face has, such as Chinese, Japanese or Korean ones, a missing glyph's
127+
// width apart, but the browser draws them from its own fonts. Without positions a run is drawn whole.
128+
glyphs: run.glyphs.some((glyph) => glyph.glyphId === 0) ? [] : run.glyphs,
121129
})),
122130
})),
123131
};

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