@open-document/core 0.13.2 → 0.13.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -63,6 +63,7 @@ export type WalkResult = {
|
|
|
63
63
|
finalRunning: boolean;
|
|
64
64
|
finalColumns?: SectionLayout['columns'];
|
|
65
65
|
finalContinuous?: boolean;
|
|
66
|
+
finalFoot?: Paragraph;
|
|
66
67
|
};
|
|
67
68
|
|
|
68
69
|
type Ctx = WalkOptions & {
|
|
@@ -116,10 +117,14 @@ const INLINE_DISPLAYS = new Set([
|
|
|
116
117
|
const STRUCTURE = 'p,h1,h2,h3,h4,h5,h6,table,ul,ol,pre,blockquote';
|
|
117
118
|
|
|
118
119
|
/** Where each block sat on its sheet, for fixed pages whose layout is by position. */
|
|
119
|
-
|
|
120
|
+
type Box = { top: number; bottom: number; left: number; right: number };
|
|
120
121
|
|
|
121
|
-
|
|
122
|
-
|
|
122
|
+
const placed = new WeakMap<Block, Box>();
|
|
123
|
+
|
|
124
|
+
function place(block: Block, box: Box) {
|
|
125
|
+
if (!placed.has(block)) {
|
|
126
|
+
placed.set(block, { top: box.top, bottom: box.bottom, left: box.left, right: box.right });
|
|
127
|
+
}
|
|
123
128
|
}
|
|
124
129
|
|
|
125
130
|
function isHidden(el: Element): boolean {
|
|
@@ -303,12 +308,32 @@ async function inlinesOf(
|
|
|
303
308
|
);
|
|
304
309
|
out.push({ type: 'link', href: node.getAttribute('href') as string, runs });
|
|
305
310
|
} else {
|
|
306
|
-
|
|
311
|
+
const inner = await inlinesOf(Array.from(node.childNodes), base, ctx, linked);
|
|
312
|
+
out.push(...shaded(node, inner));
|
|
307
313
|
}
|
|
308
314
|
}
|
|
309
315
|
return out;
|
|
310
316
|
}
|
|
311
317
|
|
|
318
|
+
/**
|
|
319
|
+
* Words set on a tint of their own — a pill, a badge — keep it as a character
|
|
320
|
+
* background, and a pill's side padding as a space on either side.
|
|
321
|
+
*/
|
|
322
|
+
function shaded(el: Element, inlines: Inline[]): Inline[] {
|
|
323
|
+
const style = getComputedStyle(el);
|
|
324
|
+
if (!INLINE_DISPLAYS.has(style.display) || style.display === 'contents') return inlines;
|
|
325
|
+
const shade = hexColor(style.backgroundColor);
|
|
326
|
+
if (!shade) return inlines;
|
|
327
|
+
const tinted = inlines.map(
|
|
328
|
+
(inline): Inline =>
|
|
329
|
+
inline.type === 'text' ? { ...inline, shade: inline.shade ?? shade } : inline,
|
|
330
|
+
);
|
|
331
|
+
const first = tinted.find((inline): inline is TextRun => inline.type === 'text');
|
|
332
|
+
if (!first || (Number.parseFloat(style.paddingLeft) || 0) < 4) return tinted;
|
|
333
|
+
const pad: TextRun = { ...first, text: '\u00a0' };
|
|
334
|
+
return [pad, ...tinted, { ...pad }];
|
|
335
|
+
}
|
|
336
|
+
|
|
312
337
|
/** Trims the whitespace a browser would not draw: at either end, and doubled between runs. */
|
|
313
338
|
function tidy(inlines: Inline[]): Inline[] {
|
|
314
339
|
const texts: TextRun[] = [];
|
|
@@ -523,10 +548,20 @@ async function blockOfElement(el: Element, ctx: Ctx, out: Block[], style: StyleI
|
|
|
523
548
|
inlines.push(...tidy(await inlinesOf([piece], ctx.baselines[style], ctx)));
|
|
524
549
|
}
|
|
525
550
|
if (inlines.some((inline) => inline.type !== 'tab')) {
|
|
551
|
+
const look = getComputedStyle(el);
|
|
552
|
+
const rule = border(look, 'Top');
|
|
526
553
|
out.push({
|
|
527
554
|
type: 'paragraph',
|
|
528
555
|
inlines,
|
|
529
556
|
rightTab: ctx.textWidth * PX_TO_TWIPS,
|
|
557
|
+
...(rule
|
|
558
|
+
? {
|
|
559
|
+
ruleAbove: {
|
|
560
|
+
...rule,
|
|
561
|
+
gap: (Number.parseFloat(look.paddingTop) || 0) * PX_TO_TWIPS,
|
|
562
|
+
},
|
|
563
|
+
}
|
|
564
|
+
: {}),
|
|
530
565
|
...spacing(el),
|
|
531
566
|
...(style !== 'Normal' ? { style } : {}),
|
|
532
567
|
});
|
|
@@ -829,10 +864,15 @@ async function tableOf(table: HTMLTableElement, ctx: Ctx): Promise<Table> {
|
|
|
829
864
|
const edge = border(style, side);
|
|
830
865
|
if (edge) borders[side.toLowerCase() as 'top'] = edge;
|
|
831
866
|
}
|
|
867
|
+
const fill =
|
|
868
|
+
dataBarFill(style) ??
|
|
869
|
+
hexColor(style.backgroundColor) ??
|
|
870
|
+
hexColor(getComputedStyle(row).backgroundColor);
|
|
832
871
|
cells.push({
|
|
833
872
|
blocks: paragraphs,
|
|
834
873
|
...(cell.colSpan > 1 ? { span: cell.colSpan } : {}),
|
|
835
874
|
borders,
|
|
875
|
+
...(fill ? { fill } : {}),
|
|
836
876
|
padding: {
|
|
837
877
|
top: (Number.parseFloat(style.paddingTop) || 0) * PX_TO_TWIPS,
|
|
838
878
|
left: (Number.parseFloat(style.paddingLeft) || 0) * PX_TO_TWIPS,
|
|
@@ -846,6 +886,31 @@ async function tableOf(table: HTMLTableElement, ctx: Ctx): Promise<Table> {
|
|
|
846
886
|
return out;
|
|
847
887
|
}
|
|
848
888
|
|
|
889
|
+
/**
|
|
890
|
+
* A cell drawn with a bar behind its value — `linear-gradient(to right, tint
|
|
891
|
+
* n%, transparent n%)`, a data bar. Neither Word nor Pages can fill part of a
|
|
892
|
+
* cell, and Pages drops a picture pinned inside one, so the value is told by
|
|
893
|
+
* how deep the cell's tint is instead of how far it reaches.
|
|
894
|
+
*/
|
|
895
|
+
function dataBarFill(style: CSSStyleDeclaration): string | null {
|
|
896
|
+
const gradient = style.backgroundImage.match(
|
|
897
|
+
/^linear-gradient\(to right,\s*((?:rgba?|color)\([^)]*\))\s+([\d.]+)%/,
|
|
898
|
+
);
|
|
899
|
+
if (!gradient?.[1] || !gradient[2]) return null;
|
|
900
|
+
const tint = hexColor(gradient[1]);
|
|
901
|
+
const ratio = Math.min(1, Number(gradient[2]) / 100);
|
|
902
|
+
if (!tint || !(ratio > 0)) return null;
|
|
903
|
+
const depth = 0.25 + 0.75 * ratio;
|
|
904
|
+
return [0, 2, 4]
|
|
905
|
+
.map((at) => {
|
|
906
|
+
const channel = Number.parseInt(tint.slice(at, at + 2), 16);
|
|
907
|
+
return Math.round(255 - (255 - channel) * depth)
|
|
908
|
+
.toString(16)
|
|
909
|
+
.padStart(2, '0');
|
|
910
|
+
})
|
|
911
|
+
.join('');
|
|
912
|
+
}
|
|
913
|
+
|
|
849
914
|
async function figureOf(figure: Element, ctx: Ctx, out: Block[]) {
|
|
850
915
|
const start = out.length;
|
|
851
916
|
await figureParts(figure, ctx, out);
|
|
@@ -955,6 +1020,7 @@ export async function docxFromPages(
|
|
|
955
1020
|
first: index === 0,
|
|
956
1021
|
};
|
|
957
1022
|
const own: Block[] = [];
|
|
1023
|
+
let foot: Paragraph | undefined;
|
|
958
1024
|
const flowBlocks = Array.from(host.querySelectorAll(`[${FLOW_BLOCK_ATTR}]`));
|
|
959
1025
|
const columnsHere = flowBlocks.length > 0 ? columnBoxOf(host) : null;
|
|
960
1026
|
if (flowBlocks.length > 0) {
|
|
@@ -1030,6 +1096,22 @@ export async function docxFromPages(
|
|
|
1030
1096
|
}
|
|
1031
1097
|
bottom = Math.max(bottom, box.bottom);
|
|
1032
1098
|
}
|
|
1099
|
+
// A last line laid out against the foot of the sheet — a cover's byline,
|
|
1100
|
+
// pushed down by `justify-content: space-between` — becomes this page's
|
|
1101
|
+
// footer: as flowed text its place would depend on how tall everything
|
|
1102
|
+
// above it came out in Word, which is never quite what the page drew.
|
|
1103
|
+
const last = own[own.length - 1];
|
|
1104
|
+
const lastBox = last ? placed.get(last) : undefined;
|
|
1105
|
+
if (
|
|
1106
|
+
last?.type === 'paragraph' &&
|
|
1107
|
+
lastBox &&
|
|
1108
|
+
own.length > 1 &&
|
|
1109
|
+
rect.bottom - options.margin - lastBox.bottom < 24 &&
|
|
1110
|
+
(last.spacingBefore ?? 0) > 48 * PX_TO_TWIPS
|
|
1111
|
+
) {
|
|
1112
|
+
own.pop();
|
|
1113
|
+
foot = { ...last, spacingBefore: 0, spacingAfter: 0 };
|
|
1114
|
+
}
|
|
1033
1115
|
// Word sets a heading's line from the font's own height, taller than the
|
|
1034
1116
|
// page's CSS line-height, so a sheet laid out to its last pixel — a
|
|
1035
1117
|
// cover's byline at the foot — would spill its last lines onto a page of
|
|
@@ -1052,7 +1134,7 @@ export async function docxFromPages(
|
|
|
1052
1134
|
// prints its own running lines, so it sits in a section without them. A
|
|
1053
1135
|
// flow set in columns is a section of that many columns. The section
|
|
1054
1136
|
// break itself starts the next page.
|
|
1055
|
-
const layout: SectionLayout = { running: !ctx.page.fixed };
|
|
1137
|
+
const layout: SectionLayout = { running: !ctx.page.fixed, ...(foot ? { foot } : {}) };
|
|
1056
1138
|
if (!ctx.page.fixed && columnsHere) {
|
|
1057
1139
|
layout.columns = { count: columnsHere.count, gap: columnsHere.gap * PX_TO_TWIPS };
|
|
1058
1140
|
}
|
|
@@ -1060,7 +1142,9 @@ export async function docxFromPages(
|
|
|
1060
1142
|
own.length > 0 &&
|
|
1061
1143
|
blocks.length > 0 &&
|
|
1062
1144
|
(layout.running !== previous.running ||
|
|
1063
|
-
(layout.columns?.count ?? 1) !== (previous.columns?.count ?? 1)
|
|
1145
|
+
(layout.columns?.count ?? 1) !== (previous.columns?.count ?? 1) ||
|
|
1146
|
+
Boolean(layout.foot) ||
|
|
1147
|
+
Boolean(previous.foot));
|
|
1064
1148
|
// Columns fill one at a time to the foot of the page unless a continuous
|
|
1065
1149
|
// break ends them, so the section after a set of columns starts on their
|
|
1066
1150
|
// page and breaks to its own with the first paragraph instead.
|
|
@@ -1133,6 +1217,7 @@ export async function docxFromPages(
|
|
|
1133
1217
|
finalRunning: previous.running,
|
|
1134
1218
|
...(previous.columns ? { finalColumns: previous.columns } : {}),
|
|
1135
1219
|
...(previous.continuous ? { finalContinuous: true } : {}),
|
|
1220
|
+
...(previous.foot ? { finalFoot: previous.foot } : {}),
|
|
1136
1221
|
};
|
|
1137
1222
|
}
|
|
1138
1223
|
|
|
@@ -28,6 +28,11 @@ describe('fontsFor', () => {
|
|
|
28
28
|
});
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
+
it('skips Apple system faces no app can pick by name', () => {
|
|
32
|
+
expect(fontsFor('ui-monospace, "SF Mono", Menlo, monospace', 'mono').ascii).toBe('Menlo');
|
|
33
|
+
expect(fontsFor('"SF Pro Text", Helvetica, sans-serif', 'sans').ascii).toBe('Helvetica');
|
|
34
|
+
});
|
|
35
|
+
|
|
31
36
|
it('falls back by generic family when nothing is named', () => {
|
|
32
37
|
expect(fontsFor('system-ui, sans-serif', 'sans').ascii).toBe('Arial');
|
|
33
38
|
expect(fontsFor('serif', 'sans').ascii).toBe('Times New Roman');
|
|
@@ -368,6 +373,59 @@ describe('buildDocxParts', () => {
|
|
|
368
373
|
expect(doc.match(/<w:cols /g)).toHaveLength(1);
|
|
369
374
|
});
|
|
370
375
|
|
|
376
|
+
it('shades a pill and rules a line above a paragraph', () => {
|
|
377
|
+
const doc = buildDocxParts({
|
|
378
|
+
...MODEL,
|
|
379
|
+
blocks: [
|
|
380
|
+
{
|
|
381
|
+
type: 'paragraph',
|
|
382
|
+
ruleAbove: { color: 'e2e8f0', size: 6, gap: 210 },
|
|
383
|
+
inlines: [{ type: 'text', text: 'OPEN-DOC', shade: 'e9effd' }],
|
|
384
|
+
},
|
|
385
|
+
],
|
|
386
|
+
})['word/document.xml'] as string;
|
|
387
|
+
expect(doc).toContain(
|
|
388
|
+
'<w:pBdr><w:top w:val="single" w:sz="6" w:space="11" w:color="e2e8f0"/></w:pBdr>',
|
|
389
|
+
);
|
|
390
|
+
expect(doc).toContain(
|
|
391
|
+
'<w:rPr><w:shd w:val="clear" w:color="auto" w:fill="e9effd"/></w:rPr><w:t xml:space="preserve">OPEN-DOC</w:t>',
|
|
392
|
+
);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('gives a sheet’s own foot a footer of its section, and the rest an empty one', () => {
|
|
396
|
+
const parts = buildDocxParts({
|
|
397
|
+
...MODEL,
|
|
398
|
+
footer: undefined,
|
|
399
|
+
blocks: [
|
|
400
|
+
{
|
|
401
|
+
type: 'paragraph',
|
|
402
|
+
inlines: [{ type: 'text', text: 'Cover' }],
|
|
403
|
+
sectionEnd: {
|
|
404
|
+
running: false,
|
|
405
|
+
foot: { type: 'paragraph', inlines: [{ type: 'text', text: 'Byline' }] },
|
|
406
|
+
},
|
|
407
|
+
},
|
|
408
|
+
{ type: 'paragraph', inlines: [{ type: 'text', text: 'Body' }] },
|
|
409
|
+
],
|
|
410
|
+
});
|
|
411
|
+
const doc = parts['word/document.xml'] as string;
|
|
412
|
+
const rels = parts['word/_rels/document.xml.rels'] as string;
|
|
413
|
+
const id = (target: string) =>
|
|
414
|
+
rels.match(new RegExp(`Id="(rId\\d+)"[^>]*Target="${target}"`))?.[1];
|
|
415
|
+
expect(parts['word/footer3.xml']).toContain('Byline');
|
|
416
|
+
// Set in the body's style, and placed a page margin above the sheet's edge.
|
|
417
|
+
expect(parts['word/footer3.xml']).not.toContain('w:val="Footer"');
|
|
418
|
+
expect(doc).toMatch(
|
|
419
|
+
new RegExp(
|
|
420
|
+
`<w:footerReference w:type="default" r:id="${id('footer3.xml')}"/>.*w:footer="1140".*Cover`,
|
|
421
|
+
),
|
|
422
|
+
);
|
|
423
|
+
// Without a footer of its own, the next section would inherit the cover's.
|
|
424
|
+
expect(doc).toMatch(
|
|
425
|
+
new RegExp(`Body.*<w:footerReference w:type="default" r:id="${id('footer2.xml')}"/>`),
|
|
426
|
+
);
|
|
427
|
+
});
|
|
428
|
+
|
|
371
429
|
it('stamps the watermark behind the text from every header', () => {
|
|
372
430
|
const parts = buildDocxParts({
|
|
373
431
|
...MODEL,
|
|
@@ -27,6 +27,8 @@ export type TextRun = {
|
|
|
27
27
|
/** Font size in half-points, only when it differs from the paragraph style's. */
|
|
28
28
|
halfPoints?: number;
|
|
29
29
|
vert?: 'superscript' | 'subscript';
|
|
30
|
+
/** Background behind the characters, `rrggbb` — an inline pill or badge. */
|
|
31
|
+
shade?: string;
|
|
30
32
|
};
|
|
31
33
|
|
|
32
34
|
export type Run =
|
|
@@ -78,6 +80,8 @@ export type Paragraph = {
|
|
|
78
80
|
hanging?: number;
|
|
79
81
|
/** Background behind the paragraph, `rrggbb` — a highlighted line of code. */
|
|
80
82
|
shade?: string;
|
|
83
|
+
/** A rule above the paragraph and the gap between it and the text, twips. */
|
|
84
|
+
ruleAbove?: { color: string; size: number; gap: number };
|
|
81
85
|
/** An exact line height, twips — a spacer that holds open a gap. */
|
|
82
86
|
lineExact?: number;
|
|
83
87
|
/**
|
|
@@ -105,6 +109,12 @@ export type SectionLayout = {
|
|
|
105
109
|
* columns of a section only where a continuous break ends it.
|
|
106
110
|
*/
|
|
107
111
|
continuous?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* A line the sheet laid out against its own foot — a cover's byline. It is
|
|
114
|
+
* this section's footer, so it sits at the foot of the page whatever came
|
|
115
|
+
* out above it.
|
|
116
|
+
*/
|
|
117
|
+
foot?: Paragraph;
|
|
108
118
|
};
|
|
109
119
|
|
|
110
120
|
export type CellBorder = { color: string /** Eighths of a point. */; size: number };
|
|
@@ -187,6 +197,7 @@ export type DocxModel = {
|
|
|
187
197
|
/** The last section's columns and start, as `SectionLayout`. */
|
|
188
198
|
finalColumns?: SectionLayout['columns'];
|
|
189
199
|
finalContinuous?: boolean;
|
|
200
|
+
finalFoot?: Paragraph;
|
|
190
201
|
/** A picture behind the text of every page, the size of the sheet — `meta.watermark`. */
|
|
191
202
|
watermark?: string;
|
|
192
203
|
images: ImagePart[];
|
|
@@ -253,6 +264,13 @@ const GENERIC = new Set([
|
|
|
253
264
|
'inherit',
|
|
254
265
|
]);
|
|
255
266
|
|
|
267
|
+
/**
|
|
268
|
+
* Apple's system faces: a page draws in them, but no app outside the system can
|
|
269
|
+
* pick one by name, so Pages and Word would fall back to Courier or Times. The
|
|
270
|
+
* next family in the stack — Menlo, Helvetica — is the one that will be there.
|
|
271
|
+
*/
|
|
272
|
+
const SYSTEM_ONLY = /^(\.?sf ?(mono|pro|compact)|sfmono-|sfns)/i;
|
|
273
|
+
|
|
256
274
|
const CJK_FONT =
|
|
257
275
|
/noto (sans|serif) (tc|sc|jp|kr|hk|cjk)|pingfang|hiragino|jhenghei|yahei|正黑|明體|楷|source han|思源|yu gothic|yu mincho|meiryo|malgun|ms mincho|ms gothic|heiti|songti|kaiti|biaukai|dfkai|simsun|simhei|mingliu|gulim|batang|lxgw|霞鶩/i;
|
|
258
276
|
|
|
@@ -268,7 +286,9 @@ export function fontsFor(stack: string, fallback: 'sans' | 'serif' | 'mono'): Fo
|
|
|
268
286
|
.split(',')
|
|
269
287
|
.map((family) => family.trim().replace(/^['"]|['"]$/g, ''))
|
|
270
288
|
.filter((family) => family !== '');
|
|
271
|
-
const real = families.filter(
|
|
289
|
+
const real = families.filter(
|
|
290
|
+
(family) => !GENERIC.has(family.toLowerCase()) && !SYSTEM_ONLY.test(family),
|
|
291
|
+
);
|
|
272
292
|
const generic =
|
|
273
293
|
families.find((family) => ['serif', 'monospace', 'ui-monospace'].includes(family)) ?? '';
|
|
274
294
|
const kind = generic === 'serif' ? 'serif' : generic.includes('monospace') ? 'mono' : fallback;
|
|
@@ -388,6 +408,7 @@ function runPropsXml(run: TextRun, ctx: WriteContext): string {
|
|
|
388
408
|
if (run.halfPoints)
|
|
389
409
|
parts.push(`<w:sz w:val="${run.halfPoints}"/><w:szCs w:val="${run.halfPoints}"/>`);
|
|
390
410
|
if (run.underline) parts.push('<w:u w:val="single"/>');
|
|
411
|
+
if (run.shade) parts.push(`<w:shd w:val="clear" w:color="auto" w:fill="${run.shade}"/>`);
|
|
391
412
|
if (run.vert) parts.push(`<w:vertAlign w:val="${run.vert}"/>`);
|
|
392
413
|
return parts.length > 0 ? `<w:rPr>${parts.join('')}</w:rPr>` : '';
|
|
393
414
|
}
|
|
@@ -491,6 +512,14 @@ export function paragraphXml(paragraph: Paragraph, ctx: WriteContext): string {
|
|
|
491
512
|
`<w:numPr><w:ilvl w:val="${paragraph.list.level}"/><w:numId w:val="${paragraph.list.numId}"/></w:numPr>`,
|
|
492
513
|
);
|
|
493
514
|
}
|
|
515
|
+
if (paragraph.ruleAbove) {
|
|
516
|
+
const { color, size, gap } = paragraph.ruleAbove;
|
|
517
|
+
// A border's space is in points, and Word allows no more than 31.
|
|
518
|
+
const space = Math.min(31, Math.round(gap / 20));
|
|
519
|
+
props.push(
|
|
520
|
+
`<w:pBdr><w:top w:val="single" w:sz="${size}" w:space="${space}" w:color="${color}"/></w:pBdr>`,
|
|
521
|
+
);
|
|
522
|
+
}
|
|
494
523
|
if (paragraph.shade)
|
|
495
524
|
props.push(`<w:shd w:val="clear" w:color="auto" w:fill="${paragraph.shade}"/>`);
|
|
496
525
|
if (paragraph.rightTab !== undefined) {
|
|
@@ -732,13 +761,19 @@ export function buildDocxParts(model: DocxModel): Record<string, string | Uint8A
|
|
|
732
761
|
const bare =
|
|
733
762
|
model.finalRunning === false ||
|
|
734
763
|
model.blocks.some((block) => block.type === 'paragraph' && block.sectionEnd?.running === false);
|
|
764
|
+
// A section with a foot of its own changes the footer, so every other
|
|
765
|
+
// section needs a footer to point at, if only an empty one.
|
|
766
|
+
const feet =
|
|
767
|
+
Boolean(model.finalFoot) ||
|
|
768
|
+
model.blocks.some((block) => block.type === 'paragraph' && block.sectionEnd?.foot);
|
|
735
769
|
const running = (kind: 'header' | 'footer') => {
|
|
736
770
|
// The watermark is drawn from the header, so a document with one needs a
|
|
737
771
|
// header even when it has no running line to put there.
|
|
738
|
-
|
|
772
|
+
const full = Boolean(model[kind]) || (kind === 'header' && Boolean(model.watermark));
|
|
773
|
+
const empty = full ? bare : kind === 'footer' && feet;
|
|
739
774
|
return {
|
|
740
|
-
full: relFor(docCtx, `${REL_BASE}/${kind}`, `${kind}1.xml`),
|
|
741
|
-
bare:
|
|
775
|
+
full: full ? relFor(docCtx, `${REL_BASE}/${kind}`, `${kind}1.xml`) : null,
|
|
776
|
+
bare: empty ? relFor(docCtx, `${REL_BASE}/${kind}`, `${kind}2.xml`) : null,
|
|
742
777
|
};
|
|
743
778
|
};
|
|
744
779
|
const headerRels = running('header');
|
|
@@ -746,9 +781,18 @@ export function buildDocxParts(model: DocxModel): Record<string, string | Uint8A
|
|
|
746
781
|
|
|
747
782
|
const { page } = model;
|
|
748
783
|
const margin = Math.round(page.margin);
|
|
749
|
-
|
|
784
|
+
const footParts: Array<{ name: string; paragraph: Paragraph }> = [];
|
|
785
|
+
docCtx.section = ({ running: on, columns, continuous, foot }) => {
|
|
750
786
|
const header = on ? headerRels.full : headerRels.bare;
|
|
751
|
-
|
|
787
|
+
let footer = on ? (footerRels.full ?? footerRels.bare) : footerRels.bare;
|
|
788
|
+
if (foot) {
|
|
789
|
+
const name = `footer${footParts.length + 3}`;
|
|
790
|
+
footParts.push({ name, paragraph: foot });
|
|
791
|
+
footer = relFor(docCtx, `${REL_BASE}/footer`, `${name}.xml`);
|
|
792
|
+
}
|
|
793
|
+
// The foot sat a page margin above the sheet's edge; a footer is placed by
|
|
794
|
+
// its distance from that edge.
|
|
795
|
+
const footerAt = foot ? margin : Math.round(margin / 2);
|
|
752
796
|
const cols =
|
|
753
797
|
columns && columns.count > 1
|
|
754
798
|
? `<w:cols w:num="${columns.count}" w:space="${Math.round(columns.gap)}"/>`
|
|
@@ -759,7 +803,7 @@ export function buildDocxParts(model: DocxModel): Record<string, string | Uint8A
|
|
|
759
803
|
(footer ? `<w:footerReference w:type="default" r:id="${footer}"/>` : '') +
|
|
760
804
|
(continuous ? '<w:type w:val="continuous"/>' : '') +
|
|
761
805
|
`<w:pgSz w:w="${Math.round(page.width)}" w:h="${Math.round(page.height)}"${page.landscape ? ' w:orient="landscape"' : ''}/>` +
|
|
762
|
-
`<w:pgMar w:top="${margin}" w:right="${margin}" w:bottom="${margin}" w:left="${margin}" w:header="${Math.round(margin / 2)}" w:footer="${
|
|
806
|
+
`<w:pgMar w:top="${margin}" w:right="${margin}" w:bottom="${margin}" w:left="${margin}" w:header="${Math.round(margin / 2)}" w:footer="${footerAt}" w:gutter="0"/>` +
|
|
763
807
|
cols +
|
|
764
808
|
'</w:sectPr>'
|
|
765
809
|
);
|
|
@@ -769,6 +813,7 @@ export function buildDocxParts(model: DocxModel): Record<string, string | Uint8A
|
|
|
769
813
|
running: model.finalRunning !== false,
|
|
770
814
|
...(model.finalColumns ? { columns: model.finalColumns } : {}),
|
|
771
815
|
...(model.finalContinuous ? { continuous: true } : {}),
|
|
816
|
+
...(model.finalFoot ? { foot: model.finalFoot } : {}),
|
|
772
817
|
});
|
|
773
818
|
const documentXml = `${XML_HEAD}<w:document ${NS}><w:body>${body}${sectPr}</w:body></w:document>`;
|
|
774
819
|
|
|
@@ -806,7 +851,6 @@ export function buildDocxParts(model: DocxModel): Record<string, string | Uint8A
|
|
|
806
851
|
['header', headerRels, 'hdr', 'Header'],
|
|
807
852
|
['footer', footerRels, 'ftr', 'Footer'],
|
|
808
853
|
] as const) {
|
|
809
|
-
if (!rels.full) continue;
|
|
810
854
|
const line: Paragraph = model[kind] ?? { type: 'paragraph', inlines: [] };
|
|
811
855
|
const stamp = kind === 'header' ? model.watermark : undefined;
|
|
812
856
|
const part = (name: string, paragraph: Paragraph) => {
|
|
@@ -821,8 +865,13 @@ export function buildDocxParts(model: DocxModel): Record<string, string | Uint8A
|
|
|
821
865
|
if (ctx.rels.length > 0) runningParts[`word/_rels/${name}.xml.rels`] = relsXml(ctx.rels);
|
|
822
866
|
runningTypes.push(`/word/${name}.xml|${kind}`);
|
|
823
867
|
};
|
|
824
|
-
part(`${kind}1`, line);
|
|
868
|
+
if (rels.full) part(`${kind}1`, line);
|
|
825
869
|
if (rels.bare) part(`${kind}2`, { type: 'paragraph', inlines: [] });
|
|
870
|
+
// A sheet's own foot is set in the body's style: its runs say only how
|
|
871
|
+
// they differ from that, not from the running footer's.
|
|
872
|
+
if (kind === 'footer') {
|
|
873
|
+
for (const foot of footParts) part(foot.name, { ...foot.paragraph, style: 'Normal' });
|
|
874
|
+
}
|
|
826
875
|
}
|
|
827
876
|
|
|
828
877
|
const settingsXml =
|