@pooder/kit 4.3.1 → 5.0.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.
Files changed (60) hide show
  1. package/.test-dist/src/CanvasService.js +249 -0
  2. package/.test-dist/src/ViewportSystem.js +75 -0
  3. package/.test-dist/src/background.js +203 -0
  4. package/.test-dist/src/bridgeSelection.js +20 -0
  5. package/.test-dist/src/constraints.js +237 -0
  6. package/.test-dist/src/coordinate.js +74 -0
  7. package/.test-dist/src/dieline.js +818 -0
  8. package/.test-dist/src/edgeScale.js +12 -0
  9. package/.test-dist/src/feature.js +754 -0
  10. package/.test-dist/src/featureComplete.js +32 -0
  11. package/.test-dist/src/film.js +167 -0
  12. package/.test-dist/src/geometry.js +506 -0
  13. package/.test-dist/src/image.js +1234 -0
  14. package/.test-dist/src/index.js +35 -0
  15. package/.test-dist/src/maskOps.js +270 -0
  16. package/.test-dist/src/mirror.js +104 -0
  17. package/.test-dist/src/renderSpec.js +2 -0
  18. package/.test-dist/src/ruler.js +343 -0
  19. package/.test-dist/src/sceneLayout.js +99 -0
  20. package/.test-dist/src/sceneLayoutModel.js +196 -0
  21. package/.test-dist/src/sceneView.js +40 -0
  22. package/.test-dist/src/sceneVisibility.js +42 -0
  23. package/.test-dist/src/size.js +332 -0
  24. package/.test-dist/src/tracer.js +544 -0
  25. package/.test-dist/src/units.js +30 -0
  26. package/.test-dist/src/white-ink.js +829 -0
  27. package/.test-dist/src/wrappedOffsets.js +33 -0
  28. package/.test-dist/tests/run.js +94 -0
  29. package/CHANGELOG.md +17 -0
  30. package/dist/index.d.mts +342 -37
  31. package/dist/index.d.ts +342 -37
  32. package/dist/index.js +3679 -865
  33. package/dist/index.mjs +3673 -868
  34. package/package.json +2 -2
  35. package/src/CanvasService.ts +300 -96
  36. package/src/ViewportSystem.ts +92 -92
  37. package/src/background.ts +230 -230
  38. package/src/bridgeSelection.ts +17 -0
  39. package/src/coordinate.ts +106 -106
  40. package/src/dieline.ts +1005 -973
  41. package/src/edgeScale.ts +19 -0
  42. package/src/feature.ts +83 -30
  43. package/src/film.ts +194 -194
  44. package/src/geometry.ts +242 -84
  45. package/src/image.ts +1582 -512
  46. package/src/index.ts +14 -10
  47. package/src/maskOps.ts +326 -0
  48. package/src/mirror.ts +128 -128
  49. package/src/renderSpec.ts +18 -0
  50. package/src/ruler.ts +449 -508
  51. package/src/sceneLayout.ts +121 -0
  52. package/src/sceneLayoutModel.ts +335 -0
  53. package/src/sceneVisibility.ts +49 -0
  54. package/src/size.ts +379 -0
  55. package/src/tracer.ts +719 -570
  56. package/src/units.ts +27 -27
  57. package/src/white-ink.ts +1018 -373
  58. package/src/wrappedOffsets.ts +33 -0
  59. package/tests/run.ts +118 -0
  60. package/tsconfig.test.json +15 -15
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConstraintRegistry = void 0;
4
+ const geometry_1 = require("./geometry");
5
+ class ConstraintRegistry {
6
+ static register(type, handler) {
7
+ this.handlers.set(type, handler);
8
+ }
9
+ static apply(x, y, feature, context, constraints // Optional override, defaults to feature.constraints
10
+ ) {
11
+ const list = constraints || feature.constraints;
12
+ if (!list || list.length === 0) {
13
+ return { x, y };
14
+ }
15
+ let currentX = x;
16
+ let currentY = y;
17
+ for (const constraint of list) {
18
+ const handler = this.handlers.get(constraint.type);
19
+ if (handler) {
20
+ const result = handler(currentX, currentY, feature, context, constraint.params || {});
21
+ currentX = result.x;
22
+ currentY = result.y;
23
+ }
24
+ }
25
+ return { x: currentX, y: currentY };
26
+ }
27
+ }
28
+ exports.ConstraintRegistry = ConstraintRegistry;
29
+ ConstraintRegistry.handlers = new Map();
30
+ // --- Built-in Strategies ---
31
+ /**
32
+ * Path Constraint Strategy (formerly placement='edge')
33
+ * Snaps the feature to the nearest point on the Dieline Path.
34
+ */
35
+ const pathConstraint = (x, y, feature, context, params) => {
36
+ // We need to denormalize, find nearest, then normalize back
37
+ // This is expensive but accurate.
38
+ const { dielineWidth, dielineHeight, geometry } = context;
39
+ if (!geometry)
40
+ return { x, y }; // Cannot snap without geometry
41
+ // Geometry is centered at (cx, cy)
42
+ // x, y are normalized (0-1) relative to bounding box
43
+ const minX = geometry.x - geometry.width / 2;
44
+ const minY = geometry.y - geometry.height / 2;
45
+ const absX = minX + x * geometry.width;
46
+ const absY = minY + y * geometry.height;
47
+ // Use geometry helper
48
+ // Note: getNearestPointOnDieline creates a fresh paper scope each time.
49
+ // Optimization: geometry object passed in context could be a reusable paper path?
50
+ // For now, keep it simple as per existing logic.
51
+ const nearest = (0, geometry_1.getNearestPointOnDieline)({ x: absX, y: absY }, geometry);
52
+ let finalX = nearest.x;
53
+ let finalY = nearest.y;
54
+ // Only allow vertical offset if explicit offset limits are provided
55
+ // Otherwise, we snap strictly to the path (offset = 0)
56
+ const hasOffsetParams = params.minOffset !== undefined || params.maxOffset !== undefined;
57
+ if (hasOffsetParams && nearest.normal) {
58
+ // Project the cursor vector onto the normal vector
59
+ // This ensures the feature stays on the "normal line" of the nearest path point
60
+ const dx = absX - nearest.x;
61
+ const dy = absY - nearest.y;
62
+ const nx = nearest.normal.x;
63
+ const ny = nearest.normal.y;
64
+ // Dot product to get scalar projection
65
+ const dist = dx * nx + dy * ny;
66
+ // Limit the offset
67
+ // geometry.width is in pixels, dielineWidth is in physical units (e.g. mm)
68
+ // We assume dielineWidth corresponds to geometry.width
69
+ const scale = dielineWidth > 0 ? geometry.width / dielineWidth : 1;
70
+ // If one is provided but the other is not, default the other to 0.
71
+ // If neither is provided (shouldn't happen due to hasOffsetParams check), default to 0.
72
+ const rawMin = params.minOffset !== undefined ? params.minOffset : 0;
73
+ const rawMax = params.maxOffset !== undefined ? params.maxOffset : 0;
74
+ // However, if we want to allow one-sided infinity, user must explicitly provide Infinity?
75
+ // Wait, user requirement: "If only one is passed, the other defaults to 0."
76
+ // This implies:
77
+ // { minOffset: -5 } -> maxOffset = 0 (range: -5 to 0)
78
+ // { maxOffset: 5 } -> minOffset = 0 (range: 0 to 5)
79
+ // { minOffset: -5, maxOffset: 5 } -> (range: -5 to 5)
80
+ const minOffset = rawMin * scale;
81
+ const maxOffset = rawMax * scale;
82
+ const clampedDist = Math.max(minOffset, Math.min(dist, maxOffset));
83
+ finalX = nearest.x + nx * clampedDist;
84
+ finalY = nearest.y + ny * clampedDist;
85
+ }
86
+ // Re-normalize
87
+ const nx = geometry.width > 0 ? (finalX - minX) / geometry.width : 0.5;
88
+ const ny = geometry.height > 0 ? (finalY - minY) / geometry.height : 0.5;
89
+ return { x: nx, y: ny };
90
+ };
91
+ /**
92
+ * Edge Constraint Strategy (Box Edge)
93
+ * Snaps the feature to the nearest allowed edge of the BOUNDING BOX.
94
+ */
95
+ const edgeConstraint = (x, y, feature, context, params) => {
96
+ const { dielineWidth, dielineHeight } = context;
97
+ const allowedEdges = params.allowedEdges || [
98
+ "top",
99
+ "bottom",
100
+ "left",
101
+ "right",
102
+ ];
103
+ const confine = params.confine || false;
104
+ const offset = params.offset || 0;
105
+ // Calculate physical distances to allowed edges
106
+ const distances = [];
107
+ if (allowedEdges.includes("top"))
108
+ distances.push({ edge: "top", dist: y * dielineHeight });
109
+ if (allowedEdges.includes("bottom"))
110
+ distances.push({ edge: "bottom", dist: (1 - y) * dielineHeight });
111
+ if (allowedEdges.includes("left"))
112
+ distances.push({ edge: "left", dist: x * dielineWidth });
113
+ if (allowedEdges.includes("right"))
114
+ distances.push({ edge: "right", dist: (1 - x) * dielineWidth });
115
+ if (distances.length === 0)
116
+ return { x, y };
117
+ // Find nearest
118
+ distances.sort((a, b) => a.dist - b.dist);
119
+ const nearest = distances[0].edge;
120
+ let newX = x;
121
+ let newY = y;
122
+ const fw = feature.width || 0;
123
+ const fh = feature.height || 0;
124
+ // Snap to edge
125
+ switch (nearest) {
126
+ case "top":
127
+ newY = 0 + offset / dielineHeight;
128
+ if (confine) {
129
+ const minX = (fw / 2) / dielineWidth;
130
+ const maxX = 1 - minX;
131
+ newX = Math.max(minX, Math.min(newX, maxX));
132
+ }
133
+ break;
134
+ case "bottom":
135
+ newY = 1 - offset / dielineHeight;
136
+ if (confine) {
137
+ const minX = (fw / 2) / dielineWidth;
138
+ const maxX = 1 - minX;
139
+ newX = Math.max(minX, Math.min(newX, maxX));
140
+ }
141
+ break;
142
+ case "left":
143
+ newX = 0 + offset / dielineWidth;
144
+ if (confine) {
145
+ const minY = (fh / 2) / dielineHeight;
146
+ const maxY = 1 - minY;
147
+ newY = Math.max(minY, Math.min(newY, maxY));
148
+ }
149
+ break;
150
+ case "right":
151
+ newX = 1 - offset / dielineWidth;
152
+ if (confine) {
153
+ const minY = (fh / 2) / dielineHeight;
154
+ const maxY = 1 - minY;
155
+ newY = Math.max(minY, Math.min(newY, maxY));
156
+ }
157
+ break;
158
+ }
159
+ return { x: newX, y: newY };
160
+ };
161
+ /**
162
+ * Internal Constraint Strategy
163
+ * Keeps the feature strictly inside the dieline bounds with optional margin.
164
+ */
165
+ const internalConstraint = (x, y, feature, context, params) => {
166
+ const { dielineWidth, dielineHeight } = context;
167
+ const margin = params.margin || 0;
168
+ const fw = feature.width || 0;
169
+ const fh = feature.height || 0;
170
+ const minX = (margin + fw / 2) / dielineWidth;
171
+ const maxX = 1 - (margin + fw / 2) / dielineWidth;
172
+ const minY = (margin + fh / 2) / dielineHeight;
173
+ const maxY = 1 - (margin + fh / 2) / dielineHeight;
174
+ // Handle case where feature is larger than container
175
+ const clampedX = minX > maxX ? 0.5 : Math.max(minX, Math.min(x, maxX));
176
+ const clampedY = minY > maxY ? 0.5 : Math.max(minY, Math.min(y, maxY));
177
+ return { x: clampedX, y: clampedY };
178
+ };
179
+ /**
180
+ * Bottom Tangent Strategy (stand protrusion)
181
+ * Forces a feature to be tangent to the dieline bottom edge from outside (below).
182
+ */
183
+ const tangentBottomConstraint = (x, y, feature, context, params) => {
184
+ const { dielineWidth, dielineHeight } = context;
185
+ const gap = params.gap || 0;
186
+ const confineX = params.confineX !== false;
187
+ const extentY = feature.shape === "circle"
188
+ ? feature.radius || 0
189
+ : (feature.height || 0) / 2;
190
+ const newY = 1 + (extentY + gap) / dielineHeight;
191
+ let newX = x;
192
+ if (confineX) {
193
+ const extentX = 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
+ return { x: newX, y: newY };
201
+ };
202
+ /**
203
+ * Lowest Tangent Strategy (Lowest Point Lock)
204
+ * Finds the lowest point of the Dieline geometry and locks the feature's Y to that level.
205
+ * Allows horizontal movement (X).
206
+ */
207
+ const lowestTangentConstraint = (x, y, feature, context, params) => {
208
+ const { dielineWidth, dielineHeight, geometry } = context;
209
+ if (!geometry)
210
+ return { x, y };
211
+ const lowest = (0, geometry_1.getLowestPointOnDieline)(geometry);
212
+ // Calculate normalized Y of the lowest point
213
+ const minY = geometry.y - geometry.height / 2;
214
+ const normY = (lowest.y - minY) / geometry.height;
215
+ const gap = params.gap || 0;
216
+ const confineX = params.confineX !== false;
217
+ const extentY = feature.shape === "circle"
218
+ ? feature.radius || 0
219
+ : (feature.height || 0) / 2;
220
+ const newY = normY + (extentY + gap) / dielineHeight;
221
+ let newX = x;
222
+ if (confineX) {
223
+ const extentX = feature.shape === "circle"
224
+ ? feature.radius || 0
225
+ : (feature.width || 0) / 2;
226
+ const minX = extentX / dielineWidth;
227
+ const maxX = 1 - extentX / dielineWidth;
228
+ newX = minX > maxX ? 0.5 : Math.max(minX, Math.min(newX, maxX));
229
+ }
230
+ return { x: newX, y: newY };
231
+ };
232
+ // Register built-ins
233
+ ConstraintRegistry.register("path", pathConstraint);
234
+ ConstraintRegistry.register("edge", edgeConstraint);
235
+ ConstraintRegistry.register("internal", internalConstraint);
236
+ ConstraintRegistry.register("tangent-bottom", tangentBottomConstraint);
237
+ ConstraintRegistry.register("lowest-tangent", lowestTangentConstraint);
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Coordinate = void 0;
4
+ class Coordinate {
5
+ /**
6
+ * Calculate layout to fit content within container while preserving aspect ratio.
7
+ */
8
+ static calculateLayout(container, content, padding = 0) {
9
+ const availableWidth = Math.max(0, container.width - padding * 2);
10
+ const availableHeight = Math.max(0, container.height - padding * 2);
11
+ if (content.width === 0 || content.height === 0) {
12
+ return { scale: 1, offsetX: 0, offsetY: 0, width: 0, height: 0 };
13
+ }
14
+ const scaleX = availableWidth / content.width;
15
+ const scaleY = availableHeight / content.height;
16
+ const scale = Math.min(scaleX, scaleY);
17
+ const width = content.width * scale;
18
+ const height = content.height * scale;
19
+ const offsetX = (container.width - width) / 2;
20
+ const offsetY = (container.height - height) / 2;
21
+ return { scale, offsetX, offsetY, width, height };
22
+ }
23
+ /**
24
+ * Convert an absolute value to a normalized value (0-1).
25
+ * @param value Absolute value (e.g., pixels)
26
+ * @param total Total dimension size (e.g., canvas width)
27
+ */
28
+ static toNormalized(value, total) {
29
+ return total === 0 ? 0 : value / total;
30
+ }
31
+ /**
32
+ * Convert a normalized value (0-1) to an absolute value.
33
+ * @param normalized Normalized value (0-1)
34
+ * @param total Total dimension size (e.g., canvas width)
35
+ */
36
+ static toAbsolute(normalized, total) {
37
+ return normalized * total;
38
+ }
39
+ /**
40
+ * Normalize a point's coordinates.
41
+ */
42
+ static normalizePoint(point, size) {
43
+ return {
44
+ x: this.toNormalized(point.x, size.width),
45
+ y: this.toNormalized(point.y, size.height),
46
+ };
47
+ }
48
+ /**
49
+ * Denormalize a point's coordinates to absolute pixels.
50
+ */
51
+ static denormalizePoint(point, size) {
52
+ return {
53
+ x: this.toAbsolute(point.x, size.width),
54
+ y: this.toAbsolute(point.y, size.height),
55
+ };
56
+ }
57
+ static convertUnit(value, from, to) {
58
+ if (from === to)
59
+ return value;
60
+ // Base unit: mm
61
+ const toMM = {
62
+ px: 0.264583, // 1px = 0.264583mm (96 DPI)
63
+ mm: 1,
64
+ cm: 10,
65
+ in: 25.4
66
+ };
67
+ const mmValue = value * (from === 'px' ? toMM.px : toMM[from] || 1);
68
+ if (to === 'px') {
69
+ return mmValue / toMM.px;
70
+ }
71
+ return mmValue / (toMM[to] || 1);
72
+ }
73
+ }
74
+ exports.Coordinate = Coordinate;