From 06fce757254d3a5608e6377a5cc25851bb163753 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 26 Jan 2023 13:52:41 +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/help/config/ReadBookConfig.kt | 8 ++++++++ .../read/page/provider/ChapterProvider.kt | 19 ++++++++++--------- .../ui/book/read/page/provider/ZhLayout.kt | 2 +- .../io/legado/app/utils/StringExtensions.kt | 8 +++++--- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt index cf3c75b01..70111a54b 100644 --- a/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt @@ -154,7 +154,15 @@ object ReadBookConfig { appCtx.putPrefBoolean(PreferKey.shareLayout, value) } } + + /** + * 两端对齐 + */ val textFullJustify get() = appCtx.getPrefBoolean(PreferKey.textFullJustify, true) + + /** + * 底部对齐 + */ val textBottomJustify get() = appCtx.getPrefBoolean(PreferKey.textBottomJustify, true) var hideStatusBar = appCtx.getPrefBoolean(PreferKey.hideStatusBar) var hideNavigationBar = appCtx.getPrefBoolean(PreferKey.hideNavigationBar) 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 6b948ac3c..fbb4167d6 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 @@ -300,10 +300,11 @@ object ChapterProvider { srcList: LinkedList? = null ): Pair { var absStartX = x - val layout = if (ReadBookConfig.useZhLayout) ZhLayout(text, textPaint, visibleWidth) - else StaticLayout( - text, textPaint, visibleWidth, Layout.Alignment.ALIGN_NORMAL, 0f, 0f, true - ) + val layout = if (ReadBookConfig.useZhLayout) { + ZhLayout(text, textPaint, visibleWidth) + } else { + StaticLayout(text, textPaint, visibleWidth, Layout.Alignment.ALIGN_NORMAL, 0f, 0f, true) + } var durY = when { //标题y轴居中 isTitleWithNoContent && textPages.size == 1 -> { @@ -375,7 +376,7 @@ object ChapterProvider { } else { 0f } - addCharsToLineLast( + addCharsToLineNatural( book, absStartX, textLine, words.toStringArray(), textPaint, startX, srcList ) @@ -416,7 +417,7 @@ object ChapterProvider { ) { var x = 0f if (!ReadBookConfig.textFullJustify) { - addCharsToLineLast(book, absStartX, textLine, words, textPaint, x, srcList) + addCharsToLineNatural(book, absStartX, textLine, words, textPaint, x, srcList) return } val bodyIndent = ReadBookConfig.paragraphIndent @@ -454,7 +455,7 @@ object ChapterProvider { srcList: LinkedList? ) { if (!ReadBookConfig.textFullJustify) { - addCharsToLineLast(book, absStartX, textLine, words, textPaint, startX, srcList) + addCharsToLineNatural(book, absStartX, textLine, words, textPaint, startX, srcList) return } val gapCount: Int = words.lastIndex @@ -470,9 +471,9 @@ object ChapterProvider { } /** - * 最后一行,自然排列 + * 自然排列 */ - private suspend fun addCharsToLineLast( + private suspend fun addCharsToLineNatural( book: Book, absStartX: Int, textLine: TextLine, diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ZhLayout.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ZhLayout.kt index 648392e83..c2aaa5a7d 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ZhLayout.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ZhLayout.kt @@ -12,7 +12,7 @@ import kotlin.math.max * */ @Suppress("MemberVisibilityCanBePrivate", "unused") class ZhLayout( - text: String, + text: CharSequence, textPaint: TextPaint, width: Int ) : Layout(text, textPaint, width, Alignment.ALIGN_NORMAL, 0f, 0f) { 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 74d7eac7a..dc58912d9 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -8,6 +8,8 @@ import android.net.Uri import android.text.Editable import io.legado.app.constant.AppPattern.dataUriRegex import java.io.File +import java.lang.Character.codePointCount +import java.lang.Character.offsetByCodePoints import java.util.* fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim() @@ -100,12 +102,12 @@ fun String?.memorySize(): Int { /** * 将字符串拆分为单个字符,包含emoji */ -fun String.toStringArray(): Array { +fun CharSequence.toStringArray(): Array { var codePointIndex = 0 return try { - Array(codePointCount(0, length)) { + Array(codePointCount(this, 0, length)) { val start = codePointIndex - codePointIndex = offsetByCodePoints(start, 1) + codePointIndex = offsetByCodePoints(this, start, 1) substring(start, codePointIndex) } } catch (e: Exception) {