From 90e870e1a939cec9640644bd74fe9123a78b1bf0 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Tue, 10 Dec 2024 15:53:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/read/page/entities/TextLine.kt | 13 +++++-------- .../app/ui/book/read/page/entities/TextPage.kt | 10 ++++++++-- .../ui/book/read/page/entities/column/TextColumn.kt | 10 +++++++++- .../ui/book/read/page/provider/TextChapterLayout.kt | 9 +++++++++ 4 files changed, 31 insertions(+), 11 deletions(-) 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 0d80f144d..806e8f50b 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 @@ -5,7 +5,6 @@ import android.graphics.Canvas import android.graphics.Paint.FontMetrics import android.os.Build import androidx.annotation.Keep -import androidx.core.graphics.withTranslation import io.legado.app.help.PaintPool import io.legado.app.help.book.isImage import io.legado.app.help.config.AppConfig @@ -183,19 +182,16 @@ data class TextLine( } val paint = PaintPool.obtain() paint.set(textPaint) + val letterSpacing = paint.letterSpacing * paint.textSize + val letterSpacingHalf = letterSpacing * 0.5f if (extraLetterSpacing != 0f) { paint.letterSpacing += extraLetterSpacing } if (wordSpacing != 0f) { paint.wordSpacing = wordSpacing } - if (extraLetterSpacingOffsetX != 0f) { - canvas.withTranslation(extraLetterSpacingOffsetX) { - canvas.drawText(text, indentSize, text.length, startX, lineBase - lineTop, paint) - } - } else { - canvas.drawText(text, indentSize, text.length, startX, lineBase - lineTop, paint) - } + val offsetX = if (atLeastApi35) letterSpacingHalf else extraLetterSpacingOffsetX + canvas.drawText(text, indentSize, text.length, startX + offsetX, lineBase - lineTop, paint) PaintPool.recycle(paint) for (i in columns.indices) { val column = columns[i] as TextColumn @@ -246,6 +242,7 @@ data class TextLine( companion object { val emptyTextLine = TextLine() private val atLeastApi26 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O + private val atLeastApi35 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM private val wordSpacingWorking by lazy { // issue 3785 3846 val paint = PaintPool.obtain() diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index dd54316b7..a802da9ba 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -2,6 +2,7 @@ package io.legado.app.ui.book.read.page.entities import android.graphics.Canvas import android.graphics.Paint +import android.os.Build import android.text.Layout import android.text.StaticLayout import androidx.annotation.Keep @@ -142,10 +143,12 @@ data class TextPage( if (isMsgPage && ChapterProvider.viewWidth > 0) { textLines.clear() val visibleWidth = ChapterProvider.visibleRight - ChapterProvider.paddingLeft + val paint = ChapterProvider.contentPaint val layout = StaticLayout( - text, ChapterProvider.contentPaint, visibleWidth, + text, paint, visibleWidth, Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false ) + val letterSpacing = paint.letterSpacing * paint.textSize var y = (ChapterProvider.visibleHeight - layout.height) / 2f if (y < 0) y = 0f for (lineIndex in 0 until layout.lineCount) { @@ -161,7 +164,10 @@ data class TextPage( text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) for (i in textLine.text.indices) { val char = textLine.text[i].toString() - val cw = StaticLayout.getDesiredWidth(char, ChapterProvider.contentPaint) + var cw = StaticLayout.getDesiredWidth(char, paint) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { + cw += letterSpacing + } val x1 = x + cw textLine.addColumn( TextColumn(start = x, end = x1, char) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/column/TextColumn.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/column/TextColumn.kt index 2f4ff82c5..4097ae30e 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/column/TextColumn.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/column/TextColumn.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.book.read.page.entities.column import android.graphics.Canvas +import android.os.Build import androidx.annotation.Keep import io.legado.app.help.config.ReadBookConfig import io.legado.app.lib.theme.ThemeStore @@ -55,7 +56,14 @@ data class TextColumn( if (textPaint.color != textColor) { textPaint.color = textColor } - canvas.drawText(charData, start, textLine.lineBase - textLine.lineTop, textPaint) + val y = textLine.lineBase - textLine.lineTop + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { + val letterSpacing = textPaint.letterSpacing * textPaint.textSize + val letterSpacingHalf = letterSpacing * 0.5f + canvas.drawText(charData, start + letterSpacingHalf, y, textPaint) + } else { + canvas.drawText(charData, start, y, textPaint) + } if (selected) { canvas.drawRect(start, 0f, end, textLine.height, view.selectedPaint) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt index b01201611..6c1e26ded 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.book.read.page.provider import android.graphics.Paint +import android.os.Build import android.text.Layout import android.text.StaticLayout import android.text.TextPaint @@ -416,6 +417,14 @@ class TextChapterLayout( var absStartX = x val widthsArray = FloatArray(text.length) textPaint.getTextWidths(text, widthsArray) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { + if (widthsArray.isNotEmpty()) { + val letterSpacing = textPaint.letterSpacing * textPaint.textSize + val letterSpacingHalf = letterSpacing * 0.5f + widthsArray[0] += letterSpacingHalf + widthsArray[widthsArray.lastIndex] += letterSpacingHalf + } + } val layout = if (ReadBookConfig.useZhLayout) { val (words, widths) = measureTextSplit(text, widthsArray) ZhLayout(text, textPaint, visibleWidth, words, widths)