正文规则添加字体

pull/475/head
gedoor 5 years ago
parent 4c5d030d61
commit 7dcd678d80
  1. 6
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 59
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt

@ -83,7 +83,7 @@ object BookHelp {
postEvent(EventBus.SAVE_CONTENT, bookChapter) postEvent(EventBus.SAVE_CONTENT, bookChapter)
} }
suspend fun saveFont(book: Book, bookChapter: BookChapter, font: ByteArray) { fun saveFont(book: Book, bookChapter: BookChapter, font: ByteArray) {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
downloadDir, downloadDir,
cacheFolderName, cacheFolderName,
@ -93,7 +93,7 @@ object BookHelp {
).writeBytes(font) ).writeBytes(font)
} }
suspend fun getFont(book: Book, bookChapter: BookChapter): File? { fun getFontPath(book: Book, bookChapter: BookChapter): String? {
val fontFile = FileUtils.getFile( val fontFile = FileUtils.getFile(
downloadDir, downloadDir,
cacheFolderName, cacheFolderName,
@ -102,7 +102,7 @@ object BookHelp {
bookChapter.getFontName() bookChapter.getFontName()
) )
if (fontFile.exists()) { if (fontFile.exists()) {
return fontFile return fontFile.absolutePath
} }
return null return null
} }

@ -11,6 +11,7 @@ import io.legado.app.constant.AppPattern
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.BookHelp
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.ui.book.read.page.entities.TextChar import io.legado.app.ui.book.read.page.entities.TextChar
@ -57,6 +58,11 @@ object ChapterProvider {
val pageLengths = arrayListOf<Int>() val pageLengths = arrayListOf<Int>()
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
var durY = 0f var durY = 0f
var paint = Pair(titlePaint, contentPaint)
BookHelp.getFontPath(book, bookChapter)?.let {
val typeface = getTypeface(it)
paint = getPaint(typeface)
}
textPages.add(TextPage()) textPages.add(TextPage())
contents.forEachIndexed { index, text -> contents.forEachIndexed { index, text ->
val matcher = AppPattern.imgPattern.matcher(text) val matcher = AppPattern.imgPattern.matcher(text)
@ -72,7 +78,7 @@ object ChapterProvider {
} }
} else { } else {
val isTitle = index == 0 val isTitle = index == 0
val textPaint = if (isTitle) titlePaint else contentPaint val textPaint = if (isTitle) paint.first else paint.second
if (!(isTitle && ReadBookConfig.titleMode == 2)) { if (!(isTitle && ReadBookConfig.titleMode == 2)) {
durY = setTypeText( durY = setTypeText(
text, durY, textPages, pageLines, text, durY, textPages, pageLines,
@ -327,8 +333,21 @@ object ChapterProvider {
* 更新样式 * 更新样式
*/ */
fun upStyle() { fun upStyle() {
typeface = try { typeface = getTypeface(ReadBookConfig.textFont)
val fontPath = ReadBookConfig.textFont getPaint(typeface).let {
titlePaint = it.first
contentPaint = it.second
}
//间距
lineSpacingExtra = ReadBookConfig.lineSpacingExtra
paragraphSpacing = ReadBookConfig.paragraphSpacing
titleTopSpacing = ReadBookConfig.titleTopSpacing.dp
titleBottomSpacing = ReadBookConfig.titleBottomSpacing.dp
upVisibleSize()
}
private fun getTypeface(fontPath: String): Typeface {
return try {
when { when {
fontPath.isContentScheme() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> { fontPath.isContentScheme() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
val fd = App.INSTANCE.contentResolver val fd = App.INSTANCE.contentResolver
@ -351,6 +370,9 @@ object ChapterProvider {
ReadBookConfig.save() ReadBookConfig.save()
Typeface.SANS_SERIF Typeface.SANS_SERIF
} }
}
private fun getPaint(typeface: Typeface): Pair<TextPaint, TextPaint> {
// 字体统一处理 // 字体统一处理
val bold = Typeface.create(typeface, Typeface.BOLD) val bold = Typeface.create(typeface, Typeface.BOLD)
val normal = Typeface.create(typeface, Typeface.NORMAL) val normal = Typeface.create(typeface, Typeface.NORMAL)
@ -371,25 +393,20 @@ object ChapterProvider {
} }
//标题 //标题
titlePaint = TextPaint() val tPaint = TextPaint()
titlePaint.color = ReadBookConfig.textColor tPaint.color = ReadBookConfig.textColor
titlePaint.letterSpacing = ReadBookConfig.letterSpacing tPaint.letterSpacing = ReadBookConfig.letterSpacing
titlePaint.typeface = titleFont tPaint.typeface = titleFont
titlePaint.textSize = with(ReadBookConfig) { textSize + titleSize }.sp.toFloat() tPaint.textSize = with(ReadBookConfig) { textSize + titleSize }.sp.toFloat()
titlePaint.isAntiAlias = true tPaint.isAntiAlias = true
//正文 //正文
contentPaint = TextPaint() val cPaint = TextPaint()
contentPaint.color = ReadBookConfig.textColor cPaint.color = ReadBookConfig.textColor
contentPaint.letterSpacing = ReadBookConfig.letterSpacing cPaint.letterSpacing = ReadBookConfig.letterSpacing
contentPaint.typeface = textFont cPaint.typeface = textFont
contentPaint.textSize = ReadBookConfig.textSize.sp.toFloat() cPaint.textSize = ReadBookConfig.textSize.sp.toFloat()
contentPaint.isAntiAlias = true cPaint.isAntiAlias = true
//间距 return Pair(tPaint, cPaint)
lineSpacingExtra = ReadBookConfig.lineSpacingExtra
paragraphSpacing = ReadBookConfig.paragraphSpacing
titleTopSpacing = ReadBookConfig.titleTopSpacing.dp
titleBottomSpacing = ReadBookConfig.titleBottomSpacing.dp
upVisibleSize()
} }
/** /**

Loading…
Cancel
Save