@wirabyte/w-pdfmaker 0.0.0-stage → 1.0.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 +53 -0
- package/LICENSE +22 -0
- package/NOTICE.md +39 -0
- package/README.md +106 -2
- package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
- package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
- package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
- package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
- package/build/fonts/Roboto.js +27 -0
- package/build/pdfmake.js +103161 -0
- package/build/pdfmake.js.map +1 -0
- package/build/pdfmake.min.js +3 -0
- package/build/pdfmake.min.js.map +1 -0
- package/build/standard-fonts/Courier.js +9 -0
- package/build/standard-fonts/Helvetica.js +9 -0
- package/build/standard-fonts/Symbol.js +9 -0
- package/build/standard-fonts/Times.js +9 -0
- package/build/standard-fonts/ZapfDingbats.js +9 -0
- package/build/vfs_fonts.js +6 -0
- package/build-vfs.js +44 -0
- 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/fonts/THSarabunIT9/LICENSE.txt +7 -0
- package/fonts/THSarabunIT9/THSarabunIT9-Bold.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9-BoldItalic.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9-Italic.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9.ttf +0 -0
- package/fonts/THSarabunNew/COPYING.GPLv2 +339 -0
- package/fonts/THSarabunNew/LICENSE.txt +13 -0
- package/fonts/THSarabunNew/THSarabunNew-Bold.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew-BoldItalic.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew-Italic.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew.ttf +0 -0
- package/index.d.ts +167 -0
- package/interfaces.d.ts +2779 -0
- package/js/DocMeasure.js +695 -0
- package/js/DocPreprocessor.js +263 -0
- package/js/DocumentContext.js +563 -0
- package/js/ElementWriter.js +488 -0
- package/js/LayoutBuilder.js +1525 -0
- package/js/Line.js +105 -0
- package/js/OutputDocument.js +73 -0
- package/js/OutputDocumentServer.js +34 -0
- package/js/PDFDocument.js +322 -0
- package/js/PageElementWriter.js +302 -0
- package/js/PageSize.js +74 -0
- package/js/Printer.js +275 -0
- package/js/Renderer.js +439 -0
- package/js/SVGMeasure.js +94 -0
- package/js/StyleContextStack.js +191 -0
- package/js/TableProcessor.js +658 -0
- package/js/TextBreaker.js +164 -0
- package/js/TextDecorator.js +164 -0
- package/js/TextInlines.js +212 -0
- package/js/URLResolver.js +122 -0
- package/js/base.js +125 -0
- package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
- package/js/browser-extensions/fonts/Roboto.js +38 -0
- package/js/browser-extensions/fonts/thai.js +28 -0
- package/js/browser-extensions/index.js +88 -0
- package/js/browser-extensions/pageUrls.js +79 -0
- package/js/browser-extensions/pdfMake.js +3 -0
- package/js/browser-extensions/standard-fonts/Courier.js +20 -0
- package/js/browser-extensions/standard-fonts/Helvetica.js +20 -0
- package/js/browser-extensions/standard-fonts/Symbol.js +17 -0
- package/js/browser-extensions/standard-fonts/Times.js +20 -0
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
- package/js/browser-extensions/virtual-fs-cjs.js +3 -0
- package/js/columnCalculator.js +159 -0
- package/js/helpers/node.js +122 -0
- package/js/helpers/tools.js +46 -0
- package/js/helpers/variableType.js +59 -0
- package/js/imageFallback.js +23 -0
- package/js/index.js +45 -0
- package/js/mergeStamp.js +194 -0
- package/js/pageNumbering.js +48 -0
- package/js/qrEnc.js +721 -0
- package/js/standardPageSizes.js +56 -0
- package/js/tableLayouts.js +98 -0
- package/js/thai.js +201 -0
- package/js/virtual-fs.js +82 -0
- package/package.json +93 -4
- package/skills/w-pdfmaker/SKILL.md +171 -0
- package/skills/w-pdfmaker/references/browser.md +36 -0
- package/skills/w-pdfmaker/references/document-definition.md +241 -0
- package/skills/w-pdfmaker/references/images.md +90 -0
- package/skills/w-pdfmaker/references/merge-stamp.md +94 -0
- package/skills/w-pdfmaker/references/thai.md +67 -0
- package/src/DocMeasure.js +780 -0
- package/src/DocPreprocessor.js +293 -0
- package/src/DocumentContext.js +605 -0
- package/src/ElementWriter.js +550 -0
- package/src/LayoutBuilder.js +1689 -0
- package/src/Line.js +114 -0
- package/src/OutputDocument.js +75 -0
- package/src/OutputDocumentServer.js +38 -0
- package/src/PDFDocument.js +352 -0
- package/src/PageElementWriter.js +343 -0
- package/src/PageSize.js +53 -0
- package/src/Printer.js +289 -0
- package/src/Renderer.js +468 -0
- package/src/SVGMeasure.js +109 -0
- package/src/StyleContextStack.js +208 -0
- package/src/TableProcessor.js +719 -0
- package/src/TextBreaker.js +166 -0
- package/src/TextDecorator.js +191 -0
- package/src/TextInlines.js +229 -0
- package/src/URLResolver.js +122 -0
- package/src/base.js +143 -0
- package/src/browser-extensions/OutputDocumentBrowser.js +80 -0
- package/src/browser-extensions/fonts/Roboto.js +27 -0
- package/src/browser-extensions/fonts/thai.js +23 -0
- package/src/browser-extensions/index.js +91 -0
- package/src/browser-extensions/pageUrls.js +67 -0
- package/src/browser-extensions/pdfMake.js +1 -0
- package/src/browser-extensions/standard-fonts/Courier.js +25 -0
- package/src/browser-extensions/standard-fonts/Helvetica.js +25 -0
- package/src/browser-extensions/standard-fonts/Symbol.js +17 -0
- package/src/browser-extensions/standard-fonts/Times.js +25 -0
- package/src/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
- package/src/browser-extensions/virtual-fs-cjs.js +1 -0
- package/src/columnCalculator.js +160 -0
- package/src/helpers/node.js +134 -0
- package/src/helpers/tools.js +44 -0
- package/src/helpers/variableType.js +50 -0
- package/src/imageFallback.js +18 -0
- package/src/index.js +45 -0
- package/src/mergeStamp.js +132 -0
- package/src/pageNumbering.js +45 -0
- package/src/qrEnc.js +796 -0
- package/src/standardPageSizes.js +52 -0
- package/src/tableLayouts.js +100 -0
- package/src/thai.js +170 -0
- package/src/virtual-fs.js +92 -0
- package/standard-fonts/Courier.js +8 -0
- package/standard-fonts/Helvetica.js +8 -0
- package/standard-fonts/Symbol.js +5 -0
- package/standard-fonts/Times.js +8 -0
- package/standard-fonts/ZapfDingbats.js +5 -0
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
import { isNumber } from './helpers/variableType';
|
|
2
|
+
import { pack, offsetVector } from './helpers/tools';
|
|
3
|
+
import DocumentContext from './DocumentContext';
|
|
4
|
+
import { EventEmitter } from 'events';
|
|
5
|
+
import { distributeLine } from './thai';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A line/vector writer, which adds elements to current page and sets
|
|
9
|
+
* their positions based on the context
|
|
10
|
+
*/
|
|
11
|
+
class ElementWriter extends EventEmitter {
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {DocumentContext} context
|
|
15
|
+
*/
|
|
16
|
+
constructor(context) {
|
|
17
|
+
super();
|
|
18
|
+
this._context = context;
|
|
19
|
+
this.contextStack = [];
|
|
20
|
+
this.verticalAlignmentStack = [];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @returns {DocumentContext}
|
|
25
|
+
*/
|
|
26
|
+
context() {
|
|
27
|
+
return this._context;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
addLine(line, dontUpdateContextPosition, index) {
|
|
31
|
+
let height = line.getHeight();
|
|
32
|
+
let context = this.context();
|
|
33
|
+
let page = context.getCurrentPage();
|
|
34
|
+
let position = this.getCurrentPositionOnPage();
|
|
35
|
+
|
|
36
|
+
if (context.availableHeight < height || !page) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
line.x = context.x + (line.x || 0);
|
|
41
|
+
line.y = context.y + (line.y || 0);
|
|
42
|
+
|
|
43
|
+
this.alignLine(line);
|
|
44
|
+
|
|
45
|
+
this.addPageItem(page, {
|
|
46
|
+
type: 'line',
|
|
47
|
+
item: line
|
|
48
|
+
}, index);
|
|
49
|
+
this.emit('lineAdded', line);
|
|
50
|
+
|
|
51
|
+
if (!dontUpdateContextPosition) {
|
|
52
|
+
context.moveDown(height);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return position;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
alignLine(line) {
|
|
59
|
+
let width = this.context().availableWidth;
|
|
60
|
+
let lineWidth = line.getWidth();
|
|
61
|
+
|
|
62
|
+
let alignment = line.inlines && line.inlines.length > 0 && line.inlines[0].alignment;
|
|
63
|
+
|
|
64
|
+
let offset = 0;
|
|
65
|
+
switch (alignment) {
|
|
66
|
+
case 'right':
|
|
67
|
+
offset = width - lineWidth;
|
|
68
|
+
break;
|
|
69
|
+
case 'center':
|
|
70
|
+
offset = (width - lineWidth) / 2;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (offset) {
|
|
75
|
+
line.x = (line.x || 0) + offset;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (alignment === 'distribute') {
|
|
79
|
+
distributeLine(line, width);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (alignment === 'justify' &&
|
|
83
|
+
!line.newLineForced &&
|
|
84
|
+
!line.lastLineInParagraph &&
|
|
85
|
+
line.inlines.length > 1) {
|
|
86
|
+
// Whitespace trimmed from the line start is not a word gap (leadingCut includes -leadingIndent)
|
|
87
|
+
let firstInline = line.inlines[0];
|
|
88
|
+
let isFirstInlineTrimmed = line.leadingCut + (firstInline.leadingIndent || 0) > 0 && firstInline.text.trim() === '';
|
|
89
|
+
let hasGapAfter = i => !line.inlines[i].noNewLine && !(i === 0 && isFirstInlineTrimmed);
|
|
90
|
+
|
|
91
|
+
let gaps = 0;
|
|
92
|
+
for (let i = 0, l = line.inlines.length - 1; i < l; i++) {
|
|
93
|
+
if (hasGapAfter(i)) {
|
|
94
|
+
gaps++;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
let additionalSpacing = gaps > 0 ? (width - lineWidth) / gaps : 0;
|
|
98
|
+
|
|
99
|
+
for (let i = 1, l = line.inlines.length; i < l; i++) {
|
|
100
|
+
let justifyShift = hasGapAfter(i - 1) ? additionalSpacing : 0;
|
|
101
|
+
offset += justifyShift;
|
|
102
|
+
|
|
103
|
+
line.inlines[i].x += offset;
|
|
104
|
+
line.inlines[i].justifyShift = justifyShift;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
addImage(image, index) {
|
|
110
|
+
let context = this.context();
|
|
111
|
+
let page = context.getCurrentPage();
|
|
112
|
+
let position = this.getCurrentPositionOnPage();
|
|
113
|
+
|
|
114
|
+
if (!page || (image.absolutePosition === undefined && context.availableHeight < image._height && page.items.length > 0)) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (image._x === undefined) {
|
|
119
|
+
image._x = image.x || 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
image.x = context.x + image._x;
|
|
123
|
+
image.y = context.y;
|
|
124
|
+
|
|
125
|
+
this.alignImage(image);
|
|
126
|
+
|
|
127
|
+
this.addPageItem(page, {
|
|
128
|
+
type: 'image',
|
|
129
|
+
item: image
|
|
130
|
+
}, index);
|
|
131
|
+
|
|
132
|
+
context.moveDown(image._height);
|
|
133
|
+
|
|
134
|
+
return position;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
addCanvas(node, index) {
|
|
138
|
+
let context = this.context();
|
|
139
|
+
let page = context.getCurrentPage();
|
|
140
|
+
let positions = [];
|
|
141
|
+
let height = node._minHeight;
|
|
142
|
+
|
|
143
|
+
if (!page || (node.absolutePosition === undefined && context.availableHeight < height)) {
|
|
144
|
+
// TODO: support for canvas larger than a page
|
|
145
|
+
// TODO: support for other overflow methods
|
|
146
|
+
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
node = pack(node);
|
|
151
|
+
node.canvas = node.canvas.map(vector => {
|
|
152
|
+
let copy = pack(vector);
|
|
153
|
+
if (copy.type === 'polyline') {
|
|
154
|
+
copy.points = copy.points.map(point => pack(point));
|
|
155
|
+
}
|
|
156
|
+
return copy;
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
this.alignCanvas(node);
|
|
160
|
+
|
|
161
|
+
node.canvas.forEach(function (vector) {
|
|
162
|
+
let position = this.addVector(vector, false, false, index);
|
|
163
|
+
positions.push(position);
|
|
164
|
+
if (index !== undefined) {
|
|
165
|
+
index++;
|
|
166
|
+
}
|
|
167
|
+
}, this);
|
|
168
|
+
|
|
169
|
+
context.moveDown(height);
|
|
170
|
+
|
|
171
|
+
return positions;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
addSVG(image, index) {
|
|
175
|
+
// TODO: same as addImage
|
|
176
|
+
let context = this.context();
|
|
177
|
+
let page = context.getCurrentPage();
|
|
178
|
+
let position = this.getCurrentPositionOnPage();
|
|
179
|
+
|
|
180
|
+
if (!page || (image.absolutePosition === undefined && context.availableHeight < image._height && page.items.length > 0)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (image._x === undefined) {
|
|
185
|
+
image._x = image.x || 0;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
image.x = context.x + image._x;
|
|
189
|
+
image.y = context.y;
|
|
190
|
+
|
|
191
|
+
this.alignImage(image);
|
|
192
|
+
|
|
193
|
+
this.addPageItem(page, {
|
|
194
|
+
type: 'svg',
|
|
195
|
+
item: pack(image)
|
|
196
|
+
}, index);
|
|
197
|
+
|
|
198
|
+
context.moveDown(image._height);
|
|
199
|
+
|
|
200
|
+
return position;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
addQr(qr, index) {
|
|
204
|
+
let context = this.context();
|
|
205
|
+
let page = context.getCurrentPage();
|
|
206
|
+
let position = this.getCurrentPositionOnPage();
|
|
207
|
+
|
|
208
|
+
if (!page || (qr.absolutePosition === undefined && context.availableHeight < qr._height)) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (qr._x === undefined) {
|
|
213
|
+
qr._x = qr.x || 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
qr.x = context.x + qr._x;
|
|
217
|
+
qr.y = context.y;
|
|
218
|
+
|
|
219
|
+
this.alignImage(qr);
|
|
220
|
+
|
|
221
|
+
for (let i = 0, l = qr._canvas.length; i < l; i++) {
|
|
222
|
+
let vector = qr._canvas[i];
|
|
223
|
+
vector.x += qr.x;
|
|
224
|
+
vector.y += qr.y;
|
|
225
|
+
this.addVector(vector, true, true, index);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
context.moveDown(qr._height);
|
|
229
|
+
|
|
230
|
+
return position;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
addAttachment(attachment, index) {
|
|
234
|
+
let context = this.context();
|
|
235
|
+
let page = context.getCurrentPage();
|
|
236
|
+
let position = this.getCurrentPositionOnPage();
|
|
237
|
+
|
|
238
|
+
if (!page || (attachment.absolutePosition === undefined && context.availableHeight < attachment._height && page.items.length > 0)) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (attachment._x === undefined) {
|
|
243
|
+
attachment._x = attachment.x || 0;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
attachment.x = context.x + attachment._x;
|
|
247
|
+
attachment.y = context.y;
|
|
248
|
+
|
|
249
|
+
this.addPageItem(page, {
|
|
250
|
+
type: 'attachment',
|
|
251
|
+
item: attachment
|
|
252
|
+
}, index);
|
|
253
|
+
|
|
254
|
+
context.moveDown(attachment._height);
|
|
255
|
+
|
|
256
|
+
return position;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
alignImage(image) {
|
|
260
|
+
let width = this.context().availableWidth;
|
|
261
|
+
let imageWidth = image._minWidth;
|
|
262
|
+
let offset = 0;
|
|
263
|
+
switch (image._alignment) {
|
|
264
|
+
case 'right':
|
|
265
|
+
offset = width - imageWidth;
|
|
266
|
+
break;
|
|
267
|
+
case 'center':
|
|
268
|
+
offset = (width - imageWidth) / 2;
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (offset) {
|
|
273
|
+
image.x = (image.x || 0) + offset;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
alignCanvas(node) {
|
|
278
|
+
let width = this.context().availableWidth;
|
|
279
|
+
let canvasWidth = node._minWidth;
|
|
280
|
+
let offset = 0;
|
|
281
|
+
switch (node._alignment) {
|
|
282
|
+
case 'right':
|
|
283
|
+
offset = width - canvasWidth;
|
|
284
|
+
break;
|
|
285
|
+
case 'center':
|
|
286
|
+
offset = (width - canvasWidth) / 2;
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
if (offset) {
|
|
290
|
+
node.canvas.forEach(vector => {
|
|
291
|
+
offsetVector(vector, offset, 0);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
addVector(vector, ignoreContextX, ignoreContextY, index, forcePage) {
|
|
297
|
+
let context = this.context();
|
|
298
|
+
let page = context.getCurrentPage();
|
|
299
|
+
if (isNumber(forcePage)) {
|
|
300
|
+
page = context.pages[forcePage];
|
|
301
|
+
}
|
|
302
|
+
let position = this.getCurrentPositionOnPage();
|
|
303
|
+
|
|
304
|
+
if (page) {
|
|
305
|
+
offsetVector(vector, ignoreContextX ? 0 : context.x, ignoreContextY ? 0 : context.y);
|
|
306
|
+
this.addPageItem(page, {
|
|
307
|
+
type: 'vector',
|
|
308
|
+
item: vector
|
|
309
|
+
}, index);
|
|
310
|
+
return position;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
beginClip(width, height) {
|
|
315
|
+
let ctx = this.context();
|
|
316
|
+
let page = ctx.getCurrentPage();
|
|
317
|
+
this.addPageItem(page, {
|
|
318
|
+
type: 'beginClip',
|
|
319
|
+
item: { x: ctx.x, y: ctx.y, width: width, height: height }
|
|
320
|
+
});
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
endClip() {
|
|
325
|
+
let ctx = this.context();
|
|
326
|
+
let page = ctx.getCurrentPage();
|
|
327
|
+
this.addPageItem(page, {
|
|
328
|
+
type: 'endClip'
|
|
329
|
+
});
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
beginVerticalAlignment(verticalAlignment) {
|
|
334
|
+
let page = this.context().getCurrentPage();
|
|
335
|
+
let item = {
|
|
336
|
+
type: 'beginVerticalAlignment',
|
|
337
|
+
item: { verticalAlignment: verticalAlignment }
|
|
338
|
+
};
|
|
339
|
+
this.addPageItem(page, item);
|
|
340
|
+
this.verticalAlignmentStack.push({ item: item, page: page });
|
|
341
|
+
return item;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
endVerticalAlignment(verticalAlignment) {
|
|
345
|
+
let page = this.context().getCurrentPage();
|
|
346
|
+
let item = {
|
|
347
|
+
type: 'endVerticalAlignment',
|
|
348
|
+
item: { verticalAlignment: verticalAlignment }
|
|
349
|
+
};
|
|
350
|
+
this.addPageItem(page, item);
|
|
351
|
+
this.verticalAlignmentStack.pop();
|
|
352
|
+
return item;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Returns the begin item of the innermost vertical alignment block
|
|
357
|
+
* if it is still open and was started on the current page.
|
|
358
|
+
*
|
|
359
|
+
* @returns {object|null}
|
|
360
|
+
*/
|
|
361
|
+
getOpenVerticalAlignment() {
|
|
362
|
+
let verticalAlignment = this.verticalAlignmentStack[this.verticalAlignmentStack.length - 1];
|
|
363
|
+
if (verticalAlignment && verticalAlignment.page === this.context().getCurrentPage()) {
|
|
364
|
+
return verticalAlignment.item;
|
|
365
|
+
}
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Returns the index on the current page where cell backgrounds are inserted.
|
|
371
|
+
* Inside an open vertical alignment block, backgrounds are inserted right after
|
|
372
|
+
* its begin item, so they are moved together with the aligned content.
|
|
373
|
+
*
|
|
374
|
+
* @returns {number}
|
|
375
|
+
*/
|
|
376
|
+
getBackgroundInsertIndex() {
|
|
377
|
+
let ctx = this.context();
|
|
378
|
+
let verticalAlignment = this.getOpenVerticalAlignment();
|
|
379
|
+
if (verticalAlignment) {
|
|
380
|
+
return ctx.getCurrentPage().items.indexOf(verticalAlignment) + 1;
|
|
381
|
+
}
|
|
382
|
+
return ctx.backgroundLength[ctx.page];
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
addFragment(block, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition) {
|
|
386
|
+
let ctx = this.context();
|
|
387
|
+
let page = ctx.getCurrentPage();
|
|
388
|
+
|
|
389
|
+
if (!useBlockXOffset && block.height > ctx.availableHeight) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// Positions are shared by leaf nodes and their containers. Update each
|
|
394
|
+
// recorded position once, after the fragment has found its destination.
|
|
395
|
+
(block.positions || []).forEach(position => {
|
|
396
|
+
let left = position.left + (position._isAbsolutePosition ? 0 : useBlockXOffset ? (block.xOffset || 0) : ctx.x);
|
|
397
|
+
let top = position.top + (position._isAbsolutePosition ? 0 : useBlockYOffset ? (block.yOffset || 0) : ctx.y);
|
|
398
|
+
let pagePosition = ctx.getCurrentPosition();
|
|
399
|
+
Object.assign(position, pagePosition, {
|
|
400
|
+
left,
|
|
401
|
+
top,
|
|
402
|
+
horizontalRatio: (left - ctx.pageMargins.left) / pagePosition.pageInnerWidth,
|
|
403
|
+
verticalRatio: (top - ctx.pageMargins.top) / pagePosition.pageInnerHeight
|
|
404
|
+
});
|
|
405
|
+
if (ctx.isAbsolutePosition) {
|
|
406
|
+
position._isAbsolutePosition = true;
|
|
407
|
+
}
|
|
408
|
+
if (this.contextStack.length > 0) {
|
|
409
|
+
page.positions = page.positions || [];
|
|
410
|
+
page.positions.push(position);
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
let unbreakableFillsInserted = 0;
|
|
415
|
+
|
|
416
|
+
block.items.forEach(item => {
|
|
417
|
+
const xOffset = item._isAbsolutePosition ? 0 : useBlockXOffset ? (block.xOffset || 0) : ctx.x;
|
|
418
|
+
const yOffset = item._isAbsolutePosition ? 0 : useBlockYOffset ? (block.yOffset || 0) : ctx.y;
|
|
419
|
+
switch (item.type) {
|
|
420
|
+
case 'line':
|
|
421
|
+
var l = item.item.clone();
|
|
422
|
+
|
|
423
|
+
l.x = (l.x || 0) + xOffset;
|
|
424
|
+
l.y = (l.y || 0) + yOffset;
|
|
425
|
+
|
|
426
|
+
this.addPageItem(page, {
|
|
427
|
+
type: 'line',
|
|
428
|
+
item: l
|
|
429
|
+
}, undefined, item._isAbsolutePosition || ctx.isAbsolutePosition);
|
|
430
|
+
break;
|
|
431
|
+
|
|
432
|
+
case 'vector':
|
|
433
|
+
var v = pack(item.item);
|
|
434
|
+
if (v.type === 'polyline') {
|
|
435
|
+
v.points = v.points.map(point => pack(point));
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
offsetVector(v, xOffset, yOffset);
|
|
439
|
+
if (v._isFillColorFromUnbreakable) {
|
|
440
|
+
// If the item is a fillColor from an unbreakable block
|
|
441
|
+
// We have to add it at the beginning of the items body array of the page
|
|
442
|
+
// (or of the open vertical alignment block, see getBackgroundInsertIndex),
|
|
443
|
+
// after the fills already inserted from this block so that they keep their draw order
|
|
444
|
+
delete v._isFillColorFromUnbreakable;
|
|
445
|
+
const endOfBackgroundItemsIndex = this.getBackgroundInsertIndex() + unbreakableFillsInserted;
|
|
446
|
+
unbreakableFillsInserted++;
|
|
447
|
+
this.addPageItem(page, {
|
|
448
|
+
type: 'vector',
|
|
449
|
+
item: v
|
|
450
|
+
}, endOfBackgroundItemsIndex, item._isAbsolutePosition || ctx.isAbsolutePosition);
|
|
451
|
+
} else {
|
|
452
|
+
this.addPageItem(page, {
|
|
453
|
+
type: 'vector',
|
|
454
|
+
item: v
|
|
455
|
+
}, undefined, item._isAbsolutePosition || ctx.isAbsolutePosition);
|
|
456
|
+
}
|
|
457
|
+
break;
|
|
458
|
+
|
|
459
|
+
case 'image':
|
|
460
|
+
case 'svg':
|
|
461
|
+
case 'attachment':
|
|
462
|
+
case 'beginClip':
|
|
463
|
+
case 'endClip':
|
|
464
|
+
case 'beginVerticalAlignment':
|
|
465
|
+
case 'endVerticalAlignment':
|
|
466
|
+
var img = pack(item.item);
|
|
467
|
+
|
|
468
|
+
img.x = (img.x || 0) + xOffset;
|
|
469
|
+
img.y = (img.y || 0) + yOffset;
|
|
470
|
+
|
|
471
|
+
this.addPageItem(page, {
|
|
472
|
+
type: item.type,
|
|
473
|
+
item: img
|
|
474
|
+
}, undefined, item._isAbsolutePosition || ctx.isAbsolutePosition);
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
if (!dontUpdateContextPosition) {
|
|
480
|
+
ctx.moveDown(block.height);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return true;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Pushes the provided context onto the stack or creates a new one
|
|
488
|
+
*
|
|
489
|
+
* pushContext(context) - pushes the provided context and makes it current
|
|
490
|
+
* pushContext(width, height) - creates and pushes a new context with the specified width and height
|
|
491
|
+
* pushContext() - creates a new context for unbreakable blocks (with current availableWidth and full-page-height)
|
|
492
|
+
*
|
|
493
|
+
* @param {DocumentContext|number} contextOrWidth
|
|
494
|
+
* @param {number} height
|
|
495
|
+
*/
|
|
496
|
+
pushContext(contextOrWidth, height) {
|
|
497
|
+
let absolutePositionContext;
|
|
498
|
+
if (contextOrWidth === undefined) {
|
|
499
|
+
// Ordinary fragments retain the page dimensions for absolute content.
|
|
500
|
+
// Explicitly sized contexts (headers, footers, backgrounds) keep their own.
|
|
501
|
+
absolutePositionContext = this.context().absolutePositionContext || this.context();
|
|
502
|
+
height = this.context().getCurrentPage().pageSize.height - this.context().pageMargins.top - this.context().pageMargins.bottom;
|
|
503
|
+
contextOrWidth = this.context().availableWidth;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if (isNumber(contextOrWidth)) {
|
|
507
|
+
let width = contextOrWidth;
|
|
508
|
+
contextOrWidth = new DocumentContext();
|
|
509
|
+
contextOrWidth.addPage({ width: width, height: height }, { left: 0, right: 0, top: 0, bottom: 0 });
|
|
510
|
+
contextOrWidth.absolutePositionContext = absolutePositionContext;
|
|
511
|
+
// Ordinary fragments remain in the same detached flow as their parent.
|
|
512
|
+
contextOrWidth.detachedBlock = absolutePositionContext ? this.context().detachedBlock : undefined;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
this.contextStack.push(this.context());
|
|
516
|
+
this._context = contextOrWidth;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
popContext() {
|
|
520
|
+
this._context = this.contextStack.pop();
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
getCurrentPositionOnPage() {
|
|
524
|
+
let context = this.context();
|
|
525
|
+
let position = context.getCurrentPosition();
|
|
526
|
+
if (context.isAbsolutePosition) {
|
|
527
|
+
position._isAbsolutePosition = true;
|
|
528
|
+
}
|
|
529
|
+
if (this.contextStack.length > 0) {
|
|
530
|
+
let page = context.getCurrentPage();
|
|
531
|
+
page.positions = page.positions || [];
|
|
532
|
+
page.positions.push(position);
|
|
533
|
+
}
|
|
534
|
+
return position;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
addPageItem(page, item, index, isAbsolutePosition = this.context().isAbsolutePosition) {
|
|
538
|
+
// keep the flag on this occurrence, not on a reusable input node or vector
|
|
539
|
+
if (isAbsolutePosition) {
|
|
540
|
+
item._isAbsolutePosition = true;
|
|
541
|
+
}
|
|
542
|
+
if (index === null || index === undefined || index < 0 || index > page.items.length) {
|
|
543
|
+
page.items.push(item);
|
|
544
|
+
} else {
|
|
545
|
+
page.items.splice(index, 0, item);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export default ElementWriter;
|