@huaqiu/js-gerbv 0.1.2 → 0.1.15

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
@@ -37,36 +37,70 @@ npm run smoke
37
37
 
38
38
  ```js
39
39
  const gerbv = require("@huaqiu/js-gerbv");
40
- const img = gerbv.createImage();
41
- gerbv.addLine(img, 0, 0, 0.5, 0, 0.01);
42
- const buf = gerbv.exportRs274x(img);
40
+ // unit: 输入单位(native 侧换算为 inch);precision: 换算后 inch 小数位
41
+ const img = gerbv.createImage({ unit: "mm", precision: 6 });
42
+
43
+ // 多段线:起点在 createPolyline;add* 只给下一点
44
+ const pl = img.createPolyline(0, 0, 0.25);
45
+ pl.addPolylineLine(12.7, 0);
46
+ pl.addPolylineArc(6.35, 0, 6.35, 6.35, false);
47
+ pl.endPolyline();
48
+
49
+ // 区域:起点在 createPolygon;add* 只给下一点
50
+ const poly = img.createPolygon(0, 0);
51
+ poly.addPolygonLine(12.7, 0);
52
+ poly.addPolygonArc(12.7, 6.35, 12.7, 12.7, false);
53
+ poly.addPolygonBezier2(6.35, 15, 0, 12.7);
54
+
55
+ const hole = poly.createHole(3, 3); // 起点在 createHole
56
+ hole.addPolygonArc(5, 5, 7, 3, false);
57
+ hole.addPolygonBezier2(5.5, 7, 3, 6);
58
+ hole.endHole();
59
+
60
+ poly.addCircleHole(9, 6, 2);
61
+ poly.endArea();
62
+
63
+ img.addRect(15, 0, 10, 8).addCircleHole(20, 4, 2.5).endArea();
64
+ img.addCircle(30, 4, 6).endArea();
65
+ img.addCircleRing(40, 4, 8, 1.5);
66
+
67
+ const buf = img.exportRs274x();
43
68
  ```
44
69
 
70
+ 输入单位由 `createImage({ unit })` 指定(`inch` 默认 / `mm`);内部与导出仍为 inch(`%MOIN*%`)。`precision` 为换算到 inch 后保留的小数位。
71
+
72
+ `createPolygon` / `createHole` / `createPolyline` 均带**起点**;后续 `add*` 只传下一点/终点。
73
+
74
+ | API | 说明 |
75
+ |-----|------|
76
+ | `createImage({ unit?, precision? })` | 返回方法式 image;换算在 native 侧 |
77
+ | `img.createPolyline(x,y,w)` | 多段线起点 + 线宽 |
78
+ | `pl.addPolylineLine(x,y)` | 直线到点 |
79
+ | `pl.addPolylineArc(cx,cy,x,y,cw?)` | 圆弧到点 |
80
+ | `pl.addPolylineBezier2/3(...)` | 贝塞尔到点 |
81
+ | `pl.endPolyline()` | 结束多段线 |
82
+ | `img.addLine` / `addArc` / `addBezier2/3` | 单段描边(完整起止) |
83
+ | `img.addFlashCircle(x,y,d)` | 光圈 flash |
84
+ | `img.addRect` / `addCircle` | 矩形/圆 region(需 `endArea`) |
85
+ | `img.addCircleRing(cx,cy,d,w)` | 圆环(外径 d、环宽 w) |
86
+ | `img.createPolygon(x,y)` | region 起点 |
87
+ | `poly/hole.addPolygonLine(x,y)` | 直线到点 |
88
+ | `poly/hole.addPolygonArc` / `Bezier2/3` | 弧/贝塞尔到点 |
89
+ | `poly.createHole(x,y)` / `hole.endHole()` | 任意轮廓孔 |
90
+ | `poly.addCircleHole(cx,cy,d)` | 整圆孔 |
91
+ | `poly.endArea()` | 写出外环 + 孔 |
92
+ | `img.exportRs274x()` / `exportDrill()` | 导出 Buffer |
93
+
45
94
  安装(含已发布的 **darwin-arm64** prebuild,无需本机编):
46
95
 
47
96
  ```bash
48
97
  npm install @huaqiu/js-gerbv
49
98
  # 或尚未上 npm 时:
50
- npm install github:Huaqiu-Electronics/js-gerbv#v0.1.2
99
+ npm install github:Huaqiu-Electronics/js-gerbv#v0.1.3
51
100
  ```
52
101
 
53
- GitHub Release 资产:`js-gerbv-v*-darwin-arm64.tar.gz`、`js-gerbv-v*-win32-x64.tar.gz`(见 Releases)。
54
-
55
102
  本地联调可用 `"@huaqiu/js-gerbv": "file:../js-gerbv"`。
56
103
 
57
- ### 发布 mac prebuild
58
-
59
- ```bash
60
- npm run build && npm run smoke # 产出 prebuilds/darwin-arm64(含捆绑 dylib)
61
- git add prebuilds/darwin-arm64
62
- git commit -m "chore: add darwin-arm64 prebuild"
63
- git tag v0.1.2 && git push origin main --tags # 触发 release workflow
64
- # npm(需登录或仓库 secret NPM_TOKEN;scoped 包需 public):
65
- npm publish --access public
66
- ```
67
-
68
104
  ## CI
69
105
 
70
106
  `.github/workflows/ci.yml`:`macos-14` / `ubuntu-22.04` / `windows-2022` 矩阵编 prebuild 并上传 artifact。
71
-
72
- 不支持单机交叉编译到 Windows;请在对应 OS runner 上编。
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * 加载当前平台的 gerbv_node 原生插件。
3
- * 优先 prebuilds/<platform>-<arch>/,其次 build/。
3
+ *
4
+ * createPolygon(x,y) / createHole(x,y) / createPolyline(x,y,w):
5
+ * 起点在 create*;后续 add* 只给下一点 / 终点。
4
6
  */
5
7
  "use strict";
6
8
 
@@ -35,7 +37,346 @@ function resolveAddon() {
35
37
  );
36
38
  }
37
39
 
40
+ /** @param {unknown} obj */
41
+ function unwrap(obj) {
42
+ if (obj && typeof obj === "object" && "__handle" in obj) {
43
+ return /** @type {{ __handle: unknown }} */ (obj).__handle;
44
+ }
45
+ return obj;
46
+ }
47
+
48
+ function requireXY(api, x, y) {
49
+ if (typeof x !== "number" || typeof y !== "number" || Number.isNaN(x) || Number.isNaN(y)) {
50
+ throw new Error(api + "(x, y) requires numeric start point");
51
+ }
52
+ }
53
+
54
+ /** 矩形外环(左下角 + 宽高) */
55
+ function seedRectCmds(x, y, w, h) {
56
+ return [
57
+ { t: "L", x, y },
58
+ { t: "L", x: x + w, y },
59
+ { t: "L", x: x + w, y: y + h },
60
+ { t: "L", x, y: y + h },
61
+ ];
62
+ }
63
+
64
+ /** 圆形外环:右点起点 + 两段半圆弧 */
65
+ function seedCircleCmds(cx, cy, diam) {
66
+ const r = diam / 2;
67
+ return [
68
+ { t: "L", x: cx + r, y: cy },
69
+ { t: "A", cx, cy, x: cx - r, y: cy, cw: false },
70
+ { t: "A", cx, cy, x: cx + r, y: cy, cw: false },
71
+ ];
72
+ }
73
+
74
+ /**
75
+ * @param {object} native
76
+ * @param {unknown} imageHandle
77
+ * @param {object[]} cmds
78
+ * @param {boolean} clear
79
+ */
80
+ function replayContour(native, imageHandle, cmds, clear) {
81
+ if (!cmds.length) return;
82
+ const h = native.createPolygon(imageHandle, !!clear);
83
+ for (const c of cmds) {
84
+ if (c.t === "L") native.addPolygonLine(h, c.x, c.y);
85
+ else if (c.t === "A")
86
+ native.addPolygonArc(h, c.cx, c.cy, c.x, c.y, !!c.cw);
87
+ else if (c.t === "B2")
88
+ native.addPolygonBezier2(h, c.c1x, c.c1y, c.x, c.y);
89
+ else if (c.t === "B3")
90
+ native.addPolygonBezier3(h, c.c1x, c.c1y, c.c2x, c.c2y, c.x, c.y);
91
+ }
92
+ native.endPolygon(h);
93
+ }
94
+
95
+ /**
96
+ * 外环:add* 只追加下一点;createHole(x,y) / addCircleHole / endArea。
97
+ * @param {object[]} cmds
98
+ * @param {object} extras
99
+ */
100
+ function wrapOuter(cmds, extras) {
101
+ const contour = {
102
+ addPolygonLine(x, y) {
103
+ cmds.push({ t: "L", x, y });
104
+ return contour;
105
+ },
106
+ addPolygonArc(cx, cy, x, y, clockwise) {
107
+ cmds.push({ t: "A", cx, cy, x, y, cw: !!clockwise });
108
+ return contour;
109
+ },
110
+ addPolygonBezier2(c1x, c1y, x, y) {
111
+ cmds.push({ t: "B2", c1x, c1y, x, y });
112
+ return contour;
113
+ },
114
+ addPolygonBezier3(c1x, c1y, c2x, c2y, x, y) {
115
+ cmds.push({ t: "B3", c1x, c1y, c2x, c2y, x, y });
116
+ return contour;
117
+ },
118
+ createHole: extras.createHole,
119
+ addCircleHole: extras.addCircleHole,
120
+ endArea: extras.endArea,
121
+ };
122
+ return contour;
123
+ }
124
+
125
+ /**
126
+ * 孔:起点已在 createHole(x,y);add* 只给下一点;endHole 结束。
127
+ * @param {object[]} cmds
128
+ */
129
+ function wrapHole(cmds) {
130
+ let ended = false;
131
+ function ensureOpen(api) {
132
+ if (ended) throw new Error(api + ": hole already ended");
133
+ }
134
+ const hole = {
135
+ addPolygonLine(x, y) {
136
+ ensureOpen("addPolygonLine");
137
+ cmds.push({ t: "L", x, y });
138
+ return hole;
139
+ },
140
+ addPolygonArc(cx, cy, x, y, clockwise) {
141
+ ensureOpen("addPolygonArc");
142
+ cmds.push({ t: "A", cx, cy, x, y, cw: !!clockwise });
143
+ return hole;
144
+ },
145
+ addPolygonBezier2(c1x, c1y, x, y) {
146
+ ensureOpen("addPolygonBezier2");
147
+ cmds.push({ t: "B2", c1x, c1y, x, y });
148
+ return hole;
149
+ },
150
+ addPolygonBezier3(c1x, c1y, c2x, c2y, x, y) {
151
+ ensureOpen("addPolygonBezier3");
152
+ cmds.push({ t: "B3", c1x, c1y, c2x, c2y, x, y });
153
+ return hole;
154
+ },
155
+ endHole() {
156
+ ended = true;
157
+ },
158
+ };
159
+ return hole;
160
+ }
161
+
162
+ /**
163
+ * @param {object} native
164
+ * @param {unknown} imageHandle
165
+ * @param {object[]} seedOuter 至少含起点
166
+ */
167
+ function wrapRegion(native, imageHandle, seedOuter) {
168
+ const outerCmds = seedOuter.slice();
169
+ /** @type {object[][]} */
170
+ const holes = [];
171
+ let finished = false;
172
+
173
+ const region = wrapOuter(outerCmds, {
174
+ createHole(x, y) {
175
+ if (finished) throw new Error("polygon already ended");
176
+ requireXY("createHole", x, y);
177
+ const holeCmds = [{ t: "L", x, y }];
178
+ holes.push(holeCmds);
179
+ return wrapHole(holeCmds);
180
+ },
181
+ addCircleHole(cx, cy, diam) {
182
+ if (finished) throw new Error("polygon already ended");
183
+ holes.push(seedCircleCmds(cx, cy, diam));
184
+ return region;
185
+ },
186
+ endArea() {
187
+ if (finished) return;
188
+ finished = true;
189
+ replayContour(native, imageHandle, outerCmds, false);
190
+ for (const holeCmds of holes) {
191
+ replayContour(native, imageHandle, holeCmds, true);
192
+ }
193
+ },
194
+ });
195
+ return region;
196
+ }
197
+
198
+ /**
199
+ * 多段线描边:createPolyline(x,y,width);add* 只给下一点;endPolyline。
200
+ * @param {object} native
201
+ * @param {unknown} imageHandle
202
+ * @param {number} x0
203
+ * @param {number} y0
204
+ * @param {number} width
205
+ */
206
+ function wrapPolyline(native, imageHandle, x0, y0, width) {
207
+ let curX = x0;
208
+ let curY = y0;
209
+ let finished = false;
210
+ function ensureOpen(api) {
211
+ if (finished) throw new Error(api + ": polyline already ended");
212
+ }
213
+ const pl = {
214
+ addPolylineLine(x, y) {
215
+ ensureOpen("addPolylineLine");
216
+ native.addLine(imageHandle, curX, curY, x, y, width);
217
+ curX = x;
218
+ curY = y;
219
+ return pl;
220
+ },
221
+ addPolylineArc(cx, cy, x, y, clockwise) {
222
+ ensureOpen("addPolylineArc");
223
+ native.addArc(imageHandle, cx, cy, curX, curY, x, y, width, !!clockwise);
224
+ curX = x;
225
+ curY = y;
226
+ return pl;
227
+ },
228
+ addPolylineBezier2(c1x, c1y, x, y) {
229
+ ensureOpen("addPolylineBezier2");
230
+ native.addBezier2(imageHandle, curX, curY, c1x, c1y, x, y, width);
231
+ curX = x;
232
+ curY = y;
233
+ return pl;
234
+ },
235
+ addPolylineBezier3(c1x, c1y, c2x, c2y, x, y) {
236
+ ensureOpen("addPolylineBezier3");
237
+ native.addBezier3(
238
+ imageHandle,
239
+ curX,
240
+ curY,
241
+ c1x,
242
+ c1y,
243
+ c2x,
244
+ c2y,
245
+ x,
246
+ y,
247
+ width
248
+ );
249
+ curX = x;
250
+ curY = y;
251
+ return pl;
252
+ },
253
+ endPolyline() {
254
+ finished = true;
255
+ },
256
+ };
257
+ return pl;
258
+ }
259
+
260
+ /**
261
+ * @param {object} native
262
+ * @param {unknown} handle
263
+ */
264
+ function wrapImage(native, handle) {
265
+ const img = {
266
+ /** @internal */
267
+ __handle: handle,
268
+ addLine(x1, y1, x2, y2, width) {
269
+ native.addLine(handle, x1, y1, x2, y2, width);
270
+ return img;
271
+ },
272
+ addArc(cx, cy, x1, y1, x2, y2, width, clockwise) {
273
+ native.addArc(handle, cx, cy, x1, y1, x2, y2, width, !!clockwise);
274
+ return img;
275
+ },
276
+ addBezier2(x1, y1, c1x, c1y, x2, y2, width) {
277
+ native.addBezier2(handle, x1, y1, c1x, c1y, x2, y2, width);
278
+ return img;
279
+ },
280
+ addBezier3(x1, y1, c1x, c1y, c2x, c2y, x2, y2, width) {
281
+ native.addBezier3(handle, x1, y1, c1x, c1y, c2x, c2y, x2, y2, width);
282
+ return img;
283
+ },
284
+ addFlashCircle(x, y, diam) {
285
+ native.addFlashCircle(handle, x, y, diam);
286
+ return img;
287
+ },
288
+ addRect(x, y, width, height) {
289
+ return wrapRegion(native, handle, seedRectCmds(x, y, width, height));
290
+ },
291
+ addCircle(cx, cy, diam) {
292
+ return wrapRegion(native, handle, seedCircleCmds(cx, cy, diam));
293
+ },
294
+ addCircleRing(cx, cy, d, w) {
295
+ const region = wrapRegion(native, handle, seedCircleCmds(cx, cy, d));
296
+ const holeD = d - 2 * w;
297
+ if (w > 0 && holeD > 0) region.addCircleHole(cx, cy, holeD);
298
+ region.endArea();
299
+ return img;
300
+ },
301
+ /** @param {number} x @param {number} y */
302
+ createPolygon(x, y) {
303
+ requireXY("createPolygon", x, y);
304
+ return wrapRegion(native, handle, [{ t: "L", x, y }]);
305
+ },
306
+ /** @param {number} x @param {number} y @param {number} width */
307
+ createPolyline(x, y, width) {
308
+ requireXY("createPolyline", x, y);
309
+ if (typeof width !== "number" || !(width > 0)) {
310
+ throw new Error("createPolyline(x, y, width) requires positive width");
311
+ }
312
+ return wrapPolyline(native, handle, x, y, width);
313
+ },
314
+ exportRs274x() {
315
+ return native.exportRs274x(handle);
316
+ },
317
+ exportDrill() {
318
+ return native.exportDrill(handle);
319
+ },
320
+ };
321
+ return img;
322
+ }
323
+
38
324
  const addonPath = resolveAddon();
39
- module.exports = require(addonPath);
40
- module.exports.__addonPath = addonPath;
41
- module.exports.__platformKey = platformKey();
325
+ const native = require(addonPath);
326
+
327
+ module.exports = {
328
+ backend: native.backend,
329
+ __addonPath: addonPath,
330
+ __platformKey: platformKey(),
331
+ /**
332
+ * @param {{ unit?: 'inch'|'mm'|'in', precision?: number }} [opts]
333
+ */
334
+ createImage(opts) {
335
+ return wrapImage(native, native.createImage(opts || {}));
336
+ },
337
+ /** @deprecated 优先用 img.createPolygon(x, y) */
338
+ createPolygon(image, x, y) {
339
+ requireXY("createPolygon", x, y);
340
+ return wrapRegion(native, unwrap(image), [{ t: "L", x, y }]);
341
+ },
342
+ /** @deprecated */
343
+ addLine(image, x1, y1, x2, y2, width) {
344
+ native.addLine(unwrap(image), x1, y1, x2, y2, width);
345
+ },
346
+ addArc(image, cx, cy, x1, y1, x2, y2, width, clockwise) {
347
+ native.addArc(unwrap(image), cx, cy, x1, y1, x2, y2, width, !!clockwise);
348
+ },
349
+ addBezier2(image, x1, y1, c1x, c1y, x2, y2, width) {
350
+ native.addBezier2(unwrap(image), x1, y1, c1x, c1y, x2, y2, width);
351
+ },
352
+ addBezier3(image, x1, y1, c1x, c1y, c2x, c2y, x2, y2, width) {
353
+ native.addBezier3(
354
+ unwrap(image),
355
+ x1,
356
+ y1,
357
+ c1x,
358
+ c1y,
359
+ c2x,
360
+ c2y,
361
+ x2,
362
+ y2,
363
+ width
364
+ );
365
+ },
366
+ addFlashCircle(image, x, y, diam) {
367
+ native.addFlashCircle(unwrap(image), x, y, diam);
368
+ },
369
+ addRect(image, x, y, width, height) {
370
+ wrapRegion(
371
+ native,
372
+ unwrap(image),
373
+ seedRectCmds(x, y, width, height)
374
+ ).endArea();
375
+ },
376
+ exportRs274x(image) {
377
+ return native.exportRs274x(unwrap(image));
378
+ },
379
+ exportDrill(image) {
380
+ return native.exportDrill(unwrap(image));
381
+ },
382
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huaqiu/js-gerbv",
3
- "version": "0.1.2",
3
+ "version": "0.1.15",
4
4
  "description": "Node N-API binding for libgerbv (efa/gerbv) RS-274X/Drill export",
5
5
  "license": "GPL-2.0-or-later",
6
6
  "repository": {
@@ -5,5 +5,5 @@
5
5
  "node": "v24.20.0",
6
6
  "modules": "137",
7
7
  "napi": "10",
8
- "packedAt": "2026-11-28T08:21:39.525Z"
8
+ "packedAt": "2026-11-29T01:37:11.341Z"
9
9
  }
Binary file
@@ -2,8 +2,8 @@
2
2
  "platform": "win32",
3
3
  "arch": "x64",
4
4
  "key": "win32-x64",
5
- "node": "v24.22.1",
5
+ "node": "v24.16.0",
6
6
  "modules": "137",
7
7
  "napi": "10",
8
- "packedAt": "2026-11-28T10:15:58.578Z"
8
+ "packedAt": "2026-11-29T01:40:12.340Z"
9
9
  }
package/scripts/smoke.js CHANGED
@@ -1,20 +1,69 @@
1
1
  #!/usr/bin/env node
2
- /** 最小冒烟:画一条线并导出 RS-274X。 */
2
+ /** 冒烟:create*(x,y) 起点;polyline;addCircleHole / addCircleRing。 */
3
3
  "use strict";
4
4
 
5
5
  const gerbv = require("../index.js");
6
- const img = gerbv.createImage();
7
- gerbv.addLine(img, 0, 0, 0.5, 0, 0.01);
8
- const buf = gerbv.exportRs274x(img);
6
+
7
+ {
8
+ const img = gerbv.createImage();
9
+ const pl = img.createPolyline(0, 0, 0.01);
10
+ pl.addPolylineLine(0.5, 0).addPolylineLine(0.5, 0.2);
11
+ pl.endPolyline();
12
+ const buf = img.exportRs274x();
13
+ if (!buf || buf.length < 32) {
14
+ console.error("inch polyline failed");
15
+ process.exit(1);
16
+ }
17
+ }
18
+
19
+ const img = gerbv.createImage({ unit: "mm", precision: 6 });
20
+
21
+ const pl = img.createPolyline(0, 0, 0.25);
22
+ pl.addPolylineLine(12.7, 0);
23
+ pl.addPolylineArc(6.35, 0, 6.35, 6.35, false);
24
+ pl.endPolyline();
25
+
26
+ const poly = img.createPolygon(0, 0);
27
+ poly.addPolygonLine(12.7, 0);
28
+ poly.addPolygonLine(12.7, 10);
29
+ poly.addPolygonLine(0, 10);
30
+
31
+ const hole = poly.createHole(2.5, 2.5);
32
+ hole.addPolygonArc(3.8, 3.8, 5.0, 2.5, false);
33
+ hole.addPolygonBezier2(4.5, 5.5, 2.5, 5.0);
34
+ hole.endHole();
35
+
36
+ poly.addCircleHole(9, 6, 2);
37
+ poly.endArea();
38
+
39
+ img.addRect(15, 0, 10, 8).addCircleHole(20, 4, 2.5).endArea();
40
+ img.addCircle(30, 4, 6).endArea();
41
+ img.addCircleRing(40, 4, 8, 1.5);
42
+
43
+ const buf = img.exportRs274x();
9
44
  if (!buf || buf.length < 32) {
10
45
  console.error("export too small", buf && buf.length);
11
46
  process.exit(1);
12
47
  }
13
48
  const text = buf.toString("utf8");
14
- if (!text.includes("%MOIN*%") && !text.includes("MOIN")) {
15
- console.error("unexpected gerber header");
49
+ if (!text.includes("G36") || !text.includes("LPC") || !text.includes("MOIN")) {
50
+ console.error("missing G36/LPC/MOIN");
16
51
  process.exit(2);
17
52
  }
53
+ try {
54
+ img.createPolygon();
55
+ console.error("createPolygon without point should throw");
56
+ process.exit(3);
57
+ } catch (_) {
58
+ /* ok */
59
+ }
60
+ try {
61
+ poly.createHole();
62
+ console.error("createHole without point should throw");
63
+ process.exit(4);
64
+ } catch (_) {
65
+ /* ok */
66
+ }
18
67
  console.log(
19
68
  "smoke ok backend=",
20
69
  gerbv.backend,
package/src/addon.cc CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Node N-API 绑定:libgerbv 几何绘制 → RS-274X / Excellon。
3
- * 坐标单位:英寸(与 libgerbv create_* API 一致);调用方负责 mm→inch。
3
+ * 内部坐标:英寸(libgerbv)。createImage({ unit, precision }) 在 native 侧换算。
4
4
  */
5
5
  #include <napi.h>
6
6
 
@@ -11,19 +11,70 @@
11
11
 
12
12
  extern "C" {
13
13
  #include "gerbv.h"
14
+ #include "gerber.h"
15
+ #include "gerb_image.h"
16
+ /* gerb_image.c 内部辅助,未进公开头文件 */
17
+ gerbv_net_t* gerb_image_return_aperture_index(
18
+ gerbv_image_t* image, gdouble lineWidth, int* apertureIndex);
14
19
  }
15
20
 
21
+ #include <algorithm>
22
+ #include <cctype>
23
+ #include <cmath>
16
24
  #include <cstdio>
17
25
  #include <filesystem>
18
26
  #include <string>
19
27
  #include <unistd.h>
28
+ #include <utility>
20
29
 
21
30
  namespace {
22
31
 
23
32
  struct ImageHandle {
24
33
  gerbv_image_t* image = nullptr;
34
+ /** 用户单位 → inch 的乘数(inch=1,mm=1/25.4) */
35
+ double toInch = 1.0;
36
+ /** 换算到 inch 后保留的小数位;<0 表示不量化 */
37
+ int precision = -1;
25
38
  };
26
39
 
40
+ /** 用户坐标/长度 → libgerbv 内部 inch(可按 precision 量化) */
41
+ double ToInch(const ImageHandle* h, double v) {
42
+ double x = v * h->toInch;
43
+ if (h->precision >= 0) {
44
+ const int p = std::min(15, h->precision);
45
+ const double s = std::pow(10.0, p);
46
+ x = std::round(x * s) / s;
47
+ }
48
+ return x;
49
+ }
50
+
51
+ std::string LowerAscii(std::string s) {
52
+ for (char& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
53
+ return s;
54
+ }
55
+
56
+ gerbv_net_t* LastNet(gerbv_image_t* image) {
57
+ gerbv_net_t* n = image->netlist;
58
+ for (; n && n->next; n = n->next) {
59
+ }
60
+ return n;
61
+ }
62
+
63
+ /** 切换到指定 polarity 的 layer,并挂一个继承该 layer 的净空 net,供后续 create_* 继承。 */
64
+ gerbv_layer_t* EnsurePolarity(gerbv_image_t* image, gerbv_polarity_t pol) {
65
+ gerbv_net_t* current = LastNet(image);
66
+ if (!current || !current->layer) return nullptr;
67
+ if (current->layer->polarity == pol) return current->layer;
68
+ gerbv_layer_t* layer = gerbv_image_return_new_layer(current->layer);
69
+ layer->polarity = pol;
70
+ current = gerber_create_new_net(current, layer, nullptr);
71
+ current->interpolation = GERBV_INTERPOLATION_LINEARx1;
72
+ current->aperture_state = GERBV_APERTURE_STATE_OFF;
73
+ current->start_x = current->stop_x = 0;
74
+ current->start_y = current->stop_y = 0;
75
+ return layer;
76
+ }
77
+
27
78
  void FreeImage(Napi::Env, ImageHandle* h) {
28
79
  if (h) {
29
80
  if (h->image) gerbv_destroy_image(h->image);
@@ -38,9 +89,37 @@ ImageHandle* Unwrap(const Napi::Value& v) {
38
89
  return v.As<ImageExt>().Data();
39
90
  }
40
91
 
92
+ /**
93
+ * createImage() | createImage({ unit?: 'inch'|'mm', precision?: number })
94
+ * unit:输入坐标单位(内部仍转成 inch 交给 libgerbv)
95
+ * precision:转到 inch 后保留的小数位(省略则不量化)
96
+ */
41
97
  Napi::Value CreateImage(const Napi::CallbackInfo& info) {
42
98
  Napi::Env env = info.Env();
43
99
  auto* h = new ImageHandle();
100
+ h->toInch = 1.0;
101
+ h->precision = -1;
102
+
103
+ if (info.Length() >= 1 && info[0].IsObject()) {
104
+ Napi::Object opts = info[0].As<Napi::Object>();
105
+ if (opts.Has("unit") && !opts.Get("unit").IsUndefined() && !opts.Get("unit").IsNull()) {
106
+ const std::string unit = LowerAscii(opts.Get("unit").ToString().Utf8Value());
107
+ if (unit == "mm" || unit == "metric") {
108
+ h->toInch = 1.0 / 25.4;
109
+ } else if (unit == "inch" || unit == "in" || unit == "imperial") {
110
+ h->toInch = 1.0;
111
+ } else {
112
+ delete h;
113
+ Napi::TypeError::New(env, "createImage: unit must be 'inch' or 'mm'")
114
+ .ThrowAsJavaScriptException();
115
+ return env.Null();
116
+ }
117
+ }
118
+ if (opts.Has("precision") && opts.Get("precision").IsNumber()) {
119
+ h->precision = opts.Get("precision").As<Napi::Number>().Int32Value();
120
+ }
121
+ }
122
+
44
123
  h->image = gerbv_create_image(nullptr, "hqpcb-lite");
45
124
  if (!h->image) {
46
125
  delete h;
@@ -53,7 +132,7 @@ Napi::Value CreateImage(const Napi::CallbackInfo& info) {
53
132
  Napi::Value AddLine(const Napi::CallbackInfo& info) {
54
133
  Napi::Env env = info.Env();
55
134
  if (info.Length() < 6) {
56
- Napi::TypeError::New(env, "addLine(image,x1,y1,x2,y2,widthInch)").ThrowAsJavaScriptException();
135
+ Napi::TypeError::New(env, "addLine(image,x1,y1,x2,y2,width)").ThrowAsJavaScriptException();
57
136
  return env.Undefined();
58
137
  }
59
138
  auto* h = Unwrap(info[0]);
@@ -61,19 +140,97 @@ Napi::Value AddLine(const Napi::CallbackInfo& info) {
61
140
  Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
62
141
  return env.Undefined();
63
142
  }
64
- const double x1 = info[1].As<Napi::Number>().DoubleValue();
65
- const double y1 = info[2].As<Napi::Number>().DoubleValue();
66
- const double x2 = info[3].As<Napi::Number>().DoubleValue();
67
- const double y2 = info[4].As<Napi::Number>().DoubleValue();
68
- const double w = info[5].As<Napi::Number>().DoubleValue();
143
+ const double x1 = ToInch(h, info[1].As<Napi::Number>().DoubleValue());
144
+ const double y1 = ToInch(h, info[2].As<Napi::Number>().DoubleValue());
145
+ const double x2 = ToInch(h, info[3].As<Napi::Number>().DoubleValue());
146
+ const double y2 = ToInch(h, info[4].As<Napi::Number>().DoubleValue());
147
+ const double w = ToInch(h, info[5].As<Napi::Number>().DoubleValue());
148
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
69
149
  gerbv_image_create_line_object(h->image, x1, y1, x2, y2, w, GERBV_APTYPE_CIRCLE);
70
150
  return env.Undefined();
71
151
  }
72
152
 
153
+ /**
154
+ * 圆弧描边。addArc(image, cx, cy, x1, y1, x2, y2, width, clockwise?=false)
155
+ * 坐标单位由 createImage({ unit }) 决定;导出为 G02/G03。
156
+ */
157
+ Napi::Value AddArc(const Napi::CallbackInfo& info) {
158
+ Napi::Env env = info.Env();
159
+ if (info.Length() < 8) {
160
+ Napi::TypeError::New(
161
+ env, "addArc(image,cx,cy,x1,y1,x2,y2,width,clockwise?=false)")
162
+ .ThrowAsJavaScriptException();
163
+ return env.Undefined();
164
+ }
165
+ auto* h = Unwrap(info[0]);
166
+ if (!h || !h->image) {
167
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
168
+ return env.Undefined();
169
+ }
170
+ const double cx = ToInch(h, info[1].As<Napi::Number>().DoubleValue());
171
+ const double cy = ToInch(h, info[2].As<Napi::Number>().DoubleValue());
172
+ const double x1 = ToInch(h, info[3].As<Napi::Number>().DoubleValue());
173
+ const double y1 = ToInch(h, info[4].As<Napi::Number>().DoubleValue());
174
+ const double x2 = ToInch(h, info[5].As<Napi::Number>().DoubleValue());
175
+ const double y2 = ToInch(h, info[6].As<Napi::Number>().DoubleValue());
176
+ const double w = ToInch(h, info[7].As<Napi::Number>().DoubleValue());
177
+ const bool clockwise = info.Length() >= 9 && info[8].ToBoolean().Value();
178
+
179
+ const double r = std::hypot(x1 - cx, y1 - cy);
180
+ if (!(r > 1e-12) || !(w > 0)) return env.Undefined();
181
+
182
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
183
+
184
+ const double RAD2DEG = 180.0 / 3.14159265358979323846;
185
+ const double a0 = std::atan2(y1 - cy, x1 - cx) * RAD2DEG;
186
+ const double a1 = std::atan2(y2 - cy, x2 - cx) * RAD2DEG;
187
+
188
+ int apertureIndex = 0;
189
+ gerbv_net_t* currentNet =
190
+ gerb_image_return_aperture_index(h->image, w, &apertureIndex);
191
+ if (!currentNet) return env.Undefined();
192
+
193
+ currentNet = gerber_create_new_net(currentNet, currentNet->layer, nullptr);
194
+ currentNet->interpolation =
195
+ clockwise ? GERBV_INTERPOLATION_CW_CIRCULAR : GERBV_INTERPOLATION_CCW_CIRCULAR;
196
+ currentNet->aperture_state = GERBV_APERTURE_STATE_ON;
197
+ currentNet->aperture = apertureIndex;
198
+ currentNet->start_x = x1;
199
+ currentNet->start_y = y1;
200
+ currentNet->stop_x = x2;
201
+ currentNet->stop_y = y2;
202
+ currentNet->cirseg = g_new0(gerbv_cirseg_t, 1);
203
+ currentNet->cirseg->cp_x = cx;
204
+ currentNet->cirseg->cp_y = cy;
205
+ currentNet->cirseg->width = r;
206
+ currentNet->cirseg->height = r;
207
+ currentNet->cirseg->angle1 = a0;
208
+ currentNet->cirseg->angle2 = a1;
209
+
210
+ double angleDiff = a1 - a0;
211
+ if (clockwise) {
212
+ while (angleDiff > 0) angleDiff -= 360.0;
213
+ if (angleDiff == 0) angleDiff = -360.0;
214
+ } else {
215
+ while (angleDiff < 0) angleDiff += 360.0;
216
+ if (angleDiff == 0) angleDiff = 360.0;
217
+ }
218
+ const int steps = std::max(1, static_cast<int>(std::fabs(angleDiff)));
219
+ for (int i = 0; i <= steps; i++) {
220
+ const double a = (a0 + (angleDiff * i) / steps) * (3.14159265358979323846 / 180.0);
221
+ const double tx = cx + r * std::cos(a);
222
+ const double ty = cy + r * std::sin(a);
223
+ gerber_update_min_and_max(
224
+ &currentNet->boundingBox, tx, ty, w / 2, w / 2, w / 2, w / 2);
225
+ }
226
+ gerber_update_image_min_max(&currentNet->boundingBox, 0, 0, h->image);
227
+ return env.Undefined();
228
+ }
229
+
73
230
  Napi::Value AddFlashCircle(const Napi::CallbackInfo& info) {
74
231
  Napi::Env env = info.Env();
75
232
  if (info.Length() < 4) {
76
- Napi::TypeError::New(env, "addFlashCircle(image,x,y,diamInch)").ThrowAsJavaScriptException();
233
+ Napi::TypeError::New(env, "addFlashCircle(image,x,y,diam)").ThrowAsJavaScriptException();
77
234
  return env.Undefined();
78
235
  }
79
236
  auto* h = Unwrap(info[0]);
@@ -81,9 +238,10 @@ Napi::Value AddFlashCircle(const Napi::CallbackInfo& info) {
81
238
  Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
82
239
  return env.Undefined();
83
240
  }
84
- const double x = info[1].As<Napi::Number>().DoubleValue();
85
- const double y = info[2].As<Napi::Number>().DoubleValue();
86
- const double d = info[3].As<Napi::Number>().DoubleValue();
241
+ const double x = ToInch(h, info[1].As<Napi::Number>().DoubleValue());
242
+ const double y = ToInch(h, info[2].As<Napi::Number>().DoubleValue());
243
+ const double d = ToInch(h, info[3].As<Napi::Number>().DoubleValue());
244
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
87
245
  // 同点起止 → libgerbv 走 FLASH
88
246
  gerbv_image_create_line_object(h->image, x, y, x, y, d, GERBV_APTYPE_CIRCLE);
89
247
  return env.Undefined();
@@ -92,7 +250,7 @@ Napi::Value AddFlashCircle(const Napi::CallbackInfo& info) {
92
250
  Napi::Value AddRect(const Napi::CallbackInfo& info) {
93
251
  Napi::Env env = info.Env();
94
252
  if (info.Length() < 5) {
95
- Napi::TypeError::New(env, "addRect(image,x,y,widthInch,heightInch)").ThrowAsJavaScriptException();
253
+ Napi::TypeError::New(env, "addRect(image,x,y,width,height)").ThrowAsJavaScriptException();
96
254
  return env.Undefined();
97
255
  }
98
256
  auto* h = Unwrap(info[0]);
@@ -100,14 +258,396 @@ Napi::Value AddRect(const Napi::CallbackInfo& info) {
100
258
  Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
101
259
  return env.Undefined();
102
260
  }
103
- const double x = info[1].As<Napi::Number>().DoubleValue();
104
- const double y = info[2].As<Napi::Number>().DoubleValue();
105
- const double w = info[3].As<Napi::Number>().DoubleValue();
106
- const double ht = info[4].As<Napi::Number>().DoubleValue();
261
+ const double x = ToInch(h, info[1].As<Napi::Number>().DoubleValue());
262
+ const double y = ToInch(h, info[2].As<Napi::Number>().DoubleValue());
263
+ const double w = ToInch(h, info[3].As<Napi::Number>().DoubleValue());
264
+ const double ht = ToInch(h, info[4].As<Napi::Number>().DoubleValue());
265
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
107
266
  gerbv_image_create_rectangle_object(h->image, x, y, w, ht);
108
267
  return env.Undefined();
109
268
  }
110
269
 
270
+ /** 区域多边形构建器(G36…G37)。贝塞尔在本侧采样为折线。 */
271
+ struct PolygonHandle {
272
+ ImageHandle* owner = nullptr;
273
+ gerbv_image_t* image = nullptr;
274
+ gerbv_layer_t* layer = nullptr;
275
+ double startX = 0;
276
+ double startY = 0;
277
+ double curX = 0;
278
+ double curY = 0;
279
+ bool hasPoint = false;
280
+ bool finished = false;
281
+ };
282
+
283
+ double ToInch(const PolygonHandle* p, double v) {
284
+ return p && p->owner ? ToInch(p->owner, v) : v;
285
+ }
286
+
287
+ void FreePolygon(Napi::Env, PolygonHandle* p) { delete p; }
288
+
289
+ using PolygonExt = Napi::External<PolygonHandle>;
290
+
291
+ PolygonHandle* UnwrapPoly(const Napi::Value& v) {
292
+ if (!v.IsExternal()) return nullptr;
293
+ return v.As<PolygonExt>().Data();
294
+ }
295
+
296
+ void PolyAppendLine(
297
+ PolygonHandle* p, double x2, double y2, bool penOn) {
298
+ gerbv_net_t* currentNet = LastNet(p->image);
299
+ currentNet = gerber_create_new_net(currentNet, p->layer, nullptr);
300
+ currentNet->interpolation = GERBV_INTERPOLATION_LINEARx1;
301
+ currentNet->aperture_state =
302
+ penOn ? GERBV_APERTURE_STATE_ON : GERBV_APERTURE_STATE_OFF;
303
+ currentNet->start_x = p->curX;
304
+ currentNet->start_y = p->curY;
305
+ currentNet->stop_x = x2;
306
+ currentNet->stop_y = y2;
307
+ if (penOn) {
308
+ gerber_update_min_and_max(
309
+ &currentNet->boundingBox, x2, y2, 0, 0, 0, 0);
310
+ gerber_update_image_min_max(&currentNet->boundingBox, 0, 0, p->image);
311
+ }
312
+ p->curX = x2;
313
+ p->curY = y2;
314
+ }
315
+
316
+ void PolyAppendArc(
317
+ PolygonHandle* p, double cx, double cy, double x2, double y2, bool clockwise) {
318
+ const double x1 = p->curX;
319
+ const double y1 = p->curY;
320
+ const double r = std::hypot(x1 - cx, y1 - cy);
321
+ if (!(r > 1e-12)) {
322
+ PolyAppendLine(p, x2, y2, true);
323
+ return;
324
+ }
325
+ const double RAD2DEG = 180.0 / 3.14159265358979323846;
326
+ const double a0 = std::atan2(y1 - cy, x1 - cx) * RAD2DEG;
327
+ const double a1 = std::atan2(y2 - cy, x2 - cx) * RAD2DEG;
328
+
329
+ gerbv_net_t* currentNet = LastNet(p->image);
330
+ currentNet = gerber_create_new_net(currentNet, p->layer, nullptr);
331
+ currentNet->interpolation =
332
+ clockwise ? GERBV_INTERPOLATION_CW_CIRCULAR : GERBV_INTERPOLATION_CCW_CIRCULAR;
333
+ currentNet->aperture_state = GERBV_APERTURE_STATE_ON;
334
+ currentNet->aperture = 0;
335
+ currentNet->start_x = x1;
336
+ currentNet->start_y = y1;
337
+ currentNet->stop_x = x2;
338
+ currentNet->stop_y = y2;
339
+ currentNet->cirseg = g_new0(gerbv_cirseg_t, 1);
340
+ currentNet->cirseg->cp_x = cx;
341
+ currentNet->cirseg->cp_y = cy;
342
+ currentNet->cirseg->width = r;
343
+ currentNet->cirseg->height = r;
344
+ currentNet->cirseg->angle1 = a0;
345
+ currentNet->cirseg->angle2 = a1;
346
+
347
+ double angleDiff = a1 - a0;
348
+ if (clockwise) {
349
+ while (angleDiff > 0) angleDiff -= 360.0;
350
+ if (angleDiff == 0) angleDiff = -360.0;
351
+ } else {
352
+ while (angleDiff < 0) angleDiff += 360.0;
353
+ if (angleDiff == 0) angleDiff = 360.0;
354
+ }
355
+ const int steps = std::max(1, static_cast<int>(std::fabs(angleDiff)));
356
+ for (int i = 0; i <= steps; i++) {
357
+ const double a = (a0 + (angleDiff * i) / steps) * (3.14159265358979323846 / 180.0);
358
+ gerber_update_min_and_max(
359
+ &currentNet->boundingBox, cx + r * std::cos(a), cy + r * std::sin(a), 0, 0, 0, 0);
360
+ }
361
+ gerber_update_image_min_max(&currentNet->boundingBox, 0, 0, p->image);
362
+ p->curX = x2;
363
+ p->curY = y2;
364
+ }
365
+
366
+ void PolyAppendBezierSampled(
367
+ PolygonHandle* p, double c1x, double c1y, double c2x, double c2y, double x2,
368
+ double y2, bool cubic, int steps) {
369
+ const double x0 = p->curX;
370
+ const double y0 = p->curY;
371
+ const int n = std::max(4, steps);
372
+ for (int i = 1; i <= n; i++) {
373
+ const double t = static_cast<double>(i) / n;
374
+ const double u = 1.0 - t;
375
+ double x, y;
376
+ if (cubic) {
377
+ x = u * u * u * x0 + 3 * u * u * t * c1x + 3 * u * t * t * c2x + t * t * t * x2;
378
+ y = u * u * u * y0 + 3 * u * u * t * c1y + 3 * u * t * t * c2y + t * t * t * y2;
379
+ } else {
380
+ x = u * u * x0 + 2 * u * t * c1x + t * t * x2;
381
+ y = u * u * y0 + 2 * u * t * c1y + t * t * y2;
382
+ }
383
+ PolyAppendLine(p, x, y, true);
384
+ }
385
+ }
386
+
387
+ bool PolyEnsureStart(PolygonHandle* p, double x, double y, Napi::Env env) {
388
+ if (p->finished) {
389
+ Napi::Error::New(env, "polygon already ended").ThrowAsJavaScriptException();
390
+ return false;
391
+ }
392
+ if (p->hasPoint) return true;
393
+ gerbv_net_t* currentNet = LastNet(p->image);
394
+ currentNet = gerber_create_new_net(currentNet, p->layer, nullptr);
395
+ currentNet->interpolation = GERBV_INTERPOLATION_PAREA_START;
396
+
397
+ p->startX = p->curX = x;
398
+ p->startY = p->curY = y;
399
+ p->hasPoint = true;
400
+
401
+ currentNet = gerber_create_new_net(LastNet(p->image), p->layer, nullptr);
402
+ currentNet->interpolation = GERBV_INTERPOLATION_LINEARx1;
403
+ currentNet->aperture_state = GERBV_APERTURE_STATE_OFF;
404
+ currentNet->start_x = x;
405
+ currentNet->start_y = y;
406
+ currentNet->stop_x = x;
407
+ currentNet->stop_y = y;
408
+ return true;
409
+ }
410
+
411
+ /**
412
+ * createPolygon(image, clear?=false) → polygon
413
+ */
414
+ Napi::Value CreatePolygon(const Napi::CallbackInfo& info) {
415
+ Napi::Env env = info.Env();
416
+ if (info.Length() < 1) {
417
+ Napi::TypeError::New(env, "createPolygon(image, clear?=false)")
418
+ .ThrowAsJavaScriptException();
419
+ return env.Null();
420
+ }
421
+ auto* h = Unwrap(info[0]);
422
+ if (!h || !h->image) {
423
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
424
+ return env.Null();
425
+ }
426
+ const bool clear = info.Length() >= 2 && info[1].ToBoolean().Value();
427
+ gerbv_layer_t* layer =
428
+ EnsurePolarity(h->image, clear ? GERBV_POLARITY_CLEAR : GERBV_POLARITY_DARK);
429
+ if (!layer) {
430
+ Napi::Error::New(env, "empty netlist").ThrowAsJavaScriptException();
431
+ return env.Null();
432
+ }
433
+ auto* poly = new PolygonHandle();
434
+ poly->owner = h;
435
+ poly->image = h->image;
436
+ poly->layer = layer;
437
+ return PolygonExt::New(env, poly, FreePolygon);
438
+ }
439
+
440
+ /** addPolygonLine(polygon, x, y) — 首点为起点,其后为直线边 */
441
+ Napi::Value AddPolygonLine(const Napi::CallbackInfo& info) {
442
+ Napi::Env env = info.Env();
443
+ if (info.Length() < 3) {
444
+ Napi::TypeError::New(env, "addPolygonLine(polygon, x, y)")
445
+ .ThrowAsJavaScriptException();
446
+ return env.Undefined();
447
+ }
448
+ auto* p = UnwrapPoly(info[0]);
449
+ if (!p || !p->image) {
450
+ Napi::Error::New(env, "invalid polygon").ThrowAsJavaScriptException();
451
+ return env.Undefined();
452
+ }
453
+ const double x = ToInch(p, info[1].As<Napi::Number>().DoubleValue());
454
+ const double y = ToInch(p, info[2].As<Napi::Number>().DoubleValue());
455
+ if (!p->hasPoint) {
456
+ if (!PolyEnsureStart(p, x, y, env)) return env.Undefined();
457
+ return env.Undefined();
458
+ }
459
+ PolyAppendLine(p, x, y, true);
460
+ return env.Undefined();
461
+ }
462
+
463
+ /** addPolygonArc(polygon, cx, cy, x, y, clockwise?=false) */
464
+ Napi::Value AddPolygonArc(const Napi::CallbackInfo& info) {
465
+ Napi::Env env = info.Env();
466
+ if (info.Length() < 5) {
467
+ Napi::TypeError::New(env, "addPolygonArc(polygon, cx, cy, x, y, clockwise?=false)")
468
+ .ThrowAsJavaScriptException();
469
+ return env.Undefined();
470
+ }
471
+ auto* p = UnwrapPoly(info[0]);
472
+ if (!p || !p->image) {
473
+ Napi::Error::New(env, "invalid polygon").ThrowAsJavaScriptException();
474
+ return env.Undefined();
475
+ }
476
+ if (!p->hasPoint) {
477
+ Napi::Error::New(env, "addPolygonArc needs a start point first")
478
+ .ThrowAsJavaScriptException();
479
+ return env.Undefined();
480
+ }
481
+ if (p->finished) {
482
+ Napi::Error::New(env, "polygon already ended").ThrowAsJavaScriptException();
483
+ return env.Undefined();
484
+ }
485
+ const double cx = ToInch(p, info[1].As<Napi::Number>().DoubleValue());
486
+ const double cy = ToInch(p, info[2].As<Napi::Number>().DoubleValue());
487
+ const double x = ToInch(p, info[3].As<Napi::Number>().DoubleValue());
488
+ const double y = ToInch(p, info[4].As<Napi::Number>().DoubleValue());
489
+ const bool cw = info.Length() >= 6 && info[5].ToBoolean().Value();
490
+ PolyAppendArc(p, cx, cy, x, y, cw);
491
+ return env.Undefined();
492
+ }
493
+
494
+ bool PolyReadyForEdge(PolygonHandle* p, Napi::Env env, const char* api) {
495
+ if (!p || !p->image) {
496
+ Napi::Error::New(env, "invalid polygon").ThrowAsJavaScriptException();
497
+ return false;
498
+ }
499
+ if (!p->hasPoint) {
500
+ Napi::Error::New(env, std::string(api) + " needs a start point first")
501
+ .ThrowAsJavaScriptException();
502
+ return false;
503
+ }
504
+ if (p->finished) {
505
+ Napi::Error::New(env, "polygon already ended").ThrowAsJavaScriptException();
506
+ return false;
507
+ }
508
+ return true;
509
+ }
510
+
511
+ /** addPolygonBezier2(polygon, c1x, c1y, x, y) — 二次贝塞尔边(本侧采样) */
512
+ Napi::Value AddPolygonBezier2(const Napi::CallbackInfo& info) {
513
+ Napi::Env env = info.Env();
514
+ if (info.Length() < 5) {
515
+ Napi::TypeError::New(env, "addPolygonBezier2(polygon, c1x, c1y, x, y)")
516
+ .ThrowAsJavaScriptException();
517
+ return env.Undefined();
518
+ }
519
+ auto* p = UnwrapPoly(info[0]);
520
+ if (!PolyReadyForEdge(p, env, "addPolygonBezier2")) return env.Undefined();
521
+ const double c1x = ToInch(p, info[1].As<Napi::Number>().DoubleValue());
522
+ const double c1y = ToInch(p, info[2].As<Napi::Number>().DoubleValue());
523
+ const double x = ToInch(p, info[3].As<Napi::Number>().DoubleValue());
524
+ const double y = ToInch(p, info[4].As<Napi::Number>().DoubleValue());
525
+ PolyAppendBezierSampled(p, c1x, c1y, c1x, c1y, x, y, false, 16);
526
+ return env.Undefined();
527
+ }
528
+
529
+ /** addPolygonBezier3(polygon, c1x, c1y, c2x, c2y, x, y) — 三次贝塞尔边(本侧采样) */
530
+ Napi::Value AddPolygonBezier3(const Napi::CallbackInfo& info) {
531
+ Napi::Env env = info.Env();
532
+ if (info.Length() < 7) {
533
+ Napi::TypeError::New(env, "addPolygonBezier3(polygon, c1x, c1y, c2x, c2y, x, y)")
534
+ .ThrowAsJavaScriptException();
535
+ return env.Undefined();
536
+ }
537
+ auto* p = UnwrapPoly(info[0]);
538
+ if (!PolyReadyForEdge(p, env, "addPolygonBezier3")) return env.Undefined();
539
+ const double c1x = ToInch(p, info[1].As<Napi::Number>().DoubleValue());
540
+ const double c1y = ToInch(p, info[2].As<Napi::Number>().DoubleValue());
541
+ const double c2x = ToInch(p, info[3].As<Napi::Number>().DoubleValue());
542
+ const double c2y = ToInch(p, info[4].As<Napi::Number>().DoubleValue());
543
+ const double x = ToInch(p, info[5].As<Napi::Number>().DoubleValue());
544
+ const double y = ToInch(p, info[6].As<Napi::Number>().DoubleValue());
545
+ PolyAppendBezierSampled(p, c1x, c1y, c2x, c2y, x, y, true, 20);
546
+ return env.Undefined();
547
+ }
548
+
549
+ /** endPolygon(polygon) — 自动闭合并写 G37 */
550
+ Napi::Value EndPolygon(const Napi::CallbackInfo& info) {
551
+ Napi::Env env = info.Env();
552
+ if (info.Length() < 1) {
553
+ Napi::TypeError::New(env, "endPolygon(polygon)").ThrowAsJavaScriptException();
554
+ return env.Undefined();
555
+ }
556
+ auto* p = UnwrapPoly(info[0]);
557
+ if (!p || !p->image) {
558
+ Napi::Error::New(env, "invalid polygon").ThrowAsJavaScriptException();
559
+ return env.Undefined();
560
+ }
561
+ if (p->finished) return env.Undefined();
562
+ if (!p->hasPoint) {
563
+ Napi::Error::New(env, "empty polygon").ThrowAsJavaScriptException();
564
+ return env.Undefined();
565
+ }
566
+ if (std::fabs(p->curX - p->startX) > 1e-9 || std::fabs(p->curY - p->startY) > 1e-9) {
567
+ PolyAppendLine(p, p->startX, p->startY, true);
568
+ }
569
+ gerbv_net_t* currentNet = LastNet(p->image);
570
+ currentNet = gerber_create_new_net(currentNet, p->layer, nullptr);
571
+ currentNet->interpolation = GERBV_INTERPOLATION_PAREA_END;
572
+ p->finished = true;
573
+ return env.Undefined();
574
+ }
575
+
576
+ void StrokeBezierSampled(
577
+ gerbv_image_t* image, double x1, double y1, double c1x, double c1y, double c2x,
578
+ double c2y, double x2, double y2, double w, bool cubic, int steps) {
579
+ const int n = std::max(4, steps);
580
+ double px = x1, py = y1;
581
+ for (int i = 1; i <= n; i++) {
582
+ const double t = static_cast<double>(i) / n;
583
+ const double u = 1.0 - t;
584
+ double x, y;
585
+ if (cubic) {
586
+ x = u * u * u * x1 + 3 * u * u * t * c1x + 3 * u * t * t * c2x + t * t * t * x2;
587
+ y = u * u * u * y1 + 3 * u * u * t * c1y + 3 * u * t * t * c2y + t * t * t * y2;
588
+ } else {
589
+ x = u * u * x1 + 2 * u * t * c1x + t * t * x2;
590
+ y = u * u * y1 + 2 * u * t * c1y + t * t * y2;
591
+ }
592
+ gerbv_image_create_line_object(image, px, py, x, y, w, GERBV_APTYPE_CIRCLE);
593
+ px = x;
594
+ py = y;
595
+ }
596
+ }
597
+
598
+ /** addBezier2(image, x1,y1, c1x,c1y, x2,y2, widthInch) — 二次描边 */
599
+ Napi::Value AddBezier2(const Napi::CallbackInfo& info) {
600
+ Napi::Env env = info.Env();
601
+ if (info.Length() < 8) {
602
+ Napi::TypeError::New(env, "addBezier2(image, x1,y1, c1x,c1y, x2,y2, widthInch)")
603
+ .ThrowAsJavaScriptException();
604
+ return env.Undefined();
605
+ }
606
+ auto* h = Unwrap(info[0]);
607
+ if (!h || !h->image) {
608
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
609
+ return env.Undefined();
610
+ }
611
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
612
+ const double c1x = ToInch(h, info[3].As<Napi::Number>().DoubleValue());
613
+ const double c1y = ToInch(h, info[4].As<Napi::Number>().DoubleValue());
614
+ StrokeBezierSampled(
615
+ h->image, ToInch(h, info[1].As<Napi::Number>().DoubleValue()),
616
+ ToInch(h, info[2].As<Napi::Number>().DoubleValue()), c1x, c1y, c1x, c1y,
617
+ ToInch(h, info[5].As<Napi::Number>().DoubleValue()),
618
+ ToInch(h, info[6].As<Napi::Number>().DoubleValue()),
619
+ ToInch(h, info[7].As<Napi::Number>().DoubleValue()), false, 16);
620
+ return env.Undefined();
621
+ }
622
+
623
+ /** addBezier3(image, x1,y1, c1x,c1y, c2x,c2y, x2,y2, width) — 三次描边 */
624
+ Napi::Value AddBezier3(const Napi::CallbackInfo& info) {
625
+ Napi::Env env = info.Env();
626
+ if (info.Length() < 10) {
627
+ Napi::TypeError::New(
628
+ env, "addBezier3(image, x1,y1, c1x,c1y, c2x,c2y, x2,y2, width)")
629
+ .ThrowAsJavaScriptException();
630
+ return env.Undefined();
631
+ }
632
+ auto* h = Unwrap(info[0]);
633
+ if (!h || !h->image) {
634
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
635
+ return env.Undefined();
636
+ }
637
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
638
+ StrokeBezierSampled(
639
+ h->image, ToInch(h, info[1].As<Napi::Number>().DoubleValue()),
640
+ ToInch(h, info[2].As<Napi::Number>().DoubleValue()),
641
+ ToInch(h, info[3].As<Napi::Number>().DoubleValue()),
642
+ ToInch(h, info[4].As<Napi::Number>().DoubleValue()),
643
+ ToInch(h, info[5].As<Napi::Number>().DoubleValue()),
644
+ ToInch(h, info[6].As<Napi::Number>().DoubleValue()),
645
+ ToInch(h, info[7].As<Napi::Number>().DoubleValue()),
646
+ ToInch(h, info[8].As<Napi::Number>().DoubleValue()),
647
+ ToInch(h, info[9].As<Napi::Number>().DoubleValue()), true, 20);
648
+ return env.Undefined();
649
+ }
650
+
111
651
  std::string ReadFileBytes(const char* path) {
112
652
  FILE* f = std::fopen(path, "rb");
113
653
  if (!f) return {};
@@ -178,8 +718,17 @@ Napi::Value ExportDrill(const Napi::CallbackInfo& info) {
178
718
  Napi::Object Init(Napi::Env env, Napi::Object exports) {
179
719
  exports.Set("createImage", Napi::Function::New(env, CreateImage));
180
720
  exports.Set("addLine", Napi::Function::New(env, AddLine));
721
+ exports.Set("addArc", Napi::Function::New(env, AddArc));
722
+ exports.Set("addBezier2", Napi::Function::New(env, AddBezier2));
723
+ exports.Set("addBezier3", Napi::Function::New(env, AddBezier3));
181
724
  exports.Set("addFlashCircle", Napi::Function::New(env, AddFlashCircle));
182
725
  exports.Set("addRect", Napi::Function::New(env, AddRect));
726
+ exports.Set("createPolygon", Napi::Function::New(env, CreatePolygon));
727
+ exports.Set("addPolygonLine", Napi::Function::New(env, AddPolygonLine));
728
+ exports.Set("addPolygonArc", Napi::Function::New(env, AddPolygonArc));
729
+ exports.Set("addPolygonBezier2", Napi::Function::New(env, AddPolygonBezier2));
730
+ exports.Set("addPolygonBezier3", Napi::Function::New(env, AddPolygonBezier3));
731
+ exports.Set("endPolygon", Napi::Function::New(env, EndPolygon));
183
732
  exports.Set("exportRs274x", Napi::Function::New(env, ExportRs274x));
184
733
  exports.Set("exportDrill", Napi::Function::New(env, ExportDrill));
185
734
  exports.Set("backend", Napi::String::New(env, "libgerbv"));