From a61e14c208295d46bb1fe52cd83a72300ec99f72 Mon Sep 17 00:00:00 2001 From: Antecer Date: Sun, 22 Nov 2020 13:22:28 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=9F=A5=E8=AF=A2=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=EF=BC=8C=E9=81=BF=E5=85=8D=E9=87=8D=E5=A4=8D=E9=81=8D?= =?UTF-8?q?=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/analyzeRule/QueryTTF.java | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) 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; } /**