This commit is contained in:
Horis
2023-09-26 20:41:06 +08:00
parent ef3b39b8b6
commit 6a78d6b424
3 changed files with 45 additions and 14 deletions
@@ -90,6 +90,15 @@ object ChapterProvider {
@JvmStatic @JvmStatic
private var titleBottomSpacing = 0 private var titleBottomSpacing = 0
@JvmStatic
private var indentCharWidth = 0f
@JvmStatic
private var titlePaintTextHeight = 0f
@JvmStatic
private var contentPaintTextHeight = 0f
@JvmStatic @JvmStatic
var typeface: Typeface? = Typeface.DEFAULT var typeface: Typeface? = Typeface.DEFAULT
private set private set
@@ -136,6 +145,7 @@ object ChapterProvider {
textPages, textPages,
stringBuilder, stringBuilder,
titlePaint, titlePaint,
titlePaintTextHeight,
isTitle = true, isTitle = true,
emptyContent = contents.isEmpty(), emptyContent = contents.isEmpty(),
isVolumeTitle = bookChapter.isVolume isVolumeTitle = bookChapter.isVolume
@@ -162,7 +172,14 @@ object ChapterProvider {
matcher.appendTail(sb) matcher.appendTail(sb)
text = sb.toString() text = sb.toString()
setTypeText( setTypeText(
book, absStartX, durY, text, textPages, stringBuilder, contentPaint, book,
absStartX,
durY,
text,
textPages,
stringBuilder,
contentPaint,
contentPaintTextHeight,
srcList = srcList srcList = srcList
).let { ).let {
absStartX = it.first absStartX = it.first
@@ -175,7 +192,14 @@ object ChapterProvider {
val text = content.substring(start, matcher.start()) val text = content.substring(start, matcher.start())
if (text.isNotBlank()) { if (text.isNotBlank()) {
setTypeText( setTypeText(
book, absStartX, durY, text, textPages, stringBuilder, contentPaint book,
absStartX,
durY,
text,
textPages,
stringBuilder,
contentPaint,
contentPaintTextHeight
).let { ).let {
absStartX = it.first absStartX = it.first
durY = it.second durY = it.second
@@ -195,7 +219,8 @@ object ChapterProvider {
if (AppConfig.enableReview) text + reviewChar else text, if (AppConfig.enableReview) text + reviewChar else text,
textPages, textPages,
stringBuilder, stringBuilder,
contentPaint contentPaint,
contentPaintTextHeight
).let { ).let {
absStartX = it.first absStartX = it.first
durY = it.second durY = it.second
@@ -305,6 +330,7 @@ object ChapterProvider {
textPages: ArrayList<TextPage>, textPages: ArrayList<TextPage>,
stringBuilder: StringBuilder, stringBuilder: StringBuilder,
textPaint: TextPaint, textPaint: TextPaint,
textHeight: Float,
isTitle: Boolean = false, isTitle: Boolean = false,
emptyContent: Boolean = false, emptyContent: Boolean = false,
isVolumeTitle: Boolean = false, isVolumeTitle: Boolean = false,
@@ -321,10 +347,10 @@ object ChapterProvider {
emptyContent && textPages.size == 1 -> { emptyContent && textPages.size == 1 -> {
val textPage = textPages.last() val textPage = textPages.last()
if (textPage.lineSize == 0) { 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() if (ty > titleTopSpacing) ty else titleTopSpacing.toFloat()
} else { } else {
var textLayoutHeight = layout.lineCount * textPaint.textHeight var textLayoutHeight = layout.lineCount * textHeight
val fistLine = textPage.getLine(0) val fistLine = textPage.getLine(0)
if (fistLine.lineTop < textLayoutHeight + titleTopSpacing) { if (fistLine.lineTop < textLayoutHeight + titleTopSpacing) {
textLayoutHeight = fistLine.lineTop - titleTopSpacing textLayoutHeight = fistLine.lineTop - titleTopSpacing
@@ -430,16 +456,17 @@ object ChapterProvider {
else -> lastLine.paragraphNum else -> lastLine.paragraphNum
} }
textLine.paragraphNum = paragraphNum textLine.paragraphNum = paragraphNum
textLine.chapterPosition = textPages.foldIndexed(sbLength) { index, acc, textPage -> textLine.chapterPosition =
acc + if (index == textPages.lastIndex) 0 else textPage.text.length (textPages.getOrNull(textPages.lastIndex - 1)?.lines?.last()?.run {
} chapterPosition + charSize + if (isParagraphEnd) 1 else 0
} ?: 0) + sbLength
textLine.pagePosition = sbLength textLine.pagePosition = sbLength
textPages.last().addLine(textLine) textPages.last().addLine(textLine)
textLine.upTopBottom(durY, textPaint) textLine.upTopBottom(durY, textPaint)
durY += textPaint.textHeight * lineSpacingExtra durY += textHeight * lineSpacingExtra
textPages.last().height = durY textPages.last().height = durY
} }
durY += textPaint.textHeight * paragraphSpacing / 10f durY += textHeight * paragraphSpacing / 10f
return Pair(absStartX, durY) return Pair(absStartX, durY)
} }
@@ -465,9 +492,8 @@ object ChapterProvider {
return return
} }
val bodyIndent = ReadBookConfig.paragraphIndent val bodyIndent = ReadBookConfig.paragraphIndent
val icw = StaticLayout.getDesiredWidth(bodyIndent, textPaint) / bodyIndent.length
for (char in bodyIndent.toStringArray()) { for (char in bodyIndent.toStringArray()) {
val x1 = x + icw val x1 = x + indentCharWidth
textLine.addColumn( textLine.addColumn(
TextColumn( TextColumn(
charData = char, charData = char,
@@ -649,6 +675,10 @@ object ChapterProvider {
paragraphSpacing = ReadBookConfig.paragraphSpacing paragraphSpacing = ReadBookConfig.paragraphSpacing
titleTopSpacing = ReadBookConfig.titleTopSpacing.dpToPx() titleTopSpacing = ReadBookConfig.titleTopSpacing.dpToPx()
titleBottomSpacing = ReadBookConfig.titleBottomSpacing.dpToPx() titleBottomSpacing = ReadBookConfig.titleBottomSpacing.dpToPx()
val bodyIndent = ReadBookConfig.paragraphIndent
indentCharWidth = StaticLayout.getDesiredWidth(bodyIndent, contentPaint) / bodyIndent.length
titlePaintTextHeight = titlePaint.textHeight
contentPaintTextHeight = contentPaint.textHeight
upLayout() upLayout()
} }
@@ -3,4 +3,4 @@ package io.legado.app.utils
import android.text.TextPaint import android.text.TextPaint
val TextPaint.textHeight: Float val TextPaint.textHeight: Float
get() = fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading get() = fontMetrics.run { descent - ascent + leading }
@@ -53,10 +53,11 @@ public class MediaTypes {
"application/adobe-page-template+xml", ".xpgt"); "application/adobe-page-template+xml", ".xpgt");
public static final MediaType PLS = new MediaType("application/pls+xml", public static final MediaType PLS = new MediaType("application/pls+xml",
".pls"); ".pls");
public static final MediaType UNKNOWN = new MediaType("application/octet-stream", "");
public static final MediaType[] mediaTypes = new MediaType[]{ public static final MediaType[] mediaTypes = new MediaType[]{
XHTML, EPUB, JPG, PNG, GIF, CSS, SVG, TTF, NCX, XPGT, OPENTYPE, WOFF, 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<String, MediaType> mediaTypesByName = new HashMap<>(); public static final Map<String, MediaType> mediaTypesByName = new HashMap<>();