This commit is contained in:
Horis 2023-10-01 23:23:41 +08:00
parent 1127100c68
commit f8e555218c
2 changed files with 18 additions and 23 deletions

View File

@ -621,7 +621,7 @@ object ChapterProvider {
exceed(absStartX, textLine) exceed(absStartX, textLine)
} }
private fun getStringArrayAndTextWidths( fun getStringArrayAndTextWidths(
text: String, text: String,
textWidths: List<Float> textWidths: List<Float>
): Pair<List<String>, List<Float>> { ): Pair<List<String>, List<Float>> {

View File

@ -3,7 +3,6 @@ package io.legado.app.ui.book.read.page.provider
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 io.legado.app.utils.toStringArray
import kotlin.math.max import kotlin.math.max
/** /**
@ -22,6 +21,11 @@ class ZhLayout(
private var lineCount = 0 private var lineCount = 0
private val curPaint = textPaint private val curPaint = textPaint
private val cnCharWitch = getDesiredWidth("", textPaint) private val cnCharWitch = getDesiredWidth("", textPaint)
private val postPanc = hashSetOf(
"", "", "", "", "", "", "", "", "", "", "}",
"", ")", ">", "]", "}", ",", ".", "?", "!", ":", "", "", ";"
)
private val prePanc = hashSetOf("", "", "", "", "", "", "(", "<", "[", "{", "")
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 {
@ -36,12 +40,14 @@ class ZhLayout(
init { init {
var line = 0 var line = 0
val words = text.toStringArray() val widthsArray = FloatArray(text.length)
curPaint.getTextWidths(text as String, widthsArray)
val (words, widths) = ChapterProvider.getStringArrayAndTextWidths(text, widthsArray.asList())
var lineW = 0f var lineW = 0f
var cwPre = 0f var cwPre = 0f
var length = 0 var length = 0
words.forEachIndexed { index, s -> words.forEachIndexed { index, s ->
val cw = getDesiredWidth(s, curPaint) val cw = widths[index]
var breakMod: BreakMod var breakMod: BreakMod
var breakLine = false var breakLine = false
lineW += cw lineW += cw
@ -67,13 +73,13 @@ class ZhLayout(
var reCheck = false var reCheck = false
var breakIndex = 0 var breakIndex = 0
if (breakMod == BreakMod.CPS_1 && if (breakMod == BreakMod.CPS_1 &&
(inCompressible(words[index]) || inCompressible(words[index - 1])) (inCompressible(widths[index]) || inCompressible(widths[index - 1]))
) reCheck = true ) reCheck = true
if (breakMod == BreakMod.CPS_2 && if (breakMod == BreakMod.CPS_2 &&
(inCompressible(words[index - 1]) || inCompressible(words[index - 2])) (inCompressible(widths[index - 1]) || inCompressible(widths[index - 2]))
) reCheck = true ) reCheck = true
if (breakMod == BreakMod.CPS_3 && if (breakMod == BreakMod.CPS_3 &&
(inCompressible(words[index]) || inCompressible(words[index - 2])) (inCompressible(widths[index]) || inCompressible(widths[index - 2]))
) reCheck = true ) reCheck = true
if (breakMod > BreakMod.BREAK_MORE_CHAR if (breakMod > BreakMod.BREAK_MORE_CHAR
&& index < words.lastIndex && isPostPanc(words[index + 1]) && index < words.lastIndex && isPostPanc(words[index + 1])
@ -90,7 +96,7 @@ class ZhLayout(
} else { } else {
breakIndex++ breakIndex++
breakLength += words[i].length breakLength += words[i].length
cwPre += getDesiredWidth(words[i], textPaint) cwPre += widths[i]
} }
if (!isPostPanc(words[i]) && !isPrePanc(words[i - 1])) { if (!isPostPanc(words[i]) && !isPrePanc(words[i - 1])) {
breakMod = BreakMod.BREAK_MORE_CHAR breakMod = BreakMod.BREAK_MORE_CHAR
@ -172,26 +178,15 @@ class ZhLayout(
} }
private fun isPostPanc(string: String): Boolean { private fun isPostPanc(string: String): Boolean {
val panc = arrayOf( return postPanc.contains(string)
"", "", "", "", "", "", "", "", "", "", "}",
"", ")", ">", "]", "}", ",", ".", "?", "!", ":", "", "", ";"
)
panc.forEach {
if (it == string) return true
}
return false
} }
private fun isPrePanc(string: String): Boolean { private fun isPrePanc(string: String): Boolean {
val panc = arrayOf("", "", "", "", "", "", "(", "<", "[", "{", "") return prePanc.contains(string)
panc.forEach {
if (it == string) return true
}
return false
} }
private fun inCompressible(string: String): Boolean { private fun inCompressible(width: Float): Boolean {
return getDesiredWidth(string, curPaint) < cnCharWitch return width < cnCharWitch
} }
private val gap = (cnCharWitch / 12.75).toFloat() private val gap = (cnCharWitch / 12.75).toFloat()