pull/1816/head
kunfei 3 years ago
parent aad982196d
commit e59746eae0
  1. 2
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 3
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 108
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
  4. 8
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt

@ -89,7 +89,7 @@ object BookController {
this.bookUrl = bookUrl this.bookUrl = bookUrl
val bitmap = runBlocking { val bitmap = runBlocking {
ImageProvider.cacheImage(book, src, bookSource) ImageProvider.cacheImage(book, src, bookSource)
ImageProvider.getImage(book, src, width) ImageProvider.getImage(book, src, width, bookSource = bookSource)
} }
return returnData.setData(bitmap) return returnData.setData(bitmap)
} }

@ -170,7 +170,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
book, book,
textChar.charData, textChar.charData,
(textChar.end - textChar.start).toInt(), (textChar.end - textChar.start).toInt(),
(lineBottom - lineTop).toInt() (lineBottom - lineTop).toInt(),
ReadBook.bookSource
) )
val rectF = if (textLine.isImage) { val rectF = if (textLine.isImage) {
RectF(textChar.start, lineTop, textChar.end, lineBottom) RectF(textChar.start, lineTop, textChar.end, lineBottom)

@ -116,7 +116,7 @@ object ChapterProvider {
if (ReadBookConfig.titleMode != 2) { if (ReadBookConfig.titleMode != 2) {
displayTitle.splitNotBlank("\n").forEach { text -> displayTitle.splitNotBlank("\n").forEach { text ->
setTypeText( setTypeText(
absStartX, durY, text, textPages, stringBuilder, titlePaint, book, absStartX, durY, text, textPages, stringBuilder, titlePaint,
isTitle = true, isTitle = true,
isTitleWithNoContent = contents.isEmpty(), isTitleWithNoContent = contents.isEmpty(),
isVolumeTitle = bookChapter.isVolume isVolumeTitle = bookChapter.isVolume
@ -142,7 +142,7 @@ object ChapterProvider {
matcher.appendTail(sb) matcher.appendTail(sb)
text = sb.toString() text = sb.toString()
setTypeText( setTypeText(
absStartX, durY, text, textPages, stringBuilder, contentPaint, book, absStartX, durY, text, textPages, stringBuilder, contentPaint,
srcList = srcList srcList = srcList
).let { ).let {
absStartX = it.first absStartX = it.first
@ -155,7 +155,7 @@ object ChapterProvider {
val text = content.substring(start, matcher.start()) val text = content.substring(start, matcher.start())
if (text.isNotBlank()) { if (text.isNotBlank()) {
setTypeText( setTypeText(
absStartX, durY, text, textPages, stringBuilder, contentPaint book, absStartX, durY, text, textPages, stringBuilder, contentPaint
).let { ).let {
absStartX = it.first absStartX = it.first
durY = it.second durY = it.second
@ -171,7 +171,7 @@ object ChapterProvider {
val text = content.substring(start, content.length) val text = content.substring(start, content.length)
if (text.isNotBlank()) { if (text.isNotBlank()) {
setTypeText( setTypeText(
absStartX, durY, text, textPages, stringBuilder, contentPaint book, absStartX, durY, text, textPages, stringBuilder, contentPaint
).let { ).let {
absStartX = it.first absStartX = it.first
durY = it.second durY = it.second
@ -199,6 +199,9 @@ object ChapterProvider {
) )
} }
/**
* 排版图片
*/
private suspend fun setTypeImage( private suspend fun setTypeImage(
book: Book, book: Book,
src: String, src: String,
@ -264,7 +267,8 @@ object ChapterProvider {
/** /**
* 排版文字 * 排版文字
*/ */
private fun setTypeText( private suspend fun setTypeText(
book: Book,
x: Int, x: Int,
y: Float, y: Float,
text: String, text: String,
@ -336,12 +340,8 @@ object ChapterProvider {
//第一行 非标题 //第一行 非标题
textLine.text = words textLine.text = words
addCharsToLineFirst( addCharsToLineFirst(
absStartX, book, absStartX, textLine, words.toStringArray(),
textLine, textPaint, desiredWidth, srcList
words.toStringArray(),
textPaint,
desiredWidth,
srcList
) )
} }
lineIndex == layout.lineCount - 1 -> { lineIndex == layout.lineCount - 1 -> {
@ -354,25 +354,16 @@ object ChapterProvider {
(visibleWidth - layout.getLineWidth(lineIndex)) / 2 (visibleWidth - layout.getLineWidth(lineIndex)) / 2
else 0f else 0f
addCharsToLineLast( addCharsToLineLast(
absStartX, book, absStartX, textLine, words.toStringArray(),
textLine, textPaint, startX, srcList
words.toStringArray(),
textPaint,
startX,
srcList
) )
} }
else -> { else -> {
//中间行 //中间行
textLine.text = words textLine.text = words
addCharsToLineMiddle( addCharsToLineMiddle(
absStartX, book, absStartX, textLine, words.toStringArray(),
textLine, textPaint, desiredWidth, 0f, srcList
words.toStringArray(),
textPaint,
desiredWidth,
0f,
srcList
) )
} }
} }
@ -392,7 +383,8 @@ object ChapterProvider {
/** /**
* 有缩进,两端对齐 * 有缩进,两端对齐
*/ */
private fun addCharsToLineFirst( private suspend fun addCharsToLineFirst(
book: Book,
absStartX: Int, absStartX: Int,
textLine: TextLine, textLine: TextLine,
words: Array<String>, words: Array<String>,
@ -402,7 +394,7 @@ object ChapterProvider {
) { ) {
var x = 0f var x = 0f
if (!ReadBookConfig.textFullJustify) { if (!ReadBookConfig.textFullJustify) {
addCharsToLineLast(absStartX, textLine, words, textPaint, x, srcList) addCharsToLineLast(book, absStartX, textLine, words, textPaint, x, srcList)
return return
} }
val bodyIndent = ReadBookConfig.paragraphIndent val bodyIndent = ReadBookConfig.paragraphIndent
@ -420,14 +412,17 @@ object ChapterProvider {
} }
if (words.size > bodyIndent.length) { if (words.size > bodyIndent.length) {
val words1 = words.copyOfRange(bodyIndent.length, words.size) val words1 = words.copyOfRange(bodyIndent.length, words.size)
addCharsToLineMiddle(absStartX, textLine, words1, textPaint, desiredWidth, x, srcList) addCharsToLineMiddle(
book, absStartX, textLine, words1, textPaint, desiredWidth, x, srcList
)
} }
} }
/** /**
* 无缩进,两端对齐 * 无缩进,两端对齐
*/ */
private fun addCharsToLineMiddle( private suspend fun addCharsToLineMiddle(
book: Book,
absStartX: Int, absStartX: Int,
textLine: TextLine, textLine: TextLine,
words: Array<String>, words: Array<String>,
@ -437,7 +432,7 @@ object ChapterProvider {
srcList: LinkedList<String>? srcList: LinkedList<String>?
) { ) {
if (!ReadBookConfig.textFullJustify) { if (!ReadBookConfig.textFullJustify) {
addCharsToLineLast(absStartX, textLine, words, textPaint, startX, srcList) addCharsToLineLast(book, absStartX, textLine, words, textPaint, startX, srcList)
return return
} }
val gapCount: Int = words.lastIndex val gapCount: Int = words.lastIndex
@ -446,24 +441,7 @@ object ChapterProvider {
words.forEachIndexed { index, char -> words.forEachIndexed { index, char ->
val cw = StaticLayout.getDesiredWidth(char, textPaint) val cw = StaticLayout.getDesiredWidth(char, 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)
if (srcList != null && char == srcReplaceChar) { addCharToLine(book, absStartX, textLine, char, x, x1, srcList)
textLine.textChars.add(
TextChar(
charData = srcList.removeFirst(),
start = absStartX + x,
end = absStartX + x1,
isImage = true
)
)
} else {
textLine.textChars.add(
TextChar(
charData = char,
start = absStartX + x,
end = absStartX + x1
)
)
}
x = x1 x = x1
} }
exceed(absStartX, textLine, words) exceed(absStartX, textLine, words)
@ -472,7 +450,8 @@ object ChapterProvider {
/** /**
* 最后一行,自然排列 * 最后一行,自然排列
*/ */
private fun addCharsToLineLast( private suspend fun addCharsToLineLast(
book: Book,
absStartX: Int, absStartX: Int,
textLine: TextLine, textLine: TextLine,
words: Array<String>, words: Array<String>,
@ -484,12 +463,32 @@ object ChapterProvider {
words.forEach { char -> words.forEach { char ->
val cw = StaticLayout.getDesiredWidth(char, textPaint) val cw = StaticLayout.getDesiredWidth(char, textPaint)
val x1 = x + cw val x1 = x + cw
addCharToLine(book, absStartX, textLine, char, x, x1, srcList)
x = x1
}
exceed(absStartX, textLine, words)
}
/**
* 添加字符
*/
private suspend fun addCharToLine(
book: Book,
absStartX: Int,
textLine: TextLine,
char: String,
xStart: Float,
xEnd: Float,
srcList: LinkedList<String>?
) {
if (srcList != null && char == srcReplaceChar) { if (srcList != null && char == srcReplaceChar) {
val src = srcList.removeFirst()
ImageProvider.cacheImage(book, src, ReadBook.bookSource)
textLine.textChars.add( textLine.textChars.add(
TextChar( TextChar(
charData = srcList.removeFirst(), charData = src,
start = absStartX + x, start = absStartX + xStart,
end = absStartX + x1, end = absStartX + xEnd,
isImage = true isImage = true
) )
) )
@ -497,14 +496,11 @@ object ChapterProvider {
textLine.textChars.add( textLine.textChars.add(
TextChar( TextChar(
charData = char, charData = char,
start = absStartX + x, start = absStartX + xStart,
end = absStartX + x1 end = absStartX + xEnd
) )
) )
} }
x = x1
}
exceed(absStartX, textLine, words)
} }
/** /**

@ -15,6 +15,7 @@ import io.legado.app.model.localBook.EpubFile
import io.legado.app.utils.BitmapUtils import io.legado.app.utils.BitmapUtils
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.isXml import io.legado.app.utils.isXml
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
@ -103,11 +104,14 @@ object ImageProvider {
book: Book, book: Book,
src: String, src: String,
width: Int, width: Int,
height: Int? = null height: Int? = null,
bookSource: BookSource? = null
): Bitmap { ): Bitmap {
val cacheBitmap = bitmapLruCache.get(src) val cacheBitmap = bitmapLruCache.get(src)
if (cacheBitmap != null) return cacheBitmap if (cacheBitmap != null) return cacheBitmap
val vFile = BookHelp.getImage(book, src) val vFile = runBlocking {
cacheImage(book, src, bookSource)
}
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
return kotlin.runCatching { return kotlin.runCatching {
val bitmap = BitmapUtils.decodeBitmap(vFile.absolutePath, width, height) val bitmap = BitmapUtils.decodeBitmap(vFile.absolutePath, width, height)

Loading…
Cancel
Save