From b6dc02ef389053c6a7575b2d84278250f6c9e992 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 22 Nov 2020 20:35:22 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 10 +++++-- .../app/data/entities/rule/ContentRule.kt | 3 +- .../java/io/legado/app/help/JsExtensions.kt | 11 ++++++- .../app/model/analyzeRule/AnalyzeRule.kt | 30 +------------------ .../legado/app/model/webBook/BookContent.kt | 9 ++++-- .../source/edit/BookSourceEditActivity.kt | 2 ++ app/src/main/res/values-zh-rHK/strings.xml | 4 +-- app/src/main/res/values-zh-rTW/strings.xml | 3 +- app/src/main/res/values-zh/strings.xml | 5 ++-- app/src/main/res/values/strings.xml | 3 +- 10 files changed, 38 insertions(+), 42 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index ca3b56d6a..69f78559f 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -4,9 +4,13 @@ * 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 **2020/11/22** -* 正文添加正确字体规则,可以通过文字轮廓对比将错误的文字替换为正确的文字 -* js添加java.readFile("path")读取本地文件,返回BiteArray -* js添加java.queryTTF(font: ByteArray?),返回字体处理类,可以编码和轮廓互查,io.legado.app.model.analyzeRule.QueryTTF +* 正文字体处理规则修改,只能写js,不需要js标记 +``` +var b64=String(result).match(/ttf;base64,([^\)]+)/); +var f1 = java.queryBase64TTF(b64[1]) +var f2 = java.queryTTF("/storage/emulated/0/Fonts/Source Han Sans CN Regular.ttf") +java.replaceFont(content, f1, f2, 58000,59000) +``` **2020/11/18** * 优化导航栏 diff --git a/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt b/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt index efe652657..d9fe9afd6 100644 --- a/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt @@ -9,6 +9,7 @@ data class ContentRule( var nextContentUrl: String? = null, var webJs: String? = null, var sourceRegex: String? = null, - var replaceRegex: String? = null, //正文获取后处理规则 + var replaceRegex: String? = null, //替换规则 + var fontJs: String? = null, var imageStyle: String? = null, //默认大小居中,FULL最大宽度 ) : Parcelable \ No newline at end of file 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 99500604f..f8d8ea87d 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -268,7 +268,16 @@ interface JsExtensions { return QueryTTF(font) } - fun replaceFont(text: String, font1: QueryTTF, font2: QueryTTF, start: Int, end: Int): String { + fun replaceFont( + text: String, + font1: QueryTTF?, + font2: QueryTTF?, + start: Int, + end: Int + ): String { + if (font1 == null || font2 == null) { + return "" + } val contentArray = text.toCharArray() contentArray.forEachIndexed { index, s -> if (s > start.toChar() && s < end.toChar()) { diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index e0a4dbb8c..8bd4870cb 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -298,34 +298,6 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { return ArrayList() } - fun getContent(ruleStr: String, text: String?): String { - if (ruleStr.isEmpty()) return "" - val ruleList = splitSourceRule(ruleStr) - var result: Any? = null - content?.let { o -> - if (ruleList.isNotEmpty()) result = o - for (sourceRule in ruleList) { - putRule(sourceRule.putMap) - result?.let { - result = when (sourceRule.mode) { - Mode.Regex -> AnalyzeByRegex.getElements( - result.toString(), - sourceRule.rule.splitNotBlank("&&") - ) - Mode.Js -> evalJS(sourceRule.rule, result, text) - Mode.Json -> getAnalyzeByJSonPath(it).getList(sourceRule.rule) - Mode.XPath -> getAnalyzeByXPath(it).getElements(sourceRule.rule) - else -> getAnalyzeByJSoup(it).getElements(sourceRule.rule) - } - if (sourceRule.replaceRegex.isNotEmpty()) { - result = replaceRegex(result.toString(), sourceRule) - } - } - } - } - return result?.toString() ?: "" - } - /** * 保存变量 */ @@ -649,7 +621,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { /** * 执行JS */ - private fun evalJS(jsStr: String, result: Any?, content: String? = null): Any? { + fun evalJS(jsStr: String, result: Any?, content: String? = null): Any? { val bindings = SimpleBindings() bindings["java"] = this bindings["cookie"] = CookieStore 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 f13df3605..46fc2d690 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 @@ -92,9 +92,14 @@ object BookContent { } content.deleteCharAt(content.length - 1) var contentStr = content.toString().htmlFormat() - val replaceRegex = bookSource.ruleContent?.replaceRegex + val fontJs = contentRule.fontJs + if (!fontJs.isNullOrBlank()) { + contentStr = analyzeRule.evalJS(fontJs, body, contentStr)?.toString() ?: "" + } + val replaceRegex = contentRule.replaceRegex if (!replaceRegex.isNullOrEmpty()) { - contentStr = analyzeRule.getContent(replaceRegex, contentStr) + analyzeRule.setContent(contentStr) + contentStr = analyzeRule.getString(replaceRegex) } Debug.log(bookSource.bookSourceUrl, "┌获取章节名称") Debug.log(bookSource.bookSourceUrl, "└${bookChapter.title}") 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 37423cc54..080b8d368 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 @@ -236,6 +236,7 @@ class BookSourceEditActivity : add(EditEntity("webJs", cr?.webJs, R.string.rule_web_js)) add(EditEntity("sourceRegex", cr?.sourceRegex, R.string.rule_source_regex)) add(EditEntity("replaceRegex", cr?.replaceRegex, R.string.rule_replace_regex)) + add(EditEntity("fontJs", cr?.fontJs, R.string.rule_font_js)) add(EditEntity("imageStyle", cr?.imageStyle, R.string.rule_image_style)) } //发现 @@ -339,6 +340,7 @@ class BookSourceEditActivity : "webJs" -> contentRule.webJs = it.value "sourceRegex" -> contentRule.sourceRegex = it.value "replaceRegex" -> contentRule.replaceRegex = it.value + "fontJs" -> contentRule.fontJs = it.value "imageStyle" -> contentRule.imageStyle = it.value } } diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 19d45011a..806def31c 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -412,7 +412,7 @@ 正文下一頁 URL 規則 (nextContentUrl) webJs 資源正則 (sourceRegex) - + 字体处理js 圖標 (sourceIcon) 列表規則 (ruleArticles) 列表下一頁規則 (ruleArticles) @@ -730,7 +730,7 @@ 恢復時忽略一些內容不恢復,方便不同手機配置不同 閱讀界面設置 图片样式(imageStyle) - 正文获取后处理规则(replaceRegex) + 替换规则(replaceRegex) 分組名稱 備註內容 默认启用替换净化 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index fe065b1d9..233bc8953 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -730,7 +730,8 @@ 復原時忽略一些內容不復原,方便不同手機配置不同 閱讀介面設定 圖片樣式(imageStyle) - 正文获取后处理规则(replaceRegex) + 取代規則(replaceRegex) + 字体处理js 分組名稱 備註內容 預設啟用取代淨化 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 31b6c20d4..e8af9544d 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -417,6 +417,9 @@ 正文规则(content) 正文下一页URL规则(nextContentUrl) webJs + 图片样式(imageStyle) + 替换规则(replaceRegex) + 字体处理js 资源正则(sourceRegex) 图标(sourceIcon) @@ -734,8 +737,6 @@ 恢复忽略列表 恢复时忽略一些内容不恢复,方便不同手机配置不同 阅读界面设置 - 图片样式(imageStyle) - 正文获取后处理规则(replaceRegex) 分组名称 备注内容 默认启用替换净化 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5bf01375f..940b8c68e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -420,7 +420,8 @@ 正文下一页URL规则(nextContentUrl) webJs 资源正则(sourceRegex) - 正文获取后处理规则(replaceRegex) + 替换规则(replaceRegex) + 字体处理js 图片样式(imageStyle) 图标(sourceIcon) From d8467c32917cbe95e4e05eda9259924a45c96dca Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 22 Nov 2020 20:37:17 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/help/JsExtensions.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 f8d8ea87d..623eb7c9d 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -242,7 +242,7 @@ interface JsExtensions { } fun queryTTF(path: String): QueryTTF? { - val qTTF = CacheManager.getQueryTTF(path) + var qTTF = CacheManager.getQueryTTF(path) if (qTTF != null) { return qTTF } @@ -265,7 +265,9 @@ interface JsExtensions { } } font ?: return null - return QueryTTF(font) + qTTF = QueryTTF(font) + CacheManager.put(path, qTTF) + return qTTF } fun replaceFont( From 29fc8005c7ff14694b1804856937dc82e3489666 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 22 Nov 2020 20:48:13 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/help/JsExtensions.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 623eb7c9d..78780f7bc 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -242,17 +242,18 @@ interface JsExtensions { } fun queryTTF(path: String): QueryTTF? { - var qTTF = CacheManager.getQueryTTF(path) + val key = md5Encode16(path) + var qTTF = CacheManager.getQueryTTF(key) if (qTTF != null) { return qTTF } val font: ByteArray? = when { path.isAbsUrl() -> runBlocking { - var x = CacheManager.getByteArray(path) + var x = CacheManager.getByteArray(key) if (x == null) { x = HttpHelper.simpleGetBytesAsync(path) x?.let { - CacheManager.put(path, it) + CacheManager.put(key, it) } } return@runBlocking x @@ -266,7 +267,7 @@ interface JsExtensions { } font ?: return null qTTF = QueryTTF(font) - CacheManager.put(path, qTTF) + CacheManager.put(key, qTTF) return qTTF }