lbrnts 0.0.12 → 0.0.13

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 (2) hide show
  1. package/dist/index.js +265 -3
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1345,6 +1345,9 @@ function arrayToMatrix(arr) {
1345
1345
  function apply(m, p) {
1346
1346
  return applyToPoint(m, p);
1347
1347
  }
1348
+ function mul(m1, m2) {
1349
+ return compose(m1, m2);
1350
+ }
1348
1351
  function matToSvg(m) {
1349
1352
  return toSVG(m);
1350
1353
  }
@@ -1678,16 +1681,275 @@ var ellipseRenderer = {
1678
1681
  }
1679
1682
  };
1680
1683
 
1684
+ // lib/svg-gen/path-data.ts
1685
+ function transformPoint(pt, matrix) {
1686
+ return apply(matrix, pt);
1687
+ }
1688
+ function shapePathToPathData(path) {
1689
+ const matrix = path.xform ? arrayToMatrix(path.xform) : identity();
1690
+ let d = "";
1691
+ for (let i = 0; i < path.prims.length; i++) {
1692
+ const prim = path.prims[i];
1693
+ const startV = path.verts[i];
1694
+ const endV = path.verts[(i + 1) % path.verts.length];
1695
+ const startPt = transformPoint({ x: startV.x, y: startV.y }, matrix);
1696
+ if (i === 0) {
1697
+ d += `M ${startPt.x} ${startPt.y}`;
1698
+ }
1699
+ if (prim.type === 0) {
1700
+ const endPt = transformPoint({ x: endV.x, y: endV.y }, matrix);
1701
+ d += ` L ${endPt.x} ${endPt.y}`;
1702
+ } else if (prim.type === 1) {
1703
+ const c0x = startV.c0x ?? startV.x;
1704
+ const c0y = startV.c0y ?? startV.y;
1705
+ const c1x = endV.c1x ?? endV.x;
1706
+ const c1y = endV.c1y ?? endV.y;
1707
+ const cp0 = transformPoint({ x: c0x, y: c0y }, matrix);
1708
+ const cp1 = transformPoint({ x: c1x, y: c1y }, matrix);
1709
+ const endPt = transformPoint({ x: endV.x, y: endV.y }, matrix);
1710
+ d += ` C ${cp0.x} ${cp0.y} ${cp1.x} ${cp1.y} ${endPt.x} ${endPt.y}`;
1711
+ }
1712
+ }
1713
+ if (d.length > 0 && path.isClosed) {
1714
+ d += " Z";
1715
+ }
1716
+ return d;
1717
+ }
1718
+ function rectToPathData(rect) {
1719
+ const matrix = rect.xform ? arrayToMatrix(rect.xform) : identity();
1720
+ const w = rect.w || 0;
1721
+ const h = rect.h || 0;
1722
+ const cr = rect.cr || 0;
1723
+ if (cr > 0) {
1724
+ const r = Math.min(cr, w / 2, h / 2);
1725
+ const p02 = transformPoint({ x: r, y: 0 }, matrix);
1726
+ const p12 = transformPoint({ x: w - r, y: 0 }, matrix);
1727
+ const p22 = transformPoint({ x: w, y: r }, matrix);
1728
+ const p32 = transformPoint({ x: w, y: h - r }, matrix);
1729
+ const p4 = transformPoint({ x: w - r, y: h }, matrix);
1730
+ const p5 = transformPoint({ x: r, y: h }, matrix);
1731
+ const p6 = transformPoint({ x: 0, y: h - r }, matrix);
1732
+ const p7 = transformPoint({ x: 0, y: r }, matrix);
1733
+ const k = 0.5522847498;
1734
+ const cp_tr1 = transformPoint({ x: w - r + r * k, y: 0 }, matrix);
1735
+ const cp_tr2 = transformPoint({ x: w, y: r - r * k }, matrix);
1736
+ const cp_br1 = transformPoint({ x: w, y: h - r + r * k }, matrix);
1737
+ const cp_br2 = transformPoint({ x: w - r + r * k, y: h }, matrix);
1738
+ const cp_bl1 = transformPoint({ x: r - r * k, y: h }, matrix);
1739
+ const cp_bl2 = transformPoint({ x: 0, y: h - r + r * k }, matrix);
1740
+ const cp_tl1 = transformPoint({ x: 0, y: r - r * k }, matrix);
1741
+ const cp_tl2 = transformPoint({ x: r - r * k, y: 0 }, matrix);
1742
+ return `M ${p02.x} ${p02.y} L ${p12.x} ${p12.y} C ${cp_tr1.x} ${cp_tr1.y} ${cp_tr2.x} ${cp_tr2.y} ${p22.x} ${p22.y} L ${p32.x} ${p32.y} C ${cp_br1.x} ${cp_br1.y} ${cp_br2.x} ${cp_br2.y} ${p4.x} ${p4.y} L ${p5.x} ${p5.y} C ${cp_bl1.x} ${cp_bl1.y} ${cp_bl2.x} ${cp_bl2.y} ${p6.x} ${p6.y} L ${p7.x} ${p7.y} C ${cp_tl1.x} ${cp_tl1.y} ${cp_tl2.x} ${cp_tl2.y} ${p02.x} ${p02.y} Z`;
1743
+ }
1744
+ const p0 = transformPoint({ x: 0, y: 0 }, matrix);
1745
+ const p1 = transformPoint({ x: w, y: 0 }, matrix);
1746
+ const p2 = transformPoint({ x: w, y: h }, matrix);
1747
+ const p3 = transformPoint({ x: 0, y: h }, matrix);
1748
+ return `M ${p0.x} ${p0.y} L ${p1.x} ${p1.y} L ${p2.x} ${p2.y} L ${p3.x} ${p3.y} Z`;
1749
+ }
1750
+ function ellipseToPathData(ellipse) {
1751
+ const matrix = ellipse.xform ? arrayToMatrix(ellipse.xform) : identity();
1752
+ const rx = ellipse.rx || 0;
1753
+ const ry = ellipse.ry || 0;
1754
+ const k = 0.5522847498;
1755
+ const p0 = transformPoint({ x: rx, y: 0 }, matrix);
1756
+ const p1 = transformPoint({ x: 0, y: ry }, matrix);
1757
+ const p2 = transformPoint({ x: -rx, y: 0 }, matrix);
1758
+ const p3 = transformPoint({ x: 0, y: -ry }, matrix);
1759
+ const cp0_1a = transformPoint({ x: rx, y: ry * k }, matrix);
1760
+ const cp0_1b = transformPoint({ x: rx * k, y: ry }, matrix);
1761
+ const cp1_2a = transformPoint({ x: -rx * k, y: ry }, matrix);
1762
+ const cp1_2b = transformPoint({ x: -rx, y: ry * k }, matrix);
1763
+ const cp2_3a = transformPoint({ x: -rx, y: -ry * k }, matrix);
1764
+ const cp2_3b = transformPoint({ x: -rx * k, y: -ry }, matrix);
1765
+ const cp3_0a = transformPoint({ x: rx * k, y: -ry }, matrix);
1766
+ const cp3_0b = transformPoint({ x: rx, y: -ry * k }, matrix);
1767
+ return `M ${p0.x} ${p0.y} C ${cp0_1a.x} ${cp0_1a.y} ${cp0_1b.x} ${cp0_1b.y} ${p1.x} ${p1.y} C ${cp1_2a.x} ${cp1_2a.y} ${cp1_2b.x} ${cp1_2b.y} ${p2.x} ${p2.y} C ${cp2_3a.x} ${cp2_3a.y} ${cp2_3b.x} ${cp2_3b.y} ${p3.x} ${p3.y} C ${cp3_0a.x} ${cp3_0a.y} ${cp3_0b.x} ${cp3_0b.y} ${p0.x} ${p0.y} Z`;
1768
+ }
1769
+
1681
1770
  // lib/svg-gen/registry/shape-group.ts
1771
+ function isClosedPathShape(shape) {
1772
+ if (shape instanceof ShapePath) {
1773
+ return shape.isClosed;
1774
+ }
1775
+ if (shape instanceof ShapeRect || shape instanceof ShapeEllipse) {
1776
+ return true;
1777
+ }
1778
+ return false;
1779
+ }
1780
+ function getPathDataForShape(shape, groupMatrix) {
1781
+ if (shape instanceof ShapePath && shape.isClosed) {
1782
+ const originalXform = shape.xform;
1783
+ if (originalXform) {
1784
+ const shapeMatrix = arrayToMatrix(originalXform);
1785
+ const combinedMatrix = mul(groupMatrix, shapeMatrix);
1786
+ shape.xform = [
1787
+ combinedMatrix.a,
1788
+ combinedMatrix.b,
1789
+ combinedMatrix.c,
1790
+ combinedMatrix.d,
1791
+ combinedMatrix.e,
1792
+ combinedMatrix.f
1793
+ ];
1794
+ const pathData2 = shapePathToPathData(shape);
1795
+ shape.xform = originalXform;
1796
+ return pathData2;
1797
+ }
1798
+ shape.xform = [
1799
+ groupMatrix.a,
1800
+ groupMatrix.b,
1801
+ groupMatrix.c,
1802
+ groupMatrix.d,
1803
+ groupMatrix.e,
1804
+ groupMatrix.f
1805
+ ];
1806
+ const pathData = shapePathToPathData(shape);
1807
+ shape.xform = originalXform;
1808
+ return pathData;
1809
+ }
1810
+ if (shape instanceof ShapeRect) {
1811
+ const originalXform = shape.xform;
1812
+ if (originalXform) {
1813
+ const shapeMatrix = arrayToMatrix(originalXform);
1814
+ const combinedMatrix = mul(groupMatrix, shapeMatrix);
1815
+ shape.xform = [
1816
+ combinedMatrix.a,
1817
+ combinedMatrix.b,
1818
+ combinedMatrix.c,
1819
+ combinedMatrix.d,
1820
+ combinedMatrix.e,
1821
+ combinedMatrix.f
1822
+ ];
1823
+ const pathData2 = rectToPathData(shape);
1824
+ shape.xform = originalXform;
1825
+ return pathData2;
1826
+ }
1827
+ shape.xform = [
1828
+ groupMatrix.a,
1829
+ groupMatrix.b,
1830
+ groupMatrix.c,
1831
+ groupMatrix.d,
1832
+ groupMatrix.e,
1833
+ groupMatrix.f
1834
+ ];
1835
+ const pathData = rectToPathData(shape);
1836
+ shape.xform = originalXform;
1837
+ return pathData;
1838
+ }
1839
+ if (shape instanceof ShapeEllipse) {
1840
+ const originalXform = shape.xform;
1841
+ if (originalXform) {
1842
+ const shapeMatrix = arrayToMatrix(originalXform);
1843
+ const combinedMatrix = mul(groupMatrix, shapeMatrix);
1844
+ shape.xform = [
1845
+ combinedMatrix.a,
1846
+ combinedMatrix.b,
1847
+ combinedMatrix.c,
1848
+ combinedMatrix.d,
1849
+ combinedMatrix.e,
1850
+ combinedMatrix.f
1851
+ ];
1852
+ const pathData2 = ellipseToPathData(shape);
1853
+ shape.xform = originalXform;
1854
+ return pathData2;
1855
+ }
1856
+ shape.xform = [
1857
+ groupMatrix.a,
1858
+ groupMatrix.b,
1859
+ groupMatrix.c,
1860
+ groupMatrix.d,
1861
+ groupMatrix.e,
1862
+ groupMatrix.f
1863
+ ];
1864
+ const pathData = ellipseToPathData(shape);
1865
+ shape.xform = originalXform;
1866
+ return pathData;
1867
+ }
1868
+ return null;
1869
+ }
1682
1870
  var groupRenderer = {
1683
1871
  match: (s) => s instanceof ShapeGroup,
1684
1872
  bbox: (grp) => {
1685
1873
  return grp.children.filter((c) => c instanceof ShapeBase).reduce((bb, c) => boxUnion(bb, bboxOfShape(c)), emptyBox());
1686
1874
  },
1687
1875
  toSvg: (grp, cutSettings, options) => {
1688
- const xform = grp.xform ? arrayToMatrix(grp.xform) : identity();
1689
- const transform = matToSvg(xform);
1690
- const children = grp.children.filter((c) => c instanceof ShapeBase).map((c) => svgForShape(c, cutSettings, options));
1876
+ const groupMatrix = grp.xform ? arrayToMatrix(grp.xform) : identity();
1877
+ const transform = matToSvg(groupMatrix);
1878
+ const shapeChildren = grp.children.filter(
1879
+ (c) => c instanceof ShapeBase
1880
+ );
1881
+ const allClosedPaths = shapeChildren.every((c) => isClosedPathShape(c));
1882
+ if (allClosedPaths && shapeChildren.length > 0) {
1883
+ const pathDataParts = [];
1884
+ for (const child of shapeChildren) {
1885
+ const pathData = getPathDataForShape(child, groupMatrix);
1886
+ if (pathData) {
1887
+ pathDataParts.push(pathData);
1888
+ }
1889
+ }
1890
+ if (pathDataParts.length > 0) {
1891
+ const combinedPathData = pathDataParts.join(" ");
1892
+ const children2 = [];
1893
+ const firstChild = shapeChildren[0];
1894
+ const cutIndex = firstChild.cutIndex;
1895
+ const stroke = colorForCutIndex(cutIndex);
1896
+ const cutSetting = cutIndex !== void 0 ? cutSettings.get(cutIndex) : void 0;
1897
+ const shouldShowFill = cutSetting && (cutSetting.type === "Scan" || cutSetting.type === "Scan+Cut");
1898
+ if (shouldShowFill && cutSetting) {
1899
+ const bbox = shapeChildren.reduce(
1900
+ (bb, c) => boxUnion(bb, bboxOfShape(c)),
1901
+ emptyBox()
1902
+ );
1903
+ const fillSettings = {
1904
+ interval: cutSetting.interval || 0.1,
1905
+ angle: cutSetting.angle || 0,
1906
+ crossHatch: cutSetting.crossHatch || false
1907
+ };
1908
+ const fillLines = generateScanLines(
1909
+ bbox,
1910
+ fillSettings,
1911
+ stroke,
1912
+ options.strokeWidth
1913
+ );
1914
+ const clipId = `clip-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
1915
+ const clipPath = {
1916
+ name: "clipPath",
1917
+ type: "element",
1918
+ value: "",
1919
+ attributes: { id: clipId },
1920
+ children: [
1921
+ leaf("path", {
1922
+ d: combinedPathData,
1923
+ "fill-rule": "nonzero",
1924
+ "clip-rule": "nonzero"
1925
+ })
1926
+ ]
1927
+ };
1928
+ const clippedGroup = {
1929
+ name: "g",
1930
+ type: "element",
1931
+ value: "",
1932
+ attributes: { "clip-path": `url(#${clipId})` },
1933
+ children: fillLines
1934
+ };
1935
+ children2.push(clipPath);
1936
+ children2.push(clippedGroup);
1937
+ }
1938
+ children2.push(
1939
+ leaf("path", {
1940
+ d: combinedPathData,
1941
+ fill: "none",
1942
+ "fill-rule": "nonzero",
1943
+ stroke,
1944
+ "stroke-width": String(options.strokeWidth)
1945
+ })
1946
+ );
1947
+ return g({}, children2);
1948
+ }
1949
+ }
1950
+ const children = shapeChildren.map(
1951
+ (c) => svgForShape(c, cutSettings, options)
1952
+ );
1691
1953
  return g({ transform }, children);
1692
1954
  }
1693
1955
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "lbrnts",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.12",
5
+ "version": "0.0.13",
6
6
  "files": [
7
7
  "dist"
8
8
  ],