@huaqiu/js-gerbv 0.1.2 → 0.1.26

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.
Files changed (47) hide show
  1. package/CMakeLists.txt +16 -3
  2. package/README.md +100 -22
  3. package/examples/example.js +266 -0
  4. package/examples/example.py +30 -0
  5. package/index.js +618 -4
  6. package/lib/add-text.js +552 -0
  7. package/lib/fill-holes.js +648 -0
  8. package/lib/hershey-futural.json +1 -0
  9. package/lib/spline.js +211 -0
  10. package/lib/system-font.js +166 -0
  11. package/package.json +8 -3
  12. package/prebuilds/darwin-arm64/gerbv_node.node +0 -0
  13. package/prebuilds/darwin-arm64/libX11.6.dylib +0 -0
  14. package/prebuilds/darwin-arm64/libXau.6.dylib +0 -0
  15. package/prebuilds/darwin-arm64/libXdmcp.6.dylib +0 -0
  16. package/prebuilds/darwin-arm64/libXext.6.dylib +0 -0
  17. package/prebuilds/darwin-arm64/libXrender.1.dylib +0 -0
  18. package/prebuilds/darwin-arm64/libbrotlicommon.1.dylib +0 -0
  19. package/prebuilds/darwin-arm64/libbrotlidec.1.dylib +0 -0
  20. package/prebuilds/darwin-arm64/libcairo.2.dylib +0 -0
  21. package/prebuilds/darwin-arm64/libfontconfig.1.dylib +0 -0
  22. package/prebuilds/darwin-arm64/libfreetype.6.dylib +0 -0
  23. package/prebuilds/darwin-arm64/libpixman-1.0.dylib +0 -0
  24. package/prebuilds/darwin-arm64/libpng16.16.dylib +0 -0
  25. package/prebuilds/darwin-arm64/libxcb-render.0.dylib +0 -0
  26. package/prebuilds/darwin-arm64/libxcb-shm.0.dylib +0 -0
  27. package/prebuilds/darwin-arm64/libxcb.1.dylib +0 -0
  28. package/prebuilds/darwin-arm64/meta.json +1 -1
  29. package/prebuilds/win32-x64/gerbv_node.node +0 -0
  30. package/prebuilds/win32-x64/libbrotlicommon.dll +0 -0
  31. package/prebuilds/win32-x64/libbrotlidec.dll +0 -0
  32. package/prebuilds/win32-x64/libbz2-1.dll +0 -0
  33. package/prebuilds/win32-x64/libcairo-2.dll +0 -0
  34. package/prebuilds/win32-x64/libexpat-1.dll +0 -0
  35. package/prebuilds/win32-x64/libfontconfig-1.dll +0 -0
  36. package/prebuilds/win32-x64/libfreetype-6.dll +0 -0
  37. package/prebuilds/win32-x64/libgraphite2.dll +0 -0
  38. package/prebuilds/win32-x64/libharfbuzz-0.dll +0 -0
  39. package/prebuilds/win32-x64/libpixman-1-0.dll +0 -0
  40. package/prebuilds/win32-x64/libpng16-16.dll +0 -0
  41. package/prebuilds/win32-x64/meta.json +2 -2
  42. package/scripts/cmake-build.js +8 -0
  43. package/scripts/pack-prebuild.js +110 -4
  44. package/scripts/smoke.js +268 -6
  45. package/src/addon.cc +1086 -16
  46. package/src/export_png.cc +416 -0
  47. package/src/export_png.h +50 -0
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,141 @@
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 "export_png.h"
22
+
23
+ #include <algorithm>
24
+ #include <cctype>
25
+ #include <cmath>
16
26
  #include <cstdio>
27
+ #include <cstdlib>
17
28
  #include <filesystem>
18
29
  #include <string>
19
30
  #include <unistd.h>
31
+ #include <utility>
32
+ #include <vector>
20
33
 
21
34
  namespace {
22
35
 
23
36
  struct ImageHandle {
24
37
  gerbv_image_t* image = nullptr;
38
+ /** 用户单位 → inch 的乘数(inch=1,mm=1/25.4) */
39
+ double toInch = 1.0;
40
+ /** 换算到 inch 后保留的小数位;<0 表示不量化 */
41
+ int precision = -1;
42
+ GerbvPngRgba layerColor{0.85, 0.7, 0.1, 1};
43
+ GerbvPngRgba objectColor{0, 0, 0, 1};
44
+ bool hasObjectColor = false;
45
+ GerbvPngRgba netColor{0, 0, 0, 1};
46
+ bool hasNetColor = false;
47
+ /** 与 netlist 顺序对齐的前景色(绘制时:网络 > 对象 > 层) */
48
+ std::vector<GerbvPngRgba> netColors;
25
49
  };
26
50
 
51
+ size_t CountNets(gerbv_image_t* image) {
52
+ size_t n = 0;
53
+ for (gerbv_net_t* net = image ? image->netlist : nullptr; net; net = net->next) ++n;
54
+ return n;
55
+ }
56
+
57
+ /** 当前生效色:网络色 > 对象色 > 层色 */
58
+ GerbvPngRgba EffectiveColor(const ImageHandle* h) {
59
+ if (h->hasNetColor) return h->netColor;
60
+ if (h->hasObjectColor) return h->objectColor;
61
+ return h->layerColor;
62
+ }
63
+
64
+ /** 为新增 net 补齐颜色条目 */
65
+ void SyncNetColors(ImageHandle* h) {
66
+ if (!h || !h->image) return;
67
+ const size_t n = CountNets(h->image);
68
+ const GerbvPngRgba c = EffectiveColor(h);
69
+ while (h->netColors.size() < n) h->netColors.push_back(c);
70
+ }
71
+
72
+ bool ColorNear(const GerbvPngRgba& a, const GerbvPngRgba& b, double eps = 1e-4) {
73
+ return std::fabs(a.r - b.r) <= eps && std::fabs(a.g - b.g) <= eps &&
74
+ std::fabs(a.b - b.b) <= eps && std::fabs(a.a - b.a) <= eps;
75
+ }
76
+
77
+ /** 把仍等于旧层色的图元刷成新层色(保留已用对象/网络色 stamp 的条目) */
78
+ void RecolorLayerNets(ImageHandle* h, const GerbvPngRgba& oldLayer) {
79
+ if (!h) return;
80
+ for (auto& c : h->netColors) {
81
+ if (ColorNear(c, oldLayer)) c = h->layerColor;
82
+ }
83
+ SyncNetColors(h);
84
+ }
85
+
86
+ /** 导出时强制整层用同一前景色(忽略 stamp) */
87
+ std::vector<GerbvPngRgba> FlatNetColors(ImageHandle* h, const GerbvPngRgba& fg) {
88
+ const size_t n = CountNets(h ? h->image : nullptr);
89
+ return std::vector<GerbvPngRgba>(n, fg);
90
+ }
91
+
92
+ /** 用户坐标/长度 → libgerbv 内部 inch(可按 precision 量化) */
93
+ double ToInch(const ImageHandle* h, double v) {
94
+ double x = v * h->toInch;
95
+ if (h->precision >= 0) {
96
+ const int p = std::min(15, h->precision);
97
+ const double s = std::pow(10.0, p);
98
+ x = std::round(x * s) / s;
99
+ }
100
+ return x;
101
+ }
102
+
103
+ std::string LowerAscii(std::string s) {
104
+ for (char& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
105
+ return s;
106
+ }
107
+
108
+ gerbv_net_t* LastNet(gerbv_image_t* image) {
109
+ gerbv_net_t* n = image->netlist;
110
+ for (; n && n->next; n = n->next) {
111
+ }
112
+ return n;
113
+ }
114
+
115
+ /** 取最后一个有意义坐标(跳过 G36/G37 标记 net,它们常为 0,0)。 */
116
+ void LastPenPosition(gerbv_image_t* image, double* x, double* y) {
117
+ *x = 0;
118
+ *y = 0;
119
+ for (gerbv_net_t* n = image->netlist; n; n = n->next) {
120
+ if (n->interpolation == GERBV_INTERPOLATION_PAREA_START ||
121
+ n->interpolation == GERBV_INTERPOLATION_PAREA_END) {
122
+ continue;
123
+ }
124
+ *x = n->stop_x;
125
+ *y = n->stop_y;
126
+ }
127
+ }
128
+
129
+ /** 切换到指定 polarity 的 layer,并挂一个继承该 layer 的净空 net,供后续 create_* 继承。 */
130
+ gerbv_layer_t* EnsurePolarity(gerbv_image_t* image, gerbv_polarity_t pol) {
131
+ gerbv_net_t* current = LastNet(image);
132
+ if (!current || !current->layer) return nullptr;
133
+ if (current->layer->polarity == pol) return current->layer;
134
+ // 保持当前坐标,禁止跳到 (0,0)——否则导出 G01X0Y0D02 易在查看器留下伪缝
135
+ // PAREA_END 由 g_new0 产生,stop 常为 0,必须回看上一笔真实坐标
136
+ double x = 0;
137
+ double y = 0;
138
+ LastPenPosition(image, &x, &y);
139
+ gerbv_layer_t* layer = gerbv_image_return_new_layer(current->layer);
140
+ layer->polarity = pol;
141
+ current = gerber_create_new_net(current, layer, nullptr);
142
+ current->interpolation = GERBV_INTERPOLATION_LINEARx1;
143
+ current->aperture_state = GERBV_APERTURE_STATE_OFF;
144
+ current->start_x = current->stop_x = x;
145
+ current->start_y = current->stop_y = y;
146
+ return layer;
147
+ }
148
+
27
149
  void FreeImage(Napi::Env, ImageHandle* h) {
28
150
  if (h) {
29
151
  if (h->image) gerbv_destroy_image(h->image);
@@ -38,9 +160,37 @@ ImageHandle* Unwrap(const Napi::Value& v) {
38
160
  return v.As<ImageExt>().Data();
39
161
  }
40
162
 
163
+ /**
164
+ * createImage() | createImage({ unit?: 'inch'|'mm', precision?: number })
165
+ * unit:输入坐标单位(内部仍转成 inch 交给 libgerbv)
166
+ * precision:转到 inch 后保留的小数位(省略则不量化)
167
+ */
41
168
  Napi::Value CreateImage(const Napi::CallbackInfo& info) {
42
169
  Napi::Env env = info.Env();
43
170
  auto* h = new ImageHandle();
171
+ h->toInch = 1.0;
172
+ h->precision = -1;
173
+
174
+ if (info.Length() >= 1 && info[0].IsObject()) {
175
+ Napi::Object opts = info[0].As<Napi::Object>();
176
+ if (opts.Has("unit") && !opts.Get("unit").IsUndefined() && !opts.Get("unit").IsNull()) {
177
+ const std::string unit = LowerAscii(opts.Get("unit").ToString().Utf8Value());
178
+ if (unit == "mm" || unit == "metric") {
179
+ h->toInch = 1.0 / 25.4;
180
+ } else if (unit == "inch" || unit == "in" || unit == "imperial") {
181
+ h->toInch = 1.0;
182
+ } else {
183
+ delete h;
184
+ Napi::TypeError::New(env, "createImage: unit must be 'inch' or 'mm'")
185
+ .ThrowAsJavaScriptException();
186
+ return env.Null();
187
+ }
188
+ }
189
+ if (opts.Has("precision") && opts.Get("precision").IsNumber()) {
190
+ h->precision = opts.Get("precision").As<Napi::Number>().Int32Value();
191
+ }
192
+ }
193
+
44
194
  h->image = gerbv_create_image(nullptr, "hqpcb-lite");
45
195
  if (!h->image) {
46
196
  delete h;
@@ -53,7 +203,7 @@ Napi::Value CreateImage(const Napi::CallbackInfo& info) {
53
203
  Napi::Value AddLine(const Napi::CallbackInfo& info) {
54
204
  Napi::Env env = info.Env();
55
205
  if (info.Length() < 6) {
56
- Napi::TypeError::New(env, "addLine(image,x1,y1,x2,y2,widthInch)").ThrowAsJavaScriptException();
206
+ Napi::TypeError::New(env, "addLine(image,x1,y1,x2,y2,width)").ThrowAsJavaScriptException();
57
207
  return env.Undefined();
58
208
  }
59
209
  auto* h = Unwrap(info[0]);
@@ -61,19 +211,100 @@ Napi::Value AddLine(const Napi::CallbackInfo& info) {
61
211
  Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
62
212
  return env.Undefined();
63
213
  }
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();
214
+ const double x1 = ToInch(h, info[1].As<Napi::Number>().DoubleValue());
215
+ const double y1 = ToInch(h, info[2].As<Napi::Number>().DoubleValue());
216
+ const double x2 = ToInch(h, info[3].As<Napi::Number>().DoubleValue());
217
+ const double y2 = ToInch(h, info[4].As<Napi::Number>().DoubleValue());
218
+ const double w = ToInch(h, info[5].As<Napi::Number>().DoubleValue());
219
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
69
220
  gerbv_image_create_line_object(h->image, x1, y1, x2, y2, w, GERBV_APTYPE_CIRCLE);
221
+ SyncNetColors(h);
222
+ return env.Undefined();
223
+ }
224
+
225
+ /**
226
+ * 圆弧描边。addArc(image, cx, cy, x1, y1, x2, y2, width, clockwise?=false)
227
+ * 坐标单位由 createImage({ unit }) 决定;导出为 G02/G03。
228
+ */
229
+ Napi::Value AddArc(const Napi::CallbackInfo& info) {
230
+ Napi::Env env = info.Env();
231
+ if (info.Length() < 8) {
232
+ Napi::TypeError::New(
233
+ env, "addArc(image,cx,cy,x1,y1,x2,y2,width,clockwise?=false)")
234
+ .ThrowAsJavaScriptException();
235
+ return env.Undefined();
236
+ }
237
+ auto* h = Unwrap(info[0]);
238
+ if (!h || !h->image) {
239
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
240
+ return env.Undefined();
241
+ }
242
+ const double cx = ToInch(h, info[1].As<Napi::Number>().DoubleValue());
243
+ const double cy = ToInch(h, info[2].As<Napi::Number>().DoubleValue());
244
+ const double x1 = ToInch(h, info[3].As<Napi::Number>().DoubleValue());
245
+ const double y1 = ToInch(h, info[4].As<Napi::Number>().DoubleValue());
246
+ const double x2 = ToInch(h, info[5].As<Napi::Number>().DoubleValue());
247
+ const double y2 = ToInch(h, info[6].As<Napi::Number>().DoubleValue());
248
+ const double w = ToInch(h, info[7].As<Napi::Number>().DoubleValue());
249
+ const bool clockwise = info.Length() >= 9 && info[8].ToBoolean().Value();
250
+
251
+ const double r = std::hypot(x1 - cx, y1 - cy);
252
+ if (!(r > 1e-12) || !(w > 0)) return env.Undefined();
253
+
254
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
255
+
256
+ const double RAD2DEG = 180.0 / 3.14159265358979323846;
257
+ const double a0 = std::atan2(y1 - cy, x1 - cx) * RAD2DEG;
258
+ const double a1 = std::atan2(y2 - cy, x2 - cx) * RAD2DEG;
259
+
260
+ int apertureIndex = 0;
261
+ gerbv_net_t* currentNet =
262
+ gerb_image_return_aperture_index(h->image, w, &apertureIndex);
263
+ if (!currentNet) return env.Undefined();
264
+
265
+ currentNet = gerber_create_new_net(currentNet, currentNet->layer, nullptr);
266
+ currentNet->interpolation =
267
+ clockwise ? GERBV_INTERPOLATION_CW_CIRCULAR : GERBV_INTERPOLATION_CCW_CIRCULAR;
268
+ currentNet->aperture_state = GERBV_APERTURE_STATE_ON;
269
+ currentNet->aperture = apertureIndex;
270
+ currentNet->start_x = x1;
271
+ currentNet->start_y = y1;
272
+ currentNet->stop_x = x2;
273
+ currentNet->stop_y = y2;
274
+ currentNet->cirseg = g_new0(gerbv_cirseg_t, 1);
275
+ currentNet->cirseg->cp_x = cx;
276
+ currentNet->cirseg->cp_y = cy;
277
+ currentNet->cirseg->width = r;
278
+ currentNet->cirseg->height = r;
279
+
280
+ double angleDiff = a1 - a0;
281
+ if (clockwise) {
282
+ while (angleDiff > 0) angleDiff -= 360.0;
283
+ if (angleDiff == 0) angleDiff = -360.0;
284
+ } else {
285
+ while (angleDiff < 0) angleDiff += 360.0;
286
+ if (angleDiff == 0) angleDiff = 360.0;
287
+ }
288
+ // 整圆:angle2 = angle1 ± 360,避免栅格化把 start==end 当成零弧
289
+ currentNet->cirseg->angle1 = a0;
290
+ currentNet->cirseg->angle2 = a0 + angleDiff;
291
+ const int steps = std::max(1, static_cast<int>(std::fabs(angleDiff)));
292
+ for (int i = 0; i <= steps; i++) {
293
+ const double a = (a0 + (angleDiff * i) / steps) * (3.14159265358979323846 / 180.0);
294
+ const double tx = cx + r * std::cos(a);
295
+ const double ty = cy + r * std::sin(a);
296
+ gerber_update_min_and_max(
297
+ &currentNet->boundingBox, tx, ty, w / 2, w / 2, w / 2, w / 2);
298
+ }
299
+ gerber_update_image_min_max(&currentNet->boundingBox, 0, 0, h->image);
300
+ SyncNetColors(h);
70
301
  return env.Undefined();
71
302
  }
72
303
 
73
304
  Napi::Value AddFlashCircle(const Napi::CallbackInfo& info) {
74
305
  Napi::Env env = info.Env();
75
306
  if (info.Length() < 4) {
76
- Napi::TypeError::New(env, "addFlashCircle(image,x,y,diamInch)").ThrowAsJavaScriptException();
307
+ Napi::TypeError::New(env, "addFlashCircle(image,x,y,diam)").ThrowAsJavaScriptException();
77
308
  return env.Undefined();
78
309
  }
79
310
  auto* h = Unwrap(info[0]);
@@ -81,18 +312,20 @@ Napi::Value AddFlashCircle(const Napi::CallbackInfo& info) {
81
312
  Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
82
313
  return env.Undefined();
83
314
  }
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();
315
+ const double x = ToInch(h, info[1].As<Napi::Number>().DoubleValue());
316
+ const double y = ToInch(h, info[2].As<Napi::Number>().DoubleValue());
317
+ const double d = ToInch(h, info[3].As<Napi::Number>().DoubleValue());
318
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
87
319
  // 同点起止 → libgerbv 走 FLASH
88
320
  gerbv_image_create_line_object(h->image, x, y, x, y, d, GERBV_APTYPE_CIRCLE);
321
+ SyncNetColors(h);
89
322
  return env.Undefined();
90
323
  }
91
324
 
92
325
  Napi::Value AddRect(const Napi::CallbackInfo& info) {
93
326
  Napi::Env env = info.Env();
94
327
  if (info.Length() < 5) {
95
- Napi::TypeError::New(env, "addRect(image,x,y,widthInch,heightInch)").ThrowAsJavaScriptException();
328
+ Napi::TypeError::New(env, "addRect(image,x,y,width,height)").ThrowAsJavaScriptException();
96
329
  return env.Undefined();
97
330
  }
98
331
  auto* h = Unwrap(info[0]);
@@ -100,11 +333,452 @@ Napi::Value AddRect(const Napi::CallbackInfo& info) {
100
333
  Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
101
334
  return env.Undefined();
102
335
  }
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();
336
+ const double x = ToInch(h, info[1].As<Napi::Number>().DoubleValue());
337
+ const double y = ToInch(h, info[2].As<Napi::Number>().DoubleValue());
338
+ const double w = ToInch(h, info[3].As<Napi::Number>().DoubleValue());
339
+ const double ht = ToInch(h, info[4].As<Napi::Number>().DoubleValue());
340
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
107
341
  gerbv_image_create_rectangle_object(h->image, x, y, w, ht);
342
+ SyncNetColors(h);
343
+ return env.Undefined();
344
+ }
345
+
346
+ /** 区域多边形构建器(G36…G37)。贝塞尔在本侧采样为折线。 */
347
+ struct PolygonHandle {
348
+ ImageHandle* owner = nullptr;
349
+ gerbv_image_t* image = nullptr;
350
+ gerbv_layer_t* layer = nullptr;
351
+ double startX = 0;
352
+ double startY = 0;
353
+ double curX = 0;
354
+ double curY = 0;
355
+ bool hasPoint = false;
356
+ bool finished = false;
357
+ };
358
+
359
+ double ToInch(const PolygonHandle* p, double v) {
360
+ return p && p->owner ? ToInch(p->owner, v) : v;
361
+ }
362
+
363
+ void FreePolygon(Napi::Env, PolygonHandle* p) { delete p; }
364
+
365
+ using PolygonExt = Napi::External<PolygonHandle>;
366
+
367
+ PolygonHandle* UnwrapPoly(const Napi::Value& v) {
368
+ if (!v.IsExternal()) return nullptr;
369
+ return v.As<PolygonExt>().Data();
370
+ }
371
+
372
+ void PolyAppendLine(
373
+ PolygonHandle* p, double x2, double y2, bool penOn) {
374
+ gerbv_net_t* currentNet = LastNet(p->image);
375
+ currentNet = gerber_create_new_net(currentNet, p->layer, nullptr);
376
+ currentNet->interpolation = GERBV_INTERPOLATION_LINEARx1;
377
+ currentNet->aperture_state =
378
+ penOn ? GERBV_APERTURE_STATE_ON : GERBV_APERTURE_STATE_OFF;
379
+ currentNet->start_x = p->curX;
380
+ currentNet->start_y = p->curY;
381
+ currentNet->stop_x = x2;
382
+ currentNet->stop_y = y2;
383
+ if (penOn) {
384
+ gerber_update_min_and_max(
385
+ &currentNet->boundingBox, x2, y2, 0, 0, 0, 0);
386
+ gerber_update_image_min_max(&currentNet->boundingBox, 0, 0, p->image);
387
+ }
388
+ p->curX = x2;
389
+ p->curY = y2;
390
+ }
391
+
392
+ void PolyAppendArc(
393
+ PolygonHandle* p, double cx, double cy, double x2, double y2, bool clockwise) {
394
+ const double x1 = p->curX;
395
+ const double y1 = p->curY;
396
+ const double r = std::hypot(x1 - cx, y1 - cy);
397
+ if (!(r > 1e-12)) {
398
+ PolyAppendLine(p, x2, y2, true);
399
+ return;
400
+ }
401
+ const double RAD2DEG = 180.0 / 3.14159265358979323846;
402
+ const double a0 = std::atan2(y1 - cy, x1 - cx) * RAD2DEG;
403
+ const double a1 = std::atan2(y2 - cy, x2 - cx) * RAD2DEG;
404
+
405
+ gerbv_net_t* currentNet = LastNet(p->image);
406
+ currentNet = gerber_create_new_net(currentNet, p->layer, nullptr);
407
+ currentNet->interpolation =
408
+ clockwise ? GERBV_INTERPOLATION_CW_CIRCULAR : GERBV_INTERPOLATION_CCW_CIRCULAR;
409
+ currentNet->aperture_state = GERBV_APERTURE_STATE_ON;
410
+ currentNet->aperture = 0;
411
+ currentNet->start_x = x1;
412
+ currentNet->start_y = y1;
413
+ currentNet->stop_x = x2;
414
+ currentNet->stop_y = y2;
415
+ currentNet->cirseg = g_new0(gerbv_cirseg_t, 1);
416
+ currentNet->cirseg->cp_x = cx;
417
+ currentNet->cirseg->cp_y = cy;
418
+ currentNet->cirseg->width = r;
419
+ currentNet->cirseg->height = r;
420
+ currentNet->cirseg->angle1 = a0;
421
+ currentNet->cirseg->angle2 = a1;
422
+
423
+ double angleDiff = a1 - a0;
424
+ if (clockwise) {
425
+ while (angleDiff > 0) angleDiff -= 360.0;
426
+ if (angleDiff == 0) angleDiff = -360.0;
427
+ } else {
428
+ while (angleDiff < 0) angleDiff += 360.0;
429
+ if (angleDiff == 0) angleDiff = 360.0;
430
+ }
431
+ const int steps = std::max(1, static_cast<int>(std::fabs(angleDiff)));
432
+ for (int i = 0; i <= steps; i++) {
433
+ const double a = (a0 + (angleDiff * i) / steps) * (3.14159265358979323846 / 180.0);
434
+ gerber_update_min_and_max(
435
+ &currentNet->boundingBox, cx + r * std::cos(a), cy + r * std::sin(a), 0, 0, 0, 0);
436
+ }
437
+ gerber_update_image_min_max(&currentNet->boundingBox, 0, 0, p->image);
438
+ p->curX = x2;
439
+ p->curY = y2;
440
+ }
441
+
442
+ void PolyAppendBezierSampled(
443
+ PolygonHandle* p, double c1x, double c1y, double c2x, double c2y, double x2,
444
+ double y2, bool cubic, int steps) {
445
+ const double x0 = p->curX;
446
+ const double y0 = p->curY;
447
+ const int n = std::max(4, steps);
448
+ for (int i = 1; i <= n; i++) {
449
+ const double t = static_cast<double>(i) / n;
450
+ const double u = 1.0 - t;
451
+ double x, y;
452
+ if (cubic) {
453
+ x = u * u * u * x0 + 3 * u * u * t * c1x + 3 * u * t * t * c2x + t * t * t * x2;
454
+ y = u * u * u * y0 + 3 * u * u * t * c1y + 3 * u * t * t * c2y + t * t * t * y2;
455
+ } else {
456
+ x = u * u * x0 + 2 * u * t * c1x + t * t * x2;
457
+ y = u * u * y0 + 2 * u * t * c1y + t * t * y2;
458
+ }
459
+ PolyAppendLine(p, x, y, true);
460
+ }
461
+ }
462
+
463
+ bool PolyEnsureStart(PolygonHandle* p, double x, double y, Napi::Env env) {
464
+ if (p->finished) {
465
+ Napi::Error::New(env, "polygon already ended").ThrowAsJavaScriptException();
466
+ return false;
467
+ }
468
+ if (p->hasPoint) return true;
469
+ gerbv_net_t* currentNet = LastNet(p->image);
470
+ currentNet = gerber_create_new_net(currentNet, p->layer, nullptr);
471
+ currentNet->interpolation = GERBV_INTERPOLATION_PAREA_START;
472
+
473
+ p->startX = p->curX = x;
474
+ p->startY = p->curY = y;
475
+ p->hasPoint = true;
476
+
477
+ currentNet = gerber_create_new_net(LastNet(p->image), p->layer, nullptr);
478
+ currentNet->interpolation = GERBV_INTERPOLATION_LINEARx1;
479
+ currentNet->aperture_state = GERBV_APERTURE_STATE_OFF;
480
+ currentNet->start_x = x;
481
+ currentNet->start_y = y;
482
+ currentNet->stop_x = x;
483
+ currentNet->stop_y = y;
484
+ return true;
485
+ }
486
+
487
+ /**
488
+ * createPolygon(image, clear?=false) → polygon
489
+ */
490
+ Napi::Value CreatePolygon(const Napi::CallbackInfo& info) {
491
+ Napi::Env env = info.Env();
492
+ if (info.Length() < 1) {
493
+ Napi::TypeError::New(env, "createPolygon(image, clear?=false)")
494
+ .ThrowAsJavaScriptException();
495
+ return env.Null();
496
+ }
497
+ auto* h = Unwrap(info[0]);
498
+ if (!h || !h->image) {
499
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
500
+ return env.Null();
501
+ }
502
+ const bool clear = info.Length() >= 2 && info[1].ToBoolean().Value();
503
+ gerbv_layer_t* layer =
504
+ EnsurePolarity(h->image, clear ? GERBV_POLARITY_CLEAR : GERBV_POLARITY_DARK);
505
+ if (!layer) {
506
+ Napi::Error::New(env, "empty netlist").ThrowAsJavaScriptException();
507
+ return env.Null();
508
+ }
509
+ auto* poly = new PolygonHandle();
510
+ poly->owner = h;
511
+ poly->image = h->image;
512
+ poly->layer = layer;
513
+ return PolygonExt::New(env, poly, FreePolygon);
514
+ }
515
+
516
+ /** addPolygonLine(polygon, x, y) — 首点为起点,其后为直线边 */
517
+ Napi::Value AddPolygonLine(const Napi::CallbackInfo& info) {
518
+ Napi::Env env = info.Env();
519
+ if (info.Length() < 3) {
520
+ Napi::TypeError::New(env, "addPolygonLine(polygon, x, y)")
521
+ .ThrowAsJavaScriptException();
522
+ return env.Undefined();
523
+ }
524
+ auto* p = UnwrapPoly(info[0]);
525
+ if (!p || !p->image) {
526
+ Napi::Error::New(env, "invalid polygon").ThrowAsJavaScriptException();
527
+ return env.Undefined();
528
+ }
529
+ const double x = ToInch(p, info[1].As<Napi::Number>().DoubleValue());
530
+ const double y = ToInch(p, info[2].As<Napi::Number>().DoubleValue());
531
+ if (!p->hasPoint) {
532
+ if (!PolyEnsureStart(p, x, y, env)) return env.Undefined();
533
+ if (p->owner) SyncNetColors(p->owner);
534
+ return env.Undefined();
535
+ }
536
+ PolyAppendLine(p, x, y, true);
537
+ if (p->owner) SyncNetColors(p->owner);
538
+ return env.Undefined();
539
+ }
540
+
541
+ /** addPolygonArc(polygon, cx, cy, x, y, clockwise?=false) */
542
+ Napi::Value AddPolygonArc(const Napi::CallbackInfo& info) {
543
+ Napi::Env env = info.Env();
544
+ if (info.Length() < 5) {
545
+ Napi::TypeError::New(env, "addPolygonArc(polygon, cx, cy, x, y, clockwise?=false)")
546
+ .ThrowAsJavaScriptException();
547
+ return env.Undefined();
548
+ }
549
+ auto* p = UnwrapPoly(info[0]);
550
+ if (!p || !p->image) {
551
+ Napi::Error::New(env, "invalid polygon").ThrowAsJavaScriptException();
552
+ return env.Undefined();
553
+ }
554
+ if (!p->hasPoint) {
555
+ Napi::Error::New(env, "addPolygonArc needs a start point first")
556
+ .ThrowAsJavaScriptException();
557
+ return env.Undefined();
558
+ }
559
+ if (p->finished) {
560
+ Napi::Error::New(env, "polygon already ended").ThrowAsJavaScriptException();
561
+ return env.Undefined();
562
+ }
563
+ const double cx = ToInch(p, info[1].As<Napi::Number>().DoubleValue());
564
+ const double cy = ToInch(p, info[2].As<Napi::Number>().DoubleValue());
565
+ const double x = ToInch(p, info[3].As<Napi::Number>().DoubleValue());
566
+ const double y = ToInch(p, info[4].As<Napi::Number>().DoubleValue());
567
+ const bool cw = info.Length() >= 6 && info[5].ToBoolean().Value();
568
+ PolyAppendArc(p, cx, cy, x, y, cw);
569
+ if (p->owner) SyncNetColors(p->owner);
570
+ return env.Undefined();
571
+ }
572
+
573
+ bool PolyReadyForEdge(PolygonHandle* p, Napi::Env env, const char* api) {
574
+ if (!p || !p->image) {
575
+ Napi::Error::New(env, "invalid polygon").ThrowAsJavaScriptException();
576
+ return false;
577
+ }
578
+ if (!p->hasPoint) {
579
+ Napi::Error::New(env, std::string(api) + " needs a start point first")
580
+ .ThrowAsJavaScriptException();
581
+ return false;
582
+ }
583
+ if (p->finished) {
584
+ Napi::Error::New(env, "polygon already ended").ThrowAsJavaScriptException();
585
+ return false;
586
+ }
587
+ return true;
588
+ }
589
+
590
+ /** addPolygonBezier2(polygon, c1x, c1y, x, y) — 二次贝塞尔边(本侧采样) */
591
+ Napi::Value AddPolygonBezier2(const Napi::CallbackInfo& info) {
592
+ Napi::Env env = info.Env();
593
+ if (info.Length() < 5) {
594
+ Napi::TypeError::New(env, "addPolygonBezier2(polygon, c1x, c1y, x, y)")
595
+ .ThrowAsJavaScriptException();
596
+ return env.Undefined();
597
+ }
598
+ auto* p = UnwrapPoly(info[0]);
599
+ if (!PolyReadyForEdge(p, env, "addPolygonBezier2")) return env.Undefined();
600
+ const double c1x = ToInch(p, info[1].As<Napi::Number>().DoubleValue());
601
+ const double c1y = ToInch(p, info[2].As<Napi::Number>().DoubleValue());
602
+ const double x = ToInch(p, info[3].As<Napi::Number>().DoubleValue());
603
+ const double y = ToInch(p, info[4].As<Napi::Number>().DoubleValue());
604
+ PolyAppendBezierSampled(p, c1x, c1y, c1x, c1y, x, y, false, 16);
605
+ if (p->owner) SyncNetColors(p->owner);
606
+ return env.Undefined();
607
+ }
608
+
609
+ /** addPolygonBezier3(polygon, c1x, c1y, c2x, c2y, x, y) — 三次贝塞尔边(本侧采样) */
610
+ Napi::Value AddPolygonBezier3(const Napi::CallbackInfo& info) {
611
+ Napi::Env env = info.Env();
612
+ if (info.Length() < 7) {
613
+ Napi::TypeError::New(env, "addPolygonBezier3(polygon, c1x, c1y, c2x, c2y, x, y)")
614
+ .ThrowAsJavaScriptException();
615
+ return env.Undefined();
616
+ }
617
+ auto* p = UnwrapPoly(info[0]);
618
+ if (!PolyReadyForEdge(p, env, "addPolygonBezier3")) return env.Undefined();
619
+ const double c1x = ToInch(p, info[1].As<Napi::Number>().DoubleValue());
620
+ const double c1y = ToInch(p, info[2].As<Napi::Number>().DoubleValue());
621
+ const double c2x = ToInch(p, info[3].As<Napi::Number>().DoubleValue());
622
+ const double c2y = ToInch(p, info[4].As<Napi::Number>().DoubleValue());
623
+ const double x = ToInch(p, info[5].As<Napi::Number>().DoubleValue());
624
+ const double y = ToInch(p, info[6].As<Napi::Number>().DoubleValue());
625
+ PolyAppendBezierSampled(p, c1x, c1y, c2x, c2y, x, y, true, 20);
626
+ if (p->owner) SyncNetColors(p->owner);
627
+ return env.Undefined();
628
+ }
629
+
630
+ /**
631
+ * addPolygonContour(polygon, x, y) — 同一 G36 内开启新轮廓(D02 跳刀)。
632
+ * RS-274X even-odd:外环+内环同区即可挖孔,无需 %LPC*%。
633
+ */
634
+ Napi::Value AddPolygonContour(const Napi::CallbackInfo& info) {
635
+ Napi::Env env = info.Env();
636
+ if (info.Length() < 3) {
637
+ Napi::TypeError::New(env, "addPolygonContour(polygon, x, y)")
638
+ .ThrowAsJavaScriptException();
639
+ return env.Undefined();
640
+ }
641
+ auto* p = UnwrapPoly(info[0]);
642
+ if (!p || !p->image) {
643
+ Napi::Error::New(env, "invalid polygon").ThrowAsJavaScriptException();
644
+ return env.Undefined();
645
+ }
646
+ if (p->finished) {
647
+ Napi::Error::New(env, "polygon already ended").ThrowAsJavaScriptException();
648
+ return env.Undefined();
649
+ }
650
+ const double x = ToInch(p, info[1].As<Napi::Number>().DoubleValue());
651
+ const double y = ToInch(p, info[2].As<Napi::Number>().DoubleValue());
652
+ if (!p->hasPoint) {
653
+ if (!PolyEnsureStart(p, x, y, env)) return env.Undefined();
654
+ if (p->owner) SyncNetColors(p->owner);
655
+ return env.Undefined();
656
+ }
657
+ // 先闭合当前轮廓
658
+ if (std::fabs(p->curX - p->startX) > 1e-9 || std::fabs(p->curY - p->startY) > 1e-9) {
659
+ PolyAppendLine(p, p->startX, p->startY, true);
660
+ }
661
+ gerbv_net_t* currentNet = LastNet(p->image);
662
+ currentNet = gerber_create_new_net(currentNet, p->layer, nullptr);
663
+ currentNet->interpolation = GERBV_INTERPOLATION_LINEARx1;
664
+ currentNet->aperture_state = GERBV_APERTURE_STATE_OFF;
665
+ currentNet->start_x = p->curX;
666
+ currentNet->start_y = p->curY;
667
+ currentNet->stop_x = x;
668
+ currentNet->stop_y = y;
669
+ p->startX = p->curX = x;
670
+ p->startY = p->curY = y;
671
+ if (p->owner) SyncNetColors(p->owner);
672
+ return env.Undefined();
673
+ }
674
+
675
+ /** endPolygon(polygon) — 自动闭合并写 G37 */
676
+ Napi::Value EndPolygon(const Napi::CallbackInfo& info) {
677
+ Napi::Env env = info.Env();
678
+ if (info.Length() < 1) {
679
+ Napi::TypeError::New(env, "endPolygon(polygon)").ThrowAsJavaScriptException();
680
+ return env.Undefined();
681
+ }
682
+ auto* p = UnwrapPoly(info[0]);
683
+ if (!p || !p->image) {
684
+ Napi::Error::New(env, "invalid polygon").ThrowAsJavaScriptException();
685
+ return env.Undefined();
686
+ }
687
+ if (p->finished) return env.Undefined();
688
+ if (!p->hasPoint) {
689
+ Napi::Error::New(env, "empty polygon").ThrowAsJavaScriptException();
690
+ return env.Undefined();
691
+ }
692
+ if (std::fabs(p->curX - p->startX) > 1e-9 || std::fabs(p->curY - p->startY) > 1e-9) {
693
+ PolyAppendLine(p, p->startX, p->startY, true);
694
+ }
695
+ gerbv_net_t* currentNet = LastNet(p->image);
696
+ const double endX = currentNet ? currentNet->stop_x : p->curX;
697
+ const double endY = currentNet ? currentNet->stop_y : p->curY;
698
+ currentNet = gerber_create_new_net(currentNet, p->layer, nullptr);
699
+ currentNet->interpolation = GERBV_INTERPOLATION_PAREA_END;
700
+ // 继承闭合点,避免后续 EnsurePolarity 读到 (0,0)
701
+ currentNet->start_x = currentNet->stop_x = endX;
702
+ currentNet->start_y = currentNet->stop_y = endY;
703
+ p->finished = true;
704
+ if (p->owner) SyncNetColors(p->owner);
705
+ return env.Undefined();
706
+ }
707
+
708
+ void StrokeBezierSampled(
709
+ gerbv_image_t* image, double x1, double y1, double c1x, double c1y, double c2x,
710
+ double c2y, double x2, double y2, double w, bool cubic, int steps) {
711
+ const int n = std::max(4, steps);
712
+ double px = x1, py = y1;
713
+ for (int i = 1; i <= n; i++) {
714
+ const double t = static_cast<double>(i) / n;
715
+ const double u = 1.0 - t;
716
+ double x, y;
717
+ if (cubic) {
718
+ x = u * u * u * x1 + 3 * u * u * t * c1x + 3 * u * t * t * c2x + t * t * t * x2;
719
+ y = u * u * u * y1 + 3 * u * u * t * c1y + 3 * u * t * t * c2y + t * t * t * y2;
720
+ } else {
721
+ x = u * u * x1 + 2 * u * t * c1x + t * t * x2;
722
+ y = u * u * y1 + 2 * u * t * c1y + t * t * y2;
723
+ }
724
+ gerbv_image_create_line_object(image, px, py, x, y, w, GERBV_APTYPE_CIRCLE);
725
+ px = x;
726
+ py = y;
727
+ }
728
+ }
729
+
730
+ /** addBezier2(image, x1,y1, c1x,c1y, x2,y2, widthInch) — 二次描边 */
731
+ Napi::Value AddBezier2(const Napi::CallbackInfo& info) {
732
+ Napi::Env env = info.Env();
733
+ if (info.Length() < 8) {
734
+ Napi::TypeError::New(env, "addBezier2(image, x1,y1, c1x,c1y, x2,y2, widthInch)")
735
+ .ThrowAsJavaScriptException();
736
+ return env.Undefined();
737
+ }
738
+ auto* h = Unwrap(info[0]);
739
+ if (!h || !h->image) {
740
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
741
+ return env.Undefined();
742
+ }
743
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
744
+ const double c1x = ToInch(h, info[3].As<Napi::Number>().DoubleValue());
745
+ const double c1y = ToInch(h, info[4].As<Napi::Number>().DoubleValue());
746
+ StrokeBezierSampled(
747
+ h->image, ToInch(h, info[1].As<Napi::Number>().DoubleValue()),
748
+ ToInch(h, info[2].As<Napi::Number>().DoubleValue()), c1x, c1y, c1x, c1y,
749
+ ToInch(h, info[5].As<Napi::Number>().DoubleValue()),
750
+ ToInch(h, info[6].As<Napi::Number>().DoubleValue()),
751
+ ToInch(h, info[7].As<Napi::Number>().DoubleValue()), false, 16);
752
+ SyncNetColors(h);
753
+ return env.Undefined();
754
+ }
755
+
756
+ /** addBezier3(image, x1,y1, c1x,c1y, c2x,c2y, x2,y2, width) — 三次描边 */
757
+ Napi::Value AddBezier3(const Napi::CallbackInfo& info) {
758
+ Napi::Env env = info.Env();
759
+ if (info.Length() < 10) {
760
+ Napi::TypeError::New(
761
+ env, "addBezier3(image, x1,y1, c1x,c1y, c2x,c2y, x2,y2, width)")
762
+ .ThrowAsJavaScriptException();
763
+ return env.Undefined();
764
+ }
765
+ auto* h = Unwrap(info[0]);
766
+ if (!h || !h->image) {
767
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
768
+ return env.Undefined();
769
+ }
770
+ EnsurePolarity(h->image, GERBV_POLARITY_DARK);
771
+ StrokeBezierSampled(
772
+ h->image, ToInch(h, info[1].As<Napi::Number>().DoubleValue()),
773
+ ToInch(h, info[2].As<Napi::Number>().DoubleValue()),
774
+ ToInch(h, info[3].As<Napi::Number>().DoubleValue()),
775
+ ToInch(h, info[4].As<Napi::Number>().DoubleValue()),
776
+ ToInch(h, info[5].As<Napi::Number>().DoubleValue()),
777
+ ToInch(h, info[6].As<Napi::Number>().DoubleValue()),
778
+ ToInch(h, info[7].As<Napi::Number>().DoubleValue()),
779
+ ToInch(h, info[8].As<Napi::Number>().DoubleValue()),
780
+ ToInch(h, info[9].As<Napi::Number>().DoubleValue()), true, 20);
781
+ SyncNetColors(h);
108
782
  return env.Undefined();
109
783
  }
110
784
 
@@ -175,13 +849,409 @@ Napi::Value ExportDrill(const Napi::CallbackInfo& info) {
175
849
  return Napi::Buffer<char>::Copy(env, data.data(), data.size());
176
850
  }
177
851
 
852
+ int HexNibble(char c) {
853
+ if (c >= '0' && c <= '9') return c - '0';
854
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
855
+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
856
+ return -1;
857
+ }
858
+
859
+ bool ParseHexColor(const std::string& sIn, GerbvPngRgba* out) {
860
+ std::string s = sIn;
861
+ if (!s.empty() && s[0] == '#') s = s.substr(1);
862
+ auto byteAt = [&](size_t i) -> int {
863
+ if (i + 1 >= s.size()) return -1;
864
+ const int hi = HexNibble(s[i]);
865
+ const int lo = HexNibble(s[i + 1]);
866
+ if (hi < 0 || lo < 0) return -1;
867
+ return (hi << 4) | lo;
868
+ };
869
+ if (s.size() == 3 || s.size() == 4) {
870
+ // #rgb / #rgba
871
+ int r = HexNibble(s[0]);
872
+ int g = HexNibble(s[1]);
873
+ int b = HexNibble(s[2]);
874
+ if (r < 0 || g < 0 || b < 0) return false;
875
+ out->r = (r * 16 + r) / 255.0;
876
+ out->g = (g * 16 + g) / 255.0;
877
+ out->b = (b * 16 + b) / 255.0;
878
+ out->a = 1.0;
879
+ if (s.size() == 4) {
880
+ int a = HexNibble(s[3]);
881
+ if (a < 0) return false;
882
+ out->a = (a * 16 + a) / 255.0;
883
+ }
884
+ return true;
885
+ }
886
+ if (s.size() == 6 || s.size() == 8) {
887
+ const int r = byteAt(0);
888
+ const int g = byteAt(2);
889
+ const int b = byteAt(4);
890
+ if (r < 0 || g < 0 || b < 0) return false;
891
+ out->r = r / 255.0;
892
+ out->g = g / 255.0;
893
+ out->b = b / 255.0;
894
+ out->a = 1.0;
895
+ if (s.size() == 8) {
896
+ const int a = byteAt(6);
897
+ if (a < 0) return false;
898
+ out->a = a / 255.0;
899
+ }
900
+ return true;
901
+ }
902
+ return false;
903
+ }
904
+
905
+ bool ParseColorValue(const Napi::Value& v, GerbvPngRgba* out, std::string* err) {
906
+ if (v.IsString()) {
907
+ const std::string s = LowerAscii(v.ToString().Utf8Value());
908
+ if (s == "black") {
909
+ *out = {0, 0, 0, 1};
910
+ return true;
911
+ }
912
+ if (s == "white") {
913
+ *out = {1, 1, 1, 1};
914
+ return true;
915
+ }
916
+ if (s == "transparent" || s == "none") {
917
+ *out = {0, 0, 0, 0};
918
+ return true;
919
+ }
920
+ if (!ParseHexColor(v.ToString().Utf8Value(), out)) {
921
+ if (err) *err = "invalid color string (use #RRGGBB / #RRGGBBAA)";
922
+ return false;
923
+ }
924
+ return true;
925
+ }
926
+ if (v.IsObject()) {
927
+ Napi::Object o = v.As<Napi::Object>();
928
+ auto num = [&](const char* k, double defV) {
929
+ if (!o.Has(k) || !o.Get(k).IsNumber()) return defV;
930
+ return o.Get(k).As<Napi::Number>().DoubleValue();
931
+ };
932
+ double r = num("r", 0), g = num("g", 0), b = num("b", 0), a = num("a", 1);
933
+ // 若任一通道 >1,按 0..255 理解
934
+ if (r > 1 || g > 1 || b > 1 || a > 1) {
935
+ r /= 255.0;
936
+ g /= 255.0;
937
+ b /= 255.0;
938
+ if (a > 1) a /= 255.0;
939
+ }
940
+ *out = {r, g, b, a};
941
+ return true;
942
+ }
943
+ if (err) *err = "color must be string or {r,g,b,a}";
944
+ return false;
945
+ }
946
+
947
+ /** setLayerColor(image, color) — 层默认色;并刷新仍为旧层色的已有图元 */
948
+ Napi::Value SetLayerColor(const Napi::CallbackInfo& info) {
949
+ Napi::Env env = info.Env();
950
+ if (info.Length() < 2) {
951
+ Napi::TypeError::New(env, "setLayerColor(image, color)").ThrowAsJavaScriptException();
952
+ return env.Undefined();
953
+ }
954
+ auto* h = Unwrap(info[0]);
955
+ if (!h) {
956
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
957
+ return env.Undefined();
958
+ }
959
+ const GerbvPngRgba oldLayer = h->layerColor;
960
+ std::string cerr;
961
+ if (!ParseColorValue(info[1], &h->layerColor, &cerr)) {
962
+ Napi::TypeError::New(env, "setLayerColor: " + cerr).ThrowAsJavaScriptException();
963
+ return env.Undefined();
964
+ }
965
+ RecolorLayerNets(h, oldLayer);
966
+ return env.Undefined();
967
+ }
968
+
969
+ /** setObjectColor(image, color|null) — 对象色(次于网络、高于层);null 清除 */
970
+ Napi::Value SetObjectColor(const Napi::CallbackInfo& info) {
971
+ Napi::Env env = info.Env();
972
+ if (info.Length() < 2) {
973
+ Napi::TypeError::New(env, "setObjectColor(image, color|null)")
974
+ .ThrowAsJavaScriptException();
975
+ return env.Undefined();
976
+ }
977
+ auto* h = Unwrap(info[0]);
978
+ if (!h) {
979
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
980
+ return env.Undefined();
981
+ }
982
+ if (info[1].IsNull() || info[1].IsUndefined()) {
983
+ h->hasObjectColor = false;
984
+ return env.Undefined();
985
+ }
986
+ std::string cerr;
987
+ if (!ParseColorValue(info[1], &h->objectColor, &cerr)) {
988
+ Napi::TypeError::New(env, "setObjectColor: " + cerr).ThrowAsJavaScriptException();
989
+ return env.Undefined();
990
+ }
991
+ h->hasObjectColor = true;
992
+ return env.Undefined();
993
+ }
994
+
995
+ /** setNetColor(image, color|null) — 网络色(最高优先);null 清除 */
996
+ Napi::Value SetNetColor(const Napi::CallbackInfo& info) {
997
+ Napi::Env env = info.Env();
998
+ if (info.Length() < 2) {
999
+ Napi::TypeError::New(env, "setNetColor(image, color|null)")
1000
+ .ThrowAsJavaScriptException();
1001
+ return env.Undefined();
1002
+ }
1003
+ auto* h = Unwrap(info[0]);
1004
+ if (!h) {
1005
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
1006
+ return env.Undefined();
1007
+ }
1008
+ if (info[1].IsNull() || info[1].IsUndefined()) {
1009
+ h->hasNetColor = false;
1010
+ return env.Undefined();
1011
+ }
1012
+ std::string cerr;
1013
+ if (!ParseColorValue(info[1], &h->netColor, &cerr)) {
1014
+ Napi::TypeError::New(env, "setNetColor: " + cerr).ThrowAsJavaScriptException();
1015
+ return env.Undefined();
1016
+ }
1017
+ h->hasNetColor = true;
1018
+ return env.Undefined();
1019
+ }
1020
+
1021
+ bool ParsePngOpts(
1022
+ Napi::Env env, const Napi::Object& opts, ImageHandle* unitRef, GerbvPngExportOpts* out,
1023
+ std::string* err
1024
+ ) {
1025
+ if (opts.Has("width") && opts.Get("width").IsNumber())
1026
+ out->widthPx = opts.Get("width").As<Napi::Number>().Int32Value();
1027
+ if (opts.Has("height") && opts.Get("height").IsNumber())
1028
+ out->heightPx = opts.Get("height").As<Napi::Number>().Int32Value();
1029
+ if (opts.Has("padding") && opts.Get("padding").IsNumber())
1030
+ out->paddingPx = opts.Get("padding").As<Napi::Number>().Int32Value();
1031
+ if (opts.Has("dpi") && opts.Get("dpi").IsNumber())
1032
+ out->dpi = opts.Get("dpi").As<Napi::Number>().DoubleValue();
1033
+
1034
+ if (opts.Has("background") && !opts.Get("background").IsUndefined()) {
1035
+ if (!ParseColorValue(opts.Get("background"), &out->background, err)) return false;
1036
+ }
1037
+ if (opts.Has("region") && opts.Get("region").IsObject()) {
1038
+ Napi::Object r = opts.Get("region").As<Napi::Object>();
1039
+ auto num = [&](const char* k, bool* ok) -> double {
1040
+ if (!r.Has(k) || !r.Get(k).IsNumber()) {
1041
+ *ok = false;
1042
+ return 0;
1043
+ }
1044
+ *ok = true;
1045
+ return r.Get(k).As<Napi::Number>().DoubleValue();
1046
+ };
1047
+ bool ok = true;
1048
+ double minX, minY, maxX, maxY;
1049
+ if (r.Has("minX")) {
1050
+ minX = num("minX", &ok);
1051
+ minY = num("minY", &ok);
1052
+ maxX = num("maxX", &ok);
1053
+ maxY = num("maxY", &ok);
1054
+ } else if (r.Has("x")) {
1055
+ const double x = num("x", &ok);
1056
+ const double y = num("y", &ok);
1057
+ const double w = num("width", &ok);
1058
+ const double h = num("height", &ok);
1059
+ minX = x;
1060
+ minY = y;
1061
+ maxX = x + w;
1062
+ maxY = y + h;
1063
+ } else {
1064
+ if (err) *err = "region needs minX..maxY or x,y,width,height";
1065
+ return false;
1066
+ }
1067
+ if (!ok) {
1068
+ if (err) *err = "region fields must be numbers";
1069
+ return false;
1070
+ }
1071
+ const double s = unitRef ? unitRef->toInch : 1.0;
1072
+ // 若有 precision,区域也量化
1073
+ auto conv = [&](double v) {
1074
+ double x = v * s;
1075
+ if (unitRef && unitRef->precision >= 0) {
1076
+ const double p = std::pow(10.0, std::min(15, unitRef->precision));
1077
+ x = std::round(x * p) / p;
1078
+ }
1079
+ return x;
1080
+ };
1081
+ out->region.enabled = true;
1082
+ out->region.minX = conv(minX);
1083
+ out->region.minY = conv(minY);
1084
+ out->region.maxX = conv(maxX);
1085
+ out->region.maxY = conv(maxY);
1086
+ }
1087
+ (void)env;
1088
+ return true;
1089
+ }
1090
+
1091
+ GerbvPngLayerInput LayerFromHandle(
1092
+ ImageHandle* h, const GerbvPngRgba& fallbackFg, int zIndex = 0
1093
+ ) {
1094
+ GerbvPngLayerInput layer;
1095
+ layer.image = h->image;
1096
+ layer.defaultFg = fallbackFg.a > 0 || fallbackFg.r + fallbackFg.g + fallbackFg.b > 0
1097
+ ? fallbackFg
1098
+ : h->layerColor;
1099
+ layer.zIndex = zIndex;
1100
+ if (!h->netColors.empty()) {
1101
+ layer.netColors = h->netColors.data();
1102
+ layer.netColorCount = h->netColors.size();
1103
+ }
1104
+ return layer;
1105
+ }
1106
+
1107
+ /**
1108
+ * exportPng(image, opts?)
1109
+ * opts: width/height/padding/background/foreground/dpi/region
1110
+ */
1111
+ Napi::Value ExportPng(const Napi::CallbackInfo& info) {
1112
+ Napi::Env env = info.Env();
1113
+ if (info.Length() < 1) {
1114
+ Napi::TypeError::New(env, "exportPng(image, opts?)").ThrowAsJavaScriptException();
1115
+ return env.Null();
1116
+ }
1117
+ auto* h = Unwrap(info[0]);
1118
+ if (!h || !h->image) {
1119
+ Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
1120
+ return env.Null();
1121
+ }
1122
+
1123
+ GerbvPngExportOpts opts;
1124
+ opts.widthPx = 800;
1125
+ GerbvPngRgba fg = h->layerColor;
1126
+ bool forceFg = false;
1127
+ if (info.Length() >= 2 && info[1].IsObject()) {
1128
+ Napi::Object o = info[1].As<Napi::Object>();
1129
+ std::string cerr;
1130
+ if (!ParsePngOpts(env, o, h, &opts, &cerr)) {
1131
+ Napi::TypeError::New(env, "exportPng: " + cerr).ThrowAsJavaScriptException();
1132
+ return env.Null();
1133
+ }
1134
+ if (o.Has("foreground") && !o.Get("foreground").IsUndefined()) {
1135
+ if (!ParseColorValue(o.Get("foreground"), &fg, &cerr)) {
1136
+ Napi::TypeError::New(env, "exportPng foreground: " + cerr)
1137
+ .ThrowAsJavaScriptException();
1138
+ return env.Null();
1139
+ }
1140
+ forceFg = true;
1141
+ }
1142
+ }
1143
+
1144
+ // flat 必须活到 Render 结束(指针指向其 data)
1145
+ std::vector<GerbvPngRgba> flat;
1146
+ GerbvPngLayerInput layer = LayerFromHandle(h, fg);
1147
+ if (forceFg) {
1148
+ flat = FlatNetColors(h, fg);
1149
+ layer.defaultFg = fg;
1150
+ layer.netColors = flat.empty() ? nullptr : flat.data();
1151
+ layer.netColorCount = flat.size();
1152
+ }
1153
+ std::vector<GerbvPngLayerInput> layers;
1154
+ layers.push_back(layer);
1155
+
1156
+ std::string png;
1157
+ std::string err;
1158
+ if (!GerbvRenderLayersToPng(layers, opts, &png, &err)) {
1159
+ Napi::Error::New(env, "exportPng failed: " + err).ThrowAsJavaScriptException();
1160
+ return env.Null();
1161
+ }
1162
+ return Napi::Buffer<char>::Copy(env, png.data(), png.size());
1163
+ }
1164
+
1165
+ /**
1166
+ * exportPngLayers([image|{image,zIndex}, ...], opts?)
1167
+ * 多层叠加;按 zIndex 升序绘制(低在下);共用 region/dpi/background。
1168
+ */
1169
+ Napi::Value ExportPngLayers(const Napi::CallbackInfo& info) {
1170
+ Napi::Env env = info.Env();
1171
+ if (info.Length() < 1 || !info[0].IsArray()) {
1172
+ Napi::TypeError::New(env, "exportPngLayers([image|{image,zIndex},...], opts?)")
1173
+ .ThrowAsJavaScriptException();
1174
+ return env.Null();
1175
+ }
1176
+ Napi::Array arr = info[0].As<Napi::Array>();
1177
+ if (arr.Length() == 0) {
1178
+ Napi::Error::New(env, "exportPngLayers: empty layers").ThrowAsJavaScriptException();
1179
+ return env.Null();
1180
+ }
1181
+
1182
+ ImageHandle* unitRef = nullptr;
1183
+ std::vector<GerbvPngLayerInput> layers;
1184
+ layers.reserve(arr.Length());
1185
+ for (uint32_t i = 0; i < arr.Length(); i++) {
1186
+ Napi::Value item = arr.Get(i);
1187
+ ImageHandle* h = nullptr;
1188
+ int zIndex = static_cast<int>(i);
1189
+ if (item.IsObject()) {
1190
+ Napi::Object o = item.As<Napi::Object>();
1191
+ if (o.Has("image")) {
1192
+ h = Unwrap(o.Get("image"));
1193
+ if (o.Has("zIndex") && o.Get("zIndex").IsNumber())
1194
+ zIndex = o.Get("zIndex").As<Napi::Number>().Int32Value();
1195
+ } else if (o.Has("zIndex") && o.Get("zIndex").IsNumber()) {
1196
+ // 允许直接传带 __handle + zIndex 的层对象
1197
+ h = Unwrap(item);
1198
+ zIndex = o.Get("zIndex").As<Napi::Number>().Int32Value();
1199
+ } else {
1200
+ h = Unwrap(item);
1201
+ }
1202
+ } else {
1203
+ h = Unwrap(item);
1204
+ }
1205
+ if (!h || !h->image) {
1206
+ Napi::Error::New(env, "exportPngLayers: invalid image in layers")
1207
+ .ThrowAsJavaScriptException();
1208
+ return env.Null();
1209
+ }
1210
+ if (!unitRef) unitRef = h;
1211
+ layers.push_back(LayerFromHandle(h, h->layerColor, zIndex));
1212
+ }
1213
+
1214
+ GerbvPngExportOpts opts;
1215
+ opts.widthPx = 800;
1216
+ if (info.Length() >= 2 && info[1].IsObject()) {
1217
+ std::string cerr;
1218
+ if (!ParsePngOpts(env, info[1].As<Napi::Object>(), unitRef, &opts, &cerr)) {
1219
+ Napi::TypeError::New(env, "exportPngLayers: " + cerr).ThrowAsJavaScriptException();
1220
+ return env.Null();
1221
+ }
1222
+ }
1223
+
1224
+ std::string png;
1225
+ std::string err;
1226
+ if (!GerbvRenderLayersToPng(layers, opts, &png, &err)) {
1227
+ Napi::Error::New(env, "exportPngLayers failed: " + err).ThrowAsJavaScriptException();
1228
+ return env.Null();
1229
+ }
1230
+ return Napi::Buffer<char>::Copy(env, png.data(), png.size());
1231
+ }
1232
+
178
1233
  Napi::Object Init(Napi::Env env, Napi::Object exports) {
179
1234
  exports.Set("createImage", Napi::Function::New(env, CreateImage));
180
1235
  exports.Set("addLine", Napi::Function::New(env, AddLine));
1236
+ exports.Set("addArc", Napi::Function::New(env, AddArc));
1237
+ exports.Set("addBezier2", Napi::Function::New(env, AddBezier2));
1238
+ exports.Set("addBezier3", Napi::Function::New(env, AddBezier3));
181
1239
  exports.Set("addFlashCircle", Napi::Function::New(env, AddFlashCircle));
182
1240
  exports.Set("addRect", Napi::Function::New(env, AddRect));
1241
+ exports.Set("createPolygon", Napi::Function::New(env, CreatePolygon));
1242
+ exports.Set("addPolygonLine", Napi::Function::New(env, AddPolygonLine));
1243
+ exports.Set("addPolygonArc", Napi::Function::New(env, AddPolygonArc));
1244
+ exports.Set("addPolygonBezier2", Napi::Function::New(env, AddPolygonBezier2));
1245
+ exports.Set("addPolygonBezier3", Napi::Function::New(env, AddPolygonBezier3));
1246
+ exports.Set("addPolygonContour", Napi::Function::New(env, AddPolygonContour));
1247
+ exports.Set("endPolygon", Napi::Function::New(env, EndPolygon));
1248
+ exports.Set("setLayerColor", Napi::Function::New(env, SetLayerColor));
1249
+ exports.Set("setObjectColor", Napi::Function::New(env, SetObjectColor));
1250
+ exports.Set("setNetColor", Napi::Function::New(env, SetNetColor));
183
1251
  exports.Set("exportRs274x", Napi::Function::New(env, ExportRs274x));
184
1252
  exports.Set("exportDrill", Napi::Function::New(env, ExportDrill));
1253
+ exports.Set("exportPng", Napi::Function::New(env, ExportPng));
1254
+ exports.Set("exportPngLayers", Napi::Function::New(env, ExportPngLayers));
185
1255
  exports.Set("backend", Napi::String::New(env, "libgerbv"));
186
1256
  return exports;
187
1257
  }