Skip to content

Commit dfd9143

Browse files
committed
Pass OCR language locale to manga text box for proper CJK rendering
1 parent f8c195a commit dfd9143

5 files changed

Lines changed: 26 additions & 4 deletions

File tree

app/src/main/java/eu/kanade/tachiyomi/data/ocr/OcrCacheManager.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ class OcrCacheManager(
479479
lineGeometries = lineGeometries?.map { lg ->
480480
chimahon.ocr.OcrLineGeometry(lg.xmin, lg.ymin, lg.xmax, lg.ymax, lg.rotation)
481481
},
482+
language = language,
482483
)
483484

484485
private fun OcrBlockData.toTextBlock() = OcrTextBlock(
@@ -491,6 +492,7 @@ class OcrCacheManager(
491492
lineGeometries = lineGeometries?.map { lg ->
492493
chimahon.ocr.OcrLineGeometry(lg.xmin, lg.ymin, lg.xmax, lg.ymax, lg.rotation)
493494
},
495+
language = language,
494496
)
495497

496498
// Legacy methods for backward compatibility during migration

app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,8 @@ class ReaderViewModel @JvmOverloads constructor(
17791779
if (ocrCache.containsKey(cacheKey)) return@withLock
17801780
}
17811781

1782-
getMokuroBlocksForPage(chapterData, pageIndex)?.let { blocks ->
1782+
getMokuroBlocksForPage(chapterData, pageIndex)?.let { rawBlocks ->
1783+
val blocks = rawBlocks.map { it.copy(language = chimahon.ocr.OcrLanguage.JAPANESE.bcp47) }
17831784
ocrCacheMutex.withLock {
17841785
ocrCache[cacheKey] = blocks
17851786
while (ocrCache.size > maxOcrCacheEntries) {
@@ -1803,6 +1804,7 @@ class ReaderViewModel @JvmOverloads constructor(
18031804
lineGeometries = it.lineGeometries?.map { lg ->
18041805
chimahon.ocr.OcrLineGeometry(lg.xmin, lg.ymin, lg.xmax, lg.ymax, lg.rotation)
18051806
},
1807+
language = it.language,
18061808
)
18071809
},
18081810
language = chimahon.ocr.OcrLanguage.JAPANESE.bcp47,
@@ -1995,7 +1997,8 @@ class ReaderViewModel @JvmOverloads constructor(
19951997
}
19961998

19971999
if (source.isLocal()) {
1998-
tryLoadMokuroBlocks(manga, domainChapter, source, page.index)?.let { blocks ->
2000+
tryLoadMokuroBlocks(manga, domainChapter, source, page.index)?.let { rawBlocks ->
2001+
val blocks = rawBlocks.map { it.copy(language = ocrLang.bcp47) }
19992002
ocrCacheMutex.withLock {
20002003
ocrCache[cacheKey] = blocks
20012004
while (ocrCache.size > maxOcrCacheEntries) {
@@ -2019,6 +2022,7 @@ class ReaderViewModel @JvmOverloads constructor(
20192022
lineGeometries = it.lineGeometries?.map { lg ->
20202023
chimahon.ocr.OcrLineGeometry(lg.xmin, lg.ymin, lg.xmax, lg.ymax, lg.rotation)
20212024
},
2025+
language = it.language,
20222026
)
20232027
},
20242028
language = ocrLang.bcp47,
@@ -2041,7 +2045,9 @@ class ReaderViewModel @JvmOverloads constructor(
20412045

20422046
val mokuroUrl = buildMokuroExtensionUrl(manga, domainChapter, source)
20432047
if (mokuroUrl != null) {
2044-
tryLoadMokuroFromUrl(mokuroUrl, manga, domainChapter, source, page.index, page.chapter.pages?.size ?: 0)?.let { blocks ->
2048+
tryLoadMokuroFromUrl(mokuroUrl, manga, domainChapter, source, page.index, page.chapter.pages?.size ?: 0)?.let { rawBlocks ->
2049+
val mokuroLang = chimahon.ocr.OcrLanguage.JAPANESE.bcp47
2050+
val blocks = rawBlocks.map { it.copy(language = mokuroLang) }
20452051
ocrCacheMutex.withLock {
20462052
ocrCache[cacheKey] = blocks
20472053
while (ocrCache.size > maxOcrCacheEntries) {
@@ -2065,9 +2071,10 @@ class ReaderViewModel @JvmOverloads constructor(
20652071
lineGeometries = it.lineGeometries?.map { lg ->
20662072
chimahon.ocr.OcrLineGeometry(lg.xmin, lg.ymin, lg.xmax, lg.ymax, lg.rotation)
20672073
},
2074+
language = it.language,
20682075
)
20692076
},
2070-
language = chimahon.ocr.OcrLanguage.JAPANESE.bcp47,
2077+
language = mokuroLang,
20712078
)
20722079
val elapsedMs = SystemClock.elapsedRealtime() - startMs
20732080
logcat { "OCR mokuro extension fetch: chapter=$chapterId page=${page.index} blocks=${blocks.size} time=${elapsedMs}ms" }
@@ -2131,6 +2138,7 @@ class ReaderViewModel @JvmOverloads constructor(
21312138
lines = result.text.split("\n").filter { it.isNotBlank() },
21322139
vertical = result.forcedOrientation == "vertical",
21332140
lineGeometries = lineGeometries,
2141+
language = ocrLang.bcp47,
21342142
)
21352143
}
21362144
}
@@ -2151,6 +2159,7 @@ class ReaderViewModel @JvmOverloads constructor(
21512159
lineGeometries = it.lineGeometries?.map { lg ->
21522160
chimahon.ocr.OcrLineGeometry(lg.xmin, lg.ymin, lg.xmax, lg.ymax, lg.rotation)
21532161
},
2162+
language = it.language,
21542163
)
21552164
},
21562165
language = ocrLang.bcp47,
@@ -2299,5 +2308,6 @@ private fun chimahon.ocr.OcrTextBlock.toViewerBlock(): eu.kanade.tachiyomi.ui.re
22992308
lineGeometries = lineGeometries?.map { lg ->
23002309
eu.kanade.tachiyomi.ui.reader.viewer.OcrLineGeometry(lg.xmin, lg.ymin, lg.xmax, lg.ymax, lg.rotation)
23012310
},
2311+
language = language,
23022312
)
23032313
}

app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/OcrSubsamplingImageView.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.view.GestureDetector
1313
import android.view.MotionEvent
1414
import android.view.View
1515
import android.view.ViewConfiguration
16+
import java.util.Locale
1617
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
1718
import eu.kanade.tachiyomi.ui.reader.viewer.pager.Pager
1819
import kotlin.math.abs
@@ -100,6 +101,12 @@ class OcrSubsamplingImageView(
100101
// Guard 3: no blocks to draw
101102
if (host.ocrBlocks.isEmpty()) return
102103

104+
// Apply locale from first block's language for proper CJK glyph rendering
105+
val blockLang = host.ocrBlocks.firstOrNull()?.language
106+
if (!blockLang.isNullOrEmpty()) {
107+
textPaint.setTextLocale(Locale.forLanguageTag(blockLang))
108+
}
109+
103110
// Draw all blocks: borders always visible, text only if active
104111
for (block in host.ocrBlocks) {
105112
drawOcrBlock(canvas, block, host)

app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/OcrTextBlock.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ data class OcrTextBlock(
2222
val lines: List<String>,
2323
val vertical: Boolean = false,
2424
val lineGeometries: List<OcrLineGeometry>? = null,
25+
val language: String = "",
2526
)
2627

2728
/**

chimahon/src/main/java/chimahon/ocr/OcrBlockData.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ data class OcrBlockData(
2020
val lines: List<String>,
2121
val vertical: Boolean,
2222
val lineGeometries: List<OcrLineGeometry>? = null,
23+
val language: String = "",
2324
)
2425

2526
@Serializable
@@ -38,4 +39,5 @@ data class OcrTextBlock(
3839
val lines: List<String>,
3940
val vertical: Boolean = false,
4041
val lineGeometries: List<OcrLineGeometry>? = null,
42+
val language: String = "",
4143
)

0 commit comments

Comments
 (0)