Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

react-native-pdf-jsi@6.1.0.patch

118 lines5.3 KB

1diff --git a/android/src/main/java/org/wonday/pdf/viewer/LruByteCache.kt b/android/src/main/java/org/wonday/pdf/viewer/LruByteCache.kt
2index c85bc1347643a681086461cdb61772aa3aace39e..5884311ec5d7b5655bd6c00e9f4b48b43f081733 100644
3--- a/android/src/main/java/org/wonday/pdf/viewer/LruByteCache.kt
4+++ b/android/src/main/java/org/wonday/pdf/viewer/LruByteCache.kt
5@@ -23,6 +23,12 @@ class LruByteCache<K, V>(
6
7 val count: Int get() = map.size
8
9+ /**
10+ * Keys that [put] never evicts to stay under [maxBytes]. The viewer pins what its last frame
11+ * drew or requested; evicting those makes the next frame request them again, endlessly.
12+ */
13+ var pinned: Set<K> = emptySet()
15 operator fun get(key: K): V? = map[key]
17 fun containsKey(key: K): Boolean = map.containsKey(key)
18@@ -34,7 +40,7 @@ class LruByteCache<K, V>(
19 size -= sizeOf(previous)
20 if (previous !== value) onEvict(key, previous)
21 }
22- trimTo(maxBytes)
23+ trimTo(maxBytes, pinned)
24 }
26 fun remove(key: K): V? {
27@@ -61,10 +67,11 @@ class LruByteCache<K, V>(
29 fun keys(): Set<K> = LinkedHashSet(map.keys)
31- fun trimTo(limit: Long) {
32+ fun trimTo(limit: Long, keep: Set<K> = emptySet()) {
33 val iterator = map.entries.iterator()
34 while (size > limit && iterator.hasNext()) {
35 val (key, value) = iterator.next()
36+ if (key in keep) continue
37 iterator.remove()
38 size -= sizeOf(value)
39 onEvict(key, value)
40diff --git a/android/src/main/java/org/wonday/pdf/viewer/PdfViewerView.kt b/android/src/main/java/org/wonday/pdf/viewer/PdfViewerView.kt
41index c51515507544fb7a56f029fff8ea2b1ee35cbe9d..0734562c2dc1b00f1b764613e3f3c5b44811b1fe 100644
42--- a/android/src/main/java/org/wonday/pdf/viewer/PdfViewerView.kt
43+++ b/android/src/main/java/org/wonday/pdf/viewer/PdfViewerView.kt
44@@ -21,6 +21,7 @@ import org.json.JSONArray
45 import org.json.JSONObject
46 import kotlin.math.abs
47 import kotlin.math.ceil
48+import kotlin.math.hypot
49 import kotlin.math.max
50 import kotlin.math.min
51 import kotlin.math.roundToInt
52@@ -40,6 +41,10 @@ data class PdfHighlightRect(val page: Int, val x: Float, val y: Float, val width
53 * Page and scale events are sent only once scrolling and zooming have settled.
54 */
55 class PdfViewerView(context: Context) : View(context) {
56+ private companion object {
57+ const val LINK_TAP_SLOP_DP = 10f
58+ }
60 var onEvent: ((JSONObject) -> Unit)? = null
62 private val main = Handler(Looper.getMainLooper())
63@@ -109,10 +114,9 @@ class PdfViewerView(context: Context) : View(context) {
64 private val scratch = RectF()
66 private val gestures = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
67- override fun onDown(e: MotionEvent): Boolean {
68- stopMotion()
69- return true
70- }
71+ // Motion stops in onTouchEvent: GestureDetector calls onDown after onDoubleTap for the
72+ // same event, so stopping here would cancel the zoom the double tap just started.
73+ override fun onDown(e: MotionEvent): Boolean = true
75 override fun onScroll(e1: MotionEvent?, e2: MotionEvent, dx: Float, dy: Float): Boolean {
76 if (!scrollEnabled || scaling) return false
77@@ -538,6 +542,7 @@ class PdfViewerView(context: Context) : View(context) {
78 drawHighlights(canvas, index, rect)
79 }
80 queue.retain(wanted)
81+ cache.pinned = wanted
82 drawIndicators(canvas, current)
84 if (scroller.computeScrollOffset()) {
85@@ -694,6 +699,7 @@ class PdfViewerView(context: Context) : View(context) {
86 when (event.actionMasked) {
87 MotionEvent.ACTION_DOWN -> {
88 touching = true
89+ stopMotion()
90 if (scrollEnabled) parent?.requestDisallowInterceptTouchEvent(true)
91 }
92 MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> touching = false
93@@ -804,7 +810,11 @@ class PdfViewerView(context: Context) : View(context) {
94 val pointX = (x - rect.left) / scale
95 val pointY = (y - rect.top) / scale
96 val docPage = pageBase + index
97- val hit = linksFor(docPage).firstOrNull { it.contains(pointX, pointY) }
98+ // Links such as citations are only a few glyphs wide, so a tap near one still counts.
99+ val slop = LINK_TAP_SLOP_DP * density / scale
100+ val hit = linksFor(docPage)
101+ .filter { it.distanceTo(pointX, pointY) <= slop }
102+ .minByOrNull { it.distanceTo(pointX, pointY) }
103 when {
104 hit?.uri != null -> emit(JSONObject().put("type", "link").put("url", hit.uri))
105 hit != null && hit.page >= 0 -> goToPage(hit.page, true)
106@@ -818,7 +828,11 @@ class PdfViewerView(context: Context) : View(context) {
107 }
109 private class PageLinkHit(val x: Float, val y: Float, val width: Float, val height: Float, val page: Int, val uri: String?) {
110- fun contains(px: Float, py: Float) = px >= x && px <= x + width && py >= y && py <= y + height
111+ fun distanceTo(px: Float, py: Float): Float {
112+ val dx = max(0f, max(x - px, px - (x + width)))
113+ val dy = max(0f, max(y - py, py - (y + height)))
114+ return hypot(dx, dy)
115+ }
116 }
118 private fun linksFor(docPage: Int): List<PageLinkHit> = links.getOrPut(docPage) {