@wirabyte/w-pdfmaker 0.0.0-stage → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +53 -0
- package/LICENSE +22 -0
- package/NOTICE.md +39 -0
- package/README.md +106 -2
- package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
- package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
- package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
- package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
- package/build/fonts/Roboto.js +27 -0
- package/build/pdfmake.js +103161 -0
- package/build/pdfmake.js.map +1 -0
- package/build/pdfmake.min.js +3 -0
- package/build/pdfmake.min.js.map +1 -0
- package/build/standard-fonts/Courier.js +9 -0
- package/build/standard-fonts/Helvetica.js +9 -0
- package/build/standard-fonts/Symbol.js +9 -0
- package/build/standard-fonts/Times.js +9 -0
- package/build/standard-fonts/ZapfDingbats.js +9 -0
- package/build/vfs_fonts.js +6 -0
- package/build-vfs.js +44 -0
- package/fonts/Roboto/Roboto-Italic.ttf +0 -0
- package/fonts/Roboto/Roboto-Medium.ttf +0 -0
- package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
- package/fonts/Roboto/Roboto-Regular.ttf +0 -0
- package/fonts/Roboto.js +8 -0
- package/fonts/THSarabunIT9/LICENSE.txt +7 -0
- package/fonts/THSarabunIT9/THSarabunIT9-Bold.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9-BoldItalic.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9-Italic.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9.ttf +0 -0
- package/fonts/THSarabunNew/COPYING.GPLv2 +339 -0
- package/fonts/THSarabunNew/LICENSE.txt +13 -0
- package/fonts/THSarabunNew/THSarabunNew-Bold.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew-BoldItalic.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew-Italic.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew.ttf +0 -0
- package/index.d.ts +167 -0
- package/interfaces.d.ts +2779 -0
- package/js/DocMeasure.js +695 -0
- package/js/DocPreprocessor.js +263 -0
- package/js/DocumentContext.js +563 -0
- package/js/ElementWriter.js +488 -0
- package/js/LayoutBuilder.js +1525 -0
- package/js/Line.js +105 -0
- package/js/OutputDocument.js +73 -0
- package/js/OutputDocumentServer.js +34 -0
- package/js/PDFDocument.js +322 -0
- package/js/PageElementWriter.js +302 -0
- package/js/PageSize.js +74 -0
- package/js/Printer.js +275 -0
- package/js/Renderer.js +439 -0
- package/js/SVGMeasure.js +94 -0
- package/js/StyleContextStack.js +191 -0
- package/js/TableProcessor.js +658 -0
- package/js/TextBreaker.js +164 -0
- package/js/TextDecorator.js +164 -0
- package/js/TextInlines.js +212 -0
- package/js/URLResolver.js +122 -0
- package/js/base.js +125 -0
- package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
- package/js/browser-extensions/fonts/Roboto.js +38 -0
- package/js/browser-extensions/fonts/thai.js +28 -0
- package/js/browser-extensions/index.js +88 -0
- package/js/browser-extensions/pageUrls.js +79 -0
- package/js/browser-extensions/pdfMake.js +3 -0
- package/js/browser-extensions/standard-fonts/Courier.js +20 -0
- package/js/browser-extensions/standard-fonts/Helvetica.js +20 -0
- package/js/browser-extensions/standard-fonts/Symbol.js +17 -0
- package/js/browser-extensions/standard-fonts/Times.js +20 -0
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
- package/js/browser-extensions/virtual-fs-cjs.js +3 -0
- package/js/columnCalculator.js +159 -0
- package/js/helpers/node.js +122 -0
- package/js/helpers/tools.js +46 -0
- package/js/helpers/variableType.js +59 -0
- package/js/imageFallback.js +23 -0
- package/js/index.js +45 -0
- package/js/mergeStamp.js +194 -0
- package/js/pageNumbering.js +48 -0
- package/js/qrEnc.js +721 -0
- package/js/standardPageSizes.js +56 -0
- package/js/tableLayouts.js +98 -0
- package/js/thai.js +201 -0
- package/js/virtual-fs.js +82 -0
- package/package.json +93 -4
- package/skills/w-pdfmaker/SKILL.md +171 -0
- package/skills/w-pdfmaker/references/browser.md +36 -0
- package/skills/w-pdfmaker/references/document-definition.md +241 -0
- package/skills/w-pdfmaker/references/images.md +90 -0
- package/skills/w-pdfmaker/references/merge-stamp.md +94 -0
- package/skills/w-pdfmaker/references/thai.md +67 -0
- package/src/DocMeasure.js +780 -0
- package/src/DocPreprocessor.js +293 -0
- package/src/DocumentContext.js +605 -0
- package/src/ElementWriter.js +550 -0
- package/src/LayoutBuilder.js +1689 -0
- package/src/Line.js +114 -0
- package/src/OutputDocument.js +75 -0
- package/src/OutputDocumentServer.js +38 -0
- package/src/PDFDocument.js +352 -0
- package/src/PageElementWriter.js +343 -0
- package/src/PageSize.js +53 -0
- package/src/Printer.js +289 -0
- package/src/Renderer.js +468 -0
- package/src/SVGMeasure.js +109 -0
- package/src/StyleContextStack.js +208 -0
- package/src/TableProcessor.js +719 -0
- package/src/TextBreaker.js +166 -0
- package/src/TextDecorator.js +191 -0
- package/src/TextInlines.js +229 -0
- package/src/URLResolver.js +122 -0
- package/src/base.js +143 -0
- package/src/browser-extensions/OutputDocumentBrowser.js +80 -0
- package/src/browser-extensions/fonts/Roboto.js +27 -0
- package/src/browser-extensions/fonts/thai.js +23 -0
- package/src/browser-extensions/index.js +91 -0
- package/src/browser-extensions/pageUrls.js +67 -0
- package/src/browser-extensions/pdfMake.js +1 -0
- package/src/browser-extensions/standard-fonts/Courier.js +25 -0
- package/src/browser-extensions/standard-fonts/Helvetica.js +25 -0
- package/src/browser-extensions/standard-fonts/Symbol.js +17 -0
- package/src/browser-extensions/standard-fonts/Times.js +25 -0
- package/src/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
- package/src/browser-extensions/virtual-fs-cjs.js +1 -0
- package/src/columnCalculator.js +160 -0
- package/src/helpers/node.js +134 -0
- package/src/helpers/tools.js +44 -0
- package/src/helpers/variableType.js +50 -0
- package/src/imageFallback.js +18 -0
- package/src/index.js +45 -0
- package/src/mergeStamp.js +132 -0
- package/src/pageNumbering.js +45 -0
- package/src/qrEnc.js +796 -0
- package/src/standardPageSizes.js +52 -0
- package/src/tableLayouts.js +100 -0
- package/src/thai.js +170 -0
- package/src/virtual-fs.js +92 -0
- package/standard-fonts/Courier.js +8 -0
- package/standard-fonts/Helvetica.js +8 -0
- package/standard-fonts/Symbol.js +5 -0
- package/standard-fonts/Times.js +8 -0
- package/standard-fonts/ZapfDingbats.js +5 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _variableType = require("./helpers/variableType");
|
|
6
|
+
var _StyleContextStack = _interopRequireDefault(require("./StyleContextStack"));
|
|
7
|
+
var _thai = require("./thai");
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} text
|
|
11
|
+
* @param {boolean} noWrap
|
|
12
|
+
* @param {boolean} breakAll
|
|
13
|
+
* @returns {Array}
|
|
14
|
+
*/
|
|
15
|
+
const splitWords = (text, noWrap, breakAll = false) => {
|
|
16
|
+
let words = [];
|
|
17
|
+
if (text === undefined || text === null) {
|
|
18
|
+
text = '';
|
|
19
|
+
} else {
|
|
20
|
+
text = String(text);
|
|
21
|
+
}
|
|
22
|
+
if (noWrap) {
|
|
23
|
+
words.push({
|
|
24
|
+
text: text
|
|
25
|
+
});
|
|
26
|
+
return words;
|
|
27
|
+
}
|
|
28
|
+
if (breakAll) {
|
|
29
|
+
return Array.from(text.replace(/\r\n/g, '\n')).map(c => {
|
|
30
|
+
if (c.match(/^\n$|^\r$/)) {
|
|
31
|
+
// new line
|
|
32
|
+
return {
|
|
33
|
+
text: '',
|
|
34
|
+
lineEnd: true
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
text: c
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
if (text.length === 0) {
|
|
43
|
+
words.push({
|
|
44
|
+
text: ''
|
|
45
|
+
});
|
|
46
|
+
return words;
|
|
47
|
+
}
|
|
48
|
+
let last = 0;
|
|
49
|
+
for (let bk of (0, _thai.lineBreaks)(text)) {
|
|
50
|
+
let word = text.slice(last, bk.position);
|
|
51
|
+
if (bk.required || word.match(/\r?\n$|\r$/)) {
|
|
52
|
+
// new line
|
|
53
|
+
word = word.replace(/\r?\n$|\r$/, '');
|
|
54
|
+
words.push({
|
|
55
|
+
text: word,
|
|
56
|
+
lineEnd: true
|
|
57
|
+
});
|
|
58
|
+
} else {
|
|
59
|
+
words.push({
|
|
60
|
+
text: word
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
last = bk.position;
|
|
64
|
+
}
|
|
65
|
+
return words;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @param {Array} words
|
|
70
|
+
* @param {boolean} noWrap
|
|
71
|
+
* @returns {?string}
|
|
72
|
+
*/
|
|
73
|
+
const getFirstWord = (words, noWrap) => {
|
|
74
|
+
let word = words[0];
|
|
75
|
+
if (word === undefined) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
if (noWrap) {
|
|
79
|
+
// text was not wrapped, we need only first word
|
|
80
|
+
let tmpWords = splitWords(word.text, false);
|
|
81
|
+
if (tmpWords[0] === undefined) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
word = tmpWords[0];
|
|
85
|
+
}
|
|
86
|
+
return word.text;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {Array} words
|
|
91
|
+
* @param {boolean} noWrap
|
|
92
|
+
* @returns {?string}
|
|
93
|
+
*/
|
|
94
|
+
const getLastWord = (words, noWrap) => {
|
|
95
|
+
let word = words[words.length - 1];
|
|
96
|
+
if (word === undefined) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
if (word.lineEnd) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
if (noWrap) {
|
|
103
|
+
// text was not wrapped, we need only last word
|
|
104
|
+
let tmpWords = splitWords(word.text, false);
|
|
105
|
+
if (tmpWords[tmpWords.length - 1] === undefined) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
word = tmpWords[tmpWords.length - 1];
|
|
109
|
+
}
|
|
110
|
+
return word.text;
|
|
111
|
+
};
|
|
112
|
+
class TextBreaker {
|
|
113
|
+
/**
|
|
114
|
+
* @param {string|Array} texts
|
|
115
|
+
* @param {StyleContextStack} styleContextStack
|
|
116
|
+
* @returns {Array}
|
|
117
|
+
*/
|
|
118
|
+
getBreaks(texts, styleContextStack) {
|
|
119
|
+
let results = [];
|
|
120
|
+
if (!Array.isArray(texts)) {
|
|
121
|
+
texts = [texts];
|
|
122
|
+
}
|
|
123
|
+
let lastWord = null;
|
|
124
|
+
for (let i = 0, l = texts.length; i < l; i++) {
|
|
125
|
+
let item = texts[i];
|
|
126
|
+
let style = null;
|
|
127
|
+
let words;
|
|
128
|
+
let breakAll = _StyleContextStack.default.getStyleProperty(item || {}, styleContextStack, 'wordBreak', 'normal') === 'break-all';
|
|
129
|
+
let noWrap = _StyleContextStack.default.getStyleProperty(item || {}, styleContextStack, 'noWrap', false);
|
|
130
|
+
if ((0, _variableType.isObject)(item)) {
|
|
131
|
+
if (item._textRef && item._textRef._textNodeRef.text) {
|
|
132
|
+
item.text = item._textRef._textNodeRef.text;
|
|
133
|
+
}
|
|
134
|
+
words = splitWords(item.text, noWrap, breakAll);
|
|
135
|
+
style = _StyleContextStack.default.copyStyle(item);
|
|
136
|
+
} else {
|
|
137
|
+
words = splitWords(item, noWrap, breakAll);
|
|
138
|
+
}
|
|
139
|
+
if (lastWord && words.length) {
|
|
140
|
+
let firstWord = getFirstWord(words, noWrap);
|
|
141
|
+
let wrapWords = splitWords(lastWord + firstWord, false);
|
|
142
|
+
if (wrapWords.length === 1) {
|
|
143
|
+
results[results.length - 1].noNewLine = true;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
for (let i2 = 0, l2 = words.length; i2 < l2; i2++) {
|
|
147
|
+
let result = {
|
|
148
|
+
text: words[i2].text
|
|
149
|
+
};
|
|
150
|
+
if (words[i2].lineEnd) {
|
|
151
|
+
result.lineEnd = true;
|
|
152
|
+
}
|
|
153
|
+
_StyleContextStack.default.copyStyle(style, result);
|
|
154
|
+
results.push(result);
|
|
155
|
+
}
|
|
156
|
+
lastWord = null;
|
|
157
|
+
if (i + 1 < l) {
|
|
158
|
+
lastWord = getLastWord(words, noWrap);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return results;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
var _default = exports.default = TextBreaker;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _variableType = require("./helpers/variableType");
|
|
6
|
+
const groupDecorations = (line, pdfDocument) => {
|
|
7
|
+
let groups = [];
|
|
8
|
+
let currentGroup = null;
|
|
9
|
+
for (let i = 0, l = line.inlines.length; i < l; i++) {
|
|
10
|
+
let inline = line.inlines[i];
|
|
11
|
+
let decoration = inline.decoration;
|
|
12
|
+
if (!decoration) {
|
|
13
|
+
currentGroup = null;
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (!Array.isArray(decoration)) {
|
|
17
|
+
decoration = [decoration];
|
|
18
|
+
}
|
|
19
|
+
let color = pdfDocument.resolveColor(pdfDocument.resolveColor(inline.decorationColor, inline.color), 'black');
|
|
20
|
+
let style = inline.decorationStyle || 'solid';
|
|
21
|
+
let thickness = (0, _variableType.isNumber)(inline.decorationThickness) ? inline.decorationThickness : null;
|
|
22
|
+
for (let ii = 0, ll = decoration.length; ii < ll; ii++) {
|
|
23
|
+
let decorationItem = decoration[ii];
|
|
24
|
+
if (!currentGroup || decorationItem !== currentGroup.decoration || style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor || thickness !== currentGroup.decorationThickness) {
|
|
25
|
+
currentGroup = {
|
|
26
|
+
line: line,
|
|
27
|
+
decoration: decorationItem,
|
|
28
|
+
decorationColor: color,
|
|
29
|
+
decorationStyle: style,
|
|
30
|
+
decorationThickness: thickness,
|
|
31
|
+
inlines: [inline]
|
|
32
|
+
};
|
|
33
|
+
groups.push(currentGroup);
|
|
34
|
+
} else {
|
|
35
|
+
currentGroup.inlines.push(inline);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return groups;
|
|
40
|
+
};
|
|
41
|
+
class TextDecorator {
|
|
42
|
+
constructor(pdfDocument) {
|
|
43
|
+
this.pdfDocument = pdfDocument;
|
|
44
|
+
}
|
|
45
|
+
drawBackground(line, x, y) {
|
|
46
|
+
let height = line.getHeight();
|
|
47
|
+
for (let i = 0, l = line.inlines.length; i < l; i++) {
|
|
48
|
+
let inline = line.inlines[i];
|
|
49
|
+
let color = this.pdfDocument.resolveColor(inline.background, undefined);
|
|
50
|
+
if (!color) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
let patternColor = this.pdfDocument.providePattern(inline.background);
|
|
54
|
+
if (patternColor !== null) {
|
|
55
|
+
color = patternColor;
|
|
56
|
+
}
|
|
57
|
+
let justifyShift = inline.justifyShift || 0;
|
|
58
|
+
let rectX = x + inline.x - justifyShift;
|
|
59
|
+
let rectWidth = inline.width + justifyShift;
|
|
60
|
+
let lineStart = x + Math.min(0, line.inlines[0].leadingIndent || 0);
|
|
61
|
+
if (line.leadingCut > 0 && rectX < lineStart) {
|
|
62
|
+
// Clip the background of trimmed whitespace at the line start,
|
|
63
|
+
// keeping any negative leading indent used by the visible text.
|
|
64
|
+
rectWidth = rectX + rectWidth - lineStart;
|
|
65
|
+
rectX = lineStart;
|
|
66
|
+
if (rectWidth <= 0) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
this.pdfDocument.fillColor(color).rect(rectX, y, rectWidth, height).fill();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
drawDecorations(line, x, y) {
|
|
74
|
+
let groups = groupDecorations(line, this.pdfDocument);
|
|
75
|
+
for (let i = 0, l = groups.length; i < l; i++) {
|
|
76
|
+
this._drawDecoration(groups[i], x, y);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
_drawDecoration(group, x, y) {
|
|
80
|
+
const maxInline = () => {
|
|
81
|
+
let max = 0;
|
|
82
|
+
for (let i = 0, l = group.inlines.length; i < l; i++) {
|
|
83
|
+
let inline = group.inlines[i];
|
|
84
|
+
max = inline.fontSize > group.inlines[max].fontSize ? i : max;
|
|
85
|
+
}
|
|
86
|
+
return group.inlines[max];
|
|
87
|
+
};
|
|
88
|
+
const width = () => {
|
|
89
|
+
let sum = 0;
|
|
90
|
+
for (let i = 0, l = group.inlines.length; i < l; i++) {
|
|
91
|
+
let justifyShift = group.inlines[i].justifyShift || 0;
|
|
92
|
+
sum += group.inlines[i].width + justifyShift;
|
|
93
|
+
}
|
|
94
|
+
return sum;
|
|
95
|
+
};
|
|
96
|
+
let firstInline = group.inlines[0];
|
|
97
|
+
let biggerInline = maxInline();
|
|
98
|
+
let totalWidth = width();
|
|
99
|
+
let lineAscent = group.line.getAscenderHeight();
|
|
100
|
+
let ascent = biggerInline.font.ascender / 1000 * biggerInline.fontSize;
|
|
101
|
+
let height = biggerInline.height;
|
|
102
|
+
let descent = height - ascent;
|
|
103
|
+
let lw = (0, _variableType.isNumber)(group.decorationThickness) ? group.decorationThickness : 0.5 + Math.floor(Math.max(biggerInline.fontSize - 8, 0) / 2) * 0.12;
|
|
104
|
+
switch (group.decoration) {
|
|
105
|
+
case 'underline':
|
|
106
|
+
y += lineAscent + descent * 0.45;
|
|
107
|
+
break;
|
|
108
|
+
case 'overline':
|
|
109
|
+
y += lineAscent - ascent * 0.85;
|
|
110
|
+
break;
|
|
111
|
+
case 'lineThrough':
|
|
112
|
+
y += lineAscent - ascent * 0.25;
|
|
113
|
+
break;
|
|
114
|
+
default:
|
|
115
|
+
throw new Error(`Unknown decoration : ${group.decoration}`);
|
|
116
|
+
}
|
|
117
|
+
if (group.inlines[0].sup) {
|
|
118
|
+
y -= group.inlines[0].fontSize * 0.75;
|
|
119
|
+
}
|
|
120
|
+
if (group.inlines[0].sub) {
|
|
121
|
+
y += group.inlines[0].fontSize * 0.35;
|
|
122
|
+
}
|
|
123
|
+
this.pdfDocument.save();
|
|
124
|
+
if (group.decorationStyle === 'double') {
|
|
125
|
+
let gap = Math.max(0.5, lw, biggerInline.fontSize * 0.15);
|
|
126
|
+
this.pdfDocument.fillColor(group.decorationColor).rect(x + firstInline.x, y - lw / 2, totalWidth, lw / 2).fill().rect(x + firstInline.x, y + gap - lw / 2, totalWidth, lw / 2).fill();
|
|
127
|
+
} else if (group.decorationStyle === 'dashed') {
|
|
128
|
+
let nbDashes = Math.ceil(totalWidth / (3.96 + 2.84));
|
|
129
|
+
let rdx = x + firstInline.x;
|
|
130
|
+
this.pdfDocument.rect(rdx, y, totalWidth, lw).clip();
|
|
131
|
+
this.pdfDocument.fillColor(group.decorationColor);
|
|
132
|
+
for (let i = 0; i < nbDashes; i++) {
|
|
133
|
+
this.pdfDocument.rect(rdx, y - lw / 2, 3.96, lw).fill();
|
|
134
|
+
rdx += 3.96 + 2.84;
|
|
135
|
+
}
|
|
136
|
+
} else if (group.decorationStyle === 'dotted') {
|
|
137
|
+
let nbDots = Math.ceil(totalWidth / (lw * 3));
|
|
138
|
+
let rx = x + firstInline.x;
|
|
139
|
+
this.pdfDocument.rect(rx, y, totalWidth, lw).clip();
|
|
140
|
+
this.pdfDocument.fillColor(group.decorationColor);
|
|
141
|
+
for (let i = 0; i < nbDots; i++) {
|
|
142
|
+
this.pdfDocument.rect(rx, y - lw / 2, lw, lw).fill();
|
|
143
|
+
rx += lw * 3;
|
|
144
|
+
}
|
|
145
|
+
} else if (group.decorationStyle === 'wavy') {
|
|
146
|
+
let sh = 0.7,
|
|
147
|
+
sv = 1;
|
|
148
|
+
let nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
|
|
149
|
+
let rwx = x + firstInline.x - 1;
|
|
150
|
+
this.pdfDocument.rect(x + firstInline.x, y - sv, totalWidth, 2 * sv).clip();
|
|
151
|
+
this.pdfDocument.lineWidth(lw / 3);
|
|
152
|
+
this.pdfDocument.moveTo(rwx, y);
|
|
153
|
+
for (let i = 0; i < nbWaves; i++) {
|
|
154
|
+
this.pdfDocument.bezierCurveTo(rwx + sh, y - sv, rwx + sh * 2, y - sv, rwx + sh * 3, y).bezierCurveTo(rwx + sh * 4, y + sv, rwx + sh * 5, y + sv, rwx + sh * 6, y);
|
|
155
|
+
rwx += sh * 6;
|
|
156
|
+
}
|
|
157
|
+
this.pdfDocument.stroke(group.decorationColor);
|
|
158
|
+
} else {
|
|
159
|
+
this.pdfDocument.fillColor(group.decorationColor).rect(x + firstInline.x, y - lw / 2, totalWidth, lw).fill();
|
|
160
|
+
}
|
|
161
|
+
this.pdfDocument.restore();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
var _default = exports.default = TextDecorator;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _TextBreaker = _interopRequireDefault(require("./TextBreaker"));
|
|
6
|
+
var _StyleContextStack = _interopRequireDefault(require("./StyleContextStack"));
|
|
7
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
const LEADING = /^(\s)+/g;
|
|
9
|
+
const TRAILING = /(\s)+$/g;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {Array} array
|
|
13
|
+
* @returns {Array}
|
|
14
|
+
*/
|
|
15
|
+
const flattenTextArray = array => {
|
|
16
|
+
function flatten(array) {
|
|
17
|
+
return array.reduce((prev, cur) => {
|
|
18
|
+
let current = Array.isArray(cur.text) ? flatten(cur.text) : cur;
|
|
19
|
+
let more = [].concat(current).some(Array.isArray);
|
|
20
|
+
return prev.concat(more ? flatten(current) : current);
|
|
21
|
+
}, []);
|
|
22
|
+
}
|
|
23
|
+
if (!Array.isArray(array)) {
|
|
24
|
+
array = [array];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// TODO: Styling in nested text (issue: https://github.com/bpampuch/pdfmake/issues/1174)
|
|
28
|
+
|
|
29
|
+
array = flatten(array);
|
|
30
|
+
return array;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Text measurement utility
|
|
35
|
+
*/
|
|
36
|
+
class TextInlines {
|
|
37
|
+
/**
|
|
38
|
+
* @param {object} pdfDocument object is instance of PDFDocument
|
|
39
|
+
*/
|
|
40
|
+
constructor(pdfDocument) {
|
|
41
|
+
this.pdfDocument = pdfDocument;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Converts an array of strings (or inline-definition-objects) into a collection
|
|
46
|
+
* of inlines and calculated minWidth/maxWidth and their min/max widths
|
|
47
|
+
*
|
|
48
|
+
* @param {Array|object} textArray an array of inline-definition-objects (or strings)
|
|
49
|
+
* @param {StyleContextStack} styleContextStack current style stack
|
|
50
|
+
* @returns {object} collection of inlines, minWidth, maxWidth
|
|
51
|
+
*/
|
|
52
|
+
buildInlines(textArray, styleContextStack) {
|
|
53
|
+
const getTrimmedWidth = item => {
|
|
54
|
+
return Math.max(0, item.width - item.leadingCut - item.trailingCut);
|
|
55
|
+
};
|
|
56
|
+
let minWidth = 0;
|
|
57
|
+
let maxWidth = 0;
|
|
58
|
+
let currentLineWidth;
|
|
59
|
+
let flattenedTextArray = flattenTextArray(textArray);
|
|
60
|
+
const textBreaker = new _TextBreaker.default();
|
|
61
|
+
let brokenText = textBreaker.getBreaks(flattenedTextArray, styleContextStack);
|
|
62
|
+
let measuredText = this.measure(brokenText, styleContextStack);
|
|
63
|
+
measuredText.forEach(inline => {
|
|
64
|
+
minWidth = Math.max(minWidth, getTrimmedWidth(inline));
|
|
65
|
+
if (!currentLineWidth) {
|
|
66
|
+
currentLineWidth = {
|
|
67
|
+
width: 0,
|
|
68
|
+
leadingCut: inline.leadingCut,
|
|
69
|
+
trailingCut: 0
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
currentLineWidth.width += inline.width;
|
|
73
|
+
currentLineWidth.trailingCut = inline.trailingCut;
|
|
74
|
+
maxWidth = Math.max(maxWidth, getTrimmedWidth(currentLineWidth));
|
|
75
|
+
if (inline.lineEnd) {
|
|
76
|
+
currentLineWidth = null;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
if (_StyleContextStack.default.getStyleProperty({}, styleContextStack, 'noWrap', false)) {
|
|
80
|
+
minWidth = maxWidth;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
items: measuredText,
|
|
84
|
+
minWidth: minWidth,
|
|
85
|
+
maxWidth: maxWidth
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
measure(array, styleContextStack) {
|
|
89
|
+
if (array.length) {
|
|
90
|
+
let leadingIndent = _StyleContextStack.default.getStyleProperty(array[0], styleContextStack, 'leadingIndent', 0);
|
|
91
|
+
if (leadingIndent) {
|
|
92
|
+
array[0].leadingCut = -leadingIndent;
|
|
93
|
+
array[0].leadingIndent = leadingIndent;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
array.forEach(item => {
|
|
97
|
+
let font = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'font', 'Roboto');
|
|
98
|
+
let bold = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'bold', false);
|
|
99
|
+
let italics = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'italics', false);
|
|
100
|
+
item.font = this.pdfDocument.provideFont(font, bold, italics);
|
|
101
|
+
item.alignment = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'alignment', 'left');
|
|
102
|
+
item.fontSize = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'fontSize', 12);
|
|
103
|
+
item.fontFeatures = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'fontFeatures', null);
|
|
104
|
+
item.characterSpacing = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'characterSpacing', 0);
|
|
105
|
+
item.color = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'color', 'black');
|
|
106
|
+
item.decoration = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decoration', null);
|
|
107
|
+
item.decorationColor = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationColor', null);
|
|
108
|
+
item.decorationStyle = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationStyle', null);
|
|
109
|
+
item.decorationThickness = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationThickness', null);
|
|
110
|
+
item.background = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'background', null);
|
|
111
|
+
item.link = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'link', null);
|
|
112
|
+
item.linkToPage = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'linkToPage', null);
|
|
113
|
+
item.linkToDestination = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'linkToDestination', null);
|
|
114
|
+
item.noWrap = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'noWrap', null);
|
|
115
|
+
item.opacity = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'opacity', 1);
|
|
116
|
+
item.sup = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'sup', false);
|
|
117
|
+
item.sub = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'sub', false);
|
|
118
|
+
if (item.sup || item.sub) {
|
|
119
|
+
// font size reduction taken from here: https://en.wikipedia.org/wiki/Subscript_and_superscript#Desktop_publishing
|
|
120
|
+
item.fontSize *= 0.58;
|
|
121
|
+
}
|
|
122
|
+
let lineHeight = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'lineHeight', 1);
|
|
123
|
+
item.width = this.widthOfText(item.text, item);
|
|
124
|
+
item.height = item.font.lineHeight(item.fontSize) * lineHeight;
|
|
125
|
+
if (!item.leadingCut) {
|
|
126
|
+
item.leadingCut = 0;
|
|
127
|
+
}
|
|
128
|
+
let preserveLeadingSpaces = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'preserveLeadingSpaces', false);
|
|
129
|
+
if (!preserveLeadingSpaces) {
|
|
130
|
+
let leadingSpaces = item.text.match(LEADING);
|
|
131
|
+
if (leadingSpaces) {
|
|
132
|
+
item.leadingCut += this.widthOfText(leadingSpaces[0], item);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
item.trailingCut = 0;
|
|
136
|
+
let preserveTrailingSpaces = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'preserveTrailingSpaces', false);
|
|
137
|
+
if (!preserveTrailingSpaces) {
|
|
138
|
+
let trailingSpaces = item.text.match(TRAILING);
|
|
139
|
+
if (trailingSpaces) {
|
|
140
|
+
item.trailingCut = this.widthOfText(trailingSpaces[0], item);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}, this);
|
|
144
|
+
return array;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Width of text
|
|
149
|
+
*
|
|
150
|
+
* @param {string} text
|
|
151
|
+
* @param {object} inline
|
|
152
|
+
* @returns {number}
|
|
153
|
+
*/
|
|
154
|
+
widthOfText(text, inline) {
|
|
155
|
+
let width = inline.font.widthOfString(text, inline.fontSize, inline.fontFeatures);
|
|
156
|
+
if (inline.characterSpacing) {
|
|
157
|
+
// Include spacing after the last character to keep adjacent inlines spaced.
|
|
158
|
+
width += inline.characterSpacing * Array.from(text).length;
|
|
159
|
+
}
|
|
160
|
+
return width;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Returns size of the specified string (without breaking it) using the current style
|
|
165
|
+
*
|
|
166
|
+
* @param {string} text text to be measured
|
|
167
|
+
* @param {object} styleContextStack current style stack
|
|
168
|
+
* @returns {object} size of the specified string
|
|
169
|
+
*/
|
|
170
|
+
sizeOfText(text, styleContextStack) {
|
|
171
|
+
//TODO: refactor - extract from measure
|
|
172
|
+
let fontName = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'font', 'Roboto');
|
|
173
|
+
let fontSize = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'fontSize', 12);
|
|
174
|
+
let fontFeatures = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'fontFeatures', null);
|
|
175
|
+
let bold = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'bold', false);
|
|
176
|
+
let italics = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'italics', false);
|
|
177
|
+
let lineHeight = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'lineHeight', 1);
|
|
178
|
+
let characterSpacing = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'characterSpacing', 0);
|
|
179
|
+
let font = this.pdfDocument.provideFont(fontName, bold, italics);
|
|
180
|
+
return {
|
|
181
|
+
width: this.widthOfText(text, {
|
|
182
|
+
font: font,
|
|
183
|
+
fontSize: fontSize,
|
|
184
|
+
characterSpacing: characterSpacing,
|
|
185
|
+
fontFeatures: fontFeatures
|
|
186
|
+
}),
|
|
187
|
+
height: font.lineHeight(fontSize) * lineHeight,
|
|
188
|
+
fontSize: fontSize,
|
|
189
|
+
lineHeight: lineHeight,
|
|
190
|
+
ascender: font.ascender / 1000 * fontSize,
|
|
191
|
+
descender: font.descender / 1000 * fontSize
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Returns size of the specified rotated string (without breaking it) using the current style
|
|
197
|
+
*
|
|
198
|
+
* @param {string} text text to be measured
|
|
199
|
+
* @param {number} angle
|
|
200
|
+
* @param {object} styleContextStack current style stack
|
|
201
|
+
* @returns {object} size of the specified string
|
|
202
|
+
*/
|
|
203
|
+
sizeOfRotatedText(text, angle, styleContextStack) {
|
|
204
|
+
let angleRad = angle * Math.PI / -180;
|
|
205
|
+
let size = this.sizeOfText(text, styleContextStack);
|
|
206
|
+
return {
|
|
207
|
+
width: Math.abs(size.height * Math.sin(angleRad)) + Math.abs(size.width * Math.cos(angleRad)),
|
|
208
|
+
height: Math.abs(size.width * Math.sin(angleRad)) + Math.abs(size.height * Math.cos(angleRad))
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
var _default = exports.default = TextInlines;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
exports.fetchUrl = fetchUrl;
|
|
6
|
+
const MAX_REDIRECTS = 30;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {string} url
|
|
10
|
+
* @param {object} headers
|
|
11
|
+
* @param {(url: string) => boolean} urlAccessPolicy
|
|
12
|
+
* @returns {Promise<Response>}
|
|
13
|
+
*/
|
|
14
|
+
async function fetchUrl(url, headers = {}, urlAccessPolicy) {
|
|
15
|
+
for (let i = 0; i <= MAX_REDIRECTS; i++) {
|
|
16
|
+
if (typeof urlAccessPolicy !== 'undefined' && urlAccessPolicy(url) !== true) {
|
|
17
|
+
throw new Error(`Access to URL denied by resource access policy: ${url}`);
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
let response = await fetch(url, {
|
|
21
|
+
headers,
|
|
22
|
+
redirect: 'manual'
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// redirect url
|
|
26
|
+
if (response.status >= 300 && response.status < 400) {
|
|
27
|
+
let location = response.headers.get('location');
|
|
28
|
+
if (!location) {
|
|
29
|
+
throw new Error('Redirect response missing Location header');
|
|
30
|
+
}
|
|
31
|
+
url = new URL(location, url).href;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// browsers do not support redirect: 'manual'
|
|
36
|
+
if (response.type === 'opaqueredirect') {
|
|
37
|
+
response = await fetch(url, {
|
|
38
|
+
headers
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
throw new Error(`Failed to fetch (status code: ${response.status})`);
|
|
43
|
+
}
|
|
44
|
+
return response;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
// w-pdfmaker: code ENOENT marks a resource that could not be downloaded (see imageFallback.js)
|
|
47
|
+
throw Object.assign(new Error(`Network request failed (url: "${url}", error: ${error.message})`, {
|
|
48
|
+
cause: error
|
|
49
|
+
}), {
|
|
50
|
+
code: 'ENOENT'
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
throw new Error(`Network request failed (url: "${url}", error: Too many redirects)`);
|
|
55
|
+
}
|
|
56
|
+
class URLResolver {
|
|
57
|
+
constructor(fs, sharedFs) {
|
|
58
|
+
this.fs = fs;
|
|
59
|
+
this.sharedFs = sharedFs;
|
|
60
|
+
this.resolving = {};
|
|
61
|
+
this.urlAccessPolicy = undefined;
|
|
62
|
+
this.errors = new Map(); // w-pdfmaker: url => failed download held back by resolve(…, holdNotFound)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {(url: string) => boolean} callback
|
|
67
|
+
*/
|
|
68
|
+
setUrlAccessPolicy(callback) {
|
|
69
|
+
this.urlAccessPolicy = callback;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {string} url
|
|
74
|
+
* @param {object} headers
|
|
75
|
+
* @param {boolean} cache
|
|
76
|
+
* @param {boolean} holdNotFound w-pdfmaker: a failed download is kept in this.errors instead of rejecting,
|
|
77
|
+
* and thrown when the resource is used (images; `fallback` can then replace it)
|
|
78
|
+
* @returns {Promise}
|
|
79
|
+
*/
|
|
80
|
+
resolve(url, headers = {}, cache = false, holdNotFound = false) {
|
|
81
|
+
if (typeof url !== 'string') {
|
|
82
|
+
return Promise.resolve();
|
|
83
|
+
}
|
|
84
|
+
const key = JSON.stringify([url, cache === true, holdNotFound === true]);
|
|
85
|
+
const resolveUrlInternal = async () => {
|
|
86
|
+
if (url.toLowerCase().startsWith('https://') || url.toLowerCase().startsWith('http://')) {
|
|
87
|
+
if (this.fs.existsSync(url)) {
|
|
88
|
+
if (cache === true && !this.sharedFs.existsSync(url)) {
|
|
89
|
+
this.fs.unlinkSync(url);
|
|
90
|
+
} else {
|
|
91
|
+
return; // url was downloaded earlier
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const response = await fetchUrl(url, headers, this.urlAccessPolicy);
|
|
95
|
+
|
|
96
|
+
// validate access policy on redirected url (in browsers, only the final URL is validated)
|
|
97
|
+
if (response.redirected) {
|
|
98
|
+
if (typeof this.urlAccessPolicy !== 'undefined' && this.urlAccessPolicy(response.url) !== true) {
|
|
99
|
+
throw new Error(`Access to URL denied by resource access policy: ${response.url}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const buffer = await response.arrayBuffer();
|
|
103
|
+
const target = cache === true ? this.sharedFs : this.fs;
|
|
104
|
+
target.writeFileSync(url, buffer);
|
|
105
|
+
}
|
|
106
|
+
// else cannot be resolved
|
|
107
|
+
};
|
|
108
|
+
if (!this.resolving[key]) {
|
|
109
|
+
this.resolving[key] = holdNotFound !== true ? resolveUrlInternal() : resolveUrlInternal().catch(error => {
|
|
110
|
+
if (error.code !== 'ENOENT') {
|
|
111
|
+
throw error; // access policy denials stay immediate
|
|
112
|
+
}
|
|
113
|
+
this.errors.set(url, error);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return this.resolving[key];
|
|
117
|
+
}
|
|
118
|
+
resolved() {
|
|
119
|
+
return Promise.all(Object.values(this.resolving));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
var _default = exports.default = URLResolver;
|