Skip to content

Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.

feat: Read aloud reads from a tapped word or the selection, and starts over after the end (#36)

Josh796fa92874ec
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt
index d78d323..232d73c 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt
@@ -666,6 +666,11 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {
return
}
val saved = ReadAloud.savedPosition(getApplication(), key)?.takeIf { it.first in 0 until doc.pageCount }
+ // a reading that went to the end starts over at the beginning, as the reference's does
+ if (saved == ReadAloud.START_OVER) {
+ readAloud = ReadAloud(getApplication(), eng, doc, viewModelScope, 0, { readAloud = null }, key)
+ return
+ }
if (saved != null) {
readAloud = ReadAloud(getApplication(), eng, doc, viewModelScope, saved.first, { readAloud = null }, key, resumeAt = saved.second to saved.third)
tell(text(R.string.msg_read_aloud_resumed), text(R.string.msg_read_aloud_start_over)) {
@@ -679,6 +684,20 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {
readAloud = ReadAloud(getApplication(), eng, doc, viewModelScope, doc.viewer.currentPage, { readAloud = null }, key)
}
+ /**
+ * While Read aloud is on, a tap on a word moves the reading there, as the reference's does. False
+ * when no word is under [at] (the tap then shows or hides the bars).
+ */
+ suspend fun readAloudAt(doc: OpenDocument, at: io.github.artcraftandroid.pdfcraft.ui.viewer.PagePoint, slopPoints: Float): Boolean {
+ val current = readAloud ?: return false
+ val eng = engine ?: return false
+ val word = withContext(eng.commands) { eng.session.wordAt(doc.id, at.page.toUInt(), at.x, at.y, slopPoints) } ?: return false
+ if (readAloud !== current) return false
+ current.stop()
+ readAloud = ReadAloud(getApplication(), eng, doc, viewModelScope, word.page.toInt(), { readAloud = null }, docKey(doc), startGlyph = word.start)
+ return true
+ }
+
// ---- Tools' remembered settings, bookmarks, stars --------------------------------------
val toolPrefs = ToolPrefs(app)
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ReadAloud.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ReadAloud.kt
index 9a74a5a..d7b9c58 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ReadAloud.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ReadAloud.kt
@@ -361,11 +361,11 @@ class ReadAloud(
fun stop() {
playing = false
- // keep the place for next time (the reference offers to resume); a finished reading starts over
+ // keep the place for next time (the reference offers to resume); a finished reading starts
+ // over at the beginning next time, as the reference's does
if (key.isNotEmpty()) {
- val e = prefs(prefsCtx).edit()
- if (finished) e.remove("pos:$key") else e.putString("pos:$key", "$page,$index,$offset")
- e.apply()
+ val (p, i, o) = if (finished) START_OVER else Triple(page, index, offset)
+ prefs(prefsCtx).edit().putString("pos:$key", "$p,$i,$o").apply()
}
runCatching {
tts.stop()
@@ -392,6 +392,9 @@ class ReadAloud(
companion object {
val SPEEDS = listOf(0.5f, 0.75f, 1f, 1.25f, 1.5f, 1.75f, 2f)
+
+ /** The saved position of a reading that went to the end: the top of the first page. */
+ val START_OVER = Triple(0, 0, 0)
/** A voice's pace at 1.0x, for turning 15 seconds into words. */
private const val WORDS_PER_MINUTE = 128f
private fun prefs(c: Context) = c.getSharedPreferences("readAloud", Context.MODE_PRIVATE)
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerOverlays.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerOverlays.kt
index 73680eb..86148f2 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerOverlays.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerOverlays.kt
@@ -227,7 +227,8 @@ fun BoxScope.SelectionMenu(vm: AppViewModel, doc: OpenDocument, sel: Selection)
vm.enterTool(doc, ViewerTool.Edit)
}
if (doc.info.allowsCopying) MenuRow(LucideIcons.Copy, stringResource(R.string.viewer_copy_text)) { vm.copySelection(doc) }
- MenuRow(LucideIcons.Volume2, stringResource(R.string.common_tool_read_aloud)) {
+ // while reading aloud, the reading moves to the selection (the reference's "from here")
+ MenuRow(LucideIcons.Volume2, stringResource(if (vm.readAloud != null) R.string.viewer_read_from_here else R.string.common_tool_read_aloud)) {
vm.startReadAloud(doc, fromSelection = true)
doc.selection.clear()
}
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerScreen.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerScreen.kt
index 8da34a0..a9b6b58 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerScreen.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerScreen.kt
@@ -205,6 +205,11 @@ fun ViewerScreen(vm: AppViewModel, doc: OpenDocument) {
vm.tapField(doc, at) -> Unit
vm.tapComment(doc, at) -> Unit
vm.tapLink(doc, at) -> Unit
+ // while reading aloud, a word moves the reading there; anywhere else shows or hides the bars
+ vm.readAloud != null && at != null && ctl.tool == ViewerTool.None -> scope.launch {
+ val slop = 12f / doc.viewer.pixelsPerPoint(at.page).coerceAtLeast(0.1f) * density.density
+ if (!vm.readAloudAt(doc, at, slop)) ctl.toggleBars()
+ }
doc.viewer.mode == PageMode.SinglePage && doc.viewer.zoom <= 1.01f && edgeTap(p.x, doc.viewer.viewW) != 0 -> {
doc.viewer.goToPage(doc.viewer.currentPage + edgeTap(p.x, doc.viewer.viewW))
ctl.barsVisible = false
diff --git a/apps/pdfcraft-android/android/app/src/main/res/values-es/strings.xml b/apps/pdfcraft-android/android/app/src/main/res/values-es/strings.xml
index adc15ee..50a7591 100644
--- a/apps/pdfcraft-android/android/app/src/main/res/values-es/strings.xml
+++ b/apps/pdfcraft-android/android/app/src/main/res/values-es/strings.xml
@@ -445,6 +445,7 @@
<string name="viewer_add_comment">Añadir comentario</string>
<string name="viewer_strikethrough">Tachar</string>
<string name="viewer_edit_text">Editar texto</string>
+ <string name="viewer_read_from_here">Leer desde aquí</string>
<string name="viewer_copy_text">Copiar texto</string>
<!-- Translator note: accessibility labels of the two handles at the ends of selected text. -->
<string name="viewer_selection_start">Inicio de la selección</string>
diff --git a/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml b/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml
index 0137410..908c4ec 100644
--- a/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml
+++ b/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml
@@ -446,6 +446,7 @@
<string name="viewer_add_comment">Add comment</string>
<string name="viewer_strikethrough">Strikethrough</string>
<string name="viewer_edit_text">Edit text</string>
+ <string name="viewer_read_from_here">Read from here</string>
<string name="viewer_copy_text">Copy text</string>
<!-- Translator note: accessibility labels of the two handles at the ends of selected text. -->
<string name="viewer_selection_start">Selection start</string>