@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,658 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _columnCalculator = _interopRequireDefault(require("./columnCalculator"));
6
+ var _variableType = require("./helpers/variableType");
7
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
+ const PAGE_BREAK_VALUES = new Set(['before', 'beforeOdd', 'beforeEven', 'after', 'afterOdd', 'afterEven']);
9
+ const hasExplicitPageBreak = cell => {
10
+ if (!cell || typeof cell !== 'object') {
11
+ return false;
12
+ }
13
+ return PAGE_BREAK_VALUES.has(cell.pageBreak);
14
+ };
15
+ class TableProcessor {
16
+ constructor(tableNode, breakableBlocks = new Set()) {
17
+ this.tableNode = tableNode;
18
+ this.breakableBlocks = breakableBlocks;
19
+ this._isCurrentRowUnbreakable = false;
20
+ }
21
+ beginTable(writer) {
22
+ const getTableInnerContentWidth = () => {
23
+ let width = 0;
24
+ tableNode.table.widths.forEach(w => {
25
+ width += w._calcWidth;
26
+ });
27
+ return width;
28
+ };
29
+ const prepareRowSpanData = () => {
30
+ let rsd = [];
31
+ let x = 0;
32
+ let lastWidth;
33
+ rsd.push({
34
+ left: 0,
35
+ rowSpan: 0
36
+ });
37
+ for (let i = 0, l = this.tableNode.table.body[0].length; i < l; i++) {
38
+ let paddings = this.layout.paddingLeft(i, this.tableNode) + this.layout.paddingRight(i, this.tableNode);
39
+ let lBorder = this.layout.vLineWidth(i, this.tableNode);
40
+ lastWidth = paddings + lBorder + this.tableNode.table.widths[i]._calcWidth;
41
+ rsd[rsd.length - 1].width = lastWidth;
42
+ x += lastWidth;
43
+ rsd.push({
44
+ left: x,
45
+ rowSpan: 0,
46
+ width: 0
47
+ });
48
+ }
49
+ return rsd;
50
+ };
51
+
52
+ // Iterate through all cells. If the current cell is the start of a
53
+ // rowSpan/colSpan, update the border property of the cells on its
54
+ // bottom/right accordingly. This is needed since each iteration of the
55
+ // line-drawing loops draws lines for a single cell, not for an entire
56
+ // rowSpan/colSpan.
57
+ const prepareCellBorders = body => {
58
+ for (let rowIndex = 0; rowIndex < body.length; rowIndex++) {
59
+ let row = body[rowIndex];
60
+ for (let colIndex = 0; colIndex < row.length; colIndex++) {
61
+ let cell = row[colIndex];
62
+ if (cell.border) {
63
+ let rowSpan = cell.rowSpan || 1;
64
+ let colSpan = cell.colSpan || 1;
65
+ for (let rowOffset = 0; rowOffset < rowSpan; rowOffset++) {
66
+ // set left border
67
+ if (cell.border[0] !== undefined && rowOffset > 0) {
68
+ setBorder(rowIndex + rowOffset, colIndex, 0, cell.border[0]);
69
+ }
70
+
71
+ // set right border
72
+ if (cell.border[2] !== undefined) {
73
+ setBorder(rowIndex + rowOffset, colIndex + colSpan - 1, 2, cell.border[2]);
74
+ }
75
+ }
76
+ for (let colOffset = 0; colOffset < colSpan; colOffset++) {
77
+ // set top border
78
+ if (cell.border[1] !== undefined && colOffset > 0) {
79
+ setBorder(rowIndex, colIndex + colOffset, 1, cell.border[1]);
80
+ }
81
+
82
+ // set bottom border
83
+ if (cell.border[3] !== undefined) {
84
+ setBorder(rowIndex + rowSpan - 1, colIndex + colOffset, 3, cell.border[3]);
85
+ }
86
+ }
87
+ }
88
+ }
89
+ }
90
+
91
+ // helper function to set the border for a given cell
92
+ function setBorder(rowIndex, colIndex, borderIndex, borderValue) {
93
+ let cell = body[rowIndex][colIndex];
94
+ cell.border = cell.border || {};
95
+ cell.border[borderIndex] = borderValue;
96
+ }
97
+ };
98
+ let tableNode;
99
+ let availableWidth;
100
+ tableNode = this.tableNode;
101
+ this.offsets = tableNode._offsets;
102
+ this.layout = tableNode._layout;
103
+ availableWidth = writer.context().availableWidth - this.offsets.total;
104
+ _columnCalculator.default.buildColumnWidths(tableNode.table.widths, availableWidth, this.offsets.total, tableNode);
105
+ this.tableWidth = tableNode._offsets.total + getTableInnerContentWidth();
106
+ this.alignTable(writer.context());
107
+ this.rowSpanData = prepareRowSpanData();
108
+ this.cleanUpRepeatables = false;
109
+
110
+ // headersRows and rowsWithoutPageBreak (headerRows + keepWithHeaderRows)
111
+ this.headerRows = 0;
112
+ this.rowsWithoutPageBreak = 0;
113
+ const headerRows = tableNode.table.headerRows;
114
+ if ((0, _variableType.isPositiveInteger)(headerRows)) {
115
+ this.headerRows = headerRows;
116
+ if (this.headerRows > tableNode.table.body.length) {
117
+ throw new Error(`Too few rows in the table. Property headerRows requires at least ${this.headerRows}, contains only ${tableNode.table.body.length}`);
118
+ }
119
+ this.rowsWithoutPageBreak = this.headerRows;
120
+ const keepWithHeaderRows = tableNode.table.keepWithHeaderRows;
121
+ if ((0, _variableType.isPositiveInteger)(keepWithHeaderRows) && !this.breakableBlocks.has(tableNode.table)) {
122
+ this.rowsWithoutPageBreak += keepWithHeaderRows;
123
+ }
124
+ }
125
+ this.dontBreakRows = !!tableNode.table.dontBreakRows && !this.breakableBlocks.has(tableNode.table.body[0]);
126
+ this.headerInUnbreakableBlock = writer.transactionLevel > 0;
127
+ if (this.rowsWithoutPageBreak || this.dontBreakRows) {
128
+ writer.beginUnbreakableBlock();
129
+ // Draw the top border of the table
130
+ this.drawHorizontalLine(0, writer);
131
+ if (this.rowsWithoutPageBreak && this.dontBreakRows) {
132
+ // We just increase the value of transactionLevel
133
+ writer.beginUnbreakableBlock();
134
+ }
135
+ }
136
+
137
+ // update the border properties of all cells before drawing any lines
138
+ prepareCellBorders(this.tableNode.table.body);
139
+ }
140
+ alignTable(context) {
141
+ const freeWidth = Math.max(0, context.availableWidth - this.tableWidth);
142
+ this.alignmentOffset = this.tableNode.tableAlignment === 'center' ? freeWidth / 2 : this.tableNode.tableAlignment === 'right' ? freeWidth : 0;
143
+ if (this.alignmentOffset) {
144
+ // Keep the container width intact: column widths have already been resolved.
145
+ // Row snapshots preserve this origin across page breaks.
146
+ context.addMargin(this.alignmentOffset, -this.alignmentOffset);
147
+ }
148
+ }
149
+ onRowBreak(rowIndex, writer) {
150
+ const detachedBlock = writer.context().detachedBlock;
151
+ return () => {
152
+ if (writer.context().detachedBlock !== detachedBlock) {
153
+ return;
154
+ }
155
+ let offset = this.rowPaddingTop + (!this.headerRows ? this.topLineWidth : 0);
156
+ writer.context().availableHeight -= this.reservedAtBottom;
157
+ writer.context().moveDown(offset);
158
+ };
159
+ }
160
+ beginRow(rowIndex, writer) {
161
+ this.dontBreakRows = !!this.tableNode.table.dontBreakRows && !this.breakableBlocks.has(this.tableNode.table.body[rowIndex]);
162
+ this.topLineWidth = this.layout.hLineWidth(rowIndex, this.tableNode);
163
+ this.rowPaddingTop = this.layout.paddingTop(rowIndex, this.tableNode);
164
+ this.bottomLineWidth = this.layout.hLineWidth(rowIndex + 1, this.tableNode);
165
+ this.rowPaddingBottom = this.layout.paddingBottom(rowIndex, this.tableNode);
166
+ this.rowCallback = this.onRowBreak(rowIndex, writer);
167
+ writer.addListener('pageChanged', this.rowCallback);
168
+ if (rowIndex == 0 && !this.dontBreakRows && !this.rowsWithoutPageBreak) {
169
+ // We store the 'y' to draw later and if necessary the top border of the table
170
+ this._tableTopBorderY = writer.context().y;
171
+ writer.context().moveDown(this.topLineWidth);
172
+ }
173
+ this.rowTopPageY = writer.context().y + this.rowPaddingTop;
174
+ const rowCells = this.tableNode.table.body[rowIndex] || [];
175
+ const rowHasPageBreak = rowCells.some(hasExplicitPageBreak);
176
+ if (this.dontBreakRows && rowHasPageBreak) {
177
+ writer.checkUnbreakableOverflow(true);
178
+ }
179
+ this._isCurrentRowUnbreakable = this.dontBreakRows && rowIndex > 0 && !rowHasPageBreak;
180
+ if (this._isCurrentRowUnbreakable) {
181
+ writer.beginUnbreakableBlock();
182
+ }
183
+ this.rowTopY = writer.context().y;
184
+ this.reservedAtBottom = this.bottomLineWidth + this.rowPaddingBottom;
185
+ writer.context().availableHeight -= this.reservedAtBottom;
186
+ writer.context().moveDown(this.rowPaddingTop);
187
+ }
188
+ drawHorizontalLine(lineIndex, writer, overrideY, moveDown = true, forcePage) {
189
+ let lineWidth = this.layout.hLineWidth(lineIndex, this.tableNode);
190
+ if (lineWidth) {
191
+ let style = this.layout.hLineStyle(lineIndex, this.tableNode);
192
+ let dash;
193
+ if (style && style.dash) {
194
+ dash = style.dash;
195
+ }
196
+ let offset = lineWidth / 2;
197
+ let currentLine = null;
198
+ let body = this.tableNode.table.body;
199
+ let cellAbove;
200
+ let currentCell;
201
+ let rowCellAbove;
202
+ for (let i = 0, l = this.rowSpanData.length; i < l; i++) {
203
+ let data = this.rowSpanData[i];
204
+ let shouldDrawLine = !data.rowSpan;
205
+ let borderColor = null;
206
+
207
+ // draw only if the current cell requires a top border or the cell in the
208
+ // row above requires a bottom border
209
+ if (shouldDrawLine && i < l - 1) {
210
+ var topBorder = false,
211
+ bottomBorder = false,
212
+ rowBottomBorder = false;
213
+
214
+ // the cell in the row above
215
+ if (lineIndex > 0) {
216
+ cellAbove = body[lineIndex - 1][i];
217
+ bottomBorder = cellAbove.border ? cellAbove.border[3] : this.layout.defaultBorder;
218
+ if (bottomBorder && cellAbove.borderColor) {
219
+ borderColor = cellAbove.borderColor[3];
220
+ }
221
+ }
222
+
223
+ // the current cell
224
+ if (lineIndex < body.length) {
225
+ currentCell = body[lineIndex][i];
226
+ topBorder = currentCell.border ? currentCell.border[1] : this.layout.defaultBorder;
227
+ if (topBorder && borderColor == null && currentCell.borderColor) {
228
+ borderColor = currentCell.borderColor[1];
229
+ }
230
+ }
231
+ shouldDrawLine = topBorder || bottomBorder;
232
+ }
233
+ if (cellAbove && cellAbove._rowSpanCurrentOffset) {
234
+ rowCellAbove = body[lineIndex - 1 - cellAbove._rowSpanCurrentOffset][i];
235
+ rowBottomBorder = rowCellAbove && rowCellAbove.border ? rowCellAbove.border[3] : this.layout.defaultBorder;
236
+ if (rowBottomBorder && rowCellAbove && rowCellAbove.borderColor) {
237
+ borderColor = rowCellAbove.borderColor[3];
238
+ }
239
+ }
240
+ if (borderColor == null) {
241
+ borderColor = typeof this.layout.hLineColor === 'function' ? this.layout.hLineColor(lineIndex, this.tableNode, i) : this.layout.hLineColor;
242
+ }
243
+ if (!currentLine && shouldDrawLine) {
244
+ currentLine = {
245
+ left: data.left,
246
+ width: 0
247
+ };
248
+ }
249
+ if (shouldDrawLine) {
250
+ let colSpanIndex = 0;
251
+ if (rowCellAbove && rowCellAbove.colSpan && rowBottomBorder) {
252
+ while (rowCellAbove.colSpan > colSpanIndex) {
253
+ currentLine.width += this.rowSpanData[i + colSpanIndex++].width || 0;
254
+ }
255
+ i += colSpanIndex - 1;
256
+ } else if (cellAbove && cellAbove.colSpan && bottomBorder) {
257
+ while (cellAbove.colSpan > colSpanIndex) {
258
+ currentLine.width += this.rowSpanData[i + colSpanIndex++].width || 0;
259
+ }
260
+ i += colSpanIndex - 1;
261
+ } else if (currentCell && currentCell.colSpan && topBorder) {
262
+ while (currentCell.colSpan > colSpanIndex) {
263
+ currentLine.width += this.rowSpanData[i + colSpanIndex++].width || 0;
264
+ }
265
+ i += colSpanIndex - 1;
266
+ } else {
267
+ currentLine.width += this.rowSpanData[i].width || 0;
268
+ }
269
+ }
270
+ let y = (overrideY || 0) + offset;
271
+ if (shouldDrawLine) {
272
+ if (currentLine && currentLine.width) {
273
+ writer.addVector({
274
+ type: 'line',
275
+ x1: currentLine.left,
276
+ x2: currentLine.left + currentLine.width,
277
+ y1: y,
278
+ y2: y,
279
+ lineWidth: lineWidth,
280
+ dash: dash,
281
+ lineColor: borderColor
282
+ }, false, (0, _variableType.isNumber)(overrideY), null, forcePage);
283
+ currentLine = null;
284
+ cellAbove = null;
285
+ currentCell = null;
286
+ rowCellAbove = null;
287
+ }
288
+ }
289
+ }
290
+ if (moveDown) {
291
+ writer.context().moveDown(lineWidth);
292
+ }
293
+ }
294
+ }
295
+ drawVerticalLine(x, y0, y1, vLineColIndex, writer, vLineRowIndex, beforeVLineColIndex) {
296
+ let width = this.layout.vLineWidth(vLineColIndex, this.tableNode);
297
+ if (width === 0) {
298
+ return;
299
+ }
300
+ let style = this.layout.vLineStyle(vLineColIndex, this.tableNode);
301
+ let dash;
302
+ if (style && style.dash) {
303
+ dash = style.dash;
304
+ }
305
+ let body = this.tableNode.table.body;
306
+ let cellBefore;
307
+ let currentCell;
308
+ let borderColor;
309
+
310
+ // the cell in the col before
311
+ if (vLineColIndex > 0) {
312
+ cellBefore = body[vLineRowIndex][beforeVLineColIndex];
313
+ if (cellBefore && cellBefore.borderColor) {
314
+ if (cellBefore.border ? cellBefore.border[2] : this.layout.defaultBorder) {
315
+ borderColor = cellBefore.borderColor[2];
316
+ }
317
+ }
318
+ }
319
+ if (borderColor == null && cellBefore && cellBefore._rowSpanCurrentOffset) {
320
+ let rowCellBeforeAbove = body[vLineRowIndex - cellBefore._rowSpanCurrentOffset][beforeVLineColIndex];
321
+ if (rowCellBeforeAbove.borderColor) {
322
+ if (rowCellBeforeAbove.border ? rowCellBeforeAbove.border[2] : this.layout.defaultBorder) {
323
+ borderColor = rowCellBeforeAbove.borderColor[2];
324
+ }
325
+ }
326
+ }
327
+
328
+ // the current cell
329
+ if (borderColor == null && vLineColIndex < body[vLineRowIndex].length) {
330
+ currentCell = body[vLineRowIndex][vLineColIndex];
331
+ if (currentCell && currentCell.borderColor) {
332
+ if (currentCell.border ? currentCell.border[0] : this.layout.defaultBorder) {
333
+ borderColor = currentCell.borderColor[0];
334
+ }
335
+ }
336
+ }
337
+ if (borderColor == null && currentCell && currentCell._rowSpanCurrentOffset) {
338
+ let rowCurrentCellAbove = body[vLineRowIndex - currentCell._rowSpanCurrentOffset][vLineColIndex];
339
+ if (rowCurrentCellAbove.borderColor) {
340
+ if (rowCurrentCellAbove.border ? rowCurrentCellAbove.border[0] : this.layout.defaultBorder) {
341
+ borderColor = rowCurrentCellAbove.borderColor[0];
342
+ }
343
+ }
344
+ }
345
+ if (borderColor == null) {
346
+ borderColor = typeof this.layout.vLineColor === 'function' ? this.layout.vLineColor(vLineColIndex, this.tableNode, vLineRowIndex) : this.layout.vLineColor;
347
+ }
348
+ writer.addVector({
349
+ type: 'line',
350
+ x1: x + width / 2,
351
+ x2: x + width / 2,
352
+ y1: y0,
353
+ y2: y1,
354
+ lineWidth: width,
355
+ dash: dash,
356
+ lineColor: borderColor
357
+ }, false, true);
358
+ }
359
+ endTable(writer) {
360
+ if (this.cleanUpRepeatables) {
361
+ writer.popFromRepeatables();
362
+ }
363
+ if (this.alignmentOffset) {
364
+ writer.context().addMargin(-this.alignmentOffset, this.alignmentOffset);
365
+ }
366
+ }
367
+ releaseRowSpans(rowIndex) {
368
+ const body = this.tableNode.table.body;
369
+ if (!this.rowSpanGroups) {
370
+ // Index connected spans once per table layout, including transitive overlaps.
371
+ this.rowSpanGroups = new Array(body.length);
372
+ for (let firstRow = 0; firstRow < body.length;) {
373
+ let lastRow = firstRow;
374
+ for (let i = firstRow; i <= lastRow; i++) {
375
+ body[i].forEach(cell => {
376
+ lastRow = Math.max(lastRow, i + (cell.rowSpan || 1) - 1);
377
+ });
378
+ }
379
+ const group = {
380
+ firstRow,
381
+ lastRow
382
+ };
383
+ for (let i = firstRow; i <= lastRow; i++) {
384
+ this.rowSpanGroups[i] = group;
385
+ }
386
+ firstRow = lastRow + 1;
387
+ }
388
+ }
389
+ const {
390
+ firstRow,
391
+ lastRow
392
+ } = this.rowSpanGroups[rowIndex];
393
+ if (firstRow === lastRow) {
394
+ return;
395
+ }
396
+ for (let i = firstRow; i <= lastRow; i++) {
397
+ this.breakableBlocks.add(body[i]);
398
+ // Discard temporary contexts only within this connected span group.
399
+ body[i].forEach(cell => delete cell._columnEndingContext);
400
+ }
401
+ delete this.tableNode._breaksBySpan;
402
+ delete this.tableNode._bottomByPage;
403
+ }
404
+ endRow(rowIndex, writer, pageBreaks) {
405
+ const getLineXs = () => {
406
+ let result = [];
407
+ let cols = 0;
408
+ for (let i = 0, l = this.tableNode.table.body[rowIndex].length; i < l; i++) {
409
+ if (!cols) {
410
+ result.push({
411
+ x: this.rowSpanData[i].left,
412
+ index: i
413
+ });
414
+ let item = this.tableNode.table.body[rowIndex][i];
415
+ cols = item._colSpan || item.colSpan || 0;
416
+ }
417
+ if (cols > 0) {
418
+ cols--;
419
+ }
420
+ }
421
+ result.push({
422
+ x: this.rowSpanData[this.rowSpanData.length - 1].left,
423
+ index: this.rowSpanData.length - 1
424
+ });
425
+ return result;
426
+ };
427
+ writer.removeListener('pageChanged', this.rowCallback);
428
+ writer.context().moveDown(this.layout.paddingBottom(rowIndex, this.tableNode));
429
+ writer.context().availableHeight += this.reservedAtBottom;
430
+ let endingPage = writer.context().page;
431
+ let endingY = writer.context().y;
432
+ let xs = getLineXs();
433
+ let ys = [];
434
+ let hasBreaks = pageBreaks && pageBreaks.length > 0;
435
+ let body = this.tableNode.table.body;
436
+ ys.push({
437
+ y0: this.rowTopY,
438
+ page: hasBreaks ? pageBreaks[0].prevPage : endingPage
439
+ });
440
+ if (hasBreaks) {
441
+ for (let i = 0, l = pageBreaks.length; i < l; i++) {
442
+ let pageBreak = pageBreaks[i];
443
+ ys[ys.length - 1].y1 = pageBreak.prevY;
444
+ ys.push({
445
+ y0: pageBreak.y,
446
+ page: pageBreak.prevPage + 1
447
+ });
448
+ }
449
+ }
450
+ ys[ys.length - 1].y1 = endingY;
451
+ let skipOrphanePadding = ys[0].y1 - ys[0].y0 === this.rowPaddingTop;
452
+ if (rowIndex === 0 && !skipOrphanePadding && !this.rowsWithoutPageBreak && !this.dontBreakRows) {
453
+ // Draw the top border of the table
454
+ let pageTableStartedAt = null;
455
+ if (pageBreaks && pageBreaks.length > 0) {
456
+ // Get the page where table started at
457
+ pageTableStartedAt = pageBreaks[0].prevPage;
458
+ }
459
+ this.drawHorizontalLine(0, writer, this._tableTopBorderY, false, pageTableStartedAt);
460
+ }
461
+ for (let yi = skipOrphanePadding ? 1 : 0, yl = ys.length; yi < yl; yi++) {
462
+ let willBreak = yi < ys.length - 1;
463
+ let rowBreakWithoutHeader = yi > 0 && !this.headerRows;
464
+ let hzLineOffset = rowBreakWithoutHeader ? 0 : this.topLineWidth;
465
+ let y1 = ys[yi].y0;
466
+ let y2 = ys[yi].y1;
467
+ if (willBreak) {
468
+ y2 = y2 + this.rowPaddingBottom;
469
+ }
470
+ if (writer.context().page != ys[yi].page) {
471
+ writer.context().page = ys[yi].page;
472
+
473
+ //TODO: buggy, availableHeight should be updated on every pageChanged event
474
+ // TableProcessor should be pageChanged listener, instead of processRow
475
+ this.reservedAtBottom = 0;
476
+ }
477
+
478
+ // Draw horizontal lines before the vertical lines so they are not overridden
479
+ if (willBreak && this.layout.hLineWhenBroken !== false) {
480
+ this.drawHorizontalLine(rowIndex + 1, writer, y2);
481
+ }
482
+ if (rowBreakWithoutHeader && this.layout.hLineWhenBroken !== false) {
483
+ this.drawHorizontalLine(rowIndex, writer, y1);
484
+ }
485
+ let alignmentBottomPosition;
486
+ for (let i = 0, l = xs.length; i < l; i++) {
487
+ let leftCellBorder = false;
488
+ let rightCellBorder = false;
489
+ let colIndex = xs[i].index;
490
+
491
+ // current cell
492
+ if (colIndex < body[rowIndex].length) {
493
+ let cell = body[rowIndex][colIndex];
494
+ leftCellBorder = cell.border ? cell.border[0] : this.layout.defaultBorder;
495
+ rightCellBorder = cell.border ? cell.border[2] : this.layout.defaultBorder;
496
+ }
497
+
498
+ // before cell
499
+ if (colIndex > 0 && !leftCellBorder) {
500
+ let cell = body[rowIndex][colIndex - 1];
501
+ leftCellBorder = cell.border ? cell.border[2] : this.layout.defaultBorder;
502
+ }
503
+
504
+ // after cell
505
+ if (colIndex + 1 < body[rowIndex].length && !rightCellBorder) {
506
+ let cell = body[rowIndex][colIndex + 1];
507
+ rightCellBorder = cell.border ? cell.border[0] : this.layout.defaultBorder;
508
+ }
509
+ if (leftCellBorder) {
510
+ this.drawVerticalLine(xs[i].x, y1 - hzLineOffset, y2 + this.bottomLineWidth, xs[i].index, writer, rowIndex, xs[i - 1] ? xs[i - 1].index : null);
511
+ }
512
+ if (i < l - 1) {
513
+ body[rowIndex][colIndex]._willBreak = body[rowIndex][colIndex]._willBreak ?? willBreak;
514
+ let bottomY = this.dontBreakRows ? y2 + this.bottomLineWidth : y2 + this.bottomLineWidth / 2;
515
+ if (willBreak || this.dontBreakRows) {
516
+ bottomY = bottomY - this.rowPaddingBottom;
517
+ }
518
+ bottomY -= this.reservedAtBottom;
519
+ if (body[rowIndex][colIndex]._bottomY === undefined) {
520
+ body[rowIndex][colIndex]._bottomY = bottomY;
521
+ }
522
+ const spanOffset = body[rowIndex][colIndex]._rowSpanCurrentOffset || 0;
523
+ const firstPage = body[rowIndex - spanOffset][colIndex]._verticalAlignmentFirstPage;
524
+ if (firstPage) {
525
+ firstPage.hasPageBreak = firstPage.hasPageBreak || hasBreaks;
526
+ if (!alignmentBottomPosition) {
527
+ alignmentBottomPosition = writer.getCurrentPositionOnPage();
528
+ // Whole rows end before their bottom padding; split rows use the break boundary.
529
+ alignmentBottomPosition.top = this.dontBreakRows && !hasBreaks ? y2 - this.rowPaddingBottom : bottomY;
530
+ }
531
+ firstPage.ends.push(alignmentBottomPosition);
532
+ }
533
+ body[rowIndex][colIndex]._rowTopPageY = this.rowTopPageY;
534
+ if (this.dontBreakRows) {
535
+ body[rowIndex][colIndex]._rowTopPageYPadding = this.rowPaddingTop;
536
+ }
537
+ body[rowIndex][colIndex]._lastPageNumber = ys[yi].page + 1;
538
+ let fillColor = body[rowIndex][colIndex]._fillColor;
539
+ let fillOpacity = body[rowIndex][colIndex]._fillOpacity;
540
+ if (!fillColor) {
541
+ fillColor = typeof this.layout.fillColor === 'function' ? this.layout.fillColor(rowIndex, this.tableNode, colIndex) : this.layout.fillColor;
542
+ }
543
+ if (!(0, _variableType.isNumber)(fillOpacity)) {
544
+ fillOpacity = typeof this.layout.fillOpacity === 'function' ? this.layout.fillOpacity(rowIndex, this.tableNode, colIndex) : this.layout.fillOpacity;
545
+ }
546
+ var overlayPattern = body[rowIndex][colIndex].overlayPattern;
547
+ var overlayOpacity = body[rowIndex][colIndex].overlayOpacity;
548
+ if (fillColor || overlayPattern) {
549
+ let widthLeftBorder = leftCellBorder ? this.layout.vLineWidth(colIndex, this.tableNode) : 0;
550
+ let widthRightBorder;
551
+ if ((colIndex === 0 || colIndex + 1 == body[rowIndex].length) && !rightCellBorder) {
552
+ widthRightBorder = this.layout.vLineWidth(colIndex + 1, this.tableNode);
553
+ } else if (rightCellBorder) {
554
+ widthRightBorder = this.layout.vLineWidth(colIndex + 1, this.tableNode) / 2;
555
+ } else {
556
+ widthRightBorder = 0;
557
+ }
558
+ let x1f = this.dontBreakRows ? xs[i].x + widthLeftBorder : xs[i].x + widthLeftBorder / 2;
559
+ let y1f = this.dontBreakRows ? y1 : y1 - hzLineOffset / 2;
560
+ let x2f = xs[i + 1].x + widthRightBorder;
561
+ let y2f = this.dontBreakRows ? y2 + this.bottomLineWidth : y2 + this.bottomLineWidth / 2;
562
+ var bgWidth = x2f - x1f;
563
+ var bgHeight = y2f - y1f;
564
+ if (fillColor) {
565
+ writer.addVector({
566
+ type: 'rect',
567
+ x: x1f,
568
+ y: y1f,
569
+ w: bgWidth,
570
+ h: bgHeight,
571
+ lineWidth: 0,
572
+ color: fillColor,
573
+ fillOpacity: fillOpacity,
574
+ // mark if we are in an unbreakable block,
575
+ // fills inside a vertical alignment block of this unbreakable block keep their position
576
+ _isFillColorFromUnbreakable: !!writer.transactionLevel && !writer.getOpenVerticalAlignment()
577
+ }, false, true, writer.getBackgroundInsertIndex());
578
+ }
579
+ if (overlayPattern) {
580
+ writer.addVector({
581
+ type: 'rect',
582
+ x: x1f,
583
+ y: y1f,
584
+ w: bgWidth,
585
+ h: bgHeight,
586
+ lineWidth: 0,
587
+ color: overlayPattern,
588
+ fillOpacity: overlayOpacity
589
+ }, false, true);
590
+ }
591
+ }
592
+ }
593
+ }
594
+ }
595
+ writer.context().page = endingPage;
596
+ writer.context().y = endingY;
597
+ let row = this.tableNode.table.body[rowIndex];
598
+ for (let i = 0, l = row.length; i < l; i++) {
599
+ if (row[i].rowSpan) {
600
+ this.rowSpanData[i].rowSpan = row[i].rowSpan;
601
+
602
+ // fix colSpans
603
+ if (row[i].colSpan && row[i].colSpan > 1) {
604
+ for (let j = 1; j < row[i].rowSpan; j++) {
605
+ this.tableNode.table.body[rowIndex + j][i]._colSpan = row[i].colSpan;
606
+ }
607
+ }
608
+
609
+ // fix rowSpans
610
+ if (row[i].rowSpan && row[i].rowSpan > 1) {
611
+ for (let j = 1; j < row[i].rowSpan; j++) {
612
+ this.tableNode.table.body[rowIndex + j][i]._rowSpanCurrentOffset = j;
613
+ }
614
+ }
615
+ }
616
+ if (this.rowSpanData[i].rowSpan > 0) {
617
+ this.rowSpanData[i].rowSpan--;
618
+ }
619
+ }
620
+ this.drawHorizontalLine(rowIndex + 1, writer);
621
+ if (this.headerRows && rowIndex === this.headerRows - 1) {
622
+ this.headerRepeatable = writer.currentBlockToRepeatable();
623
+ }
624
+ const shouldCommitCurrentRowUnbreakable = this.dontBreakRows && (rowIndex === 0 || this._isCurrentRowUnbreakable);
625
+ if (shouldCommitCurrentRowUnbreakable) {
626
+ const pageChangedCallback = () => {
627
+ if (rowIndex > 0 && !this.headerRows && this.layout.hLineWhenBroken !== false) {
628
+ // Draw the top border of the row after a page break
629
+ this.drawHorizontalLine(rowIndex, writer);
630
+ }
631
+ };
632
+ writer.addListener('pageChanged', pageChangedCallback);
633
+ if (writer.commitUnbreakableBlock()) {
634
+ this.breakableBlocks.add(this.tableNode.table.body[rowIndex]);
635
+ this.releaseRowSpans(rowIndex);
636
+ }
637
+ writer.removeListener('pageChanged', pageChangedCallback);
638
+ }
639
+ this._isCurrentRowUnbreakable = false;
640
+ if (this.headerRepeatable && (rowIndex === this.rowsWithoutPageBreak - 1 || rowIndex === this.tableNode.table.body.length - 1)) {
641
+ if (writer.commitUnbreakableBlock()) {
642
+ if (this.rowsWithoutPageBreak === this.headerRows) {
643
+ throw new Error('Table header cannot fit on a page within the available space.');
644
+ }
645
+ // Release keepWithHeaderRows, but always preserve the repeating header.
646
+ this.breakableBlocks.add(this.tableNode.table);
647
+ this.releaseRowSpans(rowIndex);
648
+ }
649
+ // Nested headers are measured with their enclosing block and repeated after it is released.
650
+ if (!this.headerInUnbreakableBlock) {
651
+ writer.pushToRepeatables(this.headerRepeatable);
652
+ this.cleanUpRepeatables = true;
653
+ }
654
+ this.headerRepeatable = null;
655
+ }
656
+ }
657
+ }
658
+ var _default = exports.default = TableProcessor;