pdfmake-acroforms 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 (115) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/LICENSE +21 -0
  3. package/README.md +75 -0
  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 +67806 -0
  10. package/build/pdfmake.js.map +1 -0
  11. package/build/pdfmake.min.js +3 -0
  12. package/build/pdfmake.min.js.map +1 -0
  13. package/build/standard-fonts/Courier.js +27 -0
  14. package/build/standard-fonts/Helvetica.js +27 -0
  15. package/build/standard-fonts/Symbol.js +21 -0
  16. package/build/standard-fonts/Times.js +27 -0
  17. package/build/standard-fonts/ZapfDingbats.js +21 -0
  18. package/build/vfs_fonts.js +6 -0
  19. package/build-vfs.js +44 -0
  20. package/fonts/Roboto/Roboto-Italic.ttf +0 -0
  21. package/fonts/Roboto/Roboto-Medium.ttf +0 -0
  22. package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  23. package/fonts/Roboto/Roboto-Regular.ttf +0 -0
  24. package/fonts/Roboto.js +8 -0
  25. package/js/3rd-party/svg-to-pdfkit/source.js +3823 -0
  26. package/js/3rd-party/svg-to-pdfkit.js +7 -0
  27. package/js/DocMeasure.js +675 -0
  28. package/js/DocPreprocessor.js +258 -0
  29. package/js/DocumentContext.js +310 -0
  30. package/js/ElementWriter.js +399 -0
  31. package/js/LayoutBuilder.js +1202 -0
  32. package/js/Line.js +101 -0
  33. package/js/OutputDocument.js +64 -0
  34. package/js/OutputDocumentServer.js +29 -0
  35. package/js/PDFDocument.js +145 -0
  36. package/js/PageElementWriter.js +164 -0
  37. package/js/PageSize.js +74 -0
  38. package/js/Printer.js +288 -0
  39. package/js/Renderer.js +513 -0
  40. package/js/SVGMeasure.js +92 -0
  41. package/js/StyleContextStack.js +191 -0
  42. package/js/TableProcessor.js +562 -0
  43. package/js/TextBreaker.js +179 -0
  44. package/js/TextDecorator.js +152 -0
  45. package/js/TextInlines.js +212 -0
  46. package/js/URLResolver.js +43 -0
  47. package/js/base.js +59 -0
  48. package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
  49. package/js/browser-extensions/fonts/Roboto.js +38 -0
  50. package/js/browser-extensions/index.js +53 -0
  51. package/js/browser-extensions/pdfMake.js +3 -0
  52. package/js/browser-extensions/standard-fonts/Courier.js +38 -0
  53. package/js/browser-extensions/standard-fonts/Helvetica.js +38 -0
  54. package/js/browser-extensions/standard-fonts/Symbol.js +23 -0
  55. package/js/browser-extensions/standard-fonts/Times.js +38 -0
  56. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +23 -0
  57. package/js/browser-extensions/virtual-fs-cjs.js +3 -0
  58. package/js/columnCalculator.js +148 -0
  59. package/js/helpers/node.js +122 -0
  60. package/js/helpers/tools.js +46 -0
  61. package/js/helpers/variableType.js +59 -0
  62. package/js/index.js +15 -0
  63. package/js/qrEnc.js +721 -0
  64. package/js/standardPageSizes.js +56 -0
  65. package/js/tableLayouts.js +98 -0
  66. package/js/virtual-fs.js +60 -0
  67. package/package.json +94 -0
  68. package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -0
  69. package/src/3rd-party/svg-to-pdfkit/source.js +2745 -0
  70. package/src/3rd-party/svg-to-pdfkit.js +3 -0
  71. package/src/DocMeasure.js +768 -0
  72. package/src/DocPreprocessor.js +289 -0
  73. package/src/DocumentContext.js +345 -0
  74. package/src/ElementWriter.js +468 -0
  75. package/src/LayoutBuilder.js +1366 -0
  76. package/src/Line.js +108 -0
  77. package/src/OutputDocument.js +64 -0
  78. package/src/OutputDocumentServer.js +32 -0
  79. package/src/PDFDocument.js +178 -0
  80. package/src/PageElementWriter.js +191 -0
  81. package/src/PageSize.js +53 -0
  82. package/src/Printer.js +306 -0
  83. package/src/Renderer.js +546 -0
  84. package/src/SVGMeasure.js +109 -0
  85. package/src/StyleContextStack.js +208 -0
  86. package/src/TableProcessor.js +620 -0
  87. package/src/TextBreaker.js +181 -0
  88. package/src/TextDecorator.js +175 -0
  89. package/src/TextInlines.js +229 -0
  90. package/src/URLResolver.js +43 -0
  91. package/src/base.js +70 -0
  92. package/src/browser-extensions/OutputDocumentBrowser.js +80 -0
  93. package/src/browser-extensions/fonts/Roboto.js +27 -0
  94. package/src/browser-extensions/index.js +55 -0
  95. package/src/browser-extensions/pdfMake.js +1 -0
  96. package/src/browser-extensions/standard-fonts/Courier.js +27 -0
  97. package/src/browser-extensions/standard-fonts/Helvetica.js +27 -0
  98. package/src/browser-extensions/standard-fonts/Symbol.js +21 -0
  99. package/src/browser-extensions/standard-fonts/Times.js +27 -0
  100. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -0
  101. package/src/browser-extensions/virtual-fs-cjs.js +1 -0
  102. package/src/columnCalculator.js +154 -0
  103. package/src/helpers/node.js +134 -0
  104. package/src/helpers/tools.js +44 -0
  105. package/src/helpers/variableType.js +50 -0
  106. package/src/index.js +16 -0
  107. package/src/qrEnc.js +796 -0
  108. package/src/standardPageSizes.js +52 -0
  109. package/src/tableLayouts.js +100 -0
  110. package/src/virtual-fs.js +66 -0
  111. package/standard-fonts/Courier.js +8 -0
  112. package/standard-fonts/Helvetica.js +8 -0
  113. package/standard-fonts/Symbol.js +5 -0
  114. package/standard-fonts/Times.js +8 -0
  115. package/standard-fonts/ZapfDingbats.js +5 -0
@@ -0,0 +1,258 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _variableType = require("./helpers/variableType");
6
+ var _node = require("./helpers/node");
7
+ const convertValueToString = value => {
8
+ if ((0, _variableType.isString)(value)) {
9
+ return value.replace(/\t/g, ' '); // expand tab as spaces
10
+ } else if ((0, _variableType.isNumber)(value) || typeof value === 'boolean') {
11
+ return value.toString();
12
+ } else if (!(0, _variableType.isValue)(value) || (0, _variableType.isEmptyObject)(value)) {
13
+ return '';
14
+ }
15
+
16
+ // TODO: throw exception ?
17
+
18
+ return value;
19
+ };
20
+ class DocPreprocessor {
21
+ preprocessDocument(docStructure) {
22
+ this.parentNode = null;
23
+ this.tocs = [];
24
+ this.nodeReferences = [];
25
+ return this.preprocessNode(docStructure, true);
26
+ }
27
+ preprocessBlock(node) {
28
+ this.parentNode = null;
29
+ this.tocs = [];
30
+ this.nodeReferences = [];
31
+ return this.preprocessNode(node);
32
+ }
33
+ preprocessNode(node, isSectionAllowed = false) {
34
+ // expand shortcuts and casting values
35
+ if (Array.isArray(node)) {
36
+ node = {
37
+ stack: node
38
+ };
39
+ } else if ((0, _variableType.isString)(node) || (0, _variableType.isNumber)(node) || typeof node === 'boolean' || !(0, _variableType.isValue)(node) || (0, _variableType.isEmptyObject)(node)) {
40
+ // text node defined as value
41
+ node = {
42
+ text: convertValueToString(node)
43
+ };
44
+ } else if ('text' in node) {
45
+ // cast value in text property
46
+ node.text = convertValueToString(node.text);
47
+ }
48
+ if (node.section) {
49
+ if (!isSectionAllowed) {
50
+ throw new Error(`Incorrect document structure, section node is only allowed at the root level of document structure: ${(0, _node.stringifyNode)(node)}`);
51
+ }
52
+ return this.preprocessSection(node);
53
+ } else if (node.columns) {
54
+ return this.preprocessColumns(node);
55
+ } else if (node.stack) {
56
+ return this.preprocessVerticalContainer(node, isSectionAllowed);
57
+ } else if (node.ul) {
58
+ return this.preprocessList(node);
59
+ } else if (node.ol) {
60
+ return this.preprocessList(node);
61
+ } else if (node.table) {
62
+ return this.preprocessTable(node);
63
+ } else if (node.text !== undefined) {
64
+ return this.preprocessText(node);
65
+ } else if (node.toc) {
66
+ return this.preprocessToc(node);
67
+ } else if (node.image) {
68
+ return this.preprocessImage(node);
69
+ } else if (node.svg) {
70
+ return this.preprocessSVG(node);
71
+ } else if (node.canvas) {
72
+ return this.preprocessCanvas(node);
73
+ } else if (node.qr) {
74
+ return this.preprocessQr(node);
75
+ } else if (node.attachment) {
76
+ return this.preprocessAttachment(node);
77
+ } else if (node.pageReference || node.textReference) {
78
+ return this.preprocessText(node);
79
+ } else if (node.acroform) {
80
+ return this.preprocessAcroForm(node);
81
+ } else {
82
+ throw new Error(`Unrecognized document structure: ${(0, _node.stringifyNode)(node)}`);
83
+ }
84
+ }
85
+ preprocessAcroForm(node) {
86
+ return node;
87
+ }
88
+ preprocessSection(node) {
89
+ node.section = this.preprocessNode(node.section);
90
+ return node;
91
+ }
92
+ preprocessColumns(node) {
93
+ let columns = node.columns;
94
+ for (let i = 0, l = columns.length; i < l; i++) {
95
+ columns[i] = this.preprocessNode(columns[i]);
96
+ }
97
+ return node;
98
+ }
99
+ preprocessVerticalContainer(node, isSectionAllowed) {
100
+ let items = node.stack;
101
+ for (let i = 0, l = items.length; i < l; i++) {
102
+ items[i] = this.preprocessNode(items[i], isSectionAllowed);
103
+ }
104
+ return node;
105
+ }
106
+ preprocessList(node) {
107
+ let items = node.ul || node.ol;
108
+ for (let i = 0, l = items.length; i < l; i++) {
109
+ items[i] = this.preprocessNode(items[i]);
110
+ }
111
+ return node;
112
+ }
113
+ preprocessTable(node) {
114
+ let col;
115
+ let row;
116
+ let cols;
117
+ let rows;
118
+ for (col = 0, cols = node.table.body[0].length; col < cols; col++) {
119
+ for (row = 0, rows = node.table.body.length; row < rows; row++) {
120
+ let rowData = node.table.body[row];
121
+ let data = rowData[col];
122
+ if (data !== undefined) {
123
+ if (data === null) {
124
+ // transform to object
125
+ data = '';
126
+ }
127
+ if (!data._span) {
128
+ rowData[col] = this.preprocessNode(data);
129
+ }
130
+ }
131
+ }
132
+ }
133
+ return node;
134
+ }
135
+ preprocessText(node) {
136
+ if (node.tocItem) {
137
+ if (!Array.isArray(node.tocItem)) {
138
+ node.tocItem = [node.tocItem];
139
+ }
140
+ for (let i = 0, l = node.tocItem.length; i < l; i++) {
141
+ if (!(0, _variableType.isString)(node.tocItem[i])) {
142
+ node.tocItem[i] = '_default_';
143
+ }
144
+ let tocItemId = node.tocItem[i];
145
+ if (!this.tocs[tocItemId]) {
146
+ this.tocs[tocItemId] = {
147
+ toc: {
148
+ _items: [],
149
+ _pseudo: true
150
+ }
151
+ };
152
+ }
153
+ if (!node.id) {
154
+ node.id = `toc-${tocItemId}-${this.tocs[tocItemId].toc._items.length}`;
155
+ }
156
+ let tocItemRef = {
157
+ _nodeRef: this._getNodeForNodeRef(node),
158
+ _textNodeRef: node
159
+ };
160
+ this.tocs[tocItemId].toc._items.push(tocItemRef);
161
+ }
162
+ }
163
+ if (node.id) {
164
+ if (this.nodeReferences[node.id]) {
165
+ if (!this.nodeReferences[node.id]._pseudo) {
166
+ throw new Error(`Node id '${node.id}' already exists`);
167
+ }
168
+ this.nodeReferences[node.id]._nodeRef = this._getNodeForNodeRef(node);
169
+ this.nodeReferences[node.id]._textNodeRef = node;
170
+ this.nodeReferences[node.id]._pseudo = false;
171
+ } else {
172
+ this.nodeReferences[node.id] = {
173
+ _nodeRef: this._getNodeForNodeRef(node),
174
+ _textNodeRef: node
175
+ };
176
+ }
177
+ }
178
+ if (node.pageReference) {
179
+ if (!this.nodeReferences[node.pageReference]) {
180
+ this.nodeReferences[node.pageReference] = {
181
+ _nodeRef: {},
182
+ _textNodeRef: {},
183
+ _pseudo: true
184
+ };
185
+ }
186
+ node.text = '00000';
187
+ node.linkToDestination = node.pageReference;
188
+ node._pageRef = this.nodeReferences[node.pageReference];
189
+ }
190
+ if (node.textReference) {
191
+ if (!this.nodeReferences[node.textReference]) {
192
+ this.nodeReferences[node.textReference] = {
193
+ _nodeRef: {},
194
+ _pseudo: true
195
+ };
196
+ }
197
+ node.text = '';
198
+ node.linkToDestination = node.textReference;
199
+ node._textRef = this.nodeReferences[node.textReference];
200
+ }
201
+ if (node.text && node.text.text) {
202
+ node.text = [this.preprocessNode(node.text)];
203
+ } else if (Array.isArray(node.text)) {
204
+ let isSetParentNode = false;
205
+ if (this.parentNode === null) {
206
+ this.parentNode = node;
207
+ isSetParentNode = true;
208
+ }
209
+ for (let i = 0, l = node.text.length; i < l; i++) {
210
+ node.text[i] = this.preprocessNode(node.text[i]);
211
+ }
212
+ if (isSetParentNode) {
213
+ this.parentNode = null;
214
+ }
215
+ }
216
+ return node;
217
+ }
218
+ preprocessToc(node) {
219
+ if (!node.toc.id) {
220
+ node.toc.id = '_default_';
221
+ }
222
+ node.toc.title = node.toc.title ? this.preprocessNode(node.toc.title) : null;
223
+ node.toc._items = [];
224
+ if (this.tocs[node.toc.id]) {
225
+ if (!this.tocs[node.toc.id].toc._pseudo) {
226
+ throw new Error(`TOC '${node.toc.id}' already exists`);
227
+ }
228
+ node.toc._items = this.tocs[node.toc.id].toc._items;
229
+ }
230
+ this.tocs[node.toc.id] = node;
231
+ return node;
232
+ }
233
+ preprocessImage(node) {
234
+ if (node.image.type !== undefined && node.image.data !== undefined && node.image.type === 'Buffer' && Array.isArray(node.image.data)) {
235
+ node.image = Buffer.from(node.image.data);
236
+ }
237
+ return node;
238
+ }
239
+ preprocessCanvas(node) {
240
+ return node;
241
+ }
242
+ preprocessSVG(node) {
243
+ return node;
244
+ }
245
+ preprocessQr(node) {
246
+ return node;
247
+ }
248
+ preprocessAttachment(node) {
249
+ return node;
250
+ }
251
+ _getNodeForNodeRef(node) {
252
+ if (this.parentNode) {
253
+ return this.parentNode;
254
+ }
255
+ return node;
256
+ }
257
+ }
258
+ var _default = exports.default = DocPreprocessor;
@@ -0,0 +1,310 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _variableType = require("./helpers/variableType");
6
+ var _events = require("events");
7
+ /**
8
+ * A store for current x, y positions and available width/height.
9
+ * It facilitates column divisions and vertical sync
10
+ */
11
+ class DocumentContext extends _events.EventEmitter {
12
+ constructor() {
13
+ super();
14
+ this.pages = [];
15
+ this.pageMargins = undefined;
16
+ this.x = undefined;
17
+ this.availableWidth = undefined;
18
+ this.availableHeight = undefined;
19
+ this.page = -1;
20
+ this.snapshots = [];
21
+ this.backgroundLength = [];
22
+ }
23
+ beginColumnGroup(marginXTopParent, bottomByPage = {}) {
24
+ this.snapshots.push({
25
+ x: this.x,
26
+ y: this.y,
27
+ availableHeight: this.availableHeight,
28
+ availableWidth: this.availableWidth,
29
+ page: this.page,
30
+ bottomByPage: bottomByPage ? bottomByPage : {},
31
+ bottomMost: {
32
+ x: this.x,
33
+ y: this.y,
34
+ availableHeight: this.availableHeight,
35
+ availableWidth: this.availableWidth,
36
+ page: this.page
37
+ },
38
+ lastColumnWidth: this.lastColumnWidth
39
+ });
40
+ this.lastColumnWidth = 0;
41
+ if (marginXTopParent) {
42
+ this.marginXTopParent = marginXTopParent;
43
+ }
44
+ }
45
+ updateBottomByPage() {
46
+ const lastSnapshot = this.snapshots[this.snapshots.length - 1];
47
+ const lastPage = this.page;
48
+ let previousBottom = -Number.MIN_VALUE;
49
+ if (lastSnapshot.bottomByPage[lastPage]) {
50
+ previousBottom = lastSnapshot.bottomByPage[lastPage];
51
+ }
52
+ lastSnapshot.bottomByPage[lastPage] = Math.max(previousBottom, this.y);
53
+ }
54
+ resetMarginXTopParent() {
55
+ this.marginXTopParent = null;
56
+ }
57
+ beginColumn(width, offset, endingCell) {
58
+ let saved = this.snapshots[this.snapshots.length - 1];
59
+ this.calculateBottomMost(saved, endingCell);
60
+ this.page = saved.page;
61
+ this.x = this.x + this.lastColumnWidth + (offset || 0);
62
+ this.y = saved.y;
63
+ this.availableWidth = width; //saved.availableWidth - offset;
64
+ this.availableHeight = saved.availableHeight;
65
+ this.lastColumnWidth = width;
66
+ }
67
+ calculateBottomMost(destContext, endingCell) {
68
+ if (endingCell) {
69
+ this.saveContextInEndingCell(endingCell);
70
+ } else {
71
+ destContext.bottomMost = bottomMostContext(this, destContext.bottomMost);
72
+ }
73
+ }
74
+ markEnding(endingCell, originalXOffset, discountY) {
75
+ this.page = endingCell._columnEndingContext.page;
76
+ this.x = endingCell._columnEndingContext.x + originalXOffset;
77
+ this.y = endingCell._columnEndingContext.y - discountY;
78
+ this.availableWidth = endingCell._columnEndingContext.availableWidth;
79
+ this.availableHeight = endingCell._columnEndingContext.availableHeight;
80
+ this.lastColumnWidth = endingCell._columnEndingContext.lastColumnWidth;
81
+ }
82
+ saveContextInEndingCell(endingCell) {
83
+ endingCell._columnEndingContext = {
84
+ page: this.page,
85
+ x: this.x,
86
+ y: this.y,
87
+ availableHeight: this.availableHeight,
88
+ availableWidth: this.availableWidth,
89
+ lastColumnWidth: this.lastColumnWidth
90
+ };
91
+ }
92
+ completeColumnGroup(height, endingCell) {
93
+ let saved = this.snapshots.pop();
94
+ this.calculateBottomMost(saved, endingCell);
95
+ this.x = saved.x;
96
+ let y = saved.bottomMost.y;
97
+ if (height) {
98
+ if (saved.page === saved.bottomMost.page) {
99
+ if (saved.y + height > y) {
100
+ y = saved.y + height;
101
+ }
102
+ } else {
103
+ y += height;
104
+ }
105
+ }
106
+ this.y = y;
107
+ this.page = saved.bottomMost.page;
108
+ this.availableWidth = saved.availableWidth;
109
+ this.availableHeight = saved.bottomMost.availableHeight;
110
+ if (height) {
111
+ this.availableHeight -= y - saved.bottomMost.y;
112
+ }
113
+ if (height && saved.bottomMost.y - saved.y < height) {
114
+ this.height = height;
115
+ } else {
116
+ this.height = saved.bottomMost.y - saved.y;
117
+ }
118
+ this.lastColumnWidth = saved.lastColumnWidth;
119
+ return saved.bottomByPage;
120
+ }
121
+ addMargin(left, right) {
122
+ this.x += left;
123
+ this.availableWidth -= left + (right || 0);
124
+ }
125
+ moveDown(offset) {
126
+ this.y += offset;
127
+ this.availableHeight -= offset;
128
+ return this.availableHeight > 0;
129
+ }
130
+ initializePage() {
131
+ this.y = this.pageMargins.top;
132
+ this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
133
+ const {
134
+ pageCtx,
135
+ isSnapshot
136
+ } = this.pageSnapshot();
137
+ pageCtx.availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right;
138
+ if (isSnapshot && this.marginXTopParent) {
139
+ pageCtx.availableWidth -= this.marginXTopParent[0];
140
+ pageCtx.availableWidth -= this.marginXTopParent[1];
141
+ }
142
+ }
143
+ pageSnapshot() {
144
+ if (this.snapshots[0]) {
145
+ return {
146
+ pageCtx: this.snapshots[0],
147
+ isSnapshot: true
148
+ };
149
+ } else {
150
+ return {
151
+ pageCtx: this,
152
+ isSnapshot: false
153
+ };
154
+ }
155
+ }
156
+ moveTo(x, y) {
157
+ if (x !== undefined && x !== null) {
158
+ this.x = x;
159
+ this.availableWidth = this.getCurrentPage().pageSize.width - this.x - this.pageMargins.right;
160
+ }
161
+ if (y !== undefined && y !== null) {
162
+ this.y = y;
163
+ this.availableHeight = this.getCurrentPage().pageSize.height - this.y - this.pageMargins.bottom;
164
+ }
165
+ }
166
+ moveToRelative(x, y) {
167
+ if (x !== undefined && x !== null) {
168
+ this.x = this.x + x;
169
+ }
170
+ if (y !== undefined && y !== null) {
171
+ this.y = this.y + y;
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
+ endDetachedBlock() {
185
+ let saved = this.snapshots.pop();
186
+ this.x = saved.x;
187
+ this.y = saved.y;
188
+ this.availableWidth = saved.availableWidth;
189
+ this.availableHeight = saved.availableHeight;
190
+ this.page = saved.page;
191
+ this.lastColumnWidth = saved.lastColumnWidth;
192
+ }
193
+ moveToNextPage(pageOrientation) {
194
+ let nextPageIndex = this.page + 1;
195
+ let prevPage = this.page;
196
+ let prevY = this.y;
197
+
198
+ // If we are in a column group
199
+ if (this.snapshots.length > 0) {
200
+ let lastSnapshot = this.snapshots[this.snapshots.length - 1];
201
+ // We have to update prevY accordingly by also taking into consideration
202
+ // the 'y' of cells that don't break page
203
+ if (lastSnapshot.bottomMost && lastSnapshot.bottomMost.y) {
204
+ prevY = Math.max(this.y, lastSnapshot.bottomMost.y);
205
+ }
206
+ }
207
+ let createNewPage = nextPageIndex >= this.pages.length;
208
+ if (createNewPage) {
209
+ let currentAvailableWidth = this.availableWidth;
210
+ let currentPageOrientation = this.getCurrentPage().pageSize.orientation;
211
+ let pageSize = getPageSize(this.getCurrentPage(), pageOrientation);
212
+ this.addPage(pageSize, null, this.getCurrentPage().customProperties);
213
+ if (currentPageOrientation === pageSize.orientation) {
214
+ this.availableWidth = currentAvailableWidth;
215
+ }
216
+ } else {
217
+ this.page = nextPageIndex;
218
+ this.initializePage();
219
+ }
220
+ return {
221
+ newPageCreated: createNewPage,
222
+ prevPage: prevPage,
223
+ prevY: prevY,
224
+ y: this.y
225
+ };
226
+ }
227
+ addPage(pageSize, pageMargin = null, customProperties = {}) {
228
+ if (pageMargin !== null) {
229
+ this.pageMargins = pageMargin;
230
+ this.x = pageMargin.left;
231
+ this.availableWidth = pageSize.width - pageMargin.left - pageMargin.right;
232
+ }
233
+ let page = {
234
+ items: [],
235
+ pageSize: pageSize,
236
+ pageMargins: this.pageMargins,
237
+ customProperties: customProperties
238
+ };
239
+ this.pages.push(page);
240
+ this.backgroundLength.push(0);
241
+ this.page = this.pages.length - 1;
242
+ this.initializePage();
243
+ this.emit('pageAdded', page);
244
+ return page;
245
+ }
246
+ getCurrentPage() {
247
+ if (this.page < 0 || this.page >= this.pages.length) {
248
+ return null;
249
+ }
250
+ return this.pages[this.page];
251
+ }
252
+ getCurrentPosition() {
253
+ let pageSize = this.getCurrentPage().pageSize;
254
+ let innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
255
+ let innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right;
256
+ return {
257
+ pageNumber: this.page + 1,
258
+ pageOrientation: pageSize.orientation,
259
+ pageInnerHeight: innerHeight,
260
+ pageInnerWidth: innerWidth,
261
+ left: this.x,
262
+ top: this.y,
263
+ verticalRatio: (this.y - this.pageMargins.top) / innerHeight,
264
+ horizontalRatio: (this.x - this.pageMargins.left) / innerWidth
265
+ };
266
+ }
267
+ }
268
+ function pageOrientation(pageOrientationString, currentPageOrientation) {
269
+ if (pageOrientationString === undefined) {
270
+ return currentPageOrientation;
271
+ } else if ((0, _variableType.isString)(pageOrientationString) && pageOrientationString.toLowerCase() === 'landscape') {
272
+ return 'landscape';
273
+ } else {
274
+ return 'portrait';
275
+ }
276
+ }
277
+ const getPageSize = (currentPage, newPageOrientation) => {
278
+ newPageOrientation = pageOrientation(newPageOrientation, currentPage.pageSize.orientation);
279
+ if (newPageOrientation !== currentPage.pageSize.orientation) {
280
+ return {
281
+ orientation: newPageOrientation,
282
+ width: currentPage.pageSize.height,
283
+ height: currentPage.pageSize.width
284
+ };
285
+ } else {
286
+ return {
287
+ orientation: currentPage.pageSize.orientation,
288
+ width: currentPage.pageSize.width,
289
+ height: currentPage.pageSize.height
290
+ };
291
+ }
292
+ };
293
+ function bottomMostContext(c1, c2) {
294
+ let r;
295
+ if (c1.page > c2.page) {
296
+ r = c1;
297
+ } else if (c2.page > c1.page) {
298
+ r = c2;
299
+ } else {
300
+ r = c1.y > c2.y ? c1 : c2;
301
+ }
302
+ return {
303
+ page: r.page,
304
+ x: r.x,
305
+ y: r.y,
306
+ availableHeight: r.availableHeight,
307
+ availableWidth: r.availableWidth
308
+ };
309
+ }
310
+ var _default = exports.default = DocumentContext;