From c37d597b08a0ef364d2c978fe859eb53a34087f8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 28 Jan 2022 21:19:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/analyzeRule/QueryTTF.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 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 067697a5f..f140c8271 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 @@ -535,17 +535,19 @@ public class QueryTTF { } else if (fmt == 4) { CmapFormat4 tab = (CmapFormat4) table; if (code > tab.endCode[tab.endCode.length - 1]) return 0; - for (int i = 0; i < tab.endCode.length; i++) { - if (tab.endCode[i] == code) { - if (tab.endCode[i] < code) ++i; - if (code < tab.startCode[i]) return 0; - if (tab.idRangeOffset[i] != 0) { - glyfID = tab.glyphIdArray[code - tab.startCode[i] + (tab.idRangeOffset[i] >> 1) - (tab.idRangeOffset.length - i)]; - } else glyfID = code + tab.idDelta[i]; - glyfID &= 0xFFFF; - break; - } + // 二分法查找数值索引 + int start = 0, middle, end = tab.endCode.length - 1; + while (start + 1 < end) { + middle = (start + end) / 2; + if (tab.endCode[middle] <= code) start = middle; + else end = middle; } + if (tab.endCode[start] < code) ++start; + if (code < tab.startCode[start]) return 0; + if (tab.idRangeOffset[start] != 0) { + 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;