@evotecit/officeimo 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +1222 -2
- package/bundles/officeimo-canopyx.js +11741 -0
- package/bundles/officeimo-canopyx.mjs +11639 -0
- package/bundles/officeimo-csv.js +1264 -0
- package/bundles/officeimo-csv.mjs +1239 -0
- package/bundles/officeimo-datatables.js +12018 -0
- package/bundles/officeimo-datatables.mjs +11915 -0
- package/bundles/officeimo-execution.js +1513 -0
- package/bundles/officeimo-execution.mjs +1489 -0
- package/bundles/officeimo-pdf.js +7009 -0
- package/bundles/officeimo-pdf.mjs +6946 -0
- package/bundles/officeimo-xlsx.js +5344 -0
- package/bundles/officeimo-xlsx.mjs +5290 -0
- package/bundles/officeimo.js +11468 -0
- package/bundles/officeimo.mjs +11369 -0
- package/dist/core/dates.d.ts +1 -0
- package/dist/core/dates.js +16 -0
- package/dist/core/errors.d.ts +12 -0
- package/dist/core/errors.js +31 -0
- package/dist/core/grouped-report.d.ts +87 -0
- package/dist/core/grouped-report.js +252 -0
- package/dist/core/index.d.ts +74 -0
- package/dist/core/index.js +37 -0
- package/dist/core/iteration.d.ts +7 -0
- package/dist/core/iteration.js +149 -0
- package/dist/core/limits.d.ts +7 -0
- package/dist/core/limits.js +56 -0
- package/dist/core/links.d.ts +5 -0
- package/dist/core/links.js +14 -0
- package/dist/core/presentation.d.ts +29 -0
- package/dist/core/presentation.js +59 -0
- package/dist/core/progress.d.ts +1 -0
- package/dist/core/progress.js +10 -0
- package/dist/core/sinks.d.ts +33 -0
- package/dist/core/sinks.js +102 -0
- package/dist/core/table.d.ts +8 -0
- package/dist/core/table.js +1 -0
- package/dist/core/value-text.d.ts +7 -0
- package/dist/core/value-text.js +13 -0
- package/dist/csv/index.d.ts +44 -0
- package/dist/csv/index.js +133 -0
- package/dist/csv/options.d.ts +1 -0
- package/dist/csv/options.js +8 -0
- package/dist/csv/structured-rows.d.ts +3 -0
- package/dist/csv/structured-rows.js +15 -0
- package/dist/execution/host.d.ts +5 -0
- package/dist/execution/host.js +164 -0
- package/dist/execution/index.d.ts +3 -0
- package/dist/execution/index.js +2 -0
- package/dist/execution/protocol.d.ts +48 -0
- package/dist/execution/protocol.js +173 -0
- package/dist/execution/types.d.ts +42 -0
- package/dist/execution/types.js +1 -0
- package/dist/execution/worker.d.ts +3 -0
- package/dist/execution/worker.js +147 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +14 -0
- package/dist/integrations/canopyx/dates.d.ts +1 -0
- package/dist/integrations/canopyx/dates.js +34 -0
- package/dist/integrations/canopyx/index.d.ts +10 -0
- package/dist/integrations/canopyx/index.js +45 -0
- package/dist/integrations/canopyx/source.d.ts +3 -0
- package/dist/integrations/canopyx/source.js +122 -0
- package/dist/integrations/canopyx/types.d.ts +83 -0
- package/dist/integrations/canopyx/types.js +1 -0
- package/dist/integrations/datatables/api.d.ts +1 -0
- package/dist/integrations/datatables/api.js +27 -0
- package/dist/integrations/datatables/headings.d.ts +1 -0
- package/dist/integrations/datatables/headings.js +65 -0
- package/dist/integrations/datatables/index.d.ts +12 -0
- package/dist/integrations/datatables/index.js +167 -0
- package/dist/integrations/datatables/snapshot.d.ts +3 -0
- package/dist/integrations/datatables/snapshot.js +206 -0
- package/dist/integrations/datatables/types.d.ts +98 -0
- package/dist/integrations/datatables/types.js +1 -0
- package/dist/integrations/options.d.ts +13 -0
- package/dist/integrations/options.js +34 -0
- package/dist/internal/capture.d.ts +4 -0
- package/dist/internal/capture.js +14 -0
- package/dist/internal/columns.d.ts +27 -0
- package/dist/internal/columns.js +1 -0
- package/dist/internal/crc32.d.ts +6 -0
- package/dist/internal/crc32.js +17 -0
- package/dist/internal/export-values.d.ts +3 -0
- package/dist/internal/export-values.js +14 -0
- package/dist/internal/rows.d.ts +6 -0
- package/dist/internal/rows.js +77 -0
- package/dist/internal/table-spans.d.ts +11 -0
- package/dist/internal/table-spans.js +58 -0
- package/dist/internal/totals.d.ts +15 -0
- package/dist/internal/totals.js +42 -0
- package/dist/opc/index.d.ts +67 -0
- package/dist/opc/index.js +233 -0
- package/dist/opc/properties.d.ts +15 -0
- package/dist/opc/properties.js +18 -0
- package/dist/opc/uri.d.ts +5 -0
- package/dist/opc/uri.js +41 -0
- package/dist/pdf/annotations.d.ts +1 -0
- package/dist/pdf/annotations.js +38 -0
- package/dist/pdf/binary.d.ts +6 -0
- package/dist/pdf/binary.js +24 -0
- package/dist/pdf/document-drawings.d.ts +15 -0
- package/dist/pdf/document-drawings.js +237 -0
- package/dist/pdf/document-grouped.d.ts +1 -0
- package/dist/pdf/document-grouped.js +244 -0
- package/dist/pdf/document-images.d.ts +19 -0
- package/dist/pdf/document-images.js +125 -0
- package/dist/pdf/document-types.d.ts +160 -0
- package/dist/pdf/document-types.js +1 -0
- package/dist/pdf/document-vectors.d.ts +11 -0
- package/dist/pdf/document-vectors.js +77 -0
- package/dist/pdf/document.d.ts +9 -0
- package/dist/pdf/document.js +270 -0
- package/dist/pdf/drawing-types.d.ts +53 -0
- package/dist/pdf/drawing-types.js +1 -0
- package/dist/pdf/font-resources.d.ts +43 -0
- package/dist/pdf/font-resources.js +202 -0
- package/dist/pdf/font-support.d.ts +1 -0
- package/dist/pdf/font-support.js +60 -0
- package/dist/pdf/font.d.ts +17 -0
- package/dist/pdf/font.js +56 -0
- package/dist/pdf/grouped-table.d.ts +6 -0
- package/dist/pdf/grouped-table.js +40 -0
- package/dist/pdf/grouped-types.d.ts +34 -0
- package/dist/pdf/grouped-types.js +1 -0
- package/dist/pdf/grouped.d.ts +10 -0
- package/dist/pdf/grouped.js +37 -0
- package/dist/pdf/index.d.ts +22 -0
- package/dist/pdf/index.js +8 -0
- package/dist/pdf/inline-text.d.ts +30 -0
- package/dist/pdf/inline-text.js +183 -0
- package/dist/pdf/inline-types.d.ts +15 -0
- package/dist/pdf/inline-types.js +1 -0
- package/dist/pdf/list-layout.d.ts +6 -0
- package/dist/pdf/list-layout.js +49 -0
- package/dist/pdf/objects.d.ts +2 -0
- package/dist/pdf/objects.js +320 -0
- package/dist/pdf/outlines.d.ts +1 -0
- package/dist/pdf/outlines.js +55 -0
- package/dist/pdf/page-context.d.ts +1 -0
- package/dist/pdf/page-context.js +12 -0
- package/dist/pdf/pages.d.ts +1 -0
- package/dist/pdf/pages.js +678 -0
- package/dist/pdf/paragraph-layout.d.ts +21 -0
- package/dist/pdf/paragraph-layout.js +71 -0
- package/dist/pdf/paragraph-styles.d.ts +1 -0
- package/dist/pdf/paragraph-styles.js +66 -0
- package/dist/pdf/png.d.ts +9 -0
- package/dist/pdf/png.js +259 -0
- package/dist/pdf/preflight.d.ts +7 -0
- package/dist/pdf/preflight.js +32 -0
- package/dist/pdf/presentation-settings.d.ts +5 -0
- package/dist/pdf/presentation-settings.js +28 -0
- package/dist/pdf/providers/harfbuzz.d.ts +60 -0
- package/dist/pdf/providers/harfbuzz.js +112 -0
- package/dist/pdf/providers/unicode13-profile.d.ts +2 -0
- package/dist/pdf/providers/unicode13-profile.js +115 -0
- package/dist/pdf/section-types.d.ts +15 -0
- package/dist/pdf/section-types.js +40 -0
- package/dist/pdf/session.d.ts +25 -0
- package/dist/pdf/session.js +55 -0
- package/dist/pdf/settings.d.ts +17 -0
- package/dist/pdf/settings.js +196 -0
- package/dist/pdf/shaped-text.d.ts +5 -0
- package/dist/pdf/shaped-text.js +205 -0
- package/dist/pdf/standard-widths.d.ts +1 -0
- package/dist/pdf/standard-widths.js +317 -0
- package/dist/pdf/structure-parent-tree.d.ts +1 -0
- package/dist/pdf/structure-parent-tree.js +57 -0
- package/dist/pdf/structure.d.ts +6 -0
- package/dist/pdf/structure.js +302 -0
- package/dist/pdf/table-band-flow.d.ts +34 -0
- package/dist/pdf/table-band-flow.js +129 -0
- package/dist/pdf/table-band-plan.d.ts +10 -0
- package/dist/pdf/table-band-plan.js +83 -0
- package/dist/pdf/table-layout.d.ts +75 -0
- package/dist/pdf/table-layout.js +217 -0
- package/dist/pdf/table-structure.d.ts +1 -0
- package/dist/pdf/table-structure.js +108 -0
- package/dist/pdf/text-shaping-types.d.ts +39 -0
- package/dist/pdf/text-shaping-types.js +1 -0
- package/dist/pdf/text.d.ts +13 -0
- package/dist/pdf/text.js +101 -0
- package/dist/pdf/truetype-subset.d.ts +5 -0
- package/dist/pdf/truetype-subset.js +217 -0
- package/dist/pdf/truetype.d.ts +1 -0
- package/dist/pdf/truetype.js +235 -0
- package/dist/pdf/types.d.ts +171 -0
- package/dist/pdf/types.js +1 -0
- package/dist/pdf/write.d.ts +6 -0
- package/dist/pdf/write.js +57 -0
- package/dist/pdf/xref.d.ts +1 -0
- package/dist/pdf/xref.js +30 -0
- package/dist/xlsx/attachments.d.ts +30 -0
- package/dist/xlsx/attachments.js +67 -0
- package/dist/xlsx/chart-bindings.d.ts +1 -0
- package/dist/xlsx/chart-bindings.js +122 -0
- package/dist/xlsx/chart-types.d.ts +73 -0
- package/dist/xlsx/chart-types.js +1 -0
- package/dist/xlsx/chart-xml.d.ts +2 -0
- package/dist/xlsx/chart-xml.js +103 -0
- package/dist/xlsx/charts.d.ts +1 -0
- package/dist/xlsx/charts.js +159 -0
- package/dist/xlsx/conditional-formatting.d.ts +1 -0
- package/dist/xlsx/conditional-formatting.js +158 -0
- package/dist/xlsx/conditional-types.d.ts +64 -0
- package/dist/xlsx/conditional-types.js +1 -0
- package/dist/xlsx/data-row-ranges.d.ts +5 -0
- package/dist/xlsx/data-row-ranges.js +39 -0
- package/dist/xlsx/differential-styles.d.ts +1 -0
- package/dist/xlsx/differential-styles.js +102 -0
- package/dist/xlsx/formula.d.ts +8 -0
- package/dist/xlsx/formula.js +104 -0
- package/dist/xlsx/grouped-layout.d.ts +61 -0
- package/dist/xlsx/grouped-layout.js +79 -0
- package/dist/xlsx/grouped.d.ts +16 -0
- package/dist/xlsx/grouped.js +161 -0
- package/dist/xlsx/index.d.ts +24 -0
- package/dist/xlsx/index.js +10 -0
- package/dist/xlsx/layout.d.ts +1 -0
- package/dist/xlsx/layout.js +186 -0
- package/dist/xlsx/options.d.ts +1 -0
- package/dist/xlsx/options.js +16 -0
- package/dist/xlsx/portable.d.ts +27 -0
- package/dist/xlsx/portable.js +44 -0
- package/dist/xlsx/preservation.d.ts +1 -0
- package/dist/xlsx/preservation.js +60 -0
- package/dist/xlsx/ranges.d.ts +1 -0
- package/dist/xlsx/ranges.js +24 -0
- package/dist/xlsx/regions.d.ts +1 -0
- package/dist/xlsx/regions.js +97 -0
- package/dist/xlsx/structured-rows.d.ts +11 -0
- package/dist/xlsx/structured-rows.js +28 -0
- package/dist/xlsx/style-xml.d.ts +1 -0
- package/dist/xlsx/style-xml.js +6 -0
- package/dist/xlsx/styles.d.ts +70 -0
- package/dist/xlsx/styles.js +203 -0
- package/dist/xlsx/table.d.ts +12 -0
- package/dist/xlsx/table.js +35 -0
- package/dist/xlsx/types.d.ts +180 -0
- package/dist/xlsx/types.js +1 -0
- package/dist/xlsx/validation-types.d.ts +18 -0
- package/dist/xlsx/validation-types.js +1 -0
- package/dist/xlsx/validation.d.ts +1 -0
- package/dist/xlsx/validation.js +92 -0
- package/dist/xlsx/values.d.ts +13 -0
- package/dist/xlsx/values.js +87 -0
- package/dist/xlsx/workbook.d.ts +50 -0
- package/dist/xlsx/workbook.js +452 -0
- package/dist/xlsx/worksheet.d.ts +71 -0
- package/dist/xlsx/worksheet.js +700 -0
- package/dist/xlsx/write.d.ts +15 -0
- package/dist/xlsx/write.js +47 -0
- package/dist/xml/index.d.ts +24 -0
- package/dist/xml/index.js +74 -0
- package/dist/zip/entry.d.ts +39 -0
- package/dist/zip/entry.js +122 -0
- package/dist/zip/index.d.ts +44 -0
- package/dist/zip/index.js +248 -0
- package/package.json +104 -4
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
import { OfficeIMOError } from "../core/errors.js";
|
|
2
|
+
import { checkAbort } from "../core/iteration.js";
|
|
3
|
+
import { pdfNumber } from "./objects.js";
|
|
4
|
+
import { color } from "./settings.js";
|
|
5
|
+
import { PdfTableBandFlow } from "./table-band-flow.js";
|
|
6
|
+
import { PdfTableStructure } from "./table-structure.js";
|
|
7
|
+
import { wrapText, wrapTextCooperatively, synchronousText, pdfBudgetText } from "./text.js";
|
|
8
|
+
import { linkAnnotation } from "./annotations.js";
|
|
9
|
+
import { unicodeHex } from "./objects.js";
|
|
10
|
+
import { captureSection, captureBookmark } from "./section-types.js";
|
|
11
|
+
import { sectionSettings } from "./page-context.js";
|
|
12
|
+
/** @internal One page of drawing commands and O(page count) references, never the complete report body. */
|
|
13
|
+
export class PdfPages {
|
|
14
|
+
objects;
|
|
15
|
+
baseSettings;
|
|
16
|
+
fonts;
|
|
17
|
+
parent;
|
|
18
|
+
resources;
|
|
19
|
+
layout;
|
|
20
|
+
structure;
|
|
21
|
+
outlines;
|
|
22
|
+
references = [];
|
|
23
|
+
current = 0;
|
|
24
|
+
commands = [];
|
|
25
|
+
commandBytes = 0;
|
|
26
|
+
annotations = [];
|
|
27
|
+
hyperlinks = 0;
|
|
28
|
+
y = 0;
|
|
29
|
+
dataTop = 0;
|
|
30
|
+
continuationTop = 0;
|
|
31
|
+
totalPages;
|
|
32
|
+
numberSettings;
|
|
33
|
+
activeSettings;
|
|
34
|
+
pendingPage;
|
|
35
|
+
bodyAdded = false;
|
|
36
|
+
sectionCount = 0;
|
|
37
|
+
sectionPages = 0;
|
|
38
|
+
sectionTitle;
|
|
39
|
+
pendingBookmarks = [];
|
|
40
|
+
pageNumberWidth = 0;
|
|
41
|
+
colors = new Map();
|
|
42
|
+
externalObjects = new Map();
|
|
43
|
+
bandFlow;
|
|
44
|
+
bandCellBudget = false;
|
|
45
|
+
tableElement;
|
|
46
|
+
tableStructure;
|
|
47
|
+
activeHeadings;
|
|
48
|
+
paintedHeaderCells = new WeakSet();
|
|
49
|
+
paintedHeaderBudgetCells = new WeakSet();
|
|
50
|
+
summaryRows = new WeakSet();
|
|
51
|
+
groups = [];
|
|
52
|
+
continuationCaption;
|
|
53
|
+
tableCaptionOwner;
|
|
54
|
+
continuationCaptionPaints = 0;
|
|
55
|
+
constructor(objects, baseSettings, fonts, parent, resources, layout, structure, outlines) {
|
|
56
|
+
this.objects = objects;
|
|
57
|
+
this.baseSettings = baseSettings;
|
|
58
|
+
this.fonts = fonts;
|
|
59
|
+
this.parent = parent;
|
|
60
|
+
this.resources = resources;
|
|
61
|
+
this.layout = layout;
|
|
62
|
+
this.structure = structure;
|
|
63
|
+
this.outlines = outlines;
|
|
64
|
+
this.activeSettings = baseSettings;
|
|
65
|
+
}
|
|
66
|
+
measurePageNumbers() {
|
|
67
|
+
const settings = this.settings, font = this.fonts.select(), max = settings.limits.maxPages;
|
|
68
|
+
const digitWidth = Math.max(...Array.from({ length: Math.min(10, max) }, (_, i) => font.width(max < 10 ? 49 + i : 48 + i)));
|
|
69
|
+
const countWidth = digitWidth * String(max).length * settings.fontSize / 1000;
|
|
70
|
+
const labels = settings.options.pageNumberLabels;
|
|
71
|
+
const literals = wrapText((labels?.prefix ?? "Page ") + (labels?.separator ?? " of ") + (labels?.suffix ?? ""), font, settings.fontSize, Number.MAX_SAFE_INTEGER, settings.limits.maxCellCharacters, 1, false)[0].width;
|
|
72
|
+
return literals + countWidth * 2 + 2;
|
|
73
|
+
}
|
|
74
|
+
get settings() { return this.activeSettings; }
|
|
75
|
+
async ensureStarted() { if (!this.current)
|
|
76
|
+
await this.start(false); }
|
|
77
|
+
/** Section changes consume no rows and do not manufacture an empty transition page. */
|
|
78
|
+
async beginSection(block) {
|
|
79
|
+
const captured = captureSection(block, this.baseSettings), active = sectionSettings(this.baseSettings, captured);
|
|
80
|
+
if (++this.sectionCount > this.baseSettings.limits.maxSections)
|
|
81
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxSections exceeded.");
|
|
82
|
+
if (this.current) {
|
|
83
|
+
if (this.bodyAdded)
|
|
84
|
+
await this.finishPage();
|
|
85
|
+
else {
|
|
86
|
+
this.pendingPage = this.current;
|
|
87
|
+
this.current = 0;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
this.activeSettings = active;
|
|
91
|
+
this.sectionPages = 0;
|
|
92
|
+
this.sectionTitle = captured.title;
|
|
93
|
+
await this.structure?.beginSection();
|
|
94
|
+
return captured;
|
|
95
|
+
}
|
|
96
|
+
addBookmark(bookmark) {
|
|
97
|
+
const captured = captureBookmark(bookmark, this.settings);
|
|
98
|
+
if ((this.outlines?.count ?? 0) + this.pendingBookmarks.length >= this.settings.limits.maxBookmarks)
|
|
99
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxBookmarks exceeded.");
|
|
100
|
+
this.settings.budget.cell(captured.title);
|
|
101
|
+
if (this.current)
|
|
102
|
+
this.outlines?.add(captured, this.current, this.y);
|
|
103
|
+
else
|
|
104
|
+
this.pendingBookmarks.push(captured);
|
|
105
|
+
}
|
|
106
|
+
add(command) {
|
|
107
|
+
this.retainPageBytes(command.length);
|
|
108
|
+
this.commands.push(command);
|
|
109
|
+
}
|
|
110
|
+
retainPageBytes(length) {
|
|
111
|
+
this.commandBytes += length;
|
|
112
|
+
if (this.commandBytes > this.settings.limits.maxPageBytes)
|
|
113
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxPageBytes exceeded.");
|
|
114
|
+
}
|
|
115
|
+
rgb(hex) {
|
|
116
|
+
let rgb = this.colors.get(hex);
|
|
117
|
+
if (!rgb) {
|
|
118
|
+
rgb = color(hex);
|
|
119
|
+
if (this.colors.size < 1024)
|
|
120
|
+
this.colors.set(hex, rgb);
|
|
121
|
+
}
|
|
122
|
+
return rgb;
|
|
123
|
+
}
|
|
124
|
+
artifact(command, type = "Layout") {
|
|
125
|
+
return this.structure ? "/Artifact << /Type /" + type + " >> BDC\n" + command + "EMC\n" : command;
|
|
126
|
+
}
|
|
127
|
+
text(text, font, size, x, baseline, foreground = "111827", element) {
|
|
128
|
+
const n = pdfNumber, shear = font.syntheticItalic ? .213 : 0;
|
|
129
|
+
const command = "q " + this.rgb(foreground) + " rg " + this.rgb(foreground) + " RG\nBT /" + font.name + " " + n(size) + " Tf " +
|
|
130
|
+
(font.syntheticBold ? n(size * .025) + " w 2 Tr " : "0 Tr ") + "1 0 " + n(shear) + " 1 " + n(x) + " " + n(baseline) + " Tm " + font.encode(text) + " Tj ET Q\n";
|
|
131
|
+
this.add(this.structure && element !== undefined ? this.structure.mark(element) + command + "EMC\n" : this.artifact(command, "Pagination"));
|
|
132
|
+
}
|
|
133
|
+
link(link, x, bottom, width, height, element) {
|
|
134
|
+
if (width <= 0)
|
|
135
|
+
return;
|
|
136
|
+
if (++this.hyperlinks > this.settings.limits.maxHyperlinks)
|
|
137
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxHyperlinks exceeded.");
|
|
138
|
+
const id = this.objects.reserve();
|
|
139
|
+
const parentKey = this.structure && element !== undefined ? this.structure.annotation(element, id) : undefined;
|
|
140
|
+
const annotation = linkAnnotation(link, x, bottom, width, height, this.settings.limits.maxPageBytes - this.commandBytes, parentKey, this.structure ? this.current : undefined);
|
|
141
|
+
this.retainPageBytes(annotation.length);
|
|
142
|
+
this.annotations.push({ id, body: annotation });
|
|
143
|
+
}
|
|
144
|
+
inline(line, size, x, baseline, element, annotationParent = element) {
|
|
145
|
+
const lineActualText = !this.structure && line.actualText !== undefined && line.actualText.length > 0;
|
|
146
|
+
if (lineActualText)
|
|
147
|
+
this.add("/Span << /ActualText <FEFF" + unicodeHex(line.actualText) + "> >> BDC\n");
|
|
148
|
+
let marks;
|
|
149
|
+
if (line.actualText !== undefined && this.structure && element !== undefined) {
|
|
150
|
+
marks = new Map();
|
|
151
|
+
// Allocate MCIDs/Link children in logical order, then paint the independently positioned visual order.
|
|
152
|
+
for (const segment of [...line.segments].sort((a, b) => (a.logicalStart ?? 0) - (b.logicalStart ?? 0))) {
|
|
153
|
+
const owner = segment.link ? this.structure.link(element, segment.link) : element;
|
|
154
|
+
marks.set(segment, { owner, mark: this.structure.mark(owner, segment.actualText) });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
for (const segment of line.segments) {
|
|
158
|
+
const registered = marks?.get(segment), owner = registered?.owner ?? (segment.link && this.structure && element !== undefined ? this.structure.link(element, segment.link) : element);
|
|
159
|
+
if (segment.glyphs) {
|
|
160
|
+
const font = segment.font, n = pdfNumber, shear = font.syntheticItalic ? .213 : 0;
|
|
161
|
+
let cursor = x, command = "q " + this.rgb(segment.color ?? "111827") + " rg " + this.rgb(segment.color ?? "111827") + " RG\nBT /" + font.name + " " + n(size) + " Tf " +
|
|
162
|
+
(font.syntheticBold ? n(size * .025) + " w 2 Tr " : "0 Tr ");
|
|
163
|
+
for (const glyph of segment.glyphs) {
|
|
164
|
+
command += "1 0 " + n(shear) + " 1 " + n(cursor + (glyph.xOffset + shear * glyph.yOffset) * size / 1000) + " " + n(baseline + glyph.yOffset * size / 1000) + " Tm " + font.encodeGlyph(glyph.id, glyph.logicalText) + " Tj\n";
|
|
165
|
+
cursor += glyph.xAdvance * size / 1000;
|
|
166
|
+
}
|
|
167
|
+
command += "ET Q\n";
|
|
168
|
+
this.add(this.structure && owner !== undefined ? (registered?.mark ?? this.structure.mark(owner, segment.actualText)) + command + "EMC\n" : this.artifact(command, "Pagination"));
|
|
169
|
+
}
|
|
170
|
+
else
|
|
171
|
+
this.text(segment.text, segment.font, size, x, baseline, segment.color, owner);
|
|
172
|
+
if (segment.link) {
|
|
173
|
+
const ascent = (segment.font.program?.ascent ?? 800) * size / 1000;
|
|
174
|
+
const descent = (segment.font.program?.descent ?? -200) * size / 1000;
|
|
175
|
+
const annotationOwner = owner ?? (this.structure && annotationParent !== undefined ? this.structure.link(annotationParent, segment.link) : undefined);
|
|
176
|
+
this.link(segment.link, x, baseline + descent, segment.width, ascent - descent, annotationOwner);
|
|
177
|
+
}
|
|
178
|
+
x += segment.width;
|
|
179
|
+
}
|
|
180
|
+
if (lineActualText)
|
|
181
|
+
this.add("EMC\n");
|
|
182
|
+
}
|
|
183
|
+
decorationText(value) {
|
|
184
|
+
if (value === undefined)
|
|
185
|
+
return "";
|
|
186
|
+
checkAbort(this.settings.options.signal);
|
|
187
|
+
return synchronousText(typeof value === "function" ? value(Object.freeze({ pageNumber: this.references.length, sectionNumber: this.sectionCount || 1, sectionPageNumber: this.sectionPages, ...(this.sectionTitle === undefined ? {} : { sectionTitle: this.sectionTitle }) })) : value, "Page header/footer");
|
|
188
|
+
}
|
|
189
|
+
decoration(text, x, y, width) {
|
|
190
|
+
if (!text)
|
|
191
|
+
return;
|
|
192
|
+
this.settings.budget.cell(pdfBudgetText(text, this.settings.budget));
|
|
193
|
+
const font = this.fonts.select(), lines = wrapText(text, font, this.settings.fontSize, width, this.settings.limits.maxCellCharacters, 1, false);
|
|
194
|
+
this.text(lines[0].text, font, this.settings.fontSize, x, y);
|
|
195
|
+
}
|
|
196
|
+
async start(tableHeadings = true, preparedHeadings) {
|
|
197
|
+
if (this.pendingPage === undefined && this.references.length >= this.settings.limits.maxPages)
|
|
198
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxPages exceeded.");
|
|
199
|
+
const { page, margins, options, fontSize } = this.settings;
|
|
200
|
+
const lineHeight = this.fonts.select().lineHeight(fontSize);
|
|
201
|
+
if (this.pendingPage !== undefined) {
|
|
202
|
+
this.current = this.pendingPage;
|
|
203
|
+
this.pendingPage = undefined;
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
this.current = this.objects.reserve();
|
|
207
|
+
this.references.push(this.current);
|
|
208
|
+
}
|
|
209
|
+
this.commands = [];
|
|
210
|
+
this.annotations = [];
|
|
211
|
+
this.commandBytes = 0;
|
|
212
|
+
this.bodyAdded = false;
|
|
213
|
+
this.sectionPages++;
|
|
214
|
+
if (options.pageNumbers !== false) {
|
|
215
|
+
this.totalPages ??= this.objects.reserve();
|
|
216
|
+
this.numberSettings ??= this.settings;
|
|
217
|
+
if (!this.pageNumberWidth)
|
|
218
|
+
this.pageNumberWidth = this.measurePageNumbers();
|
|
219
|
+
if (this.pageNumberWidth > page.width - margins.left - margins.right)
|
|
220
|
+
throw new RangeError("Page is too narrow for page numbers at the declared maxPages and font size.");
|
|
221
|
+
}
|
|
222
|
+
this.structure?.beginPage(this.current);
|
|
223
|
+
this.y = page.height - margins.top;
|
|
224
|
+
const header = this.decorationText(options.pageHeader), footer = this.decorationText(options.pageFooter);
|
|
225
|
+
if (header && margins.top < lineHeight + 4)
|
|
226
|
+
throw new RangeError("Top margin is too small for a page header.");
|
|
227
|
+
if ((footer || options.pageNumbers !== false) && margins.bottom < lineHeight + 4)
|
|
228
|
+
throw new RangeError("Bottom margin is too small for page decorations.");
|
|
229
|
+
const font = this.fonts.select(), centerOffset = ((font.program?.ascent ?? 800) + (font.program?.descent ?? -200)) * fontSize / 2000;
|
|
230
|
+
this.decoration(header, margins.left, page.height - margins.top / 2 - centerOffset, page.width - margins.left - margins.right);
|
|
231
|
+
const footerWidth = page.width - margins.left - margins.right - (options.pageNumbers !== false ? this.pageNumberWidth + this.settings.padding * 2 : 0);
|
|
232
|
+
if (footerWidth <= 0 && footer)
|
|
233
|
+
throw new RangeError("Page is too narrow for both footer text and page numbers.");
|
|
234
|
+
this.decoration(footer, margins.left, margins.bottom / 2 - centerOffset, footerWidth);
|
|
235
|
+
if (options.pageNumbers !== false) {
|
|
236
|
+
const labels = options.pageNumberLabels, suffix = labels?.suffix ?? "";
|
|
237
|
+
const x = page.width - margins.right - this.pageNumberWidth + 1, y = margins.bottom / 2 - centerOffset, label = (labels?.prefix ?? "Page ") + this.references.length + (labels?.separator ?? " of ");
|
|
238
|
+
const line = wrapText(label, font, fontSize, this.pageNumberWidth, this.settings.limits.maxCellCharacters, 1, false)[0];
|
|
239
|
+
this.settings.budget.cell(label + suffix);
|
|
240
|
+
this.text(label, font, fontSize, x, y);
|
|
241
|
+
this.add(this.artifact("q 1 0 0 1 " + pdfNumber(x + line.width) + " " + pdfNumber(y) + " cm /TotalPages Do Q\n", "Pagination"));
|
|
242
|
+
}
|
|
243
|
+
for (const bookmark of this.pendingBookmarks.splice(0))
|
|
244
|
+
this.outlines?.add(bookmark, this.current, this.y);
|
|
245
|
+
if (this.references.length === 1) {
|
|
246
|
+
if (options.title)
|
|
247
|
+
await this.paragraph(options.title, { bold: true }, fontSize * 1.5, false, 1);
|
|
248
|
+
if (options.messageTop)
|
|
249
|
+
await this.paragraph(options.messageTop, {}, fontSize, false);
|
|
250
|
+
}
|
|
251
|
+
const headers = preparedHeadings ?? (tableHeadings && this.layout ? this.activeHeadings ??= await this.layout.headers() : []), headerHeight = headers.reduce((n, h) => n + h.height, 0), continuation = tableHeadings && this.layout ? this.continuationCaption : undefined;
|
|
252
|
+
if (continuation?.lines.length) {
|
|
253
|
+
if (this.continuationCaptionPaints++ === 0)
|
|
254
|
+
this.tableCaptionOwner = this.captionAnnotations(continuation, this.tableCaptionOwner);
|
|
255
|
+
await this.paragraphs([continuation], headerHeight + lineHeight + this.settings.padding * 2, true, { artifact: true, ...(this.tableCaptionOwner === undefined ? {} : { annotationParent: this.tableCaptionOwner }) });
|
|
256
|
+
}
|
|
257
|
+
this.continuationTop = page.height - margins.top - headerHeight - (continuation?.height ?? 0);
|
|
258
|
+
if (this.y - headerHeight - lineHeight - this.settings.padding * 2 < margins.bottom)
|
|
259
|
+
throw new RangeError("PDF title and repeated headings leave no room for a data line.");
|
|
260
|
+
if (tableHeadings && this.layout && this.structure && this.tableElement === undefined) {
|
|
261
|
+
this.tableElement = this.structure.element("Table", this.groupParent);
|
|
262
|
+
this.tableStructure = new PdfTableStructure(this.structure, this.tableElement, headers);
|
|
263
|
+
await this.tableStructure.prepareHeaders();
|
|
264
|
+
}
|
|
265
|
+
await this.drawHeadings(headers);
|
|
266
|
+
this.dataTop = this.y;
|
|
267
|
+
}
|
|
268
|
+
async drawHeadings(headers) {
|
|
269
|
+
for (const header of headers) {
|
|
270
|
+
// Prepared headings can be reused by continuation pages and several logical summary tables.
|
|
271
|
+
// Charge physical repeats without recapturing callbacks or making them semantic duplicates.
|
|
272
|
+
for (const cell of header.cells)
|
|
273
|
+
if (!this.bandCellBudget && this.paintedHeaderBudgetCells.has(cell.source ?? cell))
|
|
274
|
+
this.settings.budget.cell(cell.lines.map(line => line.text).join(""));
|
|
275
|
+
const elements = this.tableStructure ? header.cells.map(cell => this.tableStructure.owner(cell.source ?? cell)) : undefined;
|
|
276
|
+
const repeated = header.cells.map(cell => this.paintedHeaderCells.has(cell.source ?? cell));
|
|
277
|
+
this.draw(header, 0, header.lines, elements, repeated);
|
|
278
|
+
for (const cell of header.cells) {
|
|
279
|
+
this.paintedHeaderCells.add(cell.source ?? cell);
|
|
280
|
+
this.paintedHeaderBudgetCells.add(cell.source ?? cell);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
async rowElements(row) {
|
|
285
|
+
const source = row.source ?? row;
|
|
286
|
+
return this.tableStructure?.prepareRow(source, "data", this.tableElement, this.summaryRows.has(source) ? 0 : undefined);
|
|
287
|
+
}
|
|
288
|
+
draw(row, firstLine, lineCount, elements, repeated = []) {
|
|
289
|
+
if (!this.layout)
|
|
290
|
+
throw new Error("A table layout is required before drawing rows.");
|
|
291
|
+
this.bodyAdded = true;
|
|
292
|
+
const { padding, fontSize } = this.settings, n = pdfNumber, height = lineCount * row.lineHeight + padding * 2;
|
|
293
|
+
for (const [index, cell] of row.cells.entries()) {
|
|
294
|
+
if (this.bandCellBudget)
|
|
295
|
+
this.settings.budget.cell((cell.height === undefined ? cell.lines.slice(firstLine, firstLine + lineCount) : cell.lines).map(line => line.text).join(""));
|
|
296
|
+
const x = this.layout.lefts[cell.first], cellHeight = cell.height ?? height;
|
|
297
|
+
if (cell.style.background)
|
|
298
|
+
this.add(this.artifact("q " + this.rgb(cell.style.background) + " rg " + n(x) + " " + n(this.y - cellHeight) + " " + n(cell.width) + " " + n(cellHeight) + " re f Q\n"));
|
|
299
|
+
this.add(this.artifact("q 0.82 0.85 0.89 RG 0.4 w " + n(x) + " " + n(this.y - cellHeight) + " " + n(cell.width) + " " + n(cellHeight) + " re S Q\n"));
|
|
300
|
+
const element = elements?.[index], artifact = cell.semanticArtifact || repeated[index], owner = cell.link && this.structure && element !== undefined ? this.structure.link(element, cell.link) : element;
|
|
301
|
+
for (let line = firstLine; line < Math.min(cell.lines.length, cell.height ? cell.lines.length : firstLine + lineCount); line++) {
|
|
302
|
+
const text = cell.lines[line], left = cell.style.alignment === "right" ? cell.width - padding - text.width : cell.style.alignment === "center" ? (cell.width - text.width) / 2 : padding;
|
|
303
|
+
this.inline(text, fontSize, x + left, this.y - padding - text.ascent - (line - firstLine) * (row.structured ? cell.lineHeight : row.lineHeight), artifact ? undefined : owner, owner);
|
|
304
|
+
}
|
|
305
|
+
if (cell.link)
|
|
306
|
+
this.link(cell.link, x, this.y - cellHeight, cell.width, cellHeight, owner);
|
|
307
|
+
}
|
|
308
|
+
this.y -= row.structured ? row.height : height;
|
|
309
|
+
}
|
|
310
|
+
async block(rows) {
|
|
311
|
+
if (this.bandFlow)
|
|
312
|
+
return this.bandFlow.block(rows);
|
|
313
|
+
const height = rows.reduce((n, row) => n + row.height, 0), bottom = this.settings.margins.bottom;
|
|
314
|
+
if (height > this.y - bottom) {
|
|
315
|
+
await this.finishPage();
|
|
316
|
+
await this.start();
|
|
317
|
+
}
|
|
318
|
+
if (height > this.dataTop - bottom)
|
|
319
|
+
throw new RangeError("A structured footer must fit on one page below the repeated headings.");
|
|
320
|
+
for (const row of rows) {
|
|
321
|
+
const elements = await this.rowElements(row);
|
|
322
|
+
this.draw(row, 0, row.lines, elements);
|
|
323
|
+
await this.tableStructure?.completeRow(row.source ?? row);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
async row(row, allowFirstFragment = false) {
|
|
327
|
+
if (this.bandFlow)
|
|
328
|
+
return this.bandFlow.row(row, allowFirstFragment);
|
|
329
|
+
const elements = await this.rowElements(row);
|
|
330
|
+
const { margins, padding } = this.settings, full = this.continuationTop - margins.bottom;
|
|
331
|
+
if (!allowFirstFragment && row.height <= full + .001 && row.height > this.y - margins.bottom + .001) {
|
|
332
|
+
await this.finishPage();
|
|
333
|
+
await this.start();
|
|
334
|
+
}
|
|
335
|
+
let first = 0;
|
|
336
|
+
while (first < row.lines) {
|
|
337
|
+
let lines = Math.min(row.lines - first, Math.floor((this.y - margins.bottom - padding * 2 + .001) / row.lineHeight));
|
|
338
|
+
if (lines <= 0) {
|
|
339
|
+
await this.finishPage();
|
|
340
|
+
await this.start();
|
|
341
|
+
lines = Math.min(row.lines - first, Math.floor((this.y - margins.bottom - padding * 2 + .001) / row.lineHeight));
|
|
342
|
+
}
|
|
343
|
+
if (lines <= 0)
|
|
344
|
+
throw new RangeError("PDF page cannot fit a table line.");
|
|
345
|
+
this.draw(row, first, lines, elements);
|
|
346
|
+
first += lines;
|
|
347
|
+
if (first < row.lines) {
|
|
348
|
+
await this.finishPage();
|
|
349
|
+
await this.start();
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
await this.tableStructure?.completeRow(row.source ?? row);
|
|
353
|
+
}
|
|
354
|
+
/** Keep a last detail row and its closing subtotals together when the complete bundle fits below
|
|
355
|
+
* fresh repeated context/headings. Oversized bundles deliberately use ordinary row splitting. */
|
|
356
|
+
async keptRows(rows, allowFirstFragment = false) {
|
|
357
|
+
if (this.bandFlow)
|
|
358
|
+
return this.bandFlow.keptRows(rows, allowFirstFragment);
|
|
359
|
+
const height = rows.reduce((total, row) => total + row.height, 0), bottom = this.settings.margins.bottom;
|
|
360
|
+
const fits = height <= this.continuationTop - bottom + .001;
|
|
361
|
+
if (fits && height > this.y - bottom + .001) {
|
|
362
|
+
await this.finishPage();
|
|
363
|
+
await this.start();
|
|
364
|
+
}
|
|
365
|
+
for (const [index, row] of rows.entries())
|
|
366
|
+
await this.row(row, !fits && index === 0 && allowFirstFragment);
|
|
367
|
+
}
|
|
368
|
+
async paragraph(text, style = {}, size = this.settings.fontSize, paginate = true, headingLevel) {
|
|
369
|
+
await this.ensureStarted();
|
|
370
|
+
const { margins, page, limits } = this.settings, font = this.fonts.select(style.bold, style.italic), lineHeight = font.lineHeight(size);
|
|
371
|
+
this.settings.budget.cell(pdfBudgetText(text, this.settings.budget));
|
|
372
|
+
const lines = await wrapTextCooperatively(text, font, size, page.width - margins.left - margins.right, limits.maxCellCharacters, limits.maxRowLines, true, this.settings.options.signal);
|
|
373
|
+
if (lineHeight > page.height - margins.top - margins.bottom)
|
|
374
|
+
throw new RangeError("PDF page cannot fit a paragraph line at the selected font metrics.");
|
|
375
|
+
if (!paginate && this.y - lines.length * lineHeight - 8 < margins.bottom)
|
|
376
|
+
throw new RangeError("PDF title/message exceeds the first page's content area.");
|
|
377
|
+
const element = this.structure?.element(headingLevel ? "H" + headingLevel : "P");
|
|
378
|
+
for (const line of lines) {
|
|
379
|
+
if (this.y - lineHeight < margins.bottom) {
|
|
380
|
+
await this.finishPage();
|
|
381
|
+
await this.start(false);
|
|
382
|
+
}
|
|
383
|
+
const x = style.alignment === "right" ? page.width - margins.right - line.width : style.alignment === "center" ? margins.left + (page.width - margins.left - margins.right - line.width) / 2 : margins.left;
|
|
384
|
+
this.bodyAdded = true;
|
|
385
|
+
this.text(line.text, font, size, x, this.y - (font.program?.ascent ?? 800) * size / 1000, style.color, element);
|
|
386
|
+
this.y -= lineHeight;
|
|
387
|
+
}
|
|
388
|
+
this.y -= 8;
|
|
389
|
+
await this.structure?.complete(element);
|
|
390
|
+
}
|
|
391
|
+
/** Reserve a meaningful next fragment before painting a kept heading; never buffer a table. */
|
|
392
|
+
async reserveSpace(height, label = "Kept PDF content") {
|
|
393
|
+
await this.ensureStarted();
|
|
394
|
+
const { page, margins } = this.settings, full = page.height - margins.top - margins.bottom;
|
|
395
|
+
if (height > full + .001)
|
|
396
|
+
throw new RangeError(label + " exceeds a fresh page's content area.");
|
|
397
|
+
if (height > this.y - margins.bottom + .001)
|
|
398
|
+
await this.pageBreak();
|
|
399
|
+
}
|
|
400
|
+
/** Paint bounded paragraphs with their captured layout and a measured next-block fragment. */
|
|
401
|
+
async paragraphs(paragraphs, nextHeight = 0, keepTogether = false, frame = {}) {
|
|
402
|
+
const visible = paragraphs.filter(paragraph => paragraph.lines.length > 0);
|
|
403
|
+
if (!visible.length)
|
|
404
|
+
return;
|
|
405
|
+
await this.ensureStarted();
|
|
406
|
+
const reservations = new Array(visible.length).fill(0);
|
|
407
|
+
for (let i = visible.length - 1; i >= 0; i--) {
|
|
408
|
+
const paragraph = visible[i], next = visible[i + 1];
|
|
409
|
+
reservations[i] = paragraph.keepWithNext ? next ? next.keepTogether || next.keepWithNext ? next.height + reservations[i + 1] : next.firstHeight : nextHeight : 0;
|
|
410
|
+
}
|
|
411
|
+
if (keepTogether && visible.length)
|
|
412
|
+
await this.reserveSpace(visible.reduce((height, paragraph) => height + paragraph.height, 0) + reservations.at(-1), "Kept PDF paragraph group");
|
|
413
|
+
for (const [index, paragraph] of visible.entries()) {
|
|
414
|
+
if (paragraph.budgetOnPaint)
|
|
415
|
+
this.settings.budget.cell(pdfBudgetText(paragraph.text.text, this.settings.budget));
|
|
416
|
+
if (paragraph.keepTogether || paragraph.keepWithNext)
|
|
417
|
+
await this.reserveSpace(paragraph.height + reservations[index], "Kept PDF paragraph chain");
|
|
418
|
+
const { margins, page } = this.settings;
|
|
419
|
+
if (paragraph.firstHeight > page.height - margins.top - margins.bottom + .001)
|
|
420
|
+
throw new RangeError("PDF page cannot fit a paragraph line and its leading spacing.");
|
|
421
|
+
if (this.y - paragraph.firstHeight < margins.bottom)
|
|
422
|
+
await this.pageBreak();
|
|
423
|
+
this.y -= paragraph.spaceBefore;
|
|
424
|
+
if (paragraph.bookmark)
|
|
425
|
+
this.addBookmark(paragraph.bookmark);
|
|
426
|
+
const element = frame.artifact ? undefined : frame.owner ?? this.structure?.element(frame.role ?? (paragraph.headingLevel ? "H" + paragraph.headingLevel : "P"), frame.parent ?? this.groupParent);
|
|
427
|
+
let firstLine = true;
|
|
428
|
+
for (const line of paragraph.lines) {
|
|
429
|
+
if (line.lineHeight > page.height - margins.top - margins.bottom + .001)
|
|
430
|
+
throw new RangeError("PDF page cannot fit a paragraph line at the selected font metrics.");
|
|
431
|
+
if (this.y - line.lineHeight < margins.bottom)
|
|
432
|
+
await this.pageBreak();
|
|
433
|
+
const left = margins.left + (frame.indent ?? 0), width = page.width - margins.right - left;
|
|
434
|
+
const x = paragraph.style.alignment === "right" ? page.width - margins.right - line.width : paragraph.style.alignment === "center" ? left + (width - line.width) / 2 : left;
|
|
435
|
+
if (firstLine && frame.marker)
|
|
436
|
+
this.inline(frame.marker.line, paragraph.size, left - frame.marker.gap - frame.marker.line.width, this.y - line.ascent, frame.marker.owner);
|
|
437
|
+
this.bodyAdded = true;
|
|
438
|
+
this.inline(line, paragraph.size, x, this.y - line.ascent, element, frame.annotationParent ?? element);
|
|
439
|
+
this.y -= line.lineHeight;
|
|
440
|
+
firstLine = false;
|
|
441
|
+
}
|
|
442
|
+
this.y -= paragraph.spaceAfter;
|
|
443
|
+
if (!frame.keepOpen)
|
|
444
|
+
await this.structure?.complete(element);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
get groupParent() { return this.groups.at(-1)?.element; }
|
|
448
|
+
/** Nest a real report group around its visible heading, tables and closing summary. */
|
|
449
|
+
async beginGroup(level, heading, minimumNextHeight) {
|
|
450
|
+
if (level !== this.groups.length)
|
|
451
|
+
throw new Error("PDF analytical groups must open in hierarchical order.");
|
|
452
|
+
await this.ensureStarted();
|
|
453
|
+
const element = this.structure?.element("Sect", this.groupParent);
|
|
454
|
+
this.groups.push({ level, headingLevel: heading.headingLevel ?? level + 2, element });
|
|
455
|
+
await this.paragraphs([heading], minimumNextHeight, true, { role: "H" + (heading.headingLevel ?? level + 2), ...(element === undefined ? {} : { parent: element }) });
|
|
456
|
+
}
|
|
457
|
+
async endGroup(level) {
|
|
458
|
+
const current = this.groups.at(-1);
|
|
459
|
+
if (!current || current.level !== level)
|
|
460
|
+
throw new Error("PDF analytical groups must close in hierarchical order.");
|
|
461
|
+
await this.structure?.complete(current.element);
|
|
462
|
+
this.outlines?.closeGroup(current.headingLevel);
|
|
463
|
+
this.groups.pop();
|
|
464
|
+
}
|
|
465
|
+
/** The generated summary label is a row heading; no localized text/style inference is used. */
|
|
466
|
+
summary(row) { this.summaryRows.add(row); }
|
|
467
|
+
/** Begin an independent table on the current page, retaining its headings on continuation pages.
|
|
468
|
+
* Group context uses captionArtifact: it stays visually complete without duplicating logical headings.
|
|
469
|
+
* An already-painted opening path only suppresses its artifact caption on that existing page. */
|
|
470
|
+
async beginTable(layout, preparedHeadings, minimumDataHeight = layout.lineHeight + this.settings.padding * 2, caption, continuationCaption, headingsOnNewPageOnly = false, captionArtifact = false, openingContextAlreadyPainted = false) {
|
|
471
|
+
if (layout.bands)
|
|
472
|
+
return this.beginBands(layout, preparedHeadings, minimumDataHeight, caption, continuationCaption, captionArtifact, openingContextAlreadyPainted);
|
|
473
|
+
await this.ensureStarted();
|
|
474
|
+
this.layout = layout;
|
|
475
|
+
this.paintedHeaderCells = new WeakSet();
|
|
476
|
+
this.continuationCaption = continuationCaption;
|
|
477
|
+
this.continuationCaptionPaints = 0;
|
|
478
|
+
this.tableCaptionOwner = undefined;
|
|
479
|
+
this.tableElement = this.structure?.element("Table", this.groupParent);
|
|
480
|
+
const headings = preparedHeadings ?? await layout.headers();
|
|
481
|
+
this.activeHeadings = headings;
|
|
482
|
+
// A closing summary can share the preceding table's headings on the current page.
|
|
483
|
+
// Its own headings remain available when it moves or splits onto a fresh page.
|
|
484
|
+
const initialHeadings = headingsOnNewPageOnly && this.bodyAdded && !this.structure ? [] : headings;
|
|
485
|
+
const height = initialHeadings.reduce((n, row) => n + row.height, 0);
|
|
486
|
+
if (caption && !(captionArtifact && openingContextAlreadyPainted)) {
|
|
487
|
+
await this.reserveSpace(caption.height + height + minimumDataHeight, "Kept PDF table caption and first data fragment");
|
|
488
|
+
this.tableCaptionOwner = captionArtifact ? this.captionAnnotations(caption)
|
|
489
|
+
: caption.lines.length ? this.structure?.element("Caption", this.tableElement) : undefined;
|
|
490
|
+
await this.paragraphs([caption], height + minimumDataHeight, true, captionArtifact
|
|
491
|
+
? { artifact: true, ...(this.tableCaptionOwner === undefined ? {} : { annotationParent: this.tableCaptionOwner }) }
|
|
492
|
+
: { role: "Caption", keepOpen: true, ...(this.tableCaptionOwner === undefined ? {} : { owner: this.tableCaptionOwner }) });
|
|
493
|
+
}
|
|
494
|
+
if (this.structure) {
|
|
495
|
+
this.tableStructure = new PdfTableStructure(this.structure, this.tableElement, headings);
|
|
496
|
+
await this.tableStructure.prepareHeaders();
|
|
497
|
+
}
|
|
498
|
+
if (this.y - height - minimumDataHeight < this.settings.margins.bottom) {
|
|
499
|
+
await this.finishPage();
|
|
500
|
+
await this.start(true, headings);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
this.continuationTop = this.settings.page.height - this.settings.margins.top - headings.reduce((n, row) => n + row.height, 0) - (continuationCaption?.height ?? 0);
|
|
504
|
+
await this.drawHeadings(initialHeadings);
|
|
505
|
+
this.dataTop = this.y;
|
|
506
|
+
}
|
|
507
|
+
/** Bind the bounded logical-window owner to the existing physical page painter. */
|
|
508
|
+
async beginBands(layout, preparedHeadings, minimumDataHeight, caption, continuation, captionArtifact, openingContextAlreadyPainted) {
|
|
509
|
+
await this.ensureStarted();
|
|
510
|
+
const logicalHeadings = preparedHeadings ?? await layout.headers(), headings = await layout.bandRows(logicalHeadings, true);
|
|
511
|
+
const headerHeight = headings[0].reduce((sum, row) => sum + row.height, 0), { page, margins, padding } = this.settings;
|
|
512
|
+
const firstFullCapacity = page.height - margins.top - margins.bottom - headerHeight - (caption?.height ?? 0);
|
|
513
|
+
const minimum = minimumDataHeight <= firstFullCapacity + .001 ? minimumDataHeight : layout.lineHeight + padding * 2;
|
|
514
|
+
const continuationCapacity = page.height - margins.top - margins.bottom - headerHeight - (continuation?.height ?? 0);
|
|
515
|
+
if (continuationCapacity + .001 < layout.lineHeight + padding * 2)
|
|
516
|
+
throw new RangeError("PDF band headings and continuation context leave no room for a data line.");
|
|
517
|
+
let openingContextPainted = captionArtifact && openingContextAlreadyPainted;
|
|
518
|
+
if (this.y - margins.bottom - headerHeight - (openingContextPainted ? 0 : caption?.height ?? 0) + .001 < minimum) {
|
|
519
|
+
await this.pageBreak();
|
|
520
|
+
openingContextPainted = false;
|
|
521
|
+
}
|
|
522
|
+
const firstCapacity = this.y - margins.bottom - headerHeight - (openingContextPainted ? 0 : caption?.height ?? 0);
|
|
523
|
+
if (firstCapacity + .001 < minimum)
|
|
524
|
+
throw new RangeError("PDF band caption and headings leave no room for a data line.");
|
|
525
|
+
let continuationPaints = 0;
|
|
526
|
+
this.tableElement = this.structure?.element("Table", this.groupParent);
|
|
527
|
+
this.paintedHeaderCells = new WeakSet();
|
|
528
|
+
let captionElement = this.structure && caption?.lines.length ? captionArtifact ? this.captionAnnotations(caption)
|
|
529
|
+
: this.structure.element("Caption", this.tableElement) : undefined;
|
|
530
|
+
if (this.structure) {
|
|
531
|
+
this.tableStructure = new PdfTableStructure(this.structure, this.tableElement, logicalHeadings);
|
|
532
|
+
}
|
|
533
|
+
this.layout = undefined;
|
|
534
|
+
this.continuationCaption = undefined;
|
|
535
|
+
this.activeHeadings = undefined;
|
|
536
|
+
this.bandFlow = new PdfTableBandFlow(layout, headings, { firstCapacity, firstFullCapacity, continuationCapacity,
|
|
537
|
+
prepareRow: async (row) => { await this.tableStructure?.prepareHeaders(); await this.rowElements(row); },
|
|
538
|
+
completeRow: async (row) => { await this.tableStructure?.completeRow(row.source ?? row); },
|
|
539
|
+
moveFirstToFreshPage: async () => { await this.pageBreak(); openingContextPainted = false; },
|
|
540
|
+
paint: async (band, headers, rows, currentCaption, firstWindow, firstBand) => {
|
|
541
|
+
if (!(firstWindow && firstBand))
|
|
542
|
+
await this.pageBreak();
|
|
543
|
+
const frame = layout.bands[band];
|
|
544
|
+
this.layout = { lefts: frame.lefts, lineHeight: layout.lineHeight, headers: async () => headers };
|
|
545
|
+
this.bandCellBudget = true;
|
|
546
|
+
try {
|
|
547
|
+
if (currentCaption?.lines.length && !(firstWindow && firstBand && openingContextPainted)) {
|
|
548
|
+
if (!firstWindow && continuationPaints++ === 0)
|
|
549
|
+
captionElement = this.captionAnnotations(currentCaption, captionElement);
|
|
550
|
+
const annotationParent = captionElement;
|
|
551
|
+
await this.paragraphs([currentCaption], headerHeight + minimum, true, firstWindow && firstBand && !captionArtifact ? { role: "Caption", keepOpen: true, ...(captionElement === undefined ? {} : { owner: captionElement }) }
|
|
552
|
+
: { artifact: true, ...(annotationParent === undefined ? {} : { annotationParent }) });
|
|
553
|
+
}
|
|
554
|
+
await this.tableStructure?.prepareHeaders();
|
|
555
|
+
await this.drawHeadings(headers);
|
|
556
|
+
this.dataTop = this.y;
|
|
557
|
+
for (const row of rows) {
|
|
558
|
+
checkAbort(this.settings.options.signal);
|
|
559
|
+
const elements = this.tableStructure ? row.cells.map(cell => this.tableStructure.owner(cell.source ?? cell)) : undefined;
|
|
560
|
+
this.draw(row, 0, row.lines, elements);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
finally {
|
|
564
|
+
this.bandCellBudget = false;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}, caption, continuation);
|
|
568
|
+
}
|
|
569
|
+
async endTable() {
|
|
570
|
+
if (this.bandFlow) {
|
|
571
|
+
const flow = this.bandFlow;
|
|
572
|
+
await flow.flush(true);
|
|
573
|
+
this.bandFlow = undefined;
|
|
574
|
+
}
|
|
575
|
+
await this.tableStructure?.finish();
|
|
576
|
+
this.layout = undefined;
|
|
577
|
+
this.tableElement = undefined;
|
|
578
|
+
this.tableStructure = undefined;
|
|
579
|
+
this.activeHeadings = undefined;
|
|
580
|
+
this.continuationCaption = undefined;
|
|
581
|
+
this.tableCaptionOwner = undefined;
|
|
582
|
+
this.continuationCaptionPaints = 0;
|
|
583
|
+
this.y -= 8;
|
|
584
|
+
}
|
|
585
|
+
/** Repeated context stays an artifact; its interactive links retain one authored label/owner. */
|
|
586
|
+
captionAnnotations(caption, existingOwner) {
|
|
587
|
+
if (!this.structure || !caption.text.runs.some(run => run.link && run.text.trim()))
|
|
588
|
+
return existingOwner;
|
|
589
|
+
const owner = existingOwner ?? this.structure.element("Caption", this.tableElement);
|
|
590
|
+
for (const run of caption.text.runs)
|
|
591
|
+
if (run.link && run.text.trim())
|
|
592
|
+
this.structure.link(owner, run.link, run.text);
|
|
593
|
+
return owner;
|
|
594
|
+
}
|
|
595
|
+
async pageBreak() { await this.ensureStarted(); await this.finishPage(); await this.start(false); }
|
|
596
|
+
registerXObject(name, id) { this.externalObjects.set(name, id); }
|
|
597
|
+
/** Height after the existing fit-to-page graphic policy, used by heading keep constraints. */
|
|
598
|
+
graphicHeight(width, height) {
|
|
599
|
+
const { page, margins } = this.settings;
|
|
600
|
+
return height * Math.min(1, (page.width - margins.left - margins.right) / width, (page.height - margins.top - margins.bottom) / height);
|
|
601
|
+
}
|
|
602
|
+
/** Place an indivisible graphic, preserving aspect ratio and scaling down to page bounds. */
|
|
603
|
+
async graphic(width, height, alignment, paint, alternativeText, caption) {
|
|
604
|
+
await this.ensureStarted();
|
|
605
|
+
const { page, margins } = this.settings, available = page.width - margins.left - margins.right;
|
|
606
|
+
const scale = Math.min(1, available / width, (page.height - margins.top - margins.bottom) / height);
|
|
607
|
+
width *= scale;
|
|
608
|
+
height *= scale;
|
|
609
|
+
const element = this.structure && alternativeText !== undefined ? this.structure.element("Figure", undefined, " /Alt <feff" + unicodeHex(alternativeText) + ">") : undefined;
|
|
610
|
+
if (caption) {
|
|
611
|
+
if (alternativeText === undefined)
|
|
612
|
+
throw new TypeError("A captioned PDF graphic requires alternativeText and cannot be decorative.");
|
|
613
|
+
await this.reserveSpace(caption.height + height, "Kept PDF graphic and caption");
|
|
614
|
+
await this.paragraphs([caption], height, true, { role: "Caption", ...(element === undefined ? {} : { parent: element }) });
|
|
615
|
+
}
|
|
616
|
+
if (this.y - height < margins.bottom)
|
|
617
|
+
await this.pageBreak();
|
|
618
|
+
const left = alignment === "right" ? page.width - margins.right - width : alignment === "center" ? margins.left + (available - width) / 2 : margins.left;
|
|
619
|
+
const command = paint(left, this.y - height, width, height);
|
|
620
|
+
this.bodyAdded = true;
|
|
621
|
+
this.add(this.structure && element !== undefined ? this.structure.mark(element) + command + "EMC\n" : this.artifact(command));
|
|
622
|
+
this.y -= height + 8;
|
|
623
|
+
await this.structure?.complete(element);
|
|
624
|
+
}
|
|
625
|
+
async listItem(item, listOwner) {
|
|
626
|
+
await this.ensureStarted();
|
|
627
|
+
if (this.structure && listOwner === undefined)
|
|
628
|
+
throw new Error("A tagged list item requires a logical list owner.");
|
|
629
|
+
const li = this.structure?.element("LI", listOwner), label = li === undefined ? undefined : this.structure.element("Lbl", li), body = li === undefined ? undefined : this.structure.element("LBody", li);
|
|
630
|
+
await this.paragraphs([item.paragraph], 0, false, { ...(body === undefined ? {} : { parent: body }), role: "P", indent: item.indent,
|
|
631
|
+
marker: { line: item.marker, gap: item.markerGap, ...(label === undefined ? {} : { owner: label }) } });
|
|
632
|
+
await this.structure?.complete(li);
|
|
633
|
+
}
|
|
634
|
+
async finishPage() {
|
|
635
|
+
const contents = this.objects.reserve(), { page, options } = this.settings;
|
|
636
|
+
await this.objects.stream(contents, this.commands.join(""), "", options.compression !== false);
|
|
637
|
+
const annotations = [];
|
|
638
|
+
for (const annotation of this.annotations) {
|
|
639
|
+
annotations.push(annotation.id);
|
|
640
|
+
await this.objects.object(annotation.id, annotation.body);
|
|
641
|
+
}
|
|
642
|
+
await this.objects.object(this.current, "<< /Type /Page /Parent " + this.parent + " 0 R /MediaBox [0 0 " + pdfNumber(page.width) + " " + pdfNumber(page.height) +
|
|
643
|
+
"] /Resources " + this.resources + " 0 R /Contents " + contents + " 0 R" +
|
|
644
|
+
(this.structure?.pageDictionary ?? "") +
|
|
645
|
+
(annotations.length ? " /Annots [" + annotations.map(id => id + " 0 R").join(" ") + "]" : "") + " >>");
|
|
646
|
+
if (this.structure)
|
|
647
|
+
await this.structure.finishPage();
|
|
648
|
+
this.commands = [];
|
|
649
|
+
this.annotations = [];
|
|
650
|
+
this.commandBytes = 0;
|
|
651
|
+
this.current = 0;
|
|
652
|
+
await this.objects.flushBytes();
|
|
653
|
+
}
|
|
654
|
+
async finish() {
|
|
655
|
+
if (this.bandFlow) {
|
|
656
|
+
const flow = this.bandFlow;
|
|
657
|
+
await flow.flush(true);
|
|
658
|
+
this.bandFlow = undefined;
|
|
659
|
+
}
|
|
660
|
+
await this.ensureStarted();
|
|
661
|
+
await this.tableStructure?.finish();
|
|
662
|
+
await this.finishPage();
|
|
663
|
+
if (this.totalPages) {
|
|
664
|
+
const font = this.fonts.select(), count = String(this.references.length), text = count + (this.numberSettings.options.pageNumberLabels?.suffix ?? ""), size = this.numberSettings.fontSize;
|
|
665
|
+
this.settings.budget.cell(count);
|
|
666
|
+
const content = "BT /" + font.name + " " + pdfNumber(size) + " Tf 1 0 0 1 0 0 Tm " + font.encode(text) + " Tj ET\n";
|
|
667
|
+
const width = wrapText(text, font, size, this.numberSettings.page.width, this.settings.limits.maxCellCharacters, 1, false)[0].width;
|
|
668
|
+
const bottom = (font.program?.bbox[1] ?? -250) * size / 1000 - 1, top = (font.program?.bbox[3] ?? 1000) * size / 1000 + 1;
|
|
669
|
+
await this.objects.stream(this.totalPages, content, "/Type /XObject /Subtype /Form /BBox [-1 " + pdfNumber(bottom) + " " + pdfNumber(width + 1) + " " + pdfNumber(top) + "] /Resources << /Font " + this.fonts.dictionary() + " >>", this.settings.options.compression !== false);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
xobjects() {
|
|
673
|
+
const entries = [...this.externalObjects].map(([name, id]) => "/" + name + " " + id + " 0 R");
|
|
674
|
+
if (this.totalPages)
|
|
675
|
+
entries.push("/TotalPages " + this.totalPages + " 0 R");
|
|
676
|
+
return entries.length ? " /XObject << " + entries.join(" ") + " >>" : "";
|
|
677
|
+
}
|
|
678
|
+
}
|