@amafil/react-native-pdf-toolkit 1.0.15 → 1.1.1

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