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)

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "Bereich",
"screenOcr.copy": "Kopieren",
"screenOcr.copyLine": "Als eine Zeile kopieren",
"screenOcr.precision": "Präzise lesen",
"screenOcr.workingPrecision": "Erneutes Lesen mit lokalem GLM-OCR… Beim ersten Lauf werden etwa 2,7 GB Modelldaten heruntergeladen.",
"screenOcr.resultPrecision": "Ergebnis der präzisen Erkennung",
"screenOcr.errorPrecision": "Präzises Lesen fehlgeschlagen. Der Originaltext bleibt unverändert.",
"screenOcr.copyImage": "Bild kopieren",
"screenOcr.saveImage": "Bild speichern",
"screenOcr.savePdf": "PDF speichern",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "Area",
"screenOcr.copy": "Copy",
"screenOcr.copyLine": "Copy as one line",
"screenOcr.precision": "Precise read",
"screenOcr.workingPrecision": "Reading again with local GLM-OCR… The first run downloads about 2.7 GB of model data.",
"screenOcr.resultPrecision": "Precise recognition result",
"screenOcr.errorPrecision": "Precise reading failed. The original text is unchanged.",
"screenOcr.copyImage": "Copy image",
"screenOcr.saveImage": "Save image",
"screenOcr.savePdf": "Save PDF",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "Área",
"screenOcr.copy": "Copiar",
"screenOcr.copyLine": "Copiar en una línea",
"screenOcr.precision": "Lectura precisa",
"screenOcr.workingPrecision": "Leyendo de nuevo con GLM-OCR local… La primera ejecución descarga unos 2,7 GB del modelo.",
"screenOcr.resultPrecision": "Resultado del reconocimiento preciso",
"screenOcr.errorPrecision": "La lectura precisa falló. El texto original no se modificó.",
"screenOcr.copyImage": "Copiar imagen",
"screenOcr.saveImage": "Guardar imagen",
"screenOcr.savePdf": "Guardar PDF",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "Zone",
"screenOcr.copy": "Copier",
"screenOcr.copyLine": "Copier sur une ligne",
"screenOcr.precision": "Lecture précise",
"screenOcr.workingPrecision": "Nouvelle lecture avec GLM-OCR local… Le premier lancement télécharge environ 2,7 Go de modèle.",
"screenOcr.resultPrecision": "Résultat de la reconnaissance précise",
"screenOcr.errorPrecision": "Échec de la lecture précise. Le texte d’origine est inchangé.",
"screenOcr.copyImage": "Copier l’image",
"screenOcr.saveImage": "Enregistrer l’image",
"screenOcr.savePdf": "Enregistrer en PDF",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "範囲選択",
"screenOcr.copy": "コピー",
"screenOcr.copyLine": "1 行でコピー",
"screenOcr.precision": "高精度で読み取り",
"screenOcr.workingPrecision": "ローカルGLM-OCRで再読み取り中… 初回は約2.7GBのモデルをダウンロードします。",
"screenOcr.resultPrecision": "高精度な認識結果",
"screenOcr.errorPrecision": "高精度な読み取りに失敗しました。元のテキストは変更されていません。",
"screenOcr.copyImage": "画像をコピー",
"screenOcr.saveImage": "画像を保存",
"screenOcr.savePdf": "PDF を保存",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "영역 선택",
"screenOcr.copy": "복사",
"screenOcr.copyLine": "한 줄로 복사",
"screenOcr.precision": "정밀 읽기",
"screenOcr.workingPrecision": "로컬 GLM-OCR로 다시 읽는 중… 첫 실행에는 약 2.7GB 모델을 날받습니다.",
"screenOcr.resultPrecision": "정밀 인식 결과",
"screenOcr.errorPrecision": "정밀 읽기에 실패했습니다. 원문은 그대로 유지됩니다.",
"screenOcr.copyImage": "이미지 복사",
"screenOcr.saveImage": "이미지 저장",
"screenOcr.savePdf": "PDF 저장",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "Área",
"screenOcr.copy": "Copiar",
"screenOcr.copyLine": "Copiar em uma linha",
"screenOcr.precision": "Leitura precisa",
"screenOcr.workingPrecision": "Lendo novamente com GLM-OCR local… A primeira execução baixa cerca de 2,7 GB do modelo.",
"screenOcr.resultPrecision": "Resultado do reconhecimento preciso",
"screenOcr.errorPrecision": "Falha na leitura precisa. O texto original permanece inalterado.",
"screenOcr.copyImage": "Copiar imagem",
"screenOcr.saveImage": "Salvar imagem",
"screenOcr.savePdf": "Salvar PDF",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "Область",
"screenOcr.copy": "Копировать",
"screenOcr.copyLine": "Копировать одной строкой",
"screenOcr.precision": "Точное чтение",
"screenOcr.workingPrecision": "Повторное чтение с помощью локальной GLM-OCR… При первом запуске будет загружено около 2,7 ГБ данных модели.",
"screenOcr.resultPrecision": "Результат точного распознавания",
"screenOcr.errorPrecision": "Не удалось выполнить точное чтение. Исходный текст не изменился.",
"screenOcr.copyImage": "Копировать изображение",
"screenOcr.saveImage": "Сохранить изображение",
"screenOcr.savePdf": "Сохранить PDF",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "เลือกพื้นที่",
"screenOcr.copy": "คัดลอก",
"screenOcr.copyLine": "คัดลอกเป็นบรรทัดเดียว",
"screenOcr.precision": "อ่านอย่างละเอียด",
"screenOcr.workingPrecision": "กำลังอ่านซ้ำด้วย GLM-OCR ในเครื่อง… การรันครั้งแรกจะดาวน์โหลดโมเดลประมาณ 2.7 GB",
"screenOcr.resultPrecision": "ผลการจดจำอย่างละเอียด",
"screenOcr.errorPrecision": "การอ่านอย่างละเอียดล้มเหลว ข้อความต้นฉบับไม่เปลี่ยนแปลง",
"screenOcr.copyImage": "คัดลอกรูปภาพ",
"screenOcr.saveImage": "บันทึกรูปภาพ",
"screenOcr.savePdf": "บันทึก PDF",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "Chọn vùng",
"screenOcr.copy": "Sao chép",
"screenOcr.copyLine": "Sao chép thành một dòng",
"screenOcr.precision": "Đọc chính xác",
"screenOcr.workingPrecision": "Đang đọc lại bằng GLM-OCR cục bộ… Lần chạy đầu sẽ tải khoảng 2,7 GB mô hình.",
"screenOcr.resultPrecision": "Kết quả nhận dạng chính xác",
"screenOcr.errorPrecision": "Đọc chính xác thất bại. Văn bản gốc không thay đổi.",
"screenOcr.copyImage": "Sao chép ảnh",
"screenOcr.saveImage": "Lưu ảnh",
"screenOcr.savePdf": "Lưu PDF",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "選取區域",
"screenOcr.copy": "複製",
"screenOcr.copyLine": "複製為一行",
"screenOcr.precision": "精確辨識",
"screenOcr.workingPrecision": "正在使用本機 GLM-OCR 重新辨識… 首次執行將下載約 2.7 GB 的模型。",
"screenOcr.resultPrecision": "精確辨識結果",
"screenOcr.errorPrecision": "精確辨識失敗。原始文字保持不變。",
"screenOcr.copyImage": "複製圖片",
"screenOcr.saveImage": "儲存圖片",
"screenOcr.savePdf": "儲存 PDF",

View file

@ -2510,6 +2510,10 @@
"screenOcr.modeRect": "选择区域",
"screenOcr.copy": "复制",
"screenOcr.copyLine": "复制为一行",
"screenOcr.precision": "精确识别",
"screenOcr.workingPrecision": "正在使用本地 GLM-OCR 重新识别… 首次运行将下载约 2.7 GB 的模型。",
"screenOcr.resultPrecision": "精确识别结果",
"screenOcr.errorPrecision": "精确识别失败。原始文本保持不变。",
"screenOcr.copyImage": "复制图片",
"screenOcr.saveImage": "保存图片",
"screenOcr.savePdf": "保存 PDF",