Horis 1 год назад
Родитель
Сommit
3d9744ed2e

+ 17 - 15
app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt

@@ -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)
-        textPaint.getTextWidths(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 widthsArray = allocateFloatArray(text.length)
+        textPaint.getTextWidthsCompat(text, widthsArray)
         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,

+ 11 - 0
app/src/main/java/io/legado/app/utils/PaintExtensions.kt

@@ -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
+    }
+}