@digicole/pdfmake-rtl 1.2.0 → 2.1.1

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 (100) hide show
  1. package/.vscode/tasks.json +17 -0
  2. package/CHANGELOG.md +83 -128
  3. package/LICENSE +22 -22
  4. package/README.md +188 -681
  5. package/build/fonts/Cairo/Cairo-Black.ttf +0 -0
  6. package/build/fonts/Cairo/Cairo-Bold.ttf +0 -0
  7. package/build/fonts/Cairo/Cairo-ExtraLight.ttf +0 -0
  8. package/build/fonts/Cairo/Cairo-Light.ttf +0 -0
  9. package/build/fonts/Cairo/Cairo-Regular.ttf +0 -0
  10. package/build/fonts/Cairo/Cairo-SemiBold.ttf +0 -0
  11. package/build/fonts/Cairo.js +27 -0
  12. package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
  13. package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
  14. package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  15. package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
  16. package/build/fonts/Roboto.js +27 -0
  17. package/build/pdfmake.js +63736 -71285
  18. package/build/pdfmake.js.map +1 -1
  19. package/build/pdfmake.min.js +2 -2
  20. package/build/pdfmake.min.js.map +1 -1
  21. package/build/standard-fonts/Courier.js +27 -0
  22. package/build/standard-fonts/Helvetica.js +27 -0
  23. package/build/standard-fonts/Symbol.js +21 -0
  24. package/build/standard-fonts/Times.js +27 -0
  25. package/build/standard-fonts/ZapfDingbats.js +21 -0
  26. package/build/vfs_fonts.js +11 -7
  27. package/build-vfs.js +44 -44
  28. package/fonts/Cairo/Cairo-Black.ttf +0 -0
  29. package/fonts/Cairo/Cairo-Bold.ttf +0 -0
  30. package/fonts/Cairo/Cairo-ExtraLight.ttf +0 -0
  31. package/fonts/Cairo/Cairo-Light.ttf +0 -0
  32. package/fonts/Cairo/Cairo-Regular.ttf +0 -0
  33. package/fonts/Cairo/Cairo-SemiBold.ttf +0 -0
  34. package/fonts/Cairo.js +8 -0
  35. package/fonts/Roboto/Roboto-Italic.ttf +0 -0
  36. package/fonts/Roboto/Roboto-Medium.ttf +0 -0
  37. package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  38. package/fonts/Roboto/Roboto-Regular.ttf +0 -0
  39. package/fonts/Roboto.js +8 -0
  40. package/index.js +26 -26
  41. package/package.json +42 -39
  42. package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -9
  43. package/src/3rd-party/svg-to-pdfkit/source.js +229 -36
  44. package/src/3rd-party/svg-to-pdfkit.js +3 -3
  45. package/src/OutputDocument.js +64 -0
  46. package/src/OutputDocumentServer.js +32 -0
  47. package/src/PDFDocument.js +174 -0
  48. package/src/PageSize.js +53 -0
  49. package/src/Renderer.js +445 -0
  50. package/src/TextBreaker.js +168 -0
  51. package/src/TextInlines.js +263 -0
  52. package/src/URLResolver.js +43 -0
  53. package/src/base.js +70 -0
  54. package/src/browser-extensions/OutputDocumentBrowser.js +80 -0
  55. package/src/browser-extensions/fonts/Cairo.js +27 -0
  56. package/src/browser-extensions/fonts/Roboto.js +27 -0
  57. package/src/browser-extensions/index.js +61 -0
  58. package/src/browser-extensions/pdfMake.js +1 -355
  59. package/src/browser-extensions/standard-fonts/Courier.js +27 -0
  60. package/src/browser-extensions/standard-fonts/Helvetica.js +27 -0
  61. package/src/browser-extensions/standard-fonts/Symbol.js +21 -0
  62. package/src/browser-extensions/standard-fonts/Times.js +27 -0
  63. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -0
  64. package/src/browser-extensions/virtual-fs-cjs.js +1 -0
  65. package/src/columnCalculator.js +154 -157
  66. package/src/docMeasure.js +802 -810
  67. package/src/docPreprocessor.js +306 -273
  68. package/src/documentContext.js +345 -340
  69. package/src/elementWriter.js +736 -411
  70. package/src/helpers/node.js +136 -0
  71. package/src/helpers/tools.js +44 -0
  72. package/src/helpers/variableType.js +50 -0
  73. package/src/index.js +16 -0
  74. package/src/layoutBuilder.js +1393 -1197
  75. package/src/line.js +122 -104
  76. package/src/pageElementWriter.js +187 -174
  77. package/src/printer.js +370 -727
  78. package/src/qrEnc.js +796 -791
  79. package/src/rtlUtils.js +500 -485
  80. package/src/standardPageSizes.js +52 -54
  81. package/src/styleContextStack.js +208 -138
  82. package/src/svgMeasure.js +109 -70
  83. package/src/tableLayouts.js +100 -0
  84. package/src/tableProcessor.js +620 -606
  85. package/src/textDecorator.js +175 -157
  86. package/src/virtual-fs.js +66 -0
  87. package/standard-fonts/Courier.js +8 -0
  88. package/standard-fonts/Helvetica.js +8 -0
  89. package/standard-fonts/Symbol.js +5 -0
  90. package/standard-fonts/Times.js +8 -0
  91. package/standard-fonts/ZapfDingbats.js +5 -0
  92. package/index.html +0 -396
  93. package/src/browser-extensions/URLBrowserResolver.js +0 -96
  94. package/src/browser-extensions/virtual-fs.js +0 -55
  95. package/src/fontProvider.js +0 -68
  96. package/src/helpers.js +0 -138
  97. package/src/imageMeasure.js +0 -62
  98. package/src/pdfKitEngine.js +0 -21
  99. package/src/textTools.js +0 -391
  100. package/src/traversalTracker.js +0 -47
@@ -1,340 +1,345 @@
1
- 'use strict';
2
-
3
- var TraversalTracker = require('./traversalTracker');
4
- var isString = require('./helpers').isString;
5
-
6
- /**
7
- * Creates an instance of DocumentContext - a store for current x, y positions and available width/height.
8
- * It facilitates column divisions and vertical sync
9
- */
10
- function DocumentContext(pageSize, pageMargins) {
11
- this.pages = [];
12
-
13
- this.pageMargins = pageMargins;
14
-
15
- this.x = pageMargins.left;
16
- this.availableWidth = pageSize.width - pageMargins.left - pageMargins.right;
17
- this.availableHeight = 0;
18
- this.page = -1;
19
-
20
- this.snapshots = [];
21
-
22
- this.tracker = new TraversalTracker();
23
-
24
- this.backgroundLength = [];
25
-
26
- this.addPage(pageSize);
27
- }
28
-
29
- DocumentContext.prototype.beginColumnGroup = function (marginXTopParent, bottomByPage = {}) {
30
- this.snapshots.push({
31
- x: this.x,
32
- y: this.y,
33
- availableHeight: this.availableHeight,
34
- availableWidth: this.availableWidth,
35
- page: this.page,
36
- bottomByPage: bottomByPage ? bottomByPage : {},
37
- bottomMost: {
38
- x: this.x,
39
- y: this.y,
40
- availableHeight: this.availableHeight,
41
- availableWidth: this.availableWidth,
42
- page: this.page
43
- },
44
- lastColumnWidth: this.lastColumnWidth
45
- });
46
-
47
- this.lastColumnWidth = 0;
48
- if (marginXTopParent) {
49
- this.marginXTopParent = marginXTopParent;
50
- }
51
- };
52
-
53
- DocumentContext.prototype.updateBottomByPage = function () {
54
- const lastSnapshot = this.snapshots[this.snapshots.length - 1];
55
- const lastPage = this.page;
56
- let previousBottom = -Number.MIN_VALUE;
57
- if (lastSnapshot.bottomByPage[lastPage]) {
58
- previousBottom = lastSnapshot.bottomByPage[lastPage];
59
- }
60
- lastSnapshot.bottomByPage[lastPage] = Math.max(previousBottom, this.y);
61
- };
62
-
63
- DocumentContext.prototype.resetMarginXTopParent = function () {
64
- this.marginXTopParent = null;
65
- };
66
-
67
- DocumentContext.prototype.beginColumn = function (width, offset, endingCell) {
68
- var saved = this.snapshots[this.snapshots.length - 1];
69
-
70
- this.calculateBottomMost(saved, endingCell);
71
-
72
- this.page = saved.page;
73
- this.x = this.x + this.lastColumnWidth + (offset || 0);
74
- this.y = saved.y;
75
- this.availableWidth = width; //saved.availableWidth - offset;
76
- this.availableHeight = saved.availableHeight;
77
-
78
- this.lastColumnWidth = width;
79
- };
80
-
81
- DocumentContext.prototype.calculateBottomMost = function (destContext, endingCell) {
82
- if (endingCell) {
83
- this.saveContextInEndingCell(endingCell);
84
- } else {
85
- destContext.bottomMost = bottomMostContext(this, destContext.bottomMost);
86
- }
87
- };
88
-
89
- DocumentContext.prototype.markEnding = function (endingCell, originalXOffset, discountY) {
90
- this.page = endingCell._columnEndingContext.page;
91
- this.x = endingCell._columnEndingContext.x + originalXOffset;
92
- this.y = endingCell._columnEndingContext.y - discountY;
93
- this.availableWidth = endingCell._columnEndingContext.availableWidth;
94
- this.availableHeight = endingCell._columnEndingContext.availableHeight;
95
- this.lastColumnWidth = endingCell._columnEndingContext.lastColumnWidth;
96
- };
97
-
98
- DocumentContext.prototype.saveContextInEndingCell = function (endingCell) {
99
- endingCell._columnEndingContext = {
100
- page: this.page,
101
- x: this.x,
102
- y: this.y,
103
- availableHeight: this.availableHeight,
104
- availableWidth: this.availableWidth,
105
- lastColumnWidth: this.lastColumnWidth
106
- };
107
- };
108
-
109
- DocumentContext.prototype.completeColumnGroup = function (height, endingCell) {
110
- var saved = this.snapshots.pop();
111
-
112
- this.calculateBottomMost(saved, endingCell);
113
-
114
- this.x = saved.x;
115
-
116
- var y = saved.bottomMost.y;
117
- if (height) {
118
- if (saved.page === saved.bottomMost.page) {
119
- if ((saved.y + height) > y) {
120
- y = saved.y + height;
121
- }
122
- } else {
123
- y += height;
124
- }
125
- }
126
-
127
- this.y = y;
128
- this.page = saved.bottomMost.page;
129
- this.availableWidth = saved.availableWidth;
130
- this.availableHeight = saved.bottomMost.availableHeight;
131
- if (height) {
132
- this.availableHeight -= (y - saved.bottomMost.y);
133
- }
134
- this.lastColumnWidth = saved.lastColumnWidth;
135
- return saved.bottomByPage;
136
- };
137
-
138
- DocumentContext.prototype.addMargin = function (left, right) {
139
- this.x += left;
140
- this.availableWidth -= left + (right || 0);
141
- };
142
-
143
- DocumentContext.prototype.moveDown = function (offset) {
144
- this.y += offset;
145
- this.availableHeight -= offset;
146
-
147
- return this.availableHeight > 0;
148
- };
149
-
150
- DocumentContext.prototype.initializePage = function () {
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
- DocumentContext.prototype.pageSnapshot = function () {
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
- DocumentContext.prototype.moveTo = function (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
- DocumentContext.prototype.moveToRelative = function (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
- DocumentContext.prototype.beginDetachedBlock = function () {
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
- DocumentContext.prototype.endDetachedBlock = function () {
201
- var 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
- function pageOrientation(pageOrientationString, currentPageOrientation) {
212
- if (pageOrientationString === undefined) {
213
- return currentPageOrientation;
214
- } else if (isString(pageOrientationString) && (pageOrientationString.toLowerCase() === 'landscape')) {
215
- return 'landscape';
216
- } else {
217
- return 'portrait';
218
- }
219
- }
220
-
221
- var getPageSize = function (currentPage, newPageOrientation) {
222
-
223
- newPageOrientation = pageOrientation(newPageOrientation, currentPage.pageSize.orientation);
224
-
225
- if (newPageOrientation !== currentPage.pageSize.orientation) {
226
- return {
227
- orientation: newPageOrientation,
228
- width: currentPage.pageSize.height,
229
- height: currentPage.pageSize.width
230
- };
231
- } else {
232
- return {
233
- orientation: currentPage.pageSize.orientation,
234
- width: currentPage.pageSize.width,
235
- height: currentPage.pageSize.height
236
- };
237
- }
238
-
239
- };
240
-
241
-
242
- DocumentContext.prototype.moveToNextPage = function (pageOrientation) {
243
- var nextPageIndex = this.page + 1;
244
-
245
- var prevPage = this.page;
246
- var prevY = this.y;
247
-
248
- // If we are in a column group
249
- if (this.snapshots.length > 0) {
250
- var lastSnapshot = this.snapshots[this.snapshots.length - 1];
251
- // We have to update prevY accordingly by also taking into consideration
252
- // the 'y' of cells that don't break page
253
- if (lastSnapshot.bottomMost && lastSnapshot.bottomMost.y) {
254
- prevY = Math.max(this.y, lastSnapshot.bottomMost.y);
255
- }
256
- }
257
-
258
- var createNewPage = nextPageIndex >= this.pages.length;
259
- if (createNewPage) {
260
- var currentAvailableWidth = this.availableWidth;
261
- var currentPageOrientation = this.getCurrentPage().pageSize.orientation;
262
-
263
- var pageSize = getPageSize(this.getCurrentPage(), pageOrientation);
264
- this.addPage(pageSize);
265
-
266
- if (currentPageOrientation === pageSize.orientation) {
267
- this.availableWidth = currentAvailableWidth;
268
- }
269
- } else {
270
- this.page = nextPageIndex;
271
- this.initializePage();
272
- }
273
-
274
- return {
275
- newPageCreated: createNewPage,
276
- prevPage: prevPage,
277
- prevY: prevY,
278
- y: this.y
279
- };
280
- };
281
-
282
-
283
- DocumentContext.prototype.addPage = function (pageSize) {
284
- var page = { items: [], pageSize: pageSize };
285
- this.pages.push(page);
286
- this.backgroundLength.push(0);
287
- this.page = this.pages.length - 1;
288
- this.initializePage();
289
-
290
- this.tracker.emit('pageAdded');
291
-
292
- return page;
293
- };
294
-
295
- DocumentContext.prototype.getCurrentPage = function () {
296
- if (this.page < 0 || this.page >= this.pages.length) {
297
- return null;
298
- }
299
-
300
- return this.pages[this.page];
301
- };
302
-
303
- DocumentContext.prototype.getCurrentPosition = function () {
304
- var pageSize = this.getCurrentPage().pageSize;
305
- var innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
306
- var innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right;
307
-
308
- return {
309
- pageNumber: this.page + 1,
310
- pageOrientation: pageSize.orientation,
311
- pageInnerHeight: innerHeight,
312
- pageInnerWidth: innerWidth,
313
- left: this.x,
314
- top: this.y,
315
- verticalRatio: ((this.y - this.pageMargins.top) / innerHeight),
316
- horizontalRatio: ((this.x - this.pageMargins.left) / innerWidth)
317
- };
318
- };
319
-
320
- function bottomMostContext(c1, c2) {
321
- var r;
322
-
323
- if (c1.page > c2.page) {
324
- r = c1;
325
- } else if (c2.page > c1.page) {
326
- r = c2;
327
- } else {
328
- r = (c1.y > c2.y) ? c1 : c2;
329
- }
330
-
331
- return {
332
- page: r.page,
333
- x: r.x,
334
- y: r.y,
335
- availableHeight: r.availableHeight,
336
- availableWidth: r.availableWidth
337
- };
338
- }
339
-
340
- module.exports = 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;