@mapka/sprites-generate 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 +17 -0
- package/README.md +25 -0
- package/lib/.buildInfo.json +1 -0
- package/lib/__bench__/generate.bench.d.ts +2 -0
- package/lib/__bench__/generate.bench.d.ts.map +1 -0
- package/lib/__bench__/generate.bench.js +27 -0
- package/lib/__bench__/generate.bench.js.map +1 -0
- package/lib/generate.d.ts +48 -0
- package/lib/generate.d.ts.map +1 -0
- package/lib/generate.js +227 -0
- package/lib/generate.js.map +1 -0
- package/lib/image.d.ts +16 -0
- package/lib/image.d.ts.map +1 -0
- package/lib/image.js +53 -0
- package/lib/image.js.map +1 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +5 -0
- package/lib/index.js.map +1 -0
- package/lib/metadata.d.ts +14 -0
- package/lib/metadata.d.ts.map +1 -0
- package/lib/metadata.js +175 -0
- package/lib/metadata.js.map +1 -0
- package/lib/types.d.ts +37 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +2 -0
- package/lib/types.js.map +1 -0
- package/lib/validate.d.ts +20 -0
- package/lib/validate.d.ts.map +1 -0
- package/lib/validate.js +65 -0
- package/lib/validate.js.map +1 -0
- package/package.json +30 -0
- package/src/__bench__/generate.bench.ts +30 -0
- package/src/declarations.d.ts +71 -0
- package/src/generate.ts +336 -0
- package/src/image.ts +81 -0
- package/src/index.ts +5 -0
- package/src/metadata.js +193 -0
- package/src/types.ts +40 -0
- package/src/validate.ts +92 -0
package/src/validate.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: nature of this validate function */
|
|
2
|
+
|
|
3
|
+
export interface Metadata {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
placeholder?: [number, number, number, number];
|
|
7
|
+
content?: [number, number, number, number];
|
|
8
|
+
stretchX?: [number, number][];
|
|
9
|
+
stretchY?: [number, number][];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface Image {
|
|
13
|
+
width?: number;
|
|
14
|
+
height?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Validates metadata that is parsed from an SVG metadata
|
|
19
|
+
*
|
|
20
|
+
* @return {Error} if metadata is invalid
|
|
21
|
+
*/
|
|
22
|
+
export function validateMetadata(img?: any, metadata?: any) {
|
|
23
|
+
if (!img || typeof img !== 'object') {
|
|
24
|
+
return new Error('image is invalid');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!metadata || typeof metadata !== 'object') {
|
|
28
|
+
return new Error('image has invalid metadata');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (typeof img.width !== 'number' || img.width <= 0) {
|
|
32
|
+
return new Error('image has invalid width');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (typeof img.height !== 'number' || img.height <= 0) {
|
|
36
|
+
return new Error('image has invalid height');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
if ('content' in metadata) {
|
|
41
|
+
const content = metadata.content;
|
|
42
|
+
if (!Array.isArray(content) || content.length !== 4 ||
|
|
43
|
+
typeof content[0] !== 'number' || typeof content[1] !== 'number' ||
|
|
44
|
+
typeof content[2] !== 'number' || typeof content[3] !== 'number') {
|
|
45
|
+
return new Error('image content area must be an array of 4 numbers');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (content[0] >= content[2] || content[1] >= content[3]) {
|
|
49
|
+
return new Error('image content area must be positive');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (content[0] < 0 || content[2] > img.width ||
|
|
53
|
+
content[1] < 0 || content[3] > img.height) {
|
|
54
|
+
return new Error('image content area must be within image bounds');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (const key of ['stretchX', 'stretchY'] as const) {
|
|
59
|
+
if (key in metadata) {
|
|
60
|
+
const stretches = metadata[key];
|
|
61
|
+
if (!Array.isArray(stretches)) {
|
|
62
|
+
return new Error(`image ${key} zones must be an array`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
for (const zone of stretches) {
|
|
66
|
+
if (!Array.isArray(zone) || zone.length !== 2 ||
|
|
67
|
+
typeof zone[0] !== 'number' || typeof zone[1] !== 'number') {
|
|
68
|
+
return new Error(`image ${key} zone must consist of two numbers`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (zone[0] >= zone[1]) {
|
|
72
|
+
return new Error(`image ${key} zone may not be zero-size`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (zone[0] < 0 || zone[1] > img.width) {
|
|
76
|
+
return new Error(`image ${key} zone must be within image bounds`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for (let i = 1; i < stretches.length; i++) {
|
|
81
|
+
// Make sure that the previous stretch's end coordinate is
|
|
82
|
+
// smaller than this stretch's begin coordinate. Expects that
|
|
83
|
+
// stretch zones are sorted ascending by their first coordinate.
|
|
84
|
+
if (stretches[i][0] <= stretches[i - 1][1]) {
|
|
85
|
+
return new Error(`image ${key} zones may not overlap`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return null;
|
|
92
|
+
}
|