Compare commits

..

1 Commits

Author SHA1 Message Date
dependabot[bot]
2b5de041dd
Merge e1744cd36e into 576a370da1 2024-06-20 11:39:12 +08:00
2 changed files with 24 additions and 57 deletions

View File

@ -57,7 +57,6 @@ import java.io.File
import java.io.FileOutputStream
import java.net.URLEncoder
import java.nio.charset.Charset
import java.security.MessageDigest
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
@ -749,68 +748,45 @@ interface JsExtensions : JsEncodeUtils {
//******************文件操作************************//
/**
* 解析字体Base64数据,返回字体解析类
* 解析字体,返回字体解析类
*/
fun queryBase64TTF(data: String?): QueryTTF? {
log("queryBase64TTF(String)方法已过时,并将在未来删除请无脑使用queryTTF(Any)替代,新方法支持传入 url、本地文件、base64、ByteArray 自动判断&自动缓存特殊情况需禁用缓存请传入第二可选参数false:Boolean")
return queryTTF(data)
fun queryBase64TTF(base64: String?): QueryTTF? {
base64DecodeToByteArray(base64)?.let {
return QueryTTF(it)
}
return null
}
/**
* 返回字体解析类
* @param data 支持url,本地文件,base64,ByteArray,自动判断,自动缓存
* @param useCache 可选开关缓存,不传入该值默认开启缓存
* @param str 支持url,本地文件,base64,自动判断,自动缓存
*/
@OptIn(ExperimentalStdlibApi::class)
fun queryTTF(data: Any?, useCache: Boolean): QueryTTF? {
fun queryTTF(str: String?): QueryTTF? {
str ?: return null
try {
var key: String? = null
var qTTF: QueryTTF?
when (data) {
is String -> {
if (useCache) {
key = MessageDigest.getInstance("SHA-256").digest(data.toByteArray()).toHexString()
qTTF = CacheManager.getQueryTTF(key)
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
val key = md5Encode16(str)
var qTTF = CacheManager.getQueryTTF(key)
if (qTTF != null) return qTTF
val font: ByteArray? = when {
str.isAbsUrl() -> AnalyzeUrl(str, source = getSource()).getByteArray()
str.isContentScheme() -> Uri.parse(str).readBytes(appCtx)
str.startsWith("/storage") -> File(str).readBytes()
else -> base64DecodeToByteArray(str)
}
if (key != null) CacheManager.put(key, qTTF)
font ?: return null
qTTF = QueryTTF(font)
CacheManager.put(key, qTTF) // debug注释掉
return qTTF
} catch (e: Exception) {
AppLog.put("[queryTTF] 获取字体处理类出错", e)
AppLog.put("获取字体处理类出错", e)
throw e
}
}
fun queryTTF(data: Any?): QueryTTF? {
return queryTTF(data, true)
}
/**
* @param text 包含错误字体的内容
* @param errorQueryTTF 错误的字体
* @param correctQueryTTF 正确的字体
* @param filter 删除 errorQueryTTF 中不存在的字符
*/
fun replaceFont(
text: String,
@ -828,7 +804,7 @@ interface JsExtensions : JsEncodeUtils {
}
// 删除轮廓数据不存在的字符
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)) {
contentArray[index] = ""
return@forEachIndexed
@ -842,11 +818,6 @@ interface JsExtensions : JsEncodeUtils {
return contentArray.joinToString("")
}
/**
* @param text 包含错误字体的内容
* @param errorQueryTTF 错误的字体
* @param correctQueryTTF 正确的字体
*/
fun replaceFont(
text: String,
errorQueryTTF: QueryTTF?,

View File

@ -685,7 +685,6 @@ public class QueryTTF {
int unicodeInclusive = 0;
int unicodeExclusive = f.glyphIdArray.length;
for (; unicodeInclusive < unicodeExclusive; unicodeInclusive++) {
if (f.glyphIdArray[unicodeInclusive] == 0) continue; // 排除轮廓索引为0的Unicode
unicodeToGlyphId.put(unicodeInclusive, f.glyphIdArray[unicodeInclusive]);
}
break;
@ -712,15 +711,12 @@ public class QueryTTF {
int idDelta = f.idDelta[segmentIndex];
int idRangeOffset = f.idRangeOffsets[segmentIndex];
for (int unicode = unicodeInclusive; unicode <= unicodeExclusive; unicode++) {
int glyphId = 0;
if (idRangeOffset == 0) {
glyphId = (unicode + idDelta) & 0xFFFF;
unicodeToGlyphId.put(unicode, (unicode + idDelta) & 0xFFFF);
} else {
int gIndex = (idRangeOffset / 2) + unicode - unicodeInclusive + segmentIndex - segCount;
if (gIndex < glyphIdArrayLength) glyphId = f.glyphIdArray[gIndex] + idDelta;
unicodeToGlyphId.put(unicode, gIndex < glyphIdArrayLength ? f.glyphIdArray[gIndex] + idDelta : 0);
}
if (glyphId == 0) continue; // 排除轮廓索引为0的Unicode
unicodeToGlyphId.put(unicode, glyphId);
}
}
break;