fix(desktop): add precise local OCR reading

This commit is contained in:
Yun Chan 2026-11-21 10:15:49 +09:00
parent 59cdddbecb
commit 25fd3ac004
30 changed files with 626 additions and 15 deletions

View file

@ -4,7 +4,7 @@
// 1) OCR 결과 표현 — OcrResult / OcrLine / OcrWord (좌표는 캡처 이미지의 **물리 픽셀**)
// 2) 엔진 출력 해석 — parseOcrEngineOutput (엔진이 준 JSON 을 검증해 정본 표현으로)
// 3) 선택 계산 — 사각형 선택(wordsInRect) · 글 흐름 선택(flowSelection) · 단어/줄 선택
// 4) 선택 → 텍스트 — selectionText (줄바꿈 유지 / 한 줄, CJK 는 공백 없이 잇기)
// 4) 선택 → 텍스트 — selectionText (줄바꿈 유지 / 한 줄, CJK · 주소/기호 토큰 공백 복원)
// 5) 강조 사각형 — selectionHighlightRects (줄 단위로 이어 붙인 사각형)
// 6) 검색 가능한 PDF 문서 — buildSearchablePdfHtml (그림 아래에 실제 글자를 깐다)
// 7) 부가 판정 — chooseOcrScale · detectLinks · pickTranslationTarget
@ -254,9 +254,14 @@ function firstChar(text: string): string {
return Array.from(text)[0] ?? ''
}
/** 한 줄 안에서 이웃한 두 단어 사이에 공백을 넣을지. 일본어 · 중국어는 엔진이 글자마다 단어로 준다. */
function joiner(previous: string, next: string): string {
return isUnspacedScript(lastChar(previous)) && isUnspacedScript(firstChar(next)) ? '' : ' '
/** 일본어 · 중국어와 좁은 간격의 주소 · 기호 토큰은 붙이되 일반 단어의 띄어쓰기는 유지한다. */
function joiner(previous: string, next: string, gapRatio: number | null = null): string {
const last = lastChar(previous)
const first = firstChar(next)
if (isUnspacedScript(last) && isUnspacedScript(first)) return ''
if (gapRatio === null || gapRatio > 0.6) return ' '
if (gapRatio <= 0.05) return ''
return /[./:\\]/u.test(last) || /^[:/?#@.&=+_~$-]/u.test(first) ? '' : ' '
}
export interface SelectionTextOptions {
@ -279,15 +284,18 @@ export function selectionText(
const lines: string[] = []
let currentLine = -1
let buffer = ''
let previousWord: OcrWord | null = null
for (const index of picked) {
const word = result.words[index]
if (word.line !== currentLine) {
if (currentLine !== -1) lines.push(buffer)
currentLine = word.line
buffer = word.text
} else {
buffer += joiner(buffer, word.text) + word.text
} else if (previousWord) {
const gapRatio = (word.x - previousWord.x - previousWord.width) / Math.max(previousWord.height, word.height)
buffer += joiner(buffer, word.text, gapRatio) + word.text
}
previousWord = word
}
if (currentLine !== -1) lines.push(buffer)