@huaqiu/js-gerbv 0.0.0-stage → 0.1.2
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.
- package/CMakeLists.txt +151 -0
- package/LICENSE +674 -0
- package/README.md +71 -2
- package/include/config.h +34 -0
- package/include/gettext.h +9 -0
- package/index.js +41 -0
- package/package.json +46 -4
- package/prebuilds/darwin-arm64/gerbv_node.node +0 -0
- package/prebuilds/darwin-arm64/libglib-2.0.0.dylib +0 -0
- package/prebuilds/darwin-arm64/libintl.8.dylib +0 -0
- package/prebuilds/darwin-arm64/libpcre2-8.0.dylib +0 -0
- package/prebuilds/darwin-arm64/meta.json +9 -0
- package/prebuilds/win32-x64/gerbv_node.node +0 -0
- package/prebuilds/win32-x64/libffi-8.dll +0 -0
- package/prebuilds/win32-x64/libgcc_s_seh-1.dll +0 -0
- package/prebuilds/win32-x64/libgio-2.0-0.dll +0 -0
- package/prebuilds/win32-x64/libglib-2.0-0.dll +0 -0
- package/prebuilds/win32-x64/libgmodule-2.0-0.dll +0 -0
- package/prebuilds/win32-x64/libgobject-2.0-0.dll +0 -0
- package/prebuilds/win32-x64/libiconv-2.dll +0 -0
- package/prebuilds/win32-x64/libintl-8.dll +0 -0
- package/prebuilds/win32-x64/libpcre2-8-0.dll +0 -0
- package/prebuilds/win32-x64/libstdc++-6.dll +0 -0
- package/prebuilds/win32-x64/libwinpthread-1.dll +0 -0
- package/prebuilds/win32-x64/meta.json +9 -0
- package/prebuilds/win32-x64/zlib1.dll +0 -0
- package/scripts/cmake-build.js +231 -0
- package/scripts/ensure-gerbv.js +42 -0
- package/scripts/pack-prebuild.js +208 -0
- package/scripts/smoke.js +25 -0
- package/shim/gdk/gdk.h +15 -0
- package/shim/gdk/gdkkeysyms.h +3 -0
- package/shim/gtk/gtk.h +7 -0
- package/src/addon.cc +191 -0
- package/src/gerber_min.c +67 -0
- package/src/gerbv_stubs.c +79 -0
- package/vcpkg.json +8 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 将 build 产物打进 prebuilds/<platform>-<arch>/。
|
|
4
|
+
* Windows:拷贝 glib 等 DLL;macOS:拷贝 glib/intl/pcre2 并改写 @loader_path。
|
|
5
|
+
*/
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
const { spawnSync } = require("child_process");
|
|
9
|
+
const fs = require("fs");
|
|
10
|
+
const path = require("path");
|
|
11
|
+
|
|
12
|
+
const root = path.resolve(__dirname, "..");
|
|
13
|
+
const platform = process.platform;
|
|
14
|
+
const arch = process.arch;
|
|
15
|
+
const key = platform + "-" + arch;
|
|
16
|
+
const outDir = path.join(root, "prebuilds", key);
|
|
17
|
+
|
|
18
|
+
const addonCandidates = [
|
|
19
|
+
path.join(root, "build", "gerbv_node.node"),
|
|
20
|
+
path.join(root, "build", "Release", "gerbv_node.node"),
|
|
21
|
+
path.join(root, "build", "Debug", "gerbv_node.node"),
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
function findAddon() {
|
|
25
|
+
for (const p of addonCandidates) {
|
|
26
|
+
if (fs.existsSync(p)) return p;
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function copyFile(src, dst) {
|
|
32
|
+
fs.mkdirSync(path.dirname(dst), { recursive: true });
|
|
33
|
+
fs.copyFileSync(src, dst);
|
|
34
|
+
console.log(" +", path.relative(root, dst));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function run(cmd, args, opts = {}) {
|
|
38
|
+
const r = spawnSync(cmd, args, { encoding: "utf8" });
|
|
39
|
+
if (r.status !== 0) {
|
|
40
|
+
if (opts.allowFail) return r;
|
|
41
|
+
console.error(cmd, args.join(" "), r.stderr || r.stdout);
|
|
42
|
+
process.exit(r.status == null ? 1 : r.status);
|
|
43
|
+
}
|
|
44
|
+
return r;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function copyWinRuntimeDlls() {
|
|
48
|
+
const dllNames = [
|
|
49
|
+
// vcpkg-style names
|
|
50
|
+
"glib-2.0-0.dll",
|
|
51
|
+
"gobject-2.0-0.dll",
|
|
52
|
+
"gio-2.0-0.dll",
|
|
53
|
+
"gmodule-2.0-0.dll",
|
|
54
|
+
"intl-8.dll",
|
|
55
|
+
"pcre2-8.dll",
|
|
56
|
+
"iconv-2.dll",
|
|
57
|
+
"ffi-8.dll",
|
|
58
|
+
"zlib1.dll",
|
|
59
|
+
// MSYS2 mingw64 names
|
|
60
|
+
"libglib-2.0-0.dll",
|
|
61
|
+
"libgobject-2.0-0.dll",
|
|
62
|
+
"libgio-2.0-0.dll",
|
|
63
|
+
"libgmodule-2.0-0.dll",
|
|
64
|
+
"libintl-8.dll",
|
|
65
|
+
"libpcre2-8-0.dll",
|
|
66
|
+
"libiconv-2.dll",
|
|
67
|
+
"libffi-8.dll",
|
|
68
|
+
"libstdc++-6.dll",
|
|
69
|
+
"libgcc_s_seh-1.dll",
|
|
70
|
+
"libwinpthread-1.dll",
|
|
71
|
+
];
|
|
72
|
+
const searchDirs = [];
|
|
73
|
+
if (process.env.VCPKG_ROOT) {
|
|
74
|
+
searchDirs.push(
|
|
75
|
+
path.join(process.env.VCPKG_ROOT, "installed", "x64-windows", "bin"),
|
|
76
|
+
path.join(process.env.VCPKG_ROOT, "installed", "x86-windows", "bin")
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const msys2Root = process.env.MSYS2_ROOT || "C:/msys64";
|
|
80
|
+
searchDirs.push(path.join(msys2Root, "mingw64", "bin"));
|
|
81
|
+
if (process.env.GERBV_RUNTIME_DIR) {
|
|
82
|
+
searchDirs.push(process.env.GERBV_RUNTIME_DIR);
|
|
83
|
+
}
|
|
84
|
+
searchDirs.push(path.join(root, "build"));
|
|
85
|
+
|
|
86
|
+
for (const name of dllNames) {
|
|
87
|
+
let found = null;
|
|
88
|
+
for (const dir of searchDirs) {
|
|
89
|
+
const p = path.join(dir, name);
|
|
90
|
+
if (fs.existsSync(p)) {
|
|
91
|
+
found = p;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (found) copyFile(found, path.join(outDir, name));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 解析 otool -L 输出中的非系统依赖路径。
|
|
101
|
+
*/
|
|
102
|
+
function otoolDeps(file) {
|
|
103
|
+
const out = run("otool", ["-L", file]).stdout || "";
|
|
104
|
+
const deps = [];
|
|
105
|
+
for (const line of out.split("\n").slice(1)) {
|
|
106
|
+
const m = line.trim().match(/^(\S+)\s+\(/);
|
|
107
|
+
if (!m) continue;
|
|
108
|
+
const p = m[1];
|
|
109
|
+
if (p.startsWith("/usr/lib/") || p.startsWith("/System/")) continue;
|
|
110
|
+
if (p.startsWith("@")) continue;
|
|
111
|
+
deps.push(p);
|
|
112
|
+
}
|
|
113
|
+
return deps;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 打包 macOS 运行时 dylib,并把安装名改成 @loader_path。
|
|
118
|
+
*/
|
|
119
|
+
function bundleMacDylibs(addonDst) {
|
|
120
|
+
const queue = otoolDeps(addonDst);
|
|
121
|
+
const seen = new Set();
|
|
122
|
+
const bundled = []; // { src, name }
|
|
123
|
+
|
|
124
|
+
while (queue.length) {
|
|
125
|
+
const src = queue.shift();
|
|
126
|
+
if (!src || seen.has(src)) continue;
|
|
127
|
+
seen.add(src);
|
|
128
|
+
if (!fs.existsSync(src)) {
|
|
129
|
+
console.warn(" warn: missing dylib", src);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const name = path.basename(src);
|
|
133
|
+
const dst = path.join(outDir, name);
|
|
134
|
+
copyFile(src, dst);
|
|
135
|
+
bundled.push({ src, name, dst });
|
|
136
|
+
for (const d of otoolDeps(src)) {
|
|
137
|
+
if (!seen.has(d)) queue.push(d);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 自身 id → @loader_path/name
|
|
142
|
+
for (const b of bundled) {
|
|
143
|
+
run("install_name_tool", ["-id", "@loader_path/" + b.name, b.dst]);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 重写 addon 与各 dylib 的依赖引用(-change 对不存在的旧路径会失败,允许忽略)
|
|
147
|
+
const rewriteTargets = [addonDst, ...bundled.map((b) => b.dst)];
|
|
148
|
+
for (const target of rewriteTargets) {
|
|
149
|
+
for (const b of bundled) {
|
|
150
|
+
run(
|
|
151
|
+
"install_name_tool",
|
|
152
|
+
["-change", b.src, "@loader_path/" + b.name, target],
|
|
153
|
+
{ allowFail: true }
|
|
154
|
+
);
|
|
155
|
+
const alt = b.src.replace(/\.(\d+)\.dylib$/, ".dylib");
|
|
156
|
+
if (alt !== b.src) {
|
|
157
|
+
run(
|
|
158
|
+
"install_name_tool",
|
|
159
|
+
["-change", alt, "@loader_path/" + b.name, target],
|
|
160
|
+
{ allowFail: true }
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// 确保从 .node 所在目录找 dylib(已存在 rpath 时允许失败)
|
|
167
|
+
run("install_name_tool", ["-add_rpath", "@loader_path", addonDst], {
|
|
168
|
+
allowFail: true,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// 改写装载名后必须重签,否则 SIP/amfid 会直接 SIGKILL(exit 137)
|
|
172
|
+
for (const f of [...bundled.map((b) => b.dst), addonDst]) {
|
|
173
|
+
run("codesign", ["--force", "--sign", "-", f]);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
console.log(
|
|
177
|
+
" mac runtime:",
|
|
178
|
+
bundled.map((b) => b.name).join(", ") || "(none)"
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function main() {
|
|
183
|
+
const addon = findAddon();
|
|
184
|
+
if (!addon) {
|
|
185
|
+
console.error("error: gerbv_node.node not found under build/");
|
|
186
|
+
process.exit(1);
|
|
187
|
+
}
|
|
188
|
+
fs.rmSync(outDir, { recursive: true, force: true });
|
|
189
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
190
|
+
const addonDst = path.join(outDir, "gerbv_node.node");
|
|
191
|
+
copyFile(addon, addonDst);
|
|
192
|
+
if (platform === "win32") copyWinRuntimeDlls();
|
|
193
|
+
if (platform === "darwin") bundleMacDylibs(addonDst);
|
|
194
|
+
|
|
195
|
+
const meta = {
|
|
196
|
+
platform,
|
|
197
|
+
arch,
|
|
198
|
+
key,
|
|
199
|
+
node: process.version,
|
|
200
|
+
modules: process.versions.modules,
|
|
201
|
+
napi: process.versions.napi || null,
|
|
202
|
+
packedAt: new Date().toISOString(),
|
|
203
|
+
};
|
|
204
|
+
fs.writeFileSync(path.join(outDir, "meta.json"), JSON.stringify(meta, null, 2) + "\n");
|
|
205
|
+
console.log("packed prebuilds/" + key);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
main();
|
package/scripts/smoke.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** 最小冒烟:画一条线并导出 RS-274X。 */
|
|
3
|
+
"use strict";
|
|
4
|
+
|
|
5
|
+
const gerbv = require("../index.js");
|
|
6
|
+
const img = gerbv.createImage();
|
|
7
|
+
gerbv.addLine(img, 0, 0, 0.5, 0, 0.01);
|
|
8
|
+
const buf = gerbv.exportRs274x(img);
|
|
9
|
+
if (!buf || buf.length < 32) {
|
|
10
|
+
console.error("export too small", buf && buf.length);
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
const text = buf.toString("utf8");
|
|
14
|
+
if (!text.includes("%MOIN*%") && !text.includes("MOIN")) {
|
|
15
|
+
console.error("unexpected gerber header");
|
|
16
|
+
process.exit(2);
|
|
17
|
+
}
|
|
18
|
+
console.log(
|
|
19
|
+
"smoke ok backend=",
|
|
20
|
+
gerbv.backend,
|
|
21
|
+
"path=",
|
|
22
|
+
gerbv.__addonPath,
|
|
23
|
+
"bytes=",
|
|
24
|
+
buf.length
|
|
25
|
+
);
|
package/shim/gdk/gdk.h
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/* GDK stub for headless libgerbv (export-only). */
|
|
2
|
+
#ifndef GERBV_SHIM_GDK_H
|
|
3
|
+
#define GERBV_SHIM_GDK_H
|
|
4
|
+
#include <glib.h>
|
|
5
|
+
typedef struct {
|
|
6
|
+
guint16 red;
|
|
7
|
+
guint16 green;
|
|
8
|
+
guint16 blue;
|
|
9
|
+
guint16 pixel;
|
|
10
|
+
} GdkColor;
|
|
11
|
+
typedef struct _GdkPixmap GdkPixmap;
|
|
12
|
+
typedef struct _GdkDrawable GdkDrawable;
|
|
13
|
+
typedef struct _GdkGC GdkGC;
|
|
14
|
+
typedef struct _GdkWindow GdkWindow;
|
|
15
|
+
#endif
|
package/shim/gtk/gtk.h
ADDED
package/src/addon.cc
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node N-API 绑定:libgerbv 几何绘制 → RS-274X / Excellon。
|
|
3
|
+
* 坐标单位:英寸(与 libgerbv create_* API 一致);调用方负责 mm→inch。
|
|
4
|
+
*/
|
|
5
|
+
#include <napi.h>
|
|
6
|
+
|
|
7
|
+
// glib 2.90 的 gwin32.h 含 C++ 模板,必须在 extern "C" 之外先包含一次;
|
|
8
|
+
// 之后 gerbv.h 内的 glib.h 会被 include guard 跳过。
|
|
9
|
+
// 同时让 libintl.h 在 config.h 定义 gettext 宏之前解析(Linux)。
|
|
10
|
+
#include <glib.h>
|
|
11
|
+
|
|
12
|
+
extern "C" {
|
|
13
|
+
#include "gerbv.h"
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
#include <cstdio>
|
|
17
|
+
#include <filesystem>
|
|
18
|
+
#include <string>
|
|
19
|
+
#include <unistd.h>
|
|
20
|
+
|
|
21
|
+
namespace {
|
|
22
|
+
|
|
23
|
+
struct ImageHandle {
|
|
24
|
+
gerbv_image_t* image = nullptr;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
void FreeImage(Napi::Env, ImageHandle* h) {
|
|
28
|
+
if (h) {
|
|
29
|
+
if (h->image) gerbv_destroy_image(h->image);
|
|
30
|
+
delete h;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
using ImageExt = Napi::External<ImageHandle>;
|
|
35
|
+
|
|
36
|
+
ImageHandle* Unwrap(const Napi::Value& v) {
|
|
37
|
+
if (!v.IsExternal()) return nullptr;
|
|
38
|
+
return v.As<ImageExt>().Data();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
Napi::Value CreateImage(const Napi::CallbackInfo& info) {
|
|
42
|
+
Napi::Env env = info.Env();
|
|
43
|
+
auto* h = new ImageHandle();
|
|
44
|
+
h->image = gerbv_create_image(nullptr, "hqpcb-lite");
|
|
45
|
+
if (!h->image) {
|
|
46
|
+
delete h;
|
|
47
|
+
Napi::Error::New(env, "gerbv_create_image failed").ThrowAsJavaScriptException();
|
|
48
|
+
return env.Null();
|
|
49
|
+
}
|
|
50
|
+
return ImageExt::New(env, h, FreeImage);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
Napi::Value AddLine(const Napi::CallbackInfo& info) {
|
|
54
|
+
Napi::Env env = info.Env();
|
|
55
|
+
if (info.Length() < 6) {
|
|
56
|
+
Napi::TypeError::New(env, "addLine(image,x1,y1,x2,y2,widthInch)").ThrowAsJavaScriptException();
|
|
57
|
+
return env.Undefined();
|
|
58
|
+
}
|
|
59
|
+
auto* h = Unwrap(info[0]);
|
|
60
|
+
if (!h || !h->image) {
|
|
61
|
+
Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
|
|
62
|
+
return env.Undefined();
|
|
63
|
+
}
|
|
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();
|
|
69
|
+
gerbv_image_create_line_object(h->image, x1, y1, x2, y2, w, GERBV_APTYPE_CIRCLE);
|
|
70
|
+
return env.Undefined();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
Napi::Value AddFlashCircle(const Napi::CallbackInfo& info) {
|
|
74
|
+
Napi::Env env = info.Env();
|
|
75
|
+
if (info.Length() < 4) {
|
|
76
|
+
Napi::TypeError::New(env, "addFlashCircle(image,x,y,diamInch)").ThrowAsJavaScriptException();
|
|
77
|
+
return env.Undefined();
|
|
78
|
+
}
|
|
79
|
+
auto* h = Unwrap(info[0]);
|
|
80
|
+
if (!h || !h->image) {
|
|
81
|
+
Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
|
|
82
|
+
return env.Undefined();
|
|
83
|
+
}
|
|
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();
|
|
87
|
+
// 同点起止 → libgerbv 走 FLASH
|
|
88
|
+
gerbv_image_create_line_object(h->image, x, y, x, y, d, GERBV_APTYPE_CIRCLE);
|
|
89
|
+
return env.Undefined();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
Napi::Value AddRect(const Napi::CallbackInfo& info) {
|
|
93
|
+
Napi::Env env = info.Env();
|
|
94
|
+
if (info.Length() < 5) {
|
|
95
|
+
Napi::TypeError::New(env, "addRect(image,x,y,widthInch,heightInch)").ThrowAsJavaScriptException();
|
|
96
|
+
return env.Undefined();
|
|
97
|
+
}
|
|
98
|
+
auto* h = Unwrap(info[0]);
|
|
99
|
+
if (!h || !h->image) {
|
|
100
|
+
Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
|
|
101
|
+
return env.Undefined();
|
|
102
|
+
}
|
|
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();
|
|
107
|
+
gerbv_image_create_rectangle_object(h->image, x, y, w, ht);
|
|
108
|
+
return env.Undefined();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
std::string ReadFileBytes(const char* path) {
|
|
112
|
+
FILE* f = std::fopen(path, "rb");
|
|
113
|
+
if (!f) return {};
|
|
114
|
+
std::fseek(f, 0, SEEK_END);
|
|
115
|
+
long n = std::ftell(f);
|
|
116
|
+
std::fseek(f, 0, SEEK_SET);
|
|
117
|
+
if (n <= 0) {
|
|
118
|
+
std::fclose(f);
|
|
119
|
+
return {};
|
|
120
|
+
}
|
|
121
|
+
std::string out(static_cast<size_t>(n), '\0');
|
|
122
|
+
const size_t got = std::fread(out.data(), 1, static_cast<size_t>(n), f);
|
|
123
|
+
std::fclose(f);
|
|
124
|
+
out.resize(got);
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
std::string TempPath(const char* suffix) {
|
|
129
|
+
static unsigned counter = 0;
|
|
130
|
+
const auto name =
|
|
131
|
+
"hqpcb-gerbv-" + std::to_string(getpid()) + "-" +
|
|
132
|
+
std::to_string(counter++) + suffix;
|
|
133
|
+
return (std::filesystem::temp_directory_path() / name).string();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
Napi::Value ExportRs274x(const Napi::CallbackInfo& info) {
|
|
137
|
+
Napi::Env env = info.Env();
|
|
138
|
+
if (info.Length() < 1) {
|
|
139
|
+
Napi::TypeError::New(env, "exportRs274x(image)").ThrowAsJavaScriptException();
|
|
140
|
+
return env.Null();
|
|
141
|
+
}
|
|
142
|
+
auto* h = Unwrap(info[0]);
|
|
143
|
+
if (!h || !h->image) {
|
|
144
|
+
Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
|
|
145
|
+
return env.Null();
|
|
146
|
+
}
|
|
147
|
+
const std::string path = TempPath(".gbr");
|
|
148
|
+
if (!gerbv_export_rs274x_file_from_image(path.c_str(), h->image, nullptr)) {
|
|
149
|
+
Napi::Error::New(env, "gerbv_export_rs274x_file_from_image failed").ThrowAsJavaScriptException();
|
|
150
|
+
return env.Null();
|
|
151
|
+
}
|
|
152
|
+
std::string data = ReadFileBytes(path.c_str());
|
|
153
|
+
std::remove(path.c_str());
|
|
154
|
+
return Napi::Buffer<char>::Copy(env, data.data(), data.size());
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
Napi::Value ExportDrill(const Napi::CallbackInfo& info) {
|
|
158
|
+
Napi::Env env = info.Env();
|
|
159
|
+
if (info.Length() < 1) {
|
|
160
|
+
Napi::TypeError::New(env, "exportDrill(image)").ThrowAsJavaScriptException();
|
|
161
|
+
return env.Null();
|
|
162
|
+
}
|
|
163
|
+
auto* h = Unwrap(info[0]);
|
|
164
|
+
if (!h || !h->image) {
|
|
165
|
+
Napi::Error::New(env, "invalid image").ThrowAsJavaScriptException();
|
|
166
|
+
return env.Null();
|
|
167
|
+
}
|
|
168
|
+
const std::string path = TempPath(".drl");
|
|
169
|
+
if (!gerbv_export_drill_file_from_image(path.c_str(), h->image, nullptr)) {
|
|
170
|
+
Napi::Error::New(env, "gerbv_export_drill_file_from_image failed").ThrowAsJavaScriptException();
|
|
171
|
+
return env.Null();
|
|
172
|
+
}
|
|
173
|
+
std::string data = ReadFileBytes(path.c_str());
|
|
174
|
+
std::remove(path.c_str());
|
|
175
|
+
return Napi::Buffer<char>::Copy(env, data.data(), data.size());
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
179
|
+
exports.Set("createImage", Napi::Function::New(env, CreateImage));
|
|
180
|
+
exports.Set("addLine", Napi::Function::New(env, AddLine));
|
|
181
|
+
exports.Set("addFlashCircle", Napi::Function::New(env, AddFlashCircle));
|
|
182
|
+
exports.Set("addRect", Napi::Function::New(env, AddRect));
|
|
183
|
+
exports.Set("exportRs274x", Napi::Function::New(env, ExportRs274x));
|
|
184
|
+
exports.Set("exportDrill", Napi::Function::New(env, ExportDrill));
|
|
185
|
+
exports.Set("backend", Napi::String::New(env, "libgerbv"));
|
|
186
|
+
return exports;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
} // namespace
|
|
190
|
+
|
|
191
|
+
NODE_API_MODULE(gerbv_node, Init)
|
package/src/gerber_min.c
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 从 efa/gerbv gerber.c 抽出的最小子集:创建 aperture/net + bbox 更新(单位 inch,无 cairo)。
|
|
3
|
+
*/
|
|
4
|
+
#include "gerbv.h"
|
|
5
|
+
#include "gerber.h"
|
|
6
|
+
#include <math.h>
|
|
7
|
+
#include <stdlib.h>
|
|
8
|
+
|
|
9
|
+
gerbv_net_t*
|
|
10
|
+
gerber_create_new_net(gerbv_net_t* currentNet, gerbv_layer_t* layer, gerbv_netstate_t* state) {
|
|
11
|
+
gerbv_net_t* newNet = g_new0(gerbv_net_t, 1);
|
|
12
|
+
currentNet->next = newNet;
|
|
13
|
+
if (layer)
|
|
14
|
+
newNet->layer = layer;
|
|
15
|
+
else
|
|
16
|
+
newNet->layer = currentNet->layer;
|
|
17
|
+
if (state)
|
|
18
|
+
newNet->state = state;
|
|
19
|
+
else
|
|
20
|
+
newNet->state = currentNet->state;
|
|
21
|
+
return newNet;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
gboolean
|
|
25
|
+
gerber_create_new_aperture(
|
|
26
|
+
gerbv_image_t* image, int* indexNumber, gerbv_aperture_type_t apertureType, gdouble parameter1, gdouble parameter2
|
|
27
|
+
) {
|
|
28
|
+
int i;
|
|
29
|
+
for (i = APERTURE_MIN; i < APERTURE_MAX; i++) {
|
|
30
|
+
if (image->aperture[i] == NULL) {
|
|
31
|
+
image->aperture[i] = g_new0(gerbv_aperture_t, 1);
|
|
32
|
+
image->aperture[i]->type = apertureType;
|
|
33
|
+
image->aperture[i]->parameter[0] = parameter1;
|
|
34
|
+
image->aperture[i]->parameter[1] = parameter2;
|
|
35
|
+
*indexNumber = i;
|
|
36
|
+
return TRUE;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return FALSE;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
void
|
|
43
|
+
gerber_update_image_min_max(
|
|
44
|
+
gerbv_render_size_t* boundingBox, double repeat_off_X, double repeat_off_Y, gerbv_image_t* image
|
|
45
|
+
) {
|
|
46
|
+
image->info->min_x = MIN(image->info->min_x, boundingBox->left);
|
|
47
|
+
image->info->min_y = MIN(image->info->min_y, boundingBox->bottom);
|
|
48
|
+
image->info->max_x = MAX(image->info->max_x, boundingBox->right + repeat_off_X);
|
|
49
|
+
image->info->max_y = MAX(image->info->max_y, boundingBox->top + repeat_off_Y);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
void
|
|
53
|
+
gerber_update_min_and_max(
|
|
54
|
+
gerbv_render_size_t* boundingBox, gdouble x, gdouble y, gdouble apertureSizeX1, gdouble apertureSizeX2,
|
|
55
|
+
gdouble apertureSizeY1, gdouble apertureSizeY2
|
|
56
|
+
) {
|
|
57
|
+
gdouble ourX1 = x - apertureSizeX1, ourY1 = y - apertureSizeY1;
|
|
58
|
+
gdouble ourX2 = x + apertureSizeX2, ourY2 = y + apertureSizeY2;
|
|
59
|
+
boundingBox->left = MIN(boundingBox->left, ourX1);
|
|
60
|
+
boundingBox->left = MIN(boundingBox->left, ourX2);
|
|
61
|
+
boundingBox->right = MAX(boundingBox->right, ourX1);
|
|
62
|
+
boundingBox->right = MAX(boundingBox->right, ourX2);
|
|
63
|
+
boundingBox->bottom = MIN(boundingBox->bottom, ourY1);
|
|
64
|
+
boundingBox->bottom = MIN(boundingBox->bottom, ourY2);
|
|
65
|
+
boundingBox->top = MAX(boundingBox->top, ourY1);
|
|
66
|
+
boundingBox->top = MAX(boundingBox->top, ourY2);
|
|
67
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 无 GTK/UI 的 stub + 从 gerbv.c 抽出的坐标变换/属性复制(供 duplicate_image)。
|
|
3
|
+
*/
|
|
4
|
+
#include "gerbv.h"
|
|
5
|
+
#include <math.h>
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
#include <stdio.h>
|
|
9
|
+
|
|
10
|
+
void free_amacro(gerbv_amacro_t* amacro) {
|
|
11
|
+
(void)amacro;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
void gerbv_attribute_destroy_HID_attribute(gerbv_HID_Attribute* attributeList, int n_attr) {
|
|
15
|
+
int i;
|
|
16
|
+
for (i = 0; i < n_attr; i++) {
|
|
17
|
+
if ((attributeList[i].type == HID_String || attributeList[i].type == HID_Label)
|
|
18
|
+
&& attributeList[i].default_val.str_value != NULL) {
|
|
19
|
+
free(attributeList[i].default_val.str_value);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (attributeList != NULL) free(attributeList);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
gerbv_HID_Attribute* gerbv_attribute_dup(gerbv_HID_Attribute* attributeList, int n_attr) {
|
|
26
|
+
gerbv_HID_Attribute* nl;
|
|
27
|
+
int i;
|
|
28
|
+
if (n_attr <= 0 || attributeList == NULL) return NULL;
|
|
29
|
+
nl = (gerbv_HID_Attribute*)malloc((size_t)n_attr * sizeof(gerbv_HID_Attribute));
|
|
30
|
+
if (nl == NULL) {
|
|
31
|
+
fprintf(stderr, "malloc failed in gerbv_attribute_dup\n");
|
|
32
|
+
exit(1);
|
|
33
|
+
}
|
|
34
|
+
for (i = 0; i < n_attr; i++) {
|
|
35
|
+
if (attributeList[i].type == HID_String || attributeList[i].type == HID_Label) {
|
|
36
|
+
nl[i] = attributeList[i];
|
|
37
|
+
if (attributeList[i].default_val.str_value != NULL)
|
|
38
|
+
nl[i].default_val.str_value = strdup(attributeList[i].default_val.str_value);
|
|
39
|
+
else
|
|
40
|
+
nl[i].default_val.str_value = NULL;
|
|
41
|
+
} else {
|
|
42
|
+
nl[i] = attributeList[i];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return nl;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const char* gerbv_interpolation_name(gerbv_interpolation_t interp) {
|
|
49
|
+
static const char* names[] = {
|
|
50
|
+
"1X linear", "10X linear", "0.1X linear", "0.01X linear", "CW circular",
|
|
51
|
+
"CCW circular", "poly area start", "poly area stop", "deleted",
|
|
52
|
+
};
|
|
53
|
+
if (interp >= 0 && (size_t)interp < sizeof(names) / sizeof(names[0])) return names[interp];
|
|
54
|
+
return "<undefined>";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
void gerbv_rotate_coord(double* x, double* y, double rad) {
|
|
58
|
+
double x0 = *x;
|
|
59
|
+
*x = x0 * cos(rad) - *y * sin(rad);
|
|
60
|
+
*y = x0 * sin(rad) + *y * cos(rad);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
void gerbv_transform_coord(double* x, double* y, const gerbv_user_transformation_t* trans) {
|
|
64
|
+
*x = trans->scaleX * *x;
|
|
65
|
+
*y = trans->scaleY * *y;
|
|
66
|
+
gerbv_rotate_coord(x, y, trans->rotation);
|
|
67
|
+
if (trans->mirrorAroundY) *x = -*x;
|
|
68
|
+
if (trans->mirrorAroundX) *y = -*y;
|
|
69
|
+
*x += trans->translateX;
|
|
70
|
+
*y += trans->translateY;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
void gerbv_stats_destroy(gerbv_stats_t* stats) {
|
|
74
|
+
if (stats) g_free(stats);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
void gerbv_drill_stats_destroy(gerbv_drill_stats_t* stats) {
|
|
78
|
+
if (stats) g_free(stats);
|
|
79
|
+
}
|