import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; import { buildDocxFromBody } from './fixtures/build-docx'; import { Ream } from '@/core/converter/ream'; import { FontRegistry } from '@/core/font'; import { flowRenderOptions } from '@/core/converter/project'; import { layoutStyledDocument } from '@/layout/styled-layout'; const FONTS = { regular: new Uint8Array(readFileSync('tests/fixtures/fonts/Roboto-Regular.ttf')), bold: new Uint8Array(readFileSync('tests/fixtures/fonts/Roboto-Bold.ttf')), }; function lineXs(commands: ReadonlyArray<{ type: string }>): Array { return commands .filter((c) => c.type === 'line') .map((c) => (c as unknown as { originX: number }).originX); } function layoutOf(docx: Uint8Array) { const flow = Ream.parse(docx).flow; return layoutStyledDocument(flow.body, { registry: FontRegistry.fromBytes(FONTS), ...flowRenderOptions(flow), }); } describe('multi-column sections (§17.6.4)', () => { // A5 portrait-ish page, 2 columns, enough lines to overflow column 1. const docx = (cols: string, lines = 30) => buildDocxFromBody( Array.from({ length: lines }, (_, i) => `line ${i}`).join( '', ) + `` + `${cols}`, ); it('fills column one, then column two, then the next page', () => { const laid = layoutOf(docx('', 90)); const xs = lineXs(laid.pages[0]!.commands); // content width = 420−72 = 348pt; column = (348−36)/2 = 156; col2 x = 36+156+36 = 228. const col1 = xs.filter((x) => Math.abs(x - 36) < 1); const col2 = xs.filter((x) => Math.abs(x - 228) < 1); expect(col1.length).toBeGreaterThan(0); expect(col2.length).toBeGreaterThan(0); // Reading order on the page: all column-1 lines come before column-2 ones. const firstCol2Idx = xs.findIndex((x) => Math.abs(x - 228) < 1); expect(xs.slice(firstCol2Idx).every((x) => Math.abs(x - 228) < 1)).toBe(true); // 90 lines at ~37 per column overflow both columns → a second page exists // and starts back at column one. expect(laid.pages.length).toBeGreaterThan(1); const xs2 = lineXs(laid.pages[1]!.commands); expect(Math.abs(xs2[0]! - 36)).toBeLessThan(1); }); it('explicit unequal w:col widths position the second column correctly', () => { const laid = layoutOf( docx('', 60), ); const xs = lineXs(laid.pages[0]!.commands); // col1 at 36pt; col2 at 36 + 150 + 36 = 222pt. expect(xs.some((x) => Math.abs(x - 222) < 1)).toBe(true); }); it('re-breaks what lands in a column of another width', () => { // §17.6.4 `w:equalWidth="0"`: a narrow column beside a wide one. Every // block is broken at the FIRST column's measure, so the wide column was // set narrow — fdo77812.docx took two pages for LibreOffice's one. const wide = layoutOf( docx( '', 60, ), ); const lines = wide.pages[0]!.commands.filter((c) => c.type === 'line').map( (c) => c as unknown as { originX: number; line: { availableWidthPt: number } }, ); // col1 = 1200tw = 60pt at x=36; col2 = 4680tw = 234pt at 36+60+36 = 132pt. const col1 = lines.filter((l) => Math.abs(l.originX - 36) < 1); const col2 = lines.filter((l) => Math.abs(l.originX - 132) < 1); expect(col1.length).toBeGreaterThan(0); expect(col2.length).toBeGreaterThan(0); expect(col1[0]!.line.availableWidthPt).toBeCloseTo(60, 0); // The paragraph that CROSSES the boundary keeps the measure it began with // — re-breaking a half-placed paragraph is a different job — so the check // is on the ones that start in the wide column. expect(col2.at(-1)!.line.availableWidthPt).toBeCloseTo(234, 0); }); it('single-column documents are untouched by the column machinery', () => { const laid = layoutOf(docx('', 5)); const xs = lineXs(laid.pages[0]!.commands); expect(xs.every((x) => Math.abs(x - 36) < 1)).toBe(true); }); }); describe('a section that ends at a continuous break (§17.6.4)', () => { it('evens out its columns', () => { // default-sect-break-cols.docx sets two words in two columns and follows // them with a continuous break: both references put one beside the other, // and we stacked them in the first column. const laid = layoutOf( buildDocxFromBody( 'First.' + 'Second.' + '' + '' + '' + 'After.' + '' + '' + '', ), ); const xs = lineXs(laid.pages[0]!.commands); expect(laid.pages).toHaveLength(1); // Column one at the left margin (1008 twips = 50.4pt), column two past it. expect(xs[0]).toBeCloseTo(50.4, 1); expect(xs[1]).toBeGreaterThan(200); }); }); describe('a column break (§17.3.3.1 w:br w:type="column")', () => { const twoCols = (bodyXml: string) => buildDocxFromBody( bodyXml + `` + `` + ``, ); it('sends what follows it to the next column, mid-paragraph', () => { // columnbreak.docx breaks inside one paragraph, and read as a soft line // break both halves stayed in the first column. const laid = layoutOf( twoCols( 'before' + '' + 'after', ), ); const xs = lineXs(laid.pages[0]!.commands); expect(xs).toHaveLength(2); expect(xs[0]).toBeCloseTo(36, 0); // column one expect(xs[1]).toBeCloseTo(228, 0); // column two expect(laid.pages).toHaveLength(1); }); it('starts a fresh page when the break falls in the last column', () => { const laid = layoutOf( twoCols( 'a' + 'b' + 'c', ), ); expect(laid.pages).toHaveLength(2); expect(lineXs(laid.pages[1]!.commands)[0]).toBeCloseTo(36, 0); }); it('breaks where it stands in a run it shares with the words', () => { // Word: "before" in column one, "after" at the head of column two. const laid = layoutOf( twoCols('beforeafter'), ); const xs = lineXs(laid.pages[0]!.commands); expect(xs).toHaveLength(2); expect(xs[0]).toBeCloseTo(36, 0); expect(xs[1]).toBeCloseTo(228, 0); }); it('leaves the line before it in its column under widow control', () => { // Word: "one" and "before" in column one, "after" in column two, one // page. Read as a line the column could not hold, the break left "before" // alone at its foot, and widow control sent it on with "after" behind it. const laid = layoutOf( twoCols( 'one' + 'before' + 'after', ), ); expect(laid.pages).toHaveLength(1); const xs = lineXs(laid.pages[0]!.commands); expect(xs.map((x) => Math.round(x))).toEqual([36, 36, 228]); }); it('sends a page break on to the next page, not the next column', () => { // Word: "Second" at the head of page 2's first column. const laid = layoutOf( twoCols('LatinSecond'), ); expect(laid.pages).toHaveLength(2); expect(lineXs(laid.pages[0]!.commands)).toHaveLength(1); expect(lineXs(laid.pages[1]!.commands)[0]).toBeCloseTo(36, 0); }); }); describe('a text frame (§17.3.1.11 w:framePr)', () => { const framed = (framePr: string, text = 'Name', after = '') => buildDocxFromBody( 'first' + `${framePr}${text}` + after + 'last' + '' + '', ); it('leaves the flow and lands where the frame says', () => { // content-control-grab-bag.docx hangs its frame off the page at // x=6561/y=2705 twips; read inline it sat between "first" and "last". const laid = layoutOf( framed( '', ), ); const lines = laid.pages[0]!.commands.filter((c) => c.type === 'line'); const texts = lines.map((c) => (c as unknown as { line: { tokens: ReadonlyArray<{ text?: string }> } }).line.tokens .map((t) => t.text ?? '') .join(''), ); const xs = lineXs(laid.pages[0]!.commands); const idx = texts.indexOf('Name'); expect(idx).toBeGreaterThanOrEqual(0); // 6561 twips = 328.05pt from the page's left edge. expect(xs[idx]).toBeCloseTo(328.05, 1); // …and the two body paragraphs sit together at the text margin. expect(xs[texts.indexOf('first')]).toBeCloseTo(68.3, 1); expect(xs[texts.indexOf('last')]).toBeCloseTo(68.3, 1); }); it('is as wide as its text when it states no width', () => { // CT-with-frame.docx's marginal note states none: taken as the full column // it excluded the whole of it and pushed the paragraph beside it away. const laid = layoutOf( framed('', '0123'), ); const body = laid.pages[0]!.commands.filter((c) => c.type === 'line') .map( (c) => c as unknown as { originX: number; line: { tokens: ReadonlyArray<{ text?: string }> } }, ) .filter((c) => c.line.tokens.map((t) => t.text ?? '').join('') === 'last'); expect(body[0]!.originX).toBeCloseTo(68.3, 1); }); it('holds consecutive paragraphs of the same frame together', () => { const laid = layoutOf( framed( '', 'one', 'two', ), ); const lines = laid.pages[0]!.commands.filter((c) => c.type === 'line').map( (c) => c as unknown as { originX: number; baselineY: number; line: { tokens: ReadonlyArray<{ text?: string }> }; }, ); const one = lines.find((l) => l.line.tokens.map((t) => t.text ?? '').join('') === 'one')!; const two = lines.find((l) => l.line.tokens.map((t) => t.text ?? '').join('') === 'two')!; // Same box, one under the other — not two frames at the same spot. expect(two.originX).toBeCloseTo(one.originX, 1); expect(two.baselineY).toBeGreaterThan(one.baselineY); }); // §17.18.104 `notBeside`: the frame keeps its own place and no text stands // beside it — the band is the whole column however narrow the frame is. const NOT_BESIDE = (x: number) => ``; it('places two notBeside frames side by side, where their anchors put them', () => { // tdf100075.docx writes exactly this pair; read as a top-and-bottom wrap // they went back into the flow, lost their x and stacked. const laid = layoutOf( framed( NOT_BESIDE(1815), 'one', `${NOT_BESIDE(4815)}two`, ), ); const lines = laid.pages[0]!.commands.filter((c) => c.type === 'line').map( (c) => c as unknown as { originX: number; baselineY: number; line: { tokens: ReadonlyArray<{ text?: string }> }; }, ); const at = (t: string) => lines.find((l) => l.line.tokens.map((k) => k.text ?? '').join('') === t)!; // 1815 and 4815 twips from the page's left edge. expect(at('one').originX).toBeCloseTo(90.75, 1); expect(at('two').originX).toBeCloseTo(240.75, 1); expect(at('two').baselineY).toBeCloseTo(at('one').baselineY, 1); }); it('lets no body text stand beside a notBeside frame', () => { const laid = layoutOf(framed(NOT_BESIDE(1815), 'one')); const lines = laid.pages[0]!.commands.filter((c) => c.type === 'line').map( (c) => c as unknown as { originX: number; baselineY: number; line: { tokens: ReadonlyArray<{ text?: string }> }; }, ); const at = (t: string) => lines.find((l) => l.line.tokens.map((k) => k.text ?? '').join('') === t)!; // The frame is 113pt wide with room to spare beside it; "last" goes under // it all the same, which is the whole of what the mode asks for. expect(at('last').baselineY).toBeGreaterThan(at('one').baselineY + 86); expect(at('last').originX).toBeCloseTo(68.3, 1); }); it('keeps a TEXT-anchored frame in the flow — it has nowhere else to stand', () => { // tdf105035_framePrB.docx styles its title and its author line as // text-anchored notBeside frames. Lifted out of the flow both landed on // the same cursor and printed over each other. const inFlow = ''; const laid = layoutOf( framed(inFlow, 'one', `${inFlow}two`), ); const lines = laid.pages[0]!.commands.filter((c) => c.type === 'line').map( (c) => c as unknown as { baselineY: number; line: { tokens: ReadonlyArray<{ text?: string }> } }, ); const at = (t: string) => lines.find((l) => l.line.tokens.map((k) => k.text ?? '').join('') === t)!; expect(at('two').baselineY).toBeGreaterThan(at('one').baselineY); }); }); describe('a measurement that names its unit (§22.9.2.15)', () => { it('is read wherever a count of twips would be', () => { // tdf116410.docx writes its tab stop, its indent and its spacing as // "85.05pt" / "-13.8pt" / "18pt". Read as plain numbers they were nothing // at all, so the paragraph lost the stop its dot leader hangs on. const docx = buildDocxFromBody( '' + '' + '' + 'AB' + '' + '', ); const line = layoutOf(docx).pages[0]!.commands.find((c) => c.type === 'line') as unknown as { originX: number; line: { tokens: ReadonlyArray<{ text?: string }> }; }; // The indent is -13.8pt from the 68.3pt margin… expect(line.originX).toBeCloseTo(68.3 - 13.8, 1); // …and the tab reached its stop through a leader of dots. expect(line.line.tokens.map((t) => t.text ?? '').join('')).toMatch(/^A\.+B$/u); }); it('is read in the unit the attribute is written in, not always twips', () => { // §17.3.2.38 — `w:sz` is HALF-POINTS, and its universal form is points: // tdf108408.docx asks for `w:val="20pt"` and read as twips it set the // sample text two hundred points tall. const docx = buildDocxFromBody( 'Sample text', ); const line = layoutOf(docx).pages[0]!.commands.find((c) => c.type === 'line') as unknown as { line: { maxFontSizePt: number }; }; expect(line.line.maxFontSizePt).toBeCloseTo(20, 1); }); });