@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/Line.js ADDED
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ class Line {
6
+ /**
7
+ * @param {number} maxWidth Maximum width this line can have
8
+ */
9
+ constructor(maxWidth) {
10
+ this.maxWidth = maxWidth;
11
+ this.leadingCut = 0;
12
+ this.trailingCut = 0;
13
+ this.inlineWidths = 0;
14
+ this.inlines = [];
15
+ }
16
+
17
+ /**
18
+ * @param {object} inline
19
+ */
20
+ addInline(inline) {
21
+ if (this.inlines.length === 0) {
22
+ this.leadingCut = inline.leadingCut || 0;
23
+ }
24
+ this.trailingCut = inline.trailingCut || 0;
25
+ inline.x = this.inlineWidths - this.leadingCut;
26
+ this.inlines.push(inline);
27
+ this.inlineWidths += inline.width;
28
+ if (inline.lineEnd) {
29
+ this.newLineForced = true;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * @returns {number}
35
+ */
36
+ getHeight() {
37
+ let max = 0;
38
+ this.inlines.forEach(item => {
39
+ max = Math.max(max, item.height || 0);
40
+ });
41
+ return max;
42
+ }
43
+
44
+ /**
45
+ * @returns {number}
46
+ */
47
+ getAscenderHeight() {
48
+ let y = 0;
49
+ this.inlines.forEach(inline => {
50
+ y = Math.max(y, inline.font.ascender / 1000 * inline.fontSize);
51
+ });
52
+ return y;
53
+ }
54
+
55
+ /**
56
+ * @returns {number}
57
+ */
58
+ getWidth() {
59
+ return this.inlineWidths - this.leadingCut - this.trailingCut;
60
+ }
61
+
62
+ /**
63
+ * @returns {number}
64
+ */
65
+ getAvailableWidth() {
66
+ return this.maxWidth - this.getWidth();
67
+ }
68
+
69
+ /**
70
+ * @param {object} inline
71
+ * @param {Array} nextInlines
72
+ * @returns {boolean}
73
+ */
74
+ hasEnoughSpaceForInline(inline, nextInlines = []) {
75
+ if (this.inlines.length === 0) {
76
+ return true;
77
+ }
78
+ if (this.newLineForced) {
79
+ return false;
80
+ }
81
+ let inlineWidth = inline.width;
82
+ let inlineTrailingCut = inline.trailingCut || 0;
83
+ if (inline.noNewLine) {
84
+ for (let i = 0, l = nextInlines.length; i < l; i++) {
85
+ let nextInline = nextInlines[i];
86
+ inlineWidth += nextInline.width;
87
+ inlineTrailingCut += nextInline.trailingCut || 0;
88
+ if (!nextInline.noNewLine) {
89
+ break;
90
+ }
91
+ }
92
+ }
93
+ return this.inlineWidths + inlineWidth - this.leadingCut - inlineTrailingCut <= this.maxWidth;
94
+ }
95
+ clone() {
96
+ let result = new Line(this.maxWidth);
97
+ for (let key in this) {
98
+ if (this.hasOwnProperty(key)) {
99
+ result[key] = this[key];
100
+ }
101
+ }
102
+ return result;
103
+ }
104
+ }
105
+ var _default = exports.default = Line;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ class OutputDocument {
6
+ /**
7
+ * @param {Promise<object>} pdfDocumentPromise
8
+ */
9
+ constructor(pdfDocumentPromise) {
10
+ this.pdfDocumentPromise = pdfDocumentPromise;
11
+ this.bufferPromise = null;
12
+ this.streamEnded = false;
13
+ this.streamEndListenerAttached = false;
14
+ }
15
+
16
+ /**
17
+ * @returns {Promise<object>}
18
+ */
19
+ async getStream() {
20
+ const stream = await this.pdfDocumentPromise;
21
+ if (this.streamEnded) {
22
+ throw new Error('PDF stream has already been consumed.');
23
+ }
24
+ if (!this.streamEndListenerAttached) {
25
+ stream.once('end', () => {
26
+ this.streamEnded = true;
27
+ });
28
+ this.streamEndListenerAttached = true;
29
+ }
30
+ return stream;
31
+ }
32
+
33
+ /**
34
+ * @returns {Promise<Buffer>}
35
+ */
36
+ getBuffer() {
37
+ const getBufferInternal = async () => {
38
+ const stream = await this.getStream();
39
+ return new Promise((resolve, reject) => {
40
+ let chunks = [];
41
+ stream.on('data', chunk => {
42
+ chunks.push(chunk);
43
+ });
44
+ stream.on('end', () => {
45
+ resolve(Buffer.concat(chunks));
46
+ });
47
+ stream.on('error', reject);
48
+ stream.end();
49
+ });
50
+ };
51
+ if (this.bufferPromise === null) {
52
+ this.bufferPromise = getBufferInternal();
53
+ }
54
+ return this.bufferPromise;
55
+ }
56
+
57
+ /**
58
+ * @returns {Promise<string>}
59
+ */
60
+ async getBase64() {
61
+ const buffer = await this.getBuffer();
62
+ return buffer.toString('base64');
63
+ }
64
+
65
+ /**
66
+ * @returns {Promise<string>}
67
+ */
68
+ async getDataUrl() {
69
+ const data = await this.getBase64();
70
+ return 'data:application/pdf;base64,' + data;
71
+ }
72
+ }
73
+ var _default = exports.default = OutputDocument;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _OutputDocument = _interopRequireDefault(require("./OutputDocument"));
6
+ var _fs = _interopRequireDefault(require("fs"));
7
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
+ class OutputDocumentServer extends _OutputDocument.default {
9
+ /**
10
+ * @param {string} filename
11
+ * @returns {Promise}
12
+ */
13
+ async write(filename) {
14
+ await this.pdfDocumentPromise;
15
+ if (this.bufferPromise !== null) {
16
+ await _fs.default.promises.writeFile(filename, await this.getBuffer());
17
+ return;
18
+ }
19
+ const stream = await this.getStream();
20
+ const writeStream = _fs.default.createWriteStream(filename);
21
+ const streamEnded = new Promise((resolve, reject) => {
22
+ stream.on('end', resolve);
23
+ stream.on('error', reject);
24
+ });
25
+ const writeClosed = new Promise((resolve, reject) => {
26
+ writeStream.on('close', resolve);
27
+ writeStream.on('error', reject);
28
+ });
29
+ stream.pipe(writeStream);
30
+ stream.end();
31
+ await Promise.all([streamEnded, writeClosed]);
32
+ }
33
+ }
34
+ var _default = exports.default = OutputDocumentServer;
@@ -0,0 +1,322 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _pdfkit = require("pdfkit");
6
+ var _fs = _interopRequireDefault(require("fs"));
7
+ var _sha = require("@noble/hashes/sha2.js");
8
+ var _variableType = require("./helpers/variableType");
9
+ var _thai = require("./thai");
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ const typeName = (bold, italics) => {
12
+ let type = 'normal';
13
+ if (bold && italics) {
14
+ type = 'bolditalics';
15
+ } else if (bold) {
16
+ type = 'bold';
17
+ } else if (italics) {
18
+ type = 'italics';
19
+ }
20
+ return type;
21
+ };
22
+ class PDFDocument extends _pdfkit.PDFDocument {
23
+ constructor(fonts = {}, images = {}, patterns = {}, attachments = {}, options = {}, virtualfs = null, localAccessPolicy = undefined, downloadErrors = new Map()) {
24
+ super(options);
25
+ this.imageCache = new Map();
26
+ this.svgCache = new Map();
27
+ this.fonts = {};
28
+ this.fontCache = {};
29
+ for (let font in fonts) {
30
+ if (fonts.hasOwnProperty(font)) {
31
+ let fontDef = fonts[font];
32
+ this.fonts[font] = {
33
+ normal: fontDef.normal,
34
+ bold: fontDef.bold,
35
+ italics: fontDef.italics,
36
+ bolditalics: fontDef.bolditalics
37
+ };
38
+ }
39
+ }
40
+ this.patterns = {};
41
+ for (let pattern in patterns) {
42
+ if (patterns.hasOwnProperty(pattern)) {
43
+ let patternDef = patterns[pattern];
44
+ this.patterns[pattern] = this.pattern(patternDef.boundingBox, patternDef.xStep, patternDef.yStep, patternDef.pattern, patternDef.colored);
45
+ }
46
+ }
47
+ this.images = images;
48
+ this.attachments = attachments;
49
+ this.virtualfs = virtualfs;
50
+ this.localAccessPolicy = localAccessPolicy;
51
+ this.downloadErrors = downloadErrors || new Map(); // w-pdfmaker: url => failed download of an images entry
52
+ }
53
+ getFontType(bold, italics) {
54
+ return typeName(bold, italics);
55
+ }
56
+ getFontFile(familyName, bold, italics) {
57
+ let type = this.getFontType(bold, italics);
58
+ if (!this.fonts[familyName] || !this.fonts[familyName][type]) {
59
+ return null;
60
+ }
61
+ return this.fonts[familyName][type];
62
+ }
63
+ provideFont(familyName, bold, italics) {
64
+ let type = this.getFontType(bold, italics);
65
+ if (this.getFontFile(familyName, bold, italics) === null) {
66
+ throw new Error(`Font '${familyName}' in style '${type}' is not defined in the font section of the document definition.`);
67
+ }
68
+ this.fontCache[familyName] = this.fontCache[familyName] || {};
69
+ if (!this.fontCache[familyName][type]) {
70
+ let def = this.fonts[familyName][type];
71
+ if (!Array.isArray(def)) {
72
+ def = [def];
73
+ } else {
74
+ def = def.slice();
75
+ }
76
+ if (typeof def[0] === 'object' && def[0].url) {
77
+ def[0] = def[0].url;
78
+ }
79
+ if (this.virtualfs && this.virtualfs.existsSync(def[0])) {
80
+ def[0] = this.virtualfs.readFileSync(def[0]);
81
+ } else {
82
+ this.validateLocalFile(def[0]);
83
+ }
84
+ this.fontCache[familyName][type] = this.font(...def)._font;
85
+ }
86
+ return this.fontCache[familyName][type];
87
+ }
88
+ provideImage(src) {
89
+ const realImageSrc = src => {
90
+ let image = this.images[src] || src;
91
+ if (typeof image === 'object' && image.url) {
92
+ image = image.url;
93
+ }
94
+ if (this.virtualfs && this.virtualfs.existsSync(image)) {
95
+ return this.virtualfs.readFileSync(image);
96
+ }
97
+ if (typeof image !== 'string') {
98
+ return image;
99
+ }
100
+ let index = image.indexOf('base64,');
101
+ if (index < 0) {
102
+ return image;
103
+ }
104
+ return Buffer.from(image.substring(index + 7), 'base64');
105
+ };
106
+ if (typeof src === 'string' && this._imageRegistry[src]) {
107
+ return this._imageRegistry[src];
108
+ }
109
+ let image;
110
+ let hash;
111
+ this.throwIfNoSource(src);
112
+ let imageSrc = realImageSrc(src);
113
+ this.throwDownloadError(imageSrc);
114
+ this.validateLocalFile(imageSrc);
115
+ try {
116
+ if (typeof imageSrc !== 'string') {
117
+ const data = imageSrc instanceof ArrayBuffer ? new Uint8Array(imageSrc) : imageSrc;
118
+ hash = Buffer.from((0, _sha.sha256)(data)).toString('base64');
119
+ }
120
+ image = this.imageCache.get(hash) || this.openImage(imageSrc);
121
+ if (!image) {
122
+ throw new Error('No image');
123
+ }
124
+ } catch (error) {
125
+ throw new Error(`Invalid image: ${error.toString()}\nImages dictionary should contain dataURL entries (or local file paths in node.js)`, {
126
+ cause: error
127
+ });
128
+ }
129
+ if (!image.obj) {
130
+ image.embed(this);
131
+ }
132
+ if (hash) {
133
+ this.imageCache.set(hash, image);
134
+ }
135
+ if (typeof src === 'string') {
136
+ this._imageRegistry[src] = image;
137
+ }
138
+ return image;
139
+ }
140
+
141
+ /**
142
+ * w-pdfmaker: the source of an image or SVG: its entry in the images dictionary, or the value itself.
143
+ *
144
+ * @param {*} src
145
+ * @returns {*}
146
+ */
147
+ imageSource(src) {
148
+ const source = (0, _variableType.isString)(src) && this.images[src] || src;
149
+ return source && typeof source === 'object' && source.url ? source.url : source;
150
+ }
151
+
152
+ /**
153
+ * w-pdfmaker: whether an image is an SVG file (a path or URL ending with .svg).
154
+ *
155
+ * @param {*} src
156
+ * @returns {boolean}
157
+ */
158
+ isSVGFile(src) {
159
+ const source = this.imageSource(src);
160
+ return (0, _variableType.isString)(source) && !/^data:/i.test(source) && /\.svg(?:[?#]|$)/i.test(source);
161
+ }
162
+
163
+ /**
164
+ * w-pdfmaker: a failed download of an images entry is thrown when the image is used.
165
+ *
166
+ * @param {*} src
167
+ */
168
+ throwDownloadError(src) {
169
+ if ((0, _variableType.isString)(src) && this.downloadErrors.has(src)) {
170
+ throw this.downloadErrors.get(src);
171
+ }
172
+ }
173
+
174
+ /**
175
+ * w-pdfmaker: a name in the images dictionary with an empty value (e.g. no signature) is a missing image.
176
+ *
177
+ * @param {*} src
178
+ */
179
+ throwIfNoSource(src) {
180
+ if ((0, _variableType.isString)(src) && Object.prototype.hasOwnProperty.call(this.images, src) && !this.images[src]) {
181
+ throw Object.assign(new Error(`Image '${src}' has no source in the images dictionary`), {
182
+ code: 'ENOENT'
183
+ });
184
+ }
185
+ }
186
+
187
+ /**
188
+ * w-pdfmaker: SVG content. A string not starting with '<' is a file: a path, a name in the images dictionary,
189
+ * or a file in the virtual file system (downloads included).
190
+ *
191
+ * @param {string|object} src
192
+ * @returns {string|object}
193
+ */
194
+ provideSVG(src) {
195
+ this.throwIfNoSource(src);
196
+ const svg = this.imageSource(src);
197
+ if (!(0, _variableType.isString)(svg) || svg.trim().startsWith('<')) {
198
+ return svg;
199
+ }
200
+ this.throwDownloadError(svg);
201
+ if (!this.svgCache.has(svg)) {
202
+ let data;
203
+ if (this.virtualfs && this.virtualfs.existsSync(svg)) {
204
+ data = this.virtualfs.readFileSync(svg);
205
+ } else {
206
+ this.validateLocalFile(svg);
207
+ data = _fs.default.readFileSync(svg);
208
+ }
209
+ // downloads are stored as ArrayBuffer; TextDecoder also drops a BOM
210
+ this.svgCache.set(svg, new TextDecoder().decode(data));
211
+ }
212
+ return this.svgCache.get(svg);
213
+ }
214
+
215
+ /**
216
+ * @param {Array} color pdfmake format: [<pattern name>, <color>]
217
+ * @returns {Array} pdfkit format: [<pattern object>, <color>]
218
+ */
219
+ providePattern(color) {
220
+ if (Array.isArray(color) && color.length === 2) {
221
+ return [this.patterns[color[0]], color[1]];
222
+ }
223
+ return null;
224
+ }
225
+ provideAttachment(src) {
226
+ const checkRequired = obj => {
227
+ if (!obj) {
228
+ throw new Error('No attachment');
229
+ }
230
+ if (!obj.src) {
231
+ throw new Error('The "src" key is required for attachments');
232
+ }
233
+ return obj;
234
+ };
235
+ let attachment = checkRequired(typeof src === 'object' ? src : this.attachments[src]);
236
+ if (typeof attachment.src === 'object' && attachment.src.url) {
237
+ attachment = {
238
+ ...attachment,
239
+ src: attachment.src.url
240
+ };
241
+ }
242
+ if (this.virtualfs && this.virtualfs.existsSync(attachment.src)) {
243
+ return {
244
+ ...attachment,
245
+ src: this.virtualfs.readFileSync(attachment.src)
246
+ };
247
+ }
248
+ this.validateLocalFile(attachment.src);
249
+ return attachment;
250
+ }
251
+ text(text, ...args) {
252
+ if (!(0, _thai.needsActualText)(text)) {
253
+ return super.text(text, ...args);
254
+ }
255
+ const [x, y, options] = args;
256
+ if (options?.lineBreak !== false) {
257
+ // wrapped text cannot be drawn part by part
258
+ this.markContent('Span', {
259
+ actual: text
260
+ });
261
+ super.text(text, ...args);
262
+ return this.endMarkedContent();
263
+ }
264
+ let partX = x;
265
+ (0, _thai.actualTextRuns)(text).forEach((run, i) => {
266
+ if (run.actual) {
267
+ this.markContent('Span', {
268
+ actual: run.text
269
+ });
270
+ }
271
+ super.text(run.text, partX, y, i === 0 ? options : {
272
+ ...options,
273
+ destination: undefined
274
+ });
275
+ if (run.actual) {
276
+ this.endMarkedContent();
277
+ }
278
+ // pdfkit advances each glyph by its xAdvance plus characterSpacing (Tc)
279
+ const [, positions] = this._font.encode(run.text, options.features);
280
+ partX += positions.reduce((sum, pos) => sum + pos.xAdvance * this._fontSize / 1000 + (options.characterSpacing || 0), 0);
281
+ });
282
+ return this;
283
+ }
284
+ resolveColor(color, defaultColor) {
285
+ color = color || defaultColor;
286
+ if (typeof this._normalizeColor === 'function') {
287
+ if ((0, _variableType.isString)(color) && this._normalizeColor(color) === null) {
288
+ // color is not valid
289
+ return defaultColor;
290
+ }
291
+ }
292
+ return color;
293
+ }
294
+ setOpenActionAsPrint() {
295
+ let printActionRef = this.ref({
296
+ Type: 'Action',
297
+ S: 'Named',
298
+ N: 'Print'
299
+ });
300
+ this._root.data.OpenAction = printActionRef;
301
+ printActionRef.end();
302
+ }
303
+ file(src, options = {}) {
304
+ this.validateLocalFile(src);
305
+ return super.file(src, options);
306
+ }
307
+ validateLocalFile(path) {
308
+ if (typeof this.localAccessPolicy === 'undefined') {
309
+ return;
310
+ }
311
+ if (!(0, _variableType.isString)(path)) {
312
+ return;
313
+ }
314
+ if (/^data:/.test(path)) {
315
+ return;
316
+ }
317
+ if (this.localAccessPolicy(path) !== true) {
318
+ throw new Error(`Access to local file denied by resource access policy: ${path}`);
319
+ }
320
+ }
321
+ }
322
+ var _default = exports.default = PDFDocument;