master
kunfei 2 years ago
parent ceda15e90e
commit ea571837c0
  1. 9
      app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt
  2. 57
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt

@ -218,7 +218,9 @@ object ReadBookConfig {
config.paragraphSpacing = value config.paragraphSpacing = value
} }
//标题位置 0:居左 1:居中 2:隐藏 /**
* 标题位置 0:居左 1:居中 2:隐藏
*/
var titleMode: Int var titleMode: Int
get() = config.titleMode get() = config.titleMode
set(value) { set(value) {
@ -230,6 +232,11 @@ object ReadBookConfig {
config.titleSize = value config.titleSize = value
} }
/**
* 是否标题居中
*/
val isMiddleTitle get() = titleMode == 1
var titleTopSpacing: Int var titleTopSpacing: Int
get() = config.titleTopSpacing get() = config.titleTopSpacing
set(value) { set(value) {

@ -136,7 +136,7 @@ object ChapterProvider {
stringBuilder, stringBuilder,
titlePaint, titlePaint,
isTitle = true, isTitle = true,
isTitleWithNoContent = contents.isEmpty(), emptyContent = contents.isEmpty(),
isVolumeTitle = bookChapter.isVolume isVolumeTitle = bookChapter.isVolume
).let { ).let {
absStartX = it.first absStartX = it.first
@ -295,7 +295,7 @@ object ChapterProvider {
stringBuilder: StringBuilder, stringBuilder: StringBuilder,
textPaint: TextPaint, textPaint: TextPaint,
isTitle: Boolean = false, isTitle: Boolean = false,
isTitleWithNoContent: Boolean = false, emptyContent: Boolean = false,
isVolumeTitle: Boolean = false, isVolumeTitle: Boolean = false,
srcList: LinkedList<String>? = null srcList: LinkedList<String>? = null
): Pair<Int, Float> { ): Pair<Int, Float> {
@ -307,7 +307,7 @@ object ChapterProvider {
} }
var durY = when { var durY = when {
//标题y轴居中 //标题y轴居中
isTitleWithNoContent && textPages.size == 1 -> { emptyContent && textPages.size == 1 -> {
val textPage = textPages.last() val textPage = textPages.last()
if (textPage.lineSize == 0) { if (textPage.lineSize == 0) {
val ty = (visibleHeight - layout.lineCount * textPaint.textHeight) / 2 val ty = (visibleHeight - layout.lineCount * textPaint.textHeight) / 2
@ -368,9 +368,9 @@ object ChapterProvider {
textLine.text = words textLine.text = words
textLine.isParagraphEnd = true textLine.isParagraphEnd = true
//标题x轴居中 //标题x轴居中
val startX = if (isTitle && ReadBookConfig.titleMode == 1 val startX = if (
|| isTitleWithNoContent isTitle &&
|| isVolumeTitle (ReadBookConfig.isMiddleTitle || emptyContent || isVolumeTitle)
) { ) {
(visibleWidth - layout.getLineWidth(lineIndex)) / 2 (visibleWidth - layout.getLineWidth(lineIndex)) / 2
} else { } else {
@ -382,12 +382,24 @@ object ChapterProvider {
) )
} }
else -> { else -> {
//中间行 if (
textLine.text = words isTitle &&
addCharsToLineMiddle( (ReadBookConfig.isMiddleTitle || emptyContent || isVolumeTitle)
book, absStartX, textLine, words.toStringArray(), ) {
textPaint, desiredWidth, 0f, srcList //标题居中
) val startX = (visibleWidth - layout.getLineWidth(lineIndex)) / 2
addCharsToLineNatural(
book, absStartX, textLine, words.toStringArray(),
textPaint, startX, srcList
)
} else {
//中间行
textLine.text = words
addCharsToLineMiddle(
book, absStartX, textLine, words.toStringArray(),
textPaint, desiredWidth, 0f, srcList
)
}
} }
} }
stringBuilder.append(words) stringBuilder.append(words)
@ -412,6 +424,7 @@ object ChapterProvider {
textLine: TextLine, textLine: TextLine,
words: Array<String>, words: Array<String>,
textPaint: TextPaint, textPaint: TextPaint,
/**自然排版长度**/
desiredWidth: Float, desiredWidth: Float,
srcList: LinkedList<String>? srcList: LinkedList<String>?
) { ) {
@ -436,7 +449,8 @@ object ChapterProvider {
if (words.size > bodyIndent.length) { if (words.size > bodyIndent.length) {
val words1 = words.copyOfRange(bodyIndent.length, words.size) val words1 = words.copyOfRange(bodyIndent.length, words.size)
addCharsToLineMiddle( addCharsToLineMiddle(
book, absStartX, textLine, words1, textPaint, desiredWidth, x, srcList book, absStartX, textLine, words1,
textPaint, desiredWidth, x, srcList
) )
} }
} }
@ -450,7 +464,9 @@ object ChapterProvider {
textLine: TextLine, textLine: TextLine,
words: Array<String>, words: Array<String>,
textPaint: TextPaint, textPaint: TextPaint,
/**自然排版长度**/
desiredWidth: Float, desiredWidth: Float,
/**起始x坐标**/
startX: Float, startX: Float,
srcList: LinkedList<String>? srcList: LinkedList<String>?
) { ) {
@ -458,9 +474,10 @@ object ChapterProvider {
addCharsToLineNatural(book, absStartX, textLine, words, textPaint, startX, srcList) addCharsToLineNatural(book, absStartX, textLine, words, textPaint, startX, srcList)
return return
} }
val residualWidth = visibleWidth - desiredWidth
val spaceSize = words.filter { it == " " }.size val spaceSize = words.filter { it == " " }.size
if (spaceSize > 0) { if (spaceSize > 0) {
val d = (visibleWidth - desiredWidth) / spaceSize val d = residualWidth / spaceSize
var x = startX var x = startX
words.forEachIndexed { index, char -> words.forEachIndexed { index, char ->
val cw = StaticLayout.getDesiredWidth(char, textPaint) val cw = StaticLayout.getDesiredWidth(char, textPaint)
@ -469,17 +486,23 @@ object ChapterProvider {
} else { } else {
(x + cw) (x + cw)
} }
addCharToLine(book, absStartX, textLine, char, x, x1, index + 1 == words.size, srcList) addCharToLine(
book, absStartX, textLine, char,
x, x1, index + 1 == words.size, srcList
)
x = x1 x = x1
} }
} else { } else {
val gapCount: Int = words.lastIndex val gapCount: Int = words.lastIndex
val d = (visibleWidth - desiredWidth) / gapCount val d = residualWidth / gapCount
var x = startX var x = startX
words.forEachIndexed { index, char -> words.forEachIndexed { index, char ->
val cw = StaticLayout.getDesiredWidth(char, textPaint) val cw = StaticLayout.getDesiredWidth(char, textPaint)
val x1 = if (index != words.lastIndex) (x + cw + d) else (x + cw) val x1 = if (index != words.lastIndex) (x + cw + d) else (x + cw)
addCharToLine(book, absStartX, textLine, char, x, x1, index + 1 == words.size, srcList) addCharToLine(
book, absStartX, textLine, char,
x, x1, index + 1 == words.size, srcList
)
x = x1 x = x1
} }
} }

Loading…
Cancel
Save