@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/index.js CHANGED
@@ -1,11 +1,19 @@
1
1
  /**
2
2
  * 加载当前平台的 gerbv_node 原生插件。
3
- * 优先 prebuilds/<platform>-<arch>/,其次 build/。
3
+ *
4
+ * createPolygon(x,y) / createHole(x,y) / createPolyline(x,y,w):
5
+ * 起点在 create*;后续 add* 只给下一点 / 终点。
4
6
  */
5
7
  "use strict";
6
8
 
7
9
  const fs = require("fs");
8
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");
9
17
 
10
18
  function platformKey() {
11
19
  return process.platform + "-" + process.arch;
@@ -35,7 +43,613 @@ function resolveAddon() {
35
43
  );
36
44
  }
37
45
 
46
+ /** @param {unknown} obj */
47
+ function unwrap(obj) {
48
+ if (obj && typeof obj === "object" && "__handle" in obj) {
49
+ return /** @type {{ __handle: unknown }} */ (obj).__handle;
50
+ }
51
+ return obj;
52
+ }
53
+
54
+ function requireXY(api, x, y) {
55
+ if (typeof x !== "number" || typeof y !== "number" || Number.isNaN(x) || Number.isNaN(y)) {
56
+ throw new Error(api + "(x, y) requires numeric start point");
57
+ }
58
+ }
59
+
60
+ /** 矩形外环(左下角 + 宽高) */
61
+ function seedRectCmds(x, y, w, h) {
62
+ return [
63
+ { t: "L", x, y },
64
+ { t: "L", x: x + w, y },
65
+ { t: "L", x: x + w, y: y + h },
66
+ { t: "L", x, y: y + h },
67
+ ];
68
+ }
69
+
70
+ /** 圆形外环:均匀折线(避免两段半圆弧在接合处留下缺口) */
71
+ function seedCircleCmds(cx, cy, diam, segments) {
72
+ const r = diam / 2;
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;
107
+ }
108
+
109
+ /**
110
+ * @param {object} native
111
+ * @param {unknown} imageHandle
112
+ * @param {object[]} cmds
113
+ * @param {boolean} clear
114
+ */
115
+ function replayContour(native, imageHandle, cmds, clear) {
116
+ if (!cmds.length) return;
117
+ const h = native.createPolygon(imageHandle, !!clear);
118
+ let curX = 0;
119
+ let curY = 0;
120
+ let has = false;
121
+ for (const c of cmds) {
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") {
128
+ native.addPolygonArc(h, c.cx, c.cy, c.x, c.y, !!c.cw);
129
+ curX = c.x;
130
+ curY = c.y;
131
+ } else if (c.t === "B2") {
132
+ native.addPolygonBezier2(h, c.c1x, c.c1y, c.x, c.y);
133
+ curX = c.x;
134
+ curY = c.y;
135
+ } else if (c.t === "B3") {
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
+ }
146
+ }
147
+ native.endPolygon(h);
148
+ }
149
+
150
+ /**
151
+ * 外环:add* 只追加下一点;createHole(x,y) / addCircleHole / endArea。
152
+ * @param {object[]} cmds
153
+ * @param {object} extras
154
+ */
155
+ function wrapOuter(cmds, extras) {
156
+ const contour = {
157
+ addPolygonLine(x, y) {
158
+ cmds.push({ t: "L", x, y });
159
+ return contour;
160
+ },
161
+ addPolygonArc(cx, cy, x, y, clockwise) {
162
+ cmds.push({ t: "A", cx, cy, x, y, cw: !!clockwise });
163
+ return contour;
164
+ },
165
+ addPolygonBezier2(c1x, c1y, x, y) {
166
+ cmds.push({ t: "B2", c1x, c1y, x, y });
167
+ return contour;
168
+ },
169
+ addPolygonBezier3(c1x, c1y, c2x, c2y, x, y) {
170
+ cmds.push({ t: "B3", c1x, c1y, c2x, c2y, x, y });
171
+ return contour;
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
+ },
182
+ createHole: extras.createHole,
183
+ addCircleHole: extras.addCircleHole,
184
+ endArea: extras.endArea,
185
+ };
186
+ return contour;
187
+ }
188
+
189
+ /**
190
+ * 孔:起点已在 createHole(x,y);add* 只给下一点;endHole 结束。
191
+ * @param {object[]} cmds
192
+ */
193
+ function wrapHole(cmds) {
194
+ let ended = false;
195
+ function ensureOpen(api) {
196
+ if (ended) throw new Error(api + ": hole already ended");
197
+ }
198
+ const hole = {
199
+ addPolygonLine(x, y) {
200
+ ensureOpen("addPolygonLine");
201
+ cmds.push({ t: "L", x, y });
202
+ return hole;
203
+ },
204
+ addPolygonArc(cx, cy, x, y, clockwise) {
205
+ ensureOpen("addPolygonArc");
206
+ cmds.push({ t: "A", cx, cy, x, y, cw: !!clockwise });
207
+ return hole;
208
+ },
209
+ addPolygonBezier2(c1x, c1y, x, y) {
210
+ ensureOpen("addPolygonBezier2");
211
+ cmds.push({ t: "B2", c1x, c1y, x, y });
212
+ return hole;
213
+ },
214
+ addPolygonBezier3(c1x, c1y, c2x, c2y, x, y) {
215
+ ensureOpen("addPolygonBezier3");
216
+ cmds.push({ t: "B3", c1x, c1y, c2x, c2y, x, y });
217
+ return hole;
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
+ },
229
+ endHole() {
230
+ ended = true;
231
+ },
232
+ };
233
+ return hole;
234
+ }
235
+
236
+ /**
237
+ * @param {object} native
238
+ * @param {unknown} imageHandle
239
+ * @param {object[]} seedOuter 至少含起点
240
+ */
241
+ function wrapRegion(native, imageHandle, seedOuter) {
242
+ const { fillWithHolesNoLpc } = require("./lib/fill-holes.js");
243
+ const outerCmds = seedOuter.slice();
244
+ /** @type {object[][]} */
245
+ const holes = [];
246
+ let finished = false;
247
+
248
+ const region = wrapOuter(outerCmds, {
249
+ createHole(x, y) {
250
+ if (finished) throw new Error("polygon already ended");
251
+ requireXY("createHole", x, y);
252
+ const holeCmds = [{ t: "L", x, y }];
253
+ holes.push(holeCmds);
254
+ return wrapHole(holeCmds);
255
+ },
256
+ addCircleHole(cx, cy, diam) {
257
+ if (finished) throw new Error("polygon already ended");
258
+ holes.push(seedCircleCmds(cx, cy, diam));
259
+ return region;
260
+ },
261
+ endArea() {
262
+ if (finished) return;
263
+ finished = 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;
270
+ }
271
+ replayContour(native, imageHandle, outerCmds, false);
272
+ },
273
+ });
274
+ return region;
275
+ }
276
+
277
+ /**
278
+ * 多段线描边:createPolyline(x,y,width);add* 只给下一点;endPolyline。
279
+ * @param {object} native
280
+ * @param {unknown} imageHandle
281
+ * @param {number} x0
282
+ * @param {number} y0
283
+ * @param {number} width
284
+ */
285
+ function wrapPolyline(native, imageHandle, x0, y0, width) {
286
+ let curX = x0;
287
+ let curY = y0;
288
+ let finished = false;
289
+ function ensureOpen(api) {
290
+ if (finished) throw new Error(api + ": polyline already ended");
291
+ }
292
+ const pl = {
293
+ addPolylineLine(x, y) {
294
+ ensureOpen("addPolylineLine");
295
+ native.addLine(imageHandle, curX, curY, x, y, width);
296
+ curX = x;
297
+ curY = y;
298
+ return pl;
299
+ },
300
+ addPolylineArc(cx, cy, x, y, clockwise) {
301
+ ensureOpen("addPolylineArc");
302
+ native.addArc(imageHandle, cx, cy, curX, curY, x, y, width, !!clockwise);
303
+ curX = x;
304
+ curY = y;
305
+ return pl;
306
+ },
307
+ addPolylineBezier2(c1x, c1y, x, y) {
308
+ ensureOpen("addPolylineBezier2");
309
+ native.addBezier2(imageHandle, curX, curY, c1x, c1y, x, y, width);
310
+ curX = x;
311
+ curY = y;
312
+ return pl;
313
+ },
314
+ addPolylineBezier3(c1x, c1y, c2x, c2y, x, y) {
315
+ ensureOpen("addPolylineBezier3");
316
+ native.addBezier3(
317
+ imageHandle,
318
+ curX,
319
+ curY,
320
+ c1x,
321
+ c1y,
322
+ c2x,
323
+ c2y,
324
+ x,
325
+ y,
326
+ width
327
+ );
328
+ curX = x;
329
+ curY = y;
330
+ return pl;
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
+ },
347
+ endPolyline() {
348
+ finished = true;
349
+ },
350
+ };
351
+ return pl;
352
+ }
353
+
354
+ /**
355
+ * @param {object} native
356
+ * @param {unknown} handle
357
+ * @param {{ name?: string, zIndex?: number, color?: string|{r,g,b,a} }} [layerOpts]
358
+ */
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
+
364
+ const img = {
365
+ /** @internal */
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
+ },
416
+ addLine(x1, y1, x2, y2, width) {
417
+ native.addLine(handle, x1, y1, x2, y2, width);
418
+ return img;
419
+ },
420
+ addArc(cx, cy, x1, y1, x2, y2, width, clockwise) {
421
+ native.addArc(handle, cx, cy, x1, y1, x2, y2, width, !!clockwise);
422
+ return img;
423
+ },
424
+ addBezier2(x1, y1, c1x, c1y, x2, y2, width) {
425
+ native.addBezier2(handle, x1, y1, c1x, c1y, x2, y2, width);
426
+ return img;
427
+ },
428
+ addBezier3(x1, y1, c1x, c1y, c2x, c2y, x2, y2, width) {
429
+ native.addBezier3(handle, x1, y1, c1x, c1y, c2x, c2y, x2, y2, width);
430
+ return img;
431
+ },
432
+ addFlashCircle(x, y, diam) {
433
+ native.addFlashCircle(handle, x, y, diam);
434
+ return img;
435
+ },
436
+ addRect(x, y, width, height) {
437
+ return wrapRegion(native, handle, seedRectCmds(x, y, width, height));
438
+ },
439
+ addCircle(cx, cy, diam) {
440
+ return wrapRegion(native, handle, seedCircleCmds(cx, cy, diam));
441
+ },
442
+ addCircleRing(cx, cy, d, w) {
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);
455
+ return img;
456
+ },
457
+ /** @param {number} x @param {number} y */
458
+ createPolygon(x, y) {
459
+ requireXY("createPolygon", x, y);
460
+ return wrapRegion(native, handle, [{ t: "L", x, y }]);
461
+ },
462
+ /** @param {number} x @param {number} y @param {number} width */
463
+ createPolyline(x, y, width) {
464
+ requireXY("createPolyline", x, y);
465
+ if (typeof width !== "number" || !(width > 0)) {
466
+ throw new Error("createPolyline(x, y, width) requires positive width");
467
+ }
468
+ return wrapPolyline(native, handle, x, y, width);
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
+ },
486
+ exportRs274x() {
487
+ return native.exportRs274x(handle);
488
+ },
489
+ exportDrill() {
490
+ return native.exportDrill(handle);
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
+ },
504
+ };
505
+ return img;
506
+ }
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
+
38
577
  const addonPath = resolveAddon();
39
- module.exports = require(addonPath);
40
- module.exports.__addonPath = addonPath;
41
- module.exports.__platformKey = platformKey();
578
+ const native = require(addonPath);
579
+
580
+ module.exports = {
581
+ backend: native.backend,
582
+ __addonPath: addonPath,
583
+ __platformKey: platformKey(),
584
+ /**
585
+ * @param {{ unit?: 'inch'|'mm'|'in', precision?: number }} [opts]
586
+ */
587
+ createImage(opts) {
588
+ return wrapImage(native, native.createImage(opts || {}));
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,
598
+ /** @deprecated 优先用 img.createPolygon(x, y) */
599
+ createPolygon(image, x, y) {
600
+ requireXY("createPolygon", x, y);
601
+ return wrapRegion(native, unwrap(image), [{ t: "L", x, y }]);
602
+ },
603
+ /** @deprecated */
604
+ addLine(image, x1, y1, x2, y2, width) {
605
+ native.addLine(unwrap(image), x1, y1, x2, y2, width);
606
+ },
607
+ addArc(image, cx, cy, x1, y1, x2, y2, width, clockwise) {
608
+ native.addArc(unwrap(image), cx, cy, x1, y1, x2, y2, width, !!clockwise);
609
+ },
610
+ addBezier2(image, x1, y1, c1x, c1y, x2, y2, width) {
611
+ native.addBezier2(unwrap(image), x1, y1, c1x, c1y, x2, y2, width);
612
+ },
613
+ addBezier3(image, x1, y1, c1x, c1y, c2x, c2y, x2, y2, width) {
614
+ native.addBezier3(
615
+ unwrap(image),
616
+ x1,
617
+ y1,
618
+ c1x,
619
+ c1y,
620
+ c2x,
621
+ c2y,
622
+ x2,
623
+ y2,
624
+ width
625
+ );
626
+ },
627
+ addFlashCircle(image, x, y, diam) {
628
+ native.addFlashCircle(unwrap(image), x, y, diam);
629
+ },
630
+ addRect(image, x, y, width, height) {
631
+ wrapRegion(
632
+ native,
633
+ unwrap(image),
634
+ seedRectCmds(x, y, width, height)
635
+ ).endArea();
636
+ },
637
+ exportRs274x(image) {
638
+ return native.exportRs274x(unwrap(image));
639
+ },
640
+ exportDrill(image) {
641
+ return native.exportDrill(unwrap(image));
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
+ },
655
+ };