From 81fc3ea7e4ddf8abf4d46687339d8c807b454058 Mon Sep 17 00:00:00 2001 From: HapeLee <63206378+HapeLee@users.noreply.github.com> Date: Thu, 11 Jun 2026 20:46:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BD=BF=E7=94=A8TextPaint=E5=89=AF?= =?UTF-8?q?=E6=9C=AC=E9=81=BF=E5=85=8D=E4=BF=AE=E6=94=B9=E5=85=B1=E4=BA=AB?= =?UTF-8?q?paint=E5=BD=B1=E5=93=8D=E7=BB=98=E5=88=B6=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../read/page/provider/TextChapterLayout.kt | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) 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 d299d7774..01d87b664 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 @@ -1365,7 +1365,7 @@ class TextChapterLayout( /** * 对有自定义字体的高亮字符重新测量宽度,确保排版和绘制使用同一套字体度量。 - * 不使用 getTextWidthsCompat 避免对子串错误添加 letterSpacing。 + * 使用 textPaint 副本测量,避免修改共享 paint 的 typeface 影响绘制线程。 */ private fun remeasureWithHighlightFonts( text: String, @@ -1373,26 +1373,22 @@ class TextChapterLayout( textPaint: TextPaint, widthsArray: FloatArray ) { - val originalTypeface = textPaint.typeface - try { - var i = 0 - while (i < text.length) { - val fontPath = charStyles[i]?.fontPath.orEmpty() - if (fontPath.isEmpty()) { i++; continue } - // 找连续使用同一字体的区间 - val segStart = i - i++ - while (i < text.length && charStyles[i]?.fontPath.orEmpty() == fontPath) i++ - val segEnd = i - val typeface = TextColumn.getTypeface(fontPath) ?: continue - textPaint.typeface = typeface - val segLen = segEnd - segStart - val segWidths = FloatArray(segLen) - textPaint.getTextWidths(text, segStart, segEnd, segWidths) - segWidths.copyInto(widthsArray, segStart) - } - } finally { - textPaint.typeface = originalTypeface + val measurePaint = TextPaint(textPaint) + var i = 0 + while (i < text.length) { + val fontPath = charStyles[i]?.fontPath.orEmpty() + if (fontPath.isEmpty()) { i++; continue } + // 找连续使用同一字体的区间 + val segStart = i + i++ + while (i < text.length && charStyles[i]?.fontPath.orEmpty() == fontPath) i++ + val segEnd = i + val typeface = TextColumn.getTypeface(fontPath) ?: continue + measurePaint.typeface = typeface + val segLen = segEnd - segStart + val segWidths = FloatArray(segLen) + measurePaint.getTextWidths(text, segStart, segEnd, segWidths) + segWidths.copyInto(widthsArray, segStart) } }