From afe9083532abe24a8189cad218db7ae1668cab2e Mon Sep 17 00:00:00 2001 From: Antecer Date: Sun, 22 Nov 2020 17:01:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8HashMap=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E5=AD=97=E5=BD=A2=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/analyzeRule/QueryTTF.java | 63 +++++++------------ 1 file changed, 24 insertions(+), 39 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 4a6152b74..8a67538eb 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 @@ -243,6 +243,9 @@ public class QueryTTF { Pair.of(0, 1) }; + public final Map CodeToGlyph = new HashMap<>(); + public final Map GlyphToCode = new HashMap<>(); + /** * 构造函数 * @@ -474,16 +477,18 @@ public class QueryTTF { } } - // 建立Unicode&Glyf映射表 - for (int i = 0; i < 130000; ++i) { - if (i == 0xFF) i = 0x3400; // 屏蔽部分编码 - int gid = GetGlyfIndex(i); + // 建立Unicode&Glyph双向表 + for (int key = 0; key < 130000; ++key) { + if (key == 0xFF) key = 0x3400; + int gid = GetGlyfIndex(key); if (gid == 0) continue; - int thisLength = Glyf.get(gid).flags.length; - short[] thisGlyf = new short[thisLength << 1]; - System.arraycopy(Glyf.get(gid).xCoordinates, 0, thisGlyf, 0, thisLength); - System.arraycopy(Glyf.get(gid).yCoordinates, 0, thisGlyf, thisLength, thisLength); - UnicodeMap.put(i, thisGlyf); + StringBuilder sb = new StringBuilder(); + for (short b : Glyf.get(gid).xCoordinates) sb.append(b); + for (short b : Glyf.get(gid).yCoordinates) sb.append(b); + String val = sb.toString(); + CodeToGlyph.put(key, val); + if(GlyphToCode.containsKey(val)) continue; + GlyphToCode.put(val, key); } } @@ -548,7 +553,6 @@ public class QueryTTF { glyfID = tab.glyphIdArray[code - tab.startCode[start] + (tab.idRangeOffset[start] >> 1) - (tab.idRangeOffset.length - start)]; } else glyfID = code + tab.idDelta[start]; glyfID &= 0xFFFF; - } else if (fmt == 6) { CmapFormat6 tab = (CmapFormat6) table; int index = code - tab.firstCode; @@ -571,42 +575,23 @@ public class QueryTTF { return glyfID; } - // 缓存查询结果,避免重复遍历 - private final Map CacheGlyhps = new HashMap<>(); - /** - * 使用轮廓数据获取Unicode值 + * 使用Unicode值获取轮廓数据 * - * @param inputGlyf 传入short[]轮廓数组 - * @return 返回Unicode十进制值 + * @param key 传入Unicode十进制值 + * @return 返回轮廓数组的String值 */ - public int GetCodeByGlyf(short[] inputGlyf) { - 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; - if (Arrays.equals(inputGlyf, g.getValue())) { - CacheGlyhps.put(g.getKey(), inputGlyf); - return g.getKey(); - } - } - return 0; + public String GetGlyfByCode(int key) { + return CodeToGlyph.getOrDefault(key, ""); } /** - * 使用Unicode值获取轮廓数据 + * 使用轮廓数据获取Unicode值 * - * @param code 传入Unicode十进制值 - * @return 返回short[]轮廓数组 + * @param val 传入轮廓数组的String值 + * @return 返回Unicode十进制值 */ - public short[] GetGlyfByCode(int code) { - if (code <= 0) return new short[0]; - return UnicodeMap.getOrDefault(code, new short[0]); + public int GetCodeByGlyf(String val) { + return GlyphToCode.getOrDefault(val, 0); } }