@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
package/src/ruler.ts CHANGED
@@ -1,508 +1,449 @@
1
- import {
2
- Extension,
3
- ExtensionContext,
4
- ContributionPointIds,
5
- CommandContribution,
6
- ConfigurationContribution,
7
- } from "@pooder/core";
8
- import { Rect, Line, Text, Group, Polygon } from "fabric";
9
- import CanvasService from "./CanvasService";
10
- import { Unit } from "./coordinate";
11
- import { formatMm } from "./units";
12
-
13
- export class RulerTool implements Extension {
14
- id = "pooder.kit.ruler";
15
-
16
- public metadata = {
17
- name: "RulerTool",
18
- };
19
-
20
- private thickness: number = 20;
21
- private gap: number = 15;
22
- private backgroundColor: string = "#f0f0f0";
23
- private textColor: string = "#333333";
24
- private lineColor: string = "#999999";
25
- private fontSize: number = 10;
26
-
27
- // Dieline context for sync
28
- private dielineWidth: number = 500;
29
- private dielineHeight: number = 500;
30
- private dielineDisplayUnit: Unit = "mm";
31
- private dielinePadding: number | string = 40;
32
- private dielineOffset: number = 0;
33
-
34
- private canvasService?: CanvasService;
35
-
36
- constructor(
37
- options?: Partial<{
38
- thickness: number;
39
- backgroundColor: string;
40
- textColor: string;
41
- lineColor: string;
42
- fontSize: number;
43
- }>,
44
- ) {
45
- if (options) {
46
- Object.assign(this, options);
47
- }
48
- }
49
-
50
- activate(context: ExtensionContext) {
51
- this.canvasService = context.services.get<CanvasService>("CanvasService");
52
- if (!this.canvasService) {
53
- console.warn("CanvasService not found for RulerTool");
54
- return;
55
- }
56
-
57
- const configService = context.services.get<any>("ConfigurationService");
58
- if (configService) {
59
- // Load initial config
60
- this.thickness = configService.get("ruler.thickness", this.thickness);
61
- this.gap = configService.get("ruler.gap", this.gap);
62
- this.backgroundColor = configService.get(
63
- "ruler.backgroundColor",
64
- this.backgroundColor,
65
- );
66
- this.textColor = configService.get("ruler.textColor", this.textColor);
67
- this.lineColor = configService.get("ruler.lineColor", this.lineColor);
68
- this.fontSize = configService.get("ruler.fontSize", this.fontSize);
69
-
70
- // Load Dieline Config
71
- this.dielineDisplayUnit = configService.get(
72
- "dieline.displayUnit",
73
- this.dielineDisplayUnit,
74
- );
75
- this.dielineWidth = configService.get("dieline.width", this.dielineWidth);
76
- this.dielineHeight = configService.get(
77
- "dieline.height",
78
- this.dielineHeight,
79
- );
80
- this.dielinePadding = configService.get(
81
- "dieline.padding",
82
- this.dielinePadding,
83
- );
84
- this.dielineOffset = configService.get(
85
- "dieline.offset",
86
- this.dielineOffset,
87
- );
88
-
89
- // Listen for changes
90
- configService.onAnyChange((e: { key: string; value: any }) => {
91
- let shouldUpdate = false;
92
- if (e.key.startsWith("ruler.")) {
93
- const prop = e.key.split(".")[1];
94
- if (prop && prop in this) {
95
- (this as any)[prop] = e.value;
96
- shouldUpdate = true;
97
- }
98
- } else if (e.key.startsWith("dieline.")) {
99
- if (e.key === "dieline.displayUnit")
100
- this.dielineDisplayUnit = e.value;
101
- if (e.key === "dieline.width") this.dielineWidth = e.value;
102
- if (e.key === "dieline.height") this.dielineHeight = e.value;
103
- if (e.key === "dieline.padding") this.dielinePadding = e.value;
104
- if (e.key === "dieline.offset") this.dielineOffset = e.value;
105
- shouldUpdate = true;
106
- }
107
-
108
- if (shouldUpdate) {
109
- this.updateRuler();
110
- }
111
- });
112
- }
113
-
114
- this.createLayer();
115
- this.updateRuler();
116
- }
117
-
118
- deactivate(context: ExtensionContext) {
119
- this.destroyLayer();
120
- this.canvasService = undefined;
121
- }
122
-
123
- contribute() {
124
- return {
125
- [ContributionPointIds.CONFIGURATIONS]: [
126
- {
127
- id: "ruler.thickness",
128
- type: "number",
129
- label: "Thickness",
130
- min: 10,
131
- max: 100,
132
- default: 20,
133
- },
134
- {
135
- id: "ruler.gap",
136
- type: "number",
137
- label: "Gap",
138
- min: 0,
139
- max: 100,
140
- default: 15,
141
- },
142
- {
143
- id: "ruler.backgroundColor",
144
- type: "color",
145
- label: "Background Color",
146
- default: "#f0f0f0",
147
- },
148
- {
149
- id: "ruler.textColor",
150
- type: "color",
151
- label: "Text Color",
152
- default: "#333333",
153
- },
154
- {
155
- id: "ruler.lineColor",
156
- type: "color",
157
- label: "Line Color",
158
- default: "#999999",
159
- },
160
- {
161
- id: "ruler.fontSize",
162
- type: "number",
163
- label: "Font Size",
164
- min: 8,
165
- max: 24,
166
- default: 10,
167
- },
168
- ] as ConfigurationContribution[],
169
- [ContributionPointIds.COMMANDS]: [
170
- {
171
- command: "setTheme",
172
- title: "Set Ruler Theme",
173
- handler: (
174
- theme: Partial<{
175
- backgroundColor: string;
176
- textColor: string;
177
- lineColor: string;
178
- fontSize: number;
179
- thickness: number;
180
- }>,
181
- ) => {
182
- const oldState = {
183
- backgroundColor: this.backgroundColor,
184
- textColor: this.textColor,
185
- lineColor: this.lineColor,
186
- fontSize: this.fontSize,
187
- thickness: this.thickness,
188
- };
189
- const newState = { ...oldState, ...theme };
190
- if (JSON.stringify(newState) === JSON.stringify(oldState))
191
- return true;
192
-
193
- Object.assign(this, newState);
194
- this.updateRuler();
195
- return true;
196
- },
197
- },
198
- ] as CommandContribution[],
199
- };
200
- }
201
-
202
- private getLayer() {
203
- return this.canvasService?.getLayer("ruler-overlay");
204
- }
205
-
206
- private createLayer() {
207
- if (!this.canvasService) return;
208
-
209
- const canvas = this.canvasService.canvas;
210
- const width = canvas.width || 800;
211
- const height = canvas.height || 600;
212
-
213
- const layer = this.canvasService.createLayer("ruler-overlay", {
214
- width,
215
- height,
216
- selectable: false,
217
- evented: false,
218
- left: 0,
219
- top: 0,
220
- originX: "left",
221
- originY: "top",
222
- });
223
-
224
- canvas.bringObjectToFront(layer);
225
- }
226
-
227
- private destroyLayer() {
228
- if (!this.canvasService) return;
229
- const layer = this.getLayer();
230
- if (layer) {
231
- this.canvasService.canvas.remove(layer);
232
- }
233
- }
234
-
235
- private createArrowLine(
236
- x1: number,
237
- y1: number,
238
- x2: number,
239
- y2: number,
240
- color: string,
241
- ): Group {
242
- const line = new Line([x1, y1, x2, y2], {
243
- stroke: color,
244
- strokeWidth: this.thickness / 20, // Scale stroke width relative to thickness (default 1)
245
- selectable: false,
246
- evented: false,
247
- });
248
-
249
- // Arrow size proportional to thickness
250
- const arrowSize = Math.max(4, this.thickness * 0.3);
251
- const angle = Math.atan2(y2 - y1, x2 - x1);
252
-
253
- // End Arrow (at x2, y2)
254
- const endArrow = new Polygon(
255
- [
256
- { x: 0, y: 0 },
257
- { x: -arrowSize, y: -arrowSize / 2 },
258
- { x: -arrowSize, y: arrowSize / 2 },
259
- ],
260
- {
261
- fill: color,
262
- left: x2,
263
- top: y2,
264
- originX: "right",
265
- originY: "center",
266
- angle: (angle * 180) / Math.PI,
267
- selectable: false,
268
- evented: false,
269
- },
270
- );
271
-
272
- // Start Arrow (at x1, y1)
273
- const startArrow = new Polygon(
274
- [
275
- { x: 0, y: 0 },
276
- { x: arrowSize, y: -arrowSize / 2 },
277
- { x: arrowSize, y: arrowSize / 2 },
278
- ],
279
- {
280
- fill: color,
281
- left: x1,
282
- top: y1,
283
- originX: "left",
284
- originY: "center",
285
- angle: (angle * 180) / Math.PI,
286
- selectable: false,
287
- evented: false,
288
- },
289
- );
290
-
291
- return new Group([line, startArrow, endArrow], {
292
- selectable: false,
293
- evented: false,
294
- });
295
- }
296
-
297
- private resolvePadding(
298
- containerWidth: number,
299
- containerHeight: number,
300
- ): number {
301
- if (typeof this.dielinePadding === "number") {
302
- return this.dielinePadding;
303
- }
304
- if (typeof this.dielinePadding === "string") {
305
- if (this.dielinePadding.endsWith("%")) {
306
- const percent = parseFloat(this.dielinePadding) / 100;
307
- return Math.min(containerWidth, containerHeight) * percent;
308
- }
309
- return parseFloat(this.dielinePadding) || 0;
310
- }
311
- return 0;
312
- }
313
-
314
- private updateRuler() {
315
- if (!this.canvasService) return;
316
- const layer = this.getLayer();
317
- if (!layer) return;
318
-
319
- layer.remove(...layer.getObjects());
320
-
321
- const { thickness, backgroundColor, lineColor, textColor, fontSize } = this;
322
- const width = this.canvasService.canvas.width || 800;
323
- const height = this.canvasService.canvas.height || 600;
324
-
325
- // Calculate Layout using Dieline properties
326
- // Add padding to match DielineTool
327
- const paddingPx = this.resolvePadding(width, height);
328
-
329
- // Sync Viewport (in case DielineTool hasn't updated it yet, or purely for consistency)
330
- this.canvasService.viewport.setPadding(paddingPx);
331
- this.canvasService.viewport.updatePhysical(
332
- this.dielineWidth,
333
- this.dielineHeight,
334
- );
335
-
336
- const layout = this.canvasService.viewport.layout;
337
-
338
- const scale = layout.scale;
339
- const offsetX = layout.offsetX;
340
- const offsetY = layout.offsetY;
341
- const visualWidth = layout.width;
342
- const visualHeight = layout.height;
343
-
344
- // Logic for Bleed Offset:
345
- // 1. If offset > 0 (Expand):
346
- // - Ruler expands to cover the bleed area.
347
- // - Dimensions show expanded size.
348
- // 2. If offset < 0 (Shrink/Cut):
349
- // - Ruler stays at original Dieline boundary (does not shrink).
350
- // - Dimensions show original size.
351
- // - Bleed area is internal, so we ignore it for ruler placement.
352
-
353
- const rawOffsetMm = this.dielineOffset || 0;
354
- // Effective offset for ruler calculations (only positive offset expands the ruler)
355
- const effectiveOffsetMm = rawOffsetMm > 0 ? rawOffsetMm : 0;
356
-
357
- // Pixel expansion based on effective offset
358
- const expandPixels = effectiveOffsetMm * scale;
359
- // Use gap configuration
360
- const gap = this.gap || 15;
361
-
362
- // New Bounding Box for Ruler
363
- const rulerLeft = offsetX - expandPixels;
364
- const rulerTop = offsetY - expandPixels;
365
- const rulerRight = offsetX + visualWidth + expandPixels;
366
- const rulerBottom = offsetY + visualHeight + expandPixels;
367
-
368
- // Display Dimensions (Physical)
369
- const displayWidthMm = this.dielineWidth + effectiveOffsetMm * 2;
370
- const displayHeightMm = this.dielineHeight + effectiveOffsetMm * 2;
371
-
372
- // Ruler Placement Coordinates
373
- // Top Ruler: Above the top boundary
374
- const topRulerY = rulerTop - gap;
375
- const topRulerXStart = rulerLeft;
376
- const topRulerXEnd = rulerRight;
377
-
378
- // Left Ruler: Left of the left boundary
379
- const leftRulerX = rulerLeft - gap;
380
- const leftRulerYStart = rulerTop;
381
- const leftRulerYEnd = rulerBottom;
382
-
383
- // 1. Top Dimension Line (X-Axis)
384
- const topDimLine = this.createArrowLine(
385
- topRulerXStart,
386
- topRulerY,
387
- topRulerXEnd,
388
- topRulerY,
389
- lineColor,
390
- );
391
- layer.add(topDimLine);
392
-
393
- // Top Extension Lines
394
- const extLen = 5;
395
- layer.add(
396
- new Line(
397
- [
398
- topRulerXStart,
399
- topRulerY - extLen,
400
- topRulerXStart,
401
- topRulerY + extLen,
402
- ],
403
- {
404
- stroke: lineColor,
405
- strokeWidth: 1,
406
- selectable: false,
407
- evented: false,
408
- },
409
- ),
410
- );
411
- layer.add(
412
- new Line(
413
- [topRulerXEnd, topRulerY - extLen, topRulerXEnd, topRulerY + extLen],
414
- {
415
- stroke: lineColor,
416
- strokeWidth: 1,
417
- selectable: false,
418
- evented: false,
419
- },
420
- ),
421
- );
422
-
423
- // Top Text (Centered)
424
- const widthStr = formatMm(displayWidthMm, this.dielineDisplayUnit);
425
- const topTextContent = `${widthStr} ${this.dielineDisplayUnit}`;
426
- const topText = new Text(topTextContent, {
427
- left: topRulerXStart + (rulerRight - rulerLeft) / 2,
428
- top: topRulerY,
429
- fontSize: fontSize,
430
- fill: textColor,
431
- fontFamily: "Arial",
432
- originX: "center",
433
- originY: "center",
434
- backgroundColor: backgroundColor, // Background mask for readability
435
- selectable: false,
436
- evented: false,
437
- });
438
- // Add small padding to text background if Fabric supports it directly or via separate rect
439
- // Fabric Text backgroundColor is tight.
440
- layer.add(topText);
441
-
442
- // 2. Left Dimension Line (Y-Axis)
443
- const leftDimLine = this.createArrowLine(
444
- leftRulerX,
445
- leftRulerYStart,
446
- leftRulerX,
447
- leftRulerYEnd,
448
- lineColor,
449
- );
450
- layer.add(leftDimLine);
451
-
452
- // Left Extension Lines
453
- layer.add(
454
- new Line(
455
- [
456
- leftRulerX - extLen,
457
- leftRulerYStart,
458
- leftRulerX + extLen,
459
- leftRulerYStart,
460
- ],
461
- {
462
- stroke: lineColor,
463
- strokeWidth: 1,
464
- selectable: false,
465
- evented: false,
466
- },
467
- ),
468
- );
469
- layer.add(
470
- new Line(
471
- [
472
- leftRulerX - extLen,
473
- leftRulerYEnd,
474
- leftRulerX + extLen,
475
- leftRulerYEnd,
476
- ],
477
- {
478
- stroke: lineColor,
479
- strokeWidth: 1,
480
- selectable: false,
481
- evented: false,
482
- },
483
- ),
484
- );
485
-
486
- // Left Text (Centered, Rotated)
487
- const heightStr = formatMm(displayHeightMm, this.dielineDisplayUnit);
488
- const leftTextContent = `${heightStr} ${this.dielineDisplayUnit}`;
489
- const leftText = new Text(leftTextContent, {
490
- left: leftRulerX,
491
- top: leftRulerYStart + (rulerBottom - rulerTop) / 2,
492
- angle: -90,
493
- fontSize: fontSize,
494
- fill: textColor,
495
- fontFamily: "Arial",
496
- originX: "center",
497
- originY: "center",
498
- backgroundColor: backgroundColor,
499
- selectable: false,
500
- evented: false,
501
- });
502
- layer.add(leftText);
503
-
504
- // Always bring ruler to front
505
- this.canvasService.canvas.bringObjectToFront(layer);
506
- this.canvasService.canvas.requestRenderAll();
507
- }
508
- }
1
+ import {
2
+ Extension,
3
+ ExtensionContext,
4
+ ContributionPointIds,
5
+ CommandContribution,
6
+ ConfigurationContribution,
7
+ ConfigurationService,
8
+ } from "@pooder/core";
9
+ import { Rect, Line, Text, Group, Polygon } from "fabric";
10
+ import CanvasService from "./CanvasService";
11
+ import { formatMm } from "./units";
12
+ import { computeSceneLayout, readSizeState } from "./sceneLayoutModel";
13
+
14
+ export class RulerTool implements Extension {
15
+ id = "pooder.kit.ruler";
16
+
17
+ public metadata = {
18
+ name: "RulerTool",
19
+ };
20
+
21
+ private thickness: number = 20;
22
+ private gap: number = 15;
23
+ private backgroundColor: string = "#f0f0f0";
24
+ private textColor: string = "#333333";
25
+ private lineColor: string = "#999999";
26
+ private fontSize: number = 10;
27
+
28
+ private canvasService?: CanvasService;
29
+ private context?: ExtensionContext;
30
+ private onCanvasResized = () => {
31
+ this.updateRuler();
32
+ };
33
+
34
+ constructor(
35
+ options?: Partial<{
36
+ thickness: number;
37
+ backgroundColor: string;
38
+ textColor: string;
39
+ lineColor: string;
40
+ fontSize: number;
41
+ }>,
42
+ ) {
43
+ if (options) {
44
+ Object.assign(this, options);
45
+ }
46
+ }
47
+
48
+ activate(context: ExtensionContext) {
49
+ this.context = context;
50
+ this.canvasService = context.services.get<CanvasService>("CanvasService");
51
+ if (!this.canvasService) {
52
+ console.warn("CanvasService not found for RulerTool");
53
+ return;
54
+ }
55
+
56
+ const configService = context.services.get<ConfigurationService>(
57
+ "ConfigurationService",
58
+ );
59
+ if (configService) {
60
+ // Load initial config
61
+ this.thickness = configService.get("ruler.thickness", this.thickness);
62
+ this.gap = configService.get("ruler.gap", this.gap);
63
+ this.backgroundColor = configService.get(
64
+ "ruler.backgroundColor",
65
+ this.backgroundColor,
66
+ );
67
+ this.textColor = configService.get("ruler.textColor", this.textColor);
68
+ this.lineColor = configService.get("ruler.lineColor", this.lineColor);
69
+ this.fontSize = configService.get("ruler.fontSize", this.fontSize);
70
+
71
+ // Listen for changes
72
+ configService.onAnyChange((e: { key: string; value: any }) => {
73
+ let shouldUpdate = false;
74
+ if (e.key.startsWith("ruler.")) {
75
+ const prop = e.key.split(".")[1];
76
+ if (prop && prop in this) {
77
+ (this as any)[prop] = e.value;
78
+ shouldUpdate = true;
79
+ }
80
+ } else if (e.key.startsWith("size.")) {
81
+ shouldUpdate = true;
82
+ }
83
+
84
+ if (shouldUpdate) {
85
+ this.updateRuler();
86
+ }
87
+ });
88
+ }
89
+
90
+ this.createLayer();
91
+ context.eventBus.on("canvas:resized", this.onCanvasResized);
92
+ this.updateRuler();
93
+ }
94
+
95
+ deactivate(context: ExtensionContext) {
96
+ context.eventBus.off("canvas:resized", this.onCanvasResized);
97
+ this.destroyLayer();
98
+ this.canvasService = undefined;
99
+ this.context = undefined;
100
+ }
101
+
102
+ contribute() {
103
+ return {
104
+ [ContributionPointIds.CONFIGURATIONS]: [
105
+ {
106
+ id: "ruler.thickness",
107
+ type: "number",
108
+ label: "Thickness",
109
+ min: 10,
110
+ max: 100,
111
+ default: 20,
112
+ },
113
+ {
114
+ id: "ruler.gap",
115
+ type: "number",
116
+ label: "Gap",
117
+ min: 0,
118
+ max: 100,
119
+ default: 15,
120
+ },
121
+ {
122
+ id: "ruler.backgroundColor",
123
+ type: "color",
124
+ label: "Background Color",
125
+ default: "#f0f0f0",
126
+ },
127
+ {
128
+ id: "ruler.textColor",
129
+ type: "color",
130
+ label: "Text Color",
131
+ default: "#333333",
132
+ },
133
+ {
134
+ id: "ruler.lineColor",
135
+ type: "color",
136
+ label: "Line Color",
137
+ default: "#999999",
138
+ },
139
+ {
140
+ id: "ruler.fontSize",
141
+ type: "number",
142
+ label: "Font Size",
143
+ min: 8,
144
+ max: 24,
145
+ default: 10,
146
+ },
147
+ ] as ConfigurationContribution[],
148
+ [ContributionPointIds.COMMANDS]: [
149
+ {
150
+ command: "setTheme",
151
+ title: "Set Ruler Theme",
152
+ handler: (
153
+ theme: Partial<{
154
+ backgroundColor: string;
155
+ textColor: string;
156
+ lineColor: string;
157
+ fontSize: number;
158
+ thickness: number;
159
+ }>,
160
+ ) => {
161
+ const oldState = {
162
+ backgroundColor: this.backgroundColor,
163
+ textColor: this.textColor,
164
+ lineColor: this.lineColor,
165
+ fontSize: this.fontSize,
166
+ thickness: this.thickness,
167
+ };
168
+ const newState = { ...oldState, ...theme };
169
+ if (JSON.stringify(newState) === JSON.stringify(oldState))
170
+ return true;
171
+
172
+ Object.assign(this, newState);
173
+ this.updateRuler();
174
+ return true;
175
+ },
176
+ },
177
+ ] as CommandContribution[],
178
+ };
179
+ }
180
+
181
+ private getLayer() {
182
+ return this.canvasService?.getLayer("ruler-overlay");
183
+ }
184
+
185
+ private createLayer() {
186
+ if (!this.canvasService) return;
187
+
188
+ const canvas = this.canvasService.canvas;
189
+ const width = canvas.width || 800;
190
+ const height = canvas.height || 600;
191
+
192
+ const layer = this.canvasService.createLayer("ruler-overlay", {
193
+ width,
194
+ height,
195
+ selectable: false,
196
+ evented: false,
197
+ left: 0,
198
+ top: 0,
199
+ originX: "left",
200
+ originY: "top",
201
+ });
202
+
203
+ canvas.bringObjectToFront(layer);
204
+ }
205
+
206
+ private destroyLayer() {
207
+ if (!this.canvasService) return;
208
+ const layer = this.getLayer();
209
+ if (layer) {
210
+ this.canvasService.canvas.remove(layer);
211
+ }
212
+ }
213
+
214
+ private createArrowLine(
215
+ x1: number,
216
+ y1: number,
217
+ x2: number,
218
+ y2: number,
219
+ color: string,
220
+ ): Group {
221
+ const line = new Line([x1, y1, x2, y2], {
222
+ stroke: color,
223
+ strokeWidth: this.thickness / 20, // Scale stroke width relative to thickness (default 1)
224
+ selectable: false,
225
+ evented: false,
226
+ });
227
+
228
+ // Arrow size proportional to thickness
229
+ const arrowSize = Math.max(4, this.thickness * 0.3);
230
+ const angle = Math.atan2(y2 - y1, x2 - x1);
231
+
232
+ // End Arrow (at x2, y2)
233
+ const endArrow = new Polygon(
234
+ [
235
+ { x: 0, y: 0 },
236
+ { x: -arrowSize, y: -arrowSize / 2 },
237
+ { x: -arrowSize, y: arrowSize / 2 },
238
+ ],
239
+ {
240
+ fill: color,
241
+ left: x2,
242
+ top: y2,
243
+ originX: "right",
244
+ originY: "center",
245
+ angle: (angle * 180) / Math.PI,
246
+ selectable: false,
247
+ evented: false,
248
+ },
249
+ );
250
+
251
+ // Start Arrow (at x1, y1)
252
+ const startArrow = new Polygon(
253
+ [
254
+ { x: 0, y: 0 },
255
+ { x: arrowSize, y: -arrowSize / 2 },
256
+ { x: arrowSize, y: arrowSize / 2 },
257
+ ],
258
+ {
259
+ fill: color,
260
+ left: x1,
261
+ top: y1,
262
+ originX: "left",
263
+ originY: "center",
264
+ angle: (angle * 180) / Math.PI,
265
+ selectable: false,
266
+ evented: false,
267
+ },
268
+ );
269
+
270
+ return new Group([line, startArrow, endArrow], {
271
+ selectable: false,
272
+ evented: false,
273
+ });
274
+ }
275
+
276
+ private updateRuler() {
277
+ if (!this.canvasService) return;
278
+ const layer = this.getLayer();
279
+ if (!layer) return;
280
+
281
+ layer.remove(...layer.getObjects());
282
+
283
+ const { backgroundColor, lineColor, textColor, fontSize } = this;
284
+
285
+ const configService = this.context?.services.get<ConfigurationService>(
286
+ "ConfigurationService",
287
+ );
288
+ if (!configService) return;
289
+ const sizeState = readSizeState(configService);
290
+ const layout = computeSceneLayout(this.canvasService, sizeState);
291
+ if (!layout) return;
292
+
293
+ const trimRect = layout.trimRect;
294
+ const cutRect = layout.cutRect;
295
+ const useCutAsRuler = layout.cutMode === "outset";
296
+ const rulerRect = useCutAsRuler ? cutRect : trimRect;
297
+ // Use gap configuration
298
+ const gap = this.gap || 15;
299
+
300
+ // New Bounding Box for Ruler
301
+ const rulerLeft = rulerRect.left;
302
+ const rulerTop = rulerRect.top;
303
+ const rulerRight = rulerRect.left + rulerRect.width;
304
+ const rulerBottom = rulerRect.top + rulerRect.height;
305
+
306
+ // Display Dimensions (Physical)
307
+ const displayWidthMm = useCutAsRuler ? layout.cutWidthMm : layout.trimWidthMm;
308
+ const displayHeightMm = useCutAsRuler
309
+ ? layout.cutHeightMm
310
+ : layout.trimHeightMm;
311
+ const displayUnit = sizeState.unit;
312
+
313
+ // Ruler Placement Coordinates
314
+ // Top Ruler: Above the top boundary
315
+ const topRulerY = rulerTop - gap;
316
+ const topRulerXStart = rulerLeft;
317
+ const topRulerXEnd = rulerRight;
318
+
319
+ // Left Ruler: Left of the left boundary
320
+ const leftRulerX = rulerLeft - gap;
321
+ const leftRulerYStart = rulerTop;
322
+ const leftRulerYEnd = rulerBottom;
323
+
324
+ // 1. Top Dimension Line (X-Axis)
325
+ const topDimLine = this.createArrowLine(
326
+ topRulerXStart,
327
+ topRulerY,
328
+ topRulerXEnd,
329
+ topRulerY,
330
+ lineColor,
331
+ );
332
+ layer.add(topDimLine);
333
+
334
+ // Top Extension Lines
335
+ const extLen = 5;
336
+ layer.add(
337
+ new Line(
338
+ [
339
+ topRulerXStart,
340
+ topRulerY - extLen,
341
+ topRulerXStart,
342
+ topRulerY + extLen,
343
+ ],
344
+ {
345
+ stroke: lineColor,
346
+ strokeWidth: 1,
347
+ selectable: false,
348
+ evented: false,
349
+ },
350
+ ),
351
+ );
352
+ layer.add(
353
+ new Line(
354
+ [topRulerXEnd, topRulerY - extLen, topRulerXEnd, topRulerY + extLen],
355
+ {
356
+ stroke: lineColor,
357
+ strokeWidth: 1,
358
+ selectable: false,
359
+ evented: false,
360
+ },
361
+ ),
362
+ );
363
+
364
+ // Top Text (Centered)
365
+ const widthStr = formatMm(displayWidthMm, displayUnit);
366
+ const topTextContent = `${widthStr} ${displayUnit}`;
367
+ const topText = new Text(topTextContent, {
368
+ left: topRulerXStart + (rulerRight - rulerLeft) / 2,
369
+ top: topRulerY,
370
+ fontSize: fontSize,
371
+ fill: textColor,
372
+ fontFamily: "Arial",
373
+ originX: "center",
374
+ originY: "center",
375
+ backgroundColor: backgroundColor, // Background mask for readability
376
+ selectable: false,
377
+ evented: false,
378
+ });
379
+ // Add small padding to text background if Fabric supports it directly or via separate rect
380
+ // Fabric Text backgroundColor is tight.
381
+ layer.add(topText);
382
+
383
+ // 2. Left Dimension Line (Y-Axis)
384
+ const leftDimLine = this.createArrowLine(
385
+ leftRulerX,
386
+ leftRulerYStart,
387
+ leftRulerX,
388
+ leftRulerYEnd,
389
+ lineColor,
390
+ );
391
+ layer.add(leftDimLine);
392
+
393
+ // Left Extension Lines
394
+ layer.add(
395
+ new Line(
396
+ [
397
+ leftRulerX - extLen,
398
+ leftRulerYStart,
399
+ leftRulerX + extLen,
400
+ leftRulerYStart,
401
+ ],
402
+ {
403
+ stroke: lineColor,
404
+ strokeWidth: 1,
405
+ selectable: false,
406
+ evented: false,
407
+ },
408
+ ),
409
+ );
410
+ layer.add(
411
+ new Line(
412
+ [
413
+ leftRulerX - extLen,
414
+ leftRulerYEnd,
415
+ leftRulerX + extLen,
416
+ leftRulerYEnd,
417
+ ],
418
+ {
419
+ stroke: lineColor,
420
+ strokeWidth: 1,
421
+ selectable: false,
422
+ evented: false,
423
+ },
424
+ ),
425
+ );
426
+
427
+ // Left Text (Centered, Rotated)
428
+ const heightStr = formatMm(displayHeightMm, displayUnit);
429
+ const leftTextContent = `${heightStr} ${displayUnit}`;
430
+ const leftText = new Text(leftTextContent, {
431
+ left: leftRulerX,
432
+ top: leftRulerYStart + (rulerBottom - rulerTop) / 2,
433
+ angle: -90,
434
+ fontSize: fontSize,
435
+ fill: textColor,
436
+ fontFamily: "Arial",
437
+ originX: "center",
438
+ originY: "center",
439
+ backgroundColor: backgroundColor,
440
+ selectable: false,
441
+ evented: false,
442
+ });
443
+ layer.add(leftText);
444
+
445
+ // Always bring ruler to front
446
+ this.canvasService.canvas.bringObjectToFront(layer);
447
+ this.canvasService.canvas.requestRenderAll();
448
+ }
449
+ }