From 8cce205cbbb4c93505ba5f8e1bb5d92b300bd292 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 6 Mar 2020 10:31:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/book/read/page/ChapterProvider.kt | 12 +++++++----- .../app/ui/book/read/page/entities/TextLine.kt | 10 ++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt index 72d648145..36dc4b5f6 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt @@ -104,11 +104,7 @@ object ChapterProvider { Layout.Alignment.ALIGN_NORMAL, 0f, 0f, true ) for (lineIndex in 0 until layout.lineCount) { - textPages.last().height = durY val textLine = TextLine(isTitle = isTitle) - textLine.lineBottom = paddingTop + durY + textPaint.textHeight - textLine.lineBase = textLine.lineBottom - textPaint.fontMetrics.descent - textLine.lineTop = paddingTop + durY val words = text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) stringBuilder.append(words) @@ -128,17 +124,23 @@ object ChapterProvider { addCharsToLineMiddle(textLine, words, textPaint, desiredWidth, 0f) } if (durY + textPaint.textHeight < visibleHeight) { + textLine.upTopBottom(durY, textPaint) textPages.last().textLines.add(textLine) durY += textPaint.textHeight * lineSpacingExtra / 10f + textPages.last().height = durY } else { textPages.last().text = stringBuilder.toString() stringBuilder.clear() pageLines.add(textPages.last().textLines.size) pageLengths.add(textPages.last().text.length) + textPages.last().height = durY //新建页面 + durY = 0f + textLine.upTopBottom(durY, textPaint) durY = textPaint.textHeight * lineSpacingExtra / 10f textPages.add(TextPage()) textPages.last().textLines.add(textLine) + textPages.last().height = durY } } durY += textPaint.textHeight * paragraphSpacing / 10f @@ -290,7 +292,7 @@ object ChapterProvider { visibleBottom = paddingTop + visibleHeight } - private val TextPaint.textHeight: Float + val TextPaint.textHeight: Float get() { return this.fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt index a9c84fb68..c8a9c9ca0 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt @@ -1,5 +1,9 @@ package io.legado.app.ui.book.read.page.entities +import android.text.TextPaint +import io.legado.app.ui.book.read.page.ChapterProvider +import io.legado.app.ui.book.read.page.ChapterProvider.textHeight + data class TextLine( var text: String = "", val textChars: ArrayList = arrayListOf(), @@ -10,6 +14,12 @@ data class TextLine( var isReadAloud: Boolean = false ) { + fun upTopBottom(durY: Float, textPaint: TextPaint) { + lineTop = ChapterProvider.paddingTop + durY + lineBottom = lineTop + textPaint.textHeight + lineBase = lineBottom - textPaint.fontMetrics.descent + } + fun addTextChar(charData: String, start: Float, end: Float) { textChars.add(TextChar(charData, start = start, end = end)) }