@amafil/react-native-pdf-toolkit 1.0.11 → 1.1.0
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/android/src/main/java/org/wonday/pdf/PdfManager.java +32 -0
- package/android/src/main/java/org/wonday/pdf/PdfView.java +1161 -0
- package/android/src/paper/java/com/facebook/react/viewmanagers/RNPDFPdfViewManagerDelegate.java +18 -0
- package/android/src/paper/java/com/facebook/react/viewmanagers/RNPDFPdfViewManagerInterface.java +6 -0
- package/fabric/RNPDFPdfNativeComponent.js +9 -1
- package/index.d.ts +68 -1
- package/index.js +89 -0
- package/index.js.flow +68 -0
- package/ios/RNPDFPdf/RNPDFPdfView.h +7 -0
- package/ios/RNPDFPdf/RNPDFPdfView.mm +1196 -0
- package/ios/RNPDFPdf/RNPDFPdfViewManager.mm +9 -16
- package/package.json +1 -1
|
@@ -24,8 +24,20 @@ import android.util.Log;
|
|
|
24
24
|
import android.net.Uri;
|
|
25
25
|
import android.util.AttributeSet;
|
|
26
26
|
import android.view.MotionEvent;
|
|
27
|
+
import android.view.ViewConfiguration;
|
|
27
28
|
import android.graphics.Canvas;
|
|
29
|
+
import android.graphics.Color;
|
|
30
|
+
import android.graphics.Paint;
|
|
31
|
+
import android.graphics.Path;
|
|
32
|
+
import android.graphics.PointF;
|
|
33
|
+
import android.graphics.RectF;
|
|
28
34
|
import android.graphics.pdf.PdfRenderer;
|
|
35
|
+
import android.widget.EditText;
|
|
36
|
+
import android.text.Editable;
|
|
37
|
+
import android.text.TextWatcher;
|
|
38
|
+
import android.text.TextUtils;
|
|
39
|
+
import android.view.Gravity;
|
|
40
|
+
import android.widget.FrameLayout;
|
|
29
41
|
|
|
30
42
|
import io.legere.pdfiumandroid.util.Config;
|
|
31
43
|
import io.legere.pdfiumandroid.util.ConfigKt;
|
|
@@ -55,6 +67,10 @@ import com.facebook.react.uimanager.events.EventDispatcher;
|
|
|
55
67
|
import com.facebook.react.uimanager.events.Event;
|
|
56
68
|
import com.facebook.react.uimanager.events.RCTEventEmitter;
|
|
57
69
|
|
|
70
|
+
import org.json.JSONArray;
|
|
71
|
+
import org.json.JSONException;
|
|
72
|
+
import org.json.JSONObject;
|
|
73
|
+
|
|
58
74
|
import static java.lang.String.format;
|
|
59
75
|
|
|
60
76
|
import java.io.FileNotFoundException;
|
|
@@ -76,6 +92,12 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
76
92
|
private boolean enableAntialiasing = true;
|
|
77
93
|
private boolean enableAnnotationRendering = true;
|
|
78
94
|
private boolean enableDoubleTapZoom = true;
|
|
95
|
+
private String annotations;
|
|
96
|
+
private boolean annotationMode = false;
|
|
97
|
+
private String annotationTool = "select";
|
|
98
|
+
private boolean annotationEditable = true;
|
|
99
|
+
private String annotationIdMode = "auto";
|
|
100
|
+
private AnnotationOverlayView annotationOverlayView;
|
|
79
101
|
|
|
80
102
|
private boolean enablePaging = false;
|
|
81
103
|
private boolean autoSpacing = false;
|
|
@@ -109,6 +131,10 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
109
131
|
super(context, set);
|
|
110
132
|
ConfigKt.setPdfiumConfig(new Config(new DefaultLogger(), AlreadyClosedBehavior.IGNORE));
|
|
111
133
|
autoScrollResumeHandler = new Handler(Looper.getMainLooper());
|
|
134
|
+
annotationOverlayView = new AnnotationOverlayView(context);
|
|
135
|
+
addView(annotationOverlayView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
|
136
|
+
bringChildToFront(annotationOverlayView);
|
|
137
|
+
updateAnnotationOverlayConfig();
|
|
112
138
|
}
|
|
113
139
|
|
|
114
140
|
@Override
|
|
@@ -150,6 +176,10 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
150
176
|
new Handler(Looper.getMainLooper()).postDelayed(() -> dispatcher.dispatchEvent(tce), 10);
|
|
151
177
|
}
|
|
152
178
|
|
|
179
|
+
if (annotationOverlayView != null) {
|
|
180
|
+
annotationOverlayView.invalidate();
|
|
181
|
+
}
|
|
182
|
+
|
|
153
183
|
// ReactContext reactContext = (ReactContext)this.getContext();
|
|
154
184
|
// reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
|
|
155
185
|
// this.getId(),
|
|
@@ -239,6 +269,21 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
239
269
|
// );
|
|
240
270
|
}
|
|
241
271
|
|
|
272
|
+
private void notifyOnChangeWithMessage(String message) {
|
|
273
|
+
WritableMap event = Arguments.createMap();
|
|
274
|
+
event.putString("message", message);
|
|
275
|
+
|
|
276
|
+
ThemedReactContext context = (ThemedReactContext) getContext();
|
|
277
|
+
EventDispatcher dispatcher = UIManagerHelper.getEventDispatcherForReactTag(context, getId());
|
|
278
|
+
int surfaceId = UIManagerHelper.getSurfaceId(this);
|
|
279
|
+
|
|
280
|
+
TopChangeEvent tce = new TopChangeEvent(surfaceId, getId(), event);
|
|
281
|
+
|
|
282
|
+
if (dispatcher != null) {
|
|
283
|
+
dispatcher.dispatchEvent(tce);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
242
287
|
@Override
|
|
243
288
|
public void onPageScrolled(int page, float positionOffset){
|
|
244
289
|
|
|
@@ -246,6 +291,10 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
246
291
|
Constants.Pinch.MINIMUM_ZOOM = this.minScale;
|
|
247
292
|
Constants.Pinch.MAXIMUM_ZOOM = this.maxScale;
|
|
248
293
|
|
|
294
|
+
if (annotationOverlayView != null) {
|
|
295
|
+
annotationOverlayView.invalidate();
|
|
296
|
+
}
|
|
297
|
+
|
|
249
298
|
}
|
|
250
299
|
|
|
251
300
|
@Override
|
|
@@ -311,6 +360,10 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
311
360
|
|
|
312
361
|
lastPageWidth = pageWidth;
|
|
313
362
|
lastPageHeight = pageHeight;
|
|
363
|
+
|
|
364
|
+
if (annotationOverlayView != null) {
|
|
365
|
+
annotationOverlayView.invalidate();
|
|
366
|
+
}
|
|
314
367
|
}
|
|
315
368
|
|
|
316
369
|
@Override
|
|
@@ -402,11 +455,43 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
402
455
|
}
|
|
403
456
|
|
|
404
457
|
configurator.load();
|
|
458
|
+
updateAnnotationOverlayConfig();
|
|
459
|
+
if (annotationOverlayView != null) {
|
|
460
|
+
annotationOverlayView.invalidate();
|
|
461
|
+
}
|
|
405
462
|
}
|
|
406
463
|
}
|
|
407
464
|
|
|
408
465
|
public void setEnableDoubleTapZoom(boolean enableDoubleTapZoom) {
|
|
409
466
|
this.enableDoubleTapZoom = enableDoubleTapZoom;
|
|
467
|
+
updateAnnotationOverlayConfig();
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
public void setAnnotations(String annotations) {
|
|
471
|
+
this.annotations = annotations;
|
|
472
|
+
if (annotationOverlayView != null) {
|
|
473
|
+
annotationOverlayView.replaceAnnotations(annotations);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
public void setAnnotationMode(boolean annotationMode) {
|
|
478
|
+
this.annotationMode = annotationMode;
|
|
479
|
+
updateAnnotationOverlayConfig();
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
public void setAnnotationTool(String annotationTool) {
|
|
483
|
+
this.annotationTool = annotationTool;
|
|
484
|
+
updateAnnotationOverlayConfig();
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
public void setAnnotationEditable(boolean annotationEditable) {
|
|
488
|
+
this.annotationEditable = annotationEditable;
|
|
489
|
+
updateAnnotationOverlayConfig();
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
public void setAnnotationIdMode(String annotationIdMode) {
|
|
493
|
+
this.annotationIdMode = annotationIdMode;
|
|
494
|
+
updateAnnotationOverlayConfig();
|
|
410
495
|
}
|
|
411
496
|
|
|
412
497
|
public void setPath(String path) {
|
|
@@ -421,6 +506,7 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
421
506
|
|
|
422
507
|
public void setEnableRTL(boolean enableRTL) {
|
|
423
508
|
this.enableRTL = enableRTL;
|
|
509
|
+
updateAnnotationOverlayConfig();
|
|
424
510
|
}
|
|
425
511
|
|
|
426
512
|
public void setScale(float scale) {
|
|
@@ -437,6 +523,7 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
437
523
|
|
|
438
524
|
public void setHorizontal(boolean horizontal) {
|
|
439
525
|
this.horizontal = horizontal;
|
|
526
|
+
updateAnnotationOverlayConfig();
|
|
440
527
|
}
|
|
441
528
|
|
|
442
529
|
public void setScrollEnabled(boolean scrollEnabled) {
|
|
@@ -470,6 +557,7 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
470
557
|
this.pageFling = false;
|
|
471
558
|
this.pageSnap = false;
|
|
472
559
|
}
|
|
560
|
+
updateAnnotationOverlayConfig();
|
|
473
561
|
}
|
|
474
562
|
|
|
475
563
|
public void setFitPolicy(int fitPolicy) {
|
|
@@ -492,6 +580,1069 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
492
580
|
|
|
493
581
|
public void setSinglePage(boolean singlePage) {
|
|
494
582
|
this.singlePage = singlePage;
|
|
583
|
+
updateAnnotationOverlayConfig();
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
private boolean isAnnotationEditingSupported() {
|
|
587
|
+
return !this.horizontal && !this.enablePaging && !this.enableRTL && !this.singlePage;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
private void updateAnnotationOverlayConfig() {
|
|
591
|
+
if (annotationOverlayView != null) {
|
|
592
|
+
annotationOverlayView.setConfiguration(
|
|
593
|
+
this.annotationMode,
|
|
594
|
+
this.annotationTool,
|
|
595
|
+
this.annotationEditable,
|
|
596
|
+
this.annotationIdMode,
|
|
597
|
+
isAnnotationEditingSupported()
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
private class AnnotationOverlayView extends FrameLayout {
|
|
603
|
+
private final java.util.ArrayList<JSONObject> draftAnnotations = new java.util.ArrayList<>();
|
|
604
|
+
private JSONObject activeInkAnnotation;
|
|
605
|
+
private JSONObject activeMarkupAnnotation;
|
|
606
|
+
private JSONObject activeTextAnnotation;
|
|
607
|
+
private PointF markupStartNormalized;
|
|
608
|
+
private EditText activeEditText;
|
|
609
|
+
private String selectedAnnotationId;
|
|
610
|
+
private JSONObject activeSelectionAnnotation;
|
|
611
|
+
private String activeSelectionHandle = "body";
|
|
612
|
+
private String activeSelectionMode = "none";
|
|
613
|
+
private RectF activeSelectionStartBounds;
|
|
614
|
+
private JSONArray activeSelectionStartPoints;
|
|
615
|
+
private int activeSelectionPageIndex = -1;
|
|
616
|
+
private float activeSelectionDownX;
|
|
617
|
+
private float activeSelectionDownY;
|
|
618
|
+
private boolean activeSelectionHasMoved;
|
|
619
|
+
private final float touchSlop;
|
|
620
|
+
private boolean annotationModeEnabled = false;
|
|
621
|
+
private String tool = "select";
|
|
622
|
+
private boolean editable = true;
|
|
623
|
+
private String idMode = "auto";
|
|
624
|
+
private boolean supported = true;
|
|
625
|
+
|
|
626
|
+
AnnotationOverlayView(Context context) {
|
|
627
|
+
super(context);
|
|
628
|
+
setWillNotDraw(false);
|
|
629
|
+
setBackgroundColor(Color.TRANSPARENT);
|
|
630
|
+
setClickable(true);
|
|
631
|
+
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
void setConfiguration(boolean annotationMode, String annotationTool, boolean annotationEditable, String annotationIdMode, boolean annotationEditingSupported) {
|
|
635
|
+
annotationModeEnabled = annotationMode;
|
|
636
|
+
tool = annotationTool == null ? "select" : annotationTool;
|
|
637
|
+
editable = annotationEditable;
|
|
638
|
+
idMode = annotationIdMode == null ? "auto" : annotationIdMode;
|
|
639
|
+
supported = annotationEditingSupported;
|
|
640
|
+
|
|
641
|
+
if (!annotationModeEnabled || !editable || !supported) {
|
|
642
|
+
commitTextEditingIfNeeded();
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
if (!annotationModeEnabled || !editable || !supported) {
|
|
646
|
+
clearSelectionInteraction();
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
invalidate();
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
void replaceAnnotations(String json) {
|
|
653
|
+
draftAnnotations.clear();
|
|
654
|
+
clearSelectionInteraction();
|
|
655
|
+
if (TextUtils.isEmpty(json)) {
|
|
656
|
+
invalidate();
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
try {
|
|
661
|
+
JSONArray annotationsArray;
|
|
662
|
+
try {
|
|
663
|
+
JSONObject document = new JSONObject(json);
|
|
664
|
+
annotationsArray = document.optJSONArray("annotations");
|
|
665
|
+
} catch (JSONException objectError) {
|
|
666
|
+
annotationsArray = null;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
if (annotationsArray == null) {
|
|
670
|
+
annotationsArray = new JSONArray(json);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
for (int i = 0; i < annotationsArray.length(); i++) {
|
|
674
|
+
JSONObject source = annotationsArray.optJSONObject(i);
|
|
675
|
+
if (source == null) {
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
JSONObject annotation = new JSONObject(source.toString());
|
|
680
|
+
if (!annotation.has("id")) {
|
|
681
|
+
annotation.put("id", nextLocalAnnotationId());
|
|
682
|
+
}
|
|
683
|
+
if (!annotation.has("page")) {
|
|
684
|
+
annotation.put("page", 1);
|
|
685
|
+
}
|
|
686
|
+
draftAnnotations.add(annotation);
|
|
687
|
+
}
|
|
688
|
+
} catch (JSONException ignored) {
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
invalidate();
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
String serializeDocument() {
|
|
695
|
+
JSONObject document = new JSONObject();
|
|
696
|
+
try {
|
|
697
|
+
document.put("editable", editable);
|
|
698
|
+
document.put("idMode", idMode);
|
|
699
|
+
document.put("annotations", new JSONArray(draftAnnotations));
|
|
700
|
+
return document.toString();
|
|
701
|
+
} catch (JSONException e) {
|
|
702
|
+
return "{}";
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
@Override
|
|
707
|
+
public boolean dispatchTouchEvent(MotionEvent event) {
|
|
708
|
+
if (activeEditText != null && event.getActionMasked() == MotionEvent.ACTION_DOWN && !isPointInsideView(event.getX(), event.getY(), activeEditText)) {
|
|
709
|
+
commitTextEditingIfNeeded();
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
return super.dispatchTouchEvent(event);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
@Override
|
|
716
|
+
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
|
717
|
+
if (!annotationModeEnabled || !editable || !supported) {
|
|
718
|
+
return false;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
if (ev.getPointerCount() > 1) {
|
|
722
|
+
return false;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
String currentTool = tool == null ? "select" : tool;
|
|
726
|
+
if ("select".equals(currentTool)) {
|
|
727
|
+
return hitTestAnnotation(ev.getX(), ev.getY(), true) != null;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
return !TextUtils.isEmpty(currentTool);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
@Override
|
|
734
|
+
public boolean onTouchEvent(MotionEvent event) {
|
|
735
|
+
if (!annotationModeEnabled || !editable || !supported) {
|
|
736
|
+
return false;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
if (event.getPointerCount() > 1) {
|
|
740
|
+
return false;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
String currentTool = tool == null ? "select" : tool;
|
|
744
|
+
if ("select".equals(currentTool)) {
|
|
745
|
+
return handleSelectTouch(event);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
int action = event.getActionMasked();
|
|
749
|
+
if ("ink".equals(currentTool)) {
|
|
750
|
+
if (action == MotionEvent.ACTION_DOWN) {
|
|
751
|
+
AnnotationHit hit = hitTest(event.getX(), event.getY());
|
|
752
|
+
if (hit == null) {
|
|
753
|
+
return false;
|
|
754
|
+
}
|
|
755
|
+
beginInk(hit, event.getX(), event.getY());
|
|
756
|
+
return true;
|
|
757
|
+
} else if (action == MotionEvent.ACTION_MOVE) {
|
|
758
|
+
AnnotationHit hit = hitTest(event.getX(), event.getY());
|
|
759
|
+
if (hit != null) {
|
|
760
|
+
appendInkPoint(hit, event.getX(), event.getY());
|
|
761
|
+
}
|
|
762
|
+
return true;
|
|
763
|
+
} else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
|
|
764
|
+
endInk();
|
|
765
|
+
return true;
|
|
766
|
+
}
|
|
767
|
+
} else if ("highlight".equals(currentTool) || "underline".equals(currentTool) || "strikeout".equals(currentTool)) {
|
|
768
|
+
if (action == MotionEvent.ACTION_DOWN) {
|
|
769
|
+
AnnotationHit hit = hitTest(event.getX(), event.getY());
|
|
770
|
+
if (hit == null) {
|
|
771
|
+
return false;
|
|
772
|
+
}
|
|
773
|
+
beginMarkup(hit, event.getX(), event.getY(), currentTool);
|
|
774
|
+
return true;
|
|
775
|
+
} else if (action == MotionEvent.ACTION_MOVE) {
|
|
776
|
+
AnnotationHit hit = hitTest(event.getX(), event.getY());
|
|
777
|
+
if (hit != null) {
|
|
778
|
+
updateMarkup(hit, event.getX(), event.getY());
|
|
779
|
+
}
|
|
780
|
+
return true;
|
|
781
|
+
} else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
|
|
782
|
+
endMarkup();
|
|
783
|
+
return true;
|
|
784
|
+
}
|
|
785
|
+
} else if ("text".equals(currentTool)) {
|
|
786
|
+
if (action == MotionEvent.ACTION_UP) {
|
|
787
|
+
AnnotationHit hit = hitTest(event.getX(), event.getY());
|
|
788
|
+
if (hit != null) {
|
|
789
|
+
createTextAnnotation(hit, event.getX(), event.getY());
|
|
790
|
+
return true;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
return false;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
@Override
|
|
799
|
+
protected void onDraw(Canvas canvas) {
|
|
800
|
+
super.onDraw(canvas);
|
|
801
|
+
|
|
802
|
+
if (PdfView.this.getPageCount() <= 0) {
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
Paint strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
807
|
+
Paint fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
808
|
+
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
809
|
+
|
|
810
|
+
for (JSONObject annotation : draftAnnotations) {
|
|
811
|
+
int pageIndex = annotation.optInt("page", 1) - 1;
|
|
812
|
+
if (pageIndex < 0 || pageIndex >= PdfView.this.getPageCount()) {
|
|
813
|
+
continue;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
String type = annotation.optString("type", "");
|
|
817
|
+
RectF rect = viewRectForAnnotation(annotation, pageIndex);
|
|
818
|
+
if (rect == null && !"ink".equals(type)) {
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
if ("ink".equals(type)) {
|
|
823
|
+
JSONArray points = annotation.optJSONArray("points");
|
|
824
|
+
if (points == null || points.length() < 2) {
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
Path path = new Path();
|
|
829
|
+
boolean first = true;
|
|
830
|
+
for (int i = 0; i < points.length(); i++) {
|
|
831
|
+
JSONObject point = points.optJSONObject(i);
|
|
832
|
+
if (point == null) {
|
|
833
|
+
continue;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
float pageX = (float) point.optDouble("x", 0f);
|
|
837
|
+
float pageY = (float) point.optDouble("y", 0f);
|
|
838
|
+
PointF viewPoint = viewPointForNormalizedPoint(pageIndex, pageX, pageY);
|
|
839
|
+
if (first) {
|
|
840
|
+
path.moveTo(viewPoint.x, viewPoint.y);
|
|
841
|
+
first = false;
|
|
842
|
+
} else {
|
|
843
|
+
path.lineTo(viewPoint.x, viewPoint.y);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
strokePaint.setStyle(Paint.Style.STROKE);
|
|
848
|
+
strokePaint.setStrokeJoin(Paint.Join.ROUND);
|
|
849
|
+
strokePaint.setStrokeCap(Paint.Cap.ROUND);
|
|
850
|
+
strokePaint.setStrokeWidth(Math.max(1f, (float) styleFor(annotation).optDouble("thickness", 2.0)));
|
|
851
|
+
strokePaint.setColor(annotationColor(annotation, Color.BLACK));
|
|
852
|
+
canvas.drawPath(path, strokePaint);
|
|
853
|
+
} else if ("text".equals(type)) {
|
|
854
|
+
fillPaint.setColor(Color.argb(200, 255, 255, 255));
|
|
855
|
+
canvas.drawRoundRect(rect, 4f, 4f, fillPaint);
|
|
856
|
+
|
|
857
|
+
strokePaint.setStyle(Paint.Style.STROKE);
|
|
858
|
+
strokePaint.setStrokeWidth(1f);
|
|
859
|
+
strokePaint.setColor(annotationColor(annotation, Color.rgb(34, 68, 170)));
|
|
860
|
+
canvas.drawRoundRect(rect, 4f, 4f, strokePaint);
|
|
861
|
+
|
|
862
|
+
textPaint.setColor(strokePaint.getColor());
|
|
863
|
+
textPaint.setTextSize((float) styleFor(annotation).optDouble("fontSize", 15.0));
|
|
864
|
+
textPaint.setTextAlign(Paint.Align.LEFT);
|
|
865
|
+
String text = annotation.optString("text", "");
|
|
866
|
+
canvas.drawText(text, rect.left + 8f, rect.top + Math.max(20f, textPaint.getTextSize() + 6f), textPaint);
|
|
867
|
+
} else if ("highlight".equals(type) || "underline".equals(type) || "strikeout".equals(type)) {
|
|
868
|
+
fillPaint.setColor(annotationColor(annotation, annotationFillColor(type)));
|
|
869
|
+
canvas.drawRect(rect, fillPaint);
|
|
870
|
+
|
|
871
|
+
if ("underline".equals(type) || "strikeout".equals(type)) {
|
|
872
|
+
strokePaint.setStyle(Paint.Style.STROKE);
|
|
873
|
+
strokePaint.setStrokeWidth(Math.max(1f, rect.height() * 0.15f));
|
|
874
|
+
strokePaint.setColor(annotationColor(annotation, annotationFillColor(type)));
|
|
875
|
+
float y = "underline".equals(type) ? rect.bottom - 2f : rect.centerY();
|
|
876
|
+
canvas.drawLine(rect.left, y, rect.right, y, strokePaint);
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
drawSelectionDecorations(canvas);
|
|
882
|
+
updateEditTextFrame();
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
private boolean handleSelectTouch(MotionEvent event) {
|
|
886
|
+
int action = event.getActionMasked();
|
|
887
|
+
if (action == MotionEvent.ACTION_DOWN) {
|
|
888
|
+
clearSelectionInteraction();
|
|
889
|
+
|
|
890
|
+
AnnotationSelectionHit hit = hitTestAnnotation(event.getX(), event.getY(), true);
|
|
891
|
+
if (hit == null) {
|
|
892
|
+
return false;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
selectAnnotation(hit.annotation);
|
|
896
|
+
activeSelectionAnnotation = hit.annotation;
|
|
897
|
+
activeSelectionHandle = hit.hitPart;
|
|
898
|
+
activeSelectionMode = "delete".equals(hit.hitPart) ? "delete" : ("resize".equals(hit.hitPart) ? "resize" : "move");
|
|
899
|
+
activeSelectionPageIndex = hit.pageIndex;
|
|
900
|
+
activeSelectionStartBounds = normalizedBoundsForAnnotation(hit.annotation);
|
|
901
|
+
activeSelectionStartPoints = copyPointsForAnnotation(hit.annotation);
|
|
902
|
+
activeSelectionDownX = event.getX();
|
|
903
|
+
activeSelectionDownY = event.getY();
|
|
904
|
+
activeSelectionHasMoved = false;
|
|
905
|
+
|
|
906
|
+
if ("delete".equals(activeSelectionMode)) {
|
|
907
|
+
deleteAnnotation(hit.annotation);
|
|
908
|
+
clearSelectionInteraction();
|
|
909
|
+
return true;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
return true;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
if (activeSelectionAnnotation == null) {
|
|
916
|
+
return false;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
if (action == MotionEvent.ACTION_MOVE) {
|
|
920
|
+
float deltaX = event.getX() - activeSelectionDownX;
|
|
921
|
+
float deltaY = event.getY() - activeSelectionDownY;
|
|
922
|
+
if (!activeSelectionHasMoved) {
|
|
923
|
+
if (Math.hypot(deltaX, deltaY) < touchSlop) {
|
|
924
|
+
return true;
|
|
925
|
+
}
|
|
926
|
+
activeSelectionHasMoved = true;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
if ("move".equals(activeSelectionMode)) {
|
|
930
|
+
moveSelectedAnnotation(deltaX, deltaY);
|
|
931
|
+
} else if ("resize".equals(activeSelectionMode)) {
|
|
932
|
+
resizeSelectedAnnotation(deltaX, deltaY);
|
|
933
|
+
}
|
|
934
|
+
return true;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
|
|
938
|
+
if (!activeSelectionHasMoved && "body".equals(activeSelectionHandle)) {
|
|
939
|
+
// keep the annotation selected; tap selection is enough for now.
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
clearSelectionInteraction();
|
|
943
|
+
return true;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
return true;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
private void drawSelectionDecorations(Canvas canvas) {
|
|
950
|
+
JSONObject annotation = getSelectedAnnotation();
|
|
951
|
+
if (annotation == null) {
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
int pageIndex = annotation.optInt("page", 1) - 1;
|
|
956
|
+
if (pageIndex < 0 || pageIndex >= PdfView.this.getPageCount()) {
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
RectF rect = viewRectForAnnotation(annotation, pageIndex);
|
|
961
|
+
if (rect == null) {
|
|
962
|
+
return;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
Paint outlinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
966
|
+
outlinePaint.setStyle(Paint.Style.STROKE);
|
|
967
|
+
outlinePaint.setColor(Color.argb(220, 34, 68, 170));
|
|
968
|
+
outlinePaint.setStrokeWidth(2f);
|
|
969
|
+
|
|
970
|
+
Paint fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
971
|
+
fillPaint.setStyle(Paint.Style.FILL);
|
|
972
|
+
|
|
973
|
+
canvas.drawRoundRect(rect, 4f, 4f, outlinePaint);
|
|
974
|
+
|
|
975
|
+
RectF deleteHandle = deleteHandleRect(rect);
|
|
976
|
+
fillPaint.setColor(Color.argb(235, 210, 48, 48));
|
|
977
|
+
canvas.drawOval(deleteHandle, fillPaint);
|
|
978
|
+
|
|
979
|
+
RectF resizeHandle = resizeHandleRect(rect);
|
|
980
|
+
fillPaint.setColor(Color.argb(235, 34, 68, 170));
|
|
981
|
+
canvas.drawRoundRect(resizeHandle, 2f, 2f, fillPaint);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
private void clearSelectionInteraction() {
|
|
985
|
+
activeSelectionAnnotation = null;
|
|
986
|
+
activeSelectionHandle = "body";
|
|
987
|
+
activeSelectionMode = "none";
|
|
988
|
+
activeSelectionStartBounds = null;
|
|
989
|
+
activeSelectionStartPoints = null;
|
|
990
|
+
activeSelectionPageIndex = -1;
|
|
991
|
+
activeSelectionHasMoved = false;
|
|
992
|
+
activeSelectionDownX = 0f;
|
|
993
|
+
activeSelectionDownY = 0f;
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
private void selectAnnotation(JSONObject annotation) {
|
|
997
|
+
if (annotation == null) {
|
|
998
|
+
selectedAnnotationId = null;
|
|
999
|
+
invalidate();
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
selectedAnnotationId = annotation.optString("id", null);
|
|
1004
|
+
invalidate();
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
private JSONObject getSelectedAnnotation() {
|
|
1008
|
+
if (TextUtils.isEmpty(selectedAnnotationId)) {
|
|
1009
|
+
return null;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
for (int i = draftAnnotations.size() - 1; i >= 0; i--) {
|
|
1013
|
+
JSONObject annotation = draftAnnotations.get(i);
|
|
1014
|
+
if (selectedAnnotationId.equals(annotation.optString("id", null))) {
|
|
1015
|
+
return annotation;
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
return null;
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
private void deleteAnnotation(JSONObject annotation) {
|
|
1023
|
+
if (annotation == null) {
|
|
1024
|
+
return;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
String annotationId = annotation.optString("id", null);
|
|
1028
|
+
for (int i = draftAnnotations.size() - 1; i >= 0; i--) {
|
|
1029
|
+
JSONObject candidate = draftAnnotations.get(i);
|
|
1030
|
+
if (annotationId != null && annotationId.equals(candidate.optString("id", null))) {
|
|
1031
|
+
draftAnnotations.remove(i);
|
|
1032
|
+
break;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
if (annotationId != null && annotationId.equals(selectedAnnotationId)) {
|
|
1037
|
+
selectedAnnotationId = null;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
invalidate();
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
private RectF normalizedBoundsForAnnotation(JSONObject annotation) {
|
|
1044
|
+
if (annotation == null) {
|
|
1045
|
+
return null;
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
String type = annotation.optString("type", "");
|
|
1049
|
+
if ("ink".equals(type)) {
|
|
1050
|
+
JSONArray points = annotation.optJSONArray("points");
|
|
1051
|
+
if (points == null || points.length() == 0) {
|
|
1052
|
+
return null;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
float minX = Float.MAX_VALUE;
|
|
1056
|
+
float minY = Float.MAX_VALUE;
|
|
1057
|
+
float maxX = -Float.MAX_VALUE;
|
|
1058
|
+
float maxY = -Float.MAX_VALUE;
|
|
1059
|
+
for (int i = 0; i < points.length(); i++) {
|
|
1060
|
+
JSONObject point = points.optJSONObject(i);
|
|
1061
|
+
if (point == null) {
|
|
1062
|
+
continue;
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
float x = (float) point.optDouble("x", 0f);
|
|
1066
|
+
float y = (float) point.optDouble("y", 0f);
|
|
1067
|
+
minX = Math.min(minX, x);
|
|
1068
|
+
minY = Math.min(minY, y);
|
|
1069
|
+
maxX = Math.max(maxX, x);
|
|
1070
|
+
maxY = Math.max(maxY, y);
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
if (minX == Float.MAX_VALUE || minY == Float.MAX_VALUE || maxX == -Float.MAX_VALUE || maxY == -Float.MAX_VALUE) {
|
|
1074
|
+
return null;
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
return new RectF(minX, minY, maxX, maxY);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
JSONObject bounds = annotation.optJSONObject("bounds");
|
|
1081
|
+
if (bounds == null) {
|
|
1082
|
+
return null;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
float x = (float) bounds.optDouble("x", 0f);
|
|
1086
|
+
float y = (float) bounds.optDouble("y", 0f);
|
|
1087
|
+
float width = (float) bounds.optDouble("width", 0f);
|
|
1088
|
+
float height = (float) bounds.optDouble("height", 0f);
|
|
1089
|
+
return new RectF(x, y, x + width, y + height);
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
private JSONArray copyPointsForAnnotation(JSONObject annotation) {
|
|
1093
|
+
if (annotation == null || !"ink".equals(annotation.optString("type", ""))) {
|
|
1094
|
+
return null;
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
JSONArray points = annotation.optJSONArray("points");
|
|
1098
|
+
if (points == null) {
|
|
1099
|
+
return null;
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
try {
|
|
1103
|
+
return new JSONArray(points.toString());
|
|
1104
|
+
} catch (JSONException ignored) {
|
|
1105
|
+
return null;
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
private void moveSelectedAnnotation(float deltaX, float deltaY) {
|
|
1110
|
+
if (activeSelectionAnnotation == null || activeSelectionStartBounds == null || activeSelectionPageIndex < 0) {
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
SizeF pageSize = PdfView.this.getPageSize(activeSelectionPageIndex);
|
|
1115
|
+
if (pageSize == null) {
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
float zoom = PdfView.this.getZoom();
|
|
1120
|
+
float scaledPageWidth = Math.max(1f, pageSize.getWidth() * zoom);
|
|
1121
|
+
float scaledPageHeight = Math.max(1f, pageSize.getHeight() * zoom);
|
|
1122
|
+
float normalizedDeltaX = deltaX / scaledPageWidth;
|
|
1123
|
+
float normalizedDeltaY = deltaY / scaledPageHeight;
|
|
1124
|
+
|
|
1125
|
+
RectF newBounds = new RectF(
|
|
1126
|
+
activeSelectionStartBounds.left + normalizedDeltaX,
|
|
1127
|
+
activeSelectionStartBounds.top + normalizedDeltaY,
|
|
1128
|
+
activeSelectionStartBounds.right + normalizedDeltaX,
|
|
1129
|
+
activeSelectionStartBounds.bottom + normalizedDeltaY
|
|
1130
|
+
);
|
|
1131
|
+
|
|
1132
|
+
setAnnotationFromNormalizedBounds(activeSelectionAnnotation, activeSelectionStartBounds, activeSelectionStartPoints, newBounds);
|
|
1133
|
+
invalidate();
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
private void resizeSelectedAnnotation(float deltaX, float deltaY) {
|
|
1137
|
+
if (activeSelectionAnnotation == null || activeSelectionStartBounds == null || activeSelectionPageIndex < 0) {
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
SizeF pageSize = PdfView.this.getPageSize(activeSelectionPageIndex);
|
|
1142
|
+
if (pageSize == null) {
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
float zoom = PdfView.this.getZoom();
|
|
1147
|
+
float scaledPageWidth = Math.max(1f, pageSize.getWidth() * zoom);
|
|
1148
|
+
float scaledPageHeight = Math.max(1f, pageSize.getHeight() * zoom);
|
|
1149
|
+
float normalizedDeltaX = deltaX / scaledPageWidth;
|
|
1150
|
+
float normalizedDeltaY = deltaY / scaledPageHeight;
|
|
1151
|
+
|
|
1152
|
+
RectF newBounds = new RectF(
|
|
1153
|
+
activeSelectionStartBounds.left,
|
|
1154
|
+
activeSelectionStartBounds.top,
|
|
1155
|
+
Math.max(activeSelectionStartBounds.left + 0.01f, activeSelectionStartBounds.right + normalizedDeltaX),
|
|
1156
|
+
Math.max(activeSelectionStartBounds.top + 0.01f, activeSelectionStartBounds.bottom + normalizedDeltaY)
|
|
1157
|
+
);
|
|
1158
|
+
|
|
1159
|
+
setAnnotationFromNormalizedBounds(activeSelectionAnnotation, activeSelectionStartBounds, activeSelectionStartPoints, newBounds);
|
|
1160
|
+
invalidate();
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
private void setAnnotationFromNormalizedBounds(JSONObject annotation, RectF startBounds, JSONArray startPoints, RectF newBounds) {
|
|
1164
|
+
if (annotation == null || newBounds == null) {
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
String type = annotation.optString("type", "");
|
|
1169
|
+
if ("ink".equals(type)) {
|
|
1170
|
+
if (startPoints == null || startBounds == null) {
|
|
1171
|
+
return;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
try {
|
|
1175
|
+
JSONArray points = new JSONArray();
|
|
1176
|
+
float startWidth = Math.max(0.001f, startBounds.width());
|
|
1177
|
+
float startHeight = Math.max(0.001f, startBounds.height());
|
|
1178
|
+
float newWidth = Math.max(0.001f, newBounds.width());
|
|
1179
|
+
float newHeight = Math.max(0.001f, newBounds.height());
|
|
1180
|
+
|
|
1181
|
+
for (int i = 0; i < startPoints.length(); i++) {
|
|
1182
|
+
JSONObject point = startPoints.optJSONObject(i);
|
|
1183
|
+
if (point == null) {
|
|
1184
|
+
continue;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
float pointX = (float) point.optDouble("x", 0f);
|
|
1188
|
+
float pointY = (float) point.optDouble("y", 0f);
|
|
1189
|
+
float xRatio = (pointX - startBounds.left) / startWidth;
|
|
1190
|
+
float yRatio = (pointY - startBounds.top) / startHeight;
|
|
1191
|
+
|
|
1192
|
+
JSONObject transformed = new JSONObject();
|
|
1193
|
+
transformed.put("x", Math.min(1f, Math.max(0f, newBounds.left + (xRatio * newWidth))));
|
|
1194
|
+
transformed.put("y", Math.min(1f, Math.max(0f, newBounds.top + (yRatio * newHeight))));
|
|
1195
|
+
points.put(transformed);
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
annotation.put("points", points);
|
|
1199
|
+
} catch (JSONException ignored) {
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
return;
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
try {
|
|
1206
|
+
annotation.put("bounds", new JSONObject()
|
|
1207
|
+
.put("x", Math.min(1f, Math.max(0f, newBounds.left)))
|
|
1208
|
+
.put("y", Math.min(1f, Math.max(0f, newBounds.top)))
|
|
1209
|
+
.put("width", Math.min(1f, Math.max(0.01f, newBounds.width())))
|
|
1210
|
+
.put("height", Math.min(1f, Math.max(0.01f, newBounds.height()))));
|
|
1211
|
+
} catch (JSONException ignored) {
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
private RectF deleteHandleRect(RectF rect) {
|
|
1216
|
+
float size = Math.max(18f, Math.min(rect.width(), rect.height()) * 0.18f);
|
|
1217
|
+
return new RectF(rect.right - size, rect.top - size, rect.right, rect.top);
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
private RectF resizeHandleRect(RectF rect) {
|
|
1221
|
+
float size = Math.max(18f, Math.min(rect.width(), rect.height()) * 0.18f);
|
|
1222
|
+
return new RectF(rect.right - size, rect.bottom - size, rect.right, rect.bottom);
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
private AnnotationSelectionHit hitTestAnnotation(float x, float y, boolean includeHandles) {
|
|
1226
|
+
if (PdfView.this.getPageCount() <= 0) {
|
|
1227
|
+
return null;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
for (int i = draftAnnotations.size() - 1; i >= 0; i--) {
|
|
1231
|
+
JSONObject annotation = draftAnnotations.get(i);
|
|
1232
|
+
int pageIndex = annotation.optInt("page", 1) - 1;
|
|
1233
|
+
if (pageIndex < 0 || pageIndex >= PdfView.this.getPageCount()) {
|
|
1234
|
+
continue;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
RectF rect = viewRectForAnnotation(annotation, pageIndex);
|
|
1238
|
+
if (rect == null) {
|
|
1239
|
+
continue;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
RectF hitRect = new RectF(rect);
|
|
1243
|
+
hitRect.inset(-12f, -12f);
|
|
1244
|
+
if (!hitRect.contains(x, y)) {
|
|
1245
|
+
continue;
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
String hitPart = "body";
|
|
1249
|
+
if (includeHandles && selectedAnnotationId != null && selectedAnnotationId.equals(annotation.optString("id", null))) {
|
|
1250
|
+
if (deleteHandleRect(rect).contains(x, y)) {
|
|
1251
|
+
hitPart = "delete";
|
|
1252
|
+
} else if (resizeHandleRect(rect).contains(x, y)) {
|
|
1253
|
+
hitPart = "resize";
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
return new AnnotationSelectionHit(annotation, pageIndex, rect, hitPart);
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
return null;
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
private final class AnnotationSelectionHit {
|
|
1264
|
+
final JSONObject annotation;
|
|
1265
|
+
final int pageIndex;
|
|
1266
|
+
final RectF rect;
|
|
1267
|
+
final String hitPart;
|
|
1268
|
+
|
|
1269
|
+
AnnotationSelectionHit(JSONObject annotation, int pageIndex, RectF rect, String hitPart) {
|
|
1270
|
+
this.annotation = annotation;
|
|
1271
|
+
this.pageIndex = pageIndex;
|
|
1272
|
+
this.rect = rect;
|
|
1273
|
+
this.hitPart = hitPart;
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
private void beginInk(AnnotationHit hit, float x, float y) {
|
|
1278
|
+
try {
|
|
1279
|
+
JSONObject annotation = new JSONObject();
|
|
1280
|
+
annotation.put("id", nextLocalAnnotationId());
|
|
1281
|
+
annotation.put("page", hit.pageIndex + 1);
|
|
1282
|
+
annotation.put("type", "ink");
|
|
1283
|
+
annotation.put("points", new JSONArray());
|
|
1284
|
+
JSONObject style = new JSONObject();
|
|
1285
|
+
style.put("color", "#111111");
|
|
1286
|
+
style.put("thickness", 2.0f);
|
|
1287
|
+
annotation.put("style", style);
|
|
1288
|
+
draftAnnotations.add(annotation);
|
|
1289
|
+
activeInkAnnotation = annotation;
|
|
1290
|
+
appendInkPoint(hit, x, y);
|
|
1291
|
+
} catch (JSONException ignored) {
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
private void appendInkPoint(AnnotationHit hit, float x, float y) {
|
|
1296
|
+
if (activeInkAnnotation == null) {
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
try {
|
|
1301
|
+
JSONArray points = activeInkAnnotation.optJSONArray("points");
|
|
1302
|
+
if (points == null) {
|
|
1303
|
+
points = new JSONArray();
|
|
1304
|
+
activeInkAnnotation.put("points", points);
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
PointF normalized = normalizedPointFor(hit, x, y);
|
|
1308
|
+
JSONObject point = new JSONObject();
|
|
1309
|
+
point.put("x", normalized.x);
|
|
1310
|
+
point.put("y", normalized.y);
|
|
1311
|
+
points.put(point);
|
|
1312
|
+
invalidate();
|
|
1313
|
+
} catch (JSONException ignored) {
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
private void endInk() {
|
|
1318
|
+
activeInkAnnotation = null;
|
|
1319
|
+
invalidate();
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
private void beginMarkup(AnnotationHit hit, float x, float y, String type) {
|
|
1323
|
+
try {
|
|
1324
|
+
markupStartNormalized = normalizedPointFor(hit, x, y);
|
|
1325
|
+
JSONObject annotation = new JSONObject();
|
|
1326
|
+
annotation.put("id", nextLocalAnnotationId());
|
|
1327
|
+
annotation.put("page", hit.pageIndex + 1);
|
|
1328
|
+
annotation.put("type", type);
|
|
1329
|
+
annotation.put("bounds", new JSONObject().put("x", markupStartNormalized.x).put("y", markupStartNormalized.y).put("width", 0).put("height", 0));
|
|
1330
|
+
annotation.put("style", new JSONObject());
|
|
1331
|
+
draftAnnotations.add(annotation);
|
|
1332
|
+
activeMarkupAnnotation = annotation;
|
|
1333
|
+
invalidate();
|
|
1334
|
+
} catch (JSONException ignored) {
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
private void updateMarkup(AnnotationHit hit, float x, float y) {
|
|
1339
|
+
if (activeMarkupAnnotation == null || markupStartNormalized == null) {
|
|
1340
|
+
return;
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
try {
|
|
1344
|
+
PointF normalized = normalizedPointFor(hit, x, y);
|
|
1345
|
+
float minX = Math.min(markupStartNormalized.x, normalized.x);
|
|
1346
|
+
float minY = Math.min(markupStartNormalized.y, normalized.y);
|
|
1347
|
+
float maxX = Math.max(markupStartNormalized.x, normalized.x);
|
|
1348
|
+
float maxY = Math.max(markupStartNormalized.y, normalized.y);
|
|
1349
|
+
activeMarkupAnnotation.put("bounds", new JSONObject()
|
|
1350
|
+
.put("x", minX)
|
|
1351
|
+
.put("y", minY)
|
|
1352
|
+
.put("width", Math.max(0f, maxX - minX))
|
|
1353
|
+
.put("height", Math.max(0f, maxY - minY)));
|
|
1354
|
+
invalidate();
|
|
1355
|
+
} catch (JSONException ignored) {
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
private void endMarkup() {
|
|
1360
|
+
activeMarkupAnnotation = null;
|
|
1361
|
+
markupStartNormalized = null;
|
|
1362
|
+
invalidate();
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
private void createTextAnnotation(AnnotationHit hit, float x, float y) {
|
|
1366
|
+
if (!editable || !supported) {
|
|
1367
|
+
return;
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
commitTextEditingIfNeeded();
|
|
1371
|
+
|
|
1372
|
+
try {
|
|
1373
|
+
PointF normalized = normalizedPointFor(hit, x, y);
|
|
1374
|
+
float width = 0.25f;
|
|
1375
|
+
float height = 0.12f;
|
|
1376
|
+
float clampedX = Math.min(Math.max(normalized.x, 0f), Math.max(0f, 1f - width));
|
|
1377
|
+
float clampedY = Math.min(Math.max(normalized.y, 0f), Math.max(0f, 1f - height));
|
|
1378
|
+
|
|
1379
|
+
JSONObject bounds = new JSONObject();
|
|
1380
|
+
bounds.put("x", clampedX);
|
|
1381
|
+
bounds.put("y", clampedY);
|
|
1382
|
+
bounds.put("width", width);
|
|
1383
|
+
bounds.put("height", height);
|
|
1384
|
+
|
|
1385
|
+
JSONObject annotation = new JSONObject();
|
|
1386
|
+
annotation.put("id", nextLocalAnnotationId());
|
|
1387
|
+
annotation.put("page", hit.pageIndex + 1);
|
|
1388
|
+
annotation.put("type", "text");
|
|
1389
|
+
annotation.put("bounds", bounds);
|
|
1390
|
+
annotation.put("text", "");
|
|
1391
|
+
JSONObject style = new JSONObject();
|
|
1392
|
+
style.put("color", "#2244aa");
|
|
1393
|
+
style.put("fontSize", 15.0f);
|
|
1394
|
+
style.put("textAlign", "left");
|
|
1395
|
+
annotation.put("style", style);
|
|
1396
|
+
draftAnnotations.add(annotation);
|
|
1397
|
+
activeTextAnnotation = annotation;
|
|
1398
|
+
|
|
1399
|
+
activeEditText = new EditText(getContext());
|
|
1400
|
+
activeEditText.setBackgroundColor(Color.argb(220, 255, 255, 255));
|
|
1401
|
+
activeEditText.setTextColor(Color.rgb(34, 68, 170));
|
|
1402
|
+
activeEditText.setPadding(12, 8, 12, 8);
|
|
1403
|
+
activeEditText.setSingleLine(false);
|
|
1404
|
+
activeEditText.setGravity(Gravity.TOP | Gravity.START);
|
|
1405
|
+
activeEditText.setTextSize(15f);
|
|
1406
|
+
activeEditText.addTextChangedListener(new TextWatcher() {
|
|
1407
|
+
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
|
|
1408
|
+
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
|
|
1409
|
+
@Override public void afterTextChanged(Editable s) {
|
|
1410
|
+
if (activeTextAnnotation != null) {
|
|
1411
|
+
try {
|
|
1412
|
+
activeTextAnnotation.put("text", s.toString());
|
|
1413
|
+
} catch (JSONException ignored) {
|
|
1414
|
+
}
|
|
1415
|
+
invalidate();
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
});
|
|
1419
|
+
|
|
1420
|
+
addView(activeEditText, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
|
1421
|
+
activeEditText.requestFocus();
|
|
1422
|
+
updateEditTextFrame();
|
|
1423
|
+
invalidate();
|
|
1424
|
+
} catch (JSONException ignored) {
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
void commitTextEditingIfNeeded() {
|
|
1429
|
+
if (activeEditText == null || activeTextAnnotation == null) {
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
try {
|
|
1434
|
+
activeTextAnnotation.put("text", activeEditText.getText() == null ? "" : activeEditText.getText().toString());
|
|
1435
|
+
} catch (JSONException ignored) {
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
activeEditText.clearFocus();
|
|
1439
|
+
removeView(activeEditText);
|
|
1440
|
+
activeEditText = null;
|
|
1441
|
+
activeTextAnnotation = null;
|
|
1442
|
+
invalidate();
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
private void updateEditTextFrame() {
|
|
1446
|
+
if (activeEditText == null || activeTextAnnotation == null) {
|
|
1447
|
+
return;
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
RectF rect = viewRectForAnnotation(activeTextAnnotation, activeTextAnnotation.optInt("page", 1) - 1);
|
|
1451
|
+
if (rect == null) {
|
|
1452
|
+
return;
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
ViewGroup.LayoutParams params = activeEditText.getLayoutParams();
|
|
1456
|
+
params.width = Math.max(1, Math.round(rect.width()));
|
|
1457
|
+
params.height = Math.max(1, Math.round(rect.height()));
|
|
1458
|
+
activeEditText.setLayoutParams(params);
|
|
1459
|
+
activeEditText.setX(rect.left);
|
|
1460
|
+
activeEditText.setY(rect.top);
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
private boolean isPointInsideView(float x, float y, View view) {
|
|
1464
|
+
if (view == null) {
|
|
1465
|
+
return false;
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
return x >= view.getX() && x <= view.getX() + view.getWidth() && y >= view.getY() && y <= view.getY() + view.getHeight();
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
private AnnotationHit hitTest(float x, float y) {
|
|
1472
|
+
if (PdfView.this.getPageCount() <= 0) {
|
|
1473
|
+
return null;
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
float zoom = PdfView.this.getZoom();
|
|
1477
|
+
float documentY = y - PdfView.this.getCurrentYOffset();
|
|
1478
|
+
for (int i = 0; i < PdfView.this.getPageCount(); i++) {
|
|
1479
|
+
SizeF pageSize = PdfView.this.getPageSize(i);
|
|
1480
|
+
if (pageSize == null) {
|
|
1481
|
+
continue;
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
float scaledPageWidth = pageSize.getWidth() * zoom;
|
|
1485
|
+
float scaledPageHeight = pageSize.getHeight() * zoom;
|
|
1486
|
+
float horizontalMargin = Math.max(0f, (PdfView.this.getWidth() - scaledPageWidth) / 2f);
|
|
1487
|
+
float pageTop = getPageTop(i, zoom);
|
|
1488
|
+
if (documentY >= pageTop && documentY <= pageTop + scaledPageHeight) {
|
|
1489
|
+
float pageX = x - horizontalMargin;
|
|
1490
|
+
if (pageX >= 0f && pageX <= scaledPageWidth) {
|
|
1491
|
+
return new AnnotationHit(i, pageSize);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
return null;
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
private PointF normalizedPointFor(AnnotationHit hit, float x, float y) {
|
|
1500
|
+
float zoom = PdfView.this.getZoom();
|
|
1501
|
+
SizeF pageSize = hit.pageSize;
|
|
1502
|
+
float scaledPageWidth = pageSize.getWidth() * zoom;
|
|
1503
|
+
float scaledPageHeight = pageSize.getHeight() * zoom;
|
|
1504
|
+
float horizontalMargin = Math.max(0f, (PdfView.this.getWidth() - scaledPageWidth) / 2f);
|
|
1505
|
+
float pageTop = getPageTop(hit.pageIndex, zoom);
|
|
1506
|
+
float pageX = x - horizontalMargin;
|
|
1507
|
+
float documentY = y - PdfView.this.getCurrentYOffset();
|
|
1508
|
+
float pageY = documentY - pageTop;
|
|
1509
|
+
return new PointF(pageX / Math.max(1f, scaledPageWidth), pageY / Math.max(1f, scaledPageHeight));
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
private PointF viewPointForNormalizedPoint(int pageIndex, float normalizedX, float normalizedY) {
|
|
1513
|
+
SizeF pageSize = PdfView.this.getPageSize(pageIndex);
|
|
1514
|
+
float zoom = PdfView.this.getZoom();
|
|
1515
|
+
float scaledPageWidth = pageSize.getWidth() * zoom;
|
|
1516
|
+
float scaledPageHeight = pageSize.getHeight() * zoom;
|
|
1517
|
+
float horizontalMargin = Math.max(0f, (PdfView.this.getWidth() - scaledPageWidth) / 2f);
|
|
1518
|
+
float pageTop = getPageTop(pageIndex, zoom) + PdfView.this.getCurrentYOffset();
|
|
1519
|
+
return new PointF(horizontalMargin + (normalizedX * scaledPageWidth), pageTop + (normalizedY * scaledPageHeight));
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
private RectF viewRectForAnnotation(JSONObject annotation, int pageIndex) {
|
|
1523
|
+
JSONObject bounds = annotation.optJSONObject("bounds");
|
|
1524
|
+
String type = annotation.optString("type", "");
|
|
1525
|
+
if (bounds == null && !"ink".equals(type)) {
|
|
1526
|
+
return null;
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
SizeF pageSize = PdfView.this.getPageSize(pageIndex);
|
|
1530
|
+
if (pageSize == null) {
|
|
1531
|
+
return null;
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
float zoom = PdfView.this.getZoom();
|
|
1535
|
+
float scaledPageWidth = pageSize.getWidth() * zoom;
|
|
1536
|
+
float scaledPageHeight = pageSize.getHeight() * zoom;
|
|
1537
|
+
float horizontalMargin = Math.max(0f, (PdfView.this.getWidth() - scaledPageWidth) / 2f);
|
|
1538
|
+
float pageTop = getPageTop(pageIndex, zoom) + PdfView.this.getCurrentYOffset();
|
|
1539
|
+
|
|
1540
|
+
if ("ink".equals(type)) {
|
|
1541
|
+
JSONArray points = annotation.optJSONArray("points");
|
|
1542
|
+
if (points == null || points.length() == 0) {
|
|
1543
|
+
return null;
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
float minX = Float.MAX_VALUE;
|
|
1547
|
+
float minY = Float.MAX_VALUE;
|
|
1548
|
+
float maxX = -Float.MAX_VALUE;
|
|
1549
|
+
float maxY = -Float.MAX_VALUE;
|
|
1550
|
+
for (int i = 0; i < points.length(); i++) {
|
|
1551
|
+
JSONObject point = points.optJSONObject(i);
|
|
1552
|
+
if (point == null) {
|
|
1553
|
+
continue;
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
float pointX = (float) point.optDouble("x", 0f);
|
|
1557
|
+
float pointY = (float) point.optDouble("y", 0f);
|
|
1558
|
+
minX = Math.min(minX, pointX);
|
|
1559
|
+
minY = Math.min(minY, pointY);
|
|
1560
|
+
maxX = Math.max(maxX, pointX);
|
|
1561
|
+
maxY = Math.max(maxY, pointY);
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
if (minX == Float.MAX_VALUE || minY == Float.MAX_VALUE || maxX == -Float.MAX_VALUE || maxY == -Float.MAX_VALUE) {
|
|
1565
|
+
return null;
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
float left = horizontalMargin + (minX * scaledPageWidth);
|
|
1569
|
+
float top = pageTop + (minY * scaledPageHeight);
|
|
1570
|
+
float right = horizontalMargin + (maxX * scaledPageWidth);
|
|
1571
|
+
float bottom = pageTop + (maxY * scaledPageHeight);
|
|
1572
|
+
return new RectF(Math.min(left, right), Math.min(top, bottom), Math.max(left, right), Math.max(top, bottom));
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
float x = (float) bounds.optDouble("x", 0f);
|
|
1576
|
+
float y = (float) bounds.optDouble("y", 0f);
|
|
1577
|
+
float width = (float) bounds.optDouble("width", 0f);
|
|
1578
|
+
float height = (float) bounds.optDouble("height", 0f);
|
|
1579
|
+
|
|
1580
|
+
float left = horizontalMargin + (x * scaledPageWidth);
|
|
1581
|
+
float top = pageTop + (y * scaledPageHeight);
|
|
1582
|
+
float right = horizontalMargin + ((x + width) * scaledPageWidth);
|
|
1583
|
+
float bottom = pageTop + ((y + height) * scaledPageHeight);
|
|
1584
|
+
|
|
1585
|
+
return new RectF(Math.min(left, right), Math.min(top, bottom), Math.max(left, right), Math.max(top, bottom));
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
private JSONObject styleFor(JSONObject annotation) {
|
|
1589
|
+
JSONObject style = annotation.optJSONObject("style");
|
|
1590
|
+
return style == null ? new JSONObject() : style;
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
private int annotationColor(JSONObject annotation, int fallback) {
|
|
1594
|
+
String color = styleFor(annotation).optString("color", null);
|
|
1595
|
+
if (TextUtils.isEmpty(color)) {
|
|
1596
|
+
return fallback;
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
try {
|
|
1600
|
+
return Color.parseColor(color);
|
|
1601
|
+
} catch (IllegalArgumentException ex) {
|
|
1602
|
+
return fallback;
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
private int annotationFillColor(String type) {
|
|
1607
|
+
if ("highlight".equals(type)) {
|
|
1608
|
+
return Color.argb(90, 255, 230, 60);
|
|
1609
|
+
}
|
|
1610
|
+
if ("underline".equals(type)) {
|
|
1611
|
+
return Color.argb(120, 50, 120, 255);
|
|
1612
|
+
}
|
|
1613
|
+
if ("strikeout".equals(type)) {
|
|
1614
|
+
return Color.argb(120, 255, 50, 50);
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
return Color.BLACK;
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
private String nextLocalAnnotationId() {
|
|
1621
|
+
return "local-" + java.util.UUID.randomUUID().toString();
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
private float getPageTop(int pageIndex, float zoom) {
|
|
1625
|
+
float pageTop = 0f;
|
|
1626
|
+
for (int i = 0; i < pageIndex; i++) {
|
|
1627
|
+
SizeF previousPageSize = PdfView.this.getPageSize(i);
|
|
1628
|
+
if (previousPageSize == null) {
|
|
1629
|
+
continue;
|
|
1630
|
+
}
|
|
1631
|
+
pageTop += previousPageSize.getHeight() * zoom;
|
|
1632
|
+
pageTop += PdfView.this.spacing * zoom;
|
|
1633
|
+
}
|
|
1634
|
+
return pageTop;
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
private final class AnnotationHit {
|
|
1638
|
+
final int pageIndex;
|
|
1639
|
+
final SizeF pageSize;
|
|
1640
|
+
|
|
1641
|
+
AnnotationHit(int pageIndex, SizeF pageSize) {
|
|
1642
|
+
this.pageIndex = pageIndex;
|
|
1643
|
+
this.pageSize = pageSize;
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
495
1646
|
}
|
|
496
1647
|
|
|
497
1648
|
/**
|
|
@@ -539,6 +1690,16 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
|
|
|
539
1690
|
this.jumpTo(page);
|
|
540
1691
|
}
|
|
541
1692
|
|
|
1693
|
+
public void saveAnnotations() {
|
|
1694
|
+
if (annotationOverlayView != null) {
|
|
1695
|
+
annotationOverlayView.commitTextEditingIfNeeded();
|
|
1696
|
+
notifyOnChangeWithMessage("annotationSaveComplete|" + annotationOverlayView.serializeDocument());
|
|
1697
|
+
return;
|
|
1698
|
+
}
|
|
1699
|
+
|
|
1700
|
+
notifyOnChangeWithMessage("annotationSaveError|Annotation overlay unavailable");
|
|
1701
|
+
}
|
|
1702
|
+
|
|
542
1703
|
private void showLog(final String str) {
|
|
543
1704
|
Log.d("PdfView", str);
|
|
544
1705
|
}
|