@pooder/kit 4.3.1 → 5.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 (60) hide show
  1. package/.test-dist/src/CanvasService.js +249 -0
  2. package/.test-dist/src/ViewportSystem.js +75 -0
  3. package/.test-dist/src/background.js +203 -0
  4. package/.test-dist/src/bridgeSelection.js +20 -0
  5. package/.test-dist/src/constraints.js +237 -0
  6. package/.test-dist/src/coordinate.js +74 -0
  7. package/.test-dist/src/dieline.js +723 -0
  8. package/.test-dist/src/edgeScale.js +12 -0
  9. package/.test-dist/src/feature.js +752 -0
  10. package/.test-dist/src/featureComplete.js +32 -0
  11. package/.test-dist/src/film.js +167 -0
  12. package/.test-dist/src/geometry.js +506 -0
  13. package/.test-dist/src/image.js +1234 -0
  14. package/.test-dist/src/index.js +35 -0
  15. package/.test-dist/src/maskOps.js +270 -0
  16. package/.test-dist/src/mirror.js +104 -0
  17. package/.test-dist/src/renderSpec.js +2 -0
  18. package/.test-dist/src/ruler.js +343 -0
  19. package/.test-dist/src/sceneLayout.js +99 -0
  20. package/.test-dist/src/sceneLayoutModel.js +196 -0
  21. package/.test-dist/src/sceneView.js +40 -0
  22. package/.test-dist/src/sceneVisibility.js +42 -0
  23. package/.test-dist/src/size.js +332 -0
  24. package/.test-dist/src/tracer.js +544 -0
  25. package/.test-dist/src/units.js +30 -0
  26. package/.test-dist/src/white-ink.js +829 -0
  27. package/.test-dist/src/wrappedOffsets.js +33 -0
  28. package/.test-dist/tests/run.js +94 -0
  29. package/CHANGELOG.md +11 -0
  30. package/dist/index.d.mts +339 -36
  31. package/dist/index.d.ts +339 -36
  32. package/dist/index.js +3572 -850
  33. package/dist/index.mjs +3565 -852
  34. package/package.json +2 -2
  35. package/src/CanvasService.ts +300 -96
  36. package/src/ViewportSystem.ts +92 -92
  37. package/src/background.ts +230 -230
  38. package/src/bridgeSelection.ts +17 -0
  39. package/src/coordinate.ts +106 -106
  40. package/src/dieline.ts +897 -973
  41. package/src/edgeScale.ts +19 -0
  42. package/src/feature.ts +83 -30
  43. package/src/film.ts +194 -194
  44. package/src/geometry.ts +242 -84
  45. package/src/image.ts +1582 -512
  46. package/src/index.ts +14 -10
  47. package/src/maskOps.ts +326 -0
  48. package/src/mirror.ts +128 -128
  49. package/src/renderSpec.ts +18 -0
  50. package/src/ruler.ts +449 -508
  51. package/src/sceneLayout.ts +121 -0
  52. package/src/sceneLayoutModel.ts +335 -0
  53. package/src/sceneVisibility.ts +49 -0
  54. package/src/size.ts +379 -0
  55. package/src/tracer.ts +719 -570
  56. package/src/units.ts +27 -27
  57. package/src/white-ink.ts +1018 -373
  58. package/src/wrappedOffsets.ts +33 -0
  59. package/tests/run.ts +118 -0
  60. package/tsconfig.test.json +15 -15
package/src/dieline.ts CHANGED
@@ -1,973 +1,897 @@
1
- import {
2
- Extension,
3
- ExtensionContext,
4
- ContributionPointIds,
5
- CommandContribution,
6
- ConfigurationContribution,
7
- } from "@pooder/core";
8
- import { Path, Pattern } from "fabric";
9
- import CanvasService from "./CanvasService";
10
- import { ImageTracer } from "./tracer";
11
- import { Unit } from "./coordinate";
12
- import { parseLengthToMm } from "./units";
13
- import {
14
- generateDielinePath,
15
- generateMaskPath,
16
- generateBleedZonePath,
17
- getPathBounds,
18
- DielineFeature,
19
- } from "./geometry";
20
-
21
- export interface DielineGeometry {
22
- shape: "rect" | "circle" | "ellipse" | "custom";
23
- unit: "mm";
24
- displayUnit: Unit;
25
- x: number;
26
- y: number;
27
- width: number;
28
- height: number;
29
- radius: number;
30
- offset: number;
31
- borderLength?: number;
32
- scale?: number;
33
- strokeWidth?: number;
34
- pathData?: string;
35
- }
36
-
37
- export interface LineStyle {
38
- width: number;
39
- color: string;
40
- dashLength: number;
41
- style: "solid" | "dashed" | "hidden";
42
- }
43
-
44
- export interface DielineState {
45
- displayUnit: Unit;
46
- shape: "rect" | "circle" | "ellipse" | "custom";
47
- width: number;
48
- height: number;
49
- radius: number;
50
- offset: number;
51
- padding: number | string;
52
- mainLine: LineStyle;
53
- offsetLine: LineStyle;
54
- insideColor: string;
55
- outsideColor: string;
56
- showBleedLines: boolean;
57
- features: DielineFeature[];
58
- pathData?: string;
59
- }
60
-
61
- export class DielineTool implements Extension {
62
- id = "pooder.kit.dieline";
63
- public metadata = {
64
- name: "DielineTool",
65
- };
66
-
67
- private state: DielineState = {
68
- displayUnit: "mm",
69
- shape: "rect",
70
- width: 500,
71
- height: 500,
72
- radius: 0,
73
- offset: 0,
74
- padding: 140,
75
- mainLine: {
76
- width: 2.7,
77
- color: "#FF0000",
78
- dashLength: 5,
79
- style: "solid",
80
- },
81
- offsetLine: {
82
- width: 2.7,
83
- color: "#FF0000",
84
- dashLength: 5,
85
- style: "solid",
86
- },
87
- insideColor: "rgba(0,0,0,0)",
88
- outsideColor: "#ffffff",
89
- showBleedLines: true,
90
- features: [],
91
- };
92
-
93
- private canvasService?: CanvasService;
94
- private context?: ExtensionContext;
95
-
96
- constructor(options?: Partial<DielineState>) {
97
- if (options) {
98
- // Deep merge for styles to avoid overwriting defaults with partial objects
99
- if (options.mainLine) {
100
- Object.assign(this.state.mainLine, options.mainLine);
101
- delete options.mainLine;
102
- }
103
- if (options.offsetLine) {
104
- Object.assign(this.state.offsetLine, options.offsetLine);
105
- delete options.offsetLine;
106
- }
107
- Object.assign(this.state, options);
108
- }
109
- }
110
-
111
- activate(context: ExtensionContext) {
112
- this.context = context;
113
- this.canvasService = context.services.get<CanvasService>("CanvasService");
114
- if (!this.canvasService) {
115
- console.warn("CanvasService not found for DielineTool");
116
- return;
117
- }
118
-
119
- const configService = context.services.get<any>("ConfigurationService");
120
- if (configService) {
121
- // Load initial config
122
- const s = this.state;
123
- s.displayUnit = configService.get("dieline.displayUnit", s.displayUnit);
124
- s.shape = configService.get("dieline.shape", s.shape);
125
- s.width = parseLengthToMm(
126
- configService.get("dieline.width", s.width),
127
- "mm",
128
- );
129
- s.height = parseLengthToMm(
130
- configService.get("dieline.height", s.height),
131
- "mm",
132
- );
133
- s.radius = parseLengthToMm(
134
- configService.get("dieline.radius", s.radius),
135
- "mm",
136
- );
137
- s.padding = configService.get("dieline.padding", s.padding);
138
- s.offset = parseLengthToMm(
139
- configService.get("dieline.offset", s.offset),
140
- "mm",
141
- );
142
-
143
- // Main Line
144
- s.mainLine.width = configService.get(
145
- "dieline.strokeWidth",
146
- s.mainLine.width,
147
- );
148
- s.mainLine.color = configService.get(
149
- "dieline.strokeColor",
150
- s.mainLine.color,
151
- );
152
- s.mainLine.dashLength = configService.get(
153
- "dieline.dashLength",
154
- s.mainLine.dashLength,
155
- );
156
- s.mainLine.style = configService.get("dieline.style", s.mainLine.style);
157
-
158
- // Offset Line
159
- s.offsetLine.width = configService.get(
160
- "dieline.offsetStrokeWidth",
161
- s.offsetLine.width,
162
- );
163
- s.offsetLine.color = configService.get(
164
- "dieline.offsetStrokeColor",
165
- s.offsetLine.color,
166
- );
167
- s.offsetLine.dashLength = configService.get(
168
- "dieline.offsetDashLength",
169
- s.offsetLine.dashLength,
170
- );
171
- s.offsetLine.style = configService.get(
172
- "dieline.offsetStyle",
173
- s.offsetLine.style,
174
- );
175
-
176
- s.insideColor = configService.get("dieline.insideColor", s.insideColor);
177
- s.outsideColor = configService.get(
178
- "dieline.outsideColor",
179
- s.outsideColor,
180
- );
181
- s.showBleedLines = configService.get(
182
- "dieline.showBleedLines",
183
- s.showBleedLines,
184
- );
185
- s.features = configService.get("dieline.features", s.features);
186
- s.pathData = configService.get("dieline.pathData", s.pathData);
187
-
188
- // Listen for changes
189
- configService.onAnyChange((e: { key: string; value: any }) => {
190
- if (e.key.startsWith("dieline.")) {
191
- switch (e.key) {
192
- case "dieline.displayUnit":
193
- s.displayUnit = e.value;
194
- break;
195
- case "dieline.shape":
196
- s.shape = e.value;
197
- break;
198
- case "dieline.width":
199
- s.width = parseLengthToMm(e.value, "mm");
200
- break;
201
- case "dieline.height":
202
- s.height = parseLengthToMm(e.value, "mm");
203
- break;
204
- case "dieline.radius":
205
- s.radius = parseLengthToMm(e.value, "mm");
206
- break;
207
- case "dieline.padding":
208
- s.padding = e.value;
209
- break;
210
- case "dieline.offset":
211
- s.offset = parseLengthToMm(e.value, "mm");
212
- break;
213
-
214
- case "dieline.strokeWidth":
215
- s.mainLine.width = e.value;
216
- break;
217
- case "dieline.strokeColor":
218
- s.mainLine.color = e.value;
219
- break;
220
- case "dieline.dashLength":
221
- s.mainLine.dashLength = e.value;
222
- break;
223
- case "dieline.style":
224
- s.mainLine.style = e.value;
225
- break;
226
-
227
- case "dieline.offsetStrokeWidth":
228
- s.offsetLine.width = e.value;
229
- break;
230
- case "dieline.offsetStrokeColor":
231
- s.offsetLine.color = e.value;
232
- break;
233
- case "dieline.offsetDashLength":
234
- s.offsetLine.dashLength = e.value;
235
- break;
236
- case "dieline.offsetStyle":
237
- s.offsetLine.style = e.value;
238
- break;
239
-
240
- case "dieline.insideColor":
241
- s.insideColor = e.value;
242
- break;
243
- case "dieline.outsideColor":
244
- s.outsideColor = e.value;
245
- break;
246
- case "dieline.showBleedLines":
247
- s.showBleedLines = e.value;
248
- break;
249
- case "dieline.features":
250
- s.features = e.value;
251
- break;
252
- case "dieline.pathData":
253
- s.pathData = e.value;
254
- break;
255
- }
256
- this.updateDieline();
257
- }
258
- });
259
- }
260
-
261
- this.createLayer();
262
- this.updateDieline();
263
- }
264
-
265
- deactivate(context: ExtensionContext) {
266
- this.destroyLayer();
267
- this.canvasService = undefined;
268
- this.context = undefined;
269
- }
270
-
271
- contribute() {
272
- const s = this.state;
273
- return {
274
- [ContributionPointIds.CONFIGURATIONS]: [
275
- {
276
- id: "dieline.displayUnit",
277
- type: "select",
278
- label: "Display Unit",
279
- options: ["mm", "cm", "in"],
280
- default: s.displayUnit,
281
- },
282
- {
283
- id: "dieline.shape",
284
- type: "select",
285
- label: "Shape",
286
- options: ["rect", "circle", "ellipse", "custom"],
287
- default: s.shape,
288
- },
289
- {
290
- id: "dieline.width",
291
- type: "number",
292
- label: "Width (mm)",
293
- min: 10,
294
- max: 2000,
295
- default: s.width,
296
- },
297
- {
298
- id: "dieline.height",
299
- type: "number",
300
- label: "Height (mm)",
301
- min: 10,
302
- max: 2000,
303
- default: s.height,
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.padding",
315
- type: "select",
316
- label: "View Padding",
317
- options: [0, 10, 20, 40, 60, 100, "2%", "5%", "10%", "15%", "20%"],
318
- default: s.padding,
319
- },
320
- {
321
- id: "dieline.offset",
322
- type: "number",
323
- label: "Bleed Offset (mm)",
324
- min: -100,
325
- max: 100,
326
- default: s.offset,
327
- },
328
- {
329
- id: "dieline.showBleedLines",
330
- type: "boolean",
331
- label: "Show Bleed Lines",
332
- default: s.showBleedLines,
333
- },
334
- {
335
- id: "dieline.strokeWidth",
336
- type: "number",
337
- label: "Line Width",
338
- min: 0.1,
339
- max: 10,
340
- step: 0.1,
341
- default: s.mainLine.width,
342
- },
343
- {
344
- id: "dieline.strokeColor",
345
- type: "color",
346
- label: "Line Color",
347
- default: s.mainLine.color,
348
- },
349
- {
350
- id: "dieline.dashLength",
351
- type: "number",
352
- label: "Dash Length",
353
- min: 1,
354
- max: 50,
355
- default: s.mainLine.dashLength,
356
- },
357
- {
358
- id: "dieline.style",
359
- type: "select",
360
- label: "Line Style",
361
- options: ["solid", "dashed", "hidden"],
362
- default: s.mainLine.style,
363
- },
364
- {
365
- id: "dieline.offsetStrokeWidth",
366
- type: "number",
367
- label: "Offset Line Width",
368
- min: 0.1,
369
- max: 10,
370
- step: 0.1,
371
- default: s.offsetLine.width,
372
- },
373
- {
374
- id: "dieline.offsetStrokeColor",
375
- type: "color",
376
- label: "Offset Line Color",
377
- default: s.offsetLine.color,
378
- },
379
- {
380
- id: "dieline.offsetDashLength",
381
- type: "number",
382
- label: "Offset Dash Length",
383
- min: 1,
384
- max: 50,
385
- default: s.offsetLine.dashLength,
386
- },
387
- {
388
- id: "dieline.offsetStyle",
389
- type: "select",
390
- label: "Offset Line Style",
391
- options: ["solid", "dashed", "hidden"],
392
- default: s.offsetLine.style,
393
- },
394
- {
395
- id: "dieline.insideColor",
396
- type: "color",
397
- label: "Inside Color",
398
- default: s.insideColor,
399
- },
400
- {
401
- id: "dieline.outsideColor",
402
- type: "color",
403
- label: "Outside Color",
404
- default: s.outsideColor,
405
- },
406
- {
407
- id: "dieline.features",
408
- type: "json",
409
- label: "Edge Features",
410
- default: s.features,
411
- },
412
- ] as ConfigurationContribution[],
413
- [ContributionPointIds.COMMANDS]: [
414
- {
415
- command: "updateFeaturePosition",
416
- title: "Update Feature Position",
417
- handler: (groupId: string, x: number, y: number) => {
418
- const configService = this.context?.services.get<any>(
419
- "ConfigurationService",
420
- );
421
- if (!configService) return;
422
-
423
- const features = configService.get("dieline.features") || [];
424
-
425
- let changed = false;
426
- const newFeatures = features.map((f: any) => {
427
- if (f.groupId === groupId) {
428
- if (f.x !== x || f.y !== y) {
429
- changed = true;
430
- return { ...f, x, y };
431
- }
432
- }
433
- return f;
434
- });
435
-
436
- if (changed) {
437
- configService.update("dieline.features", newFeatures);
438
- }
439
- },
440
- },
441
- {
442
- command: "getGeometry",
443
- title: "Get Geometry",
444
- handler: () => {
445
- return this.getGeometry();
446
- },
447
- },
448
- {
449
- command: "exportCutImage",
450
- title: "Export Cut Image",
451
- handler: () => {
452
- return this.exportCutImage();
453
- },
454
- },
455
- {
456
- command: "detectEdge",
457
- title: "Detect Edge from Image",
458
- handler: async (imageUrl: string, options?: any) => {
459
- try {
460
- // Helper to get image dimensions
461
- const loadImage = (url: string): Promise<HTMLImageElement> => {
462
- return new Promise((resolve, reject) => {
463
- const img = new Image();
464
- img.crossOrigin = "Anonymous";
465
- img.onload = () => resolve(img);
466
- img.onerror = (e) => reject(e);
467
- img.src = url;
468
- });
469
- };
470
-
471
- const [img, pathData] = await Promise.all([
472
- loadImage(imageUrl),
473
- ImageTracer.trace(imageUrl, options),
474
- ]);
475
-
476
- const bounds = getPathBounds(pathData);
477
-
478
- const currentMax = Math.max(s.width, s.height);
479
- const scale = currentMax / Math.max(bounds.width, bounds.height);
480
-
481
- const newWidth = bounds.width * scale;
482
- const newHeight = bounds.height * scale;
483
-
484
- return {
485
- pathData,
486
- width: newWidth,
487
- height: newHeight,
488
- rawBounds: bounds,
489
- imageWidth: img.width,
490
- imageHeight: img.height,
491
- };
492
- } catch (e) {
493
- console.error("Edge detection failed", e);
494
- throw e;
495
- }
496
- },
497
- },
498
- ] as CommandContribution[],
499
- };
500
- }
501
-
502
- private getLayer() {
503
- return this.canvasService?.getLayer("dieline-overlay");
504
- }
505
-
506
- private createLayer() {
507
- if (!this.canvasService) return;
508
- const width = this.canvasService.canvas.width || 800;
509
- const height = this.canvasService.canvas.height || 600;
510
-
511
- const layer = this.canvasService.createLayer("dieline-overlay", {
512
- width,
513
- height,
514
- selectable: false,
515
- evented: false,
516
- });
517
-
518
- this.canvasService.canvas.bringObjectToFront(layer);
519
-
520
- // Ensure above user layer
521
- const userLayer = this.canvasService.getLayer("user");
522
- if (userLayer) {
523
- const userIndex = this.canvasService.canvas
524
- .getObjects()
525
- .indexOf(userLayer);
526
- this.canvasService.canvas.moveObjectTo(layer, userIndex + 1);
527
- }
528
- }
529
-
530
- private destroyLayer() {
531
- if (!this.canvasService) return;
532
- const layer = this.getLayer();
533
- if (layer) {
534
- this.canvasService.canvas.remove(layer);
535
- }
536
- }
537
-
538
- private createHatchPattern(color: string = "rgba(0, 0, 0, 0.3)") {
539
- if (typeof document === "undefined") {
540
- return undefined;
541
- }
542
- const size = 20;
543
- const canvas = document.createElement("canvas");
544
- canvas.width = size;
545
- canvas.height = size;
546
- const ctx = canvas.getContext("2d");
547
- if (ctx) {
548
- // Transparent background
549
- ctx.clearRect(0, 0, size, size);
550
-
551
- // Draw diagonal /
552
- ctx.strokeStyle = color;
553
- ctx.lineWidth = 1;
554
- ctx.beginPath();
555
- ctx.moveTo(0, size);
556
- ctx.lineTo(size, 0);
557
- ctx.stroke();
558
- }
559
- // @ts-ignore
560
- return new Pattern({ source: canvas, repetition: "repeat" });
561
- }
562
-
563
- private resolvePadding(
564
- containerWidth: number,
565
- containerHeight: number,
566
- ): number {
567
- if (typeof this.state.padding === "number") {
568
- return this.state.padding;
569
- }
570
- if (typeof this.state.padding === "string") {
571
- if (this.state.padding.endsWith("%")) {
572
- const percent = parseFloat(this.state.padding) / 100;
573
- return Math.min(containerWidth, containerHeight) * percent;
574
- }
575
- return parseFloat(this.state.padding) || 0;
576
- }
577
- return 0;
578
- }
579
-
580
- public updateDieline(emitEvent: boolean = true) {
581
- if (!this.canvasService) return;
582
- const layer = this.getLayer();
583
- if (!layer) return;
584
-
585
- const {
586
- displayUnit,
587
- shape,
588
- radius,
589
- offset,
590
- mainLine,
591
- offsetLine,
592
- insideColor,
593
- outsideColor,
594
- showBleedLines,
595
- features,
596
- } = this.state;
597
- const { width, height } = this.state;
598
-
599
- const canvasW = this.canvasService.canvas.width || 800;
600
- const canvasH = this.canvasService.canvas.height || 600;
601
-
602
- // Calculate Layout based on Physical Dimensions and Canvas Size
603
- // Add padding to avoid edge hugging
604
- const paddingPx = this.resolvePadding(canvasW, canvasH);
605
-
606
- // Update Viewport System
607
- this.canvasService.viewport.setPadding(paddingPx);
608
- this.canvasService.viewport.updatePhysical(width, height);
609
-
610
- const layout = this.canvasService.viewport.layout;
611
-
612
- const scale = layout.scale;
613
- const cx = layout.offsetX + layout.width / 2;
614
- const cy = layout.offsetY + layout.height / 2;
615
-
616
- // Scaled dimensions for rendering (Pixels)
617
- const visualWidth = layout.width;
618
- const visualHeight = layout.height;
619
- const visualRadius = radius * scale;
620
- const visualOffset = offset * scale;
621
-
622
- // Clear existing objects
623
- layer.remove(...layer.getObjects());
624
-
625
- // Scale Features for Geometry Generation
626
- const absoluteFeatures = (features || []).map((f) => {
627
- const featureScale = scale;
628
-
629
- return {
630
- ...f,
631
- x: f.x,
632
- y: f.y,
633
- width: (f.width || 0) * featureScale,
634
- height: (f.height || 0) * featureScale,
635
- radius: (f.radius || 0) * featureScale,
636
- };
637
- });
638
-
639
- // Split features into Cut (Physical) and Visual (All)
640
- const cutFeatures = absoluteFeatures.filter((f) => !f.skipCut);
641
-
642
- // 1. Draw Mask (Outside)
643
- const cutW = Math.max(0, visualWidth + visualOffset * 2);
644
- const cutH = Math.max(0, visualHeight + visualOffset * 2);
645
- const cutR =
646
- visualRadius === 0 ? 0 : Math.max(0, visualRadius + visualOffset);
647
-
648
- // Use Paper.js to generate the complex mask path
649
- const maskPathData = generateMaskPath({
650
- canvasWidth: canvasW,
651
- canvasHeight: canvasH,
652
- shape,
653
- width: cutW,
654
- height: cutH,
655
- radius: cutR,
656
- x: cx,
657
- y: cy,
658
- features: cutFeatures,
659
- pathData: this.state.pathData,
660
- });
661
-
662
- const mask = new Path(maskPathData, {
663
- fill: outsideColor,
664
- stroke: null,
665
- selectable: false,
666
- evented: false,
667
- originX: "left" as const,
668
- originY: "top" as const,
669
- left: 0,
670
- top: 0,
671
- });
672
- layer.add(mask);
673
-
674
- // 2. Draw Inside Fill (Dieline Shape itself, merged with features if needed)
675
- if (
676
- insideColor &&
677
- insideColor !== "transparent" &&
678
- insideColor !== "rgba(0,0,0,0)"
679
- ) {
680
- // Generate path for the product shape (Paper) = Dieline +/- Features
681
- const productPathData = generateDielinePath({
682
- shape,
683
- width: cutW,
684
- height: cutH,
685
- radius: cutR,
686
- x: cx,
687
- y: cy,
688
- features: cutFeatures, // Use same features as mask for consistency
689
- pathData: this.state.pathData,
690
- canvasWidth: canvasW,
691
- canvasHeight: canvasH,
692
- });
693
-
694
- const insideObj = new Path(productPathData, {
695
- fill: insideColor,
696
- stroke: null,
697
- selectable: false,
698
- evented: false,
699
- originX: "left", // paper.js paths are absolute
700
- originY: "top",
701
- });
702
- layer.add(insideObj);
703
- }
704
-
705
- // 3. Draw Bleed Zone (Hatch Fill) and Offset Border
706
- if (offset !== 0) {
707
- const bleedPathData = generateBleedZonePath(
708
- {
709
- shape,
710
- width: visualWidth,
711
- height: visualHeight,
712
- radius: visualRadius,
713
- x: cx,
714
- y: cy,
715
- features: cutFeatures,
716
- pathData: this.state.pathData,
717
- canvasWidth: canvasW,
718
- canvasHeight: canvasH,
719
- },
720
- {
721
- shape,
722
- width: cutW,
723
- height: cutH,
724
- radius: cutR,
725
- x: cx,
726
- y: cy,
727
- features: cutFeatures,
728
- pathData: this.state.pathData,
729
- canvasWidth: canvasW,
730
- canvasHeight: canvasH,
731
- },
732
- visualOffset,
733
- );
734
-
735
- // Use solid red for hatch lines to match dieline, background is transparent
736
- if (showBleedLines !== false) {
737
- const pattern = this.createHatchPattern(mainLine.color);
738
- if (pattern) {
739
- const bleedObj = new Path(bleedPathData, {
740
- fill: pattern,
741
- stroke: null,
742
- selectable: false,
743
- evented: false,
744
- objectCaching: false,
745
- originX: "left",
746
- originY: "top",
747
- });
748
- layer.add(bleedObj);
749
- }
750
- }
751
-
752
- // Offset Dieline Border
753
- const offsetPathData = generateDielinePath({
754
- shape,
755
- width: cutW,
756
- height: cutH,
757
- radius: cutR,
758
- x: cx,
759
- y: cy,
760
- features: cutFeatures,
761
- pathData: this.state.pathData,
762
- canvasWidth: canvasW,
763
- canvasHeight: canvasH,
764
- });
765
-
766
- const offsetBorderObj = new Path(offsetPathData, {
767
- fill: null,
768
- stroke: offsetLine.style === "hidden" ? null : offsetLine.color,
769
- strokeWidth: offsetLine.width,
770
- strokeDashArray:
771
- offsetLine.style === "dashed"
772
- ? [offsetLine.dashLength, offsetLine.dashLength]
773
- : undefined,
774
- selectable: false,
775
- evented: false,
776
- originX: "left",
777
- originY: "top",
778
- });
779
- layer.add(offsetBorderObj);
780
- }
781
-
782
- // 4. Draw Dieline (Visual Border)
783
- const borderPathData = generateDielinePath({
784
- shape,
785
- width: visualWidth,
786
- height: visualHeight,
787
- radius: visualRadius,
788
- x: cx,
789
- y: cy,
790
- features: absoluteFeatures,
791
- pathData: this.state.pathData,
792
- canvasWidth: canvasW,
793
- canvasHeight: canvasH,
794
- });
795
-
796
- const borderObj = new Path(borderPathData, {
797
- fill: "transparent",
798
- stroke: mainLine.style === "hidden" ? null : mainLine.color,
799
- strokeWidth: mainLine.width,
800
- strokeDashArray:
801
- mainLine.style === "dashed"
802
- ? [mainLine.dashLength, mainLine.dashLength]
803
- : undefined,
804
- selectable: false,
805
- evented: false,
806
- originX: "left",
807
- originY: "top",
808
- });
809
-
810
- layer.add(borderObj);
811
-
812
- // Enforce z-index: Dieline > User
813
- const userLayer = this.canvasService.getLayer("user");
814
- if (layer && userLayer) {
815
- const layerIndex = this.canvasService.canvas.getObjects().indexOf(layer);
816
- const userIndex = this.canvasService.canvas
817
- .getObjects()
818
- .indexOf(userLayer);
819
- if (layerIndex < userIndex) {
820
- this.canvasService.canvas.moveObjectTo(layer, userIndex + 1);
821
- }
822
- } else {
823
- // If no user layer, just bring to front (safe default)
824
- this.canvasService.canvas.bringObjectToFront(layer);
825
- }
826
-
827
- // Ensure Ruler is above Dieline if it exists
828
- const rulerLayer = this.canvasService.getLayer("ruler-overlay");
829
- if (rulerLayer) {
830
- this.canvasService.canvas.bringObjectToFront(rulerLayer);
831
- }
832
-
833
- layer.dirty = true;
834
- this.canvasService.requestRenderAll();
835
-
836
- // Emit change event so other tools (like FeatureTool) can react
837
- if (emitEvent && this.context) {
838
- const geometry = this.getGeometry();
839
- if (geometry) {
840
- this.context.eventBus.emit("dieline:geometry:change", geometry);
841
- }
842
- }
843
- }
844
-
845
- public getGeometry(): DielineGeometry | null {
846
- if (!this.canvasService) return null;
847
- const {
848
- displayUnit,
849
- shape,
850
- width,
851
- height,
852
- radius,
853
- offset,
854
- mainLine,
855
- pathData,
856
- } = this.state;
857
- const canvasW = this.canvasService.canvas.width || 800;
858
- const canvasH = this.canvasService.canvas.height || 600;
859
-
860
- const paddingPx = this.resolvePadding(canvasW, canvasH);
861
-
862
- // Update Viewport System (Ensure it's up to date)
863
- this.canvasService.viewport.setPadding(paddingPx);
864
- this.canvasService.viewport.updatePhysical(width, height);
865
-
866
- const layout = this.canvasService.viewport.layout;
867
-
868
- const scale = layout.scale;
869
- const cx = layout.offsetX + layout.width / 2;
870
- const cy = layout.offsetY + layout.height / 2;
871
-
872
- const visualWidth = layout.width;
873
- const visualHeight = layout.height;
874
-
875
- return {
876
- shape,
877
- unit: "mm",
878
- displayUnit,
879
- x: cx,
880
- y: cy,
881
- width: visualWidth,
882
- height: visualHeight,
883
- radius: radius * scale,
884
- offset: offset * scale,
885
- scale,
886
- strokeWidth: mainLine.width,
887
- pathData,
888
- } as DielineGeometry;
889
- }
890
-
891
- public async exportCutImage() {
892
- if (!this.canvasService) return null;
893
- const userLayer = this.canvasService.getLayer("user");
894
-
895
- if (!userLayer) return null;
896
-
897
- // 1. Generate Path Data
898
- const { shape, width, height, radius, features, pathData } = this.state;
899
- const canvasW = this.canvasService.canvas.width || 800;
900
- const canvasH = this.canvasService.canvas.height || 600;
901
-
902
- const paddingPx = this.resolvePadding(canvasW, canvasH);
903
-
904
- // Update Viewport System
905
- this.canvasService.viewport.setPadding(paddingPx);
906
- this.canvasService.viewport.updatePhysical(width, height);
907
-
908
- const layout = this.canvasService.viewport.layout;
909
- const scale = layout.scale;
910
- const cx = layout.offsetX + layout.width / 2;
911
- const cy = layout.offsetY + layout.height / 2;
912
- const visualWidth = layout.width;
913
- const visualHeight = layout.height;
914
- const visualRadius = radius * scale;
915
-
916
- // Scale Features
917
- const absoluteFeatures = (features || []).map((f) => {
918
- const featureScale = scale;
919
-
920
- return {
921
- ...f,
922
- x: f.x,
923
- y: f.y,
924
- width: (f.width || 0) * featureScale,
925
- height: (f.height || 0) * featureScale,
926
- radius: (f.radius || 0) * featureScale,
927
- };
928
- });
929
-
930
- const cutFeatures = absoluteFeatures.filter((f) => !f.skipCut);
931
-
932
- const generatedPathData = generateDielinePath({
933
- shape,
934
- width: visualWidth,
935
- height: visualHeight,
936
- radius: visualRadius,
937
- x: cx,
938
- y: cy,
939
- features: cutFeatures,
940
- pathData,
941
- canvasWidth: canvasW,
942
- canvasHeight: canvasH,
943
- });
944
-
945
- // 2. Prepare for Export
946
- const clonedLayer = await userLayer.clone();
947
-
948
- const clipPath = new Path(generatedPathData, {
949
- originX: "left",
950
- originY: "top",
951
- left: 0,
952
- top: 0,
953
- absolutePositioned: true,
954
- });
955
-
956
- clonedLayer.clipPath = clipPath;
957
-
958
- // 3. Calculate Crop Area (The Dieline Bounds)
959
- const bounds = clipPath.getBoundingRect();
960
-
961
- // 4. Export
962
- const dataUrl = clonedLayer.toDataURL({
963
- format: "png",
964
- multiplier: 2,
965
- left: bounds.left,
966
- top: bounds.top,
967
- width: bounds.width,
968
- height: bounds.height,
969
- });
970
-
971
- return dataUrl;
972
- }
973
- }
1
+ import {
2
+ Extension,
3
+ ExtensionContext,
4
+ ContributionPointIds,
5
+ CommandContribution,
6
+ ConfigurationContribution,
7
+ ConfigurationService,
8
+ } from "@pooder/core";
9
+ import { Path, Pattern } from "fabric";
10
+ import CanvasService from "./CanvasService";
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
+ export class DielineTool implements Extension {
67
+ id = "pooder.kit.dieline";
68
+ public metadata = {
69
+ name: "DielineTool",
70
+ };
71
+
72
+ private state: DielineState = {
73
+ displayUnit: "mm",
74
+ shape: "rect",
75
+ width: 500,
76
+ height: 500,
77
+ radius: 0,
78
+ offset: 0,
79
+ padding: 140,
80
+ mainLine: {
81
+ width: 2.7,
82
+ color: "#FF0000",
83
+ dashLength: 5,
84
+ style: "solid",
85
+ },
86
+ offsetLine: {
87
+ width: 2.7,
88
+ color: "#FF0000",
89
+ dashLength: 5,
90
+ style: "solid",
91
+ },
92
+ insideColor: "rgba(0,0,0,0)",
93
+ outsideColor: "#ffffff",
94
+ showBleedLines: true,
95
+ features: [],
96
+ };
97
+
98
+ private canvasService?: CanvasService;
99
+ private context?: ExtensionContext;
100
+ private onCanvasResized = () => {
101
+ this.updateDieline();
102
+ };
103
+
104
+ constructor(options?: Partial<DielineState>) {
105
+ if (options) {
106
+ // Deep merge for styles to avoid overwriting defaults with partial objects
107
+ if (options.mainLine) {
108
+ Object.assign(this.state.mainLine, options.mainLine);
109
+ delete options.mainLine;
110
+ }
111
+ if (options.offsetLine) {
112
+ Object.assign(this.state.offsetLine, options.offsetLine);
113
+ delete options.offsetLine;
114
+ }
115
+ Object.assign(this.state, options);
116
+ }
117
+ }
118
+
119
+ activate(context: ExtensionContext) {
120
+ this.context = context;
121
+ this.canvasService = context.services.get<CanvasService>("CanvasService");
122
+ if (!this.canvasService) {
123
+ console.warn("CanvasService not found for DielineTool");
124
+ return;
125
+ }
126
+
127
+ const configService = context.services.get<ConfigurationService>(
128
+ "ConfigurationService",
129
+ );
130
+ if (configService) {
131
+ // Load initial config
132
+ const s = this.state;
133
+ const sizeState = readSizeState(configService);
134
+ s.displayUnit = sizeState.unit;
135
+ s.shape = configService.get("dieline.shape", s.shape);
136
+ s.width = sizeState.actualWidthMm;
137
+ s.height = sizeState.actualHeightMm;
138
+ s.radius = parseLengthToMm(
139
+ configService.get("dieline.radius", s.radius),
140
+ "mm",
141
+ );
142
+ s.padding = sizeState.viewPadding;
143
+ s.offset =
144
+ sizeState.cutMode === "outset"
145
+ ? sizeState.cutMarginMm
146
+ : sizeState.cutMode === "inset"
147
+ ? -sizeState.cutMarginMm
148
+ : 0;
149
+
150
+ // Main Line
151
+ s.mainLine.width = configService.get(
152
+ "dieline.strokeWidth",
153
+ s.mainLine.width,
154
+ );
155
+ s.mainLine.color = configService.get(
156
+ "dieline.strokeColor",
157
+ s.mainLine.color,
158
+ );
159
+ s.mainLine.dashLength = configService.get(
160
+ "dieline.dashLength",
161
+ s.mainLine.dashLength,
162
+ );
163
+ s.mainLine.style = configService.get("dieline.style", s.mainLine.style);
164
+
165
+ // Offset Line
166
+ s.offsetLine.width = configService.get(
167
+ "dieline.offsetStrokeWidth",
168
+ s.offsetLine.width,
169
+ );
170
+ s.offsetLine.color = configService.get(
171
+ "dieline.offsetStrokeColor",
172
+ s.offsetLine.color,
173
+ );
174
+ s.offsetLine.dashLength = configService.get(
175
+ "dieline.offsetDashLength",
176
+ s.offsetLine.dashLength,
177
+ );
178
+ s.offsetLine.style = configService.get(
179
+ "dieline.offsetStyle",
180
+ s.offsetLine.style,
181
+ );
182
+
183
+ s.insideColor = configService.get("dieline.insideColor", s.insideColor);
184
+ s.outsideColor = configService.get(
185
+ "dieline.outsideColor",
186
+ s.outsideColor,
187
+ );
188
+ s.showBleedLines = configService.get(
189
+ "dieline.showBleedLines",
190
+ s.showBleedLines,
191
+ );
192
+ s.features = configService.get("dieline.features", s.features);
193
+ s.pathData = configService.get("dieline.pathData", s.pathData);
194
+
195
+ // Listen for changes
196
+ configService.onAnyChange((e: { key: string; value: any }) => {
197
+ if (e.key.startsWith("size.")) {
198
+ const nextSize = readSizeState(configService);
199
+ s.displayUnit = nextSize.unit;
200
+ s.width = nextSize.actualWidthMm;
201
+ s.height = nextSize.actualHeightMm;
202
+ s.padding = nextSize.viewPadding;
203
+ s.offset =
204
+ nextSize.cutMode === "outset"
205
+ ? nextSize.cutMarginMm
206
+ : nextSize.cutMode === "inset"
207
+ ? -nextSize.cutMarginMm
208
+ : 0;
209
+ this.updateDieline();
210
+ return;
211
+ }
212
+
213
+ if (e.key.startsWith("dieline.")) {
214
+ switch (e.key) {
215
+ case "dieline.shape":
216
+ s.shape = e.value;
217
+ break;
218
+ case "dieline.radius":
219
+ s.radius = parseLengthToMm(e.value, "mm");
220
+ break;
221
+
222
+ case "dieline.strokeWidth":
223
+ s.mainLine.width = e.value;
224
+ break;
225
+ case "dieline.strokeColor":
226
+ s.mainLine.color = e.value;
227
+ break;
228
+ case "dieline.dashLength":
229
+ s.mainLine.dashLength = e.value;
230
+ break;
231
+ case "dieline.style":
232
+ s.mainLine.style = e.value;
233
+ break;
234
+
235
+ case "dieline.offsetStrokeWidth":
236
+ s.offsetLine.width = e.value;
237
+ break;
238
+ case "dieline.offsetStrokeColor":
239
+ s.offsetLine.color = e.value;
240
+ break;
241
+ case "dieline.offsetDashLength":
242
+ s.offsetLine.dashLength = e.value;
243
+ break;
244
+ case "dieline.offsetStyle":
245
+ s.offsetLine.style = e.value;
246
+ break;
247
+
248
+ case "dieline.insideColor":
249
+ s.insideColor = e.value;
250
+ break;
251
+ case "dieline.outsideColor":
252
+ s.outsideColor = e.value;
253
+ break;
254
+ case "dieline.showBleedLines":
255
+ s.showBleedLines = e.value;
256
+ break;
257
+ case "dieline.features":
258
+ s.features = e.value;
259
+ break;
260
+ case "dieline.pathData":
261
+ s.pathData = e.value;
262
+ break;
263
+ }
264
+ this.updateDieline();
265
+ }
266
+ });
267
+ }
268
+
269
+ context.eventBus.on("canvas:resized", this.onCanvasResized);
270
+ this.createLayer();
271
+ this.updateDieline();
272
+ }
273
+
274
+ deactivate(context: ExtensionContext) {
275
+ context.eventBus.off("canvas:resized", this.onCanvasResized);
276
+ this.destroyLayer();
277
+ this.canvasService = undefined;
278
+ this.context = undefined;
279
+ }
280
+
281
+ contribute() {
282
+ const s = this.state;
283
+ return {
284
+ [ContributionPointIds.TOOLS]: [
285
+ {
286
+ id: this.id,
287
+ name: "Dieline",
288
+ interaction: "session",
289
+ session: {
290
+ autoBegin: false,
291
+ leavePolicy: "block",
292
+ },
293
+ },
294
+ ],
295
+ [ContributionPointIds.CONFIGURATIONS]: [
296
+ {
297
+ id: "dieline.shape",
298
+ type: "select",
299
+ label: "Shape",
300
+ options: ["rect", "circle", "ellipse", "custom"],
301
+ default: s.shape,
302
+ },
303
+ {
304
+ id: "dieline.radius",
305
+ type: "number",
306
+ label: "Corner Radius (mm)",
307
+ min: 0,
308
+ max: 500,
309
+ default: s.radius,
310
+ },
311
+ {
312
+ id: "dieline.showBleedLines",
313
+ type: "boolean",
314
+ label: "Show Bleed Lines",
315
+ default: s.showBleedLines,
316
+ },
317
+ {
318
+ id: "dieline.strokeWidth",
319
+ type: "number",
320
+ label: "Line Width",
321
+ min: 0.1,
322
+ max: 10,
323
+ step: 0.1,
324
+ default: s.mainLine.width,
325
+ },
326
+ {
327
+ id: "dieline.strokeColor",
328
+ type: "color",
329
+ label: "Line Color",
330
+ default: s.mainLine.color,
331
+ },
332
+ {
333
+ id: "dieline.dashLength",
334
+ type: "number",
335
+ label: "Dash Length",
336
+ min: 1,
337
+ max: 50,
338
+ default: s.mainLine.dashLength,
339
+ },
340
+ {
341
+ id: "dieline.style",
342
+ type: "select",
343
+ label: "Line Style",
344
+ options: ["solid", "dashed", "hidden"],
345
+ default: s.mainLine.style,
346
+ },
347
+ {
348
+ id: "dieline.offsetStrokeWidth",
349
+ type: "number",
350
+ label: "Offset Line Width",
351
+ min: 0.1,
352
+ max: 10,
353
+ step: 0.1,
354
+ default: s.offsetLine.width,
355
+ },
356
+ {
357
+ id: "dieline.offsetStrokeColor",
358
+ type: "color",
359
+ label: "Offset Line Color",
360
+ default: s.offsetLine.color,
361
+ },
362
+ {
363
+ id: "dieline.offsetDashLength",
364
+ type: "number",
365
+ label: "Offset Dash Length",
366
+ min: 1,
367
+ max: 50,
368
+ default: s.offsetLine.dashLength,
369
+ },
370
+ {
371
+ id: "dieline.offsetStyle",
372
+ type: "select",
373
+ label: "Offset Line Style",
374
+ options: ["solid", "dashed", "hidden"],
375
+ default: s.offsetLine.style,
376
+ },
377
+ {
378
+ id: "dieline.insideColor",
379
+ type: "color",
380
+ label: "Inside Color",
381
+ default: s.insideColor,
382
+ },
383
+ {
384
+ id: "dieline.outsideColor",
385
+ type: "color",
386
+ label: "Outside Color",
387
+ default: s.outsideColor,
388
+ },
389
+ {
390
+ id: "dieline.features",
391
+ type: "json",
392
+ label: "Edge Features",
393
+ default: s.features,
394
+ },
395
+ ] as ConfigurationContribution[],
396
+ [ContributionPointIds.COMMANDS]: [
397
+ {
398
+ command: "updateFeaturePosition",
399
+ title: "Update Feature Position",
400
+ handler: (groupId: string, x: number, y: number) => {
401
+ const configService = this.context?.services.get<any>(
402
+ "ConfigurationService",
403
+ );
404
+ if (!configService) return;
405
+
406
+ const features = configService.get("dieline.features") || [];
407
+
408
+ let changed = false;
409
+ const newFeatures = features.map((f: any) => {
410
+ if (f.groupId === groupId) {
411
+ if (f.x !== x || f.y !== y) {
412
+ changed = true;
413
+ return { ...f, x, y };
414
+ }
415
+ }
416
+ return f;
417
+ });
418
+
419
+ if (changed) {
420
+ configService.update("dieline.features", newFeatures);
421
+ }
422
+ },
423
+ },
424
+ {
425
+ command: "exportCutImage",
426
+ title: "Export Cut Image",
427
+ handler: () => {
428
+ return this.exportCutImage();
429
+ },
430
+ },
431
+ {
432
+ command: "detectEdge",
433
+ title: "Detect Edge from Image",
434
+ handler: async (imageUrl: string, options?: any) => {
435
+ try {
436
+ const detectOptions = options || {};
437
+ const debug = detectOptions.debug === true;
438
+
439
+ // Helper to get image dimensions
440
+ const loadImage = (url: string): Promise<HTMLImageElement> => {
441
+ return new Promise((resolve, reject) => {
442
+ const img = new Image();
443
+ img.crossOrigin = "Anonymous";
444
+ img.onload = () => resolve(img);
445
+ img.onerror = (e) => reject(e);
446
+ img.src = url;
447
+ });
448
+ };
449
+
450
+ const [img, traced] = await Promise.all([
451
+ loadImage(imageUrl),
452
+ ImageTracer.traceWithBounds(imageUrl, detectOptions),
453
+ ]);
454
+ const { pathData, baseBounds, bounds } = traced;
455
+
456
+ if (debug) {
457
+ console.info("[DielineTool] detectEdge", {
458
+ imageWidth: img.width,
459
+ imageHeight: img.height,
460
+ baseBounds,
461
+ expandedBounds: bounds,
462
+ currentDielineWidth: s.width,
463
+ currentDielineHeight: s.height,
464
+ options: {
465
+ expand: detectOptions.expand ?? 0,
466
+ morphologyRadius: detectOptions.morphologyRadius,
467
+ smoothing: detectOptions.smoothing,
468
+ simplifyTolerance: detectOptions.simplifyTolerance,
469
+ threshold: detectOptions.threshold,
470
+ },
471
+ });
472
+ }
473
+
474
+ return {
475
+ pathData,
476
+ rawBounds: bounds,
477
+ baseBounds,
478
+ imageWidth: img.width,
479
+ imageHeight: img.height,
480
+ };
481
+ } catch (e) {
482
+ console.error("Edge detection failed", e);
483
+ throw e;
484
+ }
485
+ },
486
+ },
487
+ ] as CommandContribution[],
488
+ };
489
+ }
490
+
491
+ private getLayer() {
492
+ return this.canvasService?.getLayer("dieline-overlay");
493
+ }
494
+
495
+ private createLayer() {
496
+ if (!this.canvasService) return;
497
+ const width = this.canvasService.canvas.width || 800;
498
+ const height = this.canvasService.canvas.height || 600;
499
+
500
+ const layer = this.canvasService.createLayer("dieline-overlay", {
501
+ width,
502
+ height,
503
+ selectable: false,
504
+ evented: false,
505
+ });
506
+
507
+ this.canvasService.canvas.bringObjectToFront(layer);
508
+
509
+ // Ensure above user layer
510
+ const userLayer = this.canvasService.getLayer("user");
511
+ if (userLayer) {
512
+ const userIndex = this.canvasService.canvas
513
+ .getObjects()
514
+ .indexOf(userLayer);
515
+ this.canvasService.canvas.moveObjectTo(layer, userIndex + 1);
516
+ }
517
+ }
518
+
519
+ private destroyLayer() {
520
+ if (!this.canvasService) return;
521
+ const layer = this.getLayer();
522
+ if (layer) {
523
+ this.canvasService.canvas.remove(layer);
524
+ }
525
+ }
526
+
527
+ private createHatchPattern(color: string = "rgba(0, 0, 0, 0.3)") {
528
+ if (typeof document === "undefined") {
529
+ return undefined;
530
+ }
531
+ const size = 20;
532
+ const canvas = document.createElement("canvas");
533
+ canvas.width = size;
534
+ canvas.height = size;
535
+ const ctx = canvas.getContext("2d");
536
+ if (ctx) {
537
+ // Transparent background
538
+ ctx.clearRect(0, 0, size, size);
539
+
540
+ // Draw diagonal /
541
+ ctx.strokeStyle = color;
542
+ ctx.lineWidth = 1;
543
+ ctx.beginPath();
544
+ ctx.moveTo(0, size);
545
+ ctx.lineTo(size, 0);
546
+ ctx.stroke();
547
+ }
548
+ // @ts-ignore
549
+ return new Pattern({ source: canvas, repetition: "repeat" });
550
+ }
551
+
552
+ private getConfigService(): ConfigurationService | undefined {
553
+ return this.context?.services.get<ConfigurationService>("ConfigurationService");
554
+ }
555
+
556
+ private syncSizeState(configService: ConfigurationService) {
557
+ const sizeState = readSizeState(configService);
558
+ this.state.displayUnit = sizeState.unit;
559
+ this.state.width = sizeState.actualWidthMm;
560
+ this.state.height = sizeState.actualHeightMm;
561
+ this.state.padding = sizeState.viewPadding;
562
+ this.state.offset =
563
+ sizeState.cutMode === "outset"
564
+ ? sizeState.cutMarginMm
565
+ : sizeState.cutMode === "inset"
566
+ ? -sizeState.cutMarginMm
567
+ : 0;
568
+ }
569
+
570
+ private bringFeatureMarkersToFront() {
571
+ if (!this.canvasService) return;
572
+ const canvas = this.canvasService.canvas;
573
+ canvas
574
+ .getObjects()
575
+ .filter((obj: any) => obj?.data?.type === "feature-marker")
576
+ .forEach((obj: any) => canvas.bringObjectToFront(obj));
577
+ }
578
+
579
+ public updateDieline(_emitEvent: boolean = true) {
580
+ if (!this.canvasService) return;
581
+ const layer = this.getLayer();
582
+ if (!layer) return;
583
+ const configService = this.getConfigService();
584
+ if (!configService) return;
585
+
586
+ this.syncSizeState(configService);
587
+ const sceneLayout = computeSceneLayout(
588
+ this.canvasService,
589
+ readSizeState(configService),
590
+ );
591
+ if (!sceneLayout) return;
592
+
593
+ const {
594
+ shape,
595
+ radius,
596
+ mainLine,
597
+ offsetLine,
598
+ insideColor,
599
+ outsideColor,
600
+ showBleedLines,
601
+ features,
602
+ } = this.state;
603
+
604
+ const canvasW = sceneLayout.canvasWidth || this.canvasService.canvas.width || 800;
605
+ const canvasH = sceneLayout.canvasHeight || this.canvasService.canvas.height || 600;
606
+ const scale = sceneLayout.scale;
607
+ const cx = sceneLayout.trimRect.centerX;
608
+ const cy = sceneLayout.trimRect.centerY;
609
+
610
+ const visualWidth = sceneLayout.trimRect.width;
611
+ const visualHeight = sceneLayout.trimRect.height;
612
+ const visualRadius = radius * scale;
613
+ const cutW = sceneLayout.cutRect.width;
614
+ const cutH = sceneLayout.cutRect.height;
615
+ const visualOffset = (cutW - visualWidth) / 2;
616
+ const cutR =
617
+ visualRadius === 0 ? 0 : Math.max(0, visualRadius + visualOffset);
618
+
619
+ layer.remove(...layer.getObjects());
620
+
621
+ const absoluteFeatures = (features || []).map((f) => ({
622
+ ...f,
623
+ x: f.x,
624
+ y: f.y,
625
+ width: (f.width || 0) * scale,
626
+ height: (f.height || 0) * scale,
627
+ radius: (f.radius || 0) * scale,
628
+ }));
629
+ const cutFeatures = absoluteFeatures.filter((f) => !f.skipCut);
630
+
631
+ const maskPathData = generateMaskPath({
632
+ canvasWidth: canvasW,
633
+ canvasHeight: canvasH,
634
+ shape,
635
+ width: cutW,
636
+ height: cutH,
637
+ radius: cutR,
638
+ x: cx,
639
+ y: cy,
640
+ features: cutFeatures,
641
+ pathData: this.state.pathData,
642
+ });
643
+ const mask = new Path(maskPathData, {
644
+ fill: outsideColor,
645
+ stroke: null,
646
+ selectable: false,
647
+ evented: false,
648
+ originX: "left" as const,
649
+ originY: "top" as const,
650
+ left: 0,
651
+ top: 0,
652
+ });
653
+ layer.add(mask);
654
+
655
+ if (
656
+ insideColor &&
657
+ insideColor !== "transparent" &&
658
+ insideColor !== "rgba(0,0,0,0)"
659
+ ) {
660
+ const productPathData = generateDielinePath({
661
+ shape,
662
+ width: cutW,
663
+ height: cutH,
664
+ radius: cutR,
665
+ x: cx,
666
+ y: cy,
667
+ features: cutFeatures,
668
+ pathData: this.state.pathData,
669
+ canvasWidth: canvasW,
670
+ canvasHeight: canvasH,
671
+ });
672
+
673
+ const insideObj = new Path(productPathData, {
674
+ fill: insideColor,
675
+ stroke: null,
676
+ selectable: false,
677
+ evented: false,
678
+ originX: "left",
679
+ originY: "top",
680
+ });
681
+ layer.add(insideObj);
682
+ }
683
+
684
+ if (Math.abs(visualOffset) > 0.0001) {
685
+ const bleedPathData = generateBleedZonePath(
686
+ {
687
+ shape,
688
+ width: visualWidth,
689
+ height: visualHeight,
690
+ radius: visualRadius,
691
+ x: cx,
692
+ y: cy,
693
+ features: cutFeatures,
694
+ pathData: this.state.pathData,
695
+ canvasWidth: canvasW,
696
+ canvasHeight: canvasH,
697
+ },
698
+ {
699
+ shape,
700
+ width: cutW,
701
+ height: cutH,
702
+ radius: cutR,
703
+ x: cx,
704
+ y: cy,
705
+ features: cutFeatures,
706
+ pathData: this.state.pathData,
707
+ canvasWidth: canvasW,
708
+ canvasHeight: canvasH,
709
+ },
710
+ visualOffset,
711
+ );
712
+
713
+ if (showBleedLines !== false) {
714
+ const pattern = this.createHatchPattern(mainLine.color);
715
+ if (pattern) {
716
+ const bleedObj = new Path(bleedPathData, {
717
+ fill: pattern,
718
+ stroke: null,
719
+ selectable: false,
720
+ evented: false,
721
+ objectCaching: false,
722
+ originX: "left",
723
+ originY: "top",
724
+ });
725
+ layer.add(bleedObj);
726
+ }
727
+ }
728
+
729
+ const offsetPathData = generateDielinePath({
730
+ shape,
731
+ width: cutW,
732
+ height: cutH,
733
+ radius: cutR,
734
+ x: cx,
735
+ y: cy,
736
+ features: cutFeatures,
737
+ pathData: this.state.pathData,
738
+ canvasWidth: canvasW,
739
+ canvasHeight: canvasH,
740
+ });
741
+
742
+ const offsetBorderObj = new Path(offsetPathData, {
743
+ fill: null,
744
+ stroke: offsetLine.style === "hidden" ? null : offsetLine.color,
745
+ strokeWidth: offsetLine.width,
746
+ strokeDashArray:
747
+ offsetLine.style === "dashed"
748
+ ? [offsetLine.dashLength, offsetLine.dashLength]
749
+ : undefined,
750
+ selectable: false,
751
+ evented: false,
752
+ originX: "left",
753
+ originY: "top",
754
+ });
755
+ layer.add(offsetBorderObj);
756
+ }
757
+
758
+ const borderPathData = generateDielinePath({
759
+ shape,
760
+ width: visualWidth,
761
+ height: visualHeight,
762
+ radius: visualRadius,
763
+ x: cx,
764
+ y: cy,
765
+ features: absoluteFeatures,
766
+ pathData: this.state.pathData,
767
+ canvasWidth: canvasW,
768
+ canvasHeight: canvasH,
769
+ });
770
+ const borderObj = new Path(borderPathData, {
771
+ fill: "transparent",
772
+ stroke: mainLine.style === "hidden" ? null : mainLine.color,
773
+ strokeWidth: mainLine.width,
774
+ strokeDashArray:
775
+ mainLine.style === "dashed"
776
+ ? [mainLine.dashLength, mainLine.dashLength]
777
+ : undefined,
778
+ selectable: false,
779
+ evented: false,
780
+ originX: "left",
781
+ originY: "top",
782
+ });
783
+ layer.add(borderObj);
784
+
785
+ const userLayer = this.canvasService.getLayer("user");
786
+ if (layer && userLayer) {
787
+ const layerIndex = this.canvasService.canvas.getObjects().indexOf(layer);
788
+ const userIndex = this.canvasService.canvas
789
+ .getObjects()
790
+ .indexOf(userLayer);
791
+ if (layerIndex < userIndex) {
792
+ this.canvasService.canvas.moveObjectTo(layer, userIndex + 1);
793
+ }
794
+ } else {
795
+ this.canvasService.canvas.bringObjectToFront(layer);
796
+ }
797
+
798
+ // Feature tool markers can extend outside trim. Keep them above dieline mask.
799
+ this.bringFeatureMarkersToFront();
800
+
801
+ const rulerLayer = this.canvasService.getLayer("ruler-overlay");
802
+ if (rulerLayer) {
803
+ this.canvasService.canvas.bringObjectToFront(rulerLayer);
804
+ }
805
+
806
+ layer.dirty = true;
807
+ this.canvasService.requestRenderAll();
808
+ }
809
+
810
+ public getGeometry(): DielineGeometry | null {
811
+ if (!this.canvasService) return null;
812
+ const configService = this.getConfigService();
813
+ if (!configService) return null;
814
+ const sceneLayout = computeSceneLayout(
815
+ this.canvasService,
816
+ readSizeState(configService),
817
+ );
818
+ if (!sceneLayout) return null;
819
+ const sceneGeometry = buildSceneGeometry(configService, sceneLayout);
820
+ return {
821
+ ...sceneGeometry,
822
+ strokeWidth: this.state.mainLine.width,
823
+ pathData: this.state.pathData,
824
+ } as DielineGeometry;
825
+ }
826
+
827
+ public async exportCutImage() {
828
+ if (!this.canvasService) return null;
829
+ const configService = this.getConfigService();
830
+ if (!configService) return null;
831
+ const userLayer = this.canvasService.getLayer("user");
832
+ if (!userLayer) return null;
833
+
834
+ this.syncSizeState(configService);
835
+ const sceneLayout = computeSceneLayout(
836
+ this.canvasService,
837
+ readSizeState(configService),
838
+ );
839
+ if (!sceneLayout) return null;
840
+
841
+ const { shape, radius, features, pathData } = this.state;
842
+ const canvasW = sceneLayout.canvasWidth || this.canvasService.canvas.width || 800;
843
+ const canvasH = sceneLayout.canvasHeight || this.canvasService.canvas.height || 600;
844
+ const scale = sceneLayout.scale;
845
+ const cx = sceneLayout.trimRect.centerX;
846
+ const cy = sceneLayout.trimRect.centerY;
847
+ const cutW = sceneLayout.cutRect.width;
848
+ const cutH = sceneLayout.cutRect.height;
849
+ const visualRadius = radius * scale;
850
+ const visualOffset = (cutW - sceneLayout.trimRect.width) / 2;
851
+ const cutR =
852
+ visualRadius === 0 ? 0 : Math.max(0, visualRadius + visualOffset);
853
+
854
+ const absoluteFeatures = (features || []).map((f) => ({
855
+ ...f,
856
+ x: f.x,
857
+ y: f.y,
858
+ width: (f.width || 0) * scale,
859
+ height: (f.height || 0) * scale,
860
+ radius: (f.radius || 0) * scale,
861
+ }));
862
+ const cutFeatures = absoluteFeatures.filter((f) => !f.skipCut);
863
+
864
+ const generatedPathData = generateDielinePath({
865
+ shape,
866
+ width: cutW,
867
+ height: cutH,
868
+ radius: cutR,
869
+ x: cx,
870
+ y: cy,
871
+ features: cutFeatures,
872
+ pathData,
873
+ canvasWidth: canvasW,
874
+ canvasHeight: canvasH,
875
+ });
876
+
877
+ const clonedLayer = await userLayer.clone();
878
+ const clipPath = new Path(generatedPathData, {
879
+ originX: "left",
880
+ originY: "top",
881
+ left: 0,
882
+ top: 0,
883
+ absolutePositioned: true,
884
+ });
885
+ clonedLayer.clipPath = clipPath;
886
+
887
+ const bounds = clipPath.getBoundingRect();
888
+ return clonedLayer.toDataURL({
889
+ format: "png",
890
+ multiplier: 2,
891
+ left: bounds.left,
892
+ top: bounds.top,
893
+ width: bounds.width,
894
+ height: bounds.height,
895
+ });
896
+ }
897
+ }