Compare commits

...
32 Commits
Author SHA1 Message Date
sergeych a2f417d147 TTS improvements 2026-10-13 01:43:22 +04:00
sergeych 9dbe30b20c Add offline desktop speech with Silero Xenia for Russian 2026-09-28 13:45:00 +04:00
sergeych 9cd8ebc422 Page swipe navigation and history tracking improvements, version bump to 1.1. 2026-07-10 20:57:04 +03:00
sergeych 85d97d5a90 +better notes click processing
+android & universal UI tweaks
+settings to change text and background color
2026-06-09 21:30:36 +03:00
sergeych f344befbdd version bump and new screenshots 2026-05-31 12:56:14 +03:00
sergeych dbf63c351c Reading progress, persist library mode and improve scan feedback 2026-05-31 12:40:24 +03:00
sergeych e8b18ec472 1.0.7 2026-05-28 12:57:33 +03:00
sergeych 37abbf884c Adjusted icon sizes and padding in ReaderScreen for improved UI consistency. 2026-05-27 19:02:58 +03:00
sergeych 0084c478a3 fixed bug on opening a book on android 2026-05-27 10:43:18 +03:00
sergeych 6990e3c233 last published version code 2026-05-27 08:59:12 +03:00
sergeych 9f54c0aca0 Improved TOC scrolling behavior and fixed incorrect content indentation logic. Added test for image-only sections. 2026-05-25 20:48:44 +03:00
sergeych 7681590992 contents for reading 2026-05-25 20:37:32 +03:00
sergeych cde4d89eed Added URL detection in reader, improved platform URL handling, library item auto-rescan, and file association support. 2026-05-25 20:14:33 +03:00
sergeych 2e9a52f4af added menu icons 2026-05-25 10:13:51 +03:00
sergeych 2fddacb7e7 added visual settings (font/spacing/weight), imprpved linux "show file", added "share/show" to a library card 2026-05-25 10:10:01 +03:00
sergeych 3e83185e4f fixed wrong left padding in the books with different and sometimes wrong inner structure 2026-05-25 09:13:05 +03:00
sergeych 65925f2a07 v1.0.4 as published 2026-05-25 00:10:15 +03:00
sergeych 17c885b3f5 better images and correct library re-sorting 2026-05-24 18:42:52 +03:00
sergeych 89b1115169 version bump 2026-05-24 11:05:41 +03:00
sergeych 8e7dcc0307 footnotes support (as notes) 2026-05-24 11:05:19 +03:00
sergeych 50f201d3c6 title bar trim to ellipsis 2026-05-24 10:57:33 +03:00
sergeych fe4d569afa version upgrade to 3 2026-05-24 10:52:39 +03:00
sergeych c7b7ca68ab refactored library processing/views, better not interesting flag processing 2026-05-24 10:26:40 +03:00
sergeych de04b9cbed support for poetry and epigraphs 2026-05-24 09:31:32 +03:00
sergeych 316b10d015 nice border around images in the dark theme 2026-05-24 08:57:44 +03:00
sergeych 44a83a17dd improved image views 2026-05-24 08:53:23 +03:00
sergeych 1c6a80c43b fixed bug with returning from the reading state and search 2026-05-24 08:01:47 +03:00
sergeych b0f45aaf1b better stress in some russian words 2026-05-23 22:24:47 +03:00
sergeych ed66ea0d55 UI/TTS improvements 2026-05-23 21:55:34 +03:00
sergeych 0eba07b594 as sent to rustore 2026-05-23 18:30:29 +03:00
sergeych 128275402e preparing the publication 2026-05-23 17:30:14 +03:00
sergeych bad1e89c26 Add app localization 2026-05-23 16:45:42 +03:00
78 changed files with 218654 additions and 905 deletions

No files matched your search

+4
View File
@@ -17,3 +17,7 @@ captures
!*.xcworkspace/contents.xcworkspacedata
**/xcshareddata/WorkspaceSettings.xcsettings
node_modules/
/composeApp/release/
/test_books/
/.junie/
/.local/
+2
View File
@@ -31,6 +31,8 @@ in your IDE’s toolbar or build it directly from the terminal:
### Build and Run Desktop (JVM) Application
Desktop offline English/Russian speech: [CPU TTS setup and audition](docs/desktop-tts.md).
To build and run the development version of the desktop app, use the run configuration from the run widget
in your IDE’s toolbar or run it directly from the terminal:
+65 -4
View File
@@ -2,6 +2,10 @@ import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
val appVersionName = "1.1"
val appVersionCode = 10
val appVersionDisplay = "$appVersionName.$appVersionCode"
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidApplication)
@@ -10,6 +14,38 @@ plugins {
alias(libs.plugins.composeHotReload)
}
abstract class GenerateAppVersionConstantsTask : DefaultTask() {
@get:Input
abstract val versionName: Property<String>
@get:Input
abstract val versionCode: Property<Int>
@get:OutputDirectory
abstract val outputDir: DirectoryProperty
@TaskAction
fun generate() {
val outputFile = outputDir.file("net/sergeych/toread/AppVersion.kt").get().asFile
outputFile.parentFile.mkdirs()
outputFile.writeText(
"""
package net.sergeych.toread
internal const val AppVersionName = "${versionName.get()}"
internal const val AppVersionCode = ${versionCode.get()}
internal const val AppVersionDisplay = "${versionName.get()}.${versionCode.get()}"
""".trimIndent() + "\n",
)
}
}
val generateAppVersionConstants by tasks.registering(GenerateAppVersionConstantsTask::class) {
versionName.set(appVersionName)
versionCode.set(appVersionCode)
outputDir.set(layout.buildDirectory.dir("generated/appVersion/commonMain/kotlin"))
}
kotlin {
jvmToolchain(17)
@@ -33,6 +69,9 @@ kotlin {
}
sourceSets {
commonMain {
kotlin.srcDir(generateAppVersionConstants)
}
androidMain.dependencies {
implementation(libs.compose.uiToolingPreview)
implementation(libs.androidx.activity.compose)
@@ -57,10 +96,29 @@ kotlin {
jvmMain.dependencies {
implementation(compose.desktop.currentOs)
implementation(libs.kotlinx.coroutinesSwing)
implementation(libs.onnxruntime)
implementation(libs.jackson.databind)
}
jvmTest.dependencies {
implementation(libs.kotlinx.coroutinesTest)
}
}
}
tasks.register<JavaExec>("ttsSample") {
group = "verification"
description = "Generate plain English/Russian WAV samples using the local Supertonic CPU model."
dependsOn("jvmMainClasses")
mainClass.set("net.sergeych.toread.tts.TtsSampleKt")
classpath = configurations["jvmRuntimeClasspath"] + kotlin.targets["jvm"].compilations["main"].output.allOutputs
workingDir = rootProject.projectDir
args(layout.buildDirectory.dir("tts-samples").get().asFile.absolutePath)
if (providers.gradleProperty("ttsPlay").orNull == "true") args("--play")
if (providers.gradleProperty("ttsStress").orNull == "true") args("--stress")
if (providers.gradleProperty("ttsRuAccent").orNull == "true") args("--ruaccent")
if (providers.gradleProperty("ttsSilero").orNull == "true") args("--silero")
}
android {
namespace = "net.sergeych.toread"
compileSdk = libs.versions.android.compileSdk.get().toInt()
@@ -69,8 +127,8 @@ android {
applicationId = "net.sergeych.toread"
minSdk = libs.versions.android.minSdk.get().toInt()
targetSdk = libs.versions.android.targetSdk.get().toInt()
versionCode = 1
versionName = "1.0"
versionCode = appVersionCode
versionName = appVersionName
}
packaging {
resources {
@@ -99,8 +157,10 @@ compose.desktop {
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "net.sergeych.toread"
packageVersion = "1.0.0"
packageName = "To Read"
packageVersion = appVersionDisplay
fileAssociation("application/x-fictionbook+xml", "fb2", "FictionBook document")
fileAssociation("application/zip", "fb2.zip", "Zipped FictionBook document")
macOS {
iconFile.set(project.file("src/jvmMain/resources/icons/icon.icns"))
@@ -111,6 +171,7 @@ compose.desktop {
menu = true
}
linux {
packageName = "to-read"
iconFile.set(project.file("src/jvmMain/resources/icons/icon.png"))
shortcut = true
}
@@ -24,6 +24,7 @@
android:theme="@style/AppTheme">
<activity
android:exported="true"
android:launchMode="singleTop"
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@@ -39,6 +40,8 @@
<data android:scheme="content"/>
<data android:scheme="file"/>
<data android:mimeType="application/x-fictionbook+xml"/>
<data android:mimeType="application/xml"/>
<data android:mimeType="text/xml"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
@@ -48,8 +51,12 @@
<data android:scheme="content" android:mimeType="*/*" android:pathPattern=".*\\.fb2"/>
<data android:scheme="content" android:mimeType="*/*" android:pathPattern=".*\\.fb2\\.zip"/>
<data android:scheme="content" android:mimeType="application/octet-stream" android:pathPattern=".*\\.fb2"/>
<data android:scheme="content" android:mimeType="application/octet-stream" android:pathPattern=".*\\.fb2\\.zip"/>
<data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*\\.fb2"/>
<data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*\\.fb2\\.zip"/>
<data android:scheme="file" android:mimeType="application/octet-stream" android:pathPattern=".*\\.fb2"/>
<data android:scheme="file" android:mimeType="application/octet-stream" android:pathPattern=".*\\.fb2\\.zip"/>
</intent-filter>
</activity>
<provider
@@ -102,6 +102,14 @@ actual fun defaultLibraryScanPath(): String? =
?: appContext.filesDir.absolutePath
fun rememberPlatformOpenBookIntent(intent: Intent?) {
val uri = intent
?.takeIf { it.action == Intent.ACTION_VIEW }
?.data
if (uri != null && intent.flags and Intent.FLAG_GRANT_READ_URI_PERMISSION != 0) {
runCatching {
appContext.contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
}
pendingOpenBookUri = intent
?.takeIf { it.action == Intent.ACTION_VIEW }
?.data
@@ -111,9 +119,21 @@ actual suspend fun loadPlatformOpenBookRequest(): PlatformOpenBookRequest? = wit
val uri = pendingOpenBookUri ?: return@withContext null
pendingOpenBookUri = null
val bytes = readStorageUriBytes(uri.toString()) ?: return@withContext null
val displayName = displayNameFor(uri)
val sizeBytes = sizeBytesFor(uri) ?: bytes.size.toLong()
val importResult = openLibraryDatabase().useLibrary { db ->
LibraryScanner(db, ::appendLibraryLog).importExternalFileAndGetResult(
bytes = bytes,
displayName = displayName,
storageUri = uri.toString(),
sizeBytes = sizeBytes,
lastModifiedMillis = null,
)
}
appendLibraryLog("platform open imported=${importResult.imported} fileId=${importResult.fileId} uri=$uri")
PlatformOpenBookRequest(
id = "platform:${uri}",
displayName = displayNameFor(uri),
id = importResult.fileId,
displayName = displayName,
bytes = bytes,
)
}
@@ -198,7 +218,7 @@ actual suspend fun scanLibrarySubtree(
scanLibraryContentTree(Uri.parse(path), ::emitProgress)
} else {
if (path.requiresExternalFileAccess() && directoryChooser?.ensureExternalFileAccess() != true) {
error("All files access is required to scan $path.")
error(strings.allFilesAccessRequired(path))
}
openLibraryDatabase().useLibrary { db ->
val summary = LibraryScanner(db, ::appendLibraryLog).scanSubtree(File(path)) {
@@ -363,7 +383,7 @@ actual suspend fun shareLibraryBookFile(fileId: String): Boolean = withContext(D
putExtra(Intent.EXTRA_STREAM, uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
val chooser = Intent.createChooser(intent, "Share book").apply {
val chooser = Intent.createChooser(intent, strings.shareBook).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
@@ -372,14 +392,29 @@ actual suspend fun shareLibraryBookFile(fileId: String): Boolean = withContext(D
}.getOrDefault(false)
}
actual suspend fun viewLibraryBookFile(fileId: String): Boolean = false
actual suspend fun viewLibraryBookFile(folder: String, fileName: String): Boolean = false
actual fun openPlatformUrl(url: String): Boolean =
runCatching {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
appContext.startActivity(intent)
true
}.getOrDefault(false)
actual suspend fun loadBookInfoExtras(fileId: String): BookInfoExtras = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
val file = db.files.get(fileId) ?: return@useLibrary BookInfoExtras()
val clusterId = file.bodyClusterId ?: return@useLibrary BookInfoExtras()
val sourceFileName = file.originalFilename ?: file.storageUri?.substringAfterLast('/')
val clusterId = file.bodyClusterId ?: return@useLibrary BookInfoExtras(
sourceFileName = sourceFileName,
sourceFilePath = file.storageUri,
)
val readingPosition = db.readingStates.getForBodyCluster(clusterId)?.anchor?.formatHintsJson?.toReadingPosition()
BookInfoExtras(
sourceFileName = sourceFileName,
sourceFilePath = file.storageUri,
bookmarks = db.bookmarks.listForBodyCluster(clusterId).map {
BookmarkInfo(
title = it.title,
@@ -426,6 +461,42 @@ actual suspend fun saveThemeMode(mode: ThemeMode) = withContext(Dispatchers.IO)
}
}
actual suspend fun loadReaderFontSettings(): ReaderFontSettings = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
readerFontSettingsFromStorageValue(db.getAppFlag(ReaderFontSettingsFlag))
}
}
actual suspend fun saveReaderFontSettings(settings: ReaderFontSettings) = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.setAppFlag(ReaderFontSettingsFlag, settings.toStorageValue())
}
}
actual suspend fun loadReaderFullScreen(): Boolean = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.getAppFlag(ReaderFullScreenFlag)?.toBooleanStrictOrNull() ?: false
}
}
actual suspend fun saveReaderFullScreen(fullScreen: Boolean) = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.setAppFlag(ReaderFullScreenFlag, fullScreen.toString())
}
}
actual suspend fun loadReaderPagingOnly(): Boolean = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.getAppFlag(ReaderPagingOnlyFlag)?.toBooleanStrictOrNull() ?: false
}
}
actual suspend fun saveReaderPagingOnly(enabled: Boolean) = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.setAppFlag(ReaderPagingOnlyFlag, enabled.toString())
}
}
actual suspend fun loadScanDownloadsAutomatically(): Boolean = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.getAppFlag(ScanDownloadsAutomaticallyFlag)?.toBooleanStrictOrNull() ?: true
@@ -438,6 +509,41 @@ actual suspend fun saveScanDownloadsAutomatically(enabled: Boolean) = withContex
}
}
internal actual suspend fun loadLibraryFilter(): LibraryFilter = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
val saved = db.getAppFlag(LibraryFilterFlag)
when {
saved == null -> {
appendLibraryLog("warning library filter was not saved; using ${LibraryFilter.MyLibrary.name}")
LibraryFilter.MyLibrary
}
else -> runCatching { LibraryFilter.valueOf(saved) }
.getOrElse {
appendLibraryLog("warning invalid saved library filter '$saved'; using ${LibraryFilter.MyLibrary.name}")
LibraryFilter.MyLibrary
}
}
}
}
internal actual suspend fun saveLibraryFilter(filter: LibraryFilter) = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.setAppFlag(LibraryFilterFlag, filter.name)
}
}
actual suspend fun loadAppLocaleTag(): String? = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.getAppFlag(AppLocaleTagFlag)?.takeIf { it.isNotBlank() }
}
}
actual suspend fun saveAppLocaleTag(localeTag: String?) = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.setAppFlag(AppLocaleTagFlag, localeTag?.takeIf { it.isNotBlank() })
}
}
actual suspend fun loadDownloadsWasScanned(): Boolean = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.getAppFlag(DownloadsWasScannedFlag)?.toBooleanStrictOrNull()
@@ -527,7 +633,7 @@ private fun scanLibraryContentTree(
onProgress(LibraryScanProgress(scanned, imported, skipped, failed, document.name))
val result = runCatching {
val bytes = appContext.contentResolver.openInputStream(document.uri)?.use { it.readBytes() }
?: error("Could not read ${document.name}")
?: error(strings.couldNotRead(document.name))
scanner.importExternalFile(
bytes = bytes,
displayName = document.name,
@@ -659,6 +765,11 @@ private fun displayNameFor(uri: Uri): String =
if (cursor.moveToFirst()) cursor.getString(0) else null
} ?: uri.lastPathSegment ?: uri.toString()
private fun sizeBytesFor(uri: Uri): Long? =
appContext.contentResolver.query(uri, arrayOf(OpenableColumns.SIZE), null, null, null)?.use { cursor ->
if (cursor.moveToFirst() && !cursor.isNull(0)) cursor.getLong(0) else null
}
private fun LibraryScanSummary.toLibraryScanProgress(): LibraryScanProgress =
LibraryScanProgress(
scannedFiles = scannedFiles,
@@ -765,14 +876,37 @@ private fun libraryLogFile(): File =
File(appContext.filesDir, "logs/toread.log")
private fun ReadingPosition.toFormatHintsJson(): String =
"""{"firstVisibleItemIndex":$itemIndex,"firstVisibleItemScrollOffset":$scrollOffset}"""
buildString {
append("""{"firstVisibleItemIndex":$itemIndex,"firstVisibleItemScrollOffset":$scrollOffset""")
readAloudSentenceIndex?.let { append(""","readAloudSentenceIndex":$it""") }
if (backStack.isNotEmpty()) {
append(""","backStack":[""")
append(backStack.take(MaxStoredReadingBackStack).joinToString(",") { it.toFormatHintsJson() })
append("]")
}
append("}")
}
private fun String.toReadingPosition(): ReadingPosition? {
val position = toReadingPositionObject() ?: return null
val backStackJson = Regex(""""backStack"\s*:\s*\[(.*)]""").find(this)?.groupValues?.getOrNull(1)
val backStack = backStackJson
?.let { ReadingPositionObjectRegex.findAll(it).mapNotNull { match -> match.value.toReadingPositionObject() }.toList() }
.orEmpty()
.take(MaxStoredReadingBackStack)
return position.copy(backStack = backStack)
}
private fun String.toReadingPositionObject(): ReadingPosition? {
val index = Regex(""""firstVisibleItemIndex"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
val offset = Regex(""""firstVisibleItemScrollOffset"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
return if (index != null && offset != null) ReadingPosition(index, offset) else null
val sentenceIndex = Regex(""""readAloudSentenceIndex"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
return if (index != null && offset != null) ReadingPosition(index, offset, sentenceIndex) else null
}
private val ReadingPositionObjectRegex = Regex("""[{][^{}]*[}]""")
private const val MaxStoredReadingBackStack = 10
private fun String.toSearchPrefixes(): List<String> =
SearchPrefixRegex.findAll(lowercase()).map { it.value }.distinct().toList()
@@ -780,5 +914,10 @@ private val SearchPrefixRegex = Regex("""[\p{L}\p{N}]+""")
private const val ActiveReadingFileIdFlag = "active_reading_file_id"
private const val ThemeModeFlag = "theme_mode"
private const val ReaderFontSettingsFlag = "reader_font_settings"
private const val ReaderFullScreenFlag = "reader_full_screen"
private const val ReaderPagingOnlyFlag = "reader_paging_only"
private const val ScanDownloadsAutomaticallyFlag = "scan_downloads_automatically"
private const val LibraryFilterFlag = "library_filter"
private const val AppLocaleTagFlag = "app_locale_tag"
private const val DownloadsWasScannedFlag = "downloads_was_scanned"
@@ -0,0 +1,6 @@
package net.sergeych.toread
import java.util.Locale
internal actual fun platformLocaleTags(): List<String> =
listOf(Locale.getDefault().toLanguageTag())
@@ -4,6 +4,7 @@ import android.Manifest
import android.app.AlertDialog
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.Bundle
@@ -23,6 +24,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.launch
@@ -55,7 +58,7 @@ class MainActivity : ComponentActivity(), AndroidLibraryDirectoryChooser {
}
notificationPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) {}
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, true)
configureEdgeToEdgeBookWindow()
initToreadPlatform(this, this)
rememberPlatformOpenBookIntent(intent)
@@ -67,6 +70,11 @@ class MainActivity : ComponentActivity(), AndroidLibraryDirectoryChooser {
requestNotificationPermissionIfNeeded()
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) hideNavigationBar()
}
override suspend fun chooseDirectory(): String? {
val result = CompletableDeferred<String?>()
runOnUiThread {
@@ -82,8 +90,8 @@ class MainActivity : ComponentActivity(), AndroidLibraryDirectoryChooser {
private fun showDirectoryChoice(result: CompletableDeferred<String?>) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
AlertDialog.Builder(this)
.setTitle("Choose library folder")
.setItems(arrayOf("Downloads", "Other folder...")) { _, which ->
.setTitle(strings.chooseLibraryFolder)
.setItems(arrayOf(strings.downloads, strings.otherFolder)) { _, which ->
when (which) {
0 -> chooseDownloadsDirectory(result)
else -> launchSystemDirectoryPicker(result)
@@ -175,6 +183,27 @@ class MainActivity : ComponentActivity(), AndroidLibraryDirectoryChooser {
notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
private fun configureEdgeToEdgeBookWindow() {
WindowCompat.setDecorFitsSystemWindows(window, false)
window.navigationBarColor = Color.TRANSPARENT
window.statusBarColor = Color.TRANSPARENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.navigationBarDividerColor = Color.TRANSPARENT
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
window.isNavigationBarContrastEnforced = false
window.isStatusBarContrastEnforced = false
}
hideNavigationBar()
}
private fun hideNavigationBar() {
WindowCompat.getInsetsController(window, window.decorView).apply {
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
hide(WindowInsetsCompat.Type.navigationBars())
}
}
private fun downloadsPath(): String =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)?.absolutePath
?: Environment.getExternalStorageDirectory().absolutePath
@@ -185,7 +214,7 @@ private fun AndroidAppWindow(content: @Composable () -> Unit) {
androidx.compose.foundation.layout.Box(
modifier = Modifier
.fillMaxSize()
.windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Vertical)),
.windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Top)),
) {
content()
}
@@ -15,10 +15,12 @@ import android.os.Bundle
import android.os.IBinder
import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import java.util.Locale
import java.util.concurrent.atomic.AtomicLong
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
@@ -26,15 +28,17 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
actual object ReadAloudPlatform {
actual val isSupported: Boolean = true
actual val state: StateFlow<ReadAloudState> = AndroidReadAloudEngine.state
actual val settingsState: StateFlow<ReadAloudSettingsState> = AndroidReadAloudEngine.settingsState
actual fun prepare(bookTitle: String, sentences: List<ReadAloudSentence>, startIndex: Int) {
actual fun prepare(fileId: String, bookTitle: String, sentences: List<ReadAloudSentence>, startIndex: Int) {
val context = androidAppContext() ?: return
AndroidReadAloudEngine.prepare(context, bookTitle, sentences, startIndex)
AndroidReadAloudEngine.prepare(context, fileId, bookTitle, sentences, startIndex)
ReadAloudService.start(context)
}
@@ -69,7 +73,6 @@ actual object ReadAloudPlatform {
}
private object AndroidReadAloudEngine {
private const val UtterancePrefix = "read-aloud-"
private const val BeforePausePrefix = "read-aloud-before-"
private const val SpeakPrefix = "read-aloud-speak-"
private const val AfterPausePrefix = "read-aloud-after-"
@@ -77,24 +80,45 @@ private object AndroidReadAloudEngine {
private const val SelectedEngineKey = "selected_engine"
private const val EngineChoiceMadeKey = "engine_choice_made"
private const val SelectedVoiceKey = "selected_voice"
private const val TextReplacementsKey = "text_replacements"
private const val EmptyTextReplacementsJson = "[]"
private val RussianLocale = Locale.forLanguageTag("ru-RU")
private val mutableState = MutableStateFlow(ReadAloudState())
val state: StateFlow<ReadAloudState> = mutableState
private val mutableSettingsState = MutableStateFlow(ReadAloudSettingsState())
val settingsState: StateFlow<ReadAloudSettingsState> = mutableSettingsState
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private val positionSaveMutex = Mutex()
private val positionSaveVersion = AtomicLong()
private var tts: TextToSpeech? = null
private var ttsReady = false
private var ttsEngineId: String? = null
private var engineProbe: TextToSpeech? = null
private var voiceProbe: TextToSpeech? = null
private var ttsGeneration = 0L
private var engineProbeGeneration = 0L
private var voiceProbeGeneration = 0L
private var shouldSpeakWhenReady = false
private var fileId: String? = null
private var bookTitle: String = ""
private var sentences: List<ReadAloudSentence> = emptyList()
private var currentIndex: Int = 0
private var playbackGeneration = 0L
private var blockNumber = 0L
private var activeUtteranceId: String? = null
private var blocks: Iterator<ReadAloudBlock>? = null
private val queuedBlocks = linkedMapOf<String, ReadAloudBlock>()
fun prepare(context: Context, title: String, queue: List<ReadAloudSentence>, startIndex: Int) {
private fun beforeId(id: String) = "$BeforePausePrefix$id"
private fun afterId(id: String) = "$AfterPausePrefix$id"
@Synchronized
fun prepare(context: Context, fileId: String, title: String, queue: List<ReadAloudSentence>, startIndex: Int) {
shouldSpeakWhenReady = false
cancelQueue()
this.fileId = fileId
bookTitle = title
sentences = queue
currentIndex = startIndex.coerceIn(queue.indices.takeIf { queue.isNotEmpty() } ?: 0..0)
@@ -106,76 +130,94 @@ private object AndroidReadAloudEngine {
ensureTts(context.applicationContext)
}
@Synchronized
fun play(context: Context) {
if (sentences.isEmpty()) return
ensureTts(context.applicationContext)
if (!ttsReady) {
shouldSpeakWhenReady = true
mutableState.value = mutableState.value.copy(active = true, playing = true, sentenceIndex = currentIndex)
mutableState.value = mutableState.value.copy(
active = true,
playing = true,
buffering = true,
sentenceIndex = sentences.getOrNull(currentIndex)?.index,
)
return
}
speakCurrent()
}
@Synchronized
fun stop() {
shouldSpeakWhenReady = false
tts?.stop()
cancelQueue()
mutableState.value = ReadAloudState()
}
@Synchronized
fun skip(delta: Int) {
if (sentences.isEmpty()) return
val wasPlaying = mutableState.value.playing
currentIndex = (currentIndex + delta).coerceIn(sentences.indices)
val sentence = sentences.getOrNull(currentIndex)
mutableState.value = mutableState.value.copy(
active = true,
playing = wasPlaying,
sentenceIndex = currentIndex,
sentenceIndex = sentence?.index ?: currentIndex,
)
sentence?.let(::saveSentencePosition)
if (wasPlaying && ttsReady) {
speakCurrent()
}
}
@Synchronized
fun refreshSettings(context: Context) {
val appContext = context.applicationContext
mutableSettingsState.value = mutableSettingsState.value.copy(loading = true, message = null)
mutableSettingsState.value = mutableSettingsState.value.copy(
textReplacements = userTextReplacements(appContext),
loading = true,
message = null,
)
val generation = ++engineProbeGeneration
engineProbe?.shutdown()
var probe: TextToSpeech? = null
probe = TextToSpeech(appContext) { status ->
if (status != TextToSpeech.SUCCESS) {
mutableSettingsState.value = mutableSettingsState.value.copy(
loading = false,
message = "Could not load Android TTS engines.",
)
probe?.shutdown()
if (engineProbe === probe) engineProbe = null
return@TextToSpeech
}
val engines = probe?.engines.orEmpty()
.map { ReadAloudEngineOption(id = it.name, label = it.label ?: it.name) }
.sortedBy { it.label.lowercase() }
var selectedEngine = selectedEngineId(appContext).takeIf { saved ->
saved != null && engines.any { it.id == saved }
}
if (selectedEngine == null && !appContext.readAloudPrefs().getBoolean(EngineChoiceMadeKey, false)) {
selectedEngine = engines.firstOrNull { it.isRhVoice() }?.id
if (selectedEngine != null) {
appContext.readAloudPrefs().edit().putString(SelectedEngineKey, selectedEngine).apply()
}
}
mutableSettingsState.value = mutableSettingsState.value.copy(
engines = engines,
selectedEngineId = selectedEngine,
)
probe?.shutdown()
if (engineProbe === probe) engineProbe = null
refreshVoices(appContext, selectedEngine)
engineProbe = TextToSpeech(appContext) { status ->
// Dispatch even if onInit fires synchronously inside the constructor.
scope.launch(Dispatchers.Main) { handleEngineProbeInit(appContext, generation, status) }
}
engineProbe = probe
}
@Synchronized
private fun handleEngineProbeInit(context: Context, generation: Long, status: Int) {
if (generation != engineProbeGeneration) return
val probe = engineProbe ?: return
if (status != TextToSpeech.SUCCESS) {
mutableSettingsState.value = mutableSettingsState.value.copy(
loading = false, message = strings.couldNotLoadAndroidTtsEngines,
)
probe.shutdown()
engineProbe = null
return
}
val engines = probe.engines.orEmpty()
.map { ReadAloudEngineOption(id = it.name, label = it.label ?: it.name) }
.sortedBy { it.label.lowercase() }
var selectedEngine = selectedEngineId(context)
if (selectedEngine == null && !context.readAloudPrefs().getBoolean(EngineChoiceMadeKey, false)) {
selectedEngine = engines.firstOrNull { it.isRhVoice() }?.id
if (selectedEngine != null) {
context.readAloudPrefs().edit().putString(SelectedEngineKey, selectedEngine).apply()
}
}
mutableSettingsState.value = mutableSettingsState.value.copy(
engines = engines, selectedEngineId = selectedEngine,
)
probe.shutdown()
engineProbe = null
refreshVoices(context, selectedEngine)
}
@Synchronized
fun selectEngine(context: Context, engineId: String?) {
val appContext = context.applicationContext
appContext.readAloudPrefs()
@@ -184,6 +226,7 @@ private object AndroidReadAloudEngine {
.putStringOrRemove(SelectedEngineKey, engineId)
.remove(SelectedVoiceKey)
.apply()
val wasPlaying = mutableState.value.playing
resetTts()
mutableSettingsState.value = mutableSettingsState.value.copy(
selectedEngineId = engineId,
@@ -193,8 +236,10 @@ private object AndroidReadAloudEngine {
message = null,
)
refreshVoices(appContext, engineId)
if (wasPlaying) play(appContext)
}
@Synchronized
fun selectVoice(context: Context, voiceId: String?) {
val appContext = context.applicationContext
appContext.readAloudPrefs()
@@ -214,25 +259,35 @@ private object AndroidReadAloudEngine {
if (tts != null && ttsEngineId == selectedEngine) return
resetTts()
ttsEngineId = selectedEngine
val generation = ttsGeneration
tts = if (selectedEngine == null) {
TextToSpeech(appContext) { status ->
handleTtsInit(appContext, status)
scope.launch(Dispatchers.Main) { handleTtsInit(appContext, generation, status) }
}
} else {
TextToSpeech(
appContext,
{ status -> handleTtsInit(appContext, status) },
{ status -> scope.launch(Dispatchers.Main) { handleTtsInit(appContext, generation, status) } },
selectedEngine,
)
}
}
private fun handleTtsInit(context: Context, status: Int) {
@Synchronized
private fun handleTtsInit(context: Context, generation: Long, status: Int) {
if (generation != ttsGeneration) return
ttsReady = status == TextToSpeech.SUCCESS
if (ttsReady) {
applyTtsConfiguration(context)
Log.i("YereedTts", "Initialized requested engine=${ttsEngineId ?: "system default"}, voice=${tts?.voice?.name}")
tts?.setOnUtteranceProgressListener(object : UtteranceProgressListener() {
override fun onStart(utteranceId: String?) = Unit
override fun onStart(utteranceId: String?) {
handleUtteranceStart(utteranceId)
}
override fun onRangeStart(utteranceId: String?, start: Int, end: Int, frame: Int) {
handleUtteranceRange(utteranceId, start)
}
override fun onDone(utteranceId: String?) {
handleUtteranceDone(utteranceId)
@@ -240,11 +295,11 @@ private object AndroidReadAloudEngine {
@Deprecated("Deprecated in Java")
override fun onError(utteranceId: String?) {
stop()
handleUtteranceError(utteranceId)
}
override fun onError(utteranceId: String?, errorCode: Int) {
stop()
handleUtteranceError(utteranceId)
}
})
if (shouldSpeakWhenReady) {
@@ -256,77 +311,134 @@ private object AndroidReadAloudEngine {
}
}
private fun cancelQueue() {
// Invalidate callbacks before stopping: a late event must not affect the new queue.
playbackGeneration++
blocks = null
activeUtteranceId = null
queuedBlocks.clear()
tts?.stop()
}
private fun speakCurrent() {
val sentence = sentences.getOrNull(currentIndex) ?: run {
stop()
return
}
cancelQueue()
mutableState.value = ReadAloudState(active = true, playing = true, sentenceIndex = sentence.index)
val params = Bundle().apply {
putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "$SpeakPrefix$currentIndex")
}
val queueMode = if (sentence.pauseBeforeMillis > 0) {
tts?.playSilentUtterance(sentence.pauseBeforeMillis, TextToSpeech.QUEUE_FLUSH, "$BeforePausePrefix$currentIndex")
TextToSpeech.QUEUE_ADD
} else {
TextToSpeech.QUEUE_FLUSH
}
tts?.speak(sentence.spokenText, queueMode, params, "$SpeakPrefix$currentIndex")
if (sentence.pauseAfterMillis > 0) {
tts?.playSilentUtterance(sentence.pauseAfterMillis, TextToSpeech.QUEUE_ADD, "$AfterPausePrefix$currentIndex")
}
saveSentencePosition(sentence)
blocks = readAloudBlocks(
sentences, currentIndex, minOf(4_000, TextToSpeech.getMaxSpeechInputLength()),
).iterator()
fillQueue()
}
private fun handleUtteranceDone(utteranceId: String?) {
private fun fillQueue() {
val engine = tts ?: return
val remaining = blocks ?: return
// Keep the current block and two successors submitted, before playback ends.
while (queuedBlocks.size < 3 && remaining.hasNext()) {
val block = remaining.next()
val id = "$SpeakPrefix$playbackGeneration-${blockNumber++}"
queuedBlocks[id] = block
val beforeResult = if (block.pauseBeforeMillis > 0) {
engine.playSilentUtterance(block.pauseBeforeMillis, TextToSpeech.QUEUE_ADD, beforeId(id))
} else TextToSpeech.SUCCESS
val speechResult = engine.speak(block.text, TextToSpeech.QUEUE_ADD, Bundle(), id)
val afterResult = if (block.pauseAfterMillis > 0) {
engine.playSilentUtterance(block.pauseAfterMillis, TextToSpeech.QUEUE_ADD, afterId(id))
} else TextToSpeech.SUCCESS
if (beforeResult == TextToSpeech.ERROR || speechResult == TextToSpeech.ERROR ||
afterResult == TextToSpeech.ERROR) {
stop()
return
}
}
if (queuedBlocks.isEmpty()) stop()
}
private fun updatePosition(position: Int) {
if (currentIndex == position && mutableState.value.sentenceIndex == sentences[position].index) return
currentIndex = position
val sentence = sentences[position]
mutableState.value = mutableState.value.copy(sentenceIndex = sentence.index)
saveSentencePosition(sentence)
}
@Synchronized
private fun handleUtteranceStart(utteranceId: String?) {
if (!mutableState.value.playing) return
val speakIndex = utteranceId?.removePrefix(SpeakPrefix)?.takeIf { it != utteranceId }?.toIntOrNull()
val afterIndex = utteranceId?.removePrefix(AfterPausePrefix)?.takeIf { it != utteranceId }?.toIntOrNull()
val finishedIndex = afterIndex ?: speakIndex ?: return
if (finishedIndex != currentIndex) return
val currentSentence = sentences.getOrNull(currentIndex) ?: run {
stop()
return
val block = queuedBlocks[utteranceId] ?: return
activeUtteranceId = utteranceId
updatePosition(block.ranges.first().position)
}
@Synchronized
private fun handleUtteranceRange(utteranceId: String?, offset: Int) {
if (!mutableState.value.playing || utteranceId != activeUtteranceId) return
val block = queuedBlocks[utteranceId] ?: return
updatePosition(block.positionAt(offset))
}
@Synchronized
private fun handleUtteranceDone(utteranceId: String?) {
if (!mutableState.value.playing || utteranceId == null) return
val isAfter = utteranceId.startsWith(AfterPausePrefix)
val id = if (isAfter) utteranceId.removePrefix(AfterPausePrefix) else utteranceId
val block = queuedBlocks[id] ?: return
if (!isAfter) {
if (activeUtteranceId == id) updatePosition(block.ranges.last().position)
if (block.pauseAfterMillis > 0) return
}
if (speakIndex != null && currentSentence.pauseAfterMillis > 0) {
return
}
if (currentIndex < sentences.lastIndex) {
currentIndex += 1
speakCurrent()
} else {
stop()
queuedBlocks.remove(id)
fillQueue()
}
@Synchronized
private fun handleUtteranceError(utteranceId: String?) {
val id = utteranceId?.removePrefix(BeforePausePrefix)?.removePrefix(AfterPausePrefix)
if (queuedBlocks.containsKey(id)) stop()
}
fun notificationTitle(): String = bookTitle.ifBlank { strings.readAloud }
private fun saveSentencePosition(sentence: ReadAloudSentence) {
val activeFileId = fileId ?: return
val saveVersion = positionSaveVersion.incrementAndGet()
scope.launch {
positionSaveMutex.withLock {
if (saveVersion != positionSaveVersion.get()) return@withLock
runCatching {
saveLibraryReadingPosition(activeFileId, ReadingPosition(sentence.itemIndex, 0, sentence.index))
}
}
}
}
private fun refreshVoices(context: Context, engineId: String?) {
val generation = ++voiceProbeGeneration
voiceProbe?.shutdown()
var probe: TextToSpeech? = null
probe = if (engineId == null) {
TextToSpeech(context) { status ->
handleVoiceProbeInit(context, probe, status)
}
} else {
TextToSpeech(
context,
{ status -> handleVoiceProbeInit(context, probe, status) },
engineId,
)
}
voiceProbe = probe
voiceProbe = TextToSpeech(context, { status ->
scope.launch(Dispatchers.Main) { handleVoiceProbeInit(context, generation, status) }
}, engineId)
}
private fun handleVoiceProbeInit(context: Context, probe: TextToSpeech?, status: Int) {
@Synchronized
private fun handleVoiceProbeInit(context: Context, generation: Long, status: Int) {
if (generation != voiceProbeGeneration) return
val probe = voiceProbe ?: return
if (status != TextToSpeech.SUCCESS) {
mutableSettingsState.value = mutableSettingsState.value.copy(
loading = false,
message = "Could not load voices for the selected TTS engine.",
message = strings.couldNotLoadSelectedTtsVoices,
)
probe?.shutdown()
probe.shutdown()
if (voiceProbe === probe) voiceProbe = null
return
}
val voices = probe?.voices.orEmpty()
val voices = probe.voices.orEmpty()
.mapNotNull { voice ->
val locale = voice.locale ?: return@mapNotNull null
ReadAloudVoiceOption(
@@ -349,12 +461,12 @@ private object AndroidReadAloudEngine {
voices = voices,
selectedVoiceId = savedVoice,
loading = false,
message = if (voices.isEmpty()) "No offline voices reported by the selected TTS engine." else null,
message = if (voices.isEmpty()) strings.noOfflineVoices else null,
)
if (savedVoice == null && selectedVoiceId(context) != null) {
context.readAloudPrefs().edit().remove(SelectedVoiceKey).apply()
}
probe?.shutdown()
probe.shutdown()
if (voiceProbe === probe) voiceProbe = null
}
@@ -371,10 +483,11 @@ private object AndroidReadAloudEngine {
}
private fun resetTts() {
ttsGeneration++
shouldSpeakWhenReady = false
ttsReady = false
ttsEngineId = null
tts?.stop()
cancelQueue()
tts?.shutdown()
tts = null
}
@@ -385,6 +498,14 @@ private object AndroidReadAloudEngine {
private fun selectedVoiceId(context: Context): String? =
context.readAloudPrefs().getString(SelectedVoiceKey, null)
private fun userTextReplacements(context: Context): List<ReadAloudTextReplacement> {
val prefs = context.readAloudPrefs()
if (!prefs.contains(TextReplacementsKey)) {
prefs.edit().putString(TextReplacementsKey, EmptyTextReplacementsJson).apply()
}
return emptyList()
}
private fun Context.readAloudPrefs() =
getSharedPreferences(SettingsPrefs, Context.MODE_PRIVATE)
@@ -443,20 +564,20 @@ class ReadAloudService : Service() {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
val playStopAction = if (state.playing) ActionStop else ActionPlay
val playStopTitle = if (state.playing) "Stop" else "Play"
val playStopTitle = if (state.playing) strings.stop else strings.play
val playStopIcon = if (state.playing) android.R.drawable.ic_media_pause else android.R.drawable.ic_media_play
return NotificationCompat.Builder(this, ChannelId)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Read aloud")
.setContentText("Reading in the background")
.setContentTitle(AndroidReadAloudEngine.notificationTitle())
.setContentText(strings.readingInBackground)
.setContentIntent(contentIntent)
.setOngoing(state.playing)
.setOnlyAlertOnce(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.addAction(android.R.drawable.ic_media_previous, "Previous", serviceIntent(ActionBack))
.addAction(android.R.drawable.ic_media_previous, strings.previous, serviceIntent(ActionBack))
.addAction(playStopIcon, playStopTitle, serviceIntent(playStopAction))
.addAction(android.R.drawable.ic_media_next, "Next", serviceIntent(ActionForward))
.addAction(android.R.drawable.ic_media_next, strings.next, serviceIntent(ActionForward))
.build()
}
@@ -481,7 +602,7 @@ class ReadAloudService : Service() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
val channel = NotificationChannel(
ChannelId,
"Read aloud",
strings.readAloud,
NotificationManager.IMPORTANCE_LOW,
)
getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Читать</string>
</resources>
@@ -1,3 +1,3 @@
<resources>
<string name="app_name">toread</string>
<string name="app_name">To Read</string>
</resources>
@@ -13,6 +13,7 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
@@ -24,12 +25,16 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.compose.LocalLifecycleOwner
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
private const val DefaultToastDurationMillis = 1_600L
private const val DeleteUndoDurationMillis = 5_000L
private const val ScanResultToastDurationMillis = 5_000L
private const val BackgroundLibraryPageSize = 50
private data class AppToastData(
@@ -46,11 +51,17 @@ private data class PendingLibraryDelete(
val restore: () -> Unit,
)
internal data class LibraryItemRefreshRequest(
val id: Long,
val fileId: String,
)
@Composable
@Preview
fun App() {
var themeMode by remember { mutableStateOf(ThemeMode.SYSTEM) }
var systemDark by remember { mutableStateOf(isPlatformDarkTheme()) }
var localePreferenceLoaded by remember { mutableStateOf(false) }
var toast by remember { mutableStateOf<AppToastData?>(null) }
var nextToastId by remember { mutableStateOf(0L) }
val scope = rememberCoroutineScope()
@@ -78,6 +89,13 @@ fun App() {
themeMode = loadThemeMode()
}
LaunchedEffect(Unit) {
selectedAppLocaleTag = loadAppLocaleTag()?.takeIf { saved ->
availableAppLanguages.any { it.localeTag == saved }
}
localePreferenceLoaded = true
}
LaunchedEffect(toast?.id) {
val current = toast ?: return@LaunchedEffect
delay(current.durationMillis)
@@ -87,21 +105,27 @@ fun App() {
}
MaterialTheme(colorScheme = if (useDark) darkReaderColorScheme() else lightReaderColorScheme()) {
Box(Modifier.fillMaxSize()) {
Surface(Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
BookReaderApp(
onThemeToggle = {
val next = themeMode.next()
themeMode = next
showToast("Theme: ${next.displayName}")
scope.launch {
saveThemeMode(next)
}
},
onShowToast = ::showToast,
)
CompositionLocalProvider(LocalReaderThemeIsDark provides useDark) {
Box(Modifier.fillMaxSize()) {
Surface(Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
if (localePreferenceLoaded) {
BookReaderApp(
onThemeToggle = {
val next = themeMode.next()
themeMode = next
showToast(strings.themeChanged(next))
scope.launch {
saveThemeMode(next)
}
},
onShowToast = ::showToast,
)
} else {
LoadingScreen(strings.loadingOpeningBook)
}
}
AppToast(toast, modifier = Modifier.align(Alignment.BottomCenter))
}
AppToast(toast, modifier = Modifier.align(Alignment.BottomCenter))
}
}
}
@@ -153,19 +177,98 @@ private fun BookReaderApp(
) -> Unit,
) {
var state by remember { mutableStateOf<AppState>(AppState.LoadingStartup) }
var libraryBackState by remember { mutableStateOf<AppState.Library?>(null) }
var activeScan by remember { mutableStateOf<LibraryScanProgress?>(null) }
var scanJob by remember { mutableStateOf<Job?>(null) }
var pendingDelete by remember { mutableStateOf<PendingLibraryDelete?>(null) }
var pendingDeleteJob by remember { mutableStateOf<Job?>(null) }
var hiddenDeletedFileIds by remember { mutableStateOf<Set<String>>(emptySet()) }
var nextDeleteId by remember { mutableStateOf(0L) }
var nextLibraryItemRefreshId by remember { mutableStateOf(0L) }
var libraryItemRefreshRequest by remember { mutableStateOf<LibraryItemRefreshRequest?>(null) }
var imageViewer by remember { mutableStateOf<ViewedBookImage?>(null) }
var resumeGeneration by remember { mutableStateOf(0L) }
var downloadsRescanRequestGeneration by remember { mutableStateOf(0L) }
val lifecycleOwner = LocalLifecycleOwner.current
val scope = rememberCoroutineScope()
fun refreshLibraryItem(fileId: String) {
nextLibraryItemRefreshId += 1
libraryItemRefreshRequest = LibraryItemRefreshRequest(nextLibraryItemRefreshId, fileId)
}
LaunchedEffect(Unit) {
state = loadStartupState()
}
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_RESUME) {
resumeGeneration += 1
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose { lifecycleOwner.lifecycle.removeObserver(observer) }
}
LaunchedEffect(Unit) {
while (true) {
delay(500)
val requestResult = runCatching { loadPlatformOpenBookRequest() }
if (requestResult.isFailure) {
val scanPath = when (val current = state) {
is AppState.Library -> current.scanPath
is AppState.Scan -> current.scanPath
is AppState.Reader -> current.scanPath
is AppState.BookInfo -> current.scanPath
is AppState.Error, AppState.LoadingStartup -> defaultLibraryScanPath().orEmpty()
}
state = AppState.Library(
emptyList(),
scanPath,
requestResult.exceptionOrNull()?.message ?: strings.couldNotOpenLibrary,
loadLibraryFilter(),
)
continue
}
val request = requestResult.getOrNull() ?: continue
val scanPath = when (val current = state) {
is AppState.Library -> current.scanPath
is AppState.Scan -> current.scanPath
is AppState.Reader -> current.scanPath
is AppState.BookInfo -> current.scanPath
is AppState.Error, AppState.LoadingStartup -> defaultLibraryScanPath().orEmpty()
}
val nextState = runCatching {
val book = parseBookInBackground(request.bytes, request.displayName)
saveActiveReadingFileId(request.id)
AppState.Reader(
fileId = request.id,
book = book,
libraryItems = emptyList(),
scanPath = scanPath,
selectedFilter = when (val current = state) {
is AppState.Library -> current.selectedFilter
is AppState.Scan -> current.selectedFilter
is AppState.Reader -> current.selectedFilter
is AppState.BookInfo -> current.selectedFilter
is AppState.Error, AppState.LoadingStartup -> loadLibraryFilter()
},
)
}.getOrElse {
AppState.Library(emptyList(), scanPath, it.message ?: strings.couldNotOpen(request.displayName), loadLibraryFilter())
}
libraryBackState = when (val current = state) {
is AppState.Library -> current
is AppState.Scan -> AppState.Library(current.items, current.scanPath, current.message, current.selectedFilter)
is AppState.Reader, is AppState.BookInfo -> libraryBackState
is AppState.Error, AppState.LoadingStartup -> null
}
state = nextState
refreshLibraryItem(request.id)
}
}
val activeBookState = when (val current = state) {
is AppState.Reader -> current.fileId to current.libraryItems.isEmpty()
is AppState.BookInfo -> current.fileId to current.libraryItems.isEmpty()
@@ -237,15 +340,15 @@ private fun BookReaderApp(
}
onShowToast(
"Removed ${request.title}.",
"Undo",
strings.removed(request.title),
strings.undo,
{
if (pendingDelete?.id == deleteId) {
pendingDeleteJob?.cancel()
pendingDeleteJob = null
pendingDelete = null
restore()
onShowToast("Restored ${request.title}.", null, null, DefaultToastDurationMillis)
onShowToast(strings.restored(request.title), null, null, DefaultToastDurationMillis)
}
},
DeleteUndoDurationMillis,
@@ -264,12 +367,23 @@ private fun BookReaderApp(
libraryItems = current.libraryItems,
scanPath = current.scanPath,
message = current.message,
selectedFilter = current.selectedFilter,
)
is AppState.Reader -> {
scope.launch { saveActiveReadingFileId(null) }
AppState.Library(current.libraryItems, current.scanPath, current.message)
refreshLibraryItem(current.fileId)
val backState = libraryBackState?.copy(message = current.message)
?: AppState.Library(
current.libraryItems,
current.scanPath,
current.message,
current.selectedFilter,
)
libraryBackState = null
downloadsRescanRequestGeneration += 1
backState
}
is AppState.Scan -> AppState.Library(current.items, current.scanPath, current.message)
is AppState.Scan -> AppState.Library(current.items, current.scanPath, current.message, current.selectedFilter)
is AppState.Error -> AppState.LoadingStartup
is AppState.Library, AppState.LoadingStartup -> current
}
@@ -287,7 +401,23 @@ private fun BookReaderApp(
onBack = ::navigateBack,
)
fun startScan(path: String) {
fun setLibraryFilter(filter: LibraryFilter) {
state = when (val current = state) {
is AppState.Library -> current.copy(selectedFilter = filter)
is AppState.Scan -> current.copy(selectedFilter = filter)
else -> current
}
libraryBackState = libraryBackState?.copy(selectedFilter = filter)
}
fun saveAndSetLibraryFilter(filter: LibraryFilter) {
setLibraryFilter(filter)
scope.launch {
saveLibraryFilter(filter)
}
}
fun startScan(path: String, userInitiated: Boolean) {
if (scanJob?.isActive == true) return
activeScan = LibraryScanProgress(0, 0, 0, 0)
scanJob = scope.launch {
@@ -303,23 +433,38 @@ private fun BookReaderApp(
if (isDownloadsScanPath(path)) {
saveDownloadsWasScanned(true)
}
"Scanned ${it.scannedFiles}, imported ${it.importedFiles}, skipped ${it.skippedFiles}, failed ${it.failedFiles}."
strings.scanReport(it.scannedFiles, it.importedFiles, it.skippedFiles, it.failedFiles)
},
onFailure = { it.message ?: "Scan failed." },
onFailure = { it.message ?: strings.scanFailed },
)
val importedFiles = report.getOrNull()?.importedFiles
when {
importedFiles == null -> Unit
importedFiles > 0 -> {
onShowToast(
strings.newBooksAdded(importedFiles),
strings.show,
{ saveAndSetLibraryFilter(LibraryFilter.RecentlyAdded) },
ScanResultToastDurationMillis,
)
}
userInitiated -> {
onShowToast(strings.noNewBooksFound, null, null, DefaultToastDurationMillis)
}
}
scanJob = null
activeScan = null
state = when (val current = state) {
is AppState.Library -> if (report.getOrNull()?.hasLibraryChanges() == true) {
loadLibraryState(message, path)
} else {
current.copy(scanPath = path, message = message)
}
is AppState.Scan -> if (report.getOrNull()?.hasLibraryChanges() == true) {
loadLibraryState(message, path)
} else {
AppState.Library(current.items, path, message)
}
is AppState.Library -> current.copy(
scanPath = path,
message = message,
)
is AppState.Scan -> AppState.Library(
current.items,
path,
message,
selectedFilter = current.selectedFilter,
)
AppState.LoadingStartup -> loadLibraryState(message, path)
is AppState.Reader -> current.copy(message = message)
is AppState.BookInfo -> current.copy(message = message)
@@ -328,53 +473,91 @@ private fun BookReaderApp(
}
}
when (val current = state) {
AppState.LoadingStartup -> LoadingScreen("Opening book")
is AppState.Library -> LibraryScreen(
state = current,
activeScan = activeScan,
hiddenFileIds = hiddenDeletedFileIds + pendingDelete?.request?.fileId?.let(::setOf).orEmpty(),
onStateChange = { state = it },
onNavigateToScan = { state = AppState.Scan(current.items, current.scanPath, current.message) },
onStartScan = ::startScan,
onDeleteRequested = ::requestDelete,
)
is AppState.Scan -> ScanScreen(
state = current,
activeScan = activeScan,
onStateChange = { state = it },
onStartScan = { path ->
startScan(path)
state = AppState.Library(current.items, path, "Scanning...")
},
)
is AppState.Reader -> BookView(
fileId = current.fileId,
book = current.book,
onImageOpen = { imageViewer = it },
onThemeToggle = onThemeToggle,
onBookInfo = {
state = AppState.BookInfo(
fileId = current.fileId,
book = current.book,
libraryItems = current.libraryItems,
scanPath = current.scanPath,
message = current.message,
)
},
onDeleted = { message ->
state = AppState.Library(emptyList(), current.scanPath, message)
},
onDeleteRequested = ::requestDelete,
onBack = ::navigateBack,
)
is AppState.BookInfo -> BookInfoScreen(
fileId = current.fileId,
book = current.book,
onImageOpen = { imageViewer = it },
onBack = ::navigateBack,
)
is AppState.Error -> ErrorScreen(current.message, onBack = ::navigateBack)
LaunchedEffect(resumeGeneration, downloadsRescanRequestGeneration, state !is AppState.LoadingStartup) {
if (state is AppState.LoadingStartup || activeScan != null || scanJob?.isActive == true) return@LaunchedEffect
if (!loadScanDownloadsAutomatically() || !loadDownloadsWasScanned()) return@LaunchedEffect
val path = downloadsScanPath() ?: return@LaunchedEffect
state = state.withMessage(strings.scanningDownloads)
startScan(path, userInitiated = false)
}
Box(Modifier.fillMaxSize()) {
val currentLibraryState = when (val current = state) {
is AppState.Library -> current
is AppState.Reader, is AppState.BookInfo -> libraryBackState
else -> null
}
currentLibraryState?.let { libraryState ->
LibraryScreen(
state = libraryState,
activeScan = activeScan,
itemRefreshRequest = libraryItemRefreshRequest,
hiddenFileIds = hiddenDeletedFileIds + pendingDelete?.request?.fileId?.let(::setOf).orEmpty(),
onStateChange = { next ->
val currentState = state
if (next is AppState.Reader && currentState is AppState.Library) {
libraryBackState = currentState
}
state = next
},
onLibraryFilterChange = ::setLibraryFilter,
onNavigateToScan = {
libraryBackState = null
state = AppState.Scan(
libraryState.items,
libraryState.scanPath,
libraryState.message,
libraryState.selectedFilter,
)
},
onDeleteRequested = ::requestDelete,
)
}
when (val current = state) {
AppState.LoadingStartup -> LoadingScreen(strings.loadingOpeningBook)
is AppState.Library -> Unit
is AppState.Scan -> ScanScreen(
state = current,
activeScan = activeScan,
onStateChange = { state = it },
onStartScan = { path ->
startScan(path, userInitiated = true)
state = AppState.Library(current.items, path, strings.scanning, current.selectedFilter)
},
)
is AppState.Reader -> BookView(
fileId = current.fileId,
book = current.book,
onImageOpen = { imageViewer = it },
onThemeToggle = onThemeToggle,
onBookChanged = ::refreshLibraryItem,
onBookInfo = {
state = AppState.BookInfo(
fileId = current.fileId,
book = current.book,
libraryItems = current.libraryItems,
scanPath = current.scanPath,
message = current.message,
selectedFilter = current.selectedFilter,
)
},
onDeleted = { message ->
state = libraryBackState?.copy(message = message)
?: AppState.Library(emptyList(), current.scanPath, message, current.selectedFilter)
libraryBackState = null
},
onDeleteRequested = ::requestDelete,
onBack = ::navigateBack,
)
is AppState.BookInfo -> BookInfoScreen(
fileId = current.fileId,
book = current.book,
onImageOpen = { imageViewer = it },
onBack = ::navigateBack,
)
is AppState.Error -> ErrorScreen(current.message, onBack = ::navigateBack)
}
}
imageViewer?.let { image ->
@@ -392,5 +575,11 @@ internal data class ViewedBookImage(
val title: String,
)
private fun LibraryScanReport.hasLibraryChanges(): Boolean =
importedFiles > 0
private fun AppState.withMessage(message: String): AppState =
when (this) {
is AppState.Library -> copy(message = message)
is AppState.Scan -> copy(message = message)
is AppState.Reader -> copy(message = message)
is AppState.BookInfo -> copy(message = message)
is AppState.Error, AppState.LoadingStartup -> this
}
@@ -2,6 +2,8 @@ package net.sergeych.toread
import net.sergeych.toread.fb2.Fb2Book
import net.sergeych.toread.fb2.Fb2Format
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
internal sealed interface AppState {
data object LoadingStartup : AppState
@@ -9,12 +11,14 @@ internal sealed interface AppState {
val items: List<LibraryItem>,
val scanPath: String,
val message: String? = null,
val selectedFilter: LibraryFilter = LibraryFilter.MyLibrary,
) : AppState
data class Scan(
val items: List<LibraryItem>,
val scanPath: String,
val message: String? = null,
val selectedFilter: LibraryFilter = LibraryFilter.MyLibrary,
) : AppState
data class Reader(
@@ -23,6 +27,7 @@ internal sealed interface AppState {
val libraryItems: List<LibraryItem>,
val scanPath: String,
val message: String? = null,
val selectedFilter: LibraryFilter = LibraryFilter.MyLibrary,
) : AppState
data class BookInfo(
@@ -31,6 +36,7 @@ internal sealed interface AppState {
val libraryItems: List<LibraryItem>,
val scanPath: String,
val message: String? = null,
val selectedFilter: LibraryFilter = LibraryFilter.MyLibrary,
) : AppState
data class Error(val message: String) : AppState
@@ -40,47 +46,65 @@ internal suspend fun loadStartupState(): AppState {
val scanPath = try {
defaultLibraryScanPath().orEmpty()
} catch (t: Throwable) {
return AppState.Error(t.message ?: "Could not open library.")
return AppState.Error(t.message ?: strings.couldNotOpenLibrary)
}
loadPlatformOpenBookRequest()?.let { request ->
val libraryFilter = loadLibraryFilter()
val platformRequest = runCatching { loadPlatformOpenBookRequest() }.getOrElse {
return AppState.Library(emptyList(), scanPath, it.message ?: strings.couldNotOpenLibrary, libraryFilter)
}
platformRequest?.let { request ->
return runCatching {
val book = parseBookInBackground(request.bytes, request.displayName)
saveActiveReadingFileId(request.id)
AppState.Reader(
fileId = request.id,
book = Fb2Format.parse(request.bytes, request.displayName),
book = book,
libraryItems = emptyList(),
scanPath = scanPath,
selectedFilter = libraryFilter,
)
}.getOrElse {
AppState.Library(emptyList(), scanPath, it.message ?: "Could not open ${request.displayName}.")
}.getOrElse {
AppState.Library(emptyList(), scanPath, it.message ?: strings.couldNotOpen(request.displayName), libraryFilter)
}
}
val activeFileId = loadActiveReadingFileId() ?: return AppState.Library(emptyList(), scanPath)
val activeFileId = loadActiveReadingFileId() ?: return AppState.Library(emptyList(), scanPath, selectedFilter = libraryFilter)
val item = loadLibraryItem(activeFileId)
if (item == null) {
saveActiveReadingFileId(null)
return AppState.Library(emptyList(), scanPath)
return AppState.Library(emptyList(), scanPath, selectedFilter = libraryFilter)
}
return runCatching {
val bytes = openLibraryBook(activeFileId) ?: error("Book file is not available.")
val bytes = openLibraryBook(activeFileId) ?: error(strings.bookFileNotAvailable)
AppState.Reader(
fileId = activeFileId,
book = Fb2Format.parse(bytes, item.storageUri ?: item.title),
book = parseBookInBackground(bytes, item.storageUri ?: item.title),
libraryItems = emptyList(),
scanPath = scanPath,
selectedFilter = libraryFilter,
)
}.getOrElse {
saveActiveReadingFileId(null)
AppState.Library(emptyList(), scanPath, it.message ?: "Could not reopen last book.")
AppState.Library(emptyList(), scanPath, it.message ?: strings.couldNotReopenLastBook(), libraryFilter)
}
}
internal suspend fun loadLibraryState(message: String? = null, scanPath: String? = null): AppState =
internal suspend fun loadLibraryState(
message: String? = null,
scanPath: String? = null,
selectedFilter: LibraryFilter? = null,
): AppState =
runCatching {
AppState.Library(
items = emptyList(),
scanPath = scanPath ?: defaultLibraryScanPath().orEmpty(),
message = message,
selectedFilter = selectedFilter ?: loadLibraryFilter(),
)
}.getOrElse {
AppState.Error(it.message ?: "Could not open library.")
AppState.Error(it.message ?: strings.couldNotOpenLibrary)
}
internal suspend fun parseBookInBackground(input: ByteArray, fileName: String? = null): Fb2Book =
withContext(Dispatchers.Default) {
Fb2Format.parse(input, fileName)
}
@@ -51,10 +51,10 @@ internal fun BookInfoScreen(
Scaffold(
topBar = {
CenterAlignedTopAppBar(
title = { Text("Book info") },
title = { Text(strings.bookInfo) },
navigationIcon = {
IconButton(onClick = onBack) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back to reader")
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = strings.backToReader)
}
},
colors = themedTopAppBarColors(),
@@ -70,46 +70,56 @@ internal fun BookInfoScreen(
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
item {
InfoSection("Title Info") {
InfoSection(strings.titleInfo) {
CoverAndTitle(book, onImageOpen = onImageOpen)
DetailLine("Title", book.title)
DetailLine("Authors", book.authors.joinToString { it.displayName }.ifBlank { "Unknown author" })
DetailLine("Language", book.language?.uppercase() ?: "Not specified")
DetailLine("Date", book.date ?: "Not specified")
DetailLine("Genres", book.genres.joinToString().ifBlank { "Not specified" })
DetailLine("Source", book.sourceLanguage?.uppercase() ?: "Not specified")
DetailLine(strings.title, book.title)
DetailLine(strings.authors, book.authors.joinToString { it.displayName }.ifBlank { strings.unknownAuthor })
DetailLine(strings.language, book.language?.uppercase() ?: strings.notSpecified)
DetailLine(strings.date, book.date ?: strings.notSpecified)
DetailLine(strings.genres, book.genres.joinToString().ifBlank { strings.notSpecified })
DetailLine(strings.source, book.sourceLanguage?.uppercase() ?: strings.notSpecified)
if (book.annotation.isNullOrBlank().not()) {
Text(book.annotation.orEmpty(), style = MaterialTheme.typography.bodyMedium, lineHeight = 20.sp)
}
}
}
item {
InfoSection("Statistics") {
InfoSection(strings.statistics) {
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
StatBlock("Words", stats.words.formatCompact())
StatBlock("Sections", stats.sections.toString())
StatBlock("Images", stats.images.toString())
StatBlock(strings.words, stats.words.formatCompact())
StatBlock(strings.sections, stats.sections.toString())
StatBlock(strings.images, stats.images.toString())
}
}
}
item {
InfoSection("Last Reading Position") {
InfoSection(strings.sourceFile) {
if (extras == null) {
Text(strings.loading, style = MaterialTheme.typography.bodyMedium)
} else {
DetailLine(strings.fileName, extras?.sourceFileName?.ifBlank { null } ?: strings.notSpecified)
DetailLine(strings.filePath, extras?.sourceFilePath?.ifBlank { null } ?: strings.notSpecified)
}
}
}
item {
InfoSection(strings.lastReadingPosition) {
val position = extras?.lastReadingPosition
if (position == null) {
Text("No saved position", style = MaterialTheme.typography.bodyMedium)
Text(strings.noSavedPosition, style = MaterialTheme.typography.bodyMedium)
} else {
DetailLine("List item", position.itemIndex.toString())
DetailLine("Scroll offset", "${position.scrollOffset}px")
DetailLine(strings.listItem, position.itemIndex.toString())
DetailLine(strings.scrollOffset, "${position.scrollOffset}px")
}
}
}
item {
InfoSection("Bookmarks") {
InfoSection(strings.bookmarks) {
val bookmarks = extras?.bookmarks.orEmpty()
if (extras == null) {
Text("Loading...", style = MaterialTheme.typography.bodyMedium)
Text(strings.loading, style = MaterialTheme.typography.bodyMedium)
} else if (bookmarks.isEmpty()) {
Text("No bookmarks", style = MaterialTheme.typography.bodyMedium)
Text(strings.noBookmarks, style = MaterialTheme.typography.bodyMedium)
} else {
bookmarks.forEach { bookmark ->
BookmarkInfoLine(bookmark)
@@ -118,12 +128,12 @@ internal fun BookInfoScreen(
}
}
item {
InfoSection("Notes") {
InfoSection(strings.notes) {
val notes = extras?.notes.orEmpty()
if (extras == null) {
Text("Loading...", style = MaterialTheme.typography.bodyMedium)
Text(strings.loading, style = MaterialTheme.typography.bodyMedium)
} else if (notes.isEmpty()) {
Text("No notes", style = MaterialTheme.typography.bodyMedium)
Text(strings.noNotes, style = MaterialTheme.typography.bodyMedium)
} else {
notes.forEach { note ->
NoteInfoLine(note)
@@ -148,7 +158,7 @@ private fun InfoSection(title: String, content: @Composable ColumnScope.() -> Un
@Composable
private fun BookmarkInfoLine(bookmark: BookmarkInfo) {
Column(verticalArrangement = Arrangement.spacedBy(2.dp), modifier = Modifier.fillMaxWidth()) {
Text(bookmark.title?.ifBlank { null } ?: "Bookmark", style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.SemiBold)
Text(bookmark.title?.ifBlank { null } ?: strings.bookmark, style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.SemiBold)
bookmark.selectedText?.ifBlank { null }?.let {
Text(it, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.secondary, maxLines = 3)
}
@@ -6,8 +6,11 @@ import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.rememberTransformableState
import androidx.compose.foundation.gestures.transformable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
@@ -35,7 +38,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
@@ -66,6 +68,8 @@ internal fun ImageViewer(
val focusRequester = remember { FocusRequester() }
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
val imageBackgroundColor = readerImageBackgroundColor()
val imageAspectRatio = image.bitmap.width.toFloat() / image.bitmap.height.toFloat()
fun setScale(next: Float) {
scale = next.coerceIn(MinImageScale, MaxImageScale)
@@ -88,9 +92,9 @@ internal fun ImageViewer(
fun copyImage() {
scope.launch {
val message = if (copyImageToClipboard(image.bytes, image.mimeType, image.title)) {
"Image copied"
strings.imageCopied
} else {
"Copy failed"
strings.copyFailed
}
snackbarHostState.showSnackbar(message, duration = SnackbarDuration.Short)
}
@@ -110,7 +114,7 @@ internal fun ImageViewer(
verticalAlignment = Alignment.CenterVertically,
) {
IconButton(onClick = onBack) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = strings.back)
}
Text(
image.title,
@@ -119,25 +123,25 @@ internal fun ImageViewer(
modifier = Modifier.weight(1f),
)
IconButton(onClick = { setScale(scale * 1.25f) }) {
Icon(Icons.Filled.ZoomIn, contentDescription = "Zoom in")
Icon(Icons.Filled.ZoomIn, contentDescription = strings.zoomIn)
}
IconButton(onClick = { setScale(scale / 1.25f) }, enabled = scale > MinImageScale) {
Icon(Icons.Filled.ZoomOut, contentDescription = "Zoom out")
Icon(Icons.Filled.ZoomOut, contentDescription = strings.zoomOut)
}
IconButton(
onClick = ::copyImage,
) {
Icon(Icons.Filled.ContentCopy, contentDescription = "Copy image")
Icon(Icons.Filled.ContentCopy, contentDescription = strings.copyImage)
}
}
}
},
) { padding ->
Box(
BoxWithConstraints(
modifier = Modifier
.fillMaxSize()
.padding(padding)
.background(Color.Black)
.background(readerBackground())
.focusRequester(focusRequester)
.onPreviewKeyEvent { event ->
if (event.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false
@@ -171,19 +175,28 @@ internal fun ImageViewer(
},
contentAlignment = Alignment.Center,
) {
Image(
bitmap = image.bitmap,
contentDescription = image.title,
modifier = Modifier
.fillMaxSize()
val imageModifier = if (maxWidth / maxHeight > imageAspectRatio) {
Modifier.fillMaxHeight().aspectRatio(imageAspectRatio)
} else {
Modifier.fillMaxWidth().aspectRatio(imageAspectRatio)
}
Box(
modifier = imageModifier
.graphicsLayer {
scaleX = scale
scaleY = scale
translationX = offset.x
translationY = offset.y
},
contentScale = ContentScale.Fit,
)
}
.background(imageBackgroundColor),
) {
Image(
bitmap = image.bitmap,
contentDescription = image.title,
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Fit,
)
}
}
}
}
@@ -14,6 +14,6 @@ internal suspend fun deleteLibraryBook(fileId: String, title: String): LibraryDe
val deleted = runCatching { deleteLibraryItem(fileId) }.getOrDefault(false)
return LibraryDeleteResult(
deleted = deleted,
message = if (deleted) "Removed $title." else "Could not remove $title.",
message = if (deleted) strings.removed(title) else strings.couldNotRemove(title),
)
}
@@ -3,6 +3,7 @@ package net.sergeych.toread
import net.sergeych.toread.fb2.Fb2Binary
import net.sergeych.toread.fb2.Fb2Book
import net.sergeych.toread.storage.BookReadingStatus
import kotlin.math.abs
data class LibraryItem(
val fileId: String,
@@ -60,9 +61,28 @@ data class PlatformOpenBookRequest(
data class ReadingPosition(
val itemIndex: Int,
val scrollOffset: Int,
val readAloudSentenceIndex: Int? = null,
val backStack: List<ReadingPosition> = emptyList(),
)
data class ReaderFontSettings(
val fontSizeSp: Float = defaultReaderFontSizeSp(),
val lineHeightSp: Float = DefaultReaderLineHeightSp,
val fontWeight: Int = defaultReaderFontWeight(),
val fontName: String? = null,
val lightBackgroundArgb: Long? = null,
val lightTextArgb: Long? = null,
val darkBackgroundArgb: Long? = null,
val darkTextArgb: Long? = null,
val lightCustomBackgroundArgb: Long? = null,
val lightCustomTextArgb: Long? = null,
val darkCustomBackgroundArgb: Long? = null,
val darkCustomTextArgb: Long? = null,
)
data class BookInfoExtras(
val sourceFileName: String? = null,
val sourceFilePath: String? = null,
val bookmarks: List<BookmarkInfo> = emptyList(),
val notes: List<NoteInfo> = emptyList(),
val lastReadingPosition: ReadingPosition? = null,
@@ -129,7 +149,9 @@ expect suspend fun markLibraryFavorite(fileId: String, favorite: Boolean): Boole
expect suspend fun shareLibraryBookFile(fileId: String): Boolean
expect suspend fun viewLibraryBookFile(fileId: String): Boolean
expect suspend fun viewLibraryBookFile(folder: String, fileName: String): Boolean
expect fun openPlatformUrl(url: String): Boolean
expect suspend fun loadBookInfoExtras(fileId: String): BookInfoExtras
@@ -141,10 +163,30 @@ expect suspend fun loadThemeMode(): ThemeMode
expect suspend fun saveThemeMode(mode: ThemeMode)
expect suspend fun loadReaderFontSettings(): ReaderFontSettings
expect suspend fun saveReaderFontSettings(settings: ReaderFontSettings)
expect suspend fun loadReaderFullScreen(): Boolean
expect suspend fun saveReaderFullScreen(fullScreen: Boolean)
expect suspend fun loadReaderPagingOnly(): Boolean
expect suspend fun saveReaderPagingOnly(enabled: Boolean)
expect suspend fun loadScanDownloadsAutomatically(): Boolean
expect suspend fun saveScanDownloadsAutomatically(enabled: Boolean)
internal expect suspend fun loadLibraryFilter(): LibraryFilter
internal expect suspend fun saveLibraryFilter(filter: LibraryFilter)
expect suspend fun loadAppLocaleTag(): String?
expect suspend fun saveAppLocaleTag(localeTag: String?)
expect suspend fun loadDownloadsWasScanned(): Boolean
expect suspend fun saveDownloadsWasScanned(scanned: Boolean)
@@ -165,3 +207,198 @@ internal fun Fb2Book.libraryCoverBinary(): Fb2Binary? {
val image = coverImages.firstOrNull() ?: bodyImages.firstOrNull()
return image?.let(::binaryFor)
}
internal fun canShareLibraryBookFile(): Boolean =
getPlatform().name.startsWith("Android")
internal suspend fun shareLibraryBook(fileId: String): String =
if (shareLibraryBookFile(fileId)) strings.shareOpened() else strings.couldNotShareBook()
internal fun canViewLibraryBookFile(): Boolean =
getPlatform().name.startsWith("Java")
internal suspend fun viewLibraryBook(fileId: String): String {
val location = loadLibraryItem(fileId)?.storageUri?.toLibraryBookFileLocation()
?: return strings.couldNotOpenFileLocation()
return if (viewLibraryBookFile(location.folder, location.fileName)) {
strings.openedFileLocation()
} else {
strings.couldNotOpenFileLocation()
}
}
private data class LibraryBookFileLocation(
val folder: String,
val fileName: String,
)
private fun String.toLibraryBookFileLocation(): LibraryBookFileLocation? {
val separatorIndex = maxOf(lastIndexOf('/'), lastIndexOf('\\'))
if (separatorIndex < 0 || separatorIndex >= lastIndex) return null
return LibraryBookFileLocation(
folder = if (separatorIndex == 0) substring(0, 1) else substring(0, separatorIndex),
fileName = substring(separatorIndex + 1),
)
}
internal const val DefaultReaderLineHeightSp = 26f
internal const val ReaderFontSizeStepSp = 1f
internal const val ReaderLineHeightStepSp = 1f
private const val MinReaderFontSizeSp = 14f
private const val MaxReaderFontSizeSp = 34f
private const val MinReaderLineHeightSp = 18f
private const val MaxReaderLineHeightSp = 48f
private const val MinReaderFontWeight = 100
private const val MaxReaderFontWeight = 900
private val ReaderFontWeightSteps = listOf(100, 200, 300, 400, 500, 600, 700, 800, 900)
internal fun defaultReaderFontSettings(): ReaderFontSettings = ReaderFontSettings()
internal fun defaultReaderFontSizeSp(): Float =
if (getPlatform().name.startsWith("Android")) 20f else 18f
internal fun defaultReaderFontWeight(): Int =
if (getPlatform().name.startsWith("Android")) 350 else 400
internal fun ReaderFontSettings.coerced(): ReaderFontSettings =
copy(
fontSizeSp = fontSizeSp.coerceIn(MinReaderFontSizeSp, MaxReaderFontSizeSp),
lineHeightSp = lineHeightSp.coerceIn(MinReaderLineHeightSp, MaxReaderLineHeightSp),
fontWeight = fontWeight.coerceIn(MinReaderFontWeight, MaxReaderFontWeight),
fontName = fontName?.takeIf { it.isNotBlank() },
lightBackgroundArgb = lightBackgroundArgb?.coerceArgb(),
lightTextArgb = lightTextArgb?.coerceArgb(),
darkBackgroundArgb = darkBackgroundArgb?.coerceArgb(),
darkTextArgb = darkTextArgb?.coerceArgb(),
lightCustomBackgroundArgb = lightCustomBackgroundArgb?.coerceArgb(),
lightCustomTextArgb = lightCustomTextArgb?.coerceArgb(),
darkCustomBackgroundArgb = darkCustomBackgroundArgb?.coerceArgb(),
darkCustomTextArgb = darkCustomTextArgb?.coerceArgb(),
)
internal fun ReaderFontSettings.adjustFontSize(steps: Int): ReaderFontSettings {
val current = coerced()
val nextFontSize = (current.fontSizeSp + steps * ReaderFontSizeStepSp)
.coerceIn(MinReaderFontSizeSp, MaxReaderFontSizeSp)
val scale = if (current.fontSizeSp > 0f) nextFontSize / current.fontSizeSp else 1f
return current.copy(
fontSizeSp = nextFontSize,
lineHeightSp = (current.lineHeightSp * scale).coerceIn(MinReaderLineHeightSp, MaxReaderLineHeightSp),
)
}
internal fun ReaderFontSettings.adjustLineHeight(steps: Int): ReaderFontSettings =
coerced().let { current ->
current.copy(
lineHeightSp = (current.lineHeightSp + steps * ReaderLineHeightStepSp)
.coerceIn(MinReaderLineHeightSp, MaxReaderLineHeightSp),
)
}
internal fun ReaderFontSettings.adjustFontWeight(steps: Int): ReaderFontSettings {
val current = coerced()
if (steps == 0) return current
var nextWeight = current.fontWeight
repeat(abs(steps)) {
nextWeight = current.fontWeight + if (steps > 0) 50 else -50
if( nextWeight == 100 || nextWeight == 900 ) return@repeat
}
return current.copy(fontWeight = nextWeight)
}
internal fun ReaderFontSettings.toStorageValue(): String =
coerced().let { current ->
listOf(
current.fontSizeSp.toString(),
current.lineHeightSp.toString(),
current.fontWeight.toString(),
current.fontName.orEmpty(),
current.lightBackgroundArgb?.toStorageArgb().orEmpty(),
current.lightTextArgb?.toStorageArgb().orEmpty(),
current.darkBackgroundArgb?.toStorageArgb().orEmpty(),
current.darkTextArgb?.toStorageArgb().orEmpty(),
current.lightCustomBackgroundArgb?.toStorageArgb().orEmpty(),
current.lightCustomTextArgb?.toStorageArgb().orEmpty(),
current.darkCustomBackgroundArgb?.toStorageArgb().orEmpty(),
current.darkCustomTextArgb?.toStorageArgb().orEmpty(),
).joinToString("|")
}
internal fun readerFontSettingsFromStorageValue(value: String?): ReaderFontSettings {
val defaults = defaultReaderFontSettings()
val parts = value?.split("|") ?: return defaults
return ReaderFontSettings(
fontSizeSp = parts.getOrNull(0)?.toFloatOrNull() ?: defaults.fontSizeSp,
lineHeightSp = parts.getOrNull(1)?.toFloatOrNull() ?: defaults.lineHeightSp,
fontWeight = parts.getOrNull(2)?.toIntOrNull() ?: defaults.fontWeight,
fontName = parts.getOrNull(3)?.takeIf { it.isNotBlank() } ?: defaults.fontName,
lightBackgroundArgb = parts.getOrNull(4)?.storageArgbOrNull(),
lightTextArgb = parts.getOrNull(5)?.storageArgbOrNull(),
darkBackgroundArgb = parts.getOrNull(6)?.storageArgbOrNull(),
darkTextArgb = parts.getOrNull(7)?.storageArgbOrNull(),
lightCustomBackgroundArgb = parts.getOrNull(8)?.storageArgbOrNull(),
lightCustomTextArgb = parts.getOrNull(9)?.storageArgbOrNull(),
darkCustomBackgroundArgb = parts.getOrNull(10)?.storageArgbOrNull(),
darkCustomTextArgb = parts.getOrNull(11)?.storageArgbOrNull(),
).coerced()
}
internal fun ReaderFontSettings.withThemeBackgroundArgb(useDarkTheme: Boolean, argb: Long?): ReaderFontSettings =
if (useDarkTheme) copy(darkBackgroundArgb = argb?.coerceArgb()) else copy(lightBackgroundArgb = argb?.coerceArgb())
internal fun ReaderFontSettings.withThemeTextArgb(useDarkTheme: Boolean, argb: Long?): ReaderFontSettings =
if (useDarkTheme) copy(darkTextArgb = argb?.coerceArgb()) else copy(lightTextArgb = argb?.coerceArgb())
internal fun ReaderFontSettings.withThemeCustomBackgroundArgb(useDarkTheme: Boolean, argb: Long?): ReaderFontSettings =
if (useDarkTheme) {
copy(darkCustomBackgroundArgb = argb?.coerceArgb(), darkBackgroundArgb = argb?.coerceArgb())
} else {
copy(lightCustomBackgroundArgb = argb?.coerceArgb(), lightBackgroundArgb = argb?.coerceArgb())
}
internal fun ReaderFontSettings.withThemeCustomTextArgb(useDarkTheme: Boolean, argb: Long?): ReaderFontSettings =
if (useDarkTheme) {
copy(darkCustomTextArgb = argb?.coerceArgb(), darkTextArgb = argb?.coerceArgb())
} else {
copy(lightCustomTextArgb = argb?.coerceArgb(), lightTextArgb = argb?.coerceArgb())
}
internal fun ReaderFontSettings.themeBackgroundArgb(useDarkTheme: Boolean): Long? =
if (useDarkTheme) darkBackgroundArgb else lightBackgroundArgb
internal fun ReaderFontSettings.themeTextArgb(useDarkTheme: Boolean): Long? =
if (useDarkTheme) darkTextArgb else lightTextArgb
internal fun ReaderFontSettings.themeCustomBackgroundArgb(useDarkTheme: Boolean): Long? =
if (useDarkTheme) darkCustomBackgroundArgb else lightCustomBackgroundArgb
internal fun ReaderFontSettings.themeCustomTextArgb(useDarkTheme: Boolean): Long? =
if (useDarkTheme) darkCustomTextArgb else lightCustomTextArgb
internal fun ReaderFontSettings.resetThemeColorScheme(useDarkTheme: Boolean): ReaderFontSettings =
if (useDarkTheme) {
copy(
darkBackgroundArgb = null,
darkTextArgb = null,
)
} else {
copy(
lightBackgroundArgb = null,
lightTextArgb = null,
)
}
private fun Long.coerceArgb(): Long =
coerceIn(0x00000000L, 0xFFFFFFFFL)
private fun Long.toStorageArgb(): String =
coerceArgb().toString(16).padStart(8, '0').uppercase()
private fun String.storageArgbOrNull(): Long? =
trim()
.removePrefix("#")
.takeIf { it.length == 8 }
?.toLongOrNull(16)
?.coerceArgb()
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,507 @@
package net.sergeych.toread
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import net.sergeych.toread.storage.BookReadingStatus
import kotlin.math.roundToInt
internal enum class AppLanguage(val localeTag: String) {
English("en"),
Russian("ru"),
}
internal val availableAppLanguages: List<AppLanguage> = AppLanguage.entries
internal var selectedAppLocaleTag: String? by mutableStateOf(null)
internal val appLanguage: AppLanguage
get() = if (effectiveAppLocaleTags().any { it.isRussianLanguageTag() }) {
AppLanguage.Russian
} else {
AppLanguage.English
}
internal val strings: AppStrings
get() = when (appLanguage) {
AppLanguage.English -> EnglishStrings
AppLanguage.Russian -> RussianStrings
}
internal expect fun platformLocaleTags(): List<String>
private fun effectiveAppLocaleTags(): List<String> =
selectedAppLocaleTag?.let(::listOf) ?: platformLocaleTags()
private fun String.isRussianLanguageTag(): Boolean =
substringBefore('-').substringBefore('_').equals("ru", ignoreCase = true)
internal open class AppStrings {
open val appName = "To Read"
open val loadingOpeningBook = "Opening book"
open val scanning = "Scanning..."
open val scanningDownloads = "Scanning Downloads..."
open val undo = "Undo"
open val retry = "Retry"
open val done = "Done"
open val cancel = "Cancel"
open val libraryError = "Library error"
open val couldNotOpenLibrary = "Could not open library."
open val couldNotLoadLibrary = "Could not load library."
open val couldNotOpenBook = "Could not open book."
open val couldNotUpdateBook = "Could not update book."
open val bookFileNotAvailable = "Book file is not available."
open val scanFailed = "Scan failed."
open val searchFailed = "Search failed."
open val libraryRescanFailed = "Library rescan failed."
open val rescanningLibrary = "Rescanning library..."
open val theme = "Theme"
open val themeLight = "Light"
open val themeDark = "Dark"
open val themeSystem = "System"
open val libraryOptions = "Library options"
open val scanDownloadsAutomatically = "Scan Downloads automatically"
open val locale = "Locale"
open val systemLocale = "Default"
open fun appVersion(version: String): String = "Version $version"
open val scanFolder = "Scan folder"
open val chooseLibraryFilter = "Choose library filter"
open val searchLibrary = "Search library"
open val closeSearch = "Close search"
open val clearSearch = "Clear search"
open val noMatches = "No matches"
open val scanFolderOrChooseFilter = "Scan a folder or choose another library filter."
open val favorite = "Favorite"
open val unknownAuthor = "Unknown author"
open val noMetadata = "No metadata"
open val lastReadPrefix = "Last read"
open val open = "Open"
open val markAsRead = "Mark as read"
open val markAsUnread = "Mark as unread"
open val markToRead = "Mark to read"
open val removeToRead = "Remove to read"
open val removeMarks = "Remove marks"
open val notInterested = "Not interested"
open val clearMarks = "Clear marks"
open val addFavorite = "Add favorite"
open val removeFavorite = "Remove favorite"
open val delete = "Delete"
open val filterReadingNow = "Reading now"
open val filterRecentlyAdded = "Recently added"
open val filterMyLibrary = "My library"
open val filterToRead = "To read"
open val filterRead = "Read"
open val scan = "Scan"
open val rootFolder = "Root folder"
open val choose = "Choose"
open val logPrefix = "Log"
open val bookInfo = "Book info"
open val back = "Back"
open val backToLibrary = "Back to library"
open val backToReader = "Back to reader"
open val titleInfo = "Title Info"
open val title = "Title"
open val authors = "Authors"
open val language = "Language"
open val date = "Date"
open val genres = "Genres"
open val source = "Source"
open val translator = "Translator"
open val notSpecified = "Not specified"
open val statistics = "Statistics"
open val words = "Words"
open val sections = "Sections"
open val images = "Images"
open val sourceFile = "Source file"
open val fileName = "File name"
open val filePath = "Path"
open val lastReadingPosition = "Last Reading Position"
open val noSavedPosition = "No saved position"
open val listItem = "List item"
open val scrollOffset = "Scroll offset"
open val bookmarks = "Bookmarks"
open val bookmark = "Bookmark"
open val noBookmarks = "No bookmarks"
open val notes = "Notes"
open val noNotes = "No notes"
open val loading = "Loading..."
open val noImage = "No image"
open val readerTheme = "Theme"
open val tableOfContents = "Contents"
open val backToLastPosition = "Back"
open val readAloud = "Read aloud"
open val readerMenu = "Book reader menu"
open val info = "Info..."
open val readerSettings = "Settings"
open val fullScreen = "Full screen"
open val readerFontSizeIncrease = "Increase font size"
open val readerFontSizeDecrease = "Decrease font size"
open val readerLineHeightIncrease = "Increase line spacing"
open val readerLineHeightDecrease = "Decrease line spacing"
open val readerFontWeightIncrease = "Increase font weight"
open val readerFontWeightDecrease = "Decrease font weight"
open val resetReaderFontSettings = "Reset font settings"
open val revertColorScheme = "Revert color scheme"
open val readerBackgroundColor = "Reader background color"
open val readerTextColor = "Reader text color"
open val extraReaderSettings = "Extra reader settings"
open val customColor = "Custom color"
open val red = "R"
open val green = "G"
open val blue = "B"
open val readerColors = "Colors"
open val readerTypography = "Typography"
open val readerLayout = "Layout"
open val readerNavigation = "Navigation"
open val readerPagingOnly = "Paging only (no free scroll)"
open val readerProgress = "Progress"
open val closeReaderSettings = "Close reader settings"
open val share = "Share"
open val viewFile = "View file"
open val previousSentence = "Previous sentence"
open val stopReading = "Stop reading"
open val startReading = "Start reading"
open val nextSentence = "Next sentence"
open val readAloudSettings = "Read aloud settings"
open val ttsEngine = "TTS engine"
open val systemDefault = "System default"
open val offlineVoice = "Offline voice"
open val autoRussian = "Auto Russian"
open val loadingVoices = "Loading voices..."
open val preparingSpeech = "Preparing speech…"
open val offlineSpeechModelMissing = "Download the offline speech model first; see the desktop setup guide."
open val speechPlaybackFailed = "Speech playback failed. Check the offline model and audio output, then try again."
open val imageCopied = "Image copied"
open val copyFailed = "Copy failed"
open val zoomIn = "Zoom in"
open val zoomOut = "Zoom out"
open val copyImage = "Copy image"
open val chooseLibraryFolder = "Choose library folder"
open val downloads = "Downloads"
open val otherFolder = "Other folder..."
open val play = "Play"
open val stop = "Stop"
open val previous = "Previous"
open val next = "Next"
open val readingInBackground = "Reading in the background"
open val shareBook = "Share book"
open val couldNotLoadAndroidTtsEngines = "Could not load Android TTS engines."
open val couldNotLoadSelectedTtsVoices = "Could not load voices for the selected TTS engine."
open val noOfflineVoices = "No offline voices reported by the selected TTS engine."
open fun themeChanged(mode: ThemeMode): String = "$theme: ${mode.localizedName()}"
open fun removed(title: String): String = "Removed $title."
open fun restored(title: String): String = "Restored $title."
open fun couldNotRemove(title: String): String = "Could not remove $title."
open fun couldNotOpen(displayName: String): String = "Could not open $displayName."
open fun couldNotReopenLastBook(): String = "Could not reopen last book."
open fun scanReport(scanned: Int, imported: Int, skipped: Int, failed: Int): String =
"Scanned $scanned, imported $imported, skipped $skipped, failed $failed."
open val noNewBooksFound = "Nothing new has been found."
open val show = "Show"
open fun newBooksAdded(count: Int): String =
if (count == 1) "1 new book added." else "$count new books added."
open fun rescanReport(scanned: Int, updated: Int, failed: Int): String =
"Rescanned $scanned, updated $updated, failed $failed."
open fun checkingFile(currentFile: String?, scanned: Int, imported: Int, skipped: Int, failed: Int): String =
buildString {
append("Checking")
currentFile?.takeIf(String::isNotBlank)?.let { append(" $it") }
append(". Found $scanned supported files")
append(", imported $imported")
append(", skipped $skipped")
append(", failed $failed.")
}
open fun importedSkippedFailed(imported: Int, skipped: Int, failed: Int): String =
"Imported $imported, skipped $skipped, failed $failed"
open fun scannedBooks(scanned: Int): String = "Scanned $scanned books"
open fun scannedProgress(scanned: Int, total: Int, percent: Int): String =
"Scanned $scanned of $total, $percent% done"
open fun noBooksIn(filterLabel: String): String = "No books in ${filterLabel.lowercase()}"
open fun bookMenuFor(title: String): String = "Book menu for $title"
open fun markedAsRead(title: String? = null): String = title?.let { "Marked $it as read." } ?: "Marked as read."
open fun markedAsUnread(title: String): String = "Marked $title as unread."
open fun markedToRead(title: String? = null): String = title?.let { "Marked $it to read." } ?: "Marked to read."
open fun markedNotInterested(title: String? = null): String =
title?.let { "Marked $it as not interested." } ?: "Marked as not interested."
open fun removedMarks(title: String? = null): String = title?.let { "Removed marks from $it." } ?: "Cleared marks."
open fun addedToFavorites(title: String? = null): String =
title?.let { "Added $it to favorites." } ?: "Added to favorites."
open fun removedFromFavorites(title: String? = null): String =
title?.let { "Removed $it from favorites." } ?: "Removed from favorites."
open fun couldNotUpdate(title: String): String = "Could not update $title."
open fun shareOpened(): String = "Share opened."
open fun couldNotShareBook(): String = "Could not share book."
open fun openedFileLocation(): String = "Opened file location."
open fun couldNotOpenFileLocation(): String = "Could not open file location."
open fun allFilesAccessRequired(path: String): String = "All files access is required to scan $path."
open fun couldNotRead(name: String): String = "Could not read $name"
open fun progressLabel(progress: Double?): String =
progress?.let { "Progress ${(it * 100).roundToInt()}%" } ?: "Progress not recorded"
open fun appLanguageName(language: AppLanguage): String =
when (language) {
AppLanguage.English -> "English"
AppLanguage.Russian -> "Russian"
}
open fun sectionFallback(index: Int): String = "Section ${index + 1}"
open fun readingStatus(status: BookReadingStatus): String =
when (status) {
BookReadingStatus.NEW -> "New"
BookReadingStatus.TO_READ -> "To read"
BookReadingStatus.READING -> "Reading"
BookReadingStatus.READ -> "Read"
BookReadingStatus.NOT_INTERESTED -> "Not interested"
}
}
internal object EnglishStrings : AppStrings()
internal object RussianStrings : AppStrings() {
override val appName = "Читать"
override val loadingOpeningBook = "Открываем книгу"
override val scanning = "Сканируем..."
override val scanningDownloads = "Сканируем Загрузки..."
override val undo = "Отменить"
override val retry = "Повторить"
override val done = "Готово"
override val cancel = "Отмена"
override val libraryError = "Ошибка библиотеки"
override val couldNotOpenLibrary = "Не удалось открыть библиотеку."
override val couldNotLoadLibrary = "Не удалось загрузить библиотеку."
override val couldNotOpenBook = "Не удалось открыть книгу."
override val couldNotUpdateBook = "Не удалось обновить книгу."
override val bookFileNotAvailable = "Файл книги недоступен."
override val scanFailed = "Импорт не удался."
override val searchFailed = "Поиск не удался."
override val libraryRescanFailed = "Повторное сканирование библиотеки не удалось."
override val rescanningLibrary = "Пересканируем библиотеку..."
override val theme = "Тема"
override val themeLight = "светлая"
override val themeDark = "темная"
override val themeSystem = "системная"
override val libraryOptions = "Параметры библиотеки"
override val scanDownloadsAutomatically = "Автоматически сканировать Загрузки"
override val locale = "Локализация"
override val systemLocale = "Системная"
override fun appVersion(version: String): String = "Версия $version"
override val scanFolder = "Сканировать папку"
override val chooseLibraryFilter = "Выбрать фильтр библиотеки"
override val searchLibrary = "Поиск"
override val closeSearch = "Закрыть поиск"
override val clearSearch = "Очистить поиск"
override val noMatches = "Ничего не найдено"
override val scanFolderOrChooseFilter = "Импортируйте папку или выберите другой фильтр библиотеки."
override val favorite = "Избранное"
override val unknownAuthor = "Автор неизвестен"
override val noMetadata = "Нет метаданных"
override val lastReadPrefix = "Читали"
override val open = "Открыть"
override val markAsRead = "Отметить прочитанной"
override val markAsUnread = "Отметить непрочитанной"
override val markToRead = "Отложить к чтению"
override val removeToRead = "Убрать из отложенного"
override val removeMarks = "Снять отметки"
override val notInterested = "Не интересно"
override val clearMarks = "Очистить отметки"
override val addFavorite = "Добавить в избранное"
override val removeFavorite = "Убрать из избранного"
override val delete = "Удалить"
override val filterReadingNow = "Сейчас читаю"
override val filterRecentlyAdded = "Недавно добавленные"
override val filterMyLibrary = "Моя библиотека"
override val filterToRead = "К чтению"
override val filterRead = "Прочитанные"
override val scan = "Импорт"
override val rootFolder = "Корневая папка"
override val choose = "Выбрать"
override val logPrefix = "Лог"
override val bookInfo = "Информация о книге"
override val back = "Назад"
override val backToLibrary = "Вернуться в библиотеку"
override val backToReader = "Вернуться к чтению"
override val titleInfo = "Описание"
override val title = "Название"
override val authors = "Авторы"
override val language = "Язык"
override val date = "Дата"
override val genres = "Жанры"
override val source = "Исходный язык"
override val translator = "Переводчик"
override val notSpecified = "Не указано"
override val statistics = "Статистика"
override val words = "Слова"
override val sections = "Разделы"
override val images = "Иллюстрации"
override val sourceFile = "Исходный файл"
override val fileName = "Имя файла"
override val filePath = "Путь"
override val lastReadingPosition = "Последняя позиция чтения"
override val noSavedPosition = "Позиция не сохранена"
override val listItem = "Элемент списка"
override val scrollOffset = "Смещение прокрутки"
override val bookmarks = "Закладки"
override val bookmark = "Закладка"
override val noBookmarks = "Закладок нет"
override val notes = "Заметки"
override val noNotes = "Заметок нет"
override val loading = "Загрузка..."
override val noImage = "Нет изображения"
override val readerTheme = "Тема"
override val tableOfContents = "Содержание"
override val backToLastPosition = "Назад"
override val readAloud = "Читать вслух"
override val readerMenu = "Меню чтения"
override val info = "Информация..."
override val readerSettings = "Настройки"
override val fullScreen = "На весь экран"
override val readerFontSizeIncrease = "Увеличить шрифт"
override val readerFontSizeDecrease = "Уменьшить шрифт"
override val readerLineHeightIncrease = "Увеличить межстрочный интервал"
override val readerLineHeightDecrease = "Уменьшить межстрочный интервал"
override val readerFontWeightIncrease = "Увеличить насыщенность шрифта"
override val readerFontWeightDecrease = "Уменьшить насыщенность шрифта"
override val resetReaderFontSettings = "Сбросить настройки шрифта"
override val revertColorScheme = "Вернуть цветовую схему"
override val readerBackgroundColor = "Цвет фона чтения"
override val readerTextColor = "Цвет текста чтения"
override val extraReaderSettings = "Дополнительные настройки"
override val customColor = "Свой цвет"
override val red = "К"
override val green = "З"
override val blue = "С"
override val readerColors = "Цвета"
override val readerTypography = "Типографика"
override val readerLayout = "Макет"
override val readerNavigation = "Навигация"
override val readerPagingOnly = "Постраничный режим (без прокрутки)"
override val readerProgress = "Прогресс"
override val closeReaderSettings = "Закрыть настройки чтения"
override val share = "Поделиться"
override val viewFile = "Показать файл"
override val previousSentence = "Предыдущее предложение"
override val stopReading = "Остановить чтение"
override val startReading = "Начать чтение"
override val nextSentence = "Следующее предложение"
override val readAloudSettings = "Настройки чтения вслух"
override val ttsEngine = "Движок TTS"
override val systemDefault = "Системный по умолчанию"
override val offlineVoice = "Офлайн-голос"
override val autoRussian = "Авто: русский"
override val loadingVoices = "Загружаем голоса..."
override val preparingSpeech = "Подготавливаем речь…"
override val offlineSpeechModelMissing = "Сначала загрузите офлайн-модель речи по инструкции для компьютера."
override val speechPlaybackFailed = "Не удалось воспроизвести речь. Проверьте офлайн-модель и вывод звука и попробуйте снова."
override val imageCopied = "Изображение скопировано"
override val copyFailed = "Не удалось скопировать"
override val zoomIn = "Увеличить"
override val zoomOut = "Уменьшить"
override val copyImage = "Скопировать изображение"
override val chooseLibraryFolder = "Выберите папку библиотеки"
override val downloads = "Загрузки"
override val otherFolder = "Другая папка..."
override val play = "Пуск"
override val stop = "Стоп"
override val previous = "Назад"
override val next = "Вперед"
override val readingInBackground = "Чтение в фоне"
override val shareBook = "Поделиться книгой"
override val couldNotLoadAndroidTtsEngines = "Не удалось загрузить Android TTS-движки."
override val couldNotLoadSelectedTtsVoices = "Не удалось загрузить голоса для выбранного TTS-движка."
override val noOfflineVoices = "Выбранный TTS-движок не сообщил об офлайн-голосах."
override fun themeChanged(mode: ThemeMode): String = "$theme: ${mode.localizedName()}"
override fun removed(title: String): String = "Удалено: $title."
override fun restored(title: String): String = "Восстановлено: $title."
override fun couldNotRemove(title: String): String = "Не удалось удалить: $title."
override fun couldNotOpen(displayName: String): String = "Не удалось открыть $displayName."
override fun couldNotReopenLastBook(): String = "Не удалось открыть последнюю книгу."
override fun scanReport(scanned: Int, imported: Int, skipped: Int, failed: Int): String =
"Проверено: $scanned, импортировано: $imported, пропущено: $skipped, ошибок: $failed."
override val noNewBooksFound = "Ничего нового не найдено."
override val show = "Показать"
override fun newBooksAdded(count: Int): String =
when {
count % 10 == 1 && count % 100 != 11 -> "$count новая книга добавлена."
count % 10 in 2..4 && count % 100 !in 12..14 -> "$count новые книги добавлены."
else -> "$count новых книг добавлено."
}
override fun rescanReport(scanned: Int, updated: Int, failed: Int): String =
"Пересканировано: $scanned, обновлено: $updated, ошибок: $failed."
override fun checkingFile(currentFile: String?, scanned: Int, imported: Int, skipped: Int, failed: Int): String =
buildString {
append("Проверяем")
currentFile?.takeIf(String::isNotBlank)?.let { append(" $it") }
append(". Найдено поддерживаемых файлов: $scanned")
append(", импортировано: $imported")
append(", пропущено: $skipped")
append(", ошибок: $failed.")
}
override fun importedSkippedFailed(imported: Int, skipped: Int, failed: Int): String =
"Импортировано: $imported, пропущено: $skipped, ошибок: $failed"
override fun scannedBooks(scanned: Int): String = "Импортировано книг: $scanned"
override fun scannedProgress(scanned: Int, total: Int, percent: Int): String =
"Импорт: $scanned из $total, готово $percent%"
override fun noBooksIn(filterLabel: String): String = "Нет книг: ${filterLabel.lowercase()}"
override fun bookMenuFor(title: String): String = "Меню книги: $title"
override fun markedAsRead(title: String?): String =
title?.let { "Отмечено как прочитанное: $it." } ?: "Отмечено как прочитанное."
override fun markedAsUnread(title: String): String = "Отмечено как непрочитанное: $title."
override fun markedToRead(title: String?): String =
title?.let { "Отложено к чтению: $it." } ?: "Отложено к чтению."
override fun markedNotInterested(title: String?): String =
title?.let { "Отмечено как неинтересное: $it." } ?: "Отмечено как неинтересное."
override fun removedMarks(title: String?): String =
title?.let { "Сняты отметки: $it." } ?: "Отметки сняты."
override fun addedToFavorites(title: String?): String =
title?.let { "Добавлено в избранное: $it." } ?: "Добавлено в избранное."
override fun removedFromFavorites(title: String?): String =
title?.let { "Убрано из избранного: $it." } ?: "Убрано из избранного."
override fun couldNotUpdate(title: String): String = "Не удалось обновить: $title."
override fun shareOpened(): String = "Открыто окно отправки."
override fun couldNotShareBook(): String = "Не удалось поделиться книгой."
override fun openedFileLocation(): String = "Расположение файла открыто."
override fun couldNotOpenFileLocation(): String = "Не удалось открыть расположение файла."
override fun allFilesAccessRequired(path: String): String = "Для сканирования $path нужен доступ ко всем файлам."
override fun couldNotRead(name: String): String = "Не удалось прочитать $name"
override fun progressLabel(progress: Double?): String =
progress?.let { "Прогресс ${(it * 100).roundToInt()}%" } ?: "Прогресс не записан"
override fun appLanguageName(language: AppLanguage): String =
when (language) {
AppLanguage.English -> "Английский"
AppLanguage.Russian -> "Русский"
}
override fun sectionFallback(index: Int): String = "Раздел ${index + 1}"
override fun readingStatus(status: BookReadingStatus): String =
when (status) {
BookReadingStatus.NEW -> "Новая"
BookReadingStatus.TO_READ -> "К чтению"
BookReadingStatus.READING -> "Читаю"
BookReadingStatus.READ -> "Прочитана"
BookReadingStatus.NOT_INTERESTED -> "Не интересно"
}
}
internal fun ThemeMode.localizedName(): String =
when (this) {
ThemeMode.LIGHT -> strings.themeLight
ThemeMode.DARK -> strings.themeDark
ThemeMode.SYSTEM -> strings.themeSystem
}
@@ -0,0 +1,67 @@
package net.sergeych.toread
internal data class ReadAloudBlockRange(val position: Int, val start: Int, val endExclusive: Int)
internal data class ReadAloudBlock(
val text: String,
val ranges: List<ReadAloudBlockRange>,
val pauseBeforeMillis: Long,
val pauseAfterMillis: Long,
) {
fun positionAt(offset: Int): Int =
ranges.lastOrNull { it.start <= offset }?.position ?: ranges.first().position
}
/** Paragraph-sized requests. Android's hard limit counts UTF-16 characters, never UTF-8 bytes.
* Whole sentences are retained whenever they fit. An oversized sentence is split at whitespace,
* or, for an oversized word, between Unicode code points (never inside a surrogate pair).
*/
internal fun readAloudBlocks(
sentences: List<ReadAloudSentence>,
startIndex: Int,
maxLength: Int,
): Sequence<ReadAloudBlock> = sequence {
require(maxLength >= 2)
var text = StringBuilder()
var ranges = mutableListOf<ReadAloudBlockRange>()
var before = 0L
var after = 0L
var item: Int? = null
suspend fun SequenceScope<ReadAloudBlock>.flush() {
if (ranges.isEmpty()) return
yield(ReadAloudBlock(text.toString(), ranges.toList(), before, after))
text = StringBuilder()
ranges = mutableListOf()
before = 0L
after = 0L
}
for (position in startIndex.coerceAtLeast(0) until sentences.size) {
val sentence = sentences[position]
if (item != sentence.itemIndex || sentence.pauseBeforeMillis > 0 || after > 0) flush()
item = sentence.itemIndex
var offset = 0
val spoken = sentence.spokenText
while (offset < spoken.length) {
// Do not split a sentence just to fill the previous block.
if (text.isNotEmpty() && text.length + 1 + spoken.length - offset > maxLength) flush()
if (ranges.isEmpty()) before = if (offset == 0) sentence.pauseBeforeMillis else 0L
var end = minOf(spoken.length, offset + maxLength)
if (end < spoken.length) {
if (spoken[end - 1].isHighSurrogate() && spoken[end].isLowSurrogate()) end--
val whitespace = (end - 1 downTo offset + 1).firstOrNull { spoken[it].isWhitespace() }
if (whitespace != null) end = whitespace
}
if (text.isNotEmpty()) text.append(' ')
val start = text.length
text.append(spoken.substring(offset, end))
ranges += ReadAloudBlockRange(position, start, text.length)
offset = end
while (offset < spoken.length && spoken[offset].isWhitespace()) offset++
after = if (offset == spoken.length) sentence.pauseAfterMillis else 0L
if (offset < spoken.length || text.length >= maxLength || after > 0) flush()
}
}
flush()
}
@@ -17,6 +17,7 @@ data class ReadAloudState(
val active: Boolean = false,
val playing: Boolean = false,
val sentenceIndex: Int? = null,
val buffering: Boolean = false,
)
data class ReadAloudEngineOption(
@@ -31,13 +32,21 @@ data class ReadAloudVoiceOption(
val networkRequired: Boolean,
)
data class ReadAloudTextReplacement(
val from: String,
val to: String,
val caseSensitive: Boolean = false,
)
data class ReadAloudSettingsState(
val engines: List<ReadAloudEngineOption> = emptyList(),
val selectedEngineId: String? = null,
val voices: List<ReadAloudVoiceOption> = emptyList(),
val selectedVoiceId: String? = null,
val textReplacements: List<ReadAloudTextReplacement> = emptyList(),
val loading: Boolean = false,
val message: String? = null,
val usesSystemDefaults: Boolean = true,
)
expect object ReadAloudPlatform {
@@ -45,7 +54,7 @@ expect object ReadAloudPlatform {
val state: StateFlow<ReadAloudState>
val settingsState: StateFlow<ReadAloudSettingsState>
fun prepare(bookTitle: String, sentences: List<ReadAloudSentence>, startIndex: Int)
fun prepare(fileId: String, bookTitle: String, sentences: List<ReadAloudSentence>, startIndex: Int)
fun play()
fun stop()
fun skip(delta: Int)
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -55,10 +55,10 @@ internal fun ScanScreen(
Scaffold(
topBar = {
CenterAlignedTopAppBar(
title = { Text("Scan") },
title = { Text(strings.scan) },
navigationIcon = {
IconButton(onClick = { onStateChange(AppState.Library(state.items, scanPath, message)) }) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back to library")
IconButton(onClick = { onStateChange(AppState.Library(state.items, scanPath, message, state.selectedFilter)) }) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = strings.backToLibrary)
}
},
colors = themedTopAppBarColors(),
@@ -83,21 +83,21 @@ internal fun ScanScreen(
OutlinedTextField(
value = scanPath,
onValueChange = { scanPath = it },
label = { Text("Root folder") },
label = { Text(strings.rootFolder) },
singleLine = true,
modifier = Modifier.fillMaxWidth(),
)
Row(horizontalArrangement = Arrangement.spacedBy(10.dp), verticalAlignment = Alignment.CenterVertically) {
Button(
onClick = {
message = "Scanning..."
message = strings.scanning
onStartScan(scanPath)
},
enabled = !busy && scanPath.isNotBlank(),
) {
Icon(Icons.Filled.Scanner, contentDescription = null)
Spacer(Modifier.width(8.dp))
Text("Scan")
Text(strings.scan)
}
FilledTonalButton(
onClick = {
@@ -109,7 +109,7 @@ internal fun ScanScreen(
) {
Icon(Icons.Filled.FolderOpen, contentDescription = null)
Spacer(Modifier.width(8.dp))
Text("Choose")
Text(strings.choose)
}
if (busy) {
CircularProgressIndicator(modifier = Modifier.width(24.dp).height(24.dp), strokeWidth = 2.dp)
@@ -126,7 +126,7 @@ internal fun ScanScreen(
Text(it, style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.secondary)
}
libraryLogPath()?.let {
Text("Log: $it", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline)
Text("${strings.logPrefix}: $it", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline)
}
}
}
@@ -136,12 +136,4 @@ internal fun ScanScreen(
}
private fun LibraryScanProgress.toScanMessage(): String =
buildString {
append("Checking")
currentFile?.takeIf(String::isNotBlank)?.let { append(" $it") }
append(". Found $scannedFiles supported files")
append(", imported $importedFiles")
append(", skipped $skippedFiles")
append(", failed $failedFiles")
append(".")
}
strings.checkingFile(currentFile, scannedFiles, importedFiles, skippedFiles, failedFiles)
@@ -1,5 +1,6 @@
package net.sergeych.toread
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
@@ -22,15 +23,48 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.lerp
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt
@Composable
internal fun LoadingScreen(message: String) {
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(14.dp)) {
Box(
Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background),
contentAlignment = Alignment.Center,
) {
LoadingPanel(message)
}
}
@Composable
internal fun LoadingOverlay(message: String, modifier: Modifier = Modifier) {
Box(
modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background.copy(alpha = 0.58f)),
contentAlignment = Alignment.Center,
) {
LoadingPanel(message)
}
}
@Composable
private fun LoadingPanel(message: String) {
Card(
shape = RoundedCornerShape(8.dp),
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
) {
Column(
modifier = Modifier.padding(horizontal = 20.dp, vertical = 18.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
CircularProgressIndicator()
Text(message, style = MaterialTheme.typography.bodyMedium)
}
@@ -42,10 +76,10 @@ internal fun ErrorScreen(message: String, onBack: () -> Unit) {
Box(Modifier.fillMaxSize().padding(24.dp), contentAlignment = Alignment.Center) {
Card(shape = RoundedCornerShape(8.dp), colors = quietCardColors()) {
Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("Library error", style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold)
Text(strings.libraryError, style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold)
Text(message, style = MaterialTheme.typography.bodyMedium)
TextButton(onClick = onBack) {
Text("Retry")
Text(strings.retry)
}
}
}
@@ -107,9 +141,27 @@ internal fun themedTopAppBarColors(): TopAppBarColors =
@Composable
internal fun readerBackground(): Brush = SolidColor(MaterialTheme.colorScheme.background)
internal fun Long.toComposeColor(): Color =
Color(
alpha = ((this shr 24) and 0xFF).toInt(),
red = ((this shr 16) and 0xFF).toInt(),
green = ((this shr 8) and 0xFF).toInt(),
blue = (this and 0xFF).toInt(),
)
@Composable
internal fun readerImageBackgroundColor(): Color {
val surface = MaterialTheme.colorScheme.surface
return if (surface.isVisuallyDark()) lerp(surface, Color.White, 0.5f) else surface
}
private fun Color.isVisuallyDark(): Boolean {
val luminance = 0.2126f * red + 0.7152f * green + 0.0722f * blue
return luminance < 0.5f
}
internal fun Int.formatCompact(): String =
if (this >= 10_000) "${(this / 1000.0).roundToInt()}k" else toString()
internal fun Double?.progressLabel(): String =
this?.let { "Progress ${(it * 100).roundToInt()}%" } ?: "Progress not recorded"
strings.progressLabel(this)
@@ -1,7 +1,10 @@
package net.sergeych.toread
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.graphics.Color
internal val LocalReaderThemeIsDark = compositionLocalOf { false }
internal fun ThemeMode.next(): ThemeMode =
when (this) {
ThemeMode.SYSTEM -> ThemeMode.LIGHT
@@ -9,13 +12,6 @@ internal fun ThemeMode.next(): ThemeMode =
ThemeMode.DARK -> ThemeMode.SYSTEM
}
internal val ThemeMode.displayName: String
get() = when (this) {
ThemeMode.LIGHT -> "Light"
ThemeMode.DARK -> "Dark"
ThemeMode.SYSTEM -> "System"
}
internal fun lightReaderColorScheme() = androidx.compose.material3.lightColorScheme(
primary = Color(0xFF425D56),
onPrimary = Color.White,
@@ -0,0 +1,41 @@
package net.sergeych.toread
import net.sergeych.toread.storage.BookReadingStatus
import kotlin.test.Test
import kotlin.test.assertEquals
class LibraryScreenItemListTest {
@Test
fun myLibraryShowsAllBooksExceptNotInterested() {
val items = listOf(
libraryItem("file-reading", "Reading", BookReadingStatus.READING),
libraryItem("file-to-read", "To Read", BookReadingStatus.TO_READ),
libraryItem("file-read", "Read", BookReadingStatus.READ),
libraryItem("file-new", "New", BookReadingStatus.NEW),
libraryItem("file-not-interested", "Not Interested", BookReadingStatus.NOT_INTERESTED),
)
val filtered = items.myLibraryItems()
assertEquals(
listOf("file-new", "file-read", "file-reading", "file-to-read"),
filtered.map { it.fileId },
)
}
private fun libraryItem(
fileId: String,
title: String,
readingStatus: BookReadingStatus = BookReadingStatus.NEW,
): LibraryItem =
LibraryItem(
fileId = fileId,
bookId = "book-$fileId",
title = title,
format = "fb2",
sizeBytes = null,
storageUri = null,
lastSeenAt = null,
readingStatus = readingStatus,
)
}
@@ -0,0 +1,58 @@
package net.sergeych.toread
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class ReadAloudBlocksTest {
private fun sentence(index: Int, item: Int, text: String, before: Long = 0, after: Long = 0) =
ReadAloudSentence(index, item, 0, text.length, text, pauseBeforeMillis = before, pauseAfterMillis = after)
@Test
fun paragraphsKeepContextAndRangesUseSpokenText() {
val queue = listOf(
sentence(0, 1, "Первое.").copy(spokenText = "Пе́рвое."),
sentence(1, 1, "Второе."), sentence(2, 2, "Другой абзац."),
)
val blocks = readAloudBlocks(queue, 0, 4_000).toList()
assertEquals(listOf("Пе́рвое. Второе.", "Другой абзац."), blocks.map { it.text })
assertEquals(0, blocks[0].positionAt(0))
assertEquals(1, blocks[0].positionAt(9))
assertEquals(listOf("Второе.", "Другой абзац."), readAloudBlocks(queue, 1, 4_000).map { it.text }.toList())
}
@Test
fun longParagraphBreaksBetweenSentencesAndCountsCharacters() {
val queue = (0..9).map { sentence(it, 1, "я".repeat(998) + ".") }
val blocks = readAloudBlocks(queue, 0, 4_000).toList()
assertEquals(listOf(3_999, 3_999, 1_999), blocks.map { it.text.length })
assertEquals((0..9).toList(), blocks.flatMap { it.ranges }.map { it.position })
}
@Test
fun oversizedSentenceSplitsAtWordsAndNeverCutsSurrogatePairs() {
val words = List(20) { "Привет😀" }
val blocks = readAloudBlocks(listOf(sentence(0, 0, words.joinToString(" "))), 0, 30).toList()
assertEquals(words.joinToString(" "), blocks.joinToString(" ") { it.text })
assertTrue(blocks.all { it.text.length <= 30 })
val cyrillic = "я".repeat(9_001) + "."
val cyrillicBlocks = readAloudBlocks(listOf(sentence(0, 0, cyrillic)), 0, 4_000).toList()
assertEquals(cyrillic, cyrillicBlocks.joinToString("") { it.text })
assertEquals(listOf(4_000, 4_000, 1_002), cyrillicBlocks.map { it.text.length })
val giantWord = "😀".repeat(4_001)
val split = readAloudBlocks(listOf(sentence(0, 0, giantWord)), 0, 3_999).toList()
assertEquals(giantWord, split.joinToString("") { it.text })
assertTrue(split.all { it.text.length <= 3_999 && !it.text.last().isHighSurrogate() })
}
@Test
fun pausesAndStartingPositionArePreserved() {
val queue = listOf(sentence(0, 0, "Skip."), sentence(1, 0, "Heading.", 1000, 600),
sentence(2, 1, "Wait.", after = 350), sentence(3, 1, "Go."))
val blocks = readAloudBlocks(queue, 1, 4_000).toList()
assertEquals(listOf("Heading.", "Wait.", "Go."), blocks.map { it.text })
assertEquals(listOf(1000L, 0L, 0L), blocks.map { it.pauseBeforeMillis })
assertEquals(listOf(600L, 350L, 0L), blocks.map { it.pauseAfterMillis })
assertEquals(listOf(1, 2, 3), blocks.map { it.ranges.first().position })
}
}
@@ -4,7 +4,13 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import net.sergeych.toread.fb2.Fb2Block
import net.sergeych.toread.fb2.Fb2Book
import net.sergeych.toread.fb2.Fb2Epigraph
import net.sergeych.toread.fb2.Fb2EpigraphBlock
import net.sergeych.toread.fb2.Fb2ImageRef
import net.sergeych.toread.fb2.Fb2Poem
import net.sergeych.toread.fb2.Fb2PoemBlock
import net.sergeych.toread.fb2.Fb2Section
import net.sergeych.toread.fb2.Fb2Stanza
import net.sergeych.toread.fb2.Fb2Text
import net.sergeych.toread.fb2.Fb2TextSpan
@@ -53,6 +59,320 @@ class ReadAloudContentPlanTest {
assertEquals(listOf(350L, 350L, 350L, 0L), plan.sentences.map { it.pauseAfterMillis })
}
@Test
fun dotsAfterQuestionOrExclamationAreRemovedForSpeech() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(blocks = listOf(paragraph("What?.. No!.. Done."))),
),
),
)
assertEquals(
listOf("What?..", "No!..", "Done."),
plan.sentences.map { it.text },
)
assertEquals(
listOf("What?", "No!", "Done."),
plan.sentences.map { it.spokenText },
)
}
@Test
fun hardcodedReadAloudReplacementCanMarkStress() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(blocks = listOf(paragraph("Господа, идите."))),
),
),
)
assertEquals("Господа, идите.", plan.sentences.single().text)
assertEquals("Господа́, идите.", plan.sentences.single().spokenText)
}
@Test
fun hardcodedReadAloudReplacementsAreCaseSensitive() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(blocks = listOf(paragraph("ГОСПОДА, идите."))),
),
),
)
assertEquals("ГОСПОДА, идите.", plan.sentences.single().spokenText)
}
@Test
fun stressMarkersSupportRussianAndEnglishVowels() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(
blocks = listOf(
paragraph(
"/а/е/ё/и/о/у/ы/э/ю/я " +
"/А/Е/Ё/И/О/У/Ы/Э/Ю/Я " +
"'a'e'i'o'u'y " +
"'A'E'I'O'U'Y.",
),
),
),
),
),
)
assertEquals(
"а́е́ё́и́о́у́ы́э́ю́я́ " +
"А́Е́Ё́И́О́У́Ы́Э́Ю́Я́ " +
"áéíóúý " +
"ÁÉÍÓÚÝ.",
plan.sentences.single().spokenText,
)
}
@Test
fun stressMarkersIgnoreNonStressableLetters() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(blocks = listOf(paragraph("/б 'z /1."))),
),
),
)
assertEquals("/б 'z /1.", plan.sentences.single().spokenText)
}
@Test
fun poemVersesAreIncludedInReadAloudPlan() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(
blocks = listOf(
Fb2Block.Poem(
Fb2Poem(
blocks = listOf(
Fb2PoemBlock.Stanza(
Fb2Stanza(
verses = listOf(
text("Line one."),
text("Line two."),
),
),
),
),
),
),
),
),
),
),
)
assertEquals(listOf("Line one.", "Line two."), plan.sentences.map { it.text })
assertEquals(1, plan.elements.filterIsInstance<ReaderElement.Poem>().size)
}
@Test
fun epigraphsAreIncludedInReaderPlan() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
bodyEpigraphs = listOf(
Fb2Epigraph(
blocks = listOf(Fb2EpigraphBlock.Paragraph(text("Body quote."))),
textAuthors = listOf(text("Body author.")),
),
),
sections = listOf(
Fb2Section(
blocks = listOf(
Fb2Block.Epigraph(
Fb2Epigraph(
blocks = listOf(Fb2EpigraphBlock.Paragraph(text("Section quote."))),
textAuthors = listOf(text("Section author.")),
),
),
),
),
),
),
)
assertEquals(2, plan.elements.filterIsInstance<ReaderElement.Epigraph>().size)
assertEquals(
listOf("Body quote.", "Body author.", "Section quote.", "Section author."),
plan.sentences.map { it.text },
)
}
@Test
fun wrapperOnlyTopSectionDoesNotIndentChildParagraphs() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(
title = "Wrapper title",
sections = listOf(
Fb2Section(
title = "Chapter",
blocks = listOf(paragraph("Chapter text.")),
),
),
),
),
),
)
assertEquals(0, plan.elements.filterIsInstance<ReaderElement.Paragraph>().single().depth)
assertEquals(
listOf(0, 0),
plan.elements.filterIsInstance<ReaderElement.SectionTitle>().map { it.depth },
)
}
@Test
fun titleOnlyPartSectionDoesNotIndentChaptersAfterFrontMatter() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(
title = "Copyright",
blocks = listOf(paragraph("Copyright text.")),
),
Fb2Section(
title = "Part one",
sections = listOf(
Fb2Section(
title = "Chapter one",
blocks = listOf(paragraph("Chapter text.")),
),
),
),
),
),
)
assertEquals(
listOf(0, 0),
plan.elements.filterIsInstance<ReaderElement.Paragraph>().map { it.depth },
)
assertEquals(
listOf(0, 0, 0),
plan.elements.filterIsInstance<ReaderElement.SectionTitle>().map { it.depth },
)
}
@Test
fun imageOnlyPartStructureDoesNotIndentWholeBook() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(
title = "Wrapper title",
sections = listOf(
Fb2Section(
images = listOf(Fb2ImageRef("#front.jpg")),
),
Fb2Section(
title = "Part one",
blocks = listOf(Fb2Block.Image(Fb2ImageRef("#part.png"))),
sections = listOf(
Fb2Section(
title = "Chapter one",
blocks = listOf(paragraph("Chapter text.")),
),
),
),
),
),
),
),
)
assertEquals(0, plan.elements.filterIsInstance<ReaderElement.Paragraph>().single().depth)
assertEquals(
listOf(0, 0, 0),
plan.elements.filterIsInstance<ReaderElement.SectionTitle>().map { it.depth },
)
assertEquals(listOf(0, 0, 0, 0), plan.tocEntries.map { it.depth })
}
@Test
fun realNestedSectionsKeepRelativeIndent() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(
title = "Part",
blocks = listOf(paragraph("Part text.")),
sections = listOf(
Fb2Section(
title = "Nested chapter",
blocks = listOf(paragraph("Nested text.")),
),
),
),
),
),
)
assertEquals(
listOf(0, 1),
plan.elements.filterIsInstance<ReaderElement.Paragraph>().map { it.depth },
)
assertEquals(
listOf(0, 1),
plan.elements.filterIsInstance<ReaderElement.SectionTitle>().map { it.depth },
)
}
@Test
fun contentPlanIncludesClickableTableOfContentsEntries() {
val plan = buildReaderContentPlan(
Fb2Book(
title = "Book",
sections = listOf(
Fb2Section(
title = "Part",
sections = listOf(
Fb2Section(
title = "Chapter",
blocks = listOf(paragraph("Chapter text.")),
),
),
),
Fb2Section(blocks = listOf(paragraph("Untitled text."))),
),
),
)
assertEquals(listOf("Part", "Chapter", "Section 2"), plan.tocEntries.map { it.title })
assertEquals(listOf(0, 0, 0), plan.tocEntries.map { it.depth })
assertEquals(
plan.elements.filterIsInstance<ReaderElement.SectionTitle>().first(),
plan.elements[plan.tocEntries.first().itemIndex],
)
}
private fun paragraph(text: String): Fb2Block.Paragraph =
Fb2Block.Paragraph(Fb2Text(listOf(Fb2TextSpan(text))))
Fb2Block.Paragraph(text(text))
private fun text(text: String): Fb2Text =
Fb2Text(listOf(Fb2TextSpan(text)))
}
@@ -0,0 +1,24 @@
package net.sergeych.toread
import kotlin.test.Test
import kotlin.test.assertEquals
class ReaderLinkDetectionTest {
@Test
fun detectsWebUrlsAndTrimsSentencePunctuation() {
val text = "See http://example.org/path?q=1, then https://site.test/end."
val urls = text.webUrlRanges().map { text.substring(it) }
assertEquals(listOf("http://example.org/path?q=1", "https://site.test/end"), urls)
}
@Test
fun ignoresNonWebUrls() {
val text = "ftp://files.test https://secure.test"
val urls = text.webUrlRanges().map { text.substring(it) }
assertEquals(listOf("https://secure.test"), urls)
}
}
@@ -20,6 +20,7 @@ import java.awt.datatransfer.Transferable
import java.awt.datatransfer.UnsupportedFlavorException
import java.io.File
import java.io.ByteArrayInputStream
import java.net.URI
import javax.imageio.ImageIO
import java.text.SimpleDateFormat
import java.util.Date
@@ -36,6 +37,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
private var pendingOpenBookFile: File? = null
fun rememberPlatformOpenBookArguments(args: Array<String>) {
pendingOpenBookFile = args.asSequence()
.map(::File)
.firstOrNull { it.isFile && it.name.isSupportedBookFile() }
}
actual fun loadDefaultBookBytes(): ByteArray? {
var current = File(System.getProperty("user.dir")).absoluteFile
while (true) {
@@ -70,12 +79,31 @@ actual suspend fun copyImageToClipboard(bytes: ByteArray, mimeType: String, labe
actual fun defaultLibraryScanPath(): String? = findProjectRoot()?.let { File(it, "test_books").absolutePath }
actual suspend fun loadPlatformOpenBookRequest(): PlatformOpenBookRequest? = null
actual suspend fun loadPlatformOpenBookRequest(): PlatformOpenBookRequest? = withContext(Dispatchers.IO) {
val file = pendingOpenBookFile ?: return@withContext null
pendingOpenBookFile = null
val bytes = file.readBytes()
val importResult = openLibraryDatabase().useLibrary { db ->
LibraryScanner(db, ::appendLibraryLog).importExternalFileAndGetResult(
bytes = bytes,
displayName = file.name,
storageUri = file.absolutePath,
sizeBytes = file.length(),
lastModifiedMillis = file.lastModified(),
)
}
appendLibraryLog("platform open imported=${importResult.imported} fileId=${importResult.fileId} path=${file.absolutePath}")
PlatformOpenBookRequest(
id = importResult.fileId,
displayName = file.name,
bytes = bytes,
)
}
actual suspend fun chooseLibraryScanDirectory(): String? = withContext(Dispatchers.IO) {
val chooser = JFileChooser(defaultLibraryScanPath())
chooser.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
chooser.dialogTitle = "Choose library folder"
chooser.dialogTitle = strings.chooseLibraryFolder
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
chooser.selectedFile.absolutePath
} else {
@@ -293,28 +321,76 @@ actual suspend fun markLibraryFavorite(fileId: String, favorite: Boolean): Boole
actual suspend fun shareLibraryBookFile(fileId: String): Boolean = false
actual suspend fun viewLibraryBookFile(fileId: String): Boolean = withContext(Dispatchers.IO) {
actual suspend fun viewLibraryBookFile(folder: String, fileName: String): Boolean = withContext(Dispatchers.IO) {
runCatching {
if (!Desktop.isDesktopSupported()) return@withContext false
val file = openLibraryDatabase().useLibrary { db ->
db.files.get(fileId)?.storageUri?.let(::File)?.takeIf { it.isFile }
} ?: return@withContext false
val desktop = Desktop.getDesktop()
if (desktop.isSupported(Desktop.Action.BROWSE_FILE_DIR)) {
desktop.browseFileDirectory(file)
} else {
desktop.open(file.parentFile ?: file)
}
true
val directory = File(folder).takeIf { it.isDirectory } ?: return@withContext false
val file = File(directory, fileName).takeIf { it.isFile } ?: return@withContext false
selectOrOpenBookFile(directory, file)
}.getOrDefault(false)
}
actual fun openPlatformUrl(url: String): Boolean =
runCatching {
if (!Desktop.isDesktopSupported() || !Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
return@runCatching false
}
Desktop.getDesktop().browse(URI(url))
true
}.getOrDefault(false)
private fun selectOrOpenBookFile(directory: File, file: File): Boolean {
if (isLinuxDesktop() && trySelectLinuxFile(file)) return true
if (!Desktop.isDesktopSupported()) return false
val desktop = Desktop.getDesktop()
return when {
desktop.isSupported(Desktop.Action.BROWSE_FILE_DIR) -> {
desktop.browseFileDirectory(file)
true
}
desktop.isSupported(Desktop.Action.OPEN) -> {
desktop.open(directory)
true
}
else -> false
}
}
private fun isLinuxDesktop(): Boolean =
System.getProperty("os.name").orEmpty().lowercase(Locale.ROOT).contains("linux")
private fun trySelectLinuxFile(file: File): Boolean =
runCatching {
val process = ProcessBuilder(
"dbus-send",
"--session",
"--dest=org.freedesktop.FileManager1",
"--type=method_call",
"/org/freedesktop/FileManager1",
"org.freedesktop.FileManager1.ShowItems",
"array:string:${file.toURI()}",
"string:",
).start()
if (process.waitFor(2, TimeUnit.SECONDS)) {
process.exitValue() == 0
} else {
process.destroyForcibly()
false
}
}.getOrDefault(false)
actual suspend fun loadBookInfoExtras(fileId: String): BookInfoExtras = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
val file = db.files.get(fileId) ?: return@useLibrary BookInfoExtras()
val clusterId = file.bodyClusterId ?: return@useLibrary BookInfoExtras()
val sourceFileName = file.originalFilename ?: file.storageUri?.substringAfterLast(File.separatorChar)
val clusterId = file.bodyClusterId ?: return@useLibrary BookInfoExtras(
sourceFileName = sourceFileName,
sourceFilePath = file.storageUri,
)
val readingPosition = db.readingStates.getForBodyCluster(clusterId)?.anchor?.formatHintsJson?.toReadingPosition()
BookInfoExtras(
sourceFileName = sourceFileName,
sourceFilePath = file.storageUri,
bookmarks = db.bookmarks.listForBodyCluster(clusterId).map {
BookmarkInfo(
title = it.title,
@@ -361,6 +437,42 @@ actual suspend fun saveThemeMode(mode: ThemeMode) = withContext(Dispatchers.IO)
}
}
actual suspend fun loadReaderFontSettings(): ReaderFontSettings = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
readerFontSettingsFromStorageValue(db.getAppFlag(ReaderFontSettingsFlag))
}
}
actual suspend fun saveReaderFontSettings(settings: ReaderFontSettings) = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.setAppFlag(ReaderFontSettingsFlag, settings.toStorageValue())
}
}
actual suspend fun loadReaderFullScreen(): Boolean = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.getAppFlag(ReaderFullScreenFlag)?.toBooleanStrictOrNull() ?: false
}
}
actual suspend fun saveReaderFullScreen(fullScreen: Boolean) = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.setAppFlag(ReaderFullScreenFlag, fullScreen.toString())
}
}
actual suspend fun loadReaderPagingOnly(): Boolean = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.getAppFlag(ReaderPagingOnlyFlag)?.toBooleanStrictOrNull() ?: false
}
}
actual suspend fun saveReaderPagingOnly(enabled: Boolean) = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.setAppFlag(ReaderPagingOnlyFlag, enabled.toString())
}
}
actual suspend fun loadScanDownloadsAutomatically(): Boolean = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.getAppFlag(ScanDownloadsAutomaticallyFlag)?.toBooleanStrictOrNull() ?: true
@@ -373,6 +485,41 @@ actual suspend fun saveScanDownloadsAutomatically(enabled: Boolean) = withContex
}
}
internal actual suspend fun loadLibraryFilter(): LibraryFilter = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
val saved = db.getAppFlag(LibraryFilterFlag)
when {
saved == null -> {
appendLibraryLog("warning library filter was not saved; using ${LibraryFilter.MyLibrary.name}")
LibraryFilter.MyLibrary
}
else -> runCatching { LibraryFilter.valueOf(saved) }
.getOrElse {
appendLibraryLog("warning invalid saved library filter '$saved'; using ${LibraryFilter.MyLibrary.name}")
LibraryFilter.MyLibrary
}
}
}
}
internal actual suspend fun saveLibraryFilter(filter: LibraryFilter) = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.setAppFlag(LibraryFilterFlag, filter.name)
}
}
actual suspend fun loadAppLocaleTag(): String? = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.getAppFlag(AppLocaleTagFlag)?.takeIf { it.isNotBlank() }
}
}
actual suspend fun saveAppLocaleTag(localeTag: String?) = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.setAppFlag(AppLocaleTagFlag, localeTag?.takeIf { it.isNotBlank() })
}
}
actual suspend fun loadDownloadsWasScanned(): Boolean = withContext(Dispatchers.IO) {
openLibraryDatabase().useLibrary { db ->
db.getAppFlag(DownloadsWasScannedFlag)?.toBooleanStrictOrNull()
@@ -562,14 +709,37 @@ private fun libraryLogFile(): File =
File(System.getProperty("user.home"), ".toread/toread.log")
private fun ReadingPosition.toFormatHintsJson(): String =
"""{"firstVisibleItemIndex":$itemIndex,"firstVisibleItemScrollOffset":$scrollOffset}"""
buildString {
append("""{"firstVisibleItemIndex":$itemIndex,"firstVisibleItemScrollOffset":$scrollOffset""")
readAloudSentenceIndex?.let { append(""","readAloudSentenceIndex":$it""") }
if (backStack.isNotEmpty()) {
append(""","backStack":[""")
append(backStack.take(MaxStoredReadingBackStack).joinToString(",") { it.toFormatHintsJson() })
append("]")
}
append("}")
}
private fun String.toReadingPosition(): ReadingPosition? {
val position = toReadingPositionObject() ?: return null
val backStackJson = Regex(""""backStack"\s*:\s*\[(.*)]""").find(this)?.groupValues?.getOrNull(1)
val backStack = backStackJson
?.let { ReadingPositionObjectRegex.findAll(it).mapNotNull { match -> match.value.toReadingPositionObject() }.toList() }
.orEmpty()
.take(MaxStoredReadingBackStack)
return position.copy(backStack = backStack)
}
private fun String.toReadingPositionObject(): ReadingPosition? {
val index = Regex(""""firstVisibleItemIndex"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
val offset = Regex(""""firstVisibleItemScrollOffset"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
return if (index != null && offset != null) ReadingPosition(index, offset) else null
val sentenceIndex = Regex(""""readAloudSentenceIndex"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
return if (index != null && offset != null) ReadingPosition(index, offset, sentenceIndex) else null
}
private val ReadingPositionObjectRegex = Regex("""[{][^{}]*[}]""")
private const val MaxStoredReadingBackStack = 10
private fun String.toSearchPrefixes(): List<String> =
SearchPrefixRegex.findAll(lowercase()).map { it.value }.distinct().toList()
@@ -586,7 +756,15 @@ private fun runCommand(vararg command: String): String? =
}
}.getOrNull()
private fun String.isSupportedBookFile(): Boolean =
endsWith(".fb2", ignoreCase = true) || endsWith(".fb2.zip", ignoreCase = true)
private const val ActiveReadingFileIdFlag = "active_reading_file_id"
private const val ThemeModeFlag = "theme_mode"
private const val ReaderFontSettingsFlag = "reader_font_settings"
private const val ReaderFullScreenFlag = "reader_full_screen"
private const val ReaderPagingOnlyFlag = "reader_paging_only"
private const val ScanDownloadsAutomaticallyFlag = "scan_downloads_automatically"
private const val LibraryFilterFlag = "library_filter"
private const val AppLocaleTagFlag = "app_locale_tag"
private const val DownloadsWasScannedFlag = "downloads_was_scanned"
@@ -0,0 +1,6 @@
package net.sergeych.toread
import java.util.Locale
internal actual fun platformLocaleTags(): List<String> =
listOf(Locale.getDefault().toLanguageTag())
@@ -1,20 +1,54 @@
package net.sergeych.toread
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import net.sergeych.toread.tts.DesktopReadAloudPlayer
import net.sergeych.toread.tts.SpeechAudio
import net.sergeych.toread.tts.RuAccentProcess
import net.sergeych.toread.tts.SileroProcess
import net.sergeych.toread.tts.SupertonicTts
import net.sergeych.toread.tts.playDesktopAudio
actual object ReadAloudPlatform {
actual val isSupported: Boolean = false
private val mutableState = MutableStateFlow(ReadAloudState())
actual val state: StateFlow<ReadAloudState> = mutableState
private val mutableSettingsState = MutableStateFlow(ReadAloudSettingsState())
actual val settingsState: StateFlow<ReadAloudSettingsState> = mutableSettingsState
actual val isSupported: Boolean = true
private val inferenceLock = Mutex()
private var synthesizer: SupertonicTts? = null
private val accentor = if (RuAccentProcess.isInstalled()) RuAccentProcess() else null
private val silero = if (SileroProcess.isInstalled()) SileroProcess() else null
private val player = DesktopReadAloudPlayer(
scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate),
synthesize = { text, voice ->
withContext(Dispatchers.IO) {
inferenceLock.withLock {
val context = currentCoroutineContext()
context.ensureActive()
val engine = synthesizer ?: SupertonicTts().also { synthesizer = it }
context.ensureActive()
SpeechAudio(engine.synthesize(text, voice, checkCancelled = { context.ensureActive() }), engine.sampleRate)
}
}
},
playAudio = ::playDesktopAudio,
installed = { SupertonicTts.isInstalled() },
accentuate = accentor?.let { processor -> { text -> processor.accentuate(text) } },
silero = silero?.let { processor -> { text, voice -> processor.synthesize(text, voice) } },
)
actual val state: StateFlow<ReadAloudState> = player.state
actual val settingsState: StateFlow<ReadAloudSettingsState> = player.settingsState
actual fun prepare(bookTitle: String, sentences: List<ReadAloudSentence>, startIndex: Int) = Unit
actual fun play() = Unit
actual fun stop() = Unit
actual fun skip(delta: Int) = Unit
actual fun refreshSettings() = Unit
actual fun selectEngine(engineId: String?) = Unit
actual fun selectVoice(voiceId: String?) = Unit
actual fun prepare(fileId: String, bookTitle: String, sentences: List<ReadAloudSentence>, startIndex: Int) =
player.prepare(sentences, startIndex)
actual fun play() = player.play()
actual fun stop() = player.stop()
actual fun skip(delta: Int) = player.skip(delta)
actual fun refreshSettings() = player.refreshSettings()
actual fun selectEngine(engineId: String?) = player.selectEngine(engineId)
actual fun selectVoice(voiceId: String?) = player.selectVoice(voiceId)
}
@@ -3,11 +3,14 @@ package net.sergeych.toread
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
fun main() = application {
Window(
onCloseRequest = ::exitApplication,
title = "toread",
) {
App()
fun main(args: Array<String>) {
rememberPlatformOpenBookArguments(args)
application {
Window(
onCloseRequest = ::exitApplication,
title = strings.appName,
) {
App()
}
}
}
@@ -0,0 +1,70 @@
package net.sergeych.toread.tts
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.withContext
import java.io.ByteArrayInputStream
import java.io.File
import javax.sound.sampled.AudioFileFormat
import javax.sound.sampled.AudioFormat
import javax.sound.sampled.AudioInputStream
import javax.sound.sampled.AudioSystem
import javax.sound.sampled.LineEvent
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.coroutines.resume
internal fun SpeechAudio.pcm16(): ByteArray = ByteArray(samples.size * 2).also { bytes ->
samples.forEachIndexed { index, sample ->
val pcm = (sample.takeIf { it.isFinite() }?.coerceIn(-1f, 1f)?.times(32767) ?: 0f).toInt()
bytes[index * 2] = pcm.toByte()
bytes[index * 2 + 1] = (pcm shr 8).toByte()
}
}
internal suspend fun playDesktopAudio(audio: SpeechAudio) = withContext(Dispatchers.IO) {
val format = AudioFormat(audio.sampleRate.toFloat(), 16, 1, true, false)
val pcm = audio.pcm16()
val clip = AudioSystem.getClip()
try {
currentCoroutineContext().ensureActive()
clip.open(format, pcm, 0, pcm.size)
suspendCancellableCoroutine<Unit> { continuation ->
val completed = AtomicBoolean(false)
clip.addLineListener { event ->
if (event.type == LineEvent.Type.STOP && completed.compareAndSet(false, true)) {
continuation.resume(Unit)
}
}
continuation.invokeOnCancellation {
clip.stop()
clip.close()
}
if (continuation.isActive) clip.start()
}
} finally {
clip.stop()
clip.close()
}
}
internal fun SpeechAudio.writeWav(file: File) {
file.parentFile?.mkdirs()
val format = AudioFormat(sampleRate.toFloat(), 16, 1, true, false)
AudioInputStream(ByteArrayInputStream(pcm16()), format, samples.size.toLong()).use {
AudioSystem.write(it, AudioFileFormat.Type.WAVE, file)
}
}
internal fun joinSpeechAudio(parts: List<SpeechAudio>): SpeechAudio {
require(parts.isNotEmpty()) { "No speech to play" }
if (parts.size == 1) return parts.single()
val rate = parts.first().sampleRate
require(parts.all { it.sampleRate == rate }) { "Mismatched speech sample rates" }
val pause = rate / 10
val samples = FloatArray(parts.sumOf { it.samples.size } + pause * (parts.size - 1))
var offset = 0
parts.forEach { part -> part.samples.copyInto(samples, offset); offset += part.samples.size + pause }
return SpeechAudio(samples, rate)
}
@@ -0,0 +1,158 @@
package net.sergeych.toread.tts
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import net.sergeych.toread.ReadAloudEngineOption
import net.sergeych.toread.ReadAloudSentence
import net.sergeych.toread.ReadAloudSettingsState
import net.sergeych.toread.ReadAloudState
import net.sergeych.toread.ReadAloudVoiceOption
import net.sergeych.toread.strings
internal data class SpeechAudio(val samples: FloatArray, val sampleRate: Int)
/** Commands and state changes run on the UI dispatcher; inference/audio run off-thread.
* A playback job owns its lookahead, so stop/skip/voice changes cannot publish stale audio.
*/
internal class DesktopReadAloudPlayer(
private val scope: CoroutineScope,
private val synthesize: suspend (String, String) -> SpeechAudio,
private val playAudio: suspend (SpeechAudio) -> Unit,
private val installed: () -> Boolean,
private val accentuate: (suspend (String) -> String)? = null,
private val silero: (suspend (String, String) -> SpeechAudio)? = null,
) {
private fun voices(engine: String) = if (engine == "silero")
SileroProcess.voices.map { ReadAloudVoiceOption(it, it.replaceFirstChar(Char::uppercase), "ru (English: F1)", false) }
else SupertonicTts.voices.map { ReadAloudVoiceOption(it, it, "en / ru", false) }
private val defaultEngine = if (silero != null) "silero" else if (accentuate != null) "supertonic-3-ruaccent" else "supertonic-3"
private val mutableState = MutableStateFlow(ReadAloudState())
val state = mutableState.asStateFlow()
private val mutableSettings = MutableStateFlow(ReadAloudSettingsState(
engines = (if (silero != null) listOf(ReadAloudEngineOption("silero", "Silero (RU) / Supertonic (EN)")) else emptyList()) +
listOf(ReadAloudEngineOption("supertonic-3", "Supertonic 3")) +
if (accentuate != null) listOf(ReadAloudEngineOption("supertonic-3-ruaccent", "Supertonic 3 + RUAccent")) else emptyList(),
selectedEngineId = defaultEngine,
voices = voices(defaultEngine),
selectedVoiceId = if (silero != null) "xenia" else "F1",
usesSystemDefaults = false,
))
val settingsState = mutableSettings.asStateFlow()
private var queue = emptyList<ReadAloudSentence>()
private var index = 0
private var playback: Job? = null
fun prepare(sentences: List<ReadAloudSentence>, startIndex: Int) {
stop()
queue = sentences.toList()
index = if (queue.isEmpty()) 0 else startIndex.coerceIn(queue.indices)
mutableState.value = ReadAloudState(active = queue.isNotEmpty(), sentenceIndex = queue.getOrNull(index)?.index)
}
fun play() {
if (queue.isEmpty()) return
playback?.cancel()
mutableSettings.value = mutableSettings.value.copy(message = null)
if (!installed()) {
mutableState.value = mutableState.value.copy(active = true, playing = false, buffering = false)
mutableSettings.value = mutableSettings.value.copy(message = strings.offlineSpeechModelMissing)
return
}
val voice = mutableSettings.value.selectedVoiceId ?: "F1"
val useSilero = mutableSettings.value.selectedEngineId == "silero"
val useAccents = mutableSettings.value.selectedEngineId == "supertonic-3-ruaccent"
suspend fun render(text: String): SpeechAudio {
if (useSilero) return if (SupertonicTts.languageOf(text) == "ru") silero!!.invoke(text, voice)
else synthesize(text, "F1")
val prepared = if (useAccents) accentuate!!.invoke(text) else text
// Accent marks increase character count; re-chunk before entering the TTS model.
return joinSpeechAudio(SupertonicTts.chunks(SupertonicTts.russianStressToUnicode(prepared)).map { synthesize(it, voice) })
}
val chunks = queue.asSequence().withIndex().drop(index).flatMap { (position, sentence) ->
val parts = SupertonicTts.chunks(sentence.spokenText)
parts.asSequence().mapIndexed { chunkIndex, text ->
Chunk(position, sentence, text, chunkIndex == 0, chunkIndex == parts.lastIndex)
}
}.iterator()
mutableState.value = mutableState.value.copy(active = true, playing = true, buffering = true)
playback = scope.launch {
try {
coroutineScope {
// Keep at most one audio chunk ahead of the currently playing chunk.
var next = if (chunks.hasNext()) chunks.next() else null
var pending = next?.let { chunk -> async { render(chunk.text) } }
while (next != null) {
val current = next
mutableState.value = mutableState.value.copy(buffering = pending?.isCompleted != true)
val audio = pending!!.await()
next = if (chunks.hasNext()) chunks.next() else null
pending = next?.let { chunk -> async { render(chunk.text) } }
index = current.position
mutableState.value = ReadAloudState(active = true, playing = true, sentenceIndex = current.sentence.index)
if (current.first) delay(current.sentence.pauseBeforeMillis)
playAudio(audio)
if (current.last) delay(current.sentence.pauseAfterMillis)
}
}
mutableState.value = ReadAloudState()
} catch (cancelled: CancellationException) {
throw cancelled
} catch (error: Exception) {
System.err.println("Desktop TTS failed: ${error.javaClass.simpleName}: ${error.message}")
mutableState.value = mutableState.value.copy(playing = false, buffering = false)
mutableSettings.value = mutableSettings.value.copy(message = strings.speechPlaybackFailed)
}
}
}
fun stop() {
playback?.cancel()
playback = null
mutableState.value = ReadAloudState()
}
fun skip(delta: Int) {
if (queue.isEmpty()) return
val wasPlaying = mutableState.value.playing
playback?.cancel()
index = (index.toLong() + delta).coerceIn(0, queue.lastIndex.toLong()).toInt()
mutableState.value = ReadAloudState(active = true, sentenceIndex = queue[index].index)
if (wasPlaying) play()
}
fun selectVoice(id: String?) {
val voice = id ?: if (mutableSettings.value.selectedEngineId == "silero") "xenia" else "F1"
if (mutableSettings.value.voices.none { it.id == voice } || voice == mutableSettings.value.selectedVoiceId) return
mutableSettings.value = mutableSettings.value.copy(selectedVoiceId = voice)
if (mutableState.value.playing) play()
}
fun selectEngine(id: String?) {
val engine = id ?: defaultEngine
if (mutableSettings.value.engines.none { it.id == engine } || engine == mutableSettings.value.selectedEngineId) return
val options = voices(engine)
mutableSettings.value = mutableSettings.value.copy(selectedEngineId = engine, voices = options,
selectedVoiceId = mutableSettings.value.selectedVoiceId?.takeIf { id -> options.any { it.id == id } }
?: if (engine == "silero") "xenia" else "F1")
if (mutableState.value.playing) play()
}
fun refreshSettings() {
mutableSettings.value = mutableSettings.value.copy(message = if (installed()) null else strings.offlineSpeechModelMissing)
}
private data class Chunk(
val position: Int,
val sentence: ReadAloudSentence,
val text: String,
val first: Boolean,
val last: Boolean,
)
}
@@ -0,0 +1,90 @@
package net.sergeych.toread.tts
import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.isActive
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withTimeout
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.File
/** Optional desktop-only CPU accentor. Persistent local process, no HTTP or remote text service. */
internal class RuAccentProcess(private val directory: File = installationDirectory()) : AutoCloseable {
private val mapper = ObjectMapper()
private val lock = Mutex()
@Volatile private var worker: Worker? = null
init {
Runtime.getRuntime().addShutdownHook(Thread { close() })
}
suspend fun accentuate(text: String): String {
// Respect explicitly supplied stress; English needs no Russian processor.
if (SupertonicTts.languageOf(text) == "en" || '\u0301' in text || Regex("\\+[аеёиоуыэюяАЕЁИОУЫЭЮЯ]").containsMatchIn(text)) return text
return lock.withLock {
try {
currentCoroutineContext().ensureActive()
val current = worker?.takeIf { it.process.isAlive } ?: startWorker().also { worker = it }
withTimeout(90_000) {
coroutineScope {
val response = async(Dispatchers.IO) {
if (!current.ready) {
val ready = current.output.readLine() ?: error("RuAccent exited while loading")
check(mapper.readTree(ready)["ready"]?.asBoolean() == true) { "RuAccent did not initialize" }
current.ready = true
}
current.input.write(mapper.writeValueAsString(mapOf("text" to text)))
current.input.newLine()
current.input.flush()
val line = current.output.readLine() ?: error("RuAccent exited during inference")
val result = mapper.readTree(line)
check(result["error"] == null) { "RuAccent inference failed" }
result["text"]?.asText()?.takeIf { it.isNotBlank() } ?: error("Empty RuAccent response")
}
try {
response.await()
} finally {
// A blocked pipe read cannot be interrupted reliably; killing the
// cancelled worker unblocks it before the next request acquires the lock.
if (!isActive) current.process.destroyForcibly()
}
}
}
} catch (error: Exception) {
close()
throw error
}
}
}
private fun startWorker(): Worker {
val runtime = mapper.readTree(File(directory, "runtime.json"))
val process = ProcessBuilder(runtime["python"].asText(), "-u", File(directory, "worker.py").path, directory.path)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.apply {
environment()["HF_HUB_OFFLINE"] = "1"
environment()["TRANSFORMERS_OFFLINE"] = "1"
environment()["HF_HUB_DISABLE_TELEMETRY"] = "1"
}.start()
return Worker(process, process.inputStream.bufferedReader(Charsets.UTF_8), process.outputStream.bufferedWriter(Charsets.UTF_8))
}
override fun close() {
worker?.process?.destroyForcibly()
worker = null
}
private class Worker(val process: Process, val output: BufferedReader, val input: BufferedWriter, var ready: Boolean = false)
companion object {
fun installationDirectory(): File = System.getenv("YEREED_RUACCENT_HOME")?.let(::File)
?: File(SupertonicTts.modelDirectory().parentFile, "ruaccent")
fun isInstalled(): Boolean = File(installationDirectory(), "runtime.json").isFile && File(installationDirectory(), "worker.py").isFile
}
}
@@ -0,0 +1,98 @@
package net.sergeych.toread.tts
import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.isActive
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withTimeout
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.File
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.util.Base64
/** Desktop-only CPU Silero synthesizer. Persistent local process, no HTTP or remote text service. */
internal class SileroProcess(private val directory: File = installationDirectory()) : AutoCloseable {
private val mapper = ObjectMapper()
private val lock = Mutex()
@Volatile private var worker: Worker? = null
init {
Runtime.getRuntime().addShutdownHook(Thread { close() })
}
suspend fun synthesize(text: String, voice: String): SpeechAudio {
return lock.withLock {
try {
currentCoroutineContext().ensureActive()
val current = worker?.takeIf { it.process.isAlive } ?: startWorker().also { worker = it }
withTimeout(90_000) {
coroutineScope {
val response = async(Dispatchers.IO) {
if (!current.ready) {
val ready = current.output.readLine() ?: error("Silero exited while loading")
check(mapper.readTree(ready)["ready"]?.asBoolean() == true) { "Silero did not initialize" }
current.ready = true
}
current.input.write(mapper.writeValueAsString(mapOf("text" to text, "voice" to voice)))
current.input.newLine()
current.input.flush()
val line = current.output.readLine() ?: error("Silero exited during inference")
val result = mapper.readTree(line)
check(result["error"] == null) { "Silero inference failed" }
val bytes = Base64.getDecoder().decode(result["audio"].asText())
check(bytes.isNotEmpty() && bytes.size % 4 == 0) { "Invalid Silero audio" }
val buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer()
val samples = FloatArray(buffer.remaining()).also { buffer.get(it) }
check(samples.all { it.isFinite() }) { "Non-finite Silero audio" }
SpeechAudio(samples, result["sampleRate"].asInt().also { check(it == 48000) })
}
try {
response.await()
} finally {
// A blocked pipe read cannot be interrupted reliably; killing the
// cancelled worker unblocks it before the next request acquires the lock.
if (!isActive) current.process.destroyForcibly()
}
}
}
} catch (error: Exception) {
close()
throw error
}
}
}
private fun startWorker(): Worker {
val runtime = mapper.readTree(File(directory, "runtime.json"))
val script = SileroProcess::class.java.getResourceAsStream("/tts/silero-worker.py")!!.use { it.reader().readText() }
val process = ProcessBuilder(runtime["python"].asText(), "-u", "-c", script, directory.path)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.apply {
environment()["HF_HUB_OFFLINE"] = "1"
environment()["TRANSFORMERS_OFFLINE"] = "1"
environment()["HF_HUB_DISABLE_TELEMETRY"] = "1"
}.start()
return Worker(process, process.inputStream.bufferedReader(Charsets.UTF_8), process.outputStream.bufferedWriter(Charsets.UTF_8))
}
override fun close() {
worker?.process?.destroyForcibly()
worker = null
}
private class Worker(val process: Process, val output: BufferedReader, val input: BufferedWriter, var ready: Boolean = false)
companion object {
val voices = listOf("xenia", "baya", "aidar", "kseniya", "eugene")
fun installationDirectory(): File = System.getenv("YEREED_SILERO_HOME")?.let(::File)
?: File(SupertonicTts.modelDirectory().parentFile, "silero-v5_5-ru")
fun isInstalled(): Boolean = File(installationDirectory(), "runtime.json").isFile && File(installationDirectory(), "model.pt").length() > 0
}
}
@@ -0,0 +1,190 @@
package net.sergeych.toread.tts
import ai.onnxruntime.OnnxTensor
import ai.onnxruntime.OrtEnvironment
import ai.onnxruntime.OrtSession
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import java.io.File
import java.nio.FloatBuffer
import java.text.Normalizer
import java.util.Random
/** CPU-only, single-utterance adaptation of Supertone's MIT Java example.
* See docs/licenses/supertonic-code-MIT.txt. Call from one worker at a time.
*/
internal class SupertonicTts(val directory: File = modelDirectory()) : AutoCloseable {
private val mapper = ObjectMapper()
private val environment = OrtEnvironment.getEnvironment()
private val config = mapper.readTree(File(directory, "onnx/tts.json"))
private val indexer = mapper.readTree(File(directory, "onnx/unicode_indexer.json"))
.map { it.asLong() }.toLongArray()
val sampleRate: Int = config["ae"]["sample_rate"].asInt()
private val compression = config["ttl"]["chunk_compress_factor"].asInt()
private val chunkSize = config["ae"]["base_chunk_size"].asInt() * compression
private val latentDimension = config["ttl"]["latent_dim"].asInt() * compression
private val sessions = mutableListOf<OrtSession>()
private val styles = mutableMapOf<String, JsonNode>()
init {
try {
OrtSession.SessionOptions().use { options ->
// The standard (non-GPU) ONNX artifact uses CPUExecutionProvider only.
options.setIntraOpNumThreads(4.coerceAtMost(Runtime.getRuntime().availableProcessors()))
options.setInterOpNumThreads(1)
for (name in listOf("duration_predictor", "text_encoder", "vector_estimator", "vocoder")) {
sessions += environment.createSession(File(directory, "onnx/$name.onnx").path, options)
}
}
} catch (error: Throwable) {
close()
throw error
}
}
fun synthesize(
text: String,
voice: String = "F1",
language: String = languageOf(text),
seed: Long? = null,
checkCancelled: () -> Unit = {},
): FloatArray {
require(voice in voices) { "Unknown voice: $voice" }
require(language in listOf("en", "ru", "na")) { "Unsupported language: $language" }
require(text.isNotBlank() && text.length <= MAX_CHARS) { "Expected 1–$MAX_CHARS characters" }
checkCancelled()
val prepared = prepareText(text, language)
// Unknown symbols become spaces, never invalid (-1) embedding indices.
val ids = prepared.codePoints().toArray().map { point ->
indexer.getOrNull(point)?.takeIf { it >= 0 } ?: indexer[' '.code]
}.toLongArray()
val style = styles.getOrPut(voice) { mapper.readTree(File(directory, "voice_styles/$voice.json")) }
NativeResources().use { resources ->
fun tensor(value: Any) = resources.keep(OnnxTensor.createTensor(environment, value))
fun styleTensor(name: String): OnnxTensor {
val node = style[name]
val values = node["data"].flatMap { batch -> batch.flatMap { row -> row.map { it.floatValue() } } }
return resources.keep(OnnxTensor.createTensor(
environment, FloatBuffer.wrap(values.toFloatArray()), node["dims"].map { it.asLong() }.toLongArray(),
))
}
val textIds = tensor(arrayOf(ids))
val textMask = tensor(arrayOf(arrayOf(FloatArray(ids.size) { 1f })))
val styleDp = styleTensor("style_dp")
val styleTtl = styleTensor("style_ttl")
val duration = sessions[0].run(mapOf(
"text_ids" to textIds, "text_mask" to textMask, "style_dp" to styleDp,
)).use { result -> (result[0] as OnnxTensor).floatBuffer.get(0) / 1.05f }
check(duration.isFinite() && duration > 0 && duration <= 60) { "Invalid speech duration: $duration" }
checkCancelled()
val sampleCount = (duration * sampleRate).toInt()
val latentLength = (sampleCount + chunkSize - 1) / chunkSize
val random = seed?.let(::Random) ?: Random()
var latent = arrayOf(Array(latentDimension) { FloatArray(latentLength) { random.nextGaussian().toFloat() } })
val latentMask = tensor(arrayOf(arrayOf(FloatArray(latentLength) { 1f })))
val totalSteps = tensor(floatArrayOf(8f))
val encoded = resources.keep(sessions[1].run(mapOf(
"text_ids" to textIds, "text_mask" to textMask, "style_ttl" to styleTtl,
)))
val embedding = encoded[0] as OnnxTensor
repeat(8) { step ->
checkCancelled()
OnnxTensor.createTensor(environment, latent).use { noisy ->
OnnxTensor.createTensor(environment, floatArrayOf(step.toFloat())).use { currentStep ->
sessions[2].run(mapOf(
"noisy_latent" to noisy, "text_emb" to embedding, "style_ttl" to styleTtl,
"latent_mask" to latentMask, "text_mask" to textMask,
"current_step" to currentStep, "total_step" to totalSteps,
)).use { result ->
@Suppress("UNCHECKED_CAST")
latent = result[0].value as Array<Array<FloatArray>>
}
}
}
}
checkCancelled()
OnnxTensor.createTensor(environment, latent).use { finalLatent ->
return sessions[3].run(mapOf("latent" to finalLatent)).use { result ->
val samples = (result[0] as OnnxTensor).floatBuffer
check(samples.remaining() >= sampleCount) { "Truncated speech waveform" }
FloatArray(sampleCount).also { samples.get(it) }
}
}
}
}
override fun close() {
sessions.asReversed().forEach { runCatching { it.close() } }
sessions.clear()
}
companion object {
const val MAX_CHARS = 300
val voices = listOf("F1", "F2", "F3", "F4", "F5", "M1", "M2", "M3", "M4", "M5")
fun modelDirectory(): File {
val override = System.getProperty("yereed.tts.modelDir") ?: System.getenv("YEREED_TTS_MODEL_DIR")
if (!override.isNullOrBlank()) return File(override)
generateSequence(File(System.getProperty("user.dir")).absoluteFile) { it.parentFile }.forEach { root ->
File(root, ".local/tts/supertonic-3").takeIf { it.isDirectory }?.let { return it }
}
return File(System.getProperty("user.home"), ".toread/tts/supertonic-3")
}
fun isInstalled(directory: File = modelDirectory()): Boolean =
(listOf("onnx/tts.json", "onnx/unicode_indexer.json") +
listOf("duration_predictor", "text_encoder", "vector_estimator", "vocoder").map { "onnx/$it.onnx" } +
voices.map { "voice_styles/$it.json" }).all { File(directory, it).length() > 0 }
fun languageOf(text: String): String {
val russian = text.any { it in 'А'..'я' || it == 'ё' || it == 'Ё' }
val english = text.any { it in 'a'..'z' || it in 'A'..'Z' }
return when {
russian && english -> "na"
russian -> "ru"
else -> "en"
}
}
/** Bounded chunks preserve every non-whitespace character, including long unbroken words. */
fun chunks(text: String): List<String> = buildList {
var remaining = text.trim()
while (remaining.length > MAX_CHARS) {
var end = remaining.lastIndexOfAny(charArrayOf(' ', '\n', '\t'), MAX_CHARS)
if (end <= 0) end = MAX_CHARS
if (Character.isHighSurrogate(remaining[end - 1])) end--
while (end > 0 && Character.getType(remaining[end]) == Character.NON_SPACING_MARK.toInt()) end--
require(end > 0) { "Oversized combining-character sequence" }
add(remaining.substring(0, end).trim())
remaining = remaining.substring(end).trimStart()
}
if (remaining.isNotEmpty()) add(remaining)
}
internal fun prepareText(text: String, language: String): String {
var normalized = Normalizer.normalize(russianStressToUnicode(text), Normalizer.Form.NFKD)
.replace(Regex("[–‑—]"), "-")
.replace(Regex("[“”]"), "\"")
.replace(Regex("[‘’´`]"), "'")
.replace(Regex("[_\\[\\]|/#→←<>]"), " ")
.replace(Regex("[♥☆♡©\\\\]"), "")
.replace(Regex("[\\x{1F000}-\\x{1FAFF}\\x{2600}-\\x{27BF}]"), "")
if (language == "en") normalized = normalized.replace("@", " at ")
.replace("e.g.,", "for example, ").replace("i.e.,", "that is, ")
normalized = normalized.replace(Regex("\\s+"), " ").trim()
.replace(Regex(" +([,.!?;:'])"), "$1")
if (normalized.isEmpty() || normalized.last() !in ".!?;:,'\")]}…»") normalized += "."
return "<$language>$normalized</$language>"
}
/** RUAccent/Silero notation -> Cyrillic vowel plus U+0301, never a Latin lookalike. */
internal fun russianStressToUnicode(text: String): String =
text.replace(Regex("\\+([аеёиоуыэюяАЕЁИОУЫЭЮЯ])")) { it.groupValues[1] + "\u0301" }
}
}
private class NativeResources : AutoCloseable {
private val values = mutableListOf<AutoCloseable>()
fun <T : AutoCloseable> keep(value: T): T = value.also { values += it }
override fun close() = values.asReversed().forEach { runCatching { it.close() } }
}
@@ -0,0 +1,74 @@
package net.sergeych.toread.tts
import kotlinx.coroutines.runBlocking
import java.io.File
import java.util.Locale
import kotlin.system.measureNanoTime
/** Reproducible audition without opening the library, altering books, or contacting a service. */
fun main(args: Array<String>) = runBlocking {
val output = File(args.firstOrNull() ?: "build/tts-samples")
if ("--silero" in args) {
check(SileroProcess.isInstalled()) { "Install the Silero desktop runtime first." }
SileroProcess().use { engine ->
for ((label, text) in listOf(
"library" to "На двери висел старый замок, а вдали виднелся замок на холме.",
"yo" to "Ежик шел по лесу. Ребенок пьет теплое молоко.",
)) {
val audio = engine.synthesize(text, "xenia")
check(audio.samples.any { kotlin.math.abs(it) > 0.001f })
val file = File(output, "silero-desktop-$label.wav")
audio.writeWav(file)
println("Silero desktop worker: ${file.absolutePath}; ${audio.samples.size.toDouble() / audio.sampleRate}s")
if ("--play" in args) playDesktopAudio(audio)
}
}
return@runBlocking
}
check(SupertonicTts.isInstalled()) { "Run python3 scripts/download-tts-model.py first." }
lateinit var engine: SupertonicTts
val loadTime = measureNanoTime { engine = SupertonicTts() } / 1e9
println("Supertonic 3; CPU only; up to 4 threads; 8 steps; model load: %.3f s".format(Locale.ROOT, loadTime))
val stressTrial = "--stress" in args
val ruaccentTrial = "--ruaccent" in args
val russianText = "В библиотеке было тихо. Она открыла книгу и начала читать. За окном шёл осенний дождь. На двери висел старый замок, а вдали виднелся замок на холме. Завтра, подумала она, мы наконец доберёмся до моря."
val accentor = if (ruaccentTrial) RuAccentProcess() else null
val samples = if (ruaccentTrial) linkedMapOf("ru-plain" to russianText, "ru-ruaccent" to russianText) else if (stressTrial) linkedMapOf(
"ru-plain" to "Перед нами старый замок. Они стояли около замка.",
"ru-castle" to "П+еред н+ами ст+арый з+амок. Он+и сто+яли +около з+амка.",
"ru-lock" to "П+еред н+ами ст+арый зам+ок. Он+и сто+яли +около замк+а.",
) else linkedMapOf(
"en" to "The library was quiet. She opened the book and began to read. Outside, the autumn rain tapped softly against the window. Tomorrow, she thought, they would finally reach the coast.",
"ru" to russianText,
)
engine.use {
try {
for (voice in if (stressTrial || ruaccentTrial) listOf("F1") else listOf("F1", "M1")) for ((label, text) in samples) {
val language = if (stressTrial || ruaccentTrial) "ru" else label
val accentStart = System.nanoTime()
val accented = if (label == "ru-ruaccent") accentor!!.accentuate(text) else text
if (label == "ru-ruaccent") println("RuAccent (including first load): %.3f s".format(Locale.ROOT, (System.nanoTime() - accentStart) / 1e9))
val spoken = SupertonicTts.russianStressToUnicode(accented)
lateinit var audio: SpeechAudio
val elapsed = measureNanoTime {
audio = joinSpeechAudio(SupertonicTts.chunks(spoken).map { chunk ->
SpeechAudio(engine.synthesize(chunk, voice, language, seed = 42), engine.sampleRate)
})
} / 1e9
val duration = audio.samples.size.toDouble() / audio.sampleRate
check(audio.samples.all { it.isFinite() } && audio.samples.any { kotlin.math.abs(it) > 0.001f }) {
"Invalid or silent model output"
}
val file = File(output, "$label-$voice.wav")
audio.writeWav(file)
File(output, "$label-$voice.txt").writeText(spoken + "\n")
println("$label/$voice: %.3f s synthesis, %.3f s audio, RTF %.3f; %s".format(
Locale.ROOT, elapsed, duration, elapsed / duration, file.absolutePath,
))
if ("--play" in args) playDesktopAudio(audio)
}
} finally {
accentor?.close()
}
}
}
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Supertone Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,56 @@
"""Local stdin/stdout JSON protocol. Book text never leaves this process tree."""
import contextlib
import json
import os
from pathlib import Path
import sys
import time
os.environ.update(HF_HUB_OFFLINE="1", TRANSFORMERS_OFFLINE="1", HF_HUB_DISABLE_TELEMETRY="1", TOKENIZERS_PARALLELISM="false")
def load(model):
# Bound ORT worker counts before RUAccent imports its session constructor.
import onnxruntime as ort
original = ort.InferenceSession
def cpu_session(path, *args, **kwargs):
options = ort.SessionOptions()
options.intra_op_num_threads = 4
options.inter_op_num_threads = 1
kwargs["sess_options"] = options
kwargs["providers"] = ["CPUExecutionProvider"]
return original(path, *args, **kwargs)
ort.InferenceSession = cpu_session
from ruaccent import RUAccent
accentor = RUAccent()
accentor.load(omograph_model_size="turbo3.1", use_dictionary=True, device="CPU", workdir=str(model), tiny_mode=False)
return accentor
def main():
with contextlib.redirect_stdout(sys.stderr):
accentor = load(Path(sys.argv[1]))
if "--check" in sys.argv:
with contextlib.redirect_stdout(sys.stderr):
result = accentor.process_all("На двери висит замок.")
print(result, flush=True)
return
print(json.dumps({"ready": True}), flush=True)
for line in sys.stdin:
try:
text = json.loads(line)["text"]
if not isinstance(text, str) or len(text) > 2000:
raise ValueError("Expected text of at most 2000 characters")
start = time.monotonic()
with contextlib.redirect_stdout(sys.stderr):
accented = accentor.process_all(text)
print(json.dumps({"text": accented, "seconds": time.monotonic() - start}, ensure_ascii=False), flush=True)
except Exception as error:
# Do not echo private input or exception text into application logs.
print(json.dumps({"error": type(error).__name__}), flush=True)
if __name__ == "__main__":
main()
@@ -0,0 +1,47 @@
"""Persistent CPU-only Silero worker; local JSON lines, float32 audio in base64."""
import array
import base64
import contextlib
import json
import os
from pathlib import Path
import re
import sys
os.environ["CUDA_VISIBLE_DEVICES"] = ""
os.environ["TORCH_HOME"] = str(Path(sys.argv[1]).parent / "torch")
def main():
with contextlib.redirect_stdout(sys.stderr):
import torch
torch.set_num_threads(4)
torch.set_num_interop_threads(1)
model = torch.package.PackageImporter(str(Path(sys.argv[1]) / "model.pt")).load_pickle("tts_models", "model")
model.to(torch.device("cpu"))
print(json.dumps({"ready": True}), flush=True)
for line in sys.stdin:
try:
request = json.loads(line)
text = request["text"]
voice = request["voice"]
if not isinstance(text, str) or not 0 < len(text) <= 2000 or voice not in model.speakers:
raise ValueError("Invalid speech request")
# Preserve explicit reader/book stress in Silero's native notation.
text = re.sub(r"([аеёиоуыэюяАЕЁИОУЫЭЮЯ])\u0301", r"+\1", text)
with contextlib.redirect_stdout(sys.stderr), torch.inference_mode():
audio = model.apply_tts(text=text, speaker=voice, sample_rate=48000,
put_accent=True, put_yo=True,
put_stress_homo=True, put_yo_homo=True).detach().cpu().flatten()
if not audio.numel() or not torch.isfinite(audio).all():
raise ValueError("Invalid model audio")
samples = array.array("f", audio.tolist())
if sys.byteorder != "little":
samples.byteswap()
print(json.dumps({"sampleRate": 48000, "audio": base64.b64encode(samples.tobytes()).decode("ascii")}), flush=True)
except Exception as error:
print(json.dumps({"error": type(error).__name__}), flush=True)
if __name__ == "__main__":
main()
@@ -0,0 +1,186 @@
package net.sergeych.toread.tts
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import net.sergeych.toread.ReadAloudSentence
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
@OptIn(ExperimentalCoroutinesApi::class)
class DesktopReadAloudPlayerTest {
private fun sentence(index: Int, text: String) = ReadAloudSentence(index, index, 0, text.length, text)
private fun audio(id: Int) = SpeechAudio(floatArrayOf(id.toFloat()), 24000)
@Test
fun sileroDefaultsToXeniaRoutesEnglishAndResetsVoiceOnEngineChange() = runTest {
val calls = mutableListOf<String>()
var accented = false
val player = DesktopReadAloudPlayer(this, { text, voice -> calls += "super:$voice:$text"; audio(1) },
{}, { true }, { accented = true; it }, { text, voice -> calls += "silero:$voice:$text"; audio(1) })
assertEquals("silero", player.settingsState.value.selectedEngineId)
assertEquals("xenia", player.settingsState.value.selectedVoiceId)
player.prepare(listOf(sentence(0, "Привет."), sentence(1, "Hello."), sentence(2, "Привет, John.")), 0)
player.play()
advanceUntilIdle()
assertEquals(listOf("silero:xenia:Привет.", "super:F1:Hello.", "super:F1:Привет, John."), calls)
assertFalse(accented)
player.selectVoice("F1")
assertEquals("xenia", player.settingsState.value.selectedVoiceId)
player.selectEngine("supertonic-3")
assertEquals("F1", player.settingsState.value.selectedVoiceId)
player.selectEngine("silero")
assertEquals("xenia", player.settingsState.value.selectedVoiceId)
}
@Test
fun switchingAwayFromSileroCancelsPendingSpeech() = runTest {
var cancelled = false
var played = 0
val player = DesktopReadAloudPlayer(this, { _, _ -> audio(1) }, { played++ }, { true },
silero = { _, _ -> try { awaitCancellation() } finally { cancelled = true } })
player.prepare(listOf(sentence(0, "Привет.")), 0)
player.play()
runCurrent()
player.selectEngine("supertonic-3")
advanceUntilIdle()
assertTrue(cancelled)
assertEquals(1, played)
}
@Test
fun stopDuringSynthesisDiscardsAudioAndDoesNotReportAnError() = runTest {
var started = false
var played = false
val player = DesktopReadAloudPlayer(this, { _, _ -> started = true; awaitCancellation() }, { played = true }, { true })
player.prepare(listOf(sentence(7, "First.")), 0)
player.play()
runCurrent()
assertTrue(started)
assertTrue(player.state.value.buffering)
player.stop()
runCurrent()
assertFalse(played)
assertFalse(player.state.value.active)
assertNull(player.settingsState.value.message)
}
@Test
fun skipCancelsOldSynthesisAndStartsRequestedSentence() = runTest {
val played = mutableListOf<Int>()
val player = DesktopReadAloudPlayer(this, { text, _ ->
if (text == "First.") awaitCancellation() else audio(2)
}, { played += it.samples[0].toInt(); awaitCancellation() }, { true })
player.prepare(listOf(sentence(10, "First."), sentence(20, "Second.")), 0)
player.play()
runCurrent()
player.skip(1)
runCurrent()
assertEquals(listOf(2), played)
assertEquals(20, player.state.value.sentenceIndex)
player.stop()
runCurrent()
}
@Test
fun voiceChangeCancelsPlaybackAndRestartsCurrentSentence() = runTest {
val voices = mutableListOf<String>()
var cancelledAudio = 0
val player = DesktopReadAloudPlayer(this, { _, voice -> voices += voice; audio(1) }, {
try { awaitCancellation() } finally { cancelledAudio++ }
}, { true })
player.prepare(listOf(sentence(3, "Hello.")), 0)
player.play()
runCurrent()
player.selectVoice("M1")
runCurrent()
assertEquals(listOf("F1", "M1"), voices)
assertEquals(1, cancelledAudio)
assertEquals(3, player.state.value.sentenceIndex)
player.stop()
runCurrent()
assertEquals(2, cancelledAudio)
}
@Test
fun lookaheadDoesNotAdvanceHighlightBeforePlayback() = runTest {
val synthesized = mutableListOf<String>()
val player = DesktopReadAloudPlayer(this, { text, _ -> synthesized += text; audio(1) }, {
awaitCancellation()
}, { true })
player.prepare(listOf(sentence(4, "First."), sentence(8, "Second."), sentence(12, "Third.")), 0)
player.play()
runCurrent()
assertEquals(listOf("First.", "Second."), synthesized)
assertEquals(4, player.state.value.sentenceIndex)
player.stop()
runCurrent()
}
@Test
fun fullQueueUsesSpokenTextAndFinishes() = runTest {
val synthesized = mutableListOf<String>()
val player = DesktopReadAloudPlayer(this, { text, _ -> synthesized += text; audio(1) }, { delay(10) }, { true })
player.prepare(listOf(sentence(0, "1").copy(spokenText = "One.", pauseBeforeMillis = 20), sentence(1, "Two.")), 0)
player.play()
advanceUntilIdle()
assertEquals(listOf("One.", "Two."), synthesized)
assertFalse(player.state.value.playing)
assertFalse(player.state.value.active)
}
@Test
fun accentStageCanBeComparedWithPlainEngineAndNeverChangesOriginalText() = runTest {
val inputs = mutableListOf<String>()
val original = sentence(0, "замок")
val player = DesktopReadAloudPlayer(this, { text, _ -> inputs += text; audio(1) }, {}, { true }, { "з+амок" })
player.prepare(listOf(original), 0)
player.play()
advanceUntilIdle()
assertEquals(listOf("за\u0301мок"), inputs)
assertEquals("замок", original.spokenText)
player.selectEngine("supertonic-3")
player.play()
advanceUntilIdle()
assertEquals(listOf("за\u0301мок", "замок"), inputs)
}
@Test
fun accentedTextIsRechunkedToFitTtsWithoutDiscardingMarks() = runTest {
val inputs = mutableListOf<String>()
val marked = "а\u0301 ".repeat(110).trim()
val player = DesktopReadAloudPlayer(this, { text, _ -> inputs += text; audio(1) }, {}, { true }, { marked })
player.prepare(listOf(sentence(0, "а ".repeat(110))), 0)
player.play()
advanceUntilIdle()
assertTrue(inputs.size > 1)
assertTrue(inputs.all { it.length <= 300 })
assertEquals(marked.replace(" ", ""), inputs.joinToString("").replace(" ", ""))
}
@Test
fun missingModelAndInferenceErrorsRemainVisibleAndRetryable() = runTest {
var installed = false
var calls = 0
val player = DesktopReadAloudPlayer(this, { _, _ -> calls++; error("bad model") }, {}, { installed })
player.prepare(listOf(sentence(0, "Hello.")), 0)
player.play()
assertEquals(0, calls)
assertFalse(player.state.value.playing)
assertNotNull(player.settingsState.value.message)
installed = true
player.play()
advanceUntilIdle()
assertEquals(1, calls)
assertTrue(player.state.value.active)
assertFalse(player.state.value.buffering)
assertNotNull(player.settingsState.value.message)
}
}
@@ -0,0 +1,45 @@
package net.sergeych.toread.tts
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class SupertonicTextTest {
@Test
fun longSentencesAndUnbrokenWordsAreBoundedWithoutLosingText() {
for (text in listOf("Слово ".repeat(150), "a".repeat(601), "a".repeat(299) + "😀" + "b".repeat(301), "а".repeat(300) + "\u0301" + "а".repeat(50))) {
val chunks = SupertonicTts.chunks(text)
assertTrue(chunks.all { it.length <= 300 && it.isNotBlank() })
assertEquals(text.filterNot(Char::isWhitespace), chunks.joinToString("").filterNot(Char::isWhitespace))
assertTrue(chunks.none { Character.isHighSurrogate(it.last()) || Character.isLowSurrogate(it.first()) })
assertTrue(chunks.none { it.first() == '\u0301' })
}
}
@Test
fun languageSelectionHandlesRussianEnglishAndMixedText() {
assertEquals("ru", SupertonicTts.languageOf("Ёж шёл домой."))
assertEquals("en", SupertonicTts.languageOf("The library."))
assertEquals("na", SupertonicTts.languageOf("Он сказал Hello."))
}
@Test
fun preprocessingDoesNotInsertPredictedStress() {
assertEquals("<ru>На двери висит замок.</ru>", SupertonicTts.prepareText("На двери висит замок", "ru"))
assertEquals("<en>Hello!</en>", SupertonicTts.prepareText("Hello !", "en"))
}
@Test
fun ruaccentStressBecomesCombiningAcuteOnCyrillicVowels() {
assertEquals("за\u0301мок замо\u0301к Ё\u0301лка", SupertonicTts.russianStressToUnicode("з+амок зам+ок +Ёлка"))
assertEquals("2+2 +a +б а\u0301", SupertonicTts.russianStressToUnicode("2+2 +a +б а\u0301"))
assertEquals("<ru>За\u0301мок.</ru>", SupertonicTts.prepareText("З+амок", "ru"))
assertEquals("<ru>За\u0301мок.</ru>", SupertonicTts.prepareText("За\u0301мок", "ru"))
}
@Test
fun pcmConversionClipsAndHandlesNonFiniteSamples() {
val bytes = SpeechAudio(floatArrayOf(-2f, 2f, Float.NaN), 44100).pcm16()
assertEquals(listOf(1, 128, 255, 127, 0, 0), bytes.map { it.toInt() and 255 })
}
}
@@ -61,7 +61,10 @@ actual suspend fun markLibraryFavorite(fileId: String, favorite: Boolean): Boole
actual suspend fun shareLibraryBookFile(fileId: String): Boolean = false
actual suspend fun viewLibraryBookFile(fileId: String): Boolean = false
actual suspend fun viewLibraryBookFile(folder: String, fileName: String): Boolean = false
actual fun openPlatformUrl(url: String): Boolean =
window.open(url, target = "_blank") != null
actual suspend fun loadBookInfoExtras(fileId: String): BookInfoExtras = BookInfoExtras()
@@ -73,10 +76,59 @@ actual suspend fun loadThemeMode(): ThemeMode = ThemeMode.SYSTEM
actual suspend fun saveThemeMode(mode: ThemeMode) = Unit
actual suspend fun loadReaderFontSettings(): ReaderFontSettings =
readerFontSettingsFromStorageValue(window.localStorage.getItem(ReaderFontSettingsStorageKey))
actual suspend fun saveReaderFontSettings(settings: ReaderFontSettings) {
window.localStorage.setItem(ReaderFontSettingsStorageKey, settings.toStorageValue())
}
actual suspend fun loadReaderFullScreen(): Boolean =
window.localStorage.getItem(ReaderFullScreenStorageKey)?.toBooleanStrictOrNull() ?: false
actual suspend fun saveReaderFullScreen(fullScreen: Boolean) {
window.localStorage.setItem(ReaderFullScreenStorageKey, fullScreen.toString())
}
actual suspend fun loadReaderPagingOnly(): Boolean =
window.localStorage.getItem(ReaderPagingOnlyStorageKey)?.toBooleanStrictOrNull() ?: false
actual suspend fun saveReaderPagingOnly(enabled: Boolean) {
window.localStorage.setItem(ReaderPagingOnlyStorageKey, enabled.toString())
}
actual suspend fun loadScanDownloadsAutomatically(): Boolean = true
actual suspend fun saveScanDownloadsAutomatically(enabled: Boolean) = Unit
internal actual suspend fun loadLibraryFilter(): LibraryFilter =
when (val saved = window.localStorage.getItem(LibraryFilterStorageKey)) {
null -> {
warnLibraryFilterFallback("library filter was not saved")
LibraryFilter.MyLibrary
}
else -> runCatching { LibraryFilter.valueOf(saved) }
.getOrElse {
warnLibraryFilterFallback("invalid saved library filter '$saved'")
LibraryFilter.MyLibrary
}
}
internal actual suspend fun saveLibraryFilter(filter: LibraryFilter) {
window.localStorage.setItem(LibraryFilterStorageKey, filter.name)
}
actual suspend fun loadAppLocaleTag(): String? =
window.localStorage.getItem(AppLocaleStorageKey)?.takeIf { it.isNotBlank() }
actual suspend fun saveAppLocaleTag(localeTag: String?) {
if (localeTag.isNullOrBlank()) {
window.localStorage.removeItem(AppLocaleStorageKey)
} else {
window.localStorage.setItem(AppLocaleStorageKey, localeTag)
}
}
actual suspend fun loadDownloadsWasScanned(): Boolean = false
actual suspend fun saveDownloadsWasScanned(scanned: Boolean) = Unit
@@ -99,6 +151,16 @@ actual fun watchPlatformDarkTheme(onChange: (Boolean) -> Unit): () -> Unit {
actual fun libraryLogPath(): String? = null
private fun warnLibraryFilterFallback(reason: String) {
println("warning $reason; using ${LibraryFilter.MyLibrary.name}")
}
private const val AppLocaleStorageKey = "toread.appLocaleTag"
private const val LibraryFilterStorageKey = "toread.libraryFilter"
private const val ReaderFontSettingsStorageKey = "toread.readerFontSettings"
private const val ReaderFullScreenStorageKey = "toread.readerFullScreen"
private const val ReaderPagingOnlyStorageKey = "toread.readerPagingOnly"
actual fun formatLibraryLastReadTime(millis: Long): String {
val totalMinutes = millis / 60_000L
val minute = (totalMinutes % 60).toString().padStart(2, '0')
@@ -0,0 +1,6 @@
package net.sergeych.toread
import kotlinx.browser.window
internal actual fun platformLocaleTags(): List<String> =
listOf(window.navigator.language)
@@ -10,7 +10,7 @@ actual object ReadAloudPlatform {
private val mutableSettingsState = MutableStateFlow(ReadAloudSettingsState())
actual val settingsState: StateFlow<ReadAloudSettingsState> = mutableSettingsState
actual fun prepare(bookTitle: String, sentences: List<ReadAloudSentence>, startIndex: Int) = Unit
actual fun prepare(fileId: String, bookTitle: String, sentences: List<ReadAloudSentence>, startIndex: Int) = Unit
actual fun play() = Unit
actual fun stop() = Unit
actual fun skip(delta: Int) = Unit
+176
View File
@@ -0,0 +1,176 @@
# Desktop CPU speech trial
Desktop Read aloud defaults to **Silero v5_5_ru / Xenia** for Russian when its
local runtime is installed. Silero runs in a persistent CPU-only Python process
and handles its own stress and ё restoration. English and mixed-script chunks
use Supertonic 3 / F1 through ONNX Runtime's CPU provider. Mixed passages retain
Supertonic's Russian pronunciation limitations. Plain Supertonic is retained as an audition baseline. Initial listening feedback
found its Russian word stress unacceptable. An optional RUAccent CPU stage now
adds stress and restores ё before synthesis; its +vowel notation is converted to
a Cyrillic vowel followed by U+0301. Manual minimal-pair listening showed stress
changes, but this is not a comprehensive pronunciation-quality evaluation.
Android retains its existing system TTS implementation.
## Run
From the repository root:
```sh
python3 scripts/download-tts-model.py
./gradlew :composeApp:run
```
The script downloads about 401 MB into the ignored `.local/tts/supertonic-3`
directory. Downloads are revision-pinned and verified against the committed file
manifest. It reuses valid files and replaces incomplete downloads only after hash
verification. No Python packages are needed. The model license is saved alongside
its weights.
Open a book and choose **Read aloud** from the reader menu. Playback highlights the
current sentence, follows the reading position, and exposes Stop, previous/next
sentence, and voice settings. Silero offers Xenia (default), Baya, Aidar, Kseniya
and Eugene. The Supertonic engines offer F1–F5 and M1–M5. Engine and voice choices
last for this application session; each launch starts with Xenia if installed.
The model loads lazily on first playback. One chunk is prepared ahead while audio
plays. Stop, skipping, and voice changes cancel the old playback and lookahead;
Supertonic inference checks cancellation between model stages/denoising steps;
cancelling Silero inference terminates its worker, which restarts on demand. Long
sentences are split at whitespace into at most 300-character chunks. English and
Russian are selected from their scripts; mixed-script chunks use the model's
language-agnostic `na` mode. This heuristic is not general language detection.
For a model installed elsewhere, set `YEREED_TTS_MODEL_DIR` (or JVM property
`yereed.tts.modelDir`) to the directory containing `onnx/` and `voice_styles/`.
Without an override the app searches parent directories for the workspace model,
then falls back to `~/.toread/tts/supertonic-3`. Packaged applications can use that
home-directory location. The application itself never downloads models or sends
text over the network. The first Gradle build and model setup require downloads.
## Optional RUAccent stage (desktop trial)
```sh
python3 scripts/setup-ruaccent.py
./gradlew :composeApp:run
```
This creates a private Python 3.12 runtime under `.local/ruaccent-runtime` and
fetches about 726 MB of revision-pinned RUAccent assets into `.local/tts/ruaccent`.
It does not change system Python. The bootstrap supports Linux x86_64 directly;
on other systems install `uv` first. The dependency versions and model hashes
are recorded under `scripts/`. This desktop trial uses RUAccent 1.5.8.3,
`turbo3.1`, the full dictionary/rule pipeline, and CPU execution with four threads.
This is a desktop quality experiment, not an Android footprint recommendation.
After setup, restart the reader. **Supertonic 3 + RUAccent** becomes the default
engine; select **Supertonic 3** in read-aloud settings for a direct comparison.
English bypasses the accentor. Already marked Russian chunks are preserved.
The accentor is a persistent local child process communicating over stdin/stdout;
Hugging Face and Transformers run in offline mode. Book contents are never sent
to a server. Set `YEREED_RUACCENT_HOME` to override its installation directory.
Stop/skip kills a pending accentor request when necessary; the next request
restarts the worker. Displayed book text and stored files are unchanged.
The model recognizes U+0301, and preprocessing preserves it. For example,
`з+амок` becomes `за́мок`, not a Latin accented-vowel substitution. Predicted
stress can still be wrong; model-token support does not guarantee compliance.
## Audition / benchmark
```sh
./gradlew :composeApp:ttsSample
# Also play the four samples through the desktop audio device:
./gradlew :composeApp:ttsSample -PttsPlay=true
# Controlled manual stress pairs, same words/noise seed:
./gradlew :composeApp:ttsSample -PttsStress=true -PttsPlay=true
# Original plain Russian passage, then RUAccent + Supertonic:
./gradlew :composeApp:ttsSample -PttsRuAccent=true -PttsPlay=true
```
WAV files and their exact unaccented texts go to `composeApp/build/tts-samples/`.
The sample task uses the same inference and audio code as the reader without
opening or changing the library. It prints model-load time, synthesis time,
audio duration, and real-time factor (generation seconds / audio seconds).
Initial measurements on an Intel Core Ultra 9 275HX, four CPU threads, eight
steps, speed 1.05: 1.075 seconds model load; English/Russian F1/M1 samples generated
11.6–15.3 seconds of audio in 2.4–3.3 seconds (RTF 0.20–0.24). These are short
local samples, not phone/battery or long-book benchmarks. Synthesis completion
and non-silent waveforms do not establish pronunciation quality.
## Dependencies and provenance
- [Supertonic source](https://github.com/supertone-oss-archive/supertonic): the
Kotlin inference code adapts the MIT Java example; attribution is in
[licenses/supertonic-code-MIT.txt](licenses/supertonic-code-MIT.txt).
- [Supertonic 3 model](https://huggingface.co/supertone-oss-archive/supertonic-3):
revision `aafc6e32416a594460b32413efc49d7fe4ce6d46`, OpenRAIL-M weights.
Upstream development/support ended in September 2026.
- ONNX Runtime 1.23.1, standard CPU artifact; no CUDA/GPU provider.
- JSON loading uses Jackson; PCM playback uses Java Sound.
Run `./gradlew :composeApp:jvmTest` for text-boundary, PCM conversion, queue,
lookahead, cancellation, voice-change, and missing-model/error tests, together
with existing desktop/common tests. Model inference is exercised separately by
`ttsSample`, so ordinary tests do not download weights or use speakers.
The isolated virtual-display capture did not provide a usable UI image; rendered
GUI verification remains incomplete. No further screen access is used.
RUAccent measurements on the same host: about 5.9 seconds cold load, 30–43 ms
for the two warm short passages, and about 2.1 GiB resident memory for the full
accentor worker alone. The combined passage took 2.98 seconds of TTS synthesis
for 16.42 seconds of audio, excluding accentor startup. In that passage RUAccent
incorrectly chose `за́мок` for the door lock; on a shorter explicit door-lock
sentence it chose `замо́к`. These are limited observations, not an accuracy score.
## Silero Russian audition
User listening feedback rejected Supertonic's Russian quality. A separate CPU
audition uses official Silero `v5_5_ru`, with its own stress, ё, stress-homograph
and ё-homograph processing enabled. It does not use RUAccent. The same model is
now the preferred desktop Russian engine. The Russian model is CC BY-NC-SA 4.0, unlike Silero's MIT CIS
base models; see [upstream](https://github.com/snakers4/silero-models).
Setup from the repository root (requires `uv`; our existing local copy is
`.local/tools/uv`):
```sh
export UV_CACHE_DIR="$PWD/.local/uv-cache"
export UV_PYTHON_INSTALL_DIR="$PWD/.local/python"
export UV_SYSTEM_CERTS=true
.local/tools/uv venv --python 3.12.14 .local/silero-runtime
.local/tools/uv pip sync --python .local/silero-runtime/bin/python scripts/silero-requirements.txt --index-url https://download.pytorch.org/whl/cpu
mkdir -p .local/tts/silero-v5_5-ru
curl -fL https://models.silero.ai/models/tts/ru/v5_5_ru.pt -o .local/tts/silero-v5_5-ru/model.pt.part
mv .local/tts/silero-v5_5-ru/model.pt.part .local/tts/silero-v5_5-ru/model.pt
.local/silero-runtime/bin/python scripts/silero-sample.py
python3 - <<'PY'
from pathlib import Path
import json
root = Path.cwd()
(root / '.local/tts/silero-v5_5-ru/runtime.json').write_text(json.dumps({
'python': str(root / '.local/silero-runtime/bin/python')
}) + '\n')
PY
# Restart the reader after setup, or check its actual Kotlin/Python bridge:
./gradlew :composeApp:ttsSample -PttsSilero=true
```
Outputs: `composeApp/build/tts-samples/silero-*.wav`, matching input `.txt`
files, and `silero-benchmark.json` with timings, settings and the downloaded
model's SHA-256. The script uses CPU-only PyTorch, four threads and 48 kHz PCM.
Default voices are Xenia, Baya and Aidar; `--voices xenia` limits the audition.
Each reads the earlier library passage and a separate unaccented ё/context
challenge. Pronunciation quality must be judged by listening.
The desktop integration discovers `silero-v5_5-ru` beside the Supertonic model
directory; `YEREED_SILERO_HOME` can override it. `runtime.json` points to the
isolated Python executable. The worker script is bundled in the application;
model loading and inference are local. Both models are needed for bilingual use.
Initial audition on the same desktop: model load 0.38 seconds; six samples
generated 13.2–15.5 seconds of audio in 0.36–0.82 seconds (RTF 0.027–0.059,
including the first synthesis). All outputs were finite and non-silent.
Downloaded model SHA-256:
`50081637b602126ee06cb3bc8a744d25651d2da149ee8864b9a379bfdd934437`.
+1 -1
View File
@@ -20,7 +20,7 @@ The common API is `Fb2Format.parse(input: ByteArray, fileName: String? = null)`.
Import detection:
- A file is treated as ZIP when its bytes start with the ZIP local-file signature `PK\003\004` or the provided filename ends with `.zip`.
- Otherwise bytes are decoded according to the XML declaration when supported. UTF-8 is the default, and unsupported or missing encodings fall back to UTF-8. `windows-1251` is supported for legacy FB2 files.
- Otherwise bytes are decoded according to the XML declaration when supported. UTF-8 is the default, and unsupported or missing encodings fall back to UTF-8. `windows-1251` and `windows-1252` are supported for legacy FB2 files.
- In ZIP archives, the first entry ending with `.fb2` is used. If no such entry exists, the first non-directory entry is used.
ZIP support:
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Supertone Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+6 -1
View File
@@ -1,5 +1,5 @@
[versions]
agp = "8.11.2"
agp = "8.13.2"
android-compileSdk = "36"
android-minSdk = "26"
android-targetSdk = "36"
@@ -19,8 +19,12 @@ ktor = "3.4.3"
logback = "1.5.32"
material3 = "1.11.0-alpha07"
h2 = "2.4.240"
onnxruntime = "1.23.1"
jackson = "2.18.6"
[libraries]
onnxruntime = { module = "com.microsoft.onnxruntime:onnxruntime", version.ref = "onnxruntime" }
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlin-testJunit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
junit = { module = "junit:junit", version.ref = "junit" }
@@ -41,6 +45,7 @@ compose-components-resources = { module = "org.jetbrains.compose.components:comp
compose-uiToolingPreview = { module = "org.jetbrains.compose.ui:ui-tooling-preview", version.ref = "composeMultiplatform" }
kotlinx-coroutinesSwing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" }
kotlinx-coroutinesCore = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutinesTest = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
h2 = { module = "com.h2database:h2", version.ref = "h2" }
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
ktor-serverCore = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
Binary file not shown.

After

Width:  |  Height:  |  Size: 208 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 392 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 398 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 365 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

+56
View File
@@ -0,0 +1,56 @@
#!/usr/bin/env python3
"""Download the pinned Supertonic 3 CPU model. No packages or credentials required."""
import hashlib
import json
from pathlib import Path
import sys
import urllib.request
ROOT = Path(__file__).resolve().parent.parent
MANIFEST = json.loads((ROOT / "scripts/supertonic-3-model.json").read_text())
def valid(path, entry):
if not path.is_file() or path.stat().st_size != entry["size"]:
return False
digest = hashlib.sha256() if "sha256" in entry else hashlib.sha1()
if "git_blob" in entry:
digest.update(f"blob {entry['size']}\0".encode())
with path.open("rb") as source:
for chunk in iter(lambda: source.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest() == entry.get("sha256", entry.get("git_blob"))
def download(manifest, destination):
total = sum(entry["size"] for entry in manifest["files"])
print(f"{manifest['repository']}: {total / 1e6:.1f} MB; destination: {destination}", flush=True)
base = f"https://huggingface.co/{manifest['repository']}/resolve/{manifest['revision']}"
for entry in manifest["files"]:
target = destination / entry["path"]
if valid(target, entry):
print(f"Verified {entry['path']}", flush=True)
continue
target.parent.mkdir(parents=True, exist_ok=True)
temporary = target.with_suffix(target.suffix + ".part")
print(f"Downloading {entry['path']} ({entry['size'] / 1e6:.1f} MB)", flush=True)
try:
with urllib.request.urlopen(f"{base}/{entry['path']}", timeout=60) as source, temporary.open("wb") as output:
while chunk := source.read(1024 * 1024):
output.write(chunk)
if not valid(temporary, entry):
raise RuntimeError(f"Checksum mismatch: {entry['path']}")
temporary.replace(target)
finally:
temporary.unlink(missing_ok=True)
def main():
destination = Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT / ".local/tts/supertonic-3"
print("Supertonic model license: OpenRAIL-M (saved as LICENSE alongside the model).", flush=True)
download(MANIFEST, destination)
print("Ready. Run ./gradlew :composeApp:run and use Read aloud in a book.", flush=True)
if __name__ == "__main__":
main()
+231
View File
@@ -0,0 +1,231 @@
{
"repository": "ruaccent/accentuator",
"revision": "b78ae5ea1e62beaf138bed1865cd8c3b0b5ca855",
"files": [
{
"path": "README.md",
"size": 2660,
"git_blob": "7f12645cf20ed21742fd63a3b799efd8c7007734"
},
{
"path": "dictionary/accents.json.gz",
"size": 20954156,
"sha256": "aa460ebba90de00fbbf3d41d121961f605b98667e45efb7920f127473b15515e"
},
{
"path": "dictionary/accents_nn.json.gz",
"size": 845996,
"sha256": "8395664000b80c1afe09bfea3650945b0933482b8e3dee5bb9d429eb18c44935"
},
{
"path": "dictionary/omographs.json.gz",
"size": 219047,
"sha256": "04a9e81c68d65f65ba493fe0110f99e79087548c2beeec3032e2b66e28706f36"
},
{
"path": "dictionary/rule_engine/accents.json",
"size": 694782,
"git_blob": "91690536e14e133fe4017941a466fde663b96011"
},
{
"path": "dictionary/rule_engine/forms.json",
"size": 2669,
"git_blob": "6686cd615fb55c09976550aeba3ad6687ecc32dd"
},
{
"path": "dictionary/yo_homographs.json.gz",
"size": 5747,
"sha256": "c4ee777bbbab87f9eac838f370ad92974e079d02b21903e480c54b5f0c8c60d1"
},
{
"path": "dictionary/yo_words.json.gz",
"size": 548914,
"sha256": "a19fa89a964a0691d9fe4ee384783e3934904891843d8f59a1c480d67947a82a"
},
{
"path": "koziev/rulemma/rulemma.dat",
"size": 16703198,
"sha256": "bf2b3ef3ff7a0aa6e4250aa4e9c8ed568e25f825deebdb12dee1b46b785ba9fc"
},
{
"path": "koziev/rulemma/rulemma.py",
"size": 11166,
"git_blob": "a182b7d1694b13e8ac6807e2701ad17bf4064bbe"
},
{
"path": "koziev/rupostagger/__init__.py",
"size": 111,
"git_blob": "01bd02ccfc68c851542390a6d0a684481f897bb9"
},
{
"path": "koziev/rupostagger/database/ruword2tags.db",
"size": 168816640,
"sha256": "a06848e656bef642aafb4440c03554fa78f2f32dde92ea66f3f86ce9977b167e"
},
{
"path": "koziev/rupostagger/rupostagger.config",
"size": 276,
"git_blob": "4794511fc1b5ef4bc649fab5eafdf6b775206b72"
},
{
"path": "koziev/rupostagger/rupostagger.model",
"size": 2417464,
"sha256": "21b7b0bfd7427b5fdc1604052176db8aa3b139b3ce03be440cfce48536f8e5ef"
},
{
"path": "koziev/rupostagger/rupostagger.py",
"size": 7118,
"git_blob": "198a8db45678a9578dfad5bbf01d3efd8e95a7c8"
},
{
"path": "koziev/rupostagger/rusyllab.py",
"size": 18975,
"git_blob": "7f606a48ff2a5de6b6388a45caf1d41bd6089611"
},
{
"path": "koziev/rupostagger/ruword2tags.dat",
"size": 9683765,
"sha256": "dde47b5f1d48ff899887ac07812dcabd2966e48e84646f3065bfd06627c2af58"
},
{
"path": "koziev/rupostagger/ruword2tags.py",
"size": 18086,
"git_blob": "71098637747f19e44788506775e4cd1e6c5e7183"
},
{
"path": "nn/nn_accent/big.onnx",
"size": 2285217,
"sha256": "47e69d9ae19f2a82e21b1c70f6a4bbfb1abc5759e98b2e67d009c5e9d7af18c9"
},
{
"path": "nn/nn_accent/config.json",
"size": 841,
"git_blob": "0d1656a1fb93fcea726186c8bfad977c6e27c4a5"
},
{
"path": "nn/nn_accent/model.onnx",
"size": 803402,
"sha256": "4e393144e45626f6f1062a0784ef06f921b97321a8e7b87ac2a09a892286500a"
},
{
"path": "nn/nn_accent/ort_config.json",
"size": 727,
"git_blob": "5a8fc726323dda820ae4e794e4d0d514de07eee2"
},
{
"path": "nn/nn_accent/special_tokens_map.json",
"size": 99,
"git_blob": "1543e951b7c6f9c246a5e441d7162b2c65ab48a5"
},
{
"path": "nn/nn_accent/tokenizer_config.json",
"size": 257,
"git_blob": "5ea10565e634adcd37089652fbf6bb2cf05c1530"
},
{
"path": "nn/nn_accent/vocab.txt",
"size": 140,
"git_blob": "1e5a3d765a6f5a686ed3c6294efb85e9e00a431a"
},
{
"path": "nn/nn_omograph/turbo3.1/added_tokens.json",
"size": 279418,
"git_blob": "23620bf92f2ad9e1243202cff127617fe9415c9a"
},
{
"path": "nn/nn_omograph/turbo3.1/config.json",
"size": 723,
"git_blob": "2ff7dbeb01fffcb05f65ad430b08205fe6668693"
},
{
"path": "nn/nn_omograph/turbo3.1/merges.txt",
"size": 1213606,
"git_blob": "2d4658a914a3760fbf1a3a9c72b42eefcf8d8ded"
},
{
"path": "nn/nn_omograph/turbo3.1/model.onnx",
"size": 359306923,
"sha256": "2cb6a174c4cdb45bd3132b4f7c8a3779fc4b6869863180ed7d0e421bcd453dbd"
},
{
"path": "nn/nn_omograph/turbo3.1/special_tokens_map.json",
"size": 280,
"git_blob": "d5698132694f4f1bcff08fa7d937b1701812598e"
},
{
"path": "nn/nn_omograph/turbo3.1/tokenizer.json",
"size": 5532343,
"git_blob": "31ac709e3c4625c6bf80b7c97066a9cde4d9ba89"
},
{
"path": "nn/nn_omograph/turbo3.1/tokenizer_config.json",
"size": 492,
"git_blob": "15a19568f93d1b292c7ff6d4ef0fdac13bd65b53"
},
{
"path": "nn/nn_omograph/turbo3.1/vocab.json",
"size": 1555282,
"git_blob": "ede65202ec9ef483ff166bb31f76615f30048db7"
},
{
"path": "nn/nn_stress_usage_predictor/config.json",
"size": 822,
"git_blob": "df189246d87953bead249c2ee82e3985a96be8f8"
},
{
"path": "nn/nn_stress_usage_predictor/model.onnx",
"size": 116473561,
"sha256": "3d547500637b4ddfec8880ed6d1405fd50ee9d3f0131ef8a2a69dcf961dbefeb"
},
{
"path": "nn/nn_stress_usage_predictor/special_tokens_map.json",
"size": 125,
"git_blob": "a8b3208c2884c4efb86e49300fdd3dc877220cdf"
},
{
"path": "nn/nn_stress_usage_predictor/tokenizer.json",
"size": 2413536,
"git_blob": "b4d5ccba4a193e0330537378bb0bba47026bd364"
},
{
"path": "nn/nn_stress_usage_predictor/tokenizer_config.json",
"size": 368,
"git_blob": "d7d40c5e58b2c6ba58974571f617abe23221eb6c"
},
{
"path": "nn/nn_stress_usage_predictor/vocab.txt",
"size": 1080667,
"git_blob": "211012a925fc51581997b6d3ef047bcda9a842f4"
},
{
"path": "nn/nn_yo_homograph_resolver/config.json",
"size": 625,
"git_blob": "6615961d500dfe1fbbf302d1bbd4832b7e27f398"
},
{
"path": "nn/nn_yo_homograph_resolver/model.onnx",
"size": 14332169,
"sha256": "42cc85bf0c4b319dfe3d89fa17b162a92fd5e1c651a657cb3d5f44978d4e70ac"
},
{
"path": "nn/nn_yo_homograph_resolver/special_tokens_map.json",
"size": 125,
"git_blob": "a8b3208c2884c4efb86e49300fdd3dc877220cdf"
},
{
"path": "nn/nn_yo_homograph_resolver/tokenizer.json",
"size": 126952,
"git_blob": "bbab1a4bc4dfa68697338718621489e77d03e70a"
},
{
"path": "nn/nn_yo_homograph_resolver/tokenizer_config.json",
"size": 401,
"git_blob": "7d8a1ab4371909b9dcaabfcf93b9bbd56045605a"
},
{
"path": "nn/nn_yo_homograph_resolver/vocab.txt",
"size": 48576,
"git_blob": "32bc54d43a46178e70dcefb85626f52e9ddbc98b"
}
]
}
+25
View File
@@ -0,0 +1,25 @@
certifi==2026.7.22
charset-normalizer==3.5.1
filelock==4.0.4
flatbuffers==25.12.19
fsspec==2026.9.0
hf-xet==1.6.0
huggingface-hub==0.36.2
idna==3.20
numpy==2.5.3
onnxruntime==1.30.0
packaging==26.3
protobuf==7.36.2
python-crfsuite==0.9.12
pyyaml==6.0.3
razdel==0.5.0
regex==2026.9.10
requests==2.34.2
ruaccent==1.5.8.3
safetensors==0.8.0
sentencepiece==0.2.2
tokenizers==0.22.2
tqdm==4.70.1
transformers==4.57.6
typing-extensions==4.16.0
urllib3==2.8.0
+58
View File
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""Workspace-local RuAccent CPU setup. No sudo or changes to system Python."""
import hashlib
import io
import json
import os
from pathlib import Path
import platform
import runpy
import shutil
import subprocess
import urllib.request
import zipfile
ROOT = Path(__file__).resolve().parent.parent
def main():
uv = shutil.which("uv") or str(ROOT / ".local/tools/uv")
if not Path(uv).is_file():
if platform.system() != "Linux" or platform.machine() != "x86_64":
raise SystemExit("Install uv first on this platform, then rerun this setup script.")
with urllib.request.urlopen("https://pypi.org/pypi/uv/0.12.19/json", timeout=60) as source:
metadata = json.load(source)
wheel = next(x for x in metadata["urls"] if x["filename"].endswith("manylinux_2_17_x86_64.manylinux2014_x86_64.whl"))
with urllib.request.urlopen(wheel["url"], timeout=60) as source:
content = source.read()
expected = "a63d18a0aa38ee9f21a5406afbbaeb41303bcd954be9d6b7c1b95ac275e53958"
if hashlib.sha256(content).hexdigest() != expected:
raise RuntimeError("uv checksum mismatch")
with zipfile.ZipFile(io.BytesIO(content)) as archive:
name = next(x for x in archive.namelist() if x.endswith("/uv"))
Path(uv).parent.mkdir(parents=True, exist_ok=True)
Path(uv).write_bytes(archive.read(name))
Path(uv).chmod(0o755)
environment = dict(os.environ, UV_CACHE_DIR=str(ROOT / ".local/uv-cache"),
UV_PYTHON_INSTALL_DIR=str(ROOT / ".local/python"), UV_SYSTEM_CERTS="true")
venv = ROOT / ".local/ruaccent-runtime"
python = venv / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
if not python.is_file():
subprocess.run([uv, "venv", "--python", "3.12.14", str(venv)], env=environment, check=True)
subprocess.run([uv, "pip", "sync", "--python", str(python), str(ROOT / "scripts/ruaccent-requirements.txt")], env=environment, check=True)
model = ROOT / ".local/tts/ruaccent"
manifest = json.loads((ROOT / "scripts/ruaccent-model.json").read_text())
runpy.run_path(str(ROOT / "scripts/download-tts-model.py"))["download"](manifest, model)
# RUAccent 1.5.8.3 expects its rule-engine files beside the installed package.
module_path = subprocess.check_output([str(python), "-c", "import importlib.util; print(importlib.util.find_spec('ruaccent').submodule_search_locations[0])"], text=True).strip()
shutil.copytree(model / "koziev", Path(module_path) / "koziev", dirs_exist_ok=True)
worker = ROOT / "composeApp/src/jvmMain/resources/tts/ruaccent-worker.py"
shutil.copyfile(worker, model / "worker.py")
# Verify a wholly offline initialization before declaring the installation ready.
subprocess.run([str(python), str(model / "worker.py"), str(model), "--check"], check=True, env=environment)
(model / "runtime.json").write_text(json.dumps({"python": str(python), "model": "turbo3.1", "revision": manifest["revision"]}, indent=2) + "\n")
print("RuAccent ready: CPU, turbo3.1, full dictionary; no network during inference.", flush=True)
if __name__ == "__main__":
main()
+10
View File
@@ -0,0 +1,10 @@
filelock==3.32.3
fsspec==2026.7.0
jinja2==3.1.6
markupsafe==3.0.3
mpmath==1.3.0
networkx==3.6.1
setuptools==78.1.0
sympy==1.14.0
torch==2.8.0+cpu
typing-extensions==4.16.0
+67
View File
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
"""Audition official Silero v5_5_ru locally on CPU, without changing the reader."""
import argparse
import array
import hashlib
import json
import os
from pathlib import Path
import sys
import time
import wave
ROOT = Path(__file__).resolve().parent.parent
os.environ["CUDA_VISIBLE_DEVICES"] = ""
os.environ.setdefault("TORCH_HOME", str(ROOT / ".local/torch"))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--voices", nargs="+", default=["xenia", "baya", "aidar"])
args = parser.parse_args()
import torch
torch.set_num_threads(4)
torch.set_num_interop_threads(1)
torch.manual_seed(42)
model_path = ROOT / ".local/tts/silero-v5_5-ru/model.pt"
output = ROOT / "composeApp/build/tts-samples"
output.mkdir(parents=True, exist_ok=True)
start = time.perf_counter()
model = torch.package.PackageImporter(str(model_path)).load_pickle("tts_models", "model")
model.to(torch.device("cpu"))
loaded = time.perf_counter() - start
print(f"Silero v5_5_ru, CPU, 4 threads; load {loaded:.3f}s; voices {model.speakers}", flush=True)
samples = {
"ru": "В библиотеке было тихо. Она открыла книгу и начала читать. За окном шёл осенний дождь. На двери висел старый замок, а вдали виднелся замок на холме. Завтра, подумала она, мы наконец доберёмся до моря.",
"ru-yo": "Елка росла у дома. Ежик шел по лесу. Ребенок пьет теплое молоко. Все уже пришли. Он прочитал все. Она все еще ждет. Он узнает об этом завтра. Он узнает ее по голосу.",
}
report = {"model": "v5_5_ru", "sha256": hashlib.sha256(model_path.read_bytes()).hexdigest(),
"torch": torch.__version__, "device": "cpu", "threads": 4, "load_seconds": loaded,
"put_accent": True, "put_yo": True, "put_stress_homo": True, "put_yo_homo": True, "samples": []}
for voice in args.voices:
for label, text in samples.items():
start = time.perf_counter()
with torch.inference_mode():
audio = model.apply_tts(text=text, speaker=voice, sample_rate=48000,
put_accent=True, put_yo=True,
put_stress_homo=True, put_yo_homo=True).detach().cpu().flatten()
elapsed = time.perf_counter() - start
assert audio.numel() and torch.isfinite(audio).all() and audio.abs().max() > 0.001
duration = audio.numel() / 48000
pcm = array.array("h", (audio.clamp(-1, 1) * 32767).round().to(torch.int16).tolist())
if sys.byteorder != "little":
pcm.byteswap()
path = output / f"silero-{label}-{voice}.wav"
with wave.open(str(path), "wb") as wav:
wav.setparams((1, 2, 48000, 0, "NONE", "not compressed"))
wav.writeframes(pcm.tobytes())
path.with_suffix(".txt").write_text(text + "\n")
report["samples"].append({"file": path.name, "synthesis_seconds": elapsed,
"audio_seconds": duration, "rtf": elapsed / duration})
print(f"{path.name}: {elapsed:.3f}s synthesis, {duration:.3f}s audio, RTF {elapsed / duration:.3f}", flush=True)
(output / "silero-benchmark.json").write_text(json.dumps(report, indent=2) + "\n")
if __name__ == "__main__":
main()
+91
View File
@@ -0,0 +1,91 @@
{
"repository": "supertone-oss-archive/supertonic-3",
"revision": "aafc6e32416a594460b32413efc49d7fe4ce6d46",
"files": [
{
"path": "LICENSE",
"size": 15007,
"git_blob": "1e2cbe6cef94c8cdf3ed1fcebc0f5317ca7ad5a1"
},
{
"path": "onnx/duration_predictor.onnx",
"size": 3700147,
"sha256": "c3eb91414d5ff8a7a239b7fe9e34e7e2bf8a8140d8375ffb14718b1c639325db"
},
{
"path": "onnx/text_encoder.onnx",
"size": 36416150,
"sha256": "c7befd5ea8c3119769e8a6c1486c4edc6a3bc8365c67621c881bbb774b9902ff"
},
{
"path": "onnx/tts.json",
"size": 8253,
"git_blob": "28575b0793c92607a0f0a292550df9bd17709a79"
},
{
"path": "onnx/unicode_indexer.json",
"size": 277676,
"git_blob": "3aaf4d33024aff0be455633c24b3636b3a810150"
},
{
"path": "onnx/vector_estimator.onnx",
"size": 256534781,
"sha256": "883ac868ea0275ef0e991524dc64f16b3c0376efd7c320af6b53f5b780d7c61c"
},
{
"path": "onnx/vocoder.onnx",
"size": 101424195,
"sha256": "085de76dd8e8d5836d6ca66826601f615939218f90e519f70ee8a36ed2a4c4ba"
},
{
"path": "voice_styles/F1.json",
"size": 292046,
"git_blob": "421365f307ed1535d8da16845031d0e9fa3e60c5"
},
{
"path": "voice_styles/F2.json",
"size": 292423,
"git_blob": "b09a3a433df8a4856d1d7d64736a56ddf37a4ea3"
},
{
"path": "voice_styles/F3.json",
"size": 290794,
"git_blob": "a366f94c77c4b440404071f1a52bed3ccf83a5ff"
},
{
"path": "voice_styles/F4.json",
"size": 291808,
"git_blob": "39c78a52795320c6101b7eaf984c0e2352fbdc18"
},
{
"path": "voice_styles/F5.json",
"size": 291479,
"git_blob": "06983f8816ad00a8eeb7efd37a33d457587e16f8"
},
{
"path": "voice_styles/M1.json",
"size": 291748,
"git_blob": "bddfff85a7c4c140c11620bd7005d86681dccdac"
},
{
"path": "voice_styles/M2.json",
"size": 292055,
"git_blob": "602ff6c4d6d48cdb71c37461f7c175d71fe8b34b"
},
{
"path": "voice_styles/M3.json",
"size": 290198,
"git_blob": "000b60436b77619b933fdc33628ad737595e6794"
},
{
"path": "voice_styles/M4.json",
"size": 291522,
"git_blob": "aae253ae81185fa06a6469c3f1cdff04ed121ddf"
},
{
"path": "voice_styles/M5.json",
"size": 291469,
"git_blob": "0536b252698830f8536c077edcb94e1ce4768ab6"
}
]
}
@@ -15,7 +15,9 @@ data class Fb2Book(
val documentInfo: Fb2DocumentInfo = Fb2DocumentInfo(),
val bodyTitle: List<String> = emptyList(),
val bodyImages: List<Fb2ImageRef> = emptyList(),
val bodyEpigraphs: List<Fb2Epigraph> = emptyList(),
val sections: List<Fb2Section> = emptyList(),
val notes: Map<String, Fb2Section> = emptyMap(),
val binaries: List<Fb2Binary> = emptyList(),
) {
fun binaryFor(image: Fb2ImageRef): Fb2Binary? =
@@ -46,6 +48,7 @@ data class Fb2DocumentInfo(
)
data class Fb2Section(
val id: String? = null,
val title: String? = null,
val paragraphs: List<String> = emptyList(),
val images: List<Fb2ImageRef> = emptyList(),
@@ -69,10 +72,50 @@ data class Fb2Binary(
sealed interface Fb2Block {
data class Paragraph(val content: Fb2Text) : Fb2Block
data class Subtitle(val content: Fb2Text) : Fb2Block
data class Epigraph(val epigraph: Fb2Epigraph) : Fb2Block
data class Cite(val cite: Fb2Cite) : Fb2Block
data class Poem(val poem: Fb2Poem) : Fb2Block
data class Image(val image: Fb2ImageRef) : Fb2Block
data object EmptyLine : Fb2Block
}
data class Fb2Epigraph(
val blocks: List<Fb2EpigraphBlock> = emptyList(),
val textAuthors: List<Fb2Text> = emptyList(),
)
data class Fb2Cite(
val blocks: List<Fb2EpigraphBlock> = emptyList(),
val textAuthors: List<Fb2Text> = emptyList(),
)
sealed interface Fb2EpigraphBlock {
data class Paragraph(val content: Fb2Text) : Fb2EpigraphBlock
data class Subtitle(val content: Fb2Text) : Fb2EpigraphBlock
data class Poem(val poem: Fb2Poem) : Fb2EpigraphBlock
data class Cite(val cite: Fb2Cite) : Fb2EpigraphBlock
data object EmptyLine : Fb2EpigraphBlock
}
data class Fb2Poem(
val title: List<Fb2Text> = emptyList(),
val epigraphs: List<Fb2Epigraph> = emptyList(),
val blocks: List<Fb2PoemBlock> = emptyList(),
val textAuthors: List<Fb2Text> = emptyList(),
val date: String? = null,
)
sealed interface Fb2PoemBlock {
data class Subtitle(val content: Fb2Text) : Fb2PoemBlock
data class Stanza(val stanza: Fb2Stanza) : Fb2PoemBlock
}
data class Fb2Stanza(
val title: List<Fb2Text> = emptyList(),
val subtitle: Fb2Text? = null,
val verses: List<Fb2Text> = emptyList(),
)
data class Fb2Text(
val spans: List<Fb2TextSpan>,
) {
@@ -83,6 +126,8 @@ data class Fb2Text(
data class Fb2TextSpan(
val text: String,
val styles: Set<Fb2TextStyle> = emptySet(),
val href: String? = null,
val linkType: String? = null,
)
enum class Fb2TextStyle {
@@ -6,6 +6,7 @@ internal object Fb2XmlEncoding {
fun decodeXml(bytes: ByteArray): String =
when (declaredEncoding(bytes)?.lowercase()) {
"windows-1251" -> decodeWindows1251(bytes)
"windows-1252" -> decodeWindows1252(bytes)
else -> bytes.decodeToString()
}
@@ -30,6 +31,12 @@ internal object Fb2XmlEncoding {
}
}
private fun decodeWindows1252(bytes: ByteArray): String = buildString(bytes.size) {
bytes.forEach { byte ->
append(windows1252Char(byte.toInt() and 0xff))
}
}
private fun windows1251Char(value: Int): Char =
when (value) {
in 0x00..0x7f -> value.toChar()
@@ -100,4 +107,43 @@ internal object Fb2XmlEncoding {
in 0xc0..0xff -> (0x0410 + value - 0xc0).toChar()
else -> '\uFFFD'
}
private fun windows1252Char(value: Int): Char =
when (value) {
in 0x00..0x7f -> value.toChar()
0x80 -> '\u20ac'
0x81 -> '\uFFFD'
0x82 -> '\u201a'
0x83 -> '\u0192'
0x84 -> '\u201e'
0x85 -> '\u2026'
0x86 -> '\u2020'
0x87 -> '\u2021'
0x88 -> '\u02c6'
0x89 -> '\u2030'
0x8a -> '\u0160'
0x8b -> '\u2039'
0x8c -> '\u0152'
0x8d -> '\uFFFD'
0x8e -> '\u017d'
0x8f -> '\uFFFD'
0x90 -> '\uFFFD'
0x91 -> '\u2018'
0x92 -> '\u2019'
0x93 -> '\u201c'
0x94 -> '\u201d'
0x95 -> '\u2022'
0x96 -> '\u2013'
0x97 -> '\u2014'
0x98 -> '\u02dc'
0x99 -> '\u2122'
0x9a -> '\u0161'
0x9b -> '\u203a'
0x9c -> '\u0153'
0x9d -> '\uFFFD'
0x9e -> '\u017e'
0x9f -> '\u0178'
in 0xa0..0xff -> value.toChar()
else -> '\uFFFD'
}
}
@@ -13,8 +13,15 @@ internal object Fb2XmlMapper {
?: throw Fb2ParseException("FB2 document is missing description/title-info")
val documentInfo = description.first("document-info")
val body = root.children("body").firstOrNull { it.attributes["name"] != "notes" }
val bodies = root.children("body")
val body = bodies.firstOrNull { it.attributes["name"] != "notes" }
?: root.first("body")
val notes = bodies
.filter { it.attributes["name"] == "notes" }
.flatMap { it.children("section") }
.map(::sectionFrom)
.mapNotNull { section -> section.id?.let { it to section } }
.toMap()
return Fb2Book(
title = titleInfo.firstText("book-title")
@@ -32,7 +39,9 @@ internal object Fb2XmlMapper {
documentInfo = documentInfoFrom(documentInfo),
bodyTitle = body?.first("title")?.children("p")?.mapNotNull { it.text().ifBlank { null } }.orEmpty(),
bodyImages = body?.children("image")?.mapNotNull(::imageRefFrom).orEmpty(),
bodyEpigraphs = body?.children("epigraph")?.map(::epigraphFrom).orEmpty(),
sections = body?.children("section")?.map(::sectionFrom).orEmpty(),
notes = notes,
binaries = root.children("binary").mapNotNull(::binaryFrom),
)
}
@@ -124,6 +133,9 @@ internal object Fb2XmlMapper {
when (child.localName) {
"p" -> Fb2Block.Paragraph(textFrom(child))
"subtitle" -> Fb2Block.Subtitle(textFrom(child))
"epigraph" -> Fb2Block.Epigraph(epigraphFrom(child))
"cite" -> Fb2Block.Cite(citeFrom(child))
"poem" -> Fb2Block.Poem(poemFrom(child))
"image" -> imageRefFrom(child)?.let(Fb2Block::Image)
"empty-line" -> Fb2Block.EmptyLine
else -> null
@@ -131,6 +143,7 @@ internal object Fb2XmlMapper {
}
return Fb2Section(
id = element.attributes["id"]?.takeIf { it.isNotBlank() },
title = element.first("title")?.text()?.ifBlank { null },
paragraphs = blocks.filterIsInstance<Fb2Block.Paragraph>()
.mapNotNull { it.content.plainText.ifBlank { null } },
@@ -140,14 +153,82 @@ internal object Fb2XmlMapper {
)
}
private fun textFrom(element: XmlElement): Fb2Text =
Fb2Text(spansFrom(element.nodes).mergeAdjacent())
private fun poemFrom(element: XmlElement): Fb2Poem =
Fb2Poem(
title = titleFrom(element.first("title")),
epigraphs = element.children("epigraph").map(::epigraphFrom),
blocks = element.nodes.mapNotNull { node ->
val child = (node as? XmlNode.ElementNode)?.element ?: return@mapNotNull null
when (child.localName) {
"subtitle" -> Fb2PoemBlock.Subtitle(textFrom(child))
"stanza" -> Fb2PoemBlock.Stanza(stanzaFrom(child))
else -> null
}
},
textAuthors = element.children("text-author").map(::textFrom),
date = element.first("date")?.text()?.ifBlank { null },
)
private fun spansFrom(nodes: List<XmlNode>, styles: Set<Fb2TextStyle> = emptySet()): List<Fb2TextSpan> =
private fun epigraphFrom(element: XmlElement): Fb2Epigraph =
Fb2Epigraph(
blocks = element.nodes.mapNotNull { node ->
val child = (node as? XmlNode.ElementNode)?.element ?: return@mapNotNull null
when (child.localName) {
"p" -> Fb2EpigraphBlock.Paragraph(textFrom(child))
"poem" -> Fb2EpigraphBlock.Poem(poemFrom(child))
"cite" -> Fb2EpigraphBlock.Cite(citeFrom(child))
"empty-line" -> Fb2EpigraphBlock.EmptyLine
else -> null
}
},
textAuthors = element.children("text-author").map(::textFrom),
)
private fun citeFrom(element: XmlElement): Fb2Cite =
Fb2Cite(
blocks = element.nodes.mapNotNull { node ->
val child = (node as? XmlNode.ElementNode)?.element ?: return@mapNotNull null
when (child.localName) {
"p" -> Fb2EpigraphBlock.Paragraph(textFrom(child))
"subtitle" -> Fb2EpigraphBlock.Subtitle(textFrom(child))
"poem" -> Fb2EpigraphBlock.Poem(poemFrom(child))
"empty-line" -> Fb2EpigraphBlock.EmptyLine
else -> null
}
},
textAuthors = element.children("text-author").map(::textFrom),
)
private fun stanzaFrom(element: XmlElement): Fb2Stanza =
Fb2Stanza(
title = titleFrom(element.first("title")),
subtitle = element.first("subtitle")?.let(::textFrom),
verses = element.children("v").map(::textFrom),
)
private fun titleFrom(element: XmlElement?): List<Fb2Text> =
element?.children("p")?.map(::textFrom).orEmpty()
private fun textFrom(element: XmlElement): Fb2Text =
Fb2Text(spansFrom(element.nodes).mergeAdjacent().trimBoundaryWhitespace())
private fun spansFrom(
nodes: List<XmlNode>,
styles: Set<Fb2TextStyle> = emptySet(),
href: String? = null,
linkType: String? = null,
): List<Fb2TextSpan> =
nodes.flatMap { node ->
when (node) {
is XmlNode.TextNode -> listOfNotNull(
node.text.takeIf { it.isNotEmpty() }?.let { Fb2TextSpan(it, styles) }
node.text.takeIf { it.isNotEmpty() }?.let {
Fb2TextSpan(
text = it,
styles = styles,
href = href,
linkType = linkType,
)
}
)
is XmlNode.ElementNode -> {
val element = node.element
@@ -160,7 +241,13 @@ internal object Fb2XmlMapper {
"sub" -> styles + Fb2TextStyle.Subscript
else -> styles
}
spansFrom(element.nodes, nestedStyles)
val nestedHref = element.attributeLocal("href")
?.takeIf { element.localName == "a" && it.isNotBlank() }
?: href
val nestedLinkType = element.attributes["type"]
?.takeIf { element.localName == "a" && it.isNotBlank() }
?: linkType
spansFrom(element.nodes, nestedStyles, nestedHref, nestedLinkType)
}
}
}
@@ -170,7 +257,11 @@ internal object Fb2XmlMapper {
val merged = mutableListOf<Fb2TextSpan>()
forEach { span ->
val previous = merged.lastOrNull()
if (previous != null && previous.styles == span.styles) {
if (previous != null &&
previous.styles == span.styles &&
previous.href == span.href &&
previous.linkType == span.linkType
) {
merged[merged.lastIndex] = previous.copy(text = previous.text + span.text)
} else {
merged += span
@@ -179,6 +270,14 @@ internal object Fb2XmlMapper {
return merged
}
private fun List<Fb2TextSpan>.trimBoundaryWhitespace(): List<Fb2TextSpan> {
if (isEmpty()) return emptyList()
val trimmed = toMutableList()
trimmed[0] = trimmed[0].copy(text = trimmed[0].text.trimStart())
trimmed[trimmed.lastIndex] = trimmed.last().copy(text = trimmed.last().text.trimEnd())
return trimmed.filter { it.text.isNotEmpty() }
}
private fun StringBuilder.appendSection(section: Fb2Section) {
append("<section>")
section.title?.takeIf { it.isNotBlank() }?.let {
@@ -60,6 +60,23 @@ class Fb2FormatTest {
assertEquals("Привет, мир.", book.sections.single().paragraphs.single())
}
@Test
fun parsesWindows1252PlainXml() {
val book = Fb2Format.parse(windows1252Xml.encodeWindows1252(), "legacy.fb2")
assertEquals("The “Test” Book", book.title)
assertEquals("It’s 25€ for café — OK.", book.sections.single().paragraphs.single())
}
@Test
fun parsesWindows1252StoredZip() {
val zip = Fb2Zip.createStoredZip("legacy.fb2", windows1252Xml.encodeWindows1252())
val book = Fb2Format.parse(zip, "legacy.fb2.zip")
assertEquals("The “Test” Book", book.title)
assertEquals("It’s 25€ for café — OK.", book.sections.single().paragraphs.single())
}
@Test
fun fallsBackToUtf8ForUnknownEncoding() {
val xml = sampleXml.replace("encoding=\"UTF-8\"", "encoding=\"KOI8-R\"")
@@ -81,6 +98,53 @@ class Fb2FormatTest {
assertEquals("pic.png", (section.blocks[3] as Fb2Block.Image).image.binaryId)
}
@Test
fun parsesPoemsWithStanzasVersesAndInlineStyles() {
val book = Fb2Format.parseXml(poemXml)
val poem = (book.sections.single().blocks.single() as Fb2Block.Poem).poem
assertEquals("Song", poem.title.single().plainText)
val stanza = (poem.blocks.single() as Fb2PoemBlock.Stanza).stanza
assertEquals("First stanza", stanza.title.single().plainText)
assertEquals("Softly", stanza.subtitle?.plainText)
assertEquals(listOf("Line one", "Line two."), stanza.verses.map { it.plainText })
assertEquals(setOf(Fb2TextStyle.Emphasis), stanza.verses[0].spans.single().styles)
assertEquals("Poet", poem.textAuthors.single().plainText)
assertEquals("1910", poem.date)
}
@Test
fun parsesBodySectionAndPoemEpigraphs() {
val book = Fb2Format.parseXml(epigraphXml)
assertEquals("Body quote", (book.bodyEpigraphs.single().blocks.single() as Fb2EpigraphBlock.Paragraph).content.plainText)
val sectionBlocks = book.sections.single().blocks
val sectionEpigraph = (sectionBlocks[0] as Fb2Block.Epigraph).epigraph
assertEquals("Section quote", (sectionEpigraph.blocks[0] as Fb2EpigraphBlock.Paragraph).content.plainText)
assertEquals("Author", sectionEpigraph.textAuthors.single().plainText)
val cite = (sectionEpigraph.blocks[1] as Fb2EpigraphBlock.Cite).cite
assertEquals("Cited line", (cite.blocks.single() as Fb2EpigraphBlock.Paragraph).content.plainText)
assertEquals("Cited author", cite.textAuthors.single().plainText)
val poem = (sectionBlocks[1] as Fb2Block.Poem).poem
val poemEpigraph = poem.epigraphs.single()
assertEquals("Poem quote", (poemEpigraph.blocks.single() as Fb2EpigraphBlock.Paragraph).content.plainText)
}
@Test
fun parsesNoteLinksAndNotesBody() {
val book = Fb2Format.parseXml(notesXml)
val paragraph = book.sections.single().blocks.single() as Fb2Block.Paragraph
val link = paragraph.content.spans.single { it.href != null }
assertEquals("[1]", link.text)
assertEquals("#n_1", link.href)
assertEquals("note", link.linkType)
assertEquals("Note text.", (book.notes.getValue("n_1").blocks.single() as Fb2Block.Paragraph).content.plainText)
}
private val sampleXml = """
<?xml version="1.0" encoding="UTF-8"?>
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
@@ -134,6 +198,78 @@ class Fb2FormatTest {
</FictionBook>
""".trimIndent()
private val poemXml = """
<?xml version="1.0" encoding="UTF-8"?>
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<description>
<title-info>
<author><nickname>A</nickname></author>
<book-title>Poetry</book-title>
<lang>en</lang>
</title-info>
<document-info>
<author><nickname>Toread</nickname></author>
<date>2026-05-12</date>
<id>poetry</id>
<version>1.0</version>
</document-info>
</description>
<body>
<section>
<poem>
<title><p>Song</p></title>
<stanza>
<title><p>First stanza</p></title>
<subtitle>Softly</subtitle>
<v>
<emphasis>Line one</emphasis>
</v>
<v>Line two.</v>
</stanza>
<text-author>Poet</text-author>
<date>1910</date>
</poem>
</section>
</body>
</FictionBook>
""".trimIndent()
private val epigraphXml = """
<?xml version="1.0" encoding="UTF-8"?>
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<description>
<title-info>
<author><nickname>A</nickname></author>
<book-title>Epigraphs</book-title>
<lang>en</lang>
</title-info>
<document-info>
<author><nickname>Toread</nickname></author>
<date>2026-05-12</date>
<id>epigraphs</id>
<version>1.0</version>
</document-info>
</description>
<body>
<epigraph><p>Body quote</p></epigraph>
<section>
<epigraph>
<p>Section quote</p>
<cite>
<p>Cited line</p>
<text-author>Cited author</text-author>
</cite>
<text-author>Author</text-author>
</epigraph>
<poem>
<epigraph><p>Poem quote</p></epigraph>
<stanza><v>Line.</v></stanza>
</poem>
</section>
</body>
</FictionBook>
""".trimIndent()
private val windows1251Xml = """
<?xml version="1.0" encoding="windows-1251"?>
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
@@ -154,6 +290,55 @@ class Fb2FormatTest {
</FictionBook>
""".trimIndent()
private val windows1252Xml = """
<?xml version="1.0" encoding="windows-1252"?>
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<description>
<title-info>
<author><nickname>Author</nickname></author>
<book-title>The “Test” Book</book-title>
<lang>en</lang>
</title-info>
<document-info>
<author><nickname>Toread</nickname></author>
<date>2026-05-12</date>
<id>legacy-1252</id>
<version>1.0</version>
</document-info>
</description>
<body><section><p>It’s 25€ for café — OK.</p></section></body>
</FictionBook>
""".trimIndent()
private val notesXml = """
<?xml version="1.0" encoding="UTF-8"?>
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">
<description>
<title-info>
<author><nickname>A</nickname></author>
<book-title>Notes</book-title>
<lang>en</lang>
</title-info>
<document-info>
<author><nickname>Toread</nickname></author>
<date>2026-05-12</date>
<id>notes</id>
<version>1.0</version>
</document-info>
</description>
<body>
<section>
<p>Main<a l:href="#n_1" type="note">[1]</a>.</p>
</section>
</body>
<body name="notes">
<section id="n_1">
<p>Note text.</p>
</section>
</body>
</FictionBook>
""".trimIndent()
private fun String.encodeWindows1251(): ByteArray =
ByteArray(length) { index ->
val char = this[index]
@@ -166,4 +351,23 @@ class Fb2FormatTest {
}
value.toByte()
}
private fun String.encodeWindows1252(): ByteArray =
ByteArray(length) { index ->
val char = this[index]
val value = when {
char.code <= 0x7f -> char.code
char.code in 0xa0..0xff -> char.code
char == '€' -> 0x80
char == '‘' -> 0x91
char == '’' -> 0x92
char == '“' -> 0x93
char == '”' -> 0x94
char == '–' -> 0x96
char == '—' -> 0x97
char == '™' -> 0x99
else -> error("Test character $char is not mapped to windows-1252")
}
value.toByte()
}
}
@@ -702,21 +702,14 @@ private class JdbcBookFileRepository(private val connection: Connection) : BookF
FROM book_files f
LEFT JOIN books b ON b.id = f.book_id
WHERE f.duplicate_of_file_id IS NULL
ORDER BY
CASE
WHEN f.reading_status = 'READING' THEN 0
WHEN f.reading_status = 'NOT_INTERESTED' THEN 2
ELSE 1
END,
CASE WHEN f.reading_status = 'READING' THEN f.last_read_at END DESC NULLS LAST,
LOWER(COALESCE(NULLIF(b.title, ''), NULLIF(f.original_filename, ''), f.id)),
f.id
LIMIT ? OFFSET ?
""".trimIndent()
).use { statement ->
statement.setInt(1, limit)
statement.setInt(2, offset)
statement.executeQuery().use { resultSet -> resultSet.mapRows { it.toLibraryFileRecord() } }
statement.executeQuery().use { resultSet ->
resultSet.mapRows { it.toLibraryFileRecord() }
.sortedWith(LibraryFileNaturalTitleComparator)
.drop(offset)
.take(limit)
}
}
}
@@ -1097,6 +1090,50 @@ private fun String?.matchPositions(prefixes: List<String>, fieldOffset: Int): Li
private val SearchWordRegex = Regex("""[\p{L}\p{N}]+""")
private val LibraryFileNaturalTitleComparator = Comparator<LibraryFileRecord> { left, right ->
val titleCompare = naturalCompare(left.sortTitle(), right.sortTitle())
if (titleCompare != 0) titleCompare else left.fileId.compareTo(right.fileId)
}
private fun LibraryFileRecord.sortTitle(): String =
title?.takeIf(String::isNotBlank)
?: originalFilename?.takeIf(String::isNotBlank)
?: fileId
private fun naturalCompare(left: String, right: String): Int {
var leftIndex = 0
var rightIndex = 0
var exactFallback = 0
while (leftIndex < left.length && rightIndex < right.length) {
val leftChar = left[leftIndex]
val rightChar = right[rightIndex]
if (leftChar.isDigit() && rightChar.isDigit()) {
val leftStart = leftIndex
val rightStart = rightIndex
while (leftIndex < left.length && left[leftIndex].isDigit()) leftIndex += 1
while (rightIndex < right.length && right[rightIndex].isDigit()) rightIndex += 1
val leftDigits = left.substring(leftStart, leftIndex).trimStart('0')
val rightDigits = right.substring(rightStart, rightIndex).trimStart('0')
val leftNumber = leftDigits.ifEmpty { "0" }
val rightNumber = rightDigits.ifEmpty { "0" }
val lengthCompare = leftNumber.length.compareTo(rightNumber.length)
if (lengthCompare != 0) return lengthCompare
val numberCompare = leftNumber.compareTo(rightNumber)
if (numberCompare != 0) return numberCompare
val rawLengthCompare = (leftIndex - leftStart).compareTo(rightIndex - rightStart)
if (rawLengthCompare != 0) return rawLengthCompare
} else {
val foldedCompare = leftChar.lowercaseChar().compareTo(rightChar.lowercaseChar())
if (foldedCompare != 0) return foldedCompare
if (exactFallback == 0) exactFallback = leftChar.compareTo(rightChar)
leftIndex += 1
rightIndex += 1
}
}
return left.length.compareTo(right.length).takeIf { it != 0 } ?: exactFallback
}
private fun ResultSet.toLibraryFileRecord() = LibraryFileRecord(
fileId = getString("file_id"),
bookId = getString("book_id"),
@@ -2,7 +2,12 @@ package net.sergeych.toread.storage.jdbc
import net.sergeych.toread.fb2.Fb2Block
import net.sergeych.toread.fb2.Fb2Book
import net.sergeych.toread.fb2.Fb2Cite
import net.sergeych.toread.fb2.Fb2Epigraph
import net.sergeych.toread.fb2.Fb2EpigraphBlock
import net.sergeych.toread.fb2.Fb2Format
import net.sergeych.toread.fb2.Fb2Poem
import net.sergeych.toread.fb2.Fb2PoemBlock
import net.sergeych.toread.fb2.Fb2Section
import net.sergeych.toread.storage.BodyClusterRecord
import net.sergeych.toread.storage.BookBodyRecord
@@ -21,6 +26,11 @@ data class LibraryScanSummary(
val currentFile: String? = null,
)
data class LibraryImportResult(
val fileId: String,
val imported: Boolean,
)
class LibraryScanner(
private val database: H2LibraryDatabase,
private val log: (String) -> Unit = {},
@@ -80,17 +90,29 @@ class LibraryScanner(
storageUri: String,
sizeBytes: Long?,
lastModifiedMillis: Long?,
): Boolean {
): Boolean =
importExternalFileAndGetResult(bytes, displayName, storageUri, sizeBytes, lastModifiedMillis).imported
fun importExternalFileAndGetResult(
bytes: ByteArray,
displayName: String,
storageUri: String,
sizeBytes: Long?,
lastModifiedMillis: Long?,
): LibraryImportResult {
val rawSha256 = bytes.sha256Hex()
if (sizeBytes != null && database.files.findByOriginalFilenameSizeAndRawSha256(displayName, sizeBytes, rawSha256).isNotEmpty()) {
return false
if (sizeBytes != null) {
database.files.findByOriginalFilenameSizeAndRawSha256(displayName, sizeBytes, rawSha256).firstOrNull()?.let {
return LibraryImportResult(fileId = it.id, imported = false)
}
}
val now = System.currentTimeMillis()
database.files.findPrimaryDuplicateTarget(bodyClusterId = null, bodyId = null, rawSha256 = rawSha256)?.let { duplicateTarget ->
val fileId = "file-${UUID.randomUUID()}"
database.files.upsert(
BookFileRecord(
id = "file-${UUID.randomUUID()}",
id = fileId,
bookId = duplicateTarget.bookId,
bodyId = duplicateTarget.bodyId,
bodyClusterId = duplicateTarget.bodyClusterId,
@@ -109,7 +131,7 @@ class LibraryScanner(
updatedAt = now,
)
)
return true
return LibraryImportResult(fileId = fileId, imported = true)
}
val book = Fb2Format.parse(bytes, displayName)
@@ -124,6 +146,7 @@ class LibraryScanner(
database.files.findPrimaryDuplicateTarget(bodyClusterId = clusterId, bodyId = bodyId, rawSha256 = rawSha256)
}
val bookId = duplicateTarget?.bookId ?: "book-${UUID.randomUUID()}"
val fileId = "file-${UUID.randomUUID()}"
database.transaction {
if (duplicateTarget == null || duplicateTarget.bookId == null) {
@@ -172,7 +195,7 @@ class LibraryScanner(
}
files.upsert(
BookFileRecord(
id = "file-${UUID.randomUUID()}",
id = fileId,
bookId = bookId,
bodyId = bodyId,
bodyClusterId = clusterId,
@@ -192,7 +215,7 @@ class LibraryScanner(
)
)
}
return true
return LibraryImportResult(fileId = fileId, imported = true)
}
private fun importLinkedFile(file: File): Boolean {
@@ -246,7 +269,7 @@ private fun String.bookMimeType(): String =
if (endsWith(".zip", ignoreCase = true)) "application/zip" else "application/x-fictionbook+xml"
private fun Fb2Book.canonicalText(): String =
sections.flatMap { it.textBlocks() }
(bodyEpigraphs.flatMap { it.textBlocks() } + sections.flatMap { it.textBlocks() })
.joinToString(separator = "\n") { it.normalizeForBodyHash() }
.trim()
@@ -256,8 +279,11 @@ private fun Fb2Section.textBlocks(): List<String> {
blocks.forEach { block ->
when (block) {
Fb2Block.EmptyLine -> Unit
is Fb2Block.Cite -> addAll(block.cite.textBlocks())
is Fb2Block.Epigraph -> addAll(block.epigraph.textBlocks())
is Fb2Block.Image -> Unit
is Fb2Block.Paragraph -> add(block.content.plainText)
is Fb2Block.Poem -> addAll(block.poem.textBlocks())
is Fb2Block.Subtitle -> add(block.content.plainText)
}
}
@@ -266,6 +292,42 @@ private fun Fb2Section.textBlocks(): List<String> {
return current + sections.flatMap { it.textBlocks() }
}
private fun Fb2Poem.textBlocks(): List<String> = buildList {
title.forEach { add(it.plainText) }
epigraphs.forEach { addAll(it.textBlocks()) }
blocks.forEach { block ->
when (block) {
is Fb2PoemBlock.Stanza -> {
block.stanza.title.forEach { add(it.plainText) }
block.stanza.subtitle?.let { add(it.plainText) }
block.stanza.verses.forEach { add(it.plainText) }
}
is Fb2PoemBlock.Subtitle -> add(block.content.plainText)
}
}
textAuthors.forEach { add(it.plainText) }
date?.let { add(it) }
}
private fun Fb2Epigraph.textBlocks(): List<String> = buildList {
blocks.forEach { addAll(it.textBlocks()) }
textAuthors.forEach { add(it.plainText) }
}
private fun Fb2Cite.textBlocks(): List<String> = buildList {
blocks.forEach { addAll(it.textBlocks()) }
textAuthors.forEach { add(it.plainText) }
}
private fun Fb2EpigraphBlock.textBlocks(): List<String> =
when (this) {
Fb2EpigraphBlock.EmptyLine -> emptyList()
is Fb2EpigraphBlock.Cite -> cite.textBlocks()
is Fb2EpigraphBlock.Paragraph -> listOf(content.plainText)
is Fb2EpigraphBlock.Poem -> poem.textBlocks()
is Fb2EpigraphBlock.Subtitle -> listOf(content.plainText)
}
private fun String.normalizeForBodyHash(): String =
lowercase()
.replace('\u00ad'.toString(), "")
@@ -161,17 +161,16 @@ class H2LibraryDatabaseTest {
}
@Test
fun listsReadingBooksFirstNotInterestedLastThenSortsByTitle() {
val db = H2LibraryDatabase.openMemory("listsReadingBooksFirstNotInterestedLastThenSortsByTitle")
fun listsLibraryFilesByNaturalTitle() {
val db = H2LibraryDatabase.openMemory("listsLibraryFilesByNaturalTitle")
val now = 1_700_000_000_000L
db.transaction {
listOf(
Triple("book-beta", "Beta", BookReadingStatus.NEW),
Triple("book-10", "Book 10", BookReadingStatus.READING),
Triple("book-alpha", "Alpha", BookReadingStatus.READ),
Triple("book-gamma", "Gamma", BookReadingStatus.READING),
Triple("book-aardvark", "Aardvark", BookReadingStatus.READING),
Triple("book-omega", "Omega", BookReadingStatus.NOT_INTERESTED),
Triple("book-2", "Book 2", BookReadingStatus.NEW),
Triple("book-beta", "Beta", BookReadingStatus.NOT_INTERESTED),
).forEachIndexed { index, (bookId, title, status) ->
books.upsert(
BookRecord(
@@ -189,7 +188,7 @@ class H2LibraryDatabaseTest {
originalFilename = "$title.fb2",
storageKind = BookFileStorageKind.EXTERNAL_URI,
readingStatus = status,
lastReadAt = if (title == "Aardvark") now + 500 else now + 100,
lastReadAt = now + index,
createdAt = now + index,
updatedAt = now + index,
)
@@ -198,7 +197,7 @@ class H2LibraryDatabaseTest {
}
assertEquals(
listOf("Aardvark", "Gamma", "Alpha", "Beta", "Omega"),
listOf("Alpha", "Beta", "Book 2", "Book 10"),
db.files.listLibraryFiles().map { it.title },
)
db.close()
@@ -15,13 +15,14 @@ class LibraryScannerTest {
val db = H2LibraryDatabase.openMemory("scansFb2ZipSubtreeIntoLinkedLibraryRecords")
try {
val summary = LibraryScanner(db).scanSubtree(File(root, "test_books"))
val expectedFiles = LibraryScanner.countSupportedBookFiles(File(root, "test_books"))
assertEquals(1, summary.scannedFiles)
assertEquals(1, summary.importedFiles)
assertEquals(expectedFiles, summary.scannedFiles)
assertEquals(expectedFiles, summary.importedFiles)
assertEquals(0, summary.skippedFiles)
assertEquals(0, summary.failedFiles)
val file = db.files.list().single()
val file = db.files.list().first { it.storageUri?.endsWith(".fb2.zip") == true }
assertEquals("fb2.zip", file.format)
assertTrue(file.storageUri?.endsWith(".fb2.zip") == true)
val book = assertNotNull(file.bookId?.let(db.books::get))
@@ -31,7 +32,7 @@ class LibraryScannerTest {
val second = LibraryScanner(db).scanSubtree(File(root, "test_books"))
assertEquals(0, second.importedFiles)
assertEquals(1, second.skippedFiles)
assertEquals(expectedFiles, second.skippedFiles)
} finally {
db.close()
}
@@ -92,6 +93,36 @@ class LibraryScannerTest {
}
}
@Test
fun importExternalFileResultReturnsExistingFileForExactRepeat() {
val root = findProjectRoot()
val bytes = File(root, "test_books/Maraini_Zapiski-Terezy-Numy.G7vc8A.872381.fb2.zip").readBytes()
val db = H2LibraryDatabase.openMemory("importExternalFileResultReturnsExistingFileForExactRepeat")
try {
val scanner = LibraryScanner(db)
val first = scanner.importExternalFileAndGetResult(
bytes = bytes,
displayName = "opened.fb2.zip",
storageUri = "/tmp/opened.fb2.zip",
sizeBytes = bytes.size.toLong(),
lastModifiedMillis = 1L,
)
val second = scanner.importExternalFileAndGetResult(
bytes = bytes,
displayName = "opened.fb2.zip",
storageUri = "/tmp/opened.fb2.zip",
sizeBytes = bytes.size.toLong(),
lastModifiedMillis = 1L,
)
assertTrue(first.imported)
assertEquals(false, second.imported)
assertEquals(first.fileId, second.fileId)
} finally {
db.close()
}
}
private fun findProjectRoot(): File {
var current = File(System.getProperty("user.dir")).absoluteFile
while (true) {
File diff suppressed because it is too large. Load diff