diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/QueryTTF.java b/app/src/main/java/io/legado/app/model/analyzeRule/QueryTTF.java index bc90afb1f..4a6152b74 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/QueryTTF.java +++ b/app/src/main/java/io/legado/app/model/analyzeRule/QueryTTF.java @@ -7,7 +7,9 @@ import org.apache.commons.lang3.tuple.Triple; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -231,7 +233,7 @@ public class QueryTTF { private final List Loca = new LinkedList<>(); private final CmapLayout Cmap = new CmapLayout(); private final List Glyf = new LinkedList<>(); - private final Map UnicodeMap = new HashMap<>(); + private final Map UnicodeMap = new LinkedHashMap<>(); private final Pair[] pps = new Pair[]{ Pair.of(3, 10), Pair.of(0, 4), @@ -529,8 +531,7 @@ public class QueryTTF { CmapFormat table = Cmap.tables.get(fmtKey); int fmt = table.format; if (fmt == 0) { - if (code >= table.glyphIdArray.length) glyfID = 0; - else glyfID = table.glyphIdArray[code] & 0xFF; + if (code < table.glyphIdArray.length) glyfID = table.glyphIdArray[code] & 0xFF; } else if (fmt == 4) { CmapFormat4 tab = (CmapFormat4) table; if (code > tab.endCode[tab.endCode.length - 1]) return 0; @@ -570,6 +571,9 @@ public class QueryTTF { return glyfID; } + // 缓存查询结果,避免重复遍历 + private final Map CacheGlyhps = new HashMap<>(); + /** * 使用轮廓数据获取Unicode值 * @@ -577,23 +581,22 @@ public class QueryTTF { * @return 返回Unicode十进制值 */ public int GetCodeByGlyf(short[] inputGlyf) { - int unicodeVal = 0; + if (inputGlyf.length == 0) return 0; + // 优先查询缓存 + for (Map.Entry g : CacheGlyhps.entrySet()) { + if (Arrays.equals(inputGlyf, g.getValue())) { + return g.getKey(); + } + } + // 缓存内没有就查询完整的字体表 for (Map.Entry g : UnicodeMap.entrySet()) { if (inputGlyf.length != g.getValue().length) continue; - int i = inputGlyf.length; - while (i > 0) { - --i; - if (inputGlyf[i] != g.getValue()[i]) { - ++i; - break; - } - } - if (i == 0) { - unicodeVal = g.getKey(); - break; + if (Arrays.equals(inputGlyf, g.getValue())) { + CacheGlyhps.put(g.getKey(), inputGlyf); + return g.getKey(); } } - return unicodeVal; + return 0; } /**