@digicole/pdfmake-rtl 2.1.0 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +118 -83
- package/README.md +11 -10
- package/build/pdfmake.js +71 -42
- package/build/pdfmake.js.map +1 -1
- package/build/pdfmake.min.js +2 -2
- package/build/pdfmake.min.js.map +1 -1
- package/build/vfs_fonts.js +11 -11
- package/js/3rd-party/svg-to-pdfkit/source.js +3823 -0
- package/js/3rd-party/svg-to-pdfkit.js +7 -0
- package/js/DocMeasure.js +713 -0
- package/js/DocPreprocessor.js +275 -0
- package/js/DocumentContext.js +310 -0
- package/js/ElementWriter.js +687 -0
- package/js/LayoutBuilder.js +1240 -0
- package/js/Line.js +113 -0
- package/js/OutputDocument.js +64 -0
- package/js/OutputDocumentServer.js +29 -0
- package/js/PDFDocument.js +144 -0
- package/js/PageElementWriter.js +161 -0
- package/js/PageSize.js +74 -0
- package/js/Printer.js +351 -0
- package/js/Renderer.js +417 -0
- package/js/SVGMeasure.js +92 -0
- package/js/StyleContextStack.js +191 -0
- package/js/TableProcessor.js +575 -0
- package/js/TextBreaker.js +166 -0
- package/js/TextDecorator.js +152 -0
- package/js/TextInlines.js +244 -0
- package/js/URLResolver.js +43 -0
- package/js/base.js +59 -0
- package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
- package/js/browser-extensions/fonts/Cairo.js +38 -0
- package/js/browser-extensions/fonts/Roboto.js +38 -0
- package/js/browser-extensions/index.js +59 -0
- package/js/browser-extensions/pdfMake.js +3 -0
- package/js/browser-extensions/standard-fonts/Courier.js +38 -0
- package/js/browser-extensions/standard-fonts/Helvetica.js +38 -0
- package/js/browser-extensions/standard-fonts/Symbol.js +23 -0
- package/js/browser-extensions/standard-fonts/Times.js +38 -0
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +23 -0
- package/js/browser-extensions/virtual-fs-cjs.js +3 -0
- package/js/columnCalculator.js +148 -0
- package/js/helpers/node.js +123 -0
- package/js/helpers/tools.js +46 -0
- package/js/helpers/variableType.js +59 -0
- package/js/index.js +15 -0
- package/js/qrEnc.js +721 -0
- package/js/rtlUtils.js +519 -0
- package/js/standardPageSizes.js +56 -0
- package/js/tableLayouts.js +98 -0
- package/js/virtual-fs.js +60 -0
- package/package.json +1 -1
- package/src/{docMeasure.js → DocMeasure.js} +8 -8
- package/src/{elementWriter.js → ElementWriter.js} +3 -3
- package/src/{layoutBuilder.js → LayoutBuilder.js} +1406 -1393
- package/src/{tableProcessor.js → TableProcessor.js} +633 -620
- package/src/rtlUtils.js +503 -500
- /package/src/{docPreprocessor.js → DocPreprocessor.js} +0 -0
- /package/src/{documentContext.js → DocumentContext.js} +0 -0
- /package/src/{line.js → Line.js} +0 -0
- /package/src/{pageElementWriter.js → PageElementWriter.js} +0 -0
- /package/src/{printer.js → Printer.js} +0 -0
- /package/src/{svgMeasure.js → SVGMeasure.js} +0 -0
- /package/src/{styleContextStack.js → StyleContextStack.js} +0 -0
- /package/src/{textDecorator.js → TextDecorator.js} +0 -0
|
@@ -0,0 +1,275 @@
|
|
|
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
|
+
var _rtlUtils = require("./rtlUtils");
|
|
8
|
+
const convertValueToString = value => {
|
|
9
|
+
if ((0, _variableType.isString)(value)) {
|
|
10
|
+
return value.replace(/\t/g, ' '); // expand tab as spaces
|
|
11
|
+
} else if ((0, _variableType.isNumber)(value) || typeof value === 'boolean') {
|
|
12
|
+
return value.toString();
|
|
13
|
+
} else if (!(0, _variableType.isValue)(value) || (0, _variableType.isEmptyObject)(value)) {
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// TODO: throw exception ?
|
|
18
|
+
|
|
19
|
+
return value;
|
|
20
|
+
};
|
|
21
|
+
class DocPreprocessor {
|
|
22
|
+
preprocessDocument(docStructure) {
|
|
23
|
+
this.parentNode = null;
|
|
24
|
+
this.tocs = [];
|
|
25
|
+
this.nodeReferences = [];
|
|
26
|
+
|
|
27
|
+
// Check if RTL is enabled at document level
|
|
28
|
+
const documentRTL = docStructure.rtl === true;
|
|
29
|
+
|
|
30
|
+
// Process content for RTL if enabled
|
|
31
|
+
if (documentRTL && docStructure.content) {
|
|
32
|
+
docStructure.content = (0, _rtlUtils.processRTLElement)(docStructure.content, true);
|
|
33
|
+
}
|
|
34
|
+
return this.preprocessNode(docStructure, true);
|
|
35
|
+
}
|
|
36
|
+
preprocessBlock(node) {
|
|
37
|
+
this.parentNode = null;
|
|
38
|
+
this.tocs = [];
|
|
39
|
+
this.nodeReferences = [];
|
|
40
|
+
return this.preprocessNode(node);
|
|
41
|
+
}
|
|
42
|
+
preprocessNode(node, isSectionAllowed = false) {
|
|
43
|
+
// expand shortcuts and casting values
|
|
44
|
+
if (Array.isArray(node)) {
|
|
45
|
+
node = {
|
|
46
|
+
stack: node
|
|
47
|
+
};
|
|
48
|
+
} else if ((0, _variableType.isString)(node) || (0, _variableType.isNumber)(node) || typeof node === 'boolean' || !(0, _variableType.isValue)(node) || (0, _variableType.isEmptyObject)(node)) {
|
|
49
|
+
// text node defined as value
|
|
50
|
+
node = {
|
|
51
|
+
text: convertValueToString(node)
|
|
52
|
+
};
|
|
53
|
+
} else if ('text' in node) {
|
|
54
|
+
// cast value in text property
|
|
55
|
+
node.text = convertValueToString(node.text);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Apply RTL processing if rtl property is set on this node
|
|
59
|
+
if (node && typeof node === 'object' && node.rtl === true) {
|
|
60
|
+
node = (0, _rtlUtils.processRTLElement)(node, true);
|
|
61
|
+
}
|
|
62
|
+
// Auto-detect RTL for text nodes without explicit rtl property
|
|
63
|
+
else if (node && typeof node === 'object' && node.text && typeof node.text === 'string' && (0, _rtlUtils.containsRTL)(node.text) && !node.rtl) {
|
|
64
|
+
node = (0, _rtlUtils.applyRTLToNode)(node, false);
|
|
65
|
+
}
|
|
66
|
+
// Auto-detect RTL for table nodes - reverse columns if table has RTL content
|
|
67
|
+
else if (node && typeof node === 'object' && node.table && !node.rtl) {
|
|
68
|
+
node = (0, _rtlUtils.processRTLTable)(node);
|
|
69
|
+
}
|
|
70
|
+
if (node.section) {
|
|
71
|
+
if (!isSectionAllowed) {
|
|
72
|
+
throw new Error(`Incorrect document structure, section node is only allowed at the root level of document structure: ${(0, _node.stringifyNode)(node)}`);
|
|
73
|
+
}
|
|
74
|
+
return this.preprocessSection(node);
|
|
75
|
+
} else if (node.columns) {
|
|
76
|
+
return this.preprocessColumns(node);
|
|
77
|
+
} else if (node.stack) {
|
|
78
|
+
return this.preprocessVerticalContainer(node, isSectionAllowed);
|
|
79
|
+
} else if (node.ul) {
|
|
80
|
+
return this.preprocessList(node);
|
|
81
|
+
} else if (node.ol) {
|
|
82
|
+
return this.preprocessList(node);
|
|
83
|
+
} else if (node.table) {
|
|
84
|
+
return this.preprocessTable(node);
|
|
85
|
+
} else if (node.text !== undefined) {
|
|
86
|
+
return this.preprocessText(node);
|
|
87
|
+
} else if (node.toc) {
|
|
88
|
+
return this.preprocessToc(node);
|
|
89
|
+
} else if (node.image) {
|
|
90
|
+
return this.preprocessImage(node);
|
|
91
|
+
} else if (node.svg) {
|
|
92
|
+
return this.preprocessSVG(node);
|
|
93
|
+
} else if (node.canvas) {
|
|
94
|
+
return this.preprocessCanvas(node);
|
|
95
|
+
} else if (node.qr) {
|
|
96
|
+
return this.preprocessQr(node);
|
|
97
|
+
} else if (node.attachment) {
|
|
98
|
+
return this.preprocessAttachment(node);
|
|
99
|
+
} else if (node.pageReference || node.textReference) {
|
|
100
|
+
return this.preprocessText(node);
|
|
101
|
+
} else {
|
|
102
|
+
throw new Error(`Unrecognized document structure: ${(0, _node.stringifyNode)(node)}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
preprocessSection(node) {
|
|
106
|
+
node.section = this.preprocessNode(node.section);
|
|
107
|
+
return node;
|
|
108
|
+
}
|
|
109
|
+
preprocessColumns(node) {
|
|
110
|
+
let columns = node.columns;
|
|
111
|
+
for (let i = 0, l = columns.length; i < l; i++) {
|
|
112
|
+
columns[i] = this.preprocessNode(columns[i]);
|
|
113
|
+
}
|
|
114
|
+
return node;
|
|
115
|
+
}
|
|
116
|
+
preprocessVerticalContainer(node, isSectionAllowed) {
|
|
117
|
+
let items = node.stack;
|
|
118
|
+
for (let i = 0, l = items.length; i < l; i++) {
|
|
119
|
+
items[i] = this.preprocessNode(items[i], isSectionAllowed);
|
|
120
|
+
}
|
|
121
|
+
return node;
|
|
122
|
+
}
|
|
123
|
+
preprocessList(node) {
|
|
124
|
+
let items = node.ul || node.ol;
|
|
125
|
+
for (let i = 0, l = items.length; i < l; i++) {
|
|
126
|
+
items[i] = this.preprocessNode(items[i]);
|
|
127
|
+
}
|
|
128
|
+
return node;
|
|
129
|
+
}
|
|
130
|
+
preprocessTable(node) {
|
|
131
|
+
let col;
|
|
132
|
+
let row;
|
|
133
|
+
let cols;
|
|
134
|
+
let rows;
|
|
135
|
+
for (col = 0, cols = node.table.body[0].length; col < cols; col++) {
|
|
136
|
+
for (row = 0, rows = node.table.body.length; row < rows; row++) {
|
|
137
|
+
let rowData = node.table.body[row];
|
|
138
|
+
let data = rowData[col];
|
|
139
|
+
if (data !== undefined) {
|
|
140
|
+
if (data === null) {
|
|
141
|
+
// transform to object
|
|
142
|
+
data = '';
|
|
143
|
+
}
|
|
144
|
+
if (!data._span) {
|
|
145
|
+
rowData[col] = this.preprocessNode(data);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return node;
|
|
151
|
+
}
|
|
152
|
+
preprocessText(node) {
|
|
153
|
+
if (node.tocItem) {
|
|
154
|
+
if (!Array.isArray(node.tocItem)) {
|
|
155
|
+
node.tocItem = [node.tocItem];
|
|
156
|
+
}
|
|
157
|
+
for (let i = 0, l = node.tocItem.length; i < l; i++) {
|
|
158
|
+
if (!(0, _variableType.isString)(node.tocItem[i])) {
|
|
159
|
+
node.tocItem[i] = '_default_';
|
|
160
|
+
}
|
|
161
|
+
let tocItemId = node.tocItem[i];
|
|
162
|
+
if (!this.tocs[tocItemId]) {
|
|
163
|
+
this.tocs[tocItemId] = {
|
|
164
|
+
toc: {
|
|
165
|
+
_items: [],
|
|
166
|
+
_pseudo: true
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
if (!node.id) {
|
|
171
|
+
node.id = `toc-${tocItemId}-${this.tocs[tocItemId].toc._items.length}`;
|
|
172
|
+
}
|
|
173
|
+
let tocItemRef = {
|
|
174
|
+
_nodeRef: this._getNodeForNodeRef(node),
|
|
175
|
+
_textNodeRef: node
|
|
176
|
+
};
|
|
177
|
+
this.tocs[tocItemId].toc._items.push(tocItemRef);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (node.id) {
|
|
181
|
+
if (this.nodeReferences[node.id]) {
|
|
182
|
+
if (!this.nodeReferences[node.id]._pseudo) {
|
|
183
|
+
throw new Error(`Node id '${node.id}' already exists`);
|
|
184
|
+
}
|
|
185
|
+
this.nodeReferences[node.id]._nodeRef = this._getNodeForNodeRef(node);
|
|
186
|
+
this.nodeReferences[node.id]._textNodeRef = node;
|
|
187
|
+
this.nodeReferences[node.id]._pseudo = false;
|
|
188
|
+
} else {
|
|
189
|
+
this.nodeReferences[node.id] = {
|
|
190
|
+
_nodeRef: this._getNodeForNodeRef(node),
|
|
191
|
+
_textNodeRef: node
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (node.pageReference) {
|
|
196
|
+
if (!this.nodeReferences[node.pageReference]) {
|
|
197
|
+
this.nodeReferences[node.pageReference] = {
|
|
198
|
+
_nodeRef: {},
|
|
199
|
+
_textNodeRef: {},
|
|
200
|
+
_pseudo: true
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
node.text = '00000';
|
|
204
|
+
node.linkToDestination = node.pageReference;
|
|
205
|
+
node._pageRef = this.nodeReferences[node.pageReference];
|
|
206
|
+
}
|
|
207
|
+
if (node.textReference) {
|
|
208
|
+
if (!this.nodeReferences[node.textReference]) {
|
|
209
|
+
this.nodeReferences[node.textReference] = {
|
|
210
|
+
_nodeRef: {},
|
|
211
|
+
_pseudo: true
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
node.text = '';
|
|
215
|
+
node.linkToDestination = node.textReference;
|
|
216
|
+
node._textRef = this.nodeReferences[node.textReference];
|
|
217
|
+
}
|
|
218
|
+
if (node.text && node.text.text) {
|
|
219
|
+
node.text = [this.preprocessNode(node.text)];
|
|
220
|
+
} else if (Array.isArray(node.text)) {
|
|
221
|
+
let isSetParentNode = false;
|
|
222
|
+
if (this.parentNode === null) {
|
|
223
|
+
this.parentNode = node;
|
|
224
|
+
isSetParentNode = true;
|
|
225
|
+
}
|
|
226
|
+
for (let i = 0, l = node.text.length; i < l; i++) {
|
|
227
|
+
node.text[i] = this.preprocessNode(node.text[i]);
|
|
228
|
+
}
|
|
229
|
+
if (isSetParentNode) {
|
|
230
|
+
this.parentNode = null;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return node;
|
|
234
|
+
}
|
|
235
|
+
preprocessToc(node) {
|
|
236
|
+
if (!node.toc.id) {
|
|
237
|
+
node.toc.id = '_default_';
|
|
238
|
+
}
|
|
239
|
+
node.toc.title = node.toc.title ? this.preprocessNode(node.toc.title) : null;
|
|
240
|
+
node.toc._items = [];
|
|
241
|
+
if (this.tocs[node.toc.id]) {
|
|
242
|
+
if (!this.tocs[node.toc.id].toc._pseudo) {
|
|
243
|
+
throw new Error(`TOC '${node.toc.id}' already exists`);
|
|
244
|
+
}
|
|
245
|
+
node.toc._items = this.tocs[node.toc.id].toc._items;
|
|
246
|
+
}
|
|
247
|
+
this.tocs[node.toc.id] = node;
|
|
248
|
+
return node;
|
|
249
|
+
}
|
|
250
|
+
preprocessImage(node) {
|
|
251
|
+
if (node.image.type !== undefined && node.image.data !== undefined && node.image.type === 'Buffer' && Array.isArray(node.image.data)) {
|
|
252
|
+
node.image = Buffer.from(node.image.data);
|
|
253
|
+
}
|
|
254
|
+
return node;
|
|
255
|
+
}
|
|
256
|
+
preprocessCanvas(node) {
|
|
257
|
+
return node;
|
|
258
|
+
}
|
|
259
|
+
preprocessSVG(node) {
|
|
260
|
+
return node;
|
|
261
|
+
}
|
|
262
|
+
preprocessQr(node) {
|
|
263
|
+
return node;
|
|
264
|
+
}
|
|
265
|
+
preprocessAttachment(node) {
|
|
266
|
+
return node;
|
|
267
|
+
}
|
|
268
|
+
_getNodeForNodeRef(node) {
|
|
269
|
+
if (this.parentNode) {
|
|
270
|
+
return this.parentNode;
|
|
271
|
+
}
|
|
272
|
+
return node;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
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;
|