@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,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const fabric_1 = require("fabric");
|
|
4
|
+
const ViewportSystem_1 = require("./ViewportSystem");
|
|
5
|
+
class CanvasService {
|
|
6
|
+
constructor(el, options) {
|
|
7
|
+
if (el instanceof fabric_1.Canvas) {
|
|
8
|
+
this.canvas = el;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
this.canvas = new fabric_1.Canvas(el, {
|
|
12
|
+
preserveObjectStacking: true,
|
|
13
|
+
...options,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
this.viewport = new ViewportSystem_1.ViewportSystem();
|
|
17
|
+
if (this.canvas.width !== undefined && this.canvas.height !== undefined) {
|
|
18
|
+
this.viewport.updateContainer(this.canvas.width, this.canvas.height);
|
|
19
|
+
}
|
|
20
|
+
if (options?.eventBus) {
|
|
21
|
+
this.setEventBus(options.eventBus);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
setEventBus(eventBus) {
|
|
25
|
+
this.eventBus = eventBus;
|
|
26
|
+
this.setupEvents();
|
|
27
|
+
}
|
|
28
|
+
setupEvents() {
|
|
29
|
+
if (!this.eventBus)
|
|
30
|
+
return;
|
|
31
|
+
const bus = this.eventBus;
|
|
32
|
+
const forward = (name) => (e) => bus.emit(name, e);
|
|
33
|
+
this.canvas.on("selection:created", forward("selection:created"));
|
|
34
|
+
this.canvas.on("selection:updated", forward("selection:updated"));
|
|
35
|
+
this.canvas.on("selection:cleared", forward("selection:cleared"));
|
|
36
|
+
this.canvas.on("object:modified", forward("object:modified"));
|
|
37
|
+
this.canvas.on("object:added", forward("object:added"));
|
|
38
|
+
this.canvas.on("object:removed", forward("object:removed"));
|
|
39
|
+
}
|
|
40
|
+
dispose() {
|
|
41
|
+
this.canvas.dispose();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get a layer (Group) by its ID.
|
|
45
|
+
* We assume layers are Groups directly on the canvas with a data.id property.
|
|
46
|
+
*/
|
|
47
|
+
getLayer(id) {
|
|
48
|
+
return this.canvas.getObjects().find((obj) => obj.data?.id === id);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create a layer (Group) with the given ID if it doesn't exist.
|
|
52
|
+
*/
|
|
53
|
+
createLayer(id, options = {}) {
|
|
54
|
+
let layer = this.getLayer(id);
|
|
55
|
+
if (!layer) {
|
|
56
|
+
const defaultOptions = {
|
|
57
|
+
selectable: false,
|
|
58
|
+
evented: false,
|
|
59
|
+
...options,
|
|
60
|
+
data: { ...options.data, id },
|
|
61
|
+
};
|
|
62
|
+
layer = new fabric_1.Group([], defaultOptions);
|
|
63
|
+
this.canvas.add(layer);
|
|
64
|
+
}
|
|
65
|
+
return layer;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Find an object by ID, optionally within a specific layer.
|
|
69
|
+
*/
|
|
70
|
+
getObject(id, layerId) {
|
|
71
|
+
if (layerId) {
|
|
72
|
+
const layer = this.getLayer(layerId);
|
|
73
|
+
if (!layer)
|
|
74
|
+
return undefined;
|
|
75
|
+
return layer.getObjects().find((obj) => obj.data?.id === id);
|
|
76
|
+
}
|
|
77
|
+
return this.canvas.getObjects().find((obj) => obj.data?.id === id);
|
|
78
|
+
}
|
|
79
|
+
requestRenderAll() {
|
|
80
|
+
this.canvas.requestRenderAll();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.default = CanvasService;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ViewportSystem = void 0;
|
|
4
|
+
const coordinate_1 = require("./coordinate");
|
|
5
|
+
class ViewportSystem {
|
|
6
|
+
constructor(containerSize = { width: 0, height: 0 }, physicalSize = { width: 0, height: 0 }, padding = 40) {
|
|
7
|
+
this._containerSize = { width: 0, height: 0 };
|
|
8
|
+
this._physicalSize = { width: 0, height: 0 };
|
|
9
|
+
this._padding = 0;
|
|
10
|
+
this._layout = {
|
|
11
|
+
scale: 1,
|
|
12
|
+
offsetX: 0,
|
|
13
|
+
offsetY: 0,
|
|
14
|
+
width: 0,
|
|
15
|
+
height: 0,
|
|
16
|
+
};
|
|
17
|
+
this._containerSize = containerSize;
|
|
18
|
+
this._physicalSize = physicalSize;
|
|
19
|
+
this._padding = padding;
|
|
20
|
+
this.updateLayout();
|
|
21
|
+
}
|
|
22
|
+
get layout() {
|
|
23
|
+
return this._layout;
|
|
24
|
+
}
|
|
25
|
+
get scale() {
|
|
26
|
+
return this._layout.scale;
|
|
27
|
+
}
|
|
28
|
+
get offset() {
|
|
29
|
+
return { x: this._layout.offsetX, y: this._layout.offsetY };
|
|
30
|
+
}
|
|
31
|
+
updateContainer(width, height) {
|
|
32
|
+
if (this._containerSize.width === width &&
|
|
33
|
+
this._containerSize.height === height)
|
|
34
|
+
return;
|
|
35
|
+
this._containerSize = { width, height };
|
|
36
|
+
this.updateLayout();
|
|
37
|
+
}
|
|
38
|
+
updatePhysical(width, height) {
|
|
39
|
+
if (this._physicalSize.width === width && this._physicalSize.height === height)
|
|
40
|
+
return;
|
|
41
|
+
this._physicalSize = { width, height };
|
|
42
|
+
this.updateLayout();
|
|
43
|
+
}
|
|
44
|
+
setPadding(padding) {
|
|
45
|
+
if (this._padding === padding)
|
|
46
|
+
return;
|
|
47
|
+
this._padding = padding;
|
|
48
|
+
this.updateLayout();
|
|
49
|
+
}
|
|
50
|
+
updateLayout() {
|
|
51
|
+
this._layout = coordinate_1.Coordinate.calculateLayout(this._containerSize, this._physicalSize, this._padding);
|
|
52
|
+
}
|
|
53
|
+
toPixel(value) {
|
|
54
|
+
return value * this._layout.scale;
|
|
55
|
+
}
|
|
56
|
+
toPhysical(value) {
|
|
57
|
+
return this._layout.scale === 0 ? 0 : value / this._layout.scale;
|
|
58
|
+
}
|
|
59
|
+
toPixelPoint(point) {
|
|
60
|
+
return {
|
|
61
|
+
x: point.x * this._layout.scale + this._layout.offsetX,
|
|
62
|
+
y: point.y * this._layout.scale + this._layout.offsetY,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
// Convert screen coordinate (e.g. mouse event) to physical coordinate (relative to content origin)
|
|
66
|
+
toPhysicalPoint(point) {
|
|
67
|
+
if (this._layout.scale === 0)
|
|
68
|
+
return { x: 0, y: 0 };
|
|
69
|
+
return {
|
|
70
|
+
x: (point.x - this._layout.offsetX) / this._layout.scale,
|
|
71
|
+
y: (point.y - this._layout.offsetY) / this._layout.scale,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.ViewportSystem = ViewportSystem;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BackgroundTool = void 0;
|
|
4
|
+
const core_1 = require("@pooder/core");
|
|
5
|
+
const fabric_1 = require("fabric");
|
|
6
|
+
class BackgroundTool {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.id = "pooder.kit.background";
|
|
9
|
+
this.metadata = {
|
|
10
|
+
name: "BackgroundTool",
|
|
11
|
+
};
|
|
12
|
+
this.color = "";
|
|
13
|
+
this.url = "";
|
|
14
|
+
if (options) {
|
|
15
|
+
Object.assign(this, options);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
activate(context) {
|
|
19
|
+
this.canvasService = context.services.get("CanvasService");
|
|
20
|
+
if (!this.canvasService) {
|
|
21
|
+
console.warn("CanvasService not found for BackgroundTool");
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const configService = context.services.get("ConfigurationService");
|
|
25
|
+
if (configService) {
|
|
26
|
+
// Load initial config
|
|
27
|
+
this.color = configService.get("background.color", this.color);
|
|
28
|
+
this.url = configService.get("background.url", this.url);
|
|
29
|
+
// Listen for changes
|
|
30
|
+
configService.onAnyChange((e) => {
|
|
31
|
+
if (e.key.startsWith("background.")) {
|
|
32
|
+
const prop = e.key.split(".")[1];
|
|
33
|
+
console.log(`[BackgroundTool] Config change detected: ${e.key} -> ${e.value}, prop: ${prop}`);
|
|
34
|
+
if (prop && prop in this) {
|
|
35
|
+
console.log(`[BackgroundTool] Updating option ${prop} to ${e.value}`);
|
|
36
|
+
this[prop] = e.value;
|
|
37
|
+
this.updateBackground();
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
console.warn(`[BackgroundTool] Property ${prop} not found in options`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
this.initLayer();
|
|
46
|
+
this.updateBackground();
|
|
47
|
+
}
|
|
48
|
+
deactivate(context) {
|
|
49
|
+
if (this.canvasService) {
|
|
50
|
+
const layer = this.canvasService.getLayer("background");
|
|
51
|
+
if (layer) {
|
|
52
|
+
this.canvasService.canvas.remove(layer);
|
|
53
|
+
}
|
|
54
|
+
this.canvasService = undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
contribute() {
|
|
58
|
+
return {
|
|
59
|
+
[core_1.ContributionPointIds.CONFIGURATIONS]: [
|
|
60
|
+
{
|
|
61
|
+
id: "background.color",
|
|
62
|
+
type: "color",
|
|
63
|
+
label: "Background Color",
|
|
64
|
+
default: "",
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
id: "background.url",
|
|
68
|
+
type: "string",
|
|
69
|
+
label: "Image URL",
|
|
70
|
+
default: "",
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
[core_1.ContributionPointIds.COMMANDS]: [
|
|
74
|
+
{
|
|
75
|
+
command: "reset",
|
|
76
|
+
title: "Reset Background",
|
|
77
|
+
handler: () => {
|
|
78
|
+
this.updateBackground();
|
|
79
|
+
return true;
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
command: "clear",
|
|
84
|
+
title: "Clear Background",
|
|
85
|
+
handler: () => {
|
|
86
|
+
this.color = "transparent";
|
|
87
|
+
this.url = "";
|
|
88
|
+
this.updateBackground();
|
|
89
|
+
return true;
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
command: "setBackgroundColor",
|
|
94
|
+
title: "Set Background Color",
|
|
95
|
+
handler: (color) => {
|
|
96
|
+
if (this.color === color)
|
|
97
|
+
return true;
|
|
98
|
+
this.color = color;
|
|
99
|
+
this.updateBackground();
|
|
100
|
+
return true;
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
command: "setBackgroundImage",
|
|
105
|
+
title: "Set Background Image",
|
|
106
|
+
handler: (url) => {
|
|
107
|
+
if (this.url === url)
|
|
108
|
+
return true;
|
|
109
|
+
this.url = url;
|
|
110
|
+
this.updateBackground();
|
|
111
|
+
return true;
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
initLayer() {
|
|
118
|
+
if (!this.canvasService)
|
|
119
|
+
return;
|
|
120
|
+
let backgroundLayer = this.canvasService.getLayer("background");
|
|
121
|
+
if (!backgroundLayer) {
|
|
122
|
+
backgroundLayer = this.canvasService.createLayer("background", {
|
|
123
|
+
width: this.canvasService.canvas.width,
|
|
124
|
+
height: this.canvasService.canvas.height,
|
|
125
|
+
selectable: false,
|
|
126
|
+
evented: false,
|
|
127
|
+
});
|
|
128
|
+
this.canvasService.canvas.sendObjectToBack(backgroundLayer);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async updateBackground() {
|
|
132
|
+
if (!this.canvasService)
|
|
133
|
+
return;
|
|
134
|
+
const layer = this.canvasService.getLayer("background");
|
|
135
|
+
if (!layer) {
|
|
136
|
+
console.warn("[BackgroundTool] Background layer not found");
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const { color, url } = this;
|
|
140
|
+
const width = this.canvasService.canvas.width || 800;
|
|
141
|
+
const height = this.canvasService.canvas.height || 600;
|
|
142
|
+
let rect = this.canvasService.getObject("background-color-rect", "background");
|
|
143
|
+
if (rect) {
|
|
144
|
+
rect.set({
|
|
145
|
+
fill: color,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
rect = new fabric_1.Rect({
|
|
150
|
+
width,
|
|
151
|
+
height,
|
|
152
|
+
fill: color,
|
|
153
|
+
selectable: false,
|
|
154
|
+
evented: false,
|
|
155
|
+
data: {
|
|
156
|
+
id: "background-color-rect",
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
layer.add(rect);
|
|
160
|
+
layer.sendObjectToBack(rect);
|
|
161
|
+
}
|
|
162
|
+
let img = this.canvasService.getObject("background-image", "background");
|
|
163
|
+
try {
|
|
164
|
+
if (img) {
|
|
165
|
+
if (img.getSrc() !== url) {
|
|
166
|
+
if (url) {
|
|
167
|
+
await img.setSrc(url);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
layer.remove(img);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
if (url) {
|
|
176
|
+
img = await fabric_1.FabricImage.fromURL(url, { crossOrigin: "anonymous" });
|
|
177
|
+
img.set({
|
|
178
|
+
originX: "left",
|
|
179
|
+
originY: "top",
|
|
180
|
+
left: 0,
|
|
181
|
+
top: 0,
|
|
182
|
+
selectable: false,
|
|
183
|
+
evented: false,
|
|
184
|
+
data: {
|
|
185
|
+
id: "background-image",
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
img.scaleToWidth(width);
|
|
189
|
+
if (img.getScaledHeight() < height)
|
|
190
|
+
img.scaleToHeight(height);
|
|
191
|
+
layer.add(img);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
this.canvasService.requestRenderAll();
|
|
195
|
+
}
|
|
196
|
+
catch (e) {
|
|
197
|
+
console.error("[BackgroundTool] Failed to load image", e);
|
|
198
|
+
}
|
|
199
|
+
layer.dirty = true;
|
|
200
|
+
this.canvasService.requestRenderAll();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
exports.BackgroundTool = BackgroundTool;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConstraintRegistry = void 0;
|
|
4
|
+
class ConstraintRegistry {
|
|
5
|
+
static register(type, handler) {
|
|
6
|
+
this.handlers.set(type, handler);
|
|
7
|
+
}
|
|
8
|
+
static apply(x, y, feature, context) {
|
|
9
|
+
if (!feature.constraints || !feature.constraints.type) {
|
|
10
|
+
return { x, y };
|
|
11
|
+
}
|
|
12
|
+
const handler = this.handlers.get(feature.constraints.type);
|
|
13
|
+
if (handler) {
|
|
14
|
+
return handler(x, y, feature, context);
|
|
15
|
+
}
|
|
16
|
+
return { x, y };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.ConstraintRegistry = ConstraintRegistry;
|
|
20
|
+
ConstraintRegistry.handlers = new Map();
|
|
21
|
+
// --- Built-in Strategies ---
|
|
22
|
+
/**
|
|
23
|
+
* Edge Constraint Strategy
|
|
24
|
+
* Snaps the feature to the nearest allowed edge.
|
|
25
|
+
* Params:
|
|
26
|
+
* - allowedEdges: ('top' | 'bottom' | 'left' | 'right')[] (default: all)
|
|
27
|
+
* - confine: boolean (default: false) - if true, keeps feature within edge length
|
|
28
|
+
* - offset: number (default: 0) - physical offset from edge (positive = inwards usually, but here 0 is edge)
|
|
29
|
+
* For simplicity, let's say offset is additive to the edge position.
|
|
30
|
+
* Top: 0 + offset
|
|
31
|
+
* Bottom: 1 - offset
|
|
32
|
+
* Left: 0 + offset
|
|
33
|
+
* Right: 1 - offset
|
|
34
|
+
*/
|
|
35
|
+
const edgeConstraint = (x, y, feature, context) => {
|
|
36
|
+
const { dielineWidth, dielineHeight } = context;
|
|
37
|
+
const params = feature.constraints?.params || {};
|
|
38
|
+
const allowedEdges = params.allowedEdges || [
|
|
39
|
+
"top",
|
|
40
|
+
"bottom",
|
|
41
|
+
"left",
|
|
42
|
+
"right",
|
|
43
|
+
];
|
|
44
|
+
const confine = params.confine || false;
|
|
45
|
+
const offset = params.offset || 0;
|
|
46
|
+
// Calculate physical distances to allowed edges
|
|
47
|
+
const distances = [];
|
|
48
|
+
if (allowedEdges.includes("top"))
|
|
49
|
+
distances.push({ edge: "top", dist: y * dielineHeight });
|
|
50
|
+
if (allowedEdges.includes("bottom"))
|
|
51
|
+
distances.push({ edge: "bottom", dist: (1 - y) * dielineHeight });
|
|
52
|
+
if (allowedEdges.includes("left"))
|
|
53
|
+
distances.push({ edge: "left", dist: x * dielineWidth });
|
|
54
|
+
if (allowedEdges.includes("right"))
|
|
55
|
+
distances.push({ edge: "right", dist: (1 - x) * dielineWidth });
|
|
56
|
+
if (distances.length === 0)
|
|
57
|
+
return { x, y };
|
|
58
|
+
// Find nearest
|
|
59
|
+
distances.sort((a, b) => a.dist - b.dist);
|
|
60
|
+
const nearest = distances[0].edge;
|
|
61
|
+
let newX = x;
|
|
62
|
+
let newY = y;
|
|
63
|
+
const fw = feature.width || 0;
|
|
64
|
+
const fh = feature.height || 0;
|
|
65
|
+
// Snap to edge
|
|
66
|
+
switch (nearest) {
|
|
67
|
+
case "top":
|
|
68
|
+
newY = 0 + offset / dielineHeight;
|
|
69
|
+
if (confine) {
|
|
70
|
+
const minX = (fw / 2) / dielineWidth;
|
|
71
|
+
const maxX = 1 - minX;
|
|
72
|
+
newX = Math.max(minX, Math.min(newX, maxX));
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
case "bottom":
|
|
76
|
+
newY = 1 - offset / dielineHeight;
|
|
77
|
+
if (confine) {
|
|
78
|
+
const minX = (fw / 2) / dielineWidth;
|
|
79
|
+
const maxX = 1 - minX;
|
|
80
|
+
newX = Math.max(minX, Math.min(newX, maxX));
|
|
81
|
+
}
|
|
82
|
+
break;
|
|
83
|
+
case "left":
|
|
84
|
+
newX = 0 + offset / dielineWidth;
|
|
85
|
+
if (confine) {
|
|
86
|
+
const minY = (fh / 2) / dielineHeight;
|
|
87
|
+
const maxY = 1 - minY;
|
|
88
|
+
newY = Math.max(minY, Math.min(newY, maxY));
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
case "right":
|
|
92
|
+
newX = 1 - offset / dielineWidth;
|
|
93
|
+
if (confine) {
|
|
94
|
+
const minY = (fh / 2) / dielineHeight;
|
|
95
|
+
const maxY = 1 - minY;
|
|
96
|
+
newY = Math.max(minY, Math.min(newY, maxY));
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
return { x: newX, y: newY };
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Internal Constraint Strategy
|
|
104
|
+
* Keeps the feature strictly inside the dieline bounds with optional margin.
|
|
105
|
+
* Params:
|
|
106
|
+
* - margin: number (default: 0) - physical margin
|
|
107
|
+
*/
|
|
108
|
+
const internalConstraint = (x, y, feature, context) => {
|
|
109
|
+
const { dielineWidth, dielineHeight } = context;
|
|
110
|
+
const params = feature.constraints?.params || {};
|
|
111
|
+
const margin = params.margin || 0;
|
|
112
|
+
const fw = feature.width || 0;
|
|
113
|
+
const fh = feature.height || 0;
|
|
114
|
+
const minX = (margin + fw / 2) / dielineWidth;
|
|
115
|
+
const maxX = 1 - (margin + fw / 2) / dielineWidth;
|
|
116
|
+
const minY = (margin + fh / 2) / dielineHeight;
|
|
117
|
+
const maxY = 1 - (margin + fh / 2) / dielineHeight;
|
|
118
|
+
// Handle case where feature is larger than container
|
|
119
|
+
const clampedX = minX > maxX ? 0.5 : Math.max(minX, Math.min(x, maxX));
|
|
120
|
+
const clampedY = minY > maxY ? 0.5 : Math.max(minY, Math.min(y, maxY));
|
|
121
|
+
return { x: clampedX, y: clampedY };
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Bottom Tangent Strategy (stand protrusion)
|
|
125
|
+
* Forces a feature to be tangent to the dieline bottom edge from outside (below).
|
|
126
|
+
* Params:
|
|
127
|
+
* - gap: number (mm, default 0) extra clearance between dieline and protrusion
|
|
128
|
+
* - confineX: boolean (default true) keep feature within left/right bounds
|
|
129
|
+
*/
|
|
130
|
+
const tangentBottomConstraint = (x, y, feature, context) => {
|
|
131
|
+
const { dielineWidth, dielineHeight } = context;
|
|
132
|
+
const params = feature.constraints?.params || {};
|
|
133
|
+
const gap = params.gap || 0;
|
|
134
|
+
const confineX = params.confineX !== false;
|
|
135
|
+
const extentY = feature.shape === "circle"
|
|
136
|
+
? feature.radius || 0
|
|
137
|
+
: (feature.height || 0) / 2;
|
|
138
|
+
const newY = 1 + (extentY + gap) / dielineHeight;
|
|
139
|
+
let newX = x;
|
|
140
|
+
if (confineX) {
|
|
141
|
+
const extentX = feature.shape === "circle"
|
|
142
|
+
? feature.radius || 0
|
|
143
|
+
: (feature.width || 0) / 2;
|
|
144
|
+
const minX = extentX / dielineWidth;
|
|
145
|
+
const maxX = 1 - extentX / dielineWidth;
|
|
146
|
+
newX = minX > maxX ? 0.5 : Math.max(minX, Math.min(newX, maxX));
|
|
147
|
+
}
|
|
148
|
+
return { x: newX, y: newY };
|
|
149
|
+
};
|
|
150
|
+
// Register built-ins
|
|
151
|
+
ConstraintRegistry.register("edge", edgeConstraint);
|
|
152
|
+
ConstraintRegistry.register("internal", internalConstraint);
|
|
153
|
+
ConstraintRegistry.register("tangent-bottom", tangentBottomConstraint);
|
|
@@ -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;
|