circuit-to-canvas 0.0.7 → 0.0.8

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, PcbNoteText } from 'circuit-json';
2
2
  import { Matrix } from 'transformation-matrix';
3
3
 
4
4
  /**
@@ -370,4 +370,12 @@ 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 DrawPcbNoteTextParams {
374
+ ctx: CanvasContext;
375
+ text: PcbNoteText;
376
+ transform: Matrix;
377
+ colorMap: PcbColorMap;
378
+ }
379
+ declare function drawPcbNoteText(params: DrawPcbNoteTextParams): void;
380
+
381
+ 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 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, 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,25 @@ function drawPcbFabricationNotePath(params) {
1096
1096
  }
1097
1097
  }
1098
1098
 
1099
+ // lib/drawer/elements/pcb-note-text.ts
1100
+ var DEFAULT_NOTE_TEXT_COLOR = "rgb(89, 148, 220)";
1101
+ function drawPcbNoteText(params) {
1102
+ const { ctx, text, transform, colorMap } = params;
1103
+ const defaultColor = DEFAULT_NOTE_TEXT_COLOR;
1104
+ const color = text.color ?? defaultColor;
1105
+ const fontSize = text.font_size ?? 1;
1106
+ drawText({
1107
+ ctx,
1108
+ text: text.text ?? "",
1109
+ x: text.anchor_position.x,
1110
+ y: text.anchor_position.y,
1111
+ fontSize,
1112
+ color,
1113
+ transform,
1114
+ anchorAlignment: text.anchor_alignment ?? "center"
1115
+ });
1116
+ }
1117
+
1099
1118
  // lib/drawer/CircuitToCanvasDrawer.ts
1100
1119
  var CircuitToCanvasDrawer = class {
1101
1120
  ctx;
@@ -1309,6 +1328,14 @@ var CircuitToCanvasDrawer = class {
1309
1328
  colorMap: this.colorMap
1310
1329
  });
1311
1330
  }
1331
+ if (element.type === "pcb_note_text") {
1332
+ drawPcbNoteText({
1333
+ ctx: this.ctx,
1334
+ text: element,
1335
+ transform: this.realToCanvasMat,
1336
+ colorMap: this.colorMap
1337
+ });
1338
+ }
1312
1339
  }
1313
1340
  };
1314
1341
  export {
@@ -1327,6 +1354,7 @@ export {
1327
1354
  drawPcbFabricationNoteText,
1328
1355
  drawPcbHole,
1329
1356
  drawPcbNoteRect,
1357
+ drawPcbNoteText,
1330
1358
  drawPcbPlatedHole,
1331
1359
  drawPcbSilkscreenCircle,
1332
1360
  drawPcbSilkscreenLine,
@@ -18,6 +18,7 @@ import type {
18
18
  PcbFabricationNoteRect,
19
19
  PcbNoteRect,
20
20
  PcbFabricationNotePath,
21
+ PcbNoteText,
21
22
  } from "circuit-json"
22
23
  import { identity, compose, translate, scale } from "transformation-matrix"
23
24
  import type { Matrix } from "transformation-matrix"
@@ -48,6 +49,7 @@ import { drawPcbFabricationNoteText } from "./elements/pcb-fabrication-note-text
48
49
  import { drawPcbFabricationNoteRect } from "./elements/pcb-fabrication-note-rect"
49
50
  import { drawPcbNoteRect } from "./elements/pcb-note-rect"
50
51
  import { drawPcbFabricationNotePath } from "./elements/pcb-fabrication-note-path"
52
+ import { drawPcbNoteText } from "./elements/pcb-note-text"
51
53
 
52
54
  export interface DrawElementsOptions {
53
55
  layers?: string[]
@@ -307,5 +309,14 @@ export class CircuitToCanvasDrawer {
307
309
  colorMap: this.colorMap,
308
310
  })
309
311
  }
312
+
313
+ if (element.type === "pcb_note_text") {
314
+ drawPcbNoteText({
315
+ ctx: this.ctx,
316
+ text: element as PcbNoteText,
317
+ transform: this.realToCanvasMat,
318
+ colorMap: this.colorMap,
319
+ })
320
+ }
310
321
  }
311
322
  }
@@ -57,3 +57,8 @@ export {
57
57
  drawPcbFabricationNotePath,
58
58
  type DrawPcbFabricationNotePathParams,
59
59
  } from "./pcb-fabrication-note-path"
60
+
61
+ export {
62
+ drawPcbNoteText,
63
+ type DrawPcbNoteTextParams,
64
+ } from "./pcb-note-text"
@@ -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.8",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "tsup-node ./lib/index.ts --format esm --dts",
@@ -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
+ })