|
| 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 | +}); |
0 commit comments