@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
@@ -0,0 +1,416 @@
1
+ /**
2
+ * 轻量 PNG 栅格化:线/弧/flash/区域;CLEAR 挖孔;按 net 着色;多层叠加。
3
+ */
4
+ #include "export_png.h"
5
+
6
+ #include <cairo.h>
7
+
8
+ #include <algorithm>
9
+ #include <cmath>
10
+ #include <cstring>
11
+ #include <vector>
12
+
13
+ // glib 2.90 的 gwin32.h 含 C++ 模板,必须在 extern "C" 之外先包含一次。
14
+ #include <glib.h>
15
+
16
+ extern "C" {
17
+ #include "gerbv.h"
18
+ }
19
+
20
+ namespace {
21
+
22
+ cairo_status_t PngWriteCb(void* closure, const unsigned char* data, unsigned int length) {
23
+ auto* out = static_cast<std::string*>(closure);
24
+ out->append(reinterpret_cast<const char*>(data), length);
25
+ return CAIRO_STATUS_SUCCESS;
26
+ }
27
+
28
+ double ApertureWidth(gerbv_image_t* image, int aperIdx) {
29
+ if (aperIdx < 0 || aperIdx >= APERTURE_MAX) return 0;
30
+ gerbv_aperture_t* a = image->aperture[aperIdx];
31
+ if (!a) return 0;
32
+ return a->parameter[0];
33
+ }
34
+
35
+ bool IsClear(const gerbv_net_t* net) {
36
+ return net && net->layer && net->layer->polarity == GERBV_POLARITY_CLEAR;
37
+ }
38
+
39
+ GerbvPngRgba ColorForNet(
40
+ size_t netIndex, const GerbvPngRgba* netColors, size_t netColorCount,
41
+ const GerbvPngRgba& fallback
42
+ ) {
43
+ if (netColors && netIndex < netColorCount) return netColors[netIndex];
44
+ return fallback;
45
+ }
46
+
47
+ void ApplyPolarity(cairo_t* cr, bool clear, const GerbvPngRgba& fg) {
48
+ if (clear) {
49
+ cairo_set_operator(cr, CAIRO_OPERATOR_DEST_OUT);
50
+ cairo_set_source_rgba(cr, 0, 0, 0, 1);
51
+ } else {
52
+ cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
53
+ cairo_set_source_rgba(cr, fg.r, fg.g, fg.b, fg.a);
54
+ }
55
+ }
56
+
57
+ void StrokeLine(cairo_t* cr, double x1, double y1, double x2, double y2, double w) {
58
+ if (!(w > 0)) return;
59
+ cairo_set_line_width(cr, w);
60
+ cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
61
+ cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
62
+ cairo_move_to(cr, x1, y1);
63
+ cairo_line_to(cr, x2, y2);
64
+ cairo_stroke(cr);
65
+ }
66
+
67
+ void FillCircle(cairo_t* cr, double x, double y, double diam) {
68
+ if (!(diam > 0)) return;
69
+ cairo_arc(cr, x, y, diam * 0.5, 0, 2 * M_PI);
70
+ cairo_fill(cr);
71
+ }
72
+
73
+ void FillRect(cairo_t* cr, double x1, double y1, double x2, double y2) {
74
+ const double xmin = std::min(x1, x2);
75
+ const double ymin = std::min(y1, y2);
76
+ const double w = std::fabs(x2 - x1);
77
+ const double h = std::fabs(y2 - y1);
78
+ if (!(w > 0) || !(h > 0)) return;
79
+ cairo_rectangle(cr, xmin, ymin, w, h);
80
+ cairo_fill(cr);
81
+ }
82
+
83
+ void StrokeArc(cairo_t* cr, const gerbv_net_t* net, double w) {
84
+ if (!net->cirseg || !(w > 0)) return;
85
+ const double cx = net->cirseg->cp_x;
86
+ const double cy = net->cirseg->cp_y;
87
+ const double r = net->cirseg->width;
88
+ if (!(r > 1e-12)) {
89
+ StrokeLine(cr, net->start_x, net->start_y, net->stop_x, net->stop_y, w);
90
+ return;
91
+ }
92
+ double a0 = net->cirseg->angle1 * (M_PI / 180.0);
93
+ double a1 = net->cirseg->angle2 * (M_PI / 180.0);
94
+ const bool cw = net->interpolation == GERBV_INTERPOLATION_CW_CIRCULAR;
95
+ // start==end 表示整圆;cairo_arc(a,a) 为空路径,需显式 ±2π
96
+ if (std::fabs(a0 - a1) < 1e-12) {
97
+ a1 = cw ? a0 - 2 * M_PI : a0 + 2 * M_PI;
98
+ }
99
+ cairo_set_line_width(cr, w);
100
+ cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
101
+ if (cw) cairo_arc_negative(cr, cx, cy, r, a0, a1);
102
+ else cairo_arc(cr, cx, cy, r, a0, a1);
103
+ cairo_stroke(cr);
104
+ }
105
+
106
+ void AppendPolyArcPts(
107
+ std::vector<std::pair<double, double>>* pts, const gerbv_net_t* net
108
+ ) {
109
+ if (!net->cirseg) {
110
+ pts->emplace_back(net->stop_x, net->stop_y);
111
+ return;
112
+ }
113
+ const double cx = net->cirseg->cp_x;
114
+ const double cy = net->cirseg->cp_y;
115
+ const double r = net->cirseg->width;
116
+ double a0 = net->cirseg->angle1;
117
+ double a1 = net->cirseg->angle2;
118
+ double diff = a1 - a0;
119
+ const bool cw = net->interpolation == GERBV_INTERPOLATION_CW_CIRCULAR;
120
+ if (cw) {
121
+ while (diff > 0) diff -= 360;
122
+ if (diff == 0) diff = -360;
123
+ } else {
124
+ while (diff < 0) diff += 360;
125
+ if (diff == 0) diff = 360;
126
+ }
127
+ const int steps = std::max(1, static_cast<int>(std::fabs(diff)));
128
+ for (int i = 1; i <= steps; i++) {
129
+ const double a = (a0 + diff * i / steps) * (M_PI / 180.0);
130
+ pts->emplace_back(cx + r * std::cos(a), cy + r * std::sin(a));
131
+ }
132
+ }
133
+
134
+ void RenderNets(
135
+ cairo_t* cr, gerbv_image_t* image, const GerbvPngRgba* netColors, size_t netColorCount,
136
+ const GerbvPngRgba& defaultFg
137
+ ) {
138
+ bool inPoly = false;
139
+ std::vector<std::vector<std::pair<double, double>>> polyContours;
140
+ std::vector<std::pair<double, double>> polyPts;
141
+ GerbvPngRgba polyFg = defaultFg;
142
+ size_t netIndex = 0;
143
+
144
+ auto flushContour = [&]() {
145
+ if (polyPts.size() >= 3) polyContours.push_back(polyPts);
146
+ polyPts.clear();
147
+ };
148
+
149
+ for (gerbv_net_t* net = image->netlist; net; net = net->next, ++netIndex) {
150
+ const bool clear = IsClear(net);
151
+ const GerbvPngRgba fg = ColorForNet(netIndex, netColors, netColorCount, defaultFg);
152
+
153
+ if (net->interpolation == GERBV_INTERPOLATION_PAREA_START) {
154
+ inPoly = true;
155
+ polyContours.clear();
156
+ polyPts.clear();
157
+ polyFg = fg;
158
+ continue;
159
+ }
160
+ if (net->interpolation == GERBV_INTERPOLATION_PAREA_END) {
161
+ flushContour();
162
+ if (!polyContours.empty()) {
163
+ ApplyPolarity(cr, clear, polyFg);
164
+ cairo_new_path(cr);
165
+ // RS-274X 区域填充为 even-odd:同 G36 多轮廓即挖孔
166
+ cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
167
+ for (const auto& contour : polyContours) {
168
+ cairo_move_to(cr, contour[0].first, contour[0].second);
169
+ for (size_t i = 1; i < contour.size(); i++) {
170
+ cairo_line_to(cr, contour[i].first, contour[i].second);
171
+ }
172
+ cairo_close_path(cr);
173
+ }
174
+ cairo_fill(cr);
175
+ }
176
+ inPoly = false;
177
+ polyContours.clear();
178
+ polyPts.clear();
179
+ continue;
180
+ }
181
+
182
+ if (inPoly) {
183
+ if (net->aperture_state == GERBV_APERTURE_STATE_OFF) {
184
+ // D02:新轮廓起点(多轮廓挖孔)
185
+ if (!polyPts.empty()) flushContour();
186
+ polyPts.emplace_back(net->stop_x, net->stop_y);
187
+ continue;
188
+ }
189
+ if (net->interpolation == GERBV_INTERPOLATION_LINEARx1 ||
190
+ net->interpolation == GERBV_INTERPOLATION_CW_CIRCULAR ||
191
+ net->interpolation == GERBV_INTERPOLATION_CCW_CIRCULAR) {
192
+ if (polyPts.empty()) polyPts.emplace_back(net->start_x, net->start_y);
193
+ if (net->interpolation == GERBV_INTERPOLATION_CW_CIRCULAR ||
194
+ net->interpolation == GERBV_INTERPOLATION_CCW_CIRCULAR) {
195
+ AppendPolyArcPts(&polyPts, net);
196
+ } else {
197
+ polyPts.emplace_back(net->stop_x, net->stop_y);
198
+ }
199
+ }
200
+ continue;
201
+ }
202
+
203
+ ApplyPolarity(cr, clear, fg);
204
+ const double w = ApertureWidth(image, net->aperture);
205
+
206
+ if (net->aperture_state == GERBV_APERTURE_STATE_FLASH) {
207
+ gerbv_aperture_t* a =
208
+ (net->aperture >= 0 && net->aperture < APERTURE_MAX) ? image->aperture[net->aperture]
209
+ : nullptr;
210
+ if (a && a->type == GERBV_APTYPE_RECTANGLE) {
211
+ const double rw = a->parameter[0];
212
+ const double rh = a->parameter[1];
213
+ FillRect(
214
+ cr, net->stop_x - rw / 2, net->stop_y - rh / 2, net->stop_x + rw / 2,
215
+ net->stop_y + rh / 2
216
+ );
217
+ } else {
218
+ FillCircle(cr, net->stop_x, net->stop_y, w > 0 ? w : 0.01);
219
+ }
220
+ continue;
221
+ }
222
+
223
+ if (net->aperture_state != GERBV_APERTURE_STATE_ON) continue;
224
+
225
+ if (net->interpolation == GERBV_INTERPOLATION_CW_CIRCULAR ||
226
+ net->interpolation == GERBV_INTERPOLATION_CCW_CIRCULAR) {
227
+ StrokeArc(cr, net, w > 0 ? w : 0.01);
228
+ } else if (
229
+ net->interpolation == GERBV_INTERPOLATION_LINEARx1 ||
230
+ net->interpolation == GERBV_INTERPOLATION_x10 ||
231
+ net->interpolation == GERBV_INTERPOLATION_LINEARx01 ||
232
+ net->interpolation == GERBV_INTERPOLATION_LINEARx001
233
+ ) {
234
+ if (std::fabs(net->start_x - net->stop_x) < 1e-12 &&
235
+ std::fabs(net->start_y - net->stop_y) < 1e-12) {
236
+ FillCircle(cr, net->stop_x, net->stop_y, w > 0 ? w : 0.01);
237
+ } else {
238
+ StrokeLine(cr, net->start_x, net->start_y, net->stop_x, net->stop_y, w > 0 ? w : 0.01);
239
+ }
240
+ }
241
+ }
242
+ }
243
+
244
+ bool ImageBBox(gerbv_image_t* image, double* minX, double* minY, double* maxX, double* maxY) {
245
+ if (!image || !image->info) return false;
246
+ *minX = image->info->min_x;
247
+ *minY = image->info->min_y;
248
+ *maxX = image->info->max_x;
249
+ *maxY = image->info->max_y;
250
+ if (!std::isfinite(*minX) || !std::isfinite(*minY) || !std::isfinite(*maxX) ||
251
+ !std::isfinite(*maxY) || *maxX <= *minX || *maxY <= *minY) {
252
+ *minX = 1e300;
253
+ *minY = 1e300;
254
+ *maxX = -1e300;
255
+ *maxY = -1e300;
256
+ bool any = false;
257
+ for (gerbv_net_t* net = image->netlist; net; net = net->next) {
258
+ auto consider = [&](double x, double y) {
259
+ *minX = std::min(*minX, x);
260
+ *minY = std::min(*minY, y);
261
+ *maxX = std::max(*maxX, x);
262
+ *maxY = std::max(*maxY, y);
263
+ any = true;
264
+ };
265
+ consider(net->start_x, net->start_y);
266
+ consider(net->stop_x, net->stop_y);
267
+ if (net->cirseg) {
268
+ const double r = net->cirseg->width;
269
+ consider(net->cirseg->cp_x - r, net->cirseg->cp_y - r);
270
+ consider(net->cirseg->cp_x + r, net->cirseg->cp_y + r);
271
+ }
272
+ }
273
+ if (!any) {
274
+ *minX = 0;
275
+ *minY = 0;
276
+ *maxX = 1;
277
+ *maxY = 1;
278
+ }
279
+ }
280
+ return true;
281
+ }
282
+
283
+ bool UnionBBox(
284
+ const std::vector<GerbvPngLayerInput>& layers, double* minX, double* minY, double* maxX,
285
+ double* maxY
286
+ ) {
287
+ *minX = 1e300;
288
+ *minY = 1e300;
289
+ *maxX = -1e300;
290
+ *maxY = -1e300;
291
+ bool any = false;
292
+ for (const auto& layer : layers) {
293
+ auto* image = static_cast<gerbv_image_t*>(layer.image);
294
+ if (!image) continue;
295
+ double a, b, c, d;
296
+ if (!ImageBBox(image, &a, &b, &c, &d)) continue;
297
+ *minX = std::min(*minX, a);
298
+ *minY = std::min(*minY, b);
299
+ *maxX = std::max(*maxX, c);
300
+ *maxY = std::max(*maxY, d);
301
+ any = true;
302
+ }
303
+ if (!any) {
304
+ *minX = 0;
305
+ *minY = 0;
306
+ *maxX = 1;
307
+ *maxY = 1;
308
+ }
309
+ return true;
310
+ }
311
+
312
+ } // namespace
313
+
314
+ bool GerbvRenderLayersToPng(
315
+ const std::vector<GerbvPngLayerInput>& layers,
316
+ const GerbvPngExportOpts& opts,
317
+ std::string* outPng,
318
+ std::string* errMsg
319
+ ) {
320
+ if (!outPng) {
321
+ if (errMsg) *errMsg = "invalid output";
322
+ return false;
323
+ }
324
+ if (layers.empty()) {
325
+ if (errMsg) *errMsg = "no layers";
326
+ return false;
327
+ }
328
+ outPng->clear();
329
+
330
+ double minX, minY, maxX, maxY;
331
+ if (opts.region.enabled) {
332
+ minX = opts.region.minX;
333
+ minY = opts.region.minY;
334
+ maxX = opts.region.maxX;
335
+ maxY = opts.region.maxY;
336
+ if (!(maxX > minX) || !(maxY > minY)) {
337
+ if (errMsg) *errMsg = "invalid region";
338
+ return false;
339
+ }
340
+ } else {
341
+ UnionBBox(layers, &minX, &minY, &maxX, &maxY);
342
+ }
343
+
344
+ const double bw = std::max(1e-9, maxX - minX);
345
+ const double bh = std::max(1e-9, maxY - minY);
346
+ const int paddingPx = std::max(0, opts.paddingPx);
347
+
348
+ int widthPx = opts.widthPx;
349
+ int heightPx = opts.heightPx;
350
+ if (opts.dpi > 0) {
351
+ widthPx = static_cast<int>(std::lround(bw * opts.dpi)) + 2 * paddingPx;
352
+ heightPx = static_cast<int>(std::lround(bh * opts.dpi)) + 2 * paddingPx;
353
+ } else {
354
+ if (widthPx <= 0) widthPx = 800;
355
+ if (heightPx <= 0) {
356
+ heightPx = static_cast<int>(std::lround(
357
+ (widthPx - 2.0 * paddingPx) * (bh / bw) + 2.0 * paddingPx
358
+ ));
359
+ }
360
+ }
361
+ if (widthPx < 1) widthPx = 1;
362
+ if (heightPx < 1) heightPx = 1;
363
+
364
+ cairo_surface_t* surface =
365
+ cairo_image_surface_create(CAIRO_FORMAT_ARGB32, widthPx, heightPx);
366
+ if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
367
+ if (errMsg) *errMsg = "cairo_image_surface_create failed";
368
+ cairo_surface_destroy(surface);
369
+ return false;
370
+ }
371
+ cairo_t* cr = cairo_create(surface);
372
+
373
+ cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
374
+ cairo_set_source_rgba(
375
+ cr, opts.background.r, opts.background.g, opts.background.b, opts.background.a
376
+ );
377
+ cairo_paint(cr);
378
+ cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
379
+
380
+ const double drawW = std::max(1.0, widthPx - 2.0 * paddingPx);
381
+ const double drawH = std::max(1.0, heightPx - 2.0 * paddingPx);
382
+ const double scale = std::min(drawW / bw, drawH / bh);
383
+ const double ox = paddingPx + (drawW - bw * scale) * 0.5;
384
+ const double oy = paddingPx + (drawH - bh * scale) * 0.5;
385
+
386
+ cairo_translate(cr, ox, oy + bh * scale);
387
+ cairo_scale(cr, scale, -scale);
388
+ cairo_translate(cr, -minX, -minY);
389
+
390
+ // 按 zIndex 升序绘制(低在下、高在上);稳定排序保传入相对序
391
+ std::vector<size_t> order(layers.size());
392
+ for (size_t i = 0; i < order.size(); ++i) order[i] = i;
393
+ std::stable_sort(order.begin(), order.end(), [&](size_t a, size_t b) {
394
+ return layers[a].zIndex < layers[b].zIndex;
395
+ });
396
+
397
+ for (size_t idx : order) {
398
+ const auto& layer = layers[idx];
399
+ auto* image = static_cast<gerbv_image_t*>(layer.image);
400
+ if (!image) continue;
401
+ cairo_save(cr);
402
+ RenderNets(cr, image, layer.netColors, layer.netColorCount, layer.defaultFg);
403
+ cairo_restore(cr);
404
+ }
405
+
406
+ cairo_destroy(cr);
407
+ const cairo_status_t st =
408
+ cairo_surface_write_to_png_stream(surface, PngWriteCb, outPng);
409
+ cairo_surface_destroy(surface);
410
+ if (st != CAIRO_STATUS_SUCCESS) {
411
+ if (errMsg) *errMsg = cairo_status_to_string(st);
412
+ outPng->clear();
413
+ return false;
414
+ }
415
+ return true;
416
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * 将 gerbv_image 栅格化为 PNG(Cairo)。
3
+ * 支持:区域裁剪、DPI、按 net 前景色、多 image 叠加。
4
+ */
5
+ #pragma once
6
+
7
+ #include <cstddef>
8
+ #include <string>
9
+ #include <vector>
10
+
11
+ struct GerbvPngRgba {
12
+ double r = 0;
13
+ double g = 0;
14
+ double b = 0;
15
+ double a = 1;
16
+ };
17
+
18
+ struct GerbvPngRegion {
19
+ bool enabled = false;
20
+ double minX = 0; // inch
21
+ double minY = 0;
22
+ double maxX = 0;
23
+ double maxY = 0;
24
+ };
25
+
26
+ struct GerbvPngLayerInput {
27
+ void* image = nullptr;
28
+ /** 与 net 顺序对齐;空则整层用 defaultFg */
29
+ const GerbvPngRgba* netColors = nullptr;
30
+ size_t netColorCount = 0;
31
+ GerbvPngRgba defaultFg{0.85, 0.7, 0.1, 1};
32
+ /** 绘制顺序:小的在下、大的在上;相等时保持传入顺序 */
33
+ int zIndex = 0;
34
+ };
35
+
36
+ struct GerbvPngExportOpts {
37
+ int widthPx = 0; // dpi 未设时的目标宽;0=自动
38
+ int heightPx = 0; // 0=按比例
39
+ int paddingPx = 16;
40
+ double dpi = 0; // >0 时按 region/bbox 物理尺寸×dpi 定像素
41
+ GerbvPngRegion region;
42
+ GerbvPngRgba background{0, 0, 0, 1};
43
+ };
44
+
45
+ bool GerbvRenderLayersToPng(
46
+ const std::vector<GerbvPngLayerInput>& layers,
47
+ const GerbvPngExportOpts& opts,
48
+ std::string* outPng,
49
+ std::string* errMsg
50
+ );