@midscene/shared 0.15.2 → 0.15.3-beta-20250423152742.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/es/img.d.ts +5 -16
- package/dist/es/img.js +19 -3
- package/dist/lib/img.d.ts +5 -16
- package/dist/lib/img.js +19 -3
- package/dist/types/img.d.ts +5 -16
- package/package.json +1 -1
- package/src/img/box-select.ts +27 -22
package/dist/es/img.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
2
|
import Jimp from 'jimp';
|
|
3
|
-
import { R as Rect } from './index-305e7a7e.js';
|
|
4
|
-
import
|
|
3
|
+
import { R as Rect, B as BaseElement } from './index-305e7a7e.js';
|
|
4
|
+
import './constants.js';
|
|
5
5
|
|
|
6
6
|
interface Size {
|
|
7
7
|
width: number;
|
|
@@ -114,20 +114,9 @@ declare function paddingToMatchBlockByBase64(imageBase64: string, blockSize?: nu
|
|
|
114
114
|
declare function cropByRect(imageBase64: string, rect: Rect, paddingImage: boolean): Promise<string>;
|
|
115
115
|
declare function jimpToBase64(image: Jimp): Promise<string>;
|
|
116
116
|
|
|
117
|
-
type ElementType = {
|
|
118
|
-
locator?: string;
|
|
119
|
-
rect: Rect;
|
|
120
|
-
center?: [number, number];
|
|
121
|
-
id?: string;
|
|
122
|
-
indexId: number;
|
|
123
|
-
attributes?: {
|
|
124
|
-
nodeType: NodeType;
|
|
125
|
-
[key: string]: string;
|
|
126
|
-
};
|
|
127
|
-
};
|
|
128
117
|
declare const compositeElementInfoImg: (options: {
|
|
129
118
|
inputImgBase64: string;
|
|
130
|
-
elementsPositionInfo: Array<
|
|
119
|
+
elementsPositionInfo: Array<BaseElement>;
|
|
131
120
|
size?: {
|
|
132
121
|
width: number;
|
|
133
122
|
height: number;
|
|
@@ -136,8 +125,8 @@ declare const compositeElementInfoImg: (options: {
|
|
|
136
125
|
}) => Promise<string>;
|
|
137
126
|
declare const processImageElementInfo: (options: {
|
|
138
127
|
inputImgBase64: string;
|
|
139
|
-
elementsPositionInfo: Array<
|
|
140
|
-
elementsPositionInfoWithoutText: Array<
|
|
128
|
+
elementsPositionInfo: Array<BaseElement>;
|
|
129
|
+
elementsPositionInfoWithoutText: Array<BaseElement>;
|
|
141
130
|
}) => Promise<{
|
|
142
131
|
compositeElementInfoImgBase64: string;
|
|
143
132
|
compositeElementInfoImgWithoutTextBase64: string;
|
package/dist/es/img.js
CHANGED
|
@@ -217,6 +217,18 @@ async function jimpToBase64(image) {
|
|
|
217
217
|
// src/img/box-select.ts
|
|
218
218
|
import assert3 from "assert";
|
|
219
219
|
var cachedFont = null;
|
|
220
|
+
var loadFonts = async () => {
|
|
221
|
+
const Jimp = await getJimp();
|
|
222
|
+
try {
|
|
223
|
+
const fonts = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
|
|
224
|
+
return fonts;
|
|
225
|
+
} catch (error) {
|
|
226
|
+
console.warn("Error loading font, will try to load online fonts", error);
|
|
227
|
+
const onlineFonts = "https://cdn.jsdelivr.net/npm/jimp-compact@0.16.1-2/fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt";
|
|
228
|
+
const fonts = await Jimp.loadFont(onlineFonts);
|
|
229
|
+
return fonts;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
220
232
|
var createSvgOverlay = async (elements, imageWidth, imageHeight, boxPadding = 5) => {
|
|
221
233
|
const Jimp = await getJimp();
|
|
222
234
|
const image = new Jimp(imageWidth, imageHeight, 0);
|
|
@@ -261,7 +273,11 @@ var createSvgOverlay = async (elements, imageWidth, imageHeight, boxPadding = 5)
|
|
|
261
273
|
}
|
|
262
274
|
}
|
|
263
275
|
);
|
|
264
|
-
const
|
|
276
|
+
const indexId = element.indexId;
|
|
277
|
+
if (typeof indexId !== "number") {
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
const textWidth = indexId.toString().length * 8;
|
|
265
281
|
const textHeight = 12;
|
|
266
282
|
const rectWidth = textWidth + 5;
|
|
267
283
|
const rectHeight = textHeight + 4;
|
|
@@ -306,7 +322,7 @@ var createSvgOverlay = async (elements, imageWidth, imageHeight, boxPadding = 5)
|
|
|
306
322
|
}
|
|
307
323
|
);
|
|
308
324
|
try {
|
|
309
|
-
cachedFont = cachedFont || await
|
|
325
|
+
cachedFont = cachedFont || await loadFonts();
|
|
310
326
|
} catch (error) {
|
|
311
327
|
console.error("Error loading font", error);
|
|
312
328
|
}
|
|
@@ -315,7 +331,7 @@ var createSvgOverlay = async (elements, imageWidth, imageHeight, boxPadding = 5)
|
|
|
315
331
|
rectX,
|
|
316
332
|
rectY,
|
|
317
333
|
{
|
|
318
|
-
text:
|
|
334
|
+
text: indexId.toString(),
|
|
319
335
|
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
|
|
320
336
|
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
|
|
321
337
|
},
|
package/dist/lib/img.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
2
|
import Jimp from 'jimp';
|
|
3
|
-
import { R as Rect } from './index-305e7a7e.js';
|
|
4
|
-
import
|
|
3
|
+
import { R as Rect, B as BaseElement } from './index-305e7a7e.js';
|
|
4
|
+
import './constants.js';
|
|
5
5
|
|
|
6
6
|
interface Size {
|
|
7
7
|
width: number;
|
|
@@ -114,20 +114,9 @@ declare function paddingToMatchBlockByBase64(imageBase64: string, blockSize?: nu
|
|
|
114
114
|
declare function cropByRect(imageBase64: string, rect: Rect, paddingImage: boolean): Promise<string>;
|
|
115
115
|
declare function jimpToBase64(image: Jimp): Promise<string>;
|
|
116
116
|
|
|
117
|
-
type ElementType = {
|
|
118
|
-
locator?: string;
|
|
119
|
-
rect: Rect;
|
|
120
|
-
center?: [number, number];
|
|
121
|
-
id?: string;
|
|
122
|
-
indexId: number;
|
|
123
|
-
attributes?: {
|
|
124
|
-
nodeType: NodeType;
|
|
125
|
-
[key: string]: string;
|
|
126
|
-
};
|
|
127
|
-
};
|
|
128
117
|
declare const compositeElementInfoImg: (options: {
|
|
129
118
|
inputImgBase64: string;
|
|
130
|
-
elementsPositionInfo: Array<
|
|
119
|
+
elementsPositionInfo: Array<BaseElement>;
|
|
131
120
|
size?: {
|
|
132
121
|
width: number;
|
|
133
122
|
height: number;
|
|
@@ -136,8 +125,8 @@ declare const compositeElementInfoImg: (options: {
|
|
|
136
125
|
}) => Promise<string>;
|
|
137
126
|
declare const processImageElementInfo: (options: {
|
|
138
127
|
inputImgBase64: string;
|
|
139
|
-
elementsPositionInfo: Array<
|
|
140
|
-
elementsPositionInfoWithoutText: Array<
|
|
128
|
+
elementsPositionInfo: Array<BaseElement>;
|
|
129
|
+
elementsPositionInfoWithoutText: Array<BaseElement>;
|
|
141
130
|
}) => Promise<{
|
|
142
131
|
compositeElementInfoImgBase64: string;
|
|
143
132
|
compositeElementInfoImgWithoutTextBase64: string;
|
package/dist/lib/img.js
CHANGED
|
@@ -272,6 +272,18 @@ async function jimpToBase64(image) {
|
|
|
272
272
|
// src/img/box-select.ts
|
|
273
273
|
var import_node_assert3 = __toESM(require("assert"));
|
|
274
274
|
var cachedFont = null;
|
|
275
|
+
var loadFonts = async () => {
|
|
276
|
+
const Jimp = await getJimp();
|
|
277
|
+
try {
|
|
278
|
+
const fonts = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
|
|
279
|
+
return fonts;
|
|
280
|
+
} catch (error) {
|
|
281
|
+
console.warn("Error loading font, will try to load online fonts", error);
|
|
282
|
+
const onlineFonts = "https://cdn.jsdelivr.net/npm/jimp-compact@0.16.1-2/fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt";
|
|
283
|
+
const fonts = await Jimp.loadFont(onlineFonts);
|
|
284
|
+
return fonts;
|
|
285
|
+
}
|
|
286
|
+
};
|
|
275
287
|
var createSvgOverlay = async (elements, imageWidth, imageHeight, boxPadding = 5) => {
|
|
276
288
|
const Jimp = await getJimp();
|
|
277
289
|
const image = new Jimp(imageWidth, imageHeight, 0);
|
|
@@ -316,7 +328,11 @@ var createSvgOverlay = async (elements, imageWidth, imageHeight, boxPadding = 5)
|
|
|
316
328
|
}
|
|
317
329
|
}
|
|
318
330
|
);
|
|
319
|
-
const
|
|
331
|
+
const indexId = element.indexId;
|
|
332
|
+
if (typeof indexId !== "number") {
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
const textWidth = indexId.toString().length * 8;
|
|
320
336
|
const textHeight = 12;
|
|
321
337
|
const rectWidth = textWidth + 5;
|
|
322
338
|
const rectHeight = textHeight + 4;
|
|
@@ -361,7 +377,7 @@ var createSvgOverlay = async (elements, imageWidth, imageHeight, boxPadding = 5)
|
|
|
361
377
|
}
|
|
362
378
|
);
|
|
363
379
|
try {
|
|
364
|
-
cachedFont = cachedFont || await
|
|
380
|
+
cachedFont = cachedFont || await loadFonts();
|
|
365
381
|
} catch (error) {
|
|
366
382
|
console.error("Error loading font", error);
|
|
367
383
|
}
|
|
@@ -370,7 +386,7 @@ var createSvgOverlay = async (elements, imageWidth, imageHeight, boxPadding = 5)
|
|
|
370
386
|
rectX,
|
|
371
387
|
rectY,
|
|
372
388
|
{
|
|
373
|
-
text:
|
|
389
|
+
text: indexId.toString(),
|
|
374
390
|
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
|
|
375
391
|
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
|
|
376
392
|
},
|
package/dist/types/img.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
2
|
import Jimp from 'jimp';
|
|
3
|
-
import { R as Rect } from './index-305e7a7e.js';
|
|
4
|
-
import
|
|
3
|
+
import { R as Rect, B as BaseElement } from './index-305e7a7e.js';
|
|
4
|
+
import './constants.js';
|
|
5
5
|
|
|
6
6
|
interface Size {
|
|
7
7
|
width: number;
|
|
@@ -114,20 +114,9 @@ declare function paddingToMatchBlockByBase64(imageBase64: string, blockSize?: nu
|
|
|
114
114
|
declare function cropByRect(imageBase64: string, rect: Rect, paddingImage: boolean): Promise<string>;
|
|
115
115
|
declare function jimpToBase64(image: Jimp): Promise<string>;
|
|
116
116
|
|
|
117
|
-
type ElementType = {
|
|
118
|
-
locator?: string;
|
|
119
|
-
rect: Rect;
|
|
120
|
-
center?: [number, number];
|
|
121
|
-
id?: string;
|
|
122
|
-
indexId: number;
|
|
123
|
-
attributes?: {
|
|
124
|
-
nodeType: NodeType;
|
|
125
|
-
[key: string]: string;
|
|
126
|
-
};
|
|
127
|
-
};
|
|
128
117
|
declare const compositeElementInfoImg: (options: {
|
|
129
118
|
inputImgBase64: string;
|
|
130
|
-
elementsPositionInfo: Array<
|
|
119
|
+
elementsPositionInfo: Array<BaseElement>;
|
|
131
120
|
size?: {
|
|
132
121
|
width: number;
|
|
133
122
|
height: number;
|
|
@@ -136,8 +125,8 @@ declare const compositeElementInfoImg: (options: {
|
|
|
136
125
|
}) => Promise<string>;
|
|
137
126
|
declare const processImageElementInfo: (options: {
|
|
138
127
|
inputImgBase64: string;
|
|
139
|
-
elementsPositionInfo: Array<
|
|
140
|
-
elementsPositionInfoWithoutText: Array<
|
|
128
|
+
elementsPositionInfo: Array<BaseElement>;
|
|
129
|
+
elementsPositionInfoWithoutText: Array<BaseElement>;
|
|
141
130
|
}) => Promise<{
|
|
142
131
|
compositeElementInfoImgBase64: string;
|
|
143
132
|
compositeElementInfoImgWithoutTextBase64: string;
|
package/package.json
CHANGED
package/src/img/box-select.ts
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
2
|
import type Jimp from 'jimp';
|
|
3
|
-
import type {
|
|
4
|
-
import type { Rect } from '../types';
|
|
3
|
+
import type { BaseElement } from '../types';
|
|
5
4
|
import getJimp from './get-jimp';
|
|
6
5
|
import { bufferFromBase64, imageInfoOfBase64 } from './index';
|
|
7
6
|
|
|
8
|
-
// Define picture path
|
|
9
|
-
type ElementType = {
|
|
10
|
-
locator?: string;
|
|
11
|
-
rect: Rect;
|
|
12
|
-
center?: [number, number];
|
|
13
|
-
id?: string;
|
|
14
|
-
indexId: number;
|
|
15
|
-
attributes?: {
|
|
16
|
-
nodeType: NodeType;
|
|
17
|
-
[key: string]: string;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
|
|
21
7
|
let cachedFont: any = null;
|
|
22
8
|
|
|
9
|
+
const loadFonts = async () => {
|
|
10
|
+
const Jimp = await getJimp();
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const fonts = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
|
|
14
|
+
return fonts;
|
|
15
|
+
} catch (error) {
|
|
16
|
+
console.warn('Error loading font, will try to load online fonts', error);
|
|
17
|
+
const onlineFonts =
|
|
18
|
+
'https://cdn.jsdelivr.net/npm/jimp-compact@0.16.1-2/fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt';
|
|
19
|
+
const fonts = await Jimp.loadFont(onlineFonts);
|
|
20
|
+
return fonts;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
23
24
|
const createSvgOverlay = async (
|
|
24
|
-
elements: Array<
|
|
25
|
+
elements: Array<BaseElement>,
|
|
25
26
|
imageWidth: number,
|
|
26
27
|
imageHeight: number,
|
|
27
28
|
boxPadding = 5,
|
|
@@ -78,7 +79,11 @@ const createSvgOverlay = async (
|
|
|
78
79
|
);
|
|
79
80
|
|
|
80
81
|
// Calculate text position
|
|
81
|
-
const
|
|
82
|
+
const indexId = element.indexId;
|
|
83
|
+
if (typeof indexId !== 'number') {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const textWidth = indexId.toString().length * 8;
|
|
82
87
|
const textHeight = 12;
|
|
83
88
|
const rectWidth = textWidth + 5;
|
|
84
89
|
const rectHeight = textHeight + 4;
|
|
@@ -164,7 +169,7 @@ const createSvgOverlay = async (
|
|
|
164
169
|
);
|
|
165
170
|
// Draw text (simplified, as Jimp doesn't have built-in text drawing)
|
|
166
171
|
try {
|
|
167
|
-
cachedFont = cachedFont || (await
|
|
172
|
+
cachedFont = cachedFont || (await loadFonts());
|
|
168
173
|
} catch (error) {
|
|
169
174
|
console.error('Error loading font', error);
|
|
170
175
|
}
|
|
@@ -173,7 +178,7 @@ const createSvgOverlay = async (
|
|
|
173
178
|
rectX,
|
|
174
179
|
rectY,
|
|
175
180
|
{
|
|
176
|
-
text:
|
|
181
|
+
text: indexId.toString(),
|
|
177
182
|
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
|
|
178
183
|
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE,
|
|
179
184
|
},
|
|
@@ -187,7 +192,7 @@ const createSvgOverlay = async (
|
|
|
187
192
|
|
|
188
193
|
export const compositeElementInfoImg = async (options: {
|
|
189
194
|
inputImgBase64: string;
|
|
190
|
-
elementsPositionInfo: Array<
|
|
195
|
+
elementsPositionInfo: Array<BaseElement>;
|
|
191
196
|
size?: { width: number; height: number };
|
|
192
197
|
annotationPadding?: number;
|
|
193
198
|
}) => {
|
|
@@ -255,8 +260,8 @@ export const compositeElementInfoImg = async (options: {
|
|
|
255
260
|
|
|
256
261
|
export const processImageElementInfo = async (options: {
|
|
257
262
|
inputImgBase64: string;
|
|
258
|
-
elementsPositionInfo: Array<
|
|
259
|
-
elementsPositionInfoWithoutText: Array<
|
|
263
|
+
elementsPositionInfo: Array<BaseElement>;
|
|
264
|
+
elementsPositionInfoWithoutText: Array<BaseElement>;
|
|
260
265
|
}) => {
|
|
261
266
|
// Get the size of the original image
|
|
262
267
|
const base64Image = options.inputImgBase64.split(';base64,').pop();
|