From 8306a03efe243e7cbbfda3132cf024441ec18ff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=AE=E7=94=9F=EF=BC=88=E5=AD=90=E8=99=9A=EF=BC=89?= <2234839456@qq.com> Date: Sun, 1 Nov 2026 18:32:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=88=AA=E5=9B=BE?= =?UTF-8?q?=E6=89=BE=E5=AD=97=E4=BD=93=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app.ts | 4 + backend/font_util/glyph_bitmap.ts | 377 +++++++++++++++++++++++ backend/routes/glyph_match.ts | 139 +++++++++ scripts/test_glyph_match.ts | 106 +++++++ src/font-finder/matcher.ts | 187 ++++++++++++ src/font-finder/segmenter.ts | 482 ++++++++++++++++++++++++++++++ src/font-finder/ssim.ts | 92 ++++++ src/main.ts | 4 + src/pages/FontFinder.vue | 421 ++++++++++++++++++++++++++ src/pages/Home.vue | 3 + vite.config.ts | 10 + 11 files changed, 1825 insertions(+) create mode 100644 backend/font_util/glyph_bitmap.ts create mode 100644 backend/routes/glyph_match.ts create mode 100644 scripts/test_glyph_match.ts create mode 100644 src/font-finder/matcher.ts create mode 100644 src/font-finder/segmenter.ts create mode 100644 src/font-finder/ssim.ts create mode 100644 src/pages/FontFinder.vue diff --git a/backend/app.ts b/backend/app.ts index d532b7d..ca47ffd 100644 --- a/backend/app.ts +++ b/backend/app.ts @@ -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); }; diff --git a/backend/font_util/glyph_bitmap.ts b/backend/font_util/glyph_bitmap.ts new file mode 100644 index 0000000..96c2a3b --- /dev/null +++ b/backend/font_util/glyph_bitmap.ts @@ -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["get"]>>(); +/** 字形位图缓存:字体名 → 码点 → 64x64 位图(01 串);值为 null 表示该字体缺此字 */ +const glyphBitmapCache = new Map>(); + +/** + * 按魔数探测字体类型(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, +): Promise["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; + +/** 读取一个字形的轮廓,兼容优化后的紧凑坐标与复合字形 */ +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["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); +} diff --git a/backend/routes/glyph_match.ts b/backend/routes/glyph_match.ts new file mode 100644 index 0000000..bea90ff --- /dev/null +++ b/backend/routes/glyph_match.ts @@ -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 { + 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 }) }; +} diff --git a/scripts/test_glyph_match.ts b/scripts/test_glyph_match.ts new file mode 100644 index 0000000..1befeef --- /dev/null +++ b/scripts/test_glyph_match.ts @@ -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); +}); diff --git a/src/font-finder/matcher.ts b/src/font-finder/matcher.ts new file mode 100644 index 0000000..ff9b30a --- /dev/null +++ b/src/font-finder/matcher.ts @@ -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 { + 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 { + 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 }; +} diff --git a/src/font-finder/segmenter.ts b/src/font-finder/segmenter.ts new file mode 100644 index 0000000..187f5ef --- /dev/null +++ b/src/font-finder/segmenter.ts @@ -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(); +} diff --git a/src/font-finder/ssim.ts b/src/font-finder/ssim.ts new file mode 100644 index 0000000..7e771ed --- /dev/null +++ b/src/font-finder/ssim.ts @@ -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; +} diff --git a/src/main.ts b/src/main.ts index 089ef8f..eae9b85 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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") }, diff --git a/src/pages/FontFinder.vue b/src/pages/FontFinder.vue new file mode 100644 index 0000000..2ab35af --- /dev/null +++ b/src/pages/FontFinder.vue @@ -0,0 +1,421 @@ + + + + + diff --git a/src/pages/Home.vue b/src/pages/Home.vue index 9909ebe..0479123 100644 --- a/src/pages/Home.vue +++ b/src/pages/Home.vue @@ -195,6 +195,9 @@ async function refreshFonts() { 离线裁剪 + + 截图找字体 +