circuit-to-canvas 0.0.52 → 0.0.53
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
|
@@ -428,21 +428,19 @@ function getTextStartPosition(alignment, layout) {
|
|
|
428
428
|
const totalHeight = layout.height + layout.strokeWidth;
|
|
429
429
|
let x = 0;
|
|
430
430
|
let y = 0;
|
|
431
|
-
if (alignment === "center") {
|
|
431
|
+
if (alignment === "center" || alignment === "top_center" || alignment === "bottom_center") {
|
|
432
432
|
x = -totalWidth / 2;
|
|
433
433
|
} else if (alignment === "top_left" || alignment === "bottom_left" || alignment === "center_left") {
|
|
434
434
|
x = 0;
|
|
435
435
|
} else if (alignment === "top_right" || alignment === "bottom_right" || alignment === "center_right") {
|
|
436
436
|
x = -totalWidth;
|
|
437
437
|
}
|
|
438
|
-
if (alignment === "center") {
|
|
438
|
+
if (alignment === "center" || alignment === "center_left" || alignment === "center_right") {
|
|
439
439
|
y = -totalHeight / 2;
|
|
440
440
|
} else if (alignment === "top_left" || alignment === "top_right" || alignment === "top_center") {
|
|
441
441
|
y = 0;
|
|
442
442
|
} else if (alignment === "bottom_left" || alignment === "bottom_right" || alignment === "bottom_center") {
|
|
443
443
|
y = -totalHeight;
|
|
444
|
-
} else {
|
|
445
|
-
y = 0;
|
|
446
444
|
}
|
|
447
445
|
return { x, y };
|
|
448
446
|
}
|
|
@@ -14,7 +14,11 @@ export function getTextStartPosition(
|
|
|
14
14
|
let y = 0
|
|
15
15
|
|
|
16
16
|
// Horizontal alignment
|
|
17
|
-
if (
|
|
17
|
+
if (
|
|
18
|
+
alignment === "center" ||
|
|
19
|
+
alignment === "top_center" ||
|
|
20
|
+
alignment === "bottom_center"
|
|
21
|
+
) {
|
|
18
22
|
x = -totalWidth / 2
|
|
19
23
|
} else if (
|
|
20
24
|
alignment === "top_left" ||
|
|
@@ -31,7 +35,11 @@ export function getTextStartPosition(
|
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
// Vertical alignment
|
|
34
|
-
if (
|
|
38
|
+
if (
|
|
39
|
+
alignment === "center" ||
|
|
40
|
+
alignment === "center_left" ||
|
|
41
|
+
alignment === "center_right"
|
|
42
|
+
) {
|
|
35
43
|
y = -totalHeight / 2
|
|
36
44
|
} else if (
|
|
37
45
|
alignment === "top_left" ||
|
|
@@ -45,8 +53,6 @@ export function getTextStartPosition(
|
|
|
45
53
|
alignment === "bottom_center"
|
|
46
54
|
) {
|
|
47
55
|
y = -totalHeight
|
|
48
|
-
} else {
|
|
49
|
-
y = 0
|
|
50
56
|
}
|
|
51
57
|
|
|
52
58
|
return { x, y }
|
package/package.json
CHANGED
|
Binary file
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { createCanvas } from "@napi-rs/canvas"
|
|
3
|
+
import type { PcbSilkscreenText } from "circuit-json"
|
|
4
|
+
import { CircuitToCanvasDrawer } from "../../lib/drawer"
|
|
5
|
+
|
|
6
|
+
test("draw silkscreen text with different anchor alignments on top and bottom layers", async () => {
|
|
7
|
+
const SCALE = 4
|
|
8
|
+
const canvas = createCanvas(400 * SCALE, 300 * 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
|
+
// Horizontal lines
|
|
20
|
+
ctx.beginPath()
|
|
21
|
+
ctx.moveTo(10, 50)
|
|
22
|
+
ctx.lineTo(390, 50)
|
|
23
|
+
ctx.stroke()
|
|
24
|
+
ctx.beginPath()
|
|
25
|
+
ctx.moveTo(10, 150)
|
|
26
|
+
ctx.lineTo(390, 150)
|
|
27
|
+
ctx.stroke()
|
|
28
|
+
ctx.beginPath()
|
|
29
|
+
ctx.moveTo(10, 250)
|
|
30
|
+
ctx.lineTo(390, 250)
|
|
31
|
+
ctx.stroke()
|
|
32
|
+
// Vertical lines
|
|
33
|
+
ctx.beginPath()
|
|
34
|
+
ctx.moveTo(100, 10)
|
|
35
|
+
ctx.lineTo(100, 290)
|
|
36
|
+
ctx.stroke()
|
|
37
|
+
ctx.beginPath()
|
|
38
|
+
ctx.moveTo(200, 10)
|
|
39
|
+
ctx.lineTo(200, 290)
|
|
40
|
+
ctx.stroke()
|
|
41
|
+
ctx.beginPath()
|
|
42
|
+
ctx.moveTo(300, 10)
|
|
43
|
+
ctx.lineTo(300, 290)
|
|
44
|
+
ctx.stroke()
|
|
45
|
+
|
|
46
|
+
const elements: PcbSilkscreenText[] = [
|
|
47
|
+
// Top layer tests
|
|
48
|
+
{
|
|
49
|
+
type: "pcb_silkscreen_text",
|
|
50
|
+
pcb_silkscreen_text_id: "top-top-left",
|
|
51
|
+
pcb_component_id: "component1",
|
|
52
|
+
layer: "top",
|
|
53
|
+
text: "TL",
|
|
54
|
+
anchor_position: { x: 100, y: 50 },
|
|
55
|
+
anchor_alignment: "top_left",
|
|
56
|
+
font: "tscircuit2024",
|
|
57
|
+
font_size: 12,
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
type: "pcb_silkscreen_text",
|
|
61
|
+
pcb_silkscreen_text_id: "top-center",
|
|
62
|
+
pcb_component_id: "component1",
|
|
63
|
+
layer: "top",
|
|
64
|
+
text: "TC",
|
|
65
|
+
anchor_position: { x: 200, y: 50 },
|
|
66
|
+
anchor_alignment: "top_center",
|
|
67
|
+
font: "tscircuit2024",
|
|
68
|
+
font_size: 12,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
type: "pcb_silkscreen_text",
|
|
72
|
+
pcb_silkscreen_text_id: "top-top-right",
|
|
73
|
+
pcb_component_id: "component1",
|
|
74
|
+
layer: "top",
|
|
75
|
+
text: "TR",
|
|
76
|
+
anchor_position: { x: 300, y: 50 },
|
|
77
|
+
anchor_alignment: "top_right",
|
|
78
|
+
font: "tscircuit2024",
|
|
79
|
+
font_size: 12,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
type: "pcb_silkscreen_text",
|
|
83
|
+
pcb_silkscreen_text_id: "top-center-left",
|
|
84
|
+
pcb_component_id: "component1",
|
|
85
|
+
layer: "top",
|
|
86
|
+
text: "CL",
|
|
87
|
+
anchor_position: { x: 100, y: 150 },
|
|
88
|
+
anchor_alignment: "center_left",
|
|
89
|
+
font: "tscircuit2024",
|
|
90
|
+
font_size: 12,
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
type: "pcb_silkscreen_text",
|
|
94
|
+
pcb_silkscreen_text_id: "top-center",
|
|
95
|
+
pcb_component_id: "component1",
|
|
96
|
+
layer: "top",
|
|
97
|
+
text: "C",
|
|
98
|
+
anchor_position: { x: 200, y: 150 },
|
|
99
|
+
anchor_alignment: "center",
|
|
100
|
+
font: "tscircuit2024",
|
|
101
|
+
font_size: 12,
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
type: "pcb_silkscreen_text",
|
|
105
|
+
pcb_silkscreen_text_id: "top-center-right",
|
|
106
|
+
pcb_component_id: "component1",
|
|
107
|
+
layer: "top",
|
|
108
|
+
text: "CR",
|
|
109
|
+
anchor_position: { x: 300, y: 150 },
|
|
110
|
+
anchor_alignment: "center_right",
|
|
111
|
+
font: "tscircuit2024",
|
|
112
|
+
font_size: 12,
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
type: "pcb_silkscreen_text",
|
|
116
|
+
pcb_silkscreen_text_id: "top-bottom-left",
|
|
117
|
+
pcb_component_id: "component1",
|
|
118
|
+
layer: "top",
|
|
119
|
+
text: "BL",
|
|
120
|
+
anchor_position: { x: 100, y: 250 },
|
|
121
|
+
anchor_alignment: "bottom_left",
|
|
122
|
+
font: "tscircuit2024",
|
|
123
|
+
font_size: 12,
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
type: "pcb_silkscreen_text",
|
|
127
|
+
pcb_silkscreen_text_id: "top-bottom-center",
|
|
128
|
+
pcb_component_id: "component1",
|
|
129
|
+
layer: "top",
|
|
130
|
+
text: "BC",
|
|
131
|
+
anchor_position: { x: 200, y: 250 },
|
|
132
|
+
anchor_alignment: "bottom_center",
|
|
133
|
+
font: "tscircuit2024",
|
|
134
|
+
font_size: 12,
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
type: "pcb_silkscreen_text",
|
|
138
|
+
pcb_silkscreen_text_id: "top-bottom-right",
|
|
139
|
+
pcb_component_id: "component1",
|
|
140
|
+
layer: "top",
|
|
141
|
+
text: "BR",
|
|
142
|
+
anchor_position: { x: 300, y: 250 },
|
|
143
|
+
anchor_alignment: "bottom_right",
|
|
144
|
+
font: "tscircuit2024",
|
|
145
|
+
font_size: 12,
|
|
146
|
+
},
|
|
147
|
+
// Bottom layer tests (should appear mirrored horizontally)
|
|
148
|
+
{
|
|
149
|
+
type: "pcb_silkscreen_text",
|
|
150
|
+
pcb_silkscreen_text_id: "bottom-top-left",
|
|
151
|
+
pcb_component_id: "component1",
|
|
152
|
+
layer: "bottom",
|
|
153
|
+
text: "BL-TL",
|
|
154
|
+
anchor_position: { x: 50, y: 50 },
|
|
155
|
+
anchor_alignment: "top_left",
|
|
156
|
+
font: "tscircuit2024",
|
|
157
|
+
font_size: 10,
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
type: "pcb_silkscreen_text",
|
|
161
|
+
pcb_silkscreen_text_id: "bottom-top-right",
|
|
162
|
+
pcb_component_id: "component1",
|
|
163
|
+
layer: "bottom",
|
|
164
|
+
text: "BL-TR",
|
|
165
|
+
anchor_position: { x: 350, y: 50 },
|
|
166
|
+
anchor_alignment: "top_right",
|
|
167
|
+
font: "tscircuit2024",
|
|
168
|
+
font_size: 10,
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
type: "pcb_silkscreen_text",
|
|
172
|
+
pcb_silkscreen_text_id: "bottom-center-left",
|
|
173
|
+
pcb_component_id: "component1",
|
|
174
|
+
layer: "bottom",
|
|
175
|
+
text: "BL-CL",
|
|
176
|
+
anchor_position: { x: 50, y: 150 },
|
|
177
|
+
anchor_alignment: "center_left",
|
|
178
|
+
font: "tscircuit2024",
|
|
179
|
+
font_size: 10,
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
type: "pcb_silkscreen_text",
|
|
183
|
+
pcb_silkscreen_text_id: "bottom-center-right",
|
|
184
|
+
pcb_component_id: "component1",
|
|
185
|
+
layer: "bottom",
|
|
186
|
+
text: "BL-CR",
|
|
187
|
+
anchor_position: { x: 350, y: 150 },
|
|
188
|
+
anchor_alignment: "center_right",
|
|
189
|
+
font: "tscircuit2024",
|
|
190
|
+
font_size: 10,
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
type: "pcb_silkscreen_text",
|
|
194
|
+
pcb_silkscreen_text_id: "bottom-bottom-left",
|
|
195
|
+
pcb_component_id: "component1",
|
|
196
|
+
layer: "bottom",
|
|
197
|
+
text: "BL-BL",
|
|
198
|
+
anchor_position: { x: 50, y: 250 },
|
|
199
|
+
anchor_alignment: "bottom_left",
|
|
200
|
+
font: "tscircuit2024",
|
|
201
|
+
font_size: 10,
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
type: "pcb_silkscreen_text",
|
|
205
|
+
pcb_silkscreen_text_id: "bottom-bottom-right",
|
|
206
|
+
pcb_component_id: "component1",
|
|
207
|
+
layer: "bottom",
|
|
208
|
+
text: "BL-BR",
|
|
209
|
+
anchor_position: { x: 350, y: 250 },
|
|
210
|
+
anchor_alignment: "bottom_right",
|
|
211
|
+
font: "tscircuit2024",
|
|
212
|
+
font_size: 10,
|
|
213
|
+
},
|
|
214
|
+
]
|
|
215
|
+
|
|
216
|
+
drawer.drawElements(elements)
|
|
217
|
+
|
|
218
|
+
await expect(canvas.toBuffer("image/png")).toMatchPngSnapshot(
|
|
219
|
+
import.meta.path,
|
|
220
|
+
)
|
|
221
|
+
})
|