@pooder/kit 5.4.0 → 6.0.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.
Files changed (69) hide show
  1. package/.test-dist/src/coordinate.js +74 -0
  2. package/.test-dist/src/extensions/background.js +547 -0
  3. package/.test-dist/src/extensions/bridgeSelection.js +20 -0
  4. package/.test-dist/src/extensions/constraints.js +237 -0
  5. package/.test-dist/src/extensions/dieline.js +931 -0
  6. package/.test-dist/src/extensions/dielineShape.js +66 -0
  7. package/.test-dist/src/extensions/edgeScale.js +12 -0
  8. package/.test-dist/src/extensions/feature.js +910 -0
  9. package/.test-dist/src/extensions/featureComplete.js +32 -0
  10. package/.test-dist/src/extensions/film.js +226 -0
  11. package/.test-dist/src/extensions/geometry.js +609 -0
  12. package/.test-dist/src/extensions/image.js +1613 -0
  13. package/.test-dist/src/extensions/index.js +28 -0
  14. package/.test-dist/src/extensions/maskOps.js +334 -0
  15. package/.test-dist/src/extensions/mirror.js +104 -0
  16. package/.test-dist/src/extensions/ruler.js +442 -0
  17. package/.test-dist/src/extensions/sceneLayout.js +96 -0
  18. package/.test-dist/src/extensions/sceneLayoutModel.js +202 -0
  19. package/.test-dist/src/extensions/sceneVisibility.js +55 -0
  20. package/.test-dist/src/extensions/size.js +331 -0
  21. package/.test-dist/src/extensions/tracer.js +709 -0
  22. package/.test-dist/src/extensions/white-ink.js +1200 -0
  23. package/.test-dist/src/extensions/wrappedOffsets.js +33 -0
  24. package/.test-dist/src/index.js +18 -0
  25. package/.test-dist/src/services/CanvasService.js +1011 -0
  26. package/.test-dist/src/services/ViewportSystem.js +76 -0
  27. package/.test-dist/src/services/index.js +25 -0
  28. package/.test-dist/src/services/renderSpec.js +2 -0
  29. package/.test-dist/src/services/visibility.js +54 -0
  30. package/.test-dist/src/units.js +30 -0
  31. package/.test-dist/tests/run.js +148 -0
  32. package/CHANGELOG.md +6 -0
  33. package/dist/index.d.mts +150 -62
  34. package/dist/index.d.ts +150 -62
  35. package/dist/index.js +2219 -1714
  36. package/dist/index.mjs +2226 -1718
  37. package/package.json +1 -1
  38. package/src/coordinate.ts +106 -106
  39. package/src/extensions/background.ts +716 -323
  40. package/src/extensions/bridgeSelection.ts +17 -17
  41. package/src/extensions/constraints.ts +322 -322
  42. package/src/extensions/dieline.ts +1169 -1149
  43. package/src/extensions/dielineShape.ts +109 -109
  44. package/src/extensions/edgeScale.ts +19 -19
  45. package/src/extensions/feature.ts +1140 -1137
  46. package/src/extensions/featureComplete.ts +46 -46
  47. package/src/extensions/film.ts +270 -266
  48. package/src/extensions/geometry.ts +851 -885
  49. package/src/extensions/image.ts +2007 -2054
  50. package/src/extensions/index.ts +10 -11
  51. package/src/extensions/maskOps.ts +283 -283
  52. package/src/extensions/mirror.ts +128 -128
  53. package/src/extensions/ruler.ts +664 -654
  54. package/src/extensions/sceneLayout.ts +140 -140
  55. package/src/extensions/sceneLayoutModel.ts +364 -364
  56. package/src/extensions/size.ts +389 -389
  57. package/src/extensions/tracer.ts +1019 -1019
  58. package/src/extensions/white-ink.ts +1508 -1575
  59. package/src/extensions/wrappedOffsets.ts +33 -33
  60. package/src/index.ts +2 -2
  61. package/src/services/CanvasService.ts +1286 -832
  62. package/src/services/ViewportSystem.ts +95 -95
  63. package/src/services/index.ts +4 -3
  64. package/src/services/renderSpec.ts +83 -53
  65. package/src/services/visibility.ts +78 -0
  66. package/src/units.ts +27 -27
  67. package/tests/run.ts +253 -118
  68. package/tsconfig.test.json +15 -15
  69. package/src/extensions/sceneVisibility.ts +0 -64
@@ -1,322 +1,322 @@
1
- import { DielineFeature, getNearestPointOnDieline, getLowestPointOnDieline } from "./geometry";
2
-
3
- export interface ConstraintContext {
4
- dielineWidth: number;
5
- dielineHeight: number;
6
- // Context may need access to geometry functions or the geometry itself
7
- // For now, getNearestPointOnDieline creates its own paper scope, but ideally we pass a simplified geometry representation
8
- geometry?: any;
9
- }
10
-
11
- export interface ConstraintFeature extends DielineFeature {
12
- constraints?: Array<{
13
- type: string;
14
- params?: any;
15
- validateOnly?: boolean;
16
- }>;
17
- }
18
-
19
- export type ConstraintHandler = (
20
- x: number,
21
- y: number,
22
- feature: ConstraintFeature,
23
- context: ConstraintContext,
24
- params?: any
25
- ) => { x: number; y: number };
26
-
27
- export interface ConstraintConfig {
28
- type: string;
29
- params?: any;
30
- validateOnly?: boolean;
31
- }
32
-
33
- export class ConstraintRegistry {
34
- private static handlers = new Map<string, ConstraintHandler>();
35
-
36
- static register(type: string, handler: ConstraintHandler) {
37
- this.handlers.set(type, handler);
38
- }
39
-
40
- static apply(
41
- x: number,
42
- y: number,
43
- feature: ConstraintFeature,
44
- context: ConstraintContext,
45
- constraints?: ConstraintConfig[] // Optional override, defaults to feature.constraints
46
- ): { x: number; y: number } {
47
- const list = constraints || feature.constraints;
48
- if (!list || list.length === 0) {
49
- return { x, y };
50
- }
51
-
52
- let currentX = x;
53
- let currentY = y;
54
-
55
- for (const constraint of list) {
56
- const handler = this.handlers.get(constraint.type);
57
- if (handler) {
58
- const result = handler(currentX, currentY, feature, context, constraint.params || {});
59
- currentX = result.x;
60
- currentY = result.y;
61
- }
62
- }
63
-
64
- return { x: currentX, y: currentY };
65
- }
66
- }
67
-
68
- // --- Built-in Strategies ---
69
-
70
- /**
71
- * Path Constraint Strategy (formerly placement='edge')
72
- * Snaps the feature to the nearest point on the Dieline Path.
73
- */
74
- const pathConstraint: ConstraintHandler = (x, y, feature, context, params) => {
75
- // We need to denormalize, find nearest, then normalize back
76
- // This is expensive but accurate.
77
- const { dielineWidth, dielineHeight, geometry } = context;
78
- if (!geometry) return { x, y }; // Cannot snap without geometry
79
-
80
- // Geometry is centered at (cx, cy)
81
- // x, y are normalized (0-1) relative to bounding box
82
- const minX = geometry.x - geometry.width / 2;
83
- const minY = geometry.y - geometry.height / 2;
84
-
85
- const absX = minX + x * geometry.width;
86
- const absY = minY + y * geometry.height;
87
-
88
- // Use geometry helper
89
- // Note: getNearestPointOnDieline creates a fresh paper scope each time.
90
- // Optimization: geometry object passed in context could be a reusable paper path?
91
- // For now, keep it simple as per existing logic.
92
- const nearest = getNearestPointOnDieline(
93
- { x: absX, y: absY },
94
- geometry
95
- );
96
-
97
- let finalX = nearest.x;
98
- let finalY = nearest.y;
99
-
100
- // Only allow vertical offset if explicit offset limits are provided
101
- // Otherwise, we snap strictly to the path (offset = 0)
102
- const hasOffsetParams = params.minOffset !== undefined || params.maxOffset !== undefined;
103
-
104
- if (hasOffsetParams && nearest.normal) {
105
- // Project the cursor vector onto the normal vector
106
- // This ensures the feature stays on the "normal line" of the nearest path point
107
- const dx = absX - nearest.x;
108
- const dy = absY - nearest.y;
109
-
110
- const nx = nearest.normal.x;
111
- const ny = nearest.normal.y;
112
-
113
- // Dot product to get scalar projection
114
- const dist = dx * nx + dy * ny;
115
-
116
- // Limit the offset
117
- // geometry.width is in pixels, dielineWidth is in physical units (e.g. mm)
118
- // We assume dielineWidth corresponds to geometry.width
119
- const scale = dielineWidth > 0 ? geometry.width / dielineWidth : 1;
120
-
121
- // If one is provided but the other is not, default the other to 0.
122
- // If neither is provided (shouldn't happen due to hasOffsetParams check), default to 0.
123
- const rawMin = params.minOffset !== undefined ? params.minOffset : 0;
124
- const rawMax = params.maxOffset !== undefined ? params.maxOffset : 0;
125
-
126
- // However, if we want to allow one-sided infinity, user must explicitly provide Infinity?
127
- // Wait, user requirement: "If only one is passed, the other defaults to 0."
128
- // This implies:
129
- // { minOffset: -5 } -> maxOffset = 0 (range: -5 to 0)
130
- // { maxOffset: 5 } -> minOffset = 0 (range: 0 to 5)
131
- // { minOffset: -5, maxOffset: 5 } -> (range: -5 to 5)
132
-
133
- const minOffset = rawMin * scale;
134
- const maxOffset = rawMax * scale;
135
-
136
- const clampedDist = Math.max(minOffset, Math.min(dist, maxOffset));
137
-
138
- finalX = nearest.x + nx * clampedDist;
139
- finalY = nearest.y + ny * clampedDist;
140
- }
141
-
142
- // Re-normalize
143
- const nx = geometry.width > 0 ? (finalX - minX) / geometry.width : 0.5;
144
- const ny = geometry.height > 0 ? (finalY - minY) / geometry.height : 0.5;
145
-
146
- return { x: nx, y: ny };
147
- };
148
-
149
- /**
150
- * Edge Constraint Strategy (Box Edge)
151
- * Snaps the feature to the nearest allowed edge of the BOUNDING BOX.
152
- */
153
- const edgeConstraint: ConstraintHandler = (x, y, feature, context, params) => {
154
- const { dielineWidth, dielineHeight } = context;
155
- const allowedEdges = params.allowedEdges || [
156
- "top",
157
- "bottom",
158
- "left",
159
- "right",
160
- ];
161
- const confine = params.confine || false;
162
- const offset = params.offset || 0;
163
-
164
- // Calculate physical distances to allowed edges
165
- const distances: { edge: string; dist: number }[] = [];
166
-
167
- if (allowedEdges.includes("top"))
168
- distances.push({ edge: "top", dist: y * dielineHeight });
169
- if (allowedEdges.includes("bottom"))
170
- distances.push({ edge: "bottom", dist: (1 - y) * dielineHeight });
171
- if (allowedEdges.includes("left"))
172
- distances.push({ edge: "left", dist: x * dielineWidth });
173
- if (allowedEdges.includes("right"))
174
- distances.push({ edge: "right", dist: (1 - x) * dielineWidth });
175
-
176
- if (distances.length === 0) return { x, y };
177
-
178
- // Find nearest
179
- distances.sort((a, b) => a.dist - b.dist);
180
- const nearest = distances[0].edge;
181
-
182
- let newX = x;
183
- let newY = y;
184
- const fw = feature.width || 0;
185
- const fh = feature.height || 0;
186
-
187
- // Snap to edge
188
- switch (nearest) {
189
- case "top":
190
- newY = 0 + offset / dielineHeight;
191
- if (confine) {
192
- const minX = (fw / 2) / dielineWidth;
193
- const maxX = 1 - minX;
194
- newX = Math.max(minX, Math.min(newX, maxX));
195
- }
196
- break;
197
- case "bottom":
198
- newY = 1 - offset / dielineHeight;
199
- if (confine) {
200
- const minX = (fw / 2) / dielineWidth;
201
- const maxX = 1 - minX;
202
- newX = Math.max(minX, Math.min(newX, maxX));
203
- }
204
- break;
205
- case "left":
206
- newX = 0 + offset / dielineWidth;
207
- if (confine) {
208
- const minY = (fh / 2) / dielineHeight;
209
- const maxY = 1 - minY;
210
- newY = Math.max(minY, Math.min(newY, maxY));
211
- }
212
- break;
213
- case "right":
214
- newX = 1 - offset / dielineWidth;
215
- if (confine) {
216
- const minY = (fh / 2) / dielineHeight;
217
- const maxY = 1 - minY;
218
- newY = Math.max(minY, Math.min(newY, maxY));
219
- }
220
- break;
221
- }
222
-
223
- return { x: newX, y: newY };
224
- };
225
-
226
- /**
227
- * Internal Constraint Strategy
228
- * Keeps the feature strictly inside the dieline bounds with optional margin.
229
- */
230
- const internalConstraint: ConstraintHandler = (x, y, feature, context, params) => {
231
- const { dielineWidth, dielineHeight } = context;
232
- const margin = params.margin || 0;
233
- const fw = feature.width || 0;
234
- const fh = feature.height || 0;
235
-
236
- const minX = (margin + fw / 2) / dielineWidth;
237
- const maxX = 1 - (margin + fw / 2) / dielineWidth;
238
-
239
- const minY = (margin + fh / 2) / dielineHeight;
240
- const maxY = 1 - (margin + fh / 2) / dielineHeight;
241
-
242
- // Handle case where feature is larger than container
243
- const clampedX = minX > maxX ? 0.5 : Math.max(minX, Math.min(x, maxX));
244
- const clampedY = minY > maxY ? 0.5 : Math.max(minY, Math.min(y, maxY));
245
-
246
- return { x: clampedX, y: clampedY };
247
- };
248
-
249
- /**
250
- * Bottom Tangent Strategy (stand protrusion)
251
- * Forces a feature to be tangent to the dieline bottom edge from outside (below).
252
- */
253
- const tangentBottomConstraint: ConstraintHandler = (x, y, feature, context, params) => {
254
- const { dielineWidth, dielineHeight } = context;
255
- const gap = params.gap || 0;
256
- const confineX = params.confineX !== false;
257
-
258
- const extentY =
259
- feature.shape === "circle"
260
- ? feature.radius || 0
261
- : (feature.height || 0) / 2;
262
- const newY = 1 + (extentY + gap) / dielineHeight;
263
-
264
- let newX = x;
265
- if (confineX) {
266
- const extentX =
267
- feature.shape === "circle"
268
- ? feature.radius || 0
269
- : (feature.width || 0) / 2;
270
- const minX = extentX / dielineWidth;
271
- const maxX = 1 - extentX / dielineWidth;
272
- newX = minX > maxX ? 0.5 : Math.max(minX, Math.min(newX, maxX));
273
- }
274
-
275
- return { x: newX, y: newY };
276
- };
277
-
278
- /**
279
- * Lowest Tangent Strategy (Lowest Point Lock)
280
- * Finds the lowest point of the Dieline geometry and locks the feature's Y to that level.
281
- * Allows horizontal movement (X).
282
- */
283
- const lowestTangentConstraint: ConstraintHandler = (x, y, feature, context, params) => {
284
- const { dielineWidth, dielineHeight, geometry } = context;
285
- if (!geometry) return { x, y };
286
-
287
- const lowest = getLowestPointOnDieline(geometry);
288
-
289
- // Calculate normalized Y of the lowest point
290
- const minY = geometry.y - geometry.height / 2;
291
- const normY = (lowest.y - minY) / geometry.height;
292
-
293
- const gap = params.gap || 0;
294
- const confineX = params.confineX !== false;
295
-
296
- const extentY =
297
- feature.shape === "circle"
298
- ? feature.radius || 0
299
- : (feature.height || 0) / 2;
300
-
301
- const newY = normY + (extentY + gap) / dielineHeight;
302
-
303
- let newX = x;
304
- if (confineX) {
305
- const extentX =
306
- feature.shape === "circle"
307
- ? feature.radius || 0
308
- : (feature.width || 0) / 2;
309
- const minX = extentX / dielineWidth;
310
- const maxX = 1 - extentX / dielineWidth;
311
- newX = minX > maxX ? 0.5 : Math.max(minX, Math.min(newX, maxX));
312
- }
313
-
314
- return { x: newX, y: newY };
315
- };
316
-
317
- // Register built-ins
318
- ConstraintRegistry.register("path", pathConstraint);
319
- ConstraintRegistry.register("edge", edgeConstraint);
320
- ConstraintRegistry.register("internal", internalConstraint);
321
- ConstraintRegistry.register("tangent-bottom", tangentBottomConstraint);
322
- ConstraintRegistry.register("lowest-tangent", lowestTangentConstraint);
1
+ import { DielineFeature, getNearestPointOnDieline, getLowestPointOnDieline } from "./geometry";
2
+
3
+ export interface ConstraintContext {
4
+ dielineWidth: number;
5
+ dielineHeight: number;
6
+ // Context may need access to geometry functions or the geometry itself
7
+ // For now, getNearestPointOnDieline creates its own paper scope, but ideally we pass a simplified geometry representation
8
+ geometry?: any;
9
+ }
10
+
11
+ export interface ConstraintFeature extends DielineFeature {
12
+ constraints?: Array<{
13
+ type: string;
14
+ params?: any;
15
+ validateOnly?: boolean;
16
+ }>;
17
+ }
18
+
19
+ export type ConstraintHandler = (
20
+ x: number,
21
+ y: number,
22
+ feature: ConstraintFeature,
23
+ context: ConstraintContext,
24
+ params?: any
25
+ ) => { x: number; y: number };
26
+
27
+ export interface ConstraintConfig {
28
+ type: string;
29
+ params?: any;
30
+ validateOnly?: boolean;
31
+ }
32
+
33
+ export class ConstraintRegistry {
34
+ private static handlers = new Map<string, ConstraintHandler>();
35
+
36
+ static register(type: string, handler: ConstraintHandler) {
37
+ this.handlers.set(type, handler);
38
+ }
39
+
40
+ static apply(
41
+ x: number,
42
+ y: number,
43
+ feature: ConstraintFeature,
44
+ context: ConstraintContext,
45
+ constraints?: ConstraintConfig[] // Optional override, defaults to feature.constraints
46
+ ): { x: number; y: number } {
47
+ const list = constraints || feature.constraints;
48
+ if (!list || list.length === 0) {
49
+ return { x, y };
50
+ }
51
+
52
+ let currentX = x;
53
+ let currentY = y;
54
+
55
+ for (const constraint of list) {
56
+ const handler = this.handlers.get(constraint.type);
57
+ if (handler) {
58
+ const result = handler(currentX, currentY, feature, context, constraint.params || {});
59
+ currentX = result.x;
60
+ currentY = result.y;
61
+ }
62
+ }
63
+
64
+ return { x: currentX, y: currentY };
65
+ }
66
+ }
67
+
68
+ // --- Built-in Strategies ---
69
+
70
+ /**
71
+ * Path Constraint Strategy (formerly placement='edge')
72
+ * Snaps the feature to the nearest point on the Dieline Path.
73
+ */
74
+ const pathConstraint: ConstraintHandler = (x, y, feature, context, params) => {
75
+ // We need to denormalize, find nearest, then normalize back
76
+ // This is expensive but accurate.
77
+ const { dielineWidth, dielineHeight, geometry } = context;
78
+ if (!geometry) return { x, y }; // Cannot snap without geometry
79
+
80
+ // Geometry is centered at (cx, cy)
81
+ // x, y are normalized (0-1) relative to bounding box
82
+ const minX = geometry.x - geometry.width / 2;
83
+ const minY = geometry.y - geometry.height / 2;
84
+
85
+ const absX = minX + x * geometry.width;
86
+ const absY = minY + y * geometry.height;
87
+
88
+ // Use geometry helper
89
+ // Note: getNearestPointOnDieline creates a fresh paper scope each time.
90
+ // Optimization: geometry object passed in context could be a reusable paper path?
91
+ // For now, keep it simple as per existing logic.
92
+ const nearest = getNearestPointOnDieline(
93
+ { x: absX, y: absY },
94
+ geometry
95
+ );
96
+
97
+ let finalX = nearest.x;
98
+ let finalY = nearest.y;
99
+
100
+ // Only allow vertical offset if explicit offset limits are provided
101
+ // Otherwise, we snap strictly to the path (offset = 0)
102
+ const hasOffsetParams = params.minOffset !== undefined || params.maxOffset !== undefined;
103
+
104
+ if (hasOffsetParams && nearest.normal) {
105
+ // Project the cursor vector onto the normal vector
106
+ // This ensures the feature stays on the "normal line" of the nearest path point
107
+ const dx = absX - nearest.x;
108
+ const dy = absY - nearest.y;
109
+
110
+ const nx = nearest.normal.x;
111
+ const ny = nearest.normal.y;
112
+
113
+ // Dot product to get scalar projection
114
+ const dist = dx * nx + dy * ny;
115
+
116
+ // Limit the offset
117
+ // geometry.width is in pixels, dielineWidth is in physical units (e.g. mm)
118
+ // We assume dielineWidth corresponds to geometry.width
119
+ const scale = dielineWidth > 0 ? geometry.width / dielineWidth : 1;
120
+
121
+ // If one is provided but the other is not, default the other to 0.
122
+ // If neither is provided (shouldn't happen due to hasOffsetParams check), default to 0.
123
+ const rawMin = params.minOffset !== undefined ? params.minOffset : 0;
124
+ const rawMax = params.maxOffset !== undefined ? params.maxOffset : 0;
125
+
126
+ // However, if we want to allow one-sided infinity, user must explicitly provide Infinity?
127
+ // Wait, user requirement: "If only one is passed, the other defaults to 0."
128
+ // This implies:
129
+ // { minOffset: -5 } -> maxOffset = 0 (range: -5 to 0)
130
+ // { maxOffset: 5 } -> minOffset = 0 (range: 0 to 5)
131
+ // { minOffset: -5, maxOffset: 5 } -> (range: -5 to 5)
132
+
133
+ const minOffset = rawMin * scale;
134
+ const maxOffset = rawMax * scale;
135
+
136
+ const clampedDist = Math.max(minOffset, Math.min(dist, maxOffset));
137
+
138
+ finalX = nearest.x + nx * clampedDist;
139
+ finalY = nearest.y + ny * clampedDist;
140
+ }
141
+
142
+ // Re-normalize
143
+ const nx = geometry.width > 0 ? (finalX - minX) / geometry.width : 0.5;
144
+ const ny = geometry.height > 0 ? (finalY - minY) / geometry.height : 0.5;
145
+
146
+ return { x: nx, y: ny };
147
+ };
148
+
149
+ /**
150
+ * Edge Constraint Strategy (Box Edge)
151
+ * Snaps the feature to the nearest allowed edge of the BOUNDING BOX.
152
+ */
153
+ const edgeConstraint: ConstraintHandler = (x, y, feature, context, params) => {
154
+ const { dielineWidth, dielineHeight } = context;
155
+ const allowedEdges = params.allowedEdges || [
156
+ "top",
157
+ "bottom",
158
+ "left",
159
+ "right",
160
+ ];
161
+ const confine = params.confine || false;
162
+ const offset = params.offset || 0;
163
+
164
+ // Calculate physical distances to allowed edges
165
+ const distances: { edge: string; dist: number }[] = [];
166
+
167
+ if (allowedEdges.includes("top"))
168
+ distances.push({ edge: "top", dist: y * dielineHeight });
169
+ if (allowedEdges.includes("bottom"))
170
+ distances.push({ edge: "bottom", dist: (1 - y) * dielineHeight });
171
+ if (allowedEdges.includes("left"))
172
+ distances.push({ edge: "left", dist: x * dielineWidth });
173
+ if (allowedEdges.includes("right"))
174
+ distances.push({ edge: "right", dist: (1 - x) * dielineWidth });
175
+
176
+ if (distances.length === 0) return { x, y };
177
+
178
+ // Find nearest
179
+ distances.sort((a, b) => a.dist - b.dist);
180
+ const nearest = distances[0].edge;
181
+
182
+ let newX = x;
183
+ let newY = y;
184
+ const fw = feature.width || 0;
185
+ const fh = feature.height || 0;
186
+
187
+ // Snap to edge
188
+ switch (nearest) {
189
+ case "top":
190
+ newY = 0 + offset / dielineHeight;
191
+ if (confine) {
192
+ const minX = (fw / 2) / dielineWidth;
193
+ const maxX = 1 - minX;
194
+ newX = Math.max(minX, Math.min(newX, maxX));
195
+ }
196
+ break;
197
+ case "bottom":
198
+ newY = 1 - offset / dielineHeight;
199
+ if (confine) {
200
+ const minX = (fw / 2) / dielineWidth;
201
+ const maxX = 1 - minX;
202
+ newX = Math.max(minX, Math.min(newX, maxX));
203
+ }
204
+ break;
205
+ case "left":
206
+ newX = 0 + offset / dielineWidth;
207
+ if (confine) {
208
+ const minY = (fh / 2) / dielineHeight;
209
+ const maxY = 1 - minY;
210
+ newY = Math.max(minY, Math.min(newY, maxY));
211
+ }
212
+ break;
213
+ case "right":
214
+ newX = 1 - offset / dielineWidth;
215
+ if (confine) {
216
+ const minY = (fh / 2) / dielineHeight;
217
+ const maxY = 1 - minY;
218
+ newY = Math.max(minY, Math.min(newY, maxY));
219
+ }
220
+ break;
221
+ }
222
+
223
+ return { x: newX, y: newY };
224
+ };
225
+
226
+ /**
227
+ * Internal Constraint Strategy
228
+ * Keeps the feature strictly inside the dieline bounds with optional margin.
229
+ */
230
+ const internalConstraint: ConstraintHandler = (x, y, feature, context, params) => {
231
+ const { dielineWidth, dielineHeight } = context;
232
+ const margin = params.margin || 0;
233
+ const fw = feature.width || 0;
234
+ const fh = feature.height || 0;
235
+
236
+ const minX = (margin + fw / 2) / dielineWidth;
237
+ const maxX = 1 - (margin + fw / 2) / dielineWidth;
238
+
239
+ const minY = (margin + fh / 2) / dielineHeight;
240
+ const maxY = 1 - (margin + fh / 2) / dielineHeight;
241
+
242
+ // Handle case where feature is larger than container
243
+ const clampedX = minX > maxX ? 0.5 : Math.max(minX, Math.min(x, maxX));
244
+ const clampedY = minY > maxY ? 0.5 : Math.max(minY, Math.min(y, maxY));
245
+
246
+ return { x: clampedX, y: clampedY };
247
+ };
248
+
249
+ /**
250
+ * Bottom Tangent Strategy (stand protrusion)
251
+ * Forces a feature to be tangent to the dieline bottom edge from outside (below).
252
+ */
253
+ const tangentBottomConstraint: ConstraintHandler = (x, y, feature, context, params) => {
254
+ const { dielineWidth, dielineHeight } = context;
255
+ const gap = params.gap || 0;
256
+ const confineX = params.confineX !== false;
257
+
258
+ const extentY =
259
+ feature.shape === "circle"
260
+ ? feature.radius || 0
261
+ : (feature.height || 0) / 2;
262
+ const newY = 1 + (extentY + gap) / dielineHeight;
263
+
264
+ let newX = x;
265
+ if (confineX) {
266
+ const extentX =
267
+ feature.shape === "circle"
268
+ ? feature.radius || 0
269
+ : (feature.width || 0) / 2;
270
+ const minX = extentX / dielineWidth;
271
+ const maxX = 1 - extentX / dielineWidth;
272
+ newX = minX > maxX ? 0.5 : Math.max(minX, Math.min(newX, maxX));
273
+ }
274
+
275
+ return { x: newX, y: newY };
276
+ };
277
+
278
+ /**
279
+ * Lowest Tangent Strategy (Lowest Point Lock)
280
+ * Finds the lowest point of the Dieline geometry and locks the feature's Y to that level.
281
+ * Allows horizontal movement (X).
282
+ */
283
+ const lowestTangentConstraint: ConstraintHandler = (x, y, feature, context, params) => {
284
+ const { dielineWidth, dielineHeight, geometry } = context;
285
+ if (!geometry) return { x, y };
286
+
287
+ const lowest = getLowestPointOnDieline(geometry);
288
+
289
+ // Calculate normalized Y of the lowest point
290
+ const minY = geometry.y - geometry.height / 2;
291
+ const normY = (lowest.y - minY) / geometry.height;
292
+
293
+ const gap = params.gap || 0;
294
+ const confineX = params.confineX !== false;
295
+
296
+ const extentY =
297
+ feature.shape === "circle"
298
+ ? feature.radius || 0
299
+ : (feature.height || 0) / 2;
300
+
301
+ const newY = normY + (extentY + gap) / dielineHeight;
302
+
303
+ let newX = x;
304
+ if (confineX) {
305
+ const extentX =
306
+ feature.shape === "circle"
307
+ ? feature.radius || 0
308
+ : (feature.width || 0) / 2;
309
+ const minX = extentX / dielineWidth;
310
+ const maxX = 1 - extentX / dielineWidth;
311
+ newX = minX > maxX ? 0.5 : Math.max(minX, Math.min(newX, maxX));
312
+ }
313
+
314
+ return { x: newX, y: newY };
315
+ };
316
+
317
+ // Register built-ins
318
+ ConstraintRegistry.register("path", pathConstraint);
319
+ ConstraintRegistry.register("edge", edgeConstraint);
320
+ ConstraintRegistry.register("internal", internalConstraint);
321
+ ConstraintRegistry.register("tangent-bottom", tangentBottomConstraint);
322
+ ConstraintRegistry.register("lowest-tangent", lowestTangentConstraint);