pdfmake 0.3.3 → 0.3.4

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 (92) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -21
  3. package/README.md +75 -78
  4. package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
  5. package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
  6. package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  7. package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
  8. package/build/fonts/Roboto.js +27 -0
  9. package/build/pdfmake.js +64552 -64404
  10. package/build/pdfmake.js.map +1 -1
  11. package/build/pdfmake.min.js +2 -2
  12. package/build/pdfmake.min.js.map +1 -1
  13. package/build/standard-fonts/Courier.js +27 -27
  14. package/build/standard-fonts/Helvetica.js +27 -27
  15. package/build/standard-fonts/Symbol.js +21 -21
  16. package/build/standard-fonts/Times.js +27 -27
  17. package/build/standard-fonts/ZapfDingbats.js +21 -21
  18. package/build/vfs_fonts.js +5 -5
  19. package/build-vfs.js +44 -44
  20. package/fonts/Roboto.js +8 -8
  21. package/js/DocMeasure.js +6 -6
  22. package/js/DocumentContext.js +8 -3
  23. package/js/ElementWriter.js +42 -16
  24. package/js/LayoutBuilder.js +141 -78
  25. package/js/Line.js +16 -16
  26. package/js/OutputDocument.js +10 -10
  27. package/js/OutputDocumentServer.js +3 -3
  28. package/js/PDFDocument.js +3 -3
  29. package/js/PageElementWriter.js +15 -9
  30. package/js/Printer.js +28 -28
  31. package/js/Renderer.js +40 -8
  32. package/js/SVGMeasure.js +10 -10
  33. package/js/StyleContextStack.js +48 -48
  34. package/js/TableProcessor.js +14 -0
  35. package/js/TextBreaker.js +17 -17
  36. package/js/TextInlines.js +33 -33
  37. package/js/base.js +4 -4
  38. package/js/browser-extensions/OutputDocumentBrowser.js +24 -24
  39. package/js/columnCalculator.js +2 -2
  40. package/js/helpers/node.js +14 -15
  41. package/js/helpers/variableType.js +18 -18
  42. package/js/qrEnc.js +38 -38
  43. package/js/virtual-fs.js +11 -11
  44. package/package.json +8 -8
  45. package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -9
  46. package/src/3rd-party/svg-to-pdfkit/source.js +2745 -2745
  47. package/src/3rd-party/svg-to-pdfkit.js +3 -3
  48. package/src/DocMeasure.js +745 -745
  49. package/src/DocPreprocessor.js +283 -283
  50. package/src/DocumentContext.js +345 -338
  51. package/src/ElementWriter.js +441 -417
  52. package/src/LayoutBuilder.js +1336 -1262
  53. package/src/Line.js +114 -114
  54. package/src/OutputDocument.js +64 -64
  55. package/src/OutputDocumentServer.js +32 -32
  56. package/src/PDFDocument.js +174 -174
  57. package/src/PageElementWriter.js +187 -179
  58. package/src/PageSize.js +53 -53
  59. package/src/Printer.js +306 -306
  60. package/src/Renderer.js +445 -409
  61. package/src/SVGMeasure.js +109 -109
  62. package/src/StyleContextStack.js +208 -208
  63. package/src/TableProcessor.js +620 -602
  64. package/src/TextBreaker.js +168 -168
  65. package/src/TextDecorator.js +175 -175
  66. package/src/TextInlines.js +224 -224
  67. package/src/URLResolver.js +43 -43
  68. package/src/base.js +70 -70
  69. package/src/browser-extensions/OutputDocumentBrowser.js +80 -80
  70. package/src/browser-extensions/fonts/Roboto.js +27 -27
  71. package/src/browser-extensions/index.js +55 -55
  72. package/src/browser-extensions/pdfMake.js +1 -1
  73. package/src/browser-extensions/standard-fonts/Courier.js +27 -27
  74. package/src/browser-extensions/standard-fonts/Helvetica.js +27 -27
  75. package/src/browser-extensions/standard-fonts/Symbol.js +21 -21
  76. package/src/browser-extensions/standard-fonts/Times.js +27 -27
  77. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -21
  78. package/src/browser-extensions/virtual-fs-cjs.js +1 -1
  79. package/src/columnCalculator.js +154 -154
  80. package/src/helpers/node.js +134 -136
  81. package/src/helpers/tools.js +44 -44
  82. package/src/helpers/variableType.js +50 -50
  83. package/src/index.js +16 -16
  84. package/src/qrEnc.js +796 -796
  85. package/src/standardPageSizes.js +52 -52
  86. package/src/tableLayouts.js +100 -100
  87. package/src/virtual-fs.js +66 -66
  88. package/standard-fonts/Courier.js +8 -8
  89. package/standard-fonts/Helvetica.js +8 -8
  90. package/standard-fonts/Symbol.js +5 -5
  91. package/standard-fonts/Times.js +8 -8
  92. package/standard-fonts/ZapfDingbats.js +5 -5
@@ -1,168 +1,168 @@
1
- import LineBreaker from 'linebreak';
2
- import { isObject } from './helpers/variableType';
3
- import StyleContextStack from './StyleContextStack';
4
-
5
- /**
6
- * @param {string} text
7
- * @param {boolean} noWrap
8
- * @param {boolean} breakAll
9
- * @returns {Array}
10
- */
11
- const splitWords = (text, noWrap, breakAll = false) => {
12
- let words = [];
13
- if (text === undefined || text === null) {
14
- text = '';
15
- } else {
16
- text = String(text);
17
- }
18
-
19
- if (noWrap) {
20
- words.push({ text: text });
21
- return words;
22
- }
23
- if (breakAll) {
24
- return text.split('').map(c => {
25
- if(c.match(/^\n$|^\r$/)) { // new line
26
- return { text: '', lineEnd: true };
27
- }
28
- return { text: c };
29
- });
30
- }
31
-
32
- if (text.length === 0) {
33
- words.push({ text: '' });
34
- return words;
35
- }
36
-
37
- let breaker = new LineBreaker(text);
38
- let last = 0;
39
- let bk;
40
-
41
- while ((bk = breaker.nextBreak())) {
42
- let word = text.slice(last, bk.position);
43
-
44
- if (bk.required || word.match(/\r?\n$|\r$/)) { // new line
45
- word = word.replace(/\r?\n$|\r$/, '');
46
- words.push({ text: word, lineEnd: true });
47
- } else {
48
- words.push({ text: word });
49
- }
50
-
51
- last = bk.position;
52
- }
53
-
54
- return words;
55
- };
56
-
57
- /**
58
- * @param {Array} words
59
- * @param {boolean} noWrap
60
- * @returns {?string}
61
- */
62
- const getFirstWord = (words, noWrap) => {
63
- let word = words[0];
64
- if (word === undefined) {
65
- return null;
66
- }
67
-
68
- if (noWrap) { // text was not wrapped, we need only first word
69
- let tmpWords = splitWords(word.text, false);
70
- if (tmpWords[0] === undefined) {
71
- return null;
72
- }
73
- word = tmpWords[0];
74
- }
75
-
76
- return word.text;
77
- };
78
-
79
- /**
80
- * @param {Array} words
81
- * @param {boolean} noWrap
82
- * @returns {?string}
83
- */
84
- const getLastWord = (words, noWrap) => {
85
- let word = words[words.length - 1];
86
- if (word === undefined) {
87
- return null;
88
- }
89
-
90
- if (word.lineEnd) {
91
- return null;
92
- }
93
-
94
- if (noWrap) { // text was not wrapped, we need only last word
95
- let tmpWords = splitWords(word.text, false);
96
- if (tmpWords[tmpWords.length - 1] === undefined) {
97
- return null;
98
- }
99
- word = tmpWords[tmpWords.length - 1];
100
- }
101
-
102
- return word.text;
103
- };
104
-
105
- class TextBreaker {
106
- /**
107
- * @param {string|Array} texts
108
- * @param {StyleContextStack} styleContextStack
109
- * @returns {Array}
110
- */
111
- getBreaks(texts, styleContextStack) {
112
- let results = [];
113
-
114
- if (!Array.isArray(texts)) {
115
- texts = [texts];
116
- }
117
-
118
- let lastWord = null;
119
- for (let i = 0, l = texts.length; i < l; i++) {
120
- let item = texts[i];
121
- let style = null;
122
- let words;
123
- let breakAll = StyleContextStack.getStyleProperty(item || {}, styleContextStack, 'wordBreak', 'normal') === 'break-all';
124
- let noWrap = StyleContextStack.getStyleProperty(item || {}, styleContextStack, 'noWrap', false);
125
- if (isObject(item)) {
126
- if (item._textRef && item._textRef._textNodeRef.text) {
127
- item.text = item._textRef._textNodeRef.text;
128
- }
129
- words = splitWords(item.text, noWrap, breakAll);
130
- style = StyleContextStack.copyStyle(item);
131
- } else {
132
- words = splitWords(item, noWrap, breakAll);
133
- }
134
-
135
- if (lastWord && words.length) {
136
- let firstWord = getFirstWord(words, noWrap);
137
-
138
- let wrapWords = splitWords(lastWord + firstWord, false);
139
- if (wrapWords.length === 1) {
140
- results[results.length - 1].noNewLine = true;
141
- }
142
- }
143
-
144
- for (let i2 = 0, l2 = words.length; i2 < l2; i2++) {
145
- let result = {
146
- text: words[i2].text
147
- };
148
-
149
- if (words[i2].lineEnd) {
150
- result.lineEnd = true;
151
- }
152
-
153
- StyleContextStack.copyStyle(style, result);
154
-
155
- results.push(result);
156
- }
157
-
158
- lastWord = null;
159
- if (i + 1 < l) {
160
- lastWord = getLastWord(words, noWrap);
161
- }
162
- }
163
-
164
- return results;
165
- }
166
- }
167
-
168
- export default TextBreaker;
1
+ import LineBreaker from 'linebreak';
2
+ import { isObject } from './helpers/variableType';
3
+ import StyleContextStack from './StyleContextStack';
4
+
5
+ /**
6
+ * @param {string} text
7
+ * @param {boolean} noWrap
8
+ * @param {boolean} breakAll
9
+ * @returns {Array}
10
+ */
11
+ const splitWords = (text, noWrap, breakAll = false) => {
12
+ let words = [];
13
+ if (text === undefined || text === null) {
14
+ text = '';
15
+ } else {
16
+ text = String(text);
17
+ }
18
+
19
+ if (noWrap) {
20
+ words.push({ text: text });
21
+ return words;
22
+ }
23
+ if (breakAll) {
24
+ return text.split('').map(c => {
25
+ if(c.match(/^\n$|^\r$/)) { // new line
26
+ return { text: '', lineEnd: true };
27
+ }
28
+ return { text: c };
29
+ });
30
+ }
31
+
32
+ if (text.length === 0) {
33
+ words.push({ text: '' });
34
+ return words;
35
+ }
36
+
37
+ let breaker = new LineBreaker(text);
38
+ let last = 0;
39
+ let bk;
40
+
41
+ while ((bk = breaker.nextBreak())) {
42
+ let word = text.slice(last, bk.position);
43
+
44
+ if (bk.required || word.match(/\r?\n$|\r$/)) { // new line
45
+ word = word.replace(/\r?\n$|\r$/, '');
46
+ words.push({ text: word, lineEnd: true });
47
+ } else {
48
+ words.push({ text: word });
49
+ }
50
+
51
+ last = bk.position;
52
+ }
53
+
54
+ return words;
55
+ };
56
+
57
+ /**
58
+ * @param {Array} words
59
+ * @param {boolean} noWrap
60
+ * @returns {?string}
61
+ */
62
+ const getFirstWord = (words, noWrap) => {
63
+ let word = words[0];
64
+ if (word === undefined) {
65
+ return null;
66
+ }
67
+
68
+ if (noWrap) { // text was not wrapped, we need only first word
69
+ let tmpWords = splitWords(word.text, false);
70
+ if (tmpWords[0] === undefined) {
71
+ return null;
72
+ }
73
+ word = tmpWords[0];
74
+ }
75
+
76
+ return word.text;
77
+ };
78
+
79
+ /**
80
+ * @param {Array} words
81
+ * @param {boolean} noWrap
82
+ * @returns {?string}
83
+ */
84
+ const getLastWord = (words, noWrap) => {
85
+ let word = words[words.length - 1];
86
+ if (word === undefined) {
87
+ return null;
88
+ }
89
+
90
+ if (word.lineEnd) {
91
+ return null;
92
+ }
93
+
94
+ if (noWrap) { // text was not wrapped, we need only last word
95
+ let tmpWords = splitWords(word.text, false);
96
+ if (tmpWords[tmpWords.length - 1] === undefined) {
97
+ return null;
98
+ }
99
+ word = tmpWords[tmpWords.length - 1];
100
+ }
101
+
102
+ return word.text;
103
+ };
104
+
105
+ class TextBreaker {
106
+ /**
107
+ * @param {string|Array} texts
108
+ * @param {StyleContextStack} styleContextStack
109
+ * @returns {Array}
110
+ */
111
+ getBreaks(texts, styleContextStack) {
112
+ let results = [];
113
+
114
+ if (!Array.isArray(texts)) {
115
+ texts = [texts];
116
+ }
117
+
118
+ let lastWord = null;
119
+ for (let i = 0, l = texts.length; i < l; i++) {
120
+ let item = texts[i];
121
+ let style = null;
122
+ let words;
123
+ let breakAll = StyleContextStack.getStyleProperty(item || {}, styleContextStack, 'wordBreak', 'normal') === 'break-all';
124
+ let noWrap = StyleContextStack.getStyleProperty(item || {}, styleContextStack, 'noWrap', false);
125
+ if (isObject(item)) {
126
+ if (item._textRef && item._textRef._textNodeRef.text) {
127
+ item.text = item._textRef._textNodeRef.text;
128
+ }
129
+ words = splitWords(item.text, noWrap, breakAll);
130
+ style = StyleContextStack.copyStyle(item);
131
+ } else {
132
+ words = splitWords(item, noWrap, breakAll);
133
+ }
134
+
135
+ if (lastWord && words.length) {
136
+ let firstWord = getFirstWord(words, noWrap);
137
+
138
+ let wrapWords = splitWords(lastWord + firstWord, false);
139
+ if (wrapWords.length === 1) {
140
+ results[results.length - 1].noNewLine = true;
141
+ }
142
+ }
143
+
144
+ for (let i2 = 0, l2 = words.length; i2 < l2; i2++) {
145
+ let result = {
146
+ text: words[i2].text
147
+ };
148
+
149
+ if (words[i2].lineEnd) {
150
+ result.lineEnd = true;
151
+ }
152
+
153
+ StyleContextStack.copyStyle(style, result);
154
+
155
+ results.push(result);
156
+ }
157
+
158
+ lastWord = null;
159
+ if (i + 1 < l) {
160
+ lastWord = getLastWord(words, noWrap);
161
+ }
162
+ }
163
+
164
+ return results;
165
+ }
166
+ }
167
+
168
+ export default TextBreaker;
@@ -1,175 +1,175 @@
1
- import { isNumber } from './helpers/variableType';
2
-
3
- const groupDecorations = line => {
4
- let groups = [];
5
- let currentGroup = null;
6
- for (let i = 0, l = line.inlines.length; i < l; i++) {
7
- let inline = line.inlines[i];
8
- let decoration = inline.decoration;
9
- if (!decoration) {
10
- currentGroup = null;
11
- continue;
12
- }
13
- if (!Array.isArray(decoration)) {
14
- decoration = [decoration];
15
- }
16
- let color = inline.decorationColor || inline.color || 'black';
17
- let style = inline.decorationStyle || 'solid';
18
- let thickness = isNumber(inline.decorationThickness) ? inline.decorationThickness : null;
19
- for (let ii = 0, ll = decoration.length; ii < ll; ii++) {
20
- let decorationItem = decoration[ii];
21
- if (!currentGroup || decorationItem !== currentGroup.decoration ||
22
- style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor) {
23
-
24
- currentGroup = {
25
- line: line,
26
- decoration: decorationItem,
27
- decorationColor: color,
28
- decorationStyle: style,
29
- decorationThickness: thickness,
30
- inlines: [inline]
31
- };
32
- groups.push(currentGroup);
33
- } else {
34
- currentGroup.inlines.push(inline);
35
- }
36
- }
37
- }
38
-
39
- return groups;
40
- };
41
-
42
- class TextDecorator {
43
-
44
- constructor(pdfDocument) {
45
- this.pdfDocument = pdfDocument;
46
- }
47
-
48
- drawBackground(line, x, y) {
49
- let height = line.getHeight();
50
- for (let i = 0, l = line.inlines.length; i < l; i++) {
51
- let inline = line.inlines[i];
52
- if (!inline.background) {
53
- continue;
54
- }
55
-
56
- let color = inline.background;
57
- let patternColor = this.pdfDocument.providePattern(inline.background);
58
- if (patternColor !== null) {
59
- color = patternColor;
60
- }
61
-
62
- let justifyShift = (inline.justifyShift || 0);
63
- this.pdfDocument.fillColor(color)
64
- .rect(x + inline.x - justifyShift, y, inline.width + justifyShift, height)
65
- .fill();
66
- }
67
- }
68
-
69
- drawDecorations(line, x, y) {
70
- let groups = groupDecorations(line);
71
- for (let i = 0, l = groups.length; i < l; i++) {
72
- this._drawDecoration(groups[i], x, y);
73
- }
74
- }
75
-
76
- _drawDecoration(group, x, y) {
77
- const maxInline = () => {
78
- let max = 0;
79
- for (let i = 0, l = group.inlines.length; i < l; i++) {
80
- let inline = group.inlines[i];
81
- max = inline.fontSize > max ? i : max;
82
- }
83
- return group.inlines[max];
84
- };
85
-
86
- const width = () => {
87
- let sum = 0;
88
- for (let i = 0, l = group.inlines.length; i < l; i++) {
89
- let justifyShift = (group.inlines[i].justifyShift || 0);
90
- sum += group.inlines[i].width + justifyShift;
91
- }
92
- return sum;
93
- };
94
-
95
- let firstInline = group.inlines[0];
96
- let biggerInline = maxInline();
97
- let totalWidth = width();
98
- let lineAscent = group.line.getAscenderHeight();
99
- let ascent = biggerInline.font.ascender / 1000 * biggerInline.fontSize;
100
- let height = biggerInline.height;
101
- let descent = height - ascent;
102
-
103
- let lw = isNumber(group.decorationThickness)
104
- ? group.decorationThickness
105
- : 0.5 + Math.floor(Math.max(biggerInline.fontSize - 8, 0) / 2) * 0.12;
106
-
107
- switch (group.decoration) {
108
- case 'underline':
109
- y += lineAscent + descent * 0.45;
110
- break;
111
- case 'overline':
112
- y += lineAscent - (ascent * 0.85);
113
- break;
114
- case 'lineThrough':
115
- y += lineAscent - (ascent * 0.25);
116
- break;
117
- default:
118
- throw new Error(`Unknown decoration : ${group.decoration}`);
119
- }
120
-
121
- if (group.inlines[0].sup) {
122
- y -= group.inlines[0].fontSize * 0.75;
123
- }
124
- if (group.inlines[0].sub) {
125
- y += group.inlines[0].fontSize * 0.35;
126
- }
127
-
128
- this.pdfDocument.save();
129
-
130
- if (group.decorationStyle === 'double') {
131
- let gap = Math.max(0.5, lw, biggerInline.fontSize * 0.15);
132
- this.pdfDocument.fillColor(group.decorationColor)
133
- .rect(x + firstInline.x, y - lw / 2, totalWidth, lw / 2).fill()
134
- .rect(x + firstInline.x, y + gap - lw / 2, totalWidth, lw / 2).fill();
135
- } else if (group.decorationStyle === 'dashed') {
136
- let nbDashes = Math.ceil(totalWidth / (3.96 + 2.84));
137
- let rdx = x + firstInline.x;
138
- this.pdfDocument.rect(rdx, y, totalWidth, lw).clip();
139
- this.pdfDocument.fillColor(group.decorationColor);
140
- for (let i = 0; i < nbDashes; i++) {
141
- this.pdfDocument.rect(rdx, y - lw / 2, 3.96, lw).fill();
142
- rdx += 3.96 + 2.84;
143
- }
144
- } else if (group.decorationStyle === 'dotted') {
145
- let nbDots = Math.ceil(totalWidth / (lw * 3));
146
- let rx = x + firstInline.x;
147
- this.pdfDocument.rect(rx, y, totalWidth, lw).clip();
148
- this.pdfDocument.fillColor(group.decorationColor);
149
- for (let i = 0; i < nbDots; i++) {
150
- this.pdfDocument.rect(rx, y - lw / 2, lw, lw).fill();
151
- rx += (lw * 3);
152
- }
153
- } else if (group.decorationStyle === 'wavy') {
154
- let sh = 0.7, sv = 1;
155
- let nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
156
- let rwx = x + firstInline.x - 1;
157
- this.pdfDocument.rect(x + firstInline.x, y - sv, totalWidth, y + sv).clip();
158
- this.pdfDocument.lineWidth(lw / 3);
159
- this.pdfDocument.moveTo(rwx, y);
160
- for (let i = 0; i < nbWaves; i++) {
161
- this.pdfDocument.bezierCurveTo(rwx + sh, y - sv, rwx + sh * 2, y - sv, rwx + sh * 3, y)
162
- .bezierCurveTo(rwx + sh * 4, y + sv, rwx + sh * 5, y + sv, rwx + sh * 6, y);
163
- rwx += sh * 6;
164
- }
165
- this.pdfDocument.stroke(group.decorationColor);
166
- } else {
167
- this.pdfDocument.fillColor(group.decorationColor)
168
- .rect(x + firstInline.x, y - lw / 2, totalWidth, lw)
169
- .fill();
170
- }
171
- this.pdfDocument.restore();
172
- }
173
- }
174
-
175
- export default TextDecorator;
1
+ import { isNumber } from './helpers/variableType';
2
+
3
+ const groupDecorations = line => {
4
+ let groups = [];
5
+ let currentGroup = null;
6
+ for (let i = 0, l = line.inlines.length; i < l; i++) {
7
+ let inline = line.inlines[i];
8
+ let decoration = inline.decoration;
9
+ if (!decoration) {
10
+ currentGroup = null;
11
+ continue;
12
+ }
13
+ if (!Array.isArray(decoration)) {
14
+ decoration = [decoration];
15
+ }
16
+ let color = inline.decorationColor || inline.color || 'black';
17
+ let style = inline.decorationStyle || 'solid';
18
+ let thickness = isNumber(inline.decorationThickness) ? inline.decorationThickness : null;
19
+ for (let ii = 0, ll = decoration.length; ii < ll; ii++) {
20
+ let decorationItem = decoration[ii];
21
+ if (!currentGroup || decorationItem !== currentGroup.decoration ||
22
+ style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor) {
23
+
24
+ currentGroup = {
25
+ line: line,
26
+ decoration: decorationItem,
27
+ decorationColor: color,
28
+ decorationStyle: style,
29
+ decorationThickness: thickness,
30
+ inlines: [inline]
31
+ };
32
+ groups.push(currentGroup);
33
+ } else {
34
+ currentGroup.inlines.push(inline);
35
+ }
36
+ }
37
+ }
38
+
39
+ return groups;
40
+ };
41
+
42
+ class TextDecorator {
43
+
44
+ constructor(pdfDocument) {
45
+ this.pdfDocument = pdfDocument;
46
+ }
47
+
48
+ drawBackground(line, x, y) {
49
+ let height = line.getHeight();
50
+ for (let i = 0, l = line.inlines.length; i < l; i++) {
51
+ let inline = line.inlines[i];
52
+ if (!inline.background) {
53
+ continue;
54
+ }
55
+
56
+ let color = inline.background;
57
+ let patternColor = this.pdfDocument.providePattern(inline.background);
58
+ if (patternColor !== null) {
59
+ color = patternColor;
60
+ }
61
+
62
+ let justifyShift = (inline.justifyShift || 0);
63
+ this.pdfDocument.fillColor(color)
64
+ .rect(x + inline.x - justifyShift, y, inline.width + justifyShift, height)
65
+ .fill();
66
+ }
67
+ }
68
+
69
+ drawDecorations(line, x, y) {
70
+ let groups = groupDecorations(line);
71
+ for (let i = 0, l = groups.length; i < l; i++) {
72
+ this._drawDecoration(groups[i], x, y);
73
+ }
74
+ }
75
+
76
+ _drawDecoration(group, x, y) {
77
+ const maxInline = () => {
78
+ let max = 0;
79
+ for (let i = 0, l = group.inlines.length; i < l; i++) {
80
+ let inline = group.inlines[i];
81
+ max = inline.fontSize > max ? i : max;
82
+ }
83
+ return group.inlines[max];
84
+ };
85
+
86
+ const width = () => {
87
+ let sum = 0;
88
+ for (let i = 0, l = group.inlines.length; i < l; i++) {
89
+ let justifyShift = (group.inlines[i].justifyShift || 0);
90
+ sum += group.inlines[i].width + justifyShift;
91
+ }
92
+ return sum;
93
+ };
94
+
95
+ let firstInline = group.inlines[0];
96
+ let biggerInline = maxInline();
97
+ let totalWidth = width();
98
+ let lineAscent = group.line.getAscenderHeight();
99
+ let ascent = biggerInline.font.ascender / 1000 * biggerInline.fontSize;
100
+ let height = biggerInline.height;
101
+ let descent = height - ascent;
102
+
103
+ let lw = isNumber(group.decorationThickness)
104
+ ? group.decorationThickness
105
+ : 0.5 + Math.floor(Math.max(biggerInline.fontSize - 8, 0) / 2) * 0.12;
106
+
107
+ switch (group.decoration) {
108
+ case 'underline':
109
+ y += lineAscent + descent * 0.45;
110
+ break;
111
+ case 'overline':
112
+ y += lineAscent - (ascent * 0.85);
113
+ break;
114
+ case 'lineThrough':
115
+ y += lineAscent - (ascent * 0.25);
116
+ break;
117
+ default:
118
+ throw new Error(`Unknown decoration : ${group.decoration}`);
119
+ }
120
+
121
+ if (group.inlines[0].sup) {
122
+ y -= group.inlines[0].fontSize * 0.75;
123
+ }
124
+ if (group.inlines[0].sub) {
125
+ y += group.inlines[0].fontSize * 0.35;
126
+ }
127
+
128
+ this.pdfDocument.save();
129
+
130
+ if (group.decorationStyle === 'double') {
131
+ let gap = Math.max(0.5, lw, biggerInline.fontSize * 0.15);
132
+ this.pdfDocument.fillColor(group.decorationColor)
133
+ .rect(x + firstInline.x, y - lw / 2, totalWidth, lw / 2).fill()
134
+ .rect(x + firstInline.x, y + gap - lw / 2, totalWidth, lw / 2).fill();
135
+ } else if (group.decorationStyle === 'dashed') {
136
+ let nbDashes = Math.ceil(totalWidth / (3.96 + 2.84));
137
+ let rdx = x + firstInline.x;
138
+ this.pdfDocument.rect(rdx, y, totalWidth, lw).clip();
139
+ this.pdfDocument.fillColor(group.decorationColor);
140
+ for (let i = 0; i < nbDashes; i++) {
141
+ this.pdfDocument.rect(rdx, y - lw / 2, 3.96, lw).fill();
142
+ rdx += 3.96 + 2.84;
143
+ }
144
+ } else if (group.decorationStyle === 'dotted') {
145
+ let nbDots = Math.ceil(totalWidth / (lw * 3));
146
+ let rx = x + firstInline.x;
147
+ this.pdfDocument.rect(rx, y, totalWidth, lw).clip();
148
+ this.pdfDocument.fillColor(group.decorationColor);
149
+ for (let i = 0; i < nbDots; i++) {
150
+ this.pdfDocument.rect(rx, y - lw / 2, lw, lw).fill();
151
+ rx += (lw * 3);
152
+ }
153
+ } else if (group.decorationStyle === 'wavy') {
154
+ let sh = 0.7, sv = 1;
155
+ let nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
156
+ let rwx = x + firstInline.x - 1;
157
+ this.pdfDocument.rect(x + firstInline.x, y - sv, totalWidth, y + sv).clip();
158
+ this.pdfDocument.lineWidth(lw / 3);
159
+ this.pdfDocument.moveTo(rwx, y);
160
+ for (let i = 0; i < nbWaves; i++) {
161
+ this.pdfDocument.bezierCurveTo(rwx + sh, y - sv, rwx + sh * 2, y - sv, rwx + sh * 3, y)
162
+ .bezierCurveTo(rwx + sh * 4, y + sv, rwx + sh * 5, y + sv, rwx + sh * 6, y);
163
+ rwx += sh * 6;
164
+ }
165
+ this.pdfDocument.stroke(group.decorationColor);
166
+ } else {
167
+ this.pdfDocument.fillColor(group.decorationColor)
168
+ .rect(x + firstInline.x, y - lw / 2, totalWidth, lw)
169
+ .fill();
170
+ }
171
+ this.pdfDocument.restore();
172
+ }
173
+ }
174
+
175
+ export default TextDecorator;