pdfmake 0.2.4 → 0.3.0-beta.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 +10 -27
- package/README.md +11 -7
- package/build/pdfmake.js +44733 -42435
- package/build/pdfmake.js.map +1 -1
- package/build/pdfmake.min.js +2 -2
- package/build/pdfmake.min.js.map +1 -1
- package/build/standard-fonts/Courier.js +27 -0
- package/build/standard-fonts/Helvetica.js +27 -0
- package/build/standard-fonts/Symbol.js +21 -0
- package/build/standard-fonts/Times.js +27 -0
- package/build/standard-fonts/ZapfDingbats.js +21 -0
- package/build/vfs_fonts.js +2 -2
- package/build-vfs.js +2 -2
- 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/js/3rd-party/svg-to-pdfkit/source.js +4301 -0
- package/js/3rd-party/svg-to-pdfkit.js +11 -0
- package/js/DocMeasure.js +758 -0
- package/js/DocPreprocessor.js +291 -0
- package/js/DocumentContext.js +306 -0
- package/js/ElementWriter.js +400 -0
- package/js/LayoutBuilder.js +840 -0
- package/js/Line.js +125 -0
- package/js/OutputDocument.js +86 -0
- package/js/OutputDocumentServer.js +34 -0
- package/js/PDFDocument.js +190 -0
- package/js/PageElementWriter.js +165 -0
- package/js/PageSize.js +88 -0
- package/js/Printer.js +300 -0
- package/js/Renderer.js +463 -0
- package/js/SVGMeasure.js +89 -0
- package/js/StyleContextStack.js +206 -0
- package/js/TableProcessor.js +590 -0
- package/js/TextBreaker.js +182 -0
- package/js/TextDecorator.js +181 -0
- package/js/TextInlines.js +248 -0
- package/js/URLResolver.js +82 -0
- package/js/base.js +69 -0
- package/js/browser-extensions/OutputDocumentBrowser.js +131 -0
- package/js/browser-extensions/URLBrowserResolver.js +94 -0
- package/js/browser-extensions/fonts/Roboto.js +42 -0
- package/js/browser-extensions/index.js +70 -0
- package/js/browser-extensions/pdfMake.js +17 -0
- package/js/browser-extensions/standard-fonts/Courier.js +42 -0
- package/js/browser-extensions/standard-fonts/Helvetica.js +42 -0
- package/js/browser-extensions/standard-fonts/Symbol.js +27 -0
- package/js/browser-extensions/standard-fonts/Times.js +42 -0
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +27 -0
- package/js/browser-extensions/virtual-fs-cjs.js +3 -0
- package/js/columnCalculator.js +142 -0
- package/js/helpers/node.js +122 -0
- package/js/helpers/tools.js +48 -0
- package/js/helpers/variableType.js +52 -0
- package/js/index.js +21 -0
- package/js/qrEnc.js +810 -0
- package/js/standardPageSizes.js +57 -0
- package/js/tableLayouts.js +127 -0
- package/js/virtual-fs.js +77 -0
- package/package.json +26 -22
- package/src/3rd-party/svg-to-pdfkit.js +2 -2
- package/src/DocMeasure.js +703 -0
- package/src/DocPreprocessor.js +264 -0
- package/src/DocumentContext.js +309 -0
- package/src/ElementWriter.js +394 -0
- package/src/LayoutBuilder.js +821 -0
- package/src/Line.js +114 -0
- package/src/OutputDocument.js +78 -0
- package/src/OutputDocumentServer.js +26 -0
- package/src/PDFDocument.js +174 -0
- package/src/PageElementWriter.js +160 -0
- package/src/PageSize.js +53 -0
- package/src/Printer.js +277 -0
- package/src/Renderer.js +405 -0
- package/src/SVGMeasure.js +79 -0
- package/src/StyleContextStack.js +216 -0
- package/src/TableProcessor.js +558 -0
- package/src/TextBreaker.js +149 -0
- package/src/TextDecorator.js +161 -0
- package/src/TextInlines.js +223 -0
- package/src/URLResolver.js +72 -0
- package/src/base.js +61 -0
- package/src/browser-extensions/OutputDocumentBrowser.js +117 -0
- package/src/browser-extensions/URLBrowserResolver.js +49 -53
- package/src/browser-extensions/fonts/Roboto.js +27 -0
- package/src/browser-extensions/index.js +55 -0
- package/src/browser-extensions/pdfMake.js +10 -282
- package/src/browser-extensions/standard-fonts/Courier.js +27 -0
- package/src/browser-extensions/standard-fonts/Helvetica.js +27 -0
- package/src/browser-extensions/standard-fonts/Symbol.js +21 -0
- package/src/browser-extensions/standard-fonts/Times.js +27 -0
- package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -0
- package/src/browser-extensions/virtual-fs-cjs.js +1 -0
- package/src/columnCalculator.js +29 -32
- package/src/helpers/node.js +110 -0
- package/src/helpers/tools.js +39 -0
- package/src/helpers/variableType.js +39 -0
- package/src/index.js +16 -0
- package/src/qrEnc.js +15 -10
- package/src/standardPageSizes.js +1 -3
- package/src/tableLayouts.js +100 -0
- package/src/virtual-fs.js +66 -0
- package/standard-fonts/Courier.js +8 -0
- package/standard-fonts/Helvetica.js +9 -0
- package/standard-fonts/Symbol.js +5 -0
- package/standard-fonts/Times.js +8 -0
- package/standard-fonts/ZapfDingbats.js +5 -0
- package/src/browser-extensions/virtual-fs.js +0 -55
- package/src/docMeasure.js +0 -807
- package/src/docPreprocessor.js +0 -255
- package/src/documentContext.js +0 -314
- package/src/elementWriter.js +0 -322
- package/src/fontProvider.js +0 -68
- package/src/helpers.js +0 -126
- package/src/imageMeasure.js +0 -51
- package/src/layoutBuilder.js +0 -807
- package/src/line.js +0 -91
- package/src/pageElementWriter.js +0 -174
- package/src/pdfKitEngine.js +0 -21
- package/src/printer.js +0 -705
- package/src/styleContextStack.js +0 -179
- package/src/svgMeasure.js +0 -70
- package/src/tableProcessor.js +0 -561
- package/src/textDecorator.js +0 -157
- package/src/textTools.js +0 -373
- package/src/traversalTracker.js +0 -47
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
|
|
6
|
+
var _linebreak = _interopRequireDefault(require("@foliojs-fork/linebreak"));
|
|
7
|
+
|
|
8
|
+
var _variableType = require("./helpers/variableType");
|
|
9
|
+
|
|
10
|
+
var _StyleContextStack = _interopRequireDefault(require("./StyleContextStack"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} text
|
|
16
|
+
* @param {boolean} noWrap
|
|
17
|
+
* @returns {Array}
|
|
18
|
+
*/
|
|
19
|
+
const splitWords = (text, noWrap) => {
|
|
20
|
+
let words = [];
|
|
21
|
+
|
|
22
|
+
if (noWrap) {
|
|
23
|
+
words.push({
|
|
24
|
+
text: text
|
|
25
|
+
});
|
|
26
|
+
return words;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let breaker = new _linebreak.default(text);
|
|
30
|
+
let last = 0;
|
|
31
|
+
let bk;
|
|
32
|
+
|
|
33
|
+
while (bk = breaker.nextBreak()) {
|
|
34
|
+
let word = text.slice(last, bk.position);
|
|
35
|
+
|
|
36
|
+
if (bk.required || word.match(/\r?\n$|\r$/)) {
|
|
37
|
+
// new line
|
|
38
|
+
word = word.replace(/\r?\n$|\r$/, '');
|
|
39
|
+
words.push({
|
|
40
|
+
text: word,
|
|
41
|
+
lineEnd: true
|
|
42
|
+
});
|
|
43
|
+
} else {
|
|
44
|
+
words.push({
|
|
45
|
+
text: word
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
last = bk.position;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return words;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* @param {Array} words
|
|
56
|
+
* @param {boolean} noWrap
|
|
57
|
+
* @returns {?string}
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
const getFirstWord = (words, noWrap) => {
|
|
62
|
+
let word = words[0];
|
|
63
|
+
|
|
64
|
+
if (word === undefined) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (noWrap) {
|
|
69
|
+
// text was not wrapped, we need only first word
|
|
70
|
+
let tmpWords = splitWords(word.text, false);
|
|
71
|
+
|
|
72
|
+
if (tmpWords[0] === undefined) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
word = tmpWords[0];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return word.text;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* @param {Array} words
|
|
83
|
+
* @param {boolean} noWrap
|
|
84
|
+
* @returns {?string}
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
const getLastWord = (words, noWrap) => {
|
|
89
|
+
let word = words[words.length - 1];
|
|
90
|
+
|
|
91
|
+
if (word === undefined) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (word.lineEnd) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (noWrap) {
|
|
100
|
+
// text was not wrapped, we need only last word
|
|
101
|
+
let tmpWords = splitWords(word.text, false);
|
|
102
|
+
|
|
103
|
+
if (tmpWords[tmpWords.length - 1] === undefined) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
word = tmpWords[tmpWords.length - 1];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return word.text;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
class TextBreaker {
|
|
114
|
+
/**
|
|
115
|
+
* @param {string|Array} texts
|
|
116
|
+
* @param {StyleContextStack} styleContextStack
|
|
117
|
+
* @returns {Array}
|
|
118
|
+
*/
|
|
119
|
+
getBreaks(texts, styleContextStack) {
|
|
120
|
+
let results = [];
|
|
121
|
+
|
|
122
|
+
if (!Array.isArray(texts)) {
|
|
123
|
+
texts = [texts];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
let lastWord = null;
|
|
127
|
+
|
|
128
|
+
for (let i = 0, l = texts.length; i < l; i++) {
|
|
129
|
+
let item = texts[i];
|
|
130
|
+
let style = null;
|
|
131
|
+
let words;
|
|
132
|
+
|
|
133
|
+
let noWrap = _StyleContextStack.default.getStyleProperty(item || {}, styleContextStack, 'noWrap', false);
|
|
134
|
+
|
|
135
|
+
if ((0, _variableType.isObject)(item)) {
|
|
136
|
+
if (item._textRef && item._textRef._textNodeRef.text) {
|
|
137
|
+
item.text = item._textRef._textNodeRef.text;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
words = splitWords(item.text, noWrap);
|
|
141
|
+
style = _StyleContextStack.default.copyStyle(item);
|
|
142
|
+
} else {
|
|
143
|
+
words = splitWords(item, noWrap);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (lastWord && words.length) {
|
|
147
|
+
let firstWord = getFirstWord(words, noWrap);
|
|
148
|
+
let wrapWords = splitWords(lastWord + firstWord, false);
|
|
149
|
+
|
|
150
|
+
if (wrapWords.length === 1) {
|
|
151
|
+
results[results.length - 1].noNewLine = true;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
for (let i2 = 0, l2 = words.length; i2 < l2; i2++) {
|
|
156
|
+
let result = {
|
|
157
|
+
text: words[i2].text
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
if (words[i2].lineEnd) {
|
|
161
|
+
result.lineEnd = true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
_StyleContextStack.default.copyStyle(style, result);
|
|
165
|
+
|
|
166
|
+
results.push(result);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
lastWord = null;
|
|
170
|
+
|
|
171
|
+
if (i + 1 < l) {
|
|
172
|
+
lastWord = getLastWord(words, noWrap);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return results;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
var _default = TextBreaker;
|
|
182
|
+
exports.default = _default;
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
|
|
6
|
+
const groupDecorations = line => {
|
|
7
|
+
let groups = [];
|
|
8
|
+
let currentGroup = null;
|
|
9
|
+
|
|
10
|
+
for (let i = 0, l = line.inlines.length; i < l; i++) {
|
|
11
|
+
let inline = line.inlines[i];
|
|
12
|
+
let decoration = inline.decoration;
|
|
13
|
+
|
|
14
|
+
if (!decoration) {
|
|
15
|
+
currentGroup = null;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!Array.isArray(decoration)) {
|
|
20
|
+
decoration = [decoration];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let color = inline.decorationColor || inline.color || 'black';
|
|
24
|
+
let style = inline.decorationStyle || 'solid';
|
|
25
|
+
|
|
26
|
+
for (let ii = 0, ll = decoration.length; ii < ll; ii++) {
|
|
27
|
+
let decorationItem = decoration[ii];
|
|
28
|
+
|
|
29
|
+
if (!currentGroup || decorationItem !== currentGroup.decoration || style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor) {
|
|
30
|
+
currentGroup = {
|
|
31
|
+
line: line,
|
|
32
|
+
decoration: decorationItem,
|
|
33
|
+
decorationColor: color,
|
|
34
|
+
decorationStyle: style,
|
|
35
|
+
inlines: [inline]
|
|
36
|
+
};
|
|
37
|
+
groups.push(currentGroup);
|
|
38
|
+
} else {
|
|
39
|
+
currentGroup.inlines.push(inline);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return groups;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
class TextDecorator {
|
|
48
|
+
constructor(pdfDocument) {
|
|
49
|
+
this.pdfDocument = pdfDocument;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
drawBackground(line, x, y) {
|
|
53
|
+
let height = line.getHeight();
|
|
54
|
+
|
|
55
|
+
for (let i = 0, l = line.inlines.length; i < l; i++) {
|
|
56
|
+
let inline = line.inlines[i];
|
|
57
|
+
|
|
58
|
+
if (!inline.background) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let color = inline.background;
|
|
63
|
+
let patternColor = this.pdfDocument.providePattern(inline.background);
|
|
64
|
+
|
|
65
|
+
if (patternColor !== null) {
|
|
66
|
+
color = patternColor;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let justifyShift = inline.justifyShift || 0;
|
|
70
|
+
this.pdfDocument.fillColor(color).rect(x + inline.x - justifyShift, y, inline.width + justifyShift, height).fill();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
drawDecorations(line, x, y) {
|
|
75
|
+
let groups = groupDecorations(line);
|
|
76
|
+
|
|
77
|
+
for (let i = 0, l = groups.length; i < l; i++) {
|
|
78
|
+
this._drawDecoration(groups[i], x, y);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
_drawDecoration(group, x, y) {
|
|
83
|
+
const maxInline = () => {
|
|
84
|
+
let max = 0;
|
|
85
|
+
|
|
86
|
+
for (let i = 0, l = group.inlines.length; i < l; i++) {
|
|
87
|
+
let inline = group.inlines[i];
|
|
88
|
+
max = inline.fontSize > max ? i : max;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return group.inlines[max];
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const width = () => {
|
|
95
|
+
let sum = 0;
|
|
96
|
+
|
|
97
|
+
for (let i = 0, l = group.inlines.length; i < l; i++) {
|
|
98
|
+
let justifyShift = group.inlines[i].justifyShift || 0;
|
|
99
|
+
sum += group.inlines[i].width + justifyShift;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return sum;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
let firstInline = group.inlines[0];
|
|
106
|
+
let biggerInline = maxInline();
|
|
107
|
+
let totalWidth = width();
|
|
108
|
+
let lineAscent = group.line.getAscenderHeight();
|
|
109
|
+
let ascent = biggerInline.font.ascender / 1000 * biggerInline.fontSize;
|
|
110
|
+
let height = biggerInline.height;
|
|
111
|
+
let descent = height - ascent;
|
|
112
|
+
let lw = 0.5 + Math.floor(Math.max(biggerInline.fontSize - 8, 0) / 2) * 0.12;
|
|
113
|
+
|
|
114
|
+
switch (group.decoration) {
|
|
115
|
+
case 'underline':
|
|
116
|
+
y += lineAscent + descent * 0.45;
|
|
117
|
+
break;
|
|
118
|
+
|
|
119
|
+
case 'overline':
|
|
120
|
+
y += lineAscent - ascent * 0.85;
|
|
121
|
+
break;
|
|
122
|
+
|
|
123
|
+
case 'lineThrough':
|
|
124
|
+
y += lineAscent - ascent * 0.25;
|
|
125
|
+
break;
|
|
126
|
+
|
|
127
|
+
default:
|
|
128
|
+
throw new Error(`Unkown decoration : ${group.decoration}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
this.pdfDocument.save();
|
|
132
|
+
|
|
133
|
+
if (group.decorationStyle === 'double') {
|
|
134
|
+
let gap = Math.max(0.5, lw * 2);
|
|
135
|
+
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();
|
|
136
|
+
} else if (group.decorationStyle === 'dashed') {
|
|
137
|
+
let nbDashes = Math.ceil(totalWidth / (3.96 + 2.84));
|
|
138
|
+
let rdx = x + firstInline.x;
|
|
139
|
+
this.pdfDocument.rect(rdx, y, totalWidth, lw).clip();
|
|
140
|
+
this.pdfDocument.fillColor(group.decorationColor);
|
|
141
|
+
|
|
142
|
+
for (let i = 0; i < nbDashes; i++) {
|
|
143
|
+
this.pdfDocument.rect(rdx, y - lw / 2, 3.96, lw).fill();
|
|
144
|
+
rdx += 3.96 + 2.84;
|
|
145
|
+
}
|
|
146
|
+
} else if (group.decorationStyle === 'dotted') {
|
|
147
|
+
let nbDots = Math.ceil(totalWidth / (lw * 3));
|
|
148
|
+
let rx = x + firstInline.x;
|
|
149
|
+
this.pdfDocument.rect(rx, y, totalWidth, lw).clip();
|
|
150
|
+
this.pdfDocument.fillColor(group.decorationColor);
|
|
151
|
+
|
|
152
|
+
for (let i = 0; i < nbDots; i++) {
|
|
153
|
+
this.pdfDocument.rect(rx, y - lw / 2, lw, lw).fill();
|
|
154
|
+
rx += lw * 3;
|
|
155
|
+
}
|
|
156
|
+
} else if (group.decorationStyle === 'wavy') {
|
|
157
|
+
let sh = 0.7,
|
|
158
|
+
sv = 1;
|
|
159
|
+
let nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
|
|
160
|
+
let rwx = x + firstInline.x - 1;
|
|
161
|
+
this.pdfDocument.rect(x + firstInline.x, y - sv, totalWidth, y + sv).clip();
|
|
162
|
+
this.pdfDocument.lineWidth(0.24);
|
|
163
|
+
this.pdfDocument.moveTo(rwx, y);
|
|
164
|
+
|
|
165
|
+
for (let i = 0; i < nbWaves; i++) {
|
|
166
|
+
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);
|
|
167
|
+
rwx += sh * 6;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
this.pdfDocument.stroke(group.decorationColor);
|
|
171
|
+
} else {
|
|
172
|
+
this.pdfDocument.fillColor(group.decorationColor).rect(x + firstInline.x, y - lw / 2, totalWidth, lw).fill();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
this.pdfDocument.restore();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
var _default = TextDecorator;
|
|
181
|
+
exports.default = _default;
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
|
|
6
|
+
var _TextBreaker = _interopRequireDefault(require("./TextBreaker"));
|
|
7
|
+
|
|
8
|
+
var _StyleContextStack = _interopRequireDefault(require("./StyleContextStack"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
const LEADING = /^(\s)+/g;
|
|
13
|
+
const TRAILING = /(\s)+$/g;
|
|
14
|
+
/**
|
|
15
|
+
* @param {Array} array
|
|
16
|
+
* @returns {Array}
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const flattenTextArray = array => {
|
|
20
|
+
function flatten(array) {
|
|
21
|
+
return array.reduce((prev, cur) => {
|
|
22
|
+
let current = Array.isArray(cur.text) ? flatten(cur.text) : cur;
|
|
23
|
+
let more = [].concat(current).some(Array.isArray);
|
|
24
|
+
return prev.concat(more ? flatten(current) : current);
|
|
25
|
+
}, []);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!Array.isArray(array)) {
|
|
29
|
+
array = [array];
|
|
30
|
+
} // TODO: Styling in nested text (issue: https://github.com/bpampuch/pdfmake/issues/1174)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
array = flatten(array);
|
|
34
|
+
return array;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Text measurement utility
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class TextInlines {
|
|
42
|
+
/**
|
|
43
|
+
* @param {object} pdfDocument object is instance of PDFDocument
|
|
44
|
+
*/
|
|
45
|
+
constructor(pdfDocument) {
|
|
46
|
+
this.pdfDocument = pdfDocument;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Converts an array of strings (or inline-definition-objects) into a collection
|
|
50
|
+
* of inlines and calculated minWidth/maxWidth and their min/max widths
|
|
51
|
+
*
|
|
52
|
+
* @param {Array} textArray an array of inline-definition-objects (or strings)
|
|
53
|
+
* @param {StyleContextStack} styleContextStack current style stack
|
|
54
|
+
* @returns {object} collection of inlines, minWidth, maxWidth
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
buildInlines(textArray, styleContextStack) {
|
|
59
|
+
const getTrimmedWidth = item => {
|
|
60
|
+
return Math.max(0, item.width - item.leadingCut - item.trailingCut);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
let minWidth = 0;
|
|
64
|
+
let maxWidth = 0;
|
|
65
|
+
let currentLineWidth;
|
|
66
|
+
let flattenedTextArray = flattenTextArray(textArray);
|
|
67
|
+
const textBreaker = new _TextBreaker.default();
|
|
68
|
+
let breakedText = textBreaker.getBreaks(flattenedTextArray, styleContextStack);
|
|
69
|
+
let measuredText = this.measure(breakedText, styleContextStack);
|
|
70
|
+
measuredText.forEach(inline => {
|
|
71
|
+
minWidth = Math.max(minWidth, getTrimmedWidth(inline));
|
|
72
|
+
|
|
73
|
+
if (!currentLineWidth) {
|
|
74
|
+
currentLineWidth = {
|
|
75
|
+
width: 0,
|
|
76
|
+
leadingCut: inline.leadingCut,
|
|
77
|
+
trailingCut: 0
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
currentLineWidth.width += inline.width;
|
|
82
|
+
currentLineWidth.trailingCut = inline.trailingCut;
|
|
83
|
+
maxWidth = Math.max(maxWidth, getTrimmedWidth(currentLineWidth));
|
|
84
|
+
|
|
85
|
+
if (inline.lineEnd) {
|
|
86
|
+
currentLineWidth = null;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (_StyleContextStack.default.getStyleProperty({}, styleContextStack, 'noWrap', false)) {
|
|
91
|
+
minWidth = maxWidth;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
items: measuredText,
|
|
96
|
+
minWidth: minWidth,
|
|
97
|
+
maxWidth: maxWidth
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
measure(array, styleContextStack) {
|
|
102
|
+
if (array.length) {
|
|
103
|
+
let leadingIndent = _StyleContextStack.default.getStyleProperty(array[0], styleContextStack, 'leadingIndent', 0);
|
|
104
|
+
|
|
105
|
+
if (leadingIndent) {
|
|
106
|
+
array[0].leadingCut = -leadingIndent;
|
|
107
|
+
array[0].leadingIndent = leadingIndent;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
array.forEach(item => {
|
|
112
|
+
let font = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'font', 'Roboto');
|
|
113
|
+
|
|
114
|
+
let bold = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'bold', false);
|
|
115
|
+
|
|
116
|
+
let italics = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'italics', false);
|
|
117
|
+
|
|
118
|
+
item.font = this.pdfDocument.provideFont(font, bold, italics);
|
|
119
|
+
item.alignment = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'alignment', 'left');
|
|
120
|
+
item.fontSize = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'fontSize', 12);
|
|
121
|
+
item.fontFeatures = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'fontFeatures', null);
|
|
122
|
+
item.characterSpacing = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'characterSpacing', 0);
|
|
123
|
+
item.color = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'color', 'black');
|
|
124
|
+
item.decoration = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decoration', null);
|
|
125
|
+
item.decorationColor = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationColor', null);
|
|
126
|
+
item.decorationStyle = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationStyle', null);
|
|
127
|
+
item.background = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'background', null);
|
|
128
|
+
item.link = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'link', null);
|
|
129
|
+
item.linkToPage = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'linkToPage', null);
|
|
130
|
+
item.linkToDestination = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'linkToDestination', null);
|
|
131
|
+
item.noWrap = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'noWrap', null);
|
|
132
|
+
item.opacity = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'opacity', 1);
|
|
133
|
+
item.sup = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'sup', false);
|
|
134
|
+
item.sub = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'sub', false);
|
|
135
|
+
|
|
136
|
+
if (item.sup || item.sub) {
|
|
137
|
+
// font size reduction taken from here: https://en.wikipedia.org/wiki/Subscript_and_superscript#Desktop_publishing
|
|
138
|
+
item.fontSize *= 0.58;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let lineHeight = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'lineHeight', 1);
|
|
142
|
+
|
|
143
|
+
item.width = this.widthOfText(item.text, item);
|
|
144
|
+
item.height = item.font.lineHeight(item.fontSize) * lineHeight;
|
|
145
|
+
|
|
146
|
+
if (!item.leadingCut) {
|
|
147
|
+
item.leadingCut = 0;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let preserveLeadingSpaces = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'preserveLeadingSpaces', false);
|
|
151
|
+
|
|
152
|
+
if (!preserveLeadingSpaces) {
|
|
153
|
+
let leadingSpaces = item.text.match(LEADING);
|
|
154
|
+
|
|
155
|
+
if (leadingSpaces) {
|
|
156
|
+
item.leadingCut += this.widthOfText(leadingSpaces[0], item);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
item.trailingCut = 0;
|
|
161
|
+
|
|
162
|
+
let preserveTrailingSpaces = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'preserveTrailingSpaces', false);
|
|
163
|
+
|
|
164
|
+
if (!preserveTrailingSpaces) {
|
|
165
|
+
let trailingSpaces = item.text.match(TRAILING);
|
|
166
|
+
|
|
167
|
+
if (trailingSpaces) {
|
|
168
|
+
item.trailingCut = this.widthOfText(trailingSpaces[0], item);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}, this);
|
|
172
|
+
return array;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Width of text
|
|
176
|
+
*
|
|
177
|
+
* @param {string} text
|
|
178
|
+
* @param {object} inline
|
|
179
|
+
* @returns {number}
|
|
180
|
+
*/
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
widthOfText(text, inline) {
|
|
184
|
+
return inline.font.widthOfString(text, inline.fontSize, inline.fontFeatures) + (inline.characterSpacing || 0) * (text.length - 1);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Returns size of the specified string (without breaking it) using the current style
|
|
188
|
+
*
|
|
189
|
+
* @param {string} text text to be measured
|
|
190
|
+
* @param {object} styleContextStack current style stack
|
|
191
|
+
* @returns {object} size of the specified string
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
sizeOfText(text, styleContextStack) {
|
|
196
|
+
//TODO: refactor - extract from measure
|
|
197
|
+
let fontName = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'font', 'Roboto');
|
|
198
|
+
|
|
199
|
+
let fontSize = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'fontSize', 12);
|
|
200
|
+
|
|
201
|
+
let fontFeatures = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'fontFeatures', null);
|
|
202
|
+
|
|
203
|
+
let bold = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'bold', false);
|
|
204
|
+
|
|
205
|
+
let italics = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'italics', false);
|
|
206
|
+
|
|
207
|
+
let lineHeight = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'lineHeight', 1);
|
|
208
|
+
|
|
209
|
+
let characterSpacing = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'characterSpacing', 0);
|
|
210
|
+
|
|
211
|
+
let font = this.pdfDocument.provideFont(fontName, bold, italics);
|
|
212
|
+
return {
|
|
213
|
+
width: this.widthOfText(text, {
|
|
214
|
+
font: font,
|
|
215
|
+
fontSize: fontSize,
|
|
216
|
+
characterSpacing: characterSpacing,
|
|
217
|
+
fontFeatures: fontFeatures
|
|
218
|
+
}),
|
|
219
|
+
height: font.lineHeight(fontSize) * lineHeight,
|
|
220
|
+
fontSize: fontSize,
|
|
221
|
+
lineHeight: lineHeight,
|
|
222
|
+
ascender: font.ascender / 1000 * fontSize,
|
|
223
|
+
descender: font.descender / 1000 * fontSize
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Returns size of the specified rotated string (without breaking it) using the current style
|
|
228
|
+
*
|
|
229
|
+
* @param {string} text text to be measured
|
|
230
|
+
* @param {number} angle
|
|
231
|
+
* @param {object} styleContextStack current style stack
|
|
232
|
+
* @returns {object} size of the specified string
|
|
233
|
+
*/
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
sizeOfRotatedText(text, angle, styleContextStack) {
|
|
237
|
+
let angleRad = angle * Math.PI / -180;
|
|
238
|
+
let size = this.sizeOfText(text, styleContextStack);
|
|
239
|
+
return {
|
|
240
|
+
width: Math.abs(size.height * Math.sin(angleRad)) + Math.abs(size.width * Math.cos(angleRad)),
|
|
241
|
+
height: Math.abs(size.width * Math.sin(angleRad)) + Math.abs(size.height * Math.cos(angleRad))
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
var _default = TextInlines;
|
|
248
|
+
exports.default = _default;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
|
|
6
|
+
var _http = _interopRequireDefault(require("http"));
|
|
7
|
+
|
|
8
|
+
var _https = _interopRequireDefault(require("https"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
const fetchUrl = (url, headers = {}) => {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const parsedUrl = new URL(url);
|
|
15
|
+
const h = parsedUrl.protocol === 'https:' ? _https.default : _http.default;
|
|
16
|
+
let options = {
|
|
17
|
+
headers: headers
|
|
18
|
+
};
|
|
19
|
+
h.get(url, options, res => {
|
|
20
|
+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
21
|
+
// redirect url
|
|
22
|
+
fetchUrl(res.headers.location).then(buffer => {
|
|
23
|
+
resolve(buffer);
|
|
24
|
+
}, result => {
|
|
25
|
+
reject(result);
|
|
26
|
+
});
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ok = res.statusCode >= 200 && res.statusCode < 300;
|
|
31
|
+
|
|
32
|
+
if (!ok) {
|
|
33
|
+
reject(new TypeError(`Failed to fetch (status code: ${res.statusCode}, url: "${url}")`));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const chunks = [];
|
|
37
|
+
res.on('end', () => resolve(Buffer.concat(chunks)));
|
|
38
|
+
res.on('data', d => chunks.push(d));
|
|
39
|
+
}).on('error', reject);
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
class URLResolver {
|
|
44
|
+
constructor(fs) {
|
|
45
|
+
this.fs = fs;
|
|
46
|
+
this.resolving = {};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
resolve(url, headers = {}) {
|
|
50
|
+
if (!this.resolving[url]) {
|
|
51
|
+
this.resolving[url] = new Promise((resolve, reject) => {
|
|
52
|
+
if (url.toLowerCase().indexOf('https://') === 0 || url.toLowerCase().indexOf('http://') === 0) {
|
|
53
|
+
fetchUrl(url, headers).then(buffer => {
|
|
54
|
+
this.fs.writeFileSync(url, buffer);
|
|
55
|
+
resolve();
|
|
56
|
+
}, result => {
|
|
57
|
+
reject(result);
|
|
58
|
+
});
|
|
59
|
+
} else {
|
|
60
|
+
// cannot be resolved
|
|
61
|
+
resolve();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return this.resolving[url];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
resolved() {
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
Promise.all(Object.values(this.resolving)).then(() => {
|
|
72
|
+
resolve();
|
|
73
|
+
}, result => {
|
|
74
|
+
reject(result);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
var _default = URLResolver;
|
|
82
|
+
exports.default = _default;
|