Merge pull request #1513 from Xwite/master

卷名居中显示
pull/1514/head
kunfei 3 years ago committed by GitHub
commit 8ff19ba0c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt

@ -129,11 +129,12 @@ object ChapterProvider {
matcher.appendTail(sb) matcher.appendTail(sb)
text = sb.toString() text = sb.toString()
val isTitle = index == 0 val isTitle = index == 0
val isVolumeTitle = bookChapter.isVolume && isTitle && bookChapter.url == bookChapter.title
val textPaint = if (isTitle) titlePaint else contentPaint val textPaint = if (isTitle) titlePaint else contentPaint
if (!(isTitle && ReadBookConfig.titleMode == 2)) { if (!(isTitle && ReadBookConfig.titleMode == 2)) {
setTypeText( setTypeText(
absStartX, durY, text, textPages, stringBuilder, absStartX, durY, text, textPages, stringBuilder,
isTitle, textPaint, srcList isTitle, isVolumeTitle, textPaint, srcList
).let { ).let {
absStartX = it.first absStartX = it.first
durY = it.second durY = it.second
@ -146,11 +147,12 @@ object ChapterProvider {
val text = content.substring(start, matcher.start()) val text = content.substring(start, matcher.start())
if (text.isNotBlank()) { if (text.isNotBlank()) {
val isTitle = index == 0 val isTitle = index == 0
val isVolumeTitle = bookChapter.isVolume && isTitle && bookChapter.url == bookChapter.title
val textPaint = if (isTitle) titlePaint else contentPaint val textPaint = if (isTitle) titlePaint else contentPaint
if (!(isTitle && ReadBookConfig.titleMode == 2)) { if (!(isTitle && ReadBookConfig.titleMode == 2)) {
setTypeText( setTypeText(
absStartX, durY, text, textPages, absStartX, durY, text, textPages,
stringBuilder, isTitle, textPaint stringBuilder, isTitle, isVolumeTitle, textPaint
).let { ).let {
absStartX = it.first absStartX = it.first
durY = it.second durY = it.second
@ -167,11 +169,12 @@ object ChapterProvider {
val text = content.substring(start, content.length) val text = content.substring(start, content.length)
if (text.isNotBlank()) { if (text.isNotBlank()) {
val isTitle = index == 0 val isTitle = index == 0
val isVolumeTitle = bookChapter.isVolume && isTitle && bookChapter.url == bookChapter.title
val textPaint = if (isTitle) titlePaint else contentPaint val textPaint = if (isTitle) titlePaint else contentPaint
if (!(isTitle && ReadBookConfig.titleMode == 2)) { if (!(isTitle && ReadBookConfig.titleMode == 2)) {
setTypeText( setTypeText(
absStartX, durY, text, textPages, absStartX, durY, text, textPages,
stringBuilder, isTitle, textPaint stringBuilder, isTitle, isVolumeTitle, textPaint
).let { ).let {
absStartX = it.first absStartX = it.first
durY = it.second durY = it.second
@ -274,16 +277,21 @@ object ChapterProvider {
textPages: ArrayList<TextPage>, textPages: ArrayList<TextPage>,
stringBuilder: StringBuilder, stringBuilder: StringBuilder,
isTitle: Boolean, isTitle: Boolean,
isVolumeTitle: Boolean,
textPaint: TextPaint, textPaint: TextPaint,
srcList: LinkedList<String>? = null srcList: LinkedList<String>? = null
): Pair<Int, Float> { ): Pair<Int, Float> {
var absStartX = x var absStartX = x
var durY = if (isTitle) y + titleTopSpacing else y
val layout = if (ReadBookConfig.useZhLayout) { val layout = if (ReadBookConfig.useZhLayout) {
ZhLayout(text, textPaint, visibleWidth) ZhLayout(text, textPaint, visibleWidth)
} else StaticLayout( } else StaticLayout(
text, textPaint, visibleWidth, Layout.Alignment.ALIGN_NORMAL, 0f, 0f, true 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) { for (lineIndex in 0 until layout.lineCount) {
val textLine = TextLine(isTitle = isTitle) val textLine = TextLine(isTitle = isTitle)
if (durY + textPaint.textHeight > visibleHeight) { if (durY + textPaint.textHeight > visibleHeight) {
@ -309,8 +317,9 @@ object ChapterProvider {
text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
val desiredWidth = layout.getLineWidth(lineIndex) val desiredWidth = layout.getLineWidth(lineIndex)
var isLastLine = false var isLastLine = false
if (lineIndex == 0 && layout.lineCount > 1 && !isTitle) { when {
//第一行 lineIndex == 0 && layout.lineCount > 1 && !isTitle -> {
//第一行 非标题
textLine.text = words textLine.text = words
addCharsToLineFirst( addCharsToLineFirst(
absStartX, absStartX,
@ -320,11 +329,13 @@ object ChapterProvider {
desiredWidth, desiredWidth,
srcList srcList
) )
} else if (lineIndex == layout.lineCount - 1) { }
lineIndex == layout.lineCount - 1 -> {
//最后一行 //最后一行
textLine.text = "$words\n" textLine.text = "$words\n"
isLastLine = true isLastLine = true
val startX = if (isTitle && ReadBookConfig.titleMode == 1) //标题居中
val startX = if (isTitle && ReadBookConfig.titleMode == 1 || isVolumeTitle)
(visibleWidth - layout.getLineWidth(lineIndex)) / 2 (visibleWidth - layout.getLineWidth(lineIndex)) / 2
else 0f else 0f
addCharsToLineLast( addCharsToLineLast(
@ -335,7 +346,8 @@ object ChapterProvider {
startX, startX,
srcList srcList
) )
} else { }
else -> {
//中间行 //中间行
textLine.text = words textLine.text = words
addCharsToLineMiddle( addCharsToLineMiddle(
@ -348,6 +360,7 @@ object ChapterProvider {
srcList srcList
) )
} }
}
stringBuilder.append(words) stringBuilder.append(words)
if (isLastLine) stringBuilder.append("\n") if (isLastLine) stringBuilder.append("\n")
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
@ -355,7 +368,7 @@ object ChapterProvider {
durY += textPaint.textHeight * lineSpacingExtra durY += textPaint.textHeight * lineSpacingExtra
textPages.last().height = durY textPages.last().height = durY
} }
if (isTitle) durY += titleBottomSpacing if (isTitle && !isVolumeTitle) durY += titleBottomSpacing
durY += textPaint.textHeight * paragraphSpacing / 10f durY += textPaint.textHeight * paragraphSpacing / 10f
return Pair(absStartX, durY) return Pair(absStartX, durY)
} }

Loading…
Cancel
Save