circuit-to-canvas 0.0.7 → 0.0.9

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 CHANGED
@@ -1,4 +1,4 @@
1
- import { AnyCircuitElement, PcbPlatedHole, PCBVia, PCBHole, PcbSmtPad, PCBTrace, PcbBoard, PcbSilkscreenText, PcbSilkscreenRect, PcbSilkscreenCircle, PcbSilkscreenLine, PcbSilkscreenPath, PcbCutout, PcbCopperPour, PcbCopperText, PcbFabricationNoteText, PcbFabricationNoteRect, PcbNoteRect, PcbFabricationNotePath } from 'circuit-json';
1
+ import { AnyCircuitElement, PcbPlatedHole, PCBVia, PCBHole, PcbSmtPad, PCBTrace, PcbBoard, PcbSilkscreenText, PcbSilkscreenRect, PcbSilkscreenCircle, PcbSilkscreenLine, PcbSilkscreenPath, PcbCutout, PcbCopperPour, PcbCopperText, PcbFabricationNoteText, PcbFabricationNoteRect, PcbNoteRect, PcbFabricationNotePath, PcbNotePath, PcbNoteText } from 'circuit-json';
2
2
  import { Matrix } from 'transformation-matrix';
3
3
 
4
4
  /**
@@ -370,4 +370,20 @@ interface DrawPcbFabricationNotePathParams {
370
370
  }
371
371
  declare function drawPcbFabricationNotePath(params: DrawPcbFabricationNotePathParams): void;
372
372
 
373
- export { type AlphabetLayout, type AnchorAlignment, type CameraBounds, type CanvasContext, CircuitToCanvasDrawer, type CopperColorMap, type CopperLayerName, DEFAULT_PCB_COLOR_MAP, type DrawCircleParams, type DrawContext, type DrawElementsOptions, type DrawLineParams, type DrawOvalParams, type DrawPathParams, type DrawPcbBoardParams, type DrawPcbCopperPourParams, type DrawPcbCopperTextParams, type DrawPcbCutoutParams, type DrawPcbFabricationNotePathParams, type DrawPcbFabricationNoteRectParams, type DrawPcbFabricationNoteTextParams, type DrawPcbHoleParams, type DrawPcbNoteRectParams, type DrawPcbPlatedHoleParams, type DrawPcbSilkscreenCircleParams, type DrawPcbSilkscreenLineParams, type DrawPcbSilkscreenPathParams, type DrawPcbSilkscreenRectParams, type DrawPcbSilkscreenTextParams, type DrawPcbSmtPadParams, type DrawPcbTraceParams, type DrawPcbViaParams, type DrawPillParams, type DrawPolygonParams, type DrawRectParams, type DrawTextParams, type DrawerConfig, type PcbColorMap, drawCircle, drawLine, drawOval, drawPath, drawPcbBoard, drawPcbCopperPour, drawPcbCopperText, drawPcbCutout, drawPcbFabricationNotePath, drawPcbFabricationNoteRect, drawPcbFabricationNoteText, drawPcbHole, drawPcbNoteRect, drawPcbPlatedHole, drawPcbSilkscreenCircle, drawPcbSilkscreenLine, drawPcbSilkscreenPath, drawPcbSilkscreenRect, drawPcbSilkscreenText, drawPcbSmtPad, drawPcbTrace, drawPcbVia, drawPill, drawPolygon, drawRect, drawText, getAlphabetLayout, getTextStartPosition, strokeAlphabetText };
373
+ interface DrawPcbNotePathParams {
374
+ ctx: CanvasContext;
375
+ path: PcbNotePath;
376
+ transform: Matrix;
377
+ colorMap: PcbColorMap;
378
+ }
379
+ declare function drawPcbNotePath(params: DrawPcbNotePathParams): void;
380
+
381
+ interface DrawPcbNoteTextParams {
382
+ ctx: CanvasContext;
383
+ text: PcbNoteText;
384
+ transform: Matrix;
385
+ colorMap: PcbColorMap;
386
+ }
387
+ declare function drawPcbNoteText(params: DrawPcbNoteTextParams): void;
388
+
389
+ export { type AlphabetLayout, type AnchorAlignment, type CameraBounds, type CanvasContext, CircuitToCanvasDrawer, type CopperColorMap, type CopperLayerName, DEFAULT_PCB_COLOR_MAP, type DrawCircleParams, type DrawContext, type DrawElementsOptions, type DrawLineParams, type DrawOvalParams, type DrawPathParams, type DrawPcbBoardParams, type DrawPcbCopperPourParams, type DrawPcbCopperTextParams, type DrawPcbCutoutParams, type DrawPcbFabricationNotePathParams, type DrawPcbFabricationNoteRectParams, type DrawPcbFabricationNoteTextParams, type DrawPcbHoleParams, type DrawPcbNotePathParams, type DrawPcbNoteRectParams, type DrawPcbNoteTextParams, type DrawPcbPlatedHoleParams, type DrawPcbSilkscreenCircleParams, type DrawPcbSilkscreenLineParams, type DrawPcbSilkscreenPathParams, type DrawPcbSilkscreenRectParams, type DrawPcbSilkscreenTextParams, type DrawPcbSmtPadParams, type DrawPcbTraceParams, type DrawPcbViaParams, type DrawPillParams, type DrawPolygonParams, type DrawRectParams, type DrawTextParams, type DrawerConfig, type PcbColorMap, drawCircle, drawLine, drawOval, drawPath, drawPcbBoard, drawPcbCopperPour, drawPcbCopperText, drawPcbCutout, drawPcbFabricationNotePath, drawPcbFabricationNoteRect, drawPcbFabricationNoteText, drawPcbHole, drawPcbNotePath, drawPcbNoteRect, drawPcbNoteText, drawPcbPlatedHole, drawPcbSilkscreenCircle, drawPcbSilkscreenLine, drawPcbSilkscreenPath, drawPcbSilkscreenRect, drawPcbSilkscreenText, drawPcbSmtPad, drawPcbTrace, drawPcbVia, drawPill, drawPolygon, drawRect, drawText, getAlphabetLayout, getTextStartPosition, strokeAlphabetText };
package/dist/index.js CHANGED
@@ -1096,6 +1096,46 @@ function drawPcbFabricationNotePath(params) {
1096
1096
  }
1097
1097
  }
1098
1098
 
1099
+ // lib/drawer/elements/pcb-note-path.ts
1100
+ function drawPcbNotePath(params) {
1101
+ const { ctx, path, transform, colorMap } = params;
1102
+ const defaultColor = "rgb(89, 148, 220)";
1103
+ const color = path.color ?? defaultColor;
1104
+ if (!path.route || path.route.length < 2) return;
1105
+ for (let i = 0; i < path.route.length - 1; i++) {
1106
+ const start = path.route[i];
1107
+ const end = path.route[i + 1];
1108
+ if (!start || !end) continue;
1109
+ drawLine({
1110
+ ctx,
1111
+ start: { x: start.x, y: start.y },
1112
+ end: { x: end.x, y: end.y },
1113
+ strokeWidth: path.stroke_width ?? 0.1,
1114
+ stroke: color,
1115
+ transform
1116
+ });
1117
+ }
1118
+ }
1119
+
1120
+ // lib/drawer/elements/pcb-note-text.ts
1121
+ var DEFAULT_NOTE_TEXT_COLOR = "rgb(89, 148, 220)";
1122
+ function drawPcbNoteText(params) {
1123
+ const { ctx, text, transform, colorMap } = params;
1124
+ const defaultColor = DEFAULT_NOTE_TEXT_COLOR;
1125
+ const color = text.color ?? defaultColor;
1126
+ const fontSize = text.font_size ?? 1;
1127
+ drawText({
1128
+ ctx,
1129
+ text: text.text ?? "",
1130
+ x: text.anchor_position.x,
1131
+ y: text.anchor_position.y,
1132
+ fontSize,
1133
+ color,
1134
+ transform,
1135
+ anchorAlignment: text.anchor_alignment ?? "center"
1136
+ });
1137
+ }
1138
+
1099
1139
  // lib/drawer/CircuitToCanvasDrawer.ts
1100
1140
  var CircuitToCanvasDrawer = class {
1101
1141
  ctx;
@@ -1309,6 +1349,22 @@ var CircuitToCanvasDrawer = class {
1309
1349
  colorMap: this.colorMap
1310
1350
  });
1311
1351
  }
1352
+ if (element.type === "pcb_note_path") {
1353
+ drawPcbNotePath({
1354
+ ctx: this.ctx,
1355
+ path: element,
1356
+ transform: this.realToCanvasMat,
1357
+ colorMap: this.colorMap
1358
+ });
1359
+ }
1360
+ if (element.type === "pcb_note_text") {
1361
+ drawPcbNoteText({
1362
+ ctx: this.ctx,
1363
+ text: element,
1364
+ transform: this.realToCanvasMat,
1365
+ colorMap: this.colorMap
1366
+ });
1367
+ }
1312
1368
  }
1313
1369
  };
1314
1370
  export {
@@ -1326,7 +1382,9 @@ export {
1326
1382
  drawPcbFabricationNoteRect,
1327
1383
  drawPcbFabricationNoteText,
1328
1384
  drawPcbHole,
1385
+ drawPcbNotePath,
1329
1386
  drawPcbNoteRect,
1387
+ drawPcbNoteText,
1330
1388
  drawPcbPlatedHole,
1331
1389
  drawPcbSilkscreenCircle,
1332
1390
  drawPcbSilkscreenLine,
@@ -18,6 +18,8 @@ import type {
18
18
  PcbFabricationNoteRect,
19
19
  PcbNoteRect,
20
20
  PcbFabricationNotePath,
21
+ PcbNotePath,
22
+ PcbNoteText,
21
23
  } from "circuit-json"
22
24
  import { identity, compose, translate, scale } from "transformation-matrix"
23
25
  import type { Matrix } from "transformation-matrix"
@@ -48,6 +50,8 @@ import { drawPcbFabricationNoteText } from "./elements/pcb-fabrication-note-text
48
50
  import { drawPcbFabricationNoteRect } from "./elements/pcb-fabrication-note-rect"
49
51
  import { drawPcbNoteRect } from "./elements/pcb-note-rect"
50
52
  import { drawPcbFabricationNotePath } from "./elements/pcb-fabrication-note-path"
53
+ import { drawPcbNotePath } from "./elements/pcb-note-path"
54
+ import { drawPcbNoteText } from "./elements/pcb-note-text"
51
55
 
52
56
  export interface DrawElementsOptions {
53
57
  layers?: string[]
@@ -307,5 +311,23 @@ export class CircuitToCanvasDrawer {
307
311
  colorMap: this.colorMap,
308
312
  })
309
313
  }
314
+
315
+ if (element.type === "pcb_note_path") {
316
+ drawPcbNotePath({
317
+ ctx: this.ctx,
318
+ path: element as PcbNotePath,
319
+ transform: this.realToCanvasMat,
320
+ colorMap: this.colorMap,
321
+ })
322
+ }
323
+
324
+ if (element.type === "pcb_note_text") {
325
+ drawPcbNoteText({
326
+ ctx: this.ctx,
327
+ text: element as PcbNoteText,
328
+ transform: this.realToCanvasMat,
329
+ colorMap: this.colorMap,
330
+ })
331
+ }
310
332
  }
311
333
  }
@@ -57,3 +57,13 @@ export {
57
57
  drawPcbFabricationNotePath,
58
58
  type DrawPcbFabricationNotePathParams,
59
59
  } from "./pcb-fabrication-note-path"
60
+
61
+ export {
62
+ drawPcbNotePath,
63
+ type DrawPcbNotePathParams,
64
+ } from "./pcb-note-path"
65
+
66
+ export {
67
+ drawPcbNoteText,
68
+ type DrawPcbNoteTextParams,
69
+ } from "./pcb-note-text"
@@ -0,0 +1,39 @@
1
+ import type { PcbNotePath } from "circuit-json"
2
+ import type { Matrix } from "transformation-matrix"
3
+ import type { PcbColorMap, CanvasContext } from "../types"
4
+ import { drawLine } from "../shapes/line"
5
+
6
+ export interface DrawPcbNotePathParams {
7
+ ctx: CanvasContext
8
+ path: PcbNotePath
9
+ transform: Matrix
10
+ colorMap: PcbColorMap
11
+ }
12
+
13
+ export function drawPcbNotePath(params: DrawPcbNotePathParams): void {
14
+ const { ctx, path, transform, colorMap } = params
15
+
16
+ // Use the color from the path if provided, otherwise use a default color
17
+ // Notes are typically shown in a distinct color
18
+ const defaultColor = "rgb(89, 148, 220)" // Blue color for notes
19
+ const color = path.color ?? defaultColor
20
+
21
+ if (!path.route || path.route.length < 2) return
22
+
23
+ // Draw each segment of the path
24
+ for (let i = 0; i < path.route.length - 1; i++) {
25
+ const start = path.route[i]
26
+ const end = path.route[i + 1]
27
+
28
+ if (!start || !end) continue
29
+
30
+ drawLine({
31
+ ctx,
32
+ start: { x: start.x, y: start.y },
33
+ end: { x: end.x, y: end.y },
34
+ strokeWidth: path.stroke_width ?? 0.1,
35
+ stroke: color,
36
+ transform,
37
+ })
38
+ }
39
+ }
@@ -0,0 +1,34 @@
1
+ import type { PcbNoteText } from "circuit-json"
2
+ import type { Matrix } from "transformation-matrix"
3
+ import type { PcbColorMap, CanvasContext } from "../types"
4
+ import { drawText } from "../shapes/text"
5
+
6
+ export interface DrawPcbNoteTextParams {
7
+ ctx: CanvasContext
8
+ text: PcbNoteText
9
+ transform: Matrix
10
+ colorMap: PcbColorMap
11
+ }
12
+
13
+ const DEFAULT_NOTE_TEXT_COLOR = "rgb(89, 148, 220)" // Same color as note rect
14
+
15
+ export function drawPcbNoteText(params: DrawPcbNoteTextParams): void {
16
+ const { ctx, text, transform, colorMap } = params
17
+
18
+ const defaultColor = DEFAULT_NOTE_TEXT_COLOR
19
+ const color = text.color ?? defaultColor
20
+ const fontSize = text.font_size ?? 1 // Default to 1mm if not provided
21
+
22
+ // Use @tscircuit/alphabet to draw text
23
+ // Pass real-world coordinates and let drawText apply the transform
24
+ drawText({
25
+ ctx,
26
+ text: text.text ?? "",
27
+ x: text.anchor_position.x,
28
+ y: text.anchor_position.y,
29
+ fontSize,
30
+ color,
31
+ transform,
32
+ anchorAlignment: text.anchor_alignment ?? "center",
33
+ })
34
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "circuit-to-canvas",
3
3
  "main": "dist/index.js",
4
- "version": "0.0.7",
4
+ "version": "0.0.9",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "tsup-node ./lib/index.ts --format esm --dts",
@@ -0,0 +1,34 @@
1
+ import { expect, test } from "bun:test"
2
+ import { createCanvas } from "canvas"
3
+ import type { PcbNotePath } from "circuit-json"
4
+ import { CircuitToCanvasDrawer } from "../../lib/drawer"
5
+
6
+ test("draw pcb note path", async () => {
7
+ const canvas = createCanvas(100, 100)
8
+ const ctx = canvas.getContext("2d")
9
+ const drawer = new CircuitToCanvasDrawer(ctx)
10
+
11
+ ctx.fillStyle = "#1a1a1a"
12
+ ctx.fillRect(0, 0, 100, 100)
13
+
14
+ const path: PcbNotePath = {
15
+ type: "pcb_note_path",
16
+ pcb_note_path_id: "note_path1",
17
+ route: [
18
+ { x: 10, y: 50 },
19
+ { x: 30, y: 20 },
20
+ { x: 70, y: 20 },
21
+ { x: 90, y: 50 },
22
+ { x: 70, y: 80 },
23
+ { x: 30, y: 80 },
24
+ { x: 10, y: 50 },
25
+ ],
26
+ stroke_width: 2,
27
+ }
28
+
29
+ drawer.drawElements([path])
30
+
31
+ await expect(canvas.toBuffer("image/png")).toMatchPngSnapshot(
32
+ import.meta.path,
33
+ )
34
+ })
@@ -0,0 +1,73 @@
1
+ import { expect, test } from "bun:test"
2
+ import { createCanvas } from "canvas"
3
+ import type { PcbNoteText } from "circuit-json"
4
+ import { CircuitToCanvasDrawer } from "../../lib/drawer"
5
+
6
+ test("draw pcb note text with different anchor alignments", async () => {
7
+ const SCALE = 4
8
+ const canvas = createCanvas(300 * SCALE, 200 * SCALE)
9
+ const ctx = canvas.getContext("2d")
10
+ ctx.scale(SCALE, SCALE)
11
+ const drawer = new CircuitToCanvasDrawer(ctx)
12
+
13
+ ctx.fillStyle = "#1a1a1a"
14
+ ctx.fillRect(0, 0, canvas.width / SCALE, canvas.height / SCALE)
15
+
16
+ // Draw reference lines
17
+ ctx.strokeStyle = "#444444"
18
+ ctx.lineWidth = 0.5
19
+ // Top line
20
+ ctx.beginPath()
21
+ ctx.moveTo(10, 50)
22
+ ctx.lineTo(290, 50)
23
+ ctx.stroke()
24
+ // Center/baseline line
25
+ ctx.beginPath()
26
+ ctx.moveTo(10, 100)
27
+ ctx.lineTo(290, 100)
28
+ ctx.stroke()
29
+ // Bottom line
30
+ ctx.beginPath()
31
+ ctx.moveTo(10, 150)
32
+ ctx.lineTo(290, 150)
33
+ ctx.stroke()
34
+
35
+ // Test with top_left alignment
36
+ const textTopLeft: PcbNoteText = {
37
+ type: "pcb_note_text",
38
+ pcb_note_text_id: "note-top-left",
39
+ text: "TOP",
40
+ anchor_position: { x: 50, y: 50 },
41
+ anchor_alignment: "top_left",
42
+ font: "tscircuit2024",
43
+ font_size: 16,
44
+ }
45
+
46
+ // Test with center alignment
47
+ const textCenter: PcbNoteText = {
48
+ type: "pcb_note_text",
49
+ pcb_note_text_id: "note-center",
50
+ text: "CENTER",
51
+ anchor_position: { x: 150, y: 100 },
52
+ anchor_alignment: "center",
53
+ font: "tscircuit2024",
54
+ font_size: 16,
55
+ }
56
+
57
+ // Test with bottom_right alignment
58
+ const textBottomRight: PcbNoteText = {
59
+ type: "pcb_note_text",
60
+ pcb_note_text_id: "note-bottom-right",
61
+ text: "BOTTOM",
62
+ anchor_position: { x: 250, y: 150 },
63
+ anchor_alignment: "bottom_right",
64
+ font: "tscircuit2024",
65
+ font_size: 16,
66
+ }
67
+
68
+ drawer.drawElements([textTopLeft, textCenter, textBottomRight])
69
+
70
+ await expect(canvas.toBuffer("image/png")).toMatchPngSnapshot(
71
+ import.meta.path,
72
+ )
73
+ })
@@ -0,0 +1,32 @@
1
+ import { expect, test } from "bun:test"
2
+ import { createCanvas } from "canvas"
3
+ import type { PcbNoteText } from "circuit-json"
4
+ import { CircuitToCanvasDrawer } from "../../lib/drawer"
5
+
6
+ test("draw pcb note text with custom color", async () => {
7
+ const SCALE = 4
8
+ const canvas = createCanvas(100 * SCALE, 100 * SCALE)
9
+ const ctx = canvas.getContext("2d")
10
+ ctx.scale(SCALE, SCALE)
11
+ const drawer = new CircuitToCanvasDrawer(ctx)
12
+
13
+ ctx.fillStyle = "#1a1a1a"
14
+ ctx.fillRect(0, 0, canvas.width / SCALE, canvas.height / SCALE)
15
+
16
+ const text: PcbNoteText = {
17
+ type: "pcb_note_text",
18
+ pcb_note_text_id: "note-custom-color",
19
+ text: "CustYp",
20
+ anchor_position: { x: 50, y: 50 },
21
+ anchor_alignment: "center",
22
+ font: "tscircuit2024",
23
+ font_size: 8,
24
+ color: "rgba(255, 0, 255, 0.8)",
25
+ }
26
+
27
+ drawer.drawElements([text])
28
+
29
+ await expect(canvas.toBuffer("image/png")).toMatchPngSnapshot(
30
+ import.meta.path,
31
+ )
32
+ })
@@ -0,0 +1,31 @@
1
+ import { expect, test } from "bun:test"
2
+ import { createCanvas } from "canvas"
3
+ import type { PcbNoteText } from "circuit-json"
4
+ import { CircuitToCanvasDrawer } from "../../lib/drawer"
5
+
6
+ test("draw pcb note text with small font size", async () => {
7
+ const SCALE = 4
8
+ const canvas = createCanvas(100 * SCALE, 100 * SCALE)
9
+ const ctx = canvas.getContext("2d")
10
+ ctx.scale(SCALE, SCALE)
11
+ const drawer = new CircuitToCanvasDrawer(ctx)
12
+
13
+ ctx.fillStyle = "#1a1a1a"
14
+ ctx.fillRect(0, 0, canvas.width / SCALE, canvas.height / SCALE)
15
+
16
+ const text: PcbNoteText = {
17
+ type: "pcb_note_text",
18
+ pcb_note_text_id: "note-small",
19
+ text: "SMALL",
20
+ anchor_position: { x: 50, y: 50 },
21
+ anchor_alignment: "center",
22
+ font: "tscircuit2024",
23
+ font_size: 2,
24
+ }
25
+
26
+ drawer.drawElements([text])
27
+
28
+ await expect(canvas.toBuffer("image/png")).toMatchPngSnapshot(
29
+ import.meta.path,
30
+ )
31
+ })