Просмотр исходного кода

fix: Epub 文件设置高亮规则会错位

HapeLee 4 месяцев назад
Родитель
Сommit
4f73301b80

+ 15 - 10
app/src/main/java/io/legado/app/ui/book/read/ReadBookMenuBar.kt

@@ -589,14 +589,7 @@ private fun ReadBookMenuSurface(
                 }
             },
         shape = surfaceShape,
-        color = if (useLiquidGlass || useHaze) {
-            if (bottomBarProgressiveBlur) {
-                // Stable opaque background prevents flash during Haze recomposition
-                colors.background.copy(alpha = state.menuConfig.readMenuBlurAlpha.coerceIn(0, 100) / 100f)
-            } else {
-                Color.Transparent
-            }
-        } else {
+        color = if (useLiquidGlass || useHaze) Color.Transparent else {
             val baseAlpha = state.menuConfig.readMenuBlurAlpha.coerceIn(0, 100) / 100f
             val effectiveAlpha = if (expanded) baseAlpha.coerceAtLeast(0.85f) else baseAlpha
             colors.background.copy(alpha = effectiveAlpha)
@@ -3095,11 +3088,21 @@ private fun Modifier.readMenuBottomBarHazeEffect(
     blurRadiusDp: Int? = null,
 ): Modifier {
     val surfaceAlpha = menuConfig.readMenuBlurAlpha.coerceIn(0, 100) / 100f
+    val resolvedBlurTintColor = menuConfig.readMenuBlurColorNight
+        .takeIf { it != 0 && ReadStyleResolver.isNightTheme() }
+        ?.let { Color(it) }
+        ?: menuConfig.readMenuBlurColor
+            .takeIf { it != 0 && !ReadStyleResolver.isNightTheme() }
+            ?.let { Color(it) }
+            ?: menuConfig.readMenuBlurColor
+                .takeIf { it != 0 }
+                ?.let { Color(it) }
     val backgroundModifier = if (progressive) {
         Modifier.background(
             readMenuBottomBarSurfaceBrush(
                 colors = colors,
                 alpha = surfaceAlpha,
+                blurTintColor = resolvedBlurTintColor,
             )
         )
     } else {
@@ -3183,11 +3186,13 @@ private fun readMenuTopBarSurfaceBrush(
 private fun readMenuBottomBarSurfaceBrush(
     colors: ReadMenuColors,
     alpha: Float,
+    blurTintColor: Color? = null,
 ): Brush {
-    val strongColor = colors.background.copy(
+    val baseColor = blurTintColor ?: colors.background
+    val strongColor = baseColor.copy(
         alpha = alpha.coerceIn(0f, 1f),
     )
-    val weakColor = colors.background.copy(
+    val weakColor = baseColor.copy(
         alpha = (alpha * 0.72f).coerceIn(0f, 1f),
     )
     return Brush.verticalGradient(

+ 23 - 10
app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt

@@ -356,16 +356,19 @@ class TextChapterLayout(
         val sb = StringBuffer()
         var isSetTypedImage = false
         var wordCount = 0
-        contents.forEach { content ->
+        contents.forEachIndexed { contentIndex, content ->
             currentCoroutineContext().ensureActive()
+            highlightStyleContext?.bodyContentOffsets?.getOrNull(contentIndex)?.let {
+                bodyHighlightOffset = it
+            }
             if (adaptSpecialStyle) {
                 val text = content.trim()
                 if (text == "[newpage]") {
                     prepareNextPageIfNeed()
-                    return@forEach
+                    return@forEachIndexed
                 } else if (text.startsWith("<usehtml>")) {
                     setTypeHtml(imageStyle, book, text.substring(9, text.lastIndexOf("<")))
-                    return@forEach
+                    return@forEachIndexed
                 }
             }
             var text = content.replace(srcReplaceCharC, srcReplaceCharD)
@@ -1421,13 +1424,14 @@ class TextChapterLayout(
             isTitle = true,
             rules = rules
         )
+        val bodyHighlightText = buildBodyHighlightText(contents)
         val bodyStyles = createHighlightStyles(
-            text = buildBodyHighlightText(contents),
+            text = bodyHighlightText.text,
             isTitle = false,
             rules = rules
         )
         if (titleStyles == null && bodyStyles == null) return null
-        return HighlightStyleContext(titleStyles, bodyStyles)
+        return HighlightStyleContext(titleStyles, bodyStyles, bodyHighlightText.contentOffsets)
     }
 
     private fun buildTitleHighlightText(titleSegments: List<TitleSegment>?): String {
@@ -1440,14 +1444,16 @@ class TextChapterLayout(
         }
     }
 
-    private fun buildBodyHighlightText(contents: List<String>): String {
-        if (contents.isEmpty()) return ""
-        return buildString {
-            contents.forEach { content ->
+    private fun buildBodyHighlightText(contents: List<String>): BodyHighlightText {
+        if (contents.isEmpty()) return BodyHighlightText("", IntArray(0))
+        val contentOffsets = IntArray(contents.size)
+        val text = buildString {
+            contents.forEachIndexed { index, content ->
+                contentOffsets[index] = length
                 if (adaptSpecialStyle) {
                     val text = content.trim()
                     if (text == "[newpage]" || text.startsWith("<usehtml>")) {
-                        return@forEach
+                        return@forEachIndexed
                     }
                 }
                 append(
@@ -1457,6 +1463,7 @@ class TextChapterLayout(
                 append('\n')
             }
         }
+        return BodyHighlightText(text, contentOffsets)
     }
 
     private fun String.replaceImagesForHighlight(): String {
@@ -1521,6 +1528,7 @@ class TextChapterLayout(
     private data class HighlightStyleContext(
         val titleStyles: Array<CharStyle?>?,
         val bodyStyles: Array<CharStyle?>?,
+        val bodyContentOffsets: IntArray,
     ) {
         fun stylesFor(isTitle: Boolean, offset: Int, length: Int): Array<CharStyle?>? {
             if (length <= 0) return null
@@ -1546,6 +1554,11 @@ class TextChapterLayout(
         }
     }
 
+    private data class BodyHighlightText(
+        val text: String,
+        val contentOffsets: IntArray,
+    )
+
     private data class CompiledHighlightRule(
         val rule: HighlightRule,
         val regex: Regex,