添加图片样式TEXT

pull/885/head^2
gedoor 4 years ago
parent 29b5806d91
commit 0596ddafa8
  1. 130
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
  2. 2
      app/src/main/java/io/legado/app/utils/StringExtensions.kt

@ -69,6 +69,8 @@ object ChapterProvider {
@JvmStatic @JvmStatic
lateinit var contentPaint: TextPaint lateinit var contentPaint: TextPaint
private const val srcReplaceChar = "🖼"
init { init {
upStyle() upStyle()
} }
@ -86,23 +88,68 @@ object ChapterProvider {
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
var durY = 0f var durY = 0f
textPages.add(TextPage()) textPages.add(TextPage())
contents.forEachIndexed { index, text -> contents.forEachIndexed { index, content ->
val matcher = AppPattern.imgPattern.matcher(text) if (book.getImageStyle() == Book.imgStyleText) {
if (matcher.find()) { var text = content.replace(srcReplaceChar, "")
matcher.group(1)?.let { val srcList = LinkedList<String>()
if (!book.isEpub()) { val sb = StringBuffer()
val src = NetworkUtils.getAbsoluteURL(bookChapter.url, it) val matcher = AppPattern.imgPattern.matcher(text)
durY = setTypeImage( while (matcher.find()) {
book, bookChapter, src, durY, textPages, book.getImageStyle() matcher.group(1)?.let {
) srcList.add(it)
matcher.appendReplacement(sb, srcReplaceChar)
} }
} }
} else { matcher.appendTail(sb)
text = sb.toString()
val isTitle = index == 0 val isTitle = index == 0
val textPaint = if (isTitle) titlePaint else contentPaint val textPaint = if (isTitle) titlePaint else contentPaint
if (!(isTitle && ReadBookConfig.titleMode == 2)) { if (!(isTitle && ReadBookConfig.titleMode == 2)) {
durY = setTypeText(text, durY, textPages, stringBuilder, isTitle, textPaint) durY = setTypeText(
text,
durY,
textPages,
stringBuilder,
isTitle,
textPaint,
srcList
)
} }
} else if (book.getImageStyle() != Book.imgStyleText) {
content.replace(AppPattern.imgPattern.toRegex(), "\n\$0\n")
.split("\n").forEach { text ->
if (text.isNotBlank()) {
val matcher = AppPattern.imgPattern.matcher(text)
if (matcher.find()) {
matcher.group(1)?.let { mt ->
if (!book.isEpub()) {
val src = NetworkUtils.getAbsoluteURL(bookChapter.url, mt)
durY = setTypeImage(
book,
bookChapter,
src,
durY,
textPages,
book.getImageStyle()
)
}
}
} else {
val isTitle = index == 0
val textPaint = if (isTitle) titlePaint else contentPaint
if (!(isTitle && ReadBookConfig.titleMode == 2)) {
durY = setTypeText(
text,
durY,
textPages,
stringBuilder,
isTitle,
textPaint
)
}
}
}
}
} }
} }
textPages.last().height = durY + 20.dp textPages.last().height = durY + 20.dp
@ -199,7 +246,8 @@ object ChapterProvider {
textPages: ArrayList<TextPage>, textPages: ArrayList<TextPage>,
stringBuilder: StringBuilder, stringBuilder: StringBuilder,
isTitle: Boolean, isTitle: Boolean,
textPaint: TextPaint textPaint: TextPaint,
srcList: LinkedList<String>? = null
): Float { ): Float {
var durY = if (isTitle) y + titleTopSpacing else y var durY = if (isTitle) y + titleTopSpacing else y
val layout = if (ReadBookConfig.useZhLayout) { val layout = if (ReadBookConfig.useZhLayout) {
@ -216,7 +264,13 @@ object ChapterProvider {
if (lineIndex == 0 && layout.lineCount > 1 && !isTitle) { if (lineIndex == 0 && layout.lineCount > 1 && !isTitle) {
//第一行 //第一行
textLine.text = words textLine.text = words
addCharsToLineFirst(textLine, words.toStringArray(), textPaint, desiredWidth) addCharsToLineFirst(
textLine,
words.toStringArray(),
textPaint,
desiredWidth,
srcList
)
} else if (lineIndex == layout.lineCount - 1) { } else if (lineIndex == layout.lineCount - 1) {
//最后一行 //最后一行
textLine.text = "$words\n" textLine.text = "$words\n"
@ -224,11 +278,18 @@ object ChapterProvider {
val x = if (isTitle && ReadBookConfig.titleMode == 1) val x = if (isTitle && ReadBookConfig.titleMode == 1)
(visibleWidth - layout.getLineWidth(lineIndex)) / 2 (visibleWidth - layout.getLineWidth(lineIndex)) / 2
else 0f else 0f
addCharsToLineLast(textLine, words.toStringArray(), textPaint, x) addCharsToLineLast(textLine, words.toStringArray(), textPaint, x, srcList)
} else { } else {
//中间行 //中间行
textLine.text = words textLine.text = words
addCharsToLineMiddle(textLine, words.toStringArray(), textPaint, desiredWidth, 0f) addCharsToLineMiddle(
textLine,
words.toStringArray(),
textPaint,
desiredWidth,
0f,
srcList
)
} }
if (durY + textPaint.textHeight > visibleHeight) { if (durY + textPaint.textHeight > visibleHeight) {
//当前页面结束,设置各种值 //当前页面结束,设置各种值
@ -259,21 +320,30 @@ object ChapterProvider {
words: Array<String>, words: Array<String>,
textPaint: TextPaint, textPaint: TextPaint,
desiredWidth: Float, desiredWidth: Float,
srcList: LinkedList<String>?
) { ) {
var x = 0f var x = 0f
if (!ReadBookConfig.textFullJustify) { if (!ReadBookConfig.textFullJustify) {
addCharsToLineLast(textLine, words, textPaint, x) addCharsToLineLast(textLine, words, textPaint, x, srcList)
return return
} }
val bodyIndent = ReadBookConfig.paragraphIndent val bodyIndent = ReadBookConfig.paragraphIndent
val icw = StaticLayout.getDesiredWidth(bodyIndent, textPaint) / bodyIndent.length val icw = StaticLayout.getDesiredWidth(bodyIndent, textPaint) / bodyIndent.length
bodyIndent.toStringArray().forEach { bodyIndent.toStringArray().forEach {
val x1 = x + icw val x1 = x + icw
textLine.addTextChar(charData = it, start = paddingLeft + x, end = paddingLeft + x1) if (srcList != null && it == srcReplaceChar) {
textLine.addTextChar(
srcList.removeFirst(),
start = paddingLeft + x,
end = paddingLeft + x1
)
} else {
textLine.addTextChar(it, start = paddingLeft + x, end = paddingLeft + x1)
}
x = x1 x = x1
} }
val words1 = words.copyOfRange(bodyIndent.length, words.size) val words1 = words.copyOfRange(bodyIndent.length, words.size)
addCharsToLineMiddle(textLine, words1, textPaint, desiredWidth, x) addCharsToLineMiddle(textLine, words1, textPaint, desiredWidth, x, srcList)
} }
/** /**
@ -285,9 +355,10 @@ object ChapterProvider {
textPaint: TextPaint, textPaint: TextPaint,
desiredWidth: Float, desiredWidth: Float,
startX: Float, startX: Float,
srcList: LinkedList<String>?
) { ) {
if (!ReadBookConfig.textFullJustify) { if (!ReadBookConfig.textFullJustify) {
addCharsToLineLast(textLine, words, textPaint, startX) addCharsToLineLast(textLine, words, textPaint, startX, srcList)
return return
} }
val gapCount: Int = words.lastIndex val gapCount: Int = words.lastIndex
@ -296,7 +367,15 @@ object ChapterProvider {
words.forEachIndexed { index, s -> words.forEachIndexed { index, s ->
val cw = StaticLayout.getDesiredWidth(s, textPaint) val cw = StaticLayout.getDesiredWidth(s, textPaint)
val x1 = if (index != words.lastIndex) (x + cw + d) else (x + cw) val x1 = if (index != words.lastIndex) (x + cw + d) else (x + cw)
textLine.addTextChar(charData = s, start = paddingLeft + x, end = paddingLeft + x1) if (srcList != null && s == srcReplaceChar) {
textLine.addTextChar(
srcList.removeFirst(),
start = paddingLeft + x,
end = paddingLeft + x1
)
} else {
textLine.addTextChar(charData = s, start = paddingLeft + x, end = paddingLeft + x1)
}
x = x1 x = x1
} }
exceed(textLine, words) exceed(textLine, words)
@ -310,12 +389,21 @@ object ChapterProvider {
words: Array<String>, words: Array<String>,
textPaint: TextPaint, textPaint: TextPaint,
startX: Float, startX: Float,
srcList: LinkedList<String>?
) { ) {
var x = startX var x = startX
words.forEach { words.forEach {
val cw = StaticLayout.getDesiredWidth(it, textPaint) val cw = StaticLayout.getDesiredWidth(it, textPaint)
val x1 = x + cw val x1 = x + cw
textLine.addTextChar(charData = it, start = paddingLeft + x, end = paddingLeft + x1) if (srcList != null && it == srcReplaceChar) {
textLine.addTextChar(
srcList.removeFirst(),
start = paddingLeft + x,
end = paddingLeft + x1
)
} else {
textLine.addTextChar(charData = it, start = paddingLeft + x, end = paddingLeft + x1)
}
x = x1 x = x1
} }
exceed(textLine, words) exceed(textLine, words)

@ -9,7 +9,6 @@ import java.io.File
import java.util.* import java.util.*
val removeHtmlRegex = "</?(?:div|p|br|hr|h\\d|article|dd|dl)[^>]*>".toRegex() val removeHtmlRegex = "</?(?:div|p|br|hr|h\\d|article|dd|dl)[^>]*>".toRegex()
val imgRegex = "<img[^>]*>".toRegex()
val notImgHtmlRegex = "</?(?!img)[a-zA-Z]+(?=[ >])[^<>]*>".toRegex() val notImgHtmlRegex = "</?(?!img)[a-zA-Z]+(?=[ >])[^<>]*>".toRegex()
fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim() fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim()
@ -54,7 +53,6 @@ fun String?.isJsonArray(): Boolean =
fun String?.htmlFormat(): String { fun String?.htmlFormat(): String {
this ?: return "" this ?: return ""
return this return this
.replace(imgRegex, "\n$0\n")
.replace(removeHtmlRegex, "\n") .replace(removeHtmlRegex, "\n")
.replace(notImgHtmlRegex, "") .replace(notImgHtmlRegex, "")
.replace("\\s*\\n+\\s*".toRegex(), "\n  ") .replace("\\s*\\n+\\s*".toRegex(), "\n  ")

Loading…
Cancel
Save