@pooder/kit 5.3.0 → 5.3.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 -249
  2. package/.test-dist/src/ViewportSystem.js +75 -75
  3. package/.test-dist/src/background.js +203 -203
  4. package/.test-dist/src/bridgeSelection.js +20 -20
  5. package/.test-dist/src/constraints.js +237 -237
  6. package/.test-dist/src/dieline.js +818 -818
  7. package/.test-dist/src/edgeScale.js +12 -12
  8. package/.test-dist/src/feature.js +826 -826
  9. package/.test-dist/src/featureComplete.js +32 -32
  10. package/.test-dist/src/film.js +167 -167
  11. package/.test-dist/src/geometry.js +506 -506
  12. package/.test-dist/src/image.js +1250 -1250
  13. package/.test-dist/src/maskOps.js +270 -270
  14. package/.test-dist/src/mirror.js +104 -104
  15. package/.test-dist/src/renderSpec.js +2 -2
  16. package/.test-dist/src/ruler.js +343 -343
  17. package/.test-dist/src/sceneLayout.js +99 -99
  18. package/.test-dist/src/sceneLayoutModel.js +196 -196
  19. package/.test-dist/src/sceneView.js +40 -40
  20. package/.test-dist/src/sceneVisibility.js +42 -42
  21. package/.test-dist/src/size.js +332 -332
  22. package/.test-dist/src/tracer.js +544 -544
  23. package/.test-dist/src/white-ink.js +829 -829
  24. package/.test-dist/src/wrappedOffsets.js +33 -33
  25. package/CHANGELOG.md +6 -0
  26. package/dist/index.d.mts +6 -0
  27. package/dist/index.d.ts +6 -0
  28. package/dist/index.js +108 -20
  29. package/dist/index.mjs +108 -20
  30. package/package.json +1 -1
  31. package/src/coordinate.ts +106 -106
  32. package/src/extensions/background.ts +230 -230
  33. package/src/extensions/bridgeSelection.ts +17 -17
  34. package/src/extensions/constraints.ts +322 -322
  35. package/src/extensions/dieline.ts +46 -0
  36. package/src/extensions/edgeScale.ts +19 -19
  37. package/src/extensions/feature.ts +1021 -1021
  38. package/src/extensions/featureComplete.ts +46 -46
  39. package/src/extensions/film.ts +194 -194
  40. package/src/extensions/geometry.ts +752 -719
  41. package/src/extensions/image.ts +1926 -1924
  42. package/src/extensions/index.ts +11 -11
  43. package/src/extensions/maskOps.ts +283 -283
  44. package/src/extensions/mirror.ts +128 -128
  45. package/src/extensions/ruler.ts +451 -451
  46. package/src/extensions/sceneLayout.ts +140 -140
  47. package/src/extensions/sceneLayoutModel.ts +352 -342
  48. package/src/extensions/sceneVisibility.ts +71 -71
  49. package/src/extensions/size.ts +389 -389
  50. package/src/extensions/tracer.ts +58 -19
  51. package/src/extensions/white-ink.ts +1400 -1400
  52. package/src/extensions/wrappedOffsets.ts +33 -33
  53. package/src/index.ts +2 -2
  54. package/src/services/CanvasService.ts +300 -300
  55. package/src/services/ViewportSystem.ts +95 -95
  56. package/src/services/index.ts +3 -3
  57. package/src/services/renderSpec.ts +18 -18
  58. package/src/units.ts +27 -27
  59. package/tests/run.ts +118 -118
  60. package/tsconfig.test.json +15 -15
@@ -1,95 +1,95 @@
1
- import { Coordinate, Layout, Point, Size } from "../coordinate";
2
-
3
- export class ViewportSystem {
4
- private _containerSize: Size = { width: 0, height: 0 };
5
- private _physicalSize: Size = { width: 0, height: 0 };
6
- private _padding: number = 0;
7
- private _layout: Layout = {
8
- scale: 1,
9
- offsetX: 0,
10
- offsetY: 0,
11
- width: 0,
12
- height: 0,
13
- };
14
-
15
- constructor(
16
- containerSize: Size = { width: 0, height: 0 },
17
- physicalSize: Size = { width: 0, height: 0 },
18
- padding: number = 40,
19
- ) {
20
- this._containerSize = containerSize;
21
- this._physicalSize = physicalSize;
22
- this._padding = padding;
23
- this.updateLayout();
24
- }
25
-
26
- get layout(): Layout {
27
- return this._layout;
28
- }
29
-
30
- get scale(): number {
31
- return this._layout.scale;
32
- }
33
-
34
- get offset(): Point {
35
- return { x: this._layout.offsetX, y: this._layout.offsetY };
36
- }
37
-
38
- updateContainer(width: number, height: number) {
39
- if (
40
- this._containerSize.width === width &&
41
- this._containerSize.height === height
42
- )
43
- return;
44
- this._containerSize = { width, height };
45
- this.updateLayout();
46
- }
47
-
48
- updatePhysical(width: number, height: number) {
49
- if (
50
- this._physicalSize.width === width &&
51
- this._physicalSize.height === height
52
- )
53
- return;
54
- this._physicalSize = { width, height };
55
- this.updateLayout();
56
- }
57
-
58
- setPadding(padding: number) {
59
- if (this._padding === padding) return;
60
- this._padding = padding;
61
- this.updateLayout();
62
- }
63
-
64
- private updateLayout() {
65
- this._layout = Coordinate.calculateLayout(
66
- this._containerSize,
67
- this._physicalSize,
68
- this._padding,
69
- );
70
- }
71
-
72
- toPixel(value: number): number {
73
- return value * this._layout.scale;
74
- }
75
-
76
- toPhysical(value: number): number {
77
- return this._layout.scale === 0 ? 0 : value / this._layout.scale;
78
- }
79
-
80
- toPixelPoint(point: Point): Point {
81
- return {
82
- x: point.x * this._layout.scale + this._layout.offsetX,
83
- y: point.y * this._layout.scale + this._layout.offsetY,
84
- };
85
- }
86
-
87
- // Convert screen coordinate (e.g. mouse event) to physical coordinate (relative to content origin)
88
- toPhysicalPoint(point: Point): Point {
89
- if (this._layout.scale === 0) return { x: 0, y: 0 };
90
- return {
91
- x: (point.x - this._layout.offsetX) / this._layout.scale,
92
- y: (point.y - this._layout.offsetY) / this._layout.scale,
93
- };
94
- }
95
- }
1
+ import { Coordinate, Layout, Point, Size } from "../coordinate";
2
+
3
+ export class ViewportSystem {
4
+ private _containerSize: Size = { width: 0, height: 0 };
5
+ private _physicalSize: Size = { width: 0, height: 0 };
6
+ private _padding: number = 0;
7
+ private _layout: Layout = {
8
+ scale: 1,
9
+ offsetX: 0,
10
+ offsetY: 0,
11
+ width: 0,
12
+ height: 0,
13
+ };
14
+
15
+ constructor(
16
+ containerSize: Size = { width: 0, height: 0 },
17
+ physicalSize: Size = { width: 0, height: 0 },
18
+ padding: number = 40,
19
+ ) {
20
+ this._containerSize = containerSize;
21
+ this._physicalSize = physicalSize;
22
+ this._padding = padding;
23
+ this.updateLayout();
24
+ }
25
+
26
+ get layout(): Layout {
27
+ return this._layout;
28
+ }
29
+
30
+ get scale(): number {
31
+ return this._layout.scale;
32
+ }
33
+
34
+ get offset(): Point {
35
+ return { x: this._layout.offsetX, y: this._layout.offsetY };
36
+ }
37
+
38
+ updateContainer(width: number, height: number) {
39
+ if (
40
+ this._containerSize.width === width &&
41
+ this._containerSize.height === height
42
+ )
43
+ return;
44
+ this._containerSize = { width, height };
45
+ this.updateLayout();
46
+ }
47
+
48
+ updatePhysical(width: number, height: number) {
49
+ if (
50
+ this._physicalSize.width === width &&
51
+ this._physicalSize.height === height
52
+ )
53
+ return;
54
+ this._physicalSize = { width, height };
55
+ this.updateLayout();
56
+ }
57
+
58
+ setPadding(padding: number) {
59
+ if (this._padding === padding) return;
60
+ this._padding = padding;
61
+ this.updateLayout();
62
+ }
63
+
64
+ private updateLayout() {
65
+ this._layout = Coordinate.calculateLayout(
66
+ this._containerSize,
67
+ this._physicalSize,
68
+ this._padding,
69
+ );
70
+ }
71
+
72
+ toPixel(value: number): number {
73
+ return value * this._layout.scale;
74
+ }
75
+
76
+ toPhysical(value: number): number {
77
+ return this._layout.scale === 0 ? 0 : value / this._layout.scale;
78
+ }
79
+
80
+ toPixelPoint(point: Point): Point {
81
+ return {
82
+ x: point.x * this._layout.scale + this._layout.offsetX,
83
+ y: point.y * this._layout.scale + this._layout.offsetY,
84
+ };
85
+ }
86
+
87
+ // Convert screen coordinate (e.g. mouse event) to physical coordinate (relative to content origin)
88
+ toPhysicalPoint(point: Point): Point {
89
+ if (this._layout.scale === 0) return { x: 0, y: 0 };
90
+ return {
91
+ x: (point.x - this._layout.offsetX) / this._layout.scale,
92
+ y: (point.y - this._layout.offsetY) / this._layout.scale,
93
+ };
94
+ }
95
+ }
@@ -1,3 +1,3 @@
1
- export { default as CanvasService } from "./CanvasService";
2
- export * from "./renderSpec";
3
- export * from "./ViewportSystem";
1
+ export { default as CanvasService } from "./CanvasService";
2
+ export * from "./renderSpec";
3
+ export * from "./ViewportSystem";
@@ -1,18 +1,18 @@
1
- export type RenderObjectType = "rect" | "image" | "path";
2
-
3
- export type RenderProps = Record<string, any>;
4
-
5
- export interface RenderObjectSpec {
6
- id: string;
7
- type: RenderObjectType;
8
- props: RenderProps;
9
- data?: Record<string, any>;
10
- src?: string;
11
- }
12
-
13
- export interface RenderLayerSpec {
14
- id: string;
15
- objects: RenderObjectSpec[];
16
- props?: RenderProps;
17
- }
18
-
1
+ export type RenderObjectType = "rect" | "image" | "path";
2
+
3
+ export type RenderProps = Record<string, any>;
4
+
5
+ export interface RenderObjectSpec {
6
+ id: string;
7
+ type: RenderObjectType;
8
+ props: RenderProps;
9
+ data?: Record<string, any>;
10
+ src?: string;
11
+ }
12
+
13
+ export interface RenderLayerSpec {
14
+ id: string;
15
+ objects: RenderObjectSpec[];
16
+ props?: RenderProps;
17
+ }
18
+
package/src/units.ts CHANGED
@@ -1,27 +1,27 @@
1
- import { Coordinate, Unit } from "./coordinate";
2
-
3
- export function parseLengthToMm(input: number | string, defaultUnit: Unit): number {
4
- if (typeof input === "number") {
5
- if (!Number.isFinite(input)) return 0;
6
- return Coordinate.convertUnit(input, defaultUnit, "mm");
7
- }
8
-
9
- const raw = input.trim();
10
- if (!raw) return 0;
11
-
12
- const match = raw.match(/^([+-]?\d+(?:\.\d+)?)\s*(px|mm|cm|in)?$/i);
13
- if (!match) return 0;
14
-
15
- const value = Number(match[1]);
16
- if (!Number.isFinite(value)) return 0;
17
-
18
- const unit = (match[2]?.toLowerCase() as Unit | undefined) ?? defaultUnit;
19
- return Coordinate.convertUnit(value, unit, "mm");
20
- }
21
-
22
- export function formatMm(valueMm: number, displayUnit: Unit, fractionDigits: number = 2): string {
23
- if (!Number.isFinite(valueMm)) return "0";
24
- const value = Coordinate.convertUnit(valueMm, "mm", displayUnit);
25
- const rounded = Number(value.toFixed(fractionDigits));
26
- return rounded.toString();
27
- }
1
+ import { Coordinate, Unit } from "./coordinate";
2
+
3
+ export function parseLengthToMm(input: number | string, defaultUnit: Unit): number {
4
+ if (typeof input === "number") {
5
+ if (!Number.isFinite(input)) return 0;
6
+ return Coordinate.convertUnit(input, defaultUnit, "mm");
7
+ }
8
+
9
+ const raw = input.trim();
10
+ if (!raw) return 0;
11
+
12
+ const match = raw.match(/^([+-]?\d+(?:\.\d+)?)\s*(px|mm|cm|in)?$/i);
13
+ if (!match) return 0;
14
+
15
+ const value = Number(match[1]);
16
+ if (!Number.isFinite(value)) return 0;
17
+
18
+ const unit = (match[2]?.toLowerCase() as Unit | undefined) ?? defaultUnit;
19
+ return Coordinate.convertUnit(value, unit, "mm");
20
+ }
21
+
22
+ export function formatMm(valueMm: number, displayUnit: Unit, fractionDigits: number = 2): string {
23
+ if (!Number.isFinite(valueMm)) return "0";
24
+ const value = Coordinate.convertUnit(valueMm, "mm", displayUnit);
25
+ const rounded = Number(value.toFixed(fractionDigits));
26
+ return rounded.toString();
27
+ }
package/tests/run.ts CHANGED
@@ -1,118 +1,118 @@
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 { 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,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
+