@e-mc/image 0.3.3 → 0.4.1
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.
- package/LICENSE +10 -10
- package/index.d.ts +4 -4
- package/index.js +335 -335
- package/package.json +2 -3
package/LICENSE
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
Copyright 2023 An Pham
|
|
2
|
-
|
|
3
|
-
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
-
|
|
5
|
-
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
-
|
|
7
|
-
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
-
|
|
9
|
-
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
-
|
|
1
|
+
Copyright 2023 An Pham
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
11
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { ImageConstructor } from '../types/lib';
|
|
2
|
-
|
|
3
|
-
declare const Image: ImageConstructor;
|
|
4
|
-
|
|
1
|
+
import type { ImageConstructor } from '../types/lib';
|
|
2
|
+
|
|
3
|
+
declare const Image: ImageConstructor;
|
|
4
|
+
|
|
5
5
|
export = Image;
|
package/index.js
CHANGED
|
@@ -1,338 +1,338 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var _a, _b;
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
const core_1 = require("../core");
|
|
5
|
-
const kCommand = Symbol('command');
|
|
6
|
-
const kOutputAs = Symbol('outputAs');
|
|
7
|
-
const REGEXP_CROP = /\(\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*\|\s*(\d+)\s*[xX]\s*(\d+)\s*\)/;
|
|
8
|
-
const REGEXP_OPACITY = /\|\s*(\d*\.\d+)\s*\|/;
|
|
9
|
-
const REGEXP_QUALITY = /\|\s*(\d+)?(?:\s*\[\s*(photo|picture|drawing|icon|text)\s*\])?(?:\s*\[\s*(\d+)\s*\])?(?:\s*m\[\s*(\d+)\s*\])?\s*\|/;
|
|
10
|
-
const REGEXP_RESIZE = /\(\s*(\d+|auto)\s*x\s*(\d+|auto)(?:\s*\[\s*(bilinear|bicubic|hermite|bezier)\s*\])?(?:\s*\^\s*(contain|cover|scale)(?:\s*\[\s*(left|center|right)?(?:\s*\|?\s*(top|middle|bottom))?\s*\])?)?(?:\s*#\s*([a-f\d]{1,8}))?\s*\)/;
|
|
11
|
-
const REGEXP_ROTATE = /\{((?:\s*-?\d+\s*,?)+)(?:\s*#\s*([a-f\d]{1,8}))?\s*\}/i;
|
|
12
|
-
const REGEXP_METHOD = /!\s*([a-z$][\w$]*)(\()?/gi;
|
|
13
|
-
const REGEXP_SIZERANGE = /(!\s*[\w$]+)?\(\s*(\d+)\s*(?:,\s*(\d+|\*)\s*)?\)/;
|
|
14
|
-
const isNumber = (ch) => ch >= '0' && ch <= '9';
|
|
15
|
-
const parseHexDecimal = (value) => value ? +('0x' + value.toUpperCase().padEnd(8, 'F')) : NaN;
|
|
16
|
-
class Image extends core_1.Client {
|
|
17
|
-
constructor() {
|
|
18
|
-
super(...arguments);
|
|
19
|
-
this.opacityValue = NaN;
|
|
20
|
-
this[_a] = '';
|
|
21
|
-
this[_b] = '';
|
|
22
|
-
}
|
|
23
|
-
static get REGEXP_SIZERANGE() { return REGEXP_SIZERANGE; }
|
|
24
|
-
static transform(file, command, options) {
|
|
25
|
-
return Promise.resolve(options?.tempFile ? '' : null);
|
|
26
|
-
}
|
|
27
|
-
static clamp(value, min = 0, max = 1) {
|
|
28
|
-
switch (typeof value) {
|
|
29
|
-
case 'string':
|
|
30
|
-
if (!value) {
|
|
31
|
-
break;
|
|
32
|
-
}
|
|
33
|
-
case 'number':
|
|
34
|
-
return Math.min(Math.max(min, +value), max);
|
|
35
|
-
}
|
|
36
|
-
return NaN;
|
|
37
|
-
}
|
|
38
|
-
static isBinary(mime) {
|
|
39
|
-
return typeof mime === 'string' && mime.startsWith('image/') && mime !== 'image/svg+xml';
|
|
40
|
-
}
|
|
41
|
-
static toABGR(buffer) {
|
|
42
|
-
for (let i = 0, length = buffer.length; i < length; i += 4) {
|
|
43
|
-
const rgba = Uint8Array.prototype.slice.call(buffer, i, i + 4);
|
|
44
|
-
buffer[i] = rgba[3];
|
|
45
|
-
buffer[i + 1] = rgba[2];
|
|
46
|
-
buffer[i + 2] = rgba[1];
|
|
47
|
-
buffer[i + 3] = rgba[0];
|
|
48
|
-
}
|
|
49
|
-
return Buffer.from(buffer);
|
|
50
|
-
}
|
|
51
|
-
reset() {
|
|
52
|
-
super.reset();
|
|
53
|
-
this.resizeData = undefined;
|
|
54
|
-
this.cropData = undefined;
|
|
55
|
-
this.rotateData = undefined;
|
|
56
|
-
this.qualityData = undefined;
|
|
57
|
-
this.opacityValue = NaN;
|
|
58
|
-
this.methodData = undefined;
|
|
59
|
-
this[kCommand] = '';
|
|
60
|
-
this[kOutputAs] = '';
|
|
61
|
-
}
|
|
62
|
-
setCommand(value, outputAs = '') {
|
|
63
|
-
if (typeof value === 'string') {
|
|
64
|
-
this.resizeData = this.parseResize(value = value.trim());
|
|
65
|
-
this.cropData = this.parseCrop(value);
|
|
66
|
-
this.rotateData = this.parseRotate(value);
|
|
67
|
-
this.qualityData = this.parseQuality(value);
|
|
68
|
-
this.methodData = this.parseMethod(value);
|
|
69
|
-
this.opacityValue = this.parseOpacity(value);
|
|
70
|
-
this[kCommand] = value;
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
const { resize, crop, rotate, quality, method, opacity = NaN } = value;
|
|
74
|
-
this.resizeData = resize;
|
|
75
|
-
this.cropData = crop;
|
|
76
|
-
this.rotateData = rotate;
|
|
77
|
-
this.qualityData = quality;
|
|
78
|
-
this.methodData = method;
|
|
79
|
-
this.opacityValue = opacity;
|
|
80
|
-
this[kCommand] = '';
|
|
81
|
-
}
|
|
82
|
-
this[kOutputAs] = outputAs;
|
|
83
|
-
}
|
|
84
|
-
getCommand() {
|
|
85
|
-
return this[kCommand];
|
|
86
|
-
}
|
|
87
|
-
parseCommand(value) {
|
|
88
|
-
return {
|
|
89
|
-
resize: this.parseResize(value),
|
|
90
|
-
crop: this.parseCrop(value),
|
|
91
|
-
rotate: this.parseRotate(value),
|
|
92
|
-
quality: this.parseQuality(value),
|
|
93
|
-
method: this.parseMethod(value),
|
|
94
|
-
opacity: this.parseOpacity(value)
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
parseCrop(value) {
|
|
98
|
-
const match = REGEXP_CROP.exec(value);
|
|
99
|
-
if (match) {
|
|
100
|
-
return { x: +match[1], y: +match[2], width: +match[3], height: +match[4] };
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
parseOpacity(value) {
|
|
104
|
-
const match = REGEXP_OPACITY.exec(value);
|
|
105
|
-
return match ? Image.clamp(match[1]) : NaN;
|
|
106
|
-
}
|
|
107
|
-
parseQuality(value) {
|
|
108
|
-
const match = REGEXP_QUALITY.exec(value.toLowerCase());
|
|
109
|
-
if (match) {
|
|
110
|
-
return { value: match[1] ? Image.clamp(match[1], 0, 100) : NaN, preset: match[2], nearLossless: match[3] ? Image.clamp(match[3], 0, 100) : NaN, method: match[4] ? Image.clamp(match[4], 0, 6) : NaN };
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
parseResize(value) {
|
|
114
|
-
const match = REGEXP_RESIZE.exec(value.toLowerCase());
|
|
115
|
-
if (match) {
|
|
116
|
-
return { width: match[1] === 'auto' ? Infinity : +match[1], height: match[2] === 'auto' ? Infinity : +match[2], mode: match[4] || 'resize', algorithm: match[3], align: [match[5], match[6]], color: parseHexDecimal(match[7]) };
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
parseRotate(value) {
|
|
120
|
-
const match = REGEXP_ROTATE.exec(value);
|
|
121
|
-
if (match) {
|
|
122
|
-
const result = new Set();
|
|
123
|
-
for (const segment of match[1].trim().split(/\s*,\s*|\s+/)) {
|
|
124
|
-
const deg = +segment;
|
|
125
|
-
if (!isNaN(deg)) {
|
|
126
|
-
result.add(deg);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (result.size) {
|
|
130
|
-
return { values: Array.from(result), color: parseHexDecimal(match[2]) };
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
parseMethod(value) {
|
|
135
|
-
const result = [];
|
|
136
|
-
let match;
|
|
137
|
-
while (match = REGEXP_METHOD.exec(value)) {
|
|
138
|
-
if (!match[2]) {
|
|
139
|
-
result.push([match[1]]);
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
let i = match.index + match[0].length;
|
|
143
|
-
invalid: {
|
|
144
|
-
const args = [];
|
|
145
|
-
let valid = false, type = 0, next = true, current = '', stringType = '', decimal = 0, objectCount = 0, arrayCount = 0;
|
|
146
|
-
const addArg = (item) => {
|
|
147
|
-
type = 0;
|
|
148
|
-
current = '';
|
|
149
|
-
next = false;
|
|
150
|
-
args.push(item);
|
|
151
|
-
};
|
|
152
|
-
const isEscaped = (checkQuote = true) => {
|
|
153
|
-
if (!checkQuote || /["']/.test(current)) {
|
|
154
|
-
let k = 0;
|
|
155
|
-
for (let j = current.length - 1; j >= 0; --j) {
|
|
156
|
-
if (current[j] === '\\') {
|
|
157
|
-
++k;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
if (k % 2 === 1) {
|
|
161
|
-
return true;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return false;
|
|
165
|
-
};
|
|
166
|
-
found: {
|
|
167
|
-
for (const length = value.length; i < length; ++i) {
|
|
168
|
-
const ch = value[i];
|
|
169
|
-
switch (ch) {
|
|
170
|
-
case "'":
|
|
171
|
-
case '"':
|
|
172
|
-
if (type === 0) {
|
|
173
|
-
type = 3 /* METHOD_ARGTYPE.STRING */;
|
|
174
|
-
stringType = ch;
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
else if (type === 3 /* METHOD_ARGTYPE.STRING */ && ch === stringType && !isEscaped(false)) {
|
|
178
|
-
addArg(current);
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
break;
|
|
182
|
-
case '[':
|
|
183
|
-
if (type === 0) {
|
|
184
|
-
type = 1 /* METHOD_ARGTYPE.ARRAY */;
|
|
185
|
-
arrayCount = 1;
|
|
186
|
-
}
|
|
187
|
-
else if (type === 1 /* METHOD_ARGTYPE.ARRAY */ && !isEscaped()) {
|
|
188
|
-
++arrayCount;
|
|
189
|
-
}
|
|
190
|
-
break;
|
|
191
|
-
case ']':
|
|
192
|
-
if (type === 1 /* METHOD_ARGTYPE.ARRAY */ && !isEscaped() && --arrayCount === 0) {
|
|
193
|
-
try {
|
|
194
|
-
addArg(JSON.parse(current + ']'));
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
catch {
|
|
198
|
-
break invalid;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
break;
|
|
202
|
-
case '{':
|
|
203
|
-
if (type === 0) {
|
|
204
|
-
type = 2 /* METHOD_ARGTYPE.OBJECT */;
|
|
205
|
-
objectCount = 1;
|
|
206
|
-
}
|
|
207
|
-
else if (type === 2 /* METHOD_ARGTYPE.OBJECT */ && !isEscaped()) {
|
|
208
|
-
++objectCount;
|
|
209
|
-
}
|
|
210
|
-
break;
|
|
211
|
-
case '}':
|
|
212
|
-
if (type === 2 /* METHOD_ARGTYPE.OBJECT */ && !isEscaped() && --objectCount === 0) {
|
|
213
|
-
try {
|
|
214
|
-
addArg(JSON.parse(current + '}'));
|
|
215
|
-
continue;
|
|
216
|
-
}
|
|
217
|
-
catch {
|
|
218
|
-
break invalid;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
223
|
-
const isSpace = /\s/.test(ch);
|
|
224
|
-
if (type === 0) {
|
|
225
|
-
if (isSpace) {
|
|
226
|
-
continue;
|
|
227
|
-
}
|
|
228
|
-
switch (ch) {
|
|
229
|
-
case ')':
|
|
230
|
-
valid = true;
|
|
231
|
-
break found;
|
|
232
|
-
case ',':
|
|
233
|
-
if (!next) {
|
|
234
|
-
next = true;
|
|
235
|
-
continue;
|
|
236
|
-
}
|
|
237
|
-
break invalid;
|
|
238
|
-
case 't':
|
|
239
|
-
if (value.substring(i, i + 4) === 'true') {
|
|
240
|
-
addArg(true);
|
|
241
|
-
i += 3;
|
|
242
|
-
continue;
|
|
243
|
-
}
|
|
244
|
-
break invalid;
|
|
245
|
-
case 'f':
|
|
246
|
-
if (value.substring(i, i + 5) === 'false') {
|
|
247
|
-
addArg(false);
|
|
248
|
-
i += 4;
|
|
249
|
-
continue;
|
|
250
|
-
}
|
|
251
|
-
break invalid;
|
|
252
|
-
case 'n':
|
|
253
|
-
if (value.substring(i, i + 4) === 'null') {
|
|
254
|
-
addArg(null);
|
|
255
|
-
i += 3;
|
|
256
|
-
continue;
|
|
257
|
-
}
|
|
258
|
-
break invalid;
|
|
259
|
-
case 'u':
|
|
260
|
-
if (value.substring(i, i + 9) === 'undefined') {
|
|
261
|
-
addArg(undefined);
|
|
262
|
-
i += 8;
|
|
263
|
-
continue;
|
|
264
|
-
}
|
|
265
|
-
break invalid;
|
|
266
|
-
case 'N':
|
|
267
|
-
if (value.substring(i, i + 3) === 'NaN') {
|
|
268
|
-
addArg(NaN);
|
|
269
|
-
i += 2;
|
|
270
|
-
continue;
|
|
271
|
-
}
|
|
272
|
-
break invalid;
|
|
273
|
-
case '-':
|
|
274
|
-
case '+':
|
|
275
|
-
if (isNumber(value[i + 1])) {
|
|
276
|
-
type = 4 /* METHOD_ARGTYPE.NUMBER */;
|
|
277
|
-
current += ch;
|
|
278
|
-
continue;
|
|
279
|
-
}
|
|
280
|
-
break invalid;
|
|
281
|
-
default:
|
|
282
|
-
if (isNumber(ch)) {
|
|
283
|
-
type = 4 /* METHOD_ARGTYPE.NUMBER */;
|
|
284
|
-
current += ch;
|
|
285
|
-
continue;
|
|
286
|
-
}
|
|
287
|
-
break invalid;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
else if (!next) {
|
|
291
|
-
break invalid;
|
|
292
|
-
}
|
|
293
|
-
else {
|
|
294
|
-
if (type === 4 /* METHOD_ARGTYPE.NUMBER */ && !(ch === '.' && decimal++ === 0)) {
|
|
295
|
-
if (isSpace || ch === ',' || ch === ')') {
|
|
296
|
-
addArg(+current);
|
|
297
|
-
switch (ch) {
|
|
298
|
-
case ')':
|
|
299
|
-
valid = true;
|
|
300
|
-
break found;
|
|
301
|
-
case ',':
|
|
302
|
-
next = true;
|
|
303
|
-
default:
|
|
304
|
-
continue;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
else if (!isNumber(ch)) {
|
|
308
|
-
break invalid;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
current += ch;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
if (valid) {
|
|
316
|
-
result.push([match[1], args]);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
REGEXP_METHOD.lastIndex = i;
|
|
320
|
-
}
|
|
321
|
-
REGEXP_METHOD.lastIndex = 0;
|
|
322
|
-
if (result.length) {
|
|
323
|
-
return result;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
get outputAs() {
|
|
327
|
-
return this[kOutputAs];
|
|
328
|
-
}
|
|
329
|
-
set abortable(value) { }
|
|
330
|
-
get abortable() {
|
|
331
|
-
return false;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
_a = kCommand, _b = kOutputAs;
|
|
335
|
-
exports.default = Image;
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a, _b;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const core_1 = require("../core");
|
|
5
|
+
const kCommand = Symbol('command');
|
|
6
|
+
const kOutputAs = Symbol('outputAs');
|
|
7
|
+
const REGEXP_CROP = /\(\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*\|\s*(\d+)\s*[xX]\s*(\d+)\s*\)/;
|
|
8
|
+
const REGEXP_OPACITY = /\|\s*(\d*\.\d+)\s*\|/;
|
|
9
|
+
const REGEXP_QUALITY = /\|\s*(\d+)?(?:\s*\[\s*(photo|picture|drawing|icon|text)\s*\])?(?:\s*\[\s*(\d+)\s*\])?(?:\s*m\[\s*(\d+)\s*\])?\s*\|/;
|
|
10
|
+
const REGEXP_RESIZE = /\(\s*(\d+|auto)\s*x\s*(\d+|auto)(?:\s*\[\s*(bilinear|bicubic|hermite|bezier)\s*\])?(?:\s*\^\s*(contain|cover|scale)(?:\s*\[\s*(left|center|right)?(?:\s*\|?\s*(top|middle|bottom))?\s*\])?)?(?:\s*#\s*([a-f\d]{1,8}))?\s*\)/;
|
|
11
|
+
const REGEXP_ROTATE = /\{((?:\s*-?\d+\s*,?)+)(?:\s*#\s*([a-f\d]{1,8}))?\s*\}/i;
|
|
12
|
+
const REGEXP_METHOD = /!\s*([a-z$][\w$]*)(\()?/gi;
|
|
13
|
+
const REGEXP_SIZERANGE = /(!\s*[\w$]+)?\(\s*(\d+)\s*(?:,\s*(\d+|\*)\s*)?\)/;
|
|
14
|
+
const isNumber = (ch) => ch >= '0' && ch <= '9';
|
|
15
|
+
const parseHexDecimal = (value) => value ? +('0x' + value.toUpperCase().padEnd(8, 'F')) : NaN;
|
|
16
|
+
class Image extends core_1.Client {
|
|
17
|
+
constructor() {
|
|
18
|
+
super(...arguments);
|
|
19
|
+
this.opacityValue = NaN;
|
|
20
|
+
this[_a] = '';
|
|
21
|
+
this[_b] = '';
|
|
22
|
+
}
|
|
23
|
+
static get REGEXP_SIZERANGE() { return REGEXP_SIZERANGE; }
|
|
24
|
+
static transform(file, command, options) {
|
|
25
|
+
return Promise.resolve(options?.tempFile ? '' : null);
|
|
26
|
+
}
|
|
27
|
+
static clamp(value, min = 0, max = 1) {
|
|
28
|
+
switch (typeof value) {
|
|
29
|
+
case 'string':
|
|
30
|
+
if (!value) {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
case 'number':
|
|
34
|
+
return Math.min(Math.max(min, +value), max);
|
|
35
|
+
}
|
|
36
|
+
return NaN;
|
|
37
|
+
}
|
|
38
|
+
static isBinary(mime) {
|
|
39
|
+
return typeof mime === 'string' && mime.startsWith('image/') && mime !== 'image/svg+xml';
|
|
40
|
+
}
|
|
41
|
+
static toABGR(buffer) {
|
|
42
|
+
for (let i = 0, length = buffer.length; i < length; i += 4) {
|
|
43
|
+
const rgba = Uint8Array.prototype.slice.call(buffer, i, i + 4);
|
|
44
|
+
buffer[i] = rgba[3];
|
|
45
|
+
buffer[i + 1] = rgba[2];
|
|
46
|
+
buffer[i + 2] = rgba[1];
|
|
47
|
+
buffer[i + 3] = rgba[0];
|
|
48
|
+
}
|
|
49
|
+
return Buffer.from(buffer);
|
|
50
|
+
}
|
|
51
|
+
reset() {
|
|
52
|
+
super.reset();
|
|
53
|
+
this.resizeData = undefined;
|
|
54
|
+
this.cropData = undefined;
|
|
55
|
+
this.rotateData = undefined;
|
|
56
|
+
this.qualityData = undefined;
|
|
57
|
+
this.opacityValue = NaN;
|
|
58
|
+
this.methodData = undefined;
|
|
59
|
+
this[kCommand] = '';
|
|
60
|
+
this[kOutputAs] = '';
|
|
61
|
+
}
|
|
62
|
+
setCommand(value, outputAs = '') {
|
|
63
|
+
if (typeof value === 'string') {
|
|
64
|
+
this.resizeData = this.parseResize(value = value.trim());
|
|
65
|
+
this.cropData = this.parseCrop(value);
|
|
66
|
+
this.rotateData = this.parseRotate(value);
|
|
67
|
+
this.qualityData = this.parseQuality(value);
|
|
68
|
+
this.methodData = this.parseMethod(value);
|
|
69
|
+
this.opacityValue = this.parseOpacity(value);
|
|
70
|
+
this[kCommand] = value;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
const { resize, crop, rotate, quality, method, opacity = NaN } = value;
|
|
74
|
+
this.resizeData = resize;
|
|
75
|
+
this.cropData = crop;
|
|
76
|
+
this.rotateData = rotate;
|
|
77
|
+
this.qualityData = quality;
|
|
78
|
+
this.methodData = method;
|
|
79
|
+
this.opacityValue = opacity;
|
|
80
|
+
this[kCommand] = '';
|
|
81
|
+
}
|
|
82
|
+
this[kOutputAs] = outputAs;
|
|
83
|
+
}
|
|
84
|
+
getCommand() {
|
|
85
|
+
return this[kCommand];
|
|
86
|
+
}
|
|
87
|
+
parseCommand(value) {
|
|
88
|
+
return {
|
|
89
|
+
resize: this.parseResize(value),
|
|
90
|
+
crop: this.parseCrop(value),
|
|
91
|
+
rotate: this.parseRotate(value),
|
|
92
|
+
quality: this.parseQuality(value),
|
|
93
|
+
method: this.parseMethod(value),
|
|
94
|
+
opacity: this.parseOpacity(value)
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
parseCrop(value) {
|
|
98
|
+
const match = REGEXP_CROP.exec(value);
|
|
99
|
+
if (match) {
|
|
100
|
+
return { x: +match[1], y: +match[2], width: +match[3], height: +match[4] };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
parseOpacity(value) {
|
|
104
|
+
const match = REGEXP_OPACITY.exec(value);
|
|
105
|
+
return match ? Image.clamp(match[1]) : NaN;
|
|
106
|
+
}
|
|
107
|
+
parseQuality(value) {
|
|
108
|
+
const match = REGEXP_QUALITY.exec(value.toLowerCase());
|
|
109
|
+
if (match) {
|
|
110
|
+
return { value: match[1] ? Image.clamp(match[1], 0, 100) : NaN, preset: match[2], nearLossless: match[3] ? Image.clamp(match[3], 0, 100) : NaN, method: match[4] ? Image.clamp(match[4], 0, 6) : NaN };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
parseResize(value) {
|
|
114
|
+
const match = REGEXP_RESIZE.exec(value.toLowerCase());
|
|
115
|
+
if (match) {
|
|
116
|
+
return { width: match[1] === 'auto' ? Infinity : +match[1], height: match[2] === 'auto' ? Infinity : +match[2], mode: match[4] || 'resize', algorithm: match[3], align: [match[5], match[6]], color: parseHexDecimal(match[7]) };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
parseRotate(value) {
|
|
120
|
+
const match = REGEXP_ROTATE.exec(value);
|
|
121
|
+
if (match) {
|
|
122
|
+
const result = new Set();
|
|
123
|
+
for (const segment of match[1].trim().split(/\s*,\s*|\s+/)) {
|
|
124
|
+
const deg = +segment;
|
|
125
|
+
if (!isNaN(deg)) {
|
|
126
|
+
result.add(deg);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (result.size) {
|
|
130
|
+
return { values: Array.from(result), color: parseHexDecimal(match[2]) };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
parseMethod(value) {
|
|
135
|
+
const result = [];
|
|
136
|
+
let match;
|
|
137
|
+
while (match = REGEXP_METHOD.exec(value)) {
|
|
138
|
+
if (!match[2]) {
|
|
139
|
+
result.push([match[1]]);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
let i = match.index + match[0].length;
|
|
143
|
+
invalid: {
|
|
144
|
+
const args = [];
|
|
145
|
+
let valid = false, type = 0, next = true, current = '', stringType = '', decimal = 0, objectCount = 0, arrayCount = 0;
|
|
146
|
+
const addArg = (item) => {
|
|
147
|
+
type = 0;
|
|
148
|
+
current = '';
|
|
149
|
+
next = false;
|
|
150
|
+
args.push(item);
|
|
151
|
+
};
|
|
152
|
+
const isEscaped = (checkQuote = true) => {
|
|
153
|
+
if (!checkQuote || /["']/.test(current)) {
|
|
154
|
+
let k = 0;
|
|
155
|
+
for (let j = current.length - 1; j >= 0; --j) {
|
|
156
|
+
if (current[j] === '\\') {
|
|
157
|
+
++k;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (k % 2 === 1) {
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
};
|
|
166
|
+
found: {
|
|
167
|
+
for (const length = value.length; i < length; ++i) {
|
|
168
|
+
const ch = value[i];
|
|
169
|
+
switch (ch) {
|
|
170
|
+
case "'":
|
|
171
|
+
case '"':
|
|
172
|
+
if (type === 0) {
|
|
173
|
+
type = 3 /* METHOD_ARGTYPE.STRING */;
|
|
174
|
+
stringType = ch;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
else if (type === 3 /* METHOD_ARGTYPE.STRING */ && ch === stringType && !isEscaped(false)) {
|
|
178
|
+
addArg(current);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
case '[':
|
|
183
|
+
if (type === 0) {
|
|
184
|
+
type = 1 /* METHOD_ARGTYPE.ARRAY */;
|
|
185
|
+
arrayCount = 1;
|
|
186
|
+
}
|
|
187
|
+
else if (type === 1 /* METHOD_ARGTYPE.ARRAY */ && !isEscaped()) {
|
|
188
|
+
++arrayCount;
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
case ']':
|
|
192
|
+
if (type === 1 /* METHOD_ARGTYPE.ARRAY */ && !isEscaped() && --arrayCount === 0) {
|
|
193
|
+
try {
|
|
194
|
+
addArg(JSON.parse(current + ']'));
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
break invalid;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
break;
|
|
202
|
+
case '{':
|
|
203
|
+
if (type === 0) {
|
|
204
|
+
type = 2 /* METHOD_ARGTYPE.OBJECT */;
|
|
205
|
+
objectCount = 1;
|
|
206
|
+
}
|
|
207
|
+
else if (type === 2 /* METHOD_ARGTYPE.OBJECT */ && !isEscaped()) {
|
|
208
|
+
++objectCount;
|
|
209
|
+
}
|
|
210
|
+
break;
|
|
211
|
+
case '}':
|
|
212
|
+
if (type === 2 /* METHOD_ARGTYPE.OBJECT */ && !isEscaped() && --objectCount === 0) {
|
|
213
|
+
try {
|
|
214
|
+
addArg(JSON.parse(current + '}'));
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
break invalid;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
const isSpace = /\s/.test(ch);
|
|
224
|
+
if (type === 0) {
|
|
225
|
+
if (isSpace) {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
switch (ch) {
|
|
229
|
+
case ')':
|
|
230
|
+
valid = true;
|
|
231
|
+
break found;
|
|
232
|
+
case ',':
|
|
233
|
+
if (!next) {
|
|
234
|
+
next = true;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
break invalid;
|
|
238
|
+
case 't':
|
|
239
|
+
if (value.substring(i, i + 4) === 'true') {
|
|
240
|
+
addArg(true);
|
|
241
|
+
i += 3;
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
break invalid;
|
|
245
|
+
case 'f':
|
|
246
|
+
if (value.substring(i, i + 5) === 'false') {
|
|
247
|
+
addArg(false);
|
|
248
|
+
i += 4;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
break invalid;
|
|
252
|
+
case 'n':
|
|
253
|
+
if (value.substring(i, i + 4) === 'null') {
|
|
254
|
+
addArg(null);
|
|
255
|
+
i += 3;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
break invalid;
|
|
259
|
+
case 'u':
|
|
260
|
+
if (value.substring(i, i + 9) === 'undefined') {
|
|
261
|
+
addArg(undefined);
|
|
262
|
+
i += 8;
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
break invalid;
|
|
266
|
+
case 'N':
|
|
267
|
+
if (value.substring(i, i + 3) === 'NaN') {
|
|
268
|
+
addArg(NaN);
|
|
269
|
+
i += 2;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
break invalid;
|
|
273
|
+
case '-':
|
|
274
|
+
case '+':
|
|
275
|
+
if (isNumber(value[i + 1])) {
|
|
276
|
+
type = 4 /* METHOD_ARGTYPE.NUMBER */;
|
|
277
|
+
current += ch;
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
break invalid;
|
|
281
|
+
default:
|
|
282
|
+
if (isNumber(ch)) {
|
|
283
|
+
type = 4 /* METHOD_ARGTYPE.NUMBER */;
|
|
284
|
+
current += ch;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
break invalid;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
else if (!next) {
|
|
291
|
+
break invalid;
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
if (type === 4 /* METHOD_ARGTYPE.NUMBER */ && !(ch === '.' && decimal++ === 0)) {
|
|
295
|
+
if (isSpace || ch === ',' || ch === ')') {
|
|
296
|
+
addArg(+current);
|
|
297
|
+
switch (ch) {
|
|
298
|
+
case ')':
|
|
299
|
+
valid = true;
|
|
300
|
+
break found;
|
|
301
|
+
case ',':
|
|
302
|
+
next = true;
|
|
303
|
+
default:
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
else if (!isNumber(ch)) {
|
|
308
|
+
break invalid;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
current += ch;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (valid) {
|
|
316
|
+
result.push([match[1], args]);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
REGEXP_METHOD.lastIndex = i;
|
|
320
|
+
}
|
|
321
|
+
REGEXP_METHOD.lastIndex = 0;
|
|
322
|
+
if (result.length) {
|
|
323
|
+
return result;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
get outputAs() {
|
|
327
|
+
return this[kOutputAs];
|
|
328
|
+
}
|
|
329
|
+
set abortable(value) { }
|
|
330
|
+
get abortable() {
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
_a = kCommand, _b = kOutputAs;
|
|
335
|
+
exports.default = Image;
|
|
336
336
|
|
|
337
337
|
if (exports.default) {
|
|
338
338
|
module.exports = exports.default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/image",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Image constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
"license": "BSD 3-Clause",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/core": "0.
|
|
24
|
-
"@e-mc/types": "0.3.3"
|
|
23
|
+
"@e-mc/core": "0.4.1"
|
|
25
24
|
}
|
|
26
25
|
}
|