This commit is contained in:
Horis
2025-02-21 12:00:13 +08:00
parent a4442cb121
commit 3d9744ed2e
2 changed files with 28 additions and 15 deletions
@@ -1,7 +1,6 @@
package io.legado.app.ui.book.read.page.provider package io.legado.app.ui.book.read.page.provider
import android.graphics.Paint import android.graphics.Paint
import android.os.Build
import android.text.Layout import android.text.Layout
import android.text.StaticLayout import android.text.StaticLayout
import android.text.TextPaint import android.text.TextPaint
@@ -25,6 +24,7 @@ import io.legado.app.ui.book.read.page.entities.column.ReviewColumn
import io.legado.app.ui.book.read.page.entities.column.TextColumn import io.legado.app.ui.book.read.page.entities.column.TextColumn
import io.legado.app.utils.dpToPx import io.legado.app.utils.dpToPx
import io.legado.app.utils.fastSum import io.legado.app.utils.fastSum
import io.legado.app.utils.getTextWidthsCompat
import io.legado.app.utils.splitNotBlank import io.legado.app.utils.splitNotBlank
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@@ -76,18 +76,21 @@ class TextChapterLayout(
private var pendingTextPage = TextPage() private var pendingTextPage = TextPage()
private var isCompleted = false
private val job: Coroutine<*>
private val bookChapter inline get() = textChapter.chapter private val bookChapter inline get() = textChapter.chapter
private val displayTitle inline get() = textChapter.title private val displayTitle inline get() = textChapter.title
private val chaptersSize inline get() = textChapter.chaptersSize private val chaptersSize inline get() = textChapter.chaptersSize
private var durY = 0f
private var absStartX = paddingLeft
private var floatArray = FloatArray(128)
private var isCompleted = false
private val job: Coroutine<*>
var exception: Throwable? = null var exception: Throwable? = null
var channel = Channel<TextPage>(Int.MAX_VALUE) var channel = Channel<TextPage>(Int.MAX_VALUE)
var durY = 0f
var absStartX = paddingLeft
init { init {
job = Coroutine.async( job = Coroutine.async(
@@ -401,16 +404,8 @@ class TextChapterLayout(
isVolumeTitle: Boolean = false, isVolumeTitle: Boolean = false,
srcList: LinkedList<String>? = null srcList: LinkedList<String>? = null
) { ) {
val widthsArray = FloatArray(text.length) val widthsArray = allocateFloatArray(text.length)
textPaint.getTextWidths(text, widthsArray) textPaint.getTextWidthsCompat(text, widthsArray)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
if (widthsArray.isNotEmpty()) {
val letterSpacing = textPaint.letterSpacing * textPaint.textSize
val letterSpacingHalf = letterSpacing * 0.5f
widthsArray[0] += letterSpacingHalf
widthsArray[widthsArray.lastIndex] += letterSpacingHalf
}
}
val layout = if (ReadBookConfig.useZhLayout) { val layout = if (ReadBookConfig.useZhLayout) {
val (words, widths) = measureTextSplit(text, widthsArray) val (words, widths) = measureTextSplit(text, widthsArray)
val indentSize = if (isFirstLine) paragraphIndent.length else 0 val indentSize = if (isFirstLine) paragraphIndent.length else 0
@@ -786,6 +781,13 @@ class TextChapterLayout(
} }
} }
private fun allocateFloatArray(size: Int): FloatArray {
if (size > floatArray.size) {
floatArray = FloatArray(size)
}
return floatArray
}
private fun measureTextSplit( private fun measureTextSplit(
text: String, text: String,
widthsArray: FloatArray, widthsArray: FloatArray,
@@ -1,6 +1,17 @@
package io.legado.app.utils package io.legado.app.utils
import android.os.Build
import android.text.TextPaint import android.text.TextPaint
val TextPaint.textHeight: Float val TextPaint.textHeight: Float
get() = fontMetrics.run { descent - ascent + leading } get() = fontMetrics.run { descent - ascent + leading }
fun TextPaint.getTextWidthsCompat(text: String, widths: FloatArray) {
getTextWidths(text, widths)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
val letterSpacing = letterSpacing * textSize
val letterSpacingHalf = letterSpacing * 0.5f
widths[0] += letterSpacingHalf
widths[text.lastIndex] += letterSpacingHalf
}
}