@pooder/kit 5.4.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/.test-dist/src/coordinate.js +74 -0
  2. package/.test-dist/src/extensions/background.js +547 -0
  3. package/.test-dist/src/extensions/bridgeSelection.js +20 -0
  4. package/.test-dist/src/extensions/constraints.js +237 -0
  5. package/.test-dist/src/extensions/dieline.js +931 -0
  6. package/.test-dist/src/extensions/dielineShape.js +66 -0
  7. package/.test-dist/src/extensions/edgeScale.js +12 -0
  8. package/.test-dist/src/extensions/feature.js +910 -0
  9. package/.test-dist/src/extensions/featureComplete.js +32 -0
  10. package/.test-dist/src/extensions/film.js +226 -0
  11. package/.test-dist/src/extensions/geometry.js +609 -0
  12. package/.test-dist/src/extensions/image.js +1613 -0
  13. package/.test-dist/src/extensions/index.js +28 -0
  14. package/.test-dist/src/extensions/maskOps.js +334 -0
  15. package/.test-dist/src/extensions/mirror.js +104 -0
  16. package/.test-dist/src/extensions/ruler.js +442 -0
  17. package/.test-dist/src/extensions/sceneLayout.js +96 -0
  18. package/.test-dist/src/extensions/sceneLayoutModel.js +202 -0
  19. package/.test-dist/src/extensions/sceneVisibility.js +55 -0
  20. package/.test-dist/src/extensions/size.js +331 -0
  21. package/.test-dist/src/extensions/tracer.js +709 -0
  22. package/.test-dist/src/extensions/white-ink.js +1200 -0
  23. package/.test-dist/src/extensions/wrappedOffsets.js +33 -0
  24. package/.test-dist/src/index.js +18 -0
  25. package/.test-dist/src/services/CanvasService.js +1011 -0
  26. package/.test-dist/src/services/ViewportSystem.js +76 -0
  27. package/.test-dist/src/services/index.js +25 -0
  28. package/.test-dist/src/services/renderSpec.js +2 -0
  29. package/.test-dist/src/services/visibility.js +54 -0
  30. package/.test-dist/src/units.js +30 -0
  31. package/.test-dist/tests/run.js +148 -0
  32. package/CHANGELOG.md +6 -0
  33. package/dist/index.d.mts +150 -62
  34. package/dist/index.d.ts +150 -62
  35. package/dist/index.js +2219 -1714
  36. package/dist/index.mjs +2226 -1718
  37. package/package.json +1 -1
  38. package/src/coordinate.ts +106 -106
  39. package/src/extensions/background.ts +716 -323
  40. package/src/extensions/bridgeSelection.ts +17 -17
  41. package/src/extensions/constraints.ts +322 -322
  42. package/src/extensions/dieline.ts +1169 -1149
  43. package/src/extensions/dielineShape.ts +109 -109
  44. package/src/extensions/edgeScale.ts +19 -19
  45. package/src/extensions/feature.ts +1140 -1137
  46. package/src/extensions/featureComplete.ts +46 -46
  47. package/src/extensions/film.ts +270 -266
  48. package/src/extensions/geometry.ts +851 -885
  49. package/src/extensions/image.ts +2007 -2054
  50. package/src/extensions/index.ts +10 -11
  51. package/src/extensions/maskOps.ts +283 -283
  52. package/src/extensions/mirror.ts +128 -128
  53. package/src/extensions/ruler.ts +664 -654
  54. package/src/extensions/sceneLayout.ts +140 -140
  55. package/src/extensions/sceneLayoutModel.ts +364 -364
  56. package/src/extensions/size.ts +389 -389
  57. package/src/extensions/tracer.ts +1019 -1019
  58. package/src/extensions/white-ink.ts +1508 -1575
  59. package/src/extensions/wrappedOffsets.ts +33 -33
  60. package/src/index.ts +2 -2
  61. package/src/services/CanvasService.ts +1286 -832
  62. package/src/services/ViewportSystem.ts +95 -95
  63. package/src/services/index.ts +4 -3
  64. package/src/services/renderSpec.ts +83 -53
  65. package/src/services/visibility.ts +78 -0
  66. package/src/units.ts +27 -27
  67. package/tests/run.ts +253 -118
  68. package/tsconfig.test.json +15 -15
  69. package/src/extensions/sceneVisibility.ts +0 -64
@@ -0,0 +1,931 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DielineTool = void 0;
4
+ const core_1 = require("@pooder/core");
5
+ const fabric_1 = require("fabric");
6
+ const tracer_1 = require("./tracer");
7
+ const units_1 = require("../units");
8
+ const dielineShape_1 = require("./dielineShape");
9
+ const geometry_1 = require("./geometry");
10
+ const sceneLayoutModel_1 = require("./sceneLayoutModel");
11
+ const IMAGE_OBJECT_LAYER_ID = "image.user";
12
+ const DIELINE_LAYER_ID = "dieline-overlay";
13
+ class DielineTool {
14
+ constructor(options) {
15
+ this.id = "pooder.kit.dieline";
16
+ this.metadata = {
17
+ name: "DielineTool",
18
+ };
19
+ this.state = {
20
+ shape: dielineShape_1.DEFAULT_DIELINE_SHAPE,
21
+ shapeStyle: { ...dielineShape_1.DEFAULT_DIELINE_SHAPE_STYLE },
22
+ width: 500,
23
+ height: 500,
24
+ radius: 0,
25
+ offset: 0,
26
+ padding: 140,
27
+ mainLine: {
28
+ width: 2.7,
29
+ color: "#FF0000",
30
+ dashLength: 5,
31
+ style: "solid",
32
+ },
33
+ offsetLine: {
34
+ width: 2.7,
35
+ color: "#FF0000",
36
+ dashLength: 5,
37
+ style: "solid",
38
+ },
39
+ insideColor: "rgba(0,0,0,0)",
40
+ showBleedLines: true,
41
+ features: [],
42
+ };
43
+ this.specs = [];
44
+ this.effects = [];
45
+ this.renderSeq = 0;
46
+ this.onCanvasResized = () => {
47
+ this.updateDieline();
48
+ };
49
+ if (options) {
50
+ // Deep merge for styles to avoid overwriting defaults with partial objects
51
+ if (options.mainLine) {
52
+ Object.assign(this.state.mainLine, options.mainLine);
53
+ delete options.mainLine;
54
+ }
55
+ if (options.offsetLine) {
56
+ Object.assign(this.state.offsetLine, options.offsetLine);
57
+ delete options.offsetLine;
58
+ }
59
+ if (options.shapeStyle) {
60
+ this.state.shapeStyle = (0, dielineShape_1.normalizeShapeStyle)(options.shapeStyle, this.state.shapeStyle);
61
+ delete options.shapeStyle;
62
+ }
63
+ Object.assign(this.state, options);
64
+ this.state.shape = (0, dielineShape_1.normalizeDielineShape)(options.shape, this.state.shape);
65
+ }
66
+ }
67
+ activate(context) {
68
+ this.context = context;
69
+ this.canvasService = context.services.get("CanvasService");
70
+ if (!this.canvasService) {
71
+ console.warn("CanvasService not found for DielineTool");
72
+ return;
73
+ }
74
+ this.renderProducerDisposable?.dispose();
75
+ this.renderProducerDisposable = this.canvasService.registerRenderProducer(this.id, () => ({
76
+ passes: [
77
+ {
78
+ id: DIELINE_LAYER_ID,
79
+ stack: 700,
80
+ order: 0,
81
+ replace: true,
82
+ visibility: {
83
+ op: "not",
84
+ expr: {
85
+ op: "activeToolIn",
86
+ ids: ["pooder.kit.image", "pooder.kit.white-ink"],
87
+ },
88
+ },
89
+ effects: this.effects,
90
+ objects: this.specs,
91
+ },
92
+ ],
93
+ }), { priority: 250 });
94
+ const configService = context.services.get("ConfigurationService");
95
+ if (configService) {
96
+ // Load initial config
97
+ const s = this.state;
98
+ const sizeState = (0, sceneLayoutModel_1.readSizeState)(configService);
99
+ s.shape = (0, dielineShape_1.normalizeDielineShape)(configService.get("dieline.shape", s.shape), s.shape);
100
+ s.shapeStyle = (0, dielineShape_1.normalizeShapeStyle)(configService.get("dieline.shapeStyle", s.shapeStyle), s.shapeStyle);
101
+ s.width = sizeState.actualWidthMm;
102
+ s.height = sizeState.actualHeightMm;
103
+ s.radius = (0, units_1.parseLengthToMm)(configService.get("dieline.radius", s.radius), "mm");
104
+ s.padding = sizeState.viewPadding;
105
+ s.offset =
106
+ sizeState.cutMode === "outset"
107
+ ? sizeState.cutMarginMm
108
+ : sizeState.cutMode === "inset"
109
+ ? -sizeState.cutMarginMm
110
+ : 0;
111
+ // Main Line
112
+ s.mainLine.width = configService.get("dieline.strokeWidth", s.mainLine.width);
113
+ s.mainLine.color = configService.get("dieline.strokeColor", s.mainLine.color);
114
+ s.mainLine.dashLength = configService.get("dieline.dashLength", s.mainLine.dashLength);
115
+ s.mainLine.style = configService.get("dieline.style", s.mainLine.style);
116
+ // Offset Line
117
+ s.offsetLine.width = configService.get("dieline.offsetStrokeWidth", s.offsetLine.width);
118
+ s.offsetLine.color = configService.get("dieline.offsetStrokeColor", s.offsetLine.color);
119
+ s.offsetLine.dashLength = configService.get("dieline.offsetDashLength", s.offsetLine.dashLength);
120
+ s.offsetLine.style = configService.get("dieline.offsetStyle", s.offsetLine.style);
121
+ s.insideColor = configService.get("dieline.insideColor", s.insideColor);
122
+ s.showBleedLines = configService.get("dieline.showBleedLines", s.showBleedLines);
123
+ s.features = configService.get("dieline.features", s.features);
124
+ s.pathData = configService.get("dieline.pathData", s.pathData);
125
+ const sourceWidth = Number(configService.get("dieline.customSourceWidthPx", 0));
126
+ const sourceHeight = Number(configService.get("dieline.customSourceHeightPx", 0));
127
+ s.customSourceWidthPx =
128
+ Number.isFinite(sourceWidth) && sourceWidth > 0
129
+ ? sourceWidth
130
+ : undefined;
131
+ s.customSourceHeightPx =
132
+ Number.isFinite(sourceHeight) && sourceHeight > 0
133
+ ? sourceHeight
134
+ : undefined;
135
+ // Listen for changes
136
+ configService.onAnyChange((e) => {
137
+ if (e.key.startsWith("size.")) {
138
+ const nextSize = (0, sceneLayoutModel_1.readSizeState)(configService);
139
+ s.width = nextSize.actualWidthMm;
140
+ s.height = nextSize.actualHeightMm;
141
+ s.padding = nextSize.viewPadding;
142
+ s.offset =
143
+ nextSize.cutMode === "outset"
144
+ ? nextSize.cutMarginMm
145
+ : nextSize.cutMode === "inset"
146
+ ? -nextSize.cutMarginMm
147
+ : 0;
148
+ this.updateDieline();
149
+ return;
150
+ }
151
+ if (e.key.startsWith("dieline.")) {
152
+ switch (e.key) {
153
+ case "dieline.shape":
154
+ s.shape = (0, dielineShape_1.normalizeDielineShape)(e.value, s.shape);
155
+ break;
156
+ case "dieline.shapeStyle":
157
+ s.shapeStyle = (0, dielineShape_1.normalizeShapeStyle)(e.value, s.shapeStyle);
158
+ break;
159
+ case "dieline.radius":
160
+ s.radius = (0, units_1.parseLengthToMm)(e.value, "mm");
161
+ break;
162
+ case "dieline.strokeWidth":
163
+ s.mainLine.width = e.value;
164
+ break;
165
+ case "dieline.strokeColor":
166
+ s.mainLine.color = e.value;
167
+ break;
168
+ case "dieline.dashLength":
169
+ s.mainLine.dashLength = e.value;
170
+ break;
171
+ case "dieline.style":
172
+ s.mainLine.style = e.value;
173
+ break;
174
+ case "dieline.offsetStrokeWidth":
175
+ s.offsetLine.width = e.value;
176
+ break;
177
+ case "dieline.offsetStrokeColor":
178
+ s.offsetLine.color = e.value;
179
+ break;
180
+ case "dieline.offsetDashLength":
181
+ s.offsetLine.dashLength = e.value;
182
+ break;
183
+ case "dieline.offsetStyle":
184
+ s.offsetLine.style = e.value;
185
+ break;
186
+ case "dieline.insideColor":
187
+ s.insideColor = e.value;
188
+ break;
189
+ case "dieline.showBleedLines":
190
+ s.showBleedLines = e.value;
191
+ break;
192
+ case "dieline.features":
193
+ s.features = e.value;
194
+ break;
195
+ case "dieline.pathData":
196
+ s.pathData = e.value;
197
+ break;
198
+ case "dieline.customSourceWidthPx":
199
+ s.customSourceWidthPx =
200
+ Number.isFinite(Number(e.value)) && Number(e.value) > 0
201
+ ? Number(e.value)
202
+ : undefined;
203
+ break;
204
+ case "dieline.customSourceHeightPx":
205
+ s.customSourceHeightPx =
206
+ Number.isFinite(Number(e.value)) && Number(e.value) > 0
207
+ ? Number(e.value)
208
+ : undefined;
209
+ break;
210
+ }
211
+ this.updateDieline();
212
+ }
213
+ });
214
+ }
215
+ context.eventBus.on("canvas:resized", this.onCanvasResized);
216
+ this.updateDieline();
217
+ }
218
+ deactivate(context) {
219
+ context.eventBus.off("canvas:resized", this.onCanvasResized);
220
+ this.renderSeq += 1;
221
+ this.specs = [];
222
+ this.effects = [];
223
+ this.renderProducerDisposable?.dispose();
224
+ this.renderProducerDisposable = undefined;
225
+ if (this.canvasService) {
226
+ void this.canvasService.flushRenderFromProducers();
227
+ }
228
+ this.canvasService = undefined;
229
+ this.context = undefined;
230
+ }
231
+ contribute() {
232
+ const s = this.state;
233
+ return {
234
+ [core_1.ContributionPointIds.TOOLS]: [
235
+ {
236
+ id: this.id,
237
+ name: "Dieline",
238
+ interaction: "session",
239
+ session: {
240
+ autoBegin: false,
241
+ leavePolicy: "block",
242
+ },
243
+ },
244
+ ],
245
+ [core_1.ContributionPointIds.CONFIGURATIONS]: [
246
+ {
247
+ id: "dieline.shape",
248
+ type: "select",
249
+ label: "Shape",
250
+ options: Array.from(dielineShape_1.DIELINE_SHAPES),
251
+ default: s.shape,
252
+ },
253
+ {
254
+ id: "dieline.radius",
255
+ type: "number",
256
+ label: "Corner Radius (mm)",
257
+ min: 0,
258
+ max: 500,
259
+ default: s.radius,
260
+ },
261
+ {
262
+ id: "dieline.shapeStyle",
263
+ type: "json",
264
+ label: "Shape Style",
265
+ default: s.shapeStyle,
266
+ },
267
+ {
268
+ id: "dieline.showBleedLines",
269
+ type: "boolean",
270
+ label: "Show Bleed Lines",
271
+ default: s.showBleedLines,
272
+ },
273
+ {
274
+ id: "dieline.strokeWidth",
275
+ type: "number",
276
+ label: "Line Width",
277
+ min: 0.1,
278
+ max: 10,
279
+ step: 0.1,
280
+ default: s.mainLine.width,
281
+ },
282
+ {
283
+ id: "dieline.strokeColor",
284
+ type: "color",
285
+ label: "Line Color",
286
+ default: s.mainLine.color,
287
+ },
288
+ {
289
+ id: "dieline.dashLength",
290
+ type: "number",
291
+ label: "Dash Length",
292
+ min: 1,
293
+ max: 50,
294
+ default: s.mainLine.dashLength,
295
+ },
296
+ {
297
+ id: "dieline.style",
298
+ type: "select",
299
+ label: "Line Style",
300
+ options: ["solid", "dashed", "hidden"],
301
+ default: s.mainLine.style,
302
+ },
303
+ {
304
+ id: "dieline.offsetStrokeWidth",
305
+ type: "number",
306
+ label: "Offset Line Width",
307
+ min: 0.1,
308
+ max: 10,
309
+ step: 0.1,
310
+ default: s.offsetLine.width,
311
+ },
312
+ {
313
+ id: "dieline.offsetStrokeColor",
314
+ type: "color",
315
+ label: "Offset Line Color",
316
+ default: s.offsetLine.color,
317
+ },
318
+ {
319
+ id: "dieline.offsetDashLength",
320
+ type: "number",
321
+ label: "Offset Dash Length",
322
+ min: 1,
323
+ max: 50,
324
+ default: s.offsetLine.dashLength,
325
+ },
326
+ {
327
+ id: "dieline.offsetStyle",
328
+ type: "select",
329
+ label: "Offset Line Style",
330
+ options: ["solid", "dashed", "hidden"],
331
+ default: s.offsetLine.style,
332
+ },
333
+ {
334
+ id: "dieline.insideColor",
335
+ type: "color",
336
+ label: "Inside Color",
337
+ default: s.insideColor,
338
+ },
339
+ {
340
+ id: "dieline.features",
341
+ type: "json",
342
+ label: "Edge Features",
343
+ default: s.features,
344
+ },
345
+ ],
346
+ [core_1.ContributionPointIds.COMMANDS]: [
347
+ {
348
+ command: "updateFeaturePosition",
349
+ title: "Update Feature Position",
350
+ handler: (groupId, x, y) => {
351
+ const configService = this.context?.services.get("ConfigurationService");
352
+ if (!configService)
353
+ return;
354
+ const features = configService.get("dieline.features") || [];
355
+ let changed = false;
356
+ const newFeatures = features.map((f) => {
357
+ if (f.groupId === groupId) {
358
+ if (f.x !== x || f.y !== y) {
359
+ changed = true;
360
+ return { ...f, x, y };
361
+ }
362
+ }
363
+ return f;
364
+ });
365
+ if (changed) {
366
+ configService.update("dieline.features", newFeatures);
367
+ }
368
+ },
369
+ },
370
+ {
371
+ command: "exportCutImage",
372
+ title: "Export Cut Image",
373
+ handler: (options) => {
374
+ return this.exportCutImage(options);
375
+ },
376
+ },
377
+ {
378
+ command: "detectEdge",
379
+ title: "Detect Edge from Image",
380
+ handler: async (imageUrl, options) => {
381
+ try {
382
+ const detectOptions = options || {};
383
+ const debug = detectOptions.debug === true;
384
+ const tracerOptions = {
385
+ expand: detectOptions.expand ?? 0,
386
+ smoothing: detectOptions.smoothing ?? true,
387
+ simplifyTolerance: detectOptions.simplifyTolerance ?? 2,
388
+ threshold: detectOptions.threshold,
389
+ debug,
390
+ };
391
+ // Helper to get image dimensions
392
+ const loadImage = (url) => {
393
+ return new Promise((resolve, reject) => {
394
+ const img = new Image();
395
+ img.crossOrigin = "Anonymous";
396
+ img.onload = () => resolve(img);
397
+ img.onerror = (e) => reject(e);
398
+ img.src = url;
399
+ });
400
+ };
401
+ const [img, traced] = await Promise.all([
402
+ loadImage(imageUrl),
403
+ tracer_1.ImageTracer.traceWithBounds(imageUrl, tracerOptions),
404
+ ]);
405
+ const { pathData, baseBounds, bounds } = traced;
406
+ if (debug) {
407
+ console.info("[DielineTool] detectEdge", {
408
+ imageWidth: img.width,
409
+ imageHeight: img.height,
410
+ baseBounds,
411
+ expandedBounds: bounds,
412
+ currentDielineWidth: s.width,
413
+ currentDielineHeight: s.height,
414
+ options: tracerOptions,
415
+ strategy: "single-connected-silhouette",
416
+ });
417
+ }
418
+ return {
419
+ pathData,
420
+ rawBounds: bounds,
421
+ baseBounds,
422
+ imageWidth: img.width,
423
+ imageHeight: img.height,
424
+ };
425
+ }
426
+ catch (e) {
427
+ console.error("Edge detection failed", e);
428
+ throw e;
429
+ }
430
+ },
431
+ },
432
+ ],
433
+ };
434
+ }
435
+ createHatchPattern(color = "rgba(0, 0, 0, 0.3)") {
436
+ if (typeof document === "undefined") {
437
+ return undefined;
438
+ }
439
+ const size = 20;
440
+ const canvas = document.createElement("canvas");
441
+ canvas.width = size;
442
+ canvas.height = size;
443
+ const ctx = canvas.getContext("2d");
444
+ if (ctx) {
445
+ // Transparent background
446
+ ctx.clearRect(0, 0, size, size);
447
+ // Draw diagonal /
448
+ ctx.strokeStyle = color;
449
+ ctx.lineWidth = 1;
450
+ ctx.beginPath();
451
+ ctx.moveTo(0, size);
452
+ ctx.lineTo(size, 0);
453
+ ctx.stroke();
454
+ }
455
+ // @ts-ignore
456
+ return new fabric_1.Pattern({ source: canvas, repetition: "repeat" });
457
+ }
458
+ getConfigService() {
459
+ return this.context?.services.get("ConfigurationService");
460
+ }
461
+ hasImageItems() {
462
+ const configService = this.getConfigService();
463
+ if (!configService)
464
+ return false;
465
+ const items = configService.get("image.items", []);
466
+ return Array.isArray(items) && items.length > 0;
467
+ }
468
+ syncSizeState(configService) {
469
+ const sizeState = (0, sceneLayoutModel_1.readSizeState)(configService);
470
+ this.state.width = sizeState.actualWidthMm;
471
+ this.state.height = sizeState.actualHeightMm;
472
+ this.state.padding = sizeState.viewPadding;
473
+ this.state.offset =
474
+ sizeState.cutMode === "outset"
475
+ ? sizeState.cutMarginMm
476
+ : sizeState.cutMode === "inset"
477
+ ? -sizeState.cutMarginMm
478
+ : 0;
479
+ }
480
+ buildDielineSpecs(sceneLayout) {
481
+ const { shape, shapeStyle, radius, mainLine, offsetLine, insideColor, showBleedLines, features, } = this.state;
482
+ const hasImages = this.hasImageItems();
483
+ const canvasW = sceneLayout.canvasWidth || this.canvasService?.canvas.width || 800;
484
+ const canvasH = sceneLayout.canvasHeight || this.canvasService?.canvas.height || 600;
485
+ const scale = sceneLayout.scale;
486
+ const cx = sceneLayout.trimRect.centerX;
487
+ const cy = sceneLayout.trimRect.centerY;
488
+ const visualWidth = sceneLayout.trimRect.width;
489
+ const visualHeight = sceneLayout.trimRect.height;
490
+ const visualRadius = radius * scale;
491
+ const cutW = sceneLayout.cutRect.width;
492
+ const cutH = sceneLayout.cutRect.height;
493
+ const visualOffset = (cutW - visualWidth) / 2;
494
+ const cutR = visualRadius === 0 ? 0 : Math.max(0, visualRadius + visualOffset);
495
+ const absoluteFeatures = (features || []).map((f) => ({
496
+ ...f,
497
+ x: f.x,
498
+ y: f.y,
499
+ width: (f.width || 0) * scale,
500
+ height: (f.height || 0) * scale,
501
+ radius: (f.radius || 0) * scale,
502
+ }));
503
+ const cutFeatures = absoluteFeatures.filter((f) => !f.skipCut);
504
+ const specs = [];
505
+ if (insideColor &&
506
+ insideColor !== "transparent" &&
507
+ insideColor !== "rgba(0,0,0,0)" &&
508
+ !hasImages) {
509
+ const productPathData = (0, geometry_1.generateDielinePath)({
510
+ shape,
511
+ width: cutW,
512
+ height: cutH,
513
+ radius: cutR,
514
+ x: cx,
515
+ y: cy,
516
+ features: cutFeatures,
517
+ shapeStyle,
518
+ pathData: this.state.pathData,
519
+ customSourceWidthPx: this.state.customSourceWidthPx,
520
+ customSourceHeightPx: this.state.customSourceHeightPx,
521
+ canvasWidth: canvasW,
522
+ canvasHeight: canvasH,
523
+ });
524
+ specs.push({
525
+ id: "dieline.inside",
526
+ type: "path",
527
+ space: "screen",
528
+ data: { id: "dieline.inside", type: "dieline" },
529
+ props: {
530
+ pathData: productPathData,
531
+ fill: insideColor,
532
+ stroke: null,
533
+ selectable: false,
534
+ evented: false,
535
+ originX: "left",
536
+ originY: "top",
537
+ },
538
+ });
539
+ }
540
+ if (Math.abs(visualOffset) > 0.0001) {
541
+ const bleedPathData = (0, geometry_1.generateBleedZonePath)({
542
+ shape,
543
+ width: visualWidth,
544
+ height: visualHeight,
545
+ radius: visualRadius,
546
+ x: cx,
547
+ y: cy,
548
+ features: cutFeatures,
549
+ shapeStyle,
550
+ pathData: this.state.pathData,
551
+ customSourceWidthPx: this.state.customSourceWidthPx,
552
+ customSourceHeightPx: this.state.customSourceHeightPx,
553
+ canvasWidth: canvasW,
554
+ canvasHeight: canvasH,
555
+ }, {
556
+ shape,
557
+ width: cutW,
558
+ height: cutH,
559
+ radius: cutR,
560
+ x: cx,
561
+ y: cy,
562
+ features: cutFeatures,
563
+ shapeStyle,
564
+ pathData: this.state.pathData,
565
+ customSourceWidthPx: this.state.customSourceWidthPx,
566
+ customSourceHeightPx: this.state.customSourceHeightPx,
567
+ canvasWidth: canvasW,
568
+ canvasHeight: canvasH,
569
+ }, visualOffset);
570
+ if (showBleedLines !== false) {
571
+ const pattern = this.createHatchPattern(mainLine.color);
572
+ if (pattern) {
573
+ specs.push({
574
+ id: "dieline.bleed-zone",
575
+ type: "path",
576
+ space: "screen",
577
+ data: { id: "dieline.bleed-zone", type: "dieline" },
578
+ props: {
579
+ pathData: bleedPathData,
580
+ fill: pattern,
581
+ stroke: null,
582
+ selectable: false,
583
+ evented: false,
584
+ objectCaching: false,
585
+ originX: "left",
586
+ originY: "top",
587
+ },
588
+ });
589
+ }
590
+ }
591
+ const offsetPathData = (0, geometry_1.generateDielinePath)({
592
+ shape,
593
+ width: cutW,
594
+ height: cutH,
595
+ radius: cutR,
596
+ x: cx,
597
+ y: cy,
598
+ features: cutFeatures,
599
+ shapeStyle,
600
+ pathData: this.state.pathData,
601
+ customSourceWidthPx: this.state.customSourceWidthPx,
602
+ customSourceHeightPx: this.state.customSourceHeightPx,
603
+ canvasWidth: canvasW,
604
+ canvasHeight: canvasH,
605
+ });
606
+ specs.push({
607
+ id: "dieline.offset-border",
608
+ type: "path",
609
+ space: "screen",
610
+ data: { id: "dieline.offset-border", type: "dieline" },
611
+ props: {
612
+ pathData: offsetPathData,
613
+ fill: null,
614
+ stroke: offsetLine.style === "hidden" ? null : offsetLine.color,
615
+ strokeWidth: offsetLine.width,
616
+ strokeDashArray: offsetLine.style === "dashed"
617
+ ? [offsetLine.dashLength, offsetLine.dashLength]
618
+ : undefined,
619
+ selectable: false,
620
+ evented: false,
621
+ originX: "left",
622
+ originY: "top",
623
+ },
624
+ });
625
+ }
626
+ const borderPathData = (0, geometry_1.generateDielinePath)({
627
+ shape,
628
+ width: visualWidth,
629
+ height: visualHeight,
630
+ radius: visualRadius,
631
+ x: cx,
632
+ y: cy,
633
+ features: absoluteFeatures,
634
+ shapeStyle,
635
+ pathData: this.state.pathData,
636
+ customSourceWidthPx: this.state.customSourceWidthPx,
637
+ customSourceHeightPx: this.state.customSourceHeightPx,
638
+ canvasWidth: canvasW,
639
+ canvasHeight: canvasH,
640
+ });
641
+ specs.push({
642
+ id: "dieline.border",
643
+ type: "path",
644
+ space: "screen",
645
+ data: { id: "dieline.border", type: "dieline" },
646
+ props: {
647
+ pathData: borderPathData,
648
+ fill: "transparent",
649
+ stroke: mainLine.style === "hidden" ? null : mainLine.color,
650
+ strokeWidth: mainLine.width,
651
+ strokeDashArray: mainLine.style === "dashed"
652
+ ? [mainLine.dashLength, mainLine.dashLength]
653
+ : undefined,
654
+ selectable: false,
655
+ evented: false,
656
+ originX: "left",
657
+ originY: "top",
658
+ },
659
+ });
660
+ return specs;
661
+ }
662
+ buildImageClipEffects(sceneLayout) {
663
+ const { shape, shapeStyle, radius, features } = this.state;
664
+ const canvasW = sceneLayout.canvasWidth || this.canvasService?.canvas.width || 800;
665
+ const canvasH = sceneLayout.canvasHeight || this.canvasService?.canvas.height || 600;
666
+ const scale = sceneLayout.scale;
667
+ const cx = sceneLayout.trimRect.centerX;
668
+ const cy = sceneLayout.trimRect.centerY;
669
+ const visualWidth = sceneLayout.trimRect.width;
670
+ const visualRadius = radius * scale;
671
+ const cutW = sceneLayout.cutRect.width;
672
+ const cutH = sceneLayout.cutRect.height;
673
+ const visualOffset = (cutW - visualWidth) / 2;
674
+ const cutR = visualRadius === 0 ? 0 : Math.max(0, visualRadius + visualOffset);
675
+ const absoluteFeatures = (features || []).map((f) => ({
676
+ ...f,
677
+ x: f.x,
678
+ y: f.y,
679
+ width: (f.width || 0) * scale,
680
+ height: (f.height || 0) * scale,
681
+ radius: (f.radius || 0) * scale,
682
+ }));
683
+ const cutFeatures = absoluteFeatures.filter((f) => !f.skipCut);
684
+ const clipPathData = (0, geometry_1.generateDielinePath)({
685
+ shape,
686
+ width: cutW,
687
+ height: cutH,
688
+ radius: cutR,
689
+ x: cx,
690
+ y: cy,
691
+ features: cutFeatures,
692
+ shapeStyle,
693
+ pathData: this.state.pathData,
694
+ customSourceWidthPx: this.state.customSourceWidthPx,
695
+ customSourceHeightPx: this.state.customSourceHeightPx,
696
+ canvasWidth: canvasW,
697
+ canvasHeight: canvasH,
698
+ });
699
+ if (!clipPathData)
700
+ return [];
701
+ return [
702
+ {
703
+ type: "clipPath",
704
+ id: "dieline.clip.image",
705
+ targetPassIds: [IMAGE_OBJECT_LAYER_ID],
706
+ source: {
707
+ id: "dieline.effect.clip-path",
708
+ type: "path",
709
+ space: "screen",
710
+ data: {
711
+ id: "dieline.effect.clip-path",
712
+ type: "dieline-effect",
713
+ effect: "clipPath",
714
+ },
715
+ props: {
716
+ pathData: clipPathData,
717
+ fill: "#000000",
718
+ stroke: null,
719
+ originX: "left",
720
+ originY: "top",
721
+ selectable: false,
722
+ evented: false,
723
+ excludeFromExport: true,
724
+ },
725
+ },
726
+ },
727
+ ];
728
+ }
729
+ updateDieline(_emitEvent = true) {
730
+ void this.updateDielineAsync();
731
+ }
732
+ async updateDielineAsync() {
733
+ if (!this.canvasService)
734
+ return;
735
+ const configService = this.getConfigService();
736
+ if (!configService)
737
+ return;
738
+ const seq = ++this.renderSeq;
739
+ this.syncSizeState(configService);
740
+ const sceneLayout = (0, sceneLayoutModel_1.computeSceneLayout)(this.canvasService, (0, sceneLayoutModel_1.readSizeState)(configService));
741
+ if (!sceneLayout) {
742
+ if (seq !== this.renderSeq)
743
+ return;
744
+ this.specs = [];
745
+ this.effects = [];
746
+ await this.canvasService.flushRenderFromProducers();
747
+ return;
748
+ }
749
+ const nextSpecs = this.buildDielineSpecs(sceneLayout);
750
+ const nextEffects = this.buildImageClipEffects(sceneLayout);
751
+ if (seq !== this.renderSeq)
752
+ return;
753
+ this.specs = nextSpecs;
754
+ this.effects = nextEffects;
755
+ await this.canvasService.flushRenderFromProducers();
756
+ if (seq !== this.renderSeq)
757
+ return;
758
+ this.canvasService.requestRenderAll();
759
+ }
760
+ getGeometry() {
761
+ if (!this.canvasService)
762
+ return null;
763
+ const configService = this.getConfigService();
764
+ if (!configService)
765
+ return null;
766
+ const sceneLayout = (0, sceneLayoutModel_1.computeSceneLayout)(this.canvasService, (0, sceneLayoutModel_1.readSizeState)(configService));
767
+ if (!sceneLayout)
768
+ return null;
769
+ const sceneGeometry = (0, sceneLayoutModel_1.buildSceneGeometry)(configService, sceneLayout);
770
+ return {
771
+ ...sceneGeometry,
772
+ strokeWidth: this.state.mainLine.width,
773
+ pathData: this.state.pathData,
774
+ customSourceWidthPx: this.state.customSourceWidthPx,
775
+ customSourceHeightPx: this.state.customSourceHeightPx,
776
+ };
777
+ }
778
+ async exportCutImage(options) {
779
+ const debug = options?.debug === true;
780
+ if (!this.canvasService) {
781
+ console.warn("[DielineTool] exportCutImage returned null: canvas-not-ready");
782
+ return null;
783
+ }
784
+ const configService = this.getConfigService();
785
+ if (!configService) {
786
+ console.warn("[DielineTool] exportCutImage returned null: config-service-not-ready");
787
+ return null;
788
+ }
789
+ this.syncSizeState(configService);
790
+ const sceneLayout = (0, sceneLayoutModel_1.computeSceneLayout)(this.canvasService, (0, sceneLayoutModel_1.readSizeState)(configService));
791
+ if (!sceneLayout) {
792
+ console.warn("[DielineTool] exportCutImage returned null: scene-layout-null");
793
+ return null;
794
+ }
795
+ const { shape, shapeStyle, radius, features, pathData } = this.state;
796
+ const canvasW = sceneLayout.canvasWidth || this.canvasService.canvas.width || 800;
797
+ const canvasH = sceneLayout.canvasHeight || this.canvasService.canvas.height || 600;
798
+ const scale = sceneLayout.scale;
799
+ const cx = sceneLayout.trimRect.centerX;
800
+ const cy = sceneLayout.trimRect.centerY;
801
+ const cutW = sceneLayout.cutRect.width;
802
+ const cutH = sceneLayout.cutRect.height;
803
+ const visualRadius = radius * scale;
804
+ const visualOffset = (cutW - sceneLayout.trimRect.width) / 2;
805
+ const cutR = visualRadius === 0 ? 0 : Math.max(0, visualRadius + visualOffset);
806
+ const absoluteFeatures = (features || []).map((f) => ({
807
+ ...f,
808
+ x: f.x,
809
+ y: f.y,
810
+ width: (f.width || 0) * scale,
811
+ height: (f.height || 0) * scale,
812
+ radius: (f.radius || 0) * scale,
813
+ }));
814
+ const cutFeatures = absoluteFeatures.filter((f) => !f.skipCut);
815
+ const generatedPathData = (0, geometry_1.generateDielinePath)({
816
+ shape,
817
+ width: cutW,
818
+ height: cutH,
819
+ radius: cutR,
820
+ x: cx,
821
+ y: cy,
822
+ features: cutFeatures,
823
+ shapeStyle,
824
+ pathData,
825
+ customSourceWidthPx: this.state.customSourceWidthPx,
826
+ customSourceHeightPx: this.state.customSourceHeightPx,
827
+ canvasWidth: canvasW,
828
+ canvasHeight: canvasH,
829
+ });
830
+ const clipPath = new fabric_1.Path(generatedPathData, {
831
+ originX: "center",
832
+ originY: "center",
833
+ left: cx,
834
+ top: cy,
835
+ absolutePositioned: true,
836
+ });
837
+ const pathOffsetX = Number(clipPath?.pathOffset?.x);
838
+ const pathOffsetY = Number(clipPath?.pathOffset?.y);
839
+ const centerX = Number.isFinite(pathOffsetX) ? pathOffsetX : cx;
840
+ const centerY = Number.isFinite(pathOffsetY) ? pathOffsetY : cy;
841
+ clipPath.set({
842
+ originX: "center",
843
+ originY: "center",
844
+ left: centerX,
845
+ top: centerY,
846
+ absolutePositioned: true,
847
+ });
848
+ clipPath.setCoords();
849
+ const pathBounds = clipPath.getBoundingRect();
850
+ if (!Number.isFinite(pathBounds.left) ||
851
+ !Number.isFinite(pathBounds.top) ||
852
+ !Number.isFinite(pathBounds.width) ||
853
+ !Number.isFinite(pathBounds.height) ||
854
+ pathBounds.width <= 0 ||
855
+ pathBounds.height <= 0) {
856
+ console.warn("[DielineTool] exportCutImage returned null: invalid-cut-bounds", {
857
+ bounds: pathBounds,
858
+ });
859
+ return null;
860
+ }
861
+ const exportBounds = pathBounds;
862
+ const sourceImages = this.canvasService.canvas
863
+ .getObjects()
864
+ .filter((obj) => {
865
+ return obj?.data?.layerId === IMAGE_OBJECT_LAYER_ID;
866
+ });
867
+ if (!sourceImages.length) {
868
+ console.warn("[DielineTool] exportCutImage returned null: no-image-objects-on-canvas");
869
+ return null;
870
+ }
871
+ const sourceCanvasWidth = Number(this.canvasService.canvas.width || sceneLayout.canvasWidth || canvasW);
872
+ const sourceCanvasHeight = Number(this.canvasService.canvas.height || sceneLayout.canvasHeight || canvasH);
873
+ const el = document.createElement("canvas");
874
+ const exportCanvas = new fabric_1.Canvas(el, {
875
+ renderOnAddRemove: false,
876
+ selection: false,
877
+ enableRetinaScaling: false,
878
+ preserveObjectStacking: true,
879
+ });
880
+ exportCanvas.setDimensions({
881
+ width: Math.max(1, sourceCanvasWidth),
882
+ height: Math.max(1, sourceCanvasHeight),
883
+ });
884
+ try {
885
+ for (const source of sourceImages) {
886
+ const clone = await source.clone();
887
+ clone.set({
888
+ selectable: false,
889
+ evented: false,
890
+ });
891
+ clone.setCoords();
892
+ exportCanvas.add(clone);
893
+ }
894
+ exportCanvas.clipPath = clipPath;
895
+ exportCanvas.renderAll();
896
+ const dataUrl = exportCanvas.toDataURL({
897
+ format: "png",
898
+ multiplier: 2,
899
+ left: exportBounds.left,
900
+ top: exportBounds.top,
901
+ width: exportBounds.width,
902
+ height: exportBounds.height,
903
+ });
904
+ if (debug) {
905
+ console.info("[DielineTool] exportCutImage success", {
906
+ sourceCount: sourceImages.length,
907
+ bounds: exportBounds,
908
+ rawPathBounds: pathBounds,
909
+ pathOffset: {
910
+ x: Number.isFinite(pathOffsetX) ? pathOffsetX : null,
911
+ y: Number.isFinite(pathOffsetY) ? pathOffsetY : null,
912
+ },
913
+ clipPathCenter: {
914
+ x: centerX,
915
+ y: centerY,
916
+ },
917
+ cutRect: sceneLayout.cutRect,
918
+ canvasSize: {
919
+ width: Math.max(1, sourceCanvasWidth),
920
+ height: Math.max(1, sourceCanvasHeight),
921
+ },
922
+ });
923
+ }
924
+ return dataUrl;
925
+ }
926
+ finally {
927
+ exportCanvas.dispose();
928
+ }
929
+ }
930
+ }
931
+ exports.DielineTool = DielineTool;