使用HashMap存储字形数据

pull/496/head
Antecer 4 years ago
parent a61e14c208
commit afe9083532
  1. 63
      app/src/main/java/io/legado/app/model/analyzeRule/QueryTTF.java

@ -243,6 +243,9 @@ public class QueryTTF {
Pair.of(0, 1) Pair.of(0, 1)
}; };
public final Map<Integer, String> CodeToGlyph = new HashMap<>();
public final Map<String, Integer> GlyphToCode = new HashMap<>();
/** /**
* 构造函数 * 构造函数
* *
@ -474,16 +477,18 @@ public class QueryTTF {
} }
} }
// 建立Unicode&Glyf映射 // 建立Unicode&Glyph双向
for (int i = 0; i < 130000; ++i) { for (int key = 0; key < 130000; ++key) {
if (i == 0xFF) i = 0x3400; // 屏蔽部分编码 if (key == 0xFF) key = 0x3400;
int gid = GetGlyfIndex(i); int gid = GetGlyfIndex(key);
if (gid == 0) continue; if (gid == 0) continue;
int thisLength = Glyf.get(gid).flags.length; StringBuilder sb = new StringBuilder();
short[] thisGlyf = new short[thisLength << 1]; for (short b : Glyf.get(gid).xCoordinates) sb.append(b);
System.arraycopy(Glyf.get(gid).xCoordinates, 0, thisGlyf, 0, thisLength); for (short b : Glyf.get(gid).yCoordinates) sb.append(b);
System.arraycopy(Glyf.get(gid).yCoordinates, 0, thisGlyf, thisLength, thisLength); String val = sb.toString();
UnicodeMap.put(i, thisGlyf); 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)]; glyfID = tab.glyphIdArray[code - tab.startCode[start] + (tab.idRangeOffset[start] >> 1) - (tab.idRangeOffset.length - start)];
} else glyfID = code + tab.idDelta[start]; } else glyfID = code + tab.idDelta[start];
glyfID &= 0xFFFF; glyfID &= 0xFFFF;
} else if (fmt == 6) { } else if (fmt == 6) {
CmapFormat6 tab = (CmapFormat6) table; CmapFormat6 tab = (CmapFormat6) table;
int index = code - tab.firstCode; int index = code - tab.firstCode;
@ -571,42 +575,23 @@ public class QueryTTF {
return glyfID; return glyfID;
} }
// 缓存查询结果,避免重复遍历
private final Map<Integer, short[]> CacheGlyhps = new HashMap<>();
/** /**
* 使用轮廓数据获取Unicode值 * 使用Unicode值获取轮廓数据
* *
* @param inputGlyf 传入short[]轮廓数组 * @param key 传入Unicode十进制值
* @return 返回Unicode十进制 * @return 返回轮廓数组的String
*/ */
public int GetCodeByGlyf(short[] inputGlyf) { public String GetGlyfByCode(int key) {
if (inputGlyf.length == 0) return 0; return CodeToGlyph.getOrDefault(key, "");
// 优先查询缓存
for (Map.Entry<Integer, short[]> g : CacheGlyhps.entrySet()) {
if (Arrays.equals(inputGlyf, g.getValue())) {
return g.getKey();
}
}
// 缓存内没有就查询完整的字体表
for (Map.Entry<Integer, short[]> 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;
} }
/** /**
* 使用Unicode值获取轮廓数据 * 使用轮廓数据获取Unicode值
* *
* @param code 传入Unicode十进制 * @param val 传入轮廓数组的String值
* @return 返回short[]轮廓数组 * @return 返回Unicode十进制值
*/ */
public short[] GetGlyfByCode(int code) { public int GetCodeByGlyf(String val) {
if (code <= 0) return new short[0]; return GlyphToCode.getOrDefault(val, 0);
return UnicodeMap.getOrDefault(code, new short[0]);
} }
} }

Loading…
Cancel
Save