@digicole/pdfmake-rtl 2.1.0 → 2.1.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.
Files changed (65) hide show
  1. package/CHANGELOG.md +118 -83
  2. package/README.md +11 -10
  3. package/build/pdfmake.js +71 -42
  4. package/build/pdfmake.js.map +1 -1
  5. package/build/pdfmake.min.js +2 -2
  6. package/build/pdfmake.min.js.map +1 -1
  7. package/build/vfs_fonts.js +11 -11
  8. package/js/3rd-party/svg-to-pdfkit/source.js +3823 -0
  9. package/js/3rd-party/svg-to-pdfkit.js +7 -0
  10. package/js/DocMeasure.js +713 -0
  11. package/js/DocPreprocessor.js +275 -0
  12. package/js/DocumentContext.js +310 -0
  13. package/js/ElementWriter.js +687 -0
  14. package/js/LayoutBuilder.js +1240 -0
  15. package/js/Line.js +113 -0
  16. package/js/OutputDocument.js +64 -0
  17. package/js/OutputDocumentServer.js +29 -0
  18. package/js/PDFDocument.js +144 -0
  19. package/js/PageElementWriter.js +161 -0
  20. package/js/PageSize.js +74 -0
  21. package/js/Printer.js +351 -0
  22. package/js/Renderer.js +417 -0
  23. package/js/SVGMeasure.js +92 -0
  24. package/js/StyleContextStack.js +191 -0
  25. package/js/TableProcessor.js +575 -0
  26. package/js/TextBreaker.js +166 -0
  27. package/js/TextDecorator.js +152 -0
  28. package/js/TextInlines.js +244 -0
  29. package/js/URLResolver.js +43 -0
  30. package/js/base.js +59 -0
  31. package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
  32. package/js/browser-extensions/fonts/Cairo.js +38 -0
  33. package/js/browser-extensions/fonts/Roboto.js +38 -0
  34. package/js/browser-extensions/index.js +59 -0
  35. package/js/browser-extensions/pdfMake.js +3 -0
  36. package/js/browser-extensions/standard-fonts/Courier.js +38 -0
  37. package/js/browser-extensions/standard-fonts/Helvetica.js +38 -0
  38. package/js/browser-extensions/standard-fonts/Symbol.js +23 -0
  39. package/js/browser-extensions/standard-fonts/Times.js +38 -0
  40. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +23 -0
  41. package/js/browser-extensions/virtual-fs-cjs.js +3 -0
  42. package/js/columnCalculator.js +148 -0
  43. package/js/helpers/node.js +123 -0
  44. package/js/helpers/tools.js +46 -0
  45. package/js/helpers/variableType.js +59 -0
  46. package/js/index.js +15 -0
  47. package/js/qrEnc.js +721 -0
  48. package/js/rtlUtils.js +519 -0
  49. package/js/standardPageSizes.js +56 -0
  50. package/js/tableLayouts.js +98 -0
  51. package/js/virtual-fs.js +60 -0
  52. package/package.json +1 -1
  53. package/src/{docMeasure.js → DocMeasure.js} +8 -8
  54. package/src/{elementWriter.js → ElementWriter.js} +3 -3
  55. package/src/{layoutBuilder.js → LayoutBuilder.js} +1406 -1393
  56. package/src/{tableProcessor.js → TableProcessor.js} +633 -620
  57. package/src/rtlUtils.js +503 -500
  58. /package/src/{docPreprocessor.js → DocPreprocessor.js} +0 -0
  59. /package/src/{documentContext.js → DocumentContext.js} +0 -0
  60. /package/src/{line.js → Line.js} +0 -0
  61. /package/src/{pageElementWriter.js → PageElementWriter.js} +0 -0
  62. /package/src/{printer.js → Printer.js} +0 -0
  63. /package/src/{svgMeasure.js → SVGMeasure.js} +0 -0
  64. /package/src/{styleContextStack.js → StyleContextStack.js} +0 -0
  65. /package/src/{textDecorator.js → TextDecorator.js} +0 -0
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _linebreak = _interopRequireDefault(require("linebreak"));
6
+ var _variableType = require("./helpers/variableType");
7
+ var _StyleContextStack = _interopRequireDefault(require("./StyleContextStack"));
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 text.split('').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 breaker = new _linebreak.default(text);
49
+ let last = 0;
50
+ let bk;
51
+ while (bk = breaker.nextBreak()) {
52
+ let word = text.slice(last, bk.position);
53
+ if (bk.required || word.match(/\r?\n$|\r$/)) {
54
+ // new line
55
+ word = word.replace(/\r?\n$|\r$/, '');
56
+ words.push({
57
+ text: word,
58
+ lineEnd: true
59
+ });
60
+ } else {
61
+ words.push({
62
+ text: word
63
+ });
64
+ }
65
+ last = bk.position;
66
+ }
67
+ return words;
68
+ };
69
+
70
+ /**
71
+ * @param {Array} words
72
+ * @param {boolean} noWrap
73
+ * @returns {?string}
74
+ */
75
+ const getFirstWord = (words, noWrap) => {
76
+ let word = words[0];
77
+ if (word === undefined) {
78
+ return null;
79
+ }
80
+ if (noWrap) {
81
+ // text was not wrapped, we need only first word
82
+ let tmpWords = splitWords(word.text, false);
83
+ if (tmpWords[0] === undefined) {
84
+ return null;
85
+ }
86
+ word = tmpWords[0];
87
+ }
88
+ return word.text;
89
+ };
90
+
91
+ /**
92
+ * @param {Array} words
93
+ * @param {boolean} noWrap
94
+ * @returns {?string}
95
+ */
96
+ const getLastWord = (words, noWrap) => {
97
+ let word = words[words.length - 1];
98
+ if (word === undefined) {
99
+ return null;
100
+ }
101
+ if (word.lineEnd) {
102
+ return null;
103
+ }
104
+ if (noWrap) {
105
+ // text was not wrapped, we need only last word
106
+ let tmpWords = splitWords(word.text, false);
107
+ if (tmpWords[tmpWords.length - 1] === undefined) {
108
+ return null;
109
+ }
110
+ word = tmpWords[tmpWords.length - 1];
111
+ }
112
+ return word.text;
113
+ };
114
+ class TextBreaker {
115
+ /**
116
+ * @param {string|Array} texts
117
+ * @param {StyleContextStack} styleContextStack
118
+ * @returns {Array}
119
+ */
120
+ getBreaks(texts, styleContextStack) {
121
+ let results = [];
122
+ if (!Array.isArray(texts)) {
123
+ texts = [texts];
124
+ }
125
+ let lastWord = null;
126
+ for (let i = 0, l = texts.length; i < l; i++) {
127
+ let item = texts[i];
128
+ let style = null;
129
+ let words;
130
+ let breakAll = _StyleContextStack.default.getStyleProperty(item || {}, styleContextStack, 'wordBreak', 'normal') === 'break-all';
131
+ let noWrap = _StyleContextStack.default.getStyleProperty(item || {}, styleContextStack, 'noWrap', false);
132
+ if ((0, _variableType.isObject)(item)) {
133
+ if (item._textRef && item._textRef._textNodeRef.text) {
134
+ item.text = item._textRef._textNodeRef.text;
135
+ }
136
+ words = splitWords(item.text, noWrap, breakAll);
137
+ style = _StyleContextStack.default.copyStyle(item);
138
+ } else {
139
+ words = splitWords(item, noWrap, breakAll);
140
+ }
141
+ if (lastWord && words.length) {
142
+ let firstWord = getFirstWord(words, noWrap);
143
+ let wrapWords = splitWords(lastWord + firstWord, false);
144
+ if (wrapWords.length === 1) {
145
+ results[results.length - 1].noNewLine = true;
146
+ }
147
+ }
148
+ for (let i2 = 0, l2 = words.length; i2 < l2; i2++) {
149
+ let result = {
150
+ text: words[i2].text
151
+ };
152
+ if (words[i2].lineEnd) {
153
+ result.lineEnd = true;
154
+ }
155
+ _StyleContextStack.default.copyStyle(style, result);
156
+ results.push(result);
157
+ }
158
+ lastWord = null;
159
+ if (i + 1 < l) {
160
+ lastWord = getLastWord(words, noWrap);
161
+ }
162
+ }
163
+ return results;
164
+ }
165
+ }
166
+ var _default = exports.default = TextBreaker;
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _variableType = require("./helpers/variableType");
6
+ const groupDecorations = line => {
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 = 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) {
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
+ if (!inline.background) {
50
+ continue;
51
+ }
52
+ let color = inline.background;
53
+ let patternColor = this.pdfDocument.providePattern(inline.background);
54
+ if (patternColor !== null) {
55
+ color = patternColor;
56
+ }
57
+ let justifyShift = inline.justifyShift || 0;
58
+ this.pdfDocument.fillColor(color).rect(x + inline.x - justifyShift, y, inline.width + justifyShift, height).fill();
59
+ }
60
+ }
61
+ drawDecorations(line, x, y) {
62
+ let groups = groupDecorations(line);
63
+ for (let i = 0, l = groups.length; i < l; i++) {
64
+ this._drawDecoration(groups[i], x, y);
65
+ }
66
+ }
67
+ _drawDecoration(group, x, y) {
68
+ const maxInline = () => {
69
+ let max = 0;
70
+ for (let i = 0, l = group.inlines.length; i < l; i++) {
71
+ let inline = group.inlines[i];
72
+ max = inline.fontSize > max ? i : max;
73
+ }
74
+ return group.inlines[max];
75
+ };
76
+ const width = () => {
77
+ let sum = 0;
78
+ for (let i = 0, l = group.inlines.length; i < l; i++) {
79
+ let justifyShift = group.inlines[i].justifyShift || 0;
80
+ sum += group.inlines[i].width + justifyShift;
81
+ }
82
+ return sum;
83
+ };
84
+ let firstInline = group.inlines[0];
85
+ let biggerInline = maxInline();
86
+ let totalWidth = width();
87
+ let lineAscent = group.line.getAscenderHeight();
88
+ let ascent = biggerInline.font.ascender / 1000 * biggerInline.fontSize;
89
+ let height = biggerInline.height;
90
+ let descent = height - ascent;
91
+ let lw = (0, _variableType.isNumber)(group.decorationThickness) ? group.decorationThickness : 0.5 + Math.floor(Math.max(biggerInline.fontSize - 8, 0) / 2) * 0.12;
92
+ switch (group.decoration) {
93
+ case 'underline':
94
+ y += lineAscent + descent * 0.45;
95
+ break;
96
+ case 'overline':
97
+ y += lineAscent - ascent * 0.85;
98
+ break;
99
+ case 'lineThrough':
100
+ y += lineAscent - ascent * 0.25;
101
+ break;
102
+ default:
103
+ throw new Error(`Unknown decoration : ${group.decoration}`);
104
+ }
105
+ if (group.inlines[0].sup) {
106
+ y -= group.inlines[0].fontSize * 0.75;
107
+ }
108
+ if (group.inlines[0].sub) {
109
+ y += group.inlines[0].fontSize * 0.35;
110
+ }
111
+ this.pdfDocument.save();
112
+ if (group.decorationStyle === 'double') {
113
+ let gap = Math.max(0.5, lw, biggerInline.fontSize * 0.15);
114
+ 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();
115
+ } else if (group.decorationStyle === 'dashed') {
116
+ let nbDashes = Math.ceil(totalWidth / (3.96 + 2.84));
117
+ let rdx = x + firstInline.x;
118
+ this.pdfDocument.rect(rdx, y, totalWidth, lw).clip();
119
+ this.pdfDocument.fillColor(group.decorationColor);
120
+ for (let i = 0; i < nbDashes; i++) {
121
+ this.pdfDocument.rect(rdx, y - lw / 2, 3.96, lw).fill();
122
+ rdx += 3.96 + 2.84;
123
+ }
124
+ } else if (group.decorationStyle === 'dotted') {
125
+ let nbDots = Math.ceil(totalWidth / (lw * 3));
126
+ let rx = x + firstInline.x;
127
+ this.pdfDocument.rect(rx, y, totalWidth, lw).clip();
128
+ this.pdfDocument.fillColor(group.decorationColor);
129
+ for (let i = 0; i < nbDots; i++) {
130
+ this.pdfDocument.rect(rx, y - lw / 2, lw, lw).fill();
131
+ rx += lw * 3;
132
+ }
133
+ } else if (group.decorationStyle === 'wavy') {
134
+ let sh = 0.7,
135
+ sv = 1;
136
+ let nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
137
+ let rwx = x + firstInline.x - 1;
138
+ this.pdfDocument.rect(x + firstInline.x, y - sv, totalWidth, y + sv).clip();
139
+ this.pdfDocument.lineWidth(lw / 3);
140
+ this.pdfDocument.moveTo(rwx, y);
141
+ for (let i = 0; i < nbWaves; i++) {
142
+ 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);
143
+ rwx += sh * 6;
144
+ }
145
+ this.pdfDocument.stroke(group.decorationColor);
146
+ } else {
147
+ this.pdfDocument.fillColor(group.decorationColor).rect(x + firstInline.x, y - lw / 2, totalWidth, lw).fill();
148
+ }
149
+ this.pdfDocument.restore();
150
+ }
151
+ }
152
+ var _default = exports.default = TextDecorator;
@@ -0,0 +1,244 @@
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
+ var _rtlUtils = require("./rtlUtils");
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
+ const LEADING = /^(\s)+/g;
10
+ const TRAILING = /(\s)+$/g;
11
+
12
+ /**
13
+ * @param {Array} array
14
+ * @returns {Array}
15
+ */
16
+ const flattenTextArray = array => {
17
+ function flatten(array) {
18
+ return array.reduce((prev, cur) => {
19
+ let current = Array.isArray(cur.text) ? flatten(cur.text) : cur;
20
+ let more = [].concat(current).some(Array.isArray);
21
+ return prev.concat(more ? flatten(current) : current);
22
+ }, []);
23
+ }
24
+ if (!Array.isArray(array)) {
25
+ array = [array];
26
+ }
27
+
28
+ // TODO: Styling in nested text (issue: https://github.com/bpampuch/pdfmake/issues/1174)
29
+
30
+ array = flatten(array);
31
+ return array;
32
+ };
33
+
34
+ /**
35
+ * Text measurement utility
36
+ */
37
+ class TextInlines {
38
+ /**
39
+ * @param {object} pdfDocument object is instance of PDFDocument
40
+ */
41
+ constructor(pdfDocument) {
42
+ this.pdfDocument = pdfDocument;
43
+ }
44
+
45
+ /**
46
+ * Converts an array of strings (or inline-definition-objects) into a collection
47
+ * of inlines and calculated minWidth/maxWidth and their min/max widths
48
+ *
49
+ * @param {Array|object} textArray an array of inline-definition-objects (or strings)
50
+ * @param {StyleContextStack} styleContextStack current style stack
51
+ * @returns {object} collection of inlines, minWidth, maxWidth
52
+ */
53
+ buildInlines(textArray, styleContextStack) {
54
+ const getTrimmedWidth = item => {
55
+ return Math.max(0, item.width - item.leadingCut - item.trailingCut);
56
+ };
57
+ let minWidth = 0;
58
+ let maxWidth = 0;
59
+ let currentLineWidth;
60
+ let flattenedTextArray = flattenTextArray(textArray);
61
+ const textBreaker = new _TextBreaker.default();
62
+ let brokenText = textBreaker.getBreaks(flattenedTextArray, styleContextStack);
63
+ let measuredText = this.measure(brokenText, styleContextStack);
64
+ measuredText.forEach(inline => {
65
+ minWidth = Math.max(minWidth, getTrimmedWidth(inline));
66
+ if (!currentLineWidth) {
67
+ currentLineWidth = {
68
+ width: 0,
69
+ leadingCut: inline.leadingCut,
70
+ trailingCut: 0
71
+ };
72
+ }
73
+ currentLineWidth.width += inline.width;
74
+ currentLineWidth.trailingCut = inline.trailingCut;
75
+ maxWidth = Math.max(maxWidth, getTrimmedWidth(currentLineWidth));
76
+ if (inline.lineEnd) {
77
+ currentLineWidth = null;
78
+ }
79
+ });
80
+ if (_StyleContextStack.default.getStyleProperty({}, styleContextStack, 'noWrap', false)) {
81
+ minWidth = maxWidth;
82
+ }
83
+ return {
84
+ items: measuredText,
85
+ minWidth: minWidth,
86
+ maxWidth: maxWidth
87
+ };
88
+ }
89
+ measure(array, styleContextStack) {
90
+ if (array.length) {
91
+ let leadingIndent = _StyleContextStack.default.getStyleProperty(array[0], styleContextStack, 'leadingIndent', 0);
92
+ if (leadingIndent) {
93
+ array[0].leadingCut = -leadingIndent;
94
+ array[0].leadingIndent = leadingIndent;
95
+ }
96
+ }
97
+ array.forEach(item => {
98
+ // Font resolution priority:
99
+ // 1. Item-level font (set directly on the text node)
100
+ // 2. Style/named-style font (from style stack)
101
+ // 3. defaultStyle font (from document definition)
102
+ // 4. Auto-detect: Cairo for RTL/Arabic text, Roboto for LTR/Latin text
103
+ let font = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'font', null);
104
+ let bold = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'bold', false);
105
+ let italics = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'italics', false);
106
+ if (!font) {
107
+ // No font set by item, style, or defaultStyle — auto-detect from text content
108
+ if (item.text && (0, _rtlUtils.containsRTL)(item.text)) {
109
+ font = 'Cairo';
110
+ } else {
111
+ font = 'Roboto';
112
+ }
113
+ }
114
+ item.font = this.pdfDocument.provideFont(font, bold, italics);
115
+ item.alignment = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'alignment', 'left');
116
+
117
+ // RTL Support: detect direction and set isRTL on each inline
118
+ let direction = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'direction', null);
119
+ if (direction === 'rtl') {
120
+ item.isRTL = true;
121
+ item.direction = 'rtl';
122
+ } else if (direction === 'ltr') {
123
+ item.isRTL = false;
124
+ item.direction = 'ltr';
125
+ } else {
126
+ // Auto-detect from text content
127
+ let textDir = (0, _rtlUtils.getTextDirection)(item.text);
128
+ item.isRTL = textDir === 'rtl';
129
+ item.direction = textDir === 'rtl' ? 'rtl' : 'ltr';
130
+ }
131
+
132
+ // For RTL text, auto-default alignment to 'right' if not explicitly set
133
+ if (item.isRTL && item.alignment === 'left') {
134
+ let explicitAlignment = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'alignment', null);
135
+ if (!explicitAlignment) {
136
+ item.alignment = 'right';
137
+ }
138
+ }
139
+ item.fontSize = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'fontSize', 12);
140
+ item.fontFeatures = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'fontFeatures', null);
141
+ item.characterSpacing = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'characterSpacing', 0);
142
+ item.color = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'color', 'black');
143
+ item.decoration = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decoration', null);
144
+ item.decorationColor = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationColor', null);
145
+ item.decorationStyle = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationStyle', null);
146
+ item.decorationThickness = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationThickness', null);
147
+ item.background = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'background', null);
148
+ item.link = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'link', null);
149
+ item.linkToPage = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'linkToPage', null);
150
+ item.linkToDestination = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'linkToDestination', null);
151
+ item.noWrap = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'noWrap', null);
152
+ item.opacity = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'opacity', 1);
153
+ item.sup = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'sup', false);
154
+ item.sub = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'sub', false);
155
+ if (item.sup || item.sub) {
156
+ // font size reduction taken from here: https://en.wikipedia.org/wiki/Subscript_and_superscript#Desktop_publishing
157
+ item.fontSize *= 0.58;
158
+ }
159
+ let lineHeight = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'lineHeight', 1);
160
+ item.width = this.widthOfText(item.text, item);
161
+ item.height = item.font.lineHeight(item.fontSize) * lineHeight;
162
+ if (!item.leadingCut) {
163
+ item.leadingCut = 0;
164
+ }
165
+ let preserveLeadingSpaces = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'preserveLeadingSpaces', false);
166
+ if (!preserveLeadingSpaces) {
167
+ let leadingSpaces = item.text.match(LEADING);
168
+ if (leadingSpaces) {
169
+ item.leadingCut += this.widthOfText(leadingSpaces[0], item);
170
+ }
171
+ }
172
+ item.trailingCut = 0;
173
+ let preserveTrailingSpaces = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'preserveTrailingSpaces', false);
174
+ if (!preserveTrailingSpaces) {
175
+ let trailingSpaces = item.text.match(TRAILING);
176
+ if (trailingSpaces) {
177
+ item.trailingCut = this.widthOfText(trailingSpaces[0], item);
178
+ }
179
+ }
180
+ }, this);
181
+ return array;
182
+ }
183
+
184
+ /**
185
+ * Width of text
186
+ *
187
+ * @param {string} text
188
+ * @param {object} inline
189
+ * @returns {number}
190
+ */
191
+ widthOfText(text, inline) {
192
+ return inline.font.widthOfString(text, inline.fontSize, inline.fontFeatures) + (inline.characterSpacing || 0) * (text.length - 1);
193
+ }
194
+
195
+ /**
196
+ * Returns size of the specified string (without breaking it) using the current style
197
+ *
198
+ * @param {string} text text to be measured
199
+ * @param {object} styleContextStack current style stack
200
+ * @returns {object} size of the specified string
201
+ */
202
+ sizeOfText(text, styleContextStack) {
203
+ //TODO: refactor - extract from measure
204
+ let fontName = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'font', 'Roboto');
205
+ let fontSize = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'fontSize', 12);
206
+ let fontFeatures = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'fontFeatures', null);
207
+ let bold = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'bold', false);
208
+ let italics = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'italics', false);
209
+ let lineHeight = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'lineHeight', 1);
210
+ let characterSpacing = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'characterSpacing', 0);
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
+ /**
228
+ * Returns size of the specified rotated string (without breaking it) using the current style
229
+ *
230
+ * @param {string} text text to be measured
231
+ * @param {number} angle
232
+ * @param {object} styleContextStack current style stack
233
+ * @returns {object} size of the specified string
234
+ */
235
+ sizeOfRotatedText(text, angle, styleContextStack) {
236
+ let angleRad = angle * Math.PI / -180;
237
+ let size = this.sizeOfText(text, styleContextStack);
238
+ return {
239
+ width: Math.abs(size.height * Math.sin(angleRad)) + Math.abs(size.width * Math.cos(angleRad)),
240
+ height: Math.abs(size.width * Math.sin(angleRad)) + Math.abs(size.height * Math.cos(angleRad))
241
+ };
242
+ }
243
+ }
244
+ var _default = exports.default = TextInlines;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ async function fetchUrl(url, headers = {}) {
6
+ try {
7
+ const response = await fetch(url, {
8
+ headers
9
+ });
10
+ if (!response.ok) {
11
+ throw new Error(`Failed to fetch (status code: ${response.status}, url: "${url}")`);
12
+ }
13
+ return await response.arrayBuffer();
14
+ } catch (error) {
15
+ throw new Error(`Network request failed (url: "${url}", error: ${error.message})`);
16
+ }
17
+ }
18
+ class URLResolver {
19
+ constructor(fs) {
20
+ this.fs = fs;
21
+ this.resolving = {};
22
+ }
23
+ resolve(url, headers = {}) {
24
+ const resolveUrlInternal = async () => {
25
+ if (url.toLowerCase().startsWith('https://') || url.toLowerCase().startsWith('http://')) {
26
+ if (this.fs.existsSync(url)) {
27
+ return; // url was downloaded earlier
28
+ }
29
+ const buffer = await fetchUrl(url, headers);
30
+ this.fs.writeFileSync(url, buffer);
31
+ }
32
+ // else cannot be resolved
33
+ };
34
+ if (!this.resolving[url]) {
35
+ this.resolving[url] = resolveUrlInternal();
36
+ }
37
+ return this.resolving[url];
38
+ }
39
+ resolved() {
40
+ return Promise.all(Object.values(this.resolving));
41
+ }
42
+ }
43
+ var _default = exports.default = URLResolver;
package/js/base.js ADDED
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _Printer = _interopRequireDefault(require("./Printer"));
6
+ var _virtualFs = _interopRequireDefault(require("./virtual-fs"));
7
+ var _tools = require("./helpers/tools");
8
+ var _variableType = require("./helpers/variableType");
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ class pdfmake {
11
+ constructor() {
12
+ this.virtualfs = _virtualFs.default;
13
+ this.urlResolver = null;
14
+ }
15
+
16
+ /**
17
+ * @param {object} docDefinition
18
+ * @param {?object} options
19
+ * @returns {object}
20
+ */
21
+ createPdf(docDefinition, options = {}) {
22
+ if (!(0, _variableType.isObject)(docDefinition)) {
23
+ throw new Error("Parameter 'docDefinition' has an invalid type. Object expected.");
24
+ }
25
+ if (!(0, _variableType.isObject)(options)) {
26
+ throw new Error("Parameter 'options' has an invalid type. Object expected.");
27
+ }
28
+ options.progressCallback = this.progressCallback;
29
+ options.tableLayouts = this.tableLayouts;
30
+ let printer = new _Printer.default(this.fonts, this.virtualfs, this.urlResolver());
31
+ const pdfDocumentPromise = printer.createPdfKitDocument(docDefinition, options);
32
+ return this._transformToDocument(pdfDocumentPromise);
33
+ }
34
+ setProgressCallback(callback) {
35
+ this.progressCallback = callback;
36
+ }
37
+ addTableLayouts(tableLayouts) {
38
+ this.tableLayouts = (0, _tools.pack)(this.tableLayouts, tableLayouts);
39
+ }
40
+ setTableLayouts(tableLayouts) {
41
+ this.tableLayouts = tableLayouts;
42
+ }
43
+ clearTableLayouts() {
44
+ this.tableLayouts = {};
45
+ }
46
+ addFonts(fonts) {
47
+ this.fonts = (0, _tools.pack)(this.fonts, fonts);
48
+ }
49
+ setFonts(fonts) {
50
+ this.fonts = fonts;
51
+ }
52
+ clearFonts() {
53
+ this.fonts = {};
54
+ }
55
+ _transformToDocument(doc) {
56
+ return doc;
57
+ }
58
+ }
59
+ var _default = exports.default = pdfmake;