@papyrus-sdk/engine-native 0.2.19 → 0.2.21
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.
- package/LICENSE +21 -0
- package/android/src/main/cpp/CMakeLists.txt +12 -1
- package/android/src/main/cpp/papyrus_outline_loader_test.cpp +240 -0
- package/android/src/main/cpp/papyrus_text_search.cpp +162 -24
- package/android/src/main/cpp/papyrus_text_word.cpp +33 -0
- package/android/src/main/cpp/papyrus_text_word.h +19 -0
- package/android/src/main/cpp/papyrus_text_word_test.cpp +72 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +3 -3
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.kt +11 -3
- package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +296 -285
- package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerViewManager.java +9 -7
- package/android/src/main/java/com/papyrus/engine/PapyrusTextSelect.java +2 -2
- package/android/src/main/java/com/papyrus/engine/PapyrusTextSelectionGesture.java +65 -0
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +11 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +11 -6
- package/dist/index.mjs.map +1 -1
- package/ios/PapyrusNativeEngine.m +184 -0
- package/package.json +5 -5
|
@@ -19,6 +19,8 @@ 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.ViewConfiguration;
|
|
23
|
+
import android.view.ViewParent;
|
|
22
24
|
import android.widget.OverScroller;
|
|
23
25
|
|
|
24
26
|
import com.facebook.react.bridge.Arguments;
|
|
@@ -134,20 +136,36 @@ public class PapyrusPdfViewerView extends View {
|
|
|
134
136
|
private long lastTapTime = 0;
|
|
135
137
|
private float lastTapX = 0;
|
|
136
138
|
private float lastTapY = 0;
|
|
137
|
-
private static final long DOUBLE_TAP_MAX_DELTA_MS = 250;
|
|
138
139
|
private static final float DOUBLE_TAP_MAX_DISTANCE_DP = 20;
|
|
140
|
+
private final long doubleTapTimeoutMs;
|
|
139
141
|
private Runnable pendingSingleTap;
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
private static final long LONG_PRESS_MS = 450;
|
|
143
|
-
private Runnable pendingLongPress;
|
|
144
|
-
private boolean longPressFired = false;
|
|
142
|
+
private boolean suppressPageTapForGesture = false;
|
|
143
|
+
private boolean insideSelectionAtDown = false;
|
|
145
144
|
|
|
146
145
|
// Selection handles
|
|
147
146
|
private static final float HANDLE_RADIUS_DP = 10;
|
|
147
|
+
private static final float HANDLE_TOUCH_RADIUS_DP = 24;
|
|
148
148
|
private static final int HANDLE_COLOR = Color.parseColor("#4285F4");
|
|
149
149
|
private int draggedHandle = 0; // 0 = none, -1 = start handle, 1 = end handle
|
|
150
150
|
|
|
151
|
+
// Coalesced text selection requests ("latest request wins")
|
|
152
|
+
private final Object selectionLock = new Object();
|
|
153
|
+
private boolean selectionRequestInFlight = false;
|
|
154
|
+
private boolean selectionRequestPending = false;
|
|
155
|
+
private boolean selectionRequestEmit = false;
|
|
156
|
+
private long selectionRequestVersion = 0;
|
|
157
|
+
private long selectionRequestQueuedVersion = 0;
|
|
158
|
+
private String selectionRequestEngineId = null;
|
|
159
|
+
private int selectionRequestPage = -1;
|
|
160
|
+
private float selectionRequestRectX = 0f;
|
|
161
|
+
private float selectionRequestRectY = 0f;
|
|
162
|
+
private float selectionRequestRectW = 0f;
|
|
163
|
+
private float selectionRequestRectH = 0f;
|
|
164
|
+
private float selectionRequestStartX = 0f;
|
|
165
|
+
private float selectionRequestStartY = 0f;
|
|
166
|
+
private float selectionRequestEndX = 0f;
|
|
167
|
+
private float selectionRequestEndY = 0f;
|
|
168
|
+
|
|
151
169
|
private final ReactContext reactContext;
|
|
152
170
|
private final Handler eventThrottleHandler = new Handler(Looper.getMainLooper());
|
|
153
171
|
private Runnable pendingZoomEvent;
|
|
@@ -194,6 +212,7 @@ public class PapyrusPdfViewerView extends View {
|
|
|
194
212
|
super(context);
|
|
195
213
|
this.reactContext = context;
|
|
196
214
|
scaleDetector = createScaleDetector(context);
|
|
215
|
+
doubleTapTimeoutMs = ViewConfiguration.get(context).getDoubleTapTimeout();
|
|
197
216
|
flingScroller = new OverScroller(context);
|
|
198
217
|
}
|
|
199
218
|
|
|
@@ -201,6 +220,7 @@ public class PapyrusPdfViewerView extends View {
|
|
|
201
220
|
super(context, attrs);
|
|
202
221
|
this.reactContext = context;
|
|
203
222
|
scaleDetector = createScaleDetector(context);
|
|
223
|
+
doubleTapTimeoutMs = ViewConfiguration.get(context).getDoubleTapTimeout();
|
|
204
224
|
flingScroller = new OverScroller(context);
|
|
205
225
|
}
|
|
206
226
|
|
|
@@ -329,16 +349,15 @@ public class PapyrusPdfViewerView extends View {
|
|
|
329
349
|
|
|
330
350
|
@Override
|
|
331
351
|
public boolean onTouchEvent(MotionEvent event) {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
mainHandler.removeCallbacks(pendingLongPress);
|
|
338
|
-
pendingLongPress = null;
|
|
339
|
-
}
|
|
352
|
+
int action = event.getActionMasked();
|
|
353
|
+
if (action == MotionEvent.ACTION_DOWN && event.getPointerCount() == 1) {
|
|
354
|
+
requestDisallowParentInterception(true);
|
|
355
|
+
} else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
|
|
356
|
+
requestDisallowParentInterception(false);
|
|
340
357
|
}
|
|
341
358
|
|
|
359
|
+
scaleDetector.onTouchEvent(event);
|
|
360
|
+
|
|
342
361
|
if ("ink".equals(activeTool) && event.getPointerCount() == 1) {
|
|
343
362
|
return handleInkTouch(event);
|
|
344
363
|
}
|
|
@@ -532,18 +551,22 @@ public class PapyrusPdfViewerView extends View {
|
|
|
532
551
|
private boolean handleSelectTouch(MotionEvent event) {
|
|
533
552
|
float density = getResources().getDisplayMetrics().density;
|
|
534
553
|
switch (event.getActionMasked()) {
|
|
535
|
-
case MotionEvent.ACTION_DOWN:
|
|
554
|
+
case MotionEvent.ACTION_DOWN: {
|
|
536
555
|
if (event.getPointerCount() > 1) {
|
|
537
556
|
return true; // Multi-touch: ignore, let pinch zoom handle it
|
|
538
557
|
}
|
|
539
558
|
touchDownTime = System.currentTimeMillis();
|
|
540
559
|
touchDownX = event.getX();
|
|
541
560
|
touchDownY = event.getY();
|
|
542
|
-
longPressFired = false;
|
|
543
561
|
draggedHandle = 0;
|
|
562
|
+
insideSelectionAtDown = false;
|
|
544
563
|
|
|
545
|
-
|
|
546
|
-
|
|
564
|
+
boolean hasSelection =
|
|
565
|
+
isSelectingText && selectPageIndex >= 0 && !selectedRects.isEmpty();
|
|
566
|
+
suppressPageTapForGesture =
|
|
567
|
+
PapyrusTextSelectionGesture.shouldSuppressPageTap(hasSelection);
|
|
568
|
+
|
|
569
|
+
if (hasSelection) {
|
|
547
570
|
int handleHit = hitTestHandle(event.getX(), event.getY(), density);
|
|
548
571
|
if (handleHit != 0) {
|
|
549
572
|
draggedHandle = handleHit;
|
|
@@ -553,10 +576,14 @@ public class PapyrusPdfViewerView extends View {
|
|
|
553
576
|
}
|
|
554
577
|
return true;
|
|
555
578
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
579
|
+
boolean insideSelection =
|
|
580
|
+
isPointInsideSelection(event.getX(), event.getY(), density);
|
|
581
|
+
if (PapyrusTextSelectionGesture.shouldDismissSelectionOnTouch(
|
|
582
|
+
true, false, insideSelection)) {
|
|
583
|
+
clearSelectionState();
|
|
584
|
+
emitSelectionCleared();
|
|
585
|
+
} else {
|
|
586
|
+
insideSelectionAtDown = insideSelection;
|
|
560
587
|
}
|
|
561
588
|
}
|
|
562
589
|
|
|
@@ -570,87 +597,50 @@ public class PapyrusPdfViewerView extends View {
|
|
|
570
597
|
removeCallbacks(flingRunnable);
|
|
571
598
|
lastTouchX = event.getX();
|
|
572
599
|
lastTouchY = event.getY();
|
|
573
|
-
|
|
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
600
|
return true;
|
|
601
|
+
}
|
|
598
602
|
|
|
599
|
-
case MotionEvent.ACTION_MOVE:
|
|
600
|
-
if (
|
|
601
|
-
|
|
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
|
-
}
|
|
603
|
+
case MotionEvent.ACTION_MOVE: {
|
|
604
|
+
if (velocityTracker != null) {
|
|
605
|
+
velocityTracker.addMovement(event);
|
|
607
606
|
}
|
|
608
607
|
|
|
608
|
+
float moveDx = event.getX() - touchDownX;
|
|
609
|
+
float moveDy = event.getY() - touchDownY;
|
|
610
|
+
float moveDistance = (float) Math.hypot(moveDx, moveDy);
|
|
611
|
+
|
|
609
612
|
if (draggedHandle != 0 && isSelectingText && selectPageIndex >= 0) {
|
|
610
613
|
ensureLayout();
|
|
611
|
-
|
|
612
|
-
float moveDocY = event.getY() + offsetY;
|
|
613
|
-
PageFrame moveFrame = null;
|
|
614
|
-
for (PageFrame f : pageFrames) {
|
|
615
|
-
if (f.index == selectPageIndex) {
|
|
616
|
-
moveFrame = f;
|
|
617
|
-
break;
|
|
618
|
-
}
|
|
619
|
-
}
|
|
614
|
+
PageFrame moveFrame = findSelectFrame();
|
|
620
615
|
if (moveFrame != null) {
|
|
621
|
-
float nx = clamp01((
|
|
622
|
-
float ny = clamp01((
|
|
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;
|
|
616
|
+
float nx = clamp01((event.getX() + offsetX - moveFrame.left) / moveFrame.width);
|
|
617
|
+
float ny = clamp01((event.getY() + offsetY - moveFrame.top) / moveFrame.height);
|
|
626
618
|
if (draggedHandle < 0) {
|
|
627
|
-
|
|
628
|
-
if (Math.abs(ny - selectEndY) > lineHeight * 0.6f) {
|
|
629
|
-
selectStartX = 0f;
|
|
630
|
-
} else {
|
|
631
|
-
selectStartX = nx;
|
|
632
|
-
}
|
|
619
|
+
selectStartX = nx;
|
|
633
620
|
selectStartY = ny;
|
|
634
621
|
} else {
|
|
635
|
-
|
|
636
|
-
if (Math.abs(ny - selectStartY) > lineHeight * 0.6f) {
|
|
637
|
-
selectEndX = 1f;
|
|
638
|
-
} else {
|
|
639
|
-
selectEndX = nx;
|
|
640
|
-
}
|
|
622
|
+
selectEndX = nx;
|
|
641
623
|
selectEndY = ny;
|
|
642
624
|
}
|
|
643
|
-
performTextSelection();
|
|
625
|
+
performTextSelection(false);
|
|
644
626
|
}
|
|
627
|
+
lastTouchX = event.getX();
|
|
628
|
+
lastTouchY = event.getY();
|
|
645
629
|
return true;
|
|
646
630
|
}
|
|
647
631
|
|
|
648
|
-
if (
|
|
649
|
-
|
|
632
|
+
if (isSelectingText && selectPageIndex >= 0 && insideSelectionAtDown) {
|
|
633
|
+
if (!PapyrusTextSelectionGesture.shouldStartScrollFromSelection(
|
|
634
|
+
true, moveDistance, TAP_MAX_DISTANCE_DP * density)) {
|
|
635
|
+
lastTouchX = event.getX();
|
|
636
|
+
lastTouchY = event.getY();
|
|
637
|
+
return true;
|
|
638
|
+
}
|
|
639
|
+
clearSelectionState();
|
|
640
|
+
emitSelectionCleared();
|
|
641
|
+
insideSelectionAtDown = false;
|
|
650
642
|
}
|
|
651
|
-
|
|
652
|
-
float moveDy = event.getY() - touchDownY;
|
|
653
|
-
float moveDistance = (float) Math.hypot(moveDx, moveDy);
|
|
643
|
+
|
|
654
644
|
if (!isSelectingText && moveDistance > TAP_MAX_DISTANCE_DP * density) {
|
|
655
645
|
if (!scaleDetector.isInProgress() && event.getPointerCount() == 1) {
|
|
656
646
|
offsetX += lastTouchX - event.getX();
|
|
@@ -659,47 +649,27 @@ public class PapyrusPdfViewerView extends View {
|
|
|
659
649
|
emitScrollEvent(offsetY);
|
|
660
650
|
invalidate();
|
|
661
651
|
}
|
|
662
|
-
lastTouchX = event.getX();
|
|
663
|
-
lastTouchY = event.getY();
|
|
664
|
-
return true;
|
|
665
|
-
}
|
|
666
|
-
if (isSelectingText && selectPageIndex >= 0) {
|
|
667
|
-
ensureLayout();
|
|
668
|
-
float moveDocX = event.getX() + offsetX;
|
|
669
|
-
float moveDocY = event.getY() + offsetY;
|
|
670
|
-
PageFrame moveFrame = null;
|
|
671
|
-
for (PageFrame f : pageFrames) {
|
|
672
|
-
if (f.index == selectPageIndex) {
|
|
673
|
-
moveFrame = f;
|
|
674
|
-
break;
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
if (moveFrame != null) {
|
|
678
|
-
float nx = clamp01((moveDocX - moveFrame.left) / moveFrame.width);
|
|
679
|
-
float ny = clamp01((moveDocY - moveFrame.top) / moveFrame.height);
|
|
680
|
-
// Line-based selection: snap to page edge when crossing lines
|
|
681
|
-
float lineHeight = 0.025f;
|
|
682
|
-
if (Math.abs(ny - selectStartY) > lineHeight * 0.6f) {
|
|
683
|
-
selectEndX = 1f;
|
|
684
|
-
} else {
|
|
685
|
-
selectEndX = nx;
|
|
686
|
-
}
|
|
687
|
-
selectEndY = ny;
|
|
688
|
-
performTextSelection();
|
|
689
|
-
}
|
|
690
652
|
}
|
|
691
653
|
lastTouchX = event.getX();
|
|
692
654
|
lastTouchY = event.getY();
|
|
693
655
|
return true;
|
|
656
|
+
}
|
|
694
657
|
|
|
695
658
|
case MotionEvent.ACTION_UP:
|
|
696
|
-
case MotionEvent.ACTION_CANCEL:
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
659
|
+
case MotionEvent.ACTION_CANCEL: {
|
|
660
|
+
boolean wasHandleDrag = draggedHandle != 0;
|
|
661
|
+
draggedHandle = 0;
|
|
662
|
+
insideSelectionAtDown = false;
|
|
663
|
+
|
|
664
|
+
if (event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
|
|
665
|
+
suppressPageTapForGesture = false;
|
|
666
|
+
return true;
|
|
700
667
|
}
|
|
701
668
|
|
|
702
|
-
|
|
669
|
+
if (wasHandleDrag && selectPageIndex >= 0) {
|
|
670
|
+
performTextSelection(true);
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
703
673
|
|
|
704
674
|
if (!scaleDetector.isInProgress() && velocityTracker != null) {
|
|
705
675
|
velocityTracker.addMovement(event);
|
|
@@ -744,100 +714,127 @@ public class PapyrusPdfViewerView extends View {
|
|
|
744
714
|
float upDy = event.getY() - touchDownY;
|
|
745
715
|
float upDistance = (float) Math.hypot(upDx, upDy);
|
|
746
716
|
|
|
747
|
-
if (
|
|
748
|
-
if (isSelectingText && !longPressFired) {
|
|
749
|
-
emitTextSelected();
|
|
750
|
-
}
|
|
717
|
+
if (duration > TAP_MAX_DURATION_MS || upDistance > TAP_MAX_DISTANCE_DP * density) {
|
|
751
718
|
return true;
|
|
752
719
|
}
|
|
753
720
|
|
|
754
721
|
long now = System.currentTimeMillis();
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
722
|
+
boolean isDoubleTap = PapyrusTextSelectionGesture.resolveDoubleTap(
|
|
723
|
+
now,
|
|
724
|
+
lastTapTime,
|
|
725
|
+
event.getX() - lastTapX,
|
|
726
|
+
event.getY() - lastTapY,
|
|
727
|
+
doubleTapTimeoutMs,
|
|
728
|
+
DOUBLE_TAP_MAX_DISTANCE_DP * density
|
|
729
|
+
);
|
|
759
730
|
|
|
760
731
|
if (pendingSingleTap != null) {
|
|
761
732
|
mainHandler.removeCallbacks(pendingSingleTap);
|
|
762
733
|
pendingSingleTap = null;
|
|
763
734
|
}
|
|
764
735
|
|
|
765
|
-
if (isDoubleTap) {
|
|
766
|
-
lastTapTime = 0;
|
|
767
|
-
ensureLayout();
|
|
768
|
-
float docX = event.getX() + offsetX;
|
|
769
|
-
float docY = event.getY() + offsetY;
|
|
770
|
-
int pageIdx = findPageIndexAt(docX, docY);
|
|
771
|
-
if (pageIdx >= 0) {
|
|
772
|
-
PageFrame frame = pageFrames.get(pageIdx);
|
|
773
|
-
float nx = clamp01((docX - frame.left) / frame.width);
|
|
774
|
-
float ny = clamp01((docY - frame.top) / frame.height);
|
|
775
|
-
isSelectingText = true;
|
|
776
|
-
selectPageIndex = pageIdx;
|
|
777
|
-
selectStartX = nx;
|
|
778
|
-
selectStartY = ny;
|
|
779
|
-
selectEndX = nx;
|
|
780
|
-
selectEndY = ny;
|
|
781
|
-
performTextSelection();
|
|
782
|
-
}
|
|
783
|
-
} else {
|
|
736
|
+
if (PapyrusTextSelectionGesture.shouldEmitPageTap(isDoubleTap)) {
|
|
784
737
|
lastTapTime = now;
|
|
785
738
|
lastTapX = event.getX();
|
|
786
739
|
lastTapY = event.getY();
|
|
740
|
+
final float tapX = event.getX();
|
|
741
|
+
final float tapY = event.getY();
|
|
742
|
+
final boolean allowPageTap = !suppressPageTapForGesture;
|
|
743
|
+
suppressPageTapForGesture = false;
|
|
787
744
|
pendingSingleTap = () -> {
|
|
788
745
|
pendingSingleTap = null;
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
746
|
+
ensureLayout();
|
|
747
|
+
float docX = tapX + offsetX;
|
|
748
|
+
float docY = tapY + offsetY;
|
|
749
|
+
int pageIdx = findPageIndexAt(docX, docY);
|
|
750
|
+
if (pageIdx < 0) return;
|
|
751
|
+
PageFrame frame = pageFrames.get(pageIdx);
|
|
752
|
+
float nx = (docX - frame.left) / frame.width;
|
|
753
|
+
float ny = (docY - frame.top) / frame.height;
|
|
754
|
+
Annotation hit = findAnnotationAt(pageIdx, nx, ny);
|
|
755
|
+
if (hit != null) {
|
|
756
|
+
// O tap de anotação continua funcionando mesmo ao desselecionar.
|
|
757
|
+
emitAnnotationTap(hit);
|
|
758
|
+
} else if (allowPageTap) {
|
|
759
|
+
emitTap(pageIdx, clamp01(nx), clamp01(ny));
|
|
795
760
|
}
|
|
796
761
|
};
|
|
797
|
-
mainHandler.postDelayed(pendingSingleTap,
|
|
762
|
+
mainHandler.postDelayed(pendingSingleTap, doubleTapTimeoutMs + 50);
|
|
763
|
+
} else {
|
|
764
|
+
lastTapTime = 0;
|
|
765
|
+
suppressPageTapForGesture = false;
|
|
766
|
+
beginWordSelection(event.getX(), event.getY());
|
|
798
767
|
}
|
|
799
768
|
return true;
|
|
769
|
+
}
|
|
770
|
+
|
|
800
771
|
default:
|
|
801
772
|
return true;
|
|
802
773
|
}
|
|
803
774
|
}
|
|
804
775
|
|
|
805
|
-
private
|
|
806
|
-
if (
|
|
807
|
-
PageFrame frame
|
|
808
|
-
|
|
809
|
-
if (f.index == selectPageIndex) {
|
|
810
|
-
frame = f;
|
|
811
|
-
break;
|
|
812
|
-
}
|
|
776
|
+
private PageFrame findSelectFrame() {
|
|
777
|
+
if (selectPageIndex < 0) return null;
|
|
778
|
+
for (PageFrame frame : pageFrames) {
|
|
779
|
+
if (frame.index == selectPageIndex) return frame;
|
|
813
780
|
}
|
|
814
|
-
|
|
781
|
+
return null;
|
|
782
|
+
}
|
|
815
783
|
|
|
816
|
-
|
|
817
|
-
|
|
784
|
+
private void clearSelectionState() {
|
|
785
|
+
isSelectingText = false;
|
|
786
|
+
selectPageIndex = -1;
|
|
787
|
+
selectedRects.clear();
|
|
788
|
+
selectedText = "";
|
|
789
|
+
draggedHandle = 0;
|
|
790
|
+
invalidate();
|
|
791
|
+
}
|
|
818
792
|
|
|
819
|
-
|
|
793
|
+
private boolean isPointInsideSelection(float screenX, float screenY, float density) {
|
|
794
|
+
if (selectedRects.isEmpty()) return false;
|
|
795
|
+
PageFrame frame = findSelectFrame();
|
|
796
|
+
if (frame == null) return false;
|
|
797
|
+
float padding = 8f * density;
|
|
820
798
|
for (NormalizedRect rect : selectedRects) {
|
|
821
|
-
|
|
822
|
-
|
|
799
|
+
float left = frame.left - offsetX + rect.x * frame.width;
|
|
800
|
+
float top = frame.top - offsetY + rect.y * frame.height;
|
|
801
|
+
float right = left + rect.width * frame.width;
|
|
802
|
+
float bottom = top + rect.height * frame.height;
|
|
803
|
+
if (screenX >= left - padding && screenX <= right + padding &&
|
|
804
|
+
screenY >= top - padding && screenY <= bottom + padding) {
|
|
823
805
|
return true;
|
|
824
806
|
}
|
|
825
807
|
}
|
|
826
808
|
return false;
|
|
827
809
|
}
|
|
828
810
|
|
|
811
|
+
private void beginWordSelection(float screenX, float screenY) {
|
|
812
|
+
if (!PapyrusTextSelect.AVAILABLE) return;
|
|
813
|
+
PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
|
|
814
|
+
if (state == null || state.document == null) return;
|
|
815
|
+
ensureLayout();
|
|
816
|
+
float docX = screenX + offsetX;
|
|
817
|
+
float docY = screenY + offsetY;
|
|
818
|
+
int pageIdx = findPageIndexAt(docX, docY);
|
|
819
|
+
if (pageIdx < 0) return;
|
|
820
|
+
PageFrame frame = pageFrames.get(pageIdx);
|
|
821
|
+
float nx = clamp01((docX - frame.left) / frame.width);
|
|
822
|
+
float ny = clamp01((docY - frame.top) / frame.height);
|
|
823
|
+
isSelectingText = true;
|
|
824
|
+
selectPageIndex = pageIdx;
|
|
825
|
+
selectStartX = nx;
|
|
826
|
+
selectStartY = ny;
|
|
827
|
+
selectEndX = nx;
|
|
828
|
+
selectEndY = ny;
|
|
829
|
+
performTextSelection(true);
|
|
830
|
+
}
|
|
831
|
+
|
|
829
832
|
private int hitTestHandle(float screenX, float screenY, float density) {
|
|
830
833
|
if (selectedRects.isEmpty() || selectPageIndex < 0) return 0;
|
|
831
|
-
PageFrame frame =
|
|
832
|
-
for (PageFrame f : pageFrames) {
|
|
833
|
-
if (f.index == selectPageIndex) {
|
|
834
|
-
frame = f;
|
|
835
|
-
break;
|
|
836
|
-
}
|
|
837
|
-
}
|
|
834
|
+
PageFrame frame = findSelectFrame();
|
|
838
835
|
if (frame == null) return 0;
|
|
839
836
|
|
|
840
|
-
float radius =
|
|
837
|
+
float radius = HANDLE_TOUCH_RADIUS_DP * density;
|
|
841
838
|
|
|
842
839
|
// Start handle: top-left of first rect
|
|
843
840
|
NormalizedRect first = selectedRects.get(0);
|
|
@@ -854,107 +851,76 @@ public class PapyrusPdfViewerView extends View {
|
|
|
854
851
|
return 0;
|
|
855
852
|
}
|
|
856
853
|
|
|
857
|
-
private void
|
|
854
|
+
private void performTextSelection(boolean emitWhenReady) {
|
|
858
855
|
if (selectPageIndex < 0) return;
|
|
859
856
|
PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
|
|
860
857
|
if (state == null || state.document == null) return;
|
|
861
858
|
if (!PapyrusTextSelect.AVAILABLE) return;
|
|
862
859
|
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
860
|
+
synchronized (selectionLock) {
|
|
861
|
+
selectionRequestVersion += 1;
|
|
862
|
+
selectionRequestQueuedVersion = selectionRequestVersion;
|
|
863
|
+
selectionRequestEngineId = engineId;
|
|
864
|
+
selectionRequestPage = selectPageIndex;
|
|
865
|
+
selectionRequestRectX = Math.min(selectStartX, selectEndX);
|
|
866
|
+
selectionRequestRectY = Math.min(selectStartY, selectEndY);
|
|
867
|
+
selectionRequestRectW = Math.max(0.001f, Math.abs(selectEndX - selectStartX));
|
|
868
|
+
selectionRequestRectH = Math.max(0.001f, Math.abs(selectEndY - selectStartY));
|
|
869
|
+
selectionRequestStartX = selectStartX;
|
|
870
|
+
selectionRequestStartY = selectStartY;
|
|
871
|
+
selectionRequestEndX = selectEndX;
|
|
872
|
+
selectionRequestEndY = selectEndY;
|
|
873
|
+
selectionRequestEmit = selectionRequestEmit || emitWhenReady;
|
|
874
|
+
selectionRequestPending = true;
|
|
875
|
+
if (selectionRequestInFlight) return;
|
|
876
|
+
selectionRequestInFlight = true;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
final PapyrusEngineStore.EngineState requestState = state;
|
|
877
880
|
final String sourcePath = state.sourcePath;
|
|
878
|
-
SELECT_EXECUTOR.execute(() ->
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
881
|
+
SELECT_EXECUTOR.execute(() -> drainTextSelection(requestState, sourcePath));
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
private void drainTextSelection(PapyrusEngineStore.EngineState state, String sourcePath) {
|
|
885
|
+
while (true) {
|
|
886
|
+
int pageIdx;
|
|
887
|
+
float x;
|
|
888
|
+
float y;
|
|
889
|
+
float w;
|
|
890
|
+
float h;
|
|
891
|
+
float startX;
|
|
892
|
+
float startY;
|
|
893
|
+
float endX;
|
|
894
|
+
float endY;
|
|
895
|
+
boolean emit;
|
|
896
|
+
long version;
|
|
897
|
+
String requestEngineId;
|
|
898
|
+
synchronized (selectionLock) {
|
|
899
|
+
if (!selectionRequestPending) {
|
|
900
|
+
selectionRequestInFlight = false;
|
|
901
|
+
return;
|
|
895
902
|
}
|
|
896
|
-
|
|
903
|
+
selectionRequestPending = false;
|
|
904
|
+
pageIdx = selectionRequestPage;
|
|
905
|
+
x = selectionRequestRectX;
|
|
906
|
+
y = selectionRequestRectY;
|
|
907
|
+
w = selectionRequestRectW;
|
|
908
|
+
h = selectionRequestRectH;
|
|
909
|
+
startX = selectionRequestStartX;
|
|
910
|
+
startY = selectionRequestStartY;
|
|
911
|
+
endX = selectionRequestEndX;
|
|
912
|
+
endY = selectionRequestEndY;
|
|
913
|
+
emit = selectionRequestEmit;
|
|
914
|
+
selectionRequestEmit = false;
|
|
915
|
+
version = selectionRequestQueuedVersion;
|
|
916
|
+
requestEngineId = selectionRequestEngineId;
|
|
897
917
|
}
|
|
898
918
|
|
|
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
|
-
private void performTextSelection() {
|
|
933
|
-
if (selectPageIndex < 0) return;
|
|
934
|
-
PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
|
|
935
|
-
if (state == null || state.document == null) return;
|
|
936
|
-
if (!PapyrusTextSelect.AVAILABLE) return;
|
|
937
|
-
|
|
938
|
-
float minX = Math.min(selectStartX, selectEndX);
|
|
939
|
-
float minY = Math.min(selectStartY, selectEndY);
|
|
940
|
-
float maxX = Math.max(selectStartX, selectEndX);
|
|
941
|
-
float maxY = Math.max(selectStartY, selectEndY);
|
|
942
|
-
float width = Math.max(0.001f, maxX - minX);
|
|
943
|
-
float height = Math.max(0.001f, maxY - minY);
|
|
944
|
-
|
|
945
|
-
final int pageIdx = selectPageIndex;
|
|
946
|
-
final float selX = minX;
|
|
947
|
-
final float selY = minY;
|
|
948
|
-
final float selW = width;
|
|
949
|
-
final float selH = height;
|
|
950
|
-
|
|
951
|
-
final String sourcePath = state.sourcePath;
|
|
952
|
-
SELECT_EXECUTOR.execute(() -> {
|
|
953
919
|
PapyrusTextSelection selection = null;
|
|
954
920
|
try {
|
|
955
921
|
if (sourcePath != null && !sourcePath.isEmpty()) {
|
|
956
922
|
synchronized (state.pdfiumLock) {
|
|
957
|
-
selection = PapyrusTextSelect.nativeSelectTextFile(sourcePath, pageIdx,
|
|
923
|
+
selection = PapyrusTextSelect.nativeSelectTextFile(sourcePath, pageIdx, x, y, w, h, startX, startY, endX, endY);
|
|
958
924
|
}
|
|
959
925
|
} else {
|
|
960
926
|
long docPtr;
|
|
@@ -963,7 +929,7 @@ public class PapyrusPdfViewerView extends View {
|
|
|
963
929
|
}
|
|
964
930
|
if (docPtr != 0) {
|
|
965
931
|
synchronized (state.pdfiumLock) {
|
|
966
|
-
selection = PapyrusTextSelect.nativeSelectText(docPtr, pageIdx,
|
|
932
|
+
selection = PapyrusTextSelect.nativeSelectText(docPtr, pageIdx, x, y, w, h, startX, startY, endX, endY);
|
|
967
933
|
}
|
|
968
934
|
}
|
|
969
935
|
}
|
|
@@ -971,30 +937,56 @@ public class PapyrusPdfViewerView extends View {
|
|
|
971
937
|
}
|
|
972
938
|
|
|
973
939
|
final PapyrusTextSelection finalSelection = selection;
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
}
|
|
940
|
+
final long finalVersion = version;
|
|
941
|
+
final String finalEngineId = requestEngineId;
|
|
942
|
+
final int finalPage = pageIdx;
|
|
943
|
+
final boolean finalEmit = emit;
|
|
944
|
+
mainHandler.post(() -> applyTextSelection(
|
|
945
|
+
finalVersion, finalEngineId, finalPage, finalSelection, finalEmit));
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
private void applyTextSelection(
|
|
950
|
+
long version,
|
|
951
|
+
String requestEngineId,
|
|
952
|
+
int pageIdx,
|
|
953
|
+
PapyrusTextSelection selection,
|
|
954
|
+
boolean emit
|
|
955
|
+
) {
|
|
956
|
+
synchronized (selectionLock) {
|
|
957
|
+
if (version != selectionRequestVersion) return;
|
|
958
|
+
}
|
|
959
|
+
if (requestEngineId == null || !requestEngineId.equals(engineId)) return;
|
|
960
|
+
if (!isSelectingText || selectPageIndex != pageIdx) return;
|
|
961
|
+
|
|
962
|
+
selectedRects.clear();
|
|
963
|
+
if (selection != null && selection.rects != null && selection.rects.length >= 4) {
|
|
964
|
+
selectedText = selection.text != null ? selection.text : "";
|
|
965
|
+
for (int i = 0; i + 3 < selection.rects.length; i += 4) {
|
|
966
|
+
selectedRects.add(new NormalizedRect(
|
|
967
|
+
selection.rects[i],
|
|
968
|
+
selection.rects[i + 1],
|
|
969
|
+
selection.rects[i + 2],
|
|
970
|
+
selection.rects[i + 3]
|
|
971
|
+
));
|
|
972
|
+
}
|
|
973
|
+
} else {
|
|
974
|
+
selectedText = "";
|
|
975
|
+
// Movimentos de alça (emit == false) mantêm o gesto vivo para o UP
|
|
976
|
+
// pedir a extração final e emitir o estado vazio.
|
|
977
|
+
if (emit) {
|
|
978
|
+
isSelectingText = false;
|
|
979
|
+
selectPageIndex = -1;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
invalidate();
|
|
983
|
+
if (emit) {
|
|
984
|
+
emitTextSelected();
|
|
985
|
+
}
|
|
993
986
|
}
|
|
994
987
|
|
|
995
988
|
private void emitTextSelected() {
|
|
996
989
|
try {
|
|
997
|
-
if (selectedRects.isEmpty()) return;
|
|
998
990
|
WritableMap event = Arguments.createMap();
|
|
999
991
|
event.putString("text", selectedText);
|
|
1000
992
|
event.putInt("pageIndex", selectPageIndex);
|
|
@@ -1014,6 +1006,18 @@ public class PapyrusPdfViewerView extends View {
|
|
|
1014
1006
|
}
|
|
1015
1007
|
}
|
|
1016
1008
|
|
|
1009
|
+
private void emitSelectionCleared() {
|
|
1010
|
+
try {
|
|
1011
|
+
WritableMap event = Arguments.createMap();
|
|
1012
|
+
event.putString("text", "");
|
|
1013
|
+
event.putInt("pageIndex", -1);
|
|
1014
|
+
event.putArray("rects", Arguments.createArray());
|
|
1015
|
+
reactContext.getJSModule(RCTEventEmitter.class)
|
|
1016
|
+
.receiveEvent(getId(), "onTextSelected", event);
|
|
1017
|
+
} catch (Throwable ignored) {
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1017
1021
|
private int findPageIndexAt(float docX, float docY) {
|
|
1018
1022
|
for (PageFrame frame : pageFrames) {
|
|
1019
1023
|
if (docX >= frame.left && docX <= frame.left + frame.width &&
|
|
@@ -1670,7 +1674,7 @@ public class PapyrusPdfViewerView extends View {
|
|
|
1670
1674
|
WritableMap event = Arguments.createMap();
|
|
1671
1675
|
event.putDouble("offsetY", offsetYValue);
|
|
1672
1676
|
reactContext.getJSModule(RCTEventEmitter.class)
|
|
1673
|
-
.receiveEvent(getId(), "
|
|
1677
|
+
.receiveEvent(getId(), "topScroll", event);
|
|
1674
1678
|
emitVisiblePagesChanged();
|
|
1675
1679
|
pendingScrollEvent = null;
|
|
1676
1680
|
};
|
|
@@ -1679,6 +1683,13 @@ public class PapyrusPdfViewerView extends View {
|
|
|
1679
1683
|
}
|
|
1680
1684
|
}
|
|
1681
1685
|
|
|
1686
|
+
private void requestDisallowParentInterception(boolean disallow) {
|
|
1687
|
+
ViewParent parent = getParent();
|
|
1688
|
+
if (parent != null) {
|
|
1689
|
+
parent.requestDisallowInterceptTouchEvent(disallow);
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1682
1693
|
private void emitVisiblePagesChanged() {
|
|
1683
1694
|
if (ISOLATE_RENDER_TEST) return;
|
|
1684
1695
|
if (isPinching) return;
|