circuit-to-svg 0.0.133 → 0.0.135

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/README.md CHANGED
@@ -50,6 +50,12 @@ Converts a schematic circuit description to an SVG string.
50
50
 
51
51
  Converts a PCB layout description to an SVG string.
52
52
 
53
+ #### Options
54
+
55
+ - `width` and `height` – dimensions of the output SVG. Defaults to `800x600`.
56
+ - `matchBoardAspectRatio` – if `true`, adjust the SVG dimensions so the
57
+ resulting aspect ratio matches the `pcb_board` found in the circuit JSON.
58
+
53
59
  ## Contributing
54
60
 
55
61
  Contributions are welcome! Please feel free to submit a Pull Request.
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ interface Options$3 {
7
7
  shouldDrawErrors?: boolean;
8
8
  shouldDrawRatsNest?: boolean;
9
9
  layer?: "top" | "bottom";
10
+ matchBoardAspectRatio?: boolean;
10
11
  }
11
12
  interface PcbContext {
12
13
  transform: Matrix;
package/dist/index.js CHANGED
@@ -1323,16 +1323,27 @@ function convertCircuitJsonToPcbSvg(circuitJson, options) {
1323
1323
  let minY = Number.POSITIVE_INFINITY;
1324
1324
  let maxX = Number.NEGATIVE_INFINITY;
1325
1325
  let maxY = Number.NEGATIVE_INFINITY;
1326
+ let boardMinX = Number.POSITIVE_INFINITY;
1327
+ let boardMinY = Number.POSITIVE_INFINITY;
1328
+ let boardMaxX = Number.NEGATIVE_INFINITY;
1329
+ let boardMaxY = Number.NEGATIVE_INFINITY;
1326
1330
  for (const circuitJsonElm of circuitJson) {
1327
1331
  if (circuitJsonElm.type === "pcb_board") {
1328
- if (circuitJsonElm.outline && Array.isArray(circuitJsonElm.outline) && circuitJsonElm.outline.length >= 3)
1332
+ if (circuitJsonElm.outline && Array.isArray(circuitJsonElm.outline) && circuitJsonElm.outline.length >= 3) {
1329
1333
  updateBoundsToIncludeOutline(circuitJsonElm.outline);
1330
- else if ("center" in circuitJsonElm && "width" in circuitJsonElm && "height" in circuitJsonElm)
1334
+ updateBoardBoundsToIncludeOutline(circuitJsonElm.outline);
1335
+ } else if ("center" in circuitJsonElm && "width" in circuitJsonElm && "height" in circuitJsonElm) {
1331
1336
  updateBounds(
1332
1337
  circuitJsonElm.center,
1333
1338
  circuitJsonElm.width,
1334
1339
  circuitJsonElm.height
1335
1340
  );
1341
+ updateBoardBounds(
1342
+ circuitJsonElm.center,
1343
+ circuitJsonElm.width,
1344
+ circuitJsonElm.height
1345
+ );
1346
+ }
1336
1347
  } else if ("x" in circuitJsonElm && "y" in circuitJsonElm) {
1337
1348
  updateBounds({ x: circuitJsonElm.x, y: circuitJsonElm.y }, 0, 0);
1338
1349
  } else if ("route" in circuitJsonElm) {
@@ -1344,8 +1355,22 @@ function convertCircuitJsonToPcbSvg(circuitJson, options) {
1344
1355
  const padding = 1;
1345
1356
  const circuitWidth = maxX - minX + 2 * padding;
1346
1357
  const circuitHeight = maxY - minY + 2 * padding;
1347
- const svgWidth = options?.width ?? 800;
1348
- const svgHeight = options?.height ?? 600;
1358
+ let svgWidth = options?.width ?? 800;
1359
+ let svgHeight = options?.height ?? 600;
1360
+ if (options?.matchBoardAspectRatio) {
1361
+ const boardWidth = boardMaxX - boardMinX;
1362
+ const boardHeight = boardMaxY - boardMinY;
1363
+ if (boardWidth > 0 && boardHeight > 0) {
1364
+ const aspect = boardWidth / boardHeight;
1365
+ if (options?.width && !options?.height) {
1366
+ svgHeight = options.width / aspect;
1367
+ } else if (options?.height && !options?.width) {
1368
+ svgWidth = options.height * aspect;
1369
+ } else {
1370
+ svgHeight = svgWidth / aspect;
1371
+ }
1372
+ }
1373
+ }
1349
1374
  const paths = [];
1350
1375
  for (const circuitJsonElm of circuitJson) {
1351
1376
  if ("route" in circuitJsonElm && circuitJsonElm.route !== void 0) {
@@ -1434,6 +1459,14 @@ function convertCircuitJsonToPcbSvg(circuitJson, options) {
1434
1459
  maxX = Math.max(maxX, center.x + halfWidth);
1435
1460
  maxY = Math.max(maxY, center.y + halfHeight);
1436
1461
  }
1462
+ function updateBoardBounds(center, width, height) {
1463
+ const halfWidth = width / 2;
1464
+ const halfHeight = height / 2;
1465
+ boardMinX = Math.min(boardMinX, center.x - halfWidth);
1466
+ boardMinY = Math.min(boardMinY, center.y - halfHeight);
1467
+ boardMaxX = Math.max(boardMaxX, center.x + halfWidth);
1468
+ boardMaxY = Math.max(boardMaxY, center.y + halfHeight);
1469
+ }
1437
1470
  function updateBoundsToIncludeOutline(outline) {
1438
1471
  for (const point of outline) {
1439
1472
  minX = Math.min(minX, point.x);
@@ -1442,6 +1475,14 @@ function convertCircuitJsonToPcbSvg(circuitJson, options) {
1442
1475
  maxY = Math.max(maxY, point.y);
1443
1476
  }
1444
1477
  }
1478
+ function updateBoardBoundsToIncludeOutline(outline) {
1479
+ for (const point of outline) {
1480
+ boardMinX = Math.min(boardMinX, point.x);
1481
+ boardMinY = Math.min(boardMinY, point.y);
1482
+ boardMaxX = Math.max(boardMaxX, point.x);
1483
+ boardMaxY = Math.max(boardMaxY, point.y);
1484
+ }
1485
+ }
1445
1486
  function updateTraceBounds(route) {
1446
1487
  for (const point of route) {
1447
1488
  minX = Math.min(minX, point.x);