This commit is contained in:
Horis 2024-12-10 15:53:44 +08:00
parent 1c2eb8ef7d
commit 90e870e1a9
4 changed files with 31 additions and 11 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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)
}

View File

@ -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)