@pooder/kit 5.4.0 → 6.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 (69) hide show
  1. package/.test-dist/src/coordinate.js +74 -0
  2. package/.test-dist/src/extensions/background.js +547 -0
  3. package/.test-dist/src/extensions/bridgeSelection.js +20 -0
  4. package/.test-dist/src/extensions/constraints.js +237 -0
  5. package/.test-dist/src/extensions/dieline.js +931 -0
  6. package/.test-dist/src/extensions/dielineShape.js +66 -0
  7. package/.test-dist/src/extensions/edgeScale.js +12 -0
  8. package/.test-dist/src/extensions/feature.js +910 -0
  9. package/.test-dist/src/extensions/featureComplete.js +32 -0
  10. package/.test-dist/src/extensions/film.js +226 -0
  11. package/.test-dist/src/extensions/geometry.js +609 -0
  12. package/.test-dist/src/extensions/image.js +1613 -0
  13. package/.test-dist/src/extensions/index.js +28 -0
  14. package/.test-dist/src/extensions/maskOps.js +334 -0
  15. package/.test-dist/src/extensions/mirror.js +104 -0
  16. package/.test-dist/src/extensions/ruler.js +442 -0
  17. package/.test-dist/src/extensions/sceneLayout.js +96 -0
  18. package/.test-dist/src/extensions/sceneLayoutModel.js +202 -0
  19. package/.test-dist/src/extensions/sceneVisibility.js +55 -0
  20. package/.test-dist/src/extensions/size.js +331 -0
  21. package/.test-dist/src/extensions/tracer.js +709 -0
  22. package/.test-dist/src/extensions/white-ink.js +1200 -0
  23. package/.test-dist/src/extensions/wrappedOffsets.js +33 -0
  24. package/.test-dist/src/index.js +18 -0
  25. package/.test-dist/src/services/CanvasService.js +1011 -0
  26. package/.test-dist/src/services/ViewportSystem.js +76 -0
  27. package/.test-dist/src/services/index.js +25 -0
  28. package/.test-dist/src/services/renderSpec.js +2 -0
  29. package/.test-dist/src/services/visibility.js +54 -0
  30. package/.test-dist/src/units.js +30 -0
  31. package/.test-dist/tests/run.js +148 -0
  32. package/CHANGELOG.md +6 -0
  33. package/dist/index.d.mts +150 -62
  34. package/dist/index.d.ts +150 -62
  35. package/dist/index.js +2219 -1714
  36. package/dist/index.mjs +2226 -1718
  37. package/package.json +1 -1
  38. package/src/coordinate.ts +106 -106
  39. package/src/extensions/background.ts +716 -323
  40. package/src/extensions/bridgeSelection.ts +17 -17
  41. package/src/extensions/constraints.ts +322 -322
  42. package/src/extensions/dieline.ts +1169 -1149
  43. package/src/extensions/dielineShape.ts +109 -109
  44. package/src/extensions/edgeScale.ts +19 -19
  45. package/src/extensions/feature.ts +1140 -1137
  46. package/src/extensions/featureComplete.ts +46 -46
  47. package/src/extensions/film.ts +270 -266
  48. package/src/extensions/geometry.ts +851 -885
  49. package/src/extensions/image.ts +2007 -2054
  50. package/src/extensions/index.ts +10 -11
  51. package/src/extensions/maskOps.ts +283 -283
  52. package/src/extensions/mirror.ts +128 -128
  53. package/src/extensions/ruler.ts +664 -654
  54. package/src/extensions/sceneLayout.ts +140 -140
  55. package/src/extensions/sceneLayoutModel.ts +364 -364
  56. package/src/extensions/size.ts +389 -389
  57. package/src/extensions/tracer.ts +1019 -1019
  58. package/src/extensions/white-ink.ts +1508 -1575
  59. package/src/extensions/wrappedOffsets.ts +33 -33
  60. package/src/index.ts +2 -2
  61. package/src/services/CanvasService.ts +1286 -832
  62. package/src/services/ViewportSystem.ts +95 -95
  63. package/src/services/index.ts +4 -3
  64. package/src/services/renderSpec.ts +83 -53
  65. package/src/services/visibility.ts +78 -0
  66. package/src/units.ts +27 -27
  67. package/tests/run.ts +253 -118
  68. package/tsconfig.test.json +15 -15
  69. package/src/extensions/sceneVisibility.ts +0 -64
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.SceneLayoutService = void 0;
18
+ __exportStar(require("./background"), exports);
19
+ __exportStar(require("./image"), exports);
20
+ __exportStar(require("./size"), exports);
21
+ __exportStar(require("./dieline"), exports);
22
+ __exportStar(require("./feature"), exports);
23
+ __exportStar(require("./film"), exports);
24
+ __exportStar(require("./mirror"), exports);
25
+ __exportStar(require("./ruler"), exports);
26
+ __exportStar(require("./white-ink"), exports);
27
+ var sceneLayout_1 = require("./sceneLayout");
28
+ Object.defineProperty(exports, "SceneLayoutService", { enumerable: true, get: function () { return sceneLayout_1.SceneLayoutService; } });
@@ -0,0 +1,334 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createMask = createMask;
4
+ exports.inferMaskMode = inferMaskMode;
5
+ exports.analyzeAlpha = analyzeAlpha;
6
+ exports.circularMorphology = circularMorphology;
7
+ exports.fillHoles = fillHoles;
8
+ exports.countForeground = countForeground;
9
+ exports.isMaskConnected8 = isMaskConnected8;
10
+ exports.findMinimalConnectRadius = findMinimalConnectRadius;
11
+ exports.polygonSignedArea = polygonSignedArea;
12
+ function createMask(imageData, options) {
13
+ const { width, height, data } = imageData;
14
+ const { threshold, padding, paddedWidth, paddedHeight, maskMode = "auto", whiteThreshold = 240, alphaOpaqueCutoff = 250, } = options;
15
+ const resolvedMode = maskMode === "auto" ? inferMaskMode(imageData, alphaOpaqueCutoff) : maskMode;
16
+ const mask = new Uint8Array(paddedWidth * paddedHeight);
17
+ for (let y = 0; y < height; y++) {
18
+ for (let x = 0; x < width; x++) {
19
+ const srcIdx = (y * width + x) * 4;
20
+ const r = data[srcIdx];
21
+ const g = data[srcIdx + 1];
22
+ const b = data[srcIdx + 2];
23
+ const a = data[srcIdx + 3];
24
+ const destIdx = (y + padding) * paddedWidth + (x + padding);
25
+ if (resolvedMode === "alpha") {
26
+ if (a > threshold)
27
+ mask[destIdx] = 1;
28
+ }
29
+ else {
30
+ if (a > threshold &&
31
+ !(r > whiteThreshold && g > whiteThreshold && b > whiteThreshold)) {
32
+ mask[destIdx] = 1;
33
+ }
34
+ }
35
+ }
36
+ }
37
+ return mask;
38
+ }
39
+ function inferMaskMode(imageData, alphaOpaqueCutoff) {
40
+ const analysis = analyzeAlpha(imageData, alphaOpaqueCutoff);
41
+ if (analysis.minAlpha === 255)
42
+ return "whitebg";
43
+ if (analysis.veryTransparentRatio >= 0.0005)
44
+ return "alpha";
45
+ if (analysis.belowOpaqueRatio >= 0.01)
46
+ return "alpha";
47
+ return "whitebg";
48
+ }
49
+ function analyzeAlpha(imageData, alphaOpaqueCutoff) {
50
+ const { data } = imageData;
51
+ const total = data.length / 4;
52
+ let belowOpaque = 0;
53
+ let veryTransparent = 0;
54
+ let minAlpha = 255;
55
+ for (let i = 3; i < data.length; i += 4) {
56
+ const a = data[i];
57
+ if (a < minAlpha)
58
+ minAlpha = a;
59
+ if (a < alphaOpaqueCutoff)
60
+ belowOpaque++;
61
+ if (a < 32)
62
+ veryTransparent++;
63
+ }
64
+ return {
65
+ total,
66
+ minAlpha,
67
+ belowOpaqueRatio: belowOpaque / total,
68
+ veryTransparentRatio: veryTransparent / total,
69
+ };
70
+ }
71
+ function circularMorphology(mask, width, height, radius, op) {
72
+ const r = Math.max(0, Math.floor(radius));
73
+ if (r <= 0) {
74
+ return mask.slice();
75
+ }
76
+ // Disk kernel dilation (Euclidean metric).
77
+ const dilateDisk = (m, radiusPx) => {
78
+ const horizontalDist = new Int32Array(width * height);
79
+ for (let y = 0; y < height; y++) {
80
+ let lastSolid = -radiusPx * 2;
81
+ for (let x = 0; x < width; x++) {
82
+ if (m[y * width + x])
83
+ lastSolid = x;
84
+ horizontalDist[y * width + x] = x - lastSolid;
85
+ }
86
+ lastSolid = width + radiusPx * 2;
87
+ for (let x = width - 1; x >= 0; x--) {
88
+ if (m[y * width + x])
89
+ lastSolid = x;
90
+ horizontalDist[y * width + x] = Math.min(horizontalDist[y * width + x], lastSolid - x);
91
+ }
92
+ }
93
+ const result = new Uint8Array(width * height);
94
+ const r2 = radiusPx * radiusPx;
95
+ for (let x = 0; x < width; x++) {
96
+ for (let y = 0; y < height; y++) {
97
+ let found = false;
98
+ const minY = Math.max(0, y - radiusPx);
99
+ const maxY = Math.min(height - 1, y + radiusPx);
100
+ for (let dy = minY; dy <= maxY; dy++) {
101
+ const dY = dy - y;
102
+ const hDist = horizontalDist[dy * width + x];
103
+ if (hDist * hDist + dY * dY <= r2) {
104
+ found = true;
105
+ break;
106
+ }
107
+ }
108
+ if (found)
109
+ result[y * width + x] = 1;
110
+ }
111
+ }
112
+ return result;
113
+ };
114
+ // Diamond kernel erosion (L1 metric), implemented as radius iterations of
115
+ // 4-neighbor erosion. This is intentionally different from dilation kernel.
116
+ const erodeDiamond = (m, radiusPx) => {
117
+ if (radiusPx <= 0)
118
+ return m.slice();
119
+ let current = m;
120
+ for (let step = 0; step < radiusPx; step++) {
121
+ const next = new Uint8Array(width * height);
122
+ for (let y = 1; y < height - 1; y++) {
123
+ const row = y * width;
124
+ for (let x = 1; x < width - 1; x++) {
125
+ const idx = row + x;
126
+ if (current[idx] &&
127
+ current[idx - 1] &&
128
+ current[idx + 1] &&
129
+ current[idx - width] &&
130
+ current[idx + width]) {
131
+ next[idx] = 1;
132
+ }
133
+ }
134
+ }
135
+ current = next;
136
+ }
137
+ return current;
138
+ };
139
+ // Restore thin bridges removed by erosion: if a removed pixel links two
140
+ // opposite neighbors in the source mask, bring it back.
141
+ const restoreBridgePixels = (source, eroded) => {
142
+ const restored = eroded.slice();
143
+ for (let y = 1; y < height - 1; y++) {
144
+ const row = y * width;
145
+ for (let x = 1; x < width - 1; x++) {
146
+ const idx = row + x;
147
+ if (!source[idx] || restored[idx])
148
+ continue;
149
+ const up = source[idx - width] === 1;
150
+ const down = source[idx + width] === 1;
151
+ const left = source[idx - 1] === 1;
152
+ const right = source[idx + 1] === 1;
153
+ const upLeft = source[idx - width - 1] === 1;
154
+ const upRight = source[idx - width + 1] === 1;
155
+ const downLeft = source[idx + width - 1] === 1;
156
+ const downRight = source[idx + width + 1] === 1;
157
+ const keepsBridge = (left && right) ||
158
+ (up && down) ||
159
+ (upLeft && downRight) ||
160
+ (upRight && downLeft);
161
+ if (keepsBridge) {
162
+ restored[idx] = 1;
163
+ }
164
+ }
165
+ }
166
+ return restored;
167
+ };
168
+ const erodePreservingBridges = (m, radiusPx) => {
169
+ const eroded = erodeDiamond(m, radiusPx);
170
+ return restoreBridgePixels(m, eroded);
171
+ };
172
+ switch (op) {
173
+ case "dilate":
174
+ return dilateDisk(mask, r);
175
+ case "erode":
176
+ return erodePreservingBridges(mask, r);
177
+ case "closing": {
178
+ const erodeRadius = Math.max(1, Math.floor(r * 0.65));
179
+ return erodePreservingBridges(dilateDisk(mask, r), erodeRadius);
180
+ }
181
+ case "opening":
182
+ return dilateDisk(erodePreservingBridges(mask, r), r);
183
+ default:
184
+ return mask;
185
+ }
186
+ }
187
+ function fillHoles(mask, width, height) {
188
+ const background = new Uint8Array(width * height);
189
+ const queue = [];
190
+ for (let x = 0; x < width; x++) {
191
+ if (mask[x] === 0) {
192
+ background[x] = 1;
193
+ queue.push(x);
194
+ }
195
+ const lastRowIdx = (height - 1) * width + x;
196
+ if (mask[lastRowIdx] === 0) {
197
+ background[lastRowIdx] = 1;
198
+ queue.push(lastRowIdx);
199
+ }
200
+ }
201
+ for (let y = 1; y < height - 1; y++) {
202
+ const leftIdx = y * width;
203
+ const rightIdx = y * width + (width - 1);
204
+ if (mask[leftIdx] === 0) {
205
+ background[leftIdx] = 1;
206
+ queue.push(leftIdx);
207
+ }
208
+ if (mask[rightIdx] === 0) {
209
+ background[rightIdx] = 1;
210
+ queue.push(rightIdx);
211
+ }
212
+ }
213
+ let head = 0;
214
+ while (head < queue.length) {
215
+ const idx = queue[head++];
216
+ const x = idx % width;
217
+ const y = (idx - x) / width;
218
+ const up = y > 0 ? idx - width : -1;
219
+ const down = y < height - 1 ? idx + width : -1;
220
+ const left = x > 0 ? idx - 1 : -1;
221
+ const right = x < width - 1 ? idx + 1 : -1;
222
+ if (up >= 0 && mask[up] === 0 && background[up] === 0) {
223
+ background[up] = 1;
224
+ queue.push(up);
225
+ }
226
+ if (down >= 0 && mask[down] === 0 && background[down] === 0) {
227
+ background[down] = 1;
228
+ queue.push(down);
229
+ }
230
+ if (left >= 0 && mask[left] === 0 && background[left] === 0) {
231
+ background[left] = 1;
232
+ queue.push(left);
233
+ }
234
+ if (right >= 0 && mask[right] === 0 && background[right] === 0) {
235
+ background[right] = 1;
236
+ queue.push(right);
237
+ }
238
+ }
239
+ const filledMask = new Uint8Array(width * height);
240
+ for (let i = 0; i < width * height; i++) {
241
+ filledMask[i] = background[i] === 0 ? 1 : 0;
242
+ }
243
+ return filledMask;
244
+ }
245
+ function countForeground(mask) {
246
+ let c = 0;
247
+ for (let i = 0; i < mask.length; i++)
248
+ c += mask[i] ? 1 : 0;
249
+ return c;
250
+ }
251
+ function isMaskConnected8(mask, width, height) {
252
+ const total = countForeground(mask);
253
+ if (total === 0)
254
+ return true;
255
+ let start = -1;
256
+ for (let i = 0; i < mask.length; i++) {
257
+ if (mask[i]) {
258
+ start = i;
259
+ break;
260
+ }
261
+ }
262
+ if (start === -1)
263
+ return true;
264
+ const visited = new Uint8Array(mask.length);
265
+ const queue = [start];
266
+ visited[start] = 1;
267
+ let seen = 1;
268
+ let head = 0;
269
+ while (head < queue.length) {
270
+ const idx = queue[head++];
271
+ const x = idx % width;
272
+ const y = (idx - x) / width;
273
+ for (let dy = -1; dy <= 1; dy++) {
274
+ const ny = y + dy;
275
+ if (ny < 0 || ny >= height)
276
+ continue;
277
+ for (let dx = -1; dx <= 1; dx++) {
278
+ if (dx === 0 && dy === 0)
279
+ continue;
280
+ const nx = x + dx;
281
+ if (nx < 0 || nx >= width)
282
+ continue;
283
+ const nidx = ny * width + nx;
284
+ if (mask[nidx] && !visited[nidx]) {
285
+ visited[nidx] = 1;
286
+ queue.push(nidx);
287
+ seen++;
288
+ }
289
+ }
290
+ }
291
+ }
292
+ return seen === total;
293
+ }
294
+ function findMinimalConnectRadius(mask, width, height, maxRadius) {
295
+ if (maxRadius <= 0)
296
+ return 0;
297
+ if (isMaskConnected8(mask, width, height))
298
+ return 0;
299
+ let low = 0;
300
+ let high = 1;
301
+ while (high <= maxRadius) {
302
+ const closed = circularMorphology(mask, width, height, high, "closing");
303
+ if (isMaskConnected8(closed, width, height))
304
+ break;
305
+ high *= 2;
306
+ }
307
+ if (high > maxRadius)
308
+ high = maxRadius;
309
+ if (!isMaskConnected8(circularMorphology(mask, width, height, high, "closing"), width, height)) {
310
+ return high;
311
+ }
312
+ while (low + 1 < high) {
313
+ const mid = Math.floor((low + high) / 2);
314
+ const closed = circularMorphology(mask, width, height, mid, "closing");
315
+ if (isMaskConnected8(closed, width, height)) {
316
+ high = mid;
317
+ }
318
+ else {
319
+ low = mid;
320
+ }
321
+ }
322
+ return high;
323
+ }
324
+ function polygonSignedArea(points) {
325
+ if (points.length < 3)
326
+ return 0;
327
+ let sum = 0;
328
+ for (let i = 0; i < points.length; i++) {
329
+ const a = points[i];
330
+ const b = points[(i + 1) % points.length];
331
+ sum += a.x * b.y - b.x * a.y;
332
+ }
333
+ return sum / 2;
334
+ }
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MirrorTool = void 0;
4
+ const core_1 = require("@pooder/core");
5
+ class MirrorTool {
6
+ constructor(options) {
7
+ this.id = "pooder.kit.mirror";
8
+ this.metadata = {
9
+ name: "MirrorTool",
10
+ };
11
+ this.enabled = false;
12
+ if (options) {
13
+ Object.assign(this, options);
14
+ }
15
+ }
16
+ toJSON() {
17
+ return {
18
+ enabled: this.enabled,
19
+ };
20
+ }
21
+ loadFromJSON(json) {
22
+ this.enabled = json.enabled;
23
+ }
24
+ activate(context) {
25
+ this.canvasService = context.services.get("CanvasService");
26
+ if (!this.canvasService) {
27
+ console.warn("CanvasService not found for MirrorTool");
28
+ return;
29
+ }
30
+ const configService = context.services.get("ConfigurationService");
31
+ if (configService) {
32
+ // Load initial config
33
+ this.enabled = configService.get("mirror.enabled", this.enabled);
34
+ // Listen for changes
35
+ configService.onAnyChange((e) => {
36
+ if (e.key === "mirror.enabled") {
37
+ this.applyMirror(e.value);
38
+ }
39
+ });
40
+ }
41
+ // Initialize with current state (if enabled was persisted)
42
+ if (this.enabled) {
43
+ this.applyMirror(true);
44
+ }
45
+ }
46
+ deactivate(context) {
47
+ this.applyMirror(false);
48
+ this.canvasService = undefined;
49
+ }
50
+ contribute() {
51
+ return {
52
+ [core_1.ContributionPointIds.CONFIGURATIONS]: [
53
+ {
54
+ id: "mirror.enabled",
55
+ type: "boolean",
56
+ label: "Enable Mirror",
57
+ default: false,
58
+ },
59
+ ],
60
+ [core_1.ContributionPointIds.COMMANDS]: [
61
+ {
62
+ command: "setMirror",
63
+ title: "Set Mirror",
64
+ handler: (enabled) => {
65
+ this.applyMirror(enabled);
66
+ return true;
67
+ },
68
+ },
69
+ ],
70
+ };
71
+ }
72
+ applyMirror(enabled) {
73
+ if (!this.canvasService)
74
+ return;
75
+ const canvas = this.canvasService.canvas;
76
+ if (!canvas)
77
+ return;
78
+ const width = canvas.width || 800;
79
+ // Fabric.js v6+ uses viewportTransform property
80
+ let vpt = canvas.viewportTransform || [1, 0, 0, 1, 0, 0];
81
+ // Create a copy to avoid mutating the reference directly before setting
82
+ vpt = [...vpt];
83
+ // If we are enabling and currently not flipped (scaleX > 0)
84
+ // Or disabling and currently flipped (scaleX < 0)
85
+ const isFlipped = vpt[0] < 0;
86
+ if (enabled && !isFlipped) {
87
+ // Flip scale X
88
+ vpt[0] = -vpt[0]; // Flip scale
89
+ vpt[4] = width - vpt[4]; // Adjust pan X
90
+ canvas.setViewportTransform(vpt);
91
+ canvas.requestRenderAll();
92
+ this.enabled = true;
93
+ }
94
+ else if (!enabled && isFlipped) {
95
+ // Restore
96
+ vpt[0] = -vpt[0]; // Unflip scale
97
+ vpt[4] = width - vpt[4]; // Restore pan X
98
+ canvas.setViewportTransform(vpt);
99
+ canvas.requestRenderAll();
100
+ this.enabled = false;
101
+ }
102
+ }
103
+ }
104
+ exports.MirrorTool = MirrorTool;