@pooder/kit 4.0.0 → 4.2.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/.test-dist/src/CanvasService.js +83 -0
- package/.test-dist/src/ViewportSystem.js +75 -0
- package/.test-dist/src/background.js +203 -0
- package/.test-dist/src/constraints.js +153 -0
- package/.test-dist/src/coordinate.js +74 -0
- package/.test-dist/src/dieline.js +758 -0
- package/.test-dist/src/feature.js +687 -0
- package/.test-dist/src/featureComplete.js +31 -0
- package/.test-dist/src/featureDraft.js +31 -0
- package/.test-dist/src/film.js +167 -0
- package/.test-dist/src/geometry.js +292 -0
- package/.test-dist/src/image.js +421 -0
- package/.test-dist/src/index.js +31 -0
- package/.test-dist/src/mirror.js +104 -0
- package/.test-dist/src/ruler.js +383 -0
- package/.test-dist/src/tracer.js +448 -0
- package/.test-dist/src/units.js +30 -0
- package/.test-dist/src/white-ink.js +310 -0
- package/.test-dist/tests/run.js +60 -0
- package/CHANGELOG.md +12 -0
- package/dist/index.d.mts +54 -5
- package/dist/index.d.ts +54 -5
- package/dist/index.js +584 -190
- package/dist/index.mjs +581 -189
- package/package.json +3 -2
- package/src/CanvasService.ts +7 -0
- package/src/ViewportSystem.ts +92 -0
- package/src/background.ts +230 -230
- package/src/constraints.ts +207 -0
- package/src/coordinate.ts +106 -106
- package/src/dieline.ts +194 -75
- package/src/feature.ts +239 -147
- package/src/featureComplete.ts +45 -0
- package/src/film.ts +194 -194
- package/src/geometry.ts +4 -0
- package/src/image.ts +512 -512
- package/src/index.ts +1 -0
- package/src/mirror.ts +128 -128
- package/src/ruler.ts +508 -500
- package/src/tracer.ts +570 -570
- package/src/units.ts +27 -0
- package/src/white-ink.ts +373 -373
- package/tests/run.ts +81 -0
- package/tsconfig.test.json +15 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
export interface ConstraintContext {
|
|
2
|
+
dielineWidth: number;
|
|
3
|
+
dielineHeight: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface ConstraintFeature {
|
|
7
|
+
id: string;
|
|
8
|
+
groupId?: string;
|
|
9
|
+
operation: "add" | "subtract";
|
|
10
|
+
shape: "rect" | "circle";
|
|
11
|
+
x: number;
|
|
12
|
+
y: number;
|
|
13
|
+
width?: number;
|
|
14
|
+
height?: number;
|
|
15
|
+
radius?: number;
|
|
16
|
+
placement?: "edge" | "internal";
|
|
17
|
+
constraints?: {
|
|
18
|
+
type: string;
|
|
19
|
+
params?: any;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type ConstraintHandler = (
|
|
24
|
+
x: number,
|
|
25
|
+
y: number,
|
|
26
|
+
feature: ConstraintFeature,
|
|
27
|
+
context: ConstraintContext
|
|
28
|
+
) => { x: number; y: number };
|
|
29
|
+
|
|
30
|
+
export class ConstraintRegistry {
|
|
31
|
+
private static handlers = new Map<string, ConstraintHandler>();
|
|
32
|
+
|
|
33
|
+
static register(type: string, handler: ConstraintHandler) {
|
|
34
|
+
this.handlers.set(type, handler);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static apply(
|
|
38
|
+
x: number,
|
|
39
|
+
y: number,
|
|
40
|
+
feature: ConstraintFeature,
|
|
41
|
+
context: ConstraintContext
|
|
42
|
+
): { x: number; y: number } {
|
|
43
|
+
if (!feature.constraints || !feature.constraints.type) {
|
|
44
|
+
return { x, y };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const handler = this.handlers.get(feature.constraints.type);
|
|
48
|
+
if (handler) {
|
|
49
|
+
return handler(x, y, feature, context);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return { x, y };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// --- Built-in Strategies ---
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Edge Constraint Strategy
|
|
60
|
+
* Snaps the feature to the nearest allowed edge.
|
|
61
|
+
* Params:
|
|
62
|
+
* - allowedEdges: ('top' | 'bottom' | 'left' | 'right')[] (default: all)
|
|
63
|
+
* - confine: boolean (default: false) - if true, keeps feature within edge length
|
|
64
|
+
* - offset: number (default: 0) - physical offset from edge (positive = inwards usually, but here 0 is edge)
|
|
65
|
+
* For simplicity, let's say offset is additive to the edge position.
|
|
66
|
+
* Top: 0 + offset
|
|
67
|
+
* Bottom: 1 - offset
|
|
68
|
+
* Left: 0 + offset
|
|
69
|
+
* Right: 1 - offset
|
|
70
|
+
*/
|
|
71
|
+
const edgeConstraint: ConstraintHandler = (x, y, feature, context) => {
|
|
72
|
+
const { dielineWidth, dielineHeight } = context;
|
|
73
|
+
const params = feature.constraints?.params || {};
|
|
74
|
+
const allowedEdges = params.allowedEdges || [
|
|
75
|
+
"top",
|
|
76
|
+
"bottom",
|
|
77
|
+
"left",
|
|
78
|
+
"right",
|
|
79
|
+
];
|
|
80
|
+
const confine = params.confine || false;
|
|
81
|
+
const offset = params.offset || 0;
|
|
82
|
+
|
|
83
|
+
// Calculate physical distances to allowed edges
|
|
84
|
+
const distances: { edge: string; dist: number }[] = [];
|
|
85
|
+
|
|
86
|
+
if (allowedEdges.includes("top"))
|
|
87
|
+
distances.push({ edge: "top", dist: y * dielineHeight });
|
|
88
|
+
if (allowedEdges.includes("bottom"))
|
|
89
|
+
distances.push({ edge: "bottom", dist: (1 - y) * dielineHeight });
|
|
90
|
+
if (allowedEdges.includes("left"))
|
|
91
|
+
distances.push({ edge: "left", dist: x * dielineWidth });
|
|
92
|
+
if (allowedEdges.includes("right"))
|
|
93
|
+
distances.push({ edge: "right", dist: (1 - x) * dielineWidth });
|
|
94
|
+
|
|
95
|
+
if (distances.length === 0) return { x, y };
|
|
96
|
+
|
|
97
|
+
// Find nearest
|
|
98
|
+
distances.sort((a, b) => a.dist - b.dist);
|
|
99
|
+
const nearest = distances[0].edge;
|
|
100
|
+
|
|
101
|
+
let newX = x;
|
|
102
|
+
let newY = y;
|
|
103
|
+
const fw = feature.width || 0;
|
|
104
|
+
const fh = feature.height || 0;
|
|
105
|
+
|
|
106
|
+
// Snap to edge
|
|
107
|
+
switch (nearest) {
|
|
108
|
+
case "top":
|
|
109
|
+
newY = 0 + offset / dielineHeight;
|
|
110
|
+
if (confine) {
|
|
111
|
+
const minX = (fw / 2) / dielineWidth;
|
|
112
|
+
const maxX = 1 - minX;
|
|
113
|
+
newX = Math.max(minX, Math.min(newX, maxX));
|
|
114
|
+
}
|
|
115
|
+
break;
|
|
116
|
+
case "bottom":
|
|
117
|
+
newY = 1 - offset / dielineHeight;
|
|
118
|
+
if (confine) {
|
|
119
|
+
const minX = (fw / 2) / dielineWidth;
|
|
120
|
+
const maxX = 1 - minX;
|
|
121
|
+
newX = Math.max(minX, Math.min(newX, maxX));
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
124
|
+
case "left":
|
|
125
|
+
newX = 0 + offset / dielineWidth;
|
|
126
|
+
if (confine) {
|
|
127
|
+
const minY = (fh / 2) / dielineHeight;
|
|
128
|
+
const maxY = 1 - minY;
|
|
129
|
+
newY = Math.max(minY, Math.min(newY, maxY));
|
|
130
|
+
}
|
|
131
|
+
break;
|
|
132
|
+
case "right":
|
|
133
|
+
newX = 1 - offset / dielineWidth;
|
|
134
|
+
if (confine) {
|
|
135
|
+
const minY = (fh / 2) / dielineHeight;
|
|
136
|
+
const maxY = 1 - minY;
|
|
137
|
+
newY = Math.max(minY, Math.min(newY, maxY));
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return { x: newX, y: newY };
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Internal Constraint Strategy
|
|
147
|
+
* Keeps the feature strictly inside the dieline bounds with optional margin.
|
|
148
|
+
* Params:
|
|
149
|
+
* - margin: number (default: 0) - physical margin
|
|
150
|
+
*/
|
|
151
|
+
const internalConstraint: ConstraintHandler = (x, y, feature, context) => {
|
|
152
|
+
const { dielineWidth, dielineHeight } = context;
|
|
153
|
+
const params = feature.constraints?.params || {};
|
|
154
|
+
const margin = params.margin || 0;
|
|
155
|
+
const fw = feature.width || 0;
|
|
156
|
+
const fh = feature.height || 0;
|
|
157
|
+
|
|
158
|
+
const minX = (margin + fw / 2) / dielineWidth;
|
|
159
|
+
const maxX = 1 - (margin + fw / 2) / dielineWidth;
|
|
160
|
+
|
|
161
|
+
const minY = (margin + fh / 2) / dielineHeight;
|
|
162
|
+
const maxY = 1 - (margin + fh / 2) / dielineHeight;
|
|
163
|
+
|
|
164
|
+
// Handle case where feature is larger than container
|
|
165
|
+
const clampedX = minX > maxX ? 0.5 : Math.max(minX, Math.min(x, maxX));
|
|
166
|
+
const clampedY = minY > maxY ? 0.5 : Math.max(minY, Math.min(y, maxY));
|
|
167
|
+
|
|
168
|
+
return { x: clampedX, y: clampedY };
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Bottom Tangent Strategy (stand protrusion)
|
|
173
|
+
* Forces a feature to be tangent to the dieline bottom edge from outside (below).
|
|
174
|
+
* Params:
|
|
175
|
+
* - gap: number (mm, default 0) extra clearance between dieline and protrusion
|
|
176
|
+
* - confineX: boolean (default true) keep feature within left/right bounds
|
|
177
|
+
*/
|
|
178
|
+
const tangentBottomConstraint: ConstraintHandler = (x, y, feature, context) => {
|
|
179
|
+
const { dielineWidth, dielineHeight } = context;
|
|
180
|
+
const params = feature.constraints?.params || {};
|
|
181
|
+
const gap = params.gap || 0;
|
|
182
|
+
const confineX = params.confineX !== false;
|
|
183
|
+
|
|
184
|
+
const extentY =
|
|
185
|
+
feature.shape === "circle"
|
|
186
|
+
? feature.radius || 0
|
|
187
|
+
: (feature.height || 0) / 2;
|
|
188
|
+
const newY = 1 + (extentY + gap) / dielineHeight;
|
|
189
|
+
|
|
190
|
+
let newX = x;
|
|
191
|
+
if (confineX) {
|
|
192
|
+
const extentX =
|
|
193
|
+
feature.shape === "circle"
|
|
194
|
+
? feature.radius || 0
|
|
195
|
+
: (feature.width || 0) / 2;
|
|
196
|
+
const minX = extentX / dielineWidth;
|
|
197
|
+
const maxX = 1 - extentX / dielineWidth;
|
|
198
|
+
newX = minX > maxX ? 0.5 : Math.max(minX, Math.min(newX, maxX));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return { x: newX, y: newY };
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
// Register built-ins
|
|
205
|
+
ConstraintRegistry.register("edge", edgeConstraint);
|
|
206
|
+
ConstraintRegistry.register("internal", internalConstraint);
|
|
207
|
+
ConstraintRegistry.register("tangent-bottom", tangentBottomConstraint);
|
package/src/coordinate.ts
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
export interface Point {
|
|
2
|
-
x: number;
|
|
3
|
-
y: number;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export interface Size {
|
|
7
|
-
width: number;
|
|
8
|
-
height: number;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type Unit = "px" | "mm" | "cm" | "in";
|
|
12
|
-
|
|
13
|
-
export interface Layout {
|
|
14
|
-
scale: number;
|
|
15
|
-
offsetX: number;
|
|
16
|
-
offsetY: number;
|
|
17
|
-
width: number;
|
|
18
|
-
height: number;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class Coordinate {
|
|
22
|
-
/**
|
|
23
|
-
* Calculate layout to fit content within container while preserving aspect ratio.
|
|
24
|
-
*/
|
|
25
|
-
static calculateLayout(
|
|
26
|
-
container: Size,
|
|
27
|
-
content: Size,
|
|
28
|
-
padding: number = 0,
|
|
29
|
-
): Layout {
|
|
30
|
-
const availableWidth = Math.max(0, container.width - padding * 2);
|
|
31
|
-
const availableHeight = Math.max(0, container.height - padding * 2);
|
|
32
|
-
|
|
33
|
-
if (content.width === 0 || content.height === 0) {
|
|
34
|
-
return { scale: 1, offsetX: 0, offsetY: 0, width: 0, height: 0 };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const scaleX = availableWidth / content.width;
|
|
38
|
-
const scaleY = availableHeight / content.height;
|
|
39
|
-
const scale = Math.min(scaleX, scaleY);
|
|
40
|
-
|
|
41
|
-
const width = content.width * scale;
|
|
42
|
-
const height = content.height * scale;
|
|
43
|
-
|
|
44
|
-
const offsetX = (container.width - width) / 2;
|
|
45
|
-
const offsetY = (container.height - height) / 2;
|
|
46
|
-
|
|
47
|
-
return { scale, offsetX, offsetY, width, height };
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Convert an absolute value to a normalized value (0-1).
|
|
52
|
-
* @param value Absolute value (e.g., pixels)
|
|
53
|
-
* @param total Total dimension size (e.g., canvas width)
|
|
54
|
-
*/
|
|
55
|
-
static toNormalized(value: number, total: number): number {
|
|
56
|
-
return total === 0 ? 0 : value / total;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Convert a normalized value (0-1) to an absolute value.
|
|
61
|
-
* @param normalized Normalized value (0-1)
|
|
62
|
-
* @param total Total dimension size (e.g., canvas width)
|
|
63
|
-
*/
|
|
64
|
-
static toAbsolute(normalized: number, total: number): number {
|
|
65
|
-
return normalized * total;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Normalize a point's coordinates.
|
|
70
|
-
*/
|
|
71
|
-
static normalizePoint(point: Point, size: Size): Point {
|
|
72
|
-
return {
|
|
73
|
-
x: this.toNormalized(point.x, size.width),
|
|
74
|
-
y: this.toNormalized(point.y, size.height),
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Denormalize a point's coordinates to absolute pixels.
|
|
80
|
-
*/
|
|
81
|
-
static denormalizePoint(point: Point, size: Size): Point {
|
|
82
|
-
return {
|
|
83
|
-
x: this.toAbsolute(point.x, size.width),
|
|
84
|
-
y: this.toAbsolute(point.y, size.height),
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
static convertUnit(value: number, from: Unit, to: Unit): number {
|
|
89
|
-
if (from === to) return value;
|
|
90
|
-
|
|
91
|
-
// Base unit: mm
|
|
92
|
-
const toMM: Record<Unit, number> = {
|
|
93
|
-
px: 0.264583, // 1px = 0.264583mm (96 DPI)
|
|
94
|
-
mm: 1,
|
|
95
|
-
cm: 10,
|
|
96
|
-
in: 25.4
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const mmValue = value * (from === 'px' ? toMM.px : toMM[from] || 1);
|
|
100
|
-
|
|
101
|
-
if (to === 'px') {
|
|
102
|
-
return mmValue / toMM.px;
|
|
103
|
-
}
|
|
104
|
-
return mmValue / (toMM[to] || 1);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
1
|
+
export interface Point {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface Size {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type Unit = "px" | "mm" | "cm" | "in";
|
|
12
|
+
|
|
13
|
+
export interface Layout {
|
|
14
|
+
scale: number;
|
|
15
|
+
offsetX: number;
|
|
16
|
+
offsetY: number;
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class Coordinate {
|
|
22
|
+
/**
|
|
23
|
+
* Calculate layout to fit content within container while preserving aspect ratio.
|
|
24
|
+
*/
|
|
25
|
+
static calculateLayout(
|
|
26
|
+
container: Size,
|
|
27
|
+
content: Size,
|
|
28
|
+
padding: number = 0,
|
|
29
|
+
): Layout {
|
|
30
|
+
const availableWidth = Math.max(0, container.width - padding * 2);
|
|
31
|
+
const availableHeight = Math.max(0, container.height - padding * 2);
|
|
32
|
+
|
|
33
|
+
if (content.width === 0 || content.height === 0) {
|
|
34
|
+
return { scale: 1, offsetX: 0, offsetY: 0, width: 0, height: 0 };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const scaleX = availableWidth / content.width;
|
|
38
|
+
const scaleY = availableHeight / content.height;
|
|
39
|
+
const scale = Math.min(scaleX, scaleY);
|
|
40
|
+
|
|
41
|
+
const width = content.width * scale;
|
|
42
|
+
const height = content.height * scale;
|
|
43
|
+
|
|
44
|
+
const offsetX = (container.width - width) / 2;
|
|
45
|
+
const offsetY = (container.height - height) / 2;
|
|
46
|
+
|
|
47
|
+
return { scale, offsetX, offsetY, width, height };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Convert an absolute value to a normalized value (0-1).
|
|
52
|
+
* @param value Absolute value (e.g., pixels)
|
|
53
|
+
* @param total Total dimension size (e.g., canvas width)
|
|
54
|
+
*/
|
|
55
|
+
static toNormalized(value: number, total: number): number {
|
|
56
|
+
return total === 0 ? 0 : value / total;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Convert a normalized value (0-1) to an absolute value.
|
|
61
|
+
* @param normalized Normalized value (0-1)
|
|
62
|
+
* @param total Total dimension size (e.g., canvas width)
|
|
63
|
+
*/
|
|
64
|
+
static toAbsolute(normalized: number, total: number): number {
|
|
65
|
+
return normalized * total;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Normalize a point's coordinates.
|
|
70
|
+
*/
|
|
71
|
+
static normalizePoint(point: Point, size: Size): Point {
|
|
72
|
+
return {
|
|
73
|
+
x: this.toNormalized(point.x, size.width),
|
|
74
|
+
y: this.toNormalized(point.y, size.height),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Denormalize a point's coordinates to absolute pixels.
|
|
80
|
+
*/
|
|
81
|
+
static denormalizePoint(point: Point, size: Size): Point {
|
|
82
|
+
return {
|
|
83
|
+
x: this.toAbsolute(point.x, size.width),
|
|
84
|
+
y: this.toAbsolute(point.y, size.height),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static convertUnit(value: number, from: Unit, to: Unit): number {
|
|
89
|
+
if (from === to) return value;
|
|
90
|
+
|
|
91
|
+
// Base unit: mm
|
|
92
|
+
const toMM: Record<Unit, number> = {
|
|
93
|
+
px: 0.264583, // 1px = 0.264583mm (96 DPI)
|
|
94
|
+
mm: 1,
|
|
95
|
+
cm: 10,
|
|
96
|
+
in: 25.4
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const mmValue = value * (from === 'px' ? toMM.px : toMM[from] || 1);
|
|
100
|
+
|
|
101
|
+
if (to === 'px') {
|
|
102
|
+
return mmValue / toMM.px;
|
|
103
|
+
}
|
|
104
|
+
return mmValue / (toMM[to] || 1);
|
|
105
|
+
}
|
|
106
|
+
}
|