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