diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index c0161c2a1..95cc17d94 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -7,6 +7,7 @@ import io.legado.app.help.http.CookieStore import io.legado.app.help.http.SSLHelper import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeUrl +import io.legado.app.model.analyzeRule.QueryTTF import io.legado.app.utils.* import org.jsoup.Connection import org.jsoup.Jsoup @@ -226,6 +227,14 @@ interface JsExtensions { return File(path).readBytes() } + /** + * 解析字体,返回字体解析类 + */ + fun queryTTF(font: ByteArray?): QueryTTF? { + font ?: return null + return QueryTTF(font) + } + /** * 输出调试日志 */ diff --git a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt index 4285b4a2e..40b39c8cf 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt @@ -10,8 +10,10 @@ import io.legado.app.help.BookHelp import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl +import io.legado.app.model.analyzeRule.QueryTTF import io.legado.app.utils.NetworkUtils import io.legado.app.utils.htmlFormat +import io.legado.app.utils.toStringArray import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.withContext @@ -34,13 +36,21 @@ object BookContent { val content = StringBuilder() val nextUrlList = arrayListOf(baseUrl) val contentRule = bookSource.getContentRule() - contentRule.font?.let { - //todo 获取字体 - val analyzeRule = AnalyzeRule(book) - analyzeRule.setContent(body).setBaseUrl(baseUrl) - analyzeRule.getByteArray(it)?.let { font -> - BookHelp.saveFont(book, bookChapter, font) - } + val analyzeRule = AnalyzeRule(book).setContent(body, baseUrl) + val fontRule = contentRule.font + val correctFontRule = contentRule.correctFont + var font: ByteArray? = null + var correctFont: ByteArray? = null + fontRule?.let { + //todo 获取网页嵌入字体 + font = analyzeRule.getByteArray(it) + } + correctFontRule?.let { + //todo 获取正确字体 + correctFont = analyzeRule.getByteArray(it) + } + if (correctFont == null && font != null) { + BookHelp.saveFont(book, bookChapter, font!!) } var contentData = analyzeContent( book, baseUrl, body, contentRule, bookChapter, bookSource @@ -101,11 +111,20 @@ object BookContent { var contentStr = content.toString().htmlFormat() val replaceRegex = bookSource.ruleContent?.replaceRegex if (!replaceRegex.isNullOrEmpty()) { - val analyzeRule = AnalyzeRule(book) analyzeRule.setContent(contentStr).setBaseUrl(baseUrl) analyzeRule.chapter = bookChapter contentStr = analyzeRule.getString(replaceRegex) } + if (correctFont != null && font != null) { + val queryTTF = QueryTTF(font!!) + val cQueryTTF = QueryTTF(correctFont!!) + val contentArray = contentStr.toStringArray() + contentArray.forEachIndexed { index, s -> + val code = cQueryTTF.GetCodeByGlyf(queryTTF.GetGlyfByCode(s.toInt())) + contentArray[index] = code.toString() + } + contentStr = contentArray.joinToString("") + } Debug.log(bookSource.bookSourceUrl, "┌获取章节名称") Debug.log(bookSource.bookSourceUrl, "└${bookChapter.title}") Debug.log(bookSource.bookSourceUrl, "┌获取正文内容") diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index bf7a61475..fa4a12f0a 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -22,6 +22,8 @@ import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.backgroundColor import io.legado.app.ui.book.source.debug.BookSourceDebugActivity +import io.legado.app.ui.filepicker.FilePicker +import io.legado.app.ui.filepicker.FilePickerDialog import io.legado.app.ui.login.SourceLogin import io.legado.app.ui.qrcode.QrCodeActivity import io.legado.app.ui.widget.KeyboardToolPop @@ -33,11 +35,13 @@ import kotlin.math.abs class BookSourceEditActivity : VMBaseActivity(R.layout.activity_book_source_edit, false), + FilePickerDialog.CallBack, KeyboardToolPop.CallBack { override val viewModel: BookSourceEditViewModel get() = getViewModel(BookSourceEditViewModel::class.java) private val qrRequestCode = 101 + private val selectPathRequestCode = 102 private val adapter = BookSourceEditAdapter() private val sourceEntities: ArrayList = ArrayList() private val searchEntities: ArrayList = ArrayList() @@ -234,7 +238,7 @@ class BookSourceEditActivity : add(EditEntity("replaceRegex", cr?.replaceRegex, R.string.rule_replace_regex)) add(EditEntity("imageStyle", cr?.imageStyle, R.string.rule_image_style)) add(EditEntity("font", cr?.font, R.string.rule_font)) - add(EditEntity("correctFont", cr?.font, R.string.rule_correct_font)) + add(EditEntity("correctFont", cr?.correctFont, R.string.rule_correct_font)) } //发现 val er = source?.getExploreRule() @@ -339,7 +343,7 @@ class BookSourceEditActivity : "replaceRegex" -> contentRule.replaceRegex = it.value "imageStyle" -> contentRule.imageStyle = it.value "font" -> contentRule.font = it.value - "correctFont" -> contentRule.font = it.value + "correctFont" -> contentRule.correctFont = it.value } } source.ruleSearch = searchRule @@ -382,12 +386,13 @@ class BookSourceEditActivity : } private fun showHelpDialog() { - val items = arrayListOf("插入URL参数", "书源教程", "正则教程") + val items = arrayListOf("插入URL参数", "书源教程", "正则教程", "选择文件") selector(getString(R.string.help), items) { _, index -> when (index) { 0 -> insertText(AppConst.urlOption) 1 -> openUrl("https://alanskycn.gitee.io/teachme/Rule/source.html") 2 -> showRegexHelp() + 3 -> FilePicker.selectFile(this, selectPathRequestCode) } } } @@ -420,6 +425,15 @@ class BookSourceEditActivity : } } } + selectPathRequestCode -> if (resultCode == RESULT_OK) { + data?.data?.let { uri -> + if (uri.isContentScheme()) { + sendText(uri.toString()) + } else { + sendText(uri.path.toString()) + } + } + } } } diff --git a/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt b/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt index ada36f5ff..7be76347d 100644 --- a/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt +++ b/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt @@ -119,7 +119,7 @@ object FilePicker { activity: BaseActivity, requestCode: Int, title: String = activity.getString(R.string.select_file), - allowExtensions: Array, + allowExtensions: Array = arrayOf(), otherActions: List? = null, otherFun: ((action: String) -> Unit)? = null ) { @@ -177,7 +177,7 @@ object FilePicker { fragment: Fragment, requestCode: Int, title: String = fragment.getString(R.string.select_file), - allowExtensions: Array, + allowExtensions: Array = arrayOf(), otherActions: List? = null, otherFun: ((action: String) -> Unit)? = null ) { @@ -267,10 +267,15 @@ object FilePicker { private fun typesOfExtensions(allowExtensions: Array): Array { val types = hashSetOf() - allowExtensions.forEach { - when (it) { - "txt", "xml" -> types.add("text/*") - else -> types.add("application/$it") + if (allowExtensions.isNullOrEmpty()) { + types.add("*/*") + } else { + allowExtensions.forEach { + when (it) { + "*" -> types.add("*/*") + "txt", "xml" -> types.add("text/*") + else -> types.add("application/$it") + } } } return types.toTypedArray() diff --git a/app/src/main/java/io/legado/app/ui/filepicker/FilePickerDialog.kt b/app/src/main/java/io/legado/app/ui/filepicker/FilePickerDialog.kt index 082143010..4617859bd 100644 --- a/app/src/main/java/io/legado/app/ui/filepicker/FilePickerDialog.kt +++ b/app/src/main/java/io/legado/app/ui/filepicker/FilePickerDialog.kt @@ -169,7 +169,7 @@ class FilePickerDialog : DialogFragment(), fileItem?.path?.let { path -> if (mode == DIRECTORY) { toast("这是文件夹选择,不能选择文件,点击右上角的确定选择文件夹") - } else if (allowExtensions == null || + } else if (allowExtensions.isNullOrEmpty() || allowExtensions?.contains(FileUtils.getExtension(path)) == true ) { setData(path) diff --git a/app/src/main/java/io/legado/app/ui/filepicker/adapter/FileAdapter.kt b/app/src/main/java/io/legado/app/ui/filepicker/adapter/FileAdapter.kt index 728b2149c..624c44297 100644 --- a/app/src/main/java/io/legado/app/ui/filepicker/adapter/FileAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/filepicker/adapter/FileAdapter.kt @@ -97,7 +97,7 @@ class FileAdapter(context: Context, val callBack: CallBack) : text_view.setTextColor(disabledTextColor) } else { callBack.allowExtensions?.let { - if (it.contains(FileUtils.getExtension(item.path))) { + if (it.isEmpty() || it.contains(FileUtils.getExtension(item.path))) { text_view.setTextColor(primaryTextColor) } else { text_view.setTextColor(disabledTextColor)