-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.ts
407 lines (381 loc) · 12.4 KB
/
options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import {ALL_BLACK, ColorsDistributor, CyclicColorsDistributor} from './colors_distributor.ts';
import {Attributes} from './elements.ts';
import {CornersMarkerType, PosCorrectionMillimeters, RunHandlesPosition, getGlobalOptions} from './global_options.ts';
import {NO_LAYER, OptionalLayerName} from './layers.ts';
import {toFileName} from './name.ts';
/** The context for which an SVG is generated. */
export type Medium = "preview" | "laser";
export const MEDIA: Medium[] = ["preview", "laser"];
export type PartialMediaStyleAttributes = Partial<Record<Medium, Attributes>>;
/** Global attributes applied to SVG created for the given medium. */
export type MediaStyleAttributes = Readonly<Record<Medium, Attributes>>;
export function styleAttributesFromPartial({
styleAttributes = {},
defaults,
}: {
styleAttributes?: PartialMediaStyleAttributes,
defaults: MediaStyleAttributes,
}): MediaStyleAttributes {
const result: PartialMediaStyleAttributes = {};
for (const medium of MEDIA)
result[medium] = {...defaults[medium], ...styleAttributes[medium]};
return result as MediaStyleAttributes;
}
/** The side of the material. */
export type Side = "front" | "back";
export interface PartialCommonRunOptions {
id?: string;
/** The layers which belong to this run. A layer can belong to multiple runs. */
layers?: OptionalLayerName[];
styleAttributes?: PartialMediaStyleAttributes;
/**
* Side of the material on which the run should be executed. Note that for `"back"`, the contents
* of the layer needs to be mirrored in the X axis, as if viewed from the front side (which would
* actually be possible if the material was transparent).
*/
side?: Side;
/**
* Whether corners marker should be included in this run.
* Corners marker is a set of objects placed in the corners, that are not sent to the laser,
* but are included to prevent the laser software from ignoring the margins of a vector file.
*/
includeCornersMarker?: boolean;
posCorrectionMillimeters?: PosCorrectionMillimeters;
}
/** Options of a laser run. Some laser cutter software call this "layer". */
export interface CommonRunOptions {
readonly type: "cut" | "print";
readonly id: string;
readonly layers: readonly OptionalLayerName[];
readonly styleAttributes: MediaStyleAttributes;
readonly side: Side;
readonly includeCornersMarker: boolean;
readonly posCorrectionMillimeters?: PosCorrectionMillimeters;
}
export function getDefaultCutStyleAttributes({id, previewColors = ALL_BLACK}: {
id?: string,
previewColors?: ColorsDistributor,
} = {}): MediaStyleAttributes {
return {
preview: {
stroke: previewColors.get(id),
strokeWidth: 1,
vectorEffect: "non-scaling-stroke",
fill: "none",
},
laser: {
stroke: "black",
strokeWidth: getGlobalOptions().cutRunsStrokeWidth ?? 0,
vectorEffect: "non-scaling-stroke",
fill: "none",
},
};
}
export const DEFAULT_CUT_STYLE_ATTRIBUTES = getDefaultCutStyleAttributes();
export function getDefaultPrintStyleAttributes({id, previewColors = ALL_BLACK}: {
id?: string,
previewColors?: ColorsDistributor,
} = {}): MediaStyleAttributes {
return {
preview: {
fill: previewColors.get(id),
stroke: previewColors.get(id),
strokeWidth: 0,
cursor: "default",
},
laser: {
fill: "black",
stroke: "black",
strokeWidth: 0,
},
};
}
export const DEFAULT_PRINT_STYLE_ATTRIBUTES = getDefaultPrintStyleAttributes();
export interface PartialCutOptions extends PartialCommonRunOptions {
type: "cut";
}
export interface CutOptions extends CommonRunOptions {
readonly type: "cut";
}
/**
* Creates CutOptions. If layers are not specified, layer equal to the id is used. If id is
* not specified, it is set to `"cut"` and two layers are included: `"cut"` and `NO_LAYER`.
*/
export function cutOptionsFromPartial(
sheetOptions: SheetOptions,
{
id = "cut",
layers = id === "cut" ? ["cut", NO_LAYER] : [id],
styleAttributes,
side = "front",
includeCornersMarker = false,
posCorrectionMillimeters,
}: PartialCutOptions,
): CutOptions {
return {
type: "cut",
id,
layers,
styleAttributes: styleAttributesFromPartial({
styleAttributes,
defaults: getDefaultCutStyleAttributes({
id,
previewColors: sheetOptions.previewColors.cut,
}),
}),
side,
includeCornersMarker,
posCorrectionMillimeters,
};
}
export interface PartialPrintOptions extends PartialCommonRunOptions {
type: "print";
}
export interface PrintOptions extends CommonRunOptions {
readonly type: "print";
}
/**
* Creates PrintOptions. If layers are not specified, layer equal to the id is used, or `"print"`
* if id is not specified.
*/
export function printOptionsFromPartial(
sheetOptions: SheetOptions,
{
id = "print",
layers = [id],
styleAttributes,
side = "front",
includeCornersMarker = true,
posCorrectionMillimeters = sheetOptions.printPosCorrectionMillimeters,
}: PartialPrintOptions,
): PrintOptions {
return {
type: "print",
id,
layers,
styleAttributes: styleAttributesFromPartial({
styleAttributes,
defaults: getDefaultPrintStyleAttributes({
id,
previewColors: sheetOptions.previewColors.print,
}),
}),
side,
includeCornersMarker,
posCorrectionMillimeters,
};
}
export type PartialRunOptions = PartialCutOptions | PartialPrintOptions;
export type RunOptions = CutOptions | PrintOptions;
export function runOptionsFromPartial(
sheetOptions: SheetOptions, options: PartialRunOptions): RunOptions {
if (options.type === "cut")
return cutOptionsFromPartial(sheetOptions, options);
if (options.type === "print")
return printOptionsFromPartial(sheetOptions, options);
return options satisfies never;
}
const DEFAULT_FRAME_STYLE_ATTRIBUTES = styleAttributesFromPartial({
styleAttributes: {
preview: {stroke: "red"},
},
defaults: DEFAULT_CUT_STYLE_ATTRIBUTES,
});
export function getDefaultCornersMarkerStyleAttributes(args: {
id?: string,
previewColors?: ColorsDistributor,
} = {}): MediaStyleAttributes {
return styleAttributesFromPartial({
styleAttributes: {
preview: {
strokeWidth: 0,
},
laser: {
strokeWidth: 0,
},
},
defaults: getDefaultCutStyleAttributes(args),
});
}
export interface PartialCornersMarkerOptions {
enable?: boolean;
type?: CornersMarkerType;
id?: string;
styleAttributes?: PartialMediaStyleAttributes;
}
export interface CornersMarkerOptions extends Required<Readonly<PartialCornersMarkerOptions>> {}
export function cornersMarkerOptionsFromPartial(
cornersMarkerOptions: boolean | PartialCornersMarkerOptions = true): CornersMarkerOptions {
const {
type = getGlobalOptions().cornersMarkerType,
enable = !!type,
id = "corners_marker",
styleAttributes = {},
}: PartialCornersMarkerOptions =
cornersMarkerOptions === true ? {} :
cornersMarkerOptions === false ? {enable: false} :
cornersMarkerOptions;
return {
enable,
type: type || "circles",
id,
styleAttributes: styleAttributesFromPartial({
styleAttributes,
defaults: getDefaultCornersMarkerStyleAttributes({
id,
previewColors: ALL_BLACK,
}),
}),
};
}
export interface PartialReversingFrameOptions {
enable?: boolean;
id?: string;
styleAttributes?: PartialMediaStyleAttributes;
}
/**
* Properties of the reversing frame. The reversing frame is a special cut layer consisting of
* a single rectangle bounding the whole object. It allows cutting the whole work out of
* the materia and reversing it in place.
*/
export interface ReversingFrameOptions extends Required<Readonly<PartialReversingFrameOptions>> {}
export function reversingFrameOptionsFromPartial(
reversingFrameOptions: boolean | PartialReversingFrameOptions = true): ReversingFrameOptions {
const {
enable = true,
id = "reversing_frame",
styleAttributes = {},
}: PartialReversingFrameOptions = reversingFrameOptions === true ? {} :
reversingFrameOptions === false ? {enable: false} :
reversingFrameOptions;
return {
enable,
id,
styleAttributes: styleAttributesFromPartial({
styleAttributes,
defaults: DEFAULT_FRAME_STYLE_ATTRIBUTES,
}),
};
}
export const DEFAULT_PIXELS_PER_INCH = 400;
export interface PartialSheetResolution {
pixelsPerInch?: number;
pixelsPerMillimeter?: number;
pixelsPerUnit?: number;
}
export interface SheetResolution {
readonly pixelsPerUnit: number;
}
export function sheetResolutionFromPartial(
partial: PartialSheetResolution = {}, millimetersPerUnit?: number): SheetResolution {
const {
pixelsPerInch = DEFAULT_PIXELS_PER_INCH,
pixelsPerMillimeter = pixelsPerInch / 25.4,
pixelsPerUnit = pixelsPerMillimeter * (millimetersPerUnit ?? 1),
} = partial;
return {
pixelsPerUnit,
};
}
export const DEFAULT_CUT_PREVIEW_COLORS = ["#404", "#084", "#840"];
export function defaultCutPreviewColors() {
return CyclicColorsDistributor.create({
pool: DEFAULT_CUT_PREVIEW_COLORS,
initial: [[undefined, 0], ["cut", 0], ["score", 1]],
});
}
export const DEFAULT_PRINT_PREVIEW_COLORS = ["#a00", "#0a0", "#00a", "#aa0", "#a0a", "#0aa"];
export function defaultPrintPreviewColors() {
return CyclicColorsDistributor.create({
pool: DEFAULT_PRINT_PREVIEW_COLORS,
initial: [[undefined, 0], ["print", 0]],
});
}
export interface PartialPreviewColors {
cut?: ColorsDistributor;
print?: ColorsDistributor;
}
export interface PreviewColors extends Required<Readonly<PartialPreviewColors>> {}
export function previewColorsFromPartial({
cut = defaultCutPreviewColors(),
print = defaultPrintPreviewColors(),
}: PartialPreviewColors = {}): PreviewColors {
return {cut, print};
}
export interface PartialLaserRunsOptions {
/** ColorsDistributor assigning colors to runs for the laser medium. */
colorCodes?: ColorsDistributor;
handles?: RunHandlesPosition;
}
export interface LaserRunsOptions {
colorCodes?: ColorsDistributor;
handles?: RunHandlesPosition;
}
export function laserRunsOptionsFromPartial({
colorCodes = getGlobalOptions().laserRunsOptions?.colorCodes?.(),
handles = getGlobalOptions().laserRunsOptions?.handles,
}: PartialLaserRunsOptions = {}): LaserRunsOptions {
return {
colorCodes,
handles,
};
}
const NO_POS_CORRECTION: PosCorrectionMillimeters = [0, 0];
function printPosCorrectionMillimetersFromPartial(
printPosCorrection: boolean | PosCorrectionMillimeters = true): PosCorrectionMillimeters {
return printPosCorrection === true ?
getGlobalOptions().printPosCorrectionMillimeters ?? NO_POS_CORRECTION :
printPosCorrection === false ? NO_POS_CORRECTION :
printPosCorrection;
}
export const DEFAULT_SHEET_FILE_NAME = "sheet";
export interface PartialSheetOptions {
name?: string;
fileName?: string;
includeSizeInName?: boolean;
millimetersPerUnit?: number;
cornersMarker?: boolean | PartialCornersMarkerOptions;
reversingFrame?: boolean | PartialReversingFrameOptions;
resolution?: PartialSheetResolution;
previewColors?: PartialPreviewColors;
laserRunsOptions?: PartialLaserRunsOptions;
printPosCorrectionMillimeters?: boolean | PosCorrectionMillimeters;
}
export interface SheetOptions {
readonly name?: string;
readonly fileName: string;
readonly includeSizeInName: boolean;
readonly millimetersPerUnit?: number;
readonly cornersMarker: CornersMarkerOptions;
readonly reversingFrame: ReversingFrameOptions;
readonly resolution: SheetResolution;
readonly previewColors: PreviewColors;
readonly laserRunsOptions: LaserRunsOptions;
readonly printPosCorrectionMillimeters: PosCorrectionMillimeters;
}
export function sheetOptionsFromPartial({
name,
fileName = name ? toFileName(name) : DEFAULT_SHEET_FILE_NAME,
millimetersPerUnit,
includeSizeInName = millimetersPerUnit !== undefined,
cornersMarker,
reversingFrame,
resolution,
previewColors,
laserRunsOptions,
printPosCorrectionMillimeters,
}: PartialSheetOptions): SheetOptions {
return {
name,
fileName,
includeSizeInName,
millimetersPerUnit,
cornersMarker: cornersMarkerOptionsFromPartial(cornersMarker),
reversingFrame: reversingFrameOptionsFromPartial(reversingFrame),
resolution: sheetResolutionFromPartial(resolution, millimetersPerUnit),
previewColors: previewColorsFromPartial(previewColors),
laserRunsOptions: laserRunsOptionsFromPartial(laserRunsOptions),
printPosCorrectionMillimeters:
printPosCorrectionMillimetersFromPartial(printPosCorrectionMillimeters),
};
}