@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
package/lib/spline.js ADDED
@@ -0,0 +1,211 @@
1
+ /**
2
+ * 开均匀夹紧 B 样条(Cox–de Boor)采样 → 折线点。
3
+ * 控制点语义对齐 PCB GeometryNext:段起点为当前点,controls 为中间点,最后一点为终点。
4
+ */
5
+ "use strict";
6
+
7
+ /**
8
+ * @param {unknown} p
9
+ * @returns {{x:number,y:number}}
10
+ */
11
+ function parsePt(p) {
12
+ if (Array.isArray(p) && p.length >= 2) {
13
+ const x = Number(p[0]);
14
+ const y = Number(p[1]);
15
+ if (Number.isFinite(x) && Number.isFinite(y)) return { x, y };
16
+ } else if (p && typeof p === "object") {
17
+ const x = Number(/** @type {{x?:unknown}} */ (p).x);
18
+ const y = Number(/** @type {{y?:unknown}} */ (p).y);
19
+ if (Number.isFinite(x) && Number.isFinite(y)) return { x, y };
20
+ }
21
+ throw new Error("spline point must be [x,y] or {x,y}");
22
+ }
23
+
24
+ /**
25
+ * @param {unknown} points
26
+ * @returns {{x:number,y:number}[]}
27
+ */
28
+ function parsePoints(points) {
29
+ if (!Array.isArray(points) || points.length < 1) {
30
+ throw new Error("spline requires a non-empty points array");
31
+ }
32
+ return points.map(parsePt);
33
+ }
34
+
35
+ /**
36
+ * 开均匀夹紧节点:长度 n+p+2(控制点下标 0..n,次数 p)。
37
+ * @param {number} n
38
+ * @param {number} p
39
+ * @returns {number[]}
40
+ */
41
+ function openUniformKnots(n, p) {
42
+ const m = n + p + 1;
43
+ /** @type {number[]} */
44
+ const U = new Array(m + 1);
45
+ for (let i = 0; i <= p; i++) U[i] = 0;
46
+ for (let i = m - p; i <= m; i++) U[i] = 1;
47
+ const internal = n - p;
48
+ for (let j = 1; j <= internal; j++) {
49
+ U[p + j] = j / (internal + 1);
50
+ }
51
+ return U;
52
+ }
53
+
54
+ /**
55
+ * @param {number} n
56
+ * @param {number} p
57
+ * @param {number} u
58
+ * @param {number[]} U
59
+ */
60
+ function findSpan(n, p, u, U) {
61
+ if (u >= U[n + 1]) return n;
62
+ if (u <= U[p]) return p;
63
+ let low = p;
64
+ let high = n + 1;
65
+ let mid = (low + high) >> 1;
66
+ while (u < U[mid] || u >= U[mid + 1]) {
67
+ if (u < U[mid]) high = mid;
68
+ else low = mid;
69
+ mid = (low + high) >> 1;
70
+ }
71
+ return mid;
72
+ }
73
+
74
+ /**
75
+ * @param {number} i span
76
+ * @param {number} u
77
+ * @param {number} p
78
+ * @param {number[]} U
79
+ * @returns {number[]}
80
+ */
81
+ function basisFuns(i, u, p, U) {
82
+ /** @type {number[]} */
83
+ const N = new Array(p + 1).fill(0);
84
+ /** @type {number[]} */
85
+ const left = new Array(p + 1);
86
+ /** @type {number[]} */
87
+ const right = new Array(p + 1);
88
+ N[0] = 1;
89
+ for (let j = 1; j <= p; j++) {
90
+ left[j] = u - U[i + 1 - j];
91
+ right[j] = U[i + j] - u;
92
+ let saved = 0;
93
+ for (let r = 0; r < j; r++) {
94
+ const denom = right[r + 1] + left[j - r];
95
+ const temp = denom !== 0 ? N[r] / denom : 0;
96
+ N[r] = saved + right[r + 1] * temp;
97
+ saved = left[j - r] * temp;
98
+ }
99
+ N[j] = saved;
100
+ }
101
+ return N;
102
+ }
103
+
104
+ /**
105
+ * @param {{x:number,y:number}[]} ctrlPts 完整控制多边形(含起点与终点)
106
+ * @param {{ degree?: number, knots?: number[], steps?: number }} [opts]
107
+ * @returns {{x:number,y:number}[]} 不含起点的采样点(含终点)
108
+ */
109
+ function sampleBSpline(ctrlPts, opts) {
110
+ const o = opts || {};
111
+ const n = ctrlPts.length - 1;
112
+ if (n < 1) return [];
113
+ if (n === 1) return [ctrlPts[1]];
114
+
115
+ let p = typeof o.degree === "number" && o.degree >= 1 ? (o.degree | 0) : Math.min(3, n);
116
+ if (p > n) p = n;
117
+ if (p < 1) p = 1;
118
+
119
+ let U;
120
+ if (Array.isArray(o.knots) && o.knots.length === n + p + 2) {
121
+ U = o.knots.map(Number);
122
+ } else {
123
+ U = openUniformKnots(n, p);
124
+ }
125
+
126
+ const nSteps = Math.max(4, typeof o.steps === "number" && o.steps > 0 ? (o.steps | 0) : n * 10);
127
+ const u0 = U[p];
128
+ const u1 = U[n + 1];
129
+ /** @type {{x:number,y:number}[]} */
130
+ const out = [];
131
+
132
+ for (let s = 1; s <= nSteps; s++) {
133
+ if (s === nSteps) {
134
+ out.push({ x: ctrlPts[n].x, y: ctrlPts[n].y });
135
+ continue;
136
+ }
137
+ const u = u0 + ((u1 - u0) * s) / nSteps;
138
+ const span = findSpan(n, p, u, U);
139
+ const N = basisFuns(span, u, p, U);
140
+ let x = 0;
141
+ let y = 0;
142
+ for (let i = 0; i <= p; i++) {
143
+ const pt = ctrlPts[span - p + i];
144
+ x += N[i] * pt.x;
145
+ y += N[i] * pt.y;
146
+ }
147
+ out.push({ x, y });
148
+ }
149
+ return out;
150
+ }
151
+
152
+ /**
153
+ * 从当前点 + 追加点采样 B 样条。
154
+ * @param {number} curX
155
+ * @param {number} curY
156
+ * @param {unknown} points 追加控制点(末点为终点)
157
+ * @param {{ degree?: number, knots?: number[], steps?: number }} [opts]
158
+ */
159
+ function sampleSplineFrom(curX, curY, points, opts) {
160
+ const rest = parsePoints(points);
161
+ const ctrl = [{ x: curX, y: curY }, ...rest];
162
+ return sampleBSpline(ctrl, opts);
163
+ }
164
+
165
+ /**
166
+ * 规范化为轮廓命令。
167
+ * @param {unknown} points
168
+ * @param {{ degree?: number, knots?: number[], steps?: number }} [opts]
169
+ */
170
+ function splineCmd(points, opts) {
171
+ const rest = parsePoints(points);
172
+ const o = opts || {};
173
+ const last = rest[rest.length - 1];
174
+ const mid = rest.slice(0, -1).map((p) => [p.x, p.y]);
175
+ /** @type {object} */
176
+ const cmd = {
177
+ t: "S",
178
+ controls: mid,
179
+ x: last.x,
180
+ y: last.y,
181
+ };
182
+ if (typeof o.degree === "number") cmd.degree = o.degree | 0;
183
+ if (Array.isArray(o.knots)) cmd.knots = o.knots.map(Number);
184
+ if (typeof o.steps === "number" && o.steps > 0) cmd.steps = o.steps | 0;
185
+ return cmd;
186
+ }
187
+
188
+ /**
189
+ * @param {number} curX
190
+ * @param {number} curY
191
+ * @param {object} cmd {t:'S', controls, x, y, degree?, knots?, steps?}
192
+ */
193
+ function sampleSplineCmd(curX, curY, cmd) {
194
+ const mid = Array.isArray(cmd.controls) ? cmd.controls : [];
195
+ const points = [...mid, [cmd.x, cmd.y]];
196
+ return sampleSplineFrom(curX, curY, points, {
197
+ degree: cmd.degree,
198
+ knots: cmd.knots,
199
+ steps: cmd.steps,
200
+ });
201
+ }
202
+
203
+ module.exports = {
204
+ parsePt,
205
+ parsePoints,
206
+ openUniformKnots,
207
+ sampleBSpline,
208
+ sampleSplineFrom,
209
+ sampleSplineCmd,
210
+ splineCmd,
211
+ };
@@ -0,0 +1,166 @@
1
+ /**
2
+ * 解析本机可用于 opentype.js 的默认字体(仅 TTF/OTF;TTC 不可用)。
3
+ * 优先环境变量 JS_GERBV_FONT / GERBV_FONT,再按平台候选列表。
4
+ */
5
+ "use strict";
6
+
7
+ const fs = require("fs");
8
+ const os = require("os");
9
+ const path = require("path");
10
+
11
+ /** @type {string|null|undefined} */
12
+ let cachedPath;
13
+ /** @type {string|null|undefined} */
14
+ let cachedCjkPath;
15
+
16
+ function isUsableFontFile(p) {
17
+ if (!p || typeof p !== "string") return false;
18
+ const ext = path.extname(p).toLowerCase();
19
+ if (ext !== ".ttf" && ext !== ".otf") return false;
20
+ try {
21
+ return fs.existsSync(p) && fs.statSync(p).isFile();
22
+ } catch (_) {
23
+ return false;
24
+ }
25
+ }
26
+
27
+ function winFontsDir() {
28
+ const windir = process.env.WINDIR || process.env.SystemRoot || "C:\\Windows";
29
+ return path.join(windir, "Fonts");
30
+ }
31
+
32
+ /**
33
+ * 含 CJK 倾向的候选(opentype 可读的单文件)。
34
+ * @returns {string[]}
35
+ */
36
+ function cjkCandidates() {
37
+ const home = os.homedir();
38
+ if (process.platform === "darwin") {
39
+ return [
40
+ "/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
41
+ "/Library/Fonts/Arial Unicode.ttf",
42
+ path.join(home, "Library/Fonts/Arial Unicode.ttf"),
43
+ // 部分环境有拆开的 Source Han / Noto SC
44
+ path.join(home, "Library/Fonts/NotoSansSC-Regular.otf"),
45
+ path.join(home, "Library/Fonts/SourceHanSansSC-Regular.otf"),
46
+ "/Library/Fonts/NotoSansSC-Regular.otf",
47
+ "/Library/Fonts/SourceHanSansSC-Regular.otf",
48
+ ];
49
+ }
50
+ if (process.platform === "win32") {
51
+ const d = winFontsDir();
52
+ return [
53
+ path.join(d, "Arial Unicode.ttf"),
54
+ // 常见第三方安装
55
+ path.join(d, "NotoSansSC-Regular.otf"),
56
+ path.join(d, "NotoSansCJKsc-Regular.otf"),
57
+ path.join(d, "SourceHanSansSC-Regular.otf"),
58
+ path.join(home, "AppData/Local/Microsoft/Windows/Fonts/NotoSansSC-Regular.otf"),
59
+ ];
60
+ }
61
+ // linux / others
62
+ return [
63
+ "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc", // filtered out by ext
64
+ "/usr/share/fonts/opentype/noto/NotoSansCJKsc-Regular.otf",
65
+ "/usr/share/fonts/opentype/noto/NotoSansSC-Regular.otf",
66
+ "/usr/share/fonts/truetype/noto/NotoSansSC-Regular.otf",
67
+ "/usr/share/fonts/truetype/noto/NotoSansCJKsc-Regular.otf",
68
+ "/usr/share/fonts/opentype/source-han-sans/SourceHanSansSC-Regular.otf",
69
+ "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
70
+ path.join(home, ".local/share/fonts/NotoSansSC-Regular.otf"),
71
+ path.join(home, ".fonts/NotoSansSC-Regular.otf"),
72
+ ];
73
+ }
74
+
75
+ /** 拉丁/通用回退 */
76
+ function latinCandidates() {
77
+ if (process.platform === "darwin") {
78
+ return [
79
+ "/System/Library/Fonts/Supplemental/Arial.ttf",
80
+ "/Library/Fonts/Arial.ttf",
81
+ "/System/Library/Fonts/Supplemental/Times New Roman.ttf",
82
+ "/System/Library/Fonts/Helvetica.ttc",
83
+ ];
84
+ }
85
+ if (process.platform === "win32") {
86
+ const d = winFontsDir();
87
+ return [
88
+ path.join(d, "arial.ttf"),
89
+ path.join(d, "Arial.ttf"),
90
+ path.join(d, "times.ttf"),
91
+ path.join(d, "calibri.ttf"),
92
+ ];
93
+ }
94
+ return [
95
+ "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
96
+ "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
97
+ "/usr/share/fonts/truetype/freefont/FreeSans.ttf",
98
+ "/usr/share/fonts/truetype/noto/NotoSans-Regular.ttf",
99
+ ];
100
+ }
101
+
102
+ function firstExisting(list) {
103
+ for (const p of list) {
104
+ if (isUsableFontFile(p)) return path.resolve(p);
105
+ }
106
+ return null;
107
+ }
108
+
109
+ function envFont() {
110
+ const raw = process.env.JS_GERBV_FONT || process.env.GERBV_FONT || "";
111
+ const p = String(raw).trim();
112
+ if (!p) return null;
113
+ if (isUsableFontFile(p)) return path.resolve(p);
114
+ return null;
115
+ }
116
+
117
+ /**
118
+ * @param {{ preferCjk?: boolean }} [opts]
119
+ * @returns {string|null}
120
+ */
121
+ function resolveSystemFont(opts) {
122
+ const preferCjk = !!(opts && opts.preferCjk);
123
+ const env = envFont();
124
+ if (env) return env;
125
+
126
+ if (preferCjk) {
127
+ if (cachedCjkPath !== undefined && cachedCjkPath) return cachedCjkPath;
128
+ const cjk = firstExisting(cjkCandidates());
129
+ if (cjk) {
130
+ cachedCjkPath = cjk;
131
+ return cjk;
132
+ }
133
+ cachedCjkPath = null;
134
+ } else if (cachedPath !== undefined && cachedPath) {
135
+ return cachedPath;
136
+ }
137
+
138
+ const hit =
139
+ firstExisting(cjkCandidates()) || firstExisting(latinCandidates());
140
+ if (!preferCjk) cachedPath = hit;
141
+ return hit;
142
+ }
143
+
144
+ /** 重置缓存(测试用) */
145
+ function clearSystemFontCache() {
146
+ cachedPath = undefined;
147
+ cachedCjkPath = undefined;
148
+ }
149
+
150
+ /**
151
+ * fontFile 选项是否表示「用系统字体」。
152
+ * @param {unknown} v
153
+ */
154
+ function isSystemFontToken(v) {
155
+ if (v === true) return true;
156
+ if (typeof v !== "string") return false;
157
+ const s = v.trim().toLowerCase();
158
+ return s === "system" || s === "default" || s === "auto";
159
+ }
160
+
161
+ module.exports = {
162
+ resolveSystemFont,
163
+ clearSystemFontCache,
164
+ isSystemFontToken,
165
+ isUsableFontFile,
166
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huaqiu/js-gerbv",
3
- "version": "0.1.2",
3
+ "version": "0.1.26",
4
4
  "description": "Node N-API binding for libgerbv (efa/gerbv) RS-274X/Drill export",
5
5
  "license": "GPL-2.0-or-later",
6
6
  "repository": {
@@ -29,6 +29,7 @@
29
29
  },
30
30
  "files": [
31
31
  "index.js",
32
+ "lib/**",
32
33
  "prebuilds/**",
33
34
  "src/**",
34
35
  "include/**",
@@ -37,10 +38,14 @@
37
38
  "scripts/**",
38
39
  "vcpkg.json",
39
40
  "README.md",
40
- "LICENSE"
41
+ "LICENSE",
42
+ "examples/**"
41
43
  ],
42
44
  "dependencies": {
43
- "node-addon-api": "^8.9.2"
45
+ "earcut": "^3.2.4",
46
+ "node-addon-api": "^8.9.2",
47
+ "opentype.js": "^1.3.4",
48
+ "polygon-clipping": "^0.15.7"
44
49
  },
45
50
  "devDependencies": {
46
51
  "node-api-headers": "^1.9.0"
@@ -5,5 +5,5 @@
5
5
  "node": "v24.20.0",
6
6
  "modules": "137",
7
7
  "napi": "10",
8
- "packedAt": "2026-11-28T08:21:39.525Z"
8
+ "packedAt": "2026-11-29T05:08:33.527Z"
9
9
  }
Binary file
Binary file
@@ -2,8 +2,8 @@
2
2
  "platform": "win32",
3
3
  "arch": "x64",
4
4
  "key": "win32-x64",
5
- "node": "v24.22.1",
5
+ "node": "v24.16.0",
6
6
  "modules": "137",
7
7
  "napi": "10",
8
- "packedAt": "2026-11-28T10:15:58.578Z"
8
+ "packedAt": "2026-11-29T05:29:11.861Z"
9
9
  }
@@ -126,6 +126,14 @@ function resolveNodeLib() {
126
126
  for (const c of candidates) {
127
127
  if (fs.existsSync(c)) return c;
128
128
  }
129
+ // N-API 是 ABI 稳定的;任意已缓存版本的 node.lib 都可用于链接。
130
+ const cacheRoot = path.join(local, "node-gyp", "Cache");
131
+ if (fs.existsSync(cacheRoot)) {
132
+ for (const ver of fs.readdirSync(cacheRoot)) {
133
+ const lib = path.join(cacheRoot, ver, process.arch, "node.lib");
134
+ if (fs.existsSync(lib)) return lib;
135
+ }
136
+ }
129
137
  return "";
130
138
  }
131
139
 
@@ -30,6 +30,12 @@ function findAddon() {
30
30
 
31
31
  function copyFile(src, dst) {
32
32
  fs.mkdirSync(path.dirname(dst), { recursive: true });
33
+ if (path.resolve(src) === path.resolve(dst)) return;
34
+ try {
35
+ fs.unlinkSync(dst);
36
+ } catch (_) {
37
+ /* ok */
38
+ }
33
39
  fs.copyFileSync(src, dst);
34
40
  console.log(" +", path.relative(root, dst));
35
41
  }
@@ -84,29 +90,108 @@ function copyWinRuntimeDlls() {
84
90
  searchDirs.push(path.join(root, "build"));
85
91
 
86
92
  for (const name of dllNames) {
87
- let found = null;
88
93
  for (const dir of searchDirs) {
89
94
  const p = path.join(dir, name);
90
95
  if (fs.existsSync(p)) {
91
- found = p;
96
+ copyFile(p, path.join(outDir, name));
92
97
  break;
93
98
  }
94
99
  }
95
- if (found) copyFile(found, path.join(outDir, name));
100
+ }
101
+
102
+ // 递归解析真实依赖,自动覆盖 cairo / pixman / freetype 等新增运行时库。
103
+ const objdump =
104
+ process.env.OBJDUMP ||
105
+ (fs.existsSync(path.join(msys2Root, "mingw64", "bin", "objdump.exe"))
106
+ ? path.join(msys2Root, "mingw64", "bin", "objdump.exe")
107
+ : "objdump");
108
+ const systemDll =
109
+ /^(kernel32|user32|advapi32|shell32|msvcrt|ntdll|ole32|oleaut32|winmm|ws2_32|gdi32|comdlg32|version|imm32|winspool|shlwapi|userenv|crypt32|bcrypt.*|api-ms-win-.*|vcruntime.*|ucrtbase|msvcp.*|msimg32|dwrite|rpcrt4|usp10)\.dll$/i;
110
+ const queue = [path.join(outDir, "gerbv_node.node")];
111
+ const seen = new Set();
112
+ while (queue.length) {
113
+ const file = queue.shift();
114
+ if (!fs.existsSync(file)) continue;
115
+ const r = spawnSync(objdump, ["-p", file], { encoding: "utf8" });
116
+ if (r.status !== 0 || !r.stdout) continue;
117
+ for (const line of r.stdout.split(/\r?\n/)) {
118
+ const m = line.match(/DLL Name:\s*(\S+)/);
119
+ if (!m) continue;
120
+ const name = m[1];
121
+ if (name.toLowerCase() === "node.exe" || systemDll.test(name)) continue;
122
+ const key = name.toLowerCase();
123
+ if (seen.has(key)) continue;
124
+ seen.add(key);
125
+ const bundled = path.join(outDir, name);
126
+ if (fs.existsSync(bundled)) {
127
+ queue.push(bundled);
128
+ continue;
129
+ }
130
+ let found = null;
131
+ for (const dir of searchDirs) {
132
+ const p = path.join(dir, name);
133
+ if (fs.existsSync(p)) {
134
+ found = p;
135
+ break;
136
+ }
137
+ }
138
+ if (found) {
139
+ copyFile(found, bundled);
140
+ queue.push(bundled);
141
+ } else {
142
+ console.warn(" warn: unresolved DLL", name);
143
+ }
144
+ }
96
145
  }
97
146
  }
98
147
 
99
148
  /**
100
149
  * 解析 otool -L 输出中的非系统依赖路径。
150
+ * 对 @loader_path / @rpath 相对引用,尝试按 dylib 所在目录解析。
101
151
  */
102
152
  function otoolDeps(file) {
103
153
  const out = run("otool", ["-L", file]).stdout || "";
104
154
  const deps = [];
155
+ const baseDir = path.dirname(file);
105
156
  for (const line of out.split("\n").slice(1)) {
106
157
  const m = line.trim().match(/^(\S+)\s+\(/);
107
158
  if (!m) continue;
108
- const p = m[1];
159
+ let p = m[1];
109
160
  if (p.startsWith("/usr/lib/") || p.startsWith("/System/")) continue;
161
+ if (p.startsWith("@executable_path/")) continue;
162
+ if (p.startsWith("@loader_path/") || p.startsWith("@rpath/")) {
163
+ const rel = p.replace(/^@(?:loader_path|rpath)\//, "");
164
+ const candidates = [
165
+ path.join(baseDir, rel),
166
+ path.join(baseDir, "..", "lib", rel),
167
+ path.join("/opt/homebrew/lib", path.basename(rel)),
168
+ path.join("/usr/local/lib", path.basename(rel)),
169
+ ];
170
+ let resolved = null;
171
+ for (const c of candidates) {
172
+ if (fs.existsSync(c)) {
173
+ resolved = c;
174
+ break;
175
+ }
176
+ }
177
+ if (!resolved) {
178
+ // brew cellar 模糊查找同名
179
+ const name = path.basename(rel);
180
+ const brew = run(
181
+ "bash",
182
+ ["-lc", `ls /opt/homebrew/Cellar/*/*/lib/${name} 2>/dev/null | head -1`],
183
+ { allowFail: true }
184
+ );
185
+ const hit = (brew.stdout || "").trim();
186
+ if (hit && fs.existsSync(hit)) resolved = hit;
187
+ }
188
+ if (resolved) {
189
+ deps.push(resolved);
190
+ } else {
191
+ console.warn(" warn: unresolved", p, "from", path.basename(file));
192
+ }
193
+ continue;
194
+ }
110
195
  if (p.startsWith("@")) continue;
111
196
  deps.push(p);
112
197
  }
@@ -121,6 +206,7 @@ function bundleMacDylibs(addonDst) {
121
206
  const seen = new Set();
122
207
  const bundled = []; // { src, name }
123
208
 
209
+ const seenNames = new Set();
124
210
  while (queue.length) {
125
211
  const src = queue.shift();
126
212
  if (!src || seen.has(src)) continue;
@@ -130,6 +216,10 @@ function bundleMacDylibs(addonDst) {
130
216
  continue;
131
217
  }
132
218
  const name = path.basename(src);
219
+ if (name.endsWith(".node")) continue; // 本体已单独拷贝
220
+ // Homebrew 常出现 Cellar/opt 两条路径指向同名 dylib
221
+ if (seenNames.has(name)) continue;
222
+ seenNames.add(name);
133
223
  const dst = path.join(outDir, name);
134
224
  copyFile(src, dst);
135
225
  bundled.push({ src, name, dst });
@@ -160,6 +250,22 @@ function bundleMacDylibs(addonDst) {
160
250
  { allowFail: true }
161
251
  );
162
252
  }
253
+ // 常见 @rpath / @loader_path 引用
254
+ run(
255
+ "install_name_tool",
256
+ ["-change", "@rpath/" + b.name, "@loader_path/" + b.name, target],
257
+ { allowFail: true }
258
+ );
259
+ run(
260
+ "install_name_tool",
261
+ ["-change", "@loader_path/" + b.name, "@loader_path/" + b.name, target],
262
+ { allowFail: true }
263
+ );
264
+ run(
265
+ "install_name_tool",
266
+ ["-change", "@rpath/../lib/" + b.name, "@loader_path/" + b.name, target],
267
+ { allowFail: true }
268
+ );
163
269
  }
164
270
  }
165
271