@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
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/constants/index.ts
|
|
21
|
+
var constants_exports = {};
|
|
22
|
+
__export(constants_exports, {
|
|
23
|
+
CONTAINER_MINI_HEIGHT: () => CONTAINER_MINI_HEIGHT,
|
|
24
|
+
CONTAINER_MINI_WIDTH: () => CONTAINER_MINI_WIDTH,
|
|
25
|
+
NodeType: () => NodeType,
|
|
26
|
+
TEXT_MAX_SIZE: () => TEXT_MAX_SIZE,
|
|
27
|
+
TEXT_SIZE_THRESHOLD: () => TEXT_SIZE_THRESHOLD
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(constants_exports);
|
|
30
|
+
var TEXT_SIZE_THRESHOLD = 9;
|
|
31
|
+
var TEXT_MAX_SIZE = 40;
|
|
32
|
+
var CONTAINER_MINI_HEIGHT = 3;
|
|
33
|
+
var CONTAINER_MINI_WIDTH = 3;
|
|
34
|
+
var NodeType = /* @__PURE__ */ ((NodeType2) => {
|
|
35
|
+
NodeType2["CONTAINER"] = "CONTAINER Node";
|
|
36
|
+
NodeType2["FORM_ITEM"] = "FORM_ITEM Node";
|
|
37
|
+
NodeType2["BUTTON"] = "BUTTON Node";
|
|
38
|
+
NodeType2["IMG"] = "IMG Node";
|
|
39
|
+
NodeType2["TEXT"] = "TEXT Node";
|
|
40
|
+
return NodeType2;
|
|
41
|
+
})(NodeType || {});
|
|
@@ -3,7 +3,7 @@ interface PkgInfo {
|
|
|
3
3
|
version: string;
|
|
4
4
|
dir: string;
|
|
5
5
|
}
|
|
6
|
-
declare function
|
|
6
|
+
declare function getRunningPkgInfo(dir?: string): PkgInfo | null;
|
|
7
7
|
/**
|
|
8
8
|
* Find the nearest package.json file recursively
|
|
9
9
|
* @param {string} dir - Home directory
|
|
@@ -11,4 +11,4 @@ declare function getMidscenePkgInfo(dir: string): PkgInfo;
|
|
|
11
11
|
*/
|
|
12
12
|
declare function findNearestPackageJson(dir: string): string | null;
|
|
13
13
|
|
|
14
|
-
export { findNearestPackageJson,
|
|
14
|
+
export { findNearestPackageJson, getRunningPkgInfo };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/fs/index.ts
|
|
21
|
+
var fs_exports = {};
|
|
22
|
+
__export(fs_exports, {
|
|
23
|
+
findNearestPackageJson: () => findNearestPackageJson,
|
|
24
|
+
getRunningPkgInfo: () => getRunningPkgInfo
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(fs_exports);
|
|
27
|
+
var import_node_fs = require("fs");
|
|
28
|
+
var import_node_path = require("path");
|
|
29
|
+
var pkgCacheMap = {};
|
|
30
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
31
|
+
function getRunningPkgInfo(dir) {
|
|
32
|
+
if (ifInBrowser) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const dirToCheck = dir || process.cwd();
|
|
36
|
+
if (pkgCacheMap[dirToCheck]) {
|
|
37
|
+
return pkgCacheMap[dirToCheck];
|
|
38
|
+
}
|
|
39
|
+
const pkgDir = findNearestPackageJson(dirToCheck);
|
|
40
|
+
const pkgJsonFile = pkgDir ? (0, import_node_path.join)(pkgDir, "package.json") : null;
|
|
41
|
+
if (pkgDir && pkgJsonFile) {
|
|
42
|
+
const { name, version } = JSON.parse((0, import_node_fs.readFileSync)(pkgJsonFile, "utf-8"));
|
|
43
|
+
pkgCacheMap[dirToCheck] = { name, version, dir: pkgDir };
|
|
44
|
+
return pkgCacheMap[dirToCheck];
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
name: "midscene-unknown-package-name",
|
|
48
|
+
version: "0.0.0",
|
|
49
|
+
dir: dirToCheck
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function findNearestPackageJson(dir) {
|
|
53
|
+
const packageJsonPath = (0, import_node_path.join)(dir, "package.json");
|
|
54
|
+
if ((0, import_node_fs.existsSync)(packageJsonPath)) {
|
|
55
|
+
return dir;
|
|
56
|
+
}
|
|
57
|
+
const parentDir = (0, import_node_path.dirname)(dir);
|
|
58
|
+
if (parentDir === dir) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
return findNearestPackageJson(parentDir);
|
|
62
|
+
}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
|
+
import Jimp from 'jimp';
|
|
2
3
|
import { NodeType } from './constants.js';
|
|
3
4
|
|
|
4
5
|
interface Size$1 {
|
|
5
6
|
width: number;
|
|
6
7
|
height: number;
|
|
7
8
|
}
|
|
9
|
+
interface ImageInfo extends Size$1 {
|
|
10
|
+
jimpImage: Jimp;
|
|
11
|
+
}
|
|
8
12
|
/**
|
|
9
13
|
* Retrieves the dimensions of an image asynchronously
|
|
10
14
|
*
|
|
@@ -12,7 +16,7 @@ interface Size$1 {
|
|
|
12
16
|
* @returns A Promise that resolves to an object containing the width and height of the image
|
|
13
17
|
* @throws Error if the image data is invalid
|
|
14
18
|
*/
|
|
15
|
-
declare function imageInfo(image: string | Buffer): Promise<
|
|
19
|
+
declare function imageInfo(image: string | Buffer | Jimp): Promise<ImageInfo>;
|
|
16
20
|
/**
|
|
17
21
|
* Retrieves the dimensions of an image from a base64-encoded string
|
|
18
22
|
*
|
|
@@ -20,7 +24,8 @@ declare function imageInfo(image: string | Buffer): Promise<Size$1>;
|
|
|
20
24
|
* @returns A Promise that resolves to an object containing the width and height of the image
|
|
21
25
|
* @throws Error if the image data is invalid
|
|
22
26
|
*/
|
|
23
|
-
declare function imageInfoOfBase64(imageBase64: string): Promise<
|
|
27
|
+
declare function imageInfoOfBase64(imageBase64: string): Promise<ImageInfo>;
|
|
28
|
+
declare function bufferFromBase64(imageBase64: string): Promise<Buffer>;
|
|
24
29
|
/**
|
|
25
30
|
* Encodes an image file to a base64 encoded string
|
|
26
31
|
*
|
|
@@ -34,6 +39,7 @@ declare function imageInfoOfBase64(imageBase64: string): Promise<Size$1>;
|
|
|
34
39
|
declare function base64Encoded(image: string, withHeader?: boolean): string;
|
|
35
40
|
declare function base64ToPngFormat(base64: string): string;
|
|
36
41
|
|
|
42
|
+
/**
|
|
37
43
|
/**
|
|
38
44
|
* Saves a Base64-encoded image to a file
|
|
39
45
|
*
|
|
@@ -59,10 +65,14 @@ declare function transformImgPathToBase64(inputPath: string): Promise<string>;
|
|
|
59
65
|
* @returns A Promise that resolves to a base64-encoded string representing the resized image
|
|
60
66
|
* @throws An error if the width or height cannot be determined from the metadata
|
|
61
67
|
*/
|
|
62
|
-
declare function resizeImg(inputData:
|
|
68
|
+
declare function resizeImg(inputData: Buffer, newSize?: {
|
|
63
69
|
width: number;
|
|
64
70
|
height: number;
|
|
65
|
-
}): Promise<
|
|
71
|
+
}): Promise<Buffer>;
|
|
72
|
+
declare function resizeImgBase64(inputData: string, newSize?: {
|
|
73
|
+
width: number;
|
|
74
|
+
height: number;
|
|
75
|
+
}): Promise<string>;
|
|
66
76
|
/**
|
|
67
77
|
* Calculates new dimensions for an image while maintaining its aspect ratio.
|
|
68
78
|
*
|
|
@@ -103,12 +113,12 @@ interface Size {
|
|
|
103
113
|
type Rect = Point & Size;
|
|
104
114
|
|
|
105
115
|
type ElementType = {
|
|
106
|
-
locator
|
|
116
|
+
locator?: string;
|
|
107
117
|
rect: Rect;
|
|
108
|
-
center
|
|
109
|
-
id
|
|
118
|
+
center?: [number, number];
|
|
119
|
+
id?: string;
|
|
110
120
|
indexId: number;
|
|
111
|
-
attributes
|
|
121
|
+
attributes?: {
|
|
112
122
|
nodeType: NodeType;
|
|
113
123
|
[key: string]: string;
|
|
114
124
|
};
|
|
@@ -116,6 +126,10 @@ type ElementType = {
|
|
|
116
126
|
declare const compositeElementInfoImg: (options: {
|
|
117
127
|
inputImgBase64: string;
|
|
118
128
|
elementsPositionInfo: Array<ElementType>;
|
|
129
|
+
size?: {
|
|
130
|
+
width: number;
|
|
131
|
+
height: number;
|
|
132
|
+
};
|
|
119
133
|
}) => Promise<string>;
|
|
120
134
|
declare const processImageElementInfo: (options: {
|
|
121
135
|
inputImgBase64: string;
|
|
@@ -126,4 +140,4 @@ declare const processImageElementInfo: (options: {
|
|
|
126
140
|
compositeElementInfoImgWithoutTextBase64: string;
|
|
127
141
|
}>;
|
|
128
142
|
|
|
129
|
-
export { base64Encoded, base64ToPngFormat, calculateNewDimensions, compositeElementInfoImg, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, saveBase64Image, transformImgPathToBase64, trimImage };
|
|
143
|
+
export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, transformImgPathToBase64, trimImage };
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var __async = (__this, __arguments, generator) => {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
var fulfilled = (value) => {
|
|
32
|
+
try {
|
|
33
|
+
step(generator.next(value));
|
|
34
|
+
} catch (e) {
|
|
35
|
+
reject(e);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var rejected = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.throw(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
46
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/img/index.ts
|
|
51
|
+
var img_exports = {};
|
|
52
|
+
__export(img_exports, {
|
|
53
|
+
base64Encoded: () => base64Encoded,
|
|
54
|
+
base64ToPngFormat: () => base64ToPngFormat,
|
|
55
|
+
bufferFromBase64: () => bufferFromBase64,
|
|
56
|
+
calculateNewDimensions: () => calculateNewDimensions,
|
|
57
|
+
compositeElementInfoImg: () => compositeElementInfoImg,
|
|
58
|
+
imageInfo: () => imageInfo,
|
|
59
|
+
imageInfoOfBase64: () => imageInfoOfBase64,
|
|
60
|
+
processImageElementInfo: () => processImageElementInfo,
|
|
61
|
+
resizeImg: () => resizeImg,
|
|
62
|
+
resizeImgBase64: () => resizeImgBase64,
|
|
63
|
+
saveBase64Image: () => saveBase64Image,
|
|
64
|
+
transformImgPathToBase64: () => transformImgPathToBase64,
|
|
65
|
+
trimImage: () => trimImage
|
|
66
|
+
});
|
|
67
|
+
module.exports = __toCommonJS(img_exports);
|
|
68
|
+
|
|
69
|
+
// src/img/info.ts
|
|
70
|
+
var import_node_assert = __toESM(require("assert"));
|
|
71
|
+
var import_node_buffer = require("buffer");
|
|
72
|
+
var import_node_fs = require("fs");
|
|
73
|
+
|
|
74
|
+
// src/img/get-jimp.ts
|
|
75
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
76
|
+
function getJimp() {
|
|
77
|
+
return __async(this, null, function* () {
|
|
78
|
+
if (ifInBrowser) {
|
|
79
|
+
yield import("jimp/browser/lib/jimp.js");
|
|
80
|
+
return window.Jimp;
|
|
81
|
+
}
|
|
82
|
+
return (yield import("jimp")).default;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/img/info.ts
|
|
87
|
+
function imageInfo(image) {
|
|
88
|
+
return __async(this, null, function* () {
|
|
89
|
+
const Jimp = yield getJimp();
|
|
90
|
+
let jimpImage;
|
|
91
|
+
if (typeof image === "string") {
|
|
92
|
+
jimpImage = yield Jimp.read(image);
|
|
93
|
+
} else if (import_node_buffer.Buffer.isBuffer(image)) {
|
|
94
|
+
jimpImage = yield Jimp.read(image);
|
|
95
|
+
} else if (image instanceof Jimp) {
|
|
96
|
+
jimpImage = image;
|
|
97
|
+
} else {
|
|
98
|
+
throw new Error("Invalid image input: must be a string path or a Buffer");
|
|
99
|
+
}
|
|
100
|
+
const { width, height } = jimpImage.bitmap;
|
|
101
|
+
(0, import_node_assert.default)(
|
|
102
|
+
width && height,
|
|
103
|
+
`Invalid image: ${typeof image === "string" ? image : "Buffer"}`
|
|
104
|
+
);
|
|
105
|
+
return { width, height, jimpImage };
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function imageInfoOfBase64(imageBase64) {
|
|
109
|
+
return __async(this, null, function* () {
|
|
110
|
+
const buffer = yield bufferFromBase64(imageBase64);
|
|
111
|
+
return imageInfo(buffer);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function bufferFromBase64(imageBase64) {
|
|
115
|
+
return __async(this, null, function* () {
|
|
116
|
+
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, "");
|
|
117
|
+
return import_node_buffer.Buffer.from(base64Data, "base64");
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
function base64Encoded(image, withHeader = true) {
|
|
121
|
+
const imageBuffer = (0, import_node_fs.readFileSync)(image);
|
|
122
|
+
if (!withHeader) {
|
|
123
|
+
return imageBuffer.toString("base64");
|
|
124
|
+
}
|
|
125
|
+
if (image.endsWith("png")) {
|
|
126
|
+
return `data:image/png;base64,${imageBuffer.toString("base64")}`;
|
|
127
|
+
}
|
|
128
|
+
if (image.endsWith("jpg") || image.endsWith("jpeg")) {
|
|
129
|
+
return `data:image/jpeg;base64,${imageBuffer.toString("base64")}`;
|
|
130
|
+
}
|
|
131
|
+
throw new Error("unsupported image type");
|
|
132
|
+
}
|
|
133
|
+
function base64ToPngFormat(base64) {
|
|
134
|
+
return `data:image/png;base64,${base64}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/img/transform.ts
|
|
138
|
+
var import_node_buffer2 = require("buffer");
|
|
139
|
+
function saveBase64Image(options) {
|
|
140
|
+
return __async(this, null, function* () {
|
|
141
|
+
const { base64Data, outputPath } = options;
|
|
142
|
+
const base64Image = base64Data.split(";base64,").pop() || base64Data;
|
|
143
|
+
const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64");
|
|
144
|
+
const Jimp = yield getJimp();
|
|
145
|
+
const image = yield Jimp.read(imageBuffer);
|
|
146
|
+
yield image.writeAsync(outputPath);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
function transformImgPathToBase64(inputPath) {
|
|
150
|
+
return __async(this, null, function* () {
|
|
151
|
+
const Jimp = yield getJimp();
|
|
152
|
+
const image = yield Jimp.read(inputPath);
|
|
153
|
+
const buffer = yield image.getBufferAsync(Jimp.MIME_PNG);
|
|
154
|
+
return buffer.toString("base64");
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function resizeImg(inputData, newSize) {
|
|
158
|
+
return __async(this, null, function* () {
|
|
159
|
+
if (typeof inputData === "string")
|
|
160
|
+
throw Error("inputData is base64, use resizeImgBase64 instead");
|
|
161
|
+
const Jimp = yield getJimp();
|
|
162
|
+
const image = yield Jimp.read(inputData);
|
|
163
|
+
const { width, height } = image.bitmap;
|
|
164
|
+
if (!width || !height) {
|
|
165
|
+
throw Error("Undefined width or height from the input image.");
|
|
166
|
+
}
|
|
167
|
+
const finalNewSize = newSize || calculateNewDimensions(width, height);
|
|
168
|
+
image.resize(finalNewSize.width, finalNewSize.height);
|
|
169
|
+
const resizedBuffer = yield image.getBufferAsync(Jimp.MIME_PNG);
|
|
170
|
+
return resizedBuffer;
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
function resizeImgBase64(inputData, newSize) {
|
|
174
|
+
return __async(this, null, function* () {
|
|
175
|
+
const splitFlag = ";base64,";
|
|
176
|
+
const dataSplitted = inputData.split(splitFlag);
|
|
177
|
+
if (dataSplitted.length !== 2) {
|
|
178
|
+
throw Error("Invalid base64 data");
|
|
179
|
+
}
|
|
180
|
+
const imageBuffer = import_node_buffer2.Buffer.from(dataSplitted[1], "base64");
|
|
181
|
+
const buffer = yield resizeImg(imageBuffer, newSize);
|
|
182
|
+
const content = buffer.toString("base64");
|
|
183
|
+
return `${dataSplitted[0]}${splitFlag}${content}`;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
function calculateNewDimensions(originalWidth, originalHeight) {
|
|
187
|
+
const maxWidth = 2048;
|
|
188
|
+
const maxHeight = 768;
|
|
189
|
+
let newWidth = originalWidth;
|
|
190
|
+
let newHeight = originalHeight;
|
|
191
|
+
const aspectRatio = originalWidth / originalHeight;
|
|
192
|
+
if (originalWidth > maxWidth) {
|
|
193
|
+
newWidth = maxWidth;
|
|
194
|
+
newHeight = newWidth / aspectRatio;
|
|
195
|
+
}
|
|
196
|
+
if (newHeight > maxHeight) {
|
|
197
|
+
newHeight = maxHeight;
|
|
198
|
+
newWidth = newHeight * aspectRatio;
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
width: Math.round(newWidth),
|
|
202
|
+
height: Math.round(newHeight)
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function trimImage(image) {
|
|
206
|
+
return __async(this, null, function* () {
|
|
207
|
+
const Jimp = yield getJimp();
|
|
208
|
+
const jimpImage = yield Jimp.read(
|
|
209
|
+
import_node_buffer2.Buffer.isBuffer(image) ? image : import_node_buffer2.Buffer.from(image)
|
|
210
|
+
);
|
|
211
|
+
const { width, height } = jimpImage.bitmap;
|
|
212
|
+
if (width <= 3 || height <= 3) {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
const trimmedImage = jimpImage.autocrop();
|
|
216
|
+
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap;
|
|
217
|
+
const trimOffsetLeft = (width - trimmedWidth) / 2;
|
|
218
|
+
const trimOffsetTop = (height - trimmedHeight) / 2;
|
|
219
|
+
if (trimOffsetLeft === 0 && trimOffsetTop === 0) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
return {
|
|
223
|
+
trimOffsetLeft: -trimOffsetLeft,
|
|
224
|
+
trimOffsetTop: -trimOffsetTop,
|
|
225
|
+
width: trimmedWidth,
|
|
226
|
+
height: trimmedHeight
|
|
227
|
+
};
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/img/box-select.ts
|
|
232
|
+
var import_node_assert2 = __toESM(require("assert"));
|
|
233
|
+
var cachedFont = null;
|
|
234
|
+
var createSvgOverlay = (elements, imageWidth, imageHeight) => __async(void 0, null, function* () {
|
|
235
|
+
const Jimp = yield getJimp();
|
|
236
|
+
const image = new Jimp(imageWidth, imageHeight, 0);
|
|
237
|
+
const colors = [
|
|
238
|
+
{ rect: 4278190335, text: 4294967295 }
|
|
239
|
+
// red, white
|
|
240
|
+
// { rect: 0x0000ffff, text: 0xffffffff }, // blue, white
|
|
241
|
+
// { rect: 0x8b4513ff, text: 0xffffffff }, // brown, white
|
|
242
|
+
];
|
|
243
|
+
const boxPadding = 5;
|
|
244
|
+
for (let index = 0; index < elements.length; index++) {
|
|
245
|
+
const element = elements[index];
|
|
246
|
+
const color = colors[index % colors.length];
|
|
247
|
+
const paddedRect = {
|
|
248
|
+
left: Math.max(0, element.rect.left - boxPadding),
|
|
249
|
+
top: Math.max(0, element.rect.top - boxPadding),
|
|
250
|
+
width: Math.min(
|
|
251
|
+
imageWidth - element.rect.left,
|
|
252
|
+
element.rect.width + boxPadding * 2
|
|
253
|
+
),
|
|
254
|
+
height: Math.min(
|
|
255
|
+
imageHeight - element.rect.top,
|
|
256
|
+
element.rect.height + boxPadding * 2
|
|
257
|
+
)
|
|
258
|
+
};
|
|
259
|
+
image.scan(
|
|
260
|
+
paddedRect.left,
|
|
261
|
+
paddedRect.top,
|
|
262
|
+
paddedRect.width,
|
|
263
|
+
paddedRect.height,
|
|
264
|
+
function(x, y, idx) {
|
|
265
|
+
if (x === paddedRect.left || x === paddedRect.left + paddedRect.width - 1 || y === paddedRect.top || y === paddedRect.top + paddedRect.height - 1) {
|
|
266
|
+
this.bitmap.data[idx + 0] = color.rect >> 24 & 255;
|
|
267
|
+
this.bitmap.data[idx + 1] = color.rect >> 16 & 255;
|
|
268
|
+
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
269
|
+
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
);
|
|
273
|
+
const textWidth = element.indexId.toString().length * 8;
|
|
274
|
+
const textHeight = 12;
|
|
275
|
+
const rectWidth = textWidth + 5;
|
|
276
|
+
const rectHeight = textHeight + 4;
|
|
277
|
+
let rectX = paddedRect.left - rectWidth;
|
|
278
|
+
let rectY = paddedRect.top + paddedRect.height / 2 - textHeight / 2 - 2;
|
|
279
|
+
const checkOverlap = (x, y) => {
|
|
280
|
+
return elements.slice(0, index).some((otherElement) => {
|
|
281
|
+
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;
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
const isWithinBounds = (x, y) => {
|
|
285
|
+
return x >= 0 && x + rectWidth <= imageWidth && y >= 0 && y + rectHeight <= imageHeight;
|
|
286
|
+
};
|
|
287
|
+
if (checkOverlap(rectX, rectY) || !isWithinBounds(rectX, rectY)) {
|
|
288
|
+
if (!checkOverlap(paddedRect.left, paddedRect.top - rectHeight - 2) && isWithinBounds(paddedRect.left, paddedRect.top - rectHeight - 2)) {
|
|
289
|
+
rectX = paddedRect.left;
|
|
290
|
+
rectY = paddedRect.top - rectHeight - 2;
|
|
291
|
+
} else if (!checkOverlap(
|
|
292
|
+
paddedRect.left,
|
|
293
|
+
paddedRect.top + paddedRect.height + 2
|
|
294
|
+
) && isWithinBounds(paddedRect.left, paddedRect.top + paddedRect.height + 2)) {
|
|
295
|
+
rectX = paddedRect.left;
|
|
296
|
+
rectY = paddedRect.top + paddedRect.height + 2;
|
|
297
|
+
} else if (!checkOverlap(paddedRect.left + paddedRect.width + 2, paddedRect.top) && isWithinBounds(paddedRect.left + paddedRect.width + 2, paddedRect.top)) {
|
|
298
|
+
rectX = paddedRect.left + paddedRect.width + 2;
|
|
299
|
+
rectY = paddedRect.top;
|
|
300
|
+
} else {
|
|
301
|
+
rectX = paddedRect.left;
|
|
302
|
+
rectY = paddedRect.top + 2;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
image.scan(rectX, rectY, rectWidth, rectHeight, function(x, y, idx) {
|
|
306
|
+
this.bitmap.data[idx + 0] = color.rect >> 24 & 255;
|
|
307
|
+
this.bitmap.data[idx + 1] = color.rect >> 16 & 255;
|
|
308
|
+
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
309
|
+
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
310
|
+
});
|
|
311
|
+
try {
|
|
312
|
+
cachedFont = cachedFont || (yield Jimp.loadFont(Jimp.FONT_SANS_16_WHITE));
|
|
313
|
+
} catch (error) {
|
|
314
|
+
console.error("Error loading font", error);
|
|
315
|
+
}
|
|
316
|
+
image.print(
|
|
317
|
+
cachedFont,
|
|
318
|
+
rectX,
|
|
319
|
+
rectY,
|
|
320
|
+
{
|
|
321
|
+
text: element.indexId.toString(),
|
|
322
|
+
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
|
|
323
|
+
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
|
|
324
|
+
},
|
|
325
|
+
rectWidth,
|
|
326
|
+
rectHeight
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
return image;
|
|
330
|
+
});
|
|
331
|
+
var compositeElementInfoImg = (options) => __async(void 0, null, function* () {
|
|
332
|
+
(0, import_node_assert2.default)(options.inputImgBase64, "inputImgBase64 is required");
|
|
333
|
+
let width = 0;
|
|
334
|
+
let height = 0;
|
|
335
|
+
let jimpImage;
|
|
336
|
+
const Jimp = yield getJimp();
|
|
337
|
+
if (options.size) {
|
|
338
|
+
width = options.size.width;
|
|
339
|
+
height = options.size.height;
|
|
340
|
+
}
|
|
341
|
+
if (!width || !height) {
|
|
342
|
+
const info = yield imageInfoOfBase64(options.inputImgBase64);
|
|
343
|
+
width = info.width;
|
|
344
|
+
height = info.height;
|
|
345
|
+
jimpImage = info.jimpImage;
|
|
346
|
+
} else {
|
|
347
|
+
const imageBuffer = yield bufferFromBase64(options.inputImgBase64);
|
|
348
|
+
jimpImage = yield Jimp.read(imageBuffer);
|
|
349
|
+
}
|
|
350
|
+
if (!width || !height) {
|
|
351
|
+
throw Error("Image processing failed because width or height is undefined");
|
|
352
|
+
}
|
|
353
|
+
const { elementsPositionInfo } = options;
|
|
354
|
+
const result = yield Promise.resolve(jimpImage).then((image) => __async(void 0, null, function* () {
|
|
355
|
+
const svgOverlay = yield createSvgOverlay(
|
|
356
|
+
elementsPositionInfo,
|
|
357
|
+
width,
|
|
358
|
+
height
|
|
359
|
+
);
|
|
360
|
+
const svgImage = yield Jimp.read(svgOverlay);
|
|
361
|
+
const compositeImage = yield image.composite(svgImage, 0, 0, {
|
|
362
|
+
mode: Jimp.BLEND_SOURCE_OVER,
|
|
363
|
+
opacitySource: 1,
|
|
364
|
+
opacityDest: 1
|
|
365
|
+
});
|
|
366
|
+
return compositeImage;
|
|
367
|
+
})).then((compositeImage) => __async(void 0, null, function* () {
|
|
368
|
+
const base64 = yield compositeImage.getBase64Async(Jimp.MIME_PNG);
|
|
369
|
+
return base64;
|
|
370
|
+
})).catch((error) => {
|
|
371
|
+
throw error;
|
|
372
|
+
});
|
|
373
|
+
return result;
|
|
374
|
+
});
|
|
375
|
+
var processImageElementInfo = (options) => __async(void 0, null, function* () {
|
|
376
|
+
const base64Image = options.inputImgBase64.split(";base64,").pop();
|
|
377
|
+
(0, import_node_assert2.default)(base64Image, "base64Image is undefined");
|
|
378
|
+
const [
|
|
379
|
+
compositeElementInfoImgBase64,
|
|
380
|
+
compositeElementInfoImgWithoutTextBase64
|
|
381
|
+
] = yield Promise.all([
|
|
382
|
+
compositeElementInfoImg({
|
|
383
|
+
inputImgBase64: options.inputImgBase64,
|
|
384
|
+
elementsPositionInfo: options.elementsPositionInfo
|
|
385
|
+
}),
|
|
386
|
+
compositeElementInfoImg({
|
|
387
|
+
inputImgBase64: options.inputImgBase64,
|
|
388
|
+
elementsPositionInfo: options.elementsPositionInfoWithoutText
|
|
389
|
+
})
|
|
390
|
+
]);
|
|
391
|
+
return {
|
|
392
|
+
compositeElementInfoImgBase64,
|
|
393
|
+
compositeElementInfoImgWithoutTextBase64
|
|
394
|
+
};
|
|
395
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
default: () => src_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
var src_default = {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/utils.ts
|
|
21
|
+
var utils_exports = {};
|
|
22
|
+
__export(utils_exports, {
|
|
23
|
+
ifInBrowser: () => ifInBrowser,
|
|
24
|
+
uuid: () => uuid
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(utils_exports);
|
|
27
|
+
var import_node_crypto = require("crypto");
|
|
28
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
29
|
+
function uuid() {
|
|
30
|
+
if (ifInBrowser) {
|
|
31
|
+
return Math.random().toString(36).substring(2, 15);
|
|
32
|
+
}
|
|
33
|
+
return (0, import_node_crypto.randomUUID)();
|
|
34
|
+
}
|
|
@@ -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 };
|