@huaqiu/js-gerbv 0.1.15 → 0.1.27

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/index.js CHANGED
@@ -8,6 +8,12 @@
8
8
 
9
9
  const fs = require("fs");
10
10
  const path = require("path");
11
+ const { addText: drawText } = require("./lib/add-text.js");
12
+ const {
13
+ sampleSplineFrom,
14
+ sampleSplineCmd,
15
+ splineCmd,
16
+ } = require("./lib/spline.js");
11
17
 
12
18
  function platformKey() {
13
19
  return process.platform + "-" + process.arch;
@@ -61,14 +67,43 @@ function seedRectCmds(x, y, w, h) {
61
67
  ];
62
68
  }
63
69
 
64
- /** 圆形外环:右点起点 + 两段半圆弧 */
65
- function seedCircleCmds(cx, cy, diam) {
70
+ /** 圆形外环:均匀折线(避免两段半圆弧在接合处留下缺口) */
71
+ function seedCircleCmds(cx, cy, diam, segments) {
66
72
  const r = diam / 2;
67
- return [
68
- { t: "L", x: cx + r, y: cy },
69
- { t: "A", cx, cy, x: cx - r, y: cy, cw: false },
70
- { t: "A", cx, cy, x: cx + r, y: cy, cw: false },
71
- ];
73
+ const n = Math.max(24, segments || 64);
74
+ /** @type {{t:string,x:number,y:number}[]} */
75
+ const cmds = [];
76
+ for (let i = 0; i < n; i++) {
77
+ const a = (2 * Math.PI * i) / n;
78
+ cmds.push({ t: "L", x: cx + r * Math.cos(a), y: cy + r * Math.sin(a) });
79
+ }
80
+ return cmds;
81
+ }
82
+
83
+ /**
84
+ * 圆环:外环 CCW + 内环 CW,同角桥接成单轮廓(无孔、无三角缝)。
85
+ * @param {number} cx
86
+ * @param {number} cy
87
+ * @param {number} d 外径
88
+ * @param {number} w 环宽
89
+ * @param {number} [segments]
90
+ */
91
+ function seedCircleRingCmds(cx, cy, d, w, segments) {
92
+ const Ro = d / 2;
93
+ const Ri = Ro - w;
94
+ const n = Math.max(24, segments || 64);
95
+ /** @type {{t:string,x:number,y:number}[]} */
96
+ const cmds = [];
97
+ for (let i = 0; i <= n; i++) {
98
+ const a = (2 * Math.PI * i) / n;
99
+ cmds.push({ t: "L", x: cx + Ro * Math.cos(a), y: cy + Ro * Math.sin(a) });
100
+ }
101
+ for (let i = 0; i <= n; i++) {
102
+ const a = (-2 * Math.PI * i) / n;
103
+ cmds.push({ t: "L", x: cx + Ri * Math.cos(a), y: cy + Ri * Math.sin(a) });
104
+ }
105
+ cmds.push({ t: "L", x: cx + Ro, y: cy });
106
+ return cmds;
72
107
  }
73
108
 
74
109
  /**
@@ -80,14 +115,34 @@ function seedCircleCmds(cx, cy, diam) {
80
115
  function replayContour(native, imageHandle, cmds, clear) {
81
116
  if (!cmds.length) return;
82
117
  const h = native.createPolygon(imageHandle, !!clear);
118
+ let curX = 0;
119
+ let curY = 0;
120
+ let has = false;
83
121
  for (const c of cmds) {
84
- if (c.t === "L") native.addPolygonLine(h, c.x, c.y);
85
- else if (c.t === "A")
122
+ if (c.t === "L") {
123
+ native.addPolygonLine(h, c.x, c.y);
124
+ curX = c.x;
125
+ curY = c.y;
126
+ has = true;
127
+ } else if (c.t === "A") {
86
128
  native.addPolygonArc(h, c.cx, c.cy, c.x, c.y, !!c.cw);
87
- else if (c.t === "B2")
129
+ curX = c.x;
130
+ curY = c.y;
131
+ } else if (c.t === "B2") {
88
132
  native.addPolygonBezier2(h, c.c1x, c.c1y, c.x, c.y);
89
- else if (c.t === "B3")
133
+ curX = c.x;
134
+ curY = c.y;
135
+ } else if (c.t === "B3") {
90
136
  native.addPolygonBezier3(h, c.c1x, c.c1y, c.c2x, c.c2y, c.x, c.y);
137
+ curX = c.x;
138
+ curY = c.y;
139
+ } else if (c.t === "S" && has) {
140
+ for (const p of sampleSplineCmd(curX, curY, c)) {
141
+ native.addPolygonLine(h, p.x, p.y);
142
+ curX = p.x;
143
+ curY = p.y;
144
+ }
145
+ }
91
146
  }
92
147
  native.endPolygon(h);
93
148
  }
@@ -115,6 +170,15 @@ function wrapOuter(cmds, extras) {
115
170
  cmds.push({ t: "B3", c1x, c1y, c2x, c2y, x, y });
116
171
  return contour;
117
172
  },
173
+ /**
174
+ * B 样条边。points 为追加控制点(末点=终点);控制多边形=[当前, ...points]。
175
+ * @param {Array<[number,number]|{x:number,y:number}>} points
176
+ * @param {{ degree?: number, knots?: number[], steps?: number }} [opts]
177
+ */
178
+ addPolygonSpline(points, opts) {
179
+ cmds.push(splineCmd(points, opts));
180
+ return contour;
181
+ },
118
182
  createHole: extras.createHole,
119
183
  addCircleHole: extras.addCircleHole,
120
184
  endArea: extras.endArea,
@@ -152,6 +216,16 @@ function wrapHole(cmds) {
152
216
  cmds.push({ t: "B3", c1x, c1y, c2x, c2y, x, y });
153
217
  return hole;
154
218
  },
219
+ /**
220
+ * B 样条边。points 为追加控制点(末点=终点);控制多边形=[当前, ...points]。
221
+ * @param {Array<[number,number]|{x:number,y:number}>} points
222
+ * @param {{ degree?: number, knots?: number[], steps?: number }} [opts]
223
+ */
224
+ addPolygonSpline(points, opts) {
225
+ ensureOpen("addPolygonSpline");
226
+ cmds.push(splineCmd(points, opts));
227
+ return hole;
228
+ },
155
229
  endHole() {
156
230
  ended = true;
157
231
  },
@@ -165,6 +239,7 @@ function wrapHole(cmds) {
165
239
  * @param {object[]} seedOuter 至少含起点
166
240
  */
167
241
  function wrapRegion(native, imageHandle, seedOuter) {
242
+ const { fillWithHolesNoLpc } = require("./lib/fill-holes.js");
168
243
  const outerCmds = seedOuter.slice();
169
244
  /** @type {object[][]} */
170
245
  const holes = [];
@@ -186,10 +261,14 @@ function wrapRegion(native, imageHandle, seedOuter) {
186
261
  endArea() {
187
262
  if (finished) return;
188
263
  finished = true;
189
- replayContour(native, imageHandle, outerCmds, false);
190
- for (const holeCmds of holes) {
191
- replayContour(native, imageHandle, holeCmds, true);
264
+ const draw = (cmds, clear) =>
265
+ replayContour(native, imageHandle, cmds, !!clear);
266
+ // 有孔:sanitize 后外环 dark + 孔 %LPC*%(规范推荐;同 G36 多轮廓是并集不能挖空)
267
+ if (holes.length) {
268
+ fillWithHolesNoLpc(draw, outerCmds, holes);
269
+ return;
192
270
  }
271
+ replayContour(native, imageHandle, outerCmds, false);
193
272
  },
194
273
  });
195
274
  return region;
@@ -250,6 +329,21 @@ function wrapPolyline(native, imageHandle, x0, y0, width) {
250
329
  curY = y;
251
330
  return pl;
252
331
  },
332
+ /**
333
+ * B 样条描边。points 为追加控制点(末点=终点);控制多边形=[当前, ...points]。
334
+ * @param {Array<[number,number]|{x:number,y:number}>} points
335
+ * @param {{ degree?: number, knots?: number[], steps?: number }} [opts]
336
+ */
337
+ addPolylineSpline(points, opts) {
338
+ ensureOpen("addPolylineSpline");
339
+ const samples = sampleSplineFrom(curX, curY, points, opts);
340
+ for (const p of samples) {
341
+ native.addLine(imageHandle, curX, curY, p.x, p.y, width);
342
+ curX = p.x;
343
+ curY = p.y;
344
+ }
345
+ return pl;
346
+ },
253
347
  endPolyline() {
254
348
  finished = true;
255
349
  },
@@ -260,11 +354,65 @@ function wrapPolyline(native, imageHandle, x0, y0, width) {
260
354
  /**
261
355
  * @param {object} native
262
356
  * @param {unknown} handle
357
+ * @param {{ name?: string, zIndex?: number, color?: string|{r,g,b,a} }} [layerOpts]
263
358
  */
264
- function wrapImage(native, handle) {
359
+ function wrapImage(native, handle, layerOpts) {
360
+ const opts = layerOpts || {};
361
+ let zIndex = typeof opts.zIndex === "number" ? opts.zIndex : 0;
362
+ if (opts.color != null) native.setLayerColor(handle, opts.color);
363
+
265
364
  const img = {
266
365
  /** @internal */
267
366
  __handle: handle,
367
+ name: opts.name || "",
368
+ get zIndex() {
369
+ return zIndex;
370
+ },
371
+ set zIndex(v) {
372
+ zIndex = typeof v === "number" ? v : 0;
373
+ },
374
+ /** 层默认色(优先级最低) */
375
+ setLayerColor(color) {
376
+ native.setLayerColor(handle, color);
377
+ return img;
378
+ },
379
+ /** 别名:层色 */
380
+ setColor(color) {
381
+ return img.setLayerColor(color);
382
+ },
383
+ /** 对象色(次于网络);null/undefined 清除 */
384
+ setObjectColor(color) {
385
+ native.setObjectColor(handle, color == null ? null : color);
386
+ return img;
387
+ },
388
+ /** 网络色(最高优先);null/undefined 清除 */
389
+ setNetColor(color) {
390
+ native.setNetColor(handle, color == null ? null : color);
391
+ return img;
392
+ },
393
+ /**
394
+ * 内部:按轮廓命令填色(供 addText TTF 使用)。
395
+ * @param {object[]} cmds {t:'L'|'A'|'B2'|'B3', ...},世界坐标
396
+ * @param {boolean} [clear]
397
+ */
398
+ __drawContour(cmds, clear) {
399
+ replayContour(native, handle, cmds || [], !!clear);
400
+ return img;
401
+ },
402
+ /**
403
+ * 外环+孔填色(dark + %LPC*%;供 TTF 文字等使用)。
404
+ * @param {object[]} outerCmds
405
+ * @param {object[][]} holeCmdsList
406
+ */
407
+ __fillWithHoles(outerCmds, holeCmdsList) {
408
+ const { fillWithHolesNoLpc: fill } = require("./lib/fill-holes.js");
409
+ fill(
410
+ (cmds, clear) => replayContour(native, handle, cmds || [], !!clear),
411
+ outerCmds || [],
412
+ holeCmdsList || []
413
+ );
414
+ return img;
415
+ },
268
416
  addLine(x1, y1, x2, y2, width) {
269
417
  native.addLine(handle, x1, y1, x2, y2, width);
270
418
  return img;
@@ -292,10 +440,18 @@ function wrapImage(native, handle) {
292
440
  return wrapRegion(native, handle, seedCircleCmds(cx, cy, diam));
293
441
  },
294
442
  addCircleRing(cx, cy, d, w) {
295
- const region = wrapRegion(native, handle, seedCircleCmds(cx, cy, d));
296
- const holeD = d - 2 * w;
297
- if (w > 0 && holeD > 0) region.addCircleHole(cx, cy, holeD);
298
- region.endArea();
443
+ if (!(typeof d === "number" && d > 0)) return img;
444
+ if (!(typeof w === "number" && w > 0) || w >= d / 2) {
445
+ wrapRegion(native, handle, seedCircleCmds(cx, cy, d)).endArea();
446
+ return img;
447
+ }
448
+ // 中径圆孔径描边(两段半圆):无 G36/LPC,避免折线闭合缝与三角细缝
449
+ const r = d / 2 - w / 2;
450
+ const x0 = cx + r;
451
+ const y0 = cy;
452
+ const x1 = cx - r;
453
+ native.addArc(handle, cx, cy, x0, y0, x1, y0, w, false);
454
+ native.addArc(handle, cx, cy, x1, y0, x0, y0, w, false);
299
455
  return img;
300
456
  },
301
457
  /** @param {number} x @param {number} y */
@@ -311,16 +467,113 @@ function wrapImage(native, handle) {
311
467
  }
312
468
  return wrapPolyline(native, handle, x, y, width);
313
469
  },
470
+ /**
471
+ * 矢量文字(Hershey Futural 描边 → 线段)。支持 \\n 多行。
472
+ * @param {number} x 锚点 X
473
+ * @param {number} y 锚点 Y
474
+ * @param {string} text
475
+ * @param {{
476
+ * height?: number, strokeWidth?: number,
477
+ * align?: 'left'|'center'|'right',
478
+ * valign?: 'baseline'|'bottom'|'center'|'top',
479
+ * angle?: number, letterSpacing?: number, lineGap?: number,
480
+ * fontFile?: string|boolean
481
+ * }} [textOpts] fontFile:"system"/路径;非 ASCII 可省略并自动系统字体;ASCII 默认 Hershey
482
+ */
483
+ addText(x, y, text, textOpts) {
484
+ return drawText(img, x, y, text, textOpts);
485
+ },
314
486
  exportRs274x() {
315
487
  return native.exportRs274x(handle);
316
488
  },
317
489
  exportDrill() {
318
490
  return native.exportDrill(handle);
319
491
  },
492
+ /**
493
+ * 单层栅格化 PNG。
494
+ * @param {{
495
+ * width?: number, height?: number, padding?: number, dpi?: number,
496
+ * region?: {minX,minY,maxX,maxY}|{x,y,width,height},
497
+ * background?: string|{r,g,b,a}, foreground?: string|{r,g,b,a}
498
+ * }} [exportOpts]
499
+ * @returns {Buffer}
500
+ */
501
+ exportPng(exportOpts) {
502
+ return native.exportPng(handle, exportOpts || {});
503
+ },
320
504
  };
321
505
  return img;
322
506
  }
323
507
 
508
+ /**
509
+ * @param {unknown[]} layers
510
+ * @param {object} [exportOpts]
511
+ */
512
+ function exportPngLayers(layers, exportOpts) {
513
+ if (!Array.isArray(layers) || layers.length === 0) {
514
+ throw new Error("exportPngLayers requires a non-empty layers array");
515
+ }
516
+ const packed = layers.map((L, i) => {
517
+ if (L && typeof L === "object" && "image" in L) {
518
+ return {
519
+ image: unwrap(L.image),
520
+ zIndex: typeof L.zIndex === "number" ? L.zIndex : i,
521
+ };
522
+ }
523
+ return {
524
+ image: unwrap(L),
525
+ zIndex: typeof L.zIndex === "number" ? L.zIndex : i,
526
+ };
527
+ });
528
+ return native.exportPngLayers(packed, exportOpts || {});
529
+ }
530
+
531
+ /**
532
+ * @param {object} native
533
+ * @param {{ unit?: 'inch'|'mm'|'in', precision?: number }} [docOpts]
534
+ */
535
+ function wrapDocument(native, docOpts) {
536
+ const base = docOpts || {};
537
+ /** @type {ReturnType<typeof wrapImage>[]} */
538
+ const layers = [];
539
+ const doc = {
540
+ /**
541
+ * @param {{ name?: string, color?: string|{r,g,b,a}, zIndex?: number }} [layerOpts]
542
+ */
543
+ createLayer(layerOpts) {
544
+ const lo = layerOpts || {};
545
+ const z =
546
+ typeof lo.zIndex === "number" ? lo.zIndex : layers.length;
547
+ const layer = wrapImage(native, native.createImage(base), {
548
+ name: lo.name,
549
+ color: lo.color,
550
+ zIndex: z,
551
+ });
552
+ layers.push(layer);
553
+ return layer;
554
+ },
555
+ get layers() {
556
+ return layers.slice();
557
+ },
558
+ /**
559
+ * 多层导出(按各层 zIndex 升序叠加;低在下)。
560
+ * @param {{
561
+ * layers?: unknown[],
562
+ * width?: number, height?: number, padding?: number, dpi?: number,
563
+ * region?: {minX,minY,maxX,maxY}|{x,y,width,height},
564
+ * background?: string|{r,g,b,a}
565
+ * }} [exportOpts]
566
+ */
567
+ exportPng(exportOpts) {
568
+ const o = exportOpts || {};
569
+ const ls = o.layers || layers;
570
+ const { layers: _drop, ...rest } = o;
571
+ return exportPngLayers(ls, rest);
572
+ },
573
+ };
574
+ return doc;
575
+ }
576
+
324
577
  const addonPath = resolveAddon();
325
578
  const native = require(addonPath);
326
579
 
@@ -334,6 +587,14 @@ module.exports = {
334
587
  createImage(opts) {
335
588
  return wrapImage(native, native.createImage(opts || {}));
336
589
  },
590
+ /**
591
+ * 多图层文档:createLayer + exportPng(zIndex 控制叠加顺序)。
592
+ * @param {{ unit?: 'inch'|'mm'|'in', precision?: number }} [opts]
593
+ */
594
+ createDocument(opts) {
595
+ return wrapDocument(native, opts || {});
596
+ },
597
+ exportPngLayers,
337
598
  /** @deprecated 优先用 img.createPolygon(x, y) */
338
599
  createPolygon(image, x, y) {
339
600
  requireXY("createPolygon", x, y);
@@ -379,4 +640,16 @@ module.exports = {
379
640
  exportDrill(image) {
380
641
  return native.exportDrill(unwrap(image));
381
642
  },
643
+ exportPng(image, opts) {
644
+ return native.exportPng(unwrap(image), opts || {});
645
+ },
646
+ setLayerColor(image, color) {
647
+ native.setLayerColor(unwrap(image), color);
648
+ },
649
+ setObjectColor(image, color) {
650
+ native.setObjectColor(unwrap(image), color == null ? null : color);
651
+ },
652
+ setNetColor(image, color) {
653
+ native.setNetColor(unwrap(image), color == null ? null : color);
654
+ },
382
655
  };