@pooder/kit 5.3.1 → 5.4.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 (90) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/index.d.mts +243 -36
  3. package/dist/index.d.ts +243 -36
  4. package/dist/index.js +2278 -1041
  5. package/dist/index.mjs +2278 -1041
  6. package/package.json +1 -1
  7. package/src/coordinate.ts +106 -106
  8. package/src/extensions/background.ts +323 -230
  9. package/src/extensions/bridgeSelection.ts +17 -17
  10. package/src/extensions/constraints.ts +322 -322
  11. package/src/extensions/dieline.ts +1149 -1076
  12. package/src/extensions/dielineShape.ts +109 -0
  13. package/src/extensions/edgeScale.ts +19 -19
  14. package/src/extensions/feature.ts +1137 -1021
  15. package/src/extensions/featureComplete.ts +46 -46
  16. package/src/extensions/film.ts +266 -194
  17. package/src/extensions/geometry.ts +885 -752
  18. package/src/extensions/image.ts +2054 -1926
  19. package/src/extensions/index.ts +11 -11
  20. package/src/extensions/maskOps.ts +283 -283
  21. package/src/extensions/mirror.ts +128 -128
  22. package/src/extensions/ruler.ts +654 -451
  23. package/src/extensions/sceneLayout.ts +140 -140
  24. package/src/extensions/sceneLayoutModel.ts +364 -352
  25. package/src/extensions/sceneVisibility.ts +64 -71
  26. package/src/extensions/size.ts +389 -389
  27. package/src/extensions/tracer.ts +1019 -1019
  28. package/src/extensions/white-ink.ts +1567 -1514
  29. package/src/extensions/wrappedOffsets.ts +33 -33
  30. package/src/index.ts +2 -2
  31. package/src/services/CanvasService.ts +832 -300
  32. package/src/services/ViewportSystem.ts +95 -95
  33. package/src/services/index.ts +3 -3
  34. package/src/services/renderSpec.ts +53 -18
  35. package/src/units.ts +27 -27
  36. package/tests/run.ts +118 -118
  37. package/tsconfig.test.json +15 -15
  38. package/.test-dist/src/CanvasService.js +0 -249
  39. package/.test-dist/src/ViewportSystem.js +0 -75
  40. package/.test-dist/src/background.js +0 -203
  41. package/.test-dist/src/bridgeSelection.js +0 -20
  42. package/.test-dist/src/constraints.js +0 -237
  43. package/.test-dist/src/coordinate.js +0 -74
  44. package/.test-dist/src/dieline.js +0 -818
  45. package/.test-dist/src/edgeScale.js +0 -12
  46. package/.test-dist/src/extensions/background.js +0 -203
  47. package/.test-dist/src/extensions/bridgeSelection.js +0 -20
  48. package/.test-dist/src/extensions/constraints.js +0 -237
  49. package/.test-dist/src/extensions/dieline.js +0 -828
  50. package/.test-dist/src/extensions/edgeScale.js +0 -12
  51. package/.test-dist/src/extensions/feature.js +0 -825
  52. package/.test-dist/src/extensions/featureComplete.js +0 -32
  53. package/.test-dist/src/extensions/film.js +0 -167
  54. package/.test-dist/src/extensions/geometry.js +0 -545
  55. package/.test-dist/src/extensions/image.js +0 -1529
  56. package/.test-dist/src/extensions/index.js +0 -30
  57. package/.test-dist/src/extensions/maskOps.js +0 -279
  58. package/.test-dist/src/extensions/mirror.js +0 -104
  59. package/.test-dist/src/extensions/ruler.js +0 -345
  60. package/.test-dist/src/extensions/sceneLayout.js +0 -96
  61. package/.test-dist/src/extensions/sceneLayoutModel.js +0 -196
  62. package/.test-dist/src/extensions/sceneVisibility.js +0 -62
  63. package/.test-dist/src/extensions/size.js +0 -331
  64. package/.test-dist/src/extensions/tracer.js +0 -538
  65. package/.test-dist/src/extensions/white-ink.js +0 -1190
  66. package/.test-dist/src/extensions/wrappedOffsets.js +0 -33
  67. package/.test-dist/src/feature.js +0 -826
  68. package/.test-dist/src/featureComplete.js +0 -32
  69. package/.test-dist/src/film.js +0 -167
  70. package/.test-dist/src/geometry.js +0 -506
  71. package/.test-dist/src/image.js +0 -1250
  72. package/.test-dist/src/index.js +0 -18
  73. package/.test-dist/src/maskOps.js +0 -270
  74. package/.test-dist/src/mirror.js +0 -104
  75. package/.test-dist/src/renderSpec.js +0 -2
  76. package/.test-dist/src/ruler.js +0 -343
  77. package/.test-dist/src/sceneLayout.js +0 -99
  78. package/.test-dist/src/sceneLayoutModel.js +0 -196
  79. package/.test-dist/src/sceneView.js +0 -40
  80. package/.test-dist/src/sceneVisibility.js +0 -42
  81. package/.test-dist/src/services/CanvasService.js +0 -249
  82. package/.test-dist/src/services/ViewportSystem.js +0 -76
  83. package/.test-dist/src/services/index.js +0 -24
  84. package/.test-dist/src/services/renderSpec.js +0 -2
  85. package/.test-dist/src/size.js +0 -332
  86. package/.test-dist/src/tracer.js +0 -544
  87. package/.test-dist/src/units.js +0 -30
  88. package/.test-dist/src/white-ink.js +0 -829
  89. package/.test-dist/src/wrappedOffsets.js +0 -33
  90. package/.test-dist/tests/run.js +0 -94
@@ -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);