@huaqiu/js-gerbv 0.1.15 → 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 +50 -6
  3. package/examples/example.js +266 -0
  4. package/examples/example.py +30 -0
  5. package/index.js +292 -19
  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 +1 -1
  42. package/scripts/cmake-build.js +8 -0
  43. package/scripts/pack-prebuild.js +110 -4
  44. package/scripts/smoke.js +216 -3
  45. package/src/addon.cc +525 -4
  46. package/src/export_png.cc +416 -0
  47. package/src/export_png.h +50 -0
package/src/addon.cc CHANGED
@@ -18,14 +18,18 @@ gerbv_net_t* gerb_image_return_aperture_index(
18
18
  gerbv_image_t* image, gdouble lineWidth, int* apertureIndex);
19
19
  }
20
20
 
21
+ #include "export_png.h"
22
+
21
23
  #include <algorithm>
22
24
  #include <cctype>
23
25
  #include <cmath>
24
26
  #include <cstdio>
27
+ #include <cstdlib>
25
28
  #include <filesystem>
26
29
  #include <string>
27
30
  #include <unistd.h>
28
31
  #include <utility>
32
+ #include <vector>
29
33
 
30
34
  namespace {
31
35
 
@@ -35,8 +39,56 @@ struct ImageHandle {
35
39
  double toInch = 1.0;
36
40
  /** 换算到 inch 后保留的小数位;<0 表示不量化 */
37
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;
38
49
  };
39
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
+
40
92
  /** 用户坐标/长度 → libgerbv 内部 inch(可按 precision 量化) */
41
93
  double ToInch(const ImageHandle* h, double v) {
42
94
  double x = v * h->toInch;
@@ -60,18 +112,37 @@ gerbv_net_t* LastNet(gerbv_image_t* image) {
60
112
  return n;
61
113
  }
62
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
+
63
129
  /** 切换到指定 polarity 的 layer,并挂一个继承该 layer 的净空 net,供后续 create_* 继承。 */
64
130
  gerbv_layer_t* EnsurePolarity(gerbv_image_t* image, gerbv_polarity_t pol) {
65
131
  gerbv_net_t* current = LastNet(image);
66
132
  if (!current || !current->layer) return nullptr;
67
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);
68
139
  gerbv_layer_t* layer = gerbv_image_return_new_layer(current->layer);
69
140
  layer->polarity = pol;
70
141
  current = gerber_create_new_net(current, layer, nullptr);
71
142
  current->interpolation = GERBV_INTERPOLATION_LINEARx1;
72
143
  current->aperture_state = GERBV_APERTURE_STATE_OFF;
73
- current->start_x = current->stop_x = 0;
74
- current->start_y = current->stop_y = 0;
144
+ current->start_x = current->stop_x = x;
145
+ current->start_y = current->stop_y = y;
75
146
  return layer;
76
147
  }
77
148
 
@@ -147,6 +218,7 @@ Napi::Value AddLine(const Napi::CallbackInfo& info) {
147
218
  const double w = ToInch(h, info[5].As<Napi::Number>().DoubleValue());
148
219
  EnsurePolarity(h->image, GERBV_POLARITY_DARK);
149
220
  gerbv_image_create_line_object(h->image, x1, y1, x2, y2, w, GERBV_APTYPE_CIRCLE);
221
+ SyncNetColors(h);
150
222
  return env.Undefined();
151
223
  }
152
224
 
@@ -204,8 +276,6 @@ Napi::Value AddArc(const Napi::CallbackInfo& info) {
204
276
  currentNet->cirseg->cp_y = cy;
205
277
  currentNet->cirseg->width = r;
206
278
  currentNet->cirseg->height = r;
207
- currentNet->cirseg->angle1 = a0;
208
- currentNet->cirseg->angle2 = a1;
209
279
 
210
280
  double angleDiff = a1 - a0;
211
281
  if (clockwise) {
@@ -215,6 +285,9 @@ Napi::Value AddArc(const Napi::CallbackInfo& info) {
215
285
  while (angleDiff < 0) angleDiff += 360.0;
216
286
  if (angleDiff == 0) angleDiff = 360.0;
217
287
  }
288
+ // 整圆:angle2 = angle1 ± 360,避免栅格化把 start==end 当成零弧
289
+ currentNet->cirseg->angle1 = a0;
290
+ currentNet->cirseg->angle2 = a0 + angleDiff;
218
291
  const int steps = std::max(1, static_cast<int>(std::fabs(angleDiff)));
219
292
  for (int i = 0; i <= steps; i++) {
220
293
  const double a = (a0 + (angleDiff * i) / steps) * (3.14159265358979323846 / 180.0);
@@ -224,6 +297,7 @@ Napi::Value AddArc(const Napi::CallbackInfo& info) {
224
297
  &currentNet->boundingBox, tx, ty, w / 2, w / 2, w / 2, w / 2);
225
298
  }
226
299
  gerber_update_image_min_max(&currentNet->boundingBox, 0, 0, h->image);
300
+ SyncNetColors(h);
227
301
  return env.Undefined();
228
302
  }
229
303
 
@@ -244,6 +318,7 @@ Napi::Value AddFlashCircle(const Napi::CallbackInfo& info) {
244
318
  EnsurePolarity(h->image, GERBV_POLARITY_DARK);
245
319
  // 同点起止 → libgerbv 走 FLASH
246
320
  gerbv_image_create_line_object(h->image, x, y, x, y, d, GERBV_APTYPE_CIRCLE);
321
+ SyncNetColors(h);
247
322
  return env.Undefined();
248
323
  }
249
324
 
@@ -264,6 +339,7 @@ Napi::Value AddRect(const Napi::CallbackInfo& info) {
264
339
  const double ht = ToInch(h, info[4].As<Napi::Number>().DoubleValue());
265
340
  EnsurePolarity(h->image, GERBV_POLARITY_DARK);
266
341
  gerbv_image_create_rectangle_object(h->image, x, y, w, ht);
342
+ SyncNetColors(h);
267
343
  return env.Undefined();
268
344
  }
269
345
 
@@ -454,9 +530,11 @@ Napi::Value AddPolygonLine(const Napi::CallbackInfo& info) {
454
530
  const double y = ToInch(p, info[2].As<Napi::Number>().DoubleValue());
455
531
  if (!p->hasPoint) {
456
532
  if (!PolyEnsureStart(p, x, y, env)) return env.Undefined();
533
+ if (p->owner) SyncNetColors(p->owner);
457
534
  return env.Undefined();
458
535
  }
459
536
  PolyAppendLine(p, x, y, true);
537
+ if (p->owner) SyncNetColors(p->owner);
460
538
  return env.Undefined();
461
539
  }
462
540
 
@@ -488,6 +566,7 @@ Napi::Value AddPolygonArc(const Napi::CallbackInfo& info) {
488
566
  const double y = ToInch(p, info[4].As<Napi::Number>().DoubleValue());
489
567
  const bool cw = info.Length() >= 6 && info[5].ToBoolean().Value();
490
568
  PolyAppendArc(p, cx, cy, x, y, cw);
569
+ if (p->owner) SyncNetColors(p->owner);
491
570
  return env.Undefined();
492
571
  }
493
572
 
@@ -523,6 +602,7 @@ Napi::Value AddPolygonBezier2(const Napi::CallbackInfo& info) {
523
602
  const double x = ToInch(p, info[3].As<Napi::Number>().DoubleValue());
524
603
  const double y = ToInch(p, info[4].As<Napi::Number>().DoubleValue());
525
604
  PolyAppendBezierSampled(p, c1x, c1y, c1x, c1y, x, y, false, 16);
605
+ if (p->owner) SyncNetColors(p->owner);
526
606
  return env.Undefined();
527
607
  }
528
608
 
@@ -543,6 +623,52 @@ Napi::Value AddPolygonBezier3(const Napi::CallbackInfo& info) {
543
623
  const double x = ToInch(p, info[5].As<Napi::Number>().DoubleValue());
544
624
  const double y = ToInch(p, info[6].As<Napi::Number>().DoubleValue());
545
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);
546
672
  return env.Undefined();
547
673
  }
548
674
 
@@ -567,9 +693,15 @@ Napi::Value EndPolygon(const Napi::CallbackInfo& info) {
567
693
  PolyAppendLine(p, p->startX, p->startY, true);
568
694
  }
569
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;
570
698
  currentNet = gerber_create_new_net(currentNet, p->layer, nullptr);
571
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;
572
703
  p->finished = true;
704
+ if (p->owner) SyncNetColors(p->owner);
573
705
  return env.Undefined();
574
706
  }
575
707
 
@@ -617,6 +749,7 @@ Napi::Value AddBezier2(const Napi::CallbackInfo& info) {
617
749
  ToInch(h, info[5].As<Napi::Number>().DoubleValue()),
618
750
  ToInch(h, info[6].As<Napi::Number>().DoubleValue()),
619
751
  ToInch(h, info[7].As<Napi::Number>().DoubleValue()), false, 16);
752
+ SyncNetColors(h);
620
753
  return env.Undefined();
621
754
  }
622
755
 
@@ -645,6 +778,7 @@ Napi::Value AddBezier3(const Napi::CallbackInfo& info) {
645
778
  ToInch(h, info[7].As<Napi::Number>().DoubleValue()),
646
779
  ToInch(h, info[8].As<Napi::Number>().DoubleValue()),
647
780
  ToInch(h, info[9].As<Napi::Number>().DoubleValue()), true, 20);
781
+ SyncNetColors(h);
648
782
  return env.Undefined();
649
783
  }
650
784
 
@@ -715,6 +849,387 @@ Napi::Value ExportDrill(const Napi::CallbackInfo& info) {
715
849
  return Napi::Buffer<char>::Copy(env, data.data(), data.size());
716
850
  }
717
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
+
718
1233
  Napi::Object Init(Napi::Env env, Napi::Object exports) {
719
1234
  exports.Set("createImage", Napi::Function::New(env, CreateImage));
720
1235
  exports.Set("addLine", Napi::Function::New(env, AddLine));
@@ -728,9 +1243,15 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
728
1243
  exports.Set("addPolygonArc", Napi::Function::New(env, AddPolygonArc));
729
1244
  exports.Set("addPolygonBezier2", Napi::Function::New(env, AddPolygonBezier2));
730
1245
  exports.Set("addPolygonBezier3", Napi::Function::New(env, AddPolygonBezier3));
1246
+ exports.Set("addPolygonContour", Napi::Function::New(env, AddPolygonContour));
731
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));
732
1251
  exports.Set("exportRs274x", Napi::Function::New(env, ExportRs274x));
733
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));
734
1255
  exports.Set("backend", Napi::String::New(env, "libgerbv"));
735
1256
  return exports;
736
1257
  }