Compare commits

..

12 Commits

Author SHA1 Message Date
BookOasis Reader 17921cbc4c Skip non-speech symbols in reader TTS
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-10-04 07:57:57 +09:00
BookOasis Reader a7edba607a Add character-based text wrapping
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-09-29 12:36:37 +09:00
BookOasis Reader c622f74c62 Preview text reader margins while dragging
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-09-29 10:06:10 +09:00
BookOasis Reader 95b1199f1d Add adjustable text reader side margins
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-09-29 08:16:09 +09:00
BookOasis Reader eca7a3b886 Move floating TTS controls above reader menu
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-09-27 17:03:29 +09:00
BookOasis Reader ba1b4d2362 Add floating TTS stop control
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-09-27 14:23:06 +09:00
BookOasis Reader 023c3bb2f1 Resume paused TTS by source position
Build & Test / Build & Test App (push) Has been cancelled
2026-09-27 14:14:59 +09:00
BookOasis Reader aec03c6c71 Keep manual reader position while TTS is paused
Build & Test / Build & Test App (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-09-27 09:00:49 +09:00
BookOasis Reader c9c119689d Show floating TTS control only in text reader
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-09-27 01:11:17 +09:00
BookOasis Reader 38a327cdf6 Improve TTS highlight contrast
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-09-27 00:55:01 +09:00
BookOasis Reader b25679c8de Resume TTS from current reader position
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-09-27 00:36:30 +09:00
BookOasis Reader dfd57544a9 Improve TTS highlighting and download detection
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2026-09-26 21:56:54 +09:00
14 changed files with 775 additions and 77 deletions
@@ -33,6 +33,11 @@ private val fontFamilies = listOf(
MR.strings.pref_text_reader_font_mono to ReaderPreferences.TextFontFamily.MONOSPACE, MR.strings.pref_text_reader_font_mono to ReaderPreferences.TextFontFamily.MONOSPACE,
) )
private val lineBreakModes = listOf(
MR.strings.pref_text_reader_line_break_word to ReaderPreferences.TextLineBreakMode.WORD,
MR.strings.pref_text_reader_line_break_character to ReaderPreferences.TextLineBreakMode.CHARACTER,
)
@Composable @Composable
internal fun ColumnScope.TextSettingsPage(viewModel: ReaderSettingsViewModel) { internal fun ColumnScope.TextSettingsPage(viewModel: ReaderSettingsViewModel) {
val context = LocalContext.current val context = LocalContext.current
@@ -90,6 +95,36 @@ internal fun ColumnScope.TextSettingsPage(viewModel: ReaderSettingsViewModel) {
pillColor = MaterialTheme.colorScheme.surfaceContainerHighest, pillColor = MaterialTheme.colorScheme.surfaceContainerHighest,
) )
val horizontalMarginPref = viewModel.preferences.textHorizontalMargin
val horizontalMargin by horizontalMarginPref.collectAsState()
SliderItem(
label = stringResource(MR.strings.pref_text_reader_horizontal_margin),
value = horizontalMargin,
valueRange = 0..64,
steps = 15,
valueString = "$horizontalMargin dp",
onChange = { value ->
horizontalMarginPref.set(value)
viewModel.previewAndRequestTextSettingsReload()
},
pillColor = MaterialTheme.colorScheme.surfaceContainerHighest,
)
val lineBreakModePref = viewModel.preferences.textLineBreakMode
val lineBreakMode by lineBreakModePref.collectAsState()
SettingsChipRow(MR.strings.pref_text_reader_line_break_mode) {
lineBreakModes.forEach { (label, value) ->
FilterChip(
selected = lineBreakMode == value,
onClick = {
lineBreakModePref.set(value)
viewModel.previewAndRequestTextSettingsReload()
},
label = { Text(stringResource(label)) },
)
}
}
val fontFamilyPref = viewModel.preferences.textFontFamily val fontFamilyPref = viewModel.preferences.textFontFamily
val fontFamily by fontFamilyPref.collectAsState() val fontFamily by fontFamilyPref.collectAsState()
SettingsChipRow(MR.strings.pref_text_reader_font_family) { SettingsChipRow(MR.strings.pref_text_reader_font_family) {
@@ -95,7 +95,7 @@ class DownloadCache(
.stateIn(scope, SharingStarted.WhileSubscribed(), false) .stateIn(scope, SharingStarted.WhileSubscribed(), false)
private val diskCacheFile: File private val diskCacheFile: File
get() = File(context.cacheDir, "dl_index_cache_v3") get() = File(context.cacheDir, "dl_index_cache_v4")
private val rootDownloadsDirMutex = Mutex() private val rootDownloadsDirMutex = Mutex()
private var rootDownloadsDir = RootDirectory(storageManager.getDownloadsDirectory()) private var rootDownloadsDir = RootDirectory(storageManager.getDownloadsDirectory())
@@ -214,7 +214,8 @@ class DownloadManager(
mangaTitle: String, mangaTitle: String,
source: Source, source: Source,
): Boolean { ): Boolean {
return provider.findChapterDir(chapterName, chapterScanlator, chapterUrl, mangaTitle, source) != null return provider.findChapterDir(chapterName, chapterScanlator, chapterUrl, mangaTitle, source) != null ||
provider.findChapterDocument(chapterName, chapterScanlator, chapterUrl, mangaTitle, source) != null
} }
/** /**
@@ -253,8 +254,8 @@ class DownloadManager(
removeFromDownloadQueue(filteredChapters) removeFromDownloadQueue(filteredChapters)
val (mangaDir, chapterDirs) = provider.findChapterDirs(filteredChapters, manga, source) val (mangaDir, chapterDownloads) = provider.findChapterDirs(filteredChapters, manga, source)
chapterDirs.forEach { it.delete() } chapterDownloads.forEach { it.delete() }
cache.removeChapters(filteredChapters, manga) cache.removeChapters(filteredChapters, manga)
// Delete manga directory if empty // Delete manga directory if empty
@@ -149,11 +149,8 @@ class DownloadProvider(
} }
/** /**
* Returns a list of downloaded directories for the chapters that exist. * Returns downloaded chapter entries that exist, including image directories,
* * CBZ files and standalone EPUB/TXT document files.
* @param chapters the chapters to query.
* @param manga the manga of the chapter.
* @param source the source of the chapter.
*/ */
fun findChapterDirs(chapters: List<Chapter>, manga: Manga, source: Source): Pair<UniFile?, List<UniFile>> { fun findChapterDirs(chapters: List<Chapter>, manga: Manga, source: Source): Pair<UniFile?, List<UniFile>> {
val mangaDir = findMangaDir(manga.title, source) ?: return null to emptyList() val mangaDir = findMangaDir(manga.title, source) ?: return null to emptyList()
@@ -161,6 +158,13 @@ class DownloadProvider(
getValidChapterDirNames(chapter.name, chapter.scanlator, chapter.url).asSequence() getValidChapterDirNames(chapter.name, chapter.scanlator, chapter.url).asSequence()
.mapNotNull { mangaDir.findFile(it) } .mapNotNull { mangaDir.findFile(it) }
.firstOrNull() .firstOrNull()
?: findChapterDocument(
chapter.name,
chapter.scanlator,
chapter.url,
manga.title,
source,
)
} }
} }
@@ -20,13 +20,22 @@ import android.view.WindowManager
import android.widget.Toast import android.widget.Toast
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBarsPadding import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.AlertDialog import androidx.compose.material3.AlertDialog
import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
@@ -34,6 +43,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.graphics.Insets import androidx.core.graphics.Insets
@@ -271,6 +281,7 @@ class ReaderActivity : BaseActivity() {
private fun ReaderActivityBinding.setComposeOverlay(): Unit = composeOverlay.setComposeContent { private fun ReaderActivityBinding.setComposeOverlay(): Unit = composeOverlay.setComposeContent {
val state by viewModel.state.collectAsState() val state by viewModel.state.collectAsState()
val showPageNumber by readerPreferences.showPageNumber.collectAsState() val showPageNumber by readerPreferences.showPageNumber.collectAsState()
val ttsState by ReaderTtsController.state.collectAsState()
val settingsviewModel = remember { val settingsviewModel = remember {
ReaderSettingsViewModel( ReaderSettingsViewModel(
readerState = viewModel.state, readerState = viewModel.state,
@@ -296,6 +307,59 @@ class ReaderActivity : BaseActivity() {
ContentOverlay(state = state) ContentOverlay(state = state)
AppBars(state = state) AppBars(state = state)
val showFloatingTts = ttsState.active &&
ttsState.readerChapterId == state.currentChapter?.chapter?.id &&
state.currentChapter?.pages?.any { it is TextReaderPage } == true
if (showFloatingTts) {
val floatingTtsBottomPadding by animateDpAsState(
targetValue = if (state.menuVisible) 144.dp else 72.dp,
animationSpec = tween(durationMillis = 200),
label = "floatingTtsBottomPadding",
)
Row(
modifier = Modifier
.align(Alignment.BottomEnd)
.navigationBarsPadding()
.padding(end = 18.dp, bottom = floatingTtsBottomPadding),
horizontalArrangement = Arrangement.spacedBy(10.dp),
) {
IconButton(
onClick = ::toggleTts,
modifier = Modifier
.size(52.dp)
.background(
color = MaterialTheme.colorScheme.surface.copy(alpha = 0.55f),
shape = CircleShape,
),
) {
Icon(
painter = painterResource(
if (ttsState.playing) R.drawable.ic_pause_24dp else R.drawable.ic_play_arrow_24dp,
),
contentDescription = stringResource(
if (ttsState.playing) MR.strings.reader_tts_pause else MR.strings.reader_tts_start,
),
tint = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.92f),
)
}
IconButton(
onClick = { ReaderTtsController.stop(this@ReaderActivity) },
modifier = Modifier
.size(52.dp)
.background(
color = MaterialTheme.colorScheme.surface.copy(alpha = 0.55f),
shape = CircleShape,
),
) {
Icon(
painter = painterResource(R.drawable.ic_stop_24dp),
contentDescription = stringResource(MR.strings.reader_tts_stop),
tint = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.92f),
)
}
}
}
} }
val onDismissRequest = viewModel::closeDialog val onDismissRequest = viewModel::closeDialog
@@ -701,7 +765,21 @@ class ReaderActivity : BaseActivity() {
val ttsState = ReaderTtsController.state.value val ttsState = ReaderTtsController.state.value
if (ttsState.active && ttsState.readerChapterId == chapterId) { if (ttsState.active && ttsState.readerChapterId == chapterId) {
if (ttsState.playing) {
ReaderTtsController.toggle(this) ReaderTtsController.toggle(this)
} else {
val currentPage = chapter.pages
?.getOrNull((state.currentPage - 1).coerceAtLeast(0)) as? TextReaderPage
if (currentPage != null) {
ReaderTtsController.resumeFromSource(
context = this,
documentIndex = currentPage.anchor.documentIndex,
sourceOffset = currentPage.anchor.startOffset,
)
} else {
ReaderTtsController.toggle(this)
}
}
return return
} }
@@ -751,11 +829,16 @@ class ReaderActivity : BaseActivity() {
val textPages = chapter.pages?.filterIsInstance<TextReaderPage>().orEmpty() val textPages = chapter.pages?.filterIsInstance<TextReaderPage>().orEmpty()
if (textPages.isEmpty()) return if (textPages.isEmpty()) return
val targetPage = if (ttsState.sourceHighlightStart >= 0L) { val targetSourceOffset = when {
ttsState.sourceHighlightStart >= 0L -> ttsState.sourceHighlightStart
ttsState.sourceSentenceStart >= 0L -> ttsState.sourceSentenceStart
else -> -1L
}
val targetPage = if (targetSourceOffset >= 0L) {
textPages.firstOrNull { page -> textPages.firstOrNull { page ->
page.anchor.documentIndex == ttsState.documentIndex && page.anchor.documentIndex == ttsState.documentIndex &&
ttsState.sourceHighlightStart >= page.anchor.startOffset && targetSourceOffset >= page.anchor.startOffset &&
ttsState.sourceHighlightStart < page.anchor.endOffsetExclusive targetSourceOffset < page.anchor.endOffsetExclusive
} }
} else { } else {
chapter.pages?.getOrNull(ttsState.readerPageIndex) as? TextReaderPage chapter.pages?.getOrNull(ttsState.readerPageIndex) as? TextReaderPage
@@ -765,26 +848,43 @@ class ReaderActivity : BaseActivity() {
ttsHighlightedPage?.clearTtsHighlight() ttsHighlightedPage?.clearTtsHighlight()
} }
val sentenceStart = maxOf(ttsState.sourceSentenceStart, targetPage.anchor.startOffset)
val sentenceEnd = minOf(ttsState.sourceSentenceEndExclusive, targetPage.anchor.endOffsetExclusive)
val activeStart = maxOf(ttsState.sourceHighlightStart, targetPage.anchor.startOffset)
val activeEnd = minOf(ttsState.sourceHighlightEndExclusive, targetPage.anchor.endOffsetExclusive)
if ( if (
ttsState.sourceHighlightStart >= 0L && ttsState.sourceSentenceStart >= 0L &&
ttsState.sourceHighlightEndExclusive > ttsState.sourceHighlightStart ttsState.sourceSentenceEndExclusive > ttsState.sourceSentenceStart &&
sentenceEnd > sentenceStart
) { ) {
val start = maxOf(ttsState.sourceHighlightStart, targetPage.anchor.startOffset)
val end = minOf(ttsState.sourceHighlightEndExclusive, targetPage.anchor.endOffsetExclusive)
targetPage.updateTtsHighlight( targetPage.updateTtsHighlight(
start = (start - targetPage.anchor.startOffset).toInt(), sentenceStart = (sentenceStart - targetPage.anchor.startOffset).toInt(),
endExclusive = (end - targetPage.anchor.startOffset).toInt(), sentenceEndExclusive = (sentenceEnd - targetPage.anchor.startOffset).toInt(),
activeStart = if (activeEnd > activeStart) {
(activeStart - targetPage.anchor.startOffset).toInt()
} else {
-1
},
activeEndExclusive = if (activeEnd > activeStart) {
(activeEnd - targetPage.anchor.startOffset).toInt()
} else {
-1
},
) )
} else { } else {
targetPage.clearTtsHighlight() targetPage.clearTtsHighlight()
} }
ttsHighlightedPage = targetPage ttsHighlightedPage = targetPage
if (state.currentPage != targetPage.index + 1) { val targetPageIsVisible = state.currentPage == targetPage.index + 1
if (ttsState.playing && !targetPageIsVisible) {
moveToPageIndex(targetPage.index) moveToPageIndex(targetPage.index)
} }
if (ttsState.playing || targetPageIsVisible) {
state.viewer?.refreshCurrentPage() state.viewer?.refreshCurrentPage()
} }
}
/** /**
* Tells the presenter to load the next chapter and mark it as active. The progress dialog * Tells the presenter to load the next chapter and mark it as active. The progress dialog
@@ -8,15 +8,20 @@ import android.graphics.Typeface
import android.text.Layout import android.text.Layout
import android.text.Spannable import android.text.Spannable
import android.text.SpannableString import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.StaticLayout import android.text.StaticLayout
import android.text.TextPaint import android.text.TextPaint
import android.text.style.BackgroundColorSpan import android.text.style.BackgroundColorSpan
import android.text.style.ForegroundColorSpan
import android.text.style.LineHeightSpan import android.text.style.LineHeightSpan
import eu.kanade.tachiyomi.ui.reader.setting.ReaderPreferences.TextLineBreakMode
import eu.kanade.tachiyomi.ui.reader.setting.TextReaderStyle import eu.kanade.tachiyomi.ui.reader.setting.TextReaderStyle
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.InputStream import java.io.InputStream
import java.io.Reader import java.io.Reader
import java.text.BreakIterator
import java.util.Locale
import kotlin.math.roundToInt import kotlin.math.roundToInt
internal data class PaginatedTextPage( internal data class PaginatedTextPage(
@@ -26,9 +31,10 @@ internal data class PaginatedTextPage(
) )
internal object TextPageRenderer { internal object TextPageRenderer {
private const val MARGIN_DP = 32f private const val VERTICAL_MARGIN_DP = 32f
private const val STREAM_BUFFER_CHARS = 64 * 1024 private const val STREAM_BUFFER_CHARS = 64 * 1024
private const val STREAM_READ_CHARS = 8 * 1024 private const val STREAM_READ_CHARS = 8 * 1024
private const val ZERO_WIDTH_SPACE = "\u200B"
fun paginate(text: String, style: TextReaderStyle): List<PaginatedTextPage> { fun paginate(text: String, style: TextReaderStyle): List<PaginatedTextPage> {
if (text.isBlank()) return emptyList() if (text.isBlank()) return emptyList()
@@ -132,7 +138,8 @@ internal object TextPageRenderer {
if (text.isEmpty()) return emptyList() if (text.isEmpty()) return emptyList()
val metrics = resolveLayoutMetrics(style) val metrics = resolveLayoutMetrics(style)
val layout = createLayout(text, style, metrics.contentWidth) val builtLayout = createLayout(text, style, metrics.contentWidth)
val layout = builtLayout.layout
if (layout.lineCount == 0) return listOf(PageSlice(0, text.length)) if (layout.lineCount == 0) return listOf(PageSlice(0, text.length))
val pages = mutableListOf<PageSlice>() val pages = mutableListOf<PageSlice>()
@@ -147,8 +154,10 @@ internal object TextPageRenderer {
endLine++ endLine++
} }
val start = layout.getLineStart(startLine) val start = builtLayout.originalOffset(layout.getLineStart(startLine))
val end = layout.getLineEnd(endLine).coerceAtLeast(start + 1).coerceAtMost(text.length) val end = builtLayout.originalOffset(layout.getLineEnd(endLine))
.coerceAtLeast(start + 1)
.coerceAtMost(text.length)
pages += PageSlice(start, end) pages += PageSlice(start, end)
startLine = endLine + 1 startLine = endLine + 1
} }
@@ -158,7 +167,8 @@ internal object TextPageRenderer {
fun render( fun render(
text: String, text: String,
style: TextReaderStyle, style: TextReaderStyle,
highlightRange: IntRange? = null, sentenceHighlightRange: IntRange? = null,
activeHighlightRange: IntRange? = null,
): InputStream { ): InputStream {
val metrics = resolveLayoutMetrics(style) val metrics = resolveLayoutMetrics(style)
val bitmap = Bitmap.createBitmap(metrics.pageWidth, metrics.pageHeight, Bitmap.Config.ARGB_8888) val bitmap = Bitmap.createBitmap(metrics.pageWidth, metrics.pageHeight, Bitmap.Config.ARGB_8888)
@@ -167,7 +177,13 @@ internal object TextPageRenderer {
canvas.drawColor(backgroundColor) canvas.drawColor(backgroundColor)
canvas.save() canvas.save()
canvas.translate(metrics.horizontalMargin.toFloat(), metrics.verticalMargin.toFloat()) canvas.translate(metrics.horizontalMargin.toFloat(), metrics.verticalMargin.toFloat())
createLayout(text, style, metrics.contentWidth, highlightRange).draw(canvas) createLayout(
text = text,
style = style,
contentWidth = metrics.contentWidth,
sentenceHighlightRange = sentenceHighlightRange,
activeHighlightRange = activeHighlightRange,
).layout.draw(canvas)
canvas.restore() canvas.restore()
val output = ByteArrayOutputStream() val output = ByteArrayOutputStream()
@@ -180,8 +196,9 @@ internal object TextPageRenderer {
text: String, text: String,
style: TextReaderStyle, style: TextReaderStyle,
contentWidth: Int, contentWidth: Int,
highlightRange: IntRange? = null, sentenceHighlightRange: IntRange? = null,
): StaticLayout { activeHighlightRange: IntRange? = null,
): BuiltLayout {
val (_, textColor) = resolveColors(style) val (_, textColor) = resolveColors(style)
val paint = TextPaint().apply { val paint = TextPaint().apply {
isAntiAlias = true isAntiAlias = true
@@ -191,9 +208,15 @@ internal object TextPageRenderer {
?.let { Typeface.create(it, Typeface.NORMAL) } ?.let { Typeface.create(it, Typeface.NORMAL) }
?: Typeface.DEFAULT ?: Typeface.DEFAULT
} }
val styledText = addTextSpans(text, style, highlightRange) val styledText = addTextSpans(
return StaticLayout.Builder text = text,
.obtain(styledText, 0, styledText.length, paint, contentWidth) style = style,
sentenceHighlightRange = sentenceHighlightRange,
activeHighlightRange = activeHighlightRange,
)
val preparedText = prepareLineBreakText(text, styledText, style.lineBreakMode)
val layout = StaticLayout.Builder
.obtain(preparedText.text, 0, preparedText.text.length, paint, contentWidth)
.setAlignment(Layout.Alignment.ALIGN_NORMAL) .setAlignment(Layout.Alignment.ALIGN_NORMAL)
// Page slices are laid out again when rendered. Keeping font padding // Page slices are laid out again when rendered. Keeping font padding
// here can make the sliced layout taller than the same lines measured // here can make the sliced layout taller than the same lines measured
@@ -201,14 +224,66 @@ internal object TextPageRenderer {
.setIncludePad(false) .setIncludePad(false)
.setLineSpacing(0f, style.lineSpacingPercent / 100f) .setLineSpacing(0f, style.lineSpacingPercent / 100f)
.build() .build()
return BuiltLayout(layout, preparedText.originalOffsetByPreparedOffset)
}
private fun prepareLineBreakText(
originalText: String,
styledText: CharSequence,
mode: TextLineBreakMode,
): PreparedText {
if (mode == TextLineBreakMode.WORD || originalText.length < 2) {
return PreparedText(styledText, null)
}
val breakIterator = BreakIterator.getCharacterInstance(Locale.ROOT).apply {
setText(originalText)
}
val insertionOffsets = mutableListOf<Int>()
var boundary = breakIterator.first()
while (true) {
boundary = breakIterator.next()
if (boundary == BreakIterator.DONE || boundary >= originalText.length) break
if (boundary <= 0) continue
val before = originalText.codePointBefore(boundary)
val after = originalText.codePointAt(boundary)
if (!Character.isWhitespace(before) && !Character.isWhitespace(after)) {
insertionOffsets += boundary
}
}
if (insertionOffsets.isEmpty()) return PreparedText(styledText, null)
val prepared = SpannableStringBuilder(styledText)
insertionOffsets.asReversed().forEach { offset ->
prepared.insert(offset, ZERO_WIDTH_SPACE)
}
val insertedAt = BooleanArray(originalText.length + 1)
insertionOffsets.forEach { insertedAt[it] = true }
val mapping = IntArray(prepared.length + 1)
var preparedOffset = 0
mapping[0] = 0
for (originalOffset in 0 until originalText.length) {
if (insertedAt[originalOffset]) {
preparedOffset++
mapping[preparedOffset] = originalOffset
}
preparedOffset++
mapping[preparedOffset] = originalOffset + 1
}
return PreparedText(prepared, mapping)
} }
private fun resolveLayoutMetrics(style: TextReaderStyle): LayoutMetrics { private fun resolveLayoutMetrics(style: TextReaderStyle): LayoutMetrics {
val pageWidth = style.pageWidthPx.coerceAtLeast(1) val pageWidth = style.pageWidthPx.coerceAtLeast(1)
val pageHeight = style.pageHeightPx.coerceAtLeast(1) val pageHeight = style.pageHeightPx.coerceAtLeast(1)
val requestedMargin = (MARGIN_DP * style.density).roundToInt().coerceAtLeast(1) val requestedHorizontalMargin = (style.horizontalMarginDp * style.density)
val horizontalMargin = requestedMargin.coerceAtMost(((pageWidth - 1) / 2).coerceAtLeast(0)) .roundToInt()
val verticalMargin = requestedMargin.coerceAtMost(((pageHeight - 1) / 2).coerceAtLeast(0)) .coerceAtLeast(0)
val requestedVerticalMargin = (VERTICAL_MARGIN_DP * style.density).roundToInt().coerceAtLeast(1)
val horizontalMargin = requestedHorizontalMargin.coerceAtMost(((pageWidth - 1) / 2).coerceAtLeast(0))
val verticalMargin = requestedVerticalMargin.coerceAtMost(((pageHeight - 1) / 2).coerceAtLeast(0))
return LayoutMetrics( return LayoutMetrics(
pageWidth = pageWidth, pageWidth = pageWidth,
@@ -239,11 +314,13 @@ internal object TextPageRenderer {
private fun addTextSpans( private fun addTextSpans(
text: String, text: String,
style: TextReaderStyle, style: TextReaderStyle,
highlightRange: IntRange?, sentenceHighlightRange: IntRange?,
activeHighlightRange: IntRange?,
): CharSequence { ): CharSequence {
val needsParagraphSpacing = style.paragraphSpacingPx > 0 && '\n' in text val needsParagraphSpacing = style.paragraphSpacingPx > 0 && '\n' in text
val needsHighlight = highlightRange != null && !highlightRange.isEmpty() val needsSentenceHighlight = sentenceHighlightRange != null && !sentenceHighlightRange.isEmpty()
if (!needsParagraphSpacing && !needsHighlight) return text val needsActiveHighlight = activeHighlightRange != null && !activeHighlightRange.isEmpty()
if (!needsParagraphSpacing && !needsSentenceHighlight && !needsActiveHighlight) return text
val spannable = SpannableString(text) val spannable = SpannableString(text)
if (needsParagraphSpacing) { if (needsParagraphSpacing) {
@@ -258,24 +335,53 @@ internal object TextPageRenderer {
} }
} }
} }
if (needsHighlight) {
val start = highlightRange!!.first.coerceIn(0, text.length) val (_, baseTextColor) = resolveColors(style)
val endExclusive = (highlightRange.last + 1).coerceIn(start, text.length) val darkTheme = baseTextColor == Color.WHITE
if (start < endExclusive) { sentenceHighlightRange?.let { range ->
val highlightColor = if (resolveColors(style).first == Color.BLACK) { applyHighlightSpan(
Color.rgb(92, 82, 24) spannable = spannable,
} else { textLength = text.length,
Color.rgb(255, 245, 157) range = range,
backgroundColor = if (darkTheme) Color.rgb(77, 66, 25) else Color.rgb(255, 243, 176),
foregroundColor = if (darkTheme) Color.WHITE else Color.BLACK,
)
} }
activeHighlightRange?.let { range ->
applyHighlightSpan(
spannable = spannable,
textLength = text.length,
range = range,
backgroundColor = if (darkTheme) Color.rgb(255, 193, 7) else Color.rgb(255, 179, 0),
foregroundColor = Color.BLACK,
)
}
return spannable
}
private fun applyHighlightSpan(
spannable: SpannableString,
textLength: Int,
range: IntRange,
backgroundColor: Int,
foregroundColor: Int,
) {
if (range.isEmpty()) return
val start = range.first.coerceIn(0, textLength)
val endExclusive = (range.last + 1).coerceIn(start, textLength)
if (start >= endExclusive) return
spannable.setSpan( spannable.setSpan(
BackgroundColorSpan(highlightColor), BackgroundColorSpan(backgroundColor),
start,
endExclusive,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
)
spannable.setSpan(
ForegroundColorSpan(foregroundColor),
start, start,
endExclusive, endExclusive,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
) )
}
}
return spannable
} }
private class ParagraphSpacingSpan(private val spacing: Int) : LineHeightSpan { private class ParagraphSpacingSpan(private val spacing: Int) : LineHeightSpan {
@@ -292,6 +398,21 @@ internal object TextPageRenderer {
} }
} }
private data class PreparedText(
val text: CharSequence,
val originalOffsetByPreparedOffset: IntArray?,
)
private data class BuiltLayout(
val layout: StaticLayout,
val originalOffsetByLayoutOffset: IntArray?,
) {
fun originalOffset(layoutOffset: Int): Int {
val mapping = originalOffsetByLayoutOffset ?: return layoutOffset
return mapping[layoutOffset.coerceIn(0, mapping.lastIndex)]
}
}
private data class LayoutMetrics( private data class LayoutMetrics(
val pageWidth: Int, val pageWidth: Int,
val pageHeight: Int, val pageHeight: Int,
@@ -31,10 +31,20 @@ internal class TextReaderPage(
private var style: TextReaderStyle = initialStyle private var style: TextReaderStyle = initialStyle
@Volatile @Volatile
private var highlightRange: IntRange? = null private var sentenceHighlightRange: IntRange? = null
@Volatile
private var activeHighlightRange: IntRange? = null
init { init {
stream = { TextPageRenderer.render(pageTextProvider(), style, highlightRange) } stream = {
TextPageRenderer.render(
text = pageTextProvider(),
style = style,
sentenceHighlightRange = sentenceHighlightRange,
activeHighlightRange = activeHighlightRange,
)
}
status = Page.State.Ready status = Page.State.Ready
} }
@@ -51,11 +61,26 @@ internal class TextReaderPage(
fun readText(): String = pageTextProvider() fun readText(): String = pageTextProvider()
fun updateTtsHighlight(start: Int, endExclusive: Int) { fun updateTtsHighlight(
highlightRange = if (start >= 0 && endExclusive > start) start until endExclusive else null sentenceStart: Int,
sentenceEndExclusive: Int,
activeStart: Int,
activeEndExclusive: Int,
) {
sentenceHighlightRange = if (sentenceStart >= 0 && sentenceEndExclusive > sentenceStart) {
sentenceStart until sentenceEndExclusive
} else {
null
}
activeHighlightRange = if (activeStart >= 0 && activeEndExclusive > activeStart) {
activeStart until activeEndExclusive
} else {
null
}
} }
fun clearTtsHighlight() { fun clearTtsHighlight() {
highlightRange = null sentenceHighlightRange = null
activeHighlightRange = null
} }
} }
@@ -120,6 +120,13 @@ class ReaderPreferences(
val textParagraphSpacing: Preference<Int> = preferenceStore.getInt("reader_text_paragraph_spacing", 4) val textParagraphSpacing: Preference<Int> = preferenceStore.getInt("reader_text_paragraph_spacing", 4)
val textHorizontalMargin: Preference<Int> = preferenceStore.getInt("reader_text_horizontal_margin", 32)
val textLineBreakMode: Preference<TextLineBreakMode> = preferenceStore.getEnum(
"reader_text_line_break_mode",
TextLineBreakMode.WORD,
)
val textFontFamily: Preference<TextFontFamily> = preferenceStore.getEnum( val textFontFamily: Preference<TextFontFamily> = preferenceStore.getEnum(
"reader_text_font_family", "reader_text_font_family",
TextFontFamily.SYSTEM, TextFontFamily.SYSTEM,
@@ -161,6 +168,8 @@ class ReaderPreferences(
fontSize = textFontSize.get(), fontSize = textFontSize.get(),
lineSpacingPercent = textLineSpacing.get(), lineSpacingPercent = textLineSpacing.get(),
paragraphSpacing = textParagraphSpacing.get(), paragraphSpacing = textParagraphSpacing.get(),
horizontalMarginDp = textHorizontalMargin.get(),
lineBreakMode = textLineBreakMode.get(),
fontFamily = textFontFamily.get(), fontFamily = textFontFamily.get(),
readerTheme = readerTheme.get(), readerTheme = readerTheme.get(),
automaticDark = (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == automaticDark = (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) ==
@@ -301,6 +310,11 @@ class ReaderPreferences(
MONOSPACE("monospace"), MONOSPACE("monospace"),
} }
enum class TextLineBreakMode {
WORD,
CHARACTER,
}
enum class FlashColor { enum class FlashColor {
BLACK, BLACK,
WHITE, WHITE,
@@ -435,6 +449,8 @@ data class TextReaderStyle(
val fontSize: Int, val fontSize: Int,
val lineSpacingPercent: Int, val lineSpacingPercent: Int,
val paragraphSpacing: Int, val paragraphSpacing: Int,
val horizontalMarginDp: Int,
val lineBreakMode: ReaderPreferences.TextLineBreakMode,
val fontFamily: ReaderPreferences.TextFontFamily, val fontFamily: ReaderPreferences.TextFontFamily,
val readerTheme: Int, val readerTheme: Int,
val automaticDark: Boolean, val automaticDark: Boolean,
@@ -51,6 +51,11 @@ class ReaderSettingsViewModel(
} }
} }
fun previewAndRequestTextSettingsReload() {
onTextSettingsPreview()
requestTextSettingsReload()
}
/** /**
* Theme-only changes do not affect page boundaries, so avoid an unnecessary * Theme-only changes do not affect page boundaries, so avoid an unnecessary
* full repagination. * full repagination.
@@ -14,6 +14,8 @@ object ReaderTtsController {
val readerChapterId: Long = -1L, val readerChapterId: Long = -1L,
val readerPageIndex: Int = -1, val readerPageIndex: Int = -1,
val documentIndex: Int = -1, val documentIndex: Int = -1,
val sourceSentenceStart: Long = -1L,
val sourceSentenceEndExclusive: Long = -1L,
val sourceHighlightStart: Long = -1L, val sourceHighlightStart: Long = -1L,
val sourceHighlightEndExclusive: Long = -1L, val sourceHighlightEndExclusive: Long = -1L,
) )
@@ -55,6 +57,20 @@ object ReaderTtsController {
) )
} }
fun resumeFromSource(
context: Context,
documentIndex: Int,
sourceOffset: Long,
) {
if (!state.value.active) return
context.startService(
Intent(context, ReaderTtsService::class.java)
.setAction(ReaderTtsService.ACTION_RESUME_FROM_SOURCE)
.putExtra(ReaderTtsService.EXTRA_DOCUMENT_INDEX, documentIndex)
.putExtra(ReaderTtsService.EXTRA_SOURCE_OFFSET, sourceOffset),
)
}
fun stop(context: Context) { fun stop(context: Context) {
if (!state.value.active) return if (!state.value.active) return
context.startService( context.startService(
@@ -25,8 +25,10 @@ class ReaderTtsService : Service() {
private var sessionId: String? = null private var sessionId: String? = null
private var session: ReaderTtsSessionStore.SessionReader? = null private var session: ReaderTtsSessionStore.SessionReader? = null
private var pagePosition = 0 private var pagePosition = 0
private var currentPageText: String? = null private var pageSpeechStartOffset = 0
private var currentWindow: SpeechWindow? = null
private var currentSegments: List<Segment> = emptyList() private var currentSegments: List<Segment> = emptyList()
private var lookaheadConsumedChars: Map<Int, Int> = emptyMap()
private var segmentPosition = 0 private var segmentPosition = 0
private var segmentSpeechOffset = 0 private var segmentSpeechOffset = 0
private var currentUtteranceBaseOffset = 0 private var currentUtteranceBaseOffset = 0
@@ -44,6 +46,10 @@ class ReaderTtsService : Service() {
when (intent?.action) { when (intent?.action) {
ACTION_START -> startSession(intent) ACTION_START -> startSession(intent)
ACTION_TOGGLE -> if (playing) pausePlayback() else resumePlayback() ACTION_TOGGLE -> if (playing) pausePlayback() else resumePlayback()
ACTION_RESUME_FROM_SOURCE -> resumeFromSource(
documentIndex = intent.getIntExtra(EXTRA_DOCUMENT_INDEX, -1),
sourceOffset = intent.getLongExtra(EXTRA_SOURCE_OFFSET, -1L),
)
ACTION_STOP -> stopPlayback() ACTION_STOP -> stopPlayback()
ACTION_SET_SPEED -> { ACTION_SET_SPEED -> {
playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, playbackSpeed).coerceIn(MIN_SPEED, MAX_SPEED) playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, playbackSpeed).coerceIn(MIN_SPEED, MAX_SPEED)
@@ -86,8 +92,10 @@ class ReaderTtsService : Service() {
pagePosition = newSession.pages.indexOfFirst { it.readerPageIndex >= requestedPageIndex } pagePosition = newSession.pages.indexOfFirst { it.readerPageIndex >= requestedPageIndex }
.takeIf { it >= 0 } .takeIf { it >= 0 }
?: 0 ?: 0
currentPageText = null pageSpeechStartOffset = 0
currentWindow = null
currentSegments = emptyList() currentSegments = emptyList()
lookaheadConsumedChars = emptyMap()
segmentPosition = 0 segmentPosition = 0
segmentSpeechOffset = 0 segmentSpeechOffset = 0
currentUtteranceBaseOffset = 0 currentUtteranceBaseOffset = 0
@@ -200,17 +208,78 @@ class ReaderTtsService : Service() {
speakCurrentSegment() speakCurrentSegment()
} }
private fun speakCurrentSegment() { private fun resumeFromSource(
if (!playing || !initialized) return documentIndex: Int,
val session = session ?: return stopPlayback() sourceOffset: Long,
) {
if (!initialized || documentIndex < 0 || sourceOffset < 0L) return
val session = session ?: return
if (currentPageText == null) { val exactPosition = session.pages.indexOfFirst { entry ->
currentPageText = session.readPage(pagePosition) entry.documentIndex == documentIndex &&
currentSegments = segmentText(currentPageText.orEmpty()) sourceOffset >= entry.sourceStartOffset &&
sourceOffset < entry.sourceEndOffsetExclusive
}
val nextPosition = if (exactPosition < 0) {
session.pages.indexOfFirst { entry ->
entry.documentIndex == documentIndex && entry.sourceStartOffset >= sourceOffset
}
} else {
-1
}
val previousPosition = if (exactPosition < 0 && nextPosition < 0) {
session.pages.indexOfLast { entry ->
entry.documentIndex == documentIndex && entry.sourceStartOffset <= sourceOffset
}
} else {
-1
}
val newPagePosition = when {
exactPosition >= 0 -> exactPosition
nextPosition >= 0 -> nextPosition
previousPosition >= 0 -> previousPosition
else -> return
}
val entry = session.pages[newPagePosition]
val relativeOffset = (sourceOffset - entry.sourceStartOffset)
.coerceAtLeast(0L)
.coerceAtMost(Int.MAX_VALUE.toLong())
.toInt()
tts?.stop()
pagePosition = newPagePosition
pageSpeechStartOffset = relativeOffset
currentWindow = null
currentSegments = emptyList()
lookaheadConsumedChars = emptyMap()
segmentPosition = 0 segmentPosition = 0
segmentSpeechOffset = 0
currentUtteranceBaseOffset = 0
currentUtteranceId = null
playing = true
ReaderTtsController.updateState(
ReaderTtsController.State(
active = true,
playing = true,
readerChapterId = readerChapterId,
readerPageIndex = entry.readerPageIndex,
documentIndex = documentIndex,
sourceSentenceStart = sourceOffset,
sourceSentenceEndExclusive = sourceOffset,
),
)
refreshNotification()
speakCurrentSegment()
} }
if (currentSegments.isEmpty() || segmentPosition !in currentSegments.indices) { private fun speakCurrentSegment() {
if (!playing || !initialized) return
if (currentSegments.isEmpty()) {
if (!prepareCurrentSegments()) return
}
if (segmentPosition !in currentSegments.indices) {
advancePage() advancePage()
return return
} }
@@ -222,11 +291,19 @@ class ReaderTtsService : Service() {
advanceSegment() advanceSegment()
return return
} }
currentUtteranceBaseOffset = speechOffset currentUtteranceBaseOffset = speechOffset
val speechText = sanitizeSpeechText(segment.text.substring(speechOffset))
if (speechText.isBlank()) {
segmentSpeechOffset = 0
advanceSegment()
return
}
val utteranceId = "$ttsGeneration:$pagePosition:$segmentPosition:$speechOffset" val utteranceId = "$ttsGeneration:$pagePosition:$segmentPosition:$speechOffset"
currentUtteranceId = utteranceId currentUtteranceId = utteranceId
val result = tts?.speak( val result = tts?.speak(
segment.text.substring(speechOffset), speechText,
TextToSpeech.QUEUE_FLUSH, TextToSpeech.QUEUE_FLUSH,
null, null,
utteranceId, utteranceId,
@@ -238,6 +315,125 @@ class ReaderTtsService : Service() {
} }
} }
private fun prepareCurrentSegments(): Boolean {
val session = session ?: run {
stopPlayback()
return false
}
val currentEntry = session.pages.getOrNull(pagePosition) ?: run {
stopPlayback()
return false
}
val currentText = session.readPage(pagePosition)
val safeStart = pageSpeechStartOffset.coerceIn(0, currentText.length)
if (safeStart >= currentText.length) {
advancePage()
return false
}
val window = buildSpeechWindow(session, currentEntry.documentIndex, safeStart)
val currentPart = window.parts.firstOrNull { it.pagePosition == pagePosition } ?: run {
advancePage()
return false
}
val segments = segmentText(window.text)
.filter { it.start < currentPart.windowEndExclusive }
if (segments.isEmpty()) {
advancePage()
return false
}
currentWindow = window
currentSegments = segments
segmentPosition = 0
lookaheadConsumedChars = buildMap {
val last = segments.last()
window.parts
.asSequence()
.filter { it.pagePosition > pagePosition }
.forEach { part ->
val consumedEnd = min(last.endExclusive, part.windowEndExclusive)
if (consumedEnd > part.windowStart) {
put(part.pagePosition, consumedEnd - part.windowStart)
}
}
}
return true
}
private fun buildSpeechWindow(
session: ReaderTtsSessionStore.SessionReader,
documentIndex: Int,
currentPageStart: Int,
): SpeechWindow {
val text = StringBuilder()
val parts = mutableListOf<WindowPart>()
var candidatePage = pagePosition
var pagesAdded = 0
var firstPageEnd = -1
while (
candidatePage < session.pages.size &&
pagesAdded < MAX_LOOKAHEAD_PAGES &&
text.length < MAX_SPEECH_WINDOW_CHARS
) {
val entry = session.pages[candidatePage]
if (entry.documentIndex != documentIndex) break
val pageText = session.readPage(candidatePage)
val pageStart = if (candidatePage == pagePosition) currentPageStart else 0
if (pageStart >= pageText.length) {
candidatePage++
continue
}
if (text.isNotEmpty()) text.append(' ')
val windowStart = text.length
val available = (MAX_SPEECH_WINDOW_CHARS - text.length).coerceAtLeast(0)
if (available == 0) break
val takeCount = min(pageText.length - pageStart, available)
text.append(pageText, pageStart, pageStart + takeCount)
val windowEnd = text.length
parts += WindowPart(
windowStart = windowStart,
windowEndExclusive = windowEnd,
pagePosition = candidatePage,
documentIndex = entry.documentIndex,
sourceStartOffset = entry.sourceStartOffset + pageStart,
)
if (candidatePage == pagePosition) {
firstPageEnd = windowEnd
}
pagesAdded++
if (
candidatePage > pagePosition &&
firstPageEnd >= 0 &&
hasSentenceTerminatorAfter(text, firstPageEnd)
) {
break
}
if (takeCount < pageText.length - pageStart) break
candidatePage++
}
return SpeechWindow(
text = text.toString(),
parts = parts,
)
}
private fun hasSentenceTerminatorAfter(text: CharSequence, start: Int): Boolean {
for (index in start.coerceAtLeast(0) until text.length) {
if (text[index] in SENTENCE_TERMINATORS) return true
}
return false
}
private fun advanceSegment() { private fun advanceSegment() {
currentUtteranceId = null currentUtteranceId = null
segmentSpeechOffset = 0 segmentSpeechOffset = 0
@@ -254,7 +450,8 @@ class ReaderTtsService : Service() {
} }
pagePosition++ pagePosition++
currentPageText = null pageSpeechStartOffset = lookaheadConsumedChars[pagePosition] ?: 0
currentWindow = null
currentSegments = emptyList() currentSegments = emptyList()
segmentPosition = 0 segmentPosition = 0
segmentSpeechOffset = 0 segmentSpeechOffset = 0
@@ -268,8 +465,9 @@ class ReaderTtsService : Service() {
cleanupTts() cleanupTts()
session?.close() session?.close()
session = null session = null
currentPageText = null currentWindow = null
currentSegments = emptyList() currentSegments = emptyList()
lookaheadConsumedChars = emptyMap()
val oldSessionId = sessionId val oldSessionId = sessionId
sessionId = null sessionId = null
ReaderTtsSessionStore.delete(this, oldSessionId) ReaderTtsSessionStore.delete(this, oldSessionId)
@@ -296,8 +494,18 @@ class ReaderTtsService : Service() {
active = true, active = true,
playing = playing, playing = playing,
readerChapterId = readerChapterId, readerChapterId = readerChapterId,
readerPageIndex = page.readerPageIndex, readerPageIndex = if (!clearHighlight && previous.readerPageIndex >= 0) {
documentIndex = page.documentIndex, previous.readerPageIndex
} else {
page.readerPageIndex
},
documentIndex = if (!clearHighlight && previous.documentIndex >= 0) {
previous.documentIndex
} else {
page.documentIndex
},
sourceSentenceStart = if (clearHighlight) -1L else previous.sourceSentenceStart,
sourceSentenceEndExclusive = if (clearHighlight) -1L else previous.sourceSentenceEndExclusive,
sourceHighlightStart = if (clearHighlight) -1L else previous.sourceHighlightStart, sourceHighlightStart = if (clearHighlight) -1L else previous.sourceHighlightStart,
sourceHighlightEndExclusive = if (clearHighlight) -1L else previous.sourceHighlightEndExclusive, sourceHighlightEndExclusive = if (clearHighlight) -1L else previous.sourceHighlightEndExclusive,
), ),
@@ -311,22 +519,70 @@ class ReaderTtsService : Service() {
relativeEndExclusive: Int = segment.text.length, relativeEndExclusive: Int = segment.text.length,
) { ) {
val session = session ?: return val session = session ?: return
val page = session.pages.getOrNull(pagePosition) ?: return val window = currentWindow ?: return
val sentenceStart = window.mapStart(segment.start) ?: return
val sentenceEnd = window.mapEnd(segment.endExclusive) ?: return
val safeStart = relativeStart.coerceIn(0, segment.text.length) val safeStart = relativeStart.coerceIn(0, segment.text.length)
val safeEnd = relativeEndExclusive.coerceIn(safeStart, segment.text.length) val safeEnd = relativeEndExclusive.coerceIn(safeStart, segment.text.length)
val activeStart = if (safeEnd > safeStart) {
window.mapStart(segment.start + safeStart)
} else {
null
}
val activeEnd = if (safeEnd > safeStart) {
window.mapEnd(segment.start + safeEnd)
} else {
null
}
val activePosition = activeStart ?: sentenceStart
val page = session.pages.getOrNull(activePosition.pagePosition) ?: return
ReaderTtsController.updateState( ReaderTtsController.updateState(
ReaderTtsController.State( ReaderTtsController.State(
active = true, active = true,
playing = playing, playing = playing,
readerChapterId = readerChapterId, readerChapterId = readerChapterId,
readerPageIndex = page.readerPageIndex, readerPageIndex = page.readerPageIndex,
documentIndex = page.documentIndex, documentIndex = activePosition.documentIndex,
sourceHighlightStart = page.sourceStartOffset + segment.start + safeStart, sourceSentenceStart = sentenceStart.sourceOffset,
sourceHighlightEndExclusive = page.sourceStartOffset + segment.start + safeEnd, sourceSentenceEndExclusive = sentenceEnd.sourceOffset,
sourceHighlightStart = activeStart?.sourceOffset ?: -1L,
sourceHighlightEndExclusive = activeEnd?.sourceOffset ?: -1L,
), ),
) )
} }
private fun SpeechWindow.mapStart(windowOffset: Int): MappedPosition? {
if (text.isEmpty() || parts.isEmpty()) return null
val clamped = windowOffset.coerceIn(0, text.lastIndex)
val part = parts.firstOrNull { clamped in it.windowStart until it.windowEndExclusive }
?: parts.firstOrNull { it.windowStart > clamped }
?: parts.lastOrNull()
?: return null
val charIndex = clamped.coerceIn(part.windowStart, part.windowEndExclusive - 1)
return MappedPosition(
pagePosition = part.pagePosition,
documentIndex = part.documentIndex,
sourceOffset = part.sourceStartOffset + (charIndex - part.windowStart),
)
}
private fun SpeechWindow.mapEnd(windowEndExclusive: Int): MappedPosition? {
if (text.isEmpty() || parts.isEmpty()) return null
val clamped = (windowEndExclusive - 1).coerceIn(0, text.lastIndex)
val part = parts.firstOrNull { clamped in it.windowStart until it.windowEndExclusive }
?: parts.lastOrNull { it.windowEndExclusive <= clamped + 1 }
?: parts.firstOrNull()
?: return null
val charIndex = clamped.coerceIn(part.windowStart, part.windowEndExclusive - 1)
return MappedPosition(
pagePosition = part.pagePosition,
documentIndex = part.documentIndex,
sourceOffset = part.sourceStartOffset + (charIndex - part.windowStart) + 1L,
)
}
private fun refreshNotification() { private fun refreshNotification() {
if (session == null) return if (session == null) return
startForeground(Notifications.ID_READER_TTS, buildNotification(preparing = false)) startForeground(Notifications.ID_READER_TTS, buildNotification(preparing = false))
@@ -374,7 +630,11 @@ class ReaderTtsService : Service() {
mainHandler.post { mainHandler.post {
if (!playing || utteranceId != currentUtteranceId) return@post if (!playing || utteranceId != currentUtteranceId) return@post
currentSegments.getOrNull(segmentPosition)?.let { segment -> currentSegments.getOrNull(segmentPosition)?.let { segment ->
publishHighlight(segment, currentUtteranceBaseOffset, segment.text.length) publishHighlight(
segment = segment,
relativeStart = currentUtteranceBaseOffset,
relativeEndExclusive = (currentUtteranceBaseOffset + 1).coerceAtMost(segment.text.length),
)
} }
} }
} }
@@ -427,6 +687,77 @@ class ReaderTtsService : Service() {
override fun onBind(intent: Intent?): IBinder? = null override fun onBind(intent: Intent?): IBinder? = null
private fun sanitizeSpeechText(text: String): String {
if (text.isBlank()) return text
var hasReadableContent = false
var scan = 0
while (scan < text.length) {
val codePoint = text.codePointAt(scan)
if (Character.isLetterOrDigit(codePoint)) {
hasReadableContent = true
break
}
scan += Character.charCount(codePoint)
}
if (!hasReadableContent) return " ".repeat(text.length)
val chars = text.toCharArray()
var index = 0
while (index < text.length) {
val codePoint = text.codePointAt(index)
if (!isPunctuationOrSymbol(codePoint)) {
index += Character.charCount(codePoint)
continue
}
val runStart = index
var runEnd = index
var containsSymbol = false
while (runEnd < text.length) {
val current = text.codePointAt(runEnd)
if (!isPunctuationOrSymbol(current)) break
containsSymbol = containsSymbol || isSymbol(current)
runEnd += Character.charCount(current)
}
if (containsSymbol || runEnd - runStart >= 2) {
for (offset in runStart until runEnd) chars[offset] = ' '
}
index = runEnd
}
return chars.concatToString()
}
private fun isPunctuationOrSymbol(codePoint: Int): Boolean {
return when (Character.getType(codePoint)) {
Character.CONNECTOR_PUNCTUATION.toInt(),
Character.DASH_PUNCTUATION.toInt(),
Character.START_PUNCTUATION.toInt(),
Character.END_PUNCTUATION.toInt(),
Character.INITIAL_QUOTE_PUNCTUATION.toInt(),
Character.FINAL_QUOTE_PUNCTUATION.toInt(),
Character.OTHER_PUNCTUATION.toInt(),
Character.MATH_SYMBOL.toInt(),
Character.CURRENCY_SYMBOL.toInt(),
Character.MODIFIER_SYMBOL.toInt(),
Character.OTHER_SYMBOL.toInt(),
-> true
else -> false
}
}
private fun isSymbol(codePoint: Int): Boolean {
return when (Character.getType(codePoint)) {
Character.MATH_SYMBOL.toInt(),
Character.CURRENCY_SYMBOL.toInt(),
Character.MODIFIER_SYMBOL.toInt(),
Character.OTHER_SYMBOL.toInt(),
-> true
else -> false
}
}
private fun segmentText(text: String): List<Segment> { private fun segmentText(text: String): List<Segment> {
if (text.isBlank()) return emptyList() if (text.isBlank()) return emptyList()
@@ -489,9 +820,29 @@ class ReaderTtsService : Service() {
val text: String, val text: String,
) )
private data class SpeechWindow(
val text: String,
val parts: List<WindowPart>,
)
private data class WindowPart(
val windowStart: Int,
val windowEndExclusive: Int,
val pagePosition: Int,
val documentIndex: Int,
val sourceStartOffset: Long,
)
private data class MappedPosition(
val pagePosition: Int,
val documentIndex: Int,
val sourceOffset: Long,
)
companion object { companion object {
const val ACTION_START = "eu.kanade.tachiyomi.reader.tts.START" const val ACTION_START = "eu.kanade.tachiyomi.reader.tts.START"
const val ACTION_TOGGLE = "eu.kanade.tachiyomi.reader.tts.TOGGLE" const val ACTION_TOGGLE = "eu.kanade.tachiyomi.reader.tts.TOGGLE"
const val ACTION_RESUME_FROM_SOURCE = "eu.kanade.tachiyomi.reader.tts.RESUME_FROM_SOURCE"
const val ACTION_STOP = "eu.kanade.tachiyomi.reader.tts.STOP" const val ACTION_STOP = "eu.kanade.tachiyomi.reader.tts.STOP"
const val ACTION_SET_SPEED = "eu.kanade.tachiyomi.reader.tts.SET_SPEED" const val ACTION_SET_SPEED = "eu.kanade.tachiyomi.reader.tts.SET_SPEED"
const val ACTION_SET_ENGINE = "eu.kanade.tachiyomi.reader.tts.SET_ENGINE" const val ACTION_SET_ENGINE = "eu.kanade.tachiyomi.reader.tts.SET_ENGINE"
@@ -499,6 +850,8 @@ class ReaderTtsService : Service() {
const val EXTRA_SESSION_ID = "session_id" const val EXTRA_SESSION_ID = "session_id"
const val EXTRA_START_PAGE_INDEX = "start_page_index" const val EXTRA_START_PAGE_INDEX = "start_page_index"
const val EXTRA_DOCUMENT_INDEX = "document_index"
const val EXTRA_SOURCE_OFFSET = "source_offset"
const val EXTRA_CHAPTER_ID = "chapter_id" const val EXTRA_CHAPTER_ID = "chapter_id"
const val EXTRA_TITLE = "title" const val EXTRA_TITLE = "title"
const val EXTRA_SUBTITLE = "subtitle" const val EXTRA_SUBTITLE = "subtitle"
@@ -513,6 +866,9 @@ class ReaderTtsService : Service() {
private const val MIN_SPEED = 1.2f private const val MIN_SPEED = 1.2f
private const val MAX_SPEED = 3.0f private const val MAX_SPEED = 3.0f
private const val MAX_SEGMENT_CHARS = 1200 private const val MAX_SEGMENT_CHARS = 1200
private const val MAX_LOOKAHEAD_PAGES = 3
private const val MAX_SPEECH_WINDOW_CHARS = 6000
private val SENTENCE_TERMINATORS = setOf('.', '!', '?', '。', '!', '?')
private const val REQUEST_TOGGLE = 9101 private const val REQUEST_TOGGLE = 9101
private const val REQUEST_STOP = 9102 private const val REQUEST_STOP = 9102
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M6,6h12v12H6z" />
</vector>
@@ -1110,8 +1110,13 @@
<string name="pref_text_reader_tts_voice_settings">Voice data settings</string> <string name="pref_text_reader_tts_voice_settings">Voice data settings</string>
<string name="reader_tts_start">Start TTS</string> <string name="reader_tts_start">Start TTS</string>
<string name="reader_tts_pause">Pause TTS</string> <string name="reader_tts_pause">Pause TTS</string>
<string name="reader_tts_stop">Stop TTS</string>
<string name="pref_text_reader_line_spacing">Line spacing</string> <string name="pref_text_reader_line_spacing">Line spacing</string>
<string name="pref_text_reader_paragraph_spacing">Paragraph spacing</string> <string name="pref_text_reader_paragraph_spacing">Paragraph spacing</string>
<string name="pref_text_reader_horizontal_margin">Side margins</string>
<string name="pref_text_reader_line_break_mode">Line wrapping</string>
<string name="pref_text_reader_line_break_word">By word</string>
<string name="pref_text_reader_line_break_character">By character</string>
<string name="pref_text_reader_font_family">Font</string> <string name="pref_text_reader_font_family">Font</string>
<string name="pref_text_reader_font_system">System</string> <string name="pref_text_reader_font_system">System</string>
<string name="pref_text_reader_font_sans">Sans-serif</string> <string name="pref_text_reader_font_sans">Sans-serif</string>
@@ -918,8 +918,13 @@
<string name="pref_text_reader_tts_voice_settings">음성 데이터 설정</string> <string name="pref_text_reader_tts_voice_settings">음성 데이터 설정</string>
<string name="reader_tts_start">TTS 시작</string> <string name="reader_tts_start">TTS 시작</string>
<string name="reader_tts_pause">TTS 일시정지</string> <string name="reader_tts_pause">TTS 일시정지</string>
<string name="reader_tts_stop">TTS 정지</string>
<string name="pref_text_reader_line_spacing">행간</string> <string name="pref_text_reader_line_spacing">행간</string>
<string name="pref_text_reader_paragraph_spacing">문단 간격</string> <string name="pref_text_reader_paragraph_spacing">문단 간격</string>
<string name="pref_text_reader_horizontal_margin">좌우 여백</string>
<string name="pref_text_reader_line_break_mode">줄바꿈</string>
<string name="pref_text_reader_line_break_word">단어 단위</string>
<string name="pref_text_reader_line_break_character">글자 단위</string>
<string name="pref_text_reader_font_family">글꼴</string> <string name="pref_text_reader_font_family">글꼴</string>
<string name="pref_text_reader_font_system">시스템</string> <string name="pref_text_reader_font_system">시스템</string>
<string name="pref_text_reader_font_sans">고딕</string> <string name="pref_text_reader_font_sans">고딕</string>