Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 74cb92278f | |||
| 5de6a8f110 | |||
| d4789c2373 | |||
| 8dc5a0fc0b | |||
| 105551f658 | |||
| 6a57bc601f |
@@ -22,3 +22,6 @@
|
|||||||
*.woff binary
|
*.woff binary
|
||||||
*.pyc binary
|
*.pyc binary
|
||||||
*.swp binary
|
*.swp binary
|
||||||
|
|
||||||
|
# Android packages
|
||||||
|
*.apk binary
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ android {
|
|||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId = "app.mihon"
|
applicationId = "app.mihon"
|
||||||
|
|
||||||
versionCode = 30
|
versionCode = 31
|
||||||
versionName = "0.20.4"
|
versionName = "0.20.5"
|
||||||
|
|
||||||
buildConfigField("String", "COMMIT_COUNT", "\"${getLatestCommitCount()}\"")
|
buildConfigField("String", "COMMIT_COUNT", "\"${getLatestCommitCount()}\"")
|
||||||
buildConfigField("String", "COMMIT_SHA", "\"${getLatestCommitSha()}\"")
|
buildConfigField("String", "COMMIT_SHA", "\"${getLatestCommitSha()}\"")
|
||||||
|
|||||||
@@ -117,6 +117,37 @@ class DownloadProvider(
|
|||||||
.firstOrNull()
|
.firstOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a downloaded EPUB/TXT document for a chapter if it exists.
|
||||||
|
*
|
||||||
|
* Document files are checked explicitly before the generic chapter path so
|
||||||
|
* a stale image directory with the same chapter name cannot shadow them.
|
||||||
|
*/
|
||||||
|
fun findChapterDocument(
|
||||||
|
chapterName: String,
|
||||||
|
chapterScanlator: String?,
|
||||||
|
chapterUrl: String,
|
||||||
|
mangaTitle: String,
|
||||||
|
source: Source,
|
||||||
|
): UniFile? {
|
||||||
|
val mangaDir = findMangaDir(mangaTitle, source) ?: return null
|
||||||
|
val chapterDirName = getChapterDirName(chapterName, chapterScanlator, chapterUrl)
|
||||||
|
val legacyChapterDirNames = getLegacyChapterDirNames(chapterName, chapterScanlator, chapterUrl)
|
||||||
|
|
||||||
|
return buildList {
|
||||||
|
DOCUMENT_DOWNLOAD_EXTENSIONS.forEach { extension ->
|
||||||
|
add("$chapterDirName.$extension")
|
||||||
|
}
|
||||||
|
legacyChapterDirNames.forEach { legacyName ->
|
||||||
|
DOCUMENT_DOWNLOAD_EXTENSIONS.forEach { extension ->
|
||||||
|
add("$legacyName.$extension")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.asSequence()
|
||||||
|
.mapNotNull { mangaDir.findFile(it) }
|
||||||
|
.firstOrNull { it.isFile }
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of downloaded directories for the chapters that exist.
|
* Returns a list of downloaded directories for the chapters that exist.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import eu.kanade.tachiyomi.source.model.Page
|
|||||||
import eu.kanade.tachiyomi.source.online.HttpSource
|
import eu.kanade.tachiyomi.source.online.HttpSource
|
||||||
import eu.kanade.tachiyomi.ui.reader.loader.ChapterLoader
|
import eu.kanade.tachiyomi.ui.reader.loader.ChapterLoader
|
||||||
import eu.kanade.tachiyomi.ui.reader.loader.DownloadPageLoader
|
import eu.kanade.tachiyomi.ui.reader.loader.DownloadPageLoader
|
||||||
|
import eu.kanade.tachiyomi.ui.reader.loader.TextReaderAnchor
|
||||||
import eu.kanade.tachiyomi.ui.reader.loader.TextReaderPage
|
import eu.kanade.tachiyomi.ui.reader.loader.TextReaderPage
|
||||||
import eu.kanade.tachiyomi.ui.reader.model.InsertPage
|
import eu.kanade.tachiyomi.ui.reader.model.InsertPage
|
||||||
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
|
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
|
||||||
@@ -851,15 +852,29 @@ class ReaderViewModel(
|
|||||||
|
|
||||||
val oldPageCount = chapter.pages?.size?.coerceAtLeast(1) ?: 1
|
val oldPageCount = chapter.pages?.size?.coerceAtLeast(1) ?: 1
|
||||||
val requestedPage = chapter.requestedPage.coerceIn(0, oldPageCount - 1)
|
val requestedPage = chapter.requestedPage.coerceIn(0, oldPageCount - 1)
|
||||||
// Anchor at the middle of the visible page so position remains stable
|
val currentTextPage = chapter.pages?.getOrNull(requestedPage) as? TextReaderPage
|
||||||
// when font size/spacing changes the total page count.
|
val readingAnchor = currentTextPage?.anchor?.let { anchor ->
|
||||||
|
val midpoint = anchor.startOffset +
|
||||||
|
((anchor.endOffsetExclusive - anchor.startOffset).coerceAtLeast(1L) / 2L)
|
||||||
|
TextReaderAnchor(
|
||||||
|
documentIndex = anchor.documentIndex,
|
||||||
|
startOffset = midpoint,
|
||||||
|
endOffsetExclusive = midpoint + 1L,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// Keep the page-ratio fallback for non-text/malformed cases, but prefer
|
||||||
|
// the source-text anchor so font/spacing changes return to the same text.
|
||||||
val readingProgress = ((requestedPage + 0.5f) / oldPageCount)
|
val readingProgress = ((requestedPage + 0.5f) / oldPageCount)
|
||||||
.coerceIn(0f, 0.999999f)
|
.coerceIn(0f, 0.999999f)
|
||||||
|
|
||||||
textRepaginationJob?.cancel()
|
textRepaginationJob?.cancel()
|
||||||
textRepaginationJob = viewModelScope.launchIO {
|
textRepaginationJob = viewModelScope.launchIO {
|
||||||
try {
|
try {
|
||||||
val oldLoader = chapterLoader.reloadTextChapter(chapter, readingProgress) ?: return@launchIO
|
val oldLoader = chapterLoader.reloadTextChapter(
|
||||||
|
chapter = chapter,
|
||||||
|
readingAnchor = readingAnchor,
|
||||||
|
readingProgress = readingProgress,
|
||||||
|
) ?: return@launchIO
|
||||||
|
|
||||||
// Resource cleanup must survive cancellation by the next slider value.
|
// Resource cleanup must survive cancellation by the next slider value.
|
||||||
viewModelScope.launchNonCancellable {
|
viewModelScope.launchNonCancellable {
|
||||||
|
|||||||
@@ -76,7 +76,11 @@ class ChapterLoader(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
suspend fun reloadTextChapter(chapter: ReaderChapter, readingProgress: Float): PageLoader? {
|
internal suspend fun reloadTextChapter(
|
||||||
|
chapter: ReaderChapter,
|
||||||
|
readingAnchor: TextReaderAnchor?,
|
||||||
|
readingProgress: Float,
|
||||||
|
): PageLoader? {
|
||||||
return withIOContext {
|
return withIOContext {
|
||||||
val newLoader = getPageLoader(chapter)
|
val newLoader = getPageLoader(chapter)
|
||||||
if (!newLoader.usesTextReaderStyle) {
|
if (!newLoader.usesTextReaderStyle) {
|
||||||
@@ -97,9 +101,20 @@ class ChapterLoader(
|
|||||||
|
|
||||||
val oldLoader = chapter.pageLoader
|
val oldLoader = chapter.pageLoader
|
||||||
chapter.pageLoader = newLoader
|
chapter.pageLoader = newLoader
|
||||||
chapter.requestedPage = (readingProgress.coerceIn(0f, 0.999999f) * pages.size)
|
val anchoredPageIndex = readingAnchor?.let { anchor ->
|
||||||
.toInt()
|
val textPages = pages.filterIsInstance<TextReaderPage>()
|
||||||
.coerceIn(0, pages.lastIndex)
|
textPages.firstOrNull { it.anchor.contains(anchor) }?.index
|
||||||
|
?: textPages
|
||||||
|
.filter { it.anchor.documentIndex == anchor.documentIndex }
|
||||||
|
.minByOrNull { page ->
|
||||||
|
kotlin.math.abs(page.anchor.startOffset - anchor.startOffset)
|
||||||
|
}
|
||||||
|
?.index
|
||||||
|
}
|
||||||
|
chapter.requestedPage = anchoredPageIndex
|
||||||
|
?: (readingProgress.coerceIn(0f, 0.999999f) * pages.size)
|
||||||
|
.toInt()
|
||||||
|
.coerceIn(0, pages.lastIndex)
|
||||||
chapter.state = ReaderChapter.State.Loaded(pages)
|
chapter.state = ReaderChapter.State.Loaded(pages)
|
||||||
oldLoader
|
oldLoader
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
@@ -130,6 +145,33 @@ class ChapterLoader(
|
|||||||
)
|
)
|
||||||
val bookOasisRef = dbChapter.url.toBookOasisChapterRef()
|
val bookOasisRef = dbChapter.url.toBookOasisChapterRef()
|
||||||
val kavitaChapterId = dbChapter.url.toKavitaChapterId()
|
val kavitaChapterId = dbChapter.url.toKavitaChapterId()
|
||||||
|
val httpSource = source as? HttpSource
|
||||||
|
val documentFallback: (() -> PageLoader)? = when {
|
||||||
|
httpSource != null && httpSource.isBookOasisSource() && bookOasisRef != null &&
|
||||||
|
(bookOasisRef.format == "epub" || bookOasisRef.format == "txt") -> {
|
||||||
|
{
|
||||||
|
BookOasisFilePageLoader(
|
||||||
|
context,
|
||||||
|
httpSource,
|
||||||
|
bookOasisRef,
|
||||||
|
readerPreferences.textReaderStyle(context),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
httpSource != null && httpSource.isKavitaSource() && kavitaChapterId != null -> {
|
||||||
|
{
|
||||||
|
KavitaCompatPageLoader(
|
||||||
|
context = context,
|
||||||
|
chapter = chapter,
|
||||||
|
source = httpSource,
|
||||||
|
chapterCache = chapterCache,
|
||||||
|
chapterId = kavitaChapterId,
|
||||||
|
textStyle = readerPreferences.textReaderStyle(context),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
return when {
|
return when {
|
||||||
isDownloaded -> DownloadPageLoader(
|
isDownloaded -> DownloadPageLoader(
|
||||||
@@ -138,6 +180,7 @@ class ChapterLoader(
|
|||||||
source,
|
source,
|
||||||
downloadManager,
|
downloadManager,
|
||||||
downloadProvider,
|
downloadProvider,
|
||||||
|
documentFallback,
|
||||||
)
|
)
|
||||||
source is LocalSource -> source.getFormat(chapter.chapter).let { format ->
|
source is LocalSource -> source.getFormat(chapter.chapter).let { format ->
|
||||||
when (format) {
|
when (format) {
|
||||||
|
|||||||
@@ -10,12 +10,14 @@ import eu.kanade.tachiyomi.source.Source
|
|||||||
import eu.kanade.tachiyomi.source.model.Page
|
import eu.kanade.tachiyomi.source.model.Page
|
||||||
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
|
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
|
||||||
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
|
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
|
||||||
|
import kotlinx.coroutines.CancellationException
|
||||||
import mihon.core.archive.archiveReader
|
import mihon.core.archive.archiveReader
|
||||||
import mihon.core.archive.epubReader
|
import mihon.core.archive.epubReader
|
||||||
import tachiyomi.core.common.storage.extension
|
import tachiyomi.core.common.storage.extension
|
||||||
import tachiyomi.domain.manga.model.Manga
|
import tachiyomi.domain.manga.model.Manga
|
||||||
import eu.kanade.tachiyomi.ui.reader.setting.ReaderPreferences
|
import eu.kanade.tachiyomi.ui.reader.setting.ReaderPreferences
|
||||||
import uy.kohesive.injekt.injectLazy
|
import uy.kohesive.injekt.injectLazy
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loader used to load a chapter from the downloaded chapters.
|
* Loader used to load a chapter from the downloaded chapters.
|
||||||
@@ -26,6 +28,7 @@ internal class DownloadPageLoader(
|
|||||||
private val source: Source,
|
private val source: Source,
|
||||||
private val downloadManager: DownloadManager,
|
private val downloadManager: DownloadManager,
|
||||||
private val downloadProvider: DownloadProvider,
|
private val downloadProvider: DownloadProvider,
|
||||||
|
private val documentFallback: (() -> PageLoader)? = null,
|
||||||
) : PageLoader() {
|
) : PageLoader() {
|
||||||
|
|
||||||
private val context: Context by injectLazy()
|
private val context: Context by injectLazy()
|
||||||
@@ -35,9 +38,35 @@ internal class DownloadPageLoader(
|
|||||||
|
|
||||||
override var isLocal: Boolean = true
|
override var isLocal: Boolean = true
|
||||||
override val usesTextReaderStyle: Boolean
|
override val usesTextReaderStyle: Boolean
|
||||||
get() = pageLoader?.usesTextReaderStyle == true
|
get() = pageLoader?.usesTextReaderStyle == true || findDownloadedDocument() != null
|
||||||
|
|
||||||
override suspend fun getPages(): List<ReaderPage> {
|
override suspend fun getPages(): List<ReaderPage> {
|
||||||
|
check(!isRecycled)
|
||||||
|
|
||||||
|
return try {
|
||||||
|
val pages = getDownloadedPages()
|
||||||
|
if (pages.isNotEmpty() || documentFallback == null) {
|
||||||
|
pages
|
||||||
|
} else {
|
||||||
|
getFallbackPages()
|
||||||
|
}
|
||||||
|
} catch (error: Throwable) {
|
||||||
|
if (error is CancellationException || documentFallback == null) throw error
|
||||||
|
getFallbackPages()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun getDownloadedPages(): List<ReaderPage> {
|
||||||
|
val downloadedDocument = findDownloadedDocument()
|
||||||
|
if (downloadedDocument != null) {
|
||||||
|
val readableDocument = prepareDownloadedDocument(downloadedDocument)
|
||||||
|
return when (readableDocument.extension?.lowercase()) {
|
||||||
|
"epub" -> getPagesFromEpub(readableDocument)
|
||||||
|
"txt" -> getPagesFromText(readableDocument)
|
||||||
|
else -> error("Unsupported downloaded document: ${downloadedDocument.name}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val dbChapter = chapter.chapter
|
val dbChapter = chapter.chapter
|
||||||
val chapterPath = downloadProvider.findChapterDir(
|
val chapterPath = downloadProvider.findChapterDir(
|
||||||
dbChapter.name,
|
dbChapter.name,
|
||||||
@@ -46,17 +75,79 @@ internal class DownloadPageLoader(
|
|||||||
manga.title,
|
manga.title,
|
||||||
source,
|
source,
|
||||||
)
|
)
|
||||||
|
|
||||||
return if (chapterPath?.isFile == true) {
|
return if (chapterPath?.isFile == true) {
|
||||||
when (chapterPath.extension?.lowercase()) {
|
getPagesFromArchive(chapterPath)
|
||||||
"epub" -> getPagesFromEpub(chapterPath)
|
|
||||||
"txt" -> getPagesFromText(chapterPath)
|
|
||||||
else -> getPagesFromArchive(chapterPath)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
getPagesFromDirectory()
|
getPagesFromDirectory()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun findDownloadedDocument(): UniFile? {
|
||||||
|
val dbChapter = chapter.chapter
|
||||||
|
return downloadProvider.findChapterDocument(
|
||||||
|
dbChapter.name,
|
||||||
|
dbChapter.scanlator,
|
||||||
|
dbChapter.url,
|
||||||
|
manga.title,
|
||||||
|
source,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mirror SAF-backed documents into the app cache before opening them.
|
||||||
|
* EPUB uses mmap internally, which is not guaranteed to work with every
|
||||||
|
* document provider even though regular stream reads/writes succeed.
|
||||||
|
*/
|
||||||
|
private fun prepareDownloadedDocument(file: UniFile): UniFile {
|
||||||
|
val extension = file.extension?.lowercase()
|
||||||
|
?: error("Downloaded document has no extension: ${file.name}")
|
||||||
|
val sourceLength = file.length()
|
||||||
|
if (sourceLength <= 0L) {
|
||||||
|
error("Downloaded document is empty: ${file.name}")
|
||||||
|
}
|
||||||
|
|
||||||
|
val cacheDir = File(context.cacheDir, "downloaded_reader").apply { mkdirs() }
|
||||||
|
val prefix = "${source.id}-${chapter.chapter.id}-"
|
||||||
|
val target = File(
|
||||||
|
cacheDir,
|
||||||
|
"$prefix$sourceLength-${file.lastModified()}.$extension",
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!target.isFile || target.length() != sourceLength) {
|
||||||
|
cacheDir.listFiles()
|
||||||
|
?.filter { it.name.startsWith(prefix) }
|
||||||
|
?.forEach { it.delete() }
|
||||||
|
|
||||||
|
val temp = File(cacheDir, "${target.name}.part")
|
||||||
|
if (temp.exists()) temp.delete()
|
||||||
|
file.openInputStream().use { input ->
|
||||||
|
temp.outputStream().use { output -> input.copyTo(output) }
|
||||||
|
}
|
||||||
|
if (temp.length() != sourceLength) {
|
||||||
|
temp.delete()
|
||||||
|
error("Downloaded document cache copy is incomplete: ${file.name}")
|
||||||
|
}
|
||||||
|
if (!temp.renameTo(target)) {
|
||||||
|
temp.copyTo(target, overwrite = true)
|
||||||
|
temp.delete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return UniFile.fromFile(target)
|
||||||
|
?: error("Unable to open cached downloaded document: ${target.absolutePath}")
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun getFallbackPages(): List<ReaderPage> {
|
||||||
|
pageLoader?.recycle()
|
||||||
|
val fallback = documentFallback?.invoke()
|
||||||
|
?: error("Downloaded document fallback is unavailable")
|
||||||
|
pageLoader = fallback
|
||||||
|
val pages = fallback.getPages()
|
||||||
|
isLocal = fallback.isLocal
|
||||||
|
return pages
|
||||||
|
}
|
||||||
|
|
||||||
override fun recycle() {
|
override fun recycle() {
|
||||||
super.recycle()
|
super.recycle()
|
||||||
pageLoader?.recycle()
|
pageLoader?.recycle()
|
||||||
|
|||||||
@@ -68,21 +68,26 @@ internal class EpubPageLoader(
|
|||||||
.map { it.attr("idref") }
|
.map { it.attr("idref") }
|
||||||
|
|
||||||
val result = mutableListOf<ReaderPage>()
|
val result = mutableListOf<ReaderPage>()
|
||||||
spine.forEach { id ->
|
spine.forEachIndexed { documentIndex, id ->
|
||||||
val href = manifest[id]?.attr("href").orEmpty()
|
val href = manifest[id]?.attr("href").orEmpty()
|
||||||
if (href.isBlank()) return@forEach
|
if (href.isBlank()) return@forEachIndexed
|
||||||
|
|
||||||
openEntry(packageHref, href)?.use { input ->
|
openEntry(packageHref, href)?.use { input ->
|
||||||
val text = extractText(input)
|
val text = extractText(input)
|
||||||
if (text.isBlank()) return@use
|
if (text.isBlank()) return@use
|
||||||
|
|
||||||
StringReader(text).use { textReader ->
|
StringReader(text).use { textReader ->
|
||||||
TextPageRenderer.paginate(textReader, style) { pageText ->
|
TextPageRenderer.paginate(textReader, style) { page ->
|
||||||
val ref = store.append(pageText)
|
val ref = store.append(page.text)
|
||||||
result += TextReaderPage(
|
result += TextReaderPage(
|
||||||
index = result.size,
|
index = result.size,
|
||||||
pageTextProvider = { store.read(ref) },
|
pageTextProvider = { store.read(ref) },
|
||||||
initialStyle = style,
|
initialStyle = style,
|
||||||
|
anchor = TextReaderAnchor(
|
||||||
|
documentIndex = documentIndex,
|
||||||
|
startOffset = page.startOffset,
|
||||||
|
endOffsetExclusive = page.endOffsetExclusive,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,12 +29,17 @@ internal class TextPageLoader(
|
|||||||
return try {
|
return try {
|
||||||
val pages = mutableListOf<ReaderPage>()
|
val pages = mutableListOf<ReaderPage>()
|
||||||
openDecodedReader().use { reader ->
|
openDecodedReader().use { reader ->
|
||||||
TextPageRenderer.paginate(reader, style) { pageText ->
|
TextPageRenderer.paginate(reader, style) { page ->
|
||||||
val ref = store.append(pageText)
|
val ref = store.append(page.text)
|
||||||
pages += TextReaderPage(
|
pages += TextReaderPage(
|
||||||
index = pages.size,
|
index = pages.size,
|
||||||
pageTextProvider = { store.read(ref) },
|
pageTextProvider = { store.read(ref) },
|
||||||
initialStyle = style,
|
initialStyle = style,
|
||||||
|
anchor = TextReaderAnchor(
|
||||||
|
documentIndex = 0,
|
||||||
|
startOffset = page.startOffset,
|
||||||
|
endOffsetExclusive = page.endOffsetExclusive,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,21 +18,31 @@ import java.io.InputStream
|
|||||||
import java.io.Reader
|
import java.io.Reader
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
internal data class PaginatedTextPage(
|
||||||
|
val text: String,
|
||||||
|
val startOffset: Long,
|
||||||
|
val endOffsetExclusive: Long,
|
||||||
|
)
|
||||||
|
|
||||||
internal object TextPageRenderer {
|
internal object TextPageRenderer {
|
||||||
private const val MARGIN_DP = 32f
|
private const val MARGIN_DP = 32f
|
||||||
private const val STREAM_BUFFER_CHARS = 64 * 1024
|
private const val STREAM_BUFFER_CHARS = 64 * 1024
|
||||||
private const val STREAM_READ_CHARS = 8 * 1024
|
private const val STREAM_READ_CHARS = 8 * 1024
|
||||||
|
|
||||||
fun paginate(text: String, style: TextReaderStyle): List<String> {
|
fun paginate(text: String, style: TextReaderStyle): List<PaginatedTextPage> {
|
||||||
if (text.isBlank()) return emptyList()
|
if (text.isBlank()) return emptyList()
|
||||||
|
|
||||||
return calculatePageSlices(text, style)
|
return calculatePageSlices(text, style)
|
||||||
.mapNotNull { slice ->
|
.mapNotNull { slice -> createPage(text, slice, 0L) }
|
||||||
text.substring(slice.start, slice.endExclusive)
|
.ifEmpty {
|
||||||
.trim()
|
listOf(
|
||||||
.takeIf { it.isNotEmpty() }
|
PaginatedTextPage(
|
||||||
|
text = text,
|
||||||
|
startOffset = 0L,
|
||||||
|
endOffsetExclusive = text.length.toLong(),
|
||||||
|
),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
.ifEmpty { listOf(text) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -43,11 +53,12 @@ internal object TextPageRenderer {
|
|||||||
fun paginate(
|
fun paginate(
|
||||||
reader: Reader,
|
reader: Reader,
|
||||||
style: TextReaderStyle,
|
style: TextReaderStyle,
|
||||||
onPage: (String) -> Unit,
|
onPage: (PaginatedTextPage) -> Unit,
|
||||||
) {
|
) {
|
||||||
val working = StringBuilder(STREAM_BUFFER_CHARS + STREAM_READ_CHARS)
|
val working = StringBuilder(STREAM_BUFFER_CHARS + STREAM_READ_CHARS)
|
||||||
val chars = CharArray(STREAM_READ_CHARS)
|
val chars = CharArray(STREAM_READ_CHARS)
|
||||||
var firstChunk = true
|
var firstChunk = true
|
||||||
|
var workingStartOffset = 0L
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
val read = reader.read(chars)
|
val read = reader.read(chars)
|
||||||
@@ -59,6 +70,7 @@ internal object TextPageRenderer {
|
|||||||
firstChunk = false
|
firstChunk = false
|
||||||
if (chars[0] == '\uFEFF') {
|
if (chars[0] == '\uFEFF') {
|
||||||
offset = 1
|
offset = 1
|
||||||
|
workingStartOffset = 1L
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (offset < read) {
|
if (offset < read) {
|
||||||
@@ -66,33 +78,52 @@ internal object TextPageRenderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (working.length >= STREAM_BUFFER_CHARS) {
|
if (working.length >= STREAM_BUFFER_CHARS) {
|
||||||
emitCompletePages(working, style, onPage)
|
val consumed = emitCompletePages(working, style, workingStartOffset, onPage)
|
||||||
|
workingStartOffset += consumed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (working.isNotEmpty()) {
|
if (working.isNotEmpty()) {
|
||||||
paginate(working.toString(), style).forEach(onPage)
|
val text = working.toString()
|
||||||
|
calculatePageSlices(text, style)
|
||||||
|
.mapNotNull { slice -> createPage(text, slice, workingStartOffset) }
|
||||||
|
.forEach(onPage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun emitCompletePages(
|
private fun emitCompletePages(
|
||||||
working: StringBuilder,
|
working: StringBuilder,
|
||||||
style: TextReaderStyle,
|
style: TextReaderStyle,
|
||||||
onPage: (String) -> Unit,
|
baseOffset: Long,
|
||||||
) {
|
onPage: (PaginatedTextPage) -> Unit,
|
||||||
|
): Long {
|
||||||
val text = working.toString()
|
val text = working.toString()
|
||||||
val slices = calculatePageSlices(text, style)
|
val slices = calculatePageSlices(text, style)
|
||||||
if (slices.size <= 1) return
|
if (slices.size <= 1) return 0L
|
||||||
|
|
||||||
for (index in 0 until slices.lastIndex) {
|
for (index in 0 until slices.lastIndex) {
|
||||||
val slice = slices[index]
|
createPage(text, slices[index], baseOffset)?.let(onPage)
|
||||||
text.substring(slice.start, slice.endExclusive)
|
|
||||||
.trim()
|
|
||||||
.takeIf { it.isNotEmpty() }
|
|
||||||
?.let(onPage)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
working.delete(0, slices.last().start)
|
val consumed = slices.last().start
|
||||||
|
working.delete(0, consumed)
|
||||||
|
return consumed.toLong()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createPage(
|
||||||
|
text: String,
|
||||||
|
slice: PageSlice,
|
||||||
|
baseOffset: Long,
|
||||||
|
): PaginatedTextPage? {
|
||||||
|
val pageText = text.substring(slice.start, slice.endExclusive)
|
||||||
|
.trim()
|
||||||
|
.takeIf { it.isNotEmpty() }
|
||||||
|
?: return null
|
||||||
|
return PaginatedTextPage(
|
||||||
|
text = pageText,
|
||||||
|
startOffset = baseOffset + slice.start,
|
||||||
|
endOffsetExclusive = baseOffset + slice.endExclusive,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun calculatePageSlices(text: String, style: TextReaderStyle): List<PageSlice> {
|
private fun calculatePageSlices(text: String, style: TextReaderStyle): List<PageSlice> {
|
||||||
@@ -157,7 +188,10 @@ internal object TextPageRenderer {
|
|||||||
return StaticLayout.Builder
|
return StaticLayout.Builder
|
||||||
.obtain(styledText, 0, styledText.length, paint, contentWidth)
|
.obtain(styledText, 0, styledText.length, paint, contentWidth)
|
||||||
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
|
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
|
||||||
.setIncludePad(true)
|
// Page slices are laid out again when rendered. Keeping font padding
|
||||||
|
// here can make the sliced layout taller than the same lines measured
|
||||||
|
// during pagination, which clips the last line at larger font sizes.
|
||||||
|
.setIncludePad(false)
|
||||||
.setLineSpacing(0f, style.lineSpacingPercent / 100f)
|
.setLineSpacing(0f, style.lineSpacingPercent / 100f)
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,10 +8,23 @@ import eu.kanade.tachiyomi.ui.reader.setting.TextReaderStyle
|
|||||||
* A rendered text page whose visual style can be refreshed cheaply while layout-affecting
|
* A rendered text page whose visual style can be refreshed cheaply while layout-affecting
|
||||||
* changes trigger a debounced chapter repagination to rebuild page boundaries.
|
* changes trigger a debounced chapter repagination to rebuild page boundaries.
|
||||||
*/
|
*/
|
||||||
|
internal data class TextReaderAnchor(
|
||||||
|
val documentIndex: Int,
|
||||||
|
val startOffset: Long,
|
||||||
|
val endOffsetExclusive: Long,
|
||||||
|
) {
|
||||||
|
fun contains(other: TextReaderAnchor): Boolean {
|
||||||
|
if (documentIndex != other.documentIndex) return false
|
||||||
|
val point = other.startOffset
|
||||||
|
return point in startOffset until endOffsetExclusive
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal class TextReaderPage(
|
internal class TextReaderPage(
|
||||||
index: Int,
|
index: Int,
|
||||||
private val pageTextProvider: () -> String,
|
private val pageTextProvider: () -> String,
|
||||||
initialStyle: TextReaderStyle,
|
initialStyle: TextReaderStyle,
|
||||||
|
val anchor: TextReaderAnchor,
|
||||||
) : ReaderPage(index) {
|
) : ReaderPage(index) {
|
||||||
|
|
||||||
@Volatile
|
@Volatile
|
||||||
@@ -26,7 +39,8 @@ internal class TextReaderPage(
|
|||||||
index: Int,
|
index: Int,
|
||||||
pageText: String,
|
pageText: String,
|
||||||
initialStyle: TextReaderStyle,
|
initialStyle: TextReaderStyle,
|
||||||
) : this(index, { pageText }, initialStyle)
|
anchor: TextReaderAnchor,
|
||||||
|
) : this(index, { pageText }, initialStyle, anchor)
|
||||||
|
|
||||||
fun updateStyle(newStyle: TextReaderStyle) {
|
fun updateStyle(newStyle: TextReaderStyle) {
|
||||||
style = newStyle
|
style = newStyle
|
||||||
|
|||||||
@@ -36,16 +36,15 @@ class ReaderSettingsViewModel(
|
|||||||
private var textSettingsDirty = false
|
private var textSettingsDirty = false
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Layout-affecting text settings are previewed quickly, then the chapter is
|
* Layout-affecting text settings are applied only after the chapter has been
|
||||||
* repaginated after a short debounce. This keeps the slider responsive while
|
* repaginated. Applying a new style to pages that still use old boundaries
|
||||||
* ensuring the number of characters/lines on a page follows the new style.
|
* can push text below the rendered page and clip the last lines.
|
||||||
*/
|
*/
|
||||||
fun requestTextSettingsReload() {
|
fun requestTextSettingsReload() {
|
||||||
textSettingsDirty = true
|
textSettingsDirty = true
|
||||||
textSettingsReloadJob?.cancel()
|
textSettingsReloadJob?.cancel()
|
||||||
textSettingsReloadJob = viewModelScope.launch {
|
textSettingsReloadJob = viewModelScope.launch {
|
||||||
delay(250)
|
delay(250)
|
||||||
onTextSettingsPreview()
|
|
||||||
textSettingsDirty = false
|
textSettingsDirty = false
|
||||||
onTextSettingsCommit()
|
onTextSettingsCommit()
|
||||||
textSettingsReloadJob = null
|
textSettingsReloadJob = null
|
||||||
@@ -77,7 +76,6 @@ class ReaderSettingsViewModel(
|
|||||||
}
|
}
|
||||||
textSettingsReloadJob?.cancel()
|
textSettingsReloadJob?.cancel()
|
||||||
textSettingsReloadJob = null
|
textSettingsReloadJob = null
|
||||||
onTextSettingsPreview()
|
|
||||||
textSettingsDirty = false
|
textSettingsDirty = false
|
||||||
onTextSettingsCommit()
|
onTextSettingsCommit()
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user