pdfmake 0.2.1 → 0.3.0-beta.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.
- package/CHANGELOG.md +8 -14
- package/README.md +8 -6
- package/build/pdfmake.js +31416 -28217
- package/build/pdfmake.js.map +1 -1
- package/build/pdfmake.min.js +2 -2
- package/build/pdfmake.min.js.map +1 -1
- package/build/standard-fonts/Courier.js +27 -0
- package/build/standard-fonts/Helvetica.js +27 -0
- package/build/standard-fonts/Symbol.js +21 -0
- package/build/standard-fonts/Times.js +27 -0
- package/build/standard-fonts/ZapfDingbats.js +21 -0
- package/build/vfs_fonts.js +2 -2
- package/build-vfs.js +3 -3
- package/fonts/Roboto/Roboto-Italic.ttf +0 -0
- package/fonts/Roboto/Roboto-Medium.ttf +0 -0
- package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
- package/fonts/Roboto/Roboto-Regular.ttf +0 -0
- package/fonts/Roboto.js +8 -0
- package/js/3rd-party/svg-to-pdfkit/source.js +4301 -0
- package/js/3rd-party/svg-to-pdfkit.js +11 -0
- package/js/DocMeasure.js +750 -0
- package/js/DocPreprocessor.js +285 -0
- package/js/DocumentContext.js +306 -0
- package/js/ElementWriter.js +377 -0
- package/js/LayoutBuilder.js +833 -0
- package/js/Line.js +125 -0
- package/js/OutputDocument.js +86 -0
- package/js/OutputDocumentServer.js +34 -0
- package/js/PDFDocument.js +163 -0
- package/js/PageElementWriter.js +161 -0
- package/js/PageSize.js +88 -0
- package/js/Printer.js +238 -0
- package/js/Renderer.js +433 -0
- package/js/SVGMeasure.js +89 -0
- package/js/StyleContextStack.js +206 -0
- package/js/TableProcessor.js +590 -0
- package/js/TextBreaker.js +182 -0
- package/js/TextDecorator.js +181 -0
- package/js/TextInlines.js +248 -0
- package/js/URLResolver.js +79 -0
- package/js/base.js +69 -0
- package/js/browser-extensions/OutputDocumentBrowser.js +131 -0
- package/js/browser-extensions/URLBrowserResolver.js +89 -0
- package/js/browser-extensions/fonts/Roboto.js +42 -0
- package/js/browser-extensions/index.js +70 -0
- package/js/browser-extensions/pdfMake.js +17 -0
- package/js/browser-extensions/standard-fonts/Courier.js +42 -0
- package/js/browser-extensions/standard-fonts/Helvetica.js +42 -0
- package/js/browser-extensions/standard-fonts/Symbol.js +27 -0
- package/js/browser-extensions/standard-fonts/Times.js +42 -0
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +27 -0
- package/js/browser-extensions/virtual-fs-cjs.js +3 -0
- package/js/columnCalculator.js +142 -0
- package/js/helpers/node.js +122 -0
- package/js/helpers/tools.js +48 -0
- package/js/helpers/variableType.js +52 -0
- package/js/index.js +21 -0
- package/js/qrEnc.js +810 -0
- package/js/standardPageSizes.js +57 -0
- package/js/tableLayouts.js +127 -0
- package/js/virtual-fs.js +77 -0
- package/package.json +31 -25
- package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -0
- package/src/3rd-party/svg-to-pdfkit/source.js +2552 -0
- package/src/3rd-party/svg-to-pdfkit.js +3 -0
- package/src/DocMeasure.js +694 -0
- package/src/DocPreprocessor.js +258 -0
- package/src/DocumentContext.js +309 -0
- package/src/ElementWriter.js +368 -0
- package/src/LayoutBuilder.js +814 -0
- package/src/Line.js +114 -0
- package/src/OutputDocument.js +78 -0
- package/src/OutputDocumentServer.js +26 -0
- package/src/PDFDocument.js +148 -0
- package/src/PageElementWriter.js +156 -0
- package/src/PageSize.js +53 -0
- package/src/Printer.js +220 -0
- package/src/Renderer.js +370 -0
- package/src/SVGMeasure.js +79 -0
- package/src/StyleContextStack.js +216 -0
- package/src/TableProcessor.js +558 -0
- package/src/TextBreaker.js +149 -0
- package/src/TextDecorator.js +161 -0
- package/src/TextInlines.js +223 -0
- package/src/URLResolver.js +69 -0
- package/src/base.js +61 -0
- package/src/browser-extensions/OutputDocumentBrowser.js +117 -0
- package/src/browser-extensions/URLBrowserResolver.js +46 -53
- package/src/browser-extensions/fonts/Roboto.js +27 -0
- package/src/browser-extensions/index.js +55 -0
- package/src/browser-extensions/pdfMake.js +10 -287
- package/src/browser-extensions/standard-fonts/Courier.js +27 -0
- package/src/browser-extensions/standard-fonts/Helvetica.js +27 -0
- package/src/browser-extensions/standard-fonts/Symbol.js +21 -0
- package/src/browser-extensions/standard-fonts/Times.js +27 -0
- package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -0
- package/src/browser-extensions/virtual-fs-cjs.js +1 -0
- package/src/columnCalculator.js +29 -32
- package/src/helpers/node.js +110 -0
- package/src/helpers/tools.js +39 -0
- package/src/helpers/variableType.js +39 -0
- package/src/index.js +16 -0
- package/src/qrEnc.js +15 -10
- package/src/standardPageSizes.js +1 -3
- package/src/tableLayouts.js +100 -0
- package/src/virtual-fs.js +66 -0
- package/standard-fonts/Courier.js +8 -0
- package/standard-fonts/Helvetica.js +9 -0
- package/standard-fonts/Symbol.js +5 -0
- package/standard-fonts/Times.js +8 -0
- package/standard-fonts/ZapfDingbats.js +5 -0
- package/src/browser-extensions/virtual-fs.js +0 -55
- package/src/docMeasure.js +0 -807
- package/src/docPreprocessor.js +0 -255
- package/src/documentContext.js +0 -314
- package/src/elementWriter.js +0 -322
- package/src/fontProvider.js +0 -68
- package/src/helpers.js +0 -113
- package/src/imageMeasure.js +0 -51
- package/src/layoutBuilder.js +0 -807
- package/src/line.js +0 -91
- package/src/pageElementWriter.js +0 -174
- package/src/pdfKitEngine.js +0 -21
- package/src/printer.js +0 -685
- package/src/styleContextStack.js +0 -179
- package/src/svgMeasure.js +0 -70
- package/src/tableProcessor.js +0 -542
- package/src/textDecorator.js +0 -151
- package/src/textTools.js +0 -373
- package/src/traversalTracker.js +0 -47
|
@@ -0,0 +1,258 @@
|
|
|
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.pageReference || node.textReference) {
|
|
59
|
+
return this.preprocessText(node);
|
|
60
|
+
} else {
|
|
61
|
+
throw new Error(`Unrecognized document structure: ${stringifyNode(node)}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
preprocessColumns(node) {
|
|
66
|
+
let columns = node.columns;
|
|
67
|
+
|
|
68
|
+
for (let i = 0, l = columns.length; i < l; i++) {
|
|
69
|
+
columns[i] = this.preprocessNode(columns[i]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return node;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
preprocessVerticalContainer(node) {
|
|
76
|
+
let items = node.stack;
|
|
77
|
+
|
|
78
|
+
for (let i = 0, l = items.length; i < l; i++) {
|
|
79
|
+
items[i] = this.preprocessNode(items[i]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return node;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
preprocessList(node) {
|
|
86
|
+
let items = node.ul || node.ol;
|
|
87
|
+
|
|
88
|
+
for (let i = 0, l = items.length; i < l; i++) {
|
|
89
|
+
items[i] = this.preprocessNode(items[i]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return node;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
preprocessTable(node) {
|
|
96
|
+
let col;
|
|
97
|
+
let row;
|
|
98
|
+
let cols;
|
|
99
|
+
let rows;
|
|
100
|
+
|
|
101
|
+
for (col = 0, cols = node.table.body[0].length; col < cols; col++) {
|
|
102
|
+
for (row = 0, rows = node.table.body.length; row < rows; row++) {
|
|
103
|
+
let rowData = node.table.body[row];
|
|
104
|
+
let data = rowData[col];
|
|
105
|
+
if (data !== undefined) {
|
|
106
|
+
if (data === null) { // transform to object
|
|
107
|
+
data = '';
|
|
108
|
+
}
|
|
109
|
+
if (!data._span) {
|
|
110
|
+
rowData[col] = this.preprocessNode(data);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return node;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
preprocessText(node) {
|
|
120
|
+
if (node.tocItem) {
|
|
121
|
+
if (!Array.isArray(node.tocItem)) {
|
|
122
|
+
node.tocItem = [node.tocItem];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (let i = 0, l = node.tocItem.length; i < l; i++) {
|
|
126
|
+
if (!isString(node.tocItem[i])) {
|
|
127
|
+
node.tocItem[i] = '_default_';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let tocItemId = node.tocItem[i];
|
|
131
|
+
|
|
132
|
+
if (!this.tocs[tocItemId]) {
|
|
133
|
+
this.tocs[tocItemId] = { toc: { _items: [], _pseudo: true } };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!node.id) {
|
|
137
|
+
node.id = `toc-${tocItemId}-${this.tocs[tocItemId].toc._items.length}`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let tocItemRef = {
|
|
141
|
+
_nodeRef: this._getNodeForNodeRef(node),
|
|
142
|
+
_textNodeRef: node
|
|
143
|
+
};
|
|
144
|
+
this.tocs[tocItemId].toc._items.push(tocItemRef);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (node.id) {
|
|
149
|
+
if (this.nodeReferences[node.id]) {
|
|
150
|
+
if (!this.nodeReferences[node.id]._pseudo) {
|
|
151
|
+
throw new Error(`Node id '${node.id}' already exists`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
this.nodeReferences[node.id]._nodeRef = this._getNodeForNodeRef(node);
|
|
155
|
+
this.nodeReferences[node.id]._textNodeRef = node;
|
|
156
|
+
this.nodeReferences[node.id]._pseudo = false;
|
|
157
|
+
} else {
|
|
158
|
+
this.nodeReferences[node.id] = {
|
|
159
|
+
_nodeRef: this._getNodeForNodeRef(node),
|
|
160
|
+
_textNodeRef: node
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (node.pageReference) {
|
|
166
|
+
if (!this.nodeReferences[node.pageReference]) {
|
|
167
|
+
this.nodeReferences[node.pageReference] = {
|
|
168
|
+
_nodeRef: {},
|
|
169
|
+
_textNodeRef: {},
|
|
170
|
+
_pseudo: true
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
node.text = '00000';
|
|
174
|
+
node.linkToDestination = node.pageReference;
|
|
175
|
+
node._pageRef = this.nodeReferences[node.pageReference];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (node.textReference) {
|
|
179
|
+
if (!this.nodeReferences[node.textReference]) {
|
|
180
|
+
this.nodeReferences[node.textReference] = { _nodeRef: {}, _pseudo: true };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
node.text = '';
|
|
184
|
+
node.linkToDestination = node.textReference;
|
|
185
|
+
node._textRef = this.nodeReferences[node.textReference];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (node.text && node.text.text) {
|
|
189
|
+
node.text = [this.preprocessNode(node.text)];
|
|
190
|
+
} else if (Array.isArray(node.text)) {
|
|
191
|
+
let isSetParentNode = false;
|
|
192
|
+
if (this.parentNode === null) {
|
|
193
|
+
this.parentNode = node;
|
|
194
|
+
isSetParentNode = true;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
for (let i = 0, l = node.text.length; i < l; i++) {
|
|
198
|
+
node.text[i] = this.preprocessNode(node.text[i]);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (isSetParentNode) {
|
|
202
|
+
this.parentNode = null;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return node;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
preprocessToc(node) {
|
|
210
|
+
if (!node.toc.id) {
|
|
211
|
+
node.toc.id = '_default_';
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
node.toc.title = node.toc.title ? this.preprocessNode(node.toc.title) : null;
|
|
215
|
+
node.toc._items = [];
|
|
216
|
+
|
|
217
|
+
if (this.tocs[node.toc.id]) {
|
|
218
|
+
if (!this.tocs[node.toc.id].toc._pseudo) {
|
|
219
|
+
throw new Error(`TOC '${node.toc.id}' already exists`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
node.toc._items = this.tocs[node.toc.id].toc._items;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
this.tocs[node.toc.id] = node;
|
|
226
|
+
|
|
227
|
+
return node;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
preprocessImage(node) {
|
|
231
|
+
if ((node.image.type !== undefined) && (node.image.data !== undefined) && (node.image.type === 'Buffer') && Array.isArray(node.image.data)) {
|
|
232
|
+
node.image = Buffer.from(node.image.data);
|
|
233
|
+
}
|
|
234
|
+
return node;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
preprocessCanvas(node) {
|
|
238
|
+
return node;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
preprocessSVG(node) {
|
|
242
|
+
return node;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
preprocessQr(node) {
|
|
246
|
+
return node;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
_getNodeForNodeRef(node) {
|
|
250
|
+
if (this.parentNode) {
|
|
251
|
+
return this.parentNode;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return node;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export default DocPreprocessor;
|
|
@@ -0,0 +1,309 @@
|
|
|
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.endingCell = null;
|
|
22
|
+
this.backgroundLength = [];
|
|
23
|
+
|
|
24
|
+
this.addPage(pageSize);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
beginColumnGroup() {
|
|
28
|
+
this.snapshots.push({
|
|
29
|
+
x: this.x,
|
|
30
|
+
y: this.y,
|
|
31
|
+
availableHeight: this.availableHeight,
|
|
32
|
+
availableWidth: this.availableWidth,
|
|
33
|
+
page: this.page,
|
|
34
|
+
bottomMost: {
|
|
35
|
+
x: this.x,
|
|
36
|
+
y: this.y,
|
|
37
|
+
availableHeight: this.availableHeight,
|
|
38
|
+
availableWidth: this.availableWidth,
|
|
39
|
+
page: this.page
|
|
40
|
+
},
|
|
41
|
+
endingCell: this.endingCell,
|
|
42
|
+
lastColumnWidth: this.lastColumnWidth
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
this.lastColumnWidth = 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
beginColumn(width, offset, endingCell) {
|
|
49
|
+
let saved = this.snapshots[this.snapshots.length - 1];
|
|
50
|
+
|
|
51
|
+
this.calculateBottomMost(saved);
|
|
52
|
+
|
|
53
|
+
this.endingCell = endingCell;
|
|
54
|
+
this.page = saved.page;
|
|
55
|
+
this.x = this.x + this.lastColumnWidth + (offset || 0);
|
|
56
|
+
this.y = saved.y;
|
|
57
|
+
this.availableWidth = width; //saved.availableWidth - offset;
|
|
58
|
+
this.availableHeight = saved.availableHeight;
|
|
59
|
+
|
|
60
|
+
this.lastColumnWidth = width;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
calculateBottomMost(destContext) {
|
|
64
|
+
if (this.endingCell) {
|
|
65
|
+
this.saveContextInEndingCell(this.endingCell);
|
|
66
|
+
this.endingCell = null;
|
|
67
|
+
} else {
|
|
68
|
+
destContext.bottomMost = bottomMostContext(this, destContext.bottomMost);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
markEnding(endingCell) {
|
|
73
|
+
this.page = endingCell._columnEndingContext.page;
|
|
74
|
+
this.x = endingCell._columnEndingContext.x;
|
|
75
|
+
this.y = endingCell._columnEndingContext.y;
|
|
76
|
+
this.availableWidth = endingCell._columnEndingContext.availableWidth;
|
|
77
|
+
this.availableHeight = endingCell._columnEndingContext.availableHeight;
|
|
78
|
+
this.lastColumnWidth = endingCell._columnEndingContext.lastColumnWidth;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
saveContextInEndingCell(endingCell) {
|
|
82
|
+
endingCell._columnEndingContext = {
|
|
83
|
+
page: this.page,
|
|
84
|
+
x: this.x,
|
|
85
|
+
y: this.y,
|
|
86
|
+
availableHeight: this.availableHeight,
|
|
87
|
+
availableWidth: this.availableWidth,
|
|
88
|
+
lastColumnWidth: this.lastColumnWidth
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
completeColumnGroup(height) {
|
|
93
|
+
let saved = this.snapshots.pop();
|
|
94
|
+
|
|
95
|
+
this.calculateBottomMost(saved);
|
|
96
|
+
|
|
97
|
+
this.endingCell = null;
|
|
98
|
+
this.x = saved.x;
|
|
99
|
+
|
|
100
|
+
let y = saved.bottomMost.y;
|
|
101
|
+
if (height) {
|
|
102
|
+
if (saved.page === saved.bottomMost.page) {
|
|
103
|
+
if ((saved.y + height) > y) {
|
|
104
|
+
y = saved.y + height;
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
y += height;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
this.y = y;
|
|
112
|
+
this.page = saved.bottomMost.page;
|
|
113
|
+
this.availableWidth = saved.availableWidth;
|
|
114
|
+
this.availableHeight = saved.bottomMost.availableHeight;
|
|
115
|
+
if (height) {
|
|
116
|
+
this.availableHeight -= (y - saved.bottomMost.y);
|
|
117
|
+
}
|
|
118
|
+
this.lastColumnWidth = saved.lastColumnWidth;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
addMargin(left, right) {
|
|
122
|
+
this.x += left;
|
|
123
|
+
this.availableWidth -= left + (right || 0);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
moveDown(offset) {
|
|
127
|
+
this.y += offset;
|
|
128
|
+
this.availableHeight -= offset;
|
|
129
|
+
|
|
130
|
+
return this.availableHeight > 0;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
initializePage() {
|
|
134
|
+
this.y = this.pageMargins.top;
|
|
135
|
+
this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
|
|
136
|
+
this.pageSnapshot().availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
pageSnapshot() {
|
|
140
|
+
if (this.snapshots[0]) {
|
|
141
|
+
return this.snapshots[0];
|
|
142
|
+
} else {
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
moveTo(x, y) {
|
|
148
|
+
if (x !== undefined && x !== null) {
|
|
149
|
+
this.x = x;
|
|
150
|
+
this.availableWidth = this.getCurrentPage().pageSize.width - this.x - this.pageMargins.right;
|
|
151
|
+
}
|
|
152
|
+
if (y !== undefined && y !== null) {
|
|
153
|
+
this.y = y;
|
|
154
|
+
this.availableHeight = this.getCurrentPage().pageSize.height - this.y - this.pageMargins.bottom;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
moveToRelative(x, y) {
|
|
159
|
+
if (x !== undefined && x !== null) {
|
|
160
|
+
this.x = this.x + x;
|
|
161
|
+
}
|
|
162
|
+
if (y !== undefined && y !== null) {
|
|
163
|
+
this.y = this.y + y;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
beginDetachedBlock() {
|
|
168
|
+
this.snapshots.push({
|
|
169
|
+
x: this.x,
|
|
170
|
+
y: this.y,
|
|
171
|
+
availableHeight: this.availableHeight,
|
|
172
|
+
availableWidth: this.availableWidth,
|
|
173
|
+
page: this.page,
|
|
174
|
+
endingCell: this.endingCell,
|
|
175
|
+
lastColumnWidth: this.lastColumnWidth
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
endDetachedBlock() {
|
|
180
|
+
let saved = this.snapshots.pop();
|
|
181
|
+
|
|
182
|
+
this.x = saved.x;
|
|
183
|
+
this.y = saved.y;
|
|
184
|
+
this.availableWidth = saved.availableWidth;
|
|
185
|
+
this.availableHeight = saved.availableHeight;
|
|
186
|
+
this.page = saved.page;
|
|
187
|
+
this.endingCell = saved.endingCell;
|
|
188
|
+
this.lastColumnWidth = saved.lastColumnWidth;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
moveToNextPage(pageOrientation) {
|
|
192
|
+
let nextPageIndex = this.page + 1;
|
|
193
|
+
let prevPage = this.page;
|
|
194
|
+
let prevY = this.y;
|
|
195
|
+
|
|
196
|
+
let createNewPage = nextPageIndex >= this.pages.length;
|
|
197
|
+
if (createNewPage) {
|
|
198
|
+
let currentAvailableWidth = this.availableWidth;
|
|
199
|
+
let currentPageOrientation = this.getCurrentPage().pageSize.orientation;
|
|
200
|
+
|
|
201
|
+
let pageSize = getPageSize(this.getCurrentPage(), pageOrientation);
|
|
202
|
+
this.addPage(pageSize);
|
|
203
|
+
|
|
204
|
+
if (currentPageOrientation === pageSize.orientation) {
|
|
205
|
+
this.availableWidth = currentAvailableWidth;
|
|
206
|
+
}
|
|
207
|
+
} else {
|
|
208
|
+
this.page = nextPageIndex;
|
|
209
|
+
this.initializePage();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
newPageCreated: createNewPage,
|
|
214
|
+
prevPage: prevPage,
|
|
215
|
+
prevY: prevY,
|
|
216
|
+
y: this.y
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
addPage(pageSize) {
|
|
221
|
+
let page = { items: [], pageSize: pageSize };
|
|
222
|
+
this.pages.push(page);
|
|
223
|
+
this.backgroundLength.push(0);
|
|
224
|
+
this.page = this.pages.length - 1;
|
|
225
|
+
this.initializePage();
|
|
226
|
+
|
|
227
|
+
this.emit('pageAdded');
|
|
228
|
+
|
|
229
|
+
return page;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
getCurrentPage() {
|
|
233
|
+
if (this.page < 0 || this.page >= this.pages.length) {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return this.pages[this.page];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
getCurrentPosition() {
|
|
241
|
+
let pageSize = this.getCurrentPage().pageSize;
|
|
242
|
+
let innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
|
|
243
|
+
let innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right;
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
pageNumber: this.page + 1,
|
|
247
|
+
pageOrientation: pageSize.orientation,
|
|
248
|
+
pageInnerHeight: innerHeight,
|
|
249
|
+
pageInnerWidth: innerWidth,
|
|
250
|
+
left: this.x,
|
|
251
|
+
top: this.y,
|
|
252
|
+
verticalRatio: ((this.y - this.pageMargins.top) / innerHeight),
|
|
253
|
+
horizontalRatio: ((this.x - this.pageMargins.left) / innerWidth)
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function pageOrientation(pageOrientationString, currentPageOrientation) {
|
|
259
|
+
if (pageOrientationString === undefined) {
|
|
260
|
+
return currentPageOrientation;
|
|
261
|
+
} else if (isString(pageOrientationString) && (pageOrientationString.toLowerCase() === 'landscape')) {
|
|
262
|
+
return 'landscape';
|
|
263
|
+
} else {
|
|
264
|
+
return 'portrait';
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const getPageSize = (currentPage, newPageOrientation) => {
|
|
269
|
+
|
|
270
|
+
newPageOrientation = pageOrientation(newPageOrientation, currentPage.pageSize.orientation);
|
|
271
|
+
|
|
272
|
+
if (newPageOrientation !== currentPage.pageSize.orientation) {
|
|
273
|
+
return {
|
|
274
|
+
orientation: newPageOrientation,
|
|
275
|
+
width: currentPage.pageSize.height,
|
|
276
|
+
height: currentPage.pageSize.width
|
|
277
|
+
};
|
|
278
|
+
} else {
|
|
279
|
+
return {
|
|
280
|
+
orientation: currentPage.pageSize.orientation,
|
|
281
|
+
width: currentPage.pageSize.width,
|
|
282
|
+
height: currentPage.pageSize.height
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
function bottomMostContext(c1, c2) {
|
|
290
|
+
let r;
|
|
291
|
+
|
|
292
|
+
if (c1.page > c2.page) {
|
|
293
|
+
r = c1;
|
|
294
|
+
} else if (c2.page > c1.page) {
|
|
295
|
+
r = c2;
|
|
296
|
+
} else {
|
|
297
|
+
r = (c1.y > c2.y) ? c1 : c2;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return {
|
|
301
|
+
page: r.page,
|
|
302
|
+
x: r.x,
|
|
303
|
+
y: r.y,
|
|
304
|
+
availableHeight: r.availableHeight,
|
|
305
|
+
availableWidth: r.availableWidth
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export default DocumentContext;
|