Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Oct 3, 2023
1 parent bf0e45f commit c9c0e6c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
Original file line number Diff line number Diff line change
@@ -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

/**
Expand All @@ -22,14 +24,18 @@ class ZhLayout(
"", ")", ">", "]", "}", ",", ".", "?", "!", ":", "", "", ";"
)
private val prePanc = hashSetOf("", "", "", "", "", "", "(", "<", "[", "{", "")
private val cnCharWidthCache = WeakHashMap<Paint, Float>()
}

private val defaultCapacity = 10
var lineStart = IntArray(defaultCapacity)
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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit c9c0e6c

Please sign in to comment.