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