@pooder/kit 4.3.1 → 5.0.1

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