@papyrus-sdk/engine-native 0.2.16-beta.0 → 0.2.16-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,7 @@ import android.graphics.Path;
11
11
  import android.graphics.Rect;
12
12
  import android.os.Handler;
13
13
  import android.os.Looper;
14
+ import android.os.SystemClock;
14
15
  import android.util.AttributeSet;
15
16
  import android.util.LruCache;
16
17
  import android.util.Log;
@@ -50,9 +51,10 @@ public class PapyrusPdfViewerView extends View {
50
51
 
51
52
  @Override
52
53
  protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
53
- if (oldValue != null && oldValue != newValue && !oldValue.isRecycled()) {
54
- oldValue.recycle();
55
- }
54
+ // Do not recycle here to avoid flickering when a bitmap is evicted
55
+ // while it may still be referenced by lastPageBitmap or drawn on screen.
56
+ // On Android 8.0+ (API 26+), Bitmap pixel data lives in native heap
57
+ // and is garbage-collected normally without explicit recycle().
56
58
  }
57
59
  };
58
60
  private static final ColorMatrixColorFilter SEPIA_FILTER = createSepiaFilter();
@@ -80,8 +82,34 @@ public class PapyrusPdfViewerView extends View {
80
82
  private float lastTouchY = 0f;
81
83
  private boolean layoutDirty = true;
82
84
  private int renderGeneration = 0;
83
- private boolean wasScaling = false;
84
85
  private boolean isDrawingInk = false;
86
+ private boolean isPinching = false;
87
+ private float pinchScale = 1.0f;
88
+ private float pinchFocusX = 0f;
89
+ private float pinchFocusY = 0f;
90
+ private float pinchStartZoom = 1.0f;
91
+ private float pinchStartOffsetX = 0f;
92
+ private float pinchStartOffsetY = 0f;
93
+ private float pinchStartFocusX = 0f;
94
+ private float pinchStartFocusY = 0f;
95
+
96
+ // Viewport anchor for stable zoom around the focused page
97
+ private static class ViewportAnchor {
98
+ final int pageIndex;
99
+ final float pageXRatio;
100
+ final float pageYRatio;
101
+ final float screenX;
102
+ final float screenY;
103
+
104
+ ViewportAnchor(int pageIndex, float pageXRatio, float pageYRatio, float screenX, float screenY) {
105
+ this.pageIndex = pageIndex;
106
+ this.pageXRatio = pageXRatio;
107
+ this.pageYRatio = pageYRatio;
108
+ this.screenX = screenX;
109
+ this.screenY = screenY;
110
+ }
111
+ }
112
+
85
113
  private List<float[]> inkStrokePoints = new ArrayList<>();
86
114
  private int inkStrokePageIndex = -1;
87
115
  private long touchDownTime = 0;
@@ -124,6 +152,20 @@ public class PapyrusPdfViewerView extends View {
124
152
  private final Handler eventThrottleHandler = new Handler(Looper.getMainLooper());
125
153
  private Runnable pendingZoomEvent;
126
154
  private Runnable pendingScrollEvent;
155
+
156
+ // Visible pages deduplication to prevent JS/native loop in page gaps
157
+ private String lastVisiblePagesSignature = "";
158
+ private int lastStablePage = 1;
159
+ private static final float MIN_VISIBLE_RATIO = 0.03f;
160
+ private static final float VISIBLE_RATIO_BUCKET = 0.02f;
161
+ private static final float CURRENT_PAGE_SWITCH_THRESHOLD = 0.08f;
162
+ private static final boolean DEBUG_GAP = false;
163
+ private static final boolean DEBUG_RENDER_GAP = false;
164
+ private static final boolean ISOLATE_RENDER_TEST = false;
165
+ private static final long RENDER_COOLDOWN_MS = 250;
166
+ private static final float MIN_VISIBLE_PX = 16f;
167
+ private final Map<String, Long> renderRequestCooldown = new HashMap<>();
168
+
127
169
  private final OverScroller flingScroller;
128
170
  private VelocityTracker velocityTracker;
129
171
  private final Runnable flingRunnable = new Runnable() {
@@ -289,6 +331,14 @@ public class PapyrusPdfViewerView extends View {
289
331
  public boolean onTouchEvent(MotionEvent event) {
290
332
  scaleDetector.onTouchEvent(event);
291
333
 
334
+ if (event.getPointerCount() > 1) {
335
+ // Cancel any pending text selection when a second finger touches
336
+ if (pendingLongPress != null) {
337
+ mainHandler.removeCallbacks(pendingLongPress);
338
+ pendingLongPress = null;
339
+ }
340
+ }
341
+
292
342
  if ("ink".equals(activeTool) && event.getPointerCount() == 1) {
293
343
  return handleInkTouch(event);
294
344
  }
@@ -336,10 +386,6 @@ public class PapyrusPdfViewerView extends View {
336
386
  return true;
337
387
  case MotionEvent.ACTION_UP:
338
388
  case MotionEvent.ACTION_CANCEL:
339
- if (wasScaling && !scaleDetector.isInProgress()) {
340
- wasScaling = false;
341
- emitZoomChanged(zoom);
342
- }
343
389
  if (!scaleDetector.isInProgress() && velocityTracker != null) {
344
390
  velocityTracker.addMovement(event);
345
391
  velocityTracker.computeCurrentVelocity(1000, 8000);
@@ -487,6 +533,9 @@ public class PapyrusPdfViewerView extends View {
487
533
  float density = getResources().getDisplayMetrics().density;
488
534
  switch (event.getActionMasked()) {
489
535
  case MotionEvent.ACTION_DOWN:
536
+ if (event.getPointerCount() > 1) {
537
+ return true; // Multi-touch: ignore, let pinch zoom handle it
538
+ }
490
539
  touchDownTime = System.currentTimeMillis();
491
540
  touchDownX = event.getX();
492
541
  touchDownY = event.getY();
@@ -998,10 +1047,15 @@ public class PapyrusPdfViewerView extends View {
998
1047
  @Override
999
1048
  protected void onDraw(Canvas canvas) {
1000
1049
  super.onDraw(canvas);
1001
- ensureLayout();
1050
+ if (!isPinching) {
1051
+ ensureLayout();
1052
+ }
1002
1053
  canvas.drawColor(resolveBackgroundColor(pageTheme));
1003
1054
  if (pageFrames.isEmpty()) return;
1004
1055
 
1056
+ boolean inGap = isViewportCenterInsidePageGap();
1057
+ int visibleCount = 0;
1058
+
1005
1059
  for (PageFrame frame : pageFrames) {
1006
1060
  int left = Math.round(frame.left - offsetX);
1007
1061
  int top = Math.round(frame.top - offsetY);
@@ -1009,59 +1063,140 @@ public class PapyrusPdfViewerView extends View {
1009
1063
  int bottom = Math.round(top + frame.height);
1010
1064
  if (bottom < 0 || top > getHeight()) continue;
1011
1065
 
1066
+ float visibleTop = Math.max(frame.top, offsetY);
1067
+ float visibleBottom = Math.min(frame.top + frame.height, offsetY + getHeight());
1068
+ float visibleHeight = visibleBottom - visibleTop;
1069
+ boolean meaningfullyVisible = visibleHeight >= MIN_VISIBLE_PX;
1070
+ if (visibleHeight > 0) visibleCount++;
1071
+
1012
1072
  paint.setColor(resolvePageColor(pageTheme));
1013
1073
  paint.setColorFilter(null);
1014
1074
  canvas.drawRect(left, top, right, bottom, paint);
1015
1075
 
1016
1076
  String key = buildRenderKey(frame);
1017
1077
  Bitmap bitmap = RENDER_CACHE.get(key);
1078
+ boolean drawn = false;
1018
1079
  if (bitmap != null && !bitmap.isRecycled()) {
1019
1080
  try {
1020
1081
  paint.setColorFilter(resolveThemeFilter(pageTheme));
1021
1082
  canvas.drawBitmap(bitmap, null, new Rect(left, top, right, bottom), paint);
1022
1083
  paint.setColorFilter(null);
1084
+ drawn = true;
1023
1085
  } catch (RuntimeException error) {
1024
1086
  Log.w(TAG, "Failed to draw dedicated PDF page", error);
1025
1087
  }
1026
- } else {
1088
+ }
1089
+
1090
+ if (!drawn) {
1027
1091
  Bitmap fallback = lastPageBitmap.get(frame.index);
1028
1092
  if (fallback != null && !fallback.isRecycled()) {
1029
1093
  try {
1030
1094
  paint.setColorFilter(resolveThemeFilter(pageTheme));
1031
1095
  canvas.drawBitmap(fallback, null, new Rect(left, top, right, bottom), paint);
1032
1096
  paint.setColorFilter(null);
1097
+ drawn = true;
1033
1098
  } catch (RuntimeException error) {
1034
1099
  Log.w(TAG, "Failed to draw fallback PDF page", error);
1035
1100
  }
1036
1101
  }
1037
- requestRender(frame, key);
1038
1102
  }
1103
+
1104
+ if (!drawn && !isPinching) {
1105
+ if (!meaningfullyVisible) {
1106
+ if (DEBUG_RENDER_GAP) {
1107
+ Log.d(TAG, "onDraw: skip render page=" + frame.index + " visibleHeight=" + visibleHeight + " below threshold");
1108
+ }
1109
+ } else if (inGap && frame.index + 1 != lastStablePage) {
1110
+ if (DEBUG_RENDER_GAP) {
1111
+ Log.d(TAG, "onDraw: skip render page=" + frame.index + " inGap lastStablePage=" + lastStablePage);
1112
+ }
1113
+ } else {
1114
+ if (DEBUG_RENDER_GAP) {
1115
+ Log.d(TAG, "onDraw: requestRender page=" + frame.index + " key=" + key + " zoom=" + zoom + " offsetY=" + offsetY);
1116
+ }
1117
+ requestRender(frame, key);
1118
+ }
1119
+ }
1120
+
1039
1121
  drawOverlays(canvas, frame, left, top);
1040
1122
  }
1123
+
1124
+ if (DEBUG_RENDER_GAP) {
1125
+ Log.d(TAG, "onDraw: visibleCount=" + visibleCount + " inGap=" + inGap + " zoom=" + zoom + " offsetY=" + offsetY + " lastStablePage=" + lastStablePage);
1126
+ }
1127
+ }
1128
+
1129
+ private boolean isViewportCenterInsidePageGap() {
1130
+ if (pageFrames.isEmpty() || getHeight() <= 0) return false;
1131
+ float centerY = offsetY + getHeight() / 2f;
1132
+ for (PageFrame frame : pageFrames) {
1133
+ if (centerY >= frame.top && centerY <= frame.top + frame.height) {
1134
+ return false;
1135
+ }
1136
+ }
1137
+ return true;
1138
+ }
1139
+
1140
+ private float getVisibleRatioThreshold() {
1141
+ return zoom >= 2.0f ? 0.08f : MIN_VISIBLE_RATIO;
1041
1142
  }
1042
1143
 
1043
1144
  private int computeVisiblePage() {
1044
- if (pageFrames.isEmpty()) return 1;
1045
- float viewportCenterY = offsetY + getHeight() / 2f;
1046
- int bestPage = 1;
1047
- float bestDistance = Float.MAX_VALUE;
1145
+ if (pageFrames.isEmpty() || getHeight() <= 0) return Math.max(1, lastStablePage);
1146
+
1147
+ // If viewport center is inside a page gap, keep the last stable page
1148
+ if (isViewportCenterInsidePageGap()) {
1149
+ if (DEBUG_GAP) {
1150
+ Log.d(TAG, "computeVisiblePage: center in gap, keeping lastStablePage=" + lastStablePage);
1151
+ }
1152
+ return Math.max(1, lastStablePage);
1153
+ }
1154
+
1155
+ float viewportTop = offsetY;
1156
+ float viewportBottom = offsetY + getHeight();
1157
+
1158
+ int bestPage = -1;
1159
+ float bestRatio = 0f;
1160
+
1048
1161
  for (PageFrame frame : pageFrames) {
1049
- float frameCenterY = frame.top + frame.height / 2f;
1050
- float distance = Math.abs(frameCenterY - viewportCenterY);
1051
- if (distance < bestDistance) {
1052
- bestDistance = distance;
1162
+ float frameTop = frame.top;
1163
+ float frameBottom = frame.top + frame.height;
1164
+ float visibleTop = Math.max(frameTop, viewportTop);
1165
+ float visibleBottom = Math.min(frameBottom, viewportBottom);
1166
+ float visibleHeight = visibleBottom - visibleTop;
1167
+
1168
+ if (visibleHeight <= 0f || frame.height <= 0f) continue;
1169
+
1170
+ float ratio = visibleHeight / frame.height;
1171
+
1172
+ if (ratio > bestRatio) {
1173
+ bestRatio = ratio;
1053
1174
  bestPage = frame.index + 1;
1054
1175
  }
1055
1176
  }
1177
+
1178
+ // If we're essentially in a gap (no dominant page), keep the last stable page
1179
+ if (bestPage < 0 || bestRatio < CURRENT_PAGE_SWITCH_THRESHOLD) {
1180
+ return Math.max(1, lastStablePage);
1181
+ }
1182
+
1183
+ lastStablePage = bestPage;
1056
1184
  return bestPage;
1057
1185
  }
1058
1186
 
1059
1187
  private void emitPageChanged(int page) {
1188
+ if (ISOLATE_RENDER_TEST) return;
1189
+ if (isPinching) return;
1060
1190
  try {
1061
1191
  WritableMap event = Arguments.createMap();
1062
1192
  event.putInt("page", page);
1063
1193
  reactContext.getJSModule(RCTEventEmitter.class)
1064
1194
  .receiveEvent(getId(), "onPageChanged", event);
1195
+
1196
+ WritableMap aliasEvent = Arguments.createMap();
1197
+ aliasEvent.putInt("page", page);
1198
+ reactContext.getJSModule(RCTEventEmitter.class)
1199
+ .receiveEvent(getId(), "onPageChange", aliasEvent);
1065
1200
  } catch (Throwable ignored) {
1066
1201
  }
1067
1202
  }
@@ -1076,6 +1211,11 @@ public class PapyrusPdfViewerView extends View {
1076
1211
  event.putDouble("zoom", zoomValue);
1077
1212
  reactContext.getJSModule(RCTEventEmitter.class)
1078
1213
  .receiveEvent(getId(), "onZoomChanged", event);
1214
+
1215
+ WritableMap aliasEvent = Arguments.createMap();
1216
+ aliasEvent.putDouble("zoom", zoomValue);
1217
+ reactContext.getJSModule(RCTEventEmitter.class)
1218
+ .receiveEvent(getId(), "onZoomChange", aliasEvent);
1079
1219
  pendingZoomEvent = null;
1080
1220
  };
1081
1221
  eventThrottleHandler.postDelayed(pendingZoomEvent, 120);
@@ -1194,24 +1334,53 @@ public class PapyrusPdfViewerView extends View {
1194
1334
  return new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
1195
1335
  @Override
1196
1336
  public boolean onScaleBegin(ScaleGestureDetector detector) {
1197
- wasScaling = true;
1337
+ isPinching = true;
1338
+ pinchStartZoom = zoom;
1339
+ pinchStartOffsetX = offsetX;
1340
+ pinchStartOffsetY = offsetY;
1341
+ pinchFocusX = detector.getFocusX();
1342
+ pinchFocusY = detector.getFocusY();
1198
1343
  return true;
1199
1344
  }
1200
1345
 
1201
1346
  @Override
1202
1347
  public boolean onScale(ScaleGestureDetector detector) {
1203
- float oldContentWidth = Math.max(1f, contentWidth);
1204
- float oldContentHeight = Math.max(1f, contentHeight);
1205
- float focusRatioX = (offsetX + detector.getFocusX()) / oldContentWidth;
1206
- float focusRatioY = (offsetY + detector.getFocusY()) / oldContentHeight;
1207
- zoom = clamp(zoom * detector.getScaleFactor(), 0.5f, 5.0f);
1348
+ float scaleFactor = detector.getScaleFactor();
1349
+ float focusX = detector.getFocusX();
1350
+ float focusY = detector.getFocusY();
1351
+
1352
+ float oldZoom = zoom;
1353
+ float newZoom = clamp(oldZoom * scaleFactor, 0.5f, 5.0f);
1354
+
1355
+ if (Math.abs(newZoom - oldZoom) < 0.001f) {
1356
+ pinchFocusX = focusX;
1357
+ pinchFocusY = focusY;
1358
+ return true;
1359
+ }
1360
+
1361
+ ViewportAnchor anchor = captureViewportAnchor(focusX, focusY);
1362
+
1363
+ zoom = newZoom;
1364
+ ensureLayoutLight();
1365
+ restoreViewportAnchor(anchor);
1366
+
1367
+ pinchFocusX = focusX;
1368
+ pinchFocusY = focusY;
1369
+ invalidate();
1370
+ return true;
1371
+ }
1372
+
1373
+ @Override
1374
+ public void onScaleEnd(ScaleGestureDetector detector) {
1375
+ ViewportAnchor anchor = captureViewportAnchor(pinchFocusX, pinchFocusY);
1376
+
1377
+ isPinching = false;
1208
1378
  layoutDirty = true;
1209
1379
  ensureLayout();
1210
- offsetX = focusRatioX * contentWidth - detector.getFocusX();
1211
- offsetY = focusRatioY * contentHeight - detector.getFocusY();
1212
- clampOffsets();
1380
+ restoreViewportAnchor(anchor);
1381
+
1213
1382
  invalidate();
1214
- return true;
1383
+ emitZoomChanged(zoom);
1215
1384
  }
1216
1385
  });
1217
1386
  }
@@ -1246,7 +1415,7 @@ public class PapyrusPdfViewerView extends View {
1246
1415
  maxWidth = Math.max(maxWidth, width + PAGE_PADDING * 2);
1247
1416
  float left = Math.max(PAGE_PADDING, (maxWidth - width) / 2f);
1248
1417
  float top = i * cellHeight + (cellHeight - height) / 2f;
1249
- pageFrames.add(new PageFrame(i, left, top, width, height));
1418
+ pageFrames.add(new PageFrame(i, left, top, width, height, pageWidth, pageHeight));
1250
1419
  }
1251
1420
  contentWidth = maxWidth;
1252
1421
  contentHeight = Math.max(getHeight(), pageCount * cellHeight);
@@ -1261,7 +1430,7 @@ public class PapyrusPdfViewerView extends View {
1261
1430
  float height = pageHeight * baseScale * zoom;
1262
1431
  maxWidth = Math.max(maxWidth, width + PAGE_PADDING * 2);
1263
1432
  float left = Math.max(PAGE_PADDING, (maxWidth - width) / 2f);
1264
- pageFrames.add(new PageFrame(i, left, y, width, height));
1433
+ pageFrames.add(new PageFrame(i, left, y, width, height, pageWidth, pageHeight));
1265
1434
  y += height + PAGE_GAP;
1266
1435
  }
1267
1436
  contentWidth = maxWidth;
@@ -1277,8 +1446,114 @@ public class PapyrusPdfViewerView extends View {
1277
1446
  clampOffsets();
1278
1447
  }
1279
1448
 
1449
+ private ViewportAnchor captureViewportAnchor(float screenX, float screenY) {
1450
+ if (pageFrames.isEmpty()) return null;
1451
+
1452
+ float contentX = offsetX + screenX;
1453
+ float contentY = offsetY + screenY;
1454
+
1455
+ PageFrame frame = findFrameForAnchor(contentY);
1456
+
1457
+ if (frame == null) {
1458
+ int fallbackIndex = Math.max(0, Math.min(pageFrames.size() - 1, lastStablePage - 1));
1459
+ frame = pageFrames.get(fallbackIndex);
1460
+ }
1461
+
1462
+ float xRatio = frame.width <= 0f
1463
+ ? 0f
1464
+ : clamp((contentX - frame.left) / frame.width, 0f, 1f);
1465
+
1466
+ float yRatio = frame.height <= 0f
1467
+ ? 0f
1468
+ : clamp((contentY - frame.top) / frame.height, 0f, 1f);
1469
+
1470
+ return new ViewportAnchor(frame.index, xRatio, yRatio, screenX, screenY);
1471
+ }
1472
+
1473
+ private PageFrame findFrameForAnchor(float contentY) {
1474
+ PageFrame nearest = null;
1475
+ float nearestDistance = Float.MAX_VALUE;
1476
+
1477
+ for (PageFrame frame : pageFrames) {
1478
+ float top = frame.top;
1479
+ float bottom = frame.top + frame.height;
1480
+
1481
+ if (contentY >= top && contentY <= bottom) {
1482
+ return frame;
1483
+ }
1484
+
1485
+ float distance = contentY < top ? top - contentY : contentY - bottom;
1486
+
1487
+ if (distance < nearestDistance) {
1488
+ nearestDistance = distance;
1489
+ nearest = frame;
1490
+ }
1491
+ }
1492
+
1493
+ return nearest;
1494
+ }
1495
+
1496
+ private void restoreViewportAnchor(ViewportAnchor anchor) {
1497
+ if (anchor == null) return;
1498
+ if (anchor.pageIndex < 0 || anchor.pageIndex >= pageFrames.size()) return;
1499
+
1500
+ PageFrame frame = pageFrames.get(anchor.pageIndex);
1501
+
1502
+ offsetX = frame.left + frame.width * anchor.pageXRatio - anchor.screenX;
1503
+ offsetY = frame.top + frame.height * anchor.pageYRatio - anchor.screenY;
1504
+
1505
+ clampOffsets();
1506
+ }
1507
+
1508
+ private void ensureLayoutLight() {
1509
+ if (pageFrames.isEmpty()) return;
1510
+ float maxWidth = getWidth();
1511
+ int availableWidth = Math.max(1, getWidth() - PAGE_PADDING * 2);
1512
+
1513
+ if ("single".equals(viewMode)) {
1514
+ float cellHeight = getHeight();
1515
+ for (int i = 0; i < pageFrames.size(); i += 1) {
1516
+ PageFrame old = pageFrames.get(i);
1517
+ float baseScale = availableWidth / (float) old.pageWidth;
1518
+ float width = old.pageWidth * baseScale * zoom;
1519
+ float height = old.pageHeight * baseScale * zoom;
1520
+ maxWidth = Math.max(maxWidth, width + PAGE_PADDING * 2);
1521
+ float left = Math.max(PAGE_PADDING, (maxWidth - width) / 2f);
1522
+ float top = i * cellHeight + (cellHeight - height) / 2f;
1523
+ pageFrames.set(i, new PageFrame(i, left, top, width, height, old.pageWidth, old.pageHeight));
1524
+ }
1525
+ contentWidth = maxWidth;
1526
+ contentHeight = Math.max(getHeight(), pageFrames.size() * cellHeight);
1527
+ } else {
1528
+ float y = PAGE_PADDING;
1529
+ for (int i = 0; i < pageFrames.size(); i += 1) {
1530
+ PageFrame old = pageFrames.get(i);
1531
+ float baseScale = availableWidth / (float) old.pageWidth;
1532
+ float width = old.pageWidth * baseScale * zoom;
1533
+ float height = old.pageHeight * baseScale * zoom;
1534
+ maxWidth = Math.max(maxWidth, width + PAGE_PADDING * 2);
1535
+ float left = Math.max(PAGE_PADDING, (maxWidth - width) / 2f);
1536
+ pageFrames.set(i, new PageFrame(i, left, y, width, height, old.pageWidth, old.pageHeight));
1537
+ y += height + PAGE_GAP;
1538
+ }
1539
+ contentWidth = maxWidth;
1540
+ contentHeight = Math.max(getHeight(), y + PAGE_PADDING);
1541
+ }
1542
+ clampOffsets();
1543
+ }
1544
+
1280
1545
  private void requestRender(PageFrame frame, String key) {
1281
- if (loadingKeys.contains(key)) return;
1546
+ if (loadingKeys.contains(key)) {
1547
+ if (DEBUG_RENDER_GAP) Log.d(TAG, "requestRender: already loading key=" + key);
1548
+ return;
1549
+ }
1550
+ long now = SystemClock.uptimeMillis();
1551
+ Long last = renderRequestCooldown.get(key);
1552
+ if (last != null && now - last < RENDER_COOLDOWN_MS) {
1553
+ if (DEBUG_RENDER_GAP) Log.d(TAG, "requestRender: cooldown key=" + key);
1554
+ return;
1555
+ }
1556
+ renderRequestCooldown.put(key, now);
1282
1557
  PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
1283
1558
  if (state == null || state.document == null || state.isSearching) return;
1284
1559
  loadingKeys.add(key);
@@ -1290,6 +1565,10 @@ public class PapyrusPdfViewerView extends View {
1290
1565
  int renderWidth = renderSize[0];
1291
1566
  int renderHeight = renderSize[1];
1292
1567
 
1568
+ if (DEBUG_RENDER_GAP) {
1569
+ Log.d(TAG, "requestRender: start page=" + frame.index + " key=" + key + " size=" + renderWidth + "x" + renderHeight);
1570
+ }
1571
+
1293
1572
  RENDER_EXECUTOR.execute(() -> {
1294
1573
  Bitmap rendered = null;
1295
1574
  try {
@@ -1305,6 +1584,9 @@ public class PapyrusPdfViewerView extends View {
1305
1584
  if (finalRendered != null && !finalRendered.isRecycled()) {
1306
1585
  RENDER_CACHE.put(key, finalRendered);
1307
1586
  lastPageBitmap.put(frame.index, finalRendered);
1587
+ if (DEBUG_RENDER_GAP) {
1588
+ Log.d(TAG, "requestRender: complete page=" + frame.index + " key=" + key + " cacheSize=" + RENDER_CACHE.size());
1589
+ }
1308
1590
  }
1309
1591
  if (generationAtStart == renderGeneration) {
1310
1592
  invalidate();
@@ -1325,7 +1607,11 @@ public class PapyrusPdfViewerView extends View {
1325
1607
  PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
1326
1608
  int docIdentity = state == null || state.document == null ? 0 : System.identityHashCode(state.document);
1327
1609
  String source = state == null || state.sourcePath == null ? "" : state.sourcePath;
1328
- return source + ":" + docIdentity + ":" + frame.index + ":" + Math.round(frame.width) + "x" + Math.round(frame.height);
1610
+ // Bucket zoom to prevent tiny fluctuations from changing the key during scroll/pan
1611
+ float zoomBucket = Math.round(zoom * 100f) / 100f;
1612
+ int widthPx = Math.max(1, Math.round(frame.width));
1613
+ int heightPx = Math.max(1, Math.round(frame.height));
1614
+ return source + ":" + docIdentity + ":" + frame.index + ":" + widthPx + "x" + heightPx + ":z" + zoomBucket + ":" + pageTheme;
1329
1615
  }
1330
1616
 
1331
1617
  private void clampOffsets() {
@@ -1358,6 +1644,8 @@ public class PapyrusPdfViewerView extends View {
1358
1644
  }
1359
1645
 
1360
1646
  private void emitScrollEvent(float offsetYValue) {
1647
+ if (ISOLATE_RENDER_TEST) return;
1648
+ if (isPinching) return;
1361
1649
  try {
1362
1650
  if (pendingScrollEvent != null) {
1363
1651
  eventThrottleHandler.removeCallbacks(pendingScrollEvent);
@@ -1367,13 +1655,116 @@ public class PapyrusPdfViewerView extends View {
1367
1655
  event.putDouble("offsetY", offsetYValue);
1368
1656
  reactContext.getJSModule(RCTEventEmitter.class)
1369
1657
  .receiveEvent(getId(), "onScroll", event);
1658
+ emitVisiblePagesChanged();
1370
1659
  pendingScrollEvent = null;
1371
1660
  };
1372
- eventThrottleHandler.postDelayed(pendingScrollEvent, 16);
1661
+ eventThrottleHandler.postDelayed(pendingScrollEvent, 80);
1373
1662
  } catch (Throwable ignored) {
1374
1663
  }
1375
1664
  }
1376
1665
 
1666
+ private void emitVisiblePagesChanged() {
1667
+ if (ISOLATE_RENDER_TEST) return;
1668
+ if (isPinching) return;
1669
+ // Dead zone: when viewport center is in a page gap, do not emit
1670
+ // visiblePages events at all to prevent JS/native flicker loops.
1671
+ if (isViewportCenterInsidePageGap()) {
1672
+ if (DEBUG_GAP) {
1673
+ Log.d(TAG, "emitVisiblePagesChanged: center in gap, skipping. " +
1674
+ "zoom=" + zoom + " offsetY=" + offsetY + " lastStablePage=" + lastStablePage);
1675
+ }
1676
+ return;
1677
+ }
1678
+ try {
1679
+ VisiblePagesResult result = buildVisiblePagesResult();
1680
+
1681
+ // Do not emit empty visiblePages when viewport is in a page gap.
1682
+ // Preserve the last stable signature to prevent JS/native loop and flicker.
1683
+ if (!result.hasStableVisiblePage) {
1684
+ if (DEBUG_GAP) {
1685
+ Log.d(TAG, "emitVisiblePagesChanged: no stable page (gap), skipping. " +
1686
+ "zoom=" + zoom + " offsetY=" + offsetY + " lastStablePage=" + lastStablePage);
1687
+ }
1688
+ return;
1689
+ }
1690
+
1691
+ if (result.signature.equals(lastVisiblePagesSignature)) {
1692
+ return;
1693
+ }
1694
+
1695
+ lastVisiblePagesSignature = result.signature;
1696
+
1697
+ if (DEBUG_GAP) {
1698
+ Log.d(TAG, "emitVisiblePagesChanged: signature=" + result.signature +
1699
+ " zoom=" + zoom + " offsetY=" + offsetY + " lastStablePage=" + lastStablePage);
1700
+ }
1701
+
1702
+ WritableMap event = Arguments.createMap();
1703
+ event.putArray("pages", result.pages);
1704
+ reactContext.getJSModule(RCTEventEmitter.class)
1705
+ .receiveEvent(getId(), "onVisiblePagesChange", event);
1706
+ } catch (Throwable ignored) {
1707
+ }
1708
+ }
1709
+
1710
+ private static class VisiblePagesResult {
1711
+ final WritableArray pages;
1712
+ final String signature;
1713
+ final boolean hasStableVisiblePage;
1714
+
1715
+ VisiblePagesResult(WritableArray pages, String signature, boolean hasStableVisiblePage) {
1716
+ this.pages = pages;
1717
+ this.signature = signature;
1718
+ this.hasStableVisiblePage = hasStableVisiblePage;
1719
+ }
1720
+ }
1721
+
1722
+ private VisiblePagesResult buildVisiblePagesResult() {
1723
+ WritableArray pages = Arguments.createArray();
1724
+ StringBuilder signature = new StringBuilder();
1725
+
1726
+ if (pageFrames.isEmpty() || getHeight() <= 0) {
1727
+ return new VisiblePagesResult(pages, "", false);
1728
+ }
1729
+
1730
+ float viewportTop = offsetY;
1731
+ float viewportBottom = offsetY + getHeight();
1732
+ float threshold = getVisibleRatioThreshold();
1733
+
1734
+ for (PageFrame frame : pageFrames) {
1735
+ float frameTop = frame.top;
1736
+ float frameBottom = frame.top + frame.height;
1737
+ float visibleTop = Math.max(frameTop, viewportTop);
1738
+ float visibleBottom = Math.min(frameBottom, viewportBottom);
1739
+ float visibleHeight = visibleBottom - visibleTop;
1740
+
1741
+ if (visibleHeight <= 0f || frame.height <= 0f) continue;
1742
+
1743
+ float ratio = clamp(visibleHeight / frame.height, 0f, 1f);
1744
+
1745
+ // Ignore micro-visibility near page gaps to avoid jitter
1746
+ if (ratio < threshold) continue;
1747
+
1748
+ // Bucket ratio to prevent tiny fluctuations from triggering events
1749
+ float bucketedRatio =
1750
+ Math.round(ratio / VISIBLE_RATIO_BUCKET) * VISIBLE_RATIO_BUCKET;
1751
+
1752
+ WritableMap item = Arguments.createMap();
1753
+ item.putInt("pageIndex", frame.index);
1754
+ item.putDouble("visibleRatio", bucketedRatio);
1755
+ pages.pushMap(item);
1756
+
1757
+ signature
1758
+ .append(frame.index)
1759
+ .append(":")
1760
+ .append(Math.round(bucketedRatio * 100))
1761
+ .append(";");
1762
+ }
1763
+
1764
+ boolean hasStableVisiblePage = signature.length() > 0;
1765
+ return new VisiblePagesResult(pages, signature.toString(), hasStableVisiblePage);
1766
+ }
1767
+
1377
1768
  private static float clamp(float value, float min, float max) {
1378
1769
  return Math.max(min, Math.min(max, value));
1379
1770
  }
@@ -1616,13 +2007,17 @@ public class PapyrusPdfViewerView extends View {
1616
2007
  final float top;
1617
2008
  final float width;
1618
2009
  final float height;
2010
+ final int pageWidth;
2011
+ final int pageHeight;
1619
2012
 
1620
- PageFrame(int index, float left, float top, float width, float height) {
2013
+ PageFrame(int index, float left, float top, float width, float height, int pageWidth, int pageHeight) {
1621
2014
  this.index = index;
1622
2015
  this.left = left;
1623
2016
  this.top = top;
1624
2017
  this.width = width;
1625
2018
  this.height = height;
2019
+ this.pageWidth = pageWidth;
2020
+ this.pageHeight = pageHeight;
1626
2021
  }
1627
2022
  }
1628
2023
  }