@huaqiu/js-gerbv
v0.2.2
Published
Pure JS/TS-friendly Gerber RS-274X / Excellon / PNG export from geometry
Keywords
Readme
js-gerbv
纯 JS 实现:从几何构建导出 RS-274X / Excellon / PNG(原 Node N-API + efa/gerbv 路径的原地替换)。
仓库:https://github.com/Huaqiu-Electronics/js-gerbv
package.json仍标注 GPL-2.0-or-later(历史沿用)。本版本已不再链接 libgerbv。
布局
index.js # 对外 API(流式 image / document)
lib/backend/ # 纯 JS 场景图 + RS-274X / Drill / PNG
lib/add-text.js # Hershey / OpenType 文字
lib/fill-holes.js # 孔 sanitize + H/V cut-in
lib/spline.js # B 样条采样无 prebuild、无 CMake、无原生插件;npm install 即可用。
使用
npm install @huaqiu/js-gerbv
# 或本地:
npm install
npm run smokeconst gerbv = require("@huaqiu/js-gerbv");
// unit: 输入单位(内部换算为 inch);precision: 换算后 inch 小数位
const img = gerbv.createImage({ unit: "mm", precision: 6 });
const pl = img.createPolyline(0, 0, 0.25);
pl.addPolylineLine(12.7, 0);
pl.addPolylineArc(6.35, 0, 6.35, 6.35, false);
pl.addPolylineSpline([[8, 8], [10, 4], [12, 6]], { degree: 3 });
pl.endPolyline();
const poly = img.createPolygon(0, 0);
poly.addPolygonLine(12.7, 0);
poly.addPolygonArc(12.7, 6.35, 12.7, 12.7, false);
poly.addPolygonBezier2(6.35, 15, 0, 12.7);
const hole = poly.createHole(3, 3);
hole.addPolygonArc(5, 5, 7, 3, false);
hole.addPolygonSpline([[5.5, 7], [4, 6], [3, 3]], { degree: 3 });
hole.endHole();
poly.addCircleHole(9, 6, 2);
poly.endArea();
img.addRect(15, 0, 10, 8).addCircleHole(20, 4, 2.5).endArea();
img.addCircle(30, 4, 6).endArea();
img.addCircleRing(40, 4, 8, 1.5);
img.addText(0, 12, "SILK", { height: 2, strokeWidth: 0.2 });
img.addText(0, 8, "中文", { height: 2, fontFile: "system" });
img.setLayerColor("#c9a227");
img.setObjectColor("#ff4d4f");
img.setNetColor("#40c463");
const gbr = img.exportRs274x();
const png = img.exportPng({
dpi: 150,
region: { x: 0, y: 0, width: 50, height: 30 },
background: "#1a1a2e",
padding: 24,
});
const doc = gerbv.createDocument({ unit: "mm", precision: 6 });
const mask = doc.createLayer({ name: "F.Mask", color: "#1b4332", zIndex: 0 });
const copper = doc.createLayer({ name: "F.Cu", color: "#d4b21a", zIndex: 5 });
const multi = doc.exportPng({ dpi: 150, region: { minX: 0, minY: 0, maxX: 50, maxY: 30 } });输入与导出单位由 createImage({ unit }) 指定(默认 mm → %MOMM*%;inch → %MOIN*%)。坐标与孔径按该单位存储并写出;precision 为保留的小数位。PNG 的 dpi 仍按物理 inch 换算。
颜色优先级(绘制时写入每个图元):网络色 > 对象色 > 层色。
backend 常量为 "pure-js"。
示例(按类别输出到 examples/out/<category>/,并同时写出预期):
npm run example
# 只跑挖孔 + 曲线:
node examples/example.js examples/out holes curves每个用例生成:
<stem>.gbr/.png(钻孔为.exc)<stem>.expected.json— 预期说明、checks、自动校验结果- 汇总:
EXPECTED.md、expected-index.json
| 目录 | 内容 |
|------|------|
| demo/ | 综合概览 |
| shapes/ | 基础图元、光圈复用、焊盘阵列、圆角矩形、圆环 |
| holes/ | 单孔/多孔/压边/重叠并集/阵列/贴边/弧·贝塞尔重叠 |
| curves/ | 弧·贝塞尔·样条、CW/CCW、花瓣孔、螺旋弧 |
| strokes/ | 多段线、变宽、闭合环、线束 |
| text/ | 丝印、对齐、多角度 |
| colors/ | 层/对象/网络色优先级 |
| layers/ | 多层叠图 |
| drill/ | Excellon 钻孔、多刀具 |
| API | 说明 |
|-----|------|
| createImage({ unit?, precision? }) | 返回方法式 image |
| createDocument({ unit?, precision? }) | 多图层文档 |
| doc.createLayer({ name?, color?, zIndex? }) | 新建层 |
| img.setLayerColor / setColor | 层默认色 |
| img.setObjectColor(c\|null) | 对象色 |
| img.setNetColor(c\|null) | 网络色(最高优先) |
| img.addText(x,y,text,opts?) | 矢量文字;ASCII 默认 Hershey;fontFile:"system"/路径或非 ASCII 自动系统字体 |
| img.createPolyline(x,y,w) | 多段线起点 + 线宽 |
| img.createPolygon(x,y) | region 起点 |
| poly.createHole / addCircleHole / endArea | 孔与闭合 |
| img.exportRs274x() / exportDrill() | 导出 Buffer |
| img.exportPng({ dpi?, region?, width?, … }) | 单层 PNG(纯 JS 栅格) |
| doc.exportPng / exportPngLayers | 多层 PNG |
性能基准
npm run bench # scale=1;PNG 仅跑 png-dense
npm run bench:png # 各场景都导出 PNG
npm run bench:heavy # scale=2 + 全 PNG
node scripts/bench.js --scale=4 --png --out=/tmp/gerbv-bench报告写入 examples/out/bench/bench-scaleN.json(终端同步打表)。
CI
.github/workflows/ci.yml:macOS / Linux / Windows 上 npm install + npm run smoke(无需原生工具链)。
