@pooder/kit 5.3.0 → 5.4.0

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