Yokan / rebuild
Course 1 8624561

NovelPagination.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/novel/NovelPagination.kt

implementation129 lines
1package eu.friedcutlet.yokan.ui.screens.novel23internal data class NovelAnchor(val blockIndex: Int, val offset: Int = 0)45internal data class NovelPageSlice(6    val blockIndex: Int,7    val start: Int = 0,8    val endExclusive: Int = 0,9)1011internal data class NovelPage(12    val slices: List<NovelPageSlice>,13    val start: NovelAnchor,14    val end: NovelAnchor,15)1617internal data class NovelTextFit(val endExclusive: Int, val heightPx: Int)1819internal fun paginateNovelBlocks(20    blocks: List<NovelContentBlock>,21    pageHeightPx: Int,22    spacingPx: Int,23    measureText: (NovelContentBlock.Text, start: Int, availableHeightPx: Int) -> NovelTextFit?,24    measureAtomicHeight: (NovelContentBlock) -> Int,25): List<NovelPage>? {26    if (pageHeightPx <= 0 || spacingPx < 0) return null27    if (blocks.isEmpty()) return listOf(NovelPage(emptyList(), NovelAnchor(0), NovelAnchor(0)))2829    data class BuildingPage(30        val slices: MutableList<NovelPageSlice> = mutableListOf(),31        var usedHeight: Int = 0,32        var lastBlockIndex: Int? = null,33        var start: NovelAnchor? = null,34        var end: NovelAnchor? = null,35    )3637    val pages = mutableListOf<NovelPage>()38    var page = BuildingPage()3940    fun finishPage() {41        val start = page.start ?: return42        val end = page.end ?: return43        pages += NovelPage(page.slices.toList(), start, end)44        page = BuildingPage()45    }4647    fun availableFor(blockIndex: Int): Int {48        val spacing = if (page.lastBlockIndex != null && page.lastBlockIndex != blockIndex) spacingPx else 049        return (pageHeightPx.toLong() - page.usedHeight - spacing).coerceAtLeast(0).toInt()50    }5152    blocks.forEachIndexed { blockIndex, block ->53        when (block) {54            is NovelContentBlock.Text -> {55                var cursor = 056                while (cursor < block.text.length) {57                    val available = availableFor(blockIndex)58                    if (available <= 0) {59                        if (page.slices.isEmpty()) return null60                        finishPage()61                        continue62                    }63                    val fit = measureText(block, cursor, available)64                    if (fit == null) {65                        if (page.slices.isEmpty()) return null66                        finishPage()67                        continue68                    }69                    if (fit.endExclusive <= cursor || fit.endExclusive > block.text.length ||70                        fit.heightPx <= 0 || fit.heightPx > available71                    ) return null7273                    val spacing = if (page.lastBlockIndex != null && page.lastBlockIndex != blockIndex) spacingPx else 074                    page.usedHeight += spacing + fit.heightPx75                    page.slices += NovelPageSlice(blockIndex, cursor, fit.endExclusive)76                    if (page.start == null) page.start = NovelAnchor(blockIndex, cursor)77                    page.end = if (fit.endExclusive == block.text.length) {78                        NovelAnchor(blockIndex + 1)79                    } else {80                        NovelAnchor(blockIndex, fit.endExclusive)81                    }82                    page.lastBlockIndex = blockIndex83                    cursor = fit.endExclusive84                    if (cursor < block.text.length) finishPage()85                }86                if (block.text.isEmpty()) {87                    // Empty text contributes no rendered content or page slice.88                }89            }90            is NovelContentBlock.Image, NovelContentBlock.Rule -> {91                val height = measureAtomicHeight(block)92                if (height <= 0 || height > pageHeightPx) return null93                val spacing = if (page.lastBlockIndex != null && page.lastBlockIndex != blockIndex) spacingPx else 094                if (height.toLong() + spacing > pageHeightPx - page.usedHeight) {95                    if (page.slices.isEmpty()) return null96                    finishPage()97                }98                val actualSpacing = if (page.lastBlockIndex != null && page.lastBlockIndex != blockIndex) spacingPx else 099                page.usedHeight += actualSpacing + height100                page.slices += NovelPageSlice(blockIndex)101                if (page.start == null) page.start = NovelAnchor(blockIndex)102                page.end = NovelAnchor(blockIndex + 1)103                page.lastBlockIndex = blockIndex104            }105        }106    }107    finishPage()108    return pages109}110111internal fun pageIndexForNovelAnchor(pages: List<NovelPage>, anchor: NovelAnchor): Int {112    if (pages.isEmpty()) return 0113    fun compare(left: NovelAnchor, right: NovelAnchor): Int =114        compareValuesBy(left, right, NovelAnchor::blockIndex, NovelAnchor::offset)115116    return pages.indexOfFirst { compare(anchor, it.start) >= 0 && compare(anchor, it.end) < 0 }117        .takeIf { it >= 0 }118        ?: if (compare(anchor, pages.first().start) < 0) 0 else pages.lastIndex119}120121internal fun novelSpreadCount(pageCount: Int, panes: Int): Int {122    if (pageCount <= 0 || panes <= 0) return 0123    return (pageCount - 1) / panes + 1124}125126internal fun novelSpreadForPage(pageIndex: Int, panes: Int): Int {127    if (pageIndex < 0 || panes <= 0) return 0128    return pageIndex / panes129}130

Search chapters, reference and source filenames.