-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathPTFlutterDocumentController.m
More file actions
1969 lines (1616 loc) · 73.8 KB
/
Copy pathPTFlutterDocumentController.m
File metadata and controls
1969 lines (1616 loc) · 73.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "PdftronFlutterPlugin.h"
#import "PTFlutterDocumentController.h"
#import "DocumentViewFactory.h"
#include <objc/runtime.h>
static BOOL PT_addMethod(Class cls, SEL selector, void (^block)(id))
{
const IMP implementation = imp_implementationWithBlock(block);
const BOOL added = class_addMethod(cls, selector, implementation, "v@:");
if (!added) {
imp_removeBlock(implementation);
return NO;
}
return YES;
}
@interface PTFlutterDocumentController()
{
NSMutableDictionary<NSString *, NSNumber *> *_annotationToolbarItemKeyMap;
NSUInteger _annotationToolbarItemCounter;
NSMutableDictionary<NSString *, NSNumber *> *_appBarButtonItemKeyMap;
NSUInteger _appBarButtonItemCounter;
}
@property (nonatomic, strong, nullable) UIBarButtonItem *leadingNavButtonItem;
// Array of wrapped PTExtendedAnnotTypes.
@property (nonatomic, strong, nullable) NSArray<NSNumber *> *hideAnnotMenuToolsAnnotTypes;
@property (nonatomic, strong, nullable) NSString *toolbarId;
@end
@implementation PTFlutterDocumentController
- (void)viewDidLoad
{
[super viewDidLoad];
// Workaround to ensure thumbnail slider is hidden at launch.
self.thumbnailSliderHidden = YES;
self.thumbnailSliderController.view.hidden = YES;
NSNotificationCenter *center = NSNotificationCenter.defaultCenter;
PTUndoRedoManager *undoRedoManager = self.toolManager.undoRedoManager;
[center addObserver:self
selector:@selector(undoManagerSentNotification:)
name:PTUndoRedoManagerDidRedoNotification
object:undoRedoManager];
[center addObserver:self
selector:@selector(undoManagerSentNotification:)
name:PTUndoRedoManagerDidUndoNotification
object:undoRedoManager];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// bottomToolBar / thumbnailSlider enabling
self.thumbnailSliderEnabled = ![self isBottomToolbarHidden];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
if (self.needsDocumentLoaded) {
self.needsDocumentLoaded = NO;
self.needsRemoteDocumentLoaded = NO;
self.documentLoaded = YES;
if (self.initialPageNumber > 0) {
[self.pdfViewCtrl SetCurrentPage:self.initialPageNumber];
}
[self applyLayoutMode];
if (self.base64) {
NSString *filePath = self.coordinatedDocument.fileURL.path;
[self.plugin documentController:self documentLoadedFromFilePath:filePath];
} else {
[self.plugin documentController:self documentLoadedFromFilePath:nil];
}
}
if (![self.toolManager isReadonly] && self.readOnly) {
self.toolManager.readonly = YES;
}
[self.plugin.tabbedDocumentViewController.tabManager saveItems];
}
- (void)setThumbnailSliderHidden:(BOOL)hidden animated:(BOOL)animated
{
// Prevent the thumbnail slider from being shown.
// NOTE: This method will be called with hidden=NO when the bottomToolbarEnabled property is
// enabled (which is just a convenience property for the thumbnailSliderEnabled property).
if (!hidden) {
return;
}
[super setThumbnailSliderHidden:hidden animated:animated];
}
- (void)openDocumentWithURL:(NSURL *)url password:(NSString *)password
{
self.local = [url isFileURL];
self.documentLoaded = NO;
self.needsDocumentLoaded = NO;
self.needsRemoteDocumentLoaded = NO;
[super openDocumentWithURL:url password:password];
[self applyLayoutMode];
}
- (void)openDocumentWithPDFDoc:(PTPDFDoc *)document
{
self.local = YES;
self.documentLoaded = NO;
self.needsDocumentLoaded = NO;
self.needsRemoteDocumentLoaded = NO;
[super openDocumentWithPDFDoc:document];
[self applyLayoutMode];
}
- (void)didOpenDocument
{
[super didOpenDocument];
[self applyLayoutMode];
}
- (BOOL)isTopToolbarEnabled
{
return !self.topToolbarsHidden;
}
- (BOOL)areTopToolbarsEnabled
{
return !self.topToolbarsHidden;
}
- (BOOL)isNavigationBarEnabled
{
return !self.topAppNavBarHidden;;
}
- (BOOL)controlsHidden
{
if (self.navigationController) {
if ([self isTopToolbarEnabled]) {
if (![self isNavigationBarEnabled]) {
return NO;
}
return [self.navigationController isNavigationBarHidden];
}
if ([self isBottomToolbarEnabled]) {
return [self.navigationController isToolbarHidden];
}
}
return [super controlsHidden];
}
- (void)setControlsHidden:(BOOL)controlsHidden animated:(BOOL)animated
{
[super setControlsHidden:controlsHidden animated:animated];
// When the top toolbars are enabled...
if ([self areTopToolbarsEnabled] &&
// ... but the navigation bar (app nav. bar) is disabled...
![self isNavigationBarEnabled] &&
// ... and we are in a tabbed viewer...
self.tabbedDocumentViewController.tabsEnabled) {
// ... then manually toggle the tabbed viewer's tab bar visibility.
[self.tabbedDocumentViewController setTabBarHidden:controlsHidden animated:animated];
}
}
- (void)setUneditableAnnotTypes:(NSArray<NSString *> *)uneditableAnnotTypes
{
[self setAnnotationEditingPermission:uneditableAnnotTypes toValue:NO];
}
- (void)setAnnotationEditingPermission:(NSArray *)stringsArray toValue:(BOOL)value
{
PTToolManager *toolManager = self.toolManager;
for (NSObject *item in stringsArray) {
if ([item isKindOfClass:[NSString class]]) {
NSString *string = (NSString *)item;
PTExtendedAnnotType typeToSetPermission = [self convertAnnotationNameToAnnotType:string];
[toolManager annotationOptionsForAnnotType:typeToSetPermission].canEdit = value;
}
}
}
- (void)setDefaultEraserType:(NSString *)defaultEraserType {
PTToolManager *toolManager = self.toolManager;
if ([defaultEraserType isEqualToString:PTInkEraserModeAllKey]) {
toolManager.eraserMode = PTInkEraserModeAll;
} else if ([defaultEraserType isEqualToString:PTInkEraserModePointsKey]) {
toolManager.eraserMode = PTInkEraserModePoints;
}
}
- (void)hideViewModeItems:(NSArray<NSString *> *)viewModeItems
{
[self setViewModeItemVisibility:viewModeItems hidden:YES];
}
- (void)setViewModeItemVisibility:(NSArray *)stringsArray hidden:(BOOL)value
{
for (NSString * viewModeItemString in stringsArray) {
if ([viewModeItemString isEqualToString:PTViewModeColorModeKey]) {
self.settingsViewController.colorModeLightHidden = value;
self.settingsViewController.colorModeDarkHidden = value;
self.settingsViewController.colorModeSepiaHidden = value;
} else if ([viewModeItemString isEqualToString:PTViewModeRotationKey]) {
self.settingsViewController.pageRotationHidden = value;
} else if ([viewModeItemString isEqualToString:PTViewModeCropKey]) {
self.settingsViewController.cropPagesHidden = value;
} else if ([viewModeItemString isEqualToString:PTViewModeVerticalScrollingKey]) {
self.settingsViewController.viewModeContinuousHidden = value;
}
}
}
#pragma mark - <PTBookmarkViewControllerDelegate>
- (void)bookmarkViewController:(PTBookmarkViewController *)bookmarkViewController didAddBookmark:(PTUserBookmark *)bookmark
{
[super bookmarkViewController:bookmarkViewController didAddBookmark:bookmark];
[self bookmarksModified];
}
- (void)bookmarkViewController:(PTBookmarkViewController *)bookmarkViewController didRemoveBookmark:(PTUserBookmark *)bookmark
{
[super bookmarkViewController:bookmarkViewController didRemoveBookmark:bookmark];
[self bookmarksModified];
}
- (void)bookmarkViewController:(PTBookmarkViewController *)bookmarkViewController didModifyBookmark:(PTUserBookmark *)bookmark
{
[super bookmarkViewController:bookmarkViewController didModifyBookmark:bookmark];
[self bookmarksModified];
}
-(void)bookmarksModified
{
__block NSString* json;
NSError* error;
BOOL exceptionOccurred = [self.pdfViewCtrl DocLockReadWithBlock:^(PTPDFDoc * _Nullable doc) {
json = [PTBookmarkManager.defaultManager exportBookmarksFromDoc:doc];
} error:&error];
if(exceptionOccurred)
{
NSLog(@"Error: %@", error.description);
}
[self.plugin documentController:self bookmarksDidChange:json];
}
- (void)bookmarkViewControllerDidCancel:(PTBookmarkViewController *)bookmarkViewController
{
[bookmarkViewController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - <PTToolManagerDelegate>
- (void)toolManagerToolChanged:(PTToolManager *)toolManager
{
PTTool *tool = toolManager.tool;
const BOOL backToPan = tool.backToPanToolAfterUse;
[super toolManagerToolChanged:toolManager];
if (tool.backToPanToolAfterUse != backToPan) {
tool.backToPanToolAfterUse = backToPan;
}
}
-(void)toolManager:(PTToolManager*)toolManager willRemoveAnnotation:(nonnull PTAnnot *)annotation onPageNumber:(int)pageNumber
{
NSString* annotationsWithActionString = [self generateAnnotationsWithActionString:@[annotation] onPageNumber:pageNumber action:PTDeleteActionKey];
if (annotationsWithActionString) {
[self.plugin documentController:self annotationsChangedWithActionString:annotationsWithActionString];
}
if (!self.isAnnotationManagerEnabled || self.userId == nil) {
NSString* xfdf = [self generateXfdfCommandWithAdded:Nil modified:Nil removed:@[annotation]];
[self.plugin documentController:self annotationsAsXFDFCommand:xfdf];
}
}
-(void)toolManager:(PTToolManager *)toolManager annotationRemoved:(PTAnnot *)annotation onPageNumber:(unsigned long)pageNumber
{
if (self.isAnnotationManagerEnabled && self.userId) {
NSString *xfdf = [self.pdfViewCtrl.externalAnnotManager GetLastXFDF];
[self.plugin documentController:self annotationsAsXFDFCommand:xfdf];
}
}
- (void)toolManager:(PTToolManager *)toolManager annotationAdded:(PTAnnot *)annotation onPageNumber:(unsigned long)pageNumber
{
NSString* annotationsWithActionString = [self generateAnnotationsWithActionString:@[annotation] onPageNumber:pageNumber action:PTAddActionKey];
if (annotationsWithActionString) {
[self.plugin documentController:self annotationsChangedWithActionString:annotationsWithActionString];
}
NSString* xfdf;
if (self.isAnnotationManagerEnabled && self.userId) {
xfdf = [self.pdfViewCtrl.externalAnnotManager GetLastXFDF];
} else {
xfdf = [self generateXfdfCommandWithAdded:@[annotation] modified:Nil removed:Nil];
}
[self.plugin documentController:self annotationsAsXFDFCommand:xfdf];
}
- (void)toolManager:(PTToolManager *)toolManager annotationModified:(PTAnnot *)annotation onPageNumber:(unsigned long)pageNumber
{
NSString* annotationsWithActionString = [self generateAnnotationsWithActionString:@[annotation] onPageNumber:pageNumber action:PTModifyActionKey];
if (annotationsWithActionString) {
[self.plugin documentController:self annotationsChangedWithActionString:annotationsWithActionString];
}
NSString* xfdf;
if (self.isAnnotationManagerEnabled && self.userId) {
xfdf = [self.pdfViewCtrl.externalAnnotManager GetLastXFDF];
} else {
xfdf = [self generateXfdfCommandWithAdded:Nil modified:@[annotation] removed:Nil];
}
[self.plugin documentController:self annotationsAsXFDFCommand:xfdf];
}
- (void)toolManager:(PTToolManager *)toolManager didSelectAnnotation:(PTAnnot *)annotation onPageNumber:(unsigned long)pageNumber
{
if (annotation.IsValid) {
__block NSString *uniqueId;
__block PTPDFRect *screenRect;
NSError *error;
[self.pdfViewCtrl DocLockReadWithBlock:^(PTPDFDoc * _Nullable doc) {
PTObj *uniqueIdObj = [annotation GetUniqueID];
if ([uniqueIdObj IsValid] && [uniqueIdObj IsString]) {
uniqueId = [uniqueIdObj GetAsPDFText];
}
screenRect = [self.pdfViewCtrl GetScreenRectForAnnot:annotation
page_num:(int)pageNumber];
} error:&error];
if (error) {
NSLog(@"An error occurred: %@", error);
return;
}
NSDictionary *annotDict = @{
PTAnnotationIdKey: uniqueId ?: @"",
PTAnnotationPageNumberKey: [NSNumber numberWithLong:pageNumber],
PTRectKey: @{
PTX1Key: @([screenRect GetX1]),
PTY1Key: @([screenRect GetY1]),
PTX2Key: @([screenRect GetX2]),
PTY2Key: @([screenRect GetY2]),
PTWidthKey: @([screenRect Width]),
PTHeightKey: @([screenRect Height]),
},
};
[self.plugin documentController:self annotationsSelected:[PdftronFlutterPlugin PT_idToJSONString:@[annotDict]]];
}
}
- (void)toolManager:(PTToolManager *)toolManager formFieldDataModified:(PTAnnot *)annotation onPageNumber:(unsigned long)pageNumber {
if (annotation.GetType == e_ptWidget) {
NSError *error;
__block NSString *fieldName;
__block NSString *fieldValue;
[self.pdfViewCtrl DocLockReadWithBlock:^(PTPDFDoc * _Nullable doc) {
PTWidget *widget = [[PTWidget alloc] initWithAnn:annotation];
PTField *field = [widget GetField];
fieldName = [field IsValid] ? [field GetName] : @"";
fieldValue = [field IsValid] ? [field GetValueAsString] : @"";
} error:&error];
if (error) {
NSLog(@"An error occurred: %@", error);
return;
}
if (fieldName && fieldValue) {
NSDictionary *fieldDict = @{
PTFormFieldNameKey: fieldName,
PTFormFieldValueKey: fieldValue,
};
[self.plugin documentController:self formFieldValueChanged:[PdftronFlutterPlugin PT_idToJSONString:@[fieldDict]]];
}
// TODO: collab manager
/*
copied from RN
if (!self.collaborationManager) {
PTVectorAnnot *annots = [[PTVectorAnnot alloc] init];
[annots add:annot];
[self rnt_sendExportAnnotationCommandWithAction:PTModifyAnnotationActionKey xfdfCommand:[self generateXfdfCommand:[[PTVectorAnnot alloc] init] modified:annots deleted:[[PTVectorAnnot alloc] init]]];
}
*/
}
}
- (BOOL)toolManager:(PTToolManager *)toolManager shouldShowMenu:(UIMenuController *)menuController forAnnotation:(PTAnnot *)annotation onPageNumber:(unsigned long)pageNumber
{
BOOL result = [super toolManager:toolManager shouldShowMenu:menuController forAnnotation:annotation onPageNumber:pageNumber];
if (!result) {
return NO;
}
BOOL showMenu = YES;
if (annotation) {
showMenu = [self filterMenuItemsForAnnotationSelectionMenu:menuController forAnnotation:annotation];
} else {
showMenu = [self filterMenuItemsForLongPressMenu:menuController];
}
return showMenu;
}
- (void)toolManager:(nonnull PTToolManager *)toolManager pageMovedFromPageNumber:(int)oldPageNumber toPageNumber:(int)newPageNumber
{
NSDictionary *resultDict = @{
PTPreviousPageNumberKey: [NSNumber numberWithInt:oldPageNumber],
PTPageNumberKey: [NSNumber numberWithInt:newPageNumber],
};
[self.plugin documentController:self pageMoved:[PdftronFlutterPlugin PT_idToJSONString:resultDict]];
}
- (BOOL)filterMenuItemsForAnnotationSelectionMenu:(UIMenuController *)menuController forAnnotation:(PTAnnot *)annot
{
__block PTExtendedAnnotType annotType = PTExtendedAnnotTypeUnknown;
NSError *error = nil;
[self.pdfViewCtrl DocLockReadWithBlock:^(PTPDFDoc *doc) {
if ([annot IsValid]) {
annotType = annot.extendedAnnotType;
}
} error:&error];
if (error) {
NSLog(@"%@", error);
}
if ([self.hideAnnotMenuToolsAnnotTypes containsObject:@(annotType)]) {
return NO;
}
NSString *editString = ([annot GetType] == e_ptFreeText) ? PTEditTextMenuItemIdentifierKey : PTEditInkMenuItemIdentifierKey;
// Mapping from menu item title to identifier.
NSDictionary<NSString *, NSString *> *map = @{
PTStyleMenuItemTitleKey: PTStyleMenuItemIdentifierKey,
PTNoteMenuItemTitleKey: PTNoteMenuItemIdentifierKey,
PTCopyMenuItemTitleKey: PTCopyMenuItemIdentifierKey,
PTDeleteMenuItemTitleKey: PTDeleteMenuItemIdentifierKey,
PTTypeMenuItemTitleKey: PTTypeMenuItemIdentifierKey,
PTSearchMenuItemTitleKey: PTSearchMenuItemIdentifierKey,
PTEditMenuItemTitleKey: editString,
PTFlattenMenuItemTitleKey: PTFlattenMenuItemIdentifierKey,
PTOpenMenuItemTitleKey: PTOpenMenuItemIdentifierKey,
PTCalibrateMenuItemTitleKey: PTCalibrateMenuItemIdentifierKey,
};
// Get the localized title for each menu item.
NSMutableDictionary<NSString *, NSString *> *localizedMap = [NSMutableDictionary dictionary];
for (NSString *key in map) {
NSString *localizedKey = PTLocalizedString(key, nil);
if (!localizedKey) {
localizedKey = key;
}
localizedMap[localizedKey] = map[key];
}
NSMutableArray<UIMenuItem *> *permittedItems = [NSMutableArray array];
for (UIMenuItem *menuItem in menuController.menuItems) {
NSString *menuItemId = localizedMap[menuItem.title];
if (!self.annotationMenuItems) {
[permittedItems addObject:menuItem];
}
else {
if (menuItemId && [self.annotationMenuItems containsObject:menuItemId]) {
[permittedItems addObject:menuItem];
}
}
// Override action of of overridden annotation menu items.
if (menuItemId && [self.overrideAnnotationMenuBehavior containsObject:menuItemId]) {
NSString *actionName = [NSString stringWithFormat:@"overriddenPressed_%@",
menuItemId];
const SEL selector = NSSelectorFromString(actionName);
PT_addMethod([self class], selector, ^(id self) {
[self overriddenAnnotationMenuItemPressed:menuItemId];
});
menuItem.action = selector;
}
}
menuController.menuItems = [permittedItems copy];
return YES;
}
- (BOOL)filterMenuItemsForLongPressMenu:(UIMenuController *)menuController {
if (!self.longPressMenuEnabled) {
menuController.menuItems = nil;
return NO;
}
// Mapping from menu item title to identifier.
NSDictionary<NSString *, NSString *> *map = @{
PTCopyMenuItemTitleKey: PTCopyMenuItemIdentifierKey,
PTSearchMenuItemTitleKey: PTSearchMenuItemIdentifierKey,
PTShareMenuItemTitleKey: PTShareMenuItemIdentifierKey,
PTReadMenuItemTitleKey: PTReadMenuItemIdentifierKey,
};
// Get the localized title for each menu item.
NSMutableDictionary<NSString *, NSString *> *localizedMap = [NSMutableDictionary dictionary];
for (NSString *key in map) {
NSString *localizedKey = PTLocalizedString(key, nil);
if (!localizedKey) {
localizedKey = key;
}
localizedMap[localizedKey] = map[key];
}
NSMutableArray<UIMenuItem *> *permittedItems = [NSMutableArray array];
for (UIMenuItem *menuItem in menuController.menuItems) {
NSString *menuItemId = localizedMap[menuItem.title];
if (!self.longPressMenuItems) {
[permittedItems addObject:menuItem];
}
else {
if (!menuItemId) {
// If it is not one of copy, search, share and read, then it should be added
[permittedItems addObject:menuItem];
} else if ([self.longPressMenuItems containsObject:menuItemId]) {
[permittedItems addObject:menuItem];
}
}
// Override action of of overridden annotation menu items.
if (menuItemId && [self.overrideLongPressMenuBehavior containsObject:menuItemId]) {
NSString *actionName = [NSString stringWithFormat:@"overriddenPressed_%@",
menuItemId];
const SEL selector = NSSelectorFromString(actionName);
PT_addMethod([self class], selector, ^(id self) {
[self overriddenLongPressMenuItemPressed:menuItemId];
});
menuItem.action = selector;
}
}
menuController.menuItems = [permittedItems copy];
return YES;
}
- (void)overriddenAnnotationMenuItemPressed:(NSString *)menuItemId
{
NSMutableArray<PTAnnot *> *annotations = [NSMutableArray array];
if ([self.toolManager.tool isKindOfClass:[PTAnnotEditTool class]]) {
PTAnnotEditTool *annotEdit = (PTAnnotEditTool *)self.toolManager.tool;
if (annotEdit.selectedAnnotations.count > 0) {
[annotations addObjectsFromArray:annotEdit.selectedAnnotations];
}
}
else if (self.toolManager.tool.currentAnnotation) {
[annotations addObject:self.toolManager.tool.currentAnnotation];
}
const int pageNumber = self.toolManager.tool.annotationPageNumber;
NSArray *annotArray = [self generateAnnotationDictArray:annotations onPageNumber:pageNumber];
NSDictionary* resultDict = @{
PTAnnotationMenuItemKey: menuItemId,
PTAnnotationListKey: annotArray,
};
[self.plugin documentController:self annotationMenuPressed:[PdftronFlutterPlugin PT_idToJSONString:resultDict]];
}
- (void)overriddenLongPressMenuItemPressed:(NSString *)menuItemId
{
NSMutableString *selectedText = [NSMutableString string];
NSError *error = nil;
[self.pdfViewCtrl DocLockReadWithBlock:^(PTPDFDoc *doc) {
if (![self.pdfViewCtrl HasSelection]) {
return;
}
const int selectionBeginPage = self.pdfViewCtrl.selectionBeginPage;
const int selectionEndPage = self.pdfViewCtrl.selectionEndPage;
for (int pageNumber = selectionBeginPage; pageNumber <= selectionEndPage; pageNumber++) {
if ([self.pdfViewCtrl HasSelectionOnPage:pageNumber]) {
PTSelection *selection = [self.pdfViewCtrl GetSelection:pageNumber];
NSString *selectionText = [selection GetAsUnicode];
[selectedText appendString:selectionText];
}
}
} error:&error];
if (error) {
NSLog(@"%@", error);
}
NSDictionary *resultDict = @{
PTLongPressMenuItemKey: menuItemId,
PTLongPressTextKey: selectedText,
};
[self.plugin documentController:self longPressMenuPressed:[PdftronFlutterPlugin PT_idToJSONString:resultDict]];
}
- (void)pdfViewCtrl:(PTPDFViewCtrl*)pdfViewCtrl pdfScrollViewDidZoom:(UIScrollView *)scrollView
{
const double zoom = self.pdfViewCtrl.zoom * self.pdfViewCtrl.zoomScale;
[self.plugin documentController:self zoomChanged:[NSNumber numberWithDouble:zoom]];
}
- (void)pdfViewCtrl:(PTPDFViewCtrl*)pdfViewCtrl pageNumberChangedFrom:(int)oldPageNumber To:(int)newPageNumber
{
NSDictionary *resultDict = @{
PTPreviousPageNumberKey: [NSNumber numberWithInt:oldPageNumber],
PTPageNumberKey: [NSNumber numberWithInt:newPageNumber],
};
[self.plugin documentController:self pageChanged:[PdftronFlutterPlugin PT_idToJSONString:resultDict]];
}
- (void)pdfViewCtrl:(PTPDFViewCtrl *)pdfViewCtrl pdfScrollViewDidScroll:(UIScrollView *)scrollView
{
[self.plugin documentController:self scrollChanged:@""];
}
-(NSString*)generateXfdfCommandWithAdded:(NSArray<PTAnnot*>*)added modified:(NSArray<PTAnnot*>*)modified removed:(NSArray<PTAnnot*>*)removed
{
PTPDFDoc* pdfDoc = self.document;
if (pdfDoc) {
PTVectorAnnot* addedV = [[PTVectorAnnot alloc] init];
for(PTAnnot* annot in added)
{
[addedV add:annot];
}
PTVectorAnnot* modifiedV = [[PTVectorAnnot alloc] init];
for(PTAnnot* annot in modified)
{
[modifiedV add:annot];
}
PTVectorAnnot* removedV = [[PTVectorAnnot alloc] init];
for(PTAnnot* annot in removed)
{
[removedV add:annot];
}
NSError *error;
__block PTFDFDoc* fdfDoc;
[self.pdfViewCtrl DocLockReadWithBlock:^(PTPDFDoc * _Nullable doc) {
fdfDoc = [pdfDoc FDFExtractCommand:addedV annot_modified:modifiedV annot_deleted:removedV];
} error:&error];
if (!error) {
return [fdfDoc SaveAsXFDFToString];
}
NSLog(@"Error: %@", error.description);
}
return Nil;
}
-(NSString*)generateAnnotationsWithActionString:(NSArray<PTAnnot *> *)annotations onPageNumber:(unsigned long)pageNumber action:(NSString *)action
{
NSArray<NSDictionary *>* annotDictArray = [self generateAnnotationDictArray:annotations onPageNumber:pageNumber];
NSDictionary<NSString *, NSString *>* resultDict = @{
PTAnnotationListKey : [PdftronFlutterPlugin PT_idToJSONString:annotDictArray],
PTActionKey : action,
};
return [PdftronFlutterPlugin PT_idToJSONString:resultDict];
}
- (NSArray*)generateAnnotationDictArray:(NSArray<PTAnnot *> *)annotations onPageNumber:(unsigned long)pageNumber
{
NSMutableArray<NSDictionary *>* resultArray = [[NSMutableArray alloc] init];
for (PTAnnot * annotation in annotations) {
if (annotation.IsValid) {
__block NSString *uniqueId;
NSError *error;
[self.pdfViewCtrl DocLockReadWithBlock:^(PTPDFDoc * _Nullable doc) {
PTObj *uniqueIdObj = [annotation GetUniqueID];
if ([uniqueIdObj IsValid] && [uniqueIdObj IsString]) {
uniqueId = [uniqueIdObj GetAsPDFText];
}
} error:&error];
if (error) {
NSLog(@"An error occurred: %@", error);
return nil;
}
if (!uniqueId) {
continue;
}
NSDictionary *annotDict = @{
PTAnnotationIdKey: uniqueId,
PTAnnotationPageNumberKey: [NSNumber numberWithLong:pageNumber],
};
[resultArray addObject:annotDict];
}
}
return [resultArray copy];
}
-(PTExtendedAnnotType)convertAnnotationNameToAnnotType:(NSString*)annotationName
{
NSDictionary<NSString *, NSNumber *>* typeMap = @{
PTAnnotationCreateStickyToolKey : @(PTExtendedAnnotTypeText),
PTStickyToolButtonKey : @(PTExtendedAnnotTypeText),
PTAnnotationCreateFreeHandToolKey : @(PTExtendedAnnotTypeInk),
PTAnnotationCreateTextHighlightToolKey : @(PTExtendedAnnotTypeHighlight),
PTAnnotationCreateTextUnderlineToolKey : @(PTExtendedAnnotTypeUnderline),
PTAnnotationCreateTextSquigglyToolKey : @(PTExtendedAnnotTypeSquiggly),
PTAnnotationCreateTextStrikeoutToolKey : @(PTExtendedAnnotTypeStrikeOut),
PTAnnotationCreateFreeTextToolKey : @(PTExtendedAnnotTypeFreeText),
PTAnnotationCreateCalloutToolKey : @(PTExtendedAnnotTypeCallout),
PTAnnotationCreateSignatureToolKey : @(PTExtendedAnnotTypeSignature),
PTAnnotationCreateLineToolKey : @(PTExtendedAnnotTypeLine),
PTAnnotationCreateArrowToolKey : @(PTExtendedAnnotTypeArrow),
PTAnnotationCreatePolylineToolKey : @(PTExtendedAnnotTypePolyline),
PTAnnotationCreateStampToolKey : @(PTExtendedAnnotTypeImageStamp),
PTAnnotationCreateRectangleToolKey : @(PTExtendedAnnotTypeSquare),
PTAnnotationCreateEllipseToolKey : @(PTExtendedAnnotTypeCircle),
PTAnnotationCreatePolygonToolKey : @(PTExtendedAnnotTypePolygon),
PTAnnotationCreatePolygonCloudToolKey : @(PTExtendedAnnotTypeCloudy),
PTAnnotationCreateDistanceMeasurementToolKey : @(PTExtendedAnnotTypeRuler),
PTAnnotationCreatePerimeterMeasurementToolKey : @(PTExtendedAnnotTypePerimeter),
PTAnnotationCreateAreaMeasurementToolKey : @(PTExtendedAnnotTypeArea),
PTAnnotationCreateFileAttachmentToolKey : @(PTExtendedAnnotTypeFileAttachment),
PTAnnotationCreateSoundToolKey : @(PTExtendedAnnotTypeSound),
PTAnnotationCreateFreeHighlighterToolKey : @(PTExtendedAnnotTypeFreehandHighlight),
PTPencilKitDrawingToolKey: @(PTExtendedAnnotTypePencilDrawing),
PTAnnotationCreateFreeHighlighterToolKey: @(PTExtendedAnnotTypeFreehandHighlight),
PTAnnotationCreateRubberStampToolKey: @(PTExtendedAnnotTypeStamp),
PTAnnotationCreateRedactionToolKey : @(PTExtendedAnnotTypeRedact),
PTAnnotationCreateLinkToolKey : @(PTExtendedAnnotTypeLink),
// @"PTPanToolKey" : @(),
// @"FormCreateTextField" : @(),
// @"FormCreateCheckboxField" : @(),
// @"FormCreateRadioField" : @(),
// @"FormCreateComboBoxField" : @(),
// @"FormCreateListBoxField" : @()
};
PTExtendedAnnotType annotType = PTExtendedAnnotTypeUnknown;
if( typeMap[annotationName] )
{
annotType = [typeMap[annotationName] unsignedIntValue];
}
return annotType;
}
-(PTAnnotType)annotTypeForString:(NSString *)string {
if ([string isEqualToString:PTAnnotationCreateStickyToolKey]) {
return e_ptText;
} else if ([string isEqualToString:PTAnnotationCreateLinkToolKey]) {
return e_ptLink;
} else if ([string isEqualToString:PTAnnotationCreateFreeTextToolKey]) {
return e_ptFreeText;
} else if ([string isEqualToString:PTAnnotationCreateLineToolKey]) {
return e_ptLine;
} else if ([string isEqualToString:PTAnnotationCreateRectangleToolKey]) {
return e_ptSquare;
} else if ([string isEqualToString:PTAnnotationCreateEllipseToolKey]) {
return e_ptCircle;
} else if ([string isEqualToString:PTAnnotationCreatePolygonToolKey]) {
return e_ptPolygon;
} else if ([string isEqualToString:PTAnnotationCreatePolylineToolKey]) {
return e_ptPolyline;
} else if ([string isEqualToString:PTAnnotationCreateFreeHighlighterToolKey]) {
return e_ptHighlight;
} else if ([string isEqualToString:PTAnnotationCreateTextUnderlineToolKey]) {
return e_ptUnderline;
} else if ([string isEqualToString:PTAnnotationCreateTextSquigglyToolKey]) {
return e_ptSquiggly;
} else if ([string isEqualToString:PTAnnotationCreateTextStrikeoutToolKey]) {
return e_ptStrikeOut;
} else if ([string isEqualToString:PTAnnotationCreateStampToolKey]) {
return e_ptStamp;
} else if ([string isEqualToString:PTAnnotationCreateFreeHandToolKey]) {
return e_ptInk;
} else if ([string isEqualToString:PTAnnotationCreateFileAttachmentToolKey]) {
return e_ptFileAttachment;
} else if ([string isEqualToString:PTAnnotationCreateSoundToolKey]) {
return e_ptSound;
} else if ([string isEqualToString:PTFormCreateTextFieldToolKey]) {
return e_ptWidget;
} else if ([string isEqualToString:PTAnnotationCreateRedactionToolKey]) {
return e_ptRedact;
// } else if ([string isEqualToString:@"");
// return e_ptCaret;
// } else if ([string isEqualToString:@"");
// return e_ptPopup;
// } else if ([string isEqualToString:@"");
// return e_ptMovie;
// } else if ([string isEqualToString:@"");
// return e_ptScreen;
// } else if ([string isEqualToString:@"");
// return e_ptPrinterMark;
// } else if ([string isEqualToString:@"");
// return e_ptTrapNet;
// } else if ([string isEqualToString:@"");
// return e_pt3D;
// } else if ([string isEqualToString:@"");
// return e_ptProjection;
// } else if ([string isEqualToString:@"");
// return e_ptRichMedia;
// } else if ([string isEqualToString:@"");
// return e_ptUnknown;
// } else if ([string isEqualToString:@"");
// return e_ptWatermark;
}
return e_ptUnknown;
}
- (BOOL)toolManager:(PTToolManager *)toolManager shouldHandleLinkAnnotation:(PTAnnot *)annotation orLinkInfo:(PTLinkInfo *)linkInfo onPageNumber:(unsigned long)pageNumber
{
if (![self.overrideBehavior containsObject:PTLinkPressLinkAnnotationKey]) {
return YES;
}
PTPDFViewCtrl *pdfViewCtrl = self.pdfViewCtrl;
__block NSString *url = nil;
NSError *error = nil;
[pdfViewCtrl DocLockReadWithBlock:^(PTPDFDoc * _Nullable doc) {
// Check for a valid link annotation.
if (![annotation IsValid] ||
annotation.extendedAnnotType != PTExtendedAnnotTypeLink) {
return;
}
PTLink *linkAnnot = [[PTLink alloc] initWithAnn:annotation];
// Check for a valid URI action.
PTAction *action = [linkAnnot GetAction];
if (![action IsValid] ||
[action GetType] != e_ptURI) {
return;
}
PTObj *actionObj = [action GetSDFObj];
if (![actionObj IsValid]) {
return;
}
// Get the action's URI.
PTObj *uriObj = [actionObj FindObj:PTURILinkAnnotationKey];
if ([uriObj IsValid] && [uriObj IsString]) {
url = [uriObj GetAsPDFText];
}
} error:&error];
if (error) {
NSLog(@"%@", error);
}
if (url) {
NSDictionary* behaviorDict = @{
PTActionLinkAnnotationKey: PTLinkPressLinkAnnotationKey,
PTDataLinkAnnotationKey: @{
PTURLLinkAnnotationKey: url,
},
};
[self.plugin documentController:self behaviorActivated:[PdftronFlutterPlugin PT_idToJSONString: behaviorDict]];
// Link handled.
return NO;
}
return YES;
}
#pragma mark - Notification
- (void)undoManagerSentNotification:(NSNotification *) notification
{
if (self.isAnnotationManagerEnabled && self.userId) {
NSString* xfdf = [self.pdfViewCtrl.externalAnnotManager GetLastXFDF];
[self.plugin documentController:self annotationsAsXFDFCommand:xfdf];
} else {
// For UndoRedoStateChanged event
}
}
#pragma mark - <PTPDFViewCtrlDelegate>
- (void)pdfViewCtrl:(PTPDFViewCtrl *)pdfViewCtrl onSetDoc:(PTPDFDoc *)doc
{
[super pdfViewCtrl:pdfViewCtrl onSetDoc:doc];
// to align with Android's document-opened-event timing.
if (self.local && !self.documentLoaded) {
self.needsDocumentLoaded = YES;
}
else if (!self.local && !self.documentLoaded && self.needsRemoteDocumentLoaded) {
self.needsDocumentLoaded = YES;
}
else if (!self.local && !self.documentLoaded && self.coordinatedDocument.fileURL) {
self.needsDocumentLoaded = YES;
}
}
- (void)pdfViewCtrl:(PTPDFViewCtrl *)pdfViewCtrl downloadEventType:(PTDownloadedType)type pageNumber:(int)pageNum message:(NSString *)message
{
if (type == e_ptdownloadedtype_finished && !self.documentLoaded) {
self.needsRemoteDocumentLoaded = YES;
}
[super pdfViewCtrl:pdfViewCtrl downloadEventType:type pageNumber:pageNum message:message];
}
#pragma mark - <PTOutlineViewControllerDelegate>
- (void)outlineViewControllerDidCancel:(PTOutlineViewController *)outlineViewController
{
[outlineViewController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - <PTAnnotationViewControllerDelegate>
- (void)annotationViewControllerDidCancel:(PTAnnotationViewController *)annotationViewController
{
[annotationViewController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Viewer Settings
- (void)initViewerSettings
{
_base64 = NO;
_readOnly = NO;
_showNavButton = YES;
_longPressMenuEnabled = true;
_annotationToolbarSwitcherHidden = NO;
_topToolbarsHidden = NO;
_topAppNavBarHidden = NO;
_bottomToolbarHidden = NO;
_toolbarsHiddenOnTap = YES;
_readOnly = NO;