lbrnts 0.0.19 → 0.0.20
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.
- package/dist/index.d.ts +10 -1
- package/dist/index.js +121 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -373,6 +373,15 @@ interface GenerateSvgOptions {
|
|
|
373
373
|
|
|
374
374
|
declare function generateLightBurnSvg(root: LightBurnBaseElement | LightBurnBaseElement[], options?: GenerateSvgOptions): string;
|
|
375
375
|
|
|
376
|
+
interface SplitLightBurnProjectFile {
|
|
377
|
+
fileName: string;
|
|
378
|
+
content: string;
|
|
379
|
+
cutIndex: number;
|
|
380
|
+
cutSettingName: string;
|
|
381
|
+
shapeCount: number;
|
|
382
|
+
}
|
|
383
|
+
declare function splitLightBurnProjectByCutSetting(input: string | LightBurnProject, originalFileName?: string): SplitLightBurnProjectFile[];
|
|
384
|
+
|
|
376
385
|
declare const parseXml: (xml: string) => XmlJson;
|
|
377
386
|
|
|
378
|
-
export { LightBurnBaseElement as BaseLightBurnElement, CutSetting, type CutSettingInit, type GenerateSvgOptions, LightBurnBaseElement, LightBurnProject, type LightBurnProjectInit, type Mat, Notes, type Prim, ShapeBase, ShapeBitmap, ShapeEllipse, ShapeGroup, ShapePath, type ShapePathInit, ShapeRect, ShapeText, Thumbnail, UIPrefs, VariableText, type Vert, generateLightBurnSvg, parseXml };
|
|
387
|
+
export { LightBurnBaseElement as BaseLightBurnElement, CutSetting, type CutSettingInit, type GenerateSvgOptions, LightBurnBaseElement, LightBurnProject, type LightBurnProjectInit, type Mat, Notes, type Prim, ShapeBase, ShapeBitmap, ShapeEllipse, ShapeGroup, ShapePath, type ShapePathInit, ShapeRect, ShapeText, type SplitLightBurnProjectFile, Thumbnail, UIPrefs, VariableText, type Vert, generateLightBurnSvg, parseXml, splitLightBurnProjectByCutSetting };
|
package/dist/index.js
CHANGED
|
@@ -1948,6 +1948,125 @@ function generateLightBurnSvg(root, options) {
|
|
|
1948
1948
|
const svgTree = assembleSvg(nodes, layout, []);
|
|
1949
1949
|
return stringify(svgTree);
|
|
1950
1950
|
}
|
|
1951
|
+
|
|
1952
|
+
// lib/split-by-cut-setting.ts
|
|
1953
|
+
function parseProject(input) {
|
|
1954
|
+
if (input instanceof LightBurnProject) return input;
|
|
1955
|
+
const parsed = LightBurnBaseElement.parse(input);
|
|
1956
|
+
if (!(parsed instanceof LightBurnProject)) {
|
|
1957
|
+
throw new Error("Expected a LightBurnProject XML document");
|
|
1958
|
+
}
|
|
1959
|
+
return parsed;
|
|
1960
|
+
}
|
|
1961
|
+
function fileBaseName(name, cutIndex) {
|
|
1962
|
+
return name.trim().toLowerCase().replace(/[<>:"/\\|?*\x00-\x1F]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "") || `cut-index-${cutIndex}`;
|
|
1963
|
+
}
|
|
1964
|
+
function uniqueFileName(baseName, usedFileNames) {
|
|
1965
|
+
let fileName = `${baseName}.lbrn2`;
|
|
1966
|
+
let suffix = 2;
|
|
1967
|
+
while (usedFileNames.has(fileName)) {
|
|
1968
|
+
fileName = `${baseName}-${suffix}.lbrn2`;
|
|
1969
|
+
suffix += 1;
|
|
1970
|
+
}
|
|
1971
|
+
usedFileNames.add(fileName);
|
|
1972
|
+
return fileName;
|
|
1973
|
+
}
|
|
1974
|
+
function folderNameForFile(fileName) {
|
|
1975
|
+
if (!fileName) return void 0;
|
|
1976
|
+
const baseName = fileName.split(/[\\/]/).pop() ?? fileName;
|
|
1977
|
+
return fileBaseName(baseName.replace(/\.lbrn2?$/i, ""), 0);
|
|
1978
|
+
}
|
|
1979
|
+
function cloneGroup(group, children) {
|
|
1980
|
+
const clone = Object.assign(
|
|
1981
|
+
Object.create(Object.getPrototypeOf(group)),
|
|
1982
|
+
group
|
|
1983
|
+
);
|
|
1984
|
+
clone.children = children;
|
|
1985
|
+
return clone;
|
|
1986
|
+
}
|
|
1987
|
+
function countShapes(element) {
|
|
1988
|
+
if (element instanceof ShapeGroup) {
|
|
1989
|
+
return element.children.reduce((sum, child) => sum + countShapes(child), 0);
|
|
1990
|
+
}
|
|
1991
|
+
return element instanceof ShapeBase ? 1 : 0;
|
|
1992
|
+
}
|
|
1993
|
+
function filterShapeForCutIndex(element, cutIndex) {
|
|
1994
|
+
if (element instanceof ShapeGroup) {
|
|
1995
|
+
if (element.cutIndex === cutIndex) {
|
|
1996
|
+
return {
|
|
1997
|
+
shape: cloneGroup(element, [...element.children]),
|
|
1998
|
+
count: countShapes(element)
|
|
1999
|
+
};
|
|
2000
|
+
}
|
|
2001
|
+
const children = [];
|
|
2002
|
+
let count = 0;
|
|
2003
|
+
for (const child of element.children) {
|
|
2004
|
+
const result = filterShapeForCutIndex(child, cutIndex);
|
|
2005
|
+
if (result.shape) children.push(result.shape);
|
|
2006
|
+
count += result.count;
|
|
2007
|
+
}
|
|
2008
|
+
return children.length > 0 ? { shape: cloneGroup(element, children), count } : { count };
|
|
2009
|
+
}
|
|
2010
|
+
if (element instanceof ShapeBase && element.cutIndex === cutIndex) {
|
|
2011
|
+
return { shape: element, count: 1 };
|
|
2012
|
+
}
|
|
2013
|
+
return { count: 0 };
|
|
2014
|
+
}
|
|
2015
|
+
function projectForCutSetting(project, cutSetting) {
|
|
2016
|
+
const children = [];
|
|
2017
|
+
const cutIndex = cutSetting.index;
|
|
2018
|
+
let shapeCount = 0;
|
|
2019
|
+
for (const child of project.children) {
|
|
2020
|
+
if (child instanceof CutSetting) {
|
|
2021
|
+
if (child === cutSetting) children.push(child);
|
|
2022
|
+
continue;
|
|
2023
|
+
}
|
|
2024
|
+
if (child instanceof ShapeBase) {
|
|
2025
|
+
const result = filterShapeForCutIndex(child, cutIndex);
|
|
2026
|
+
if (result.shape) children.push(result.shape);
|
|
2027
|
+
shapeCount += result.count;
|
|
2028
|
+
continue;
|
|
2029
|
+
}
|
|
2030
|
+
children.push(child);
|
|
2031
|
+
}
|
|
2032
|
+
return {
|
|
2033
|
+
project: new LightBurnProject({
|
|
2034
|
+
appVersion: project.appVersion,
|
|
2035
|
+
formatVersion: project.formatVersion,
|
|
2036
|
+
materialHeight: project.materialHeight,
|
|
2037
|
+
mirrorX: project.mirrorX,
|
|
2038
|
+
mirrorY: project.mirrorY,
|
|
2039
|
+
children
|
|
2040
|
+
}),
|
|
2041
|
+
shapeCount
|
|
2042
|
+
};
|
|
2043
|
+
}
|
|
2044
|
+
function splitLightBurnProjectByCutSetting(input, originalFileName) {
|
|
2045
|
+
const project = parseProject(input);
|
|
2046
|
+
const folderName = folderNameForFile(originalFileName);
|
|
2047
|
+
const usedFileNames = /* @__PURE__ */ new Set();
|
|
2048
|
+
const files = [];
|
|
2049
|
+
for (const cutSetting of project.children) {
|
|
2050
|
+
if (!(cutSetting instanceof CutSetting) || cutSetting.index === void 0) {
|
|
2051
|
+
continue;
|
|
2052
|
+
}
|
|
2053
|
+
const cutIndex = cutSetting.index;
|
|
2054
|
+
const cutSettingName = cutSetting.name ?? `CutIndex ${cutIndex}`;
|
|
2055
|
+
const split = projectForCutSetting(project, cutSetting);
|
|
2056
|
+
const fileName = uniqueFileName(
|
|
2057
|
+
fileBaseName(cutSettingName, cutIndex),
|
|
2058
|
+
usedFileNames
|
|
2059
|
+
);
|
|
2060
|
+
files.push({
|
|
2061
|
+
fileName: folderName ? `${folderName}/${fileName}` : fileName,
|
|
2062
|
+
content: split.project.getString(),
|
|
2063
|
+
cutIndex,
|
|
2064
|
+
cutSettingName,
|
|
2065
|
+
shapeCount: split.shapeCount
|
|
2066
|
+
});
|
|
2067
|
+
}
|
|
2068
|
+
return files;
|
|
2069
|
+
}
|
|
1951
2070
|
export {
|
|
1952
2071
|
LightBurnBaseElement as BaseLightBurnElement,
|
|
1953
2072
|
CutSetting,
|
|
@@ -1965,5 +2084,6 @@ export {
|
|
|
1965
2084
|
UIPrefs,
|
|
1966
2085
|
VariableText,
|
|
1967
2086
|
generateLightBurnSvg,
|
|
1968
|
-
parseXml
|
|
2087
|
+
parseXml,
|
|
2088
|
+
splitLightBurnProjectByCutSetting
|
|
1969
2089
|
};
|