feat: 新增截图找字体功能

This commit is contained in:
崮生(子虚) 2026-11-01 18:32:47 +08:00
parent 7790242cef
commit 8306a03efe
11 changed files with 1825 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import { handleGetConfig } from "./routes/config";
import { handleStats, handleStatsEvent } from "./routes/stats";
import { handleUpload } from "./routes/upload";
import { handleFontSubset } from "./routes/subset";
import { handleGlyphMatch } from "./routes/glyph_match";
import { handleFontDetail } from "./routes/font_detail";
import { handleFontMeta } from "./routes/font_meta";
import { startTempCleaner } from "./temp_cleaner";
@ -178,6 +179,9 @@ const fontApiMiddleware: cMiddleware = async (req, res, next) => {
if (url.pathname === "/api" && req.method === "GET") {
return handleFontSubset(req, res);
}
if (url.pathname === "/api/glyph-match" && req.method === "POST") {
return handleGlyphMatch(req, res);
}
return next(req, res);
};

View File

@ -0,0 +1,377 @@
/**
* 服务端字形位图光栅化 + IoU 比对 —— 截图找字体的服务端比对核心
*
* 不依赖任何 canvas / DOM:直接取 fonteditor 解析出的 glyf 二次贝塞尔轮廓,
* 扁平化为多边形后扫描线填充(奇偶规则),再紧致归一化到 64x64 二值网格。
* 归一化算法与前端 segmenter 完全一致(留 12% 边距、等比缩放、居中、
* 像素中心采样),保证两侧特征空间相同,位图可直接 IoU 比对。
*
* 缓存两层:字体对象(解析一次)+ 字形位图(字体×字符),进程内常驻。
*/
import { Font } from "../../vendor/fonteditor-core/lib/ttf/font";
import transformGlyfContours from "../../vendor/fonteditor-core/lib/ttf/util/transformGlyfContours";
/** 归一化网格边长(与前端 segmenter.GRID 一致) */
const GRID = 64;
/** 归一化留边比例(与前端一致:12%) */
const MARGIN_RATIO = 0.12;
/** 字体对象缓存:字体名 → 解析后的 ttfObject(含 glyf/cmap) */
const fontObjectCache = new Map<string, ReturnType<ReturnType<typeof Font.create>["get"]>>();
/** 字形位图缓存:字体名 → 码点 → 64x64 位图(01 串);值为 null 表示该字体缺此字 */
const glyphBitmapCache = new Map<string, Map<number, Uint8Array | null>>();
/**
* 按魔数探测字体类型(woff2 'wOF2' / woff 'wOFF' / otf 'OTTO' / 其余按 ttf)
*/
function detectType(buf: ArrayBuffer): "ttf" | "otf" | "woff" | "woff2" {
const b = new Uint8Array(buf, 0, 4);
if (b[0] === 0x77 && b[1] === 0x4f && b[2] === 0x46 && b[3] === 0x32) return "woff2";
if (b[0] === 0x77 && b[1] === 0x4f) return "woff";
if (b[0] === 0x4f && b[1] === 0x54 && b[2] === 0x54 && b[3] === 0x4f) return "otf";
return "ttf";
}
/**
* 解析字体(带进程内缓存)
* 传入读取字体文件的函数,避免本模块直接依赖 shared/interface 的 IO 层
*/
export function loadFontObject(
fontName: string,
readBuffer: (name: string) => Promise<ArrayBuffer | null>,
): Promise<ReturnType<ReturnType<typeof Font.create>["get"]> | null> {
const hit = fontObjectCache.get(fontName);
if (hit) return Promise.resolve(hit);
return readBuffer(fontName).then((buf) => {
if (!buf) return null;
const ttf = Font.create(buf, { type: detectType(buf) }).get();
fontObjectCache.set(fontName, ttf);
return ttf;
});
}
/** 二维点(已扁平化的多边形顶点,y 已翻转) */
interface Pt {
x: number;
y: number;
}
/** 字体轮廓点(TrueType 曲线标志) */
interface GlyphPoint extends Pt {
onCurve: boolean;
}
/** fonteditor 可能返回对象点或 [x, y, onCurve] 三元组 */
type RawContour = Array<GlyphPoint | number>;
/** 读取一个字形的轮廓,兼容优化后的紧凑坐标与复合字形 */
function readGlyphContours(ttf: any, glyf: any): RawContour[] {
if (!glyf) return [];
if (glyf.compound) return transformGlyfContours(glyf, ttf);
if (Array.isArray(glyf.contours) && Array.isArray(glyf.contours[0])) return glyf.contours;
const xArr = glyf._xArr;
const yArr = glyf._yArr;
const flags = glyf._flags;
const endPts = glyf.endPtsOfContours;
if (!xArr || !yArr || !flags || !endPts) return [];
const contours: RawContour[] = [];
let start = 0;
for (const end of endPts) {
const contour: GlyphPoint[] = [];
for (let i = start; i <= end; i++) {
contour.push({ x: xArr[i], y: yArr[i], onCurve: (flags[i] & 1) !== 0 });
}
contours.push(contour);
start = end + 1;
}
return contours;
}
/** 把对象点或扁平三元组统一成对象点 */
function normalizeContour(contour: RawContour): GlyphPoint[] {
const points: GlyphPoint[] = [];
for (let i = 0; i < contour.length;) {
const value = contour[i]!;
if (typeof value !== "number") {
points.push(value);
i++;
continue;
}
const y = contour[i + 1];
const onCurve = contour[i + 2];
if (typeof y === "number" && typeof onCurve === "number") {
points.push({ x: value, y, onCurve: onCurve !== 0 });
}
i += 3;
}
return points;
}
/**
* 单条轮廓(含二次贝塞尔控制点)扁平化为多边形顶点序列
* fonteditor 的轮廓点序列是 TrueType 规范:连续 off-curve 点之间隐含中点 on-curve。
* y 轴翻转(字形坐标 y 向上,位图 y 向下);二次曲线细分 4 段足够 64 网格精度。
*/
function flattenContour(rawContour: RawContour): Pt[] {
const contour = normalizeContour(rawContour);
if (contour.length < 2) return [];
const MID_STEPS = 4;
const mid = (a: Pt, b: Pt): Pt => ({ x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 });
const quad = (p0: Pt, pc: Pt, p1: Pt, out: Pt[]) => {
for (let i = 1; i <= MID_STEPS; i++) {
const t = i / MID_STEPS;
const mt = 1 - t;
out.push({
x: mt * mt * p0.x + 2 * mt * t * pc.x + t * t * p1.x,
y: mt * mt * p0.y + 2 * mt * t * pc.y + t * t * p1.y,
});
}
};
/** 先把轮廓规整为 on-curve 起点 */
const pts = contour.map((p) => ({ x: p.x, y: -p.y }));
let startIdx = pts.findIndex((_, i) => contour[i]!.onCurve);
/** 全 off-curve(极罕见):首尾中点为隐含起点 */
if (startIdx === -1) {
const n = pts.length;
const startPt = mid(pts[n - 1]!, pts[0]!);
const poly: Pt[] = [startPt];
for (let i = 0; i < n; i++) {
const cur = pts[i]!;
const next = pts[(i + 1) % n]!;
if (contour[i]!.onCurve) {
poly.push(cur);
} else {
const end = contour[(i + 1) % n]!.onCurve ? next : mid(cur, next);
quad(poly[poly.length - 1]!, cur, end, poly);
}
}
return poly;
}
/** 正常路径:从 on-curve 起点绕一圈 */
const n = pts.length;
const poly: Pt[] = [];
let prevOn = pts[startIdx]!;
poly.push(prevOn);
for (let k = 1; k <= n; k++) {
const i = (startIdx + k) % n;
const cur = pts[i]!;
if (contour[i]!.onCurve) {
poly.push(cur);
prevOn = cur;
} else {
const nextIdx = (i + 1) % n;
const end = contour[nextIdx]!.onCurve ? pts[nextIdx]! : mid(cur, pts[nextIdx]!);
quad(prevOn, cur, end, poly);
/** off-curve 段结束后的隐含 on-curve 点也要成为下一段的起点 */
poly.push(end);
prevOn = end;
}
}
return poly;
}
/**
* 扫描线填充多边形集合(奇偶规则),输出覆盖网格的二值位图
* polys 顶点已是目标网格坐标系(含翻转与缩放)
*/
function scanlineFill(polys: Pt[][]): Uint8Array {
const grid = new Uint8Array(GRID * GRID);
for (const poly of polys) {
if (poly.length < 3) continue;
for (let y = 0; y < GRID; y++) {
const sy = y + 0.5;
/** 收集该扫描线与全部边的交点 x */
const xs: number[] = [];
for (let i = 0; i < poly.length; i++) {
const a = poly[i]!;
const b = poly[(i + 1) % poly.length]!;
/** 水平边无交点 */
if (a.y === b.y) continue;
if ((sy >= a.y && sy < b.y) || (sy >= b.y && sy < a.y)) {
xs.push(a.x + ((sy - a.y) / (b.y - a.y)) * (b.x - a.x));
}
}
if (xs.length < 2) continue;
xs.sort((p, q) => p - q);
for (let k = 0; k + 1 < xs.length; k += 2) {
const x0 = Math.max(0, Math.ceil(xs[k]! - 0.5));
const x1 = Math.min(GRID - 1, Math.floor(xs[k + 1]! - 0.5));
for (let x = x0; x <= x1; x++) grid[y * GRID + x] = 1;
}
}
}
return grid;
}
/**
* 紧致裁剪 + 归一化多边形到 64x64 内框(与前端 maskToNormalizedRGBA 同参)
* 直接对多边形坐标做变换(而非对位图二次采样),避免双重采样损失
*/
function normalizePolys(polys: Pt[][]): Pt[][] {
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
for (const poly of polys) {
for (const p of poly) {
if (p.x < minX) minX = p.x;
if (p.x > maxX) maxX = p.x;
if (p.y < minY) minY = p.y;
if (p.y > maxY) maxY = p.y;
}
}
const w = maxX - minX;
const h = maxY - minY;
/** 退化(空轮廓/纯水平线)保护 */
if (!(w > 0) || !(h > 0)) return [];
const margin = GRID * MARGIN_RATIO;
const inner = GRID - margin * 2;
const scale = Math.min(inner / w, inner / h);
const dw = w * scale;
const dh = h * scale;
const ox = (GRID - dw) / 2 - minX * scale;
const oy = (GRID - dh) / 2 - minY * scale;
return polys.map((poly) => poly.map((p) => ({ x: p.x * scale + ox, y: p.y * scale + oy })));
}
/**
* 取字体的指定码点字形归一化位图(带缓存;缺字返回 null)
*/
export function getGlyphBitmap(
ttf: ReturnType<ReturnType<typeof Font.create>["get"]>,
fontName: string,
codePoint: number,
): Uint8Array | null {
let byFont = glyphBitmapCache.get(fontName);
if (!byFont) {
byFont = new Map();
glyphBitmapCache.set(fontName, byFont);
}
if (byFont.has(codePoint)) return byFont.get(codePoint)!;
let bitmap: Uint8Array | null = null;
try {
/** cmap 的 key 是码点的十进制值;无 cmap 时按 glyf.unicode 兜底扫描 */
let gid = ttf.cmap?.[codePoint as unknown as number] as number | undefined;
if (gid === undefined && ttf.glyf) {
for (let i = 0; i < ttf.glyf.length; i++) {
const u = ttf.glyf[i]!.unicode;
if (u === codePoint || (Array.isArray(u) && u.includes(codePoint))) {
gid = i;
break;
}
}
}
const glyf = typeof gid === "number" ? ttf.glyf?.[gid] : undefined;
const contours = readGlyphContours(ttf, glyf);
if (contours.length > 0) {
const polys = contours.map(flattenContour).filter((p) => p.length >= 3);
const normalized = normalizePolys(polys);
bitmap = normalized.length > 0 ? scanlineFill(normalized) : null;
/** 全空位图视为缺字 */
if (bitmap && !bitmap.some((v) => v)) bitmap = null;
}
} catch {
bitmap = null;
}
byFont.set(codePoint, bitmap);
return bitmap;
}
/**
* IoU(交并比)—— 两个 64x64 二值位图的相似度
* 严格交集版本,对 1px 级错位敏感;容错场景用 glyphSimilarity
*/
export function bitmapIoU(a: Uint8Array, b: Uint8Array): number {
let inter = 0;
let union = 0;
for (let i = 0; i < a.length; i++) {
const va = a[i]!;
const vb = b[i]!;
if (va | vb) union++;
if (va & vb) inter++;
}
return union === 0 ? 0 : inter / union;
}
/**
* 距离变换(Chamfer 3-4 两遍扫描)—— 每个 0 像素到最近 1 像素的距离
* 用于容错比对:拍照残余错切/字重差异下笔画不完全重合,
* 用「笔画像素到对方笔画的平均距离」替代硬交集
*/
function distanceTransform(mask: Uint8Array): Float32Array {
const INF = 1e9;
const dt = new Float32Array(GRID * GRID);
for (let i = 0; i < dt.length; i++) dt[i] = mask[i] ? 0 : INF;
/** 正向扫描(左上 → 右下) */
for (let y = 0; y < GRID; y++) {
for (let x = 0; x < GRID; x++) {
const i = y * GRID + x;
let v = dt[i]!;
if (x > 0) v = Math.min(v, dt[i - 1]! + 3);
if (y > 0) v = Math.min(v, dt[i - GRID]! + 3);
if (x > 0 && y > 0) v = Math.min(v, dt[i - GRID - 1]! + 4);
if (x < GRID - 1 && y > 0) v = Math.min(v, dt[i - GRID + 1]! + 4);
dt[i] = v;
}
}
/** 反向扫描(右下 → 左上) */
for (let y = GRID - 1; y >= 0; y--) {
for (let x = GRID - 1; x >= 0; x--) {
const i = y * GRID + x;
let v = dt[i]!;
if (x < GRID - 1) v = Math.min(v, dt[i + 1]! + 3);
if (y < GRID - 1) v = Math.min(v, dt[i + GRID]! + 3);
if (x < GRID - 1 && y < GRID - 1) v = Math.min(v, dt[i + GRID + 1]! + 4);
if (x > 0 && y < GRID - 1) v = Math.min(v, dt[i + GRID - 1]! + 4);
dt[i] = v;
}
}
return dt;
}
/** a 的笔画像素在 dtB 距离场上的平均距离(越近越像);空掩码返回 -1 */
function meanDistance(a: Uint8Array, dtB: Float32Array, dx: number, dy: number): number {
let sum = 0;
let count = 0;
for (let y = 0; y < GRID; y++) {
const sy = y + dy;
if (sy < 0 || sy >= GRID) continue;
for (let x = 0; x < GRID; x++) {
if (!a[y * GRID + x]) continue;
const sx = x + dx;
if (sx < 0 || sx >= GRID) continue;
sum += dtB[sy * GRID + sx]!;
count++;
}
}
return count === 0 ? -1 : sum / count / 3; /** 除以 3 归一到像素单位 */
}
/**
* 容错字形相似度 —— 对称 Chamfer 距离 + ±2px 平移搜索
*
* 拍照字形经倾角校正后仍可能残余 1~2px 平移/错切,且截图字重与字体
* 实际字重常有差异(加粗/材质晕染)。距离场打分对此不敏感:
* score = exp(-max(meanDist(a→B), meanDist(b→A)) / σ),σ=4px
* 平移搜索取 25 个偏移中的最优值,覆盖亚像素级残余错位。
* 每字形开销约 2 次距离变换 + 25 次均值遍历(~100k 整数/浮点运算),仍为微秒~亚毫秒级。
*/
export function glyphSimilarity(a: Uint8Array, b: Uint8Array): number {
const dtA = distanceTransform(a);
const dtB = distanceTransform(b);
let worst = Infinity;
for (let dy = -2; dy <= 2; dy++) {
for (let dx = -2; dx <= 2; dx++) {
const dAB = meanDistance(a, dtB, dx, dy);
const dBA = meanDistance(b, dtA, -dx, -dy);
if (dAB < 0 || dBA < 0) continue;
const m = Math.max(dAB, dBA);
if (m < worst) worst = m;
}
}
if (!isFinite(worst)) return 0;
return Math.exp(-worst / 4);
}

View File

@ -0,0 +1,139 @@
/**
* POST /api/glyph-match —— 截图找字体:一次请求对全库字体做字形相似度比对
*
* 请求体:{ glyphs: [{ char: string, bitmap: string }] }
* bitmap 为 4096 字符的 01 串(64x64 归一化二值位图,前端 segmenter 产线)
* 响应:{ results: [{ font, score, missing, glyphs: [{ char, score, bitmap, queryIndex }] }] }
* 按平均分降序;bitmap 为该字体渲染的归一化位图(01 串,前端画预览小图)
* queryIndex 对应该字在请求 glyphs 中的位置,候选字体缺字时仍能保持预览对应关系
*
* 性能:字形位图按 字体×字符 进程内缓存(首次请求某字时惰性光栅化),
* 之后每次比对只计算归一化位图相似度。
*/
import { jsonResponse, findFontPath, readFontBuffer } from "../shared";
import { readdir } from "../interface";
import { fontDirs } from "../config";
import { loadFontObject, getGlyphBitmap, glyphSimilarity } from "../font_util/glyph_bitmap";
/** 单个查询字形 */
interface QueryGlyph {
char: string;
bitmap: string;
}
/** 响应中的单字形结果 */
interface GlyphResult {
char: string;
score: number;
bitmap: string;
queryIndex: number;
}
/** 响应中的单字体结果 */
interface FontResult {
font: string;
score: number;
missing: boolean;
glyphs: GlyphResult[];
}
/** 01 串 → Uint8Array;非法输入返回 null */
function parseBitmap(s: unknown): Uint8Array | null {
if (typeof s !== "string" || s.length !== 64 * 64) return null;
const out = new Uint8Array(s.length);
for (let i = 0; i < s.length; i++) {
const c = s.charCodeAt(i);
if (c !== 48 && c !== 49) return null;
out[i] = c - 48;
}
return out;
}
/** Uint8Array 位图 → 01 串 */
function encodeBitmap(b: Uint8Array): string {
let s = "";
for (let i = 0; i < b.length; i++) s += b[i] ? "1" : "0";
return s;
}
/** 遍历字体目录收集候选字体文件名(含临时上传) */
async function listCandidateFonts(): Promise<string[]> {
const names: string[] = [];
for (const dir of fontDirs) {
try {
const entries = await readdir(dir);
for (const entry of entries) {
if (entry.isFile() && /\.(ttf|otf|woff|woff2)$/i.test(entry.name)) {
names.push(entry.name);
}
}
} catch {}
}
return names;
}
export async function handleGlyphMatch(req: Request, _res: Response) {
let body: { glyphs?: QueryGlyph[] };
try {
/** 项目 HTTP 服务把请求体放在 _bodyBuffer,不能直接调用 Request.json() */
const buf = (req as Request & { _bodyBuffer?: ArrayBuffer })._bodyBuffer;
if (!buf || buf.byteLength === 0) {
return { req, res: jsonResponse({ error: "request body required" }, 400) };
}
body = JSON.parse(new TextDecoder().decode(buf));
} catch {
return { req, res: jsonResponse({ error: "invalid json" }, 400) };
}
const queryGlyphs = (body.glyphs ?? [])
.map((g, queryIndex) => ({
char: typeof g?.char === "string" ? g.char : "",
bitmap: parseBitmap(g?.bitmap),
queryIndex,
}))
.filter((g): g is { char: string; bitmap: Uint8Array; queryIndex: number } =>
g.char.length > 0 && g.bitmap !== null,
);
if (queryGlyphs.length === 0) {
return { req, res: jsonResponse({ error: "glyphs required (char + 64x64 bitmap)" }, 400) };
}
const fonts = await listCandidateFonts();
const results: FontResult[] = [];
for (const font of fonts) {
const fontPath = await findFontPath(font);
if (!fontPath) continue;
const ttf = await loadFontObject(font, async (name) => {
const p = await findFontPath(name);
if (!p) return null;
return readFontBuffer(p);
});
if (!ttf) continue;
const glyphResults: GlyphResult[] = [];
let missing = false;
let scoreSum = 0;
for (const q of queryGlyphs) {
const cp = q.char.codePointAt(0)!;
const candidate = getGlyphBitmap(ttf, font, cp);
if (!candidate) {
missing = true;
continue;
}
const score = glyphSimilarity(q.bitmap, candidate);
scoreSum += score;
glyphResults.push({ char: q.char, score, bitmap: encodeBitmap(candidate), queryIndex: q.queryIndex });
}
if (glyphResults.length === 0) continue;
results.push({
font,
score: scoreSum / glyphResults.length,
missing,
glyphs: glyphResults,
});
}
results.sort((a, b) => b.score - a.score);
return { req, res: jsonResponse({ results }) };
}

106
scripts/test_glyph_match.ts Normal file
View File

@ -0,0 +1,106 @@
/**
* 截图找字体 —— 服务端比对自测(无需浏览器,直接测核心算法)
*
* 自测方法(self-consistent 验证):
* 1. 用 getGlyphBitmap 渲染「思源黑体」的目标字,得到 64x64 归一化位图
* 2. 把这些位图当作「用户截图切分后的查询输入」
* 3. 跑全库 glyphSimilarity 比对,思源黑体必须排第一(自渲自比,满分 1.0)
*
* 这一步验证的是服务端光栅化 + 比对的核心链路正确性。
* 前端二值化/切分用另一组注入测试覆盖。
*/
import "../backend/server/node";
import { loadFontObject, getGlyphBitmap, glyphSimilarity } from "../backend/font_util/glyph_bitmap";
import { findFontPath, readFontBuffer } from "../backend/shared";
import { readdir } from "../backend/interface";
import { fontDirs } from "../backend/config";
const TARGET_FONT = "思源黑体.ttf";
const TEXT = "天地玄黄";
async function main() {
console.log("=== 服务端字形比对自测 ===");
/** 1. 渲染目标字体的查询位图(模拟截图切分输入) */
const ttf = await loadFontObject(TARGET_FONT, async (name) => {
const p = await findFontPath(name);
return p ? readFontBuffer(p) : null;
});
if (!ttf) throw new Error(`无法加载目标字体 ${TARGET_FONT}`);
const queryBitmaps: Uint8Array[] = [];
for (const ch of TEXT) {
const cp = ch.codePointAt(0)!;
const bm = getGlyphBitmap(ttf, TARGET_FONT, cp);
if (!bm) throw new Error(`${TARGET_FONT} 渲染 ${ch} 失败(返回空位图)`);
const ink = bm.reduce((a, b) => a + b, 0);
console.log(` ${ch} (U+${cp.toString(16)}): 位图墨点数=${ink}`);
if (ink < 20) throw new Error(`${ch} 位图墨点过少,光栅化可能有问题`);
queryBitmaps.push(bm);
}
console.log(`✓ 目标字体 ${TARGET_FONT} 的「${TEXT}」渲染成功\n`);
/** 2. 遍历全库字体,用同样的字做 glyphSimilarity 比对 */
const fontNames: string[] = [];
for (const dir of fontDirs) {
try {
for (const e of await readdir(dir)) {
if (e.isFile() && /\.(ttf|otf|woff|woff2)$/i.test(e.name)) fontNames.push(e.name);
}
} catch {}
}
console.log(`全库共 ${fontNames.length} 个字体,开始比对…`);
const results: Array<{ font: string; score: number; perChar: number[] }> = [];
for (const font of fontNames) {
const f = await loadFontObject(font, async (name) => {
const p = await findFontPath(name);
return p ? readFontBuffer(p) : null;
});
if (!f) continue;
const perChar: number[] = [];
let sum = 0;
let missing = false;
for (let i = 0; i < TEXT.length; i++) {
const cp = TEXT.codePointAt(i)!;
const cand = getGlyphBitmap(f, font, cp);
if (!cand) {
missing = true;
continue;
}
const s = glyphSimilarity(queryBitmaps[i]!, cand);
perChar.push(s);
sum += s;
}
results.push({ font, score: perChar.length ? sum / perChar.length : 0, perChar });
if (missing) console.log(` ${font}: 缺字`);
}
results.sort((a, b) => b.score - a.score);
console.log("\n=== 比对结果(Top 10)===");
for (const r of results.slice(0, 10)) {
console.log(
`${r.score.toFixed(4)} ${r.font} [${r.perChar.map((s) => s.toFixed(3)).join(", ")}]`,
);
}
/** 3. 断言:目标字体必须排第一,且分数接近 1.0 */
const top = results[0]!;
if (top.font !== TARGET_FONT) {
console.error(`\n❌ FAIL: 目标字体 ${TARGET_FONT} 应排第一,实际第一是 ${top.font}`);
const targetRank = results.findIndex((r) => r.font === TARGET_FONT);
const target = results[targetRank];
console.error(` 目标字体实际排第 ${targetRank + 1},分数 ${target?.score.toFixed(4)}`);
process.exit(1);
}
if (top.score < 0.95) {
console.error(`\n❌ FAIL: 目标字体自渲自比分数应接近 1.0,实际 ${top.score.toFixed(4)}`);
process.exit(1);
}
console.log(`\n✅ PASS: ${TARGET_FONT} 正确排第一,分数 ${top.score.toFixed(4)}(自渲自比)`);
}
main().catch((err) => {
console.error("自测异常:", err);
process.exit(1);
});

187
src/font-finder/matcher.ts Normal file
View File

@ -0,0 +1,187 @@
/**
* 候选字体比对引擎 —— 渲染侧
*
* 对每个候选字体:经后端 subset API 拿到只含目标字符的最小 woff2(服务端有缓存,
* 同字符组合只裁一次)→ FontFace 加载 → canvas 渲染每个字 → 与截图切出的字形
* 归一化后逐一 SSIM,取平均分。
*
* 竞态安全:每个字体用独立 family 名(`ff-match-<序号>-<随机>`),
* 比对完立即 delete() 释放,不污染全局字体空间。
*/
import { calculateSSIM } from "./ssim";
import {
imageToMask,
maskToNormalizedRGBA,
rgbaToDataURL,
smartSegment,
deskewMask,
type Mask,
} from "./segmenter";
/** 单个候选字形的比对结果 */
export interface GlyphMatch {
/** 识别用的字符 */
char: string;
/** 截图字形归一化预览(DataURL) */
queryPreview: string;
/** 候选字体渲染字形归一化预览(DataURL) */
candidatePreview: string;
/** 该字的 SSIM 得分 0~1 */
score: number;
}
/** 单个字体的比对结果 */
export interface FontMatchResult {
/** 字体文件名(后端 font 名) */
font: string;
/** 所有字形平均 SSIM 得分 */
score: number;
/** 逐字明细 */
glyphs: GlyphMatch[];
/** 该字体缺字(subset 后渲染不出目标字符) */
missing: boolean;
/** 比对耗时 ms */
elapsed: number;
}
/**
* 渲染缓冲 canvas 尺寸 —— 远大于归一化网格,保证小笔画细节在降采样前已充分光栅化
*/
const RENDER_SIZE = 128;
/** 共享渲染 canvas(同步使用,无并发问题) */
let renderCanvas: HTMLCanvasElement | null = null;
/**
* 用指定 family 渲染单个字符,返回紧致掩码
* 用 alpha 通道做掩码(不依赖填色),字墨即前景
*/
function renderCharMask(char: string, family: string, fontSize: number): Mask | null {
if (!renderCanvas) renderCanvas = document.createElement("canvas");
const canvas = renderCanvas;
canvas.width = RENDER_SIZE;
canvas.height = RENDER_SIZE;
const ctx = canvas.getContext("2d", { willReadFrequently: true })!;
ctx.clearRect(0, 0, RENDER_SIZE, RENDER_SIZE);
ctx.font = `${fontSize}px ${family}, sans-serif`;
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.fillStyle = "#000";
ctx.fillText(char, RENDER_SIZE / 2, RENDER_SIZE / 2);
const image = ctx.getImageData(0, 0, RENDER_SIZE, RENDER_SIZE);
const { width: w, height: h, data } = image;
/** alpha > 32 视为笔画 */
const mask = new Uint8Array(w * h);
let area = 0;
for (let i = 0; i < w * h; i++) {
if (data[i * 4 + 3]! > 32) {
mask[i] = 1;
area++;
}
}
if (area < 4) return null;
return { data: mask, w, h };
}
/**
* 从完整掩码中紧致裁剪出笔画包围盒子掩码
*(渲染的 mask 四周有大量空白,与截图切分产物的紧致形态对齐)
*/
function tightCrop(mask: Mask): Mask {
const { data, w, h } = mask;
let left = w, right = -1, top = h, bottom = -1;
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
if (data[y * w + x]) {
if (x < left) left = x;
if (x > right) right = x;
if (y < top) top = y;
if (y > bottom) bottom = y;
}
}
}
if (right < 0) return mask;
const cw = right - left + 1;
const ch = bottom - top + 1;
const crop = new Uint8Array(cw * ch);
for (let y = 0; y < ch; y++) {
for (let x = 0; x < cw; x++) {
crop[y * cw + x] = data[(y + top) * w + (x + left)]!;
}
}
return { data: crop, w: cw, h: ch };
}
/**
* 加载候选字体子集 —— 走后端 subset API(GET /api?font=&text=&outType=woff2)
* 只请求目标字符,传输量极小且命中服务端 subset 缓存
*/
async function loadSubsetFontFace(font: string, text: string): Promise<FontFace> {
const url = `/api?font=${encodeURIComponent(font)}&text=${encodeURIComponent(text)}&outType=woff2`;
const res = await fetch(url);
if (!res.ok) throw new Error(`subset failed: ${font} HTTP ${res.status}`);
const buf = await res.arrayBuffer();
const family = `ff-match-${font.replace(/[^a-z0-9]/gi, "")}-${Math.random().toString(36).slice(2, 8)}`;
const face = new FontFace(family, buf);
await face.load();
document.fonts.add(face);
(face as FontFace & { _family?: string })._family = family;
return face;
}
/**
* 对单个候选字体执行比对
*
* @param font 字体文件名
* @param text 用户确认的选区文字(与 queryMasks 一一对应)
* @param queryMasks 截图切分出的归一化 RGBA 字形序列
*/
export async function matchFont(
font: string,
text: string,
queryMasks: Uint8Array[],
previews: string[],
): Promise<FontMatchResult> {
const t0 = performance.now();
const glyphs: GlyphMatch[] = [];
let missing = false;
let face: FontFace | null = null;
try {
face = await loadSubsetFontFace(font, text);
const family = (face as FontFace & { _family?: string })._family!;
for (let i = 0; i < text.length; i++) {
const char = text[i]!;
const rawMask = renderCharMask(char, family, 96);
if (!rawMask) {
missing = true;
continue;
}
const candidateRGBA = maskToNormalizedRGBA(tightCrop(rawMask));
const score = calculateSSIM(queryMasks[i]!, candidateRGBA, 64, 64);
glyphs.push({
char,
queryPreview: previews[i]!,
candidatePreview: rgbaToDataURL(candidateRGBA),
score,
});
}
const score = glyphs.length ? glyphs.reduce((s, g) => s + g.score, 0) / glyphs.length : 0;
return { font, score, glyphs, missing, elapsed: performance.now() - t0 };
} finally {
if (face) document.fonts.delete(face);
}
}
/** 截图 ImageData → 归一化 RGBA 序列 + 预览
* 管线:自适应二值化 → 去噪 → 倾角校正 → 切分 → 归一化
* 已知字数时按方块字等分切分(字间粘连也能切开);未知时用投影切分
*/
export function prepareQueryGlyphs(image: ImageData, count?: number): { masks: Uint8Array[]; previews: string[] } {
const raw = imageToMask(image);
const mask = deskewMask(raw);
const segments = smartSegment(mask, count);
const masks = segments.map((s) => maskToNormalizedRGBA(s));
const previews = masks.map((m) => rgbaToDataURL(m));
return { masks, previews };
}

View File

@ -0,0 +1,482 @@
/**
* 字形切分与归一化 —— 截图找字体的图像处理层
*
* 流程:截图选区 → 灰度 + Otsu 二值化 → 垂直投影切分单字 → 紧致裁剪 →
* 等比缩放居中到固定尺寸网格。渲染侧(canvas 画的候选字形)走同一条
* mask → normalize 路径,保证两侧特征空间一致,SSIM 才有意义。
*/
/** 归一化网格边长(64px 足够保留笔画结构,SSIM 11px 窗口也有约 5x5 个窗口) */
export const GRID = 64;
/** 二值化掩码:1 = 笔画(前景),0 = 背景 */
export interface Mask {
/** 掩码数据,长度 w*h */
data: Uint8Array;
/** 宽 */
w: number;
/** 高 */
h: number;
}
/**
* Otsu 大津法求二值化阈值
* 输入为灰度直方图(256 桶),返回使类间方差最大的阈值
*/
export function otsuThreshold(hist: Float64Array): number {
const total = hist.reduce((a, b) => a + b, 0);
if (total === 0) return 128;
let sumAll = 0;
for (let i = 0; i < 256; i++) sumAll += i * hist[i]!;
let sumBg = 0;
let weightBg = 0;
let best = -1;
let threshold = 128;
for (let t = 0; t < 256; t++) {
weightBg += hist[t]!;
if (weightBg === 0) continue;
const weightFg = total - weightBg;
if (weightFg === 0) break;
sumBg += t * hist[t]!;
const meanBg = sumBg / weightBg;
const meanFg = (sumAll - sumBg) / weightFg;
const between = weightBg * weightFg * (meanBg - meanFg) * (meanBg - meanFg);
if (between > best) {
best = between;
threshold = t;
}
}
return threshold;
}
/**
* RGBA 图像数据 → 二值掩码(墨色 = 前景)—— 照片级鲁棒版
*
* 三层防线应对拍照场景:
* 1. 双通道择优:彩色文字(如金色压暗背景)在亮度通道可能无对比度,
* 同时算「亮度」和「HSV 明度 V」两个通道的掩码,取前景占比更接近
* 正文文字典型值(~12%)的那个
* 2. Sauvola 自适应局部阈值:光照渐变/材质纹理下全局 Otsu 会整片误判,
* 局部窗口(15px)按邻域均值+方差逐像素求阈值,积分图加速
* 3. 连通域去噪:8 邻域 flood fill,扢掉面积小于主组件 1/20 的孤立噪声
*/
export function imageToMask(image: ImageData): Mask {
const { width: w, height: h, data } = image;
const n = w * h;
const lum = new Uint8Array(n);
const val = new Uint8Array(n);
for (let i = 0; i < n; i++) {
const idx = i * 4;
const r = data[idx]!, g = data[idx + 1]!, b = data[idx + 2]!;
lum[i] = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
val[i] = Math.max(r, g, b);
}
const lumMask = sauvolaMask(lum, w, h);
const valMask = sauvolaMask(val, w, h);
/** 前景占比(文字通常 5%~30%,取更接近 12% 的通道) */
const frac = (m: Uint8Array) => {
let c = 0;
for (let i = 0; i < n; i++) c += m[i]!;
return c / n;
};
const fl = frac(lumMask);
const fv = frac(valMask);
const better = Math.abs(fl - 0.12) <= Math.abs(fv - 0.12) ? lumMask : valMask;
const mask = new Uint8Array(n);
mask.set(better);
/** 极性反转:亮字暗底(霓虹/反光材质)时前景占比会 >50%,文字恒为少数側 → 取反 */
const f = frac(mask);
if (f > 0.5) {
for (let i = 0; i < n; i++) mask[i] = mask[i] ? 0 : 1;
}
despeckleInPlace(mask, w, h);
return { data: mask, w, h };
}
/**
* Sauvola 局部自适应阈值二值化(墨暗前提交路)
* t(x,y) = m * (1 + k * (s/R - 1)),窗口 15、k=0.2、R=128
*/
function sauvolaMask(gray: Uint8Array, w: number, h: number): Uint8Array {
const n = w * h;
/** 积分图(sum / sum^2),尺寸 (w+1)*(h+1) */
const w1 = w + 1;
const isum = new Float64Array(w1 * (h + 1));
const isq = new Float64Array(w1 * (h + 1));
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const v = gray[y * w + x]!;
const ip = (y + 1) * w1 + x + 1;
isum[ip] = v + isum[ip - 1]! + isum[ip - w1]! - isum[ip - w1 - 1]!;
isq[ip] = v * v + isq[ip - 1]! + isq[ip - w1]! - isq[ip - w1 - 1]!;
}
}
const WIN = 15;
const r = WIN >> 1;
const K = 0.2;
const R = 128;
const out = new Uint8Array(n);
for (let y = 0; y < h; y++) {
const y1 = Math.max(0, y - r), y2 = Math.min(h, y + r + 1);
for (let x = 0; x < w; x++) {
const x1 = Math.max(0, x - r), x2 = Math.min(w, x + r + 1);
const cnt = (x2 - x1) * (y2 - y1);
const s = isum[y2 * w1 + x2]! - isum[y1 * w1 + x2]! - isum[y2 * w1 + x1]! + isum[y1 * w1 + x1]!;
const sq = isq[y2 * w1 + x2]! - isq[y1 * w1 + x2]! - isq[y2 * w1 + x1]! + isq[y1 * w1 + x1]!;
const m = s / cnt;
const sd = Math.sqrt(Math.max(0, sq / cnt - m * m));
const t = m * (1 + K * (sd / R - 1));
out[y * w + x] = gray[y * w + x]! <= t ? 1 : 0;
}
}
return out;
}
/**
* 连通域去噪(4 邻域洪泛)
* 以最大组件为基准,面积 < max(4, 主组件/20) 的组件视为噪声抹除。
* 栈式 flood fill,避免递归爆栈;扫描线式访问保证缓存友好。
*/
function despeckleInPlace(mask: Uint8Array, w: number, h: number): void {
const n = w * h;
const visited = new Uint8Array(n);
const components: Array<{ area: number; pixels: number[] }> = [];
const stack: number[] = [];
for (let i = 0; i < n; i++) {
if (!mask[i] || visited[i]) continue;
const pixels: number[] = [];
stack.push(i);
visited[i] = 1;
while (stack.length) {
const p = stack.pop()!;
pixels.push(p);
const px = p % w;
const py = (p - px) / w;
const neighbors = [
px > 0 ? p - 1 : -1,
px < w - 1 ? p + 1 : -1,
py > 0 ? p - w : -1,
py < h - 1 ? p + w : -1,
];
for (const q of neighbors) {
if (q >= 0 && mask[q] && !visited[q]) {
visited[q] = 1;
stack.push(q);
}
}
}
components.push({ area: pixels.length, pixels });
}
if (components.length < 2) return;
const maxArea = Math.max(...components.map((c) => c.area));
const minKeep = Math.max(4, Math.floor(maxArea / 20));
for (const c of components) {
if (c.area < minKeep) {
for (const p of c.pixels) mask[p] = 0;
}
}
}
/**
* 倾角自动校正 —— 拍照旋转/倾斜 {-12°..12°} 内找使行投影方差最大的角度
* (文字行水平时横画笔画在同一行堆叠,投影最「尖」;歪斜时被拉平)。
* 粗扫步进 1.5°,最优点附近 0.25° 细扫。返回校正后的新掩码。
*/
export function deskewMask(mask: Mask): Mask {
const { data, w, h } = mask;
/** 墨点坐标收集 */
const xs: number[] = [];
const ys: number[] = [];
let cx = 0;
let cy = 0;
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
if (data[y * w + x]) {
xs.push(x);
ys.push(y);
cx += x;
cy += y;
}
}
}
if (xs.length < 8) return mask;
cx /= xs.length;
cy /= ys.length;
/** 给定角度的行投影方差(以中心为轴旋转) */
const profileVariance = (angle: number): number => {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const rows = new Float64Array(h + 1);
for (let i = 0; i < xs.length; i++) {
const dx = xs[i]! - cx;
const dy = ys[i]! - cy;
const ry = Math.round(sin * dx + cos * dy + cy);
if (ry >= 0 && ry <= h) rows[ry]!++;
}
let mean = 0;
for (const v of rows) mean += v;
mean /= rows.length;
let vr = 0;
for (const v of rows) vr += (v - mean) * (v - mean);
return vr / rows.length;
};
/** 粗扫 + 细扫 */
let best = 0;
let bestV = -1;
for (let a = -12; a <= 12; a += 1.5) {
const v = profileVariance((a * Math.PI) / 180);
if (v > bestV) {
bestV = v;
best = a;
}
}
for (let a = best - 1.5; a <= best + 1.5; a += 0.25) {
const v = profileVariance((a * Math.PI) / 180);
if (v > bestV) {
bestV = v;
best = a;
}
}
/** 校正量过小则不动(避免无谓的重采样损失) */
if (Math.abs(best) < 0.3) return mask;
/** 反向旋转回正(最近邻采样),并紧致裁剪 */
const rad = (-best * Math.PI) / 180;
const cos = Math.cos(rad);
const sin = Math.sin(rad);
let minX = w, maxX = -1, minY = h, maxY = -1;
const nx: number[] = [];
const ny: number[] = [];
for (let i = 0; i < xs.length; i++) {
const dx = xs[i]! - cx;
const dy = ys[i]! - cy;
const x = Math.round(cos * dx - sin * dy + cx);
const y = Math.round(sin * dx + cos * dy + cy);
nx.push(x);
ny.push(y);
if (x < minX) minX = x;
if (x > maxX) maxX = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
}
if (maxX < 0 || maxY < 0) return mask;
const cw = Math.min(w, maxX - minX + 1);
const chh = Math.min(h, maxY - minY + 1);
const out = new Uint8Array(cw * chh);
for (let i = 0; i < nx.length; i++) {
const x = nx[i]! - minX;
const y = ny[i]! - minY;
if (x >= 0 && x < cw && y >= 0 && y < chh) out[y * cw + x] = 1;
}
return { data: out, w: cw, h: chh };
}
/**
* 垂直投影切分单字(适用于横向排版的等高文字行)
* 按列是否有笔画分界;过窄的段并入相邻段(笔画断裂/粘连兜底)
*/
export function segmentByProjection(mask: Mask, options?: { minGap?: number; minArea?: number }): Mask[] {
const { data, w, h } = mask;
const minGap = options?.minGap ?? 2;
const minArea = options?.minArea ?? Math.max(8, Math.round(w * h * 0.0005));
const colHasInk = new Uint8Array(w);
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
if (data[y * w + x]) {
colHasInk[x] = 1;
break;
}
}
}
/** 初始切分:连续有墨列段,允许 minGap 宽度的间隙跨过 */
const segments: Array<[number, number]> = [];
let start = -1;
let gapRun = 0;
for (let x = 0; x < w; x++) {
if (colHasInk[x]) {
if (start === -1) start = x;
gapRun = 0;
} else if (start !== -1) {
gapRun++;
if (gapRun > minGap) {
segments.push([start, x - gapRun]);
start = -1;
}
}
}
if (start !== -1) segments.push([start, w - 1]);
/** 逐段紧致裁剪 + 噪声过滤 */
const result: Mask[] = [];
for (const [x1, x2] of segments) {
let top = h, bottom = -1, area = 0;
for (let y = 0; y < h; y++) {
for (let x = x1; x <= x2; x++) {
if (data[y * w + x]) {
area++;
if (y < top) top = y;
if (y > bottom) bottom = y;
}
}
}
if (bottom < 0 || area < minArea) continue;
const cw = x2 - x1 + 1;
const ch = bottom - top + 1;
const crop = new Uint8Array(cw * ch);
for (let y = 0; y < ch; y++) {
for (let x = 0; x < cw; x++) {
crop[y * cw + x] = data[(y + top) * w + (x1 + x)]!;
}
}
result.push({ data: crop, w: cw, h: ch });
}
return result;
}
/**
* 按指定字数切分掩码 —— 方块字(CJK)专用切分策略
*
* 垂直投影切分在「字间笔画粘连」时失效(如行楷/美术字「乾坤」连成一个墨块),
* 而中文本质是等宽方块字,按字数等分选区更可靠:
* 1. 先找出整行笔画的包围盒(左右顶紧,避免两侧空白稀释单元格宽度)
* 2. 按字数 N 把包围盒均分为 N 个等宽单元格
* 3. 每格边界在局部 ±20% 区间内微调:寻找投影墨量最小的 x 作为切线
* (优先沿字间隙切,粘连时也最多切到笔画细处,而非硬切主笔)
* 4. 每格独立紧致裁剪(含上下方向)
*/
export function segmentByCount(mask: Mask, count: number): Mask[] {
if (count <= 0) return [];
if (count === 1) {
const segs = segmentByProjection(mask, { minGap: mask.w });
return segs.slice(0, 1);
}
const { data, w, h } = mask;
/** 整行包围盒 */
let left = w, right = -1;
const colInk = new Uint32Array(w);
for (let x = 0; x < w; x++) {
let c = 0;
for (let y = 0; y < h; y++) c += data[y * w + x]!;
colInk[x] = c;
if (c > 0) {
if (x < left) left = x;
if (x > right) right = x;
}
}
if (right < 0) return [];
const totalW = right - left + 1;
const cellW = totalW / count;
/** 微调窗口:单元格宽度的 20%(粘连字最多允许 ±20% 的切线偏移) */
const adjustRange = Math.max(1, Math.round(cellW * 0.2));
/** 计算 N+1 条切线的 x 坐标:首尾贴包围盒,中间按局部墨量最小微调 */
const cuts: number[] = [left];
for (let i = 1; i < count; i++) {
const ideal = Math.round(left + i * cellW);
let bestX = ideal;
let bestInk = Infinity;
for (let x = Math.max(left + 1, ideal - adjustRange); x <= Math.min(right, ideal + adjustRange); x++) {
const ink = colInk[x]!;
if (ink < bestInk) {
bestInk = ink;
bestX = x;
}
}
cuts.push(bestX);
}
cuts.push(right + 1);
/** 逐单元格紧致裁剪 */
const result: Mask[] = [];
for (let i = 0; i < count; i++) {
const x1 = cuts[i]!;
const x2 = cuts[i + 1]! - 1;
let top = h, bottom = -1;
for (let y = 0; y < h; y++) {
for (let x = x1; x <= x2; x++) {
if (data[y * w + x]) {
if (y < top) top = y;
if (y > bottom) bottom = y;
}
}
}
/** 空单元格(该字全空白)仍占位返回 1x1 空掩码,保持与输入文字的下标对齐 */
if (bottom < 0) {
result.push({ data: new Uint8Array(1), w: 1, h: 1 });
continue;
}
const cw = x2 - x1 + 1;
const ch = bottom - top + 1;
const crop = new Uint8Array(cw * ch);
for (let y = 0; y < ch; y++) {
for (let x = 0; x < cw; x++) {
crop[y * cw + x] = data[(y + top) * w + (x1 + x)]!;
}
}
result.push({ data: crop, w: cw, h: ch });
}
return result;
}
/**
* 智能切分:投影切分与按字数切分择优
* 已知字数时用 segmentByCount(方块字粘连场景),未知时用投影切分
*/
export function smartSegment(mask: Mask, count?: number): Mask[] {
if (count && count > 0) return segmentByCount(mask, count);
return segmentByProjection(mask);
}
/**
* 掩码归一化到 GRID x GRID 的 RGBA 像素(供 SSIM 与预览共用)
* 等比缩放、居中放置,四周留白约 12%;输出灰度图:笔画 0(黑)、背景 255(白)
*/
export function maskToNormalizedRGBA(mask: Mask): Uint8Array {
const { data, w, h } = mask;
/** 有效绘制区(留 12% 边距,笔画贴边时 SSIM 窗口越界补偿) */
const margin = Math.round(GRID * 0.12);
const inner = GRID - margin * 2;
const scale = Math.min(inner / w, inner / h);
const dw = Math.max(1, Math.round(w * scale));
const dh = Math.max(1, Math.round(h * scale));
const ox = Math.round((GRID - dw) / 2);
const oy = Math.round((GRID - dh) / 2);
/** 目标网格先落 0/1,再转灰度 RGBA */
const grid = new Uint8Array(GRID * GRID);
for (let y = 0; y < dh; y++) {
const sy = Math.min(h - 1, Math.floor((y + 0.5) / scale));
for (let x = 0; x < dw; x++) {
const sx = Math.min(w - 1, Math.floor((x + 0.5) / scale));
if (data[sy * w + sx]) grid[(y + oy) * GRID + (x + ox)] = 1;
}
}
const out = new Uint8Array(GRID * GRID * 4);
for (let i = 0; i < GRID * GRID; i++) {
const v = grid[i] ? 0 : 255;
out[i * 4] = v;
out[i * 4 + 1] = v;
out[i * 4 + 2] = v;
out[i * 4 + 3] = 255;
}
return out;
}
/** 归一化 RGBA 转DataURL,用于结果预览小图 */
export function rgbaToDataURL(rgba: Uint8Array): string {
const canvas = document.createElement("canvas");
canvas.width = GRID;
canvas.height = GRID;
const ctx = canvas.getContext("2d")!;
const img = ctx.createImageData(GRID, GRID);
img.data.set(rgba);
ctx.putImageData(img, 0, 0);
return canvas.toDataURL();
}

92
src/font-finder/ssim.ts Normal file
View File

@ -0,0 +1,92 @@
/**
* SSIM(结构相似性)计算 —— Wang 2004 实现
*
* 与 scripts/ssim.ts(基准测试用)同算法,此处为前端字体识别页面独立持有,
* 输入约定一致:RGBA 像素数据,width*height*4 === data.length,否则返回 0。
*/
export function calculateSSIM(a: Uint8Array, b: Uint8Array, width: number, height: number): number {
if (a.length !== b.length) return 0;
if (width * height * 4 !== a.length) return 0;
if (width === 0 || height === 0) return 0;
/** 转灰度并提取到独立数组 */
const N = width * height;
const grayA = new Float64Array(N);
const grayB = new Float64Array(N);
for (let i = 0; i < N; i++) {
const idx = i * 4;
grayA[i] = 0.299 * a[idx]! + 0.587 * a[idx + 1]! + 0.114 * a[idx + 2]!;
grayB[i] = 0.299 * b[idx]! + 0.587 * b[idx + 1]! + 0.114 * b[idx + 2]!;
}
/** 构建积分图: S(x,y) = sum of gray[0..x-1, 0..y-1] */
const w1 = width + 1;
const intA = new Float64Array(w1 * (height + 1));
const intA2 = new Float64Array(w1 * (height + 1));
const intB = new Float64Array(w1 * (height + 1));
const intB2 = new Float64Array(w1 * (height + 1));
const intAB = new Float64Array(w1 * (height + 1));
for (let y = 0; y < height; y++) {
const rowOff = y * width;
const irowOff = (y + 1) * w1;
for (let x = 0; x < width; x++) {
const va = grayA[rowOff + x]!;
const vb = grayB[rowOff + x]!;
const ip = irowOff + x + 1;
intA[ip] = va + intA[ip - 1]! + intA[ip - w1]! - intA[ip - w1 - 1]!;
intA2[ip] = va * va + intA2[ip - 1]! + intA2[ip - w1]! - intA2[ip - w1 - 1]!;
intB[ip] = vb + intB[ip - 1]! + intB[ip - w1]! - intB[ip - w1 - 1]!;
intB2[ip] = vb * vb + intB2[ip - 1]! + intB2[ip - w1]! - intB2[ip - w1 - 1]!;
intAB[ip] = va * vb + intAB[ip - 1]! + intAB[ip - w1]! - intAB[ip - w1 - 1]!;
}
}
/** 从积分图计算矩形区域 [x1, x2) x [y1, y2) 的和 */
const rectSum = (img: Float64Array, x1: number, y1: number, x2: number, y2: number) =>
img[y2 * w1 + x2]! - img[y1 * w1 + x2]! - img[y2 * w1 + x1]! + img[y1 * w1 + x1]!;
/** 11x11 窗口, 半径=5 */
const R = 5;
/** (0.01 * 255)^2 */
const C1 = 6.5025;
/** (0.03 * 255)^2 */
const C2 = 58.5225;
/** 窗口尺寸过小时(< 11px)退化为全图单窗口,避免 SSIM 恒 0 */
const wx1 = Math.max(1, Math.min(R, Math.floor(width / 2)));
const wx2 = Math.max(wx1 + 1, width - Math.min(R, Math.floor(width / 2)));
const wy1 = Math.max(1, Math.min(R, Math.floor(height / 2)));
const wy2 = Math.max(wy1 + 1, height - Math.min(R, Math.floor(height / 2)));
let ssimSum = 0;
let windowCount = 0;
for (let y = wy1; y < wy2; y++) {
for (let x = wx1; x < wx2; x++) {
const x1 = x - R, x2 = x + R + 1;
const y1 = y - R, y2 = y + R + 1;
const n = (2 * R + 1) * (2 * R + 1);
const sA = rectSum(intA, x1, y1, x2, y2);
const sA2 = rectSum(intA2, x1, y1, x2, y2);
const sB = rectSum(intB, x1, y1, x2, y2);
const sB2 = rectSum(intB2, x1, y1, x2, y2);
const sAB = rectSum(intAB, x1, y1, x2, y2);
const muA = sA / n;
const muB = sB / n;
const sigmaA2 = sA2 / n - muA * muA;
const sigmaB2 = sB2 / n - muB * muB;
const sigmaAB = sAB / n - muA * muB;
ssimSum +=
((2 * muA * muB + C1) * (2 * sigmaAB + C2)) /
((muA * muA + muB * muB + C1) * (sigmaA2 + sigmaB2 + C2));
windowCount++;
}
}
return windowCount === 0 ? 0 : ssimSum / windowCount;
}

View File

@ -14,6 +14,10 @@ import App from "./App.vue";
const routes = [
{ path: "/", component: () => import("./pages/Home.vue") },
{ path: "/offline-subset", component: () => import("./pages/OfflineSubset.vue") },
/**
* 截图找字体 —— 上传文字截图,与站内字体库做字形相似度比对
*/
{ path: "/font-finder", component: () => import("./pages/FontFinder.vue") },
{ path: "/demo", component: () => import("./pages/Demo.vue") },
{ path: "/docs", component: () => import("./pages/Docs.vue") },
{ path: "/fonts", component: () => import("./pages/FontList.vue") },

421
src/pages/FontFinder.vue Normal file
View File

@ -0,0 +1,421 @@
<script setup lang="ts">
/**
* 截图找字体页面 —— 上传文字截图,在站内字体库中找出最相似的字体
*
* 流程:
* 1. 上传/粘贴文字截图,框选一行文字
* 2. 自动切分图块,用户逐块填写对应字符,无法确认的块可留空
* 3. 一次请求提交所有已填写图块,服务端完成全库字形比对
* 4. 按平均分排序展示,附逐字对比小图
*/
import { ref, computed, useTemplateRef } from "vue";
import { usePageSeo } from "../useSeo";
import { prepareQueryGlyphs } from "../font-finder/matcher";
import { rgbaToDataURL } from "../font-finder/segmenter";
/** 服务端返回的单字体结果(与 backend/routes/glyph_match.ts 对齐) */
interface ServerFontResult {
font: string;
score: number;
missing: boolean;
glyphs: Array<{ char: string; score: number; bitmap: string; queryIndex: number }>;
}
/** 归一化 RGBA(0/255 灰度)→ 4096 字符 01 串(笔画=1) */
function rgbaToBitString(rgba: Uint8Array): string {
let s = "";
for (let i = 0; i < 64 * 64; i++) s += rgba[i * 4] === 0 ? "1" : "0";
return s;
}
/** 01 串 → 灰度预览 DataURL */
function bitStringToDataURL(bits: string): string {
const rgba = new Uint8Array(64 * 64 * 4);
for (let i = 0; i < bits.length; i++) {
const v = bits[i] === "1" ? 0 : 255;
rgba[i * 4] = v;
rgba[i * 4 + 1] = v;
rgba[i * 4 + 2] = v;
rgba[i * 4 + 3] = 255;
}
return rgbaToDataURL(rgba);
}
usePageSeo({
title: "截图找字体 | 以图搜字体 | 在线字体识别",
description: "上传文字截图,框选并确认文字,即可在站内字体库中查找字形最相似的字体。",
path: "/font-finder",
priority: 0.8,
changefreq: "weekly",
});
/** 图片文件输入 */
const fileInput = useTemplateRef<HTMLInputElement>("fileInput");
/** 图片展示元素(用于计算选区相对坐标) */
const imgEl = useTemplateRef<HTMLImageElement>("imgEl");
/** 选区覆盖层容器 */
const stageEl = useTemplateRef<HTMLDivElement>("stageEl");
/** 原始截图对象 URL */
const imageUrl = ref("");
/** 选区(相对展示尺寸的像素坐标) */
const selection = ref<{ x: number; y: number; w: number; h: number } | null>(null);
/** 选区拖拽进行时状态 */
const dragging = ref<{ x0: number; y0: number; x1: number; y1: number } | null>(null);
/** 是否正在比对 */
const matching = ref(false);
/** 比对进度(已完成字体数) */
const doneCount = ref(0);
/** 总字体数 */
const totalCount = ref(0);
/** 结果列表(按分数降序) */
const results = ref<ServerFontResult[]>([]);
/** 错误信息 */
const errorMsg = ref("");
/** 切分出的字形预览 */
const glyphPreviews = ref<string[]>([]);
/** 每个图块对应的一个字符,空字符串表示不参与识别 */
const glyphChars = ref<string[]>([]);
/** 切分出的归一化字形(非响应式,体积较大) */
let queryMasks: Uint8Array[] = [];
/** 当前步骤:1上传 2框选 3输入 4结果(用于步骤条高亮) */
const step = computed(() => {
if (!imageUrl.value) return 1;
if (!selection.value) return 2;
if (!results.value.length) return 3;
return 4;
});
/** 识别耗时(ms,结果展示用) */
const elapsedMs = ref(0);
/** 重选图片:清空当前图片与全部状态 */
function resetImage() {
if (imageUrl.value) URL.revokeObjectURL(imageUrl.value);
imageUrl.value = "";
selection.value = null;
dragging.value = null;
glyphPreviews.value = [];
glyphChars.value = [];
queryMasks = [];
results.value = [];
errorMsg.value = "";
}
/** 图片显示尺寸(选区坐标换算回原始像素用) */
let displayedWidth = 0;
let displayedHeight = 0;
let naturalWidth = 0;
let naturalHeight = 0;
/** 已填写对应字符的图块下标,顺序与识别请求中的 glyphs 一致 */
const filledGlyphIndices = computed(() => {
const indices: number[] = [];
for (const [index, char] of glyphChars.value.entries()) {
if (char && queryMasks[index]) indices.push(index);
}
return indices;
});
/** 是否满足开始识别条件:至少填写一个图块 */
const canMatch = computed(
() => !!selection.value && filledGlyphIndices.value.length > 0 && !matching.value,
);
/** 选择图片 */
function onFileSelect(e: Event) {
const input = e.target as HTMLInputElement;
const file = input.files?.[0];
if (file) loadImage(file);
input.value = "";
}
/** 粘贴截图 */
async function onPaste(e: ClipboardEvent) {
const item = [...(e.clipboardData?.items ?? [])].find((i) => i.type.startsWith("image/"));
const file = item?.getAsFile();
if (file) loadImage(file);
}
/** 加载图片并重置状态 */
function loadImage(file: File) {
errorMsg.value = "";
results.value = [];
glyphPreviews.value = [];
glyphChars.value = [];
queryMasks = [];
selection.value = null;
if (imageUrl.value) URL.revokeObjectURL(imageUrl.value);
imageUrl.value = URL.createObjectURL(file);
}
/** 图片加载完成:记录展示/原始尺寸 */
function onImgLoad() {
const img = imgEl.value!;
displayedWidth = img.clientWidth;
displayedHeight = img.clientHeight;
naturalWidth = img.naturalWidth;
naturalHeight = img.naturalHeight;
}
/** 选区拖拽开始 */
function onPointerDown(e: PointerEvent) {
if (!stageEl.value || !imageUrl.value) return;
/** 阻止默认行为:图片原生拖拽 ghost、文本选择(触屏长按菜单)都会干扰框选 */
e.preventDefault();
const rect = stageEl.value.getBoundingClientRect();
dragging.value = { x0: e.clientX - rect.left, y0: e.clientY - rect.top, x1: e.clientX - rect.left, y1: e.clientY - rect.top };
(e.target as HTMLElement).setPointerCapture(e.pointerId);
}
/** 选区拖拽移动 */
function onPointerMove(e: PointerEvent) {
if (!dragging.value || !stageEl.value) return;
const rect = stageEl.value.getBoundingClientRect();
dragging.value.x1 = Math.max(0, Math.min(displayedWidth, e.clientX - rect.left));
dragging.value.y1 = Math.max(0, Math.min(displayedHeight, e.clientY - rect.top));
}
/** 选区拖拽结束:落定选区并切分字形 */
function onPointerUp() {
if (!dragging.value) return;
const { x0, y0, x1, y1 } = dragging.value;
dragging.value = null;
const x = Math.min(x0, x1), y = Math.min(y0, y1);
const w = Math.abs(x1 - x0), h = Math.abs(y1 - y0);
/** 最小选区尺寸兜底(避免误点产生空选区) */
if (w < 8 || h < 8) {
selection.value = null;
glyphPreviews.value = [];
return;
}
selection.value = { x, y, w, h };
segmentSelection();
}
/** 对当前选区切分字形(预览 + 数量校验) */
function segmentSelection() {
const sel = selection.value;
const img = imgEl.value;
if (!sel || !img) return;
errorMsg.value = "";
results.value = [];
/** 展示坐标 → 原始像素坐标 */
const sx = naturalWidth / displayedWidth;
const sy = naturalHeight / displayedHeight;
const cropW = Math.max(1, Math.round(sel.w * sx));
const cropH = Math.max(1, Math.round(sel.h * sy));
const canvas = document.createElement("canvas");
canvas.width = cropW;
canvas.height = cropH;
const ctx = canvas.getContext("2d", { willReadFrequently: true })!;
ctx.drawImage(
img,
Math.round(sel.x * sx), Math.round(sel.y * sy), cropW, cropH,
0, 0, cropW, cropH,
);
const { masks, previews } = prepareQueryGlyphs(ctx.getImageData(0, 0, cropW, cropH));
glyphPreviews.value = previews;
glyphChars.value = previews.map(() => "");
queryMasks = masks;
}
/** 修改对应字符后,旧结果不再有效 */
function onGlyphInput() {
results.value = [];
}
/** 服务端结果 → 展示用预览图(同字体同字只转一次) */
const glyphPreviewCache = new Map<string, string>();
function serverGlyphPreview(font: string, char: string, bitmap: string): string {
const key = font + "|" + char;
let url = glyphPreviewCache.get(key);
if (!url) {
url = bitStringToDataURL(bitmap);
glyphPreviewCache.set(key, url);
}
return url;
}
/** 结果中的查询序号 → 原始图块预览,留空图块与候选字体缺字均不会造成错位 */
function queryPreview(queryIndex: number): string {
const sourceIndex = filledGlyphIndices.value[queryIndex];
return sourceIndex === undefined ? "" : (glyphPreviews.value[sourceIndex] ?? "");
}
/** 开始识别:一次请求,只提交已填写字符的图块 */
async function startMatch() {
if (!canMatch.value) return;
matching.value = true;
errorMsg.value = "";
results.value = [];
doneCount.value = 0;
const t0 = performance.now();
try {
const res = await fetch("/api/glyph-match", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
glyphs: filledGlyphIndices.value.map((sourceIndex) => ({
char: glyphChars.value[sourceIndex]!,
bitmap: rgbaToBitString(queryMasks[sourceIndex]!),
})),
}),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = (await res.json()) as { results?: ServerFontResult[]; error?: string };
if (data.error) throw new Error(data.error);
results.value = data.results ?? [];
totalCount.value = results.value.length;
doneCount.value = results.value.length;
elapsedMs.value = Math.round(performance.now() - t0);
} catch (err) {
errorMsg.value = `识别失败:${err instanceof Error ? err.message : String(err)}`;
} finally {
matching.value = false;
}
}
/** 分数展示为百分比 */
const pct = (s: number) => `${(s * 100).toFixed(1)}%`;
/** 分数对应颜色 */
const scoreColor = (s: number) => (s > 0.9 ? "#16a34a" : s > 0.8 ? "#d97706" : "#999");
</script>
<template>
<div style="max-width: 960px; margin: 0 auto; padding: 24px 16px 64px" @paste="onPaste">
<h1 style="font-size: 26px; margin: 0 0 8px">截图找字体</h1>
<p style="color: #666; margin: 0 0 20px; font-size: 14px">
上传含文字的截图,与站内字体库比对,找出最相似的字体。
</p>
<!-- 步骤指示器 -->
<div style="display: flex; gap: 6px; margin-bottom: 20px; font-size: 13px; flex-wrap: wrap">
<span v-for="(label, i) in ['1 上传截图', '2 框选文字', '3 输入文字并识别']" :key="i" :style="{ padding: '5px 12px', borderRadius: '999px', fontWeight: step >= i + 1 && step <= i + 2 ? 600 : 400, color: step === i + 1 ? '#fff' : step > i + 1 ? '#16a34a' : '#999', background: step === i + 1 ? '#1677ff' : 'transparent', border: `1px solid ${step === i + 1 ? '#1677ff' : step > i + 1 ? '#16a34a' : '#ddd'}` }">
{{ step > i + 1 ? '✓ ' : '' }}{{ label }}
</span>
<span style="margin-left: auto; align-self: center">
<button v-if="imageUrl && !matching" style="padding: 4px 12px; border: 1px solid #d9d9d9; border-radius: 6px; background: #fff; font-size: 13px; cursor: pointer" @click="resetImage">重新开始</button>
</span>
</div>
<!-- ─── 第 1+2 步:图片与框选 ─── -->
<div v-if="!imageUrl" style="border: 2px dashed #bbb; border-radius: 12px; padding: 56px 16px; text-align: center; cursor: pointer; transition: border-color .15s" @click="fileInput?.click()">
<div style="font-size: 34px">🖼️</div>
<div style="color: #555; font-size: 16px; margin-top: 10px; font-weight: 500">点击选择图片,或直接 Ctrl+V 粘贴截图</div>
<div style="color: #aaa; font-size: 13px; margin-top: 6px">支持 png / jpg / webp</div>
</div>
<div v-else>
<div style="font-size: 15px; font-weight: 600; margin-bottom: 8px; color: #333">
{{ selection ? '2️⃣ 框选文字 ✅ — 可重新拖拽调整选区' : '2️⃣ 框选文字 —— 请在图中拖拽框选一行文字' }}
</div>
<div
ref="stageEl"
style="position: relative; display: inline-block; max-width: 100%; border: 1px solid #ddd; border-radius: 8px; overflow: hidden; cursor: crosshair; user-select: none; touch-action: none"
@pointerdown="onPointerDown"
@pointermove="onPointerMove"
@pointerup="onPointerUp"
@dragstart.prevent
>
<img ref="imgEl" :src="imageUrl" draggable="false" style="display: block; max-width: 100%; -webkit-user-drag: none" @load="onImgLoad" />
<div
v-if="selection"
:style="{ position: 'absolute', left: selection.x + 'px', top: selection.y + 'px', width: selection.w + 'px', height: selection.h + 'px', border: '2px solid #1677ff', background: 'rgba(22,119,255,0.12)', pointerEvents: 'none' }"
/>
<div
v-if="dragging"
:style="{ position: 'absolute', left: Math.min(dragging.x0, dragging.x1) + 'px', top: Math.min(dragging.y0, dragging.y1) + 'px', width: Math.abs(dragging.x1 - dragging.x0) + 'px', height: Math.abs(dragging.y1 - dragging.y0) + 'px', border: '2px dashed #1677ff', pointerEvents: 'none' }"
/>
</div>
</div>
<input ref="fileInput" type="file" accept="image/*" style="display: none" @change="onFileSelect" />
<!-- ─── 第 3 步:逐块确认文字并识别(框选后始终显示) ─── -->
<div v-if="selection" style="margin-top: 20px; border: 1px solid #e5e5e5; border-radius: 12px; padding: 18px 20px; background: #fafafa">
<div style="font-size: 15px; font-weight: 600; margin-bottom: 10px; color: #333">3️⃣ 确认每个图块对应的文字</div>
<div v-if="glyphPreviews.length" style="display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-start; margin-bottom: 14px">
<div v-for="(p, i) in glyphPreviews" :key="i" style="width: 52px; text-align: center">
<img :src="p" style="width: 52px; height: 52px; image-rendering: pixelated; border: 1px solid #ddd; border-radius: 6px; background: #fff; box-sizing: border-box" />
<input
v-model.trim="glyphChars[i]"
maxlength="1"
placeholder="字"
:aria-label="`第 ${i + 1} 个图块对应的文字`"
style="width: 52px; height: 34px; margin-top: 6px; padding: 4px; border: 1.5px solid #d9d9d9; border-radius: 6px; font-size: 16px; text-align: center; outline: none; box-sizing: border-box; background: #fff"
@input="onGlyphInput"
/>
</div>
</div>
<div v-else style="color: #d97706; font-size: 13px; margin-bottom: 14px">
⚠️ 未从选区中切分出图块,请调整选区后重试
</div>
<div style="display: flex; gap: 12px; flex-wrap: wrap; align-items: center; justify-content: space-between">
<span style="color: #777; font-size: 13px; line-height: 1.6">
每个图块填写一个字;无法确认或粘连在一起的图块可留空,至少填写一个字。
</span>
<button
:disabled="!canMatch"
style="padding: 11px 28px; border: none; border-radius: 8px; background: #1677ff; color: #fff; font-size: 16px; font-weight: 600; cursor: pointer; white-space: nowrap"
:style="{ opacity: canMatch ? 1 : 0.45, cursor: canMatch ? 'pointer' : 'not-allowed' }"
@click="startMatch"
>
开始识别
</button>
</div>
<div v-if="filledGlyphIndices.length" style="color: #1677ff; font-size: 13px; margin-top: 8px">
已填写 {{ filledGlyphIndices.length }} 个字,留空的图块不会参与识别。
</div>
<!-- 识别中进度 -->
<div v-if="matching" style="margin-top: 14px; padding: 12px 16px; border: 1px solid #91caff; background: #e6f4ff; border-radius: 8px; font-size: 14px; color: #0958d9; display: flex; align-items: center; gap: 10px">
<span style="display: inline-block; width: 16px; height: 16px; border: 2px solid #1677ff; border-top-color: transparent; border-radius: 50%; animation: ff-spin 0.8s linear infinite"></span>
正在与站内字体库比对,请稍候…
</div>
</div>
<div v-if="errorMsg" style="margin-top: 12px; padding: 10px 14px; border: 1px solid #fca5a5; background: #fef2f2; border-radius: 8px; color: #dc2626; font-size: 13px">{{ errorMsg }}</div>
<!-- ─── 第 4 步:结果 ─── -->
<div v-if="results.length" style="margin-top: 28px">
<h2 style="font-size: 17px; margin: 0 0 12px">识别结果 <span style="font-weight: 400; color: #999; font-size: 13px">共比对 {{ results.length }} 个字体 · 耗时 {{ elapsedMs }}ms · 左图=截图 右图=该字体渲染</span></h2>
<div
v-for="r in results"
:key="r.font"
style="border: 1px solid #eee; border-radius: 10px; padding: 14px 16px; margin-bottom: 10px; display: flex; gap: 14px; align-items: center; flex-wrap: wrap"
>
<div style="min-width: 200px; flex: 1">
<div style="font-size: 15px; font-weight: 600">{{ r.font }}</div>
<div v-if="r.missing" style="font-size: 12px; color: #d97706">该字体缺少部分目标字符</div>
<div style="margin-top: 6px; height: 8px; background: #f0f0f0; border-radius: 4px; overflow: hidden">
<div :style="{ width: pct(r.score), height: '100%', background: scoreColor(r.score) }" />
</div>
</div>
<div :style="{ fontSize: '20px', fontWeight: 700, color: scoreColor(r.score) }">{{ pct(r.score) }}</div>
<div style="display: flex; gap: 6px; flex-wrap: wrap">
<div v-for="g in r.glyphs" :key="g.queryIndex" style="text-align: center">
<div style="display: flex; gap: 2px">
<img :src="queryPreview(g.queryIndex)" style="width: 34px; height: 34px; image-rendering: pixelated; border: 1px solid #eee; border-radius: 4px" title="截图" />
<img :src="serverGlyphPreview(r.font, g.char, g.bitmap)" style="width: 34px; height: 34px; image-rendering: pixelated; border: 1px solid #eee; border-radius: 4px" title="该字体渲染" />
</div>
<div style="font-size: 10px; color: #999; margin-top: 2px">{{ g.char }} {{ pct(g.score) }}</div>
</div>
</div>
</div>
</div>
</div>
</template>
<style>
/** 识别中加载图标旋转动画(scoped 会隔离 keyframes,用全局 style) */
@keyframes ff-spin {
to {
transform: rotate(360deg);
}
}
</style>

View File

@ -195,6 +195,9 @@ async function refreshFonts() {
<router-link to="/offline-subset" style="font-size: 13px; color: #16a34a; text-decoration: none; border: 1px solid #16a34a; border-radius: 6px; padding: 4px 12px; display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; flex-shrink: 0">
离线裁剪
</router-link>
<router-link to="/font-finder" style="font-size: 13px; color: #7c3aed; text-decoration: none; border: 1px solid #7c3aed; border-radius: 6px; padding: 4px 12px; display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; flex-shrink: 0">
截图找字体
</router-link>
<router-link
v-if="isDev"
to="/__dev"

View File

@ -27,6 +27,16 @@ const sitemapRoutes = [
];
export default defineConfig({
/**
* 跳过启动时的依赖预构建扫描(noDiscovery)
*
* Vite 8 的 rolldown 扫描器解析 src/components/CodeBlock.vue 的虚拟模块会
* 报 PARSE_ERROR(组件内几百行手写分词器脚本),且扫描失败只会打印错误并
* 跳过预构建 —— 服务照常运行(实测所有页面 200)。这里主动关掉扫描:
* 行为与「扫描失败后跳过」完全一致,但启动日志干净无报错。
* 代价是依赖不做预打包、走运行时按需转换,dev 冷启动首屏略慢。
*/
optimizeDeps: { noDiscovery: true },
plugins: [
vue(),
pilot({ locale: "zh" }),