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

+ 8 - 6
app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt

@@ -625,16 +625,18 @@ object ChapterProvider {
         text: String,
         text: String,
         textWidths: List<Float>
         textWidths: List<Float>
     ): Pair<List<String>, List<Float>> {
     ): Pair<List<String>, List<Float>> {
-        val strList = arrayListOf<String>()
-        val textWidthList = arrayListOf<Float>()
+        val charArray = text.toCharArray()
+        val strList = ArrayList<String>()
+        val textWidthList = ArrayList<Float>()
+        val lastIndex = text.lastIndex
         for (i in textWidths.indices) {
         for (i in textWidths.indices) {
-            if (text[i].isLowSurrogate()) {
+            if (charArray[i].isLowSurrogate()) {
                 continue
                 continue
             }
             }
-            val char = if (i + 1 < text.lastIndex && text[i + 1].isLowSurrogate()) {
-                text.substring(i, i + 2)
+            val char = if (i + 1 < lastIndex && charArray[i + 1].isLowSurrogate()) {
+                charArray[i].toString() + charArray[i + 1].toString()
             } else {
             } else {
-                text.substring(i, i + 1)
+                charArray[i].toString()
             }
             }
             strList.add(char)
             strList.add(char)
             textWidthList.add(textWidths[i])
             textWidthList.add(textWidths[i])

+ 11 - 5
app/src/main/java/io/legado/app/ui/book/read/page/provider/ZhLayout.kt

@@ -1,8 +1,10 @@
 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.Rect
 import android.graphics.Rect
 import android.text.Layout
 import android.text.Layout
 import android.text.TextPaint
 import android.text.TextPaint
+import java.util.WeakHashMap
 import kotlin.math.max
 import kotlin.math.max
 
 
 /**
 /**
@@ -22,6 +24,7 @@ class ZhLayout(
             "】", ")", ">", "]", "}", ",", ".", "?", "!", ":", "」", ";", ";"
             "】", ")", ">", "]", "}", ",", ".", "?", "!", ":", "」", ";", ";"
         )
         )
         private val prePanc = hashSetOf("“", "(", "《", "【", "‘", "‘", "(", "<", "[", "{", "「")
         private val prePanc = hashSetOf("“", "(", "《", "【", "‘", "‘", "(", "<", "[", "{", "「")
+        private val cnCharWidthCache = WeakHashMap<Paint, Float>()
     }
     }
 
 
     private val defaultCapacity = 10
     private val defaultCapacity = 10
@@ -29,7 +32,10 @@ class ZhLayout(
     var lineWidth = FloatArray(defaultCapacity)
     var lineWidth = FloatArray(defaultCapacity)
     private var lineCount = 0
     private var lineCount = 0
     private val curPaint = textPaint
     private val curPaint = textPaint
-    private val cnCharWitch = getDesiredWidth("我", textPaint)
+    private val cnCharWidth = cnCharWidthCache[textPaint]
+        ?: getDesiredWidth("我", textPaint).also {
+            cnCharWidthCache[textPaint] = it
+        }
 
 
     enum class BreakMod { NORMAL, BREAK_ONE_CHAR, BREAK_MORE_CHAR, CPS_1, CPS_2, CPS_3, }
     enum class BreakMod { NORMAL, BREAK_ONE_CHAR, BREAK_MORE_CHAR, CPS_1, CPS_2, CPS_3, }
     class Locate {
     class Locate {
@@ -197,10 +203,10 @@ class ZhLayout(
     }
     }
 
 
     private fun inCompressible(width: Float): Boolean {
     private fun inCompressible(width: Float): Boolean {
-        return width < cnCharWitch
+        return width < cnCharWidth
     }
     }
 
 
-    private val gap = (cnCharWitch / 12.75).toFloat()
+    private val gap = (cnCharWidth / 12.75).toFloat()
     private fun getPostPancOffset(string: String): Float {
     private fun getPostPancOffset(string: String): Float {
         val textRect = Rect()
         val textRect = Rect()
         curPaint.getTextBounds(string, 0, 1, textRect)
         curPaint.getTextBounds(string, 0, 1, textRect)
@@ -210,8 +216,8 @@ class ZhLayout(
     private fun getPrePancOffset(string: String): Float {
     private fun getPrePancOffset(string: String): Float {
         val textRect = Rect()
         val textRect = Rect()
         curPaint.getTextBounds(string, 0, 1, textRect)
         curPaint.getTextBounds(string, 0, 1, textRect)
-        val d = max(cnCharWitch - textRect.right.toFloat() - gap, 0f)
-        return cnCharWitch / 2 - d
+        val d = max(cnCharWidth - textRect.right.toFloat() - gap, 0f)
+        return cnCharWidth / 2 - d
     }
     }
 
 
     fun getDesiredWidth(sting: String, paint: TextPaint) = paint.measureText(sting)
     fun getDesiredWidth(sting: String, paint: TextPaint) = paint.measureText(sting)