// ECMA-376 Part 1 §20.4.2.10/§20.4.2.11 — where an anchored drawing stands on
// its page, measured against Word (Word for Mac 16, PDF export of synthetic
// probes: every `relativeFrom` × every alignment and an offset, in a header, a
// footer and the body, on an odd and an even page). Word places a header's or
// a footer's drawing exactly as it places the body's, and every expected
// number below is one Word printed.
//
// #143: a first-page header picture as wide as the page, aligned right
// against the PAGE, was aligned against the text area instead — a white strip
// as wide as the right margin down the side of the sheet, and the picture's
// left side cut by as much.
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { buildDocxFromBody } from './fixtures/build-docx';
import { buildTinyPng } from './fixtures/build-png';
import type { FixtureImage } 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';
import { readDocx } from '@/word/docx-reader';
const FONTS = {
regular: new Uint8Array(readFileSync('tests/fixtures/fonts/Roboto-Regular.ttf')),
bold: new Uint8Array(readFileSync('tests/fixtures/fonts/Roboto-Bold.ttf')),
};
const EMU = 12700;
const PNG: FixtureImage = {
contentType: 'image/png',
bytes: buildTinyPng(4, 4, [48, 112, 208, 255]),
extension: 'png',
};
/** One `wp:positionH`/`wp:positionV`: a keyword, or an offset in points. */
const pos = (axis: 'H' | 'V', rel: string, at: string | number): string =>
`` +
(typeof at === 'number'
? `${String(Math.round(at * EMU))}`
: `${at}`) +
``;
let docPrId = 1;
/** A floating picture `wPt` × `hPt`, wrapped in no text. */
function picture(wPt: number, hPt: number, posH: string, posV: string, behind = false): string {
const id = docPrId++;
const cx = Math.round(wPt * EMU);
const cy = Math.round(hPt * EMU);
return (
`` +
`${posH}${posV}` +
`` +
'' +
`` +
'' +
`` +
'' +
''
);
}
/** Paragraphs of exactly 12pt lines, so every renderer agrees where they are. */
const P = (inner: string): string =>
`${inner}`;
const T = (text: string): string => `${text}`;
const BR = '';
const PAGE_BREAK = P('');
/**
* #143's page: A4, its margins 72pt left, 49.25pt right, 57.6pt top, 43.15pt
* bottom; the header 14.2pt from the top edge and the footer 28.35pt up from
* the bottom one.
*/
const pgMar =
'' +
'';
type Where = 'header' | 'footer' | 'body';
/** A two-page document whose header, footer or body (on each page) holds `pictures`. */
function twoPages(where: Where, pictures: string, band = P(T('band'))): Uint8Array {
const refs =
(where === 'header' ? '' : '') +
(where === 'footer' ? '' : '');
const content = where === 'body' ? P(pictures) : P(T('one'));
const body = `${content}${PAGE_BREAK}${where === 'body' ? P(pictures) : P(T('two'))}`;
const bandXml = band.replace('', `${pictures}`);
return buildDocxFromBody(`${body}${refs}${pgMar}`, {
...(where === 'body' ? { images: { rIdP: PNG } } : {}),
...(where === 'header' ? { headerXml: bandXml, headerImages: { rIdP: PNG } } : {}),
...(where === 'footer' ? { footerXml: bandXml, footerImages: { rIdP: PNG } } : {}),
});
}
/** Each picture on each page, by its width: its left edge and its top (from the page's top). */
function placed(docx: Uint8Array): Array