@devries-cameron20 / t3code Mirror · read-only
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, 04:45 UTC

main
ts 84 lines 4.41 KB
Raw
1 import JSZip from "jszip";
2 import { expect, it, vi } from "vite-plus/test";
3 import type { DisplayPage, GlyphRunPrimitive } from "@betteroffice/docx/layout/render";
4 import { openWordDocument } from "./word-document";
5 import { withWholeMissingGlyphs } from "./word-glyphs";
6
7 const ns = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"';
8
9 function relationship(id: string, type: string, target: string) {
10 return `<Relationship Id="${id}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/${type}" Target="${target}"/>`;
11 }
12
13 /** A document with a numbered Calibri paragraph and an Arial paragraph of Thai, which no bundled face covers. */
14 function document_() {
15 const run = (font: string, text: string) =>
16 `<w:r><w:rPr><w:rFonts w:ascii="${font}" w:hAnsi="${font}" w:cs="${font}"/></w:rPr><w:t>${text}</w:t></w:r>`;
17
18 return new JSZip()
19 .file(
20 "[Content_Types].xml",
21 '<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="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/><Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/></Types>',
22 )
23 .file(
24 "_rels/.rels",
25 `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">${relationship("rId1", "officeDocument", "word/document.xml")}</Relationships>`,
26 )
27 .file(
28 "word/_rels/document.xml.rels",
29 `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">${relationship("rId1", "numbering", "numbering.xml")}</Relationships>`,
30 )
31 .file(
32 "word/numbering.xml",
33 `<w:numbering ${ns}><w:abstractNum w:abstractNumId="0"><w:lvl w:ilvl="0"><w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%1."/><w:pPr><w:ind w:left="720" w:hanging="360"/></w:pPr><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr></w:lvl></w:abstractNum><w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num></w:numbering>`,
34 )
35 .file(
36 "word/document.xml",
37 `<w:document ${ns}><w:body><w:p><w:pPr><w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr></w:pPr>${run("Calibri", "Numbered")}</w:p><w:p>${run("Arial", "ภาษาไทย")}</w:p><w:sectPr><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440"/></w:sectPr></w:body></w:document>`,
38 )
39 .generateAsync({ type: "uint8array" });
40 }
41
42 it("draws list numbers with the document's faces, and text no bundled face covers with the system's", async () => {
43 const opened = await openWordDocument(await document_(), document.fonts);
44 const drawn: { text: string; font: string }[] = [];
45 using _fillText = vi
46 .spyOn(OffscreenCanvasRenderingContext2D.prototype, "fillText")
47 .mockImplementation(function (this: OffscreenCanvasRenderingContext2D, text) {
48 drawn.push({ text, font: this.font });
49 });
50
51 await opened.paint(0, 1, 1, new AbortController().signal);
52
53 expect(drawn.map((call) => call.text)).toEqual(expect.arrayContaining(["1.", "ภาษาไทย"]));
54 expect(drawn.find((call) => call.text === "1.")?.font).toContain("Calibri");
55 expect([...document.fonts].map((face) => face.family.replaceAll('"', ""))).toContain("Calibri");
56 });
57
58 it("joins missing glyphs only across runs styled alike, with clusters into the joined text", () => {
59 const run = (text: string, at: number, style: Partial<GlyphRunPrimitive> = {}) =>
60 ({
61 kind: "glyphRun",
62 fontId: 2,
63 size: 16,
64 color: "#000000",
65 text,
66 glyphs: [{ id: 0, x: at * 10, y: 20, cluster: 0, advance: 10 }],
67 docStart: at,
68 docEnd: at + 1,
69 ...style,
70 }) satisfies GlyphRunPrimitive;
71 const page = {
72 width: 100,
73 height: 100,
74 primitives: [run("ภ", 0), run("า", 1), run("ษ", 2, { textOutline: true })],
75 } as unknown as DisplayPage;
76 const measure = new OffscreenCanvas(1, 1).getContext("2d")!;
77
78 const [joined, outlined] = withWholeMissingGlyphs(page, measure)
79 .primitives as GlyphRunPrimitive[];
80
81 expect(joined?.text).toBe("ภา");
82 expect(joined?.glyphs.map((glyph) => glyph.cluster)).toEqual([0, 3]);
83 expect(outlined).toMatchObject({ text: "ษ", textOutline: true });
84 });