@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
@@ -1,364 +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
- 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
- }
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
+ }