From 3e83d5076277ed0559789aba5582a87bf0b943d4 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 9 Dec 2020 17:51:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=89=E6=8B=A9=E6=96=87=E6=9C=AC=E6=97=B6?= =?UTF-8?q?=E4=BC=98=E5=85=88=E9=80=89=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/book/read/page/ReadView.kt | 114 ++++++++++++++---- 1 file changed, 93 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt index 292d283dd..c2de83697 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt @@ -86,14 +86,9 @@ class ReadView(context: Context, attrs: AttributeSet) : private val blRect = RectF(10F, height * 0.66f, width * 0.33f, height - 10f) private val bcRect = RectF(width * 0.33f, height * 0.66f, width * 0.66f, height - 10f) private val brRect = RectF(width * 0.66f, height * 0.66f, width - 10f, height - 10f) - private val autoPageRect by lazy { - Rect() - } - private val autoPagePint by lazy { - Paint().apply { - color = context.accentColor - } - } + private val autoPageRect by lazy { Rect() } + private val autoPagePint by lazy { Paint().apply { color = context.accentColor } } + private val boundary by lazy { BreakIterator.getWordInstance(Locale.getDefault()) } init { addView(nextPage) @@ -247,27 +242,104 @@ class ReadView(context: Context, attrs: AttributeSet) : /** * 长按选择 */ - private fun onLongPress() { + private fun onLongPress() = with(curPage.textPage) { curPage.selectText(startX, startY) { relativePage, lineIndex, charIndex -> isTextSelected = true firstRelativePage = relativePage firstLineIndex = lineIndex firstCharIndex = charIndex - val boundary = BreakIterator.getWordInstance(Locale.getDefault()) - val lineText = curPage.textPage.textLines[lineIndex].text - boundary.setText(lineText) - var start = boundary.first() - var end = boundary.next() - while (end != BreakIterator.DONE) { - if (charIndex in start until end) { - firstCharIndex = start - break + var lineStart = lineIndex + var lineEnd = lineIndex + var start: Int + var end: Int + if (lineIndex - 1 > 0 && lineIndex + 1 < lineSize) { + // 中间行 + val lineText = with(textLines) { + get(lineIndex - 1).text + get(lineIndex).text + get(lineIndex + 1).text + } + boundary.setText(lineText) + start = boundary.first() + end = boundary.next() + val cIndex = textLines[lineIndex - 1].text.length + charIndex + while (end != BreakIterator.DONE) { + if (cIndex in start until end) { + break + } + start = end + end = boundary.next() + } + if (start < textLines[lineIndex - 1].text.length) { + lineStart = lineIndex - 1 + } else { + start -= textLines[lineIndex - 1].text.length + } + if (end > textLines[lineIndex - 1].text.length + textLines[lineIndex].text.length) { + lineEnd = lineIndex + 1 + end = (end - textLines[lineIndex - 1].text.length + - textLines[lineIndex].text.length) + } else { + end = end - textLines[lineIndex - 1].text.length - 1 + } + } else if (lineIndex - 1 > 0) { + // 尾行 + val lineText = with(textLines) { + get(lineIndex - 1).text + get(lineIndex).text + } + boundary.setText(lineText) + start = boundary.first() + end = boundary.next() + val cIndex = textLines[lineIndex - 1].text.length + charIndex + while (end != BreakIterator.DONE) { + if (cIndex in start until end) { + break + } + start = end + end = boundary.next() + } + if (start < textLines[lineIndex - 1].text.length) { + lineStart = lineIndex - 1 + } else { + start -= textLines[lineIndex - 1].text.length + } + end = end - textLines[lineIndex - 1].text.length - 1 + } else if (lineIndex + 1 < lineSize) { + // 首行 + val lineText = with(textLines) { + get(lineIndex).text + get(lineIndex + 1).text + } + boundary.setText(lineText) + start = boundary.first() + end = boundary.next() + while (end != BreakIterator.DONE) { + if (charIndex in start until end) { + break + } + start = end + end = boundary.next() + } + if (end > textLines[lineIndex].text.length) { + lineEnd = lineIndex + 1 + end -= textLines[lineIndex].text.length + } else { + end -= 1 } - start = end + } else { + // 单行 + val lineText = textLines[lineIndex].text + boundary.setText(lineText) + start = boundary.first() end = boundary.next() + while (end != BreakIterator.DONE) { + if (charIndex in start until end) { + break + } + start = end + end = boundary.next() + } + end -= 1 } - curPage.selectStartMoveIndex(firstRelativePage, firstLineIndex, firstCharIndex) - curPage.selectEndMoveIndex(firstRelativePage, firstLineIndex, end - 1) + curPage.selectStartMoveIndex(firstRelativePage, lineStart, start) + curPage.selectEndMoveIndex(firstRelativePage, lineEnd, end) } }