pdfmake-acroforms 0.3.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.
Files changed (115) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/LICENSE +21 -0
  3. package/README.md +75 -0
  4. package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
  5. package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
  6. package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  7. package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
  8. package/build/fonts/Roboto.js +27 -0
  9. package/build/pdfmake.js +67806 -0
  10. package/build/pdfmake.js.map +1 -0
  11. package/build/pdfmake.min.js +3 -0
  12. package/build/pdfmake.min.js.map +1 -0
  13. package/build/standard-fonts/Courier.js +27 -0
  14. package/build/standard-fonts/Helvetica.js +27 -0
  15. package/build/standard-fonts/Symbol.js +21 -0
  16. package/build/standard-fonts/Times.js +27 -0
  17. package/build/standard-fonts/ZapfDingbats.js +21 -0
  18. package/build/vfs_fonts.js +6 -0
  19. package/build-vfs.js +44 -0
  20. package/fonts/Roboto/Roboto-Italic.ttf +0 -0
  21. package/fonts/Roboto/Roboto-Medium.ttf +0 -0
  22. package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  23. package/fonts/Roboto/Roboto-Regular.ttf +0 -0
  24. package/fonts/Roboto.js +8 -0
  25. package/js/3rd-party/svg-to-pdfkit/source.js +3823 -0
  26. package/js/3rd-party/svg-to-pdfkit.js +7 -0
  27. package/js/DocMeasure.js +675 -0
  28. package/js/DocPreprocessor.js +258 -0
  29. package/js/DocumentContext.js +310 -0
  30. package/js/ElementWriter.js +399 -0
  31. package/js/LayoutBuilder.js +1202 -0
  32. package/js/Line.js +101 -0
  33. package/js/OutputDocument.js +64 -0
  34. package/js/OutputDocumentServer.js +29 -0
  35. package/js/PDFDocument.js +145 -0
  36. package/js/PageElementWriter.js +164 -0
  37. package/js/PageSize.js +74 -0
  38. package/js/Printer.js +288 -0
  39. package/js/Renderer.js +513 -0
  40. package/js/SVGMeasure.js +92 -0
  41. package/js/StyleContextStack.js +191 -0
  42. package/js/TableProcessor.js +562 -0
  43. package/js/TextBreaker.js +179 -0
  44. package/js/TextDecorator.js +152 -0
  45. package/js/TextInlines.js +212 -0
  46. package/js/URLResolver.js +43 -0
  47. package/js/base.js +59 -0
  48. package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
  49. package/js/browser-extensions/fonts/Roboto.js +38 -0
  50. package/js/browser-extensions/index.js +53 -0
  51. package/js/browser-extensions/pdfMake.js +3 -0
  52. package/js/browser-extensions/standard-fonts/Courier.js +38 -0
  53. package/js/browser-extensions/standard-fonts/Helvetica.js +38 -0
  54. package/js/browser-extensions/standard-fonts/Symbol.js +23 -0
  55. package/js/browser-extensions/standard-fonts/Times.js +38 -0
  56. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +23 -0
  57. package/js/browser-extensions/virtual-fs-cjs.js +3 -0
  58. package/js/columnCalculator.js +148 -0
  59. package/js/helpers/node.js +122 -0
  60. package/js/helpers/tools.js +46 -0
  61. package/js/helpers/variableType.js +59 -0
  62. package/js/index.js +15 -0
  63. package/js/qrEnc.js +721 -0
  64. package/js/standardPageSizes.js +56 -0
  65. package/js/tableLayouts.js +98 -0
  66. package/js/virtual-fs.js +60 -0
  67. package/package.json +94 -0
  68. package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -0
  69. package/src/3rd-party/svg-to-pdfkit/source.js +2745 -0
  70. package/src/3rd-party/svg-to-pdfkit.js +3 -0
  71. package/src/DocMeasure.js +768 -0
  72. package/src/DocPreprocessor.js +289 -0
  73. package/src/DocumentContext.js +345 -0
  74. package/src/ElementWriter.js +468 -0
  75. package/src/LayoutBuilder.js +1366 -0
  76. package/src/Line.js +108 -0
  77. package/src/OutputDocument.js +64 -0
  78. package/src/OutputDocumentServer.js +32 -0
  79. package/src/PDFDocument.js +178 -0
  80. package/src/PageElementWriter.js +191 -0
  81. package/src/PageSize.js +53 -0
  82. package/src/Printer.js +306 -0
  83. package/src/Renderer.js +546 -0
  84. package/src/SVGMeasure.js +109 -0
  85. package/src/StyleContextStack.js +208 -0
  86. package/src/TableProcessor.js +620 -0
  87. package/src/TextBreaker.js +181 -0
  88. package/src/TextDecorator.js +175 -0
  89. package/src/TextInlines.js +229 -0
  90. package/src/URLResolver.js +43 -0
  91. package/src/base.js +70 -0
  92. package/src/browser-extensions/OutputDocumentBrowser.js +80 -0
  93. package/src/browser-extensions/fonts/Roboto.js +27 -0
  94. package/src/browser-extensions/index.js +55 -0
  95. package/src/browser-extensions/pdfMake.js +1 -0
  96. package/src/browser-extensions/standard-fonts/Courier.js +27 -0
  97. package/src/browser-extensions/standard-fonts/Helvetica.js +27 -0
  98. package/src/browser-extensions/standard-fonts/Symbol.js +21 -0
  99. package/src/browser-extensions/standard-fonts/Times.js +27 -0
  100. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -0
  101. package/src/browser-extensions/virtual-fs-cjs.js +1 -0
  102. package/src/columnCalculator.js +154 -0
  103. package/src/helpers/node.js +134 -0
  104. package/src/helpers/tools.js +44 -0
  105. package/src/helpers/variableType.js +50 -0
  106. package/src/index.js +16 -0
  107. package/src/qrEnc.js +796 -0
  108. package/src/standardPageSizes.js +52 -0
  109. package/src/tableLayouts.js +100 -0
  110. package/src/virtual-fs.js +66 -0
  111. package/standard-fonts/Courier.js +8 -0
  112. package/standard-fonts/Helvetica.js +8 -0
  113. package/standard-fonts/Symbol.js +5 -0
  114. package/standard-fonts/Times.js +8 -0
  115. package/standard-fonts/ZapfDingbats.js +5 -0
@@ -0,0 +1,768 @@
1
+ import TextInlines from './TextInlines';
2
+ import StyleContextStack from './StyleContextStack';
3
+ import ColumnCalculator from './columnCalculator';
4
+ import { defaultTableLayout } from './tableLayouts';
5
+ import { isString, isNumber, isObject } from './helpers/variableType';
6
+ import { stringifyNode, getNodeId, getNodeMargin } from './helpers/node';
7
+ import { pack } from './helpers/tools';
8
+ import qrEncoder from './qrEnc.js';
9
+
10
+ class DocMeasure {
11
+ constructor(
12
+ pdfDocument,
13
+ styleDictionary,
14
+ defaultStyle,
15
+ svgMeasure,
16
+ tableLayouts
17
+ ) {
18
+ this.pdfDocument = pdfDocument;
19
+ this.textInlines = new TextInlines(pdfDocument);
20
+ this.styleStack = new StyleContextStack(styleDictionary, defaultStyle);
21
+ this.svgMeasure = svgMeasure;
22
+ this.tableLayouts = tableLayouts;
23
+ this.autoImageIndex = 1;
24
+ }
25
+
26
+ /**
27
+ * Measures all nodes and sets min/max-width properties required for the second
28
+ * layout-pass.
29
+ *
30
+ * @param {object} docStructure document-definition-object
31
+ * @returns {object} document-measurement-object
32
+ */
33
+ measureDocument(docStructure) {
34
+ return this.measureNode(docStructure);
35
+ }
36
+
37
+ measureBlock(node) {
38
+ return this.measureNode(node);
39
+ }
40
+
41
+ measureNode(node) {
42
+ return this.styleStack.auto(node, () => {
43
+ // TODO: refactor + rethink whether this is the proper way to handle margins
44
+ node._margin = getNodeMargin(node, this.styleStack);
45
+
46
+ if (node.section) {
47
+ return extendMargins(this.measureSection(node));
48
+ } else if (node.columns) {
49
+ return extendMargins(this.measureColumns(node));
50
+ } else if (node.stack) {
51
+ return extendMargins(this.measureVerticalContainer(node));
52
+ } else if (node.ul) {
53
+ return extendMargins(this.measureUnorderedList(node));
54
+ } else if (node.ol) {
55
+ return extendMargins(this.measureOrderedList(node));
56
+ } else if (node.table) {
57
+ return extendMargins(this.measureTable(node));
58
+ } else if (node.text !== undefined) {
59
+ return extendMargins(this.measureLeaf(node));
60
+ } else if (node.toc) {
61
+ return extendMargins(this.measureToc(node));
62
+ } else if (node.image) {
63
+ return extendMargins(this.measureImage(node));
64
+ } else if (node.svg) {
65
+ return extendMargins(this.measureSVG(node));
66
+ } else if (node.canvas) {
67
+ return extendMargins(this.measureCanvas(node));
68
+ } else if (node.qr) {
69
+ return extendMargins(this.measureQr(node));
70
+ } else if (node.attachment) {
71
+ return extendMargins(this.measureAttachment(node));
72
+ } else if (node.acroform) {
73
+ return extendMargins(this.measureAcroForm(node));
74
+ } else {
75
+ throw new Error(`Unrecognized document structure: ${stringifyNode(node)}`);
76
+ }
77
+ });
78
+
79
+ function extendMargins(node) {
80
+ let margin = node._margin;
81
+
82
+ if (margin) {
83
+ node._minWidth += margin[0] + margin[2];
84
+ node._maxWidth += margin[0] + margin[2];
85
+ }
86
+
87
+ return node;
88
+ }
89
+ }
90
+
91
+ measureImageWithDimensions(node, dimensions) {
92
+ if (node.fit) {
93
+ let factor = (dimensions.width / dimensions.height > node.fit[0] / node.fit[1]) ? node.fit[0] / dimensions.width : node.fit[1] / dimensions.height;
94
+ node._width = node._minWidth = node._maxWidth = dimensions.width * factor;
95
+ node._height = dimensions.height * factor;
96
+ } else if (node.cover) {
97
+ node._width = node._minWidth = node._maxWidth = node.cover.width;
98
+ node._height = node._minHeight = node._maxHeight = node.cover.height;
99
+ } else {
100
+ let ratio = dimensions.width / dimensions.height;
101
+
102
+ node._width = node._minWidth = node._maxWidth = node.width || (node.height ? (node.height * ratio) : dimensions.width);
103
+ node._height = node.height || (node.width ? node.width / ratio : dimensions.height);
104
+
105
+ if (isNumber(node.maxWidth) && node.maxWidth < node._width) {
106
+ node._width = node._minWidth = node._maxWidth = node.maxWidth;
107
+ node._height = node._width * dimensions.height / dimensions.width;
108
+ }
109
+
110
+ if (isNumber(node.maxHeight) && node.maxHeight < node._height) {
111
+ node._height = node.maxHeight;
112
+ node._width = node._minWidth = node._maxWidth = node._height * dimensions.width / dimensions.height;
113
+ }
114
+
115
+ if (isNumber(node.minWidth) && node.minWidth > node._width) {
116
+ node._width = node._minWidth = node._maxWidth = node.minWidth;
117
+ node._height = node._width * dimensions.height / dimensions.width;
118
+ }
119
+
120
+ if (isNumber(node.minHeight) && node.minHeight > node._height) {
121
+ node._height = node.minHeight;
122
+ node._width = node._minWidth = node._maxWidth = node._height * dimensions.width / dimensions.height;
123
+ }
124
+ }
125
+
126
+ node._alignment = this.styleStack.getProperty('alignment');
127
+ }
128
+
129
+ convertIfBase64Image(node) {
130
+ if (/^data:image\/(jpeg|jpg|png);base64,/.test(node.image)) { // base64 image
131
+ let label = `$$pdfmake$$${this.autoImageIndex++}`;
132
+ this.pdfDocument.images[label] = node.image;
133
+ node.image = label;
134
+ }
135
+ }
136
+
137
+ measureImage(node) {
138
+ this.convertIfBase64Image(node);
139
+
140
+ let image = this.pdfDocument.provideImage(node.image);
141
+
142
+ let imageSize = { width: image.width, height: image.height };
143
+
144
+ // If EXIF orientation calls for it, swap width and height
145
+ if (image.orientation > 4) {
146
+ imageSize = { width: image.height, height: image.width };
147
+ }
148
+
149
+ this.measureImageWithDimensions(node, imageSize);
150
+
151
+ return node;
152
+ }
153
+
154
+ measureSVG(node) {
155
+ let dimensions = this.svgMeasure.measureSVG(node.svg);
156
+
157
+ this.measureImageWithDimensions(node, dimensions);
158
+
159
+ node.font = this.styleStack.getProperty('font');
160
+
161
+ // SVG requires a defined width and height
162
+ if (!isNumber(node._width) && !isNumber(node._height)) {
163
+ throw new Error('SVG is missing defined width and height.');
164
+ } else if (!isNumber(node._width)) {
165
+ throw new Error('SVG is missing defined width.');
166
+ } else if (!isNumber(node._height)) {
167
+ throw new Error('SVG is missing defined height.');
168
+ }
169
+
170
+ // scale SVG based on final dimension
171
+ node.svg = this.svgMeasure.writeDimensions(node.svg, { width: node._width, height: node._height });
172
+
173
+ return node;
174
+ }
175
+
176
+ measureLeaf(node) {
177
+
178
+ if (node._textRef && node._textRef._textNodeRef.text) {
179
+ node.text = node._textRef._textNodeRef.text;
180
+ }
181
+
182
+ // Make sure style properties of the node itself are considered when building inlines.
183
+ // We could also just pass [node] to buildInlines, but that fails for bullet points.
184
+ let styleStack = this.styleStack.clone();
185
+ styleStack.push(node);
186
+
187
+ let data = this.textInlines.buildInlines(node.text, styleStack);
188
+
189
+ node._inlines = data.items;
190
+ node._minWidth = data.minWidth;
191
+ node._maxWidth = data.maxWidth;
192
+
193
+ return node;
194
+ }
195
+
196
+ measureToc(node) {
197
+ if (node.toc.title) {
198
+ node.toc.title = this.measureNode(node.toc.title);
199
+ }
200
+
201
+ if (node.toc._items.length > 0) {
202
+ let body = [];
203
+ let textStyle = node.toc.textStyle || {};
204
+ let numberStyle = node.toc.numberStyle || textStyle;
205
+ let textMargin = node.toc.textMargin || [0, 0, 0, 0];
206
+
207
+ if (node.toc.sortBy === 'title') {
208
+ node.toc._items.sort((a, b) => {
209
+ return a._textNodeRef.text.localeCompare(b._textNodeRef.text, node.toc.sortLocale);
210
+ });
211
+ }
212
+
213
+ for (let i = 0, l = node.toc._items.length; i < l; i++) {
214
+ let item = node.toc._items[i];
215
+ let lineStyle = item._textNodeRef.tocStyle || textStyle;
216
+ let lineMargin = item._textNodeRef.tocMargin || textMargin;
217
+ let lineNumberStyle = item._textNodeRef.tocNumberStyle || numberStyle;
218
+ let destination = getNodeId(item._nodeRef);
219
+ body.push([
220
+ { text: item._textNodeRef.text, linkToDestination: destination, alignment: 'left', style: lineStyle, margin: lineMargin },
221
+ { text: '00000', linkToDestination: destination, alignment: 'right', _tocItemRef: item._nodeRef, style: lineNumberStyle, margin: [0, lineMargin[1], 0, lineMargin[3]] }
222
+ ]);
223
+
224
+ if (node.toc.outlines) {
225
+ item._textNodeRef.outline = item._textNodeRef.outline || true;
226
+ }
227
+ }
228
+
229
+ node.toc._table = {
230
+ table: {
231
+ dontBreakRows: true,
232
+ widths: ['*', 'auto'],
233
+ body: body
234
+ },
235
+ layout: 'noBorders'
236
+ };
237
+
238
+ node.toc._table = this.measureNode(node.toc._table);
239
+ }
240
+
241
+ return node;
242
+ }
243
+
244
+ measureVerticalContainer(node) {
245
+ let items = node.stack;
246
+
247
+ node._minWidth = 0;
248
+ node._maxWidth = 0;
249
+
250
+ for (let i = 0, l = items.length; i < l; i++) {
251
+ items[i] = this.measureNode(items[i]);
252
+
253
+ node._minWidth = Math.max(node._minWidth, items[i]._minWidth);
254
+ node._maxWidth = Math.max(node._maxWidth, items[i]._maxWidth);
255
+ }
256
+
257
+ return node;
258
+ }
259
+
260
+ gapSizeForList() {
261
+ return this.textInlines.sizeOfText('9. ', this.styleStack);
262
+ }
263
+
264
+ buildUnorderedMarker(item, styleStack, gapSize, type) {
265
+ function buildDisc(gapSize, color) {
266
+ // TODO: ascender-based calculations
267
+ let radius = gapSize.fontSize / 6;
268
+ return {
269
+ canvas: [{
270
+ x: radius,
271
+ y: (gapSize.height / gapSize.lineHeight) + gapSize.descender - gapSize.fontSize / 3,
272
+ r1: radius,
273
+ r2: radius,
274
+ type: 'ellipse',
275
+ color: color
276
+ }]
277
+ };
278
+ }
279
+
280
+ function buildSquare(gapSize, color) {
281
+ // TODO: ascender-based calculations
282
+ let size = gapSize.fontSize / 3;
283
+ return {
284
+ canvas: [{
285
+ x: 0,
286
+ y: (gapSize.height / gapSize.lineHeight) + gapSize.descender - (gapSize.fontSize / 3) - (size / 2),
287
+ h: size,
288
+ w: size,
289
+ type: 'rect',
290
+ color: color
291
+ }]
292
+ };
293
+ }
294
+
295
+ function buildCircle(gapSize, color) {
296
+ // TODO: ascender-based calculations
297
+ let radius = gapSize.fontSize / 6;
298
+ return {
299
+ canvas: [{
300
+ x: radius,
301
+ y: (gapSize.height / gapSize.lineHeight) + gapSize.descender - gapSize.fontSize / 3,
302
+ r1: radius,
303
+ r2: radius,
304
+ type: 'ellipse',
305
+ lineColor: color
306
+ }]
307
+ };
308
+ }
309
+
310
+ let marker;
311
+ let color = StyleContextStack.getStyleProperty(item, styleStack, 'markerColor', undefined) || styleStack.getProperty('color') || 'black';
312
+
313
+ switch (type) {
314
+ case 'circle':
315
+ marker = buildCircle(gapSize, color);
316
+ break;
317
+
318
+ case 'square':
319
+ marker = buildSquare(gapSize, color);
320
+ break;
321
+
322
+ case 'none':
323
+ marker = {};
324
+ break;
325
+
326
+ case 'disc':
327
+ default:
328
+ marker = buildDisc(gapSize, color);
329
+ break;
330
+ }
331
+
332
+ marker._minWidth = marker._maxWidth = gapSize.width;
333
+ marker._minHeight = marker._maxHeight = gapSize.height;
334
+
335
+ return marker;
336
+ }
337
+
338
+ buildOrderedMarker(item, counter, styleStack, type, separator) {
339
+ function prepareAlpha(counter) {
340
+ function toAlpha(num) {
341
+ return (num >= 26 ? toAlpha((num / 26 >> 0) - 1) : '') + 'abcdefghijklmnopqrstuvwxyz'[num % 26 >> 0];
342
+ }
343
+
344
+ if (counter < 1) {
345
+ return counter.toString();
346
+ }
347
+
348
+ return toAlpha(counter - 1);
349
+ }
350
+
351
+ function prepareRoman(counter) {
352
+ if (counter < 1 || counter > 4999) {
353
+ return counter.toString();
354
+ }
355
+ let num = counter;
356
+ let lookup = { M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 };
357
+ let roman = '';
358
+ for (let i in lookup) {
359
+ while (num >= lookup[i]) {
360
+ roman += i;
361
+ num -= lookup[i];
362
+ }
363
+ }
364
+ return roman;
365
+ }
366
+
367
+ function prepareDecimal(counter) {
368
+ return counter.toString();
369
+ }
370
+
371
+ let counterText;
372
+ switch (type) {
373
+ case 'none':
374
+ counterText = null;
375
+ break;
376
+
377
+ case 'upper-alpha':
378
+ counterText = prepareAlpha(counter).toUpperCase();
379
+ break;
380
+
381
+ case 'lower-alpha':
382
+ counterText = prepareAlpha(counter);
383
+ break;
384
+
385
+ case 'upper-roman':
386
+ counterText = prepareRoman(counter);
387
+ break;
388
+
389
+ case 'lower-roman':
390
+ counterText = prepareRoman(counter).toLowerCase();
391
+ break;
392
+
393
+ case 'decimal':
394
+ default:
395
+ counterText = prepareDecimal(counter);
396
+ break;
397
+ }
398
+
399
+ if (counterText === null) {
400
+ return {};
401
+ }
402
+
403
+ if (separator) {
404
+ if (Array.isArray(separator)) {
405
+ if (separator[0]) {
406
+ counterText = separator[0] + counterText;
407
+ }
408
+
409
+ if (separator[1]) {
410
+ counterText += separator[1];
411
+ }
412
+ counterText += ' ';
413
+ } else {
414
+ counterText += `${separator} `;
415
+ }
416
+ }
417
+
418
+ let markerColor = StyleContextStack.getStyleProperty(item, styleStack, 'markerColor', undefined) || styleStack.getProperty('color') || 'black';
419
+ let textArray = {
420
+ text: counterText,
421
+ color: markerColor
422
+ };
423
+
424
+ return { _inlines: this.textInlines.buildInlines(textArray, styleStack).items };
425
+ }
426
+
427
+ measureUnorderedList(node) {
428
+ let style = this.styleStack.clone();
429
+ let items = node.ul;
430
+ node.type = node.type || 'disc';
431
+ node._gapSize = this.gapSizeForList();
432
+ node._minWidth = 0;
433
+ node._maxWidth = 0;
434
+
435
+ for (let i = 0, l = items.length; i < l; i++) {
436
+ let item = items[i] = this.measureNode(items[i]);
437
+
438
+ if (!item.ol && !item.ul) {
439
+ item.listMarker = this.buildUnorderedMarker(item, style, node._gapSize, item.listType || node.type);
440
+ }
441
+
442
+ node._minWidth = Math.max(node._minWidth, items[i]._minWidth + node._gapSize.width);
443
+ node._maxWidth = Math.max(node._maxWidth, items[i]._maxWidth + node._gapSize.width);
444
+ }
445
+
446
+ return node;
447
+ }
448
+
449
+ measureOrderedList(node) {
450
+ let style = this.styleStack.clone();
451
+ let items = node.ol;
452
+ node.type = node.type || 'decimal';
453
+ node.separator = node.separator || '.';
454
+ node.reversed = node.reversed || false;
455
+ if (!isNumber(node.start)) {
456
+ node.start = node.reversed ? items.length : 1;
457
+ }
458
+ node._gapSize = this.gapSizeForList();
459
+ node._minWidth = 0;
460
+ node._maxWidth = 0;
461
+
462
+ let counter = node.start;
463
+ for (let i = 0, l = items.length; i < l; i++) {
464
+ let item = items[i] = this.measureNode(items[i]);
465
+
466
+ if (!item.ol && !item.ul) {
467
+ let counterValue = isNumber(item.counter) ? item.counter : counter;
468
+ item.listMarker = this.buildOrderedMarker(item, counterValue, style, item.listType || node.type, node.separator);
469
+ if (item.listMarker._inlines) {
470
+ node._gapSize.width = Math.max(node._gapSize.width, item.listMarker._inlines[0].width);
471
+ }
472
+
473
+ if (node.reversed) {
474
+ counter--;
475
+ } else {
476
+ counter++;
477
+ }
478
+ }
479
+
480
+ node._minWidth = Math.max(node._minWidth, items[i]._minWidth);
481
+ node._maxWidth = Math.max(node._maxWidth, items[i]._maxWidth);
482
+
483
+ }
484
+
485
+ node._minWidth += node._gapSize.width;
486
+ node._maxWidth += node._gapSize.width;
487
+
488
+ for (let i = 0, l = items.length; i < l; i++) {
489
+ let item = items[i];
490
+ if (!item.ol && !item.ul) {
491
+ item.listMarker._minWidth = item.listMarker._maxWidth = node._gapSize.width;
492
+ }
493
+ }
494
+
495
+ return node;
496
+ }
497
+
498
+ measureSection(node) {
499
+ // TODO: properties
500
+
501
+ node.section = this.measureNode(node.section);
502
+
503
+ return node;
504
+ }
505
+
506
+ measureColumns(node) {
507
+ let columns = node.columns;
508
+ node._gap = this.styleStack.getProperty('columnGap') || 0;
509
+
510
+ for (let i = 0, l = columns.length; i < l; i++) {
511
+ columns[i] = this.measureNode(columns[i]);
512
+ }
513
+
514
+ let measures = ColumnCalculator.measureMinMax(columns);
515
+
516
+ let numGaps = (columns.length > 0) ? (columns.length - 1) : 0;
517
+ node._minWidth = measures.min + node._gap * numGaps;
518
+ node._maxWidth = measures.max + node._gap * numGaps;
519
+
520
+ return node;
521
+ }
522
+
523
+ measureTable(node) {
524
+ extendTableWidths(node);
525
+ node._layout = getLayout(this.tableLayouts);
526
+ node._offsets = getOffsets(node._layout);
527
+
528
+ let colSpans = [];
529
+ let col;
530
+ let row;
531
+ let cols;
532
+ let rows;
533
+
534
+ for (col = 0, cols = node.table.body[0].length; col < cols; col++) {
535
+ let c = node.table.widths[col];
536
+ c._minWidth = 0;
537
+ c._maxWidth = 0;
538
+
539
+ for (row = 0, rows = node.table.body.length; row < rows; row++) {
540
+ let rowData = node.table.body[row];
541
+ let data = rowData[col];
542
+ if (data === undefined) {
543
+ throw new Error(`Malformed table row, a cell is undefined.\nRow index: ${row}\nColumn index: ${col}\nRow data: ${stringifyNode(rowData)}`);
544
+ }
545
+ if (data === null) { // transform to object
546
+ data = '';
547
+ }
548
+
549
+ if (!data._span) {
550
+ data = rowData[col] = this.styleStack.auto(data, measureCb(this, data));
551
+
552
+ if (data.colSpan && data.colSpan > 1) {
553
+ markSpans(rowData, col, data.colSpan);
554
+ colSpans.push({ col: col, span: data.colSpan, minWidth: data._minWidth, maxWidth: data._maxWidth });
555
+ } else {
556
+ c._minWidth = Math.max(c._minWidth, data._minWidth);
557
+ c._maxWidth = Math.max(c._maxWidth, data._maxWidth);
558
+ }
559
+ }
560
+
561
+ if (data.rowSpan && data.rowSpan > 1) {
562
+ markVSpans(node.table, row, col, data.rowSpan);
563
+ }
564
+ }
565
+ }
566
+
567
+ extendWidthsForColSpans();
568
+
569
+ let measures = ColumnCalculator.measureMinMax(node.table.widths);
570
+
571
+ node._minWidth = measures.min + node._offsets.total;
572
+ node._maxWidth = measures.max + node._offsets.total;
573
+
574
+ return node;
575
+
576
+ function measureCb(_this, data) {
577
+ return () => {
578
+ if (isObject(data)) {
579
+ data.fillColor = _this.styleStack.getProperty('fillColor');
580
+ data.fillOpacity = _this.styleStack.getProperty('fillOpacity');
581
+ }
582
+ return _this.measureNode(data);
583
+ };
584
+ }
585
+
586
+ function getLayout(tableLayouts) {
587
+ let layout = node.layout;
588
+
589
+ if (isString(layout)) {
590
+ layout = tableLayouts[layout];
591
+ }
592
+
593
+ return pack(defaultTableLayout, layout);
594
+ }
595
+
596
+ function getOffsets(layout) {
597
+ let offsets = [];
598
+ let totalOffset = 0;
599
+ let prevRightPadding = 0;
600
+
601
+ for (let i = 0, l = node.table.widths.length; i < l; i++) {
602
+ let lOffset = prevRightPadding + layout.vLineWidth(i, node) + layout.paddingLeft(i, node);
603
+ offsets.push(lOffset);
604
+ totalOffset += lOffset;
605
+ prevRightPadding = layout.paddingRight(i, node);
606
+ }
607
+
608
+ totalOffset += prevRightPadding + layout.vLineWidth(node.table.widths.length, node);
609
+
610
+ return {
611
+ total: totalOffset,
612
+ offsets: offsets
613
+ };
614
+ }
615
+
616
+ function extendWidthsForColSpans() {
617
+ let q;
618
+ let j;
619
+
620
+ for (let i = 0, l = colSpans.length; i < l; i++) {
621
+ let span = colSpans[i];
622
+
623
+ let currentMinMax = getMinMax(span.col, span.span, node._offsets);
624
+ let minDifference = span.minWidth - currentMinMax.minWidth;
625
+ let maxDifference = span.maxWidth - currentMinMax.maxWidth;
626
+
627
+ if (minDifference > 0) {
628
+ q = minDifference / span.span;
629
+
630
+ for (j = 0; j < span.span; j++) {
631
+ node.table.widths[span.col + j]._minWidth += q;
632
+ }
633
+ }
634
+
635
+ if (maxDifference > 0) {
636
+ q = maxDifference / span.span;
637
+
638
+ for (j = 0; j < span.span; j++) {
639
+ node.table.widths[span.col + j]._maxWidth += q;
640
+ }
641
+ }
642
+ }
643
+ }
644
+
645
+ function getMinMax(col, span, offsets) {
646
+ let result = { minWidth: 0, maxWidth: 0 };
647
+
648
+ for (let i = 0; i < span; i++) {
649
+ result.minWidth += node.table.widths[col + i]._minWidth + (i ? offsets.offsets[col + i] : 0);
650
+ result.maxWidth += node.table.widths[col + i]._maxWidth + (i ? offsets.offsets[col + i] : 0);
651
+ }
652
+
653
+ return result;
654
+ }
655
+
656
+ function markSpans(rowData, col, span) {
657
+ for (let i = 1; i < span; i++) {
658
+ rowData[col + i] = {
659
+ _span: true,
660
+ _minWidth: 0,
661
+ _maxWidth: 0,
662
+ rowSpan: rowData[col].rowSpan
663
+ };
664
+ }
665
+ }
666
+
667
+ function markVSpans(table, row, col, span) {
668
+ for (let i = 1; i < span; i++) {
669
+ table.body[row + i][col] = {
670
+ _span: true,
671
+ _minWidth: 0,
672
+ _maxWidth: 0,
673
+ fillColor: table.body[row][col].fillColor,
674
+ fillOpacity: table.body[row][col].fillOpacity
675
+ };
676
+ }
677
+ }
678
+
679
+ function extendTableWidths(node) {
680
+ if (!node.table.widths) {
681
+ node.table.widths = 'auto';
682
+ }
683
+
684
+ if (isString(node.table.widths)) {
685
+ node.table.widths = [node.table.widths];
686
+
687
+ while (node.table.widths.length < node.table.body[0].length) {
688
+ node.table.widths.push(node.table.widths[node.table.widths.length - 1]);
689
+ }
690
+ }
691
+
692
+ for (let i = 0, l = node.table.widths.length; i < l; i++) {
693
+ let w = node.table.widths[i];
694
+ if (isNumber(w) || isString(w)) {
695
+ node.table.widths[i] = { width: w };
696
+ }
697
+ }
698
+ }
699
+ }
700
+
701
+ measureCanvas(node) {
702
+ let w = 0;
703
+ let h = 0;
704
+
705
+ for (let i = 0, l = node.canvas.length; i < l; i++) {
706
+ let vector = node.canvas[i];
707
+
708
+ switch (vector.type) {
709
+ case 'ellipse':
710
+ w = Math.max(w, vector.x + vector.r1);
711
+ h = Math.max(h, vector.y + vector.r2);
712
+ break;
713
+ case 'rect':
714
+ w = Math.max(w, vector.x + vector.w);
715
+ h = Math.max(h, vector.y + vector.h);
716
+ break;
717
+ case 'line':
718
+ w = Math.max(w, vector.x1, vector.x2);
719
+ h = Math.max(h, vector.y1, vector.y2);
720
+ break;
721
+ case 'polyline':
722
+ for (let i2 = 0, l2 = vector.points.length; i2 < l2; i2++) {
723
+ w = Math.max(w, vector.points[i2].x);
724
+ h = Math.max(h, vector.points[i2].y);
725
+ }
726
+ break;
727
+ }
728
+ }
729
+
730
+ node._minWidth = node._maxWidth = w;
731
+ node._minHeight = node._maxHeight = h;
732
+ node._alignment = this.styleStack.getProperty('alignment');
733
+
734
+ return node;
735
+ }
736
+
737
+ measureQr(node) {
738
+ node = qrEncoder.measure(node);
739
+ node._alignment = this.styleStack.getProperty('alignment');
740
+ return node;
741
+ }
742
+
743
+ measureAttachment(node) {
744
+ node._width = node.width || 7;
745
+ node._height = node.height || 18;
746
+
747
+ return node;
748
+ }
749
+
750
+ measureAcroForm(node) {
751
+ node._minWidth = 10;
752
+ node._minHeight = 10;
753
+
754
+ let font = StyleContextStack.getStyleProperty(node, this.styleStack, 'font', 'Roboto');
755
+ let bold = StyleContextStack.getStyleProperty(node, this.styleStack, 'bold', false);
756
+ let italics = StyleContextStack.getStyleProperty(node, this.styleStack, 'italics', false);
757
+
758
+ node.font = font;
759
+ node.bold = bold;
760
+ node.italics = italics;
761
+
762
+ node.alignment = StyleContextStack.getStyleProperty(node, this.styleStack, 'alignment', 'left');
763
+
764
+ return node;
765
+ }
766
+ }
767
+
768
+ export default DocMeasure;