|
|
@ -28,7 +28,7 @@ object ChapterProvider { |
|
|
|
var visibleHeight = 0 |
|
|
|
var visibleHeight = 0 |
|
|
|
var visibleRight = 0 |
|
|
|
var visibleRight = 0 |
|
|
|
var visibleBottom = 0 |
|
|
|
var visibleBottom = 0 |
|
|
|
private var lineSpacingExtra = 0f |
|
|
|
private var lineSpacingExtra = 0 |
|
|
|
private var paragraphSpacing = 0 |
|
|
|
private var paragraphSpacing = 0 |
|
|
|
var typeface: Typeface = Typeface.SANS_SERIF |
|
|
|
var typeface: Typeface = Typeface.SANS_SERIF |
|
|
|
var titlePaint = TextPaint() |
|
|
|
var titlePaint = TextPaint() |
|
|
@ -51,7 +51,7 @@ object ChapterProvider { |
|
|
|
val pageLengths = arrayListOf<Int>() |
|
|
|
val pageLengths = arrayListOf<Int>() |
|
|
|
val stringBuilder = StringBuilder() |
|
|
|
val stringBuilder = StringBuilder() |
|
|
|
var surplusText = content |
|
|
|
var surplusText = content |
|
|
|
var durY = 0 |
|
|
|
var durY = 0f |
|
|
|
textPages.add(TextPage()) |
|
|
|
textPages.add(TextPage()) |
|
|
|
while (surplusText.isNotEmpty()) { |
|
|
|
while (surplusText.isNotEmpty()) { |
|
|
|
if (textPages.first().textLines.isEmpty()) { |
|
|
|
if (textPages.first().textLines.isEmpty()) { |
|
|
@ -60,7 +60,9 @@ object ChapterProvider { |
|
|
|
if (end > 0) { |
|
|
|
if (end > 0) { |
|
|
|
val title = surplusText.substring(0, end) |
|
|
|
val title = surplusText.substring(0, end) |
|
|
|
surplusText = surplusText.substring(end + 1) |
|
|
|
surplusText = surplusText.substring(end + 1) |
|
|
|
durY = joinTitle(title, durY, textPages, pageLines, pageLengths, stringBuilder) |
|
|
|
durY = setTypeText( |
|
|
|
|
|
|
|
title, durY, textPages, pageLines, pageLengths, stringBuilder, true |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
//正文 |
|
|
|
//正文 |
|
|
@ -73,7 +75,8 @@ object ChapterProvider { |
|
|
|
text = surplusText |
|
|
|
text = surplusText |
|
|
|
surplusText = "" |
|
|
|
surplusText = "" |
|
|
|
} |
|
|
|
} |
|
|
|
durY = joinBody(text, durY, textPages, pageLines, pageLengths, stringBuilder) |
|
|
|
durY = |
|
|
|
|
|
|
|
setTypeText(text, durY, textPages, pageLines, pageLengths, stringBuilder, false) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
textPages.last().height = durY + 20.dp |
|
|
|
textPages.last().height = durY + 20.dp |
|
|
@ -103,116 +106,59 @@ object ChapterProvider { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 标题 |
|
|
|
* 排版文字 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun joinTitle( |
|
|
|
private fun setTypeText( |
|
|
|
title: String, |
|
|
|
|
|
|
|
y: Int, |
|
|
|
|
|
|
|
textPages: ArrayList<TextPage>, |
|
|
|
|
|
|
|
pageLines: ArrayList<Int>, |
|
|
|
|
|
|
|
pageLengths: ArrayList<Int>, |
|
|
|
|
|
|
|
stringBuilder: StringBuilder |
|
|
|
|
|
|
|
): Int { |
|
|
|
|
|
|
|
var durY = y |
|
|
|
|
|
|
|
val layout = StaticLayout( |
|
|
|
|
|
|
|
title, titlePaint, visibleWidth, |
|
|
|
|
|
|
|
Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, true |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
for (lineIndex in 0 until layout.lineCount) { |
|
|
|
|
|
|
|
textPages.last().height = durY |
|
|
|
|
|
|
|
val textLine = TextLine(isTitle = true) |
|
|
|
|
|
|
|
if (durY + layout.getLineBaseline(lineIndex) - layout.getLineTop(lineIndex) |
|
|
|
|
|
|
|
+ contentPaint.fontMetrics.descent < visibleHeight |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
textPages.last().textLines.add(textLine) |
|
|
|
|
|
|
|
durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
textPages.last().text = stringBuilder.toString() |
|
|
|
|
|
|
|
stringBuilder.clear() |
|
|
|
|
|
|
|
pageLines.add(textPages.last().textLines.size) |
|
|
|
|
|
|
|
pageLengths.add(textPages.last().text.length) |
|
|
|
|
|
|
|
//新页面 |
|
|
|
|
|
|
|
durY = layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) |
|
|
|
|
|
|
|
textPages.add(TextPage()) |
|
|
|
|
|
|
|
textPages.last().textLines.add(textLine) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
textLine.lineTop = (paddingTop + durY - |
|
|
|
|
|
|
|
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))).toFloat() |
|
|
|
|
|
|
|
textLine.lineBase = (paddingTop + durY - |
|
|
|
|
|
|
|
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))).toFloat() |
|
|
|
|
|
|
|
textLine.lineBottom = textLine.lineBase + contentPaint.fontMetrics.descent |
|
|
|
|
|
|
|
val words = |
|
|
|
|
|
|
|
title.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) |
|
|
|
|
|
|
|
stringBuilder.append(words) |
|
|
|
|
|
|
|
textLine.text = words |
|
|
|
|
|
|
|
val desiredWidth = layout.getLineWidth(lineIndex) |
|
|
|
|
|
|
|
if (lineIndex != layout.lineCount - 1) { |
|
|
|
|
|
|
|
addCharsToLineMiddle(textLine, words, titlePaint, desiredWidth, 0f) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
//最后一行 |
|
|
|
|
|
|
|
val x = if (ReadBookConfig.titleCenter) |
|
|
|
|
|
|
|
(visibleWidth - layout.getLineWidth(lineIndex)) / 2 |
|
|
|
|
|
|
|
else 0f |
|
|
|
|
|
|
|
addCharsToLineLast(textLine, words, stringBuilder, titlePaint, x) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
durY += paragraphSpacing |
|
|
|
|
|
|
|
return durY |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 正文 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private fun joinBody( |
|
|
|
|
|
|
|
text: String, |
|
|
|
text: String, |
|
|
|
y: Int, |
|
|
|
y: Float, |
|
|
|
textPages: ArrayList<TextPage>, |
|
|
|
textPages: ArrayList<TextPage>, |
|
|
|
pageLines: ArrayList<Int>, |
|
|
|
pageLines: ArrayList<Int>, |
|
|
|
pageLengths: ArrayList<Int>, |
|
|
|
pageLengths: ArrayList<Int>, |
|
|
|
stringBuilder: StringBuilder |
|
|
|
stringBuilder: StringBuilder, |
|
|
|
): Int { |
|
|
|
isTitle: Boolean |
|
|
|
|
|
|
|
): Float { |
|
|
|
var durY = y |
|
|
|
var durY = y |
|
|
|
|
|
|
|
val textPaint = if (isTitle) titlePaint else contentPaint |
|
|
|
val layout = StaticLayout( |
|
|
|
val layout = StaticLayout( |
|
|
|
text, contentPaint, visibleWidth, |
|
|
|
text, textPaint, visibleWidth, |
|
|
|
Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, true |
|
|
|
Layout.Alignment.ALIGN_NORMAL, 0f, 0f, true |
|
|
|
) |
|
|
|
) |
|
|
|
for (lineIndex in 0 until layout.lineCount) { |
|
|
|
for (lineIndex in 0 until layout.lineCount) { |
|
|
|
textPages.last().height = durY |
|
|
|
textPages.last().height = durY |
|
|
|
val textLine = TextLine() |
|
|
|
val textLine = TextLine(isTitle = isTitle) |
|
|
|
if (durY + layout.getLineBaseline(lineIndex) - layout.getLineTop(lineIndex) |
|
|
|
if (durY + textPaint.textHeight < visibleHeight) { |
|
|
|
+ contentPaint.fontMetrics.descent < visibleHeight |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
textPages.last().textLines.add(textLine) |
|
|
|
textPages.last().textLines.add(textLine) |
|
|
|
durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) |
|
|
|
durY += textPaint.textHeight + lineSpacingExtra |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
textPages.last().text = stringBuilder.toString() |
|
|
|
textPages.last().text = stringBuilder.toString() |
|
|
|
stringBuilder.clear() |
|
|
|
stringBuilder.clear() |
|
|
|
pageLines.add(textPages.last().textLines.size) |
|
|
|
pageLines.add(textPages.last().textLines.size) |
|
|
|
pageLengths.add(textPages.last().text.length) |
|
|
|
pageLengths.add(textPages.last().text.length) |
|
|
|
//新页面 |
|
|
|
//新页面 |
|
|
|
durY = layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) |
|
|
|
durY = textPaint.textHeight + lineSpacingExtra |
|
|
|
textPages.add(TextPage()) |
|
|
|
textPages.add(TextPage()) |
|
|
|
textPages.last().textLines.add(textLine) |
|
|
|
textPages.last().textLines.add(textLine) |
|
|
|
} |
|
|
|
} |
|
|
|
textLine.lineTop = (paddingTop + durY - |
|
|
|
textLine.lineBottom = paddingTop + durY - lineSpacingExtra |
|
|
|
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))).toFloat() |
|
|
|
textLine.lineBase = textLine.lineBottom - textPaint.fontMetrics.descent |
|
|
|
textLine.lineBase = (paddingTop + durY - |
|
|
|
textLine.lineTop = textLine.lineBottom - textPaint.textHeight |
|
|
|
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))).toFloat() |
|
|
|
|
|
|
|
textLine.lineBottom = textLine.lineBase + contentPaint.fontMetrics.descent |
|
|
|
|
|
|
|
val words = |
|
|
|
val words = |
|
|
|
text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) |
|
|
|
text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) |
|
|
|
stringBuilder.append(words) |
|
|
|
stringBuilder.append(words) |
|
|
|
textLine.text = words |
|
|
|
textLine.text = words |
|
|
|
val desiredWidth = layout.getLineWidth(lineIndex) |
|
|
|
val desiredWidth = layout.getLineWidth(lineIndex) |
|
|
|
if (lineIndex == 0 && layout.lineCount > 1) { |
|
|
|
if (lineIndex == 0 && layout.lineCount > 1 && !isTitle) { |
|
|
|
//第一行 |
|
|
|
//第一行 |
|
|
|
addCharsToLineFirst(textLine, words, contentPaint, desiredWidth) |
|
|
|
addCharsToLineFirst(textLine, words, textPaint, desiredWidth) |
|
|
|
} else if (lineIndex == layout.lineCount - 1) { |
|
|
|
} else if (lineIndex == layout.lineCount - 1) { |
|
|
|
//最后一行 |
|
|
|
//最后一行 |
|
|
|
addCharsToLineLast(textLine, words, stringBuilder, contentPaint, 0f) |
|
|
|
val x = if (isTitle && ReadBookConfig.titleCenter) |
|
|
|
|
|
|
|
(visibleWidth - layout.getLineWidth(lineIndex)) / 2 |
|
|
|
|
|
|
|
else 0f |
|
|
|
|
|
|
|
addCharsToLineLast(textLine, words, stringBuilder, textPaint, x) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
//中间行 |
|
|
|
//中间行 |
|
|
|
addCharsToLineMiddle(textLine, words, contentPaint, desiredWidth, 0f) |
|
|
|
addCharsToLineMiddle(textLine, words, textPaint, desiredWidth, 0f) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
durY += paragraphSpacing |
|
|
|
durY += paragraphSpacing |
|
|
@ -346,7 +292,7 @@ object ChapterProvider { |
|
|
|
val bold = if (ReadBookConfig.textBold) Typeface.BOLD else Typeface.NORMAL |
|
|
|
val bold = if (ReadBookConfig.textBold) Typeface.BOLD else Typeface.NORMAL |
|
|
|
contentPaint.typeface = Typeface.create(typeface, bold) |
|
|
|
contentPaint.typeface = Typeface.create(typeface, bold) |
|
|
|
//间距 |
|
|
|
//间距 |
|
|
|
lineSpacingExtra = ReadBookConfig.lineSpacingExtra.dp.toFloat() |
|
|
|
lineSpacingExtra = ReadBookConfig.lineSpacingExtra.dp |
|
|
|
paragraphSpacing = ReadBookConfig.paragraphSpacing.dp |
|
|
|
paragraphSpacing = ReadBookConfig.paragraphSpacing.dp |
|
|
|
titlePaint.textSize = (ReadBookConfig.textSize + 2).dp.toFloat() |
|
|
|
titlePaint.textSize = (ReadBookConfig.textSize + 2).dp.toFloat() |
|
|
|
contentPaint.textSize = ReadBookConfig.textSize.dp.toFloat() |
|
|
|
contentPaint.textSize = ReadBookConfig.textSize.dp.toFloat() |
|
|
@ -366,4 +312,8 @@ object ChapterProvider { |
|
|
|
visibleBottom = paddingTop + visibleHeight |
|
|
|
visibleBottom = paddingTop + visibleHeight |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val TextPaint.textHeight: Float |
|
|
|
|
|
|
|
get() { |
|
|
|
|
|
|
|
return this.fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |