@pooder/kit 5.4.0 → 6.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 (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 +935 -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 +1788 -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 +1032 -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 +57 -0
  30. package/.test-dist/src/units.js +30 -0
  31. package/.test-dist/tests/run.js +150 -0
  32. package/CHANGELOG.md +12 -0
  33. package/dist/index.d.mts +164 -62
  34. package/dist/index.d.ts +164 -62
  35. package/dist/index.js +2433 -1719
  36. package/dist/index.mjs +2442 -1723
  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 +1173 -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 +2240 -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 +1317 -832
  62. package/src/services/ViewportSystem.ts +95 -95
  63. package/src/services/index.ts +4 -3
  64. package/src/services/renderSpec.ts +85 -53
  65. package/src/services/visibility.ts +83 -0
  66. package/src/units.ts +27 -27
  67. package/tests/run.ts +258 -118
  68. package/tsconfig.test.json +15 -15
  69. package/src/extensions/sceneVisibility.ts +0 -64
package/tests/run.ts CHANGED
@@ -1,118 +1,258 @@
1
- import { pickExitIndex, scoreOutsideAbove } from "../src/bridgeSelection";
2
- import { sampleWrappedOffsets, wrappedDistance } from "../src/wrappedOffsets";
3
- import {
4
- circularMorphology,
5
- createMask,
6
- fillHoles,
7
- findMinimalConnectRadius,
8
- isMaskConnected8,
9
- } from "../src/maskOps";
10
- import { computeDetectEdgeSize } from "../src/edgeScale";
11
-
12
- function assert(condition: unknown, message: string) {
13
- if (!condition) throw new Error(message);
14
- }
15
-
16
- function testWrappedOffsets() {
17
- assert(wrappedDistance(100, 10, 30) === 20, "distance 10->30 should be 20");
18
- assert(wrappedDistance(100, 90, 10) === 20, "distance 90->10 should wrap to 20");
19
-
20
- const a = sampleWrappedOffsets(100, 10, 30, 5);
21
- assert(
22
- JSON.stringify(a) === JSON.stringify([10, 15, 20, 25, 30]),
23
- `unexpected sample: ${JSON.stringify(a)}`,
24
- );
25
-
26
- const b = sampleWrappedOffsets(100, 90, 10, 3);
27
- assert(
28
- JSON.stringify(b) === JSON.stringify([90, 0, 10]),
29
- `unexpected wrap sample: ${JSON.stringify(b)}`,
30
- );
31
- }
32
-
33
- function testBridgeSelection() {
34
- const idx = pickExitIndex([
35
- { insideAbove: true, insideBelow: true },
36
- { insideAbove: false, insideBelow: true },
37
- { insideAbove: false, insideBelow: false },
38
- ]);
39
- assert(idx === 1, `expected exit index 1, got ${idx}`);
40
-
41
- const none = pickExitIndex([{ insideAbove: true, insideBelow: true }]);
42
- assert(none === -1, `expected -1, got ${none}`);
43
-
44
- const score = scoreOutsideAbove([
45
- { outsideAbove: true },
46
- { outsideAbove: false },
47
- { outsideAbove: true },
48
- ]);
49
- assert(score === 2, `expected score 2, got ${score}`);
50
- }
51
-
52
- function testMaskOps() {
53
- const width = 50;
54
- const height = 50;
55
- const mask = new Uint8Array(width * height);
56
- mask[10 * width + 10] = 1;
57
- mask[10 * width + 20] = 1;
58
-
59
- const r = findMinimalConnectRadius(mask, width, height, 20);
60
- const closed = circularMorphology(mask, width, height, r, "closing");
61
- assert(isMaskConnected8(closed, width, height), `closed mask should be connected (r=${r})`);
62
- if (r > 0) {
63
- const closedPrev = circularMorphology(mask, width, height, r - 1, "closing");
64
- assert(
65
- !isMaskConnected8(closedPrev, width, height),
66
- `r should be minimal (r=${r})`,
67
- );
68
- }
69
-
70
- const donut = new Uint8Array(9 * 9);
71
- for (let y = 1; y <= 7; y++) {
72
- for (let x = 1; x <= 7; x++) donut[y * 9 + x] = 1;
73
- }
74
- for (let y = 3; y <= 5; y++) {
75
- for (let x = 3; x <= 5; x++) donut[y * 9 + x] = 0;
76
- }
77
- const filled = fillHoles(donut, 9, 9);
78
- assert(filled[4 * 9 + 4] === 1, "hole should be filled");
79
-
80
- const imgW = 2;
81
- const imgH = 1;
82
- const rgba = new Uint8ClampedArray([
83
- 255, 255, 255, 255, 10, 10, 10, 254,
84
- ]);
85
- const imageData = { width: imgW, height: imgH, data: rgba } as unknown as ImageData;
86
- const paddedWidth = imgW + 4;
87
- const paddedHeight = imgH + 4;
88
- const created = createMask(imageData, {
89
- threshold: 10,
90
- padding: 2,
91
- paddedWidth,
92
- paddedHeight,
93
- maskMode: "auto",
94
- alphaOpaqueCutoff: 250,
95
- });
96
- assert(created[2 * paddedWidth + 2] === 0, "white pixel should be background");
97
- assert(created[2 * paddedWidth + 3] === 1, "non-white pixel should be foreground");
98
- }
99
-
100
- function testEdgeScale() {
101
- const currentMax = 100;
102
- const baseBounds = { width: 50, height: 20 };
103
- const expandedBounds = { width: 70, height: 40 };
104
- const { width, height, scale } = computeDetectEdgeSize(currentMax, baseBounds, expandedBounds);
105
- assert(scale === 2, `expected scale 2, got ${scale}`);
106
- assert(width === 140, `expected width 140, got ${width}`);
107
- assert(height === 80, `expected height 80, got ${height}`);
108
- }
109
-
110
- function main() {
111
- testWrappedOffsets();
112
- testBridgeSelection();
113
- testMaskOps();
114
- testEdgeScale();
115
- console.log("ok");
116
- }
117
-
118
- main();
1
+ import {
2
+ pickExitIndex,
3
+ scoreOutsideAbove,
4
+ } from "../src/extensions/bridgeSelection";
5
+ import {
6
+ sampleWrappedOffsets,
7
+ wrappedDistance,
8
+ } from "../src/extensions/wrappedOffsets";
9
+ import {
10
+ circularMorphology,
11
+ createMask,
12
+ fillHoles,
13
+ findMinimalConnectRadius,
14
+ isMaskConnected8,
15
+ } from "../src/extensions/maskOps";
16
+ import { computeDetectEdgeSize } from "../src/extensions/edgeScale";
17
+ import { evaluateVisibilityExpr } from "../src/services/visibility";
18
+
19
+ function assert(condition: unknown, message: string) {
20
+ if (!condition) throw new Error(message);
21
+ }
22
+
23
+ function testWrappedOffsets() {
24
+ assert(wrappedDistance(100, 10, 30) === 20, "distance 10->30 should be 20");
25
+ assert(wrappedDistance(100, 90, 10) === 20, "distance 90->10 should wrap to 20");
26
+
27
+ const a = sampleWrappedOffsets(100, 10, 30, 5);
28
+ assert(
29
+ JSON.stringify(a) === JSON.stringify([10, 15, 20, 25, 30]),
30
+ `unexpected sample: ${JSON.stringify(a)}`,
31
+ );
32
+
33
+ const b = sampleWrappedOffsets(100, 90, 10, 3);
34
+ assert(
35
+ JSON.stringify(b) === JSON.stringify([90, 0, 10]),
36
+ `unexpected wrap sample: ${JSON.stringify(b)}`,
37
+ );
38
+ }
39
+
40
+ function testBridgeSelection() {
41
+ const idx = pickExitIndex([
42
+ { insideAbove: true, insideBelow: true },
43
+ { insideAbove: false, insideBelow: true },
44
+ { insideAbove: false, insideBelow: false },
45
+ ]);
46
+ assert(idx === 1, `expected exit index 1, got ${idx}`);
47
+
48
+ const none = pickExitIndex([{ insideAbove: true, insideBelow: true }]);
49
+ assert(none === -1, `expected -1, got ${none}`);
50
+
51
+ const score = scoreOutsideAbove([
52
+ { outsideAbove: true },
53
+ { outsideAbove: false },
54
+ { outsideAbove: true },
55
+ ]);
56
+ assert(score === 2, `expected score 2, got ${score}`);
57
+ }
58
+
59
+ function testMaskOps() {
60
+ const width = 50;
61
+ const height = 50;
62
+ const mask = new Uint8Array(width * height);
63
+ mask[10 * width + 10] = 1;
64
+ mask[10 * width + 20] = 1;
65
+
66
+ const r = findMinimalConnectRadius(mask, width, height, 20);
67
+ const closed = circularMorphology(mask, width, height, r, "closing");
68
+ assert(isMaskConnected8(closed, width, height), `closed mask should be connected (r=${r})`);
69
+ if (r > 0) {
70
+ const closedPrev = circularMorphology(mask, width, height, r - 1, "closing");
71
+ assert(
72
+ !isMaskConnected8(closedPrev, width, height),
73
+ `r should be minimal (r=${r})`,
74
+ );
75
+ }
76
+
77
+ const donut = new Uint8Array(9 * 9);
78
+ for (let y = 1; y <= 7; y++) {
79
+ for (let x = 1; x <= 7; x++) donut[y * 9 + x] = 1;
80
+ }
81
+ for (let y = 3; y <= 5; y++) {
82
+ for (let x = 3; x <= 5; x++) donut[y * 9 + x] = 0;
83
+ }
84
+ const filled = fillHoles(donut, 9, 9);
85
+ assert(filled[4 * 9 + 4] === 1, "hole should be filled");
86
+
87
+ const imgW = 2;
88
+ const imgH = 1;
89
+ const rgba = new Uint8ClampedArray([
90
+ 255, 255, 255, 255, 10, 10, 10, 254,
91
+ ]);
92
+ const imageData = { width: imgW, height: imgH, data: rgba } as unknown as ImageData;
93
+ const paddedWidth = imgW + 4;
94
+ const paddedHeight = imgH + 4;
95
+ const created = createMask(imageData, {
96
+ threshold: 10,
97
+ padding: 2,
98
+ paddedWidth,
99
+ paddedHeight,
100
+ maskMode: "auto",
101
+ alphaOpaqueCutoff: 250,
102
+ });
103
+ assert(created[2 * paddedWidth + 2] === 0, "white pixel should be background");
104
+ assert(created[2 * paddedWidth + 3] === 1, "non-white pixel should be foreground");
105
+ }
106
+
107
+ function testEdgeScale() {
108
+ const currentMax = 100;
109
+ const baseBounds = { width: 50, height: 20 };
110
+ const expandedBounds = { width: 70, height: 40 };
111
+ const { width, height, scale } = computeDetectEdgeSize(currentMax, baseBounds, expandedBounds);
112
+ assert(scale === 2, `expected scale 2, got ${scale}`);
113
+ assert(width === 140, `expected width 140, got ${width}`);
114
+ assert(height === 80, `expected height 80, got ${height}`);
115
+ }
116
+
117
+ function testVisibilityDsl() {
118
+ const layers = new Map([
119
+ ["ruler-overlay", { exists: true, objectCount: 2 }],
120
+ ["feature-overlay", { exists: true, objectCount: 0 }],
121
+ ]);
122
+
123
+ const context = {
124
+ activeToolId: "pooder.kit.image",
125
+ isSessionActive: (toolId: string) => toolId === "pooder.kit.feature",
126
+ hasAnyActiveSession: () => true,
127
+ layers,
128
+ };
129
+
130
+ assert(
131
+ evaluateVisibilityExpr({ op: "const", value: true }, context) === true,
132
+ "const true failed",
133
+ );
134
+ assert(
135
+ evaluateVisibilityExpr({ op: "const", value: false }, context) === false,
136
+ "const false failed",
137
+ );
138
+ assert(
139
+ evaluateVisibilityExpr(
140
+ { op: "activeToolIn", ids: ["pooder.kit.image"] },
141
+ context,
142
+ ) === true,
143
+ "activeToolIn true failed",
144
+ );
145
+ assert(
146
+ evaluateVisibilityExpr(
147
+ { op: "activeToolIn", ids: ["pooder.kit.white-ink"] },
148
+ context,
149
+ ) === false,
150
+ "activeToolIn false failed",
151
+ );
152
+ assert(
153
+ evaluateVisibilityExpr(
154
+ { op: "sessionActive", toolId: "pooder.kit.feature" },
155
+ context,
156
+ ) === true,
157
+ "sessionActive true failed",
158
+ );
159
+ assert(
160
+ evaluateVisibilityExpr(
161
+ { op: "sessionActive", toolId: "pooder.kit.ruler" },
162
+ context,
163
+ ) === false,
164
+ "sessionActive false failed",
165
+ );
166
+ assert(
167
+ evaluateVisibilityExpr({ op: "anySessionActive" }, context) === true,
168
+ "anySessionActive true failed",
169
+ );
170
+ assert(
171
+ evaluateVisibilityExpr(
172
+ { op: "layerExists", layerId: "ruler-overlay" },
173
+ context,
174
+ ) === true,
175
+ "layerExists true failed",
176
+ );
177
+ assert(
178
+ evaluateVisibilityExpr(
179
+ { op: "layerExists", layerId: "missing-layer" },
180
+ context,
181
+ ) === false,
182
+ "layerExists false failed",
183
+ );
184
+
185
+ const comparisons: Array<{
186
+ cmp: ">" | ">=" | "==" | "<" | "<=";
187
+ value: number;
188
+ expected: boolean;
189
+ }> = [
190
+ { cmp: ">", value: 1, expected: true },
191
+ { cmp: ">=", value: 2, expected: true },
192
+ { cmp: "==", value: 2, expected: true },
193
+ { cmp: "<", value: 2, expected: false },
194
+ { cmp: "<=", value: 1, expected: false },
195
+ ];
196
+ comparisons.forEach((entry) => {
197
+ assert(
198
+ evaluateVisibilityExpr(
199
+ {
200
+ op: "layerObjectCount",
201
+ layerId: "ruler-overlay",
202
+ cmp: entry.cmp,
203
+ value: entry.value,
204
+ },
205
+ context,
206
+ ) === entry.expected,
207
+ `layerObjectCount ${entry.cmp} failed`,
208
+ );
209
+ });
210
+
211
+ assert(
212
+ evaluateVisibilityExpr(
213
+ {
214
+ op: "not",
215
+ expr: { op: "activeToolIn", ids: ["pooder.kit.white-ink"] },
216
+ },
217
+ context,
218
+ ) === true,
219
+ "not failed",
220
+ );
221
+ assert(
222
+ evaluateVisibilityExpr(
223
+ {
224
+ op: "all",
225
+ exprs: [
226
+ { op: "layerExists", layerId: "ruler-overlay" },
227
+ { op: "sessionActive", toolId: "pooder.kit.feature" },
228
+ ],
229
+ },
230
+ context,
231
+ ) === true,
232
+ "all failed",
233
+ );
234
+ assert(
235
+ evaluateVisibilityExpr(
236
+ {
237
+ op: "any",
238
+ exprs: [
239
+ { op: "layerExists", layerId: "missing-layer" },
240
+ { op: "activeToolIn", ids: ["pooder.kit.image"] },
241
+ ],
242
+ },
243
+ context,
244
+ ) === true,
245
+ "any failed",
246
+ );
247
+ }
248
+
249
+ function main() {
250
+ testWrappedOffsets();
251
+ testBridgeSelection();
252
+ testMaskOps();
253
+ testEdgeScale();
254
+ testVisibilityDsl();
255
+ console.log("ok");
256
+ }
257
+
258
+ main();
@@ -1,15 +1,15 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "CommonJS",
5
- "lib": ["ES2020", "DOM"],
6
- "moduleResolution": "Node",
7
- "esModuleInterop": true,
8
- "skipLibCheck": true,
9
- "strict": true,
10
- "outDir": ".test-dist",
11
- "rootDir": "."
12
- },
13
- "include": ["tests/**/*.ts", "src/**/*.ts"]
14
- }
15
-
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "CommonJS",
5
+ "lib": ["ES2020", "DOM"],
6
+ "moduleResolution": "Node",
7
+ "esModuleInterop": true,
8
+ "skipLibCheck": true,
9
+ "strict": true,
10
+ "outDir": ".test-dist",
11
+ "rootDir": "."
12
+ },
13
+ "include": ["tests/**/*.ts", "src/**/*.ts"]
14
+ }
15
+
@@ -1,64 +0,0 @@
1
- import { Service, ServiceContext, WORKBENCH_SERVICE } from "@pooder/core";
2
- import { CanvasService } from "../services";
3
-
4
- const CANVAS_SERVICE_ID = "CanvasService";
5
- const HIDDEN_DIELINE_TOOLS = new Set(["pooder.kit.image", "pooder.kit.white-ink"]);
6
- const HIDDEN_RULER_TOOLS = new Set(["pooder.kit.white-ink"]);
7
-
8
- export class SceneVisibilityService implements Service {
9
- private context?: ServiceContext;
10
- private activeToolId: string | null = null;
11
- private canvasService?: CanvasService;
12
-
13
- init(context: ServiceContext) {
14
- if (this.context) {
15
- this.dispose(this.context);
16
- }
17
-
18
- const canvasService =
19
- context.get<CanvasService>(CANVAS_SERVICE_ID);
20
- if (!canvasService) {
21
- throw new Error("[SceneVisibilityService] CanvasService is required.");
22
- }
23
-
24
- this.context = context;
25
- this.canvasService = canvasService;
26
- this.activeToolId = context.get(WORKBENCH_SERVICE)?.activeToolId ?? null;
27
- context.eventBus.on("tool:activated", this.onToolActivated);
28
- context.eventBus.on("object:added", this.onObjectAdded);
29
- this.apply();
30
- }
31
-
32
- dispose(context: ServiceContext) {
33
- const activeContext = this.context ?? context;
34
- activeContext.eventBus.off("tool:activated", this.onToolActivated);
35
- activeContext.eventBus.off("object:added", this.onObjectAdded);
36
- this.context = undefined;
37
- this.activeToolId = null;
38
- this.canvasService = undefined;
39
- }
40
-
41
- private onToolActivated = (e: { id: string | null }) => {
42
- this.activeToolId = e.id;
43
- this.apply();
44
- };
45
-
46
- private onObjectAdded = () => {
47
- this.apply();
48
- };
49
-
50
- private apply() {
51
- if (!this.canvasService) return;
52
-
53
- const dielineLayer = this.canvasService.getLayer("dieline-overlay");
54
- if (dielineLayer) {
55
- const visible = !HIDDEN_DIELINE_TOOLS.has(this.activeToolId || "");
56
- this.canvasService.setLayerVisibility("dieline-overlay", visible);
57
- }
58
-
59
- const rulerVisible = !HIDDEN_RULER_TOOLS.has(this.activeToolId || "");
60
- this.canvasService.setLayerVisibility("ruler-overlay", rulerVisible);
61
-
62
- this.canvasService.requestRenderAll();
63
- }
64
- }