From ab18495e4cc84c9e9bec677bbf862101761496b0 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 6 May 2021 10:05:39 +0800 Subject: [PATCH 1/6] =?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 | 294 +++++++++--------- 1 file changed, 148 insertions(+), 146 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 9cac2d230..041a6539b 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 @@ -147,19 +147,19 @@ public class QueryTTF { } private static class ByteArrayReader { - public int Index; - public byte[] Buffer; + public int index; + public byte[] buffer; public ByteArrayReader(byte[] buffer, int index) { - Buffer = buffer; - Index = index; + this.buffer = buffer; + this.index = index; } public long ReadUIntX(long len) { long result = 0; for (long i = 0; i < len; ++i) { result <<= 8; - result |= Buffer[Index++] & 0xFF; + result |= buffer[index++] & 0xFF; } return result; } @@ -187,17 +187,17 @@ public class QueryTTF { public String ReadStrings(int len, Charset charset) { byte[] result = len > 0 ? new byte[len] : null; - for (int i = 0; i < len; ++i) result[i] = Buffer[Index++]; + for (int i = 0; i < len; ++i) result[i] = buffer[index++]; return new String(result, charset); } public byte GetByte() { - return Buffer[Index++]; + return buffer[index++]; } public byte[] GetBytes(int len) { byte[] result = len > 0 ? new byte[len] : null; - for (int i = 0; i < len; ++i) result[i] = Buffer[Index++]; + for (int i = 0; i < len; ++i) result[i] = buffer[index++]; return result; } @@ -214,10 +214,10 @@ public class QueryTTF { } } - private final ByteArrayReader FontReader; + private final ByteArrayReader fontReader; private final Header fileHeader = new Header(); - private final List Directorys = new LinkedList<>(); - private final NameLayout Name = new NameLayout(); + private final List directorys = new LinkedList<>(); + private final NameLayout name = new NameLayout(); private final HeadLayout head = new HeadLayout(); private final MaxpLayout maxp = new MaxpLayout(); private final List loca = new LinkedList<>(); @@ -232,10 +232,10 @@ public class QueryTTF { Pair.of(0, 1) }; - public final Map CodeToGlyph = new HashMap<>(); - public final Map GlyphToCode = new HashMap<>(); - private int LimitMix = 0; - private int LimitMax = 0; + public final Map codeToGlyph = new HashMap<>(); + public final Map glyphToCode = new HashMap<>(); + private int limitMix = 0; + private int limitMax = 0; /** * 构造函数 @@ -243,162 +243,162 @@ public class QueryTTF { * @param buffer 传入TTF字体二进制数组 */ public QueryTTF(byte[] buffer) { - FontReader = new ByteArrayReader(buffer, 0); + fontReader = new ByteArrayReader(buffer, 0); // 获取文件头 - fileHeader.majorVersion = FontReader.ReadUInt16(); - fileHeader.minorVersion = FontReader.ReadUInt16(); - fileHeader.numOfTables = FontReader.ReadUInt16(); - fileHeader.searchRange = FontReader.ReadUInt16(); - fileHeader.entrySelector = FontReader.ReadUInt16(); - fileHeader.rangeShift = FontReader.ReadUInt16(); + fileHeader.majorVersion = fontReader.ReadUInt16(); + fileHeader.minorVersion = fontReader.ReadUInt16(); + fileHeader.numOfTables = fontReader.ReadUInt16(); + fileHeader.searchRange = fontReader.ReadUInt16(); + fileHeader.entrySelector = fontReader.ReadUInt16(); + fileHeader.rangeShift = fontReader.ReadUInt16(); // 获取目录 for (int i = 0; i < fileHeader.numOfTables; ++i) { Directory d = new Directory(); - d.tag = FontReader.ReadStrings(4, StandardCharsets.US_ASCII); - d.checkSum = FontReader.ReadUInt32(); - d.offset = FontReader.ReadUInt32(); - d.length = FontReader.ReadUInt32(); - Directorys.add(d); + d.tag = fontReader.ReadStrings(4, StandardCharsets.US_ASCII); + d.checkSum = fontReader.ReadUInt32(); + d.offset = fontReader.ReadUInt32(); + d.length = fontReader.ReadUInt32(); + directorys.add(d); } // 解析表 name (字体信息,包含版权、名称、作者等...) - for (Directory Temp : Directorys) { + for (Directory Temp : directorys) { if (Temp.tag.equals("name")) { - FontReader.Index = Temp.offset; - Name.format = FontReader.ReadUInt16(); - Name.count = FontReader.ReadUInt16(); - Name.stringOffset = FontReader.ReadUInt16(); - for (int i = 0; i < Name.count; ++i) { + fontReader.index = Temp.offset; + name.format = fontReader.ReadUInt16(); + name.count = fontReader.ReadUInt16(); + name.stringOffset = fontReader.ReadUInt16(); + for (int i = 0; i < name.count; ++i) { NameRecord record = new NameRecord(); - record.platformID = FontReader.ReadUInt16(); - record.encodingID = FontReader.ReadUInt16(); - record.languageID = FontReader.ReadUInt16(); - record.nameID = FontReader.ReadUInt16(); - record.length = FontReader.ReadUInt16(); - record.offset = FontReader.ReadUInt16(); - Name.records.add(record); + record.platformID = fontReader.ReadUInt16(); + record.encodingID = fontReader.ReadUInt16(); + record.languageID = fontReader.ReadUInt16(); + record.nameID = fontReader.ReadUInt16(); + record.length = fontReader.ReadUInt16(); + record.offset = fontReader.ReadUInt16(); + name.records.add(record); } } } // 解析表 head (获取 head.indexToLocFormat) - for (Directory Temp : Directorys) { + for (Directory Temp : directorys) { if (Temp.tag.equals("head")) { - FontReader.Index = Temp.offset; - head.majorVersion = FontReader.ReadUInt16(); - head.minorVersion = FontReader.ReadUInt16(); - head.fontRevision = FontReader.ReadUInt32(); - head.checkSumAdjustment = FontReader.ReadUInt32(); - head.magicNumber = FontReader.ReadUInt32(); - head.flags = FontReader.ReadUInt16(); - head.unitsPerEm = FontReader.ReadUInt16(); - head.created = FontReader.ReadUInt64(); - head.modified = FontReader.ReadUInt64(); - head.xMin = FontReader.ReadInt16(); - head.yMin = FontReader.ReadInt16(); - head.xMax = FontReader.ReadInt16(); - head.yMax = FontReader.ReadInt16(); - head.macStyle = FontReader.ReadUInt16(); - head.lowestRecPPEM = FontReader.ReadUInt16(); - head.fontDirectionHint = FontReader.ReadInt16(); - head.indexToLocFormat = FontReader.ReadInt16(); - head.glyphDataFormat = FontReader.ReadInt16(); + fontReader.index = Temp.offset; + head.majorVersion = fontReader.ReadUInt16(); + head.minorVersion = fontReader.ReadUInt16(); + head.fontRevision = fontReader.ReadUInt32(); + head.checkSumAdjustment = fontReader.ReadUInt32(); + head.magicNumber = fontReader.ReadUInt32(); + head.flags = fontReader.ReadUInt16(); + head.unitsPerEm = fontReader.ReadUInt16(); + head.created = fontReader.ReadUInt64(); + head.modified = fontReader.ReadUInt64(); + head.xMin = fontReader.ReadInt16(); + head.yMin = fontReader.ReadInt16(); + head.xMax = fontReader.ReadInt16(); + head.yMax = fontReader.ReadInt16(); + head.macStyle = fontReader.ReadUInt16(); + head.lowestRecPPEM = fontReader.ReadUInt16(); + head.fontDirectionHint = fontReader.ReadInt16(); + head.indexToLocFormat = fontReader.ReadInt16(); + head.glyphDataFormat = fontReader.ReadInt16(); } } // 解析表 maxp (获取 maxp.numGlyphs) - for (Directory Temp : Directorys) { + for (Directory Temp : directorys) { if (Temp.tag.equals("maxp")) { - FontReader.Index = Temp.offset; - maxp.majorVersion = FontReader.ReadUInt16(); - maxp.minorVersion = FontReader.ReadUInt16(); - maxp.numGlyphs = FontReader.ReadUInt16(); - maxp.maxPoints = FontReader.ReadUInt16(); - maxp.maxContours = FontReader.ReadUInt16(); - maxp.maxCompositePoints = FontReader.ReadUInt16(); - maxp.maxCompositeContours = FontReader.ReadUInt16(); - maxp.maxZones = FontReader.ReadUInt16(); - maxp.maxTwilightPoints = FontReader.ReadUInt16(); - maxp.maxStorage = FontReader.ReadUInt16(); - maxp.maxFunctionDefs = FontReader.ReadUInt16(); - maxp.maxInstructionDefs = FontReader.ReadUInt16(); - maxp.maxStackElements = FontReader.ReadUInt16(); - maxp.maxSizeOfInstructions = FontReader.ReadUInt16(); - maxp.maxComponentElements = FontReader.ReadUInt16(); - maxp.maxComponentDepth = FontReader.ReadUInt16(); + fontReader.index = Temp.offset; + maxp.majorVersion = fontReader.ReadUInt16(); + maxp.minorVersion = fontReader.ReadUInt16(); + maxp.numGlyphs = fontReader.ReadUInt16(); + maxp.maxPoints = fontReader.ReadUInt16(); + maxp.maxContours = fontReader.ReadUInt16(); + maxp.maxCompositePoints = fontReader.ReadUInt16(); + maxp.maxCompositeContours = fontReader.ReadUInt16(); + maxp.maxZones = fontReader.ReadUInt16(); + maxp.maxTwilightPoints = fontReader.ReadUInt16(); + maxp.maxStorage = fontReader.ReadUInt16(); + maxp.maxFunctionDefs = fontReader.ReadUInt16(); + maxp.maxInstructionDefs = fontReader.ReadUInt16(); + maxp.maxStackElements = fontReader.ReadUInt16(); + maxp.maxSizeOfInstructions = fontReader.ReadUInt16(); + maxp.maxComponentElements = fontReader.ReadUInt16(); + maxp.maxComponentDepth = fontReader.ReadUInt16(); } } // 解析表 loca (轮廓数据偏移地址表) - for (Directory Temp : Directorys) { + for (Directory Temp : directorys) { if (Temp.tag.equals("loca")) { - FontReader.Index = Temp.offset; + fontReader.index = Temp.offset; int offset = head.indexToLocFormat == 0 ? 2 : 4; for (long i = 0; i < Temp.length; i += offset) { - loca.add(offset == 2 ? FontReader.ReadUInt16() << 1 : FontReader.ReadUInt32()); + loca.add(offset == 2 ? fontReader.ReadUInt16() << 1 : fontReader.ReadUInt32()); } } } // 解析表 cmap (Unicode编码轮廓索引对照表) - for (Directory Temp : Directorys) { + for (Directory Temp : directorys) { if (Temp.tag.equals("cmap")) { - FontReader.Index = Temp.offset; - Cmap.version = FontReader.ReadUInt16(); - Cmap.numTables = FontReader.ReadUInt16(); + fontReader.index = Temp.offset; + Cmap.version = fontReader.ReadUInt16(); + Cmap.numTables = fontReader.ReadUInt16(); for (int i = 0; i < Cmap.numTables; ++i) { CmapRecord record = new CmapRecord(); - record.platformID = FontReader.ReadUInt16(); - record.encodingID = FontReader.ReadUInt16(); - record.offset = FontReader.ReadUInt32(); + record.platformID = fontReader.ReadUInt16(); + record.encodingID = fontReader.ReadUInt16(); + record.offset = fontReader.ReadUInt32(); Cmap.records.add(record); } for (int i = 0; i < Cmap.numTables; ++i) { int fmtOffset = Cmap.records.get(i).offset; - FontReader.Index = Temp.offset + fmtOffset; - int EndIndex = FontReader.Index; + fontReader.index = Temp.offset + fmtOffset; + int EndIndex = fontReader.index; - int format = FontReader.ReadUInt16(); + int format = fontReader.ReadUInt16(); if (Cmap.tables.containsKey(fmtOffset)) continue; if (format == 0) { CmapFormat f = new CmapFormat(); f.format = format; - f.length = FontReader.ReadUInt16(); - f.language = FontReader.ReadUInt16(); - f.glyphIdArray = FontReader.GetBytes(f.length - 6); + f.length = fontReader.ReadUInt16(); + f.language = fontReader.ReadUInt16(); + f.glyphIdArray = fontReader.GetBytes(f.length - 6); Cmap.tables.put(fmtOffset, f); } else if (format == 4) { CmapFormat4 f = new CmapFormat4(); f.format = format; - f.length = FontReader.ReadUInt16(); - f.language = FontReader.ReadUInt16(); - f.segCountX2 = FontReader.ReadUInt16(); + f.length = fontReader.ReadUInt16(); + f.language = fontReader.ReadUInt16(); + f.segCountX2 = fontReader.ReadUInt16(); int segCount = f.segCountX2 >> 1; - f.searchRange = FontReader.ReadUInt16(); - f.entrySelector = FontReader.ReadUInt16(); - f.rangeShift = FontReader.ReadUInt16(); - f.endCode = FontReader.GetUInt16Array(segCount); - f.reservedPad = FontReader.ReadUInt16(); - f.startCode = FontReader.GetUInt16Array(segCount); - f.idDelta = FontReader.GetInt16Array(segCount); - f.idRangeOffset = FontReader.GetUInt16Array(segCount); - f.glyphIdArray = FontReader.GetUInt16Array((EndIndex + f.length - FontReader.Index) >> 1); + f.searchRange = fontReader.ReadUInt16(); + f.entrySelector = fontReader.ReadUInt16(); + f.rangeShift = fontReader.ReadUInt16(); + f.endCode = fontReader.GetUInt16Array(segCount); + f.reservedPad = fontReader.ReadUInt16(); + f.startCode = fontReader.GetUInt16Array(segCount); + f.idDelta = fontReader.GetInt16Array(segCount); + f.idRangeOffset = fontReader.GetUInt16Array(segCount); + f.glyphIdArray = fontReader.GetUInt16Array((EndIndex + f.length - fontReader.index) >> 1); Cmap.tables.put(fmtOffset, f); } else if (format == 6) { CmapFormat6 f = new CmapFormat6(); f.format = format; - f.length = FontReader.ReadUInt16(); - f.language = FontReader.ReadUInt16(); - f.firstCode = FontReader.ReadUInt16(); - f.entryCount = FontReader.ReadUInt16(); - f.glyphIdArray = FontReader.GetUInt16Array(f.entryCount); + f.length = fontReader.ReadUInt16(); + f.language = fontReader.ReadUInt16(); + f.firstCode = fontReader.ReadUInt16(); + f.entryCount = fontReader.ReadUInt16(); + f.glyphIdArray = fontReader.GetUInt16Array(f.entryCount); Cmap.tables.put(fmtOffset, f); } else if (format == 12) { CmapFormat12 f = new CmapFormat12(); f.format = format; - f.reserved = FontReader.ReadUInt16(); - f.length = FontReader.ReadUInt32(); - f.language = FontReader.ReadUInt32(); - f.numGroups = FontReader.ReadUInt32(); + f.reserved = fontReader.ReadUInt16(); + f.length = fontReader.ReadUInt32(); + f.language = fontReader.ReadUInt32(); + f.numGroups = fontReader.ReadUInt32(); f.groups = new ArrayList<>(f.numGroups); for (int n = 0; n < f.numGroups; ++n) { - f.groups.add(Triple.of(FontReader.ReadUInt32(), FontReader.ReadUInt32(), FontReader.ReadUInt32())); + f.groups.add(Triple.of(fontReader.ReadUInt32(), fontReader.ReadUInt32(), fontReader.ReadUInt32())); } Cmap.tables.put(fmtOffset, f); } @@ -406,30 +406,30 @@ public class QueryTTF { } } // 解析表 glyf (字体轮廓数据表) - for (Directory Temp : Directorys) { + for (Directory Temp : directorys) { if (Temp.tag.equals("glyf")) { - FontReader.Index = Temp.offset; + fontReader.index = Temp.offset; for (int i = 0; i < maxp.numGlyphs; ++i) { - FontReader.Index = Temp.offset + loca.get(i); + fontReader.index = Temp.offset + loca.get(i); - short numberOfContours = FontReader.ReadInt16(); + short numberOfContours = fontReader.ReadInt16(); if (numberOfContours > 0) { GlyfLayout g = new GlyfLayout(); g.numberOfContours = numberOfContours; - g.xMin = FontReader.ReadInt16(); - g.yMin = FontReader.ReadInt16(); - g.xMax = FontReader.ReadInt16(); - g.yMax = FontReader.ReadInt16(); - g.endPtsOfContours = FontReader.GetUInt16Array(numberOfContours); - g.instructionLength = FontReader.ReadUInt16(); - g.instructions = FontReader.GetBytes(g.instructionLength); + g.xMin = fontReader.ReadInt16(); + g.yMin = fontReader.ReadInt16(); + g.xMax = fontReader.ReadInt16(); + g.yMax = fontReader.ReadInt16(); + g.endPtsOfContours = fontReader.GetUInt16Array(numberOfContours); + g.instructionLength = fontReader.ReadUInt16(); + g.instructions = fontReader.GetBytes(g.instructionLength); int flagLength = g.endPtsOfContours[g.endPtsOfContours.length - 1] + 1; // 获取轮廓点描述标志 g.flags = new byte[flagLength]; for (int n = 0; n < flagLength; ++n) { - g.flags[n] = FontReader.GetByte(); + g.flags[n] = fontReader.GetByte(); if ((g.flags[n] & 0x08) != 0x00) { - for (int m = FontReader.ReadUInt8(); m > 0; --m) { + for (int m = fontReader.ReadUInt8(); m > 0; --m) { g.flags[++n] = g.flags[n - 1]; } } @@ -439,9 +439,9 @@ public class QueryTTF { for (int n = 0; n < flagLength; ++n) { short same = (short) ((g.flags[n] & 0x10) != 0 ? 1 : -1); if ((g.flags[n] & 0x02) != 0) { - g.xCoordinates[n] = (short) (same * FontReader.ReadUInt8()); + g.xCoordinates[n] = (short) (same * fontReader.ReadUInt8()); } else { - g.xCoordinates[n] = same == 1 ? (short) 0 : FontReader.ReadInt16(); + g.xCoordinates[n] = same == 1 ? (short) 0 : fontReader.ReadInt16(); } } // 获取轮廓点描述y轴相对值 @@ -449,9 +449,9 @@ public class QueryTTF { for (int n = 0; n < flagLength; ++n) { short same = (short) ((g.flags[n] & 0x20) != 0 ? 1 : -1); if ((g.flags[n] & 0x04) != 0) { - g.yCoordinates[n] = (short) (same * FontReader.ReadUInt8()); + g.yCoordinates[n] = (short) (same * fontReader.ReadUInt8()); } else { - g.yCoordinates[n] = same == 1 ? (short) 0 : FontReader.ReadInt16(); + g.yCoordinates[n] = same == 1 ? (short) 0 : fontReader.ReadInt16(); } } // 相对坐标转绝对坐标 @@ -478,11 +478,11 @@ public class QueryTTF { for (short b : glyf.get(gid).xCoordinates) sb.append(b); for (short b : glyf.get(gid).yCoordinates) sb.append(b); String val = sb.toString(); - if (LimitMix == 0) LimitMix = key; - LimitMax = key; - CodeToGlyph.put(key, val); - if (GlyphToCode.containsKey(val)) continue; - GlyphToCode.put(val, key); + if (limitMix == 0) limitMix = key; + limitMax = key; + codeToGlyph.put(key, val); + if (glyphToCode.containsKey(val)) continue; + glyphToCode.put(val, key); } } @@ -493,15 +493,15 @@ public class QueryTTF { * @return 返回查询结果字符串 */ public String getNameById(int nameId) { - for (Directory Temp : Directorys) { + for (Directory Temp : directorys) { if (!Temp.tag.equals("name")) continue; - FontReader.Index = Temp.offset; + fontReader.index = Temp.offset; break; } - for (NameRecord record : Name.records) { + for (NameRecord record : name.records) { if (record.nameID != nameId) continue; - FontReader.Index += Name.stringOffset + record.offset; - return FontReader.ReadStrings(record.length, record.platformID == 1 ? StandardCharsets.UTF_8 : StandardCharsets.UTF_16BE); + fontReader.index += name.stringOffset + record.offset; + return fontReader.ReadStrings(record.length, record.platformID == 1 ? StandardCharsets.UTF_8 : StandardCharsets.UTF_16BE); } return "error"; } @@ -528,6 +528,7 @@ public class QueryTTF { int glyfID = 0; CmapFormat table = Cmap.tables.get(fmtKey); + assert table != null; int fmt = table.format; if (fmt == 0) { if (code < table.glyphIdArray.length) glyfID = table.glyphIdArray[code] & 0xFF; @@ -576,7 +577,7 @@ public class QueryTTF { * @return 返回bool查询结果 */ public boolean inLimit(char code) { - return (LimitMix <= code) && (code < LimitMax); + return (limitMix <= code) && (code < limitMax); } /** @@ -586,7 +587,7 @@ public class QueryTTF { * @return 返回轮廓数组的String值 */ public String getGlyfByCode(int key) { - return CodeToGlyph.getOrDefault(key, ""); + return codeToGlyph.getOrDefault(key, ""); } /** @@ -596,6 +597,7 @@ public class QueryTTF { * @return 返回Unicode十进制值 */ public int getCodeByGlyf(String val) { - return GlyphToCode.getOrDefault(val, 0); + //noinspection ConstantConditions + return glyphToCode.getOrDefault(val, 0); } } From a5dbea26b862eb9926b3412d36e1435c5732fc41 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 6 May 2021 12:13:56 +0800 Subject: [PATCH 2/6] =?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/src/main/java/io/legado/app/help/JsExtensions.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 851660fdf..43fcd60a9 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -318,7 +318,7 @@ interface JsExtensions { if (font1 == null || font2 == null) return text val contentArray = text.toCharArray() contentArray.forEachIndexed { index, s -> - val oldCode = s.toInt() + val oldCode = s.code if (font1.inLimit(s)) { val code = font2.getCodeByGlyf(font1.getGlyfByCode(oldCode)) if (code != 0) contentArray[index] = code.toChar() From 11bf63e6a615400a2a4773dcfdcec9f3f56d1105 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 6 May 2021 17:14:37 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E9=A2=84=E4=B8=8B=E8=BD=BD=E7=AB=A0?= =?UTF-8?q?=E8=8A=82=E5=8F=AF=E8=B0=83=E6=95=B4=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/constant/PreferKey.kt | 2 +- app/src/main/java/io/legado/app/help/AppConfig.kt | 8 ++++++-- app/src/main/java/io/legado/app/service/help/ReadBook.kt | 4 ++-- .../java/io/legado/app/ui/config/OtherConfigFragment.kt | 8 ++++++++ app/src/main/java/io/legado/app/ui/main/MainViewModel.kt | 7 +++---- app/src/main/res/values-zh-rHK/strings.xml | 2 +- app/src/main/res/values-zh-rTW/strings.xml | 2 +- app/src/main/res/values-zh/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- app/src/main/res/xml/pref_config_other.xml | 5 ++--- 10 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 651830256..bd0e86fc6 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -66,7 +66,7 @@ object PreferKey { const val importKeepName = "importKeepName" const val screenOrientation = "screenOrientation" const val syncBookProgress = "syncBookProgress" - const val preDownload = "preDownload" + const val preDownloadNum = "preDownloadNum" const val autoRefresh = "auto_refresh" const val defaultToRead = "defaultToRead" const val exportCharset = "exportCharset" diff --git a/app/src/main/java/io/legado/app/help/AppConfig.kt b/app/src/main/java/io/legado/app/help/AppConfig.kt index cda42eed4..a30f5c614 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -184,14 +184,18 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { val syncBookProgress get() = appCtx.getPrefBoolean(PreferKey.syncBookProgress, true) - val preDownload get() = appCtx.getPrefBoolean(PreferKey.preDownload, true) + var preDownloadNum + get() = appCtx.getPrefInt(PreferKey.preDownloadNum, 10) + set(value) { + appCtx.putPrefInt(PreferKey.preDownloadNum, value) + } val mediaButtonOnExit get() = appCtx.getPrefBoolean("mediaButtonOnExit", true) val replaceEnableDefault get() = appCtx.getPrefBoolean(PreferKey.replaceEnableDefault, true) val fullScreenGesturesSupport: Boolean - get () = appCtx.getPrefBoolean(PreferKey.fullScreenGesturesSupport, false) + get() = appCtx.getPrefBoolean(PreferKey.fullScreenGesturesSupport, false) private fun getPrefUserAgent(): String { val ua = appCtx.getPrefString(PreferKey.userAgent) diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index f1f33f4ed..ffb4c8f9b 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -137,7 +137,7 @@ object ReadBook { callBack?.upContent() } loadContent(durChapterIndex.plus(1), upContent, false) - if (AppConfig.preDownload) { + if (AppConfig.preDownloadNum) { GlobalScope.launch(Dispatchers.IO) { for (i in 2..9) { delay(1000) @@ -169,7 +169,7 @@ object ReadBook { callBack?.upContent() } loadContent(durChapterIndex.minus(1), upContent, false) - if (AppConfig.preDownload) { + if (AppConfig.preDownloadNum) { GlobalScope.launch(Dispatchers.IO) { for (i in 2..9) { delay(1000) diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index 9c09e7620..d5d0b1364 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -72,6 +72,14 @@ class OtherConfigFragment : BasePreferenceFragment(), override fun onPreferenceTreeClick(preference: Preference?): Boolean { when (preference?.key) { PreferKey.userAgent -> showUserAgentDialog() + PreferKey.preDownloadNum -> NumberPickerDialog(requireContext()) + .setTitle(getString(R.string.pre_download)) + .setMaxValue(9999) + .setMinValue(1) + .setValue(AppConfig.preDownloadNum) + .show { + AppConfig.preDownloadNum = it + } PreferKey.threadCount -> NumberPickerDialog(requireContext()) .setTitle(getString(R.string.threads_num_title)) .setMaxValue(999) diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 27a140167..48afc90ce 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -84,9 +84,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) { appDb.bookDao.update(book) appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.insert(*toc.toTypedArray()) - if (AppConfig.preDownload) { - cacheBook(webBook, book) - } + cacheBook(webBook, book) }.onError { it.printStackTrace() }.onFinally { @@ -114,7 +112,8 @@ class MainViewModel(application: Application) : BaseViewModel(application) { private fun cacheBook(webBook: WebBook, book: Book) { execute { if (book.totalChapterNum > book.durChapterIndex) { - val downloadToIndex = min(book.totalChapterNum, book.durChapterIndex.plus(10)) + val downloadToIndex = + min(book.totalChapterNum, book.durChapterIndex.plus(AppConfig.preDownloadNum)) for (i in book.durChapterIndex until downloadToIndex) { appDb.bookChapterDao.getChapter(book.bookUrl, i)?.let { chapter -> if (!BookHelp.hasContent(book, chapter)) { diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index cab096ed7..7ae5fa882 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -800,7 +800,7 @@ 导出书单 导入书单 预下载 - 预先下载10章正文 + 预先下载%d章正文 启用排序 背景图片 导出文件夹 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 99f5030d0..486ec2e49 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -804,7 +804,7 @@ 匯出書單 匯入書單 預下載 - 預先下載10章正文 + 預先下載%d章正文 啟用排序 背景圖片 匯出資料夾 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 240704cac..f29989897 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -804,7 +804,7 @@ 导出书单 导入书单 预下载 - 预先下载10章正文 + 预先下载%d章正文 启用排序 背景图片 导出文件夹 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bd230bc1f..ce9bfc9fa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -805,7 +805,7 @@ 导出书单 导入书单 预下载 - 预先下载10章正文 + 预先下载%d章正文 启用排序 背景图片 导出文件夹 diff --git a/app/src/main/res/xml/pref_config_other.xml b/app/src/main/res/xml/pref_config_other.xml index 66a9cc4fa..8dadf0940 100644 --- a/app/src/main/res/xml/pref_config_other.xml +++ b/app/src/main/res/xml/pref_config_other.xml @@ -61,9 +61,8 @@ android:title="@string/high_brush_title" android:summary="@string/high_brush_summary" /> - From ff6f4bf6eedb29cea3e83a0e6ad961db609f8513 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 6 May 2021 17:20:17 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E9=A2=84=E4=B8=8B=E8=BD=BD=E7=AB=A0?= =?UTF-8?q?=E8=8A=82=E5=8F=AF=E8=B0=83=E6=95=B4=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/ui/config/OtherConfigFragment.kt | 6 ++++++ app/src/main/res/values-zh-rHK/strings.xml | 2 +- app/src/main/res/values-zh-rTW/strings.xml | 2 +- app/src/main/res/values-zh/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index d5d0b1364..527d84732 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -53,6 +53,7 @@ class OtherConfigFragment : BasePreferenceFragment(), putPrefBoolean(PreferKey.processText, isProcessTextEnabled()) addPreferencesFromResource(R.xml.pref_config_other) upPreferenceSummary(PreferKey.userAgent, AppConfig.userAgent) + upPreferenceSummary(PreferKey.preDownloadNum, AppConfig.preDownloadNum.toString()) upPreferenceSummary(PreferKey.threadCount, AppConfig.threadCount.toString()) upPreferenceSummary(PreferKey.webPort, webPort.toString()) upPreferenceSummary(PreferKey.defaultCover, getPrefString(PreferKey.defaultCover)) @@ -114,6 +115,9 @@ class OtherConfigFragment : BasePreferenceFragment(), override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { when (key) { + PreferKey.preDownloadNum -> { + upPreferenceSummary(key, AppConfig.preDownloadNum.toString()) + } PreferKey.threadCount -> { upPreferenceSummary(key, AppConfig.threadCount.toString()) postEvent(PreferKey.threadCount, "") @@ -149,6 +153,8 @@ class OtherConfigFragment : BasePreferenceFragment(), private fun upPreferenceSummary(preferenceKey: String, value: String?) { val preference = findPreference(preferenceKey) ?: return when (preferenceKey) { + PreferKey.preDownloadNum -> preference.summary = + getString(R.string.pre_download_s, value) PreferKey.threadCount -> preference.summary = getString(R.string.threads_num, value) PreferKey.webPort -> preference.summary = getString(R.string.web_port_summary, value) else -> if (preference is ListPreference) { diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 7ae5fa882..9e66af1f0 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -800,7 +800,7 @@ 导出书单 导入书单 预下载 - 预先下载%d章正文 + 预先下载%s章正文 启用排序 背景图片 导出文件夹 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 486ec2e49..de3414c63 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -804,7 +804,7 @@ 匯出書單 匯入書單 預下載 - 預先下載%d章正文 + 預先下載%s章正文 啟用排序 背景圖片 匯出資料夾 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index f29989897..e25c7283c 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -804,7 +804,7 @@ 导出书单 导入书单 预下载 - 预先下载%d章正文 + 预先下载%s章正文 启用排序 背景图片 导出文件夹 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ce9bfc9fa..2b302583a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -805,7 +805,7 @@ 导出书单 导入书单 预下载 - 预先下载%d章正文 + 预先下载%s章正文 启用排序 背景图片 导出文件夹 From 5b40199891dedd2fcd5177c1cce64d8b854d3f26 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 6 May 2021 17:28:20 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E9=A2=84=E4=B8=8B=E8=BD=BD=E7=AB=A0?= =?UTF-8?q?=E8=8A=82=E5=8F=AF=E8=B0=83=E6=95=B4=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/service/help/ReadBook.kt | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index ffb4c8f9b..0eef72768 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -21,6 +21,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import splitties.init.appCtx +import kotlin.math.min @Suppress("MemberVisibilityCanBePrivate") @@ -137,12 +138,12 @@ object ReadBook { callBack?.upContent() } loadContent(durChapterIndex.plus(1), upContent, false) - if (AppConfig.preDownloadNum) { - GlobalScope.launch(Dispatchers.IO) { - for (i in 2..9) { - delay(1000) - download(durChapterIndex + i) - } + GlobalScope.launch(Dispatchers.IO) { + val maxChapterIndex = + min(chapterSize, durChapterIndex + AppConfig.preDownloadNum) + for (i in 2 until maxChapterIndex) { + delay(1000) + download(durChapterIndex + i) } } } @@ -169,12 +170,12 @@ object ReadBook { callBack?.upContent() } loadContent(durChapterIndex.minus(1), upContent, false) - if (AppConfig.preDownloadNum) { - GlobalScope.launch(Dispatchers.IO) { - for (i in 2..9) { - delay(1000) - download(durChapterIndex - i) - } + GlobalScope.launch(Dispatchers.IO) { + val maxChapterIndex = + min(chapterSize, durChapterIndex + AppConfig.preDownloadNum) + for (i in 2 until maxChapterIndex) { + delay(1000) + download(durChapterIndex - i) } } } From 05895aa4b887c71e971f038fb1de51c9568d0f4d Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 6 May 2021 17:30:09 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E9=A2=84=E4=B8=8B=E8=BD=BD=E7=AB=A0?= =?UTF-8?q?=E8=8A=82=E5=8F=AF=E8=B0=83=E6=95=B4=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/service/help/ReadBook.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index 0eef72768..88e7e6fa3 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -140,8 +140,8 @@ object ReadBook { loadContent(durChapterIndex.plus(1), upContent, false) GlobalScope.launch(Dispatchers.IO) { val maxChapterIndex = - min(chapterSize, durChapterIndex + AppConfig.preDownloadNum) - for (i in 2 until maxChapterIndex) { + min(chapterSize - 1, durChapterIndex + AppConfig.preDownloadNum) + for (i in 2..maxChapterIndex) { delay(1000) download(durChapterIndex + i) } @@ -172,8 +172,8 @@ object ReadBook { loadContent(durChapterIndex.minus(1), upContent, false) GlobalScope.launch(Dispatchers.IO) { val maxChapterIndex = - min(chapterSize, durChapterIndex + AppConfig.preDownloadNum) - for (i in 2 until maxChapterIndex) { + min(chapterSize - 1, durChapterIndex + AppConfig.preDownloadNum) + for (i in 2..maxChapterIndex) { delay(1000) download(durChapterIndex - i) }