pdfmake 0.3.2 → 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 (94) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE +21 -21
  3. package/README.md +75 -78
  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 +64813 -64584
  10. package/build/pdfmake.js.map +1 -1
  11. package/build/pdfmake.min.js +2 -2
  12. package/build/pdfmake.min.js.map +1 -1
  13. package/build/standard-fonts/Courier.js +27 -27
  14. package/build/standard-fonts/Helvetica.js +27 -27
  15. package/build/standard-fonts/Symbol.js +21 -21
  16. package/build/standard-fonts/Times.js +27 -27
  17. package/build/standard-fonts/ZapfDingbats.js +21 -21
  18. package/build/vfs_fonts.js +5 -5
  19. package/build-vfs.js +44 -44
  20. package/fonts/Roboto.js +8 -8
  21. package/js/3rd-party/svg-to-pdfkit/source.js +1 -1
  22. package/js/DocMeasure.js +11 -6
  23. package/js/DocumentContext.js +8 -3
  24. package/js/ElementWriter.js +42 -16
  25. package/js/LayoutBuilder.js +144 -78
  26. package/js/Line.js +16 -16
  27. package/js/OutputDocument.js +10 -10
  28. package/js/OutputDocumentServer.js +3 -3
  29. package/js/PDFDocument.js +3 -3
  30. package/js/PageElementWriter.js +15 -9
  31. package/js/Printer.js +28 -28
  32. package/js/Renderer.js +40 -8
  33. package/js/SVGMeasure.js +10 -10
  34. package/js/StyleContextStack.js +74 -51
  35. package/js/TableProcessor.js +14 -0
  36. package/js/TextBreaker.js +17 -17
  37. package/js/TextDecorator.js +12 -3
  38. package/js/TextInlines.js +34 -33
  39. package/js/base.js +4 -4
  40. package/js/browser-extensions/OutputDocumentBrowser.js +24 -24
  41. package/js/columnCalculator.js +2 -2
  42. package/js/helpers/node.js +47 -23
  43. package/js/helpers/variableType.js +18 -18
  44. package/js/qrEnc.js +38 -38
  45. package/js/virtual-fs.js +11 -11
  46. package/package.json +12 -12
  47. package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -9
  48. package/src/3rd-party/svg-to-pdfkit/source.js +2745 -2745
  49. package/src/3rd-party/svg-to-pdfkit.js +3 -3
  50. package/src/DocMeasure.js +745 -738
  51. package/src/DocPreprocessor.js +283 -283
  52. package/src/DocumentContext.js +345 -338
  53. package/src/ElementWriter.js +441 -417
  54. package/src/LayoutBuilder.js +1336 -1258
  55. package/src/Line.js +114 -114
  56. package/src/OutputDocument.js +64 -64
  57. package/src/OutputDocumentServer.js +32 -32
  58. package/src/PDFDocument.js +174 -174
  59. package/src/PageElementWriter.js +187 -179
  60. package/src/PageSize.js +53 -53
  61. package/src/Printer.js +306 -306
  62. package/src/Renderer.js +445 -409
  63. package/src/SVGMeasure.js +109 -109
  64. package/src/StyleContextStack.js +208 -179
  65. package/src/TableProcessor.js +620 -602
  66. package/src/TextBreaker.js +168 -168
  67. package/src/TextDecorator.js +175 -161
  68. package/src/TextInlines.js +224 -223
  69. package/src/URLResolver.js +43 -43
  70. package/src/base.js +70 -70
  71. package/src/browser-extensions/OutputDocumentBrowser.js +80 -80
  72. package/src/browser-extensions/fonts/Roboto.js +27 -27
  73. package/src/browser-extensions/index.js +55 -55
  74. package/src/browser-extensions/pdfMake.js +1 -1
  75. package/src/browser-extensions/standard-fonts/Courier.js +27 -27
  76. package/src/browser-extensions/standard-fonts/Helvetica.js +27 -27
  77. package/src/browser-extensions/standard-fonts/Symbol.js +21 -21
  78. package/src/browser-extensions/standard-fonts/Times.js +27 -27
  79. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -21
  80. package/src/browser-extensions/virtual-fs-cjs.js +1 -1
  81. package/src/columnCalculator.js +154 -154
  82. package/src/helpers/node.js +134 -110
  83. package/src/helpers/tools.js +44 -44
  84. package/src/helpers/variableType.js +50 -50
  85. package/src/index.js +16 -16
  86. package/src/qrEnc.js +796 -796
  87. package/src/standardPageSizes.js +52 -52
  88. package/src/tableLayouts.js +100 -100
  89. package/src/virtual-fs.js +66 -66
  90. package/standard-fonts/Courier.js +8 -8
  91. package/standard-fonts/Helvetica.js +8 -8
  92. package/standard-fonts/Symbol.js +5 -5
  93. package/standard-fonts/Times.js +8 -8
  94. package/standard-fonts/ZapfDingbats.js +5 -5
@@ -1,338 +1,345 @@
1
- import { isString } from './helpers/variableType';
2
- import { EventEmitter } from 'events';
3
-
4
- /**
5
- * A store for current x, y positions and available width/height.
6
- * It facilitates column divisions and vertical sync
7
- */
8
- class DocumentContext extends EventEmitter {
9
- constructor() {
10
- super();
11
- this.pages = [];
12
- this.pageMargins = undefined;
13
- this.x = undefined;
14
- this.availableWidth = undefined;
15
- this.availableHeight = undefined;
16
- this.page = -1;
17
-
18
- this.snapshots = [];
19
- this.backgroundLength = [];
20
- }
21
-
22
- beginColumnGroup(marginXTopParent, bottomByPage = {}) {
23
- this.snapshots.push({
24
- x: this.x,
25
- y: this.y,
26
- availableHeight: this.availableHeight,
27
- availableWidth: this.availableWidth,
28
- page: this.page,
29
- bottomByPage: bottomByPage ? bottomByPage : {},
30
- bottomMost: {
31
- x: this.x,
32
- y: this.y,
33
- availableHeight: this.availableHeight,
34
- availableWidth: this.availableWidth,
35
- page: this.page
36
- },
37
- lastColumnWidth: this.lastColumnWidth
38
- });
39
-
40
- this.lastColumnWidth = 0;
41
- if (marginXTopParent) {
42
- this.marginXTopParent = marginXTopParent;
43
- }
44
- }
45
-
46
- updateBottomByPage() {
47
- const lastSnapshot = this.snapshots[this.snapshots.length - 1];
48
- const lastPage = this.page;
49
- let previousBottom = -Number.MIN_VALUE;
50
- if (lastSnapshot.bottomByPage[lastPage]) {
51
- previousBottom = lastSnapshot.bottomByPage[lastPage];
52
- }
53
- lastSnapshot.bottomByPage[lastPage] = Math.max(previousBottom, this.y);
54
- }
55
-
56
- resetMarginXTopParent() {
57
- this.marginXTopParent = null;
58
- }
59
-
60
- beginColumn(width, offset, endingCell) {
61
- let saved = this.snapshots[this.snapshots.length - 1];
62
-
63
- this.calculateBottomMost(saved, endingCell);
64
-
65
- this.page = saved.page;
66
- this.x = this.x + this.lastColumnWidth + (offset || 0);
67
- this.y = saved.y;
68
- this.availableWidth = width; //saved.availableWidth - offset;
69
- this.availableHeight = saved.availableHeight;
70
-
71
- this.lastColumnWidth = width;
72
- }
73
-
74
- calculateBottomMost(destContext, endingCell) {
75
- if (endingCell) {
76
- this.saveContextInEndingCell(endingCell);
77
- } else {
78
- destContext.bottomMost = bottomMostContext(this, destContext.bottomMost);
79
- }
80
- }
81
-
82
- markEnding(endingCell, originalXOffset, discountY) {
83
- this.page = endingCell._columnEndingContext.page;
84
- this.x = endingCell._columnEndingContext.x + originalXOffset;
85
- this.y = endingCell._columnEndingContext.y - discountY;
86
- this.availableWidth = endingCell._columnEndingContext.availableWidth;
87
- this.availableHeight = endingCell._columnEndingContext.availableHeight;
88
- this.lastColumnWidth = endingCell._columnEndingContext.lastColumnWidth;
89
- }
90
-
91
- saveContextInEndingCell(endingCell) {
92
- endingCell._columnEndingContext = {
93
- page: this.page,
94
- x: this.x,
95
- y: this.y,
96
- availableHeight: this.availableHeight,
97
- availableWidth: this.availableWidth,
98
- lastColumnWidth: this.lastColumnWidth
99
- };
100
- }
101
-
102
- completeColumnGroup(height, endingCell) {
103
- let saved = this.snapshots.pop();
104
-
105
- this.calculateBottomMost(saved, endingCell);
106
-
107
- this.x = saved.x;
108
-
109
- let y = saved.bottomMost.y;
110
- if (height) {
111
- if (saved.page === saved.bottomMost.page) {
112
- if ((saved.y + height) > y) {
113
- y = saved.y + height;
114
- }
115
- } else {
116
- y += height;
117
- }
118
- }
119
-
120
- this.y = y;
121
- this.page = saved.bottomMost.page;
122
- this.availableWidth = saved.availableWidth;
123
- this.availableHeight = saved.bottomMost.availableHeight;
124
- if (height) {
125
- this.availableHeight -= (y - saved.bottomMost.y);
126
- }
127
- this.lastColumnWidth = saved.lastColumnWidth;
128
- return saved.bottomByPage;
129
- }
130
-
131
- addMargin(left, right) {
132
- this.x += left;
133
- this.availableWidth -= left + (right || 0);
134
- }
135
-
136
- moveDown(offset) {
137
- this.y += offset;
138
- this.availableHeight -= offset;
139
-
140
- return this.availableHeight > 0;
141
- }
142
-
143
- initializePage() {
144
- this.y = this.pageMargins.top;
145
- this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
146
- const { pageCtx, isSnapshot } = this.pageSnapshot();
147
- pageCtx.availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right;
148
- if (isSnapshot && this.marginXTopParent) {
149
- pageCtx.availableWidth -= this.marginXTopParent[0];
150
- pageCtx.availableWidth -= this.marginXTopParent[1];
151
- }
152
- }
153
-
154
- pageSnapshot() {
155
- if (this.snapshots[0]) {
156
- return { pageCtx: this.snapshots[0], isSnapshot: true };
157
- } else {
158
- return { pageCtx: this, isSnapshot: false };
159
- }
160
- }
161
-
162
- moveTo(x, y) {
163
- if (x !== undefined && x !== null) {
164
- this.x = x;
165
- this.availableWidth = this.getCurrentPage().pageSize.width - this.x - this.pageMargins.right;
166
- }
167
- if (y !== undefined && y !== null) {
168
- this.y = y;
169
- this.availableHeight = this.getCurrentPage().pageSize.height - this.y - this.pageMargins.bottom;
170
- }
171
- }
172
-
173
- moveToRelative(x, y) {
174
- if (x !== undefined && x !== null) {
175
- this.x = this.x + x;
176
- }
177
- if (y !== undefined && y !== null) {
178
- this.y = this.y + y;
179
- }
180
- }
181
-
182
- beginDetachedBlock() {
183
- this.snapshots.push({
184
- x: this.x,
185
- y: this.y,
186
- availableHeight: this.availableHeight,
187
- availableWidth: this.availableWidth,
188
- page: this.page,
189
- lastColumnWidth: this.lastColumnWidth
190
- });
191
- }
192
-
193
- endDetachedBlock() {
194
- let saved = this.snapshots.pop();
195
-
196
- this.x = saved.x;
197
- this.y = saved.y;
198
- this.availableWidth = saved.availableWidth;
199
- this.availableHeight = saved.availableHeight;
200
- this.page = saved.page;
201
- this.lastColumnWidth = saved.lastColumnWidth;
202
- }
203
-
204
- moveToNextPage(pageOrientation) {
205
- let nextPageIndex = this.page + 1;
206
- let prevPage = this.page;
207
- let prevY = this.y;
208
-
209
- // If we are in a column group
210
- if (this.snapshots.length > 0) {
211
- let lastSnapshot = this.snapshots[this.snapshots.length - 1];
212
- // We have to update prevY accordingly by also taking into consideration
213
- // the 'y' of cells that don't break page
214
- if (lastSnapshot.bottomMost && lastSnapshot.bottomMost.y) {
215
- prevY = Math.max(this.y, lastSnapshot.bottomMost.y);
216
- }
217
- }
218
-
219
- let createNewPage = nextPageIndex >= this.pages.length;
220
- if (createNewPage) {
221
- let currentAvailableWidth = this.availableWidth;
222
- let currentPageOrientation = this.getCurrentPage().pageSize.orientation;
223
-
224
- let pageSize = getPageSize(this.getCurrentPage(), pageOrientation);
225
- this.addPage(pageSize, null, this.getCurrentPage().customProperties);
226
-
227
- if (currentPageOrientation === pageSize.orientation) {
228
- this.availableWidth = currentAvailableWidth;
229
- }
230
- } else {
231
- this.page = nextPageIndex;
232
- this.initializePage();
233
- }
234
-
235
- return {
236
- newPageCreated: createNewPage,
237
- prevPage: prevPage,
238
- prevY: prevY,
239
- y: this.y
240
- };
241
- }
242
-
243
- addPage(pageSize, pageMargin = null, customProperties = {}) {
244
- if (pageMargin !== null) {
245
- this.pageMargins = pageMargin;
246
- this.x = pageMargin.left;
247
- this.availableWidth = pageSize.width - pageMargin.left - pageMargin.right;
248
- }
249
-
250
- let page = { items: [], pageSize: pageSize, pageMargins: this.pageMargins, customProperties: customProperties };
251
- this.pages.push(page);
252
- this.backgroundLength.push(0);
253
- this.page = this.pages.length - 1;
254
- this.initializePage();
255
-
256
- this.emit('pageAdded', page);
257
-
258
- return page;
259
- }
260
-
261
- getCurrentPage() {
262
- if (this.page < 0 || this.page >= this.pages.length) {
263
- return null;
264
- }
265
-
266
- return this.pages[this.page];
267
- }
268
-
269
- getCurrentPosition() {
270
- let pageSize = this.getCurrentPage().pageSize;
271
- let innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
272
- let innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right;
273
-
274
- return {
275
- pageNumber: this.page + 1,
276
- pageOrientation: pageSize.orientation,
277
- pageInnerHeight: innerHeight,
278
- pageInnerWidth: innerWidth,
279
- left: this.x,
280
- top: this.y,
281
- verticalRatio: ((this.y - this.pageMargins.top) / innerHeight),
282
- horizontalRatio: ((this.x - this.pageMargins.left) / innerWidth)
283
- };
284
- }
285
- }
286
-
287
- function pageOrientation(pageOrientationString, currentPageOrientation) {
288
- if (pageOrientationString === undefined) {
289
- return currentPageOrientation;
290
- } else if (isString(pageOrientationString) && (pageOrientationString.toLowerCase() === 'landscape')) {
291
- return 'landscape';
292
- } else {
293
- return 'portrait';
294
- }
295
- }
296
-
297
- const getPageSize = (currentPage, newPageOrientation) => {
298
-
299
- newPageOrientation = pageOrientation(newPageOrientation, currentPage.pageSize.orientation);
300
-
301
- if (newPageOrientation !== currentPage.pageSize.orientation) {
302
- return {
303
- orientation: newPageOrientation,
304
- width: currentPage.pageSize.height,
305
- height: currentPage.pageSize.width
306
- };
307
- } else {
308
- return {
309
- orientation: currentPage.pageSize.orientation,
310
- width: currentPage.pageSize.width,
311
- height: currentPage.pageSize.height
312
- };
313
- }
314
-
315
- };
316
-
317
-
318
- function bottomMostContext(c1, c2) {
319
- let r;
320
-
321
- if (c1.page > c2.page) {
322
- r = c1;
323
- } else if (c2.page > c1.page) {
324
- r = c2;
325
- } else {
326
- r = (c1.y > c2.y) ? c1 : c2;
327
- }
328
-
329
- return {
330
- page: r.page,
331
- x: r.x,
332
- y: r.y,
333
- availableHeight: r.availableHeight,
334
- availableWidth: r.availableWidth
335
- };
336
- }
337
-
338
- export default DocumentContext;
1
+ import { isString } from './helpers/variableType';
2
+ import { EventEmitter } from 'events';
3
+
4
+ /**
5
+ * A store for current x, y positions and available width/height.
6
+ * It facilitates column divisions and vertical sync
7
+ */
8
+ class DocumentContext extends EventEmitter {
9
+ constructor() {
10
+ super();
11
+ this.pages = [];
12
+ this.pageMargins = undefined;
13
+ this.x = undefined;
14
+ this.availableWidth = undefined;
15
+ this.availableHeight = undefined;
16
+ this.page = -1;
17
+
18
+ this.snapshots = [];
19
+ this.backgroundLength = [];
20
+ }
21
+
22
+ beginColumnGroup(marginXTopParent, bottomByPage = {}) {
23
+ this.snapshots.push({
24
+ x: this.x,
25
+ y: this.y,
26
+ availableHeight: this.availableHeight,
27
+ availableWidth: this.availableWidth,
28
+ page: this.page,
29
+ bottomByPage: bottomByPage ? bottomByPage : {},
30
+ bottomMost: {
31
+ x: this.x,
32
+ y: this.y,
33
+ availableHeight: this.availableHeight,
34
+ availableWidth: this.availableWidth,
35
+ page: this.page
36
+ },
37
+ lastColumnWidth: this.lastColumnWidth
38
+ });
39
+
40
+ this.lastColumnWidth = 0;
41
+ if (marginXTopParent) {
42
+ this.marginXTopParent = marginXTopParent;
43
+ }
44
+ }
45
+
46
+ updateBottomByPage() {
47
+ const lastSnapshot = this.snapshots[this.snapshots.length - 1];
48
+ const lastPage = this.page;
49
+ let previousBottom = -Number.MIN_VALUE;
50
+ if (lastSnapshot.bottomByPage[lastPage]) {
51
+ previousBottom = lastSnapshot.bottomByPage[lastPage];
52
+ }
53
+ lastSnapshot.bottomByPage[lastPage] = Math.max(previousBottom, this.y);
54
+ }
55
+
56
+ resetMarginXTopParent() {
57
+ this.marginXTopParent = null;
58
+ }
59
+
60
+ beginColumn(width, offset, endingCell) {
61
+ let saved = this.snapshots[this.snapshots.length - 1];
62
+
63
+ this.calculateBottomMost(saved, endingCell);
64
+
65
+ this.page = saved.page;
66
+ this.x = this.x + this.lastColumnWidth + (offset || 0);
67
+ this.y = saved.y;
68
+ this.availableWidth = width; //saved.availableWidth - offset;
69
+ this.availableHeight = saved.availableHeight;
70
+
71
+ this.lastColumnWidth = width;
72
+ }
73
+
74
+ calculateBottomMost(destContext, endingCell) {
75
+ if (endingCell) {
76
+ this.saveContextInEndingCell(endingCell);
77
+ } else {
78
+ destContext.bottomMost = bottomMostContext(this, destContext.bottomMost);
79
+ }
80
+ }
81
+
82
+ markEnding(endingCell, originalXOffset, discountY) {
83
+ this.page = endingCell._columnEndingContext.page;
84
+ this.x = endingCell._columnEndingContext.x + originalXOffset;
85
+ this.y = endingCell._columnEndingContext.y - discountY;
86
+ this.availableWidth = endingCell._columnEndingContext.availableWidth;
87
+ this.availableHeight = endingCell._columnEndingContext.availableHeight;
88
+ this.lastColumnWidth = endingCell._columnEndingContext.lastColumnWidth;
89
+ }
90
+
91
+ saveContextInEndingCell(endingCell) {
92
+ endingCell._columnEndingContext = {
93
+ page: this.page,
94
+ x: this.x,
95
+ y: this.y,
96
+ availableHeight: this.availableHeight,
97
+ availableWidth: this.availableWidth,
98
+ lastColumnWidth: this.lastColumnWidth
99
+ };
100
+ }
101
+
102
+ completeColumnGroup(height, endingCell) {
103
+ let saved = this.snapshots.pop();
104
+
105
+ this.calculateBottomMost(saved, endingCell);
106
+
107
+ this.x = saved.x;
108
+
109
+ let y = saved.bottomMost.y;
110
+ if (height) {
111
+ if (saved.page === saved.bottomMost.page) {
112
+ if ((saved.y + height) > y) {
113
+ y = saved.y + height;
114
+ }
115
+ } else {
116
+ y += height;
117
+ }
118
+ }
119
+
120
+ this.y = y;
121
+ this.page = saved.bottomMost.page;
122
+ this.availableWidth = saved.availableWidth;
123
+ this.availableHeight = saved.bottomMost.availableHeight;
124
+ if (height) {
125
+ this.availableHeight -= (y - saved.bottomMost.y);
126
+ }
127
+
128
+ if (height && (saved.bottomMost.y - saved.y < height)) {
129
+ this.height = height;
130
+ } else {
131
+ this.height = saved.bottomMost.y - saved.y;
132
+ }
133
+
134
+ this.lastColumnWidth = saved.lastColumnWidth;
135
+ return saved.bottomByPage;
136
+ }
137
+
138
+ addMargin(left, right) {
139
+ this.x += left;
140
+ this.availableWidth -= left + (right || 0);
141
+ }
142
+
143
+ moveDown(offset) {
144
+ this.y += offset;
145
+ this.availableHeight -= offset;
146
+
147
+ return this.availableHeight > 0;
148
+ }
149
+
150
+ initializePage() {
151
+ this.y = this.pageMargins.top;
152
+ this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
153
+ const { pageCtx, isSnapshot } = this.pageSnapshot();
154
+ pageCtx.availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right;
155
+ if (isSnapshot && this.marginXTopParent) {
156
+ pageCtx.availableWidth -= this.marginXTopParent[0];
157
+ pageCtx.availableWidth -= this.marginXTopParent[1];
158
+ }
159
+ }
160
+
161
+ pageSnapshot() {
162
+ if (this.snapshots[0]) {
163
+ return { pageCtx: this.snapshots[0], isSnapshot: true };
164
+ } else {
165
+ return { pageCtx: this, isSnapshot: false };
166
+ }
167
+ }
168
+
169
+ moveTo(x, y) {
170
+ if (x !== undefined && x !== null) {
171
+ this.x = x;
172
+ this.availableWidth = this.getCurrentPage().pageSize.width - this.x - this.pageMargins.right;
173
+ }
174
+ if (y !== undefined && y !== null) {
175
+ this.y = y;
176
+ this.availableHeight = this.getCurrentPage().pageSize.height - this.y - this.pageMargins.bottom;
177
+ }
178
+ }
179
+
180
+ moveToRelative(x, y) {
181
+ if (x !== undefined && x !== null) {
182
+ this.x = this.x + x;
183
+ }
184
+ if (y !== undefined && y !== null) {
185
+ this.y = this.y + y;
186
+ }
187
+ }
188
+
189
+ beginDetachedBlock() {
190
+ this.snapshots.push({
191
+ x: this.x,
192
+ y: this.y,
193
+ availableHeight: this.availableHeight,
194
+ availableWidth: this.availableWidth,
195
+ page: this.page,
196
+ lastColumnWidth: this.lastColumnWidth
197
+ });
198
+ }
199
+
200
+ endDetachedBlock() {
201
+ let saved = this.snapshots.pop();
202
+
203
+ this.x = saved.x;
204
+ this.y = saved.y;
205
+ this.availableWidth = saved.availableWidth;
206
+ this.availableHeight = saved.availableHeight;
207
+ this.page = saved.page;
208
+ this.lastColumnWidth = saved.lastColumnWidth;
209
+ }
210
+
211
+ moveToNextPage(pageOrientation) {
212
+ let nextPageIndex = this.page + 1;
213
+ let prevPage = this.page;
214
+ let prevY = this.y;
215
+
216
+ // If we are in a column group
217
+ if (this.snapshots.length > 0) {
218
+ let lastSnapshot = this.snapshots[this.snapshots.length - 1];
219
+ // We have to update prevY accordingly by also taking into consideration
220
+ // the 'y' of cells that don't break page
221
+ if (lastSnapshot.bottomMost && lastSnapshot.bottomMost.y) {
222
+ prevY = Math.max(this.y, lastSnapshot.bottomMost.y);
223
+ }
224
+ }
225
+
226
+ let createNewPage = nextPageIndex >= this.pages.length;
227
+ if (createNewPage) {
228
+ let currentAvailableWidth = this.availableWidth;
229
+ let currentPageOrientation = this.getCurrentPage().pageSize.orientation;
230
+
231
+ let pageSize = getPageSize(this.getCurrentPage(), pageOrientation);
232
+ this.addPage(pageSize, null, this.getCurrentPage().customProperties);
233
+
234
+ if (currentPageOrientation === pageSize.orientation) {
235
+ this.availableWidth = currentAvailableWidth;
236
+ }
237
+ } else {
238
+ this.page = nextPageIndex;
239
+ this.initializePage();
240
+ }
241
+
242
+ return {
243
+ newPageCreated: createNewPage,
244
+ prevPage: prevPage,
245
+ prevY: prevY,
246
+ y: this.y
247
+ };
248
+ }
249
+
250
+ addPage(pageSize, pageMargin = null, customProperties = {}) {
251
+ if (pageMargin !== null) {
252
+ this.pageMargins = pageMargin;
253
+ this.x = pageMargin.left;
254
+ this.availableWidth = pageSize.width - pageMargin.left - pageMargin.right;
255
+ }
256
+
257
+ let page = { items: [], pageSize: pageSize, pageMargins: this.pageMargins, customProperties: customProperties };
258
+ this.pages.push(page);
259
+ this.backgroundLength.push(0);
260
+ this.page = this.pages.length - 1;
261
+ this.initializePage();
262
+
263
+ this.emit('pageAdded', page);
264
+
265
+ return page;
266
+ }
267
+
268
+ getCurrentPage() {
269
+ if (this.page < 0 || this.page >= this.pages.length) {
270
+ return null;
271
+ }
272
+
273
+ return this.pages[this.page];
274
+ }
275
+
276
+ getCurrentPosition() {
277
+ let pageSize = this.getCurrentPage().pageSize;
278
+ let innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
279
+ let innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right;
280
+
281
+ return {
282
+ pageNumber: this.page + 1,
283
+ pageOrientation: pageSize.orientation,
284
+ pageInnerHeight: innerHeight,
285
+ pageInnerWidth: innerWidth,
286
+ left: this.x,
287
+ top: this.y,
288
+ verticalRatio: ((this.y - this.pageMargins.top) / innerHeight),
289
+ horizontalRatio: ((this.x - this.pageMargins.left) / innerWidth)
290
+ };
291
+ }
292
+ }
293
+
294
+ function pageOrientation(pageOrientationString, currentPageOrientation) {
295
+ if (pageOrientationString === undefined) {
296
+ return currentPageOrientation;
297
+ } else if (isString(pageOrientationString) && (pageOrientationString.toLowerCase() === 'landscape')) {
298
+ return 'landscape';
299
+ } else {
300
+ return 'portrait';
301
+ }
302
+ }
303
+
304
+ const getPageSize = (currentPage, newPageOrientation) => {
305
+
306
+ newPageOrientation = pageOrientation(newPageOrientation, currentPage.pageSize.orientation);
307
+
308
+ if (newPageOrientation !== currentPage.pageSize.orientation) {
309
+ return {
310
+ orientation: newPageOrientation,
311
+ width: currentPage.pageSize.height,
312
+ height: currentPage.pageSize.width
313
+ };
314
+ } else {
315
+ return {
316
+ orientation: currentPage.pageSize.orientation,
317
+ width: currentPage.pageSize.width,
318
+ height: currentPage.pageSize.height
319
+ };
320
+ }
321
+
322
+ };
323
+
324
+
325
+ function bottomMostContext(c1, c2) {
326
+ let r;
327
+
328
+ if (c1.page > c2.page) {
329
+ r = c1;
330
+ } else if (c2.page > c1.page) {
331
+ r = c2;
332
+ } else {
333
+ r = (c1.y > c2.y) ? c1 : c2;
334
+ }
335
+
336
+ return {
337
+ page: r.page,
338
+ x: r.x,
339
+ y: r.y,
340
+ availableHeight: r.availableHeight,
341
+ availableWidth: r.availableWidth
342
+ };
343
+ }
344
+
345
+ export default DocumentContext;