@flowaccount/pdfmake 1.0.5 → 1.0.6-staging.3

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.
@@ -1,434 +1,442 @@
1
- 'use strict';
2
-
3
- var Line = require('./line');
4
- var isNumber = require('./helpers').isNumber;
5
- var pack = require('./helpers').pack;
6
- var offsetVector = require('./helpers').offsetVector;
7
- var DocumentContext = require('./documentContext');
8
-
9
- /**
10
- * Creates an instance of ElementWriter - a line/vector writer, which adds
11
- * elements to current page and sets their positions based on the context
12
- */
13
- function ElementWriter(context, tracker) {
14
- this.context = context;
15
- this.contextStack = [];
16
- this.tracker = tracker;
17
- }
18
-
19
- function addPageItem(page, item, index) {
20
- if (index === null || index === undefined || index < 0 || index > page.items.length) {
21
- page.items.push(item);
22
- } else {
23
- page.items.splice(index, 0, item);
24
- }
25
- }
26
-
27
- ElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, index) {
28
- var height = line.getHeight();
29
- var context = this.context;
30
- var page = context.getCurrentPage(),
31
- position = this.getCurrentPositionOnPage();
32
-
33
- if (context.availableHeight < height || !page) {
34
- return false;
35
- }
36
-
37
- line.x = context.x + (line.x || 0);
38
- line.y = context.y + (line.y || 0);
39
-
40
- this.alignLine(line);
41
-
42
- addPageItem(page, {
43
- type: 'line',
44
- item: line
45
- }, index);
46
- this.tracker.emit('lineAdded', line);
47
-
48
- if (!dontUpdateContextPosition) {
49
- context.moveDown(height);
50
- }
51
-
52
- return position;
53
- };
54
-
55
- ElementWriter.prototype.alignLine = function (line) {
56
- var width = this.context.availableWidth;
57
- var lineWidth = line.getWidth();
58
-
59
- var alignment = line.inlines && line.inlines.length > 0 && line.inlines[0].alignment;
60
-
61
- var offset = 0;
62
- switch (alignment) {
63
- case 'right':
64
- offset = width - lineWidth;
65
- break;
66
- case 'center':
67
- offset = (width - lineWidth) / 2;
68
- break;
69
- }
70
-
71
- if (offset) {
72
- line.x = (line.x || 0) + offset;
73
- }
74
-
75
- if (alignment === 'justify' &&
76
- !line.newLineForced &&
77
- !line.lastLineInParagraph &&
78
- line.inlines.length > 1) {
79
- var additionalSpacing = (width - lineWidth) / (line.inlines.length - 1);
80
-
81
- for (var i = 1, l = line.inlines.length; i < l; i++) {
82
- offset = i * additionalSpacing;
83
-
84
- line.inlines[i].x += offset;
85
- line.inlines[i].justifyShift = additionalSpacing;
86
- }
87
- }
88
- };
89
-
90
- ElementWriter.prototype.addImage = function (image, index, type) {
91
- var context = this.context;
92
- var page = context.getCurrentPage(),
93
- position = this.getCurrentPositionOnPage();
94
-
95
- if (!page || (image.absolutePosition === undefined && context.availableHeight < image._height && page.items.length > 0)) {
96
- return false;
97
- }
98
-
99
- if (image._x === undefined) {
100
- image._x = image.x || 0;
101
- }
102
-
103
- image.x = context.x + image._x;
104
- image.y = context.y;
105
-
106
- this.alignImage(image);
107
-
108
- addPageItem(page, {
109
- type: type || 'image',
110
- item: image
111
- }, index);
112
-
113
- context.moveDown(image._height);
114
-
115
- return position;
116
- };
117
-
118
- ElementWriter.prototype.addSVG = function (image, index) {
119
- return this.addImage(image, index, 'svg');
120
- };
121
-
122
- ElementWriter.prototype.addQr = function (qr, index) {
123
- var context = this.context;
124
- var page = context.getCurrentPage(),
125
- position = this.getCurrentPositionOnPage();
126
-
127
- if (!page || (qr.absolutePosition === undefined && context.availableHeight < qr._height)) {
128
- return false;
129
- }
130
-
131
- if (qr._x === undefined) {
132
- qr._x = qr.x || 0;
133
- }
134
-
135
- qr.x = context.x + qr._x;
136
- qr.y = context.y;
137
-
138
- this.alignImage(qr);
139
-
140
- for (var i = 0, l = qr._canvas.length; i < l; i++) {
141
- var vector = qr._canvas[i];
142
- vector.x += qr.x;
143
- vector.y += qr.y;
144
- this.addVector(vector, true, true, index);
145
- }
146
-
147
- context.moveDown(qr._height);
148
-
149
- return position;
150
- };
151
-
152
- ElementWriter.prototype.alignImage = function (image) {
153
- var width = this.context.availableWidth;
154
- var imageWidth = image._minWidth;
155
- var offset = 0;
156
- switch (image._alignment) {
157
- case 'right':
158
- offset = width - imageWidth;
159
- break;
160
- case 'center':
161
- offset = (width - imageWidth) / 2;
162
- break;
163
- }
164
-
165
- if (offset) {
166
- image.x = (image.x || 0) + offset;
167
- }
168
- };
169
-
170
- ElementWriter.prototype.alignCanvas = function (node) {
171
- var width = this.context.availableWidth;
172
- var canvasWidth = node._minWidth;
173
- var offset = 0;
174
- switch (node._alignment) {
175
- case 'right':
176
- offset = width - canvasWidth;
177
- break;
178
- case 'center':
179
- offset = (width - canvasWidth) / 2;
180
- break;
181
- }
182
- if (offset) {
183
- node.canvas.forEach(function (vector) {
184
- offsetVector(vector, offset, 0);
185
- });
186
- }
187
- };
188
-
189
- ElementWriter.prototype.addVector = function (vector, ignoreContextX, ignoreContextY, index, forcePage) {
190
- var context = this.context;
191
- var page = context.getCurrentPage();
192
- if (isNumber(forcePage)) {
193
- page = context.pages[forcePage];
194
- }
195
- var position = this.getCurrentPositionOnPage();
196
-
197
- if (page) {
198
- offsetVector(vector, ignoreContextX ? 0 : context.x, ignoreContextY ? 0 : context.y);
199
- addPageItem(page, {
200
- type: 'vector',
201
- item: vector
202
- }, index);
203
- return position;
204
- }
205
- };
206
-
207
- ElementWriter.prototype.beginClip = function (width, height) {
208
- var ctx = this.context;
209
- var page = ctx.getCurrentPage();
210
- page.items.push({
211
- type: 'beginClip',
212
- item: { x: ctx.x, y: ctx.y, width: width, height: height }
213
- });
214
- return true;
215
- };
216
-
217
- ElementWriter.prototype.endClip = function () {
218
- var ctx = this.context;
219
- var page = ctx.getCurrentPage();
220
- page.items.push({
221
- type: 'endClip'
222
- });
223
- return true;
224
- };
225
-
226
- function cloneLine(line) {
227
- var result = new Line(line.maxWidth);
228
-
229
- for (var key in line) {
230
- if (line.hasOwnProperty(key)) {
231
- result[key] = line[key];
232
- }
233
- }
234
-
235
- return result;
236
- }
237
-
238
- ElementWriter.prototype.addFragment = function (block, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition, isFooter) {
239
- var ctx = this.context;
240
- var page = ctx.getCurrentPage();
241
-
242
- if (!useBlockXOffset && block.height > ctx.availableHeight) {
243
- return false;
244
- }
245
-
246
- if (isFooter && block._footerGapOption && block._footerGapOption.enabled) {
247
- var gapHeight = ctx.availableHeight - block.height;
248
- if (gapHeight > 0) {
249
- var gapTopY = ctx.y;
250
- var colSpec = block._footerGapOption.columns || null;
251
- if (colSpec) {
252
- var rawWidths = colSpec.content.vLines || [];
253
-
254
- if (rawWidths && rawWidths.length > 1) {
255
- var span = rawWidths[rawWidths.length - 1];
256
- if (span <= 0) span = 1;
257
- var scale = ctx.availableWidth / span;
258
- var style = (colSpec.style || {});
259
- var lw = style.lineWidth != null ? style.lineWidth : 0.5;
260
- var lc = style.color || '#000000';
261
- var dashCfg = style.dash;
262
- var includeOuter = colSpec.includeOuter !== false;
263
- var startIndex = includeOuter ? 0 : 1;
264
- var endIndex = includeOuter ? rawWidths.length : rawWidths.length - 1;
265
-
266
- for (var ci = startIndex; ci < endIndex; ci++) {
267
- var xGuide = ctx.x + rawWidths[ci] - 0.25;
268
- page.items.push({
269
- type: 'vector',
270
- item: {
271
- type: 'line',
272
- x1: xGuide,
273
- y1: gapTopY,
274
- x2: xGuide,
275
- y2: gapTopY + gapHeight,
276
- lineWidth: lw,
277
- lineColor: lc,
278
- dash: dashCfg ? {
279
- length: dashCfg.length,
280
- space: dashCfg.space != null ? dashCfg.space : dashCfg.gap
281
- } : undefined,
282
- _footerGuideLine: true
283
- }
284
- });
285
- }
286
- }
287
- }
288
- ctx.moveDown(gapHeight);
289
- }
290
- } else if(isFooter){
291
- ctx.moveDown(ctx.availableHeight - block.height);
292
- }
293
-
294
- block.items.forEach(function (item) {
295
- switch (item.type) {
296
- case 'line':
297
- var l = cloneLine(item.item);
298
-
299
- if (l._node) {
300
- l._node.positions[0].pageNumber = ctx.page + 1;
301
- }
302
- l.x = (l.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
303
- l.y = (l.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
304
-
305
- page.items.push({
306
- type: 'line',
307
- item: l
308
- });
309
- break;
310
-
311
- case 'vector':
312
- var v = pack(item.item);
313
-
314
- offsetVector(v, useBlockXOffset ? (block.xOffset || 0) : ctx.x, useBlockYOffset ? (block.yOffset || 0) : ctx.y);
315
- if (v._isFillColorFromUnbreakable) {
316
- // If the item is a fillColor from an unbreakable block
317
- // We have to add it at the beginning of the items body array of the page
318
- delete v._isFillColorFromUnbreakable;
319
- const endOfBackgroundItemsIndex = ctx.backgroundLength[ctx.page];
320
- page.items.splice(endOfBackgroundItemsIndex, 0, {
321
- type: 'vector',
322
- item: v
323
- });
324
- } else {
325
- page.items.push({
326
- type: 'vector',
327
- item: v
328
- });
329
- }
330
- break;
331
-
332
- case 'image':
333
- case 'svg':
334
- case 'beginVerticalAlign':
335
- case 'endVerticalAlign':
336
- case 'beginClip':
337
- case 'endClip':
338
- var it = pack(item.item);
339
- it.x = (it.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
340
- it.y = (it.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
341
-
342
- page.items.push({
343
- type: item.type,
344
- item: it
345
- });
346
- break;
347
- }
348
- });
349
-
350
- if (!dontUpdateContextPosition) {
351
- ctx.moveDown(block.height);
352
- }
353
-
354
- return true;
355
- };
356
-
357
- /**
358
- * Pushes the provided context onto the stack or creates a new one
359
- *
360
- * pushContext(context) - pushes the provided context and makes it current
361
- * pushContext(width, height) - creates and pushes a new context with the specified width and height
362
- * pushContext() - creates a new context for unbreakable blocks (with current availableWidth and full-page-height)
363
- */
364
- ElementWriter.prototype.pushContext = function (contextOrWidth, height) {
365
- if (contextOrWidth === undefined) {
366
- height = this.context.getCurrentPage().height - this.context.pageMargins.top - this.context.pageMargins.bottom;
367
- contextOrWidth = this.context.availableWidth;
368
- }
369
-
370
- if (isNumber(contextOrWidth)) {
371
- if (this.context._footerGapOption) {
372
- contextOrWidth = new DocumentContext({width: contextOrWidth, height: height}, {left: 0, right: 0, top: 0, bottom: 0}, this.context._footerGapOption);
373
- } else {
374
- contextOrWidth = new DocumentContext({ width: contextOrWidth, height: height }, { left: 0, right: 0, top: 0, bottom: 0 });
375
- }
376
- }
377
-
378
- this.contextStack.push(this.context);
379
- this.context = contextOrWidth;
380
- };
381
-
382
- ElementWriter.prototype.popContext = function () {
383
- this.context = this.contextStack.pop();
384
- };
385
-
386
- ElementWriter.prototype.getCurrentPositionOnPage = function () {
387
- return (this.contextStack[0] || this.context).getCurrentPosition();
388
- };
389
-
390
- ElementWriter.prototype.beginClip = function (width, height) {
391
- var ctx = this.context;
392
- var page = ctx.getCurrentPage();
393
- var item = {
394
- type: 'beginClip',
395
- item: { x: ctx.x, y: ctx.y, width: width, height: height }
396
- };
397
- page.items.push(item);
398
- return item;
399
- };
400
-
401
- ElementWriter.prototype.endClip = function () {
402
- var ctx = this.context;
403
- var page = ctx.getCurrentPage();
404
- var item = {
405
- type: 'endClip'
406
- };
407
- page.items.push(item);
408
- return item;
409
- };
410
-
411
- ElementWriter.prototype.beginVerticalAlign = function (verticalAlign) {
412
- var ctx = this.context;
413
- var page = ctx.getCurrentPage();
414
- var item = {
415
- type: 'beginVerticalAlign',
416
- item: { verticalAlign: verticalAlign }
417
- };
418
- page.items.push(item);
419
- return item;
420
- };
421
-
422
- ElementWriter.prototype.endVerticalAlign = function (verticalAlign) {
423
- var ctx = this.context;
424
- var page = ctx.getCurrentPage();
425
- var item = {
426
- type: 'endVerticalAlign',
427
- item: { verticalAlign: verticalAlign }
428
- };
429
- page.items.push(item);
430
- return item;
431
- };
432
-
433
-
434
- module.exports = ElementWriter;
1
+ 'use strict';
2
+
3
+ var Line = require('./line');
4
+ var isNumber = require('./helpers').isNumber;
5
+ var pack = require('./helpers').pack;
6
+ var offsetVector = require('./helpers').offsetVector;
7
+ var DocumentContext = require('./documentContext');
8
+
9
+ /**
10
+ * Creates an instance of ElementWriter - a line/vector writer, which adds
11
+ * elements to current page and sets their positions based on the context
12
+ */
13
+ function ElementWriter(context, tracker) {
14
+ this.context = context;
15
+ this.contextStack = [];
16
+ this.tracker = tracker;
17
+ }
18
+
19
+ function addPageItem(page, item, index) {
20
+ if (index === null || index === undefined || index < 0 || index > page.items.length) {
21
+ page.items.push(item);
22
+ } else {
23
+ page.items.splice(index, 0, item);
24
+ }
25
+ }
26
+
27
+ ElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, index) {
28
+ var height = line.getHeight();
29
+ var context = this.context;
30
+ var page = context.getCurrentPage(),
31
+ position = this.getCurrentPositionOnPage();
32
+
33
+ if (context.availableHeight < height || !page) {
34
+ return false;
35
+ }
36
+
37
+ line.x = context.x + (line.x || 0);
38
+ line.y = context.y + (line.y || 0);
39
+
40
+ this.alignLine(line);
41
+
42
+ addPageItem(page, {
43
+ type: 'line',
44
+ item: line
45
+ }, index);
46
+ this.tracker.emit('lineAdded', line);
47
+
48
+ if (!dontUpdateContextPosition) {
49
+ context.moveDown(height);
50
+ }
51
+
52
+ return position;
53
+ };
54
+
55
+ ElementWriter.prototype.alignLine = function (line) {
56
+ var width = this.context.availableWidth;
57
+ var lineWidth = line.getWidth();
58
+
59
+ var alignment = line.inlines && line.inlines.length > 0 && line.inlines[0].alignment;
60
+
61
+ var offset = 0;
62
+ switch (alignment) {
63
+ case 'right':
64
+ offset = width - lineWidth;
65
+ break;
66
+ case 'center':
67
+ offset = (width - lineWidth) / 2;
68
+ break;
69
+ }
70
+
71
+ if (offset) {
72
+ line.x = (line.x || 0) + offset;
73
+ }
74
+
75
+ if (alignment === 'justify' &&
76
+ !line.newLineForced &&
77
+ !line.lastLineInParagraph &&
78
+ line.inlines.length > 1) {
79
+ var additionalSpacing = (width - lineWidth) / (line.inlines.length - 1);
80
+
81
+ for (var i = 1, l = line.inlines.length; i < l; i++) {
82
+ offset = i * additionalSpacing;
83
+
84
+ line.inlines[i].x += offset;
85
+ line.inlines[i].justifyShift = additionalSpacing;
86
+ }
87
+ }
88
+ };
89
+
90
+ ElementWriter.prototype.addImage = function (image, index, type) {
91
+ var context = this.context;
92
+ var page = context.getCurrentPage(),
93
+ position = this.getCurrentPositionOnPage();
94
+
95
+ if (!page || (image.absolutePosition === undefined && context.availableHeight < image._height && page.items.length > 0)) {
96
+ return false;
97
+ }
98
+
99
+ if (image._x === undefined) {
100
+ image._x = image.x || 0;
101
+ }
102
+
103
+ image.x = context.x + image._x;
104
+ image.y = context.y;
105
+
106
+ this.alignImage(image);
107
+
108
+ addPageItem(page, {
109
+ type: type || 'image',
110
+ item: image
111
+ }, index);
112
+
113
+ context.moveDown(image._height);
114
+
115
+ return position;
116
+ };
117
+
118
+ ElementWriter.prototype.addSVG = function (image, index) {
119
+ return this.addImage(image, index, 'svg');
120
+ };
121
+
122
+ ElementWriter.prototype.addQr = function (qr, index) {
123
+ var context = this.context;
124
+ var page = context.getCurrentPage(),
125
+ position = this.getCurrentPositionOnPage();
126
+
127
+ if (!page || (qr.absolutePosition === undefined && context.availableHeight < qr._height)) {
128
+ return false;
129
+ }
130
+
131
+ if (qr._x === undefined) {
132
+ qr._x = qr.x || 0;
133
+ }
134
+
135
+ qr.x = context.x + qr._x;
136
+ qr.y = context.y;
137
+
138
+ this.alignImage(qr);
139
+
140
+ for (var i = 0, l = qr._canvas.length; i < l; i++) {
141
+ var vector = qr._canvas[i];
142
+ vector.x += qr.x;
143
+ vector.y += qr.y;
144
+ this.addVector(vector, true, true, index);
145
+ }
146
+
147
+ context.moveDown(qr._height);
148
+
149
+ return position;
150
+ };
151
+
152
+ ElementWriter.prototype.alignImage = function (image) {
153
+ var width = this.context.availableWidth;
154
+ var imageWidth = image._minWidth;
155
+ var offset = 0;
156
+ switch (image._alignment) {
157
+ case 'right':
158
+ offset = width - imageWidth;
159
+ break;
160
+ case 'center':
161
+ offset = (width - imageWidth) / 2;
162
+ break;
163
+ }
164
+
165
+ if (offset) {
166
+ image.x = (image.x || 0) + offset;
167
+ }
168
+ };
169
+
170
+ ElementWriter.prototype.alignCanvas = function (node) {
171
+ var width = this.context.availableWidth;
172
+ var canvasWidth = node._minWidth;
173
+ var offset = 0;
174
+ switch (node._alignment) {
175
+ case 'right':
176
+ offset = width - canvasWidth;
177
+ break;
178
+ case 'center':
179
+ offset = (width - canvasWidth) / 2;
180
+ break;
181
+ }
182
+ if (offset) {
183
+ node.canvas.forEach(function (vector) {
184
+ offsetVector(vector, offset, 0);
185
+ });
186
+ }
187
+ };
188
+
189
+ ElementWriter.prototype.addVector = function (vector, ignoreContextX, ignoreContextY, index, forcePage) {
190
+ var context = this.context;
191
+ var page = context.getCurrentPage();
192
+ if (isNumber(forcePage)) {
193
+ page = context.pages[forcePage];
194
+ }
195
+ var position = this.getCurrentPositionOnPage();
196
+
197
+ if (page) {
198
+ offsetVector(vector, ignoreContextX ? 0 : context.x, ignoreContextY ? 0 : context.y);
199
+ addPageItem(page, {
200
+ type: 'vector',
201
+ item: vector
202
+ }, index);
203
+ return position;
204
+ }
205
+ };
206
+
207
+ ElementWriter.prototype.beginClip = function (width, height) {
208
+ var ctx = this.context;
209
+ var page = ctx.getCurrentPage();
210
+ page.items.push({
211
+ type: 'beginClip',
212
+ item: { x: ctx.x, y: ctx.y, width: width, height: height }
213
+ });
214
+ return true;
215
+ };
216
+
217
+ ElementWriter.prototype.endClip = function () {
218
+ var ctx = this.context;
219
+ var page = ctx.getCurrentPage();
220
+ page.items.push({
221
+ type: 'endClip'
222
+ });
223
+ return true;
224
+ };
225
+
226
+ function cloneLine(line) {
227
+ var result = new Line(line.maxWidth);
228
+
229
+ for (var key in line) {
230
+ if (line.hasOwnProperty(key)) {
231
+ result[key] = line[key];
232
+ }
233
+ }
234
+
235
+ return result;
236
+ }
237
+
238
+ ElementWriter.prototype.addFragment = function (block, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition, isFooter) {
239
+ var ctx = this.context;
240
+ var page = ctx.getCurrentPage();
241
+
242
+ if (!useBlockXOffset && block.height > ctx.availableHeight) {
243
+ return false;
244
+ }
245
+
246
+ // draw line for inline image template
247
+ if (isFooter && block._footerGapOption && block._footerGapOption.enabled) {
248
+ var gapHeight = ctx.availableHeight - block.height;
249
+ if (gapHeight > 0) {
250
+ var gapTopY = ctx.y;
251
+ var colSpec = block._footerGapOption.columns || null;
252
+ if (colSpec) {
253
+ var rawWidths = colSpec.content.vLines || [];
254
+
255
+ if (rawWidths && rawWidths.length > 1) {
256
+ var span = rawWidths[rawWidths.length - 1];
257
+ if (span <= 0) span = 1;
258
+ var style = (colSpec.style || {});
259
+ var lw = style.lineWidth != null ? style.lineWidth : 0.5;
260
+ var lc = style.color || '#000000';
261
+ var dashCfg = style.dash;
262
+ var includeOuter = colSpec.includeOuter !== false;
263
+ var startIndex = includeOuter ? 0 : 1;
264
+ var endIndex = includeOuter ? rawWidths.length : rawWidths.length - 1;
265
+
266
+ for (var ci = startIndex; ci < endIndex; ci++) {
267
+ var xGuide = ctx.x + rawWidths[ci] - 0.25;
268
+ page.items.push({
269
+ type: 'vector',
270
+ item: {
271
+ type: 'line',
272
+ x1: xGuide,
273
+ y1: gapTopY,
274
+ x2: xGuide,
275
+ y2: gapTopY + gapHeight,
276
+ lineWidth: lw,
277
+ lineColor: lc,
278
+ dash: dashCfg ? {
279
+ length: dashCfg.length,
280
+ space: dashCfg.space != null ? dashCfg.space : dashCfg.gap
281
+ } : undefined,
282
+ _footerGuideLine: true
283
+ }
284
+ });
285
+ }
286
+ }
287
+ }
288
+ ctx.moveDown(gapHeight);
289
+ }
290
+ } else if(isFooter){
291
+ ctx.moveDown(ctx.availableHeight - block.height);
292
+ }
293
+
294
+ // Handle _footerGapOption positioning without isFooter (for breakable footer stacks)
295
+ if (!isFooter && block._footerGapOption && block._footerGapOption.enabled) {
296
+ var breakableGapHeight = ctx.availableHeight - block.height;
297
+ if (breakableGapHeight > 0) {
298
+ ctx.moveDown(breakableGapHeight);
299
+ }
300
+ }
301
+
302
+ block.items.forEach(function (item) {
303
+ switch (item.type) {
304
+ case 'line':
305
+ var l = cloneLine(item.item);
306
+
307
+ if (l._node) {
308
+ l._node.positions[0].pageNumber = ctx.page + 1;
309
+ }
310
+ l.x = (l.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
311
+ l.y = (l.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
312
+
313
+ page.items.push({
314
+ type: 'line',
315
+ item: l
316
+ });
317
+ break;
318
+
319
+ case 'vector':
320
+ var v = pack(item.item);
321
+
322
+ offsetVector(v, useBlockXOffset ? (block.xOffset || 0) : ctx.x, useBlockYOffset ? (block.yOffset || 0) : ctx.y);
323
+ if (v._isFillColorFromUnbreakable) {
324
+ // If the item is a fillColor from an unbreakable block
325
+ // We have to add it at the beginning of the items body array of the page
326
+ delete v._isFillColorFromUnbreakable;
327
+ const endOfBackgroundItemsIndex = ctx.backgroundLength[ctx.page];
328
+ page.items.splice(endOfBackgroundItemsIndex, 0, {
329
+ type: 'vector',
330
+ item: v
331
+ });
332
+ } else {
333
+ page.items.push({
334
+ type: 'vector',
335
+ item: v
336
+ });
337
+ }
338
+ break;
339
+
340
+ case 'image':
341
+ case 'svg':
342
+ case 'beginVerticalAlign':
343
+ case 'endVerticalAlign':
344
+ case 'beginClip':
345
+ case 'endClip':
346
+ var it = pack(item.item);
347
+ it.x = (it.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
348
+ it.y = (it.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
349
+
350
+ page.items.push({
351
+ type: item.type,
352
+ item: it
353
+ });
354
+ break;
355
+ }
356
+ });
357
+
358
+ if (!dontUpdateContextPosition) {
359
+ ctx.moveDown(block.height);
360
+ }
361
+
362
+ return true;
363
+ };
364
+
365
+ /**
366
+ * Pushes the provided context onto the stack or creates a new one
367
+ *
368
+ * pushContext(context) - pushes the provided context and makes it current
369
+ * pushContext(width, height) - creates and pushes a new context with the specified width and height
370
+ * pushContext() - creates a new context for unbreakable blocks (with current availableWidth and full-page-height)
371
+ */
372
+ ElementWriter.prototype.pushContext = function (contextOrWidth, height) {
373
+ if (contextOrWidth === undefined) {
374
+ height = this.context.getCurrentPage().height - this.context.pageMargins.top - this.context.pageMargins.bottom;
375
+ contextOrWidth = this.context.availableWidth;
376
+ }
377
+
378
+ if (isNumber(contextOrWidth)) {
379
+ if (this.context._footerGapOption) {
380
+ contextOrWidth = new DocumentContext({width: contextOrWidth, height: height}, {left: 0, right: 0, top: 0, bottom: 0}, this.context._footerGapOption);
381
+ } else {
382
+ contextOrWidth = new DocumentContext({ width: contextOrWidth, height: height }, { left: 0, right: 0, top: 0, bottom: 0 });
383
+ }
384
+ }
385
+
386
+ this.contextStack.push(this.context);
387
+ this.context = contextOrWidth;
388
+ };
389
+
390
+ ElementWriter.prototype.popContext = function () {
391
+ this.context = this.contextStack.pop();
392
+ };
393
+
394
+ ElementWriter.prototype.getCurrentPositionOnPage = function () {
395
+ return (this.contextStack[0] || this.context).getCurrentPosition();
396
+ };
397
+
398
+ ElementWriter.prototype.beginClip = function (width, height) {
399
+ var ctx = this.context;
400
+ var page = ctx.getCurrentPage();
401
+ var item = {
402
+ type: 'beginClip',
403
+ item: { x: ctx.x, y: ctx.y, width: width, height: height }
404
+ };
405
+ page.items.push(item);
406
+ return item;
407
+ };
408
+
409
+ ElementWriter.prototype.endClip = function () {
410
+ var ctx = this.context;
411
+ var page = ctx.getCurrentPage();
412
+ var item = {
413
+ type: 'endClip'
414
+ };
415
+ page.items.push(item);
416
+ return item;
417
+ };
418
+
419
+ ElementWriter.prototype.beginVerticalAlign = function (verticalAlign) {
420
+ var ctx = this.context;
421
+ var page = ctx.getCurrentPage();
422
+ var item = {
423
+ type: 'beginVerticalAlign',
424
+ item: { verticalAlign: verticalAlign }
425
+ };
426
+ page.items.push(item);
427
+ return item;
428
+ };
429
+
430
+ ElementWriter.prototype.endVerticalAlign = function (verticalAlign) {
431
+ var ctx = this.context;
432
+ var page = ctx.getCurrentPage();
433
+ var item = {
434
+ type: 'endVerticalAlign',
435
+ item: { verticalAlign: verticalAlign }
436
+ };
437
+ page.items.push(item);
438
+ return item;
439
+ };
440
+
441
+
442
+ module.exports = ElementWriter;