From a2082857fcd39c5dbc0f95c522646618bfcbde5b Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 26 Mar 2020 21:17:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/localBook/AnalyzeTxtFile.kt | 99 +++++++++++-------- .../app/ui/book/read/ReadBookActivity.kt | 1 + .../app/ui/book/read/ReadBookViewModel.kt | 1 + .../ui/book/read/page/entities/TextPage.kt | 9 +- app/src/main/res/values/strings.xml | 1 + 5 files changed, 64 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt index f867434fa..1f35d1454 100644 --- a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt @@ -60,18 +60,18 @@ class AnalyzeTxtFile { var allLength = 0 //获取文件中的数据到buffer,直到没有数据为止 - while (bookStream.read(buffer, 0, buffer.size).also { length = it } > 0) { + while (bookStream.read(buffer).also { length = it } > 0) { blockPos++ - var blockContent = String(buffer, charset) - val lastN = blockContent.lastIndexOf("\n") - if (lastN > 0) { - blockContent = blockContent.substring(0, lastN) - length = blockContent.toByteArray(charset).size - allLength += length - bookStream.seek(allLength.toLong()) - } //如果存在Chapter if (rulePattern != null) { //将数据转换成String + var blockContent = String(buffer, charset) + val lastN = blockContent.lastIndexOf("\n") + if (lastN > 0) { + blockContent = blockContent.substring(0, lastN) + length = blockContent.toByteArray(charset).size + allLength += length + bookStream.seek(allLength.toLong()) + } //当前Block下使过的String的指针 var seekPos = 0 //进行正则匹配 @@ -81,47 +81,62 @@ class AnalyzeTxtFile { val chapterStart = matcher.start() //获取章节内容 val chapterContent = blockContent.substring(seekPos, chapterStart) - if (chapterContent.length > 30000 && pattern == null) { + val chapterLength = chapterContent.toByteArray(charset).size + if (chapterLength > 30000 && pattern == null) { //移除不匹配的规则 tocRules.remove(tocRule) return analyze(bookStream, book, null) } //如果 seekPos == 0 && nextChapterPos != 0 表示当前block处前面有一段内容 - //第一种情况一定是序章 第二种情况可能是上一个章节的内容 + //第一种情况一定是序章 第二种情况是上一个章节的内容 if (seekPos == 0 && chapterStart != 0) { //获取当前章节的内容 - val contentSize = chapterContent.toByteArray(charset).size.toLong() - if (toc.isEmpty()) { + if (toc.isEmpty()) { //如果当前没有章节,那么就是序章 + //加入简介 if (StringUtils.trim(chapterContent).isNotEmpty()) { - val chapter = BookChapter() - chapter.title = "前言" - chapter.start = 0 - chapter.end = contentSize - toc.add(chapter) - } - } else { - toc.last().let { - it.end = it.end!! + contentSize + val qyChapter = BookChapter() + qyChapter.title = "前言" + qyChapter.start = 0 + qyChapter.end = chapterLength.toLong() + toc.add(qyChapter) } + //创建当前章节 + val curChapter = BookChapter() + curChapter.title = matcher.group() + curChapter.start = chapterLength.toLong() + toc.add(curChapter) + } else { //否则就block分割之后,上一个章节的剩余内容 + //获取上一章节 + val lastChapter = toc.last() + //将当前段落添加上一章去 + lastChapter.end = + lastChapter.end!! + chapterLength.toLong() + //创建当前章节 + val curChapter = BookChapter() + curChapter.title = matcher.group() + curChapter.start = lastChapter.end + toc.add(curChapter) } - //创建当前章节 - val curChapter = BookChapter() - curChapter.title = matcher.group() - curChapter.start = (toc.lastOrNull()?.end ?: 0) + contentSize - toc.add(curChapter) } else { - val lastChapter = toc.lastOrNull() - lastChapter?.let { - //上一章节结束等于这一章节开始 - it.end = it.start!! + chapterContent.toByteArray(charset).size + if (toc.isNotEmpty()) { //获取章节内容 + //获取上一章节 + val lastChapter = toc.last() + lastChapter.end = + lastChapter.start!! + chapterContent.toByteArray(charset).size.toLong() + //创建当前章节 + val curChapter = BookChapter() + curChapter.title = matcher.group() + curChapter.start = lastChapter.end + toc.add(curChapter) + } else { //如果章节不存在则创建章节 + val curChapter = BookChapter() + curChapter.title = matcher.group() + curChapter.start = 0 + curChapter.end = 0 + toc.add(curChapter) } - //创建当前章节 - val curChapter = BookChapter() - curChapter.title = matcher.group() - curChapter.start = lastChapter?.end ?: 0 - toc.add(curChapter) } //设置指针偏移 - seekPos += chapterContent.length + seekPos += chapterLength } } else { //进行本地虚拟分章 //章节在buffer的偏移量 @@ -165,7 +180,8 @@ class AnalyzeTxtFile { //block的偏移点 curOffset += length.toLong() - if (rulePattern != null) { //设置上一章的结尾 + if (rulePattern != null) { + //设置上一章的结尾 val lastChapter = toc.last() lastChapter.end = curOffset } @@ -231,10 +247,9 @@ class AnalyzeTxtFile { val bookFile = getBookFile(App.INSTANCE, book) //获取文件流 val bookStream = RandomAccessFile(bookFile, "r") - bookStream.seek(bookChapter.start ?: 0) - val extent = (bookChapter.end!! - bookChapter.start!!).toInt() - val content = ByteArray(extent) - bookStream.read(content, 0, extent) + val content = ByteArray((bookChapter.end!! - bookChapter.start!!).toInt()) + bookStream.seek(bookChapter.start!!) + bookStream.read(content) return String(content, book.fileCharset()) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 043c419eb..93e39afad 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -225,6 +225,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo R.id.menu_copy_text -> TextDialog.show(supportFragmentManager, ReadBook.curTextChapter?.getContent()) R.id.menu_update_toc -> ReadBook.book?.let { + ReadBook.upMsg(getString(R.string.toc_updateing)) viewModel.loadChapterList(it) } R.id.menu_enable_replace -> ReadBook.book?.let { diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 68c3260b2..936393af9 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -107,6 +107,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { App.db.bookChapterDao().insert(*it.toTypedArray()) App.db.bookDao().update(book) ReadBook.chapterSize = it.size + ReadBook.upMsg(null) ReadBook.loadContent(resetPageOffset = true) } } else { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index 2fcf2f7e2..e81af6986 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -44,12 +44,11 @@ data class TextPage( if (y < 0) y = 0f for (lineIndex in 0 until layout.lineCount) { val textLine = TextLine() - textLine.lineTop = (ChapterProvider.paddingTop + y - - (layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))) - textLine.lineBase = (ChapterProvider.paddingTop + y - - (layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))) + textLine.lineTop = ChapterProvider.paddingTop + y + layout.getLineTop(lineIndex) + textLine.lineBase = + ChapterProvider.paddingTop + y + layout.getLineBaseline(lineIndex) textLine.lineBottom = - textLine.lineBase + ChapterProvider.contentPaint.fontMetrics.descent + ChapterProvider.paddingTop + y + layout.getLineBottom(lineIndex) var x = ChapterProvider.paddingLeft + (ChapterProvider.visibleWidth - layout.getLineMax(lineIndex)) / 2 textLine.text = diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4afe3398e..674cebb98 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -647,4 +647,5 @@ 阅读需要访问存储卡权限,请点击下方的"授予权限"按钮,或前往“设置”—“应用权限”—打开所需权限。如果授予权限后仍然不正常,请点击右上角的“选择文件夹”,使用系统文件夹选择器。 全文朗读中不能朗读选中文字 扩展到刘海 + 更新目录中