forked from foliojs/pdfkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient.js
More file actions
268 lines (229 loc) · 6.04 KB
/
gradient.js
File metadata and controls
268 lines (229 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import PDFObject from './object';
const { number } = PDFObject;
class PDFGradient {
constructor(doc) {
this.doc = doc;
this.stops = [];
this.embedded = false;
this.transform = [1, 0, 0, 1, 0, 0];
}
stop(pos, color, opacity) {
if (opacity == null) {
opacity = 1;
}
color = this.doc._normalizeColor(color);
if (this.stops.length === 0) {
if (color.length === 3) {
this._colorSpace = 'DeviceRGB';
} else if (color.length === 4) {
this._colorSpace = 'DeviceCMYK';
} else if (color.length === 1) {
this._colorSpace = 'DeviceGray';
} else {
throw new Error('Unknown color space');
}
} else if (
(this._colorSpace === 'DeviceRGB' && color.length !== 3) ||
(this._colorSpace === 'DeviceCMYK' && color.length !== 4) ||
(this._colorSpace === 'DeviceGray' && color.length !== 1)
) {
throw new Error('All gradient stops must use the same color space');
}
opacity = Math.max(0, Math.min(1, opacity));
this.stops.push([pos, color, opacity]);
return this;
}
setTransform(m11, m12, m21, m22, dx, dy) {
this.transform = [m11, m12, m21, m22, dx, dy];
return this;
}
embed(m) {
let fn;
const stopsLength = this.stops.length;
if (stopsLength === 0) {
return;
}
this.embedded = true;
this.matrix = m;
// if the last stop comes before 100%, add a copy at 100%
const last = this.stops[stopsLength - 1];
if (last[0] < 1) {
this.stops.push([1, last[1], last[2]]);
}
const bounds = [];
const encode = [];
const stops = [];
for (let i = 0; i < stopsLength - 1; i++) {
encode.push(0, 1);
if (i + 2 !== stopsLength) {
bounds.push(this.stops[i + 1][0]);
}
fn = this.doc.ref({
FunctionType: 2,
Domain: [0, 1],
C0: this.stops[i + 0][1],
C1: this.stops[i + 1][1],
N: 1,
});
stops.push(fn);
fn.end();
}
// if there are only two stops, we don't need a stitching function
if (stopsLength === 1) {
fn = stops[0];
} else {
fn = this.doc.ref({
FunctionType: 3, // stitching function
Domain: [0, 1],
Functions: stops,
Bounds: bounds,
Encode: encode,
});
fn.end();
}
this.id = `Sh${++this.doc._gradCount}`;
const shader = this.shader(fn);
shader.end();
const pattern = this.doc.ref({
Type: 'Pattern',
PatternType: 2,
Shading: shader,
Matrix: this.matrix.map(number),
});
pattern.end();
if (this.stops.some((stop) => stop[2] < 1)) {
let grad = this.opacityGradient();
grad._colorSpace = 'DeviceGray';
for (let stop of this.stops) {
grad.stop(stop[0], [stop[2]]);
}
grad = grad.embed(this.matrix);
const pageBBox = [0, 0, this.doc.page.width, this.doc.page.height];
const form = this.doc.ref({
Type: 'XObject',
Subtype: 'Form',
FormType: 1,
BBox: pageBBox,
Group: {
Type: 'Group',
S: 'Transparency',
CS: 'DeviceGray',
},
Resources: {
ProcSet: ['PDF', 'Text', 'ImageB', 'ImageC', 'ImageI'],
Pattern: {
Sh1: grad,
},
},
});
form.write('/Pattern cs /Sh1 scn');
form.end(`${pageBBox.join(' ')} re f`);
const gstate = this.doc.ref({
Type: 'ExtGState',
SMask: {
Type: 'Mask',
S: 'Luminosity',
G: form,
},
});
gstate.end();
const opacityPattern = this.doc.ref({
Type: 'Pattern',
PatternType: 1,
PaintType: 1,
TilingType: 2,
BBox: pageBBox,
XStep: pageBBox[2],
YStep: pageBBox[3],
Resources: {
ProcSet: ['PDF', 'Text', 'ImageB', 'ImageC', 'ImageI'],
Pattern: {
Sh1: pattern,
},
ExtGState: {
Gs1: gstate,
},
},
});
opacityPattern.write('/Gs1 gs /Pattern cs /Sh1 scn');
opacityPattern.end(`${pageBBox.join(' ')} re f`);
this.doc.page.patterns[this.id] = opacityPattern;
} else {
this.doc.page.patterns[this.id] = pattern;
}
return pattern;
}
apply(stroke) {
// apply gradient transform to existing document ctm
const [m0, m1, m2, m3, m4, m5] = this.doc._ctm;
const [m11, m12, m21, m22, dx, dy] = this.transform;
const m = [
m0 * m11 + m2 * m12,
m1 * m11 + m3 * m12,
m0 * m21 + m2 * m22,
m1 * m21 + m3 * m22,
m0 * dx + m2 * dy + m4,
m1 * dx + m3 * dy + m5,
];
if (!this.embedded || m.join(' ') !== this.matrix.join(' ')) {
this.embed(m);
}
this.doc._setColorSpace('Pattern', stroke);
const op = stroke ? 'SCN' : 'scn';
return this.doc.addContent(`/${this.id} ${op}`);
}
}
class PDFLinearGradient extends PDFGradient {
constructor(doc, x1, y1, x2, y2) {
super(doc);
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
shader(fn) {
return this.doc.ref({
ShadingType: 2,
ColorSpace: this._colorSpace,
Coords: [this.x1, this.y1, this.x2, this.y2],
Function: fn,
Extend: [true, true],
});
}
opacityGradient() {
return new PDFLinearGradient(this.doc, this.x1, this.y1, this.x2, this.y2);
}
}
class PDFRadialGradient extends PDFGradient {
constructor(doc, x1, y1, r1, x2, y2, r2) {
super(doc);
this.doc = doc;
this.x1 = x1;
this.y1 = y1;
this.r1 = r1;
this.x2 = x2;
this.y2 = y2;
this.r2 = r2;
}
shader(fn) {
return this.doc.ref({
ShadingType: 3,
ColorSpace: this._colorSpace,
Coords: [this.x1, this.y1, this.r1, this.x2, this.y2, this.r2],
Function: fn,
Extend: [true, true],
});
}
opacityGradient() {
return new PDFRadialGradient(
this.doc,
this.x1,
this.y1,
this.r1,
this.x2,
this.y2,
this.r2,
);
}
}
export default { PDFGradient, PDFLinearGradient, PDFRadialGradient };