circuit-to-canvas 0.0.44 → 0.0.45

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.js CHANGED
@@ -1843,9 +1843,6 @@ function drawPcbCopperPour(params) {
1843
1843
  // lib/drawer/elements/pcb-copper-text.ts
1844
1844
  import { applyToPoint as applyToPoint13 } from "transformation-matrix";
1845
1845
  var DEFAULT_PADDING = { left: 0.2, right: 0.2, top: 0.2, bottom: 0.2 };
1846
- function layerToCopperColor(layer, colorMap) {
1847
- return colorMap.copper[layer] ?? colorMap.copper.top;
1848
- }
1849
1846
  function mapAnchorAlignment2(alignment) {
1850
1847
  if (!alignment) return "center";
1851
1848
  if (alignment.includes("left")) return "center_left";
@@ -1867,13 +1864,11 @@ function drawPcbCopperText(params) {
1867
1864
  ...DEFAULT_PADDING,
1868
1865
  ...text.knockout_padding
1869
1866
  };
1870
- const textColor = layerToCopperColor(text.layer, colorMap);
1867
+ const textColor = colorMap.copper[text.layer] ?? colorMap.copper.top;
1871
1868
  const layout = getAlphabetLayout(content, fontSize);
1872
1869
  const totalWidth = layout.width + layout.strokeWidth;
1873
1870
  const alignment = mapAnchorAlignment2(text.anchor_alignment);
1874
1871
  const startPos = getTextStartPosition(alignment, layout);
1875
- const startX = startPos.x;
1876
- const startY = startPos.y;
1877
1872
  ctx.save();
1878
1873
  ctx.translate(x, y);
1879
1874
  if (text.is_mirrored) ctx.scale(-1, 1);
@@ -1886,29 +1881,22 @@ function drawPcbCopperText(params) {
1886
1881
  const paddingRight = padding.right * scale2;
1887
1882
  const paddingTop = padding.top * scale2;
1888
1883
  const paddingBottom = padding.bottom * scale2;
1889
- const textBoxTop = startY - layout.strokeWidth / 2;
1890
- const textBoxBottom = startY + layout.height + layout.strokeWidth / 2;
1891
- const textBoxHeight = textBoxBottom - textBoxTop;
1892
- const xOffset = startX - paddingLeft;
1893
- const yOffset = textBoxTop - paddingTop;
1894
- const knockoutWidth = totalWidth + paddingLeft + paddingRight;
1895
- const knockoutHeight = textBoxHeight + paddingTop + paddingBottom;
1884
+ const rectX = startPos.x - paddingLeft * 4;
1885
+ const rectY = startPos.y - paddingTop * 4;
1886
+ const rectWidth = totalWidth + paddingLeft * 2 + paddingRight * 2;
1887
+ const rectHeight = layout.height + layout.strokeWidth + paddingTop * 2 + paddingBottom * 2;
1896
1888
  ctx.fillStyle = textColor;
1897
- ctx.fillRect(xOffset, yOffset, knockoutWidth, knockoutHeight);
1898
- const previousCompositeOperation = ctx.globalCompositeOperation;
1899
- ctx.globalCompositeOperation = "destination-out";
1900
- ctx.fillStyle = "rgba(0,0,0,1)";
1901
- ctx.strokeStyle = "rgba(0,0,0,1)";
1902
- strokeAlphabetText({ ctx, text: content, fontSize, startX, startY });
1903
- if (previousCompositeOperation) {
1904
- ctx.globalCompositeOperation = previousCompositeOperation;
1905
- } else {
1906
- ctx.globalCompositeOperation = "source-over";
1907
- }
1889
+ ctx.fillRect(rectX, rectY, rectWidth, rectHeight);
1908
1890
  } else {
1909
1891
  ctx.strokeStyle = textColor;
1910
- strokeAlphabetText({ ctx, text: content, fontSize, startX, startY });
1911
1892
  }
1893
+ strokeAlphabetText({
1894
+ ctx,
1895
+ text: content,
1896
+ fontSize,
1897
+ startX: startPos.x,
1898
+ startY: startPos.y
1899
+ });
1912
1900
  ctx.restore();
1913
1901
  }
1914
1902
 
@@ -1,13 +1,12 @@
1
- import type { PcbCopperText } from "circuit-json"
1
+ import type { NinePointAnchor, PcbCopperText } from "circuit-json"
2
2
  import type { Matrix } from "transformation-matrix"
3
3
  import { applyToPoint } from "transformation-matrix"
4
- import type { PcbColorMap, CanvasContext } from "../types"
5
4
  import {
6
5
  getAlphabetLayout,
7
- strokeAlphabetText,
8
6
  getTextStartPosition,
9
- type AnchorAlignment,
7
+ strokeAlphabetText,
10
8
  } from "../shapes/text"
9
+ import type { CanvasContext, PcbColorMap } from "../types"
11
10
 
12
11
  export interface DrawPcbCopperTextParams {
13
12
  ctx: CanvasContext
@@ -18,14 +17,8 @@ export interface DrawPcbCopperTextParams {
18
17
 
19
18
  const DEFAULT_PADDING = { left: 0.2, right: 0.2, top: 0.2, bottom: 0.2 }
20
19
 
21
- function layerToCopperColor(layer: string, colorMap: PcbColorMap): string {
22
- return (
23
- colorMap.copper[layer as keyof typeof colorMap.copper] ??
24
- colorMap.copper.top
25
- )
26
- }
27
-
28
- function mapAnchorAlignment(alignment?: string): AnchorAlignment {
20
+ function mapAnchorAlignment(alignment?: string): NinePointAnchor {
21
+ // Vertical component is intentionally collapsed to center; callers only care about left/center/right.
29
22
  if (!alignment) return "center"
30
23
  if (alignment.includes("left")) return "center_left"
31
24
  if (alignment.includes("right")) return "center_right"
@@ -49,13 +42,13 @@ export function drawPcbCopperText(params: DrawPcbCopperTextParams): void {
49
42
  ...DEFAULT_PADDING,
50
43
  ...text.knockout_padding,
51
44
  }
52
- const textColor = layerToCopperColor(text.layer, colorMap)
45
+ const textColor =
46
+ colorMap.copper[text.layer as keyof typeof colorMap.copper] ??
47
+ colorMap.copper.top
53
48
  const layout = getAlphabetLayout(content, fontSize)
54
49
  const totalWidth = layout.width + layout.strokeWidth
55
50
  const alignment = mapAnchorAlignment(text.anchor_alignment)
56
51
  const startPos = getTextStartPosition(alignment, layout)
57
- const startX = startPos.x
58
- const startY = startPos.y
59
52
 
60
53
  ctx.save()
61
54
  ctx.translate(x, y)
@@ -71,32 +64,25 @@ export function drawPcbCopperText(params: DrawPcbCopperTextParams): void {
71
64
  const paddingRight = padding.right * scale
72
65
  const paddingTop = padding.top * scale
73
66
  const paddingBottom = padding.bottom * scale
74
- // Calculate knockout rectangle to cover the text box
75
- const textBoxTop = startY - layout.strokeWidth / 2
76
- const textBoxBottom = startY + layout.height + layout.strokeWidth / 2
77
- const textBoxHeight = textBoxBottom - textBoxTop
78
-
79
- const xOffset = startX - paddingLeft
80
- const yOffset = textBoxTop - paddingTop
81
- const knockoutWidth = totalWidth + paddingLeft + paddingRight
82
- const knockoutHeight = textBoxHeight + paddingTop + paddingBottom
67
+ const rectX = startPos.x - paddingLeft * 4
68
+ const rectY = startPos.y - paddingTop * 4
69
+ const rectWidth = totalWidth + paddingLeft * 2 + paddingRight * 2
70
+ const rectHeight =
71
+ layout.height + layout.strokeWidth + paddingTop * 2 + paddingBottom * 2
83
72
 
73
+ // Draw knockout rectangle
84
74
  ctx.fillStyle = textColor
85
- ctx.fillRect(xOffset, yOffset, knockoutWidth, knockoutHeight)
86
-
87
- const previousCompositeOperation = ctx.globalCompositeOperation
88
- ctx.globalCompositeOperation = "destination-out"
89
- ctx.fillStyle = "rgba(0,0,0,1)"
90
- ctx.strokeStyle = "rgba(0,0,0,1)"
91
- strokeAlphabetText({ ctx, text: content, fontSize, startX, startY })
92
- if (previousCompositeOperation) {
93
- ctx.globalCompositeOperation = previousCompositeOperation
94
- } else {
95
- ctx.globalCompositeOperation = "source-over"
96
- }
75
+ ctx.fillRect(rectX, rectY, rectWidth, rectHeight)
97
76
  } else {
98
77
  ctx.strokeStyle = textColor
99
- strokeAlphabetText({ ctx, text: content, fontSize, startX, startY })
100
78
  }
79
+
80
+ strokeAlphabetText({
81
+ ctx,
82
+ text: content,
83
+ fontSize,
84
+ startX: startPos.x,
85
+ startY: startPos.y,
86
+ })
101
87
  ctx.restore()
102
88
  }
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.44",
4
+ "version": "0.0.45",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "tsup-node ./lib/index.ts --format esm --dts",