From b5bea5e3c4948487330119b86d9dbe10bf36c8f0 Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Tue, 26 Sep 2023 20:41:06 +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 --- .../read/page/provider/ChapterProvider.kt | 54 ++++++++++++++----- .../io/legado/app/utils/PaintExtensions.kt | 2 +- .../me/ag2s/epublib/domain/MediaTypes.java | 3 +- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 523ac5621..a06c75172 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -90,6 +90,15 @@ object ChapterProvider { @JvmStatic private var titleBottomSpacing = 0 + @JvmStatic + private var indentCharWidth = 0f + + @JvmStatic + private var titlePaintTextHeight = 0f + + @JvmStatic + private var contentPaintTextHeight = 0f + @JvmStatic var typeface: Typeface? = Typeface.DEFAULT private set @@ -136,6 +145,7 @@ object ChapterProvider { textPages, stringBuilder, titlePaint, + titlePaintTextHeight, isTitle = true, emptyContent = contents.isEmpty(), isVolumeTitle = bookChapter.isVolume @@ -162,7 +172,14 @@ object ChapterProvider { matcher.appendTail(sb) text = sb.toString() setTypeText( - book, absStartX, durY, text, textPages, stringBuilder, contentPaint, + book, + absStartX, + durY, + text, + textPages, + stringBuilder, + contentPaint, + contentPaintTextHeight, srcList = srcList ).let { absStartX = it.first @@ -175,7 +192,14 @@ object ChapterProvider { val text = content.substring(start, matcher.start()) if (text.isNotBlank()) { setTypeText( - book, absStartX, durY, text, textPages, stringBuilder, contentPaint + book, + absStartX, + durY, + text, + textPages, + stringBuilder, + contentPaint, + contentPaintTextHeight ).let { absStartX = it.first durY = it.second @@ -195,7 +219,8 @@ object ChapterProvider { if (AppConfig.enableReview) text + reviewChar else text, textPages, stringBuilder, - contentPaint + contentPaint, + contentPaintTextHeight ).let { absStartX = it.first durY = it.second @@ -305,6 +330,7 @@ object ChapterProvider { textPages: ArrayList, stringBuilder: StringBuilder, textPaint: TextPaint, + textHeight: Float, isTitle: Boolean = false, emptyContent: Boolean = false, isVolumeTitle: Boolean = false, @@ -321,10 +347,10 @@ object ChapterProvider { emptyContent && textPages.size == 1 -> { val textPage = textPages.last() if (textPage.lineSize == 0) { - val ty = (visibleHeight - layout.lineCount * textPaint.textHeight) / 2 + val ty = (visibleHeight - layout.lineCount * textHeight) / 2 if (ty > titleTopSpacing) ty else titleTopSpacing.toFloat() } else { - var textLayoutHeight = layout.lineCount * textPaint.textHeight + var textLayoutHeight = layout.lineCount * textHeight val fistLine = textPage.getLine(0) if (fistLine.lineTop < textLayoutHeight + titleTopSpacing) { textLayoutHeight = fistLine.lineTop - titleTopSpacing @@ -430,16 +456,17 @@ object ChapterProvider { else -> lastLine.paragraphNum } textLine.paragraphNum = paragraphNum - textLine.chapterPosition = textPages.foldIndexed(sbLength) { index, acc, textPage -> - acc + if (index == textPages.lastIndex) 0 else textPage.text.length - } + textLine.chapterPosition = + (textPages.getOrNull(textPages.lastIndex - 1)?.lines?.last()?.run { + chapterPosition + charSize + if (isParagraphEnd) 1 else 0 + } ?: 0) + sbLength textLine.pagePosition = sbLength textPages.last().addLine(textLine) textLine.upTopBottom(durY, textPaint) - durY += textPaint.textHeight * lineSpacingExtra + durY += textHeight * lineSpacingExtra textPages.last().height = durY } - durY += textPaint.textHeight * paragraphSpacing / 10f + durY += textHeight * paragraphSpacing / 10f return Pair(absStartX, durY) } @@ -465,9 +492,8 @@ object ChapterProvider { return } val bodyIndent = ReadBookConfig.paragraphIndent - val icw = StaticLayout.getDesiredWidth(bodyIndent, textPaint) / bodyIndent.length for (char in bodyIndent.toStringArray()) { - val x1 = x + icw + val x1 = x + indentCharWidth textLine.addColumn( TextColumn( charData = char, @@ -649,6 +675,10 @@ object ChapterProvider { paragraphSpacing = ReadBookConfig.paragraphSpacing titleTopSpacing = ReadBookConfig.titleTopSpacing.dpToPx() titleBottomSpacing = ReadBookConfig.titleBottomSpacing.dpToPx() + val bodyIndent = ReadBookConfig.paragraphIndent + indentCharWidth = StaticLayout.getDesiredWidth(bodyIndent, contentPaint) / bodyIndent.length + titlePaintTextHeight = titlePaint.textHeight + contentPaintTextHeight = contentPaint.textHeight upLayout() } diff --git a/app/src/main/java/io/legado/app/utils/PaintExtensions.kt b/app/src/main/java/io/legado/app/utils/PaintExtensions.kt index 5feccd464..c48835ef8 100644 --- a/app/src/main/java/io/legado/app/utils/PaintExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/PaintExtensions.kt @@ -3,4 +3,4 @@ package io.legado.app.utils import android.text.TextPaint val TextPaint.textHeight: Float - get() = fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading \ No newline at end of file + get() = fontMetrics.run { descent - ascent + leading } diff --git a/modules/book/src/main/java/me/ag2s/epublib/domain/MediaTypes.java b/modules/book/src/main/java/me/ag2s/epublib/domain/MediaTypes.java index 630545136..ff6233d15 100644 --- a/modules/book/src/main/java/me/ag2s/epublib/domain/MediaTypes.java +++ b/modules/book/src/main/java/me/ag2s/epublib/domain/MediaTypes.java @@ -53,10 +53,11 @@ public class MediaTypes { "application/adobe-page-template+xml", ".xpgt"); public static final MediaType PLS = new MediaType("application/pls+xml", ".pls"); + public static final MediaType UNKNOWN = new MediaType("application/octet-stream", ""); public static final MediaType[] mediaTypes = new MediaType[]{ XHTML, EPUB, JPG, PNG, GIF, CSS, SVG, TTF, NCX, XPGT, OPENTYPE, WOFF, - SMIL, PLS, JAVASCRIPT, MP3, MP4, OGG + SMIL, PLS, JAVASCRIPT, MP3, MP4, OGG, UNKNOWN }; public static final Map mediaTypesByName = new HashMap<>();