fix: 高亮规则排版

This commit is contained in:
HapeLee committed 2026-06-11 13:34:21 +08:00
1 parent 541de1958a
commit fc408c42dd
3 files changed
+10 -3

No files matched your search

@@ -2774,6 +2774,7 @@ class ReadBookViewModel(
} }
} }
is ConfigUpdate.DoubleHorizontalPage -> { is ConfigUpdate.DoubleHorizontalPage -> {
ReadConfig.doubleHorizontalPage = update.value
viewModelScope.launch { viewModelScope.launch {
readSettingsRepository.setDoubleHorizontalPage(update.value) readSettingsRepository.setDoubleHorizontalPage(update.value)
} }
@@ -75,12 +75,14 @@ data class TextColumn(
val needRestoreTypeface = customTypeface != null val needRestoreTypeface = customTypeface != null
if (needRestoreSize) { if (needRestoreSize) {
val originalSize = textPaint.textSize val originalSize = textPaint.textSize
val originalTypeface = if (needRestoreTypeface) textPaint.typeface else null
textPaint.textSize = textLine.titleTextSize!! textPaint.textSize = textLine.titleTextSize!!
if (needRestoreColor) textPaint.color = drawColor if (needRestoreColor) textPaint.color = drawColor
if (needRestoreTypeface) textPaint.typeface = customTypeface if (needRestoreTypeface) textPaint.typeface = customTypeface
val y = textLine.lineBase - textLine.lineTop val y = textLine.lineBase - textLine.lineTop
drawText(canvas, y, textPaint) drawText(canvas, y, textPaint)
textPaint.textSize = originalSize textPaint.textSize = originalSize
if (needRestoreTypeface) textPaint.typeface = originalTypeface
} else if (needRestoreColor || needRestoreTypeface) { } else if (needRestoreColor || needRestoreTypeface) {
val originalColor = textPaint.color val originalColor = textPaint.color
val originalTypeface = textPaint.typeface val originalTypeface = textPaint.typeface
@@ -1365,6 +1365,7 @@ class TextChapterLayout(
/** /**
* 对有自定义字体的高亮字符重新测量宽度,确保排版和绘制使用同一套字体度量。 * 对有自定义字体的高亮字符重新测量宽度,确保排版和绘制使用同一套字体度量。
* 不使用 getTextWidthsCompat 避免对子串错误添加 letterSpacing。
*/ */
private fun remeasureWithHighlightFonts( private fun remeasureWithHighlightFonts(
text: String, text: String,
@@ -1373,6 +1374,7 @@ class TextChapterLayout(
widthsArray: FloatArray widthsArray: FloatArray
) { ) {
val originalTypeface = textPaint.typeface val originalTypeface = textPaint.typeface
try {
var i = 0 var i = 0
while (i < text.length) { while (i < text.length) {
val fontPath = charStyles[i]?.fontPath.orEmpty() val fontPath = charStyles[i]?.fontPath.orEmpty()
@@ -1384,13 +1386,15 @@ class TextChapterLayout(
val segEnd = i val segEnd = i
val typeface = TextColumn.getTypeface(fontPath) ?: continue val typeface = TextColumn.getTypeface(fontPath) ?: continue
textPaint.typeface = typeface textPaint.typeface = typeface
val segment = text.substring(segStart, segEnd) val segLen = segEnd - segStart
val segWidths = FloatArray(segEnd - segStart) val segWidths = FloatArray(segLen)
textPaint.getTextWidthsCompat(segment, segWidths) textPaint.getTextWidths(text, segStart, segEnd, segWidths)
segWidths.copyInto(widthsArray, segStart) segWidths.copyInto(widthsArray, segStart)
} }
} finally {
textPaint.typeface = originalTypeface textPaint.typeface = originalTypeface
} }
}
/** /**
* 对文本应用高亮规则,返回每字符的样式数组。无匹配时返回 null。 * 对文本应用高亮规则,返回每字符的样式数组。无匹配时返回 null。