pdfmake 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE +21 -21
  3. package/README.md +75 -78
  4. package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
  5. package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
  6. package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  7. package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
  8. package/build/fonts/Roboto.js +27 -0
  9. package/build/pdfmake.js +64813 -64584
  10. package/build/pdfmake.js.map +1 -1
  11. package/build/pdfmake.min.js +2 -2
  12. package/build/pdfmake.min.js.map +1 -1
  13. package/build/standard-fonts/Courier.js +27 -27
  14. package/build/standard-fonts/Helvetica.js +27 -27
  15. package/build/standard-fonts/Symbol.js +21 -21
  16. package/build/standard-fonts/Times.js +27 -27
  17. package/build/standard-fonts/ZapfDingbats.js +21 -21
  18. package/build/vfs_fonts.js +5 -5
  19. package/build-vfs.js +44 -44
  20. package/fonts/Roboto.js +8 -8
  21. package/js/3rd-party/svg-to-pdfkit/source.js +1 -1
  22. package/js/DocMeasure.js +11 -6
  23. package/js/DocumentContext.js +8 -3
  24. package/js/ElementWriter.js +42 -16
  25. package/js/LayoutBuilder.js +144 -78
  26. package/js/Line.js +16 -16
  27. package/js/OutputDocument.js +10 -10
  28. package/js/OutputDocumentServer.js +3 -3
  29. package/js/PDFDocument.js +3 -3
  30. package/js/PageElementWriter.js +15 -9
  31. package/js/Printer.js +28 -28
  32. package/js/Renderer.js +40 -8
  33. package/js/SVGMeasure.js +10 -10
  34. package/js/StyleContextStack.js +74 -51
  35. package/js/TableProcessor.js +14 -0
  36. package/js/TextBreaker.js +17 -17
  37. package/js/TextDecorator.js +12 -3
  38. package/js/TextInlines.js +34 -33
  39. package/js/base.js +4 -4
  40. package/js/browser-extensions/OutputDocumentBrowser.js +24 -24
  41. package/js/columnCalculator.js +2 -2
  42. package/js/helpers/node.js +47 -23
  43. package/js/helpers/variableType.js +18 -18
  44. package/js/qrEnc.js +38 -38
  45. package/js/virtual-fs.js +11 -11
  46. package/package.json +12 -12
  47. package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -9
  48. package/src/3rd-party/svg-to-pdfkit/source.js +2745 -2745
  49. package/src/3rd-party/svg-to-pdfkit.js +3 -3
  50. package/src/DocMeasure.js +745 -738
  51. package/src/DocPreprocessor.js +283 -283
  52. package/src/DocumentContext.js +345 -338
  53. package/src/ElementWriter.js +441 -417
  54. package/src/LayoutBuilder.js +1336 -1258
  55. package/src/Line.js +114 -114
  56. package/src/OutputDocument.js +64 -64
  57. package/src/OutputDocumentServer.js +32 -32
  58. package/src/PDFDocument.js +174 -174
  59. package/src/PageElementWriter.js +187 -179
  60. package/src/PageSize.js +53 -53
  61. package/src/Printer.js +306 -306
  62. package/src/Renderer.js +445 -409
  63. package/src/SVGMeasure.js +109 -109
  64. package/src/StyleContextStack.js +208 -179
  65. package/src/TableProcessor.js +620 -602
  66. package/src/TextBreaker.js +168 -168
  67. package/src/TextDecorator.js +175 -161
  68. package/src/TextInlines.js +224 -223
  69. package/src/URLResolver.js +43 -43
  70. package/src/base.js +70 -70
  71. package/src/browser-extensions/OutputDocumentBrowser.js +80 -80
  72. package/src/browser-extensions/fonts/Roboto.js +27 -27
  73. package/src/browser-extensions/index.js +55 -55
  74. package/src/browser-extensions/pdfMake.js +1 -1
  75. package/src/browser-extensions/standard-fonts/Courier.js +27 -27
  76. package/src/browser-extensions/standard-fonts/Helvetica.js +27 -27
  77. package/src/browser-extensions/standard-fonts/Symbol.js +21 -21
  78. package/src/browser-extensions/standard-fonts/Times.js +27 -27
  79. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -21
  80. package/src/browser-extensions/virtual-fs-cjs.js +1 -1
  81. package/src/columnCalculator.js +154 -154
  82. package/src/helpers/node.js +134 -110
  83. package/src/helpers/tools.js +44 -44
  84. package/src/helpers/variableType.js +50 -50
  85. package/src/index.js +16 -16
  86. package/src/qrEnc.js +796 -796
  87. package/src/standardPageSizes.js +52 -52
  88. package/src/tableLayouts.js +100 -100
  89. package/src/virtual-fs.js +66 -66
  90. package/standard-fonts/Courier.js +8 -8
  91. package/standard-fonts/Helvetica.js +8 -8
  92. package/standard-fonts/Symbol.js +5 -5
  93. package/standard-fonts/Times.js +8 -8
  94. package/standard-fonts/ZapfDingbats.js +5 -5
@@ -1,417 +1,441 @@
1
- import { isNumber } from './helpers/variableType';
2
- import { pack, offsetVector } from './helpers/tools';
3
- import DocumentContext from './DocumentContext';
4
- import { EventEmitter } from 'events';
5
-
6
- /**
7
- * A line/vector writer, which adds elements to current page and sets
8
- * their positions based on the context
9
- */
10
- class ElementWriter extends EventEmitter {
11
-
12
- /**
13
- * @param {DocumentContext} context
14
- */
15
- constructor(context) {
16
- super();
17
- this._context = context;
18
- this.contextStack = [];
19
- }
20
-
21
- /**
22
- * @returns {DocumentContext}
23
- */
24
- context() {
25
- return this._context;
26
- }
27
-
28
- addLine(line, dontUpdateContextPosition, index) {
29
- let height = line.getHeight();
30
- let context = this.context();
31
- let page = context.getCurrentPage();
32
- let position = this.getCurrentPositionOnPage();
33
-
34
- if (context.availableHeight < height || !page) {
35
- return false;
36
- }
37
-
38
- line.x = context.x + (line.x || 0);
39
- line.y = context.y + (line.y || 0);
40
-
41
- this.alignLine(line);
42
-
43
- addPageItem(page, {
44
- type: 'line',
45
- item: line
46
- }, index);
47
- this.emit('lineAdded', line);
48
-
49
- if (!dontUpdateContextPosition) {
50
- context.moveDown(height);
51
- }
52
-
53
- return position;
54
- }
55
-
56
- alignLine(line) {
57
- let width = this.context().availableWidth;
58
- let lineWidth = line.getWidth();
59
-
60
- let alignment = line.inlines && line.inlines.length > 0 && line.inlines[0].alignment;
61
-
62
- let offset = 0;
63
- switch (alignment) {
64
- case 'right':
65
- offset = width - lineWidth;
66
- break;
67
- case 'center':
68
- offset = (width - lineWidth) / 2;
69
- break;
70
- }
71
-
72
- if (offset) {
73
- line.x = (line.x || 0) + offset;
74
- }
75
-
76
- if (alignment === 'justify' &&
77
- !line.newLineForced &&
78
- !line.lastLineInParagraph &&
79
- line.inlines.length > 1) {
80
- let additionalSpacing = (width - lineWidth) / (line.inlines.length - 1);
81
-
82
- for (let i = 1, l = line.inlines.length; i < l; i++) {
83
- offset = i * additionalSpacing;
84
-
85
- line.inlines[i].x += offset;
86
- line.inlines[i].justifyShift = additionalSpacing;
87
- }
88
- }
89
- }
90
-
91
- addImage(image, index) {
92
- let context = this.context();
93
- let page = context.getCurrentPage();
94
- let position = this.getCurrentPositionOnPage();
95
-
96
- if (!page || (image.absolutePosition === undefined && context.availableHeight < image._height && page.items.length > 0)) {
97
- return false;
98
- }
99
-
100
- if (image._x === undefined) {
101
- image._x = image.x || 0;
102
- }
103
-
104
- image.x = context.x + image._x;
105
- image.y = context.y;
106
-
107
- this.alignImage(image);
108
-
109
- addPageItem(page, {
110
- type: 'image',
111
- item: image
112
- }, index);
113
-
114
- context.moveDown(image._height);
115
-
116
- return position;
117
- }
118
-
119
- addCanvas(node, index) {
120
- let context = this.context();
121
- let page = context.getCurrentPage();
122
- let positions = [];
123
- let height = node._minHeight;
124
-
125
- if (!page || (node.absolutePosition === undefined && context.availableHeight < height)) {
126
- // TODO: support for canvas larger than a page
127
- // TODO: support for other overflow methods
128
-
129
- return false;
130
- }
131
-
132
- this.alignCanvas(node);
133
-
134
- node.canvas.forEach(function (vector) {
135
- let position = this.addVector(vector, false, false, index);
136
- positions.push(position);
137
- if (index !== undefined) {
138
- index++;
139
- }
140
- }, this);
141
-
142
- context.moveDown(height);
143
-
144
- return positions;
145
- }
146
-
147
- addSVG(image, index) {
148
- // TODO: same as addImage
149
- let context = this.context();
150
- let page = context.getCurrentPage();
151
- let position = this.getCurrentPositionOnPage();
152
-
153
- if (!page || (image.absolutePosition === undefined && context.availableHeight < image._height && page.items.length > 0)) {
154
- return false;
155
- }
156
-
157
- if (image._x === undefined) {
158
- image._x = image.x || 0;
159
- }
160
-
161
- image.x = context.x + image._x;
162
- image.y = context.y;
163
-
164
- this.alignImage(image);
165
-
166
- addPageItem(page, {
167
- type: 'svg',
168
- item: image
169
- }, index);
170
-
171
- context.moveDown(image._height);
172
-
173
- return position;
174
- }
175
-
176
- addQr(qr, index) {
177
- let context = this.context();
178
- let page = context.getCurrentPage();
179
- let position = this.getCurrentPositionOnPage();
180
-
181
- if (!page || (qr.absolutePosition === undefined && context.availableHeight < qr._height)) {
182
- return false;
183
- }
184
-
185
- if (qr._x === undefined) {
186
- qr._x = qr.x || 0;
187
- }
188
-
189
- qr.x = context.x + qr._x;
190
- qr.y = context.y;
191
-
192
- this.alignImage(qr);
193
-
194
- for (let i = 0, l = qr._canvas.length; i < l; i++) {
195
- let vector = qr._canvas[i];
196
- vector.x += qr.x;
197
- vector.y += qr.y;
198
- this.addVector(vector, true, true, index);
199
- }
200
-
201
- context.moveDown(qr._height);
202
-
203
- return position;
204
- }
205
-
206
- addAttachment(attachment, index) {
207
- let context = this.context();
208
- let page = context.getCurrentPage();
209
- let position = this.getCurrentPositionOnPage();
210
-
211
- if (!page || (attachment.absolutePosition === undefined && context.availableHeight < attachment._height && page.items.length > 0)) {
212
- return false;
213
- }
214
-
215
- if (attachment._x === undefined) {
216
- attachment._x = attachment.x || 0;
217
- }
218
-
219
- attachment.x = context.x + attachment._x;
220
- attachment.y = context.y;
221
-
222
- addPageItem(page, {
223
- type: 'attachment',
224
- item: attachment
225
- }, index);
226
-
227
- context.moveDown(attachment._height);
228
-
229
- return position;
230
- }
231
-
232
- alignImage(image) {
233
- let width = this.context().availableWidth;
234
- let imageWidth = image._minWidth;
235
- let offset = 0;
236
- switch (image._alignment) {
237
- case 'right':
238
- offset = width - imageWidth;
239
- break;
240
- case 'center':
241
- offset = (width - imageWidth) / 2;
242
- break;
243
- }
244
-
245
- if (offset) {
246
- image.x = (image.x || 0) + offset;
247
- }
248
- }
249
-
250
- alignCanvas(node) {
251
- let width = this.context().availableWidth;
252
- let canvasWidth = node._minWidth;
253
- let offset = 0;
254
- switch (node._alignment) {
255
- case 'right':
256
- offset = width - canvasWidth;
257
- break;
258
- case 'center':
259
- offset = (width - canvasWidth) / 2;
260
- break;
261
- }
262
- if (offset) {
263
- node.canvas.forEach(vector => {
264
- offsetVector(vector, offset, 0);
265
- });
266
- }
267
- }
268
-
269
- addVector(vector, ignoreContextX, ignoreContextY, index, forcePage) {
270
- let context = this.context();
271
- let page = context.getCurrentPage();
272
- if (isNumber(forcePage)) {
273
- page = context.pages[forcePage];
274
- }
275
- let position = this.getCurrentPositionOnPage();
276
-
277
- if (page) {
278
- offsetVector(vector, ignoreContextX ? 0 : context.x, ignoreContextY ? 0 : context.y);
279
- addPageItem(page, {
280
- type: 'vector',
281
- item: vector
282
- }, index);
283
- return position;
284
- }
285
- }
286
-
287
- beginClip(width, height) {
288
- let ctx = this.context();
289
- let page = ctx.getCurrentPage();
290
- page.items.push({
291
- type: 'beginClip',
292
- item: { x: ctx.x, y: ctx.y, width: width, height: height }
293
- });
294
- return true;
295
- }
296
-
297
- endClip() {
298
- let ctx = this.context();
299
- let page = ctx.getCurrentPage();
300
- page.items.push({
301
- type: 'endClip'
302
- });
303
- return true;
304
- }
305
-
306
- addFragment(block, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition) {
307
- let ctx = this.context();
308
- let page = ctx.getCurrentPage();
309
-
310
- if (!useBlockXOffset && block.height > ctx.availableHeight) {
311
- return false;
312
- }
313
-
314
- block.items.forEach(item => {
315
- switch (item.type) {
316
- case 'line':
317
- var l = item.item.clone();
318
-
319
- if (l._node) {
320
- l._node.positions[0].pageNumber = ctx.page + 1;
321
- }
322
- l.x = (l.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
323
- l.y = (l.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
324
-
325
- page.items.push({
326
- type: 'line',
327
- item: l
328
- });
329
- break;
330
-
331
- case 'vector':
332
- var v = pack(item.item);
333
-
334
- offsetVector(v, useBlockXOffset ? (block.xOffset || 0) : ctx.x, useBlockYOffset ? (block.yOffset || 0) : ctx.y);
335
- if (v._isFillColorFromUnbreakable) {
336
- // If the item is a fillColor from an unbreakable block
337
- // We have to add it at the beginning of the items body array of the page
338
- delete v._isFillColorFromUnbreakable;
339
- const endOfBackgroundItemsIndex = ctx.backgroundLength[ctx.page];
340
- page.items.splice(endOfBackgroundItemsIndex, 0, {
341
- type: 'vector',
342
- item: v
343
- });
344
- } else {
345
- page.items.push({
346
- type: 'vector',
347
- item: v
348
- });
349
- }
350
- break;
351
-
352
- case 'image':
353
- case 'svg':
354
- var img = pack(item.item);
355
-
356
- img.x = (img.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
357
- img.y = (img.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
358
-
359
- page.items.push({
360
- type: item.type,
361
- item: img
362
- });
363
- break;
364
- }
365
- });
366
-
367
- if (!dontUpdateContextPosition) {
368
- ctx.moveDown(block.height);
369
- }
370
-
371
- return true;
372
- }
373
-
374
- /**
375
- * Pushes the provided context onto the stack or creates a new one
376
- *
377
- * pushContext(context) - pushes the provided context and makes it current
378
- * pushContext(width, height) - creates and pushes a new context with the specified width and height
379
- * pushContext() - creates a new context for unbreakable blocks (with current availableWidth and full-page-height)
380
- *
381
- * @param {DocumentContext|number} contextOrWidth
382
- * @param {number} height
383
- */
384
- pushContext(contextOrWidth, height) {
385
- if (contextOrWidth === undefined) {
386
- height = this.context().getCurrentPage().height - this.context().pageMargins.top - this.context().pageMargins.bottom;
387
- contextOrWidth = this.context().availableWidth;
388
- }
389
-
390
- if (isNumber(contextOrWidth)) {
391
- let width = contextOrWidth;
392
- contextOrWidth = new DocumentContext();
393
- contextOrWidth.addPage({ width: width, height: height }, { left: 0, right: 0, top: 0, bottom: 0 });
394
- }
395
-
396
- this.contextStack.push(this.context());
397
- this._context = contextOrWidth;
398
- }
399
-
400
- popContext() {
401
- this._context = this.contextStack.pop();
402
- }
403
-
404
- getCurrentPositionOnPage() {
405
- return (this.contextStack[0] || this.context()).getCurrentPosition();
406
- }
407
- }
408
-
409
- function addPageItem(page, item, index) {
410
- if (index === null || index === undefined || index < 0 || index > page.items.length) {
411
- page.items.push(item);
412
- } else {
413
- page.items.splice(index, 0, item);
414
- }
415
- }
416
-
417
- export default ElementWriter;
1
+ import { isNumber } from './helpers/variableType';
2
+ import { pack, offsetVector } from './helpers/tools';
3
+ import DocumentContext from './DocumentContext';
4
+ import { EventEmitter } from 'events';
5
+
6
+ /**
7
+ * A line/vector writer, which adds elements to current page and sets
8
+ * their positions based on the context
9
+ */
10
+ class ElementWriter extends EventEmitter {
11
+
12
+ /**
13
+ * @param {DocumentContext} context
14
+ */
15
+ constructor(context) {
16
+ super();
17
+ this._context = context;
18
+ this.contextStack = [];
19
+ }
20
+
21
+ /**
22
+ * @returns {DocumentContext}
23
+ */
24
+ context() {
25
+ return this._context;
26
+ }
27
+
28
+ addLine(line, dontUpdateContextPosition, index) {
29
+ let height = line.getHeight();
30
+ let context = this.context();
31
+ let page = context.getCurrentPage();
32
+ let position = this.getCurrentPositionOnPage();
33
+
34
+ if (context.availableHeight < height || !page) {
35
+ return false;
36
+ }
37
+
38
+ line.x = context.x + (line.x || 0);
39
+ line.y = context.y + (line.y || 0);
40
+
41
+ this.alignLine(line);
42
+
43
+ addPageItem(page, {
44
+ type: 'line',
45
+ item: line
46
+ }, index);
47
+ this.emit('lineAdded', line);
48
+
49
+ if (!dontUpdateContextPosition) {
50
+ context.moveDown(height);
51
+ }
52
+
53
+ return position;
54
+ }
55
+
56
+ alignLine(line) {
57
+ let width = this.context().availableWidth;
58
+ let lineWidth = line.getWidth();
59
+
60
+ let alignment = line.inlines && line.inlines.length > 0 && line.inlines[0].alignment;
61
+
62
+ let offset = 0;
63
+ switch (alignment) {
64
+ case 'right':
65
+ offset = width - lineWidth;
66
+ break;
67
+ case 'center':
68
+ offset = (width - lineWidth) / 2;
69
+ break;
70
+ }
71
+
72
+ if (offset) {
73
+ line.x = (line.x || 0) + offset;
74
+ }
75
+
76
+ if (alignment === 'justify' &&
77
+ !line.newLineForced &&
78
+ !line.lastLineInParagraph &&
79
+ line.inlines.length > 1) {
80
+ let additionalSpacing = (width - lineWidth) / (line.inlines.length - 1);
81
+
82
+ for (let i = 1, l = line.inlines.length; i < l; i++) {
83
+ offset = i * additionalSpacing;
84
+
85
+ line.inlines[i].x += offset;
86
+ line.inlines[i].justifyShift = additionalSpacing;
87
+ }
88
+ }
89
+ }
90
+
91
+ addImage(image, index) {
92
+ let context = this.context();
93
+ let page = context.getCurrentPage();
94
+ let position = this.getCurrentPositionOnPage();
95
+
96
+ if (!page || (image.absolutePosition === undefined && context.availableHeight < image._height && page.items.length > 0)) {
97
+ return false;
98
+ }
99
+
100
+ if (image._x === undefined) {
101
+ image._x = image.x || 0;
102
+ }
103
+
104
+ image.x = context.x + image._x;
105
+ image.y = context.y;
106
+
107
+ this.alignImage(image);
108
+
109
+ addPageItem(page, {
110
+ type: 'image',
111
+ item: image
112
+ }, index);
113
+
114
+ context.moveDown(image._height);
115
+
116
+ return position;
117
+ }
118
+
119
+ addCanvas(node, index) {
120
+ let context = this.context();
121
+ let page = context.getCurrentPage();
122
+ let positions = [];
123
+ let height = node._minHeight;
124
+
125
+ if (!page || (node.absolutePosition === undefined && context.availableHeight < height)) {
126
+ // TODO: support for canvas larger than a page
127
+ // TODO: support for other overflow methods
128
+
129
+ return false;
130
+ }
131
+
132
+ this.alignCanvas(node);
133
+
134
+ node.canvas.forEach(function (vector) {
135
+ let position = this.addVector(vector, false, false, index);
136
+ positions.push(position);
137
+ if (index !== undefined) {
138
+ index++;
139
+ }
140
+ }, this);
141
+
142
+ context.moveDown(height);
143
+
144
+ return positions;
145
+ }
146
+
147
+ addSVG(image, index) {
148
+ // TODO: same as addImage
149
+ let context = this.context();
150
+ let page = context.getCurrentPage();
151
+ let position = this.getCurrentPositionOnPage();
152
+
153
+ if (!page || (image.absolutePosition === undefined && context.availableHeight < image._height && page.items.length > 0)) {
154
+ return false;
155
+ }
156
+
157
+ if (image._x === undefined) {
158
+ image._x = image.x || 0;
159
+ }
160
+
161
+ image.x = context.x + image._x;
162
+ image.y = context.y;
163
+
164
+ this.alignImage(image);
165
+
166
+ addPageItem(page, {
167
+ type: 'svg',
168
+ item: image
169
+ }, index);
170
+
171
+ context.moveDown(image._height);
172
+
173
+ return position;
174
+ }
175
+
176
+ addQr(qr, index) {
177
+ let context = this.context();
178
+ let page = context.getCurrentPage();
179
+ let position = this.getCurrentPositionOnPage();
180
+
181
+ if (!page || (qr.absolutePosition === undefined && context.availableHeight < qr._height)) {
182
+ return false;
183
+ }
184
+
185
+ if (qr._x === undefined) {
186
+ qr._x = qr.x || 0;
187
+ }
188
+
189
+ qr.x = context.x + qr._x;
190
+ qr.y = context.y;
191
+
192
+ this.alignImage(qr);
193
+
194
+ for (let i = 0, l = qr._canvas.length; i < l; i++) {
195
+ let vector = qr._canvas[i];
196
+ vector.x += qr.x;
197
+ vector.y += qr.y;
198
+ this.addVector(vector, true, true, index);
199
+ }
200
+
201
+ context.moveDown(qr._height);
202
+
203
+ return position;
204
+ }
205
+
206
+ addAttachment(attachment, index) {
207
+ let context = this.context();
208
+ let page = context.getCurrentPage();
209
+ let position = this.getCurrentPositionOnPage();
210
+
211
+ if (!page || (attachment.absolutePosition === undefined && context.availableHeight < attachment._height && page.items.length > 0)) {
212
+ return false;
213
+ }
214
+
215
+ if (attachment._x === undefined) {
216
+ attachment._x = attachment.x || 0;
217
+ }
218
+
219
+ attachment.x = context.x + attachment._x;
220
+ attachment.y = context.y;
221
+
222
+ addPageItem(page, {
223
+ type: 'attachment',
224
+ item: attachment
225
+ }, index);
226
+
227
+ context.moveDown(attachment._height);
228
+
229
+ return position;
230
+ }
231
+
232
+ alignImage(image) {
233
+ let width = this.context().availableWidth;
234
+ let imageWidth = image._minWidth;
235
+ let offset = 0;
236
+ switch (image._alignment) {
237
+ case 'right':
238
+ offset = width - imageWidth;
239
+ break;
240
+ case 'center':
241
+ offset = (width - imageWidth) / 2;
242
+ break;
243
+ }
244
+
245
+ if (offset) {
246
+ image.x = (image.x || 0) + offset;
247
+ }
248
+ }
249
+
250
+ alignCanvas(node) {
251
+ let width = this.context().availableWidth;
252
+ let canvasWidth = node._minWidth;
253
+ let offset = 0;
254
+ switch (node._alignment) {
255
+ case 'right':
256
+ offset = width - canvasWidth;
257
+ break;
258
+ case 'center':
259
+ offset = (width - canvasWidth) / 2;
260
+ break;
261
+ }
262
+ if (offset) {
263
+ node.canvas.forEach(vector => {
264
+ offsetVector(vector, offset, 0);
265
+ });
266
+ }
267
+ }
268
+
269
+ addVector(vector, ignoreContextX, ignoreContextY, index, forcePage) {
270
+ let context = this.context();
271
+ let page = context.getCurrentPage();
272
+ if (isNumber(forcePage)) {
273
+ page = context.pages[forcePage];
274
+ }
275
+ let position = this.getCurrentPositionOnPage();
276
+
277
+ if (page) {
278
+ offsetVector(vector, ignoreContextX ? 0 : context.x, ignoreContextY ? 0 : context.y);
279
+ addPageItem(page, {
280
+ type: 'vector',
281
+ item: vector
282
+ }, index);
283
+ return position;
284
+ }
285
+ }
286
+
287
+ beginClip(width, height) {
288
+ let ctx = this.context();
289
+ let page = ctx.getCurrentPage();
290
+ page.items.push({
291
+ type: 'beginClip',
292
+ item: { x: ctx.x, y: ctx.y, width: width, height: height }
293
+ });
294
+ return true;
295
+ }
296
+
297
+ endClip() {
298
+ let ctx = this.context();
299
+ let page = ctx.getCurrentPage();
300
+ page.items.push({
301
+ type: 'endClip'
302
+ });
303
+ return true;
304
+ }
305
+
306
+ beginVerticalAlignment(verticalAlignment) {
307
+ let page = this.context().getCurrentPage();
308
+ let item = {
309
+ type: 'beginVerticalAlignment',
310
+ item: { verticalAlignment: verticalAlignment }
311
+ };
312
+ page.items.push(item);
313
+ return item;
314
+ }
315
+
316
+ endVerticalAlignment(verticalAlignment) {
317
+ let page = this.context().getCurrentPage();
318
+ let item = {
319
+ type: 'endVerticalAlignment',
320
+ item: { verticalAlignment: verticalAlignment }
321
+ };
322
+ page.items.push(item);
323
+ return item;
324
+ }
325
+
326
+ addFragment(block, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition) {
327
+ let ctx = this.context();
328
+ let page = ctx.getCurrentPage();
329
+
330
+ if (!useBlockXOffset && block.height > ctx.availableHeight) {
331
+ return false;
332
+ }
333
+
334
+ block.items.forEach(item => {
335
+ switch (item.type) {
336
+ case 'line':
337
+ var l = item.item.clone();
338
+
339
+ if (l._node) {
340
+ l._node.positions[0].pageNumber = ctx.page + 1;
341
+ }
342
+ l.x = (l.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
343
+ l.y = (l.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
344
+
345
+ page.items.push({
346
+ type: 'line',
347
+ item: l
348
+ });
349
+ break;
350
+
351
+ case 'vector':
352
+ var v = pack(item.item);
353
+
354
+ offsetVector(v, useBlockXOffset ? (block.xOffset || 0) : ctx.x, useBlockYOffset ? (block.yOffset || 0) : ctx.y);
355
+ if (v._isFillColorFromUnbreakable) {
356
+ // If the item is a fillColor from an unbreakable block
357
+ // We have to add it at the beginning of the items body array of the page
358
+ delete v._isFillColorFromUnbreakable;
359
+ const endOfBackgroundItemsIndex = ctx.backgroundLength[ctx.page];
360
+ page.items.splice(endOfBackgroundItemsIndex, 0, {
361
+ type: 'vector',
362
+ item: v
363
+ });
364
+ } else {
365
+ page.items.push({
366
+ type: 'vector',
367
+ item: v
368
+ });
369
+ }
370
+ break;
371
+
372
+ case 'image':
373
+ case 'svg':
374
+ case 'beginClip':
375
+ case 'endClip':
376
+ case 'beginVerticalAlignment':
377
+ case 'endVerticalAlignment':
378
+ var img = pack(item.item);
379
+
380
+ img.x = (img.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
381
+ img.y = (img.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
382
+
383
+ page.items.push({
384
+ type: item.type,
385
+ item: img
386
+ });
387
+ break;
388
+ }
389
+ });
390
+
391
+ if (!dontUpdateContextPosition) {
392
+ ctx.moveDown(block.height);
393
+ }
394
+
395
+ return true;
396
+ }
397
+
398
+ /**
399
+ * Pushes the provided context onto the stack or creates a new one
400
+ *
401
+ * pushContext(context) - pushes the provided context and makes it current
402
+ * pushContext(width, height) - creates and pushes a new context with the specified width and height
403
+ * pushContext() - creates a new context for unbreakable blocks (with current availableWidth and full-page-height)
404
+ *
405
+ * @param {DocumentContext|number} contextOrWidth
406
+ * @param {number} height
407
+ */
408
+ pushContext(contextOrWidth, height) {
409
+ if (contextOrWidth === undefined) {
410
+ height = this.context().getCurrentPage().height - this.context().pageMargins.top - this.context().pageMargins.bottom;
411
+ contextOrWidth = this.context().availableWidth;
412
+ }
413
+
414
+ if (isNumber(contextOrWidth)) {
415
+ let width = contextOrWidth;
416
+ contextOrWidth = new DocumentContext();
417
+ contextOrWidth.addPage({ width: width, height: height }, { left: 0, right: 0, top: 0, bottom: 0 });
418
+ }
419
+
420
+ this.contextStack.push(this.context());
421
+ this._context = contextOrWidth;
422
+ }
423
+
424
+ popContext() {
425
+ this._context = this.contextStack.pop();
426
+ }
427
+
428
+ getCurrentPositionOnPage() {
429
+ return (this.contextStack[0] || this.context()).getCurrentPosition();
430
+ }
431
+ }
432
+
433
+ function addPageItem(page, item, index) {
434
+ if (index === null || index === undefined || index < 0 || index > page.items.length) {
435
+ page.items.push(item);
436
+ } else {
437
+ page.items.splice(index, 0, item);
438
+ }
439
+ }
440
+
441
+ export default ElementWriter;