@flowaccount/pdfmake 1.0.6-staging.3 → 1.0.6

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.
@@ -1,157 +1,157 @@
1
- 'use strict';
2
-
3
- var isArray = require('./helpers').isArray;
4
- var isPattern = require('./helpers').isPattern;
5
- var getPattern = require('./helpers').getPattern;
6
-
7
- function groupDecorations(line) {
8
- var groups = [], currentGroup = null;
9
- for (var i = 0, l = line.inlines.length; i < l; i++) {
10
- var inline = line.inlines[i];
11
- var decoration = inline.decoration;
12
- if (!decoration) {
13
- currentGroup = null;
14
- continue;
15
- }
16
- if (!isArray(decoration)) {
17
- decoration = [decoration];
18
- }
19
- var color = inline.decorationColor || inline.color || 'black';
20
- var style = inline.decorationStyle || 'solid';
21
- for (var ii = 0, ll = decoration.length; ii < ll; ii++) {
22
- var decorationItem = decoration[ii];
23
- if (!currentGroup || decorationItem !== currentGroup.decoration ||
24
- style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor) {
25
-
26
- currentGroup = {
27
- line: line,
28
- decoration: decorationItem,
29
- decorationColor: color,
30
- decorationStyle: style,
31
- inlines: [inline]
32
- };
33
- groups.push(currentGroup);
34
- } else {
35
- currentGroup.inlines.push(inline);
36
- }
37
- }
38
- }
39
-
40
- return groups;
41
- }
42
-
43
- function drawDecoration(group, x, y, pdfKitDoc) {
44
- function maxInline() {
45
- var max = 0;
46
- for (var i = 0, l = group.inlines.length; i < l; i++) {
47
- var inline = group.inlines[i];
48
- max = inline.fontSize > max ? i : max;
49
- }
50
- return group.inlines[max];
51
- }
52
- function width() {
53
- var sum = 0;
54
- for (var i = 0, l = group.inlines.length; i < l; i++) {
55
- var justifyShift = (group.inlines[i].justifyShift || 0);
56
- sum += group.inlines[i].width + justifyShift;
57
- }
58
- return sum;
59
- }
60
- var firstInline = group.inlines[0],
61
- biggerInline = maxInline(),
62
- totalWidth = width(),
63
- lineAscent = group.line.getAscenderHeight(),
64
- ascent = biggerInline.font.ascender / 1000 * biggerInline.fontSize,
65
- height = biggerInline.height,
66
- descent = height - ascent;
67
-
68
- var lw = 0.5 + Math.floor(Math.max(biggerInline.fontSize - 8, 0) / 2) * 0.12;
69
-
70
- switch (group.decoration) {
71
- case 'underline':
72
- y += lineAscent + descent * 0.45;
73
- break;
74
- case 'overline':
75
- y += lineAscent - (ascent * 0.85);
76
- break;
77
- case 'lineThrough':
78
- y += lineAscent - (ascent * 0.25);
79
- break;
80
- default:
81
- throw 'Unknown decoration : ' + group.decoration;
82
- }
83
- pdfKitDoc.save();
84
-
85
- if (group.decorationStyle === 'double') {
86
- var gap = Math.max(0.5, lw * 2);
87
- pdfKitDoc.fillColor(group.decorationColor)
88
- .rect(x + firstInline.x, y - lw / 2, totalWidth, lw / 2).fill()
89
- .rect(x + firstInline.x, y + gap - lw / 2, totalWidth, lw / 2).fill();
90
- } else if (group.decorationStyle === 'dashed') {
91
- var nbDashes = Math.ceil(totalWidth / (3.96 + 2.84));
92
- var rdx = x + firstInline.x;
93
- pdfKitDoc.rect(rdx, y, totalWidth, lw).clip();
94
- pdfKitDoc.fillColor(group.decorationColor);
95
- for (var i = 0; i < nbDashes; i++) {
96
- pdfKitDoc.rect(rdx, y - lw / 2, 3.96, lw).fill();
97
- rdx += 3.96 + 2.84;
98
- }
99
- } else if (group.decorationStyle === 'dotted') {
100
- var nbDots = Math.ceil(totalWidth / (lw * 3));
101
- var rx = x + firstInline.x;
102
- pdfKitDoc.rect(rx, y, totalWidth, lw).clip();
103
- pdfKitDoc.fillColor(group.decorationColor);
104
- for (var ii = 0; ii < nbDots; ii++) {
105
- pdfKitDoc.rect(rx, y - lw / 2, lw, lw).fill();
106
- rx += (lw * 3);
107
- }
108
- } else if (group.decorationStyle === 'wavy') {
109
- var sh = 0.7, sv = 1;
110
- var nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
111
- var rwx = x + firstInline.x - 1;
112
- pdfKitDoc.rect(x + firstInline.x, y - sv, totalWidth, y + sv).clip();
113
- pdfKitDoc.lineWidth(0.24);
114
- pdfKitDoc.moveTo(rwx, y);
115
- for (var iii = 0; iii < nbWaves; iii++) {
116
- pdfKitDoc.bezierCurveTo(rwx + sh, y - sv, rwx + sh * 2, y - sv, rwx + sh * 3, y)
117
- .bezierCurveTo(rwx + sh * 4, y + sv, rwx + sh * 5, y + sv, rwx + sh * 6, y);
118
- rwx += sh * 6;
119
- }
120
- pdfKitDoc.stroke(group.decorationColor);
121
- } else {
122
- pdfKitDoc.fillColor(group.decorationColor)
123
- .rect(x + firstInline.x, y - lw / 2, totalWidth, lw)
124
- .fill();
125
- }
126
- pdfKitDoc.restore();
127
- }
128
-
129
- function drawDecorations(line, x, y, pdfKitDoc) {
130
- var groups = groupDecorations(line);
131
- for (var i = 0, l = groups.length; i < l; i++) {
132
- drawDecoration(groups[i], x, y, pdfKitDoc);
133
- }
134
- }
135
-
136
- function drawBackground(line, x, y, patterns, pdfKitDoc) {
137
- var height = line.getHeight();
138
- for (var i = 0, l = line.inlines.length; i < l; i++) {
139
- var inline = line.inlines[i];
140
- if (!inline.background) {
141
- continue;
142
- }
143
- var color = inline.background;
144
- if (isPattern(inline.background)) {
145
- color = getPattern(inline.background, patterns);
146
- }
147
- var justifyShift = (inline.justifyShift || 0);
148
- pdfKitDoc.fillColor(color)
149
- .rect(x + inline.x - justifyShift, y, inline.width + justifyShift, height)
150
- .fill();
151
- }
152
- }
153
-
154
- module.exports = {
155
- drawBackground: drawBackground,
156
- drawDecorations: drawDecorations
157
- };
1
+ 'use strict';
2
+
3
+ var isArray = require('./helpers').isArray;
4
+ var isPattern = require('./helpers').isPattern;
5
+ var getPattern = require('./helpers').getPattern;
6
+
7
+ function groupDecorations(line) {
8
+ var groups = [], currentGroup = null;
9
+ for (var i = 0, l = line.inlines.length; i < l; i++) {
10
+ var inline = line.inlines[i];
11
+ var decoration = inline.decoration;
12
+ if (!decoration) {
13
+ currentGroup = null;
14
+ continue;
15
+ }
16
+ if (!isArray(decoration)) {
17
+ decoration = [decoration];
18
+ }
19
+ var color = inline.decorationColor || inline.color || 'black';
20
+ var style = inline.decorationStyle || 'solid';
21
+ for (var ii = 0, ll = decoration.length; ii < ll; ii++) {
22
+ var decorationItem = decoration[ii];
23
+ if (!currentGroup || decorationItem !== currentGroup.decoration ||
24
+ style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor) {
25
+
26
+ currentGroup = {
27
+ line: line,
28
+ decoration: decorationItem,
29
+ decorationColor: color,
30
+ decorationStyle: style,
31
+ inlines: [inline]
32
+ };
33
+ groups.push(currentGroup);
34
+ } else {
35
+ currentGroup.inlines.push(inline);
36
+ }
37
+ }
38
+ }
39
+
40
+ return groups;
41
+ }
42
+
43
+ function drawDecoration(group, x, y, pdfKitDoc) {
44
+ function maxInline() {
45
+ var max = 0;
46
+ for (var i = 0, l = group.inlines.length; i < l; i++) {
47
+ var inline = group.inlines[i];
48
+ max = inline.fontSize > max ? i : max;
49
+ }
50
+ return group.inlines[max];
51
+ }
52
+ function width() {
53
+ var sum = 0;
54
+ for (var i = 0, l = group.inlines.length; i < l; i++) {
55
+ var justifyShift = (group.inlines[i].justifyShift || 0);
56
+ sum += group.inlines[i].width + justifyShift;
57
+ }
58
+ return sum;
59
+ }
60
+ var firstInline = group.inlines[0],
61
+ biggerInline = maxInline(),
62
+ totalWidth = width(),
63
+ lineAscent = group.line.getAscenderHeight(),
64
+ ascent = biggerInline.font.ascender / 1000 * biggerInline.fontSize,
65
+ height = biggerInline.height,
66
+ descent = height - ascent;
67
+
68
+ var lw = 0.5 + Math.floor(Math.max(biggerInline.fontSize - 8, 0) / 2) * 0.12;
69
+
70
+ switch (group.decoration) {
71
+ case 'underline':
72
+ y += lineAscent + descent * 0.45;
73
+ break;
74
+ case 'overline':
75
+ y += lineAscent - (ascent * 0.85);
76
+ break;
77
+ case 'lineThrough':
78
+ y += lineAscent - (ascent * 0.25);
79
+ break;
80
+ default:
81
+ throw 'Unknown decoration : ' + group.decoration;
82
+ }
83
+ pdfKitDoc.save();
84
+
85
+ if (group.decorationStyle === 'double') {
86
+ var gap = Math.max(0.5, lw * 2);
87
+ pdfKitDoc.fillColor(group.decorationColor)
88
+ .rect(x + firstInline.x, y - lw / 2, totalWidth, lw / 2).fill()
89
+ .rect(x + firstInline.x, y + gap - lw / 2, totalWidth, lw / 2).fill();
90
+ } else if (group.decorationStyle === 'dashed') {
91
+ var nbDashes = Math.ceil(totalWidth / (3.96 + 2.84));
92
+ var rdx = x + firstInline.x;
93
+ pdfKitDoc.rect(rdx, y, totalWidth, lw).clip();
94
+ pdfKitDoc.fillColor(group.decorationColor);
95
+ for (var i = 0; i < nbDashes; i++) {
96
+ pdfKitDoc.rect(rdx, y - lw / 2, 3.96, lw).fill();
97
+ rdx += 3.96 + 2.84;
98
+ }
99
+ } else if (group.decorationStyle === 'dotted') {
100
+ var nbDots = Math.ceil(totalWidth / (lw * 3));
101
+ var rx = x + firstInline.x;
102
+ pdfKitDoc.rect(rx, y, totalWidth, lw).clip();
103
+ pdfKitDoc.fillColor(group.decorationColor);
104
+ for (var ii = 0; ii < nbDots; ii++) {
105
+ pdfKitDoc.rect(rx, y - lw / 2, lw, lw).fill();
106
+ rx += (lw * 3);
107
+ }
108
+ } else if (group.decorationStyle === 'wavy') {
109
+ var sh = 0.7, sv = 1;
110
+ var nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
111
+ var rwx = x + firstInline.x - 1;
112
+ pdfKitDoc.rect(x + firstInline.x, y - sv, totalWidth, y + sv).clip();
113
+ pdfKitDoc.lineWidth(0.24);
114
+ pdfKitDoc.moveTo(rwx, y);
115
+ for (var iii = 0; iii < nbWaves; iii++) {
116
+ pdfKitDoc.bezierCurveTo(rwx + sh, y - sv, rwx + sh * 2, y - sv, rwx + sh * 3, y)
117
+ .bezierCurveTo(rwx + sh * 4, y + sv, rwx + sh * 5, y + sv, rwx + sh * 6, y);
118
+ rwx += sh * 6;
119
+ }
120
+ pdfKitDoc.stroke(group.decorationColor);
121
+ } else {
122
+ pdfKitDoc.fillColor(group.decorationColor)
123
+ .rect(x + firstInline.x, y - lw / 2, totalWidth, lw)
124
+ .fill();
125
+ }
126
+ pdfKitDoc.restore();
127
+ }
128
+
129
+ function drawDecorations(line, x, y, pdfKitDoc) {
130
+ var groups = groupDecorations(line);
131
+ for (var i = 0, l = groups.length; i < l; i++) {
132
+ drawDecoration(groups[i], x, y, pdfKitDoc);
133
+ }
134
+ }
135
+
136
+ function drawBackground(line, x, y, patterns, pdfKitDoc) {
137
+ var height = line.getHeight();
138
+ for (var i = 0, l = line.inlines.length; i < l; i++) {
139
+ var inline = line.inlines[i];
140
+ if (!inline.background) {
141
+ continue;
142
+ }
143
+ var color = inline.background;
144
+ if (isPattern(inline.background)) {
145
+ color = getPattern(inline.background, patterns);
146
+ }
147
+ var justifyShift = (inline.justifyShift || 0);
148
+ pdfKitDoc.fillColor(color)
149
+ .rect(x + inline.x - justifyShift, y, inline.width + justifyShift, height)
150
+ .fill();
151
+ }
152
+ }
153
+
154
+ module.exports = {
155
+ drawBackground: drawBackground,
156
+ drawDecorations: drawDecorations
157
+ };