pdfmake 0.2.13 → 0.3.0-beta.10

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