pdfmake 0.2.4 → 0.3.0-beta.2

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