pdfmake 0.3.0-beta.3 → 0.3.0-beta.4
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 +5 -0
- package/build/pdfmake.js +2122 -8531
- 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 +6 -5
- package/js/3rd-party/svg-to-pdfkit/source.js +247 -920
- package/js/3rd-party/svg-to-pdfkit.js +0 -3
- package/js/DocMeasure.js +6 -139
- package/js/DocPreprocessor.js +2 -54
- package/js/DocumentContext.js +0 -40
- package/js/ElementWriter.js +2 -70
- package/js/LayoutBuilder.js +20 -165
- package/js/Line.js +6 -25
- package/js/OutputDocument.js +4 -13
- package/js/OutputDocumentServer.js +0 -6
- package/js/PDFDocument.js +1 -46
- package/js/PageElementWriter.js +7 -31
- package/js/PageSize.js +2 -16
- package/js/Printer.js +5 -46
- package/js/Renderer.js +11 -98
- package/js/SVGMeasure.js +1 -20
- package/js/StyleContextStack.js +12 -36
- package/js/TableProcessor.js +36 -117
- package/js/TextBreaker.js +2 -44
- package/js/TextDecorator.js +1 -38
- package/js/TextInlines.js +8 -49
- package/js/URLResolver.js +0 -13
- package/js/base.js +1 -17
- package/js/browser-extensions/OutputDocumentBrowser.js +5 -17
- package/js/browser-extensions/URLBrowserResolver.js +0 -17
- package/js/browser-extensions/fonts/Roboto.js +0 -4
- package/js/browser-extensions/index.js +0 -16
- package/js/browser-extensions/pdfMake.js +2 -4
- package/js/browser-extensions/standard-fonts/Courier.js +0 -4
- package/js/browser-extensions/standard-fonts/Helvetica.js +0 -4
- package/js/browser-extensions/standard-fonts/Symbol.js +0 -4
- package/js/browser-extensions/standard-fonts/Times.js +0 -4
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +0 -4
- package/js/columnCalculator.js +6 -18
- package/js/helpers/node.js +3 -27
- package/js/helpers/tools.js +0 -8
- package/js/helpers/variableType.js +4 -9
- package/js/index.js +0 -6
- package/js/qrEnc.js +126 -215
- package/js/tableLayouts.js +1 -28
- package/js/virtual-fs.js +3 -19
- package/package.json +2 -2
- package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -9
package/js/LayoutBuilder.js
CHANGED
|
@@ -2,44 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.default = void 0;
|
|
5
|
-
|
|
6
5
|
var _DocPreprocessor = _interopRequireDefault(require("./DocPreprocessor"));
|
|
7
|
-
|
|
8
6
|
var _DocMeasure = _interopRequireDefault(require("./DocMeasure"));
|
|
9
|
-
|
|
10
7
|
var _DocumentContext = _interopRequireDefault(require("./DocumentContext"));
|
|
11
|
-
|
|
12
8
|
var _PageElementWriter = _interopRequireDefault(require("./PageElementWriter"));
|
|
13
|
-
|
|
14
9
|
var _columnCalculator = _interopRequireDefault(require("./columnCalculator"));
|
|
15
|
-
|
|
16
10
|
var _TableProcessor = _interopRequireDefault(require("./TableProcessor"));
|
|
17
|
-
|
|
18
11
|
var _Line = _interopRequireDefault(require("./Line"));
|
|
19
|
-
|
|
20
12
|
var _variableType = require("./helpers/variableType");
|
|
21
|
-
|
|
22
13
|
var _node = require("./helpers/node");
|
|
23
|
-
|
|
24
14
|
var _tools = require("./helpers/tools");
|
|
25
|
-
|
|
26
15
|
var _TextInlines = _interopRequireDefault(require("./TextInlines"));
|
|
27
|
-
|
|
28
16
|
var _StyleContextStack = _interopRequireDefault(require("./StyleContextStack"));
|
|
29
|
-
|
|
30
17
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
31
|
-
|
|
32
18
|
function addAll(target, otherArray) {
|
|
33
19
|
otherArray.forEach(item => {
|
|
34
20
|
target.push(item);
|
|
35
21
|
});
|
|
36
22
|
}
|
|
23
|
+
|
|
37
24
|
/**
|
|
38
25
|
* Layout engine which turns document-definition-object into a set of pages, lines, inlines
|
|
39
26
|
* and vectors ready to be rendered into a PDF
|
|
40
27
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
28
|
class LayoutBuilder {
|
|
44
29
|
/**
|
|
45
30
|
* @param {object} pageSize - an object defining page width and height
|
|
@@ -52,10 +37,10 @@ class LayoutBuilder {
|
|
|
52
37
|
this.svgMeasure = svgMeasure;
|
|
53
38
|
this.tableLayouts = {};
|
|
54
39
|
}
|
|
55
|
-
|
|
56
40
|
registerTableLayouts(tableLayouts) {
|
|
57
41
|
this.tableLayouts = (0, _tools.pack)(this.tableLayouts, tableLayouts);
|
|
58
42
|
}
|
|
43
|
+
|
|
59
44
|
/**
|
|
60
45
|
* Executes layout engine on document-definition-object and creates an array of pages
|
|
61
46
|
* containing positioned Blocks, Lines and inlines
|
|
@@ -71,14 +56,11 @@ class LayoutBuilder {
|
|
|
71
56
|
* @param {object} pageBreakBeforeFct
|
|
72
57
|
* @returns {Array} an array of pages
|
|
73
58
|
*/
|
|
74
|
-
|
|
75
|
-
|
|
76
59
|
layoutDocument(docStructure, pdfDocument, styleDictionary, defaultStyle, background, header, footer, watermark, pageBreakBeforeFct) {
|
|
77
60
|
function addPageBreaksIfNecessary(linearNodeList, pages) {
|
|
78
61
|
if (typeof pageBreakBeforeFct !== 'function') {
|
|
79
62
|
return false;
|
|
80
63
|
}
|
|
81
|
-
|
|
82
64
|
linearNodeList = linearNodeList.filter(node => node.positions.length > 0);
|
|
83
65
|
linearNodeList.forEach(node => {
|
|
84
66
|
let nodeInfo = {};
|
|
@@ -93,46 +75,37 @@ class LayoutBuilder {
|
|
|
93
75
|
nodeInfo.stack = Array.isArray(node.stack);
|
|
94
76
|
node.nodeInfo = nodeInfo;
|
|
95
77
|
});
|
|
96
|
-
|
|
97
78
|
for (let index = 0; index < linearNodeList.length; index++) {
|
|
98
79
|
let node = linearNodeList[index];
|
|
99
|
-
|
|
100
80
|
if (node.pageBreak !== 'before' && !node.pageBreakCalculated) {
|
|
101
81
|
node.pageBreakCalculated = true;
|
|
102
82
|
let pageNumber = node.nodeInfo.pageNumbers[0];
|
|
103
|
-
|
|
104
83
|
if (pageBreakBeforeFct(node.nodeInfo, {
|
|
105
84
|
getFollowingNodesOnPage: () => {
|
|
106
85
|
let followingNodesOnPage = [];
|
|
107
|
-
|
|
108
86
|
for (let ii = index + 1, l = linearNodeList.length; ii < l; ii++) {
|
|
109
87
|
if (linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber) > -1) {
|
|
110
88
|
followingNodesOnPage.push(linearNodeList[ii].nodeInfo);
|
|
111
89
|
}
|
|
112
90
|
}
|
|
113
|
-
|
|
114
91
|
return followingNodesOnPage;
|
|
115
92
|
},
|
|
116
93
|
getNodesOnNextPage: () => {
|
|
117
94
|
let nodesOnNextPage = [];
|
|
118
|
-
|
|
119
95
|
for (let ii = index + 1, l = linearNodeList.length; ii < l; ii++) {
|
|
120
96
|
if (linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber + 1) > -1) {
|
|
121
97
|
nodesOnNextPage.push(linearNodeList[ii].nodeInfo);
|
|
122
98
|
}
|
|
123
99
|
}
|
|
124
|
-
|
|
125
100
|
return nodesOnNextPage;
|
|
126
101
|
},
|
|
127
102
|
getPreviousNodesOnPage: () => {
|
|
128
103
|
let previousNodesOnPage = [];
|
|
129
|
-
|
|
130
104
|
for (let ii = 0; ii < index; ii++) {
|
|
131
105
|
if (linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber) > -1) {
|
|
132
106
|
previousNodesOnPage.push(linearNodeList[ii].nodeInfo);
|
|
133
107
|
}
|
|
134
108
|
}
|
|
135
|
-
|
|
136
109
|
return previousNodesOnPage;
|
|
137
110
|
}
|
|
138
111
|
})) {
|
|
@@ -141,29 +114,22 @@ class LayoutBuilder {
|
|
|
141
114
|
}
|
|
142
115
|
}
|
|
143
116
|
}
|
|
144
|
-
|
|
145
117
|
return false;
|
|
146
118
|
}
|
|
147
|
-
|
|
148
119
|
this.docPreprocessor = new _DocPreprocessor.default();
|
|
149
120
|
this.docMeasure = new _DocMeasure.default(pdfDocument, styleDictionary, defaultStyle, this.svgMeasure, this.tableLayouts);
|
|
150
|
-
|
|
151
121
|
function resetXYs(result) {
|
|
152
122
|
result.linearNodeList.forEach(node => {
|
|
153
123
|
node.resetXY();
|
|
154
124
|
});
|
|
155
125
|
}
|
|
156
|
-
|
|
157
126
|
let result = this.tryLayoutDocument(docStructure, pdfDocument, styleDictionary, defaultStyle, background, header, footer, watermark);
|
|
158
|
-
|
|
159
127
|
while (addPageBreaksIfNecessary(result.linearNodeList, result.pages)) {
|
|
160
128
|
resetXYs(result);
|
|
161
129
|
result = this.tryLayoutDocument(docStructure, pdfDocument, styleDictionary, defaultStyle, background, header, footer, watermark);
|
|
162
130
|
}
|
|
163
|
-
|
|
164
131
|
return result.pages;
|
|
165
132
|
}
|
|
166
|
-
|
|
167
133
|
tryLayoutDocument(docStructure, pdfDocument, styleDictionary, defaultStyle, background, header, footer, watermark) {
|
|
168
134
|
this.linearNodeList = [];
|
|
169
135
|
docStructure = this.docPreprocessor.preprocessDocument(docStructure);
|
|
@@ -175,23 +141,19 @@ class LayoutBuilder {
|
|
|
175
141
|
this.addBackground(background);
|
|
176
142
|
this.processNode(docStructure);
|
|
177
143
|
this.addHeadersAndFooters(header, footer);
|
|
178
|
-
|
|
179
144
|
if (watermark != null) {
|
|
180
145
|
this.addWatermark(watermark, pdfDocument, defaultStyle);
|
|
181
146
|
}
|
|
182
|
-
|
|
183
147
|
return {
|
|
184
148
|
pages: this.writer.context().pages,
|
|
185
149
|
linearNodeList: this.linearNodeList
|
|
186
150
|
};
|
|
187
151
|
}
|
|
188
|
-
|
|
189
152
|
addBackground(background) {
|
|
190
153
|
let backgroundGetter = typeof background === 'function' ? background : () => background;
|
|
191
154
|
let context = this.writer.context();
|
|
192
155
|
let pageSize = context.getCurrentPage().pageSize;
|
|
193
156
|
let pageBackground = backgroundGetter(context.page + 1, pageSize);
|
|
194
|
-
|
|
195
157
|
if (pageBackground) {
|
|
196
158
|
this.writer.beginUnbreakableBlock(pageSize.width, pageSize.height);
|
|
197
159
|
pageBackground = this.docPreprocessor.preprocessDocument(pageBackground);
|
|
@@ -200,19 +162,16 @@ class LayoutBuilder {
|
|
|
200
162
|
context.backgroundLength[context.page] += pageBackground.positions.length;
|
|
201
163
|
}
|
|
202
164
|
}
|
|
203
|
-
|
|
204
165
|
addStaticRepeatable(headerOrFooter, sizeFunction) {
|
|
205
|
-
this.addDynamicRepeatable(() =>
|
|
166
|
+
this.addDynamicRepeatable(() =>
|
|
167
|
+
// copy to new object
|
|
206
168
|
JSON.parse(JSON.stringify(headerOrFooter)), sizeFunction);
|
|
207
169
|
}
|
|
208
|
-
|
|
209
170
|
addDynamicRepeatable(nodeGetter, sizeFunction) {
|
|
210
171
|
let pages = this.writer.context().pages;
|
|
211
|
-
|
|
212
172
|
for (let pageIndex = 0, l = pages.length; pageIndex < l; pageIndex++) {
|
|
213
173
|
this.writer.context().page = pageIndex;
|
|
214
174
|
let node = nodeGetter(pageIndex + 1, l, this.writer.context().pages[pageIndex].pageSize);
|
|
215
|
-
|
|
216
175
|
if (node) {
|
|
217
176
|
let sizes = sizeFunction(this.writer.context().getCurrentPage().pageSize, this.pageMargins);
|
|
218
177
|
this.writer.beginUnbreakableBlock(sizes.width, sizes.height);
|
|
@@ -222,7 +181,6 @@ class LayoutBuilder {
|
|
|
222
181
|
}
|
|
223
182
|
}
|
|
224
183
|
}
|
|
225
|
-
|
|
226
184
|
addHeadersAndFooters(header, footer) {
|
|
227
185
|
const headerSizeFct = (pageSize, pageMargins) => ({
|
|
228
186
|
x: 0,
|
|
@@ -230,39 +188,33 @@ class LayoutBuilder {
|
|
|
230
188
|
width: pageSize.width,
|
|
231
189
|
height: pageMargins.top
|
|
232
190
|
});
|
|
233
|
-
|
|
234
191
|
const footerSizeFct = (pageSize, pageMargins) => ({
|
|
235
192
|
x: 0,
|
|
236
193
|
y: pageSize.height - pageMargins.bottom,
|
|
237
194
|
width: pageSize.width,
|
|
238
195
|
height: pageMargins.bottom
|
|
239
196
|
});
|
|
240
|
-
|
|
241
197
|
if (typeof header === 'function') {
|
|
242
198
|
this.addDynamicRepeatable(header, headerSizeFct);
|
|
243
199
|
} else if (header) {
|
|
244
200
|
this.addStaticRepeatable(header, headerSizeFct);
|
|
245
201
|
}
|
|
246
|
-
|
|
247
202
|
if (typeof footer === 'function') {
|
|
248
203
|
this.addDynamicRepeatable(footer, footerSizeFct);
|
|
249
204
|
} else if (footer) {
|
|
250
205
|
this.addStaticRepeatable(footer, footerSizeFct);
|
|
251
206
|
}
|
|
252
207
|
}
|
|
253
|
-
|
|
254
208
|
addWatermark(watermark, pdfDocument, defaultStyle) {
|
|
255
209
|
if ((0, _variableType.isString)(watermark)) {
|
|
256
210
|
watermark = {
|
|
257
211
|
'text': watermark
|
|
258
212
|
};
|
|
259
213
|
}
|
|
260
|
-
|
|
261
214
|
if (!watermark.text) {
|
|
262
215
|
// empty watermark text
|
|
263
216
|
return;
|
|
264
217
|
}
|
|
265
|
-
|
|
266
218
|
watermark.font = watermark.font || defaultStyle.font || 'Roboto';
|
|
267
219
|
watermark.fontSize = watermark.fontSize || 'auto';
|
|
268
220
|
watermark.color = watermark.color || 'black';
|
|
@@ -270,15 +222,12 @@ class LayoutBuilder {
|
|
|
270
222
|
watermark.bold = watermark.bold || false;
|
|
271
223
|
watermark.italics = watermark.italics || false;
|
|
272
224
|
watermark.angle = (0, _variableType.isValue)(watermark.angle) ? watermark.angle : null;
|
|
273
|
-
|
|
274
225
|
if (watermark.angle === null) {
|
|
275
226
|
watermark.angle = Math.atan2(this.pageSize.height, this.pageSize.width) * -180 / Math.PI;
|
|
276
227
|
}
|
|
277
|
-
|
|
278
228
|
if (watermark.fontSize === 'auto') {
|
|
279
229
|
watermark.fontSize = getWatermarkFontSize(this.pageSize, watermark, pdfDocument);
|
|
280
230
|
}
|
|
281
|
-
|
|
282
231
|
let watermarkObject = {
|
|
283
232
|
text: watermark.text,
|
|
284
233
|
font: pdfDocument.provideFont(watermark.font, watermark.bold, watermark.italics),
|
|
@@ -289,11 +238,9 @@ class LayoutBuilder {
|
|
|
289
238
|
};
|
|
290
239
|
watermarkObject._size = getWatermarkSize(watermark, pdfDocument);
|
|
291
240
|
let pages = this.writer.context().pages;
|
|
292
|
-
|
|
293
241
|
for (let i = 0, l = pages.length; i < l; i++) {
|
|
294
242
|
pages[i].watermark = watermarkObject;
|
|
295
243
|
}
|
|
296
|
-
|
|
297
244
|
function getWatermarkSize(watermark, pdfDocument) {
|
|
298
245
|
let textInlines = new _TextInlines.default(pdfDocument);
|
|
299
246
|
let styleContextStack = new _StyleContextStack.default(null, {
|
|
@@ -311,7 +258,6 @@ class LayoutBuilder {
|
|
|
311
258
|
rotatedSize: rotatedSize
|
|
312
259
|
};
|
|
313
260
|
}
|
|
314
|
-
|
|
315
261
|
function getWatermarkFontSize(pageSize, watermark, pdfDocument) {
|
|
316
262
|
let textInlines = new _TextInlines.default(pdfDocument);
|
|
317
263
|
let styleContextStack = new _StyleContextStack.default(null, {
|
|
@@ -320,22 +266,20 @@ class LayoutBuilder {
|
|
|
320
266
|
italics: watermark.italics
|
|
321
267
|
});
|
|
322
268
|
let rotatedSize;
|
|
269
|
+
|
|
323
270
|
/**
|
|
324
271
|
* Binary search the best font size.
|
|
325
272
|
* Initial bounds [0, 1000]
|
|
326
273
|
* Break when range < 1
|
|
327
274
|
*/
|
|
328
|
-
|
|
329
275
|
let a = 0;
|
|
330
276
|
let b = 1000;
|
|
331
277
|
let c = (a + b) / 2;
|
|
332
|
-
|
|
333
278
|
while (Math.abs(a - b) > 1) {
|
|
334
279
|
styleContextStack.push({
|
|
335
280
|
fontSize: c
|
|
336
281
|
});
|
|
337
282
|
rotatedSize = textInlines.sizeOfRotatedText(watermark.text, watermark.angle, styleContextStack);
|
|
338
|
-
|
|
339
283
|
if (rotatedSize.width > pageSize.width) {
|
|
340
284
|
b = c;
|
|
341
285
|
c = (a + b) / 2;
|
|
@@ -348,90 +292,70 @@ class LayoutBuilder {
|
|
|
348
292
|
c = (a + b) / 2;
|
|
349
293
|
}
|
|
350
294
|
}
|
|
351
|
-
|
|
352
295
|
styleContextStack.pop();
|
|
353
296
|
}
|
|
354
297
|
/*
|
|
355
298
|
End binary search
|
|
356
299
|
*/
|
|
357
|
-
|
|
358
|
-
|
|
359
300
|
return c;
|
|
360
301
|
}
|
|
361
302
|
}
|
|
362
|
-
|
|
363
303
|
processNode(node) {
|
|
364
304
|
const applyMargins = callback => {
|
|
365
305
|
let margin = node._margin;
|
|
366
|
-
|
|
367
306
|
if (node.pageBreak === 'before') {
|
|
368
307
|
this.writer.moveToNextPage(node.pageOrientation);
|
|
369
308
|
} else if (node.pageBreak === 'beforeOdd') {
|
|
370
309
|
this.writer.moveToNextPage(node.pageOrientation);
|
|
371
|
-
|
|
372
310
|
if ((this.writer.context().page + 1) % 2 === 1) {
|
|
373
311
|
this.writer.moveToNextPage(node.pageOrientation);
|
|
374
312
|
}
|
|
375
313
|
} else if (node.pageBreak === 'beforeEven') {
|
|
376
314
|
this.writer.moveToNextPage(node.pageOrientation);
|
|
377
|
-
|
|
378
315
|
if ((this.writer.context().page + 1) % 2 === 0) {
|
|
379
316
|
this.writer.moveToNextPage(node.pageOrientation);
|
|
380
317
|
}
|
|
381
318
|
}
|
|
382
|
-
|
|
383
319
|
if (margin) {
|
|
384
320
|
this.writer.context().moveDown(margin[1]);
|
|
385
321
|
this.writer.context().addMargin(margin[0], margin[2]);
|
|
386
322
|
}
|
|
387
|
-
|
|
388
323
|
callback();
|
|
389
|
-
|
|
390
324
|
if (margin) {
|
|
391
325
|
this.writer.context().addMargin(-margin[0], -margin[2]);
|
|
392
326
|
this.writer.context().moveDown(margin[3]);
|
|
393
327
|
}
|
|
394
|
-
|
|
395
328
|
if (node.pageBreak === 'after') {
|
|
396
329
|
this.writer.moveToNextPage(node.pageOrientation);
|
|
397
330
|
} else if (node.pageBreak === 'afterOdd') {
|
|
398
331
|
this.writer.moveToNextPage(node.pageOrientation);
|
|
399
|
-
|
|
400
332
|
if ((this.writer.context().page + 1) % 2 === 1) {
|
|
401
333
|
this.writer.moveToNextPage(node.pageOrientation);
|
|
402
334
|
}
|
|
403
335
|
} else if (node.pageBreak === 'afterEven') {
|
|
404
336
|
this.writer.moveToNextPage(node.pageOrientation);
|
|
405
|
-
|
|
406
337
|
if ((this.writer.context().page + 1) % 2 === 0) {
|
|
407
338
|
this.writer.moveToNextPage(node.pageOrientation);
|
|
408
339
|
}
|
|
409
340
|
}
|
|
410
341
|
};
|
|
411
|
-
|
|
412
342
|
this.linearNodeList.push(node);
|
|
413
343
|
decorateNode(node);
|
|
414
344
|
applyMargins(() => {
|
|
415
345
|
let unbreakable = node.unbreakable;
|
|
416
|
-
|
|
417
346
|
if (unbreakable) {
|
|
418
347
|
this.writer.beginUnbreakableBlock();
|
|
419
348
|
}
|
|
420
|
-
|
|
421
349
|
let absPosition = node.absolutePosition;
|
|
422
|
-
|
|
423
350
|
if (absPosition) {
|
|
424
351
|
this.writer.context().beginDetachedBlock();
|
|
425
352
|
this.writer.context().moveTo(absPosition.x || 0, absPosition.y || 0);
|
|
426
353
|
}
|
|
427
|
-
|
|
428
354
|
let relPosition = node.relativePosition;
|
|
429
|
-
|
|
430
355
|
if (relPosition) {
|
|
431
356
|
this.writer.context().beginDetachedBlock();
|
|
432
357
|
this.writer.context().moveToRelative(relPosition.x || 0, relPosition.y || 0);
|
|
433
358
|
}
|
|
434
|
-
|
|
435
359
|
if (node.stack) {
|
|
436
360
|
this.processVerticalContainer(node);
|
|
437
361
|
} else if (node.columns) {
|
|
@@ -459,97 +383,80 @@ class LayoutBuilder {
|
|
|
459
383
|
} else if (!node._span) {
|
|
460
384
|
throw new Error(`Unrecognized document structure: ${(0, _node.stringifyNode)(node)}`);
|
|
461
385
|
}
|
|
462
|
-
|
|
463
386
|
if (absPosition || relPosition) {
|
|
464
387
|
this.writer.context().endDetachedBlock();
|
|
465
388
|
}
|
|
466
|
-
|
|
467
389
|
if (unbreakable) {
|
|
468
390
|
this.writer.commitUnbreakableBlock();
|
|
469
391
|
}
|
|
470
392
|
});
|
|
471
|
-
}
|
|
472
|
-
|
|
393
|
+
}
|
|
473
394
|
|
|
395
|
+
// vertical container
|
|
474
396
|
processVerticalContainer(node) {
|
|
475
397
|
node.stack.forEach(item => {
|
|
476
398
|
this.processNode(item);
|
|
477
|
-
addAll(node.positions, item.positions);
|
|
478
|
-
}, this);
|
|
479
|
-
} // columns
|
|
399
|
+
addAll(node.positions, item.positions);
|
|
480
400
|
|
|
401
|
+
//TODO: paragraph gap
|
|
402
|
+
}, this);
|
|
403
|
+
}
|
|
481
404
|
|
|
405
|
+
// columns
|
|
482
406
|
processColumns(columnNode) {
|
|
483
407
|
let columns = columnNode.columns;
|
|
484
408
|
let availableWidth = this.writer.context().availableWidth;
|
|
485
409
|
let gaps = gapArray(columnNode._gap);
|
|
486
|
-
|
|
487
410
|
if (gaps) {
|
|
488
411
|
availableWidth -= (gaps.length - 1) * columnNode._gap;
|
|
489
412
|
}
|
|
490
|
-
|
|
491
413
|
_columnCalculator.default.buildColumnWidths(columns, availableWidth);
|
|
492
|
-
|
|
493
414
|
let result = this.processRow(columns, columns, gaps);
|
|
494
415
|
addAll(columnNode.positions, result.positions);
|
|
495
|
-
|
|
496
416
|
function gapArray(gap) {
|
|
497
417
|
if (!gap) {
|
|
498
418
|
return null;
|
|
499
419
|
}
|
|
500
|
-
|
|
501
420
|
let gaps = [];
|
|
502
421
|
gaps.push(0);
|
|
503
|
-
|
|
504
422
|
for (let i = columns.length - 1; i > 0; i--) {
|
|
505
423
|
gaps.push(gap);
|
|
506
424
|
}
|
|
507
|
-
|
|
508
425
|
return gaps;
|
|
509
426
|
}
|
|
510
427
|
}
|
|
511
|
-
|
|
512
428
|
processRow(columns, widths, gaps, tableBody, tableRow, height) {
|
|
513
429
|
const storePageBreakData = data => {
|
|
514
430
|
let pageDesc;
|
|
515
|
-
|
|
516
431
|
for (let i = 0, l = pageBreaks.length; i < l; i++) {
|
|
517
432
|
let desc = pageBreaks[i];
|
|
518
|
-
|
|
519
433
|
if (desc.prevPage === data.prevPage) {
|
|
520
434
|
pageDesc = desc;
|
|
521
435
|
break;
|
|
522
436
|
}
|
|
523
437
|
}
|
|
524
|
-
|
|
525
438
|
if (!pageDesc) {
|
|
526
439
|
pageDesc = data;
|
|
527
440
|
pageBreaks.push(pageDesc);
|
|
528
441
|
}
|
|
529
|
-
|
|
530
442
|
pageDesc.prevY = Math.max(pageDesc.prevY, data.prevY);
|
|
531
443
|
pageDesc.y = Math.min(pageDesc.y, data.y);
|
|
532
444
|
};
|
|
533
|
-
|
|
534
445
|
let pageBreaks = [];
|
|
535
446
|
let positions = [];
|
|
536
447
|
this.writer.addListener('pageChanged', storePageBreakData);
|
|
537
448
|
widths = widths || columns;
|
|
538
449
|
this.writer.context().beginColumnGroup();
|
|
539
|
-
|
|
540
450
|
for (let i = 0, l = columns.length; i < l; i++) {
|
|
541
451
|
let column = columns[i];
|
|
542
452
|
let width = widths[i]._calcWidth;
|
|
543
453
|
let leftOffset = colLeftOffset(i);
|
|
544
|
-
|
|
545
454
|
if (column.colSpan && column.colSpan > 1) {
|
|
546
455
|
for (let j = 1; j < column.colSpan; j++) {
|
|
547
456
|
width += widths[++i]._calcWidth + gaps[i];
|
|
548
457
|
}
|
|
549
458
|
}
|
|
550
|
-
|
|
551
459
|
this.writer.context().beginColumn(width, leftOffset, getEndingCell(column, i));
|
|
552
|
-
|
|
553
460
|
if (!column._span) {
|
|
554
461
|
this.processNode(column);
|
|
555
462
|
addAll(positions, column.positions);
|
|
@@ -558,38 +465,31 @@ class LayoutBuilder {
|
|
|
558
465
|
this.writer.context().markEnding(column);
|
|
559
466
|
}
|
|
560
467
|
}
|
|
561
|
-
|
|
562
468
|
this.writer.context().completeColumnGroup(height);
|
|
563
469
|
this.writer.removeListener('pageChanged', storePageBreakData);
|
|
564
470
|
return {
|
|
565
471
|
pageBreaks: pageBreaks,
|
|
566
472
|
positions: positions
|
|
567
473
|
};
|
|
568
|
-
|
|
569
474
|
function colLeftOffset(i) {
|
|
570
475
|
if (gaps && gaps.length > i) {
|
|
571
476
|
return gaps[i];
|
|
572
477
|
}
|
|
573
|
-
|
|
574
478
|
return 0;
|
|
575
479
|
}
|
|
576
|
-
|
|
577
480
|
function getEndingCell(column, columnIndex) {
|
|
578
481
|
if (column.rowSpan && column.rowSpan > 1) {
|
|
579
482
|
let endingRow = tableRow + column.rowSpan - 1;
|
|
580
|
-
|
|
581
483
|
if (endingRow >= tableBody.length) {
|
|
582
484
|
throw new Error(`Row span for column ${columnIndex} (with indexes starting from 0) exceeded row count`);
|
|
583
485
|
}
|
|
584
|
-
|
|
585
486
|
return tableBody[endingRow][columnIndex];
|
|
586
487
|
}
|
|
587
|
-
|
|
588
488
|
return null;
|
|
589
489
|
}
|
|
590
|
-
}
|
|
591
|
-
|
|
490
|
+
}
|
|
592
491
|
|
|
492
|
+
// lists
|
|
593
493
|
processList(orderedList, node) {
|
|
594
494
|
const addMarkerToFirstLeaf = line => {
|
|
595
495
|
// I'm not very happy with the way list processing is implemented
|
|
@@ -597,7 +497,6 @@ class LayoutBuilder {
|
|
|
597
497
|
if (nextMarker) {
|
|
598
498
|
let marker = nextMarker;
|
|
599
499
|
nextMarker = null;
|
|
600
|
-
|
|
601
500
|
if (marker.canvas) {
|
|
602
501
|
let vector = marker.canvas[0];
|
|
603
502
|
(0, _tools.offsetVector)(vector, -marker._minWidth, 0);
|
|
@@ -611,7 +510,6 @@ class LayoutBuilder {
|
|
|
611
510
|
}
|
|
612
511
|
}
|
|
613
512
|
};
|
|
614
|
-
|
|
615
513
|
let items = orderedList ? node.ol : node.ul;
|
|
616
514
|
let gapSize = node._gapSize;
|
|
617
515
|
this.writer.context().addMargin(gapSize.width);
|
|
@@ -624,18 +522,16 @@ class LayoutBuilder {
|
|
|
624
522
|
});
|
|
625
523
|
this.writer.removeListener('lineAdded', addMarkerToFirstLeaf);
|
|
626
524
|
this.writer.context().addMargin(-gapSize.width);
|
|
627
|
-
}
|
|
628
|
-
|
|
525
|
+
}
|
|
629
526
|
|
|
527
|
+
// tables
|
|
630
528
|
processTable(tableNode) {
|
|
631
529
|
let processor = new _TableProcessor.default(tableNode);
|
|
632
530
|
processor.beginTable(this.writer);
|
|
633
531
|
let rowHeights = tableNode.table.heights;
|
|
634
|
-
|
|
635
532
|
for (let i = 0, l = tableNode.table.body.length; i < l; i++) {
|
|
636
533
|
processor.beginRow(i, this.writer);
|
|
637
534
|
let height;
|
|
638
|
-
|
|
639
535
|
if (typeof rowHeights === 'function') {
|
|
640
536
|
height = rowHeights(i);
|
|
641
537
|
} else if (Array.isArray(rowHeights)) {
|
|
@@ -643,167 +539,130 @@ class LayoutBuilder {
|
|
|
643
539
|
} else {
|
|
644
540
|
height = rowHeights;
|
|
645
541
|
}
|
|
646
|
-
|
|
647
542
|
if (height === 'auto') {
|
|
648
543
|
height = undefined;
|
|
649
544
|
}
|
|
650
|
-
|
|
651
545
|
let result = this.processRow(tableNode.table.body[i], tableNode.table.widths, tableNode._offsets.offsets, tableNode.table.body, i, height);
|
|
652
546
|
addAll(tableNode.positions, result.positions);
|
|
653
547
|
processor.endRow(i, this.writer, result.pageBreaks);
|
|
654
548
|
}
|
|
655
|
-
|
|
656
549
|
processor.endTable(this.writer);
|
|
657
|
-
}
|
|
658
|
-
|
|
550
|
+
}
|
|
659
551
|
|
|
552
|
+
// leafs (texts)
|
|
660
553
|
processLeaf(node) {
|
|
661
554
|
let line = this.buildNextLine(node);
|
|
662
|
-
|
|
663
555
|
if (line && (node.tocItem || node.id)) {
|
|
664
556
|
line._node = node;
|
|
665
557
|
}
|
|
666
|
-
|
|
667
558
|
let currentHeight = line ? line.getHeight() : 0;
|
|
668
559
|
let maxHeight = node.maxHeight || -1;
|
|
669
|
-
|
|
670
560
|
if (line) {
|
|
671
561
|
let nodeId = (0, _node.getNodeId)(node);
|
|
672
|
-
|
|
673
562
|
if (nodeId) {
|
|
674
563
|
line.id = nodeId;
|
|
675
564
|
}
|
|
676
565
|
}
|
|
677
|
-
|
|
678
566
|
if (node._tocItemRef) {
|
|
679
567
|
line._pageNodeRef = node._tocItemRef;
|
|
680
568
|
}
|
|
681
|
-
|
|
682
569
|
if (node._pageRef) {
|
|
683
570
|
line._pageNodeRef = node._pageRef._nodeRef;
|
|
684
571
|
}
|
|
685
|
-
|
|
686
572
|
if (line && line.inlines && Array.isArray(line.inlines)) {
|
|
687
573
|
for (let i = 0, l = line.inlines.length; i < l; i++) {
|
|
688
574
|
if (line.inlines[i]._tocItemRef) {
|
|
689
575
|
line.inlines[i]._pageNodeRef = line.inlines[i]._tocItemRef;
|
|
690
576
|
}
|
|
691
|
-
|
|
692
577
|
if (line.inlines[i]._pageRef) {
|
|
693
578
|
line.inlines[i]._pageNodeRef = line.inlines[i]._pageRef._nodeRef;
|
|
694
579
|
}
|
|
695
580
|
}
|
|
696
581
|
}
|
|
697
|
-
|
|
698
582
|
while (line && (maxHeight === -1 || currentHeight < maxHeight)) {
|
|
699
583
|
let positions = this.writer.addLine(line);
|
|
700
584
|
node.positions.push(positions);
|
|
701
585
|
line = this.buildNextLine(node);
|
|
702
|
-
|
|
703
586
|
if (line) {
|
|
704
587
|
currentHeight += line.getHeight();
|
|
705
588
|
}
|
|
706
589
|
}
|
|
707
590
|
}
|
|
708
|
-
|
|
709
591
|
processToc(node) {
|
|
710
592
|
if (node.toc.title) {
|
|
711
593
|
this.processNode(node.toc.title);
|
|
712
594
|
}
|
|
713
|
-
|
|
714
595
|
if (node.toc._table) {
|
|
715
596
|
this.processNode(node.toc._table);
|
|
716
597
|
}
|
|
717
598
|
}
|
|
718
|
-
|
|
719
599
|
buildNextLine(textNode) {
|
|
720
600
|
function cloneInline(inline) {
|
|
721
601
|
let newInline = inline.constructor();
|
|
722
|
-
|
|
723
602
|
for (let key in inline) {
|
|
724
603
|
newInline[key] = inline[key];
|
|
725
604
|
}
|
|
726
|
-
|
|
727
605
|
return newInline;
|
|
728
606
|
}
|
|
729
|
-
|
|
730
607
|
if (!textNode._inlines || textNode._inlines.length === 0) {
|
|
731
608
|
return null;
|
|
732
609
|
}
|
|
733
|
-
|
|
734
610
|
let line = new _Line.default(this.writer.context().availableWidth);
|
|
735
611
|
const textInlines = new _TextInlines.default(null);
|
|
736
612
|
let isForceContinue = false;
|
|
737
|
-
|
|
738
613
|
while (textNode._inlines && textNode._inlines.length > 0 && (line.hasEnoughSpaceForInline(textNode._inlines[0], textNode._inlines.slice(1)) || isForceContinue)) {
|
|
739
614
|
let isHardWrap = false;
|
|
740
|
-
|
|
741
615
|
let inline = textNode._inlines.shift();
|
|
742
|
-
|
|
743
616
|
isForceContinue = false;
|
|
744
|
-
|
|
745
617
|
if (!inline.noWrap && inline.text.length > 1 && inline.width > line.getAvailableWidth()) {
|
|
746
618
|
let widthPerChar = inline.width / inline.text.length;
|
|
747
619
|
let maxChars = Math.floor(line.getAvailableWidth() / widthPerChar);
|
|
748
|
-
|
|
749
620
|
if (maxChars < 1) {
|
|
750
621
|
maxChars = 1;
|
|
751
622
|
}
|
|
752
|
-
|
|
753
623
|
if (maxChars < inline.text.length) {
|
|
754
624
|
let newInline = cloneInline(inline);
|
|
755
625
|
newInline.text = inline.text.substr(maxChars);
|
|
756
626
|
inline.text = inline.text.substr(0, maxChars);
|
|
757
627
|
newInline.width = textInlines.widthOfText(newInline.text, newInline);
|
|
758
628
|
inline.width = textInlines.widthOfText(inline.text, inline);
|
|
759
|
-
|
|
760
629
|
textNode._inlines.unshift(newInline);
|
|
761
|
-
|
|
762
630
|
isHardWrap = true;
|
|
763
631
|
}
|
|
764
632
|
}
|
|
765
|
-
|
|
766
633
|
line.addInline(inline);
|
|
767
634
|
isForceContinue = inline.noNewLine && !isHardWrap;
|
|
768
635
|
}
|
|
769
|
-
|
|
770
636
|
line.lastLineInParagraph = textNode._inlines.length === 0;
|
|
771
637
|
return line;
|
|
772
|
-
}
|
|
773
|
-
|
|
638
|
+
}
|
|
774
639
|
|
|
640
|
+
// images
|
|
775
641
|
processImage(node) {
|
|
776
642
|
let position = this.writer.addImage(node);
|
|
777
643
|
node.positions.push(position);
|
|
778
644
|
}
|
|
779
|
-
|
|
780
645
|
processCanvas(node) {
|
|
781
646
|
let positions = this.writer.addCanvas(node);
|
|
782
647
|
addAll(node.positions, positions);
|
|
783
648
|
}
|
|
784
|
-
|
|
785
649
|
processSVG(node) {
|
|
786
650
|
let position = this.writer.addSVG(node);
|
|
787
651
|
node.positions.push(position);
|
|
788
652
|
}
|
|
789
|
-
|
|
790
653
|
processQr(node) {
|
|
791
654
|
let position = this.writer.addQr(node);
|
|
792
655
|
node.positions.push(position);
|
|
793
656
|
}
|
|
794
|
-
|
|
795
657
|
processAttachment(node) {
|
|
796
658
|
let position = this.writer.addAttachment(node);
|
|
797
659
|
node.positions.push(position);
|
|
798
660
|
}
|
|
799
|
-
|
|
800
661
|
}
|
|
801
|
-
|
|
802
662
|
function decorateNode(node) {
|
|
803
663
|
let x = node.x;
|
|
804
664
|
let y = node.y;
|
|
805
665
|
node.positions = [];
|
|
806
|
-
|
|
807
666
|
if (Array.isArray(node.canvas)) {
|
|
808
667
|
node.canvas.forEach(vector => {
|
|
809
668
|
let x = vector.x;
|
|
@@ -812,7 +671,6 @@ function decorateNode(node) {
|
|
|
812
671
|
let y1 = vector.y1;
|
|
813
672
|
let x2 = vector.x2;
|
|
814
673
|
let y2 = vector.y2;
|
|
815
|
-
|
|
816
674
|
vector.resetXY = () => {
|
|
817
675
|
vector.x = x;
|
|
818
676
|
vector.y = y;
|
|
@@ -823,11 +681,9 @@ function decorateNode(node) {
|
|
|
823
681
|
};
|
|
824
682
|
});
|
|
825
683
|
}
|
|
826
|
-
|
|
827
684
|
node.resetXY = () => {
|
|
828
685
|
node.x = x;
|
|
829
686
|
node.y = y;
|
|
830
|
-
|
|
831
687
|
if (Array.isArray(node.canvas)) {
|
|
832
688
|
node.canvas.forEach(vector => {
|
|
833
689
|
vector.resetXY();
|
|
@@ -835,6 +691,5 @@ function decorateNode(node) {
|
|
|
835
691
|
}
|
|
836
692
|
};
|
|
837
693
|
}
|
|
838
|
-
|
|
839
694
|
var _default = LayoutBuilder;
|
|
840
695
|
exports.default = _default;
|