@pooder/kit 5.3.1 → 5.4.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 (90) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/index.d.mts +243 -36
  3. package/dist/index.d.ts +243 -36
  4. package/dist/index.js +2278 -1041
  5. package/dist/index.mjs +2278 -1041
  6. package/package.json +1 -1
  7. package/src/coordinate.ts +106 -106
  8. package/src/extensions/background.ts +323 -230
  9. package/src/extensions/bridgeSelection.ts +17 -17
  10. package/src/extensions/constraints.ts +322 -322
  11. package/src/extensions/dieline.ts +1149 -1076
  12. package/src/extensions/dielineShape.ts +109 -0
  13. package/src/extensions/edgeScale.ts +19 -19
  14. package/src/extensions/feature.ts +1137 -1021
  15. package/src/extensions/featureComplete.ts +46 -46
  16. package/src/extensions/film.ts +266 -194
  17. package/src/extensions/geometry.ts +885 -752
  18. package/src/extensions/image.ts +2054 -1926
  19. package/src/extensions/index.ts +11 -11
  20. package/src/extensions/maskOps.ts +283 -283
  21. package/src/extensions/mirror.ts +128 -128
  22. package/src/extensions/ruler.ts +654 -451
  23. package/src/extensions/sceneLayout.ts +140 -140
  24. package/src/extensions/sceneLayoutModel.ts +364 -352
  25. package/src/extensions/sceneVisibility.ts +64 -71
  26. package/src/extensions/size.ts +389 -389
  27. package/src/extensions/tracer.ts +1019 -1019
  28. package/src/extensions/white-ink.ts +1567 -1514
  29. package/src/extensions/wrappedOffsets.ts +33 -33
  30. package/src/index.ts +2 -2
  31. package/src/services/CanvasService.ts +832 -300
  32. package/src/services/ViewportSystem.ts +95 -95
  33. package/src/services/index.ts +3 -3
  34. package/src/services/renderSpec.ts +53 -18
  35. package/src/units.ts +27 -27
  36. package/tests/run.ts +118 -118
  37. package/tsconfig.test.json +15 -15
  38. package/.test-dist/src/CanvasService.js +0 -249
  39. package/.test-dist/src/ViewportSystem.js +0 -75
  40. package/.test-dist/src/background.js +0 -203
  41. package/.test-dist/src/bridgeSelection.js +0 -20
  42. package/.test-dist/src/constraints.js +0 -237
  43. package/.test-dist/src/coordinate.js +0 -74
  44. package/.test-dist/src/dieline.js +0 -818
  45. package/.test-dist/src/edgeScale.js +0 -12
  46. package/.test-dist/src/extensions/background.js +0 -203
  47. package/.test-dist/src/extensions/bridgeSelection.js +0 -20
  48. package/.test-dist/src/extensions/constraints.js +0 -237
  49. package/.test-dist/src/extensions/dieline.js +0 -828
  50. package/.test-dist/src/extensions/edgeScale.js +0 -12
  51. package/.test-dist/src/extensions/feature.js +0 -825
  52. package/.test-dist/src/extensions/featureComplete.js +0 -32
  53. package/.test-dist/src/extensions/film.js +0 -167
  54. package/.test-dist/src/extensions/geometry.js +0 -545
  55. package/.test-dist/src/extensions/image.js +0 -1529
  56. package/.test-dist/src/extensions/index.js +0 -30
  57. package/.test-dist/src/extensions/maskOps.js +0 -279
  58. package/.test-dist/src/extensions/mirror.js +0 -104
  59. package/.test-dist/src/extensions/ruler.js +0 -345
  60. package/.test-dist/src/extensions/sceneLayout.js +0 -96
  61. package/.test-dist/src/extensions/sceneLayoutModel.js +0 -196
  62. package/.test-dist/src/extensions/sceneVisibility.js +0 -62
  63. package/.test-dist/src/extensions/size.js +0 -331
  64. package/.test-dist/src/extensions/tracer.js +0 -538
  65. package/.test-dist/src/extensions/white-ink.js +0 -1190
  66. package/.test-dist/src/extensions/wrappedOffsets.js +0 -33
  67. package/.test-dist/src/feature.js +0 -826
  68. package/.test-dist/src/featureComplete.js +0 -32
  69. package/.test-dist/src/film.js +0 -167
  70. package/.test-dist/src/geometry.js +0 -506
  71. package/.test-dist/src/image.js +0 -1250
  72. package/.test-dist/src/index.js +0 -18
  73. package/.test-dist/src/maskOps.js +0 -270
  74. package/.test-dist/src/mirror.js +0 -104
  75. package/.test-dist/src/renderSpec.js +0 -2
  76. package/.test-dist/src/ruler.js +0 -343
  77. package/.test-dist/src/sceneLayout.js +0 -99
  78. package/.test-dist/src/sceneLayoutModel.js +0 -196
  79. package/.test-dist/src/sceneView.js +0 -40
  80. package/.test-dist/src/sceneVisibility.js +0 -42
  81. package/.test-dist/src/services/CanvasService.js +0 -249
  82. package/.test-dist/src/services/ViewportSystem.js +0 -76
  83. package/.test-dist/src/services/index.js +0 -24
  84. package/.test-dist/src/services/renderSpec.js +0 -2
  85. package/.test-dist/src/size.js +0 -332
  86. package/.test-dist/src/tracer.js +0 -544
  87. package/.test-dist/src/units.js +0 -30
  88. package/.test-dist/src/white-ink.js +0 -829
  89. package/.test-dist/src/wrappedOffsets.js +0 -33
  90. package/.test-dist/tests/run.js +0 -94
@@ -1,352 +1,364 @@
1
- import type { ConfigurationService } from "@pooder/core";
2
- import type { CanvasService } from "../services";
3
- import { Coordinate, Unit } from "../coordinate";
4
- import { parseLengthToMm } from "../units";
5
-
6
- export type SizeConstraintMode = "free" | "lockAspect" | "equal";
7
- export type CutMode = "trim" | "outset" | "inset";
8
-
9
- export interface SizeState {
10
- unit: Unit;
11
- actualWidthMm: number;
12
- actualHeightMm: number;
13
- constraintMode: SizeConstraintMode;
14
- aspectRatio: number;
15
- cutMode: CutMode;
16
- cutMarginMm: number;
17
- viewPadding: number | string;
18
- minMm: number;
19
- maxMm: number;
20
- stepMm: number;
21
- }
22
-
23
- export interface SceneRect {
24
- left: number;
25
- top: number;
26
- width: number;
27
- height: number;
28
- centerX: number;
29
- centerY: number;
30
- }
31
-
32
- export interface SceneLayoutSnapshot {
33
- scale: number;
34
- canvasWidth: number;
35
- canvasHeight: number;
36
- trimRect: SceneRect;
37
- cutRect: SceneRect;
38
- bleedRect: SceneRect;
39
- trimWidthMm: number;
40
- trimHeightMm: number;
41
- cutWidthMm: number;
42
- cutHeightMm: number;
43
- cutMode: CutMode;
44
- cutMarginMm: number;
45
- }
46
-
47
- export interface SceneGeometrySnapshot {
48
- shape: "rect" | "circle" | "ellipse" | "custom";
49
- unit: "mm";
50
- displayUnit: Unit;
51
- x: number;
52
- y: number;
53
- width: number;
54
- height: number;
55
- radius: number;
56
- offset: number;
57
- scale: number;
58
- pathData?: string;
59
- customSourceWidthPx?: number;
60
- customSourceHeightPx?: number;
61
- }
62
-
63
- const DEFAULT_SIZE_STATE: SizeState = {
64
- unit: "mm",
65
- actualWidthMm: 500,
66
- actualHeightMm: 500,
67
- constraintMode: "free",
68
- aspectRatio: 1,
69
- cutMode: "trim",
70
- cutMarginMm: 0,
71
- viewPadding: 140,
72
- minMm: 10,
73
- maxMm: 2000,
74
- stepMm: 0.1,
75
- };
76
-
77
- function clamp(value: number, min: number, max: number): number {
78
- return Math.max(min, Math.min(max, value));
79
- }
80
-
81
- function roundToStep(value: number, step: number): number {
82
- if (!Number.isFinite(step) || step <= 0) return value;
83
- return Math.round(value / step) * step;
84
- }
85
-
86
- export function sanitizeMmValue(
87
- valueMm: number,
88
- limits: { minMm: number; maxMm: number; stepMm: number },
89
- ): number {
90
- if (!Number.isFinite(valueMm)) return limits.minMm;
91
- const rounded = roundToStep(valueMm, limits.stepMm);
92
- return clamp(rounded, limits.minMm, limits.maxMm);
93
- }
94
-
95
- export function normalizeUnit(value: unknown): Unit {
96
- if (value === "cm" || value === "in") return value;
97
- return "mm";
98
- }
99
-
100
- export function normalizeConstraintMode(value: unknown): SizeConstraintMode {
101
- if (value === "lockAspect" || value === "equal") return value;
102
- return "free";
103
- }
104
-
105
- export function normalizeCutMode(value: unknown): CutMode {
106
- if (value === "outset" || value === "inset") return value;
107
- return "trim";
108
- }
109
-
110
- export function toMm(value: number, fromUnit: Unit): number {
111
- return Coordinate.convertUnit(value, fromUnit, "mm");
112
- }
113
-
114
- export function fromMm(valueMm: number, toUnit: Unit): number {
115
- return Coordinate.convertUnit(valueMm, "mm", toUnit);
116
- }
117
-
118
- export function resolvePaddingPx(
119
- raw: number | string,
120
- containerWidth: number,
121
- containerHeight: number,
122
- ): number {
123
- if (typeof raw === "number") return Math.max(0, raw);
124
- if (typeof raw === "string") {
125
- if (raw.endsWith("%")) {
126
- const percent = parseFloat(raw) / 100;
127
- if (!Number.isFinite(percent)) return 0;
128
- return Math.max(0, Math.min(containerWidth, containerHeight) * percent);
129
- }
130
- const fixed = parseFloat(raw);
131
- return Number.isFinite(fixed) ? Math.max(0, fixed) : 0;
132
- }
133
- return 0;
134
- }
135
-
136
- export function readSizeState(configService: ConfigurationService): SizeState {
137
- const unit = normalizeUnit(
138
- configService.get("size.unit", DEFAULT_SIZE_STATE.unit),
139
- );
140
-
141
- const minMm = Math.max(
142
- 0.1,
143
- Number(configService.get("size.minMm", DEFAULT_SIZE_STATE.minMm)),
144
- );
145
- const maxMm = Math.max(
146
- minMm,
147
- Number(configService.get("size.maxMm", DEFAULT_SIZE_STATE.maxMm)),
148
- );
149
- const stepMm = Math.max(
150
- 0.001,
151
- Number(configService.get("size.stepMm", DEFAULT_SIZE_STATE.stepMm)),
152
- );
153
-
154
- const actualWidthMm = sanitizeMmValue(
155
- parseLengthToMm(
156
- configService.get("size.actualWidthMm", DEFAULT_SIZE_STATE.actualWidthMm),
157
- "mm",
158
- ),
159
- { minMm, maxMm, stepMm },
160
- );
161
- const actualHeightMm = sanitizeMmValue(
162
- parseLengthToMm(
163
- configService.get(
164
- "size.actualHeightMm",
165
- DEFAULT_SIZE_STATE.actualHeightMm,
166
- ),
167
- "mm",
168
- ),
169
- { minMm, maxMm, stepMm },
170
- );
171
-
172
- const aspectRaw = Number(
173
- configService.get("size.aspectRatio", DEFAULT_SIZE_STATE.aspectRatio),
174
- );
175
- const aspectRatio =
176
- Number.isFinite(aspectRaw) && aspectRaw > 0
177
- ? aspectRaw
178
- : actualWidthMm / Math.max(0.001, actualHeightMm);
179
-
180
- const cutMarginMm = Math.max(
181
- 0,
182
- parseLengthToMm(
183
- configService.get("size.cutMarginMm", DEFAULT_SIZE_STATE.cutMarginMm),
184
- "mm",
185
- ),
186
- );
187
-
188
- const viewPadding = configService.get(
189
- "size.viewPadding",
190
- DEFAULT_SIZE_STATE.viewPadding,
191
- );
192
-
193
- return {
194
- unit,
195
- actualWidthMm,
196
- actualHeightMm,
197
- constraintMode: normalizeConstraintMode(
198
- configService.get(
199
- "size.constraintMode",
200
- DEFAULT_SIZE_STATE.constraintMode,
201
- ),
202
- ),
203
- aspectRatio,
204
- cutMode: normalizeCutMode(
205
- configService.get("size.cutMode", DEFAULT_SIZE_STATE.cutMode),
206
- ),
207
- cutMarginMm,
208
- viewPadding,
209
- minMm,
210
- maxMm,
211
- stepMm,
212
- };
213
- }
214
-
215
- function rectByCenter(
216
- centerX: number,
217
- centerY: number,
218
- width: number,
219
- height: number,
220
- ): SceneRect {
221
- return {
222
- left: centerX - width / 2,
223
- top: centerY - height / 2,
224
- width,
225
- height,
226
- centerX,
227
- centerY,
228
- };
229
- }
230
-
231
- function getCutSizeMm(size: SizeState): { widthMm: number; heightMm: number } {
232
- if (size.cutMode === "trim") {
233
- return { widthMm: size.actualWidthMm, heightMm: size.actualHeightMm };
234
- }
235
-
236
- const delta = size.cutMarginMm * 2;
237
- if (size.cutMode === "outset") {
238
- return {
239
- widthMm: size.actualWidthMm + delta,
240
- heightMm: size.actualHeightMm + delta,
241
- };
242
- }
243
-
244
- return {
245
- widthMm: Math.max(size.minMm, size.actualWidthMm - delta),
246
- heightMm: Math.max(size.minMm, size.actualHeightMm - delta),
247
- };
248
- }
249
-
250
- export function computeSceneLayout(
251
- canvasService: CanvasService,
252
- size: SizeState,
253
- ): SceneLayoutSnapshot | null {
254
- const canvasWidth = canvasService.canvas.width || 0;
255
- const canvasHeight = canvasService.canvas.height || 0;
256
- if (canvasWidth <= 0 || canvasHeight <= 0) return null;
257
-
258
- const { widthMm: cutWidthMm, heightMm: cutHeightMm } = getCutSizeMm(size);
259
- const viewWidthMm = Math.max(size.actualWidthMm, cutWidthMm);
260
- const viewHeightMm = Math.max(size.actualHeightMm, cutHeightMm);
261
- if (
262
- !Number.isFinite(viewWidthMm) ||
263
- !Number.isFinite(viewHeightMm) ||
264
- viewWidthMm <= 0 ||
265
- viewHeightMm <= 0
266
- ) {
267
- return null;
268
- }
269
-
270
- const paddingPx = resolvePaddingPx(
271
- size.viewPadding,
272
- canvasWidth,
273
- canvasHeight,
274
- );
275
- canvasService.viewport.updateContainer(canvasWidth, canvasHeight);
276
- canvasService.viewport.setPadding(paddingPx);
277
- canvasService.viewport.updatePhysical(viewWidthMm, viewHeightMm);
278
-
279
- const layout = canvasService.viewport.layout;
280
- if (
281
- !Number.isFinite(layout.scale) ||
282
- !Number.isFinite(layout.offsetX) ||
283
- !Number.isFinite(layout.offsetY) ||
284
- layout.scale <= 0
285
- ) {
286
- return null;
287
- }
288
-
289
- const centerX = layout.offsetX + layout.width / 2;
290
- const centerY = layout.offsetY + layout.height / 2;
291
- const trimWidthPx = size.actualWidthMm * layout.scale;
292
- const trimHeightPx = size.actualHeightMm * layout.scale;
293
- const cutWidthPx = cutWidthMm * layout.scale;
294
- const cutHeightPx = cutHeightMm * layout.scale;
295
-
296
- const trimRect = rectByCenter(centerX, centerY, trimWidthPx, trimHeightPx);
297
- const cutRect = rectByCenter(centerX, centerY, cutWidthPx, cutHeightPx);
298
- const bleedRect = rectByCenter(
299
- centerX,
300
- centerY,
301
- Math.max(trimWidthPx, cutWidthPx),
302
- Math.max(trimHeightPx, cutHeightPx),
303
- );
304
-
305
- return {
306
- scale: layout.scale,
307
- canvasWidth,
308
- canvasHeight,
309
- trimRect,
310
- cutRect,
311
- bleedRect,
312
- trimWidthMm: size.actualWidthMm,
313
- trimHeightMm: size.actualHeightMm,
314
- cutWidthMm,
315
- cutHeightMm,
316
- cutMode: size.cutMode,
317
- cutMarginMm: size.cutMarginMm,
318
- };
319
- }
320
-
321
- export function buildSceneGeometry(
322
- configService: ConfigurationService,
323
- layout: SceneLayoutSnapshot,
324
- ): SceneGeometrySnapshot {
325
- const radiusMm = parseLengthToMm(
326
- configService.get("dieline.radius", 0),
327
- "mm",
328
- );
329
- const offset = (layout.cutRect.width - layout.trimRect.width) / 2;
330
- const sourceWidth = Number(configService.get("dieline.customSourceWidthPx", 0));
331
- const sourceHeight = Number(
332
- configService.get("dieline.customSourceHeightPx", 0),
333
- );
334
-
335
- return {
336
- shape: configService.get("dieline.shape", "rect"),
337
- unit: "mm",
338
- displayUnit: normalizeUnit(configService.get("size.unit", "mm")),
339
- x: layout.trimRect.centerX,
340
- y: layout.trimRect.centerY,
341
- width: layout.trimRect.width,
342
- height: layout.trimRect.height,
343
- radius: radiusMm * layout.scale,
344
- offset,
345
- scale: layout.scale,
346
- pathData: configService.get("dieline.pathData"),
347
- customSourceWidthPx:
348
- Number.isFinite(sourceWidth) && sourceWidth > 0 ? sourceWidth : undefined,
349
- customSourceHeightPx:
350
- Number.isFinite(sourceHeight) && sourceHeight > 0 ? sourceHeight : undefined,
351
- };
352
- }
1
+ import type { ConfigurationService } from "@pooder/core";
2
+ import type { CanvasService } from "../services";
3
+ import { Coordinate, Unit } from "../coordinate";
4
+ import { parseLengthToMm } from "../units";
5
+ import {
6
+ DEFAULT_DIELINE_SHAPE,
7
+ DEFAULT_DIELINE_SHAPE_STYLE,
8
+ normalizeShapeStyle,
9
+ normalizeDielineShape,
10
+ } from "./dielineShape";
11
+ import type { DielineShape, DielineShapeStyle } from "./dielineShape";
12
+
13
+ export type SizeConstraintMode = "free" | "lockAspect" | "equal";
14
+ export type CutMode = "trim" | "outset" | "inset";
15
+
16
+ export interface SizeState {
17
+ unit: Unit;
18
+ actualWidthMm: number;
19
+ actualHeightMm: number;
20
+ constraintMode: SizeConstraintMode;
21
+ aspectRatio: number;
22
+ cutMode: CutMode;
23
+ cutMarginMm: number;
24
+ viewPadding: number | string;
25
+ minMm: number;
26
+ maxMm: number;
27
+ stepMm: number;
28
+ }
29
+
30
+ export interface SceneRect {
31
+ left: number;
32
+ top: number;
33
+ width: number;
34
+ height: number;
35
+ centerX: number;
36
+ centerY: number;
37
+ }
38
+
39
+ export interface SceneLayoutSnapshot {
40
+ scale: number;
41
+ canvasWidth: number;
42
+ canvasHeight: number;
43
+ trimRect: SceneRect;
44
+ cutRect: SceneRect;
45
+ bleedRect: SceneRect;
46
+ trimWidthMm: number;
47
+ trimHeightMm: number;
48
+ cutWidthMm: number;
49
+ cutHeightMm: number;
50
+ cutMode: CutMode;
51
+ cutMarginMm: number;
52
+ }
53
+
54
+ export interface SceneGeometrySnapshot {
55
+ shape: DielineShape;
56
+ shapeStyle: DielineShapeStyle;
57
+ unit: "px";
58
+ x: number;
59
+ y: number;
60
+ width: number;
61
+ height: number;
62
+ radius: number;
63
+ offset: number;
64
+ scale: number;
65
+ pathData?: string;
66
+ customSourceWidthPx?: number;
67
+ customSourceHeightPx?: number;
68
+ }
69
+
70
+ const DEFAULT_SIZE_STATE: SizeState = {
71
+ unit: "mm",
72
+ actualWidthMm: 500,
73
+ actualHeightMm: 500,
74
+ constraintMode: "free",
75
+ aspectRatio: 1,
76
+ cutMode: "trim",
77
+ cutMarginMm: 0,
78
+ viewPadding: 140,
79
+ minMm: 10,
80
+ maxMm: 2000,
81
+ stepMm: 0.1,
82
+ };
83
+
84
+ function clamp(value: number, min: number, max: number): number {
85
+ return Math.max(min, Math.min(max, value));
86
+ }
87
+
88
+ function roundToStep(value: number, step: number): number {
89
+ if (!Number.isFinite(step) || step <= 0) return value;
90
+ return Math.round(value / step) * step;
91
+ }
92
+
93
+ export function sanitizeMmValue(
94
+ valueMm: number,
95
+ limits: { minMm: number; maxMm: number; stepMm: number },
96
+ ): number {
97
+ if (!Number.isFinite(valueMm)) return limits.minMm;
98
+ const rounded = roundToStep(valueMm, limits.stepMm);
99
+ return clamp(rounded, limits.minMm, limits.maxMm);
100
+ }
101
+
102
+ export function normalizeUnit(value: unknown): Unit {
103
+ if (value === "cm" || value === "in") return value;
104
+ return "mm";
105
+ }
106
+
107
+ export function normalizeConstraintMode(value: unknown): SizeConstraintMode {
108
+ if (value === "lockAspect" || value === "equal") return value;
109
+ return "free";
110
+ }
111
+
112
+ export function normalizeCutMode(value: unknown): CutMode {
113
+ if (value === "outset" || value === "inset") return value;
114
+ return "trim";
115
+ }
116
+
117
+ export function toMm(value: number, fromUnit: Unit): number {
118
+ return Coordinate.convertUnit(value, fromUnit, "mm");
119
+ }
120
+
121
+ export function fromMm(valueMm: number, toUnit: Unit): number {
122
+ return Coordinate.convertUnit(valueMm, "mm", toUnit);
123
+ }
124
+
125
+ export function resolvePaddingPx(
126
+ raw: number | string,
127
+ containerWidth: number,
128
+ containerHeight: number,
129
+ ): number {
130
+ if (typeof raw === "number") return Math.max(0, raw);
131
+ if (typeof raw === "string") {
132
+ if (raw.endsWith("%")) {
133
+ const percent = parseFloat(raw) / 100;
134
+ if (!Number.isFinite(percent)) return 0;
135
+ return Math.max(0, Math.min(containerWidth, containerHeight) * percent);
136
+ }
137
+ const fixed = parseFloat(raw);
138
+ return Number.isFinite(fixed) ? Math.max(0, fixed) : 0;
139
+ }
140
+ return 0;
141
+ }
142
+
143
+ export function readSizeState(configService: ConfigurationService): SizeState {
144
+ const unit = normalizeUnit(
145
+ configService.get("size.unit", DEFAULT_SIZE_STATE.unit),
146
+ );
147
+
148
+ const minMm = Math.max(
149
+ 0.1,
150
+ Number(configService.get("size.minMm", DEFAULT_SIZE_STATE.minMm)),
151
+ );
152
+ const maxMm = Math.max(
153
+ minMm,
154
+ Number(configService.get("size.maxMm", DEFAULT_SIZE_STATE.maxMm)),
155
+ );
156
+ const stepMm = Math.max(
157
+ 0.001,
158
+ Number(configService.get("size.stepMm", DEFAULT_SIZE_STATE.stepMm)),
159
+ );
160
+
161
+ const actualWidthMm = sanitizeMmValue(
162
+ parseLengthToMm(
163
+ configService.get("size.actualWidthMm", DEFAULT_SIZE_STATE.actualWidthMm),
164
+ "mm",
165
+ ),
166
+ { minMm, maxMm, stepMm },
167
+ );
168
+ const actualHeightMm = sanitizeMmValue(
169
+ parseLengthToMm(
170
+ configService.get(
171
+ "size.actualHeightMm",
172
+ DEFAULT_SIZE_STATE.actualHeightMm,
173
+ ),
174
+ "mm",
175
+ ),
176
+ { minMm, maxMm, stepMm },
177
+ );
178
+
179
+ const aspectRaw = Number(
180
+ configService.get("size.aspectRatio", DEFAULT_SIZE_STATE.aspectRatio),
181
+ );
182
+ const aspectRatio =
183
+ Number.isFinite(aspectRaw) && aspectRaw > 0
184
+ ? aspectRaw
185
+ : actualWidthMm / Math.max(0.001, actualHeightMm);
186
+
187
+ const cutMarginMm = Math.max(
188
+ 0,
189
+ parseLengthToMm(
190
+ configService.get("size.cutMarginMm", DEFAULT_SIZE_STATE.cutMarginMm),
191
+ "mm",
192
+ ),
193
+ );
194
+
195
+ const viewPadding = configService.get(
196
+ "size.viewPadding",
197
+ DEFAULT_SIZE_STATE.viewPadding,
198
+ );
199
+
200
+ return {
201
+ unit,
202
+ actualWidthMm,
203
+ actualHeightMm,
204
+ constraintMode: normalizeConstraintMode(
205
+ configService.get(
206
+ "size.constraintMode",
207
+ DEFAULT_SIZE_STATE.constraintMode,
208
+ ),
209
+ ),
210
+ aspectRatio,
211
+ cutMode: normalizeCutMode(
212
+ configService.get("size.cutMode", DEFAULT_SIZE_STATE.cutMode),
213
+ ),
214
+ cutMarginMm,
215
+ viewPadding,
216
+ minMm,
217
+ maxMm,
218
+ stepMm,
219
+ };
220
+ }
221
+
222
+ function rectByCenter(
223
+ centerX: number,
224
+ centerY: number,
225
+ width: number,
226
+ height: number,
227
+ ): SceneRect {
228
+ return {
229
+ left: centerX - width / 2,
230
+ top: centerY - height / 2,
231
+ width,
232
+ height,
233
+ centerX,
234
+ centerY,
235
+ };
236
+ }
237
+
238
+ function getCutSizeMm(size: SizeState): { widthMm: number; heightMm: number } {
239
+ if (size.cutMode === "trim") {
240
+ return { widthMm: size.actualWidthMm, heightMm: size.actualHeightMm };
241
+ }
242
+
243
+ const delta = size.cutMarginMm * 2;
244
+ if (size.cutMode === "outset") {
245
+ return {
246
+ widthMm: size.actualWidthMm + delta,
247
+ heightMm: size.actualHeightMm + delta,
248
+ };
249
+ }
250
+
251
+ return {
252
+ widthMm: Math.max(size.minMm, size.actualWidthMm - delta),
253
+ heightMm: Math.max(size.minMm, size.actualHeightMm - delta),
254
+ };
255
+ }
256
+
257
+ export function computeSceneLayout(
258
+ canvasService: CanvasService,
259
+ size: SizeState,
260
+ ): SceneLayoutSnapshot | null {
261
+ const canvasWidth = canvasService.canvas.width || 0;
262
+ const canvasHeight = canvasService.canvas.height || 0;
263
+ if (canvasWidth <= 0 || canvasHeight <= 0) return null;
264
+
265
+ const { widthMm: cutWidthMm, heightMm: cutHeightMm } = getCutSizeMm(size);
266
+ const viewWidthMm = Math.max(size.actualWidthMm, cutWidthMm);
267
+ const viewHeightMm = Math.max(size.actualHeightMm, cutHeightMm);
268
+ if (
269
+ !Number.isFinite(viewWidthMm) ||
270
+ !Number.isFinite(viewHeightMm) ||
271
+ viewWidthMm <= 0 ||
272
+ viewHeightMm <= 0
273
+ ) {
274
+ return null;
275
+ }
276
+
277
+ const paddingPx = resolvePaddingPx(
278
+ size.viewPadding,
279
+ canvasWidth,
280
+ canvasHeight,
281
+ );
282
+ canvasService.viewport.updateContainer(canvasWidth, canvasHeight);
283
+ canvasService.viewport.setPadding(paddingPx);
284
+ canvasService.viewport.updatePhysical(viewWidthMm, viewHeightMm);
285
+
286
+ const layout = canvasService.viewport.layout;
287
+ if (
288
+ !Number.isFinite(layout.scale) ||
289
+ !Number.isFinite(layout.offsetX) ||
290
+ !Number.isFinite(layout.offsetY) ||
291
+ layout.scale <= 0
292
+ ) {
293
+ return null;
294
+ }
295
+
296
+ const centerX = layout.offsetX + layout.width / 2;
297
+ const centerY = layout.offsetY + layout.height / 2;
298
+ const trimWidthPx = size.actualWidthMm * layout.scale;
299
+ const trimHeightPx = size.actualHeightMm * layout.scale;
300
+ const cutWidthPx = cutWidthMm * layout.scale;
301
+ const cutHeightPx = cutHeightMm * layout.scale;
302
+
303
+ const trimRect = rectByCenter(centerX, centerY, trimWidthPx, trimHeightPx);
304
+ const cutRect = rectByCenter(centerX, centerY, cutWidthPx, cutHeightPx);
305
+ const bleedRect = rectByCenter(
306
+ centerX,
307
+ centerY,
308
+ Math.max(trimWidthPx, cutWidthPx),
309
+ Math.max(trimHeightPx, cutHeightPx),
310
+ );
311
+
312
+ return {
313
+ scale: layout.scale,
314
+ canvasWidth,
315
+ canvasHeight,
316
+ trimRect,
317
+ cutRect,
318
+ bleedRect,
319
+ trimWidthMm: size.actualWidthMm,
320
+ trimHeightMm: size.actualHeightMm,
321
+ cutWidthMm,
322
+ cutHeightMm,
323
+ cutMode: size.cutMode,
324
+ cutMarginMm: size.cutMarginMm,
325
+ };
326
+ }
327
+
328
+ export function buildSceneGeometry(
329
+ configService: ConfigurationService,
330
+ layout: SceneLayoutSnapshot,
331
+ ): SceneGeometrySnapshot {
332
+ const radiusMm = parseLengthToMm(
333
+ configService.get("dieline.radius", 0),
334
+ "mm",
335
+ );
336
+ const offset = (layout.cutRect.width - layout.trimRect.width) / 2;
337
+ const sourceWidth = Number(configService.get("dieline.customSourceWidthPx", 0));
338
+ const sourceHeight = Number(
339
+ configService.get("dieline.customSourceHeightPx", 0),
340
+ );
341
+ const shapeStyle = normalizeShapeStyle(
342
+ configService.get("dieline.shapeStyle", DEFAULT_DIELINE_SHAPE_STYLE),
343
+ );
344
+
345
+ return {
346
+ shape: normalizeDielineShape(
347
+ configService.get("dieline.shape", DEFAULT_DIELINE_SHAPE),
348
+ ),
349
+ shapeStyle,
350
+ unit: "px",
351
+ x: layout.trimRect.centerX,
352
+ y: layout.trimRect.centerY,
353
+ width: layout.trimRect.width,
354
+ height: layout.trimRect.height,
355
+ radius: radiusMm * layout.scale,
356
+ offset,
357
+ scale: layout.scale,
358
+ pathData: configService.get("dieline.pathData"),
359
+ customSourceWidthPx:
360
+ Number.isFinite(sourceWidth) && sourceWidth > 0 ? sourceWidth : undefined,
361
+ customSourceHeightPx:
362
+ Number.isFinite(sourceHeight) && sourceHeight > 0 ? sourceHeight : undefined,
363
+ };
364
+ }