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

@ -157,6 +157,64 @@ describe('선택 → 텍스트', () => {
expect(selectionText(result, allWords(result), { singleLine: true })).toBe('日本語 OCR テスト次の行')
})
it('selectionText_주소와기호가여러단어로인식된경우_좁은픽셀간격은공백없이잇는다', () => {
const result = parseOcrEngineOutput({
lang: 'ko',
lines: [
{
words: [
{ t: '"https', x: 10, y: 10, w: 63, h: 18.5 },
{ t: '://api.example.com/v1/chat', x: 78, y: 10, w: 281.5, h: 18.5 },
{ t: '?model=gpt-4&lang=ko', x: 363, y: 10, w: 216.5, h: 18.5 }
]
},
{
words: [
{ t: 'user.name+tag@example.', x: 10, y: 50, w: 237.5, h: 18.5 },
{ t: 'co.', x: 252, y: 58, w: 28.5, h: 10.5 },
{ t: 'kr', x: 285.5, y: 54, w: 20, h: 14.5 }
]
}
]
})
if (!result) throw new Error('no result')
expect(selectionText(result, allWords(result))).toBe(
'"https://api.example.com/v1/chat?model=gpt-4&lang=ko\nuser.name+tag@example.co.kr'
)
})
it('selectionText_일반단어사이의넓은픽셀간격_공백을유지한다', () => {
const result = parseOcrEngineOutput({
lang: 'ko',
lines: [
{
words: [
{ t: 'const', x: 10, y: 10, w: 53.5, h: 13.5 },
{ t: 'url', x: 76, y: 9, w: 31.5, h: 14.5 }
]
}
]
})
if (!result) throw new Error('no result')
expect(selectionText(result, allWords(result))).toBe('const url')
})
it('selectionText_자간이좁은일반문장_실제단어공백은유지한다', () => {
const result = parseOcrEngineOutput({
lang: 'en-US',
lines: [
{
words: [
{ t: 'hello', x: 10, y: 10, w: 25, h: 16 },
{ t: 'world', x: 39, y: 10, w: 30, h: 16 }
]
}
]
})
if (!result) throw new Error('no result')
expect(selectionText(result, allWords(result))).toBe('hello world')
})
it('selectionText_아무것도안고름_빈문자열', () => {
expect(selectionText(sample(), [])).toBe('')
})

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)