@wirabyte/w-pdfmaker 0.0.0-stage → 1.0.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.
Files changed (142) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/LICENSE +22 -0
  3. package/NOTICE.md +39 -0
  4. package/README.md +106 -2
  5. package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
  6. package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
  7. package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  8. package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
  9. package/build/fonts/Roboto.js +27 -0
  10. package/build/pdfmake.js +103161 -0
  11. package/build/pdfmake.js.map +1 -0
  12. package/build/pdfmake.min.js +3 -0
  13. package/build/pdfmake.min.js.map +1 -0
  14. package/build/standard-fonts/Courier.js +9 -0
  15. package/build/standard-fonts/Helvetica.js +9 -0
  16. package/build/standard-fonts/Symbol.js +9 -0
  17. package/build/standard-fonts/Times.js +9 -0
  18. package/build/standard-fonts/ZapfDingbats.js +9 -0
  19. package/build/vfs_fonts.js +6 -0
  20. package/build-vfs.js +44 -0
  21. package/fonts/Roboto/Roboto-Italic.ttf +0 -0
  22. package/fonts/Roboto/Roboto-Medium.ttf +0 -0
  23. package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  24. package/fonts/Roboto/Roboto-Regular.ttf +0 -0
  25. package/fonts/Roboto.js +8 -0
  26. package/fonts/THSarabunIT9/LICENSE.txt +7 -0
  27. package/fonts/THSarabunIT9/THSarabunIT9-Bold.ttf +0 -0
  28. package/fonts/THSarabunIT9/THSarabunIT9-BoldItalic.ttf +0 -0
  29. package/fonts/THSarabunIT9/THSarabunIT9-Italic.ttf +0 -0
  30. package/fonts/THSarabunIT9/THSarabunIT9.ttf +0 -0
  31. package/fonts/THSarabunNew/COPYING.GPLv2 +339 -0
  32. package/fonts/THSarabunNew/LICENSE.txt +13 -0
  33. package/fonts/THSarabunNew/THSarabunNew-Bold.ttf +0 -0
  34. package/fonts/THSarabunNew/THSarabunNew-BoldItalic.ttf +0 -0
  35. package/fonts/THSarabunNew/THSarabunNew-Italic.ttf +0 -0
  36. package/fonts/THSarabunNew/THSarabunNew.ttf +0 -0
  37. package/index.d.ts +167 -0
  38. package/interfaces.d.ts +2779 -0
  39. package/js/DocMeasure.js +695 -0
  40. package/js/DocPreprocessor.js +263 -0
  41. package/js/DocumentContext.js +563 -0
  42. package/js/ElementWriter.js +488 -0
  43. package/js/LayoutBuilder.js +1525 -0
  44. package/js/Line.js +105 -0
  45. package/js/OutputDocument.js +73 -0
  46. package/js/OutputDocumentServer.js +34 -0
  47. package/js/PDFDocument.js +322 -0
  48. package/js/PageElementWriter.js +302 -0
  49. package/js/PageSize.js +74 -0
  50. package/js/Printer.js +275 -0
  51. package/js/Renderer.js +439 -0
  52. package/js/SVGMeasure.js +94 -0
  53. package/js/StyleContextStack.js +191 -0
  54. package/js/TableProcessor.js +658 -0
  55. package/js/TextBreaker.js +164 -0
  56. package/js/TextDecorator.js +164 -0
  57. package/js/TextInlines.js +212 -0
  58. package/js/URLResolver.js +122 -0
  59. package/js/base.js +125 -0
  60. package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
  61. package/js/browser-extensions/fonts/Roboto.js +38 -0
  62. package/js/browser-extensions/fonts/thai.js +28 -0
  63. package/js/browser-extensions/index.js +88 -0
  64. package/js/browser-extensions/pageUrls.js +79 -0
  65. package/js/browser-extensions/pdfMake.js +3 -0
  66. package/js/browser-extensions/standard-fonts/Courier.js +20 -0
  67. package/js/browser-extensions/standard-fonts/Helvetica.js +20 -0
  68. package/js/browser-extensions/standard-fonts/Symbol.js +17 -0
  69. package/js/browser-extensions/standard-fonts/Times.js +20 -0
  70. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
  71. package/js/browser-extensions/virtual-fs-cjs.js +3 -0
  72. package/js/columnCalculator.js +159 -0
  73. package/js/helpers/node.js +122 -0
  74. package/js/helpers/tools.js +46 -0
  75. package/js/helpers/variableType.js +59 -0
  76. package/js/imageFallback.js +23 -0
  77. package/js/index.js +45 -0
  78. package/js/mergeStamp.js +194 -0
  79. package/js/pageNumbering.js +48 -0
  80. package/js/qrEnc.js +721 -0
  81. package/js/standardPageSizes.js +56 -0
  82. package/js/tableLayouts.js +98 -0
  83. package/js/thai.js +201 -0
  84. package/js/virtual-fs.js +82 -0
  85. package/package.json +93 -4
  86. package/skills/w-pdfmaker/SKILL.md +171 -0
  87. package/skills/w-pdfmaker/references/browser.md +36 -0
  88. package/skills/w-pdfmaker/references/document-definition.md +241 -0
  89. package/skills/w-pdfmaker/references/images.md +90 -0
  90. package/skills/w-pdfmaker/references/merge-stamp.md +94 -0
  91. package/skills/w-pdfmaker/references/thai.md +67 -0
  92. package/src/DocMeasure.js +780 -0
  93. package/src/DocPreprocessor.js +293 -0
  94. package/src/DocumentContext.js +605 -0
  95. package/src/ElementWriter.js +550 -0
  96. package/src/LayoutBuilder.js +1689 -0
  97. package/src/Line.js +114 -0
  98. package/src/OutputDocument.js +75 -0
  99. package/src/OutputDocumentServer.js +38 -0
  100. package/src/PDFDocument.js +352 -0
  101. package/src/PageElementWriter.js +343 -0
  102. package/src/PageSize.js +53 -0
  103. package/src/Printer.js +289 -0
  104. package/src/Renderer.js +468 -0
  105. package/src/SVGMeasure.js +109 -0
  106. package/src/StyleContextStack.js +208 -0
  107. package/src/TableProcessor.js +719 -0
  108. package/src/TextBreaker.js +166 -0
  109. package/src/TextDecorator.js +191 -0
  110. package/src/TextInlines.js +229 -0
  111. package/src/URLResolver.js +122 -0
  112. package/src/base.js +143 -0
  113. package/src/browser-extensions/OutputDocumentBrowser.js +80 -0
  114. package/src/browser-extensions/fonts/Roboto.js +27 -0
  115. package/src/browser-extensions/fonts/thai.js +23 -0
  116. package/src/browser-extensions/index.js +91 -0
  117. package/src/browser-extensions/pageUrls.js +67 -0
  118. package/src/browser-extensions/pdfMake.js +1 -0
  119. package/src/browser-extensions/standard-fonts/Courier.js +25 -0
  120. package/src/browser-extensions/standard-fonts/Helvetica.js +25 -0
  121. package/src/browser-extensions/standard-fonts/Symbol.js +17 -0
  122. package/src/browser-extensions/standard-fonts/Times.js +25 -0
  123. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
  124. package/src/browser-extensions/virtual-fs-cjs.js +1 -0
  125. package/src/columnCalculator.js +160 -0
  126. package/src/helpers/node.js +134 -0
  127. package/src/helpers/tools.js +44 -0
  128. package/src/helpers/variableType.js +50 -0
  129. package/src/imageFallback.js +18 -0
  130. package/src/index.js +45 -0
  131. package/src/mergeStamp.js +132 -0
  132. package/src/pageNumbering.js +45 -0
  133. package/src/qrEnc.js +796 -0
  134. package/src/standardPageSizes.js +52 -0
  135. package/src/tableLayouts.js +100 -0
  136. package/src/thai.js +170 -0
  137. package/src/virtual-fs.js +92 -0
  138. package/standard-fonts/Courier.js +8 -0
  139. package/standard-fonts/Helvetica.js +8 -0
  140. package/standard-fonts/Symbol.js +5 -0
  141. package/standard-fonts/Times.js +8 -0
  142. package/standard-fonts/ZapfDingbats.js +5 -0
package/js/Renderer.js ADDED
@@ -0,0 +1,439 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _TextDecorator = _interopRequireDefault(require("./TextDecorator"));
6
+ var _TextInlines = _interopRequireDefault(require("./TextInlines"));
7
+ var _variableType = require("./helpers/variableType");
8
+ var _svgToPdfkit = _interopRequireDefault(require("@pdfmake/svg-to-pdfkit"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ const findFont = (fonts, requiredFonts, defaultFont) => {
11
+ for (let i = 0; i < requiredFonts.length; i++) {
12
+ let requiredFont = requiredFonts[i].toLowerCase();
13
+ for (let font in fonts) {
14
+ if (font.toLowerCase() === requiredFont) {
15
+ return font;
16
+ }
17
+ }
18
+ }
19
+ return defaultFont;
20
+ };
21
+
22
+ /**
23
+ * Shift the "y" height of the text baseline up or down (superscript or subscript,
24
+ * respectively). The exact shift can / should be changed according to standard
25
+ * conventions.
26
+ *
27
+ * @param {number} y
28
+ * @param {object} inline
29
+ * @returns {number}
30
+ */
31
+ const offsetText = (y, inline) => {
32
+ let newY = y;
33
+ if (inline.sup) {
34
+ newY -= inline.fontSize * 0.75;
35
+ }
36
+ if (inline.sub) {
37
+ newY += inline.fontSize * 0.35;
38
+ }
39
+ return newY;
40
+ };
41
+ class Renderer {
42
+ constructor(pdfDocument, progressCallback) {
43
+ this.pdfDocument = pdfDocument;
44
+ this.progressCallback = progressCallback;
45
+ this.outlineMap = [];
46
+ }
47
+ renderPages(pages) {
48
+ this.pdfDocument._pdfMakePages = pages; // TODO: Why?
49
+
50
+ let totalItems = 0;
51
+ if (this.progressCallback) {
52
+ pages.forEach(page => {
53
+ totalItems += page.items.length;
54
+ });
55
+ }
56
+ let renderedItems = 0;
57
+ for (let i = 0; i < pages.length; i++) {
58
+ this.pdfDocument.addPage({
59
+ size: [pages[i].pageSize.width, pages[i].pageSize.height]
60
+ });
61
+ let page = pages[i];
62
+ for (let ii = 0, il = page.items.length; ii < il; ii++) {
63
+ let item = page.items[ii];
64
+ switch (item.type) {
65
+ case 'vector':
66
+ this.renderVector(item.item);
67
+ break;
68
+ case 'line':
69
+ this.renderLine(item.item, item.item.x, item.item.y);
70
+ break;
71
+ case 'image':
72
+ this.renderImage(item.item);
73
+ break;
74
+ case 'svg':
75
+ this.renderSVG(item.item);
76
+ break;
77
+ case 'attachment':
78
+ this.renderAttachment(item.item);
79
+ break;
80
+ case 'beginClip':
81
+ this.beginClip(item.item);
82
+ break;
83
+ case 'endClip':
84
+ this.endClip();
85
+ break;
86
+ case 'beginVerticalAlignment':
87
+ this.beginVerticalAlignment(item.item);
88
+ break;
89
+ case 'endVerticalAlignment':
90
+ this.endVerticalAlignment(item.item);
91
+ break;
92
+ }
93
+ renderedItems++;
94
+ if (this.progressCallback) {
95
+ this.progressCallback(renderedItems / totalItems);
96
+ }
97
+ }
98
+ if (page.watermark) {
99
+ this.renderWatermark(page);
100
+ }
101
+ }
102
+ }
103
+ renderLine(line, x, y) {
104
+ const pages = this.pdfDocument._pdfMakePages;
105
+ function preparePageNodeRefLine(_pageNodeRef, inline) {
106
+ let newWidth;
107
+ let diffWidth;
108
+ let textInlines = new _TextInlines.default(null);
109
+ if (_pageNodeRef.positions === undefined) {
110
+ throw new Error('Page reference id not found');
111
+ }
112
+ let pageNumber = _pageNodeRef.positions[0].pageNumber;
113
+ pageNumber = pages?.[pageNumber - 1]?.pageLabel ?? pageNumber.toString(); // w-pdfmaker: section page numbering
114
+
115
+ inline.text = pageNumber;
116
+ newWidth = textInlines.widthOfText(inline.text, inline);
117
+ diffWidth = inline.width - newWidth;
118
+ inline.width = newWidth;
119
+ switch (inline.alignment) {
120
+ case 'right':
121
+ inline.x += diffWidth;
122
+ break;
123
+ case 'center':
124
+ inline.x += diffWidth / 2;
125
+ break;
126
+ }
127
+ }
128
+ if (line._outline) {
129
+ let parentOutline = this.pdfDocument.outline;
130
+ if (line._outline.parentId && this.outlineMap[line._outline.parentId]) {
131
+ parentOutline = this.outlineMap[line._outline.parentId];
132
+ }
133
+ let outline = parentOutline.addItem(line._outline.text, {
134
+ expanded: line._outline.expanded
135
+ });
136
+ if (line._outline.id) {
137
+ this.outlineMap[line._outline.id] = outline;
138
+ }
139
+ }
140
+ if (line._pageNodeRef) {
141
+ preparePageNodeRefLine(line._pageNodeRef, line.inlines[0]);
142
+ }
143
+ x = x || 0;
144
+ y = y || 0;
145
+ let lineHeight = line.getHeight();
146
+ let ascenderHeight = line.getAscenderHeight();
147
+ let descent = lineHeight - ascenderHeight;
148
+ const textDecorator = new _TextDecorator.default(this.pdfDocument);
149
+ textDecorator.drawBackground(line, x, y);
150
+
151
+ //TODO: line.optimizeInlines();
152
+ //TODO: lines without differently styled inlines should be written to pdf as one stream
153
+ for (let i = 0, l = line.inlines.length; i < l; i++) {
154
+ let inline = line.inlines[i];
155
+ let shiftToBaseline = lineHeight - inline.font.ascender / 1000 * inline.fontSize - descent;
156
+ if (inline._pageNodeRef) {
157
+ preparePageNodeRefLine(inline._pageNodeRef, inline);
158
+ }
159
+ let options = {
160
+ lineBreak: false,
161
+ textWidth: inline.width,
162
+ characterSpacing: inline.characterSpacing,
163
+ wordCount: 1,
164
+ link: inline.link
165
+ };
166
+ if (inline.linkToDestination) {
167
+ options.goTo = inline.linkToDestination;
168
+ }
169
+ if (line.id && i === 0) {
170
+ options.destination = line.id;
171
+ }
172
+ if (inline.fontFeatures) {
173
+ options.features = inline.fontFeatures;
174
+ }
175
+ let opacity = (0, _variableType.isNumber)(inline.opacity) ? inline.opacity : 1;
176
+ this.pdfDocument.opacity(opacity);
177
+ this.pdfDocument.fill(this.pdfDocument.resolveColor(inline.color, 'black'));
178
+ this.pdfDocument._font = inline.font;
179
+ this.pdfDocument.fontSize(inline.fontSize);
180
+ let shiftedY = offsetText(y + shiftToBaseline, inline);
181
+ this.pdfDocument.text(inline.text, x + inline.x, shiftedY, options);
182
+ if (inline.linkToPage) {
183
+ this.pdfDocument.ref({
184
+ Type: 'Action',
185
+ S: 'GoTo',
186
+ D: [inline.linkToPage, 0, 0]
187
+ }).end();
188
+ this.pdfDocument.annotate(x + inline.x, shiftedY, inline.width, inline.height, {
189
+ Subtype: 'Link',
190
+ Dest: [inline.linkToPage - 1, 'XYZ', null, null, null]
191
+ });
192
+ }
193
+ }
194
+
195
+ // TODO: Decorations won't draw correctly for superscript
196
+ textDecorator.drawDecorations(line, x, y);
197
+ }
198
+ renderVector(vector) {
199
+ //TODO: pdf optimization (there's no need to write all properties everytime)
200
+ this.pdfDocument.lineWidth(vector.lineWidth || 1);
201
+ if (vector.dash) {
202
+ this.pdfDocument.dash(vector.dash.length, {
203
+ space: vector.dash.space || vector.dash.length,
204
+ phase: vector.dash.phase || 0
205
+ });
206
+ } else {
207
+ this.pdfDocument.undash();
208
+ }
209
+ this.pdfDocument.lineJoin(vector.lineJoin || 'miter');
210
+ this.pdfDocument.lineCap(vector.lineCap || 'butt');
211
+
212
+ //TODO: clipping
213
+
214
+ let gradient = null;
215
+ switch (vector.type) {
216
+ case 'ellipse':
217
+ this.pdfDocument.ellipse(vector.x, vector.y, vector.r1, vector.r2);
218
+ if (vector.linearGradient) {
219
+ gradient = this.pdfDocument.linearGradient(vector.x - vector.r1, vector.y, vector.x + vector.r1, vector.y);
220
+ }
221
+ break;
222
+ case 'rect':
223
+ if (vector.r) {
224
+ this.pdfDocument.roundedRect(vector.x, vector.y, vector.w, vector.h, vector.r);
225
+ } else {
226
+ this.pdfDocument.rect(vector.x, vector.y, vector.w, vector.h);
227
+ }
228
+ if (vector.linearGradient) {
229
+ gradient = this.pdfDocument.linearGradient(vector.x, vector.y, vector.x + vector.w, vector.y);
230
+ }
231
+ break;
232
+ case 'line':
233
+ this.pdfDocument.moveTo(vector.x1, vector.y1);
234
+ this.pdfDocument.lineTo(vector.x2, vector.y2);
235
+ break;
236
+ case 'polyline':
237
+ if (vector.points.length === 0) {
238
+ break;
239
+ }
240
+ this.pdfDocument.moveTo(vector.points[0].x, vector.points[0].y);
241
+ for (let i = 1, l = vector.points.length; i < l; i++) {
242
+ this.pdfDocument.lineTo(vector.points[i].x, vector.points[i].y);
243
+ }
244
+ if (vector.points.length > 1) {
245
+ let p1 = vector.points[0];
246
+ let pn = vector.points[vector.points.length - 1];
247
+ if (vector.closePath || p1.x === pn.x && p1.y === pn.y) {
248
+ this.pdfDocument.closePath();
249
+ }
250
+ }
251
+ break;
252
+ case 'path':
253
+ this.pdfDocument.path(vector.d);
254
+ break;
255
+ }
256
+ if (vector.linearGradient && gradient) {
257
+ let step = 1 / (vector.linearGradient.length - 1);
258
+ for (let i = 0; i < vector.linearGradient.length; i++) {
259
+ gradient.stop(i * step, vector.linearGradient[i]);
260
+ }
261
+ vector.color = gradient;
262
+ }
263
+ let patternColor = this.pdfDocument.providePattern(vector.color);
264
+ if (patternColor !== null) {
265
+ vector.color = patternColor;
266
+ }
267
+ let fillOpacity = (0, _variableType.isNumber)(vector.fillOpacity) ? vector.fillOpacity : 1;
268
+ let strokeOpacity = (0, _variableType.isNumber)(vector.strokeOpacity) ? vector.strokeOpacity : 1;
269
+ if (vector.color && vector.lineColor) {
270
+ this.pdfDocument.fillColor(this.pdfDocument.resolveColor(vector.color, 'black'), fillOpacity);
271
+ this.pdfDocument.strokeColor(this.pdfDocument.resolveColor(vector.lineColor, 'black'), strokeOpacity);
272
+ this.pdfDocument.fillAndStroke();
273
+ } else if (vector.color) {
274
+ this.pdfDocument.fillColor(this.pdfDocument.resolveColor(vector.color, 'black'), fillOpacity);
275
+ this.pdfDocument.fill();
276
+ } else {
277
+ this.pdfDocument.strokeColor(this.pdfDocument.resolveColor(vector.lineColor, 'black'), strokeOpacity);
278
+ this.pdfDocument.stroke();
279
+ }
280
+ }
281
+ renderImage(image) {
282
+ const source = this.pdfDocument.provideImage(image.image);
283
+ let opacity = (0, _variableType.isNumber)(image.opacity) ? image.opacity : 1;
284
+ this.pdfDocument.opacity(opacity);
285
+ if (image.cover) {
286
+ const align = image.cover.align || 'center';
287
+ const valign = image.cover.valign || 'center';
288
+ const width = image.cover.width ? image.cover.width : image.width;
289
+ const height = image.cover.height ? image.cover.height : image.height;
290
+ this.pdfDocument.save();
291
+ this.pdfDocument.rect(image.x, image.y, width, height).clip();
292
+ this.pdfDocument.image(source, image.x, image.y, {
293
+ cover: [width, height],
294
+ align: align,
295
+ valign: valign
296
+ });
297
+ this.pdfDocument.restore();
298
+ } else {
299
+ this.pdfDocument.image(source, image.x, image.y, {
300
+ width: image._width,
301
+ height: image._height
302
+ });
303
+ }
304
+ if (image.link) {
305
+ this.pdfDocument.link(image.x, image.y, image._width, image._height, image.link);
306
+ }
307
+ if (image.linkToPage) {
308
+ this.pdfDocument.ref({
309
+ Type: 'Action',
310
+ S: 'GoTo',
311
+ D: [image.linkToPage, 0, 0]
312
+ }).end();
313
+ this.pdfDocument.annotate(image.x, image.y, image._width, image._height, {
314
+ Subtype: 'Link',
315
+ Dest: [image.linkToPage - 1, 'XYZ', null, null, null]
316
+ });
317
+ }
318
+ if (image.linkToDestination) {
319
+ this.pdfDocument.goTo(image.x, image.y, image._width, image._height, image.linkToDestination);
320
+ }
321
+ if (image.linkToFile) {
322
+ const attachment = this.pdfDocument.provideAttachment(image.linkToFile);
323
+ this.pdfDocument.fileAnnotation(image.x, image.y, image._width, image._height, attachment,
324
+ // add empty rectangle as file annotation appearance with the same size as the rendered image
325
+ {
326
+ AP: {
327
+ N: {
328
+ Type: 'XObject',
329
+ Subtype: 'Form',
330
+ FormType: 1,
331
+ BBox: [image.x, image.y, image._width, image._height]
332
+ }
333
+ }
334
+ });
335
+ }
336
+ }
337
+ renderSVG(svg) {
338
+ let options = {
339
+ width: svg._width,
340
+ height: svg._height,
341
+ assumePt: true,
342
+ useCSS: !(0, _variableType.isString)(svg.svg),
343
+ ...svg.options
344
+ };
345
+ options.fontCallback = (family, bold, italic) => {
346
+ let fontsFamily = family.split(',').map(f => f.trim().replace(/('|")/g, ''));
347
+ let font = findFont(this.pdfDocument.fonts, fontsFamily, svg._font || svg.font || 'Roboto');
348
+ let loadedFont = this.pdfDocument.provideFont(font, bold, italic);
349
+ let alias = `pdfmake:${JSON.stringify([font, this.pdfDocument.getFontType(bold, italic)])}`;
350
+ this.pdfDocument._fontFamilies[alias] = loadedFont;
351
+ return alias;
352
+ };
353
+ options.imageCallback = link => {
354
+ let src = link.replace(/\s+/g, '');
355
+ this.pdfDocument.provideImage(src);
356
+ return src;
357
+ };
358
+ (0, _svgToPdfkit.default)(this.pdfDocument, svg.svg, svg.x, svg.y, options);
359
+ if (svg.link) {
360
+ this.pdfDocument.link(svg.x, svg.y, svg._width, svg._height, svg.link);
361
+ }
362
+ if (svg.linkToPage) {
363
+ this.pdfDocument.ref({
364
+ Type: 'Action',
365
+ S: 'GoTo',
366
+ D: [svg.linkToPage, 0, 0]
367
+ }).end();
368
+ this.pdfDocument.annotate(svg.x, svg.y, svg._width, svg._height, {
369
+ Subtype: 'Link',
370
+ Dest: [svg.linkToPage - 1, 'XYZ', null, null, null]
371
+ });
372
+ }
373
+ if (svg.linkToDestination) {
374
+ this.pdfDocument.goTo(svg.x, svg.y, svg._width, svg._height, svg.linkToDestination);
375
+ }
376
+ }
377
+ renderAttachment(attachment) {
378
+ const file = this.pdfDocument.provideAttachment(attachment.attachment);
379
+ const options = {};
380
+ if (attachment.icon) {
381
+ options.Name = attachment.icon;
382
+ }
383
+ this.pdfDocument.fileAnnotation(attachment.x, attachment.y, attachment._width, attachment._height, file, options);
384
+ }
385
+ beginClip(rect) {
386
+ this.pdfDocument.save();
387
+ this.pdfDocument.addContent(`${rect.x} ${rect.y} ${rect.width} ${rect.height} re`);
388
+ this.pdfDocument.clip();
389
+ }
390
+ endClip() {
391
+ this.pdfDocument.restore();
392
+ }
393
+ getVerticalAlignmentOffset(item) {
394
+ if (item.isCellContentMultiPage || item.verticalAlignment !== 'middle' && item.verticalAlignment !== 'bottom') {
395
+ return null;
396
+ }
397
+ const nodeHeight = item.getNodeHeight();
398
+ const viewHeight = item.getViewHeight();
399
+ if (!Number.isFinite(nodeHeight) || !Number.isFinite(viewHeight) || nodeHeight < 0 || viewHeight < nodeHeight) {
400
+ return null;
401
+ }
402
+ if (item.verticalAlignment === 'middle') {
403
+ return (viewHeight - nodeHeight) / 2;
404
+ }
405
+
406
+ // bottom
407
+ return viewHeight - nodeHeight;
408
+ }
409
+ beginVerticalAlignment(item) {
410
+ const offset = this.getVerticalAlignmentOffset(item);
411
+ if (offset !== null) {
412
+ this.pdfDocument.save();
413
+ this.pdfDocument.translate(0, offset);
414
+ }
415
+ }
416
+ endVerticalAlignment(item) {
417
+ if (this.getVerticalAlignmentOffset(item) !== null) {
418
+ this.pdfDocument.restore();
419
+ }
420
+ }
421
+ renderWatermark(page) {
422
+ let watermark = page.watermark;
423
+ this.pdfDocument.fill(this.pdfDocument.resolveColor(watermark.color, 'black'));
424
+ this.pdfDocument.opacity(watermark.opacity);
425
+ this.pdfDocument.save();
426
+ this.pdfDocument.rotate(watermark.angle, {
427
+ origin: [this.pdfDocument.page.width / 2, this.pdfDocument.page.height / 2]
428
+ });
429
+ let x = this.pdfDocument.page.width / 2 - watermark._size.size.width / 2;
430
+ let y = this.pdfDocument.page.height / 2 - watermark._size.size.height / 2;
431
+ this.pdfDocument._font = watermark.font;
432
+ this.pdfDocument.fontSize(watermark.fontSize);
433
+ this.pdfDocument.text(watermark.text, x, y, {
434
+ lineBreak: false
435
+ });
436
+ this.pdfDocument.restore();
437
+ }
438
+ }
439
+ var _default = exports.default = Renderer;
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _xmldoc = require("xmldoc");
6
+ var _variableType = require("./helpers/variableType");
7
+ /**
8
+ * Strip unit postfix, parse number, but return undefined instead of NaN for bad input
9
+ *
10
+ * @param {string} textVal
11
+ * @returns {?number}
12
+ */
13
+ const stripUnits = textVal => {
14
+ let n = parseFloat(textVal);
15
+ if (typeof n !== 'number' || isNaN(n)) {
16
+ return undefined;
17
+ }
18
+ return n;
19
+ };
20
+
21
+ /**
22
+ * Make sure it's valid XML and the root tag is <svg/>, returns xmldoc DOM
23
+ *
24
+ * @param {string} svgString
25
+ * @returns {object}
26
+ */
27
+ const parseSVG = svgString => {
28
+ let doc;
29
+ try {
30
+ doc = new _xmldoc.XmlDocument(svgString);
31
+ } catch (error) {
32
+ throw new Error('Invalid svg document (' + error + ')', {
33
+ cause: error
34
+ });
35
+ }
36
+ if (doc.name !== "svg") {
37
+ throw new Error('Invalid svg document (expected <svg>)');
38
+ }
39
+ return doc;
40
+ };
41
+ class SVGMeasure {
42
+ constructor() {}
43
+ measureSVG(svg) {
44
+ let width, height, viewBox;
45
+ if ((0, _variableType.isString)(svg)) {
46
+ let doc = parseSVG(svg);
47
+ width = doc.attr.width;
48
+ height = doc.attr.height;
49
+ viewBox = doc.attr.viewBox;
50
+ } else if (typeof SVGElement !== 'undefined' && svg instanceof SVGElement && typeof getComputedStyle === 'function') {
51
+ width = svg.getAttribute("width");
52
+ height = svg.getAttribute("height");
53
+ viewBox = svg.getAttribute("viewBox");
54
+ } else {
55
+ throw new Error('Invalid SVG document');
56
+ }
57
+ let docWidth = stripUnits(width);
58
+ let docHeight = stripUnits(height);
59
+ if ((docWidth === undefined || docHeight === undefined) && typeof viewBox === 'string') {
60
+ let viewBoxParts = viewBox.split(/[,\s]+/);
61
+ if (viewBoxParts.length !== 4) {
62
+ throw new Error("Unexpected svg viewBox format, should have 4 entries but found: '" + viewBox + "'");
63
+ }
64
+ if (docWidth === undefined) {
65
+ docWidth = stripUnits(viewBoxParts[2]);
66
+ }
67
+ if (docHeight === undefined) {
68
+ docHeight = stripUnits(viewBoxParts[3]);
69
+ }
70
+ }
71
+ return {
72
+ width: docWidth,
73
+ height: docHeight
74
+ };
75
+ }
76
+ writeDimensions(svg, dimensions) {
77
+ if ((0, _variableType.isString)(svg)) {
78
+ let doc = parseSVG(svg);
79
+ if (typeof doc.attr.viewBox !== 'string') {
80
+ doc.attr.viewBox = `0 0 ${stripUnits(doc.attr.width)} ${stripUnits(doc.attr.height)}`;
81
+ }
82
+ doc.attr.width = "" + dimensions.width;
83
+ doc.attr.height = "" + dimensions.height;
84
+ return doc.toString();
85
+ }
86
+ if (!svg.hasAttribute('viewBox')) {
87
+ svg.setAttribute('viewBox', `0 0 ${stripUnits(svg.getAttribute('width'))} ${stripUnits(svg.getAttribute('height'))}`);
88
+ }
89
+ svg.setAttribute('width', "" + dimensions.width);
90
+ svg.setAttribute('height', "" + dimensions.height);
91
+ return svg;
92
+ }
93
+ }
94
+ var _default = exports.default = SVGMeasure;