-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
images.js
234 lines (197 loc) · 5.08 KB
/
images.js
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
import PDFImage from '../image';
export default {
initImages() {
this._imageRegistry = {};
return (this._imageCount = 0);
},
image(src, x, y, options = {}) {
let bh, bp, bw, image, ip, left, left1, rotateAngle, originX, originY;
if (typeof x === 'object') {
options = x;
x = null;
}
// Ignore orientation based on document options or image options
const ignoreOrientation =
options.ignoreOrientation ||
(options.ignoreOrientation !== false && this.options.ignoreOrientation);
x = (left = x != null ? x : options.x) != null ? left : this.x;
y = (left1 = y != null ? y : options.y) != null ? left1 : this.y;
if (typeof src === 'string') {
image = this._imageRegistry[src];
}
if (!image) {
if (src.width && src.height) {
image = src;
} else {
image = this.openImage(src);
}
}
if (!image.obj) {
image.embed(this);
}
if (this.page.xobjects[image.label] == null) {
this.page.xobjects[image.label] = image.obj;
}
let { width, height } = image;
// If EXIF orientation calls for it, swap width and height
if (!ignoreOrientation && image.orientation > 4) {
[width, height] = [height, width];
}
let w = options.width || width;
let h = options.height || height;
if (options.width && !options.height) {
const wp = w / width;
w = width * wp;
h = height * wp;
} else if (options.height && !options.width) {
const hp = h / height;
w = width * hp;
h = height * hp;
} else if (options.scale) {
w = width * options.scale;
h = height * options.scale;
} else if (options.fit) {
[bw, bh] = options.fit;
bp = bw / bh;
ip = width / height;
if (ip > bp) {
w = bw;
h = bw / ip;
} else {
h = bh;
w = bh * ip;
}
} else if (options.cover) {
[bw, bh] = options.cover;
bp = bw / bh;
ip = width / height;
if (ip > bp) {
h = bh;
w = bh * ip;
} else {
w = bw;
h = bw / ip;
}
}
if (options.fit || options.cover) {
if (options.align === 'center') {
x = x + bw / 2 - w / 2;
} else if (options.align === 'right') {
x = x + bw - w;
}
if (options.valign === 'center') {
y = y + bh / 2 - h / 2;
} else if (options.valign === 'bottom') {
y = y + bh - h;
}
}
if (!ignoreOrientation) {
switch (image.orientation) {
// No orientation (need to flip image, though, because of the default transform matrix on the document)
default:
case 1:
h = -h;
y -= h;
rotateAngle = 0;
break;
// Flip Horizontal
case 2:
w = -w;
h = -h;
x -= w;
y -= h;
rotateAngle = 0;
break;
// Rotate 180 degrees
case 3:
originX = x;
originY = y;
h = -h;
x -= w;
rotateAngle = 180;
break;
// Flip vertical
case 4:
// Do nothing, image will be flipped
break;
// Flip horizontally and rotate 270 degrees CW
case 5:
originX = x;
originY = y;
[w, h] = [h, w];
y -= h;
rotateAngle = 90;
break;
// Rotate 90 degrees CW
case 6:
originX = x;
originY = y;
[w, h] = [h, w];
h = -h;
rotateAngle = 90;
break;
// Flip horizontally and rotate 90 degrees CW
case 7:
originX = x;
originY = y;
[w, h] = [h, w];
h = -h;
w = -w;
x -= w;
rotateAngle = 90;
break;
// Rotate 270 degrees CW
case 8:
originX = x;
originY = y;
[w, h] = [h, w];
h = -h;
x -= w;
y -= h;
rotateAngle = -90;
break;
}
} else {
h = -h;
y -= h;
rotateAngle = 0;
}
// create link annotations if the link option is given
if (options.link != null) {
this.link(x, y, w, h, options.link);
}
if (options.goTo != null) {
this.goTo(x, y, w, h, options.goTo);
}
if (options.destination != null) {
this.addNamedDestination(options.destination, 'XYZ', x, y, null);
}
// Set the current y position to below the image if it is in the document flow
if (this.y === y) {
this.y += h;
}
this.save();
if (rotateAngle) {
this.rotate(rotateAngle, {
origin: [originX, originY]
});
}
this.transform(w, 0, 0, h, x, y);
this.addContent(`/${image.label} Do`);
this.restore();
return this;
},
openImage(src) {
let image;
if (typeof src === 'string') {
image = this._imageRegistry[src];
}
if (!image) {
image = PDFImage.open(src, `I${++this._imageCount}`);
if (typeof src === 'string') {
this._imageRegistry[src] = image;
}
}
return image;
}
};