Skip to content

Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.

feat: zoom out past fit-width in continuous mode (#8)

Ivan Malinovski1a4f29281840
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/PageCanvas.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/PageCanvas.kt
index 9283976..8d3887a 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/PageCanvas.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/PageCanvas.kt
@@ -99,7 +99,8 @@ fun PageCanvas(
onDoubleTap()
flinger[0]?.cancel()
val from = state.zoom
- val to = if (from < 1.6f) 2.5f else 1f
+ // zoomed out: back to fit-width; fit-width: in; zoomed in: back to fit-width
+ val to = if (from < 0.99f) 1f else if (from < 1.6f) 2.5f else 1f
flinger[0] = scope.launch {
val anim = Animatable(from)
anim.animateTo(to, tween(DOUBLE_TAP_MS)) { state.zoomTo(value, p) }
@@ -194,7 +195,7 @@ private fun DrawScope.drawPages(state: ViewerState, images: PageImages, gen: Lon
// a thin shadow line around the paper, as a printed page on a grey desk
drawRect(look.pageShadow, topLeft = Offset(r.left - 1f, r.top - 1f), size = Size(r.width + 2f, r.height + 2f))
drawRect(if (look.invert) Color.Black else Color.White, topLeft = r.topLeft, size = r.size)
- val base = images.get(ImageKey(i, gen, 0)) ?: images.latestBase(i)
+ val base = images.get(ImageKey(i, gen, state.baseLevel())) ?: images.get(ImageKey(i, gen, 0)) ?: images.latestBase(i)
if (base != null) drawImageIn(base, r, look.invert)
if (level > 0) {
val s = state.levelScale(i, level, maxScale)
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerState.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerState.kt
index cdc1412..2a5924a 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerState.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerState.kt
@@ -55,7 +55,8 @@ class ViewerState(pages: List<PageSize>) {
return DocLayout(pages, mode, viewW, viewH, gap, margin, padTop, padBottom).also { cachedLayout = it }
}
- val minZoom: Float get() = MIN_ZOOM
+ /** Continuous pages zoom out past fit-width; single-page mode rests on one page filling the width. */
+ val minZoom: Float get() = if (mode == PageMode.Continuous) ZOOM_OUT_LIMIT else MIN_ZOOM
val maxZoom: Float get() = MAX_ZOOM
/** The page the reader is on: the one at the middle of the screen. */
@@ -249,13 +250,19 @@ class ViewerState(pages: List<PageSize>) {
if (level > 0) {
for (i in visible) tilesFor(i, gen, level, maxScale, out)
}
- val around = visiblePages(1.5f)
+ val around = visiblePages(if (zoom < LOWRES_BELOW) 0.5f else 1.5f)
for (i in around) if (i !in visible) out.add(baseRequest(i, gen, maxScale))
return out
}
- private fun baseRequest(i: Int, gen: Long, maxScale: Float) =
- ImageRequest(ImageKey(i, gen, 0), min(layout.scaleOf(i), maxScale), null)
+ /** Zoomed far out, whole pages are drawn from half-scale renders ([LOWRES_LEVEL]); many fit on screen. */
+ fun baseLevel(): Int = if (zoom < LOWRES_BELOW) LOWRES_LEVEL else 0
+
+ private fun baseRequest(i: Int, gen: Long, maxScale: Float): ImageRequest {
+ val level = baseLevel()
+ val factor = if (level == LOWRES_LEVEL) 0.5f else 1f
+ return ImageRequest(ImageKey(i, gen, level), min(layout.scaleOf(i) * factor, maxScale), null)
+ }
/** 0 at about zoom 1; then one level per half octave of zoom. */
fun detailLevel(): Int = if (zoom < 1.2f) 0 else ceil(ln(zoom) / ln(LEVEL_STEP)).toInt().coerceAtLeast(1)
@@ -290,7 +297,13 @@ class ViewerState(pages: List<PageSize>) {
}
companion object {
+ /** Fit-width: the least single-page mode zooms to. */
const val MIN_ZOOM = 1f
+
+ /** How far continuous mode zooms out (a quarter of fit-width). */
+ const val ZOOM_OUT_LIMIT = 0.25f
+ const val LOWRES_BELOW = 0.6f
+ const val LOWRES_LEVEL = -1
const val MAX_ZOOM = 10f
const val TILE = 512
const val LEVEL_STEP = 1.4142135f
diff --git a/apps/pdfcraft-android/android/app/src/test/java/io/github/artcraftandroid/pdfcraft/ui/ViewerGeometryTest.kt b/apps/pdfcraft-android/android/app/src/test/java/io/github/artcraftandroid/pdfcraft/ui/ViewerGeometryTest.kt
index 008232b..c683099 100644
--- a/apps/pdfcraft-android/android/app/src/test/java/io/github/artcraftandroid/pdfcraft/ui/ViewerGeometryTest.kt
+++ b/apps/pdfcraft-android/android/app/src/test/java/io/github/artcraftandroid/pdfcraft/ui/ViewerGeometryTest.kt
@@ -79,13 +79,48 @@ class ViewerGeometryTest {
s.zoomTo(100f, Offset.Zero)
assertEquals(ViewerState.MAX_ZOOM, s.zoom, 0.0001f)
s.zoomTo(0.01f, Offset.Zero)
- assertEquals(ViewerState.MIN_ZOOM, s.zoom, 0.0001f)
+ assertEquals(ViewerState.ZOOM_OUT_LIMIT, s.zoom, 0.0001f)
+ s.zoomTo(1f, Offset.Zero)
s.scrollBy(0f, 1_000_000f)
assertEquals(0f, s.scrollY, 0.0001f)
s.scrollBy(0f, -1_000_000f)
assertEquals(s.layout.height - s.viewH, s.scrollY, 0.5f)
}
+ @Test
+ fun continuousZoomsOutPastFitWidthAndCentresTheDocument() {
+ val s = state(List(3) { letter })
+ s.zoomTo(0.5f, Offset(540f, 700f))
+ assertEquals(0.5f, s.zoom, 0.0001f)
+ // the document is narrower than the screen: centred horizontally
+ assertEquals(-(s.viewW - s.layout.width * 0.5f) / 2f, s.scrollX, 0.5f)
+ // pages are mapped back correctly while zoomed out
+ val p = s.screenToPage(s.pageToScreen(1, 100f, 200f))!!
+ assertEquals(1, p.page)
+ assertEquals(100f, p.x, 0.1f)
+ assertEquals(200f, p.y, 0.1f)
+ }
+
+ @Test
+ fun singlePageStaysAtFitWidth() {
+ val s = state(List(3) { letter })
+ s.switchMode(PageMode.SinglePage)
+ s.zoomTo(0.3f, Offset.Zero)
+ assertEquals(ViewerState.MIN_ZOOM, s.zoom, 0.0001f)
+ }
+
+ @Test
+ fun zoomedOutPagesRenderAtHalfScale() {
+ val s = state(List(20) { letter })
+ val full = s.requests(1, 100f).first()
+ assertEquals(0, full.key.level)
+ s.zoomTo(0.3f, Offset(540f, 700f))
+ val out = s.requests(1, 100f)
+ assertTrue(out.isNotEmpty())
+ assertTrue(out.all { it.key.level == ViewerState.LOWRES_LEVEL && it.tile == null })
+ assertEquals(full.scale * 0.5f, out.first().scale, 0.001f)
+ }
+
@Test
fun pageMappingRoundTrips() {
val s = state(List(4) { letter })