pdfmake 0.2.14 → 0.2.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/build/pdfmake.js +39163 -38748
- package/build/pdfmake.js.map +1 -1
- package/build/pdfmake.min.js +2 -2
- package/build/pdfmake.min.js.map +1 -1
- package/build/vfs_fonts.js +2 -2
- package/build-vfs.js +2 -2
- package/package.json +24 -22
- package/src/browser-extensions/pdfMake.js +30 -4
- package/src/documentContext.js +13 -1
- package/src/imageMeasure.js +10 -1
- package/src/layoutBuilder.js +1126 -989
- package/src/printer.js +17 -0
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/pdfmake.iml +0 -11
- package/.idea/vcs.xml +0 -6
package/src/layoutBuilder.js
CHANGED
|
@@ -1,989 +1,1126 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var TraversalTracker = require('./traversalTracker');
|
|
4
|
-
var DocPreprocessor = require('./docPreprocessor');
|
|
5
|
-
var DocMeasure = require('./docMeasure');
|
|
6
|
-
var DocumentContext = require('./documentContext');
|
|
7
|
-
var PageElementWriter = require('./pageElementWriter');
|
|
8
|
-
var ColumnCalculator = require('./columnCalculator');
|
|
9
|
-
var TableProcessor = require('./tableProcessor');
|
|
10
|
-
var Line = require('./line');
|
|
11
|
-
var isString = require('./helpers').isString;
|
|
12
|
-
var isArray = require('./helpers').isArray;
|
|
13
|
-
var isUndefined = require('./helpers').isUndefined;
|
|
14
|
-
var isNull = require('./helpers').isNull;
|
|
15
|
-
var pack = require('./helpers').pack;
|
|
16
|
-
var offsetVector = require('./helpers').offsetVector;
|
|
17
|
-
var fontStringify = require('./helpers').fontStringify;
|
|
18
|
-
var getNodeId = require('./helpers').getNodeId;
|
|
19
|
-
var isFunction = require('./helpers').isFunction;
|
|
20
|
-
var TextTools = require('./textTools');
|
|
21
|
-
var StyleContextStack = require('./styleContextStack');
|
|
22
|
-
var isNumber = require('./helpers').isNumber;
|
|
23
|
-
|
|
24
|
-
function addAll(target, otherArray) {
|
|
25
|
-
otherArray.forEach(function (item) {
|
|
26
|
-
target.push(item);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Creates an instance of LayoutBuilder - layout engine which turns document-definition-object
|
|
32
|
-
* into a set of pages, lines, inlines and vectors ready to be rendered into a PDF
|
|
33
|
-
*
|
|
34
|
-
* @param {Object} pageSize - an object defining page width and height
|
|
35
|
-
* @param {Object} pageMargins - an object defining top, left, right and bottom margins
|
|
36
|
-
*/
|
|
37
|
-
function LayoutBuilder(pageSize, pageMargins, imageMeasure, svgMeasure) {
|
|
38
|
-
this.pageSize = pageSize;
|
|
39
|
-
this.pageMargins = pageMargins;
|
|
40
|
-
this.tracker = new TraversalTracker();
|
|
41
|
-
this.imageMeasure = imageMeasure;
|
|
42
|
-
this.svgMeasure = svgMeasure;
|
|
43
|
-
this.tableLayouts = {};
|
|
44
|
-
this.nestedLevel = 0;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
LayoutBuilder.prototype.registerTableLayouts = function (tableLayouts) {
|
|
48
|
-
this.tableLayouts = pack(this.tableLayouts, tableLayouts);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Executes layout engine on document-definition-object and creates an array of pages
|
|
53
|
-
* containing positioned Blocks, Lines and inlines
|
|
54
|
-
*
|
|
55
|
-
* @param {Object} docStructure document-definition-object
|
|
56
|
-
* @param {Object} fontProvider font provider
|
|
57
|
-
* @param {Object} styleDictionary dictionary with style definitions
|
|
58
|
-
* @param {Object} defaultStyle default style definition
|
|
59
|
-
* @return {Array} an array of pages
|
|
60
|
-
*/
|
|
61
|
-
LayoutBuilder.prototype.layoutDocument = function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark, pageBreakBeforeFct) {
|
|
62
|
-
|
|
63
|
-
function addPageBreaksIfNecessary(linearNodeList, pages) {
|
|
64
|
-
|
|
65
|
-
if (!isFunction(pageBreakBeforeFct)) {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
linearNodeList = linearNodeList.filter(function (node) {
|
|
70
|
-
return node.positions.length > 0;
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
linearNodeList.forEach(function (node) {
|
|
74
|
-
var nodeInfo = {};
|
|
75
|
-
[
|
|
76
|
-
'id', 'text', 'ul', 'ol', 'table', 'image', 'qr', 'canvas', 'svg', 'columns',
|
|
77
|
-
'headlineLevel', 'style', 'pageBreak', 'pageOrientation',
|
|
78
|
-
'width', 'height'
|
|
79
|
-
].forEach(function (key) {
|
|
80
|
-
if (node[key] !== undefined) {
|
|
81
|
-
nodeInfo[key] = node[key];
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
nodeInfo.startPosition = node.positions[0];
|
|
85
|
-
nodeInfo.pageNumbers = Array.from(new Set(node.positions.map(function (node) { return node.pageNumber; })));
|
|
86
|
-
nodeInfo.pages = pages.length;
|
|
87
|
-
nodeInfo.stack = isArray(node.stack);
|
|
88
|
-
|
|
89
|
-
node.nodeInfo = nodeInfo;
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
for (var index = 0; index < linearNodeList.length; index++) {
|
|
93
|
-
var node = linearNodeList[index];
|
|
94
|
-
if (node.pageBreak !== 'before' && !node.pageBreakCalculated) {
|
|
95
|
-
node.pageBreakCalculated = true;
|
|
96
|
-
var pageNumber = node.nodeInfo.pageNumbers[0];
|
|
97
|
-
var followingNodesOnPage = [];
|
|
98
|
-
var nodesOnNextPage = [];
|
|
99
|
-
var previousNodesOnPage = [];
|
|
100
|
-
if (pageBreakBeforeFct.length > 1) {
|
|
101
|
-
for (var ii = index + 1, l = linearNodeList.length; ii < l; ii++) {
|
|
102
|
-
if (linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber) > -1) {
|
|
103
|
-
followingNodesOnPage.push(linearNodeList[ii].nodeInfo);
|
|
104
|
-
}
|
|
105
|
-
if (pageBreakBeforeFct.length > 2 && linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber + 1) > -1) {
|
|
106
|
-
nodesOnNextPage.push(linearNodeList[ii].nodeInfo);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
if (pageBreakBeforeFct.length > 3) {
|
|
111
|
-
for (var ii = 0; ii < index; ii++) {
|
|
112
|
-
if (linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber) > -1) {
|
|
113
|
-
previousNodesOnPage.push(linearNodeList[ii].nodeInfo);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (pageBreakBeforeFct(node.nodeInfo, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage)) {
|
|
118
|
-
node.pageBreak = 'before';
|
|
119
|
-
return true;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
this.docPreprocessor = new DocPreprocessor();
|
|
128
|
-
this.docMeasure = new DocMeasure(fontProvider, styleDictionary, defaultStyle, this.imageMeasure, this.svgMeasure, this.tableLayouts, images);
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
function resetXYs(result) {
|
|
132
|
-
result.linearNodeList.forEach(function (node) {
|
|
133
|
-
node.resetXY();
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
var result = this.tryLayoutDocument(docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark);
|
|
138
|
-
while (addPageBreaksIfNecessary(result.linearNodeList, result.pages)) {
|
|
139
|
-
resetXYs(result);
|
|
140
|
-
result = this.tryLayoutDocument(docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return result.pages;
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
LayoutBuilder.prototype.tryLayoutDocument = function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark, pageBreakBeforeFct) {
|
|
147
|
-
|
|
148
|
-
this.linearNodeList = [];
|
|
149
|
-
docStructure = this.docPreprocessor.preprocessDocument(docStructure);
|
|
150
|
-
docStructure = this.docMeasure.measureDocument(docStructure);
|
|
151
|
-
|
|
152
|
-
this.writer = new PageElementWriter(
|
|
153
|
-
new DocumentContext(this.pageSize, this.pageMargins), this.tracker);
|
|
154
|
-
|
|
155
|
-
var _this = this;
|
|
156
|
-
this.writer.context().tracker.startTracking('pageAdded', function () {
|
|
157
|
-
_this.addBackground(background);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
this.addBackground(background);
|
|
161
|
-
this.processNode(docStructure);
|
|
162
|
-
this.addHeadersAndFooters(header, footer);
|
|
163
|
-
if (watermark != null) {
|
|
164
|
-
this.addWatermark(watermark, fontProvider, defaultStyle);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return { pages: this.writer.context().pages, linearNodeList: this.linearNodeList };
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
LayoutBuilder.prototype.addBackground = function (background) {
|
|
172
|
-
var backgroundGetter = isFunction(background) ? background : function () {
|
|
173
|
-
return background;
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
var context = this.writer.context();
|
|
177
|
-
var pageSize = context.getCurrentPage().pageSize;
|
|
178
|
-
|
|
179
|
-
var pageBackground = backgroundGetter(context.page + 1, pageSize);
|
|
180
|
-
|
|
181
|
-
if (pageBackground) {
|
|
182
|
-
this.writer.beginUnbreakableBlock(pageSize.width, pageSize.height);
|
|
183
|
-
pageBackground = this.docPreprocessor.preprocessDocument(pageBackground);
|
|
184
|
-
this.processNode(this.docMeasure.measureDocument(pageBackground));
|
|
185
|
-
this.writer.commitUnbreakableBlock(0, 0);
|
|
186
|
-
context.backgroundLength[context.page] += pageBackground.positions.length;
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
LayoutBuilder.prototype.addStaticRepeatable = function (headerOrFooter, sizeFunction) {
|
|
191
|
-
this.addDynamicRepeatable(function () {
|
|
192
|
-
return JSON.parse(JSON.stringify(headerOrFooter)); // copy to new object
|
|
193
|
-
}, sizeFunction);
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
LayoutBuilder.prototype.addDynamicRepeatable = function (nodeGetter, sizeFunction) {
|
|
197
|
-
var pages = this.writer.context().pages;
|
|
198
|
-
|
|
199
|
-
for (var pageIndex = 0, l = pages.length; pageIndex < l; pageIndex++) {
|
|
200
|
-
this.writer.context().page = pageIndex;
|
|
201
|
-
|
|
202
|
-
var node = nodeGetter(pageIndex + 1, l, this.writer.context().pages[pageIndex].pageSize);
|
|
203
|
-
|
|
204
|
-
if (node) {
|
|
205
|
-
var sizes = sizeFunction(this.writer.context().getCurrentPage().pageSize, this.pageMargins);
|
|
206
|
-
this.writer.beginUnbreakableBlock(sizes.width, sizes.height);
|
|
207
|
-
node = this.docPreprocessor.preprocessDocument(node);
|
|
208
|
-
this.processNode(this.docMeasure.measureDocument(node));
|
|
209
|
-
this.writer.commitUnbreakableBlock(sizes.x, sizes.y);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
LayoutBuilder.prototype.addHeadersAndFooters = function (header, footer) {
|
|
215
|
-
var headerSizeFct = function (pageSize, pageMargins) {
|
|
216
|
-
return {
|
|
217
|
-
x: 0,
|
|
218
|
-
y: 0,
|
|
219
|
-
width: pageSize.width,
|
|
220
|
-
height: pageMargins.top
|
|
221
|
-
};
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
var footerSizeFct = function (pageSize, pageMargins) {
|
|
225
|
-
return {
|
|
226
|
-
x: 0,
|
|
227
|
-
y: pageSize.height - pageMargins.bottom,
|
|
228
|
-
width: pageSize.width,
|
|
229
|
-
height: pageMargins.bottom
|
|
230
|
-
};
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
if (isFunction(header)) {
|
|
234
|
-
this.addDynamicRepeatable(header, headerSizeFct);
|
|
235
|
-
} else if (header) {
|
|
236
|
-
this.addStaticRepeatable(header, headerSizeFct);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (isFunction(footer)) {
|
|
240
|
-
this.addDynamicRepeatable(footer, footerSizeFct);
|
|
241
|
-
} else if (footer) {
|
|
242
|
-
this.addStaticRepeatable(footer, footerSizeFct);
|
|
243
|
-
}
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
LayoutBuilder.prototype.addWatermark = function (watermark, fontProvider, defaultStyle) {
|
|
247
|
-
if (isString(watermark)) {
|
|
248
|
-
watermark = { 'text': watermark };
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
if (!watermark.text) { // empty watermark text
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
watermark
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
watermark.
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
vector.
|
|
353
|
-
vector.
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
self.
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
self.
|
|
399
|
-
} else if (node.
|
|
400
|
-
self.
|
|
401
|
-
} else if (node.
|
|
402
|
-
self.
|
|
403
|
-
} else if (node.
|
|
404
|
-
self.
|
|
405
|
-
} else if (node.
|
|
406
|
-
self.
|
|
407
|
-
} else if (node.
|
|
408
|
-
self.
|
|
409
|
-
} else if (node.
|
|
410
|
-
self.
|
|
411
|
-
} else if (node.
|
|
412
|
-
self.
|
|
413
|
-
} else if (node.
|
|
414
|
-
self.
|
|
415
|
-
} else if (
|
|
416
|
-
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
if (
|
|
420
|
-
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
if (
|
|
424
|
-
self.writer.
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
self.writer.
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
self.writer.
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
self.writer.
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
//
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
if (
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
if
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
if (
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
}
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
if (
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
this.writer.
|
|
972
|
-
}
|
|
973
|
-
|
|
974
|
-
this.writer
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var TraversalTracker = require('./traversalTracker');
|
|
4
|
+
var DocPreprocessor = require('./docPreprocessor');
|
|
5
|
+
var DocMeasure = require('./docMeasure');
|
|
6
|
+
var DocumentContext = require('./documentContext');
|
|
7
|
+
var PageElementWriter = require('./pageElementWriter');
|
|
8
|
+
var ColumnCalculator = require('./columnCalculator');
|
|
9
|
+
var TableProcessor = require('./tableProcessor');
|
|
10
|
+
var Line = require('./line');
|
|
11
|
+
var isString = require('./helpers').isString;
|
|
12
|
+
var isArray = require('./helpers').isArray;
|
|
13
|
+
var isUndefined = require('./helpers').isUndefined;
|
|
14
|
+
var isNull = require('./helpers').isNull;
|
|
15
|
+
var pack = require('./helpers').pack;
|
|
16
|
+
var offsetVector = require('./helpers').offsetVector;
|
|
17
|
+
var fontStringify = require('./helpers').fontStringify;
|
|
18
|
+
var getNodeId = require('./helpers').getNodeId;
|
|
19
|
+
var isFunction = require('./helpers').isFunction;
|
|
20
|
+
var TextTools = require('./textTools');
|
|
21
|
+
var StyleContextStack = require('./styleContextStack');
|
|
22
|
+
var isNumber = require('./helpers').isNumber;
|
|
23
|
+
|
|
24
|
+
function addAll(target, otherArray) {
|
|
25
|
+
otherArray.forEach(function (item) {
|
|
26
|
+
target.push(item);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Creates an instance of LayoutBuilder - layout engine which turns document-definition-object
|
|
32
|
+
* into a set of pages, lines, inlines and vectors ready to be rendered into a PDF
|
|
33
|
+
*
|
|
34
|
+
* @param {Object} pageSize - an object defining page width and height
|
|
35
|
+
* @param {Object} pageMargins - an object defining top, left, right and bottom margins
|
|
36
|
+
*/
|
|
37
|
+
function LayoutBuilder(pageSize, pageMargins, imageMeasure, svgMeasure) {
|
|
38
|
+
this.pageSize = pageSize;
|
|
39
|
+
this.pageMargins = pageMargins;
|
|
40
|
+
this.tracker = new TraversalTracker();
|
|
41
|
+
this.imageMeasure = imageMeasure;
|
|
42
|
+
this.svgMeasure = svgMeasure;
|
|
43
|
+
this.tableLayouts = {};
|
|
44
|
+
this.nestedLevel = 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
LayoutBuilder.prototype.registerTableLayouts = function (tableLayouts) {
|
|
48
|
+
this.tableLayouts = pack(this.tableLayouts, tableLayouts);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Executes layout engine on document-definition-object and creates an array of pages
|
|
53
|
+
* containing positioned Blocks, Lines and inlines
|
|
54
|
+
*
|
|
55
|
+
* @param {Object} docStructure document-definition-object
|
|
56
|
+
* @param {Object} fontProvider font provider
|
|
57
|
+
* @param {Object} styleDictionary dictionary with style definitions
|
|
58
|
+
* @param {Object} defaultStyle default style definition
|
|
59
|
+
* @return {Array} an array of pages
|
|
60
|
+
*/
|
|
61
|
+
LayoutBuilder.prototype.layoutDocument = function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark, pageBreakBeforeFct) {
|
|
62
|
+
|
|
63
|
+
function addPageBreaksIfNecessary(linearNodeList, pages) {
|
|
64
|
+
|
|
65
|
+
if (!isFunction(pageBreakBeforeFct)) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
linearNodeList = linearNodeList.filter(function (node) {
|
|
70
|
+
return node.positions.length > 0;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
linearNodeList.forEach(function (node) {
|
|
74
|
+
var nodeInfo = {};
|
|
75
|
+
[
|
|
76
|
+
'id', 'text', 'ul', 'ol', 'table', 'image', 'qr', 'canvas', 'svg', 'columns',
|
|
77
|
+
'headlineLevel', 'style', 'pageBreak', 'pageOrientation',
|
|
78
|
+
'width', 'height'
|
|
79
|
+
].forEach(function (key) {
|
|
80
|
+
if (node[key] !== undefined) {
|
|
81
|
+
nodeInfo[key] = node[key];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
nodeInfo.startPosition = node.positions[0];
|
|
85
|
+
nodeInfo.pageNumbers = Array.from(new Set(node.positions.map(function (node) { return node.pageNumber; })));
|
|
86
|
+
nodeInfo.pages = pages.length;
|
|
87
|
+
nodeInfo.stack = isArray(node.stack);
|
|
88
|
+
|
|
89
|
+
node.nodeInfo = nodeInfo;
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
for (var index = 0; index < linearNodeList.length; index++) {
|
|
93
|
+
var node = linearNodeList[index];
|
|
94
|
+
if (node.pageBreak !== 'before' && !node.pageBreakCalculated) {
|
|
95
|
+
node.pageBreakCalculated = true;
|
|
96
|
+
var pageNumber = node.nodeInfo.pageNumbers[0];
|
|
97
|
+
var followingNodesOnPage = [];
|
|
98
|
+
var nodesOnNextPage = [];
|
|
99
|
+
var previousNodesOnPage = [];
|
|
100
|
+
if (pageBreakBeforeFct.length > 1) {
|
|
101
|
+
for (var ii = index + 1, l = linearNodeList.length; ii < l; ii++) {
|
|
102
|
+
if (linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber) > -1) {
|
|
103
|
+
followingNodesOnPage.push(linearNodeList[ii].nodeInfo);
|
|
104
|
+
}
|
|
105
|
+
if (pageBreakBeforeFct.length > 2 && linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber + 1) > -1) {
|
|
106
|
+
nodesOnNextPage.push(linearNodeList[ii].nodeInfo);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (pageBreakBeforeFct.length > 3) {
|
|
111
|
+
for (var ii = 0; ii < index; ii++) {
|
|
112
|
+
if (linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber) > -1) {
|
|
113
|
+
previousNodesOnPage.push(linearNodeList[ii].nodeInfo);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (pageBreakBeforeFct(node.nodeInfo, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage)) {
|
|
118
|
+
node.pageBreak = 'before';
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
this.docPreprocessor = new DocPreprocessor();
|
|
128
|
+
this.docMeasure = new DocMeasure(fontProvider, styleDictionary, defaultStyle, this.imageMeasure, this.svgMeasure, this.tableLayouts, images);
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
function resetXYs(result) {
|
|
132
|
+
result.linearNodeList.forEach(function (node) {
|
|
133
|
+
node.resetXY();
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
var result = this.tryLayoutDocument(docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark);
|
|
138
|
+
while (addPageBreaksIfNecessary(result.linearNodeList, result.pages)) {
|
|
139
|
+
resetXYs(result);
|
|
140
|
+
result = this.tryLayoutDocument(docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return result.pages;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
LayoutBuilder.prototype.tryLayoutDocument = function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark, pageBreakBeforeFct) {
|
|
147
|
+
|
|
148
|
+
this.linearNodeList = [];
|
|
149
|
+
docStructure = this.docPreprocessor.preprocessDocument(docStructure);
|
|
150
|
+
docStructure = this.docMeasure.measureDocument(docStructure);
|
|
151
|
+
|
|
152
|
+
this.writer = new PageElementWriter(
|
|
153
|
+
new DocumentContext(this.pageSize, this.pageMargins), this.tracker);
|
|
154
|
+
|
|
155
|
+
var _this = this;
|
|
156
|
+
this.writer.context().tracker.startTracking('pageAdded', function () {
|
|
157
|
+
_this.addBackground(background);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
this.addBackground(background);
|
|
161
|
+
this.processNode(docStructure);
|
|
162
|
+
this.addHeadersAndFooters(header, footer);
|
|
163
|
+
if (watermark != null) {
|
|
164
|
+
this.addWatermark(watermark, fontProvider, defaultStyle);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return { pages: this.writer.context().pages, linearNodeList: this.linearNodeList };
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
LayoutBuilder.prototype.addBackground = function (background) {
|
|
172
|
+
var backgroundGetter = isFunction(background) ? background : function () {
|
|
173
|
+
return background;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
var context = this.writer.context();
|
|
177
|
+
var pageSize = context.getCurrentPage().pageSize;
|
|
178
|
+
|
|
179
|
+
var pageBackground = backgroundGetter(context.page + 1, pageSize);
|
|
180
|
+
|
|
181
|
+
if (pageBackground) {
|
|
182
|
+
this.writer.beginUnbreakableBlock(pageSize.width, pageSize.height);
|
|
183
|
+
pageBackground = this.docPreprocessor.preprocessDocument(pageBackground);
|
|
184
|
+
this.processNode(this.docMeasure.measureDocument(pageBackground));
|
|
185
|
+
this.writer.commitUnbreakableBlock(0, 0);
|
|
186
|
+
context.backgroundLength[context.page] += pageBackground.positions.length;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
LayoutBuilder.prototype.addStaticRepeatable = function (headerOrFooter, sizeFunction) {
|
|
191
|
+
this.addDynamicRepeatable(function () {
|
|
192
|
+
return JSON.parse(JSON.stringify(headerOrFooter)); // copy to new object
|
|
193
|
+
}, sizeFunction);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
LayoutBuilder.prototype.addDynamicRepeatable = function (nodeGetter, sizeFunction) {
|
|
197
|
+
var pages = this.writer.context().pages;
|
|
198
|
+
|
|
199
|
+
for (var pageIndex = 0, l = pages.length; pageIndex < l; pageIndex++) {
|
|
200
|
+
this.writer.context().page = pageIndex;
|
|
201
|
+
|
|
202
|
+
var node = nodeGetter(pageIndex + 1, l, this.writer.context().pages[pageIndex].pageSize);
|
|
203
|
+
|
|
204
|
+
if (node) {
|
|
205
|
+
var sizes = sizeFunction(this.writer.context().getCurrentPage().pageSize, this.pageMargins);
|
|
206
|
+
this.writer.beginUnbreakableBlock(sizes.width, sizes.height);
|
|
207
|
+
node = this.docPreprocessor.preprocessDocument(node);
|
|
208
|
+
this.processNode(this.docMeasure.measureDocument(node));
|
|
209
|
+
this.writer.commitUnbreakableBlock(sizes.x, sizes.y);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
LayoutBuilder.prototype.addHeadersAndFooters = function (header, footer) {
|
|
215
|
+
var headerSizeFct = function (pageSize, pageMargins) {
|
|
216
|
+
return {
|
|
217
|
+
x: 0,
|
|
218
|
+
y: 0,
|
|
219
|
+
width: pageSize.width,
|
|
220
|
+
height: pageMargins.top
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
var footerSizeFct = function (pageSize, pageMargins) {
|
|
225
|
+
return {
|
|
226
|
+
x: 0,
|
|
227
|
+
y: pageSize.height - pageMargins.bottom,
|
|
228
|
+
width: pageSize.width,
|
|
229
|
+
height: pageMargins.bottom
|
|
230
|
+
};
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
if (isFunction(header)) {
|
|
234
|
+
this.addDynamicRepeatable(header, headerSizeFct);
|
|
235
|
+
} else if (header) {
|
|
236
|
+
this.addStaticRepeatable(header, headerSizeFct);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (isFunction(footer)) {
|
|
240
|
+
this.addDynamicRepeatable(footer, footerSizeFct);
|
|
241
|
+
} else if (footer) {
|
|
242
|
+
this.addStaticRepeatable(footer, footerSizeFct);
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
LayoutBuilder.prototype.addWatermark = function (watermark, fontProvider, defaultStyle) {
|
|
247
|
+
if (isString(watermark)) {
|
|
248
|
+
watermark = { 'text': watermark };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (!watermark.text) { // empty watermark text
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
var pages = this.writer.context().pages;
|
|
256
|
+
for (var i = 0, l = pages.length; i < l; i++) {
|
|
257
|
+
pages[i].watermark = getWatermarkObject({ ...watermark }, pages[i].pageSize, fontProvider, defaultStyle);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function getWatermarkObject(watermark, pageSize, fontProvider, defaultStyle) {
|
|
261
|
+
watermark.font = watermark.font || defaultStyle.font || 'Roboto';
|
|
262
|
+
watermark.fontSize = watermark.fontSize || 'auto';
|
|
263
|
+
watermark.color = watermark.color || 'black';
|
|
264
|
+
watermark.opacity = isNumber(watermark.opacity) ? watermark.opacity : 0.6;
|
|
265
|
+
watermark.bold = watermark.bold || false;
|
|
266
|
+
watermark.italics = watermark.italics || false;
|
|
267
|
+
watermark.angle = !isUndefined(watermark.angle) && !isNull(watermark.angle) ? watermark.angle : null;
|
|
268
|
+
|
|
269
|
+
if (watermark.angle === null) {
|
|
270
|
+
watermark.angle = Math.atan2(pageSize.height, pageSize.width) * -180 / Math.PI;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (watermark.fontSize === 'auto') {
|
|
274
|
+
watermark.fontSize = getWatermarkFontSize(pageSize, watermark, fontProvider);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
var watermarkObject = {
|
|
278
|
+
text: watermark.text,
|
|
279
|
+
font: fontProvider.provideFont(watermark.font, watermark.bold, watermark.italics),
|
|
280
|
+
fontSize: watermark.fontSize,
|
|
281
|
+
color: watermark.color,
|
|
282
|
+
opacity: watermark.opacity,
|
|
283
|
+
angle: watermark.angle
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
watermarkObject._size = getWatermarkSize(watermark, fontProvider);
|
|
287
|
+
|
|
288
|
+
return watermarkObject;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function getWatermarkSize(watermark, fontProvider) {
|
|
292
|
+
var textTools = new TextTools(fontProvider);
|
|
293
|
+
var styleContextStack = new StyleContextStack(null, { font: watermark.font, bold: watermark.bold, italics: watermark.italics });
|
|
294
|
+
|
|
295
|
+
styleContextStack.push({
|
|
296
|
+
fontSize: watermark.fontSize
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
var size = textTools.sizeOfString(watermark.text, styleContextStack);
|
|
300
|
+
var rotatedSize = textTools.sizeOfRotatedText(watermark.text, watermark.angle, styleContextStack);
|
|
301
|
+
|
|
302
|
+
return { size: size, rotatedSize: rotatedSize };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function getWatermarkFontSize(pageSize, watermark, fontProvider) {
|
|
306
|
+
var textTools = new TextTools(fontProvider);
|
|
307
|
+
var styleContextStack = new StyleContextStack(null, { font: watermark.font, bold: watermark.bold, italics: watermark.italics });
|
|
308
|
+
var rotatedSize;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Binary search the best font size.
|
|
312
|
+
* Initial bounds [0, 1000]
|
|
313
|
+
* Break when range < 1
|
|
314
|
+
*/
|
|
315
|
+
var a = 0;
|
|
316
|
+
var b = 1000;
|
|
317
|
+
var c = (a + b) / 2;
|
|
318
|
+
while (Math.abs(a - b) > 1) {
|
|
319
|
+
styleContextStack.push({
|
|
320
|
+
fontSize: c
|
|
321
|
+
});
|
|
322
|
+
rotatedSize = textTools.sizeOfRotatedText(watermark.text, watermark.angle, styleContextStack);
|
|
323
|
+
if (rotatedSize.width > pageSize.width) {
|
|
324
|
+
b = c;
|
|
325
|
+
c = (a + b) / 2;
|
|
326
|
+
} else if (rotatedSize.width < pageSize.width) {
|
|
327
|
+
if (rotatedSize.height > pageSize.height) {
|
|
328
|
+
b = c;
|
|
329
|
+
c = (a + b) / 2;
|
|
330
|
+
} else {
|
|
331
|
+
a = c;
|
|
332
|
+
c = (a + b) / 2;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
styleContextStack.pop();
|
|
336
|
+
}
|
|
337
|
+
/*
|
|
338
|
+
End binary search
|
|
339
|
+
*/
|
|
340
|
+
return c;
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
function decorateNode(node) {
|
|
345
|
+
var x = node.x, y = node.y;
|
|
346
|
+
node.positions = [];
|
|
347
|
+
|
|
348
|
+
if (isArray(node.canvas)) {
|
|
349
|
+
node.canvas.forEach(function (vector) {
|
|
350
|
+
var x = vector.x, y = vector.y, x1 = vector.x1, y1 = vector.y1, x2 = vector.x2, y2 = vector.y2;
|
|
351
|
+
vector.resetXY = function () {
|
|
352
|
+
vector.x = x;
|
|
353
|
+
vector.y = y;
|
|
354
|
+
vector.x1 = x1;
|
|
355
|
+
vector.y1 = y1;
|
|
356
|
+
vector.x2 = x2;
|
|
357
|
+
vector.y2 = y2;
|
|
358
|
+
};
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
node.resetXY = function () {
|
|
363
|
+
node.x = x;
|
|
364
|
+
node.y = y;
|
|
365
|
+
if (isArray(node.canvas)) {
|
|
366
|
+
node.canvas.forEach(function (vector) {
|
|
367
|
+
vector.resetXY();
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
LayoutBuilder.prototype.processNode = function (node) {
|
|
374
|
+
var self = this;
|
|
375
|
+
|
|
376
|
+
this.linearNodeList.push(node);
|
|
377
|
+
decorateNode(node);
|
|
378
|
+
|
|
379
|
+
applyMargins(function () {
|
|
380
|
+
var unbreakable = node.unbreakable;
|
|
381
|
+
if (unbreakable) {
|
|
382
|
+
self.writer.beginUnbreakableBlock();
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
var absPosition = node.absolutePosition;
|
|
386
|
+
if (absPosition) {
|
|
387
|
+
self.writer.context().beginDetachedBlock();
|
|
388
|
+
self.writer.context().moveTo(absPosition.x || 0, absPosition.y || 0);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
var relPosition = node.relativePosition;
|
|
392
|
+
if (relPosition) {
|
|
393
|
+
self.writer.context().beginDetachedBlock();
|
|
394
|
+
self.writer.context().moveToRelative(relPosition.x || 0, relPosition.y || 0);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (node.stack) {
|
|
398
|
+
self.processVerticalContainer(node);
|
|
399
|
+
} else if (node.columns) {
|
|
400
|
+
self.processColumns(node);
|
|
401
|
+
} else if (node.ul) {
|
|
402
|
+
self.processList(false, node);
|
|
403
|
+
} else if (node.ol) {
|
|
404
|
+
self.processList(true, node);
|
|
405
|
+
} else if (node.table) {
|
|
406
|
+
self.processTable(node);
|
|
407
|
+
} else if (node.text !== undefined) {
|
|
408
|
+
self.processLeaf(node);
|
|
409
|
+
} else if (node.toc) {
|
|
410
|
+
self.processToc(node);
|
|
411
|
+
} else if (node.image) {
|
|
412
|
+
self.processImage(node);
|
|
413
|
+
} else if (node.svg) {
|
|
414
|
+
self.processSVG(node);
|
|
415
|
+
} else if (node.canvas) {
|
|
416
|
+
self.processCanvas(node);
|
|
417
|
+
} else if (node.qr) {
|
|
418
|
+
self.processQr(node);
|
|
419
|
+
} else if (!node._span) {
|
|
420
|
+
throw 'Unrecognized document structure: ' + JSON.stringify(node, fontStringify);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (absPosition || relPosition) {
|
|
424
|
+
self.writer.context().endDetachedBlock();
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (unbreakable) {
|
|
428
|
+
self.writer.commitUnbreakableBlock();
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
function applyMargins(callback) {
|
|
433
|
+
var margin = node._margin;
|
|
434
|
+
|
|
435
|
+
if (node.pageBreak === 'before') {
|
|
436
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
437
|
+
} else if (node.pageBreak === 'beforeOdd') {
|
|
438
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
439
|
+
if ((self.writer.context().page + 1) % 2 === 1) {
|
|
440
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
441
|
+
}
|
|
442
|
+
} else if (node.pageBreak === 'beforeEven') {
|
|
443
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
444
|
+
if ((self.writer.context().page + 1) % 2 === 0) {
|
|
445
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const isDetachedBlock = node.relativePosition || node.absolutePosition;
|
|
450
|
+
|
|
451
|
+
// Detached nodes have no margins, their position is only determined by 'x' and 'y'
|
|
452
|
+
if (margin && !isDetachedBlock) {
|
|
453
|
+
const availableHeight = self.writer.context().availableHeight;
|
|
454
|
+
// If top margin is bigger than available space, move to next page
|
|
455
|
+
// Necessary for nodes inside tables
|
|
456
|
+
if (availableHeight - margin[1] < 0) {
|
|
457
|
+
// Consume the whole available space
|
|
458
|
+
self.writer.context().moveDown(availableHeight);
|
|
459
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
460
|
+
/**
|
|
461
|
+
* TODO - Something to consider:
|
|
462
|
+
* Right now the node starts at the top of next page (after header)
|
|
463
|
+
* Another option would be to apply just the top margin that has not been consumed in the page before
|
|
464
|
+
* It would something like: this.write.context().moveDown(margin[1] - availableHeight)
|
|
465
|
+
*/
|
|
466
|
+
} else {
|
|
467
|
+
self.writer.context().moveDown(margin[1]);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// Apply lateral margins
|
|
471
|
+
self.writer.context().addMargin(margin[0], margin[2]);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
callback();
|
|
475
|
+
|
|
476
|
+
// Detached nodes have no margins, their position is only determined by 'x' and 'y'
|
|
477
|
+
if (margin && !isDetachedBlock) {
|
|
478
|
+
const availableHeight = self.writer.context().availableHeight;
|
|
479
|
+
// If bottom margin is bigger than available space, move to next page
|
|
480
|
+
// Necessary for nodes inside tables
|
|
481
|
+
if (availableHeight - margin[3] < 0) {
|
|
482
|
+
self.writer.context().moveDown(availableHeight);
|
|
483
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
484
|
+
/**
|
|
485
|
+
* TODO - Something to consider:
|
|
486
|
+
* Right now next node starts at the top of next page (after header)
|
|
487
|
+
* Another option would be to apply the bottom margin that has not been consumed in the next page?
|
|
488
|
+
* It would something like: this.write.context().moveDown(margin[3] - availableHeight)
|
|
489
|
+
*/
|
|
490
|
+
} else {
|
|
491
|
+
self.writer.context().moveDown(margin[3]);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// Apply lateral margins
|
|
495
|
+
self.writer.context().addMargin(-margin[0], -margin[2]);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (node.pageBreak === 'after') {
|
|
499
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
500
|
+
} else if (node.pageBreak === 'afterOdd') {
|
|
501
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
502
|
+
if ((self.writer.context().page + 1) % 2 === 1) {
|
|
503
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
504
|
+
}
|
|
505
|
+
} else if (node.pageBreak === 'afterEven') {
|
|
506
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
507
|
+
if ((self.writer.context().page + 1) % 2 === 0) {
|
|
508
|
+
self.writer.moveToNextPage(node.pageOrientation);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
// vertical container
|
|
515
|
+
LayoutBuilder.prototype.processVerticalContainer = function (node) {
|
|
516
|
+
var self = this;
|
|
517
|
+
node.stack.forEach(function (item) {
|
|
518
|
+
self.processNode(item);
|
|
519
|
+
addAll(node.positions, item.positions);
|
|
520
|
+
|
|
521
|
+
//TODO: paragraph gap
|
|
522
|
+
});
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
// columns
|
|
526
|
+
LayoutBuilder.prototype.processColumns = function (columnNode) {
|
|
527
|
+
this.nestedLevel++;
|
|
528
|
+
var columns = columnNode.columns;
|
|
529
|
+
var availableWidth = this.writer.context().availableWidth;
|
|
530
|
+
var gaps = gapArray(columnNode._gap);
|
|
531
|
+
|
|
532
|
+
if (gaps) {
|
|
533
|
+
availableWidth -= (gaps.length - 1) * columnNode._gap;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
ColumnCalculator.buildColumnWidths(columns, availableWidth);
|
|
537
|
+
var result = this.processRow({
|
|
538
|
+
marginX: columnNode._margin ? [columnNode._margin[0], columnNode._margin[2]] : [0, 0],
|
|
539
|
+
cells: columns,
|
|
540
|
+
widths: columns,
|
|
541
|
+
gaps
|
|
542
|
+
});
|
|
543
|
+
addAll(columnNode.positions, result.positions);
|
|
544
|
+
|
|
545
|
+
this.nestedLevel--;
|
|
546
|
+
if (this.nestedLevel === 0) {
|
|
547
|
+
this.writer.context().resetMarginXTopParent();
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function gapArray(gap) {
|
|
551
|
+
if (!gap) {
|
|
552
|
+
return null;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
var gaps = [];
|
|
556
|
+
gaps.push(0);
|
|
557
|
+
|
|
558
|
+
for (var i = columns.length - 1; i > 0; i--) {
|
|
559
|
+
gaps.push(gap);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
return gaps;
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Searches for a cell in the same row that starts a rowspan and is positioned immediately before the current cell.
|
|
568
|
+
* Alternatively, it finds a cell where the colspan initiating the rowspan extends to the cell just before the current one.
|
|
569
|
+
*
|
|
570
|
+
* @param {Array<object>} arr - An array representing cells in a row.
|
|
571
|
+
* @param {number} i - The index of the current cell to search backward from.
|
|
572
|
+
* @returns {object|null} The starting cell of the rowspan if found; otherwise, `null`.
|
|
573
|
+
*/
|
|
574
|
+
LayoutBuilder.prototype._findStartingRowSpanCell = function (arr, i) {
|
|
575
|
+
var requiredColspan = 1;
|
|
576
|
+
for (var index = i - 1; index >= 0; index--) {
|
|
577
|
+
if (!arr[index]._span) {
|
|
578
|
+
if (arr[index].rowSpan > 1 && (arr[index].colSpan || 1) === requiredColspan) {
|
|
579
|
+
return arr[index];
|
|
580
|
+
} else {
|
|
581
|
+
return null;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
requiredColspan++;
|
|
585
|
+
}
|
|
586
|
+
return null;
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Retrieves a page break description for a specified page from a list of page breaks.
|
|
591
|
+
*
|
|
592
|
+
* @param {Array<object>} pageBreaks - An array of page break descriptions, each containing `prevPage` properties.
|
|
593
|
+
* @param {number} page - The page number to find the associated page break for.
|
|
594
|
+
* @returns {object|undefined} The page break description object for the specified page if found; otherwise, `undefined`.
|
|
595
|
+
*/
|
|
596
|
+
LayoutBuilder.prototype._getPageBreak = function (pageBreaks, page) {
|
|
597
|
+
return pageBreaks.find(desc => desc.prevPage === page);
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
LayoutBuilder.prototype._getPageBreakListBySpan = function (tableNode, page, rowIndex) {
|
|
601
|
+
if (!tableNode || !tableNode._breaksBySpan) {
|
|
602
|
+
return null;
|
|
603
|
+
}
|
|
604
|
+
const breaksList = tableNode._breaksBySpan.filter(desc => desc.prevPage === page && rowIndex <= desc.rowIndexOfSpanEnd);
|
|
605
|
+
|
|
606
|
+
var y = Number.MAX_VALUE,
|
|
607
|
+
prevY = Number.MIN_VALUE;
|
|
608
|
+
|
|
609
|
+
breaksList.forEach(b => {
|
|
610
|
+
prevY = Math.max(b.prevY, prevY);
|
|
611
|
+
y = Math.min(b.y, y);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
return {
|
|
615
|
+
prevPage: page,
|
|
616
|
+
prevY: prevY,
|
|
617
|
+
y: y
|
|
618
|
+
};
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
LayoutBuilder.prototype._findSameRowPageBreakByRowSpanData = function (breaksBySpan, page, rowIndex) {
|
|
622
|
+
if (!breaksBySpan) {
|
|
623
|
+
return null;
|
|
624
|
+
}
|
|
625
|
+
return breaksBySpan.find(desc => desc.prevPage === page && rowIndex === desc.rowIndexOfSpanEnd);
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
LayoutBuilder.prototype._updatePageBreaksData = function (pageBreaks, tableNode, rowIndex) {
|
|
629
|
+
Object.keys(tableNode._bottomByPage).forEach(p => {
|
|
630
|
+
const page = Number(p);
|
|
631
|
+
const pageBreak = this._getPageBreak(pageBreaks, page);
|
|
632
|
+
if (pageBreak) {
|
|
633
|
+
pageBreak.prevY = Math.max(pageBreak.prevY, tableNode._bottomByPage[page]);
|
|
634
|
+
}
|
|
635
|
+
if (tableNode._breaksBySpan && tableNode._breaksBySpan.length > 0) {
|
|
636
|
+
const breaksBySpanList = tableNode._breaksBySpan.filter(pb => pb.prevPage === page && rowIndex <= pb.rowIndexOfSpanEnd);
|
|
637
|
+
if (breaksBySpanList && breaksBySpanList.length > 0) {
|
|
638
|
+
breaksBySpanList.forEach(b => {
|
|
639
|
+
b.prevY = Math.max(b.prevY, tableNode._bottomByPage[page]);
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Resolves the Y-coordinates for a target object by comparing two break points.
|
|
648
|
+
*
|
|
649
|
+
* @param {object} break1 - The first break point with `prevY` and `y` properties.
|
|
650
|
+
* @param {object} break2 - The second break point with `prevY` and `y` properties.
|
|
651
|
+
* @param {object} target - The target object to be updated with resolved Y-coordinates.
|
|
652
|
+
* @property {number} target.prevY - Updated to the maximum `prevY` value between `break1` and `break2`.
|
|
653
|
+
* @property {number} target.y - Updated to the minimum `y` value between `break1` and `break2`.
|
|
654
|
+
*/
|
|
655
|
+
LayoutBuilder.prototype._resolveBreakY = function (break1, break2, target) {
|
|
656
|
+
target.prevY = Math.max(break1.prevY, break2.prevY);
|
|
657
|
+
target.y = Math.min(break1.y, break2.y);
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
LayoutBuilder.prototype._storePageBreakData = function (data, startsRowSpan, pageBreaks, tableNode) {
|
|
661
|
+
var pageDesc;
|
|
662
|
+
var pageDescBySpan;
|
|
663
|
+
|
|
664
|
+
if (!startsRowSpan) {
|
|
665
|
+
pageDesc = this._getPageBreak(pageBreaks, data.prevPage);
|
|
666
|
+
pageDescBySpan = this._getPageBreakListBySpan(tableNode, data.prevPage, data.rowIndex);
|
|
667
|
+
if (!pageDesc) {
|
|
668
|
+
pageDesc = Object.assign({}, data);
|
|
669
|
+
pageBreaks.push(pageDesc);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
if (pageDescBySpan) {
|
|
673
|
+
this._resolveBreakY(pageDesc, pageDescBySpan, pageDesc);
|
|
674
|
+
}
|
|
675
|
+
this._resolveBreakY(pageDesc, data, pageDesc);
|
|
676
|
+
} else {
|
|
677
|
+
var breaksBySpan = tableNode && tableNode._breaksBySpan || null;
|
|
678
|
+
pageDescBySpan = this._findSameRowPageBreakByRowSpanData(breaksBySpan, data.prevPage, data.rowIndex);
|
|
679
|
+
if (!pageDescBySpan) {
|
|
680
|
+
pageDescBySpan = Object.assign({}, data, {
|
|
681
|
+
rowIndexOfSpanEnd: data.rowIndex + data.rowSpan - 1
|
|
682
|
+
});
|
|
683
|
+
if (!tableNode._breaksBySpan) {
|
|
684
|
+
tableNode._breaksBySpan = [];
|
|
685
|
+
}
|
|
686
|
+
tableNode._breaksBySpan.push(pageDescBySpan);
|
|
687
|
+
}
|
|
688
|
+
pageDescBySpan.prevY = Math.max(pageDescBySpan.prevY, data.prevY);
|
|
689
|
+
pageDescBySpan.y = Math.min(pageDescBySpan.y, data.y);
|
|
690
|
+
pageDesc = this._getPageBreak(pageBreaks, data.prevPage);
|
|
691
|
+
if (pageDesc) {
|
|
692
|
+
this._resolveBreakY(pageDesc, pageDescBySpan, pageDesc);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Calculates the left offset for a column based on the specified gap values.
|
|
699
|
+
*
|
|
700
|
+
* @param {number} i - The index of the column for which the offset is being calculated.
|
|
701
|
+
* @param {Array<number>} gaps - An array of gap values for each column.
|
|
702
|
+
* @returns {number} The left offset for the column. Returns `gaps[i]` if it exists, otherwise `0`.
|
|
703
|
+
*/
|
|
704
|
+
LayoutBuilder.prototype._colLeftOffset = function (i, gaps) {
|
|
705
|
+
if (gaps && gaps.length > i) {
|
|
706
|
+
return gaps[i];
|
|
707
|
+
}
|
|
708
|
+
return 0;
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* Retrieves the ending cell for a row span in case it exists in a specified table column.
|
|
713
|
+
*
|
|
714
|
+
* @param {Array<Array<object>>} tableBody - The table body, represented as a 2D array of cell objects.
|
|
715
|
+
* @param {number} rowIndex - The index of the starting row for the row span.
|
|
716
|
+
* @param {object} column - The column object containing row span information.
|
|
717
|
+
* @param {number} columnIndex - The index of the column within the row.
|
|
718
|
+
* @returns {object|null} The cell at the end of the row span if it exists; otherwise, `null`.
|
|
719
|
+
* @throws {Error} If the row span extends beyond the total row count.
|
|
720
|
+
*/
|
|
721
|
+
LayoutBuilder.prototype._getRowSpanEndingCell = function (tableBody, rowIndex, column, columnIndex) {
|
|
722
|
+
if (column.rowSpan && column.rowSpan > 1) {
|
|
723
|
+
var endingRow = rowIndex + column.rowSpan - 1;
|
|
724
|
+
if (endingRow >= tableBody.length) {
|
|
725
|
+
throw new Error(`Row span for column ${columnIndex} (with indexes starting from 0) exceeded row count`);
|
|
726
|
+
}
|
|
727
|
+
return tableBody[endingRow][columnIndex];
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
return null;
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
LayoutBuilder.prototype.processRow = function ({ marginX = [0, 0], dontBreakRows = false, rowsWithoutPageBreak = 0, cells, widths, gaps, tableNode, tableBody, rowIndex, height }) {
|
|
734
|
+
var self = this;
|
|
735
|
+
var isUnbreakableRow = dontBreakRows || rowIndex <= rowsWithoutPageBreak - 1;
|
|
736
|
+
var pageBreaks = [];
|
|
737
|
+
var pageBreaksByRowSpan = [];
|
|
738
|
+
var positions = [];
|
|
739
|
+
var willBreakByHeight = false;
|
|
740
|
+
widths = widths || cells;
|
|
741
|
+
|
|
742
|
+
// Check if row should break by height
|
|
743
|
+
if (!isUnbreakableRow && height > self.writer.context().availableHeight) {
|
|
744
|
+
willBreakByHeight = true;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
// Use the marginX if we are in a top level table/column (not nested)
|
|
748
|
+
const marginXParent = self.nestedLevel === 1 ? marginX : null;
|
|
749
|
+
const _bottomByPage = tableNode ? tableNode._bottomByPage : null;
|
|
750
|
+
this.writer.context().beginColumnGroup(marginXParent, _bottomByPage);
|
|
751
|
+
|
|
752
|
+
for (var i = 0, l = cells.length; i < l; i++) {
|
|
753
|
+
var cell = cells[i];
|
|
754
|
+
|
|
755
|
+
// Page change handler
|
|
756
|
+
|
|
757
|
+
this.tracker.auto('pageChanged', storePageBreakClosure, function () {
|
|
758
|
+
var width = widths[i]._calcWidth;
|
|
759
|
+
var leftOffset = self._colLeftOffset(i, gaps);
|
|
760
|
+
// Check if exists and retrieve the cell that started the rowspan in case we are in the cell just after
|
|
761
|
+
var startingSpanCell = self._findStartingRowSpanCell(cells, i);
|
|
762
|
+
|
|
763
|
+
if (cell.colSpan && cell.colSpan > 1) {
|
|
764
|
+
for (var j = 1; j < cell.colSpan; j++) {
|
|
765
|
+
width += widths[++i]._calcWidth + gaps[i];
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// if rowspan starts in this cell, we retrieve the last cell affected by the rowspan
|
|
770
|
+
const rowSpanEndingCell = self._getRowSpanEndingCell(tableBody, rowIndex, cell, i);
|
|
771
|
+
if (rowSpanEndingCell) {
|
|
772
|
+
// We store a reference of the ending cell in the first cell of the rowspan
|
|
773
|
+
cell._endingCell = rowSpanEndingCell;
|
|
774
|
+
cell._endingCell._startingRowSpanY = cell._startingRowSpanY;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// If we are after a cell that started a rowspan
|
|
778
|
+
var endOfRowSpanCell = null;
|
|
779
|
+
if (startingSpanCell && startingSpanCell._endingCell) {
|
|
780
|
+
// Reference to the last cell of the rowspan
|
|
781
|
+
endOfRowSpanCell = startingSpanCell._endingCell;
|
|
782
|
+
// Store if we are in an unbreakable block when we save the context and the originalX
|
|
783
|
+
if (self.writer.transactionLevel > 0) {
|
|
784
|
+
endOfRowSpanCell._isUnbreakableContext = true;
|
|
785
|
+
endOfRowSpanCell._originalXOffset = self.writer.originalX;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// We pass the endingSpanCell reference to store the context just after processing rowspan cell
|
|
790
|
+
self.writer.context().beginColumn(width, leftOffset, endOfRowSpanCell);
|
|
791
|
+
|
|
792
|
+
if (!cell._span) {
|
|
793
|
+
self.processNode(cell);
|
|
794
|
+
self.writer.context().updateBottomByPage();
|
|
795
|
+
addAll(positions, cell.positions);
|
|
796
|
+
} else if (cell._columnEndingContext) {
|
|
797
|
+
var discountY = 0;
|
|
798
|
+
if (dontBreakRows) {
|
|
799
|
+
// Calculate how many points we have to discount to Y when dontBreakRows and rowSpan are combined
|
|
800
|
+
const ctxBeforeRowSpanLastRow = self.writer.writer.contextStack[self.writer.writer.contextStack.length - 1];
|
|
801
|
+
discountY = ctxBeforeRowSpanLastRow.y - cell._startingRowSpanY;
|
|
802
|
+
}
|
|
803
|
+
var originalXOffset = 0;
|
|
804
|
+
// If context was saved from an unbreakable block and we are not in an unbreakable block anymore
|
|
805
|
+
// We have to sum the originalX (X before starting unbreakable block) to X
|
|
806
|
+
if (cell._isUnbreakableContext && !self.writer.transactionLevel) {
|
|
807
|
+
originalXOffset = cell._originalXOffset;
|
|
808
|
+
}
|
|
809
|
+
// row-span ending
|
|
810
|
+
// Recover the context after processing the rowspanned cell
|
|
811
|
+
self.writer.context().markEnding(cell, originalXOffset, discountY);
|
|
812
|
+
}
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// Check if last cell is part of a span
|
|
817
|
+
var endingSpanCell = null;
|
|
818
|
+
var lastColumn = cells.length > 0 ? cells[cells.length - 1] : null;
|
|
819
|
+
if (lastColumn) {
|
|
820
|
+
// Previous column cell has a rowspan
|
|
821
|
+
if (lastColumn._endingCell) {
|
|
822
|
+
endingSpanCell = lastColumn._endingCell;
|
|
823
|
+
// Previous column cell is part of a span
|
|
824
|
+
} else if (lastColumn._span === true) {
|
|
825
|
+
// We get the cell that started the span where we set a reference to the ending cell
|
|
826
|
+
const startingSpanCell = this._findStartingRowSpanCell(cells, cells.length);
|
|
827
|
+
if (startingSpanCell) {
|
|
828
|
+
// Context will be stored here (ending cell)
|
|
829
|
+
endingSpanCell = startingSpanCell._endingCell;
|
|
830
|
+
// Store if we are in an unbreakable block when we save the context and the originalX
|
|
831
|
+
if (this.writer.transactionLevel > 0) {
|
|
832
|
+
endingSpanCell._isUnbreakableContext = true;
|
|
833
|
+
endingSpanCell._originalXOffset = this.writer.originalX;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
// If content did not break page, check if we should break by height
|
|
840
|
+
if (willBreakByHeight && !isUnbreakableRow && pageBreaks.length === 0) {
|
|
841
|
+
this.writer.context().moveDown(this.writer.context().availableHeight);
|
|
842
|
+
this.writer.moveToNextPage();
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
var bottomByPage = this.writer.context().completeColumnGroup(height, endingSpanCell);
|
|
846
|
+
|
|
847
|
+
if (tableNode) {
|
|
848
|
+
tableNode._bottomByPage = bottomByPage;
|
|
849
|
+
// If there are page breaks in this row, update data with prevY of last cell
|
|
850
|
+
this._updatePageBreaksData(pageBreaks, tableNode, rowIndex);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
return {
|
|
854
|
+
pageBreaksBySpan: pageBreaksByRowSpan,
|
|
855
|
+
pageBreaks: pageBreaks,
|
|
856
|
+
positions: positions
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
function storePageBreakClosure(data) {
|
|
860
|
+
const startsRowSpan = cell.rowSpan && cell.rowSpan > 1;
|
|
861
|
+
if (startsRowSpan) {
|
|
862
|
+
data.rowSpan = cell.rowSpan;
|
|
863
|
+
}
|
|
864
|
+
data.rowIndex = rowIndex;
|
|
865
|
+
self._storePageBreakData(data, startsRowSpan, pageBreaks, tableNode);
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
// lists
|
|
871
|
+
LayoutBuilder.prototype.processList = function (orderedList, node) {
|
|
872
|
+
var self = this,
|
|
873
|
+
items = orderedList ? node.ol : node.ul,
|
|
874
|
+
gapSize = node._gapSize;
|
|
875
|
+
|
|
876
|
+
this.writer.context().addMargin(gapSize.width);
|
|
877
|
+
|
|
878
|
+
var nextMarker;
|
|
879
|
+
this.tracker.auto('lineAdded', addMarkerToFirstLeaf, function () {
|
|
880
|
+
items.forEach(function (item) {
|
|
881
|
+
nextMarker = item.listMarker;
|
|
882
|
+
self.processNode(item);
|
|
883
|
+
addAll(node.positions, item.positions);
|
|
884
|
+
});
|
|
885
|
+
});
|
|
886
|
+
|
|
887
|
+
this.writer.context().addMargin(-gapSize.width);
|
|
888
|
+
|
|
889
|
+
function addMarkerToFirstLeaf(line) {
|
|
890
|
+
// I'm not very happy with the way list processing is implemented
|
|
891
|
+
// (both code and algorithm should be rethinked)
|
|
892
|
+
if (nextMarker) {
|
|
893
|
+
var marker = nextMarker;
|
|
894
|
+
nextMarker = null;
|
|
895
|
+
|
|
896
|
+
if (marker.canvas) {
|
|
897
|
+
var vector = marker.canvas[0];
|
|
898
|
+
|
|
899
|
+
offsetVector(vector, -marker._minWidth, 0);
|
|
900
|
+
self.writer.addVector(vector);
|
|
901
|
+
} else if (marker._inlines) {
|
|
902
|
+
var markerLine = new Line(self.pageSize.width);
|
|
903
|
+
markerLine.addInline(marker._inlines[0]);
|
|
904
|
+
markerLine.x = -marker._minWidth;
|
|
905
|
+
markerLine.y = line.getAscenderHeight() - markerLine.getAscenderHeight();
|
|
906
|
+
self.writer.addLine(markerLine, true);
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
|
|
912
|
+
// tables
|
|
913
|
+
LayoutBuilder.prototype.processTable = function (tableNode) {
|
|
914
|
+
this.nestedLevel++;
|
|
915
|
+
var processor = new TableProcessor(tableNode);
|
|
916
|
+
|
|
917
|
+
processor.beginTable(this.writer);
|
|
918
|
+
|
|
919
|
+
var rowHeights = tableNode.table.heights;
|
|
920
|
+
for (var i = 0, l = tableNode.table.body.length; i < l; i++) {
|
|
921
|
+
// if dontBreakRows and row starts a rowspan
|
|
922
|
+
// we store the 'y' of the beginning of each rowSpan
|
|
923
|
+
if (processor.dontBreakRows) {
|
|
924
|
+
tableNode.table.body[i].forEach(cell => {
|
|
925
|
+
if (cell.rowSpan && cell.rowSpan > 1) {
|
|
926
|
+
cell._startingRowSpanY = this.writer.context().y;
|
|
927
|
+
}
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
processor.beginRow(i, this.writer);
|
|
932
|
+
|
|
933
|
+
var height;
|
|
934
|
+
if (isFunction(rowHeights)) {
|
|
935
|
+
height = rowHeights(i);
|
|
936
|
+
} else if (isArray(rowHeights)) {
|
|
937
|
+
height = rowHeights[i];
|
|
938
|
+
} else {
|
|
939
|
+
height = rowHeights;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
if (height === 'auto') {
|
|
943
|
+
height = undefined;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
var pageBeforeProcessing = this.writer.context().page;
|
|
947
|
+
|
|
948
|
+
var result = this.processRow({
|
|
949
|
+
marginX: tableNode._margin ? [tableNode._margin[0], tableNode._margin[2]] : [0, 0],
|
|
950
|
+
dontBreakRows: processor.dontBreakRows,
|
|
951
|
+
rowsWithoutPageBreak: processor.rowsWithoutPageBreak,
|
|
952
|
+
cells: tableNode.table.body[i],
|
|
953
|
+
widths: tableNode.table.widths,
|
|
954
|
+
gaps: tableNode._offsets.offsets,
|
|
955
|
+
tableBody: tableNode.table.body,
|
|
956
|
+
tableNode,
|
|
957
|
+
rowIndex: i,
|
|
958
|
+
height
|
|
959
|
+
});
|
|
960
|
+
addAll(tableNode.positions, result.positions);
|
|
961
|
+
|
|
962
|
+
if (!result.pageBreaks || result.pageBreaks.length === 0) {
|
|
963
|
+
var breaksBySpan = tableNode && tableNode._breaksBySpan || null;
|
|
964
|
+
var breakBySpanData = this._findSameRowPageBreakByRowSpanData(breaksBySpan, pageBeforeProcessing, i);
|
|
965
|
+
if (breakBySpanData) {
|
|
966
|
+
var finalBreakBySpanData = this._getPageBreakListBySpan(tableNode, breakBySpanData.prevPage, i);
|
|
967
|
+
result.pageBreaks.push(finalBreakBySpanData);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
processor.endRow(i, this.writer, result.pageBreaks);
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
processor.endTable(this.writer);
|
|
975
|
+
this.nestedLevel--;
|
|
976
|
+
if (this.nestedLevel === 0) {
|
|
977
|
+
this.writer.context().resetMarginXTopParent();
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
|
|
981
|
+
// leafs (texts)
|
|
982
|
+
LayoutBuilder.prototype.processLeaf = function (node) {
|
|
983
|
+
var line = this.buildNextLine(node);
|
|
984
|
+
if (line && (node.tocItem || node.id)) {
|
|
985
|
+
line._node = node;
|
|
986
|
+
}
|
|
987
|
+
var currentHeight = (line) ? line.getHeight() : 0;
|
|
988
|
+
var maxHeight = node.maxHeight || -1;
|
|
989
|
+
|
|
990
|
+
if (line) {
|
|
991
|
+
var nodeId = getNodeId(node);
|
|
992
|
+
if (nodeId) {
|
|
993
|
+
line.id = nodeId;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
if (node._tocItemRef) {
|
|
998
|
+
line._pageNodeRef = node._tocItemRef;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
if (node._pageRef) {
|
|
1002
|
+
line._pageNodeRef = node._pageRef._nodeRef;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
if (line && line.inlines && isArray(line.inlines)) {
|
|
1006
|
+
for (var i = 0, l = line.inlines.length; i < l; i++) {
|
|
1007
|
+
if (line.inlines[i]._tocItemRef) {
|
|
1008
|
+
line.inlines[i]._pageNodeRef = line.inlines[i]._tocItemRef;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
if (line.inlines[i]._pageRef) {
|
|
1012
|
+
line.inlines[i]._pageNodeRef = line.inlines[i]._pageRef._nodeRef;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
while (line && (maxHeight === -1 || currentHeight < maxHeight)) {
|
|
1018
|
+
var positions = this.writer.addLine(line);
|
|
1019
|
+
node.positions.push(positions);
|
|
1020
|
+
line = this.buildNextLine(node);
|
|
1021
|
+
if (line) {
|
|
1022
|
+
currentHeight += line.getHeight();
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
};
|
|
1026
|
+
|
|
1027
|
+
LayoutBuilder.prototype.processToc = function (node) {
|
|
1028
|
+
if (node.toc.title) {
|
|
1029
|
+
this.processNode(node.toc.title);
|
|
1030
|
+
}
|
|
1031
|
+
if (node.toc._table) {
|
|
1032
|
+
this.processNode(node.toc._table);
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
|
|
1036
|
+
LayoutBuilder.prototype.buildNextLine = function (textNode) {
|
|
1037
|
+
|
|
1038
|
+
function cloneInline(inline) {
|
|
1039
|
+
var newInline = inline.constructor();
|
|
1040
|
+
for (var key in inline) {
|
|
1041
|
+
newInline[key] = inline[key];
|
|
1042
|
+
}
|
|
1043
|
+
return newInline;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
if (!textNode._inlines || textNode._inlines.length === 0) {
|
|
1047
|
+
return null;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
var line = new Line(this.writer.context().availableWidth);
|
|
1051
|
+
var textTools = new TextTools(null);
|
|
1052
|
+
|
|
1053
|
+
var isForceContinue = false;
|
|
1054
|
+
while (textNode._inlines && textNode._inlines.length > 0 &&
|
|
1055
|
+
(line.hasEnoughSpaceForInline(textNode._inlines[0], textNode._inlines.slice(1)) || isForceContinue)) {
|
|
1056
|
+
var isHardWrap = false;
|
|
1057
|
+
var inline = textNode._inlines.shift();
|
|
1058
|
+
isForceContinue = false;
|
|
1059
|
+
|
|
1060
|
+
if (!inline.noWrap && inline.text.length > 1 && inline.width > line.getAvailableWidth()) {
|
|
1061
|
+
var widthPerChar = inline.width / inline.text.length;
|
|
1062
|
+
var maxChars = Math.floor(line.getAvailableWidth() / widthPerChar);
|
|
1063
|
+
if (maxChars < 1) {
|
|
1064
|
+
maxChars = 1;
|
|
1065
|
+
}
|
|
1066
|
+
if (maxChars < inline.text.length) {
|
|
1067
|
+
var newInline = cloneInline(inline);
|
|
1068
|
+
|
|
1069
|
+
newInline.text = inline.text.substr(maxChars);
|
|
1070
|
+
inline.text = inline.text.substr(0, maxChars);
|
|
1071
|
+
|
|
1072
|
+
newInline.width = textTools.widthOfString(newInline.text, newInline.font, newInline.fontSize, newInline.characterSpacing, newInline.fontFeatures);
|
|
1073
|
+
inline.width = textTools.widthOfString(inline.text, inline.font, inline.fontSize, inline.characterSpacing, inline.fontFeatures);
|
|
1074
|
+
|
|
1075
|
+
textNode._inlines.unshift(newInline);
|
|
1076
|
+
isHardWrap = true;
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
line.addInline(inline);
|
|
1081
|
+
|
|
1082
|
+
isForceContinue = inline.noNewLine && !isHardWrap;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
line.lastLineInParagraph = textNode._inlines.length === 0;
|
|
1086
|
+
|
|
1087
|
+
return line;
|
|
1088
|
+
};
|
|
1089
|
+
|
|
1090
|
+
// images
|
|
1091
|
+
LayoutBuilder.prototype.processImage = function (node) {
|
|
1092
|
+
var position = this.writer.addImage(node);
|
|
1093
|
+
node.positions.push(position);
|
|
1094
|
+
};
|
|
1095
|
+
|
|
1096
|
+
LayoutBuilder.prototype.processSVG = function (node) {
|
|
1097
|
+
var position = this.writer.addSVG(node);
|
|
1098
|
+
node.positions.push(position);
|
|
1099
|
+
};
|
|
1100
|
+
|
|
1101
|
+
LayoutBuilder.prototype.processCanvas = function (node) {
|
|
1102
|
+
var height = node._minHeight;
|
|
1103
|
+
|
|
1104
|
+
if (node.absolutePosition === undefined && this.writer.context().availableHeight < height) {
|
|
1105
|
+
// TODO: support for canvas larger than a page
|
|
1106
|
+
// TODO: support for other overflow methods
|
|
1107
|
+
|
|
1108
|
+
this.writer.moveToNextPage();
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
this.writer.alignCanvas(node);
|
|
1112
|
+
|
|
1113
|
+
node.canvas.forEach(function (vector) {
|
|
1114
|
+
var position = this.writer.addVector(vector);
|
|
1115
|
+
node.positions.push(position);
|
|
1116
|
+
}, this);
|
|
1117
|
+
|
|
1118
|
+
this.writer.context().moveDown(height);
|
|
1119
|
+
};
|
|
1120
|
+
|
|
1121
|
+
LayoutBuilder.prototype.processQr = function (node) {
|
|
1122
|
+
var position = this.writer.addQr(node);
|
|
1123
|
+
node.positions.push(position);
|
|
1124
|
+
};
|
|
1125
|
+
|
|
1126
|
+
module.exports = LayoutBuilder;
|