@digicole/pdfmake-rtl 2.1.0 → 2.1.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.
Files changed (65) hide show
  1. package/CHANGELOG.md +118 -83
  2. package/README.md +11 -10
  3. package/build/pdfmake.js +71 -42
  4. package/build/pdfmake.js.map +1 -1
  5. package/build/pdfmake.min.js +2 -2
  6. package/build/pdfmake.min.js.map +1 -1
  7. package/build/vfs_fonts.js +11 -11
  8. package/js/3rd-party/svg-to-pdfkit/source.js +3823 -0
  9. package/js/3rd-party/svg-to-pdfkit.js +7 -0
  10. package/js/DocMeasure.js +713 -0
  11. package/js/DocPreprocessor.js +275 -0
  12. package/js/DocumentContext.js +310 -0
  13. package/js/ElementWriter.js +687 -0
  14. package/js/LayoutBuilder.js +1240 -0
  15. package/js/Line.js +113 -0
  16. package/js/OutputDocument.js +64 -0
  17. package/js/OutputDocumentServer.js +29 -0
  18. package/js/PDFDocument.js +144 -0
  19. package/js/PageElementWriter.js +161 -0
  20. package/js/PageSize.js +74 -0
  21. package/js/Printer.js +351 -0
  22. package/js/Renderer.js +417 -0
  23. package/js/SVGMeasure.js +92 -0
  24. package/js/StyleContextStack.js +191 -0
  25. package/js/TableProcessor.js +575 -0
  26. package/js/TextBreaker.js +166 -0
  27. package/js/TextDecorator.js +152 -0
  28. package/js/TextInlines.js +244 -0
  29. package/js/URLResolver.js +43 -0
  30. package/js/base.js +59 -0
  31. package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
  32. package/js/browser-extensions/fonts/Cairo.js +38 -0
  33. package/js/browser-extensions/fonts/Roboto.js +38 -0
  34. package/js/browser-extensions/index.js +59 -0
  35. package/js/browser-extensions/pdfMake.js +3 -0
  36. package/js/browser-extensions/standard-fonts/Courier.js +38 -0
  37. package/js/browser-extensions/standard-fonts/Helvetica.js +38 -0
  38. package/js/browser-extensions/standard-fonts/Symbol.js +23 -0
  39. package/js/browser-extensions/standard-fonts/Times.js +38 -0
  40. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +23 -0
  41. package/js/browser-extensions/virtual-fs-cjs.js +3 -0
  42. package/js/columnCalculator.js +148 -0
  43. package/js/helpers/node.js +123 -0
  44. package/js/helpers/tools.js +46 -0
  45. package/js/helpers/variableType.js +59 -0
  46. package/js/index.js +15 -0
  47. package/js/qrEnc.js +721 -0
  48. package/js/rtlUtils.js +519 -0
  49. package/js/standardPageSizes.js +56 -0
  50. package/js/tableLayouts.js +98 -0
  51. package/js/virtual-fs.js +60 -0
  52. package/package.json +1 -1
  53. package/src/{docMeasure.js → DocMeasure.js} +8 -8
  54. package/src/{elementWriter.js → ElementWriter.js} +3 -3
  55. package/src/{layoutBuilder.js → LayoutBuilder.js} +1406 -1393
  56. package/src/{tableProcessor.js → TableProcessor.js} +633 -620
  57. package/src/rtlUtils.js +503 -500
  58. /package/src/{docPreprocessor.js → DocPreprocessor.js} +0 -0
  59. /package/src/{documentContext.js → DocumentContext.js} +0 -0
  60. /package/src/{line.js → Line.js} +0 -0
  61. /package/src/{pageElementWriter.js → PageElementWriter.js} +0 -0
  62. /package/src/{printer.js → Printer.js} +0 -0
  63. /package/src/{svgMeasure.js → SVGMeasure.js} +0 -0
  64. /package/src/{styleContextStack.js → StyleContextStack.js} +0 -0
  65. /package/src/{textDecorator.js → TextDecorator.js} +0 -0
@@ -1,620 +1,633 @@
1
- import ColumnCalculator from './columnCalculator';
2
- import { isNumber, isPositiveInteger } from './helpers/variableType';
3
-
4
- class TableProcessor {
5
- constructor(tableNode) {
6
- this.tableNode = tableNode;
7
- }
8
-
9
- beginTable(writer) {
10
- const getTableInnerContentWidth = () => {
11
- let width = 0;
12
-
13
- tableNode.table.widths.forEach(w => {
14
- width += w._calcWidth;
15
- });
16
-
17
- return width;
18
- };
19
-
20
- const prepareRowSpanData = () => {
21
- let rsd = [];
22
- let x = 0;
23
- let lastWidth = 0;
24
-
25
- rsd.push({ left: 0, rowSpan: 0 });
26
-
27
- for (let i = 0, l = this.tableNode.table.body[0].length; i < l; i++) {
28
- let paddings = this.layout.paddingLeft(i, this.tableNode) + this.layout.paddingRight(i, this.tableNode);
29
- let lBorder = this.layout.vLineWidth(i, this.tableNode);
30
- lastWidth = paddings + lBorder + this.tableNode.table.widths[i]._calcWidth;
31
- rsd[rsd.length - 1].width = lastWidth;
32
- x += lastWidth;
33
- rsd.push({ left: x, rowSpan: 0, width: 0 });
34
- }
35
-
36
- return rsd;
37
- };
38
-
39
- // Iterate through all cells. If the current cell is the start of a
40
- // rowSpan/colSpan, update the border property of the cells on its
41
- // bottom/right accordingly. This is needed since each iteration of the
42
- // line-drawing loops draws lines for a single cell, not for an entire
43
- // rowSpan/colSpan.
44
- const prepareCellBorders = body => {
45
- for (let rowIndex = 0; rowIndex < body.length; rowIndex++) {
46
- let row = body[rowIndex];
47
-
48
- for (let colIndex = 0; colIndex < row.length; colIndex++) {
49
- let cell = row[colIndex];
50
-
51
- if (cell.border) {
52
- let rowSpan = cell.rowSpan || 1;
53
- let colSpan = cell.colSpan || 1;
54
-
55
- for (let rowOffset = 0; rowOffset < rowSpan; rowOffset++) {
56
- // set left border
57
- if (cell.border[0] !== undefined && rowOffset > 0) {
58
- setBorder(rowIndex + rowOffset, colIndex, 0, cell.border[0]);
59
- }
60
-
61
- // set right border
62
- if (cell.border[2] !== undefined) {
63
- setBorder(rowIndex + rowOffset, colIndex + colSpan - 1, 2, cell.border[2]);
64
- }
65
- }
66
-
67
- for (let colOffset = 0; colOffset < colSpan; colOffset++) {
68
- // set top border
69
- if (cell.border[1] !== undefined && colOffset > 0) {
70
- setBorder(rowIndex, colIndex + colOffset, 1, cell.border[1]);
71
- }
72
-
73
- // set bottom border
74
- if (cell.border[3] !== undefined) {
75
- setBorder(rowIndex + rowSpan - 1, colIndex + colOffset, 3, cell.border[3]);
76
- }
77
- }
78
- }
79
- }
80
- }
81
-
82
- // helper function to set the border for a given cell
83
- function setBorder(rowIndex, colIndex, borderIndex, borderValue) {
84
- let cell = body[rowIndex][colIndex];
85
- cell.border = cell.border || {};
86
- cell.border[borderIndex] = borderValue;
87
- }
88
- };
89
-
90
- let tableNode;
91
- let availableWidth;
92
-
93
- tableNode = this.tableNode;
94
- this.offsets = tableNode._offsets;
95
- this.layout = tableNode._layout;
96
-
97
- availableWidth = writer.context().availableWidth - this.offsets.total;
98
- ColumnCalculator.buildColumnWidths(tableNode.table.widths, availableWidth, this.offsets.total, tableNode);
99
-
100
- this.tableWidth = tableNode._offsets.total + getTableInnerContentWidth();
101
- this.rowSpanData = prepareRowSpanData();
102
- this.cleanUpRepeatables = false;
103
-
104
- // headersRows and rowsWithoutPageBreak (headerRows + keepWithHeaderRows)
105
- this.headerRows = 0;
106
- this.rowsWithoutPageBreak = 0;
107
-
108
- const headerRows = tableNode.table.headerRows;
109
-
110
- if (isPositiveInteger(headerRows)) {
111
- this.headerRows = headerRows;
112
-
113
- if (this.headerRows > tableNode.table.body.length) {
114
- throw new Error(`Too few rows in the table. Property headerRows requires at least ${this.headerRows}, contains only ${tableNode.table.body.length}`);
115
- }
116
-
117
- this.rowsWithoutPageBreak = this.headerRows;
118
-
119
- const keepWithHeaderRows = tableNode.table.keepWithHeaderRows;
120
-
121
- if (isPositiveInteger(keepWithHeaderRows)) {
122
- this.rowsWithoutPageBreak += keepWithHeaderRows;
123
- }
124
- }
125
-
126
- this.dontBreakRows = tableNode.table.dontBreakRows || false;
127
-
128
- if (this.rowsWithoutPageBreak || this.dontBreakRows) {
129
- writer.beginUnbreakableBlock();
130
- // Draw the top border of the table
131
- this.drawHorizontalLine(0, writer);
132
- if (this.rowsWithoutPageBreak && this.dontBreakRows) {
133
- // We just increase the value of transactionLevel
134
- writer.beginUnbreakableBlock();
135
- }
136
- }
137
-
138
- // update the border properties of all cells before drawing any lines
139
- prepareCellBorders(this.tableNode.table.body);
140
- }
141
-
142
- onRowBreak(rowIndex, writer) {
143
- return () => {
144
- let offset = this.rowPaddingTop + (!this.headerRows ? this.topLineWidth : 0);
145
- writer.context().availableHeight -= this.reservedAtBottom;
146
- writer.context().moveDown(offset);
147
- };
148
- }
149
-
150
- beginRow(rowIndex, writer) {
151
- this.topLineWidth = this.layout.hLineWidth(rowIndex, this.tableNode);
152
- this.rowPaddingTop = this.layout.paddingTop(rowIndex, this.tableNode);
153
- this.bottomLineWidth = this.layout.hLineWidth(rowIndex + 1, this.tableNode);
154
- this.rowPaddingBottom = this.layout.paddingBottom(rowIndex, this.tableNode);
155
-
156
- this.rowCallback = this.onRowBreak(rowIndex, writer);
157
- writer.addListener('pageChanged', this.rowCallback);
158
- if (rowIndex == 0 && !this.dontBreakRows && !this.rowsWithoutPageBreak) {
159
- // We store the 'y' to draw later and if necessary the top border of the table
160
- this._tableTopBorderY = writer.context().y;
161
- writer.context().moveDown(this.topLineWidth);
162
- }
163
-
164
- this.rowTopPageY = writer.context().y + this.rowPaddingTop;
165
-
166
- if (this.dontBreakRows && rowIndex > 0) {
167
- writer.beginUnbreakableBlock();
168
- }
169
- this.rowTopY = writer.context().y;
170
- this.reservedAtBottom = this.bottomLineWidth + this.rowPaddingBottom;
171
-
172
- writer.context().availableHeight -= this.reservedAtBottom;
173
-
174
- writer.context().moveDown(this.rowPaddingTop);
175
- }
176
-
177
- drawHorizontalLine(lineIndex, writer, overrideY, moveDown = true, forcePage) {
178
- let lineWidth = this.layout.hLineWidth(lineIndex, this.tableNode);
179
- if (lineWidth) {
180
- let style = this.layout.hLineStyle(lineIndex, this.tableNode);
181
- let dash;
182
- if (style && style.dash) {
183
- dash = style.dash;
184
- }
185
-
186
- let offset = lineWidth / 2;
187
- let currentLine = null;
188
- let body = this.tableNode.table.body;
189
- let cellAbove;
190
- let currentCell;
191
- let rowCellAbove;
192
-
193
- for (let i = 0, l = this.rowSpanData.length; i < l; i++) {
194
- let data = this.rowSpanData[i];
195
- let shouldDrawLine = !data.rowSpan;
196
- let borderColor = null;
197
-
198
- // draw only if the current cell requires a top border or the cell in the
199
- // row above requires a bottom border
200
- if (shouldDrawLine && i < l - 1) {
201
- var topBorder = false, bottomBorder = false, rowBottomBorder = false;
202
-
203
- // the cell in the row above
204
- if (lineIndex > 0) {
205
- cellAbove = body[lineIndex - 1][i];
206
- bottomBorder = cellAbove.border ? cellAbove.border[3] : this.layout.defaultBorder;
207
- if (bottomBorder && cellAbove.borderColor) {
208
- borderColor = cellAbove.borderColor[3];
209
- }
210
- }
211
-
212
- // the current cell
213
- if (lineIndex < body.length) {
214
- currentCell = body[lineIndex][i];
215
- topBorder = currentCell.border ? currentCell.border[1] : this.layout.defaultBorder;
216
- if (topBorder && borderColor == null && currentCell.borderColor) {
217
- borderColor = currentCell.borderColor[1];
218
- }
219
- }
220
-
221
- shouldDrawLine = topBorder || bottomBorder;
222
- }
223
-
224
- if (cellAbove && cellAbove._rowSpanCurrentOffset) {
225
- rowCellAbove = body[lineIndex - 1 - cellAbove._rowSpanCurrentOffset][i];
226
- rowBottomBorder = rowCellAbove && rowCellAbove.border ? rowCellAbove.border[3] : this.layout.defaultBorder;
227
- if (rowBottomBorder && rowCellAbove && rowCellAbove.borderColor) {
228
- borderColor = rowCellAbove.borderColor[3];
229
- }
230
- }
231
-
232
- if (borderColor == null) {
233
- borderColor = typeof this.layout.hLineColor === 'function' ? this.layout.hLineColor(lineIndex, this.tableNode, i) : this.layout.hLineColor;
234
- }
235
-
236
- if (!currentLine && shouldDrawLine) {
237
- currentLine = { left: data.left, width: 0 };
238
- }
239
-
240
- if (shouldDrawLine) {
241
- let colSpanIndex = 0;
242
- if (rowCellAbove && rowCellAbove.colSpan && rowBottomBorder) {
243
- while (rowCellAbove.colSpan > colSpanIndex) {
244
- currentLine.width += (this.rowSpanData[i + colSpanIndex++].width || 0);
245
- }
246
- i += colSpanIndex - 1;
247
- } else if (cellAbove && cellAbove.colSpan && bottomBorder) {
248
- while (cellAbove.colSpan > colSpanIndex) {
249
- currentLine.width += (this.rowSpanData[i + colSpanIndex++].width || 0);
250
- }
251
- i += colSpanIndex - 1;
252
- } else if (currentCell && currentCell.colSpan && topBorder) {
253
- while (currentCell.colSpan > colSpanIndex) {
254
- currentLine.width += (this.rowSpanData[i + colSpanIndex++].width || 0);
255
- }
256
- i += colSpanIndex - 1;
257
- } else {
258
- currentLine.width += (this.rowSpanData[i].width || 0);
259
- }
260
- }
261
-
262
- let y = (overrideY || 0) + offset;
263
-
264
- if (shouldDrawLine) {
265
- if (currentLine && currentLine.width) {
266
- writer.addVector({
267
- type: 'line',
268
- x1: currentLine.left,
269
- x2: currentLine.left + currentLine.width,
270
- y1: y,
271
- y2: y,
272
- lineWidth: lineWidth,
273
- dash: dash,
274
- lineColor: borderColor
275
- }, false, isNumber(overrideY), null, forcePage);
276
- currentLine = null;
277
- borderColor = null;
278
- cellAbove = null;
279
- currentCell = null;
280
- rowCellAbove = null;
281
- }
282
- }
283
- }
284
-
285
- if (moveDown) {
286
- writer.context().moveDown(lineWidth);
287
- }
288
- }
289
- }
290
-
291
- drawVerticalLine(x, y0, y1, vLineColIndex, writer, vLineRowIndex, beforeVLineColIndex) {
292
- let width = this.layout.vLineWidth(vLineColIndex, this.tableNode);
293
- if (width === 0) {
294
- return;
295
- }
296
- let style = this.layout.vLineStyle(vLineColIndex, this.tableNode);
297
- let dash;
298
- if (style && style.dash) {
299
- dash = style.dash;
300
- }
301
-
302
- let body = this.tableNode.table.body;
303
- let cellBefore;
304
- let currentCell;
305
- let borderColor;
306
-
307
- // the cell in the col before
308
- if (vLineColIndex > 0) {
309
- cellBefore = body[vLineRowIndex][beforeVLineColIndex];
310
- if (cellBefore && cellBefore.borderColor) {
311
- if (cellBefore.border ? cellBefore.border[2] : this.layout.defaultBorder) {
312
- borderColor = cellBefore.borderColor[2];
313
- }
314
- }
315
- }
316
-
317
- // the current cell
318
- if (borderColor == null && vLineColIndex < body.length) {
319
- currentCell = body[vLineRowIndex][vLineColIndex];
320
- if (currentCell && currentCell.borderColor) {
321
- if (currentCell.border ? currentCell.border[0] : this.layout.defaultBorder) {
322
- borderColor = currentCell.borderColor[0];
323
- }
324
- }
325
- }
326
-
327
- if (borderColor == null && cellBefore && cellBefore._rowSpanCurrentOffset) {
328
- let rowCellBeforeAbove = body[vLineRowIndex - cellBefore._rowSpanCurrentOffset][beforeVLineColIndex];
329
- if (rowCellBeforeAbove.borderColor) {
330
- if (rowCellBeforeAbove.border ? rowCellBeforeAbove.border[2] : this.layout.defaultBorder) {
331
- borderColor = rowCellBeforeAbove.borderColor[2];
332
- }
333
- }
334
- }
335
-
336
- if (borderColor == null && currentCell && currentCell._rowSpanCurrentOffset) {
337
- let rowCurrentCellAbove = body[vLineRowIndex - currentCell._rowSpanCurrentOffset][vLineColIndex];
338
- if (rowCurrentCellAbove.borderColor) {
339
- if (rowCurrentCellAbove.border ? rowCurrentCellAbove.border[2] : this.layout.defaultBorder) {
340
- borderColor = rowCurrentCellAbove.borderColor[2];
341
- }
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
-
349
- writer.addVector({
350
- type: 'line',
351
- x1: x + width / 2,
352
- x2: x + width / 2,
353
- y1: y0,
354
- y2: y1,
355
- lineWidth: width,
356
- dash: dash,
357
- lineColor: borderColor
358
- }, false, true);
359
- cellBefore = null;
360
- currentCell = null;
361
- borderColor = null;
362
- }
363
-
364
- endTable(writer) {
365
- if (this.cleanUpRepeatables) {
366
- writer.popFromRepeatables();
367
- }
368
- }
369
-
370
- endRow(rowIndex, writer, pageBreaks) {
371
- const getLineXs = () => {
372
- let result = [];
373
- let cols = 0;
374
-
375
- for (let i = 0, l = this.tableNode.table.body[rowIndex].length; i < l; i++) {
376
- if (!cols) {
377
- result.push({ x: this.rowSpanData[i].left, index: i });
378
-
379
- let item = this.tableNode.table.body[rowIndex][i];
380
- cols = (item._colSpan || item.colSpan || 0);
381
- }
382
- if (cols > 0) {
383
- cols--;
384
- }
385
- }
386
-
387
- result.push({ x: this.rowSpanData[this.rowSpanData.length - 1].left, index: this.rowSpanData.length - 1 });
388
-
389
- return result;
390
- };
391
-
392
- writer.removeListener('pageChanged', this.rowCallback);
393
- writer.context().moveDown(this.layout.paddingBottom(rowIndex, this.tableNode));
394
- writer.context().availableHeight += this.reservedAtBottom;
395
-
396
- let endingPage = writer.context().page;
397
- let endingY = writer.context().y;
398
-
399
- let xs = getLineXs();
400
-
401
- let ys = [];
402
-
403
- let hasBreaks = pageBreaks && pageBreaks.length > 0;
404
- let body = this.tableNode.table.body;
405
-
406
- ys.push({
407
- y0: this.rowTopY,
408
- page: hasBreaks ? pageBreaks[0].prevPage : endingPage
409
- });
410
-
411
- if (hasBreaks) {
412
- for (let i = 0, l = pageBreaks.length; i < l; i++) {
413
- let pageBreak = pageBreaks[i];
414
- ys[ys.length - 1].y1 = pageBreak.prevY;
415
-
416
- ys.push({ y0: pageBreak.y, page: pageBreak.prevPage + 1 });
417
- }
418
- }
419
-
420
- ys[ys.length - 1].y1 = endingY;
421
-
422
- let skipOrphanePadding = (ys[0].y1 - ys[0].y0 === this.rowPaddingTop);
423
- if (rowIndex === 0 && !skipOrphanePadding && !this.rowsWithoutPageBreak && !this.dontBreakRows) {
424
- // Draw the top border of the table
425
- let pageTableStartedAt = null;
426
- if (pageBreaks && pageBreaks.length > 0) {
427
- // Get the page where table started at
428
- pageTableStartedAt = pageBreaks[0].prevPage;
429
- }
430
- this.drawHorizontalLine(0, writer, this._tableTopBorderY, false, pageTableStartedAt);
431
- }
432
- for (let yi = (skipOrphanePadding ? 1 : 0), yl = ys.length; yi < yl; yi++) {
433
- let willBreak = yi < ys.length - 1;
434
- let rowBreakWithoutHeader = (yi > 0 && !this.headerRows);
435
- let hzLineOffset = rowBreakWithoutHeader ? 0 : this.topLineWidth;
436
- let y1 = ys[yi].y0;
437
- let y2 = ys[yi].y1;
438
-
439
- if (willBreak) {
440
- y2 = y2 + this.rowPaddingBottom;
441
- }
442
-
443
- if (writer.context().page != ys[yi].page) {
444
- writer.context().page = ys[yi].page;
445
-
446
- //TODO: buggy, availableHeight should be updated on every pageChanged event
447
- // TableProcessor should be pageChanged listener, instead of processRow
448
- this.reservedAtBottom = 0;
449
- }
450
-
451
- // Draw horizontal lines before the vertical lines so they are not overridden
452
- if (willBreak && this.layout.hLineWhenBroken !== false) {
453
- this.drawHorizontalLine(rowIndex + 1, writer, y2);
454
- }
455
- if (rowBreakWithoutHeader && this.layout.hLineWhenBroken !== false) {
456
- this.drawHorizontalLine(rowIndex, writer, y1);
457
- }
458
-
459
- for (let i = 0, l = xs.length; i < l; i++) {
460
- let leftCellBorder = false;
461
- let rightCellBorder = false;
462
- let colIndex = xs[i].index;
463
-
464
- // current cell
465
- if (colIndex < body[rowIndex].length) {
466
- let cell = body[rowIndex][colIndex];
467
- leftCellBorder = cell.border ? cell.border[0] : this.layout.defaultBorder;
468
- rightCellBorder = cell.border ? cell.border[2] : this.layout.defaultBorder;
469
- }
470
-
471
- // before cell
472
- if (colIndex > 0 && !leftCellBorder) {
473
- let cell = body[rowIndex][colIndex - 1];
474
- leftCellBorder = cell.border ? cell.border[2] : this.layout.defaultBorder;
475
- }
476
-
477
- // after cell
478
- if (colIndex + 1 < body[rowIndex].length && !rightCellBorder) {
479
- let cell = body[rowIndex][colIndex + 1];
480
- rightCellBorder = cell.border ? cell.border[0] : this.layout.defaultBorder;
481
- }
482
-
483
- if (leftCellBorder) {
484
- this.drawVerticalLine(xs[i].x, y1 - hzLineOffset, y2 + this.bottomLineWidth, xs[i].index, writer, rowIndex, xs[i - 1] ? xs[i - 1].index : null);
485
- }
486
-
487
- if (i < l - 1) {
488
- body[rowIndex][colIndex]._willBreak = body[rowIndex][colIndex]._willBreak ?? willBreak;
489
- if (body[rowIndex][colIndex]._bottomY === undefined) {
490
- let bottomY = this.dontBreakRows ? y2 + this.bottomLineWidth : y2 + (this.bottomLineWidth / 2);
491
- if (willBreak || this.dontBreakRows) {
492
- bottomY = bottomY - this.rowPaddingBottom;
493
- }
494
- body[rowIndex][colIndex]._bottomY = bottomY - this.reservedAtBottom;
495
- }
496
- body[rowIndex][colIndex]._rowTopPageY = this.rowTopPageY;
497
- if (this.dontBreakRows) {
498
- body[rowIndex][colIndex]._rowTopPageYPadding = this.rowPaddingTop;
499
- }
500
-
501
- body[rowIndex][colIndex]._lastPageNumber = ys[yi].page + 1;
502
-
503
- let fillColor = body[rowIndex][colIndex].fillColor;
504
- let fillOpacity = body[rowIndex][colIndex].fillOpacity;
505
- if (!fillColor) {
506
- fillColor = typeof this.layout.fillColor === 'function' ? this.layout.fillColor(rowIndex, this.tableNode, colIndex) : this.layout.fillColor;
507
- }
508
- if (!isNumber(fillOpacity)) {
509
- fillOpacity = typeof this.layout.fillOpacity === 'function' ? this.layout.fillOpacity(rowIndex, this.tableNode, colIndex) : this.layout.fillOpacity;
510
- }
511
- var overlayPattern = body[rowIndex][colIndex].overlayPattern;
512
- var overlayOpacity = body[rowIndex][colIndex].overlayOpacity;
513
- if (fillColor || overlayPattern) {
514
- let widthLeftBorder = leftCellBorder ? this.layout.vLineWidth(colIndex, this.tableNode) : 0;
515
- let widthRightBorder;
516
- if ((colIndex === 0 || colIndex + 1 == body[rowIndex].length) && !rightCellBorder) {
517
- widthRightBorder = this.layout.vLineWidth(colIndex + 1, this.tableNode);
518
- } else if (rightCellBorder) {
519
- widthRightBorder = this.layout.vLineWidth(colIndex + 1, this.tableNode) / 2;
520
- } else {
521
- widthRightBorder = 0;
522
- }
523
-
524
- let x1f = this.dontBreakRows ? xs[i].x + widthLeftBorder : xs[i].x + (widthLeftBorder / 2);
525
- let y1f = this.dontBreakRows ? y1 : y1 - (hzLineOffset / 2);
526
- let x2f = xs[i + 1].x + widthRightBorder;
527
- let y2f = this.dontBreakRows ? y2 + this.bottomLineWidth : y2 + (this.bottomLineWidth / 2);
528
- var bgWidth = x2f - x1f;
529
- var bgHeight = y2f - y1f;
530
- if (fillColor) {
531
- writer.addVector({
532
- type: 'rect',
533
- x: x1f,
534
- y: y1f,
535
- w: bgWidth,
536
- h: bgHeight,
537
- lineWidth: 0,
538
- color: fillColor,
539
- fillOpacity: fillOpacity,
540
- // mark if we are in an unbreakable block
541
- _isFillColorFromUnbreakable: !!writer.transactionLevel
542
- }, false, true, writer.context().backgroundLength[writer.context().page]);
543
- }
544
-
545
- if (overlayPattern) {
546
- writer.addVector({
547
- type: 'rect',
548
- x: x1f,
549
- y: y1f,
550
- w: bgWidth,
551
- h: bgHeight,
552
- lineWidth: 0,
553
- color: overlayPattern,
554
- fillOpacity: overlayOpacity
555
- }, false, true);
556
- }
557
- }
558
- }
559
- }
560
- }
561
-
562
- writer.context().page = endingPage;
563
- writer.context().y = endingY;
564
-
565
- let row = this.tableNode.table.body[rowIndex];
566
- for (let i = 0, l = row.length; i < l; i++) {
567
- if (row[i].rowSpan) {
568
- this.rowSpanData[i].rowSpan = row[i].rowSpan;
569
-
570
- // fix colSpans
571
- if (row[i].colSpan && row[i].colSpan > 1) {
572
- for (let j = 1; j < row[i].rowSpan; j++) {
573
- this.tableNode.table.body[rowIndex + j][i]._colSpan = row[i].colSpan;
574
- }
575
- }
576
-
577
- // fix rowSpans
578
- if (row[i].rowSpan && row[i].rowSpan > 1) {
579
- for (let j = 1; j < row[i].rowSpan; j++) {
580
- this.tableNode.table.body[rowIndex + j][i]._rowSpanCurrentOffset = j;
581
- }
582
- }
583
- }
584
-
585
- if (this.rowSpanData[i].rowSpan > 0) {
586
- this.rowSpanData[i].rowSpan--;
587
- }
588
- }
589
-
590
- this.drawHorizontalLine(rowIndex + 1, writer);
591
-
592
- if (this.headerRows && rowIndex === this.headerRows - 1) {
593
- this.headerRepeatable = writer.currentBlockToRepeatable();
594
- }
595
-
596
- if (this.dontBreakRows) {
597
- const pageChangedCallback = () => {
598
- if (rowIndex > 0 && !this.headerRows && this.layout.hLineWhenBroken !== false) {
599
- // Draw the top border of the row after a page break
600
- this.drawHorizontalLine(rowIndex, writer);
601
- }
602
- };
603
-
604
- writer.addListener('pageChanged', pageChangedCallback);
605
-
606
- writer.commitUnbreakableBlock();
607
-
608
- writer.removeListener('pageChanged', pageChangedCallback);
609
- }
610
-
611
- if (this.headerRepeatable && (rowIndex === (this.rowsWithoutPageBreak - 1) || rowIndex === this.tableNode.table.body.length - 1)) {
612
- writer.commitUnbreakableBlock();
613
- writer.pushToRepeatables(this.headerRepeatable);
614
- this.cleanUpRepeatables = true;
615
- this.headerRepeatable = null;
616
- }
617
- }
618
- }
619
-
620
- export default TableProcessor;
1
+ import ColumnCalculator from './columnCalculator';
2
+ import { isNumber, isPositiveInteger } from './helpers/variableType';
3
+
4
+ class TableProcessor {
5
+ constructor(tableNode) {
6
+ this.tableNode = tableNode;
7
+ }
8
+
9
+ beginTable(writer) {
10
+ const getTableInnerContentWidth = () => {
11
+ let width = 0;
12
+
13
+ tableNode.table.widths.forEach(w => {
14
+ width += w._calcWidth;
15
+ });
16
+
17
+ return width;
18
+ };
19
+
20
+ const prepareRowSpanData = () => {
21
+ let rsd = [];
22
+ let x = 0;
23
+ let lastWidth = 0;
24
+
25
+ rsd.push({ left: 0, rowSpan: 0 });
26
+
27
+ for (let i = 0, l = this.tableNode.table.body[0].length; i < l; i++) {
28
+ let paddings = this.layout.paddingLeft(i, this.tableNode) + this.layout.paddingRight(i, this.tableNode);
29
+ let lBorder = this.layout.vLineWidth(i, this.tableNode);
30
+ lastWidth = paddings + lBorder + this.tableNode.table.widths[i]._calcWidth;
31
+ rsd[rsd.length - 1].width = lastWidth;
32
+ x += lastWidth;
33
+ rsd.push({ left: x, rowSpan: 0, width: 0 });
34
+ }
35
+
36
+ return rsd;
37
+ };
38
+
39
+ // Iterate through all cells. If the current cell is the start of a
40
+ // rowSpan/colSpan, update the border property of the cells on its
41
+ // bottom/right accordingly. This is needed since each iteration of the
42
+ // line-drawing loops draws lines for a single cell, not for an entire
43
+ // rowSpan/colSpan.
44
+ const prepareCellBorders = body => {
45
+ for (let rowIndex = 0; rowIndex < body.length; rowIndex++) {
46
+ let row = body[rowIndex];
47
+
48
+ for (let colIndex = 0; colIndex < row.length; colIndex++) {
49
+ let cell = row[colIndex];
50
+
51
+ if (cell.border) {
52
+ let rowSpan = cell.rowSpan || 1;
53
+ let colSpan = cell.colSpan || 1;
54
+
55
+ for (let rowOffset = 0; rowOffset < rowSpan; rowOffset++) {
56
+ // set left border
57
+ if (cell.border[0] !== undefined && rowOffset > 0) {
58
+ setBorder(rowIndex + rowOffset, colIndex, 0, cell.border[0]);
59
+ }
60
+
61
+ // set right border
62
+ if (cell.border[2] !== undefined) {
63
+ setBorder(rowIndex + rowOffset, colIndex + colSpan - 1, 2, cell.border[2]);
64
+ }
65
+ }
66
+
67
+ for (let colOffset = 0; colOffset < colSpan; colOffset++) {
68
+ // set top border
69
+ if (cell.border[1] !== undefined && colOffset > 0) {
70
+ setBorder(rowIndex, colIndex + colOffset, 1, cell.border[1]);
71
+ }
72
+
73
+ // set bottom border
74
+ if (cell.border[3] !== undefined) {
75
+ setBorder(rowIndex + rowSpan - 1, colIndex + colOffset, 3, cell.border[3]);
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
81
+
82
+ // helper function to set the border for a given cell
83
+ function setBorder(rowIndex, colIndex, borderIndex, borderValue) {
84
+ let cell = body[rowIndex][colIndex];
85
+ cell.border = cell.border || {};
86
+ cell.border[borderIndex] = borderValue;
87
+ }
88
+ };
89
+
90
+ let tableNode;
91
+ let availableWidth;
92
+
93
+ tableNode = this.tableNode;
94
+ this.offsets = tableNode._offsets;
95
+ this.layout = tableNode._layout;
96
+
97
+ availableWidth = writer.context().availableWidth - this.offsets.total;
98
+ ColumnCalculator.buildColumnWidths(tableNode.table.widths, availableWidth, this.offsets.total, tableNode);
99
+
100
+ this.tableWidth = tableNode._offsets.total + getTableInnerContentWidth();
101
+ this.rowSpanData = prepareRowSpanData();
102
+
103
+ // RTL table right-alignment: shift the table grid to the right
104
+ // when the table doesn't fill the full available width
105
+ if (tableNode.table._rtl) {
106
+ let fullAvailableWidth = writer.context().availableWidth;
107
+ let rtlOffset = fullAvailableWidth - this.tableWidth;
108
+ if (rtlOffset > 0.5) { // only shift if there's meaningful space
109
+ for (let i = 0; i < this.rowSpanData.length; i++) {
110
+ this.rowSpanData[i].left += rtlOffset;
111
+ }
112
+ }
113
+ }
114
+
115
+ this.cleanUpRepeatables = false;
116
+
117
+ // headersRows and rowsWithoutPageBreak (headerRows + keepWithHeaderRows)
118
+ this.headerRows = 0;
119
+ this.rowsWithoutPageBreak = 0;
120
+
121
+ const headerRows = tableNode.table.headerRows;
122
+
123
+ if (isPositiveInteger(headerRows)) {
124
+ this.headerRows = headerRows;
125
+
126
+ if (this.headerRows > tableNode.table.body.length) {
127
+ throw new Error(`Too few rows in the table. Property headerRows requires at least ${this.headerRows}, contains only ${tableNode.table.body.length}`);
128
+ }
129
+
130
+ this.rowsWithoutPageBreak = this.headerRows;
131
+
132
+ const keepWithHeaderRows = tableNode.table.keepWithHeaderRows;
133
+
134
+ if (isPositiveInteger(keepWithHeaderRows)) {
135
+ this.rowsWithoutPageBreak += keepWithHeaderRows;
136
+ }
137
+ }
138
+
139
+ this.dontBreakRows = tableNode.table.dontBreakRows || false;
140
+
141
+ if (this.rowsWithoutPageBreak || this.dontBreakRows) {
142
+ writer.beginUnbreakableBlock();
143
+ // Draw the top border of the table
144
+ this.drawHorizontalLine(0, writer);
145
+ if (this.rowsWithoutPageBreak && this.dontBreakRows) {
146
+ // We just increase the value of transactionLevel
147
+ writer.beginUnbreakableBlock();
148
+ }
149
+ }
150
+
151
+ // update the border properties of all cells before drawing any lines
152
+ prepareCellBorders(this.tableNode.table.body);
153
+ }
154
+
155
+ onRowBreak(rowIndex, writer) {
156
+ return () => {
157
+ let offset = this.rowPaddingTop + (!this.headerRows ? this.topLineWidth : 0);
158
+ writer.context().availableHeight -= this.reservedAtBottom;
159
+ writer.context().moveDown(offset);
160
+ };
161
+ }
162
+
163
+ beginRow(rowIndex, writer) {
164
+ this.topLineWidth = this.layout.hLineWidth(rowIndex, this.tableNode);
165
+ this.rowPaddingTop = this.layout.paddingTop(rowIndex, this.tableNode);
166
+ this.bottomLineWidth = this.layout.hLineWidth(rowIndex + 1, this.tableNode);
167
+ this.rowPaddingBottom = this.layout.paddingBottom(rowIndex, this.tableNode);
168
+
169
+ this.rowCallback = this.onRowBreak(rowIndex, writer);
170
+ writer.addListener('pageChanged', this.rowCallback);
171
+ if (rowIndex == 0 && !this.dontBreakRows && !this.rowsWithoutPageBreak) {
172
+ // We store the 'y' to draw later and if necessary the top border of the table
173
+ this._tableTopBorderY = writer.context().y;
174
+ writer.context().moveDown(this.topLineWidth);
175
+ }
176
+
177
+ this.rowTopPageY = writer.context().y + this.rowPaddingTop;
178
+
179
+ if (this.dontBreakRows && rowIndex > 0) {
180
+ writer.beginUnbreakableBlock();
181
+ }
182
+ this.rowTopY = writer.context().y;
183
+ this.reservedAtBottom = this.bottomLineWidth + this.rowPaddingBottom;
184
+
185
+ writer.context().availableHeight -= this.reservedAtBottom;
186
+
187
+ writer.context().moveDown(this.rowPaddingTop);
188
+ }
189
+
190
+ drawHorizontalLine(lineIndex, writer, overrideY, moveDown = true, forcePage) {
191
+ let lineWidth = this.layout.hLineWidth(lineIndex, this.tableNode);
192
+ if (lineWidth) {
193
+ let style = this.layout.hLineStyle(lineIndex, this.tableNode);
194
+ let dash;
195
+ if (style && style.dash) {
196
+ dash = style.dash;
197
+ }
198
+
199
+ let offset = lineWidth / 2;
200
+ let currentLine = null;
201
+ let body = this.tableNode.table.body;
202
+ let cellAbove;
203
+ let currentCell;
204
+ let rowCellAbove;
205
+
206
+ for (let i = 0, l = this.rowSpanData.length; i < l; i++) {
207
+ let data = this.rowSpanData[i];
208
+ let shouldDrawLine = !data.rowSpan;
209
+ let borderColor = null;
210
+
211
+ // draw only if the current cell requires a top border or the cell in the
212
+ // row above requires a bottom border
213
+ if (shouldDrawLine && i < l - 1) {
214
+ var topBorder = false, bottomBorder = false, rowBottomBorder = false;
215
+
216
+ // the cell in the row above
217
+ if (lineIndex > 0) {
218
+ cellAbove = body[lineIndex - 1][i];
219
+ bottomBorder = cellAbove.border ? cellAbove.border[3] : this.layout.defaultBorder;
220
+ if (bottomBorder && cellAbove.borderColor) {
221
+ borderColor = cellAbove.borderColor[3];
222
+ }
223
+ }
224
+
225
+ // the current cell
226
+ if (lineIndex < body.length) {
227
+ currentCell = body[lineIndex][i];
228
+ topBorder = currentCell.border ? currentCell.border[1] : this.layout.defaultBorder;
229
+ if (topBorder && borderColor == null && currentCell.borderColor) {
230
+ borderColor = currentCell.borderColor[1];
231
+ }
232
+ }
233
+
234
+ shouldDrawLine = topBorder || bottomBorder;
235
+ }
236
+
237
+ if (cellAbove && cellAbove._rowSpanCurrentOffset) {
238
+ rowCellAbove = body[lineIndex - 1 - cellAbove._rowSpanCurrentOffset][i];
239
+ rowBottomBorder = rowCellAbove && rowCellAbove.border ? rowCellAbove.border[3] : this.layout.defaultBorder;
240
+ if (rowBottomBorder && rowCellAbove && rowCellAbove.borderColor) {
241
+ borderColor = rowCellAbove.borderColor[3];
242
+ }
243
+ }
244
+
245
+ if (borderColor == null) {
246
+ borderColor = typeof this.layout.hLineColor === 'function' ? this.layout.hLineColor(lineIndex, this.tableNode, i) : this.layout.hLineColor;
247
+ }
248
+
249
+ if (!currentLine && shouldDrawLine) {
250
+ currentLine = { left: data.left, width: 0 };
251
+ }
252
+
253
+ if (shouldDrawLine) {
254
+ let colSpanIndex = 0;
255
+ if (rowCellAbove && rowCellAbove.colSpan && rowBottomBorder) {
256
+ while (rowCellAbove.colSpan > colSpanIndex) {
257
+ currentLine.width += (this.rowSpanData[i + colSpanIndex++].width || 0);
258
+ }
259
+ i += colSpanIndex - 1;
260
+ } else if (cellAbove && cellAbove.colSpan && bottomBorder) {
261
+ while (cellAbove.colSpan > colSpanIndex) {
262
+ currentLine.width += (this.rowSpanData[i + colSpanIndex++].width || 0);
263
+ }
264
+ i += colSpanIndex - 1;
265
+ } else if (currentCell && currentCell.colSpan && topBorder) {
266
+ while (currentCell.colSpan > colSpanIndex) {
267
+ currentLine.width += (this.rowSpanData[i + colSpanIndex++].width || 0);
268
+ }
269
+ i += colSpanIndex - 1;
270
+ } else {
271
+ currentLine.width += (this.rowSpanData[i].width || 0);
272
+ }
273
+ }
274
+
275
+ let y = (overrideY || 0) + offset;
276
+
277
+ if (shouldDrawLine) {
278
+ if (currentLine && currentLine.width) {
279
+ writer.addVector({
280
+ type: 'line',
281
+ x1: currentLine.left,
282
+ x2: currentLine.left + currentLine.width,
283
+ y1: y,
284
+ y2: y,
285
+ lineWidth: lineWidth,
286
+ dash: dash,
287
+ lineColor: borderColor
288
+ }, false, isNumber(overrideY), null, forcePage);
289
+ currentLine = null;
290
+ borderColor = null;
291
+ cellAbove = null;
292
+ currentCell = null;
293
+ rowCellAbove = null;
294
+ }
295
+ }
296
+ }
297
+
298
+ if (moveDown) {
299
+ writer.context().moveDown(lineWidth);
300
+ }
301
+ }
302
+ }
303
+
304
+ drawVerticalLine(x, y0, y1, vLineColIndex, writer, vLineRowIndex, beforeVLineColIndex) {
305
+ let width = this.layout.vLineWidth(vLineColIndex, this.tableNode);
306
+ if (width === 0) {
307
+ return;
308
+ }
309
+ let style = this.layout.vLineStyle(vLineColIndex, this.tableNode);
310
+ let dash;
311
+ if (style && style.dash) {
312
+ dash = style.dash;
313
+ }
314
+
315
+ let body = this.tableNode.table.body;
316
+ let cellBefore;
317
+ let currentCell;
318
+ let borderColor;
319
+
320
+ // the cell in the col before
321
+ if (vLineColIndex > 0) {
322
+ cellBefore = body[vLineRowIndex][beforeVLineColIndex];
323
+ if (cellBefore && cellBefore.borderColor) {
324
+ if (cellBefore.border ? cellBefore.border[2] : this.layout.defaultBorder) {
325
+ borderColor = cellBefore.borderColor[2];
326
+ }
327
+ }
328
+ }
329
+
330
+ // the current cell
331
+ if (borderColor == null && vLineColIndex < body.length) {
332
+ currentCell = body[vLineRowIndex][vLineColIndex];
333
+ if (currentCell && currentCell.borderColor) {
334
+ if (currentCell.border ? currentCell.border[0] : this.layout.defaultBorder) {
335
+ borderColor = currentCell.borderColor[0];
336
+ }
337
+ }
338
+ }
339
+
340
+ if (borderColor == null && cellBefore && cellBefore._rowSpanCurrentOffset) {
341
+ let rowCellBeforeAbove = body[vLineRowIndex - cellBefore._rowSpanCurrentOffset][beforeVLineColIndex];
342
+ if (rowCellBeforeAbove.borderColor) {
343
+ if (rowCellBeforeAbove.border ? rowCellBeforeAbove.border[2] : this.layout.defaultBorder) {
344
+ borderColor = rowCellBeforeAbove.borderColor[2];
345
+ }
346
+ }
347
+ }
348
+
349
+ if (borderColor == null && currentCell && currentCell._rowSpanCurrentOffset) {
350
+ let rowCurrentCellAbove = body[vLineRowIndex - currentCell._rowSpanCurrentOffset][vLineColIndex];
351
+ if (rowCurrentCellAbove.borderColor) {
352
+ if (rowCurrentCellAbove.border ? rowCurrentCellAbove.border[2] : this.layout.defaultBorder) {
353
+ borderColor = rowCurrentCellAbove.borderColor[2];
354
+ }
355
+ }
356
+ }
357
+
358
+ if (borderColor == null) {
359
+ borderColor = typeof this.layout.vLineColor === 'function' ? this.layout.vLineColor(vLineColIndex, this.tableNode, vLineRowIndex) : this.layout.vLineColor;
360
+ }
361
+
362
+ writer.addVector({
363
+ type: 'line',
364
+ x1: x + width / 2,
365
+ x2: x + width / 2,
366
+ y1: y0,
367
+ y2: y1,
368
+ lineWidth: width,
369
+ dash: dash,
370
+ lineColor: borderColor
371
+ }, false, true);
372
+ cellBefore = null;
373
+ currentCell = null;
374
+ borderColor = null;
375
+ }
376
+
377
+ endTable(writer) {
378
+ if (this.cleanUpRepeatables) {
379
+ writer.popFromRepeatables();
380
+ }
381
+ }
382
+
383
+ endRow(rowIndex, writer, pageBreaks) {
384
+ const getLineXs = () => {
385
+ let result = [];
386
+ let cols = 0;
387
+
388
+ for (let i = 0, l = this.tableNode.table.body[rowIndex].length; i < l; i++) {
389
+ if (!cols) {
390
+ result.push({ x: this.rowSpanData[i].left, index: i });
391
+
392
+ let item = this.tableNode.table.body[rowIndex][i];
393
+ cols = (item._colSpan || item.colSpan || 0);
394
+ }
395
+ if (cols > 0) {
396
+ cols--;
397
+ }
398
+ }
399
+
400
+ result.push({ x: this.rowSpanData[this.rowSpanData.length - 1].left, index: this.rowSpanData.length - 1 });
401
+
402
+ return result;
403
+ };
404
+
405
+ writer.removeListener('pageChanged', this.rowCallback);
406
+ writer.context().moveDown(this.layout.paddingBottom(rowIndex, this.tableNode));
407
+ writer.context().availableHeight += this.reservedAtBottom;
408
+
409
+ let endingPage = writer.context().page;
410
+ let endingY = writer.context().y;
411
+
412
+ let xs = getLineXs();
413
+
414
+ let ys = [];
415
+
416
+ let hasBreaks = pageBreaks && pageBreaks.length > 0;
417
+ let body = this.tableNode.table.body;
418
+
419
+ ys.push({
420
+ y0: this.rowTopY,
421
+ page: hasBreaks ? pageBreaks[0].prevPage : endingPage
422
+ });
423
+
424
+ if (hasBreaks) {
425
+ for (let i = 0, l = pageBreaks.length; i < l; i++) {
426
+ let pageBreak = pageBreaks[i];
427
+ ys[ys.length - 1].y1 = pageBreak.prevY;
428
+
429
+ ys.push({ y0: pageBreak.y, page: pageBreak.prevPage + 1 });
430
+ }
431
+ }
432
+
433
+ ys[ys.length - 1].y1 = endingY;
434
+
435
+ let skipOrphanePadding = (ys[0].y1 - ys[0].y0 === this.rowPaddingTop);
436
+ if (rowIndex === 0 && !skipOrphanePadding && !this.rowsWithoutPageBreak && !this.dontBreakRows) {
437
+ // Draw the top border of the table
438
+ let pageTableStartedAt = null;
439
+ if (pageBreaks && pageBreaks.length > 0) {
440
+ // Get the page where table started at
441
+ pageTableStartedAt = pageBreaks[0].prevPage;
442
+ }
443
+ this.drawHorizontalLine(0, writer, this._tableTopBorderY, false, pageTableStartedAt);
444
+ }
445
+ for (let yi = (skipOrphanePadding ? 1 : 0), yl = ys.length; yi < yl; yi++) {
446
+ let willBreak = yi < ys.length - 1;
447
+ let rowBreakWithoutHeader = (yi > 0 && !this.headerRows);
448
+ let hzLineOffset = rowBreakWithoutHeader ? 0 : this.topLineWidth;
449
+ let y1 = ys[yi].y0;
450
+ let y2 = ys[yi].y1;
451
+
452
+ if (willBreak) {
453
+ y2 = y2 + this.rowPaddingBottom;
454
+ }
455
+
456
+ if (writer.context().page != ys[yi].page) {
457
+ writer.context().page = ys[yi].page;
458
+
459
+ //TODO: buggy, availableHeight should be updated on every pageChanged event
460
+ // TableProcessor should be pageChanged listener, instead of processRow
461
+ this.reservedAtBottom = 0;
462
+ }
463
+
464
+ // Draw horizontal lines before the vertical lines so they are not overridden
465
+ if (willBreak && this.layout.hLineWhenBroken !== false) {
466
+ this.drawHorizontalLine(rowIndex + 1, writer, y2);
467
+ }
468
+ if (rowBreakWithoutHeader && this.layout.hLineWhenBroken !== false) {
469
+ this.drawHorizontalLine(rowIndex, writer, y1);
470
+ }
471
+
472
+ for (let i = 0, l = xs.length; i < l; i++) {
473
+ let leftCellBorder = false;
474
+ let rightCellBorder = false;
475
+ let colIndex = xs[i].index;
476
+
477
+ // current cell
478
+ if (colIndex < body[rowIndex].length) {
479
+ let cell = body[rowIndex][colIndex];
480
+ leftCellBorder = cell.border ? cell.border[0] : this.layout.defaultBorder;
481
+ rightCellBorder = cell.border ? cell.border[2] : this.layout.defaultBorder;
482
+ }
483
+
484
+ // before cell
485
+ if (colIndex > 0 && !leftCellBorder) {
486
+ let cell = body[rowIndex][colIndex - 1];
487
+ leftCellBorder = cell.border ? cell.border[2] : this.layout.defaultBorder;
488
+ }
489
+
490
+ // after cell
491
+ if (colIndex + 1 < body[rowIndex].length && !rightCellBorder) {
492
+ let cell = body[rowIndex][colIndex + 1];
493
+ rightCellBorder = cell.border ? cell.border[0] : this.layout.defaultBorder;
494
+ }
495
+
496
+ if (leftCellBorder) {
497
+ this.drawVerticalLine(xs[i].x, y1 - hzLineOffset, y2 + this.bottomLineWidth, xs[i].index, writer, rowIndex, xs[i - 1] ? xs[i - 1].index : null);
498
+ }
499
+
500
+ if (i < l - 1) {
501
+ body[rowIndex][colIndex]._willBreak = body[rowIndex][colIndex]._willBreak ?? willBreak;
502
+ if (body[rowIndex][colIndex]._bottomY === undefined) {
503
+ let bottomY = this.dontBreakRows ? y2 + this.bottomLineWidth : y2 + (this.bottomLineWidth / 2);
504
+ if (willBreak || this.dontBreakRows) {
505
+ bottomY = bottomY - this.rowPaddingBottom;
506
+ }
507
+ body[rowIndex][colIndex]._bottomY = bottomY - this.reservedAtBottom;
508
+ }
509
+ body[rowIndex][colIndex]._rowTopPageY = this.rowTopPageY;
510
+ if (this.dontBreakRows) {
511
+ body[rowIndex][colIndex]._rowTopPageYPadding = this.rowPaddingTop;
512
+ }
513
+
514
+ body[rowIndex][colIndex]._lastPageNumber = ys[yi].page + 1;
515
+
516
+ let fillColor = body[rowIndex][colIndex].fillColor;
517
+ let fillOpacity = body[rowIndex][colIndex].fillOpacity;
518
+ if (!fillColor) {
519
+ fillColor = typeof this.layout.fillColor === 'function' ? this.layout.fillColor(rowIndex, this.tableNode, colIndex) : this.layout.fillColor;
520
+ }
521
+ if (!isNumber(fillOpacity)) {
522
+ fillOpacity = typeof this.layout.fillOpacity === 'function' ? this.layout.fillOpacity(rowIndex, this.tableNode, colIndex) : this.layout.fillOpacity;
523
+ }
524
+ var overlayPattern = body[rowIndex][colIndex].overlayPattern;
525
+ var overlayOpacity = body[rowIndex][colIndex].overlayOpacity;
526
+ if (fillColor || overlayPattern) {
527
+ let widthLeftBorder = leftCellBorder ? this.layout.vLineWidth(colIndex, this.tableNode) : 0;
528
+ let widthRightBorder;
529
+ if ((colIndex === 0 || colIndex + 1 == body[rowIndex].length) && !rightCellBorder) {
530
+ widthRightBorder = this.layout.vLineWidth(colIndex + 1, this.tableNode);
531
+ } else if (rightCellBorder) {
532
+ widthRightBorder = this.layout.vLineWidth(colIndex + 1, this.tableNode) / 2;
533
+ } else {
534
+ widthRightBorder = 0;
535
+ }
536
+
537
+ let x1f = this.dontBreakRows ? xs[i].x + widthLeftBorder : xs[i].x + (widthLeftBorder / 2);
538
+ let y1f = this.dontBreakRows ? y1 : y1 - (hzLineOffset / 2);
539
+ let x2f = xs[i + 1].x + widthRightBorder;
540
+ let y2f = this.dontBreakRows ? y2 + this.bottomLineWidth : y2 + (this.bottomLineWidth / 2);
541
+ var bgWidth = x2f - x1f;
542
+ var bgHeight = y2f - y1f;
543
+ if (fillColor) {
544
+ writer.addVector({
545
+ type: 'rect',
546
+ x: x1f,
547
+ y: y1f,
548
+ w: bgWidth,
549
+ h: bgHeight,
550
+ lineWidth: 0,
551
+ color: fillColor,
552
+ fillOpacity: fillOpacity,
553
+ // mark if we are in an unbreakable block
554
+ _isFillColorFromUnbreakable: !!writer.transactionLevel
555
+ }, false, true, writer.context().backgroundLength[writer.context().page]);
556
+ }
557
+
558
+ if (overlayPattern) {
559
+ writer.addVector({
560
+ type: 'rect',
561
+ x: x1f,
562
+ y: y1f,
563
+ w: bgWidth,
564
+ h: bgHeight,
565
+ lineWidth: 0,
566
+ color: overlayPattern,
567
+ fillOpacity: overlayOpacity
568
+ }, false, true);
569
+ }
570
+ }
571
+ }
572
+ }
573
+ }
574
+
575
+ writer.context().page = endingPage;
576
+ writer.context().y = endingY;
577
+
578
+ let row = this.tableNode.table.body[rowIndex];
579
+ for (let i = 0, l = row.length; i < l; i++) {
580
+ if (row[i].rowSpan) {
581
+ this.rowSpanData[i].rowSpan = row[i].rowSpan;
582
+
583
+ // fix colSpans
584
+ if (row[i].colSpan && row[i].colSpan > 1) {
585
+ for (let j = 1; j < row[i].rowSpan; j++) {
586
+ this.tableNode.table.body[rowIndex + j][i]._colSpan = row[i].colSpan;
587
+ }
588
+ }
589
+
590
+ // fix rowSpans
591
+ if (row[i].rowSpan && row[i].rowSpan > 1) {
592
+ for (let j = 1; j < row[i].rowSpan; j++) {
593
+ this.tableNode.table.body[rowIndex + j][i]._rowSpanCurrentOffset = j;
594
+ }
595
+ }
596
+ }
597
+
598
+ if (this.rowSpanData[i].rowSpan > 0) {
599
+ this.rowSpanData[i].rowSpan--;
600
+ }
601
+ }
602
+
603
+ this.drawHorizontalLine(rowIndex + 1, writer);
604
+
605
+ if (this.headerRows && rowIndex === this.headerRows - 1) {
606
+ this.headerRepeatable = writer.currentBlockToRepeatable();
607
+ }
608
+
609
+ if (this.dontBreakRows) {
610
+ const pageChangedCallback = () => {
611
+ if (rowIndex > 0 && !this.headerRows && this.layout.hLineWhenBroken !== false) {
612
+ // Draw the top border of the row after a page break
613
+ this.drawHorizontalLine(rowIndex, writer);
614
+ }
615
+ };
616
+
617
+ writer.addListener('pageChanged', pageChangedCallback);
618
+
619
+ writer.commitUnbreakableBlock();
620
+
621
+ writer.removeListener('pageChanged', pageChangedCallback);
622
+ }
623
+
624
+ if (this.headerRepeatable && (rowIndex === (this.rowsWithoutPageBreak - 1) || rowIndex === this.tableNode.table.body.length - 1)) {
625
+ writer.commitUnbreakableBlock();
626
+ writer.pushToRepeatables(this.headerRepeatable);
627
+ this.cleanUpRepeatables = true;
628
+ this.headerRepeatable = null;
629
+ }
630
+ }
631
+ }
632
+
633
+ export default TableProcessor;