@papyrus-sdk/engine-native 0.2.19 → 0.2.20
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.
|
@@ -19,6 +19,7 @@ import android.view.MotionEvent;
|
|
|
19
19
|
import android.view.ScaleGestureDetector;
|
|
20
20
|
import android.view.VelocityTracker;
|
|
21
21
|
import android.view.View;
|
|
22
|
+
import android.view.ViewParent;
|
|
22
23
|
import android.widget.OverScroller;
|
|
23
24
|
|
|
24
25
|
import com.facebook.react.bridge.Arguments;
|
|
@@ -138,11 +139,6 @@ public class PapyrusPdfViewerView extends View {
|
|
|
138
139
|
private static final float DOUBLE_TAP_MAX_DISTANCE_DP = 20;
|
|
139
140
|
private Runnable pendingSingleTap;
|
|
140
141
|
|
|
141
|
-
// Long-press detection
|
|
142
|
-
private static final long LONG_PRESS_MS = 450;
|
|
143
|
-
private Runnable pendingLongPress;
|
|
144
|
-
private boolean longPressFired = false;
|
|
145
|
-
|
|
146
142
|
// Selection handles
|
|
147
143
|
private static final float HANDLE_RADIUS_DP = 10;
|
|
148
144
|
private static final int HANDLE_COLOR = Color.parseColor("#4285F4");
|
|
@@ -329,16 +325,15 @@ public class PapyrusPdfViewerView extends View {
|
|
|
329
325
|
|
|
330
326
|
@Override
|
|
331
327
|
public boolean onTouchEvent(MotionEvent event) {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
mainHandler.removeCallbacks(pendingLongPress);
|
|
338
|
-
pendingLongPress = null;
|
|
339
|
-
}
|
|
328
|
+
int action = event.getActionMasked();
|
|
329
|
+
if (action == MotionEvent.ACTION_DOWN && event.getPointerCount() == 1) {
|
|
330
|
+
requestDisallowParentInterception(true);
|
|
331
|
+
} else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
|
|
332
|
+
requestDisallowParentInterception(false);
|
|
340
333
|
}
|
|
341
334
|
|
|
335
|
+
scaleDetector.onTouchEvent(event);
|
|
336
|
+
|
|
342
337
|
if ("ink".equals(activeTool) && event.getPointerCount() == 1) {
|
|
343
338
|
return handleInkTouch(event);
|
|
344
339
|
}
|
|
@@ -539,7 +534,6 @@ public class PapyrusPdfViewerView extends View {
|
|
|
539
534
|
touchDownTime = System.currentTimeMillis();
|
|
540
535
|
touchDownX = event.getX();
|
|
541
536
|
touchDownY = event.getY();
|
|
542
|
-
longPressFired = false;
|
|
543
537
|
draggedHandle = 0;
|
|
544
538
|
|
|
545
539
|
// Check if touching a selection handle
|
|
@@ -553,10 +547,14 @@ public class PapyrusPdfViewerView extends View {
|
|
|
553
547
|
}
|
|
554
548
|
return true;
|
|
555
549
|
}
|
|
556
|
-
//
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
550
|
+
// Any new touch that is not on a selection handle starts normal
|
|
551
|
+
// scrolling. Keeping the selection body "active" here made a
|
|
552
|
+
// regular finger drag look like a stuck scroll after double-tap.
|
|
553
|
+
if (PapyrusTextSelectionGesture.shouldContinueAfterSelectionTouch(handleHit != 0)) {
|
|
554
|
+
isSelectingText = false;
|
|
555
|
+
selectPageIndex = -1;
|
|
556
|
+
selectedRects.clear();
|
|
557
|
+
selectedText = "";
|
|
560
558
|
}
|
|
561
559
|
}
|
|
562
560
|
|
|
@@ -571,41 +569,9 @@ public class PapyrusPdfViewerView extends View {
|
|
|
571
569
|
lastTouchX = event.getX();
|
|
572
570
|
lastTouchY = event.getY();
|
|
573
571
|
|
|
574
|
-
// Start long-press timer
|
|
575
|
-
pendingLongPress = () -> {
|
|
576
|
-
pendingLongPress = null;
|
|
577
|
-
longPressFired = true;
|
|
578
|
-
// Long press: select word at this point
|
|
579
|
-
ensureLayout();
|
|
580
|
-
float docX = touchDownX + offsetX;
|
|
581
|
-
float docY = touchDownY + offsetY;
|
|
582
|
-
int pageIdx = findPageIndexAt(docX, docY);
|
|
583
|
-
if (pageIdx >= 0) {
|
|
584
|
-
PageFrame frame = pageFrames.get(pageIdx);
|
|
585
|
-
float nx = clamp01((docX - frame.left) / frame.width);
|
|
586
|
-
float ny = clamp01((docY - frame.top) / frame.height);
|
|
587
|
-
isSelectingText = true;
|
|
588
|
-
selectPageIndex = pageIdx;
|
|
589
|
-
selectStartX = nx;
|
|
590
|
-
selectStartY = ny;
|
|
591
|
-
selectEndX = nx;
|
|
592
|
-
selectEndY = ny;
|
|
593
|
-
performWordSelection();
|
|
594
|
-
}
|
|
595
|
-
};
|
|
596
|
-
mainHandler.postDelayed(pendingLongPress, LONG_PRESS_MS);
|
|
597
572
|
return true;
|
|
598
573
|
|
|
599
574
|
case MotionEvent.ACTION_MOVE:
|
|
600
|
-
if (pendingLongPress != null) {
|
|
601
|
-
float cancelDx = event.getX() - touchDownX;
|
|
602
|
-
float cancelDy = event.getY() - touchDownY;
|
|
603
|
-
if (Math.hypot(cancelDx, cancelDy) > TAP_MAX_DISTANCE_DP * density) {
|
|
604
|
-
mainHandler.removeCallbacks(pendingLongPress);
|
|
605
|
-
pendingLongPress = null;
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
|
|
609
575
|
if (draggedHandle != 0 && isSelectingText && selectPageIndex >= 0) {
|
|
610
576
|
ensureLayout();
|
|
611
577
|
float moveDocX = event.getX() + offsetX;
|
|
@@ -620,24 +586,14 @@ public class PapyrusPdfViewerView extends View {
|
|
|
620
586
|
if (moveFrame != null) {
|
|
621
587
|
float nx = clamp01((moveDocX - moveFrame.left) / moveFrame.width);
|
|
622
588
|
float ny = clamp01((moveDocY - moveFrame.top) / moveFrame.height);
|
|
623
|
-
// Line-based selection: when dragging vertically across lines,
|
|
624
|
-
// snap X to page edge so the entire line gets selected
|
|
625
|
-
float lineHeight = 0.025f;
|
|
626
589
|
if (draggedHandle < 0) {
|
|
627
|
-
//
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
} else {
|
|
631
|
-
selectStartX = nx;
|
|
632
|
-
}
|
|
590
|
+
// Preserve the actual horizontal position instead of expanding
|
|
591
|
+
// the selection to the whole line when crossing line breaks.
|
|
592
|
+
selectStartX = PapyrusTextSelectionGesture.resolveSelectionEndpoint(nx);
|
|
633
593
|
selectStartY = ny;
|
|
634
594
|
} else {
|
|
635
|
-
//
|
|
636
|
-
|
|
637
|
-
selectEndX = 1f;
|
|
638
|
-
} else {
|
|
639
|
-
selectEndX = nx;
|
|
640
|
-
}
|
|
595
|
+
// Preserve the actual horizontal position for the end handle too.
|
|
596
|
+
selectEndX = PapyrusTextSelectionGesture.resolveSelectionEndpoint(nx);
|
|
641
597
|
selectEndY = ny;
|
|
642
598
|
}
|
|
643
599
|
performTextSelection();
|
|
@@ -677,13 +633,8 @@ public class PapyrusPdfViewerView extends View {
|
|
|
677
633
|
if (moveFrame != null) {
|
|
678
634
|
float nx = clamp01((moveDocX - moveFrame.left) / moveFrame.width);
|
|
679
635
|
float ny = clamp01((moveDocY - moveFrame.top) / moveFrame.height);
|
|
680
|
-
//
|
|
681
|
-
|
|
682
|
-
if (Math.abs(ny - selectStartY) > lineHeight * 0.6f) {
|
|
683
|
-
selectEndX = 1f;
|
|
684
|
-
} else {
|
|
685
|
-
selectEndX = nx;
|
|
686
|
-
}
|
|
636
|
+
// Keep the endpoint under the finger while dragging across lines.
|
|
637
|
+
selectEndX = PapyrusTextSelectionGesture.resolveSelectionEndpoint(nx);
|
|
687
638
|
selectEndY = ny;
|
|
688
639
|
performTextSelection();
|
|
689
640
|
}
|
|
@@ -694,11 +645,6 @@ public class PapyrusPdfViewerView extends View {
|
|
|
694
645
|
|
|
695
646
|
case MotionEvent.ACTION_UP:
|
|
696
647
|
case MotionEvent.ACTION_CANCEL:
|
|
697
|
-
if (pendingLongPress != null) {
|
|
698
|
-
mainHandler.removeCallbacks(pendingLongPress);
|
|
699
|
-
pendingLongPress = null;
|
|
700
|
-
}
|
|
701
|
-
|
|
702
648
|
draggedHandle = 0;
|
|
703
649
|
|
|
704
650
|
if (!scaleDetector.isInProgress() && velocityTracker != null) {
|
|
@@ -744,8 +690,8 @@ public class PapyrusPdfViewerView extends View {
|
|
|
744
690
|
float upDy = event.getY() - touchDownY;
|
|
745
691
|
float upDistance = (float) Math.hypot(upDx, upDy);
|
|
746
692
|
|
|
747
|
-
if (
|
|
748
|
-
if (isSelectingText
|
|
693
|
+
if (duration > TAP_MAX_DURATION_MS || upDistance > TAP_MAX_DISTANCE_DP * density) {
|
|
694
|
+
if (isSelectingText) {
|
|
749
695
|
emitTextSelected();
|
|
750
696
|
}
|
|
751
697
|
return true;
|
|
@@ -762,7 +708,38 @@ public class PapyrusPdfViewerView extends View {
|
|
|
762
708
|
pendingSingleTap = null;
|
|
763
709
|
}
|
|
764
710
|
|
|
765
|
-
if (isDoubleTap) {
|
|
711
|
+
if (PapyrusTextSelectionGesture.shouldEmitPageTap(isDoubleTap)) {
|
|
712
|
+
lastTapTime = now;
|
|
713
|
+
lastTapX = event.getX();
|
|
714
|
+
lastTapY = event.getY();
|
|
715
|
+
final float tapX = event.getX();
|
|
716
|
+
final float tapY = event.getY();
|
|
717
|
+
pendingSingleTap = () -> {
|
|
718
|
+
pendingSingleTap = null;
|
|
719
|
+
if (isSelectingText) {
|
|
720
|
+
isSelectingText = false;
|
|
721
|
+
selectPageIndex = -1;
|
|
722
|
+
selectedRects.clear();
|
|
723
|
+
selectedText = "";
|
|
724
|
+
invalidate();
|
|
725
|
+
}
|
|
726
|
+
ensureLayout();
|
|
727
|
+
float docX = tapX + offsetX;
|
|
728
|
+
float docY = tapY + offsetY;
|
|
729
|
+
int pageIdx = findPageIndexAt(docX, docY);
|
|
730
|
+
if (pageIdx < 0) return;
|
|
731
|
+
PageFrame frame = pageFrames.get(pageIdx);
|
|
732
|
+
float nx = (docX - frame.left) / frame.width;
|
|
733
|
+
float ny = (docY - frame.top) / frame.height;
|
|
734
|
+
Annotation hit = findAnnotationAt(pageIdx, nx, ny);
|
|
735
|
+
if (hit != null) {
|
|
736
|
+
emitAnnotationTap(hit);
|
|
737
|
+
} else {
|
|
738
|
+
emitTap(pageIdx, clamp01(nx), clamp01(ny));
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
mainHandler.postDelayed(pendingSingleTap, DOUBLE_TAP_MAX_DELTA_MS + 50);
|
|
742
|
+
} else {
|
|
766
743
|
lastTapTime = 0;
|
|
767
744
|
ensureLayout();
|
|
768
745
|
float docX = event.getX() + offsetX;
|
|
@@ -780,21 +757,6 @@ public class PapyrusPdfViewerView extends View {
|
|
|
780
757
|
selectEndY = ny;
|
|
781
758
|
performTextSelection();
|
|
782
759
|
}
|
|
783
|
-
} else {
|
|
784
|
-
lastTapTime = now;
|
|
785
|
-
lastTapX = event.getX();
|
|
786
|
-
lastTapY = event.getY();
|
|
787
|
-
pendingSingleTap = () -> {
|
|
788
|
-
pendingSingleTap = null;
|
|
789
|
-
if (isSelectingText) {
|
|
790
|
-
isSelectingText = false;
|
|
791
|
-
selectPageIndex = -1;
|
|
792
|
-
selectedRects.clear();
|
|
793
|
-
selectedText = "";
|
|
794
|
-
invalidate();
|
|
795
|
-
}
|
|
796
|
-
};
|
|
797
|
-
mainHandler.postDelayed(pendingSingleTap, DOUBLE_TAP_MAX_DELTA_MS + 50);
|
|
798
760
|
}
|
|
799
761
|
return true;
|
|
800
762
|
default:
|
|
@@ -802,30 +764,6 @@ public class PapyrusPdfViewerView extends View {
|
|
|
802
764
|
}
|
|
803
765
|
}
|
|
804
766
|
|
|
805
|
-
private boolean hitTestSelection(float screenX, float screenY) {
|
|
806
|
-
if (!isSelectingText || selectPageIndex < 0 || selectedRects.isEmpty()) return false;
|
|
807
|
-
PageFrame frame = null;
|
|
808
|
-
for (PageFrame f : pageFrames) {
|
|
809
|
-
if (f.index == selectPageIndex) {
|
|
810
|
-
frame = f;
|
|
811
|
-
break;
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
if (frame == null) return false;
|
|
815
|
-
|
|
816
|
-
float nx = clamp01((screenX + offsetX - frame.left) / frame.width);
|
|
817
|
-
float ny = clamp01((screenY + offsetY - frame.top) / frame.height);
|
|
818
|
-
|
|
819
|
-
// Check if point is inside any selected rect
|
|
820
|
-
for (NormalizedRect rect : selectedRects) {
|
|
821
|
-
if (nx >= rect.x && nx <= rect.x + rect.width &&
|
|
822
|
-
ny >= rect.y && ny <= rect.y + rect.height) {
|
|
823
|
-
return true;
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
return false;
|
|
827
|
-
}
|
|
828
|
-
|
|
829
767
|
private int hitTestHandle(float screenX, float screenY, float density) {
|
|
830
768
|
if (selectedRects.isEmpty() || selectPageIndex < 0) return 0;
|
|
831
769
|
PageFrame frame = null;
|
|
@@ -854,81 +792,6 @@ public class PapyrusPdfViewerView extends View {
|
|
|
854
792
|
return 0;
|
|
855
793
|
}
|
|
856
794
|
|
|
857
|
-
private void performWordSelection() {
|
|
858
|
-
if (selectPageIndex < 0) return;
|
|
859
|
-
PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
|
|
860
|
-
if (state == null || state.document == null) return;
|
|
861
|
-
if (!PapyrusTextSelect.AVAILABLE) return;
|
|
862
|
-
|
|
863
|
-
// Use a larger rect to capture the whole word
|
|
864
|
-
float wordW = 0.05f;
|
|
865
|
-
float wordH = 0.03f;
|
|
866
|
-
float minX = Math.max(0f, selectStartX - wordW / 2f);
|
|
867
|
-
float minY = Math.max(0f, selectStartY - wordH / 2f);
|
|
868
|
-
float maxX = Math.min(1f, selectStartX + wordW / 2f);
|
|
869
|
-
float maxY = Math.min(1f, selectStartY + wordH / 2f);
|
|
870
|
-
|
|
871
|
-
final int pageIdx = selectPageIndex;
|
|
872
|
-
final float selX = minX;
|
|
873
|
-
final float selY = minY;
|
|
874
|
-
final float selW = Math.max(0.001f, maxX - minX);
|
|
875
|
-
final float selH = Math.max(0.001f, maxY - minY);
|
|
876
|
-
|
|
877
|
-
final String sourcePath = state.sourcePath;
|
|
878
|
-
SELECT_EXECUTOR.execute(() -> {
|
|
879
|
-
PapyrusTextSelection selection = null;
|
|
880
|
-
try {
|
|
881
|
-
if (sourcePath != null && !sourcePath.isEmpty()) {
|
|
882
|
-
synchronized (state.pdfiumLock) {
|
|
883
|
-
selection = PapyrusTextSelect.nativeSelectTextFile(sourcePath, pageIdx, selX, selY, selW, selH);
|
|
884
|
-
}
|
|
885
|
-
} else {
|
|
886
|
-
long docPtr;
|
|
887
|
-
synchronized (state.pdfiumLock) {
|
|
888
|
-
docPtr = extractNativeDocPointer(state.document);
|
|
889
|
-
}
|
|
890
|
-
if (docPtr != 0) {
|
|
891
|
-
synchronized (state.pdfiumLock) {
|
|
892
|
-
selection = PapyrusTextSelect.nativeSelectText(docPtr, pageIdx, selX, selY, selW, selH);
|
|
893
|
-
}
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
} catch (Throwable ignored) {
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
final PapyrusTextSelection finalSelection = selection;
|
|
900
|
-
mainHandler.post(() -> {
|
|
901
|
-
if (!isSelectingText && selectPageIndex != pageIdx) return;
|
|
902
|
-
selectedRects.clear();
|
|
903
|
-
if (finalSelection != null && finalSelection.rects != null && finalSelection.rects.length >= 4) {
|
|
904
|
-
selectedText = finalSelection.text != null ? finalSelection.text : "";
|
|
905
|
-
for (int i = 0; i + 3 < finalSelection.rects.length; i += 4) {
|
|
906
|
-
selectedRects.add(new NormalizedRect(
|
|
907
|
-
finalSelection.rects[i],
|
|
908
|
-
finalSelection.rects[i + 1],
|
|
909
|
-
finalSelection.rects[i + 2],
|
|
910
|
-
finalSelection.rects[i + 3]
|
|
911
|
-
));
|
|
912
|
-
}
|
|
913
|
-
// Set selection bounds to cover the word
|
|
914
|
-
if (!selectedRects.isEmpty()) {
|
|
915
|
-
float sx = selectedRects.get(0).x;
|
|
916
|
-
float sy = selectedRects.get(0).y;
|
|
917
|
-
float ex = selectedRects.get(selectedRects.size() - 1).x + selectedRects.get(selectedRects.size() - 1).width;
|
|
918
|
-
float ey = selectedRects.get(selectedRects.size() - 1).y + selectedRects.get(selectedRects.size() - 1).height;
|
|
919
|
-
selectStartX = sx;
|
|
920
|
-
selectStartY = sy;
|
|
921
|
-
selectEndX = ex;
|
|
922
|
-
selectEndY = ey;
|
|
923
|
-
}
|
|
924
|
-
} else {
|
|
925
|
-
selectedText = "";
|
|
926
|
-
}
|
|
927
|
-
invalidate();
|
|
928
|
-
});
|
|
929
|
-
});
|
|
930
|
-
}
|
|
931
|
-
|
|
932
795
|
private void performTextSelection() {
|
|
933
796
|
if (selectPageIndex < 0) return;
|
|
934
797
|
PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
|
|
@@ -1679,6 +1542,13 @@ public class PapyrusPdfViewerView extends View {
|
|
|
1679
1542
|
}
|
|
1680
1543
|
}
|
|
1681
1544
|
|
|
1545
|
+
private void requestDisallowParentInterception(boolean disallow) {
|
|
1546
|
+
ViewParent parent = getParent();
|
|
1547
|
+
if (parent != null) {
|
|
1548
|
+
parent.requestDisallowInterceptTouchEvent(disallow);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1682
1552
|
private void emitVisiblePagesChanged() {
|
|
1683
1553
|
if (ISOLATE_RENDER_TEST) return;
|
|
1684
1554
|
if (isPinching) return;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
package com.papyrus.engine;
|
|
2
|
+
|
|
3
|
+
/** Small, platform-independent rules for the PDF text-selection gesture. */
|
|
4
|
+
final class PapyrusTextSelectionGesture {
|
|
5
|
+
private PapyrusTextSelectionGesture() {}
|
|
6
|
+
|
|
7
|
+
static boolean shouldActivate(boolean isDoubleTap) {
|
|
8
|
+
return isDoubleTap;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static boolean shouldEmitPageTap(boolean isDoubleTap) {
|
|
12
|
+
return !isDoubleTap;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static boolean shouldContinueAfterSelectionTouch(boolean hitHandle) {
|
|
16
|
+
return !hitHandle;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Keep the selection endpoint where the user's finger actually landed. */
|
|
20
|
+
static float resolveSelectionEndpoint(float normalizedX) {
|
|
21
|
+
return Math.max(0f, Math.min(1f, normalizedX));
|
|
22
|
+
}
|
|
23
|
+
}
|