mirror of
https://github.com/gedoor/legado.git
synced 2024-07-02 23:17:32 +08:00
Compare commits
5 Commits
f132e92772
...
72e07d9bb6
Author | SHA1 | Date | |
---|---|---|---|
|
72e07d9bb6 | ||
|
85b1e7b548 | ||
|
d7ae1d4f0a | ||
|
c907ed51b7 | ||
|
d1af37e04b |
|
@ -57,6 +57,7 @@ import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.net.URLEncoder
|
import java.net.URLEncoder
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
|
import java.security.MessageDigest
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
@ -748,45 +749,68 @@ interface JsExtensions : JsEncodeUtils {
|
||||||
//******************文件操作************************//
|
//******************文件操作************************//
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析字体,返回字体解析类
|
* 解析字体Base64数据,返回字体解析类
|
||||||
*/
|
*/
|
||||||
fun queryBase64TTF(base64: String?): QueryTTF? {
|
fun queryBase64TTF(data: String?): QueryTTF? {
|
||||||
base64DecodeToByteArray(base64)?.let {
|
log("queryBase64TTF(String)方法已过时,并将在未来删除;请无脑使用queryTTF(Any)替代,新方法支持传入 url、本地文件、base64、ByteArray 自动判断&自动缓存,特殊情况需禁用缓存请传入第二可选参数false:Boolean")
|
||||||
return QueryTTF(it)
|
return queryTTF(data)
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回字体解析类
|
* 返回字体解析类
|
||||||
* @param str 支持url,本地文件,base64,自动判断,自动缓存
|
* @param data 支持url,本地文件,base64,ByteArray,自动判断,自动缓存
|
||||||
|
* @param useCache 可选开关缓存,不传入该值默认开启缓存
|
||||||
*/
|
*/
|
||||||
fun queryTTF(str: String?): QueryTTF? {
|
@OptIn(ExperimentalStdlibApi::class)
|
||||||
str ?: return null
|
fun queryTTF(data: Any?, useCache: Boolean): QueryTTF? {
|
||||||
try {
|
try {
|
||||||
val key = md5Encode16(str)
|
var key: String? = null
|
||||||
var qTTF = CacheManager.getQueryTTF(key)
|
var qTTF: QueryTTF?
|
||||||
if (qTTF != null) return qTTF
|
when (data) {
|
||||||
val font: ByteArray? = when {
|
is String -> {
|
||||||
str.isAbsUrl() -> AnalyzeUrl(str, source = getSource()).getByteArray()
|
if (useCache) {
|
||||||
str.isContentScheme() -> Uri.parse(str).readBytes(appCtx)
|
key = MessageDigest.getInstance("SHA-256").digest(data.toByteArray()).toHexString()
|
||||||
str.startsWith("/storage") -> File(str).readBytes()
|
qTTF = CacheManager.getQueryTTF(key)
|
||||||
else -> base64DecodeToByteArray(str)
|
if (qTTF != null) return qTTF
|
||||||
|
}
|
||||||
|
val font: ByteArray? = when {
|
||||||
|
data.isContentScheme() -> Uri.parse(data).readBytes(appCtx)
|
||||||
|
data.startsWith("/storage") -> File(data).readBytes()
|
||||||
|
data.isAbsUrl() -> AnalyzeUrl(data, source = getSource()).getByteArray()
|
||||||
|
else -> base64DecodeToByteArray(data)
|
||||||
|
}
|
||||||
|
font ?: return null
|
||||||
|
qTTF = QueryTTF(font)
|
||||||
|
}
|
||||||
|
|
||||||
|
is ByteArray -> {
|
||||||
|
if (useCache) {
|
||||||
|
key = MessageDigest.getInstance("SHA-256").digest(data).toHexString()
|
||||||
|
qTTF = CacheManager.getQueryTTF(key)
|
||||||
|
if (qTTF != null) return qTTF
|
||||||
|
}
|
||||||
|
qTTF = QueryTTF(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> return null
|
||||||
}
|
}
|
||||||
font ?: return null
|
if (key != null) CacheManager.put(key, qTTF)
|
||||||
qTTF = QueryTTF(font)
|
|
||||||
CacheManager.put(key, qTTF) // debug注释掉
|
|
||||||
return qTTF
|
return qTTF
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
AppLog.put("获取字体处理类出错", e)
|
AppLog.put("[queryTTF] 获取字体处理类出错", e)
|
||||||
throw e
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun queryTTF(data: Any?): QueryTTF? {
|
||||||
|
return queryTTF(data, true)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param text 包含错误字体的内容
|
* @param text 包含错误字体的内容
|
||||||
* @param errorQueryTTF 错误的字体
|
* @param errorQueryTTF 错误的字体
|
||||||
* @param correctQueryTTF 正确的字体
|
* @param correctQueryTTF 正确的字体
|
||||||
|
* @param filter 删除 errorQueryTTF 中不存在的字符
|
||||||
*/
|
*/
|
||||||
fun replaceFont(
|
fun replaceFont(
|
||||||
text: String,
|
text: String,
|
||||||
|
@ -804,7 +828,7 @@ interface JsExtensions : JsEncodeUtils {
|
||||||
}
|
}
|
||||||
// 删除轮廓数据不存在的字符
|
// 删除轮廓数据不存在的字符
|
||||||
var glyf = errorQueryTTF.getGlyfByUnicode(oldCode) // 轮廓数据不存在
|
var glyf = errorQueryTTF.getGlyfByUnicode(oldCode) // 轮廓数据不存在
|
||||||
if (errorQueryTTF.getGlyfIdByUnicode(oldCode) == 0) glyf = null; // 轮廓数据指向保留索引0
|
if (errorQueryTTF.getGlyfIdByUnicode(oldCode) == 0) glyf = null // 轮廓数据指向保留索引0
|
||||||
if (filter && (glyf == null)) {
|
if (filter && (glyf == null)) {
|
||||||
contentArray[index] = ""
|
contentArray[index] = ""
|
||||||
return@forEachIndexed
|
return@forEachIndexed
|
||||||
|
@ -818,6 +842,11 @@ interface JsExtensions : JsEncodeUtils {
|
||||||
return contentArray.joinToString("")
|
return contentArray.joinToString("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param text 包含错误字体的内容
|
||||||
|
* @param errorQueryTTF 错误的字体
|
||||||
|
* @param correctQueryTTF 正确的字体
|
||||||
|
*/
|
||||||
fun replaceFont(
|
fun replaceFont(
|
||||||
text: String,
|
text: String,
|
||||||
errorQueryTTF: QueryTTF?,
|
errorQueryTTF: QueryTTF?,
|
||||||
|
|
|
@ -685,6 +685,7 @@ public class QueryTTF {
|
||||||
int unicodeInclusive = 0;
|
int unicodeInclusive = 0;
|
||||||
int unicodeExclusive = f.glyphIdArray.length;
|
int unicodeExclusive = f.glyphIdArray.length;
|
||||||
for (; unicodeInclusive < unicodeExclusive; unicodeInclusive++) {
|
for (; unicodeInclusive < unicodeExclusive; unicodeInclusive++) {
|
||||||
|
if (f.glyphIdArray[unicodeInclusive] == 0) continue; // 排除轮廓索引为0的Unicode
|
||||||
unicodeToGlyphId.put(unicodeInclusive, f.glyphIdArray[unicodeInclusive]);
|
unicodeToGlyphId.put(unicodeInclusive, f.glyphIdArray[unicodeInclusive]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -711,12 +712,15 @@ public class QueryTTF {
|
||||||
int idDelta = f.idDelta[segmentIndex];
|
int idDelta = f.idDelta[segmentIndex];
|
||||||
int idRangeOffset = f.idRangeOffsets[segmentIndex];
|
int idRangeOffset = f.idRangeOffsets[segmentIndex];
|
||||||
for (int unicode = unicodeInclusive; unicode <= unicodeExclusive; unicode++) {
|
for (int unicode = unicodeInclusive; unicode <= unicodeExclusive; unicode++) {
|
||||||
|
int glyphId = 0;
|
||||||
if (idRangeOffset == 0) {
|
if (idRangeOffset == 0) {
|
||||||
unicodeToGlyphId.put(unicode, (unicode + idDelta) & 0xFFFF);
|
glyphId = (unicode + idDelta) & 0xFFFF;
|
||||||
} else {
|
} else {
|
||||||
int gIndex = (idRangeOffset / 2) + unicode - unicodeInclusive + segmentIndex - segCount;
|
int gIndex = (idRangeOffset / 2) + unicode - unicodeInclusive + segmentIndex - segCount;
|
||||||
unicodeToGlyphId.put(unicode, gIndex < glyphIdArrayLength ? f.glyphIdArray[gIndex] + idDelta : 0);
|
if (gIndex < glyphIdArrayLength) glyphId = f.glyphIdArray[gIndex] + idDelta;
|
||||||
}
|
}
|
||||||
|
if (glyphId == 0) continue; // 排除轮廓索引为0的Unicode
|
||||||
|
unicodeToGlyphId.put(unicode, glyphId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -38,7 +38,7 @@ quickChineseTransfer = "0.2.13"
|
||||||
room = "2.6.1"
|
room = "2.6.1"
|
||||||
splitties = "3.0.0"
|
splitties = "3.0.0"
|
||||||
|
|
||||||
activity = "1.8.2"
|
activity = "1.9.0"
|
||||||
kotlinxSerialization = "1.6.3"
|
kotlinxSerialization = "1.6.3"
|
||||||
swiperefreshlayout = "1.1.0"
|
swiperefreshlayout = "1.1.0"
|
||||||
viewpager2 = "1.0.0"
|
viewpager2 = "1.0.0"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user