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