@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.
- package/.test-dist/src/coordinate.js +74 -0
- package/.test-dist/src/extensions/background.js +547 -0
- package/.test-dist/src/extensions/bridgeSelection.js +20 -0
- package/.test-dist/src/extensions/constraints.js +237 -0
- package/.test-dist/src/extensions/dieline.js +931 -0
- package/.test-dist/src/extensions/dielineShape.js +66 -0
- package/.test-dist/src/extensions/edgeScale.js +12 -0
- package/.test-dist/src/extensions/feature.js +910 -0
- package/.test-dist/src/extensions/featureComplete.js +32 -0
- package/.test-dist/src/extensions/film.js +226 -0
- package/.test-dist/src/extensions/geometry.js +609 -0
- package/.test-dist/src/extensions/image.js +1613 -0
- package/.test-dist/src/extensions/index.js +28 -0
- package/.test-dist/src/extensions/maskOps.js +334 -0
- package/.test-dist/src/extensions/mirror.js +104 -0
- package/.test-dist/src/extensions/ruler.js +442 -0
- package/.test-dist/src/extensions/sceneLayout.js +96 -0
- package/.test-dist/src/extensions/sceneLayoutModel.js +202 -0
- package/.test-dist/src/extensions/sceneVisibility.js +55 -0
- package/.test-dist/src/extensions/size.js +331 -0
- package/.test-dist/src/extensions/tracer.js +709 -0
- package/.test-dist/src/extensions/white-ink.js +1200 -0
- package/.test-dist/src/extensions/wrappedOffsets.js +33 -0
- package/.test-dist/src/index.js +18 -0
- package/.test-dist/src/services/CanvasService.js +1011 -0
- package/.test-dist/src/services/ViewportSystem.js +76 -0
- package/.test-dist/src/services/index.js +25 -0
- package/.test-dist/src/services/renderSpec.js +2 -0
- package/.test-dist/src/services/visibility.js +54 -0
- package/.test-dist/src/units.js +30 -0
- package/.test-dist/tests/run.js +148 -0
- package/CHANGELOG.md +6 -0
- package/dist/index.d.mts +150 -62
- package/dist/index.d.ts +150 -62
- package/dist/index.js +2219 -1714
- package/dist/index.mjs +2226 -1718
- package/package.json +1 -1
- package/src/coordinate.ts +106 -106
- package/src/extensions/background.ts +716 -323
- package/src/extensions/bridgeSelection.ts +17 -17
- package/src/extensions/constraints.ts +322 -322
- package/src/extensions/dieline.ts +1169 -1149
- package/src/extensions/dielineShape.ts +109 -109
- package/src/extensions/edgeScale.ts +19 -19
- package/src/extensions/feature.ts +1140 -1137
- package/src/extensions/featureComplete.ts +46 -46
- package/src/extensions/film.ts +270 -266
- package/src/extensions/geometry.ts +851 -885
- package/src/extensions/image.ts +2007 -2054
- package/src/extensions/index.ts +10 -11
- package/src/extensions/maskOps.ts +283 -283
- package/src/extensions/mirror.ts +128 -128
- package/src/extensions/ruler.ts +664 -654
- package/src/extensions/sceneLayout.ts +140 -140
- package/src/extensions/sceneLayoutModel.ts +364 -364
- package/src/extensions/size.ts +389 -389
- package/src/extensions/tracer.ts +1019 -1019
- package/src/extensions/white-ink.ts +1508 -1575
- package/src/extensions/wrappedOffsets.ts +33 -33
- package/src/index.ts +2 -2
- package/src/services/CanvasService.ts +1286 -832
- package/src/services/ViewportSystem.ts +95 -95
- package/src/services/index.ts +4 -3
- package/src/services/renderSpec.ts +83 -53
- package/src/services/visibility.ts +78 -0
- package/src/units.ts +27 -27
- package/tests/run.ts +253 -118
- package/tsconfig.test.json +15 -15
- package/src/extensions/sceneVisibility.ts +0 -64
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.resolveFeaturePosition = resolveFeaturePosition;
|
|
7
|
+
exports.generateDielinePath = generateDielinePath;
|
|
8
|
+
exports.generateBleedZonePath = generateBleedZonePath;
|
|
9
|
+
exports.getLowestPointOnDieline = getLowestPointOnDieline;
|
|
10
|
+
exports.getNearestPointOnDieline = getNearestPointOnDieline;
|
|
11
|
+
exports.getPathBounds = getPathBounds;
|
|
12
|
+
const paper_1 = __importDefault(require("paper"));
|
|
13
|
+
const bridgeSelection_1 = require("./bridgeSelection");
|
|
14
|
+
const dielineShape_1 = require("./dielineShape");
|
|
15
|
+
const wrappedOffsets_1 = require("./wrappedOffsets");
|
|
16
|
+
/**
|
|
17
|
+
* Resolves the absolute position of a feature based on normalized coordinates.
|
|
18
|
+
*/
|
|
19
|
+
function resolveFeaturePosition(feature, geometry) {
|
|
20
|
+
const { x, y, width, height } = geometry;
|
|
21
|
+
// geometry.x/y is the Center.
|
|
22
|
+
const left = x - width / 2;
|
|
23
|
+
const top = y - height / 2;
|
|
24
|
+
return {
|
|
25
|
+
x: left + feature.x * width,
|
|
26
|
+
y: top + feature.y * height,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Initializes paper.js project if not already initialized.
|
|
31
|
+
*/
|
|
32
|
+
function ensurePaper(width, height) {
|
|
33
|
+
if (!paper_1.default.project) {
|
|
34
|
+
paper_1.default.setup(new paper_1.default.Size(width, height));
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
paper_1.default.view.viewSize = new paper_1.default.Size(width, height);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const isBridgeDebugEnabled = () => Boolean(globalThis.__POODER_BRIDGE_DEBUG__);
|
|
41
|
+
function normalizePathItem(shape) {
|
|
42
|
+
let result = shape;
|
|
43
|
+
if (typeof result.resolveCrossings === "function")
|
|
44
|
+
result = result.resolveCrossings();
|
|
45
|
+
if (typeof result.reduce === "function")
|
|
46
|
+
result = result.reduce({});
|
|
47
|
+
if (typeof result.reorient === "function")
|
|
48
|
+
result = result.reorient(true, true);
|
|
49
|
+
if (typeof result.reduce === "function")
|
|
50
|
+
result = result.reduce({});
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
function getBridgeDelta(itemBounds, overlap) {
|
|
54
|
+
return Math.max(overlap, Math.min(5, Math.max(1, itemBounds.height * 0.02)));
|
|
55
|
+
}
|
|
56
|
+
function getExitHit(args) {
|
|
57
|
+
const { mainShape, x, bridgeBottom, toY, eps, delta, overlap, op } = args;
|
|
58
|
+
const ray = new paper_1.default.Path.Line({
|
|
59
|
+
from: [x, bridgeBottom],
|
|
60
|
+
to: [x, toY],
|
|
61
|
+
insert: false,
|
|
62
|
+
});
|
|
63
|
+
const intersections = mainShape.getIntersections(ray) || [];
|
|
64
|
+
ray.remove();
|
|
65
|
+
const validHits = intersections.filter((i) => i.point.y < bridgeBottom - eps);
|
|
66
|
+
if (validHits.length === 0)
|
|
67
|
+
return null;
|
|
68
|
+
validHits.sort((a, b) => b.point.y - a.point.y);
|
|
69
|
+
const flags = validHits.map((h) => {
|
|
70
|
+
const above = h.point.add(new paper_1.default.Point(0, -delta));
|
|
71
|
+
const below = h.point.add(new paper_1.default.Point(0, delta));
|
|
72
|
+
return {
|
|
73
|
+
insideAbove: mainShape.contains(above),
|
|
74
|
+
insideBelow: mainShape.contains(below),
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
const idx = (0, bridgeSelection_1.pickExitIndex)(flags);
|
|
78
|
+
if (idx < 0)
|
|
79
|
+
return null;
|
|
80
|
+
if (isBridgeDebugEnabled()) {
|
|
81
|
+
console.debug("Geometry: Bridge ray", {
|
|
82
|
+
x,
|
|
83
|
+
validHits: validHits.length,
|
|
84
|
+
idx,
|
|
85
|
+
delta,
|
|
86
|
+
overlap,
|
|
87
|
+
op,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const hit = validHits[idx];
|
|
91
|
+
return { point: hit.point, location: hit };
|
|
92
|
+
}
|
|
93
|
+
function selectOuterChain(args) {
|
|
94
|
+
const { mainShape, pointsA, pointsB, delta, overlap, op } = args;
|
|
95
|
+
const scoreA = (0, bridgeSelection_1.scoreOutsideAbove)(pointsA.map((p) => ({
|
|
96
|
+
outsideAbove: !mainShape.contains(p.add(new paper_1.default.Point(0, -delta))),
|
|
97
|
+
})));
|
|
98
|
+
const scoreB = (0, bridgeSelection_1.scoreOutsideAbove)(pointsB.map((p) => ({
|
|
99
|
+
outsideAbove: !mainShape.contains(p.add(new paper_1.default.Point(0, -delta))),
|
|
100
|
+
})));
|
|
101
|
+
const ratioA = scoreA / pointsA.length;
|
|
102
|
+
const ratioB = scoreB / pointsB.length;
|
|
103
|
+
if (isBridgeDebugEnabled()) {
|
|
104
|
+
console.debug("Geometry: Bridge chain", {
|
|
105
|
+
scoreA,
|
|
106
|
+
scoreB,
|
|
107
|
+
lenA: pointsA.length,
|
|
108
|
+
lenB: pointsB.length,
|
|
109
|
+
ratioA,
|
|
110
|
+
ratioB,
|
|
111
|
+
delta,
|
|
112
|
+
overlap,
|
|
113
|
+
op,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const ratioEps = 1e-6;
|
|
117
|
+
if (Math.abs(ratioA - ratioB) > ratioEps) {
|
|
118
|
+
return ratioA > ratioB ? pointsA : pointsB;
|
|
119
|
+
}
|
|
120
|
+
if (scoreA !== scoreB)
|
|
121
|
+
return scoreA > scoreB ? pointsA : pointsB;
|
|
122
|
+
return pointsA.length <= pointsB.length ? pointsA : pointsB;
|
|
123
|
+
}
|
|
124
|
+
function fitPathItemToRect(item, rect, fitMode) {
|
|
125
|
+
const { left, top, width, height } = rect;
|
|
126
|
+
const bounds = item.bounds;
|
|
127
|
+
if (width <= 0 ||
|
|
128
|
+
height <= 0 ||
|
|
129
|
+
!Number.isFinite(bounds.width) ||
|
|
130
|
+
!Number.isFinite(bounds.height) ||
|
|
131
|
+
bounds.width <= 0 ||
|
|
132
|
+
bounds.height <= 0) {
|
|
133
|
+
item.position = new paper_1.default.Point(left + width / 2, top + height / 2);
|
|
134
|
+
return item;
|
|
135
|
+
}
|
|
136
|
+
item.translate(new paper_1.default.Point(-bounds.left, -bounds.top));
|
|
137
|
+
if (fitMode === "stretch") {
|
|
138
|
+
item.scale(width / bounds.width, height / bounds.height, new paper_1.default.Point(0, 0));
|
|
139
|
+
item.translate(new paper_1.default.Point(left, top));
|
|
140
|
+
return item;
|
|
141
|
+
}
|
|
142
|
+
const uniformScale = Math.min(width / bounds.width, height / bounds.height);
|
|
143
|
+
item.scale(uniformScale, uniformScale, new paper_1.default.Point(0, 0));
|
|
144
|
+
const scaledWidth = bounds.width * uniformScale;
|
|
145
|
+
const scaledHeight = bounds.height * uniformScale;
|
|
146
|
+
item.translate(new paper_1.default.Point(left + (width - scaledWidth) / 2, top + (height - scaledHeight) / 2));
|
|
147
|
+
return item;
|
|
148
|
+
}
|
|
149
|
+
function createNormalizedHeartPath(params) {
|
|
150
|
+
const { lobeSpread, notchDepth, tipSharpness } = params;
|
|
151
|
+
const halfSpread = 0.22 + lobeSpread * 0.18;
|
|
152
|
+
const notchY = 0.06 + notchDepth * 0.2;
|
|
153
|
+
const shoulderY = 0.24 + notchDepth * 0.2;
|
|
154
|
+
const topLift = 0.12 + (1 - notchDepth) * 0.06;
|
|
155
|
+
const topY = notchY - topLift;
|
|
156
|
+
const sideCtrlY = shoulderY - (0.18 - notchDepth * 0.08);
|
|
157
|
+
const lowerCtrlY = 0.58 + (1 - tipSharpness) * 0.16;
|
|
158
|
+
const tipCtrlX = 0.34 - tipSharpness * 0.2;
|
|
159
|
+
const notchCtrlX = 0.06 + lobeSpread * 0.06;
|
|
160
|
+
const lobeCtrlX = 0.1 + lobeSpread * 0.08;
|
|
161
|
+
const notchCtrlY = notchY - topLift * 0.45;
|
|
162
|
+
const xPeakL = 0.5 - halfSpread;
|
|
163
|
+
const xPeakR = 0.5 + halfSpread;
|
|
164
|
+
const heartPath = new paper_1.default.Path({ insert: false });
|
|
165
|
+
heartPath.moveTo(new paper_1.default.Point(0.5, notchY));
|
|
166
|
+
heartPath.cubicCurveTo(new paper_1.default.Point(0.5 - notchCtrlX, notchCtrlY), new paper_1.default.Point(xPeakL + lobeCtrlX, topY), new paper_1.default.Point(xPeakL, topY));
|
|
167
|
+
heartPath.cubicCurveTo(new paper_1.default.Point(xPeakL - lobeCtrlX, topY), new paper_1.default.Point(0, sideCtrlY), new paper_1.default.Point(0, shoulderY));
|
|
168
|
+
heartPath.cubicCurveTo(new paper_1.default.Point(0, lowerCtrlY), new paper_1.default.Point(tipCtrlX, 1), new paper_1.default.Point(0.5, 1));
|
|
169
|
+
heartPath.cubicCurveTo(new paper_1.default.Point(1 - tipCtrlX, 1), new paper_1.default.Point(1, lowerCtrlY), new paper_1.default.Point(1, shoulderY));
|
|
170
|
+
heartPath.cubicCurveTo(new paper_1.default.Point(1, sideCtrlY), new paper_1.default.Point(xPeakR + lobeCtrlX, topY), new paper_1.default.Point(xPeakR, topY));
|
|
171
|
+
heartPath.cubicCurveTo(new paper_1.default.Point(xPeakR - lobeCtrlX, topY), new paper_1.default.Point(0.5 + notchCtrlX, notchCtrlY), new paper_1.default.Point(0.5, notchY));
|
|
172
|
+
heartPath.closed = true;
|
|
173
|
+
return heartPath;
|
|
174
|
+
}
|
|
175
|
+
function createHeartBaseShape(options) {
|
|
176
|
+
const { x, y, width, height } = options;
|
|
177
|
+
const w = Math.max(0, width);
|
|
178
|
+
const h = Math.max(0, height);
|
|
179
|
+
const left = x - w / 2;
|
|
180
|
+
const top = y - h / 2;
|
|
181
|
+
const fitMode = (0, dielineShape_1.getShapeFitMode)(options.shapeStyle);
|
|
182
|
+
const heartParams = (0, dielineShape_1.getHeartShapeParams)(options.shapeStyle);
|
|
183
|
+
const rawHeart = createNormalizedHeartPath(heartParams);
|
|
184
|
+
return fitPathItemToRect(rawHeart, { left, top, width: w, height: h }, fitMode);
|
|
185
|
+
}
|
|
186
|
+
const BUILTIN_SHAPE_BUILDERS = {
|
|
187
|
+
rect: (options) => {
|
|
188
|
+
const { x, y, width, height, radius } = options;
|
|
189
|
+
return new paper_1.default.Path.Rectangle({
|
|
190
|
+
point: [x - width / 2, y - height / 2],
|
|
191
|
+
size: [Math.max(0, width), Math.max(0, height)],
|
|
192
|
+
radius: Math.max(0, radius),
|
|
193
|
+
});
|
|
194
|
+
},
|
|
195
|
+
circle: (options) => {
|
|
196
|
+
const { x, y, width, height } = options;
|
|
197
|
+
const r = Math.min(width, height) / 2;
|
|
198
|
+
return new paper_1.default.Path.Circle({
|
|
199
|
+
center: new paper_1.default.Point(x, y),
|
|
200
|
+
radius: Math.max(0, r),
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
ellipse: (options) => {
|
|
204
|
+
const { x, y, width, height } = options;
|
|
205
|
+
return new paper_1.default.Path.Ellipse({
|
|
206
|
+
center: new paper_1.default.Point(x, y),
|
|
207
|
+
radius: [Math.max(0, width / 2), Math.max(0, height / 2)],
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
heart: createHeartBaseShape,
|
|
211
|
+
};
|
|
212
|
+
function createCustomBaseShape(options) {
|
|
213
|
+
const { pathData, customSourceWidthPx, customSourceHeightPx, x, y, width, height, } = options;
|
|
214
|
+
if (typeof pathData !== "string" || pathData.trim().length === 0) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
const center = new paper_1.default.Point(x, y);
|
|
218
|
+
const hasMultipleSubPaths = ((pathData.match(/[Mm]/g) || []).length ?? 0) > 1;
|
|
219
|
+
const path = hasMultipleSubPaths
|
|
220
|
+
? new paper_1.default.CompoundPath(pathData)
|
|
221
|
+
: (() => {
|
|
222
|
+
const single = new paper_1.default.Path();
|
|
223
|
+
single.pathData = pathData;
|
|
224
|
+
return single;
|
|
225
|
+
})();
|
|
226
|
+
const sourceWidth = Number(customSourceWidthPx ?? 0);
|
|
227
|
+
const sourceHeight = Number(customSourceHeightPx ?? 0);
|
|
228
|
+
if (Number.isFinite(sourceWidth) &&
|
|
229
|
+
Number.isFinite(sourceHeight) &&
|
|
230
|
+
sourceWidth > 0 &&
|
|
231
|
+
sourceHeight > 0 &&
|
|
232
|
+
width > 0 &&
|
|
233
|
+
height > 0) {
|
|
234
|
+
// Preserve original detect-space offset/expand by mapping source image
|
|
235
|
+
// coordinates directly into the target dieline frame.
|
|
236
|
+
const targetLeft = x - width / 2;
|
|
237
|
+
const targetTop = y - height / 2;
|
|
238
|
+
path.scale(width / sourceWidth, height / sourceHeight, new paper_1.default.Point(0, 0));
|
|
239
|
+
path.translate(new paper_1.default.Point(targetLeft, targetTop));
|
|
240
|
+
return path;
|
|
241
|
+
}
|
|
242
|
+
if (width > 0 && height > 0 && path.bounds.width > 0 && path.bounds.height > 0) {
|
|
243
|
+
// Fallback for malformed custom-path metadata.
|
|
244
|
+
path.position = center;
|
|
245
|
+
path.scale(width / path.bounds.width, height / path.bounds.height);
|
|
246
|
+
return path;
|
|
247
|
+
}
|
|
248
|
+
path.position = center;
|
|
249
|
+
return path;
|
|
250
|
+
}
|
|
251
|
+
function createBaseShape(options) {
|
|
252
|
+
const { shape } = options;
|
|
253
|
+
if (shape === "custom") {
|
|
254
|
+
const customShape = createCustomBaseShape(options);
|
|
255
|
+
if (customShape)
|
|
256
|
+
return customShape;
|
|
257
|
+
return BUILTIN_SHAPE_BUILDERS[dielineShape_1.DEFAULT_DIELINE_SHAPE](options);
|
|
258
|
+
}
|
|
259
|
+
return BUILTIN_SHAPE_BUILDERS[shape](options);
|
|
260
|
+
}
|
|
261
|
+
function resolveBridgeBasePath(shape, anchor) {
|
|
262
|
+
if (shape instanceof paper_1.default.Path) {
|
|
263
|
+
return shape;
|
|
264
|
+
}
|
|
265
|
+
if (shape instanceof paper_1.default.CompoundPath) {
|
|
266
|
+
const children = (shape.children || []).filter((child) => child instanceof paper_1.default.Path);
|
|
267
|
+
if (!children.length)
|
|
268
|
+
return null;
|
|
269
|
+
let best = children[0];
|
|
270
|
+
let bestDistance = Infinity;
|
|
271
|
+
for (const child of children) {
|
|
272
|
+
const location = child.getNearestLocation(anchor);
|
|
273
|
+
const point = location?.point;
|
|
274
|
+
if (!point)
|
|
275
|
+
continue;
|
|
276
|
+
const distance = point.getDistance(anchor);
|
|
277
|
+
if (distance < bestDistance) {
|
|
278
|
+
bestDistance = distance;
|
|
279
|
+
best = child;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return best;
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Creates a Paper.js Item for a single feature.
|
|
288
|
+
*/
|
|
289
|
+
function createFeatureItem(feature, center) {
|
|
290
|
+
let item;
|
|
291
|
+
if (feature.shape === "rect") {
|
|
292
|
+
const w = feature.width || 10;
|
|
293
|
+
const h = feature.height || 10;
|
|
294
|
+
const r = feature.radius || 0;
|
|
295
|
+
item = new paper_1.default.Path.Rectangle({
|
|
296
|
+
point: [center.x - w / 2, center.y - h / 2],
|
|
297
|
+
size: [w, h],
|
|
298
|
+
radius: r,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
// Circle
|
|
303
|
+
const r = feature.radius || 5;
|
|
304
|
+
item = new paper_1.default.Path.Circle({
|
|
305
|
+
center: center,
|
|
306
|
+
radius: r,
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
if (feature.rotation) {
|
|
310
|
+
item.rotate(feature.rotation, center);
|
|
311
|
+
}
|
|
312
|
+
return item;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Internal helper to generate the Perimeter Shape (Base + Edge Features).
|
|
316
|
+
*/
|
|
317
|
+
function getPerimeterShape(options) {
|
|
318
|
+
// 1. Create Base Shape
|
|
319
|
+
let mainShape = createBaseShape(options);
|
|
320
|
+
const { features } = options;
|
|
321
|
+
if (features && features.length > 0) {
|
|
322
|
+
// Filter for Edge Features (Default is Edge, unless explicit 'surface')
|
|
323
|
+
const edgeFeatures = features.filter((f) => !f.renderBehavior || f.renderBehavior === "edge");
|
|
324
|
+
const adds = [];
|
|
325
|
+
const subtracts = [];
|
|
326
|
+
edgeFeatures.forEach((f) => {
|
|
327
|
+
const pos = resolveFeaturePosition(f, options);
|
|
328
|
+
const center = new paper_1.default.Point(pos.x, pos.y);
|
|
329
|
+
const item = createFeatureItem(f, center);
|
|
330
|
+
// Handle Bridge logic: Create a connection shape to the main body
|
|
331
|
+
if (f.bridge && f.bridge.type === "vertical") {
|
|
332
|
+
const itemBounds = item.bounds;
|
|
333
|
+
const mainBounds = mainShape.bounds;
|
|
334
|
+
const bridgeTop = mainBounds.top;
|
|
335
|
+
const bridgeBottom = itemBounds.top;
|
|
336
|
+
if (bridgeBottom > bridgeTop) {
|
|
337
|
+
const overlap = 2;
|
|
338
|
+
const rayPadding = 10;
|
|
339
|
+
const eps = 0.1;
|
|
340
|
+
const delta = getBridgeDelta(itemBounds, overlap);
|
|
341
|
+
const toY = bridgeTop - rayPadding;
|
|
342
|
+
const inset = Math.min(1, Math.max(0, itemBounds.width * 0.01));
|
|
343
|
+
const xLeft = itemBounds.left + inset;
|
|
344
|
+
const xRight = itemBounds.right - inset;
|
|
345
|
+
const bridgeBasePath = resolveBridgeBasePath(mainShape, center);
|
|
346
|
+
const canBridge = !!bridgeBasePath && xRight - xLeft > eps;
|
|
347
|
+
if (canBridge && bridgeBasePath) {
|
|
348
|
+
const leftHit = getExitHit({
|
|
349
|
+
mainShape: bridgeBasePath,
|
|
350
|
+
x: xLeft,
|
|
351
|
+
bridgeBottom,
|
|
352
|
+
toY,
|
|
353
|
+
eps,
|
|
354
|
+
delta,
|
|
355
|
+
overlap,
|
|
356
|
+
op: f.operation,
|
|
357
|
+
});
|
|
358
|
+
const rightHit = getExitHit({
|
|
359
|
+
mainShape: bridgeBasePath,
|
|
360
|
+
x: xRight,
|
|
361
|
+
bridgeBottom,
|
|
362
|
+
toY,
|
|
363
|
+
eps,
|
|
364
|
+
delta,
|
|
365
|
+
overlap,
|
|
366
|
+
op: f.operation,
|
|
367
|
+
});
|
|
368
|
+
if (leftHit && rightHit) {
|
|
369
|
+
const pathLength = bridgeBasePath.length;
|
|
370
|
+
const leftOffset = leftHit.location.offset;
|
|
371
|
+
const rightOffset = rightHit.location.offset;
|
|
372
|
+
const distanceA = (0, wrappedOffsets_1.wrappedDistance)(pathLength, leftOffset, rightOffset);
|
|
373
|
+
const distanceB = (0, wrappedOffsets_1.wrappedDistance)(pathLength, rightOffset, leftOffset);
|
|
374
|
+
const countFor = (d) => Math.max(8, Math.min(80, Math.ceil(d / 6)));
|
|
375
|
+
const offsetsA = (0, wrappedOffsets_1.sampleWrappedOffsets)(pathLength, leftOffset, rightOffset, countFor(distanceA));
|
|
376
|
+
const offsetsB = (0, wrappedOffsets_1.sampleWrappedOffsets)(pathLength, rightOffset, leftOffset, countFor(distanceB));
|
|
377
|
+
const pointsA = offsetsA
|
|
378
|
+
.map((o) => bridgeBasePath.getPointAt(o))
|
|
379
|
+
.filter((p) => Boolean(p));
|
|
380
|
+
const pointsB = offsetsB
|
|
381
|
+
.map((o) => bridgeBasePath.getPointAt(o))
|
|
382
|
+
.filter((p) => Boolean(p));
|
|
383
|
+
if (pointsA.length >= 2 && pointsB.length >= 2) {
|
|
384
|
+
let topBase = selectOuterChain({
|
|
385
|
+
mainShape: bridgeBasePath,
|
|
386
|
+
pointsA,
|
|
387
|
+
pointsB,
|
|
388
|
+
delta,
|
|
389
|
+
overlap,
|
|
390
|
+
op: f.operation,
|
|
391
|
+
});
|
|
392
|
+
const dist2 = (a, b) => {
|
|
393
|
+
const dx = a.x - b.x;
|
|
394
|
+
const dy = a.y - b.y;
|
|
395
|
+
return dx * dx + dy * dy;
|
|
396
|
+
};
|
|
397
|
+
if (dist2(topBase[0], leftHit.point) >
|
|
398
|
+
dist2(topBase[0], rightHit.point)) {
|
|
399
|
+
topBase = topBase.slice().reverse();
|
|
400
|
+
}
|
|
401
|
+
topBase = topBase.slice();
|
|
402
|
+
topBase[0] = leftHit.point;
|
|
403
|
+
topBase[topBase.length - 1] = rightHit.point;
|
|
404
|
+
const capShiftY = f.operation === "subtract"
|
|
405
|
+
? -Math.max(overlap * 2, delta)
|
|
406
|
+
: overlap;
|
|
407
|
+
const topPoints = topBase.map((p) => p.add(new paper_1.default.Point(0, capShiftY)));
|
|
408
|
+
const bridgeBottomY = bridgeBottom + overlap * 2;
|
|
409
|
+
const bridgePoly = new paper_1.default.Path({ insert: false });
|
|
410
|
+
for (const p of topPoints)
|
|
411
|
+
bridgePoly.add(p);
|
|
412
|
+
bridgePoly.add(new paper_1.default.Point(xRight, bridgeBottomY));
|
|
413
|
+
bridgePoly.add(new paper_1.default.Point(xLeft, bridgeBottomY));
|
|
414
|
+
bridgePoly.closed = true;
|
|
415
|
+
const unitedItem = item.unite(bridgePoly);
|
|
416
|
+
item.remove();
|
|
417
|
+
bridgePoly.remove();
|
|
418
|
+
if (f.operation === "add") {
|
|
419
|
+
adds.push(unitedItem);
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
subtracts.push(unitedItem);
|
|
423
|
+
}
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (f.operation === "add") {
|
|
429
|
+
adds.push(item);
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
subtracts.push(item);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
if (f.operation === "add") {
|
|
437
|
+
adds.push(item);
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
subtracts.push(item);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
else {
|
|
445
|
+
if (f.operation === "add") {
|
|
446
|
+
adds.push(item);
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
subtracts.push(item);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
// 2. Process Additions (Union)
|
|
454
|
+
if (adds.length > 0) {
|
|
455
|
+
for (const item of adds) {
|
|
456
|
+
try {
|
|
457
|
+
const temp = mainShape.unite(item);
|
|
458
|
+
mainShape.remove();
|
|
459
|
+
item.remove();
|
|
460
|
+
mainShape = normalizePathItem(temp);
|
|
461
|
+
}
|
|
462
|
+
catch (e) {
|
|
463
|
+
console.error("Geometry: Failed to unite feature", e);
|
|
464
|
+
item.remove();
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
// 3. Process Subtractions (Difference)
|
|
469
|
+
if (subtracts.length > 0) {
|
|
470
|
+
for (const item of subtracts) {
|
|
471
|
+
try {
|
|
472
|
+
const temp = mainShape.subtract(item);
|
|
473
|
+
mainShape.remove();
|
|
474
|
+
item.remove();
|
|
475
|
+
mainShape = normalizePathItem(temp);
|
|
476
|
+
}
|
|
477
|
+
catch (e) {
|
|
478
|
+
console.error("Geometry: Failed to subtract feature", e);
|
|
479
|
+
item.remove();
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return mainShape;
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Applies Internal/Surface features to a shape.
|
|
488
|
+
*/
|
|
489
|
+
function applySurfaceFeatures(shape, features, options) {
|
|
490
|
+
const surfaceFeatures = features.filter((f) => f.renderBehavior === "surface");
|
|
491
|
+
if (surfaceFeatures.length === 0)
|
|
492
|
+
return shape;
|
|
493
|
+
let result = shape;
|
|
494
|
+
// Internal features are usually subtractive (holes)
|
|
495
|
+
// But we support 'add' too (islands? maybe just unite)
|
|
496
|
+
for (const f of surfaceFeatures) {
|
|
497
|
+
const pos = resolveFeaturePosition(f, options);
|
|
498
|
+
const center = new paper_1.default.Point(pos.x, pos.y);
|
|
499
|
+
const item = createFeatureItem(f, center);
|
|
500
|
+
try {
|
|
501
|
+
if (f.operation === "add") {
|
|
502
|
+
const temp = result.unite(item);
|
|
503
|
+
result.remove();
|
|
504
|
+
item.remove();
|
|
505
|
+
result = normalizePathItem(temp);
|
|
506
|
+
}
|
|
507
|
+
else {
|
|
508
|
+
const temp = result.subtract(item);
|
|
509
|
+
result.remove();
|
|
510
|
+
item.remove();
|
|
511
|
+
result = normalizePathItem(temp);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
catch (e) {
|
|
515
|
+
console.error("Geometry: Failed to apply surface feature", e);
|
|
516
|
+
item.remove();
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
return result;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Generates the path data for the Dieline (Product Shape).
|
|
523
|
+
*/
|
|
524
|
+
function generateDielinePath(options) {
|
|
525
|
+
const paperWidth = options.canvasWidth || options.width * 2 || 2000;
|
|
526
|
+
const paperHeight = options.canvasHeight || options.height * 2 || 2000;
|
|
527
|
+
ensurePaper(paperWidth, paperHeight);
|
|
528
|
+
paper_1.default.project.activeLayer.removeChildren();
|
|
529
|
+
const perimeter = getPerimeterShape(options);
|
|
530
|
+
const finalShape = applySurfaceFeatures(perimeter, options.features, options);
|
|
531
|
+
const pathData = finalShape.pathData;
|
|
532
|
+
finalShape.remove();
|
|
533
|
+
return pathData;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Generates the path data for the Bleed Zone.
|
|
537
|
+
*/
|
|
538
|
+
function generateBleedZonePath(originalOptions, offsetOptions, offset) {
|
|
539
|
+
const paperWidth = originalOptions.canvasWidth || originalOptions.width * 2 || 2000;
|
|
540
|
+
const paperHeight = originalOptions.canvasHeight || originalOptions.height * 2 || 2000;
|
|
541
|
+
ensurePaper(paperWidth, paperHeight);
|
|
542
|
+
paper_1.default.project.activeLayer.removeChildren();
|
|
543
|
+
// 1. Generate Original Shape
|
|
544
|
+
const pOriginal = getPerimeterShape(originalOptions);
|
|
545
|
+
const shapeOriginal = applySurfaceFeatures(pOriginal, originalOptions.features, originalOptions);
|
|
546
|
+
// 2. Generate Offset Shape
|
|
547
|
+
const pOffset = getPerimeterShape(offsetOptions);
|
|
548
|
+
const shapeOffset = applySurfaceFeatures(pOffset, offsetOptions.features, offsetOptions);
|
|
549
|
+
// 3. Calculate Difference
|
|
550
|
+
let bleedZone;
|
|
551
|
+
if (offset > 0) {
|
|
552
|
+
bleedZone = shapeOffset.subtract(shapeOriginal);
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
bleedZone = shapeOriginal.subtract(shapeOffset);
|
|
556
|
+
}
|
|
557
|
+
const pathData = bleedZone.pathData;
|
|
558
|
+
shapeOriginal.remove();
|
|
559
|
+
shapeOffset.remove();
|
|
560
|
+
bleedZone.remove();
|
|
561
|
+
return pathData;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Finds the lowest point (Max Y) on the Dieline geometry (Base Shape ONLY).
|
|
565
|
+
*/
|
|
566
|
+
function getLowestPointOnDieline(options) {
|
|
567
|
+
ensurePaper(options.width * 2, options.height * 2);
|
|
568
|
+
paper_1.default.project.activeLayer.removeChildren();
|
|
569
|
+
const shape = createBaseShape(options);
|
|
570
|
+
const bounds = shape.bounds;
|
|
571
|
+
const result = {
|
|
572
|
+
x: bounds.center.x,
|
|
573
|
+
y: bounds.bottom,
|
|
574
|
+
};
|
|
575
|
+
shape.remove();
|
|
576
|
+
return result;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Finds the nearest point on the Dieline geometry (Base Shape ONLY) for a given target point.
|
|
580
|
+
* Used for constraining feature movement.
|
|
581
|
+
*/
|
|
582
|
+
function getNearestPointOnDieline(point, options) {
|
|
583
|
+
ensurePaper(options.width * 2, options.height * 2);
|
|
584
|
+
paper_1.default.project.activeLayer.removeChildren();
|
|
585
|
+
// We constrain to the BASE shape, not including other features,
|
|
586
|
+
// because usually you want to snap to the main edge.
|
|
587
|
+
const shape = createBaseShape(options);
|
|
588
|
+
const p = new paper_1.default.Point(point.x, point.y);
|
|
589
|
+
const location = shape.getNearestLocation(p);
|
|
590
|
+
const result = {
|
|
591
|
+
x: location.point.x,
|
|
592
|
+
y: location.point.y,
|
|
593
|
+
normal: location.normal ? { x: location.normal.x, y: location.normal.y } : undefined
|
|
594
|
+
};
|
|
595
|
+
shape.remove();
|
|
596
|
+
return result;
|
|
597
|
+
}
|
|
598
|
+
function getPathBounds(pathData) {
|
|
599
|
+
const path = new paper_1.default.Path();
|
|
600
|
+
path.pathData = pathData;
|
|
601
|
+
const bounds = path.bounds;
|
|
602
|
+
path.remove();
|
|
603
|
+
return {
|
|
604
|
+
x: bounds.x,
|
|
605
|
+
y: bounds.y,
|
|
606
|
+
width: bounds.width,
|
|
607
|
+
height: bounds.height,
|
|
608
|
+
};
|
|
609
|
+
}
|