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
@@ -0,0 +1,264 @@
1
+ import { isString, isNumber, isValue, isEmptyObject } from './helpers/variableType';
2
+ import { stringifyNode } from './helpers/node';
3
+
4
+ const convertValueToString = value => {
5
+ if (isString(value)) {
6
+ return value.replace(/\t/g, ' '); // expand tab as spaces
7
+ } else if (isNumber(value) || typeof value === 'boolean') {
8
+ return value.toString();
9
+ } else if (!isValue(value) || isEmptyObject(value)) {
10
+ return '';
11
+ }
12
+
13
+ // TODO: throw exception ?
14
+
15
+ return value;
16
+ };
17
+
18
+ class DocPreprocessor {
19
+ preprocessDocument(docStructure) {
20
+ this.parentNode = null;
21
+ this.tocs = [];
22
+ this.nodeReferences = [];
23
+ return this.preprocessNode(docStructure);
24
+ }
25
+
26
+ preprocessNode(node) {
27
+ // expand shortcuts and casting values
28
+ if (Array.isArray(node)) {
29
+ node = { stack: node };
30
+ } else if (isString(node) || isNumber(node) || typeof node === 'boolean' || !isValue(node) || isEmptyObject(node)) { // text node defined as value
31
+ node = { text: convertValueToString(node) };
32
+ } else if ('text' in node) { // cast value in text property
33
+ node.text = convertValueToString(node.text);
34
+ }
35
+
36
+ if (node.columns) {
37
+ return this.preprocessColumns(node);
38
+ } else if (node.stack) {
39
+ return this.preprocessVerticalContainer(node);
40
+ } else if (node.ul) {
41
+ return this.preprocessList(node);
42
+ } else if (node.ol) {
43
+ return this.preprocessList(node);
44
+ } else if (node.table) {
45
+ return this.preprocessTable(node);
46
+ } else if (node.text !== undefined) {
47
+ return this.preprocessText(node);
48
+ } else if (node.toc) {
49
+ return this.preprocessToc(node);
50
+ } else if (node.image) {
51
+ return this.preprocessImage(node);
52
+ } else if (node.svg) {
53
+ return this.preprocessSVG(node);
54
+ } else if (node.canvas) {
55
+ return this.preprocessCanvas(node);
56
+ } else if (node.qr) {
57
+ return this.preprocessQr(node);
58
+ } else if (node.attachment) {
59
+ return this.preprocessAttachment(node);
60
+ } else if (node.pageReference || node.textReference) {
61
+ return this.preprocessText(node);
62
+ } else {
63
+ throw new Error(`Unrecognized document structure: ${stringifyNode(node)}`);
64
+ }
65
+ }
66
+
67
+ preprocessColumns(node) {
68
+ let columns = node.columns;
69
+
70
+ for (let i = 0, l = columns.length; i < l; i++) {
71
+ columns[i] = this.preprocessNode(columns[i]);
72
+ }
73
+
74
+ return node;
75
+ }
76
+
77
+ preprocessVerticalContainer(node) {
78
+ let items = node.stack;
79
+
80
+ for (let i = 0, l = items.length; i < l; i++) {
81
+ items[i] = this.preprocessNode(items[i]);
82
+ }
83
+
84
+ return node;
85
+ }
86
+
87
+ preprocessList(node) {
88
+ let items = node.ul || node.ol;
89
+
90
+ for (let i = 0, l = items.length; i < l; i++) {
91
+ items[i] = this.preprocessNode(items[i]);
92
+ }
93
+
94
+ return node;
95
+ }
96
+
97
+ preprocessTable(node) {
98
+ let col;
99
+ let row;
100
+ let cols;
101
+ let rows;
102
+
103
+ for (col = 0, cols = node.table.body[0].length; col < cols; col++) {
104
+ for (row = 0, rows = node.table.body.length; row < rows; row++) {
105
+ let rowData = node.table.body[row];
106
+ let data = rowData[col];
107
+ if (data !== undefined) {
108
+ if (data === null) { // transform to object
109
+ data = '';
110
+ }
111
+ if (!data._span) {
112
+ rowData[col] = this.preprocessNode(data);
113
+ }
114
+ }
115
+ }
116
+ }
117
+
118
+ return node;
119
+ }
120
+
121
+ preprocessText(node) {
122
+ if (node.tocItem) {
123
+ if (!Array.isArray(node.tocItem)) {
124
+ node.tocItem = [node.tocItem];
125
+ }
126
+
127
+ for (let i = 0, l = node.tocItem.length; i < l; i++) {
128
+ if (!isString(node.tocItem[i])) {
129
+ node.tocItem[i] = '_default_';
130
+ }
131
+
132
+ let tocItemId = node.tocItem[i];
133
+
134
+ if (!this.tocs[tocItemId]) {
135
+ this.tocs[tocItemId] = { toc: { _items: [], _pseudo: true } };
136
+ }
137
+
138
+ if (!node.id) {
139
+ node.id = `toc-${tocItemId}-${this.tocs[tocItemId].toc._items.length}`;
140
+ }
141
+
142
+ let tocItemRef = {
143
+ _nodeRef: this._getNodeForNodeRef(node),
144
+ _textNodeRef: node
145
+ };
146
+ this.tocs[tocItemId].toc._items.push(tocItemRef);
147
+ }
148
+ }
149
+
150
+ if (node.id) {
151
+ if (this.nodeReferences[node.id]) {
152
+ if (!this.nodeReferences[node.id]._pseudo) {
153
+ throw new Error(`Node id '${node.id}' already exists`);
154
+ }
155
+
156
+ this.nodeReferences[node.id]._nodeRef = this._getNodeForNodeRef(node);
157
+ this.nodeReferences[node.id]._textNodeRef = node;
158
+ this.nodeReferences[node.id]._pseudo = false;
159
+ } else {
160
+ this.nodeReferences[node.id] = {
161
+ _nodeRef: this._getNodeForNodeRef(node),
162
+ _textNodeRef: node
163
+ };
164
+ }
165
+ }
166
+
167
+ if (node.pageReference) {
168
+ if (!this.nodeReferences[node.pageReference]) {
169
+ this.nodeReferences[node.pageReference] = {
170
+ _nodeRef: {},
171
+ _textNodeRef: {},
172
+ _pseudo: true
173
+ };
174
+ }
175
+ node.text = '00000';
176
+ node.linkToDestination = node.pageReference;
177
+ node._pageRef = this.nodeReferences[node.pageReference];
178
+ }
179
+
180
+ if (node.textReference) {
181
+ if (!this.nodeReferences[node.textReference]) {
182
+ this.nodeReferences[node.textReference] = { _nodeRef: {}, _pseudo: true };
183
+ }
184
+
185
+ node.text = '';
186
+ node.linkToDestination = node.textReference;
187
+ node._textRef = this.nodeReferences[node.textReference];
188
+ }
189
+
190
+ if (node.text && node.text.text) {
191
+ node.text = [this.preprocessNode(node.text)];
192
+ } else if (Array.isArray(node.text)) {
193
+ let isSetParentNode = false;
194
+ if (this.parentNode === null) {
195
+ this.parentNode = node;
196
+ isSetParentNode = true;
197
+ }
198
+
199
+ for (let i = 0, l = node.text.length; i < l; i++) {
200
+ node.text[i] = this.preprocessNode(node.text[i]);
201
+ }
202
+
203
+ if (isSetParentNode) {
204
+ this.parentNode = null;
205
+ }
206
+ }
207
+
208
+ return node;
209
+ }
210
+
211
+ preprocessToc(node) {
212
+ if (!node.toc.id) {
213
+ node.toc.id = '_default_';
214
+ }
215
+
216
+ node.toc.title = node.toc.title ? this.preprocessNode(node.toc.title) : null;
217
+ node.toc._items = [];
218
+
219
+ if (this.tocs[node.toc.id]) {
220
+ if (!this.tocs[node.toc.id].toc._pseudo) {
221
+ throw new Error(`TOC '${node.toc.id}' already exists`);
222
+ }
223
+
224
+ node.toc._items = this.tocs[node.toc.id].toc._items;
225
+ }
226
+
227
+ this.tocs[node.toc.id] = node;
228
+
229
+ return node;
230
+ }
231
+
232
+ preprocessImage(node) {
233
+ if ((node.image.type !== undefined) && (node.image.data !== undefined) && (node.image.type === 'Buffer') && Array.isArray(node.image.data)) {
234
+ node.image = Buffer.from(node.image.data);
235
+ }
236
+ return node;
237
+ }
238
+
239
+ preprocessCanvas(node) {
240
+ return node;
241
+ }
242
+
243
+ preprocessSVG(node) {
244
+ return node;
245
+ }
246
+
247
+ preprocessQr(node) {
248
+ return node;
249
+ }
250
+
251
+ preprocessAttachment(node) {
252
+ return node;
253
+ }
254
+
255
+ _getNodeForNodeRef(node) {
256
+ if (this.parentNode) {
257
+ return this.parentNode;
258
+ }
259
+
260
+ return node;
261
+ }
262
+ }
263
+
264
+ export default DocPreprocessor;
@@ -0,0 +1,324 @@
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(pageSize, pageMargins) {
10
+ super();
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
+ this.backgroundLength = [];
22
+
23
+ this.addPage(pageSize);
24
+ }
25
+
26
+ beginColumnGroup(marginXTopParent) {
27
+ this.snapshots.push({
28
+ x: this.x,
29
+ y: this.y,
30
+ availableHeight: this.availableHeight,
31
+ availableWidth: this.availableWidth,
32
+ page: this.page,
33
+ bottomMost: {
34
+ x: this.x,
35
+ y: this.y,
36
+ availableHeight: this.availableHeight,
37
+ availableWidth: this.availableWidth,
38
+ page: this.page
39
+ },
40
+ lastColumnWidth: this.lastColumnWidth
41
+ });
42
+
43
+ this.lastColumnWidth = 0;
44
+ if (marginXTopParent) {
45
+ this.marginXTopParent = marginXTopParent;
46
+ }
47
+ }
48
+
49
+ resetMarginXTopParent() {
50
+ this.marginXTopParent = null;
51
+ }
52
+
53
+ beginColumn(width, offset, endingCell) {
54
+ let saved = this.snapshots[this.snapshots.length - 1];
55
+
56
+ this.calculateBottomMost(saved, endingCell);
57
+
58
+ this.page = saved.page;
59
+ this.x = this.x + this.lastColumnWidth + (offset || 0);
60
+ this.y = saved.y;
61
+ this.availableWidth = width; //saved.availableWidth - offset;
62
+ this.availableHeight = saved.availableHeight;
63
+
64
+ this.lastColumnWidth = width;
65
+ }
66
+
67
+ calculateBottomMost(destContext, endingCell) {
68
+ if (endingCell) {
69
+ this.saveContextInEndingCell(endingCell);
70
+ } else {
71
+ destContext.bottomMost = bottomMostContext(this, destContext.bottomMost);
72
+ }
73
+ }
74
+
75
+ markEnding(endingCell, originalXOffset, discountY) {
76
+ this.page = endingCell._columnEndingContext.page;
77
+ this.x = endingCell._columnEndingContext.x + originalXOffset;
78
+ this.y = endingCell._columnEndingContext.y - discountY;
79
+ this.availableWidth = endingCell._columnEndingContext.availableWidth;
80
+ this.availableHeight = endingCell._columnEndingContext.availableHeight;
81
+ this.lastColumnWidth = endingCell._columnEndingContext.lastColumnWidth;
82
+ }
83
+
84
+ saveContextInEndingCell(endingCell) {
85
+ endingCell._columnEndingContext = {
86
+ page: this.page,
87
+ x: this.x,
88
+ y: this.y,
89
+ availableHeight: this.availableHeight,
90
+ availableWidth: this.availableWidth,
91
+ lastColumnWidth: this.lastColumnWidth
92
+ };
93
+ }
94
+
95
+ completeColumnGroup(height, endingCell) {
96
+ let saved = this.snapshots.pop();
97
+
98
+ this.calculateBottomMost(saved, endingCell);
99
+
100
+ this.x = saved.x;
101
+
102
+ let y = saved.bottomMost.y;
103
+ if (height) {
104
+ if (saved.page === saved.bottomMost.page) {
105
+ if ((saved.y + height) > y) {
106
+ y = saved.y + height;
107
+ }
108
+ } else {
109
+ y += height;
110
+ }
111
+ }
112
+
113
+ this.y = y;
114
+ this.page = saved.bottomMost.page;
115
+ this.availableWidth = saved.availableWidth;
116
+ this.availableHeight = saved.bottomMost.availableHeight;
117
+ if (height) {
118
+ this.availableHeight -= (y - saved.bottomMost.y);
119
+ }
120
+ this.lastColumnWidth = saved.lastColumnWidth;
121
+ }
122
+
123
+ addMargin(left, right) {
124
+ this.x += left;
125
+ this.availableWidth -= left + (right || 0);
126
+ }
127
+
128
+ moveDown(offset) {
129
+ this.y += offset;
130
+ this.availableHeight -= offset;
131
+
132
+ return this.availableHeight > 0;
133
+ }
134
+
135
+ initializePage() {
136
+ this.y = this.pageMargins.top;
137
+ this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
138
+ const { pageCtx, isSnapshot } = this.pageSnapshot();
139
+ pageCtx.availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right;
140
+ if (isSnapshot && this.marginXTopParent) {
141
+ pageCtx.availableWidth -= this.marginXTopParent[0];
142
+ pageCtx.availableWidth -= this.marginXTopParent[1];
143
+ }
144
+ }
145
+
146
+ pageSnapshot() {
147
+ if (this.snapshots[0]) {
148
+ return { pageCtx: this.snapshots[0], isSnapshot: true };
149
+ } else {
150
+ return { pageCtx: this, isSnapshot: false };
151
+ }
152
+ }
153
+
154
+ moveTo(x, y) {
155
+ if (x !== undefined && x !== null) {
156
+ this.x = x;
157
+ this.availableWidth = this.getCurrentPage().pageSize.width - this.x - this.pageMargins.right;
158
+ }
159
+ if (y !== undefined && y !== null) {
160
+ this.y = y;
161
+ this.availableHeight = this.getCurrentPage().pageSize.height - this.y - this.pageMargins.bottom;
162
+ }
163
+ }
164
+
165
+ moveToRelative(x, y) {
166
+ if (x !== undefined && x !== null) {
167
+ this.x = this.x + x;
168
+ }
169
+ if (y !== undefined && y !== null) {
170
+ this.y = this.y + y;
171
+ }
172
+ }
173
+
174
+ beginDetachedBlock() {
175
+ this.snapshots.push({
176
+ x: this.x,
177
+ y: this.y,
178
+ availableHeight: this.availableHeight,
179
+ availableWidth: this.availableWidth,
180
+ page: this.page,
181
+ lastColumnWidth: this.lastColumnWidth
182
+ });
183
+ }
184
+
185
+ endDetachedBlock() {
186
+ let saved = this.snapshots.pop();
187
+
188
+ this.x = saved.x;
189
+ this.y = saved.y;
190
+ this.availableWidth = saved.availableWidth;
191
+ this.availableHeight = saved.availableHeight;
192
+ this.page = saved.page;
193
+ this.lastColumnWidth = saved.lastColumnWidth;
194
+ }
195
+
196
+ moveToNextPage(pageOrientation) {
197
+ let nextPageIndex = this.page + 1;
198
+ let prevPage = this.page;
199
+ let prevY = this.y;
200
+
201
+ // If we are in a column group
202
+ if (this.snapshots.length > 0) {
203
+ let lastSnapshot = this.snapshots[this.snapshots.length - 1];
204
+ // We have to update prevY accordingly by also taking into consideration
205
+ // the 'y' of cells that don't break page
206
+ if (lastSnapshot.bottomMost && lastSnapshot.bottomMost.y) {
207
+ prevY = Math.max(this.y, lastSnapshot.bottomMost.y);
208
+ }
209
+ }
210
+
211
+ let createNewPage = nextPageIndex >= this.pages.length;
212
+ if (createNewPage) {
213
+ let currentAvailableWidth = this.availableWidth;
214
+ let currentPageOrientation = this.getCurrentPage().pageSize.orientation;
215
+
216
+ let pageSize = getPageSize(this.getCurrentPage(), pageOrientation);
217
+ this.addPage(pageSize);
218
+
219
+ if (currentPageOrientation === pageSize.orientation) {
220
+ this.availableWidth = currentAvailableWidth;
221
+ }
222
+ } else {
223
+ this.page = nextPageIndex;
224
+ this.initializePage();
225
+ }
226
+
227
+ return {
228
+ newPageCreated: createNewPage,
229
+ prevPage: prevPage,
230
+ prevY: prevY,
231
+ y: this.y
232
+ };
233
+ }
234
+
235
+ addPage(pageSize) {
236
+ let page = { items: [], pageSize: pageSize };
237
+ this.pages.push(page);
238
+ this.backgroundLength.push(0);
239
+ this.page = this.pages.length - 1;
240
+ this.initializePage();
241
+
242
+ this.emit('pageAdded');
243
+
244
+ return page;
245
+ }
246
+
247
+ getCurrentPage() {
248
+ if (this.page < 0 || this.page >= this.pages.length) {
249
+ return null;
250
+ }
251
+
252
+ return this.pages[this.page];
253
+ }
254
+
255
+ getCurrentPosition() {
256
+ let pageSize = this.getCurrentPage().pageSize;
257
+ let innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
258
+ let innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right;
259
+
260
+ return {
261
+ pageNumber: this.page + 1,
262
+ pageOrientation: pageSize.orientation,
263
+ pageInnerHeight: innerHeight,
264
+ pageInnerWidth: innerWidth,
265
+ left: this.x,
266
+ top: this.y,
267
+ verticalRatio: ((this.y - this.pageMargins.top) / innerHeight),
268
+ horizontalRatio: ((this.x - this.pageMargins.left) / innerWidth)
269
+ };
270
+ }
271
+ }
272
+
273
+ function pageOrientation(pageOrientationString, currentPageOrientation) {
274
+ if (pageOrientationString === undefined) {
275
+ return currentPageOrientation;
276
+ } else if (isString(pageOrientationString) && (pageOrientationString.toLowerCase() === 'landscape')) {
277
+ return 'landscape';
278
+ } else {
279
+ return 'portrait';
280
+ }
281
+ }
282
+
283
+ const getPageSize = (currentPage, newPageOrientation) => {
284
+
285
+ newPageOrientation = pageOrientation(newPageOrientation, currentPage.pageSize.orientation);
286
+
287
+ if (newPageOrientation !== currentPage.pageSize.orientation) {
288
+ return {
289
+ orientation: newPageOrientation,
290
+ width: currentPage.pageSize.height,
291
+ height: currentPage.pageSize.width
292
+ };
293
+ } else {
294
+ return {
295
+ orientation: currentPage.pageSize.orientation,
296
+ width: currentPage.pageSize.width,
297
+ height: currentPage.pageSize.height
298
+ };
299
+ }
300
+
301
+ };
302
+
303
+
304
+ function bottomMostContext(c1, c2) {
305
+ let r;
306
+
307
+ if (c1.page > c2.page) {
308
+ r = c1;
309
+ } else if (c2.page > c1.page) {
310
+ r = c2;
311
+ } else {
312
+ r = (c1.y > c2.y) ? c1 : c2;
313
+ }
314
+
315
+ return {
316
+ page: r.page,
317
+ x: r.x,
318
+ y: r.y,
319
+ availableHeight: r.availableHeight,
320
+ availableWidth: r.availableWidth
321
+ };
322
+ }
323
+
324
+ export default DocumentContext;