Compare commits

...

6 Commits

Author SHA1 Message Date
Antecer
85b1e7b548 优化轮廓索引为0的Unicode排除方式 2024-06-20 17:11:53 +08:00
Antecer
d7ae1d4f0a 解析字体时排除轮廓索引为0的Unicode编码 2024-06-20 16:57:23 +08:00
Antecer
c907ed51b7 增加queryTTF函数可使用的数据类型 2024-06-20 12:09:12 +08:00
Antecer
576a370da1 优化复合字体轮廓数据结构 2024-06-20 10:57:02 +08:00
Antecer
d6c2b5eceb 替换轮廓数据时忽略索引0 2024-06-20 10:54:29 +08:00
Antecer
da7093214f 修复format4字形索引gIndex参数计算错误的BUG 2024-06-19 19:00:47 +08:00
2 changed files with 78 additions and 35 deletions

View File

@ -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,
@ -802,8 +826,9 @@ interface JsExtensions : JsEncodeUtils {
if (errorQueryTTF.isBlankUnicode(oldCode)) { if (errorQueryTTF.isBlankUnicode(oldCode)) {
return@forEachIndexed return@forEachIndexed
} }
val glyf = errorQueryTTF.getGlyfByUnicode(oldCode)
// 删除轮廓数据不存在的字符 // 删除轮廓数据不存在的字符
var glyf = errorQueryTTF.getGlyfByUnicode(oldCode) // 轮廓数据不存在
if (errorQueryTTF.getGlyfIdByUnicode(oldCode) == 0) glyf = null // 轮廓数据指向保留索引0
if (filter && (glyf == null)) { if (filter && (glyf == null)) {
contentArray[index] = "" contentArray[index] = ""
return@forEachIndexed return@forEachIndexed
@ -817,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?,

View File

@ -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; 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;
@ -773,12 +777,12 @@ public class QueryTTF {
var dataTable = directorys.get("glyf"); var dataTable = directorys.get("glyf");
assert dataTable != null; assert dataTable != null;
int glyfCount = maxp.numGlyphs; int glyfCount = maxp.numGlyphs;
glyfArray = new GlyfLayout[glyfCount + 1]; // 创建容器多创建一个作为保留区 glyfArray = new GlyfLayout[glyfCount]; // 创建字形容器
var reader = new BufferReader(buffer, 0); var reader = new BufferReader(buffer, 0);
for (int index = 1; index <= glyfCount; index++) { for (int index = 0; index < glyfCount; index++) {
if (loca[index - 1] == loca[index]) continue; // 当前loca与下一个loca相同表示这个字形不存在 if (loca[index] == loca[index + 1]) continue; // 当前loca与下一个loca相同表示这个字形不存在
int offset = dataTable.offset + loca[index - 1]; int offset = dataTable.offset + loca[index];
// 读GlyphHeaders // 读GlyphHeaders
var glyph = new GlyfLayout(); var glyph = new GlyfLayout();
reader.position(offset); reader.position(offset);
@ -893,7 +897,7 @@ public class QueryTTF {
if ((glyphTableComponent.flags & 0x20) == 0) break; if ((glyphTableComponent.flags & 0x20) == 0) break;
} }
} }
glyfArray[index] = glyph; // 根据文档 glyfId=0 作为保留区使用这里赋值从索引1开始 glyfArray[index] = glyph;
} }
} }
@ -919,7 +923,15 @@ public class QueryTTF {
// 复合字形 // 复合字形
LinkedList<String> glyphIdList = new LinkedList<>(); LinkedList<String> glyphIdList = new LinkedList<>();
for (var g : glyph.glyphComponent) { for (var g : glyph.glyphComponent) {
glyphIdList.add(String.valueOf(g.glyphIndex)); glyphIdList.add("{" +
"flags:" + g.flags + "," +
"glyphIndex:" + g.glyphIndex + "," +
"arg1:" + g.argument1 + "," +
"arg2:" + g.argument2 + "," +
"xScale:" + g.xScale + "," +
"scale01:" + g.scale01 + "," +
"scale10:" + g.scale10 + "," +
"yScale:" + g.yScale + "}");
} }
glyphString = "[" + String.join(",", glyphIdList) + "]"; glyphString = "[" + String.join(",", glyphIdList) + "]";
} }
@ -965,10 +977,11 @@ public class QueryTTF {
int glyfArrayLength = glyfArray.length; int glyfArrayLength = glyfArray.length;
for (var item : unicodeToGlyphId.entrySet()) { for (var item : unicodeToGlyphId.entrySet()) {
int key = item.getKey(); int key = item.getKey();
int val = item.getValue() + 1; // glyphArray已根据TTF文档将索引0作为保留位这里从1开始索引 int val = item.getValue();
if (val >= glyfArrayLength) continue; if (val >= glyfArrayLength) continue;
String glyfString = getGlyfById(val); String glyfString = getGlyfById(val);
unicodeToGlyph.put(key, glyfString); unicodeToGlyph.put(key, glyfString);
if (glyfString == null) continue; // null 不能用作hashmap的key
glyphToUnicode.put(glyfString, key); glyphToUnicode.put(glyfString, key);
} }
// Log.i("QueryTTF", "字体处理完成"); // Log.i("QueryTTF", "字体处理完成");
@ -986,8 +999,8 @@ public class QueryTTF {
*/ */
public int getGlyfIdByUnicode(int unicode) { public int getGlyfIdByUnicode(int unicode) {
var result = unicodeToGlyphId.get(unicode); var result = unicodeToGlyphId.get(unicode);
if (result == null) return 0; if (result == null) return 0; // 如果找不到Unicode对应的轮廓索引就返回默认值0
return result + 1; // 根据TTF文档轮廓索引的定义从1开始 return result;
} }
/** /**
@ -1008,7 +1021,7 @@ public class QueryTTF {
*/ */
public int getUnicodeByGlyf(String glyph) { public int getUnicodeByGlyf(String glyph) {
var result = glyphToUnicode.get(glyph); var result = glyphToUnicode.get(glyph);
if (result == null) return 0; if (result == null) return 0; // 如果轮廓数据找不到对应的Unicode就返回默认值0
return result; return result;
} }