feat: 优化代码

pull/106/head
kunfei 5 years ago
parent d4fa3f1915
commit c255d6c6f1
  1. 141
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt

@ -20,6 +20,7 @@ import io.legado.app.utils.getPrefString
import io.legado.app.utils.removePref
@Suppress("DEPRECATION")
object ChapterProvider {
var viewWidth = 0
var viewHeight = 0
@ -31,6 +32,7 @@ object ChapterProvider {
private var paragraphSpacing = 0
var titlePaint = TextPaint()
var contentPaint = TextPaint()
private var bodyIndent = BookHelp.bodyIndent
init {
upStyle(ReadBookConfig.durConfig)
@ -64,6 +66,9 @@ object ChapterProvider {
paragraphSpacing = config.paragraphSpacing.dp
titlePaint.textSize = (config.textSize + 2).dp.toFloat()
contentPaint.textSize = config.textSize.dp.toFloat()
bodyIndent = BookHelp.bodyIndent
upSize(config)
}
@ -74,14 +79,12 @@ object ChapterProvider {
visibleHeight = viewHeight - paddingTop - config.paddingBottom.dp
}
@Suppress("DEPRECATION")
fun getTextChapter(
bookChapter: BookChapter,
content: String,
chapterSize: Int,
isHtml: Boolean = false
): TextChapter {
val bodyIndent = BookHelp.bodyIndent
val textPages = arrayListOf<TextPage>()
val pageLines = arrayListOf<Int>()
val pageLengths = arrayListOf<Int>()
@ -95,9 +98,45 @@ object ChapterProvider {
if (end > 0) {
val title = surplusText.substring(0, end)
surplusText = surplusText.substring(end + 1)
durY = joinTitle(title, durY, textPages)
}
} else {
//正文
val end = surplusText.indexOf("\n")
val text: String
if (end >= 0) {
text = surplusText.substring(0, end)
surplusText = surplusText.substring(end + 1)
} else {
text = surplusText
surplusText = ""
}
durY = joinBody(text, durY, textPages)
}
}
for ((index, item) in textPages.withIndex()) {
item.index = index
item.pageSize = textPages.size
item.chapterIndex = bookChapter.index
item.chapterSize = chapterSize
item.title = bookChapter.title
}
return TextChapter(
bookChapter.index,
bookChapter.title,
bookChapter.url,
textPages,
pageLines,
pageLengths,
chapterSize
)
}
private fun joinTitle(title: String, y: Int, textPages: ArrayList<TextPage>): Int {
var durY = y
val layout = StaticLayout(
title, titlePaint, visibleWidth,
Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, false
Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, true
)
for (lineIndex in 0 until layout.lineCount) {
durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex)
@ -109,8 +148,12 @@ object ChapterProvider {
textPages.add(TextPage())
textPages.last().textLines.add(textLine)
}
textLine.lineBottom = layout.getLineBottom(lineIndex)
textLine.lineTop = layout.getLineTop(lineIndex)
textLine.lineBottom =
paddingTop + durY - layout.getLineBottom(lineIndex) + layout.getLineBaseline(
lineIndex
)
textLine.lineTop =
paddingTop + durY - layout.getLineBottom(lineIndex) + layout.getLineTop(lineIndex)
val words = title.substring(
layout.getLineStart(lineIndex),
layout.getLineEnd(lineIndex)
@ -130,11 +173,8 @@ object ChapterProvider {
}
val textChar = TextChar(
charData = char,
leftBottomPosition = Point(paddingLeft + x, paddingTop + durY),
rightTopPosition = Point(
paddingLeft + x1,
paddingTop + durY - (textLine.lineBottom - textLine.lineTop)
)
leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom),
rightTopPosition = Point(paddingLeft + x1, textLine.lineTop)
)
textLine.textChars.add(textChar)
x = x1
@ -148,11 +188,8 @@ object ChapterProvider {
val x1 = (x + cw).toInt()
val textChar = TextChar(
charData = char,
leftBottomPosition = Point(paddingLeft + x, paddingTop + durY),
rightTopPosition = Point(
paddingLeft + x1,
paddingTop + durY - (textLine.lineBottom - textLine.lineTop)
)
leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom),
rightTopPosition = Point(paddingLeft + x1, textLine.lineTop)
)
textLine.textChars.add(textChar)
x = x1
@ -160,21 +197,14 @@ object ChapterProvider {
}
}
durY += paragraphSpacing
return durY
}
} else {
//正文
val end = surplusText.indexOf("\n")
val text: String
if (end >= 0) {
text = surplusText.substring(0, end)
surplusText = surplusText.substring(end + 1)
} else {
text = surplusText
surplusText = ""
}
private fun joinBody(text: String, y: Int, textPages: ArrayList<TextPage>): Int {
var durY = y
val layout = StaticLayout(
text, contentPaint, visibleWidth,
Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, false
Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, true
)
for (lineIndex in 0 until layout.lineCount) {
val textLine = TextLine(isTitle = false)
@ -186,8 +216,14 @@ object ChapterProvider {
textPages.add(TextPage())
textPages.last().textLines.add(textLine)
}
textLine.lineBottom = layout.getLineBottom(lineIndex)
textLine.lineTop = layout.getLineTop(lineIndex)
textLine.lineBottom =
paddingTop + durY - layout.getLineBottom(lineIndex) + layout.getLineBaseline(
lineIndex
)
textLine.lineTop =
paddingTop + durY - layout.getLineBottom(lineIndex) + layout.getLineTop(
lineIndex
)
var words =
text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
val desiredWidth = layout.getLineMax(lineIndex)
@ -198,11 +234,8 @@ object ChapterProvider {
var x1 = (x + icw).toInt()
val textChar = TextChar(
charData = bodyIndent,
leftBottomPosition = Point(paddingLeft + x, paddingTop + durY),
rightTopPosition = Point(
paddingLeft + x1,
paddingTop + durY - (textLine.lineBottom - textLine.lineTop)
)
leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom),
rightTopPosition = Point(paddingLeft + x1, textLine.lineTop)
)
textLine.textChars.add(textChar)
x = x1
@ -219,11 +252,8 @@ object ChapterProvider {
}
val textChar1 = TextChar(
charData = char,
leftBottomPosition = Point(paddingLeft + x, paddingTop + durY),
rightTopPosition = Point(
paddingLeft + x1,
paddingTop + durY - (textLine.lineBottom - textLine.lineTop)
)
leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom),
rightTopPosition = Point(paddingLeft + x1, textLine.lineTop)
)
textLine.textChars.add(textChar1)
x = x1
@ -237,11 +267,8 @@ object ChapterProvider {
val x1 = (x + cw).toInt()
val textChar = TextChar(
charData = char,
leftBottomPosition = Point(paddingLeft + x, paddingTop + durY),
rightTopPosition = Point(
paddingLeft + x1,
paddingTop + durY - (textLine.lineBottom - textLine.lineTop)
)
leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom),
rightTopPosition = Point(paddingLeft + x1, textLine.lineTop)
)
textLine.textChars.add(textChar)
x = x1
@ -261,11 +288,8 @@ object ChapterProvider {
}
val textChar = TextChar(
charData = char,
leftBottomPosition = Point(paddingLeft + x, paddingTop + durY),
rightTopPosition = Point(
paddingLeft + x1,
paddingTop + durY - (textLine.lineBottom - textLine.lineTop)
)
leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom),
rightTopPosition = Point(paddingLeft + x1, textLine.lineTop)
)
textLine.textChars.add(textChar)
x = x1
@ -273,23 +297,6 @@ object ChapterProvider {
}
}
durY += paragraphSpacing
}
}
for ((index, item) in textPages.withIndex()) {
item.index = index
item.pageSize = textPages.size
item.chapterIndex = bookChapter.index
item.chapterSize = chapterSize
item.title = bookChapter.title
}
return TextChapter(
bookChapter.index,
bookChapter.title,
bookChapter.url,
textPages,
pageLines,
pageLengths,
chapterSize
)
return durY
}
}
Loading…
Cancel
Save