@midscene/shared 0.7.2 → 0.7.3-beta-20241104100519.0
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/dist/browser/constants.js +41 -0
- package/dist/{types → browser}/fs.d.ts +2 -2
- package/dist/browser/fs.js +62 -0
- package/dist/{types → browser}/img.d.ts +23 -9
- package/dist/browser/img.js +395 -0
- package/dist/browser/index.d.ts +3 -0
- package/dist/browser/index.js +26 -0
- package/dist/browser/utils.d.ts +4 -0
- package/dist/browser/utils.js +34 -0
- package/dist/es/constants.d.ts +13 -0
- package/dist/es/fs.d.ts +14 -0
- package/dist/es/fs.js +17 -14
- package/dist/es/img.d.ts +143 -0
- package/dist/es/img.js +271 -187
- package/dist/es/index.d.ts +3 -0
- package/dist/es/index.js +1 -3
- package/dist/es/utils.d.ts +4 -0
- package/dist/es/utils.js +13 -0
- package/dist/lib/constants.d.ts +13 -0
- package/dist/lib/fs.d.ts +14 -0
- package/dist/lib/fs.js +18 -25
- package/dist/lib/img.d.ts +143 -0
- package/dist/lib/img.js +270 -185
- package/dist/lib/index.d.ts +3 -0
- package/dist/lib/index.js +1 -3
- package/dist/lib/utils.d.ts +4 -0
- package/dist/lib/utils.js +39 -0
- package/dist/types/index.d.ts +2 -2
- package/package.json +37 -20
- package/src/fs/index.ts +16 -12
- package/src/img/box-select.ts +185 -170
- package/src/img/get-jimp.ts +11 -0
- package/src/img/index.ts +2 -0
- package/src/img/info.ts +24 -6
- package/src/img/jimp.d.ts +4 -0
- package/src/img/transform.ts +31 -9
- package/src/index.ts +1 -3
- package/src/utils.ts +10 -0
- /package/dist/{types → browser}/constants.d.ts +0 -0
package/dist/es/img.js
CHANGED
|
@@ -1,27 +1,74 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
1
22
|
// src/img/info.ts
|
|
2
23
|
import assert from "assert";
|
|
3
|
-
import { Buffer } from "buffer";
|
|
24
|
+
import { Buffer as Buffer2 } from "buffer";
|
|
4
25
|
import { readFileSync } from "fs";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
assert(
|
|
17
|
-
width && height,
|
|
18
|
-
`Invalid image: ${typeof image === "string" ? image : "Buffer"}`
|
|
19
|
-
);
|
|
20
|
-
return { width, height };
|
|
26
|
+
|
|
27
|
+
// src/img/get-jimp.ts
|
|
28
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
29
|
+
function getJimp() {
|
|
30
|
+
return __async(this, null, function* () {
|
|
31
|
+
if (ifInBrowser) {
|
|
32
|
+
yield import("jimp/browser/lib/jimp.js");
|
|
33
|
+
return window.Jimp;
|
|
34
|
+
}
|
|
35
|
+
return (yield import("jimp")).default;
|
|
36
|
+
});
|
|
21
37
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
38
|
+
|
|
39
|
+
// src/img/info.ts
|
|
40
|
+
function imageInfo(image) {
|
|
41
|
+
return __async(this, null, function* () {
|
|
42
|
+
const Jimp = yield getJimp();
|
|
43
|
+
let jimpImage;
|
|
44
|
+
if (typeof image === "string") {
|
|
45
|
+
jimpImage = yield Jimp.read(image);
|
|
46
|
+
} else if (Buffer2.isBuffer(image)) {
|
|
47
|
+
jimpImage = yield Jimp.read(image);
|
|
48
|
+
} else if (image instanceof Jimp) {
|
|
49
|
+
jimpImage = image;
|
|
50
|
+
} else {
|
|
51
|
+
throw new Error("Invalid image input: must be a string path or a Buffer");
|
|
52
|
+
}
|
|
53
|
+
const { width, height } = jimpImage.bitmap;
|
|
54
|
+
assert(
|
|
55
|
+
width && height,
|
|
56
|
+
`Invalid image: ${typeof image === "string" ? image : "Buffer"}`
|
|
57
|
+
);
|
|
58
|
+
return { width, height, jimpImage };
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function imageInfoOfBase64(imageBase64) {
|
|
62
|
+
return __async(this, null, function* () {
|
|
63
|
+
const buffer = yield bufferFromBase64(imageBase64);
|
|
64
|
+
return imageInfo(buffer);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function bufferFromBase64(imageBase64) {
|
|
68
|
+
return __async(this, null, function* () {
|
|
69
|
+
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, "");
|
|
70
|
+
return Buffer2.from(base64Data, "base64");
|
|
71
|
+
});
|
|
25
72
|
}
|
|
26
73
|
function base64Encoded(image, withHeader = true) {
|
|
27
74
|
const imageBuffer = readFileSync(image);
|
|
@@ -41,32 +88,53 @@ function base64ToPngFormat(base64) {
|
|
|
41
88
|
}
|
|
42
89
|
|
|
43
90
|
// src/img/transform.ts
|
|
44
|
-
import { Buffer as
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
91
|
+
import { Buffer as Buffer3 } from "buffer";
|
|
92
|
+
function saveBase64Image(options) {
|
|
93
|
+
return __async(this, null, function* () {
|
|
94
|
+
const { base64Data, outputPath } = options;
|
|
95
|
+
const base64Image = base64Data.split(";base64,").pop() || base64Data;
|
|
96
|
+
const imageBuffer = Buffer3.from(base64Image, "base64");
|
|
97
|
+
const Jimp = yield getJimp();
|
|
98
|
+
const image = yield Jimp.read(imageBuffer);
|
|
99
|
+
yield image.writeAsync(outputPath);
|
|
100
|
+
});
|
|
52
101
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
102
|
+
function transformImgPathToBase64(inputPath) {
|
|
103
|
+
return __async(this, null, function* () {
|
|
104
|
+
const Jimp = yield getJimp();
|
|
105
|
+
const image = yield Jimp.read(inputPath);
|
|
106
|
+
const buffer = yield image.getBufferAsync(Jimp.MIME_PNG);
|
|
107
|
+
return buffer.toString("base64");
|
|
108
|
+
});
|
|
57
109
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
110
|
+
function resizeImg(inputData, newSize) {
|
|
111
|
+
return __async(this, null, function* () {
|
|
112
|
+
if (typeof inputData === "string")
|
|
113
|
+
throw Error("inputData is base64, use resizeImgBase64 instead");
|
|
114
|
+
const Jimp = yield getJimp();
|
|
115
|
+
const image = yield Jimp.read(inputData);
|
|
116
|
+
const { width, height } = image.bitmap;
|
|
117
|
+
if (!width || !height) {
|
|
118
|
+
throw Error("Undefined width or height from the input image.");
|
|
119
|
+
}
|
|
120
|
+
const finalNewSize = newSize || calculateNewDimensions(width, height);
|
|
121
|
+
image.resize(finalNewSize.width, finalNewSize.height);
|
|
122
|
+
const resizedBuffer = yield image.getBufferAsync(Jimp.MIME_PNG);
|
|
123
|
+
return resizedBuffer;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function resizeImgBase64(inputData, newSize) {
|
|
127
|
+
return __async(this, null, function* () {
|
|
128
|
+
const splitFlag = ";base64,";
|
|
129
|
+
const dataSplitted = inputData.split(splitFlag);
|
|
130
|
+
if (dataSplitted.length !== 2) {
|
|
131
|
+
throw Error("Invalid base64 data");
|
|
132
|
+
}
|
|
133
|
+
const imageBuffer = Buffer3.from(dataSplitted[1], "base64");
|
|
134
|
+
const buffer = yield resizeImg(imageBuffer, newSize);
|
|
135
|
+
const content = buffer.toString("base64");
|
|
136
|
+
return `${dataSplitted[0]}${splitFlag}${content}`;
|
|
137
|
+
});
|
|
70
138
|
}
|
|
71
139
|
function calculateNewDimensions(originalWidth, originalHeight) {
|
|
72
140
|
const maxWidth = 2048;
|
|
@@ -87,169 +155,183 @@ function calculateNewDimensions(originalWidth, originalHeight) {
|
|
|
87
155
|
height: Math.round(newHeight)
|
|
88
156
|
};
|
|
89
157
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
158
|
+
function trimImage(image) {
|
|
159
|
+
return __async(this, null, function* () {
|
|
160
|
+
const Jimp = yield getJimp();
|
|
161
|
+
const jimpImage = yield Jimp.read(
|
|
162
|
+
Buffer3.isBuffer(image) ? image : Buffer3.from(image)
|
|
163
|
+
);
|
|
164
|
+
const { width, height } = jimpImage.bitmap;
|
|
165
|
+
if (width <= 3 || height <= 3) {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
const trimmedImage = jimpImage.autocrop();
|
|
169
|
+
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap;
|
|
170
|
+
const trimOffsetLeft = (width - trimmedWidth) / 2;
|
|
171
|
+
const trimOffsetTop = (height - trimmedHeight) / 2;
|
|
172
|
+
if (trimOffsetLeft === 0 && trimOffsetTop === 0) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
trimOffsetLeft: -trimOffsetLeft,
|
|
177
|
+
trimOffsetTop: -trimOffsetTop,
|
|
178
|
+
width: trimmedWidth,
|
|
179
|
+
height: trimmedHeight
|
|
180
|
+
};
|
|
181
|
+
});
|
|
111
182
|
}
|
|
112
183
|
|
|
113
184
|
// src/img/box-select.ts
|
|
114
185
|
import assert2 from "assert";
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const
|
|
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
|
-
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
);
|
|
156
|
-
const textWidth = element.indexId.toString().length * 8;
|
|
157
|
-
const textHeight = 12;
|
|
158
|
-
const rectWidth = textWidth + 5;
|
|
159
|
-
const rectHeight = textHeight + 4;
|
|
160
|
-
let rectX = paddedRect.left - rectWidth;
|
|
161
|
-
let rectY = paddedRect.top + paddedRect.height / 2 - textHeight / 2 - 2;
|
|
162
|
-
const checkOverlap = (x, y) => {
|
|
163
|
-
return elements2.slice(0, index).some((otherElement) => {
|
|
164
|
-
return x < otherElement.rect.left + otherElement.rect.width && x + rectWidth > otherElement.rect.left && y < otherElement.rect.top + otherElement.rect.height && y + rectHeight > otherElement.rect.top;
|
|
165
|
-
});
|
|
166
|
-
};
|
|
167
|
-
const isWithinBounds = (x, y) => {
|
|
168
|
-
return x >= 0 && x + rectWidth <= imageWidth2 && y >= 0 && y + rectHeight <= imageHeight2;
|
|
169
|
-
};
|
|
170
|
-
if (checkOverlap(rectX, rectY) || !isWithinBounds(rectX, rectY)) {
|
|
171
|
-
if (!checkOverlap(paddedRect.left, paddedRect.top - rectHeight - 2) && isWithinBounds(paddedRect.left, paddedRect.top - rectHeight - 2)) {
|
|
172
|
-
rectX = paddedRect.left;
|
|
173
|
-
rectY = paddedRect.top - rectHeight - 2;
|
|
174
|
-
} else if (!checkOverlap(
|
|
175
|
-
paddedRect.left,
|
|
176
|
-
paddedRect.top + paddedRect.height + 2
|
|
177
|
-
) && isWithinBounds(
|
|
178
|
-
paddedRect.left,
|
|
179
|
-
paddedRect.top + paddedRect.height + 2
|
|
180
|
-
)) {
|
|
181
|
-
rectX = paddedRect.left;
|
|
182
|
-
rectY = paddedRect.top + paddedRect.height + 2;
|
|
183
|
-
} else if (!checkOverlap(
|
|
184
|
-
paddedRect.left + paddedRect.width + 2,
|
|
185
|
-
paddedRect.top
|
|
186
|
-
) && isWithinBounds(paddedRect.left + paddedRect.width + 2, paddedRect.top)) {
|
|
187
|
-
rectX = paddedRect.left + paddedRect.width + 2;
|
|
188
|
-
rectY = paddedRect.top;
|
|
189
|
-
} else {
|
|
190
|
-
rectX = paddedRect.left;
|
|
191
|
-
rectY = paddedRect.top + 2;
|
|
186
|
+
var cachedFont = null;
|
|
187
|
+
var createSvgOverlay = (elements, imageWidth, imageHeight) => __async(void 0, null, function* () {
|
|
188
|
+
const Jimp = yield getJimp();
|
|
189
|
+
const image = new Jimp(imageWidth, imageHeight, 0);
|
|
190
|
+
const colors = [
|
|
191
|
+
{ rect: 4278190335, text: 4294967295 }
|
|
192
|
+
// red, white
|
|
193
|
+
// { rect: 0x0000ffff, text: 0xffffffff }, // blue, white
|
|
194
|
+
// { rect: 0x8b4513ff, text: 0xffffffff }, // brown, white
|
|
195
|
+
];
|
|
196
|
+
const boxPadding = 5;
|
|
197
|
+
for (let index = 0; index < elements.length; index++) {
|
|
198
|
+
const element = elements[index];
|
|
199
|
+
const color = colors[index % colors.length];
|
|
200
|
+
const paddedRect = {
|
|
201
|
+
left: Math.max(0, element.rect.left - boxPadding),
|
|
202
|
+
top: Math.max(0, element.rect.top - boxPadding),
|
|
203
|
+
width: Math.min(
|
|
204
|
+
imageWidth - element.rect.left,
|
|
205
|
+
element.rect.width + boxPadding * 2
|
|
206
|
+
),
|
|
207
|
+
height: Math.min(
|
|
208
|
+
imageHeight - element.rect.top,
|
|
209
|
+
element.rect.height + boxPadding * 2
|
|
210
|
+
)
|
|
211
|
+
};
|
|
212
|
+
image.scan(
|
|
213
|
+
paddedRect.left,
|
|
214
|
+
paddedRect.top,
|
|
215
|
+
paddedRect.width,
|
|
216
|
+
paddedRect.height,
|
|
217
|
+
function(x, y, idx) {
|
|
218
|
+
if (x === paddedRect.left || x === paddedRect.left + paddedRect.width - 1 || y === paddedRect.top || y === paddedRect.top + paddedRect.height - 1) {
|
|
219
|
+
this.bitmap.data[idx + 0] = color.rect >> 24 & 255;
|
|
220
|
+
this.bitmap.data[idx + 1] = color.rect >> 16 & 255;
|
|
221
|
+
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
222
|
+
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
192
223
|
}
|
|
193
224
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
225
|
+
);
|
|
226
|
+
const textWidth = element.indexId.toString().length * 8;
|
|
227
|
+
const textHeight = 12;
|
|
228
|
+
const rectWidth = textWidth + 5;
|
|
229
|
+
const rectHeight = textHeight + 4;
|
|
230
|
+
let rectX = paddedRect.left - rectWidth;
|
|
231
|
+
let rectY = paddedRect.top + paddedRect.height / 2 - textHeight / 2 - 2;
|
|
232
|
+
const checkOverlap = (x, y) => {
|
|
233
|
+
return elements.slice(0, index).some((otherElement) => {
|
|
234
|
+
return x < otherElement.rect.left + otherElement.rect.width && x + rectWidth > otherElement.rect.left && y < otherElement.rect.top + otherElement.rect.height && y + rectHeight > otherElement.rect.top;
|
|
199
235
|
});
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
236
|
+
};
|
|
237
|
+
const isWithinBounds = (x, y) => {
|
|
238
|
+
return x >= 0 && x + rectWidth <= imageWidth && y >= 0 && y + rectHeight <= imageHeight;
|
|
239
|
+
};
|
|
240
|
+
if (checkOverlap(rectX, rectY) || !isWithinBounds(rectX, rectY)) {
|
|
241
|
+
if (!checkOverlap(paddedRect.left, paddedRect.top - rectHeight - 2) && isWithinBounds(paddedRect.left, paddedRect.top - rectHeight - 2)) {
|
|
242
|
+
rectX = paddedRect.left;
|
|
243
|
+
rectY = paddedRect.top - rectHeight - 2;
|
|
244
|
+
} else if (!checkOverlap(
|
|
245
|
+
paddedRect.left,
|
|
246
|
+
paddedRect.top + paddedRect.height + 2
|
|
247
|
+
) && isWithinBounds(paddedRect.left, paddedRect.top + paddedRect.height + 2)) {
|
|
248
|
+
rectX = paddedRect.left;
|
|
249
|
+
rectY = paddedRect.top + paddedRect.height + 2;
|
|
250
|
+
} else if (!checkOverlap(paddedRect.left + paddedRect.width + 2, paddedRect.top) && isWithinBounds(paddedRect.left + paddedRect.width + 2, paddedRect.top)) {
|
|
251
|
+
rectX = paddedRect.left + paddedRect.width + 2;
|
|
252
|
+
rectY = paddedRect.top;
|
|
253
|
+
} else {
|
|
254
|
+
rectX = paddedRect.left;
|
|
255
|
+
rectY = paddedRect.top + 2;
|
|
256
|
+
}
|
|
213
257
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
258
|
+
image.scan(rectX, rectY, rectWidth, rectHeight, function(x, y, idx) {
|
|
259
|
+
this.bitmap.data[idx + 0] = color.rect >> 24 & 255;
|
|
260
|
+
this.bitmap.data[idx + 1] = color.rect >> 16 & 255;
|
|
261
|
+
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
262
|
+
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
263
|
+
});
|
|
264
|
+
try {
|
|
265
|
+
cachedFont = cachedFont || (yield Jimp.loadFont(Jimp.FONT_SANS_16_WHITE));
|
|
266
|
+
} catch (error) {
|
|
267
|
+
console.error("Error loading font", error);
|
|
268
|
+
}
|
|
269
|
+
image.print(
|
|
270
|
+
cachedFont,
|
|
271
|
+
rectX,
|
|
272
|
+
rectY,
|
|
273
|
+
{
|
|
274
|
+
text: element.indexId.toString(),
|
|
275
|
+
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
|
|
276
|
+
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
|
|
277
|
+
},
|
|
278
|
+
rectWidth,
|
|
279
|
+
rectHeight
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
return image;
|
|
283
|
+
});
|
|
284
|
+
var compositeElementInfoImg = (options) => __async(void 0, null, function* () {
|
|
285
|
+
assert2(options.inputImgBase64, "inputImgBase64 is required");
|
|
286
|
+
let width = 0;
|
|
287
|
+
let height = 0;
|
|
288
|
+
let jimpImage;
|
|
289
|
+
const Jimp = yield getJimp();
|
|
290
|
+
if (options.size) {
|
|
291
|
+
width = options.size.width;
|
|
292
|
+
height = options.size.height;
|
|
293
|
+
}
|
|
294
|
+
if (!width || !height) {
|
|
295
|
+
const info = yield imageInfoOfBase64(options.inputImgBase64);
|
|
296
|
+
width = info.width;
|
|
297
|
+
height = info.height;
|
|
298
|
+
jimpImage = info.jimpImage;
|
|
299
|
+
} else {
|
|
300
|
+
const imageBuffer = yield bufferFromBase64(options.inputImgBase64);
|
|
301
|
+
jimpImage = yield Jimp.read(imageBuffer);
|
|
302
|
+
}
|
|
223
303
|
if (!width || !height) {
|
|
224
304
|
throw Error("Image processing failed because width or height is undefined");
|
|
225
305
|
}
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
306
|
+
const { elementsPositionInfo } = options;
|
|
307
|
+
const result = yield Promise.resolve(jimpImage).then((image) => __async(void 0, null, function* () {
|
|
308
|
+
const svgOverlay = yield createSvgOverlay(
|
|
309
|
+
elementsPositionInfo,
|
|
310
|
+
width,
|
|
311
|
+
height
|
|
312
|
+
);
|
|
313
|
+
const svgImage = yield Jimp.read(svgOverlay);
|
|
314
|
+
const compositeImage = yield image.composite(svgImage, 0, 0, {
|
|
315
|
+
mode: Jimp.BLEND_SOURCE_OVER,
|
|
235
316
|
opacitySource: 1,
|
|
236
317
|
opacityDest: 1
|
|
237
318
|
});
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
return
|
|
242
|
-
}).catch((error) => {
|
|
319
|
+
return compositeImage;
|
|
320
|
+
})).then((compositeImage) => __async(void 0, null, function* () {
|
|
321
|
+
const base64 = yield compositeImage.getBase64Async(Jimp.MIME_PNG);
|
|
322
|
+
return base64;
|
|
323
|
+
})).catch((error) => {
|
|
243
324
|
throw error;
|
|
244
325
|
});
|
|
245
|
-
|
|
246
|
-
|
|
326
|
+
return result;
|
|
327
|
+
});
|
|
328
|
+
var processImageElementInfo = (options) => __async(void 0, null, function* () {
|
|
247
329
|
const base64Image = options.inputImgBase64.split(";base64,").pop();
|
|
248
330
|
assert2(base64Image, "base64Image is undefined");
|
|
249
331
|
const [
|
|
250
332
|
compositeElementInfoImgBase64,
|
|
251
333
|
compositeElementInfoImgWithoutTextBase64
|
|
252
|
-
] =
|
|
334
|
+
] = yield Promise.all([
|
|
253
335
|
compositeElementInfoImg({
|
|
254
336
|
inputImgBase64: options.inputImgBase64,
|
|
255
337
|
elementsPositionInfo: options.elementsPositionInfo
|
|
@@ -263,16 +345,18 @@ var processImageElementInfo = async (options) => {
|
|
|
263
345
|
compositeElementInfoImgBase64,
|
|
264
346
|
compositeElementInfoImgWithoutTextBase64
|
|
265
347
|
};
|
|
266
|
-
};
|
|
348
|
+
});
|
|
267
349
|
export {
|
|
268
350
|
base64Encoded,
|
|
269
351
|
base64ToPngFormat,
|
|
352
|
+
bufferFromBase64,
|
|
270
353
|
calculateNewDimensions,
|
|
271
354
|
compositeElementInfoImg,
|
|
272
355
|
imageInfo,
|
|
273
356
|
imageInfoOfBase64,
|
|
274
357
|
processImageElementInfo,
|
|
275
358
|
resizeImg,
|
|
359
|
+
resizeImgBase64,
|
|
276
360
|
saveBase64Image,
|
|
277
361
|
transformImgPathToBase64,
|
|
278
362
|
trimImage
|
package/dist/es/index.js
CHANGED
package/dist/es/utils.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
import { randomUUID } from "crypto";
|
|
3
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
4
|
+
function uuid() {
|
|
5
|
+
if (ifInBrowser) {
|
|
6
|
+
return Math.random().toString(36).substring(2, 15);
|
|
7
|
+
}
|
|
8
|
+
return randomUUID();
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
ifInBrowser,
|
|
12
|
+
uuid
|
|
13
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare const TEXT_SIZE_THRESHOLD = 9;
|
|
2
|
+
declare const TEXT_MAX_SIZE = 40;
|
|
3
|
+
declare const CONTAINER_MINI_HEIGHT = 3;
|
|
4
|
+
declare const CONTAINER_MINI_WIDTH = 3;
|
|
5
|
+
declare enum NodeType {
|
|
6
|
+
CONTAINER = "CONTAINER Node",
|
|
7
|
+
FORM_ITEM = "FORM_ITEM Node",
|
|
8
|
+
BUTTON = "BUTTON Node",
|
|
9
|
+
IMG = "IMG Node",
|
|
10
|
+
TEXT = "TEXT Node"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { CONTAINER_MINI_HEIGHT, CONTAINER_MINI_WIDTH, NodeType, TEXT_MAX_SIZE, TEXT_SIZE_THRESHOLD };
|
package/dist/lib/fs.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface PkgInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
version: string;
|
|
4
|
+
dir: string;
|
|
5
|
+
}
|
|
6
|
+
declare function getRunningPkgInfo(dir?: string): PkgInfo | null;
|
|
7
|
+
/**
|
|
8
|
+
* Find the nearest package.json file recursively
|
|
9
|
+
* @param {string} dir - Home directory
|
|
10
|
+
* @returns {string|null} - The most recent package.json file path or null
|
|
11
|
+
*/
|
|
12
|
+
declare function findNearestPackageJson(dir: string): string | null;
|
|
13
|
+
|
|
14
|
+
export { findNearestPackageJson, getRunningPkgInfo };
|