This commit is contained in:
Horis
2023-10-03 17:23:38 +08:00
parent f8194b2f67
commit 1fbf951754
2 changed files with 19 additions and 11 deletions
@@ -625,16 +625,18 @@ object ChapterProvider {
text: String,
textWidths: 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) {
if (text[i].isLowSurrogate()) {
if (charArray[i].isLowSurrogate()) {
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 {
text.substring(i, i + 1)
charArray[i].toString()
}
strList.add(char)
textWidthList.add(textWidths[i])
@@ -1,8 +1,10 @@
package io.legado.app.ui.book.read.page.provider
import android.graphics.Paint
import android.graphics.Rect
import android.text.Layout
import android.text.TextPaint
import java.util.WeakHashMap
import kotlin.math.max
/**
@@ -22,6 +24,7 @@ class ZhLayout(
"", ")", ">", "]", "}", ",", ".", "?", "!", ":", "", "", ";"
)
private val prePanc = hashSetOf("", "", "", "", "", "", "(", "<", "[", "{", "")
private val cnCharWidthCache = WeakHashMap<Paint, Float>()
}
private val defaultCapacity = 10
@@ -29,7 +32,10 @@ class ZhLayout(
var lineWidth = FloatArray(defaultCapacity)
private var lineCount = 0
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, }
class Locate {
@@ -197,10 +203,10 @@ class ZhLayout(
}
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 {
val textRect = Rect()
curPaint.getTextBounds(string, 0, 1, textRect)
@@ -210,8 +216,8 @@ class ZhLayout(
private fun getPrePancOffset(string: String): Float {
val textRect = Rect()
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)