dsh-stm32 0.1.7 → 0.1.9
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/README.md +567 -516
- package/README_EN.md +544 -510
- package/client/client.js +7033 -682
- package/docs/reading-guide.md +921 -859
- package/lib/client.js +7033 -682
- package/lib/commands/register.js +2 -0
- package/lib/data/firmware-packages.js +35 -0
- package/lib/data/firmware-pins.js +7 -0
- package/lib/data/install-guides.js +110 -0
- package/lib/data/ioc-presets.js +118 -0
- package/lib/engine/artifacts.js +123 -0
- package/lib/engine/breakpoint-hits.js +73 -0
- package/lib/engine/build.js +161 -5
- package/lib/engine/channel-tap.js +227 -0
- package/lib/engine/channels.js +48 -0
- package/lib/engine/clocks.js +192 -0
- package/lib/engine/datasheet/auto.js +74 -0
- package/lib/engine/datasheet/digest.js +74 -0
- package/lib/engine/datasheet/lcsc.js +60 -0
- package/lib/engine/datasheet/pinout.js +119 -0
- package/lib/engine/datasheet/price.js +152 -0
- package/lib/engine/datasheet/search.js +147 -0
- package/lib/engine/datasheet/store.js +371 -0
- package/lib/engine/debug-session.js +48 -2
- package/lib/engine/ecad/autobuild.js +156 -0
- package/lib/engine/ecad/bom.js +238 -0
- package/lib/engine/ecad/brief.js +205 -0
- package/lib/engine/ecad/checks.js +239 -0
- package/lib/engine/ecad/design.js +177 -0
- package/lib/engine/ecad/dfm.js +314 -0
- package/lib/engine/ecad/easyeda.js +454 -0
- package/lib/engine/ecad/fabrication.js +104 -0
- package/lib/engine/ecad/gen/footprint.js +159 -0
- package/lib/engine/ecad/gen/symbol.js +103 -0
- package/lib/engine/ecad/import.js +372 -0
- package/lib/engine/ecad/jlcpcb.js +141 -0
- package/lib/engine/ecad/libs.js +316 -0
- package/lib/engine/ecad/models3d.js +89 -0
- package/lib/engine/ecad/netgate.js +286 -0
- package/lib/engine/ecad/normalize.js +182 -0
- package/lib/engine/ecad/package.js +126 -0
- package/lib/engine/ecad/padmerge.js +128 -0
- package/lib/engine/ecad/padprecheck.js +123 -0
- package/lib/engine/ecad/part-import.js +257 -0
- package/lib/engine/ecad/pcb/apply.js +53 -0
- package/lib/engine/ecad/pcb/emc-deep.js +396 -0
- package/lib/engine/ecad/pcb/emc.js +275 -0
- package/lib/engine/ecad/pcb/geometry.js +63 -0
- package/lib/engine/ecad/pcb/gerber.js +499 -0
- package/lib/engine/ecad/pcb/hand-sync.js +242 -0
- package/lib/engine/ecad/pcb/impedance.js +119 -0
- package/lib/engine/ecad/pcb/ipc-open.js +78 -0
- package/lib/engine/ecad/pcb/ipc-script.js +276 -0
- package/lib/engine/ecad/pcb/ipc.js +129 -0
- package/lib/engine/ecad/pcb/krt.js +182 -0
- package/lib/engine/ecad/pcb/links.js +393 -0
- package/lib/engine/ecad/pcb/mech.js +201 -0
- package/lib/engine/ecad/pcb/model-fix.js +178 -0
- package/lib/engine/ecad/pcb/netlist.js +146 -0
- package/lib/engine/ecad/pcb/pcb-svg.js +199 -0
- package/lib/engine/ecad/pcb/placement.js +140 -0
- package/lib/engine/ecad/pcb/power.js +203 -0
- package/lib/engine/ecad/pcb/render3d.js +208 -0
- package/lib/engine/ecad/pcb/route.js +167 -0
- package/lib/engine/ecad/pcb/router.js +469 -0
- package/lib/engine/ecad/pcb/rules.js +122 -0
- package/lib/engine/ecad/pcb/scene.js +513 -0
- package/lib/engine/ecad/pcb/stackup.js +154 -0
- package/lib/engine/ecad/pcb/swap.js +222 -0
- package/lib/engine/ecad/pcb/tracks.js +70 -0
- package/lib/engine/ecad/runtime.js +227 -0
- package/lib/engine/ecad/toolchain.js +146 -0
- package/lib/engine/ecad/tsci.js +362 -0
- package/lib/engine/ecad/zip.js +98 -0
- package/lib/engine/event-bus.js +158 -0
- package/lib/engine/event-log.js +335 -0
- package/lib/engine/fault-hints.js +146 -0
- package/lib/engine/generate-script.js +4 -1
- package/lib/engine/generate.js +145 -5
- package/lib/engine/interrupts.js +247 -0
- package/lib/engine/ioc-diff.js +369 -0
- package/lib/engine/ioc-plan.js +12 -2
- package/lib/engine/ioc-sync.js +58 -0
- package/lib/engine/mcu-info.js +134 -0
- package/lib/engine/msys2-install.js +31 -6
- package/lib/engine/panel-actions.js +103 -0
- package/lib/engine/pdf/curve.js +321 -0
- package/lib/engine/pdf/png.js +83 -0
- package/lib/engine/pdf/reader.js +336 -0
- package/lib/engine/pdf/schematic.js +93 -0
- package/lib/engine/pdf/tables.js +249 -0
- package/lib/engine/pdf/toolchain.js +50 -0
- package/lib/engine/pinout.js +140 -0
- package/lib/engine/probe-info.js +95 -0
- package/lib/engine/probe.js +45 -0
- package/lib/engine/progress.js +99 -0
- package/lib/engine/project-conf.js +254 -0
- package/lib/engine/project-layout.js +198 -0
- package/lib/engine/project-repair.js +205 -0
- package/lib/engine/schematic/checks.js +153 -0
- package/lib/engine/schematic/draw.js +390 -0
- package/lib/engine/schematic/edit.js +229 -0
- package/lib/engine/schematic/kicad-sch.js +225 -0
- package/lib/engine/schematic/model.js +442 -0
- package/lib/engine/schematic/nets.js +219 -0
- package/lib/engine/schematic/pcb.js +144 -0
- package/lib/engine/schematic/review.js +134 -0
- package/lib/engine/schematic/sexpr.js +134 -0
- package/lib/engine/schematic/to-ioc.js +197 -0
- package/lib/engine/schematic/transform.js +25 -0
- package/lib/engine/session-context.js +106 -0
- package/lib/engine/source-slice.js +59 -0
- package/lib/engine/tool-events.js +119 -0
- package/lib/engine/toolchain-gate.js +8 -0
- package/lib/engine/watch-admin.js +101 -0
- package/lib/engine/watch-record.js +39 -0
- package/lib/engine/watch-recorder.js +116 -0
- package/lib/engine/web-api.js +1123 -4
- package/lib/engine/web-routes.js +18 -1
- package/lib/engine/workspace-memo.js +41 -0
- package/lib/index.js +118 -24
- package/lib/skill/stm32-workflow.js +1 -1
- package/lib/tools/build.js +10 -2
- package/lib/tools/code-patch.js +2 -2
- package/lib/tools/datasheet.js +447 -0
- package/lib/tools/debug-session.js +9 -4
- package/lib/tools/debug.js +2 -2
- package/lib/tools/doctor.js +2 -2
- package/lib/tools/ecad.js +903 -0
- package/lib/tools/flash.js +2 -2
- package/lib/tools/gen.js +166 -0
- package/lib/tools/generate.js +7 -5
- package/lib/tools/import-circuit.js +146 -0
- package/lib/tools/ioc-apply.js +11 -2
- package/lib/tools/ioc-inspect.js +2 -2
- package/lib/tools/ioc-plan.js +2 -2
- package/lib/tools/ioc-verify.js +2 -2
- package/lib/tools/layout.js +92 -0
- package/lib/tools/mcu-info.js +163 -0
- package/lib/tools/part-import.js +182 -0
- package/lib/tools/pcb.js +2316 -0
- package/lib/tools/pdf.js +423 -0
- package/lib/tools/project-create.js +28 -0
- package/lib/tools/project-dir.js +25 -1
- package/lib/tools/regs.js +2 -2
- package/lib/tools/route-plan.js +2 -2
- package/lib/tools/rtt.js +10 -2
- package/lib/tools/sch-draw.js +433 -0
- package/lib/tools/sch-edit.js +145 -0
- package/lib/tools/schematic.js +817 -0
- package/lib/tools/serial.js +22 -0
- package/lib/tools/watch.js +24 -2
- package/package.json +1 -1
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 面板直接触发的构建与烧录。
|
|
3
|
+
*
|
|
4
|
+
* 人在面板上点「构建 / 烧录」时不经过模型:这里直接调引擎的 `runBuild` / `runFlash`,
|
|
5
|
+
* 并把结果作为 `stm32_build` / `stm32_flash` 事件写进同一条总线(source 标 user)。
|
|
6
|
+
* 这样时间线、故障页、产物页看到的仍是同一种事件,不需要为「人点的」再开一套数据。
|
|
7
|
+
*/
|
|
8
|
+
import { emitEvent } from "./event-bus.js";
|
|
9
|
+
import { sha256OfPath } from "./artifacts.js";
|
|
10
|
+
import { runBuild } from "./build.js";
|
|
11
|
+
import { runFlash } from "./flash.js";
|
|
12
|
+
/** 构建摘要:与工具埋点的 resultDigest 同形状,面板不必区分来源。 */
|
|
13
|
+
export function buildDigest(report) {
|
|
14
|
+
const digest = {
|
|
15
|
+
ok: report.ok,
|
|
16
|
+
projectDir: report.projectDir,
|
|
17
|
+
buildDir: report.buildDir,
|
|
18
|
+
durationMs: report.durationMs,
|
|
19
|
+
warningCount: report.warningCount,
|
|
20
|
+
};
|
|
21
|
+
if (report.exitCode !== undefined)
|
|
22
|
+
digest["exitCode"] = report.exitCode;
|
|
23
|
+
if (report.memoryUsage.length > 0)
|
|
24
|
+
digest["memoryUsage"] = report.memoryUsage;
|
|
25
|
+
if (report.elf !== undefined) {
|
|
26
|
+
digest["elf"] = report.elf.path;
|
|
27
|
+
const sha = sha256OfPath(report.elf.path);
|
|
28
|
+
if (sha !== "")
|
|
29
|
+
digest["elfSha256"] = sha;
|
|
30
|
+
}
|
|
31
|
+
if (report.errors.length > 0)
|
|
32
|
+
digest["errors"] = report.errors.slice(0, 20);
|
|
33
|
+
return digest;
|
|
34
|
+
}
|
|
35
|
+
export function flashDigest(report) {
|
|
36
|
+
const digest = {
|
|
37
|
+
ok: report.ok,
|
|
38
|
+
projectDir: report.projectDir,
|
|
39
|
+
elfPath: report.elfPath,
|
|
40
|
+
elfSize: report.elfSize,
|
|
41
|
+
durationMs: report.durationMs,
|
|
42
|
+
programmed: report.programmed,
|
|
43
|
+
verified: report.verified,
|
|
44
|
+
};
|
|
45
|
+
if (report.exitCode !== undefined)
|
|
46
|
+
digest["exitCode"] = report.exitCode;
|
|
47
|
+
if (report.probeError !== null)
|
|
48
|
+
digest["probeError"] = report.probeError;
|
|
49
|
+
if (report.errors.length > 0)
|
|
50
|
+
digest["errors"] = report.errors.slice(0, 20);
|
|
51
|
+
return digest;
|
|
52
|
+
}
|
|
53
|
+
export async function buildFromPanel(deps, request) {
|
|
54
|
+
emitEvent({
|
|
55
|
+
source: "user",
|
|
56
|
+
kind: "stm32_build",
|
|
57
|
+
status: "start",
|
|
58
|
+
summary: "面板:构建 " + request.projectDir,
|
|
59
|
+
payload: { projectDir: request.projectDir, clean: request.clean === true },
|
|
60
|
+
});
|
|
61
|
+
const report = await runBuild(deps.subprocess, {
|
|
62
|
+
projectDir: request.projectDir,
|
|
63
|
+
...(request.clean === true ? { clean: true } : {}),
|
|
64
|
+
...(request.target === undefined ? {} : { target: request.target }),
|
|
65
|
+
...(request.artifacts === undefined ? {} : { artifacts: request.artifacts }),
|
|
66
|
+
...(request.timeoutMs === undefined ? {} : { timeoutMs: request.timeoutMs }),
|
|
67
|
+
});
|
|
68
|
+
emitEvent({
|
|
69
|
+
source: "user",
|
|
70
|
+
kind: "stm32_build",
|
|
71
|
+
status: report.ok ? "ok" : "fail",
|
|
72
|
+
summary: report.message,
|
|
73
|
+
payload: { result: buildDigest(report) },
|
|
74
|
+
});
|
|
75
|
+
return report;
|
|
76
|
+
}
|
|
77
|
+
export async function flashFromPanel(deps, request) {
|
|
78
|
+
emitEvent({
|
|
79
|
+
source: "user",
|
|
80
|
+
kind: "stm32_flash",
|
|
81
|
+
status: "start",
|
|
82
|
+
summary: "面板:烧录 " + request.projectDir,
|
|
83
|
+
payload: { projectDir: request.projectDir },
|
|
84
|
+
});
|
|
85
|
+
const flashRequest = { projectDir: request.projectDir };
|
|
86
|
+
if (request.elfPath !== undefined)
|
|
87
|
+
flashRequest.elfPath = request.elfPath;
|
|
88
|
+
if (request.buildDir !== undefined)
|
|
89
|
+
flashRequest.buildDir = request.buildDir;
|
|
90
|
+
if (request.adapterSpeed !== undefined)
|
|
91
|
+
flashRequest.adapterSpeed = request.adapterSpeed;
|
|
92
|
+
if (request.allowNoDebug === true)
|
|
93
|
+
flashRequest.allowNoDebug = true;
|
|
94
|
+
const report = await runFlash(deps.subprocess, flashRequest);
|
|
95
|
+
emitEvent({
|
|
96
|
+
source: "user",
|
|
97
|
+
kind: "stm32_flash",
|
|
98
|
+
status: report.ok ? "ok" : "fail",
|
|
99
|
+
summary: report.message,
|
|
100
|
+
payload: { result: flashDigest(report) },
|
|
101
|
+
});
|
|
102
|
+
return report;
|
|
103
|
+
}
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
const HUE_NAMES = ["red", "orange", "yellow", "green", "cyan", "blue", "violet", "magenta"];
|
|
2
|
+
/** 把 RGB 归到 8 个色相桶;灰色(饱和度低)返回 -1。 */
|
|
3
|
+
function hueBucket(r, g, b) {
|
|
4
|
+
const max = Math.max(r, g, b);
|
|
5
|
+
const min = Math.min(r, g, b);
|
|
6
|
+
const sat = max === 0 ? 0 : (max - min) / max;
|
|
7
|
+
if (sat < 0.45 || max < 90)
|
|
8
|
+
return -1;
|
|
9
|
+
const delta = max - min;
|
|
10
|
+
let hue = 0;
|
|
11
|
+
if (max === r)
|
|
12
|
+
hue = ((g - b) / delta + 6) % 6;
|
|
13
|
+
else if (max === g)
|
|
14
|
+
hue = (b - r) / delta + 2;
|
|
15
|
+
else
|
|
16
|
+
hue = (r - g) / delta + 4;
|
|
17
|
+
const degrees = hue * 60;
|
|
18
|
+
return Math.min(7, Math.floor(((degrees + 22.5) % 360) / 45));
|
|
19
|
+
}
|
|
20
|
+
const BUCKET_RGB = {
|
|
21
|
+
0: "#ff0000",
|
|
22
|
+
1: "#ff8000",
|
|
23
|
+
2: "#ffff00",
|
|
24
|
+
3: "#00c000",
|
|
25
|
+
4: "#00e0e0",
|
|
26
|
+
5: "#3399ff",
|
|
27
|
+
6: "#8000ff",
|
|
28
|
+
7: "#ff00aa",
|
|
29
|
+
};
|
|
30
|
+
export function digitizeCurves(image, calibration, options = {}) {
|
|
31
|
+
const notes = [];
|
|
32
|
+
const maxPoints = options.maxPoints ?? 40;
|
|
33
|
+
const columns = new Map();
|
|
34
|
+
const pixelCount = new Map();
|
|
35
|
+
for (let y = 0; y < image.height; y += 1) {
|
|
36
|
+
for (let x = 0; x < image.width; x += 1) {
|
|
37
|
+
const index = (y * image.width + x) * 4;
|
|
38
|
+
const bucket = hueBucket(image.rgba[index] ?? 0, image.rgba[index + 1] ?? 0, image.rgba[index + 2] ?? 0);
|
|
39
|
+
if (bucket < 0)
|
|
40
|
+
continue;
|
|
41
|
+
pixelCount.set(bucket, (pixelCount.get(bucket) ?? 0) + 1);
|
|
42
|
+
let byColumn = columns.get(bucket);
|
|
43
|
+
if (byColumn === undefined) {
|
|
44
|
+
byColumn = new Map();
|
|
45
|
+
columns.set(bucket, byColumn);
|
|
46
|
+
}
|
|
47
|
+
const ys = byColumn.get(x);
|
|
48
|
+
if (ys === undefined)
|
|
49
|
+
byColumn.set(x, [y]);
|
|
50
|
+
else
|
|
51
|
+
ys.push(y);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const yTicks = calibration.y.ticks;
|
|
55
|
+
const xTicks = calibration.x.ticks;
|
|
56
|
+
const yLow = Math.min(...yTicks.map((tick) => tick.value));
|
|
57
|
+
const yHigh = Math.max(...yTicks.map((tick) => tick.value));
|
|
58
|
+
const ySpan = Math.abs(yHigh - yLow);
|
|
59
|
+
const xLow = Math.min(...xTicks.map((tick) => tick.value));
|
|
60
|
+
const xHigh = Math.max(...xTicks.map((tick) => tick.value));
|
|
61
|
+
const xSpan = Math.abs(xHigh - xLow);
|
|
62
|
+
const series = [];
|
|
63
|
+
for (const [bucket, byColumn] of [...columns.entries()].sort((a, b) => a[0] - b[0])) {
|
|
64
|
+
const pixels = pixelCount.get(bucket) ?? 0;
|
|
65
|
+
if (pixels < 120) {
|
|
66
|
+
notes.push("跳过 " + HUE_NAMES[bucket] + ":像素太少(" + pixels + ")");
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const sortedX = [...byColumn.keys()].sort((a, b) => a - b);
|
|
70
|
+
if (sortedX.length < 8) {
|
|
71
|
+
notes.push("跳过 " + HUE_NAMES[bucket] + ":横向跨度太小(" + sortedX.length + " 列)");
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const step = Math.max(1, Math.floor(sortedX.length / maxPoints));
|
|
75
|
+
const points = [];
|
|
76
|
+
for (let index = 0; index < sortedX.length; index += step) {
|
|
77
|
+
const x = sortedX[index] ?? 0;
|
|
78
|
+
const ys = byColumn.get(x) ?? [];
|
|
79
|
+
if (ys.length === 0)
|
|
80
|
+
continue;
|
|
81
|
+
const median = ys.sort((a, b) => a - b)[Math.floor(ys.length / 2)] ?? 0;
|
|
82
|
+
points.push({
|
|
83
|
+
x: round(calibration.x.a * x + calibration.x.b, 3),
|
|
84
|
+
y: round(calibration.y.a * median + calibration.y.b, 4),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const ys = points.map((point) => point.y);
|
|
88
|
+
const inside = ys.filter((value) => value >= yLow - ySpan * 0.06 && value <= yHigh + ySpan * 0.06).length;
|
|
89
|
+
if (points.length === 0 || inside / points.length < 0.6) {
|
|
90
|
+
notes.push("跳过 " + HUE_NAMES[bucket] + ":多数像素落在坐标轴范围外(疑似图例、链接文字或页眉)");
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const xs = points.map((point) => point.x);
|
|
94
|
+
const outOfRange = xs.some((value) => value < xLow - xSpan * 0.2 || value > xHigh + xSpan * 0.2);
|
|
95
|
+
if (outOfRange)
|
|
96
|
+
notes.push(HUE_NAMES[bucket] + " 的横向范围超出坐标轴,端点值可能不可信");
|
|
97
|
+
series.push({ hue: HUE_NAMES[bucket] ?? "unknown", label: "", color: BUCKET_RGB[bucket] ?? "#888888", points, pixels });
|
|
98
|
+
}
|
|
99
|
+
if (series.length === 0)
|
|
100
|
+
notes.push("没有找到饱和色调的曲线像素:这张图可能不是彩色曲线");
|
|
101
|
+
return { calibration, series, notes };
|
|
102
|
+
}
|
|
103
|
+
function round(value, digits) {
|
|
104
|
+
const factor = 10 ** digits;
|
|
105
|
+
return Math.round(value * factor) / factor;
|
|
106
|
+
}
|
|
107
|
+
/** 从一个数字词里取值;"1.235"、"−40"、"0"、"1,2" 都能认,认不出返回 null。 */
|
|
108
|
+
function numberOfWord(text) {
|
|
109
|
+
const cleaned = text.replace(/[−–—]/gu, "-").replace(/,/gu, ".").trim();
|
|
110
|
+
if (!/^-?\d+(?:\.\d+)?$/u.test(cleaned))
|
|
111
|
+
return null;
|
|
112
|
+
const value = Number(cleaned);
|
|
113
|
+
return Number.isFinite(value) ? value : null;
|
|
114
|
+
}
|
|
115
|
+
function median(values) {
|
|
116
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
117
|
+
return sorted[Math.floor(sorted.length / 2)] ?? 0;
|
|
118
|
+
}
|
|
119
|
+
function fitLine(pairs) {
|
|
120
|
+
const n = pairs.length;
|
|
121
|
+
const sumX = pairs.reduce((acc, item) => acc + item.pixel, 0);
|
|
122
|
+
const sumY = pairs.reduce((acc, item) => acc + item.value, 0);
|
|
123
|
+
const sumXY = pairs.reduce((acc, item) => acc + item.pixel * item.value, 0);
|
|
124
|
+
const sumXX = pairs.reduce((acc, item) => acc + item.pixel * item.pixel, 0);
|
|
125
|
+
const denominator = n * sumXX - sumX * sumX;
|
|
126
|
+
if (denominator === 0)
|
|
127
|
+
return { a: 0, b: sumY / Math.max(1, n) };
|
|
128
|
+
const a = (n * sumXY - sumX * sumY) / denominator;
|
|
129
|
+
return { a, b: (sumY - a * sumX) / n };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* 从词框里认坐标轴刻度。
|
|
133
|
+
*
|
|
134
|
+
* 纵向刻度:同一列(x 中心接近)、竖直排列、竖直方向随值单调 → 纵轴。
|
|
135
|
+
* 横向刻度:同一行(y 基线接近)、水平排列 → 横轴。
|
|
136
|
+
*/
|
|
137
|
+
export function calibrateFromWords(words, options = {}) {
|
|
138
|
+
const scale = options.scale ?? 1;
|
|
139
|
+
const bounds = options.bounds;
|
|
140
|
+
/** 只认落在图区范围内的刻度:同页两张图时,靠这个把隔壁图的刻度筛掉(S124)。 */
|
|
141
|
+
const insideBounds = (word) => {
|
|
142
|
+
if (bounds === undefined)
|
|
143
|
+
return true;
|
|
144
|
+
const cx = ((word.x0 + word.x1) / 2) * scale;
|
|
145
|
+
const cy = ((word.y0 + word.y1) / 2) * scale;
|
|
146
|
+
const padX = (bounds.x1 - bounds.x0) * 0.25;
|
|
147
|
+
const padY = (bounds.y1 - bounds.y0) * 0.25;
|
|
148
|
+
return cx >= bounds.x0 - padX && cx <= bounds.x1 + padX && cy >= bounds.y0 - padY && cy <= bounds.y1 + padY;
|
|
149
|
+
};
|
|
150
|
+
const numeric = words
|
|
151
|
+
.filter((word) => insideBounds(word))
|
|
152
|
+
.map((word) => ({ word, value: numberOfWord(word.text) }))
|
|
153
|
+
.filter((item) => item.value !== null);
|
|
154
|
+
if (numeric.length < 6)
|
|
155
|
+
return { ok: false, error: "页面上认出的数字太少(" + numeric.length + " 个),无法标定坐标轴" };
|
|
156
|
+
const columns = new Map();
|
|
157
|
+
for (const item of numeric) {
|
|
158
|
+
const key = Math.round(((item.word.x0 + item.word.x1) / 2) * 2) / 2;
|
|
159
|
+
const list = columns.get(key);
|
|
160
|
+
const center = (item.word.y0 + item.word.y1) / 2;
|
|
161
|
+
if (list === undefined)
|
|
162
|
+
columns.set(key, [{ value: item.value, y: center }]);
|
|
163
|
+
else
|
|
164
|
+
list.push({ value: item.value, y: center });
|
|
165
|
+
}
|
|
166
|
+
let bestColumn = null;
|
|
167
|
+
for (const [key, items] of columns) {
|
|
168
|
+
if (items.length < 3)
|
|
169
|
+
continue;
|
|
170
|
+
if (bestColumn === null || items.length > bestColumn.items.length)
|
|
171
|
+
bestColumn = { key, items };
|
|
172
|
+
}
|
|
173
|
+
if (bestColumn === null)
|
|
174
|
+
return { ok: false, error: "没有找到成列的纵轴刻度" };
|
|
175
|
+
const yTicks = bestColumn.items
|
|
176
|
+
.map((item) => ({ value: item.value, pixel: item.y * scale }))
|
|
177
|
+
.sort((a, b) => a.pixel - b.pixel);
|
|
178
|
+
const ySpread = Math.abs((yTicks[yTicks.length - 1]?.value ?? 0) - (yTicks[0]?.value ?? 0));
|
|
179
|
+
if (ySpread === 0)
|
|
180
|
+
return { ok: false, error: "纵轴刻度值全都相同,无法标定" };
|
|
181
|
+
const rows = new Map();
|
|
182
|
+
for (const item of numeric) {
|
|
183
|
+
const key = Math.round(((item.word.y0 + item.word.y1) / 2) * 2) / 2;
|
|
184
|
+
const list = rows.get(key);
|
|
185
|
+
const center = (item.word.x0 + item.word.x1) / 2;
|
|
186
|
+
if (list === undefined)
|
|
187
|
+
rows.set(key, [{ value: item.value, x: center }]);
|
|
188
|
+
else
|
|
189
|
+
list.push({ value: item.value, x: center });
|
|
190
|
+
}
|
|
191
|
+
let bestRow = null;
|
|
192
|
+
for (const [key, items] of rows) {
|
|
193
|
+
if (items.length < 3)
|
|
194
|
+
continue;
|
|
195
|
+
if (key === bestColumn.key)
|
|
196
|
+
continue;
|
|
197
|
+
if (bestRow === null || items.length > bestRow.items.length)
|
|
198
|
+
bestRow = { key, items };
|
|
199
|
+
}
|
|
200
|
+
if (bestRow === null)
|
|
201
|
+
return { ok: false, error: "没有找到成行的横轴刻度" };
|
|
202
|
+
const xTicks = bestRow.items
|
|
203
|
+
.map((item) => ({ value: item.value, pixel: item.x * scale }))
|
|
204
|
+
.sort((a, b) => a.pixel - b.pixel);
|
|
205
|
+
const yFit = fitLine(yTicks);
|
|
206
|
+
const xFit = fitLine(xTicks);
|
|
207
|
+
const notes = [
|
|
208
|
+
"纵轴刻度 " + yTicks.length + " 个(" + yTicks[0]?.value + "…" + yTicks[yTicks.length - 1]?.value + ")",
|
|
209
|
+
"横轴刻度 " + xTicks.length + " 个(" + xTicks[0]?.value + "…" + xTicks[xTicks.length - 1]?.value + ")",
|
|
210
|
+
bounds === undefined
|
|
211
|
+
? "标定按最近的一列/一行数字拟合:同页若有多张图,结果可能落到隔壁那张"
|
|
212
|
+
: "刻度只取图区附近的那一组(同页多图时按彩色像素外接框筛过)",
|
|
213
|
+
];
|
|
214
|
+
/** 刻度间距是否均匀:均匀的刻度拟合才可信,绝对值大小不影响线性标定。 */
|
|
215
|
+
const irregularity = (ticks) => {
|
|
216
|
+
const gaps = ticks.slice(1).map((item, index) => Math.abs(item.pixel - (ticks[index]?.pixel ?? 0)));
|
|
217
|
+
const middle = median(gaps);
|
|
218
|
+
if (middle === 0)
|
|
219
|
+
return 0;
|
|
220
|
+
return Math.max(...gaps.map((gap) => Math.abs(gap - middle) / middle));
|
|
221
|
+
};
|
|
222
|
+
const yIrregular = irregularity(yTicks);
|
|
223
|
+
const xIrregular = irregularity(xTicks);
|
|
224
|
+
if (yIrregular > 0.4)
|
|
225
|
+
notes.push("纵轴刻度间距不均匀(最大偏差 " + Math.round(yIrregular * 100) + "%),拟合可能不稳");
|
|
226
|
+
if (xIrregular > 0.4)
|
|
227
|
+
notes.push("横轴刻度间距不均匀(最大偏差 " + Math.round(xIrregular * 100) + "%),拟合可能不稳");
|
|
228
|
+
return {
|
|
229
|
+
ok: true,
|
|
230
|
+
calibration: {
|
|
231
|
+
x: { a: xFit.a, b: xFit.b, ticks: xTicks, unit: "" },
|
|
232
|
+
y: { a: yFit.a, b: yFit.b, ticks: yTicks, unit: "" },
|
|
233
|
+
notes,
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* 彩色像素的外接框(S124)。
|
|
239
|
+
*
|
|
240
|
+
* 一张页可能有两张图,刻度文字也各有一套。先量出「彩色曲线占了哪块地方」,
|
|
241
|
+
* 再用它把刻度候选筛到同一块地方,才能避免标定到隔壁那张图。
|
|
242
|
+
*/
|
|
243
|
+
export function chromaBounds(image, margin = 0) {
|
|
244
|
+
let x0 = Number.POSITIVE_INFINITY;
|
|
245
|
+
let y0 = Number.POSITIVE_INFINITY;
|
|
246
|
+
let x1 = -1;
|
|
247
|
+
let y1 = -1;
|
|
248
|
+
for (let y = 0; y < image.height; y += 1) {
|
|
249
|
+
for (let x = 0; x < image.width; x += 1) {
|
|
250
|
+
const at = (y * image.width + x) * 4;
|
|
251
|
+
const bucket = hueBucket(image.rgba[at] ?? 0, image.rgba[at + 1] ?? 0, image.rgba[at + 2] ?? 0);
|
|
252
|
+
if (bucket < 0)
|
|
253
|
+
continue;
|
|
254
|
+
if (x < x0)
|
|
255
|
+
x0 = x;
|
|
256
|
+
if (y < y0)
|
|
257
|
+
y0 = y;
|
|
258
|
+
if (x > x1)
|
|
259
|
+
x1 = x;
|
|
260
|
+
if (y > y1)
|
|
261
|
+
y1 = y;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (x1 < 0 || y1 < 0)
|
|
265
|
+
return null;
|
|
266
|
+
return {
|
|
267
|
+
x0: Math.max(0, x0 - margin),
|
|
268
|
+
y0: Math.max(0, y0 - margin),
|
|
269
|
+
x1: Math.min(image.width - 1, x1 + margin),
|
|
270
|
+
y1: Math.min(image.height - 1, y1 + margin),
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* 图例的「颜色 → 名字」(S124)。
|
|
275
|
+
*
|
|
276
|
+
* 手册的图例是「一小段彩色线段 + 一个词」。词能从 bbox 里拿到,颜色只能从图里取:
|
|
277
|
+
* 在词的左边找一小块彩色像素,取出现最多的色相,就得到这条线的名字。
|
|
278
|
+
* 图例文字若被转成轮廓,这里自然拿不到,返回空表即可。
|
|
279
|
+
*/
|
|
280
|
+
export function legendLabels(words, image, bounds, options = {}) {
|
|
281
|
+
const scale = options.scale ?? 1;
|
|
282
|
+
const labels = new Map();
|
|
283
|
+
const candidates = words.filter((word) => /^[A-Za-z][A-Za-z0-9 .%°-]{0,18}$/u.test(word.text.trim()) && word.text.trim().length >= 2);
|
|
284
|
+
for (const word of candidates) {
|
|
285
|
+
const y0 = Math.round(word.y0 * scale);
|
|
286
|
+
const y1 = Math.round(word.y1 * scale);
|
|
287
|
+
const xStart = Math.round(word.x0 * scale) - Math.round(30 * scale);
|
|
288
|
+
const xEnd = Math.round(word.x0 * scale) - 2;
|
|
289
|
+
if (y0 < 0 || y1 >= image.height || xEnd <= 0)
|
|
290
|
+
continue;
|
|
291
|
+
if (bounds !== null) {
|
|
292
|
+
const inside = y0 >= bounds.y0 - Math.round(24 * scale) && y1 <= bounds.y1 + Math.round(24 * scale) && xEnd >= bounds.x0 - Math.round(60 * scale);
|
|
293
|
+
if (!inside)
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
const counts = new Map();
|
|
297
|
+
for (let y = Math.max(0, y0); y <= Math.min(image.height - 1, y1); y += 1) {
|
|
298
|
+
for (let x = Math.max(0, xStart); x <= Math.min(image.width - 1, xEnd); x += 1) {
|
|
299
|
+
const at = (y * image.width + x) * 4;
|
|
300
|
+
const bucket = hueBucket(image.rgba[at] ?? 0, image.rgba[at + 1] ?? 0, image.rgba[at + 2] ?? 0);
|
|
301
|
+
if (bucket < 0)
|
|
302
|
+
continue;
|
|
303
|
+
counts.set(bucket, (counts.get(bucket) ?? 0) + 1);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
let bestBucket = -1;
|
|
307
|
+
let bestCount = 0;
|
|
308
|
+
for (const [bucket, count] of counts) {
|
|
309
|
+
if (count > bestCount) {
|
|
310
|
+
bestBucket = bucket;
|
|
311
|
+
bestCount = count;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (bestBucket < 0 || bestCount < 12)
|
|
315
|
+
continue;
|
|
316
|
+
if (labels.has(bestBucket))
|
|
317
|
+
continue;
|
|
318
|
+
labels.set(bestBucket, word.text.trim());
|
|
319
|
+
}
|
|
320
|
+
return labels;
|
|
321
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
|
|
2
|
+
function chunkType(buffer, offset) {
|
|
3
|
+
return buffer.toString("latin1", offset, offset + 4);
|
|
4
|
+
}
|
|
5
|
+
export function decodePng(buffer) {
|
|
6
|
+
if (buffer.length < 8 || SIGNATURE.some((byte, index) => buffer[index] !== byte)) {
|
|
7
|
+
return { ok: false, error: "不是 PNG 文件" };
|
|
8
|
+
}
|
|
9
|
+
let offset = 8;
|
|
10
|
+
let width = 0;
|
|
11
|
+
let height = 0;
|
|
12
|
+
let colorType = 0;
|
|
13
|
+
let bitDepth = 0;
|
|
14
|
+
let interlace = 0;
|
|
15
|
+
const idat = [];
|
|
16
|
+
while (offset + 8 <= buffer.length) {
|
|
17
|
+
const length = buffer.readUInt32BE(offset);
|
|
18
|
+
const type = chunkType(buffer, offset + 4);
|
|
19
|
+
const body = buffer.subarray(offset + 8, offset + 8 + length);
|
|
20
|
+
if (type === "IHDR") {
|
|
21
|
+
width = body.readUInt32BE(0);
|
|
22
|
+
height = body.readUInt32BE(4);
|
|
23
|
+
bitDepth = body[8] ?? 0;
|
|
24
|
+
colorType = body[9] ?? 0;
|
|
25
|
+
interlace = body[12] ?? 0;
|
|
26
|
+
}
|
|
27
|
+
else if (type === "IDAT") {
|
|
28
|
+
idat.push(Buffer.from(body));
|
|
29
|
+
}
|
|
30
|
+
else if (type === "IEND") {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
offset += 12 + length;
|
|
34
|
+
}
|
|
35
|
+
if (bitDepth !== 8 || interlace !== 0 || (colorType !== 2 && colorType !== 6)) {
|
|
36
|
+
return { ok: false, error: "只支持 8 位非隔行 RGB/RGBA PNG(实际 bitDepth " + bitDepth + " colorType " + colorType + ")" };
|
|
37
|
+
}
|
|
38
|
+
const inflate = inflateSync(Buffer.concat(idat));
|
|
39
|
+
const channels = colorType === 6 ? 4 : 3;
|
|
40
|
+
const stride = width * channels;
|
|
41
|
+
const raw = new Uint8Array(height * stride);
|
|
42
|
+
let source = 0;
|
|
43
|
+
for (let y = 0; y < height; y += 1) {
|
|
44
|
+
const filter = inflate[source] ?? 0;
|
|
45
|
+
source += 1;
|
|
46
|
+
const row = y * stride;
|
|
47
|
+
for (let x = 0; x < stride; x += 1) {
|
|
48
|
+
const value = inflate[source + x] ?? 0;
|
|
49
|
+
const left = x >= channels ? (raw[row + x - channels] ?? 0) : 0;
|
|
50
|
+
const up = y > 0 ? (raw[row - stride + x] ?? 0) : 0;
|
|
51
|
+
const upLeft = y > 0 && x >= channels ? (raw[row - stride + x - channels] ?? 0) : 0;
|
|
52
|
+
let reconstructed = value;
|
|
53
|
+
if (filter === 1)
|
|
54
|
+
reconstructed = value + left;
|
|
55
|
+
else if (filter === 2)
|
|
56
|
+
reconstructed = value + up;
|
|
57
|
+
else if (filter === 3)
|
|
58
|
+
reconstructed = value + ((left + up) >> 1);
|
|
59
|
+
else if (filter === 4)
|
|
60
|
+
reconstructed = value + paeth(left, up, upLeft);
|
|
61
|
+
raw[row + x] = reconstructed & 0xff;
|
|
62
|
+
}
|
|
63
|
+
source += stride;
|
|
64
|
+
}
|
|
65
|
+
const rgba = new Uint8Array(width * height * 4);
|
|
66
|
+
for (let index = 0; index < width * height; index += 1) {
|
|
67
|
+
rgba[index * 4] = raw[index * channels] ?? 0;
|
|
68
|
+
rgba[index * 4 + 1] = raw[index * channels + 1] ?? 0;
|
|
69
|
+
rgba[index * 4 + 2] = raw[index * channels + 2] ?? 0;
|
|
70
|
+
rgba[index * 4 + 3] = channels === 4 ? (raw[index * channels + 3] ?? 255) : 255;
|
|
71
|
+
}
|
|
72
|
+
return { ok: true, image: { width, height, rgba } };
|
|
73
|
+
}
|
|
74
|
+
function paeth(a, b, c) {
|
|
75
|
+
const p = a + b - c;
|
|
76
|
+
const pa = Math.abs(p - a);
|
|
77
|
+
const pb = Math.abs(p - b);
|
|
78
|
+
const pc = Math.abs(p - c);
|
|
79
|
+
if (pa <= pb && pa <= pc)
|
|
80
|
+
return a;
|
|
81
|
+
return pb <= pc ? b : c;
|
|
82
|
+
}
|
|
83
|
+
import { inflateSync } from "node:zlib";
|