From f540e2f2a5fc5fbf88ef9f8b157a4ae28ba308e0 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 14 Jul 2020 22:05:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/data/entities/rule/ContentRule.kt | 3 ++- .../app/ui/book/read/page/ContentTextView.kt | 6 +++++- .../book/read/page/provider/ChapterProvider.kt | 5 +++-- .../ui/book/read/page/provider/ImageProvider.kt | 3 ++- .../java/io/legado/app/utils/StringExtensions.kt | 16 ++++++++++------ .../main/java/io/legado/app/utils/StringUtils.kt | 10 ---------- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt b/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt index 84cc2db3f..df7a8dae3 100644 --- a/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt @@ -8,5 +8,6 @@ data class ContentRule( var content: String? = null, var nextContentUrl: String? = null, var webJs: String? = null, - var sourceRegex: String? = null + var sourceRegex: String? = null, + var imageStyle: Int = 0 //0默认大小居中,1最大宽度 ) : Parcelable \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt index 8d8dea9df..e2365b047 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt @@ -158,7 +158,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at ) { textLine.textChars.forEach { textChar -> val rectF = RectF(textChar.start, lineTop, textChar.end, lineBottom) - ImageProvider.getImage(ReadBook.book!!, textChar.charData)?.let { + ImageProvider.getImage( + ReadBook.book!!, + textPage.chapterIndex, + textChar.charData + )?.let { canvas.drawBitmap(it, null, rectF, null) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 0632870f4..d74d3fefe 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -69,7 +69,7 @@ object ChapterProvider { src?.let { durY = setTypeImage( - book, src, durY, textPages + book, bookChapter, src, durY, textPages ) } } else { @@ -113,12 +113,13 @@ object ChapterProvider { private fun setTypeImage( book: Book, + chapter: BookChapter, src: String, y: Float, textPages: ArrayList ): Float { var durY = y - ImageProvider.getImage(book, src)?.let { + ImageProvider.getImage(book, chapter.index, src)?.let { var height = it.height var width = it.width if (it.width > visibleWidth) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt index 8270254a1..e3bb11bce 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt @@ -13,8 +13,9 @@ import java.io.FileOutputStream object ImageProvider { + val bitmaps = hashMapOf() - fun getImage(book: Book, src: String): Bitmap? { + fun getImage(book: Book, chapterIndex: Int, src: String): Bitmap? { val vFile = FileUtils.getFile( App.INSTANCE.externalFilesDir, "${MD5Utils.md5Encode16(src)}.jpg", diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index 03909dc95..6da5a6cc6 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -1,5 +1,8 @@ package io.legado.app.utils +val removeHtmlRegex = + "]*>".toRegex() + fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim() fun String?.isContentPath(): Boolean = this?.startsWith("content://") == true @@ -32,12 +35,13 @@ fun String?.isJsonArray(): Boolean = str.startsWith("[") && str.endsWith("]") } ?: false -fun String?.htmlFormat(): String = - this?.replace("]*>".toRegex(), "\n") - ?.replace("\\s*\\n+\\s*".toRegex(), "\n  ") - ?.replace("^[\\n\\s]+".toRegex(), "  ") - ?.replace("[\\n\\s]+$".toRegex(), "") - ?: "" +fun String?.htmlFormat(): String { + this ?: return "" + return this.replace(removeHtmlRegex, "\n") + .replace("\\s*\\n+\\s*".toRegex(), "\n  ") + .replace("^[\\n\\s]+".toRegex(), "  ") + .replace("[\\n\\s]+$".toRegex(), "") +} fun String.splitNotBlank(vararg delimiter: String): Array = run { this.split(*delimiter).map { it.trim() }.filterNot { it.isBlank() }.toTypedArray() diff --git a/app/src/main/java/io/legado/app/utils/StringUtils.kt b/app/src/main/java/io/legado/app/utils/StringUtils.kt index dda534eed..76f076af3 100644 --- a/app/src/main/java/io/legado/app/utils/StringUtils.kt +++ b/app/src/main/java/io/legado/app/utils/StringUtils.kt @@ -274,14 +274,4 @@ object StringUtils { return buf.toString() } - fun formatHtml(html: String): String { - return if (isEmpty(html)) "" else html.replace( - "(?i)<(br[\\s/]*|/*p.*?|/*div.*?)>".toRegex(), - "\n" - )// 替换特定标签为换行符 - .replace("<[script>]*.*?>| ".toRegex(), "")// 删除script标签对和空格转义符 - .replace("\\s*\\n+\\s*".toRegex(), "\n  ")// 移除空行,并增加段前缩进2个汉字 - .replace("^[\\n\\s]+".toRegex(), "  ")//移除开头空行,并增加段前缩进2个汉字 - .replace("[\\n\\s]+$".toRegex(), "") //移除尾部空行 - } }