From 2bf8185a16ce7935d2777af6686c7baac98f9ab8 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Mon, 10 Jan 2022 21:37:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=B7=E5=90=8D=E5=B1=85=E4=B8=AD=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../read/page/provider/ChapterProvider.kt | 99 +++++++++++-------- 1 file changed, 56 insertions(+), 43 deletions(-) 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 a91771c38..e9dadb9aa 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 @@ -129,11 +129,12 @@ object ChapterProvider { matcher.appendTail(sb) text = sb.toString() val isTitle = index == 0 + val isVolumeTitle = bookChapter.isVolume && isTitle && bookChapter.url == bookChapter.title val textPaint = if (isTitle) titlePaint else contentPaint if (!(isTitle && ReadBookConfig.titleMode == 2)) { setTypeText( absStartX, durY, text, textPages, stringBuilder, - isTitle, textPaint, srcList + isTitle, isVolumeTitle, textPaint, srcList ).let { absStartX = it.first durY = it.second @@ -146,11 +147,12 @@ object ChapterProvider { val text = content.substring(start, matcher.start()) if (text.isNotBlank()) { val isTitle = index == 0 + val isVolumeTitle = bookChapter.isVolume && isTitle && bookChapter.url == bookChapter.title val textPaint = if (isTitle) titlePaint else contentPaint if (!(isTitle && ReadBookConfig.titleMode == 2)) { setTypeText( absStartX, durY, text, textPages, - stringBuilder, isTitle, textPaint + stringBuilder, isTitle, isVolumeTitle, textPaint ).let { absStartX = it.first durY = it.second @@ -167,11 +169,12 @@ object ChapterProvider { val text = content.substring(start, content.length) if (text.isNotBlank()) { val isTitle = index == 0 + val isVolumeTitle = bookChapter.isVolume && isTitle && bookChapter.url == bookChapter.title val textPaint = if (isTitle) titlePaint else contentPaint if (!(isTitle && ReadBookConfig.titleMode == 2)) { setTypeText( absStartX, durY, text, textPages, - stringBuilder, isTitle, textPaint + stringBuilder, isTitle, isVolumeTitle, textPaint ).let { absStartX = it.first durY = it.second @@ -274,16 +277,21 @@ object ChapterProvider { textPages: ArrayList, stringBuilder: StringBuilder, isTitle: Boolean, + isVolumeTitle: Boolean, textPaint: TextPaint, srcList: LinkedList? = null ): Pair { var absStartX = x - var durY = if (isTitle) y + titleTopSpacing else y val layout = if (ReadBookConfig.useZhLayout) { ZhLayout(text, textPaint, visibleWidth) } else StaticLayout( text, textPaint, visibleWidth, Layout.Alignment.ALIGN_NORMAL, 0f, 0f, true ) + var durY = when { + isVolumeTitle -> ((visibleHeight - layout.lineCount * textPaint.textHeight) / 2).toFloat() + isTitle -> y + titleTopSpacing + else -> y + } for (lineIndex in 0 until layout.lineCount) { val textLine = TextLine(isTitle = isTitle) if (durY + textPaint.textHeight > visibleHeight) { @@ -309,44 +317,49 @@ object ChapterProvider { text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) val desiredWidth = layout.getLineWidth(lineIndex) var isLastLine = false - if (lineIndex == 0 && layout.lineCount > 1 && !isTitle) { - //第一行 - textLine.text = words - addCharsToLineFirst( - absStartX, - textLine, - words.toStringArray(), - textPaint, - desiredWidth, - srcList - ) - } else if (lineIndex == layout.lineCount - 1) { - //最后一行 - textLine.text = "$words\n" - isLastLine = true - val startX = if (isTitle && ReadBookConfig.titleMode == 1) - (visibleWidth - layout.getLineWidth(lineIndex)) / 2 - else 0f - addCharsToLineLast( - absStartX, - textLine, - words.toStringArray(), - textPaint, - startX, - srcList - ) - } else { - //中间行 - textLine.text = words - addCharsToLineMiddle( - absStartX, - textLine, - words.toStringArray(), - textPaint, - desiredWidth, - 0f, - srcList - ) + when { + lineIndex == 0 && layout.lineCount > 1 && !isTitle -> { + //第一行 非标题 + textLine.text = words + addCharsToLineFirst( + absStartX, + textLine, + words.toStringArray(), + textPaint, + desiredWidth, + srcList + ) + } + lineIndex == layout.lineCount - 1 -> { + //最后一行 + textLine.text = "$words\n" + isLastLine = true + //标题居中 + val startX = if (isTitle && ReadBookConfig.titleMode == 1 || isVolumeTitle) + (visibleWidth - layout.getLineWidth(lineIndex)) / 2 + else 0f + addCharsToLineLast( + absStartX, + textLine, + words.toStringArray(), + textPaint, + startX, + srcList + ) + } + else -> { + //中间行 + textLine.text = words + addCharsToLineMiddle( + absStartX, + textLine, + words.toStringArray(), + textPaint, + desiredWidth, + 0f, + srcList + ) + } } stringBuilder.append(words) if (isLastLine) stringBuilder.append("\n") @@ -355,7 +368,7 @@ object ChapterProvider { durY += textPaint.textHeight * lineSpacingExtra textPages.last().height = durY } - if (isTitle) durY += titleBottomSpacing + if (isTitle && !isVolumeTitle) durY += titleBottomSpacing durY += textPaint.textHeight * paragraphSpacing / 10f return Pair(absStartX, durY) }