字体跟随背景,每个背景对应一个字体

pull/352/head
gedoor 4 years ago
parent b32a581984
commit 98e11facb2
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 11
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt
  3. 9
      app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt
  4. 24
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
  5. 38
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt

@ -22,7 +22,6 @@ object PreferKey {
const val cleanCache = "cleanCache" const val cleanCache = "cleanCache"
const val saveTabPosition = "saveTabPosition" const val saveTabPosition = "saveTabPosition"
const val pageAnim = "pageAnim" const val pageAnim = "pageAnim"
const val readBookFont = "readBookFont"
const val fontFolder = "fontFolder" const val fontFolder = "fontFolder"
const val backupPath = "backupUri" const val backupPath = "backupUri"
const val restoreIgnore = "restoreIgnore" const val restoreIgnore = "restoreIgnore"

@ -83,10 +83,12 @@ object ReadBookConfig {
fun save() { fun save() {
Coroutine.async { Coroutine.async {
synchronized(this) {
val json = GSON.toJson(configList) val json = GSON.toJson(configList)
FileUtils.createFileIfNotExist(configFilePath).writeText(json) FileUtils.createFileIfNotExist(configFilePath).writeText(json)
} }
} }
}
fun resetDur() { fun resetDur() {
defaultConfigs[styleSelect].let { defaultConfigs[styleSelect].let {
@ -149,6 +151,12 @@ object ReadBookConfig {
private val config get() = if (shareLayout) getConfig(5) else durConfig private val config get() = if (shareLayout) getConfig(5) else durConfig
var textFont: String
get() = config.textFont
set(value) {
config.textFont = value
}
var textBold: Int var textBold: Int
get() = config.textBold get() = config.textBold
set(value) { set(value) {
@ -296,6 +304,7 @@ object ReadBookConfig {
private var darkStatusIconNight: Boolean = false,//晚上是否暗色状态栏 private var darkStatusIconNight: Boolean = false,//晚上是否暗色状态栏
private var textColor: String = "#3E3D3B",//白天文字颜色 private var textColor: String = "#3E3D3B",//白天文字颜色
private var textColorNight: String = "#ADADAD",//夜间文字颜色 private var textColorNight: String = "#ADADAD",//夜间文字颜色
var textFont: String = "",//字体
var textBold: Int = 0,//是否粗体字 0:正常, 1:粗体, 2:细体 var textBold: Int = 0,//是否粗体字 0:正常, 1:粗体, 2:细体
var textSize: Int = 20,//文字大小 var textSize: Int = 20,//文字大小
var letterSpacing: Float = 0.1f,//字间距 var letterSpacing: Float = 0.1f,//字间距
@ -318,7 +327,7 @@ object ReadBookConfig {
var footerPaddingRight: Int = 16, var footerPaddingRight: Int = 16,
var footerPaddingTop: Int = 6, var footerPaddingTop: Int = 6,
var showHeaderLine: Boolean = false, var showHeaderLine: Boolean = false,
var showFooterLine: Boolean = true var showFooterLine: Boolean = true,
) { ) {
fun setBg(bgType: Int, bg: String) { fun setBg(bgType: Int, bg: String) {
if (AppConfig.isNightTheme) { if (AppConfig.isNightTheme) {

@ -9,7 +9,6 @@ import androidx.core.view.get
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseDialogFragment import io.legado.app.base.BaseDialogFragment
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.selector import io.legado.app.lib.dialogs.selector
@ -256,10 +255,12 @@ class ReadStyleDialog : BaseDialogFragment(), FontSelectDialog.CallBack {
} }
override val curFontPath: String override val curFontPath: String
get() = requireContext().getPrefString(PreferKey.readBookFont) ?: "" get() = ReadBookConfig.textFont
override fun selectFile(path: String) { override fun selectFont(path: String) {
requireContext().putPrefString(PreferKey.readBookFont, path) if (path != ReadBookConfig.textFont) {
ReadBookConfig.textFont = path
postEvent(EventBus.UP_CONFIG, true) postEvent(EventBus.UP_CONFIG, true)
} }
} }
}

@ -1,14 +1,13 @@
package io.legado.app.ui.book.read.page.provider package io.legado.app.ui.book.read.page.provider
import android.graphics.Typeface import android.graphics.Typeface
import android.net.Uri
import android.os.Build import android.os.Build
import android.text.Layout import android.text.Layout
import android.text.StaticLayout import android.text.StaticLayout
import android.text.TextPaint import android.text.TextPaint
import android.text.TextUtils
import io.legado.app.App import io.legado.app.App
import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern
import io.legado.app.constant.PreferKey
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
@ -382,18 +381,27 @@ object ChapterProvider {
*/ */
fun upStyle() { fun upStyle() {
typeface = try { typeface = try {
val fontPath = App.INSTANCE.getPrefString(PreferKey.readBookFont) val fontPath = ReadBookConfig.textFont
if (!TextUtils.isEmpty(fontPath)) { when {
Typeface.createFromFile(fontPath) fontPath.isContentPath() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
} else { val fd = App.INSTANCE.contentResolver
when (AppConfig.systemTypefaces) { .openFileDescriptor(Uri.parse(fontPath), "r")!!
.fileDescriptor
Typeface.Builder(fd).build()
}
fontPath.isContentPath() -> {
Typeface.createFromFile(RealPathUtil.getPath(App.INSTANCE, Uri.parse(fontPath)))
}
fontPath.isNotEmpty() -> Typeface.createFromFile(fontPath)
else -> when (AppConfig.systemTypefaces) {
1 -> Typeface.SERIF 1 -> Typeface.SERIF
2 -> Typeface.MONOSPACE 2 -> Typeface.MONOSPACE
else -> Typeface.SANS_SERIF else -> Typeface.SANS_SERIF
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
App.INSTANCE.removePref(PreferKey.readBookFont) ReadBookConfig.textFont = ""
ReadBookConfig.save()
Typeface.SANS_SERIF Typeface.SANS_SERIF
} }
// 字体统一处理 // 字体统一处理

@ -15,7 +15,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseDialogFragment import io.legado.app.base.BaseDialogFragment
import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.Permissions
@ -28,7 +27,6 @@ import io.legado.app.utils.*
import kotlinx.android.synthetic.main.dialog_font_select.* import kotlinx.android.synthetic.main.dialog_font_select.*
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File import java.io.File
import java.util.* import java.util.*
@ -170,34 +168,16 @@ class FontSelectDialog : BaseDialogFragment(),
override fun onClick(docItem: DocItem) { override fun onClick(docItem: DocItem) {
execute { execute {
fontFolder.listFiles()?.forEach { FileUtils.deleteFile(fontFolder.absolutePath)
it.delete() callBack?.selectFont(docItem.uri.toString())
}
if (docItem.isContentPath) {
val file = FileUtils.createFileIfNotExist(fontFolder, docItem.name)
@Suppress("BlockingMethodInNonBlockingContext")
requireActivity().contentResolver.openInputStream(docItem.uri)?.use { input ->
file.outputStream().use { output ->
input.copyTo(output)
}
}
callBack?.selectFile(file.path)
} else {
val file = File(docItem.uri.toString())
file.copyTo(FileUtils.createFileIfNotExist(fontFolder, file.name), true)
.absolutePath.let { path ->
if (curFilePath != path) {
withContext(Main) {
callBack?.selectFile(path)
}
}
}
}
}.onSuccess { }.onSuccess {
dialog?.dismiss() dialog?.dismiss()
} }
} }
/**
* 字体文件夹
*/
override fun onFilePicked(requestCode: Int, currentPath: String) { override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) { when (requestCode) {
fontFolderRequestCode -> { fontFolderRequestCode -> {
@ -231,11 +211,7 @@ class FontSelectDialog : BaseDialogFragment(),
} }
private fun onDefaultFontChange() { private fun onDefaultFontChange() {
if (curFilePath == "") { callBack?.selectFont("")
postEvent(EventBus.UP_CONFIG, true)
} else {
callBack?.selectFile("")
}
} }
override val curFilePath: String get() = callBack?.curFontPath ?: "" override val curFilePath: String get() = callBack?.curFontPath ?: ""
@ -244,7 +220,7 @@ class FontSelectDialog : BaseDialogFragment(),
get() = (parentFragment as? CallBack) ?: (activity as? CallBack) get() = (parentFragment as? CallBack) ?: (activity as? CallBack)
interface CallBack { interface CallBack {
fun selectFile(path: String) fun selectFont(path: String)
val curFontPath: String val curFontPath: String
} }
} }
Loading…
Cancel
Save