@wirabyte/w-pdfmaker 0.0.0-stage → 1.0.2
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/CHANGELOG.md +53 -0
- package/LICENSE +22 -0
- package/NOTICE.md +39 -0
- package/README.md +106 -2
- package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
- package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
- package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
- package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
- package/build/fonts/Roboto.js +27 -0
- package/build/pdfmake.js +103161 -0
- package/build/pdfmake.js.map +1 -0
- package/build/pdfmake.min.js +3 -0
- package/build/pdfmake.min.js.map +1 -0
- package/build/standard-fonts/Courier.js +9 -0
- package/build/standard-fonts/Helvetica.js +9 -0
- package/build/standard-fonts/Symbol.js +9 -0
- package/build/standard-fonts/Times.js +9 -0
- package/build/standard-fonts/ZapfDingbats.js +9 -0
- package/build/vfs_fonts.js +6 -0
- package/build-vfs.js +44 -0
- package/fonts/Roboto/Roboto-Italic.ttf +0 -0
- package/fonts/Roboto/Roboto-Medium.ttf +0 -0
- package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
- package/fonts/Roboto/Roboto-Regular.ttf +0 -0
- package/fonts/Roboto.js +8 -0
- package/fonts/THSarabunIT9/LICENSE.txt +7 -0
- package/fonts/THSarabunIT9/THSarabunIT9-Bold.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9-BoldItalic.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9-Italic.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9.ttf +0 -0
- package/fonts/THSarabunNew/COPYING.GPLv2 +339 -0
- package/fonts/THSarabunNew/LICENSE.txt +13 -0
- package/fonts/THSarabunNew/THSarabunNew-Bold.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew-BoldItalic.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew-Italic.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew.ttf +0 -0
- package/index.d.ts +167 -0
- package/interfaces.d.ts +2779 -0
- package/js/DocMeasure.js +695 -0
- package/js/DocPreprocessor.js +263 -0
- package/js/DocumentContext.js +563 -0
- package/js/ElementWriter.js +488 -0
- package/js/LayoutBuilder.js +1525 -0
- package/js/Line.js +105 -0
- package/js/OutputDocument.js +73 -0
- package/js/OutputDocumentServer.js +34 -0
- package/js/PDFDocument.js +322 -0
- package/js/PageElementWriter.js +302 -0
- package/js/PageSize.js +74 -0
- package/js/Printer.js +275 -0
- package/js/Renderer.js +439 -0
- package/js/SVGMeasure.js +94 -0
- package/js/StyleContextStack.js +191 -0
- package/js/TableProcessor.js +658 -0
- package/js/TextBreaker.js +164 -0
- package/js/TextDecorator.js +164 -0
- package/js/TextInlines.js +212 -0
- package/js/URLResolver.js +122 -0
- package/js/base.js +125 -0
- package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
- package/js/browser-extensions/fonts/Roboto.js +38 -0
- package/js/browser-extensions/fonts/thai.js +28 -0
- package/js/browser-extensions/index.js +88 -0
- package/js/browser-extensions/pageUrls.js +79 -0
- package/js/browser-extensions/pdfMake.js +3 -0
- package/js/browser-extensions/standard-fonts/Courier.js +20 -0
- package/js/browser-extensions/standard-fonts/Helvetica.js +20 -0
- package/js/browser-extensions/standard-fonts/Symbol.js +17 -0
- package/js/browser-extensions/standard-fonts/Times.js +20 -0
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
- package/js/browser-extensions/virtual-fs-cjs.js +3 -0
- package/js/columnCalculator.js +159 -0
- package/js/helpers/node.js +122 -0
- package/js/helpers/tools.js +46 -0
- package/js/helpers/variableType.js +59 -0
- package/js/imageFallback.js +23 -0
- package/js/index.js +45 -0
- package/js/mergeStamp.js +194 -0
- package/js/pageNumbering.js +48 -0
- package/js/qrEnc.js +721 -0
- package/js/standardPageSizes.js +56 -0
- package/js/tableLayouts.js +98 -0
- package/js/thai.js +201 -0
- package/js/virtual-fs.js +82 -0
- package/package.json +93 -4
- package/skills/w-pdfmaker/SKILL.md +171 -0
- package/skills/w-pdfmaker/references/browser.md +36 -0
- package/skills/w-pdfmaker/references/document-definition.md +241 -0
- package/skills/w-pdfmaker/references/images.md +90 -0
- package/skills/w-pdfmaker/references/merge-stamp.md +94 -0
- package/skills/w-pdfmaker/references/thai.md +67 -0
- package/src/DocMeasure.js +780 -0
- package/src/DocPreprocessor.js +293 -0
- package/src/DocumentContext.js +605 -0
- package/src/ElementWriter.js +550 -0
- package/src/LayoutBuilder.js +1689 -0
- package/src/Line.js +114 -0
- package/src/OutputDocument.js +75 -0
- package/src/OutputDocumentServer.js +38 -0
- package/src/PDFDocument.js +352 -0
- package/src/PageElementWriter.js +343 -0
- package/src/PageSize.js +53 -0
- package/src/Printer.js +289 -0
- package/src/Renderer.js +468 -0
- package/src/SVGMeasure.js +109 -0
- package/src/StyleContextStack.js +208 -0
- package/src/TableProcessor.js +719 -0
- package/src/TextBreaker.js +166 -0
- package/src/TextDecorator.js +191 -0
- package/src/TextInlines.js +229 -0
- package/src/URLResolver.js +122 -0
- package/src/base.js +143 -0
- package/src/browser-extensions/OutputDocumentBrowser.js +80 -0
- package/src/browser-extensions/fonts/Roboto.js +27 -0
- package/src/browser-extensions/fonts/thai.js +23 -0
- package/src/browser-extensions/index.js +91 -0
- package/src/browser-extensions/pageUrls.js +67 -0
- package/src/browser-extensions/pdfMake.js +1 -0
- package/src/browser-extensions/standard-fonts/Courier.js +25 -0
- package/src/browser-extensions/standard-fonts/Helvetica.js +25 -0
- package/src/browser-extensions/standard-fonts/Symbol.js +17 -0
- package/src/browser-extensions/standard-fonts/Times.js +25 -0
- package/src/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
- package/src/browser-extensions/virtual-fs-cjs.js +1 -0
- package/src/columnCalculator.js +160 -0
- package/src/helpers/node.js +134 -0
- package/src/helpers/tools.js +44 -0
- package/src/helpers/variableType.js +50 -0
- package/src/imageFallback.js +18 -0
- package/src/index.js +45 -0
- package/src/mergeStamp.js +132 -0
- package/src/pageNumbering.js +45 -0
- package/src/qrEnc.js +796 -0
- package/src/standardPageSizes.js +52 -0
- package/src/tableLayouts.js +100 -0
- package/src/thai.js +170 -0
- package/src/virtual-fs.js +92 -0
- package/standard-fonts/Courier.js +8 -0
- package/standard-fonts/Helvetica.js +8 -0
- package/standard-fonts/Symbol.js +5 -0
- package/standard-fonts/Times.js +8 -0
- package/standard-fonts/ZapfDingbats.js +5 -0
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
import { isString } from './helpers/variableType';
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A store for current x, y positions and available width/height.
|
|
6
|
+
* It facilitates column divisions and vertical sync
|
|
7
|
+
*/
|
|
8
|
+
class DocumentContext extends EventEmitter {
|
|
9
|
+
constructor() {
|
|
10
|
+
super();
|
|
11
|
+
this.pages = [];
|
|
12
|
+
this.pageMargins = undefined;
|
|
13
|
+
this.x = undefined;
|
|
14
|
+
this.availableWidth = undefined;
|
|
15
|
+
this.availableHeight = undefined;
|
|
16
|
+
this.page = -1;
|
|
17
|
+
this.activeMarginLeft = 0;
|
|
18
|
+
this.activeMarginRight = 0;
|
|
19
|
+
|
|
20
|
+
this.snapshots = [];
|
|
21
|
+
this.backgroundLength = [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
beginColumnGroup(marginXTopParent, bottomByPage = {}, snakingColumns = false, columnGap = 0, columnWidths = null) {
|
|
25
|
+
this.snapshots.push({
|
|
26
|
+
x: this.x,
|
|
27
|
+
y: this.y,
|
|
28
|
+
availableHeight: this.availableHeight,
|
|
29
|
+
availableWidth: this.availableWidth,
|
|
30
|
+
page: this.page,
|
|
31
|
+
bottomByPage: bottomByPage ? bottomByPage : {},
|
|
32
|
+
bottomMost: {
|
|
33
|
+
x: this.x,
|
|
34
|
+
y: this.y,
|
|
35
|
+
availableHeight: this.availableHeight,
|
|
36
|
+
availableWidth: this.availableWidth,
|
|
37
|
+
page: this.page
|
|
38
|
+
},
|
|
39
|
+
lastColumnWidth: this.lastColumnWidth,
|
|
40
|
+
snakingColumns: snakingColumns,
|
|
41
|
+
activeMarginLeft: this.activeMarginLeft,
|
|
42
|
+
activeMarginRight: this.activeMarginRight,
|
|
43
|
+
columnOffset: this.pageMargins ? this.x - this.pageMargins.left : 0,
|
|
44
|
+
gap: columnGap,
|
|
45
|
+
columnWidths: columnWidths
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
this.lastColumnWidth = 0;
|
|
49
|
+
if (marginXTopParent) {
|
|
50
|
+
this.marginXTopParent = marginXTopParent;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
updateBottomByPage() {
|
|
55
|
+
const lastSnapshot = this.snapshots[this.snapshots.length - 1];
|
|
56
|
+
if (!lastSnapshot) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const lastPage = this.page;
|
|
60
|
+
let previousBottom = -Number.MIN_VALUE;
|
|
61
|
+
if (lastSnapshot.bottomByPage && lastSnapshot.bottomByPage[lastPage]) {
|
|
62
|
+
previousBottom = lastSnapshot.bottomByPage[lastPage];
|
|
63
|
+
}
|
|
64
|
+
if (lastSnapshot.bottomByPage) {
|
|
65
|
+
lastSnapshot.bottomByPage[lastPage] = Math.max(previousBottom, this.y);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
resetMarginXTopParent() {
|
|
70
|
+
this.marginXTopParent = null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Find the most recent (deepest) snaking column group snapshot.
|
|
75
|
+
* @returns {object|null}
|
|
76
|
+
*/
|
|
77
|
+
getSnakingSnapshot() {
|
|
78
|
+
for (let i = this.snapshots.length - 1; i >= 0; i--) {
|
|
79
|
+
// Positioned content must not flow through its parent's columns.
|
|
80
|
+
if (this.snapshots[i].isDetachedBlock) {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
if (this.snapshots[i].snakingColumns) {
|
|
84
|
+
return this.snapshots[i];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
inSnakingColumns() {
|
|
91
|
+
return !!this.getSnakingSnapshot();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Check if we're inside a nested non-snaking column group (e.g., a table row)
|
|
96
|
+
* within an outer snaking column group. This is used to prevent snaking-specific
|
|
97
|
+
* breaks inside table cells — the table's own page break mechanism should handle
|
|
98
|
+
* row breaks, and column breaks should happen between rows.
|
|
99
|
+
* @returns {boolean}
|
|
100
|
+
*/
|
|
101
|
+
isInNestedNonSnakingGroup() {
|
|
102
|
+
for (let i = this.snapshots.length - 1; i >= 0; i--) {
|
|
103
|
+
let snap = this.snapshots[i];
|
|
104
|
+
if (snap.snakingColumns) {
|
|
105
|
+
return false; // Reached snaking snapshot without finding inner group
|
|
106
|
+
}
|
|
107
|
+
if (!snap.overflowed) {
|
|
108
|
+
return true; // Found non-snaking, non-overflowed inner group
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
beginColumn(width, offset, endingCell) {
|
|
115
|
+
// Find the correct snapshot for this column group.
|
|
116
|
+
// When a snaking column break (moveToNextColumn) occurs during inner column
|
|
117
|
+
// processing, overflowed snapshots may sit above this column group's snapshot.
|
|
118
|
+
// We need to skip past those to find the one from our beginColumnGroup call.
|
|
119
|
+
let saved = this.snapshots[this.snapshots.length - 1];
|
|
120
|
+
if (saved && saved.overflowed) {
|
|
121
|
+
for (let i = this.snapshots.length - 1; i >= 0; i--) {
|
|
122
|
+
if (!this.snapshots[i].overflowed) {
|
|
123
|
+
saved = this.snapshots[i];
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
this.calculateBottomMost(saved, endingCell);
|
|
130
|
+
|
|
131
|
+
this.page = saved.page;
|
|
132
|
+
this.x = this.x + this.lastColumnWidth + (offset || 0);
|
|
133
|
+
this.y = saved.y;
|
|
134
|
+
this.availableWidth = width; //saved.availableWidth - offset;
|
|
135
|
+
this.availableHeight = saved.availableHeight;
|
|
136
|
+
|
|
137
|
+
this.lastColumnWidth = width;
|
|
138
|
+
saved.columnStart = { page: this.page, itemsLength: this.getCurrentPage().items.length };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
calculateBottomMost(destContext, endingCell) {
|
|
142
|
+
if (endingCell) {
|
|
143
|
+
this.saveContextInEndingCell(endingCell);
|
|
144
|
+
} else {
|
|
145
|
+
destContext.bottomMost = bottomMostContext(this, destContext.bottomMost);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
markEnding(endingCell, originalXOffset, discountY) {
|
|
150
|
+
this.page = endingCell._columnEndingContext.page;
|
|
151
|
+
this.x = endingCell._columnEndingContext.x + originalXOffset;
|
|
152
|
+
this.y = endingCell._columnEndingContext.y - discountY;
|
|
153
|
+
this.availableWidth = endingCell._columnEndingContext.availableWidth;
|
|
154
|
+
this.availableHeight = endingCell._columnEndingContext.availableHeight;
|
|
155
|
+
this.lastColumnWidth = endingCell._columnEndingContext.lastColumnWidth;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
saveContextInEndingCell(endingCell) {
|
|
159
|
+
endingCell._columnEndingContext = {
|
|
160
|
+
page: this.page,
|
|
161
|
+
x: this.x,
|
|
162
|
+
y: this.y,
|
|
163
|
+
availableHeight: this.availableHeight,
|
|
164
|
+
availableWidth: this.availableWidth,
|
|
165
|
+
lastColumnWidth: this.lastColumnWidth
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
completeColumnGroup(height, endingCell) {
|
|
170
|
+
let saved = this.snapshots.pop();
|
|
171
|
+
|
|
172
|
+
// Track the maximum bottom position across all columns (including overflowed).
|
|
173
|
+
// Critical for snaking: content after columns must appear below the tallest column.
|
|
174
|
+
let maxBottomY = this.y;
|
|
175
|
+
let maxBottomPage = this.page;
|
|
176
|
+
let maxBottomAvailableHeight = this.availableHeight;
|
|
177
|
+
|
|
178
|
+
let overflowed = saved.overflowed;
|
|
179
|
+
|
|
180
|
+
// Pop overflowed snapshots created by moveToNextColumn (snaking columns).
|
|
181
|
+
// Merge their bottomMost values to find the true maximum.
|
|
182
|
+
while (saved && saved.overflowed) {
|
|
183
|
+
let bm = bottomMostContext(
|
|
184
|
+
{
|
|
185
|
+
page: maxBottomPage,
|
|
186
|
+
y: maxBottomY,
|
|
187
|
+
availableHeight: maxBottomAvailableHeight
|
|
188
|
+
},
|
|
189
|
+
saved.bottomMost || {}
|
|
190
|
+
);
|
|
191
|
+
maxBottomPage = bm.page;
|
|
192
|
+
maxBottomY = bm.y;
|
|
193
|
+
maxBottomAvailableHeight = bm.availableHeight;
|
|
194
|
+
saved = this.snapshots.pop();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (!saved) {
|
|
198
|
+
return {};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (overflowed) {
|
|
202
|
+
// Apply the max bottom from all overflowed columns to this base snapshot
|
|
203
|
+
if (
|
|
204
|
+
maxBottomPage > saved.bottomMost.page ||
|
|
205
|
+
(maxBottomPage === saved.bottomMost.page &&
|
|
206
|
+
maxBottomY > saved.bottomMost.y)
|
|
207
|
+
) {
|
|
208
|
+
saved.bottomMost = {
|
|
209
|
+
x: saved.x,
|
|
210
|
+
y: maxBottomY,
|
|
211
|
+
page: maxBottomPage,
|
|
212
|
+
availableHeight: maxBottomAvailableHeight,
|
|
213
|
+
availableWidth: saved.availableWidth
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
this.calculateBottomMost(saved, endingCell);
|
|
219
|
+
|
|
220
|
+
this.x = saved.x;
|
|
221
|
+
|
|
222
|
+
let y = saved.bottomMost.y;
|
|
223
|
+
if (height) {
|
|
224
|
+
if (saved.page === saved.bottomMost.page) {
|
|
225
|
+
if ((saved.y + height) > y) {
|
|
226
|
+
y = saved.y + height;
|
|
227
|
+
}
|
|
228
|
+
} else {
|
|
229
|
+
y += height;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
this.y = y;
|
|
234
|
+
this.page = saved.bottomMost.page;
|
|
235
|
+
this.availableWidth = saved.availableWidth;
|
|
236
|
+
this.availableHeight = saved.bottomMost.availableHeight;
|
|
237
|
+
if (height) {
|
|
238
|
+
this.availableHeight -= (y - saved.bottomMost.y);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (height && (saved.bottomMost.y - saved.y < height)) {
|
|
242
|
+
this.height = height;
|
|
243
|
+
} else {
|
|
244
|
+
this.height = saved.bottomMost.y - saved.y;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
this.lastColumnWidth = saved.lastColumnWidth;
|
|
248
|
+
return saved.bottomByPage;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Move to the next column in a column group (snaking columns).
|
|
253
|
+
* Creates an overflowed snapshot to track that we've moved to the next column.
|
|
254
|
+
* @returns {object} Position info for the new column
|
|
255
|
+
*/
|
|
256
|
+
moveToNextColumn() {
|
|
257
|
+
let prevY = this.y;
|
|
258
|
+
let snakingSnapshot = this.getSnakingSnapshot();
|
|
259
|
+
|
|
260
|
+
if (!snakingSnapshot) {
|
|
261
|
+
return { prevY: prevY, y: this.y };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Update snaking snapshot's bottomMost with current position BEFORE resetting.
|
|
265
|
+
// This captures where content reached in the current column (overflow point).
|
|
266
|
+
this.calculateBottomMost(snakingSnapshot, null);
|
|
267
|
+
|
|
268
|
+
// Calculate new X position: move right by current column width + gap
|
|
269
|
+
let overflowCount = 0;
|
|
270
|
+
for (let i = this.snapshots.length - 1; i >= 0; i--) {
|
|
271
|
+
if (this.snapshots[i].overflowed) {
|
|
272
|
+
overflowCount++;
|
|
273
|
+
} else {
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
let currentColumnWidth = (snakingSnapshot.columnWidths && snakingSnapshot.columnWidths[overflowCount]) || this.lastColumnWidth || this.availableWidth;
|
|
279
|
+
let nextColumnWidth = (snakingSnapshot.columnWidths && snakingSnapshot.columnWidths[overflowCount + 1]) || currentColumnWidth;
|
|
280
|
+
let marginWidth = this.activeMarginLeft - (snakingSnapshot.activeMarginLeft || 0) + this.activeMarginRight - (snakingSnapshot.activeMarginRight || 0);
|
|
281
|
+
|
|
282
|
+
this.lastColumnWidth = nextColumnWidth;
|
|
283
|
+
|
|
284
|
+
let newX = this.x + (currentColumnWidth || 0) + (snakingSnapshot.gap || 0);
|
|
285
|
+
let newY = snakingSnapshot.y;
|
|
286
|
+
|
|
287
|
+
this.snapshots.push({
|
|
288
|
+
x: newX,
|
|
289
|
+
y: newY,
|
|
290
|
+
availableHeight: snakingSnapshot.availableHeight,
|
|
291
|
+
availableWidth: nextColumnWidth,
|
|
292
|
+
page: this.page,
|
|
293
|
+
overflowed: true,
|
|
294
|
+
bottomMost: {
|
|
295
|
+
x: newX,
|
|
296
|
+
y: newY,
|
|
297
|
+
availableHeight: snakingSnapshot.availableHeight,
|
|
298
|
+
availableWidth: nextColumnWidth,
|
|
299
|
+
page: this.page
|
|
300
|
+
},
|
|
301
|
+
lastColumnWidth: nextColumnWidth,
|
|
302
|
+
snakingColumns: true,
|
|
303
|
+
activeMarginLeft: snakingSnapshot.activeMarginLeft,
|
|
304
|
+
activeMarginRight: snakingSnapshot.activeMarginRight,
|
|
305
|
+
columnOffset: snakingSnapshot.columnOffset,
|
|
306
|
+
gap: snakingSnapshot.gap,
|
|
307
|
+
columnWidths: snakingSnapshot.columnWidths
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
this.x = newX;
|
|
311
|
+
this.y = newY;
|
|
312
|
+
this.availableHeight = snakingSnapshot.availableHeight;
|
|
313
|
+
this.availableWidth = nextColumnWidth - marginWidth;
|
|
314
|
+
|
|
315
|
+
// Sync non-overflowed inner snapshots (e.g. inner column groups for
|
|
316
|
+
// product/price rows) with the new snaking column position.
|
|
317
|
+
// Without this, inner beginColumn would read stale y/page/x values.
|
|
318
|
+
for (let i = this.snapshots.length - 2; i >= 0; i--) {
|
|
319
|
+
let snapshot = this.snapshots[i];
|
|
320
|
+
if (snapshot.overflowed || snapshot.snakingColumns) {
|
|
321
|
+
break; // Stop at first overflowed or snaking snapshot
|
|
322
|
+
}
|
|
323
|
+
snapshot.x = newX;
|
|
324
|
+
snapshot.y = newY;
|
|
325
|
+
snapshot.page = this.page;
|
|
326
|
+
snapshot.availableHeight = snakingSnapshot.availableHeight;
|
|
327
|
+
if (snapshot.bottomMost) {
|
|
328
|
+
snapshot.bottomMost.x = newX;
|
|
329
|
+
snapshot.bottomMost.y = newY;
|
|
330
|
+
snapshot.bottomMost.page = this.page;
|
|
331
|
+
snapshot.bottomMost.availableHeight = snakingSnapshot.availableHeight;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return { prevY: prevY, y: this.y };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Reset snaking column state when moving to a new page.
|
|
340
|
+
* Clears overflowed snapshots, resets X to left margin, sets width to first column,
|
|
341
|
+
* and syncs all snapshots to new page coordinates.
|
|
342
|
+
*/
|
|
343
|
+
resetSnakingColumnsForNewPage() {
|
|
344
|
+
let snakingSnapshot = this.getSnakingSnapshot();
|
|
345
|
+
if (!snakingSnapshot) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
let pageTopY = this.pageMargins.top;
|
|
350
|
+
let pageInnerHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
|
|
351
|
+
|
|
352
|
+
// When moving to new page, start at first column.
|
|
353
|
+
// Reset width to FIRST column width, not last column from previous page.
|
|
354
|
+
let firstColumnWidth = snakingSnapshot.columnWidths ? snakingSnapshot.columnWidths[0] : (this.lastColumnWidth || this.availableWidth);
|
|
355
|
+
|
|
356
|
+
// Clean up overflowed snapshots
|
|
357
|
+
while (this.snapshots.length > 1 && this.snapshots[this.snapshots.length - 1].overflowed) {
|
|
358
|
+
this.snapshots.pop();
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Preserve margins opened inside the column group, including list indentation.
|
|
362
|
+
let columnX = this.pageMargins.left + (snakingSnapshot.columnOffset ?? (this.marginXTopParent ? this.marginXTopParent[0] : 0));
|
|
363
|
+
let marginLeft = this.activeMarginLeft - (snakingSnapshot.activeMarginLeft || 0);
|
|
364
|
+
let marginRight = this.activeMarginRight - (snakingSnapshot.activeMarginRight || 0);
|
|
365
|
+
this.x = columnX + marginLeft;
|
|
366
|
+
this.availableWidth = firstColumnWidth - marginLeft - marginRight;
|
|
367
|
+
this.lastColumnWidth = firstColumnWidth;
|
|
368
|
+
|
|
369
|
+
// Sync snapshots in this flow to the new page state, preserving detached parents.
|
|
370
|
+
// Inner non-snaking groups also need the new page coordinates.
|
|
371
|
+
// This ensures nested structures (like inner product/price columns)
|
|
372
|
+
// don't retain stale values that would cause layout corruption.
|
|
373
|
+
for (let i = this.snapshots.length - 1; i >= 0; i--) {
|
|
374
|
+
let snapshot = this.snapshots[i];
|
|
375
|
+
if (snapshot.isDetachedBlock) {
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
let isSnakingSnapshot = !!snapshot.snakingColumns;
|
|
379
|
+
snapshot.x = columnX + (snapshot.activeMarginLeft || 0) - (snakingSnapshot.activeMarginLeft || 0);
|
|
380
|
+
snapshot.y = isSnakingSnapshot ? pageTopY : this.y;
|
|
381
|
+
snapshot.availableHeight = isSnakingSnapshot ? pageInnerHeight : this.availableHeight;
|
|
382
|
+
snapshot.page = this.page;
|
|
383
|
+
if (snapshot.bottomMost) {
|
|
384
|
+
snapshot.bottomMost.x = snapshot.x;
|
|
385
|
+
snapshot.bottomMost.y = isSnakingSnapshot ? pageTopY : this.y;
|
|
386
|
+
snapshot.bottomMost.availableHeight = isSnakingSnapshot ? pageInnerHeight : this.availableHeight;
|
|
387
|
+
snapshot.bottomMost.page = this.page;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
addMargin(left, right) {
|
|
393
|
+
this.activeMarginLeft += left;
|
|
394
|
+
this.activeMarginRight += right || 0;
|
|
395
|
+
this.x += left;
|
|
396
|
+
this.availableWidth -= left + (right || 0);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
moveDown(offset) {
|
|
400
|
+
this.y += offset;
|
|
401
|
+
this.availableHeight -= offset;
|
|
402
|
+
|
|
403
|
+
return this.availableHeight > 0;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
initializePage() {
|
|
407
|
+
this.y = this.pageMargins.top;
|
|
408
|
+
this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
|
|
409
|
+
const { pageCtx, isSnapshot } = this.pageSnapshot();
|
|
410
|
+
pageCtx.availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right;
|
|
411
|
+
if (isSnapshot && this.marginXTopParent) {
|
|
412
|
+
pageCtx.availableWidth -= this.marginXTopParent[0];
|
|
413
|
+
pageCtx.availableWidth -= this.marginXTopParent[1];
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
pageSnapshot() {
|
|
418
|
+
if (this.snapshots[0]) {
|
|
419
|
+
return { pageCtx: this.snapshots[0], isSnapshot: true };
|
|
420
|
+
} else {
|
|
421
|
+
return { pageCtx: this, isSnapshot: false };
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
moveTo(x, y) {
|
|
426
|
+
const context = this.absolutePositionContext || this;
|
|
427
|
+
if (x !== undefined && x !== null) {
|
|
428
|
+
this.x = x;
|
|
429
|
+
this.availableWidth = context.getCurrentPage().pageSize.width - this.x - context.pageMargins.right;
|
|
430
|
+
}
|
|
431
|
+
if (y !== undefined && y !== null) {
|
|
432
|
+
this.y = y;
|
|
433
|
+
this.availableHeight = context.getCurrentPage().pageSize.height - this.y - context.pageMargins.bottom;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
moveToRelative(x, y) {
|
|
438
|
+
if (x !== undefined && x !== null) {
|
|
439
|
+
this.x = this.x + x;
|
|
440
|
+
}
|
|
441
|
+
if (y !== undefined && y !== null) {
|
|
442
|
+
this.y = this.y + y;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
beginDetachedBlock() {
|
|
447
|
+
this.snapshots.push({
|
|
448
|
+
isDetachedBlock: true,
|
|
449
|
+
detachedBlock: this.detachedBlock,
|
|
450
|
+
x: this.x,
|
|
451
|
+
y: this.y,
|
|
452
|
+
availableHeight: this.availableHeight,
|
|
453
|
+
availableWidth: this.availableWidth,
|
|
454
|
+
page: this.page,
|
|
455
|
+
lastColumnWidth: this.lastColumnWidth
|
|
456
|
+
});
|
|
457
|
+
this.detachedBlock = {};
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
endDetachedBlock() {
|
|
461
|
+
let saved = this.snapshots.pop();
|
|
462
|
+
|
|
463
|
+
this.detachedBlock = saved.detachedBlock;
|
|
464
|
+
this.x = saved.x;
|
|
465
|
+
this.y = saved.y;
|
|
466
|
+
this.availableWidth = saved.availableWidth;
|
|
467
|
+
this.availableHeight = saved.availableHeight;
|
|
468
|
+
this.page = saved.page;
|
|
469
|
+
this.lastColumnWidth = saved.lastColumnWidth;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
moveToNextPage(pageOrientation) {
|
|
473
|
+
let nextPageIndex = this.page + 1;
|
|
474
|
+
let prevPage = this.page;
|
|
475
|
+
let prevY = this.y;
|
|
476
|
+
|
|
477
|
+
// If we are in a column group
|
|
478
|
+
if (this.snapshots.length > 0) {
|
|
479
|
+
let lastSnapshot = this.snapshots[this.snapshots.length - 1];
|
|
480
|
+
// We have to update prevY accordingly by also taking into consideration
|
|
481
|
+
// the 'y' of cells that don't break page
|
|
482
|
+
if (lastSnapshot.bottomMost && lastSnapshot.bottomMost.y) {
|
|
483
|
+
prevY = Math.max(this.y, lastSnapshot.bottomMost.y);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
let createNewPage = nextPageIndex >= this.pages.length;
|
|
488
|
+
if (createNewPage) {
|
|
489
|
+
let currentAvailableWidth = this.availableWidth;
|
|
490
|
+
let currentPageOrientation = this.getCurrentPage().pageSize.orientation;
|
|
491
|
+
|
|
492
|
+
let pageSize = getPageSize(this.getCurrentPage(), pageOrientation);
|
|
493
|
+
this.addPage(pageSize, null, this.getCurrentPage().customProperties);
|
|
494
|
+
|
|
495
|
+
if (currentPageOrientation === pageSize.orientation) {
|
|
496
|
+
this.availableWidth = currentAvailableWidth;
|
|
497
|
+
}
|
|
498
|
+
} else {
|
|
499
|
+
this.page = nextPageIndex;
|
|
500
|
+
this.initializePage();
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
return {
|
|
504
|
+
newPageCreated: createNewPage,
|
|
505
|
+
prevPage: prevPage,
|
|
506
|
+
prevY: prevY,
|
|
507
|
+
y: this.y
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
addPage(pageSize, pageMargin = null, customProperties = {}) {
|
|
512
|
+
if (pageMargin !== null) {
|
|
513
|
+
this.pageMargins = pageMargin;
|
|
514
|
+
this.x = pageMargin.left;
|
|
515
|
+
this.availableWidth = pageSize.width - pageMargin.left - pageMargin.right;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
let page = { items: [], pageSize: { ...pageSize }, pageMargins: this.pageMargins, customProperties: customProperties };
|
|
519
|
+
this.pages.push(page);
|
|
520
|
+
this.backgroundLength.push(0);
|
|
521
|
+
this.page = this.pages.length - 1;
|
|
522
|
+
this.initializePage();
|
|
523
|
+
|
|
524
|
+
this.emit('pageAdded', page);
|
|
525
|
+
|
|
526
|
+
return page;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
getCurrentPage() {
|
|
530
|
+
if (this.page < 0 || this.page >= this.pages.length) {
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
return this.pages[this.page];
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
getCurrentPosition() {
|
|
538
|
+
let pageSize = this.getCurrentPage().pageSize;
|
|
539
|
+
let innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
|
|
540
|
+
let innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right;
|
|
541
|
+
|
|
542
|
+
return {
|
|
543
|
+
pageNumber: this.page + 1,
|
|
544
|
+
pageOrientation: pageSize.orientation,
|
|
545
|
+
pageInnerHeight: innerHeight,
|
|
546
|
+
pageInnerWidth: innerWidth,
|
|
547
|
+
left: this.x,
|
|
548
|
+
top: this.y,
|
|
549
|
+
verticalRatio: ((this.y - this.pageMargins.top) / innerHeight),
|
|
550
|
+
horizontalRatio: ((this.x - this.pageMargins.left) / innerWidth)
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function pageOrientation(pageOrientationString, currentPageOrientation) {
|
|
556
|
+
if (pageOrientationString === undefined) {
|
|
557
|
+
return currentPageOrientation;
|
|
558
|
+
} else if (isString(pageOrientationString) && (pageOrientationString.toLowerCase() === 'landscape')) {
|
|
559
|
+
return 'landscape';
|
|
560
|
+
} else {
|
|
561
|
+
return 'portrait';
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const getPageSize = (currentPage, newPageOrientation) => {
|
|
566
|
+
|
|
567
|
+
newPageOrientation = pageOrientation(newPageOrientation, currentPage.pageSize.orientation);
|
|
568
|
+
|
|
569
|
+
if (newPageOrientation !== currentPage.pageSize.orientation) {
|
|
570
|
+
return {
|
|
571
|
+
orientation: newPageOrientation,
|
|
572
|
+
width: currentPage.pageSize.height,
|
|
573
|
+
height: currentPage.pageSize.width
|
|
574
|
+
};
|
|
575
|
+
} else {
|
|
576
|
+
return {
|
|
577
|
+
orientation: currentPage.pageSize.orientation,
|
|
578
|
+
width: currentPage.pageSize.width,
|
|
579
|
+
height: currentPage.pageSize.height
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
function bottomMostContext(c1, c2) {
|
|
586
|
+
let r;
|
|
587
|
+
|
|
588
|
+
if (c1.page > c2.page) {
|
|
589
|
+
r = c1;
|
|
590
|
+
} else if (c2.page > c1.page) {
|
|
591
|
+
r = c2;
|
|
592
|
+
} else {
|
|
593
|
+
r = (c1.y > c2.y) ? c1 : c2;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
return {
|
|
597
|
+
page: r.page,
|
|
598
|
+
x: r.x,
|
|
599
|
+
y: r.y,
|
|
600
|
+
availableHeight: r.availableHeight,
|
|
601
|
+
availableWidth: r.availableWidth
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
export default DocumentContext;
|