@papyrus-sdk/engine-native 0.2.20 → 0.2.22-beta.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/LICENSE +21 -0
- package/android/src/main/cpp/CMakeLists.txt +12 -1
- package/android/src/main/cpp/papyrus_outline_loader_test.cpp +240 -0
- package/android/src/main/cpp/papyrus_text_search.cpp +162 -24
- package/android/src/main/cpp/papyrus_text_word.cpp +33 -0
- package/android/src/main/cpp/papyrus_text_word.h +19 -0
- package/android/src/main/cpp/papyrus_text_word_test.cpp +72 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +3 -3
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.kt +11 -3
- package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +279 -138
- package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerViewManager.java +9 -7
- package/android/src/main/java/com/papyrus/engine/PapyrusTextSelect.java +2 -2
- package/android/src/main/java/com/papyrus/engine/PapyrusTextSelectionGesture.java +44 -2
- package/dist/index.d.mts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +43 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +42 -11
- package/dist/index.mjs.map +1 -1
- package/ios/PapyrusEngineStore.h +3 -0
- package/ios/PapyrusEngineStore.m +29 -7
- package/ios/PapyrusNativeEngine.m +184 -0
- package/ios/PapyrusPdfDocumentView.h +24 -0
- package/ios/PapyrusPdfDocumentView.m +658 -0
- package/ios/PapyrusPdfDocumentViewManager.m +32 -0
- package/package.json +5 -5
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
#import "PapyrusPdfDocumentView.h"
|
|
2
|
+
|
|
3
|
+
#import <QuartzCore/QuartzCore.h>
|
|
4
|
+
#import <dispatch/dispatch.h>
|
|
5
|
+
#import <math.h>
|
|
6
|
+
|
|
7
|
+
#import "PapyrusEngineStore.h"
|
|
8
|
+
|
|
9
|
+
static const CGFloat PapyrusPdfMinimumZoom = 0.5;
|
|
10
|
+
static const CGFloat PapyrusPdfMaximumZoom = 4.0;
|
|
11
|
+
static const CGFloat PapyrusPdfZoomEventTolerance = 0.02;
|
|
12
|
+
static const NSTimeInterval PapyrusPdfZoomEventInterval = 0.10;
|
|
13
|
+
static const NSTimeInterval PapyrusPdfScrollEventInterval = 0.08;
|
|
14
|
+
static void *PapyrusPdfScrollObservationContext = &PapyrusPdfScrollObservationContext;
|
|
15
|
+
|
|
16
|
+
static NSDictionary *PapyrusNormalizedSelectionRect(CGRect rect, CGRect pageBounds) {
|
|
17
|
+
if (CGRectIsNull(rect) || CGRectIsEmpty(rect)) return nil;
|
|
18
|
+
rect = CGRectIntersection(rect, pageBounds);
|
|
19
|
+
if (CGRectIsNull(rect) || CGRectIsEmpty(rect)) return nil;
|
|
20
|
+
|
|
21
|
+
CGFloat pageWidth = CGRectGetWidth(pageBounds);
|
|
22
|
+
CGFloat pageHeight = CGRectGetHeight(pageBounds);
|
|
23
|
+
if (!isfinite(pageWidth) || !isfinite(pageHeight) || pageWidth <= 0 || pageHeight <= 0) {
|
|
24
|
+
return nil;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
CGFloat x = (CGRectGetMinX(rect) - CGRectGetMinX(pageBounds)) / pageWidth;
|
|
28
|
+
CGFloat y = 1.0 - ((CGRectGetMaxY(rect) - CGRectGetMinY(pageBounds)) / pageHeight);
|
|
29
|
+
CGFloat width = CGRectGetWidth(rect) / pageWidth;
|
|
30
|
+
CGFloat height = CGRectGetHeight(rect) / pageHeight;
|
|
31
|
+
if (!isfinite(x) || !isfinite(y) || !isfinite(width) || !isfinite(height)) return nil;
|
|
32
|
+
|
|
33
|
+
x = MIN(1.0, MAX(0.0, x));
|
|
34
|
+
y = MIN(1.0, MAX(0.0, y));
|
|
35
|
+
width = MIN(1.0 - x, MAX(0.0, width));
|
|
36
|
+
height = MIN(1.0 - y, MAX(0.0, height));
|
|
37
|
+
if (width <= 0 || height <= 0) return nil;
|
|
38
|
+
|
|
39
|
+
return @{@"x" : @(x), @"y" : @(y), @"width" : @(width), @"height" : @(height)};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@interface PapyrusPdfDocumentView ()
|
|
43
|
+
@property (nonatomic, strong) PDFView *pdfView;
|
|
44
|
+
@property (nonatomic, weak) UIScrollView *observedScrollView;
|
|
45
|
+
@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;
|
|
46
|
+
@property (nonatomic, strong) UITapGestureRecognizer *doubleTapRecognizer;
|
|
47
|
+
@property (nonatomic, assign) CGSize lastLayoutSize;
|
|
48
|
+
@property (nonatomic, assign) NSInteger lastEmittedPage;
|
|
49
|
+
@property (nonatomic, assign) CGFloat lastEmittedZoom;
|
|
50
|
+
@property (nonatomic, copy) NSString *lastVisiblePagesSignature;
|
|
51
|
+
@property (nonatomic, assign) BOOL zoomEventScheduled;
|
|
52
|
+
@property (nonatomic, assign) BOOL suppressScaleSynchronization;
|
|
53
|
+
@property (nonatomic, assign) BOOL scrollEventScheduled;
|
|
54
|
+
@property (nonatomic, assign) CGFloat pendingScrollOffsetY;
|
|
55
|
+
@property (nonatomic, assign) CFTimeInterval lastScrollEventTime;
|
|
56
|
+
@property (nonatomic, copy) NSString *lastSelectionSignature;
|
|
57
|
+
@property (nonatomic, assign) NSInteger lastSelectionPageIndex;
|
|
58
|
+
@property (nonatomic, assign) BOOL lastSelectionWasActive;
|
|
59
|
+
@end
|
|
60
|
+
|
|
61
|
+
@implementation PapyrusPdfDocumentView
|
|
62
|
+
|
|
63
|
+
- (instancetype)initWithFrame:(CGRect)frame {
|
|
64
|
+
self = [super initWithFrame:frame];
|
|
65
|
+
if (!self) return nil;
|
|
66
|
+
|
|
67
|
+
_pageTheme = @"normal";
|
|
68
|
+
_viewMode = @"continuous";
|
|
69
|
+
_zoom = 1.0;
|
|
70
|
+
_currentPage = 1;
|
|
71
|
+
_lastEmittedPage = NSNotFound;
|
|
72
|
+
_lastSelectionPageIndex = NSNotFound;
|
|
73
|
+
_lastEmittedZoom = NAN;
|
|
74
|
+
_lastLayoutSize = CGSizeZero;
|
|
75
|
+
self.clipsToBounds = YES;
|
|
76
|
+
self.backgroundColor = UIColor.whiteColor;
|
|
77
|
+
|
|
78
|
+
_pdfView = [[PDFView alloc] initWithFrame:CGRectZero];
|
|
79
|
+
_pdfView.displayBox = kPDFDisplayBoxCropBox;
|
|
80
|
+
_pdfView.displayDirection = kPDFDisplayDirectionVertical;
|
|
81
|
+
_pdfView.displayMode = kPDFDisplaySinglePageContinuous;
|
|
82
|
+
_pdfView.displaysPageBreaks = YES;
|
|
83
|
+
_pdfView.autoScales = YES;
|
|
84
|
+
_pdfView.userInteractionEnabled = YES;
|
|
85
|
+
_pdfView.backgroundColor = UIColor.whiteColor;
|
|
86
|
+
_pdfView.clipsToBounds = YES;
|
|
87
|
+
[self addSubview:_pdfView];
|
|
88
|
+
|
|
89
|
+
_tapRecognizer = [[UITapGestureRecognizer alloc]
|
|
90
|
+
initWithTarget:self
|
|
91
|
+
action:@selector(handleDocumentTap:)];
|
|
92
|
+
_tapRecognizer.delegate = self;
|
|
93
|
+
_tapRecognizer.cancelsTouchesInView = NO;
|
|
94
|
+
_tapRecognizer.delaysTouchesBegan = NO;
|
|
95
|
+
[_pdfView addGestureRecognizer:_tapRecognizer];
|
|
96
|
+
|
|
97
|
+
_doubleTapRecognizer = [[UITapGestureRecognizer alloc]
|
|
98
|
+
initWithTarget:self
|
|
99
|
+
action:@selector(handleDocumentDoubleTap:)];
|
|
100
|
+
_doubleTapRecognizer.numberOfTapsRequired = 2;
|
|
101
|
+
_doubleTapRecognizer.delegate = self;
|
|
102
|
+
_doubleTapRecognizer.cancelsTouchesInView = NO;
|
|
103
|
+
_doubleTapRecognizer.delaysTouchesBegan = NO;
|
|
104
|
+
[_pdfView addGestureRecognizer:_doubleTapRecognizer];
|
|
105
|
+
[_tapRecognizer requireGestureRecognizerToFail:_doubleTapRecognizer];
|
|
106
|
+
|
|
107
|
+
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
|
108
|
+
[center addObserver:self
|
|
109
|
+
selector:@selector(handlePdfPageChanged:)
|
|
110
|
+
name:PDFViewPageChangedNotification
|
|
111
|
+
object:_pdfView];
|
|
112
|
+
[center addObserver:self
|
|
113
|
+
selector:@selector(handlePdfScaleChanged:)
|
|
114
|
+
name:PDFViewScaleChangedNotification
|
|
115
|
+
object:_pdfView];
|
|
116
|
+
[center addObserver:self
|
|
117
|
+
selector:@selector(handlePdfVisiblePagesChanged:)
|
|
118
|
+
name:PDFViewVisiblePagesChangedNotification
|
|
119
|
+
object:_pdfView];
|
|
120
|
+
[center addObserver:self
|
|
121
|
+
selector:@selector(handlePdfSelectionChanged:)
|
|
122
|
+
name:PDFViewSelectionChangedNotification
|
|
123
|
+
object:_pdfView];
|
|
124
|
+
[center addObserver:self
|
|
125
|
+
selector:@selector(handleStoredDocumentChanged:)
|
|
126
|
+
name:PapyrusEngineStoreDocumentDidChangeNotification
|
|
127
|
+
object:[PapyrusEngineStore shared]];
|
|
128
|
+
|
|
129
|
+
return self;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
- (void)dealloc {
|
|
133
|
+
[self stopObservingScrollView];
|
|
134
|
+
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
135
|
+
self.tapRecognizer.delegate = nil;
|
|
136
|
+
self.doubleTapRecognizer.delegate = nil;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
- (void)setSelectionActive:(BOOL)selectionActive {
|
|
140
|
+
_selectionActive = selectionActive;
|
|
141
|
+
if (!selectionActive) {
|
|
142
|
+
[self clearCurrentSelection];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
- (void)layoutSubviews {
|
|
147
|
+
[super layoutSubviews];
|
|
148
|
+
self.pdfView.frame = self.bounds;
|
|
149
|
+
|
|
150
|
+
if (!CGSizeEqualToSize(self.lastLayoutSize, self.bounds.size)) {
|
|
151
|
+
self.lastLayoutSize = self.bounds.size;
|
|
152
|
+
[self updateFitScalePreservingViewport];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
[self refreshScrollViewObservation];
|
|
156
|
+
[self emitVisiblePagesIfNeeded];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
- (void)setEngineId:(NSString *)engineId {
|
|
160
|
+
NSString *normalized = engineId.length > 0 ? [engineId copy] : nil;
|
|
161
|
+
if ((_engineId == normalized) || [_engineId isEqualToString:normalized]) return;
|
|
162
|
+
_engineId = normalized;
|
|
163
|
+
[self reloadDocumentFromStore];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
- (void)setPageTheme:(NSString *)pageTheme {
|
|
167
|
+
_pageTheme = pageTheme.length > 0 ? [pageTheme copy] : @"normal";
|
|
168
|
+
// Non-normal page themes route to the compatibility viewer in JavaScript.
|
|
169
|
+
self.backgroundColor = UIColor.whiteColor;
|
|
170
|
+
self.pdfView.backgroundColor = UIColor.whiteColor;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
- (void)setViewMode:(NSString *)viewMode {
|
|
174
|
+
NSString *normalized = [viewMode isEqualToString:@"single"] ? @"single" : @"continuous";
|
|
175
|
+
if ([_viewMode isEqualToString:normalized]) return;
|
|
176
|
+
CGFloat desiredZoom = [self clampedZoom:self.zoom];
|
|
177
|
+
BOOL wasSuppressingScaleSynchronization = self.suppressScaleSynchronization;
|
|
178
|
+
self.suppressScaleSynchronization = YES;
|
|
179
|
+
PDFPage *currentPage = self.pdfView.currentPage;
|
|
180
|
+
_viewMode = normalized;
|
|
181
|
+
self.pdfView.displayMode = [normalized isEqualToString:@"single"]
|
|
182
|
+
? kPDFDisplaySinglePage
|
|
183
|
+
: kPDFDisplaySinglePageContinuous;
|
|
184
|
+
_zoom = desiredZoom;
|
|
185
|
+
[self updateFitScalePreservingViewport];
|
|
186
|
+
if (currentPage) {
|
|
187
|
+
[self.pdfView goToPage:currentPage];
|
|
188
|
+
} else {
|
|
189
|
+
[self applyCurrentPage];
|
|
190
|
+
}
|
|
191
|
+
_zoom = desiredZoom;
|
|
192
|
+
[self applyNormalizedZoom];
|
|
193
|
+
self.suppressScaleSynchronization = wasSuppressingScaleSynchronization;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
- (void)setCurrentPage:(NSInteger)currentPage {
|
|
197
|
+
_currentPage = MAX(1, currentPage);
|
|
198
|
+
[self applyCurrentPage];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
- (void)setZoom:(CGFloat)zoom {
|
|
202
|
+
if (!isfinite(zoom)) return;
|
|
203
|
+
CGFloat normalized = [self clampedZoom:zoom];
|
|
204
|
+
CGFloat currentNativeZoom = [self normalizedZoomForPdfView];
|
|
205
|
+
_zoom = normalized;
|
|
206
|
+
if (currentNativeZoom > 0 && fabs(currentNativeZoom - normalized) <= 0.015) {
|
|
207
|
+
self.lastEmittedZoom = normalized;
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
[self applyNormalizedZoom];
|
|
211
|
+
self.lastEmittedZoom = normalized;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
- (void)handleStoredDocumentChanged:(NSNotification *)notification {
|
|
215
|
+
NSString *changedEngineId = [notification.userInfo[PapyrusEngineStoreEngineIdKey] copy];
|
|
216
|
+
if (changedEngineId.length == 0) return;
|
|
217
|
+
|
|
218
|
+
__weak typeof(self) weakSelf = self;
|
|
219
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
220
|
+
typeof(self) strongSelf = weakSelf;
|
|
221
|
+
if (!strongSelf || ![changedEngineId isEqualToString:strongSelf.engineId]) return;
|
|
222
|
+
[strongSelf reloadDocumentFromStore];
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
- (void)reloadDocumentFromStore {
|
|
227
|
+
NSAssert(NSThread.isMainThread, @"PDFView must only be updated on the main thread");
|
|
228
|
+
PDFDocument *document = self.engineId.length > 0
|
|
229
|
+
? [[PapyrusEngineStore shared] documentForEngine:self.engineId]
|
|
230
|
+
: nil;
|
|
231
|
+
if (self.pdfView.document == document) {
|
|
232
|
+
[self applyCurrentPage];
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
CGFloat desiredZoom = [self clampedZoom:self.zoom];
|
|
237
|
+
BOOL wasSuppressingScaleSynchronization = self.suppressScaleSynchronization;
|
|
238
|
+
self.suppressScaleSynchronization = YES;
|
|
239
|
+
self.lastVisiblePagesSignature = nil;
|
|
240
|
+
self.lastEmittedPage = NSNotFound;
|
|
241
|
+
[self clearCurrentSelection];
|
|
242
|
+
self.pdfView.document = document;
|
|
243
|
+
if (!document) {
|
|
244
|
+
self.lastEmittedZoom = NAN;
|
|
245
|
+
_zoom = desiredZoom;
|
|
246
|
+
self.suppressScaleSynchronization = wasSuppressingScaleSynchronization;
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
[self configureDisplayMode];
|
|
251
|
+
self.pdfView.autoScales = YES;
|
|
252
|
+
[self.pdfView layoutDocumentView];
|
|
253
|
+
[self updateScaleLimitsForFitScale:self.pdfView.scaleFactorForSizeToFit];
|
|
254
|
+
self.pdfView.autoScales = NO;
|
|
255
|
+
_zoom = desiredZoom;
|
|
256
|
+
[self applyNormalizedZoom];
|
|
257
|
+
[self applyCurrentPage];
|
|
258
|
+
_zoom = desiredZoom;
|
|
259
|
+
[self applyNormalizedZoom];
|
|
260
|
+
[self refreshScrollViewObservation];
|
|
261
|
+
[self emitVisiblePagesIfNeeded];
|
|
262
|
+
self.suppressScaleSynchronization = wasSuppressingScaleSynchronization;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
- (void)configureDisplayMode {
|
|
266
|
+
self.pdfView.displayBox = kPDFDisplayBoxCropBox;
|
|
267
|
+
self.pdfView.displayDirection = kPDFDisplayDirectionVertical;
|
|
268
|
+
self.pdfView.displaysPageBreaks = YES;
|
|
269
|
+
self.pdfView.displayMode = [self.viewMode isEqualToString:@"single"]
|
|
270
|
+
? kPDFDisplaySinglePage
|
|
271
|
+
: kPDFDisplaySinglePageContinuous;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
- (void)updateFitScalePreservingViewport {
|
|
275
|
+
if (!self.pdfView.document || CGRectGetWidth(self.bounds) <= 0 || CGRectGetHeight(self.bounds) <= 0) return;
|
|
276
|
+
|
|
277
|
+
CGFloat desiredZoom = [self clampedZoom:self.zoom];
|
|
278
|
+
BOOL wasSuppressingScaleSynchronization = self.suppressScaleSynchronization;
|
|
279
|
+
self.suppressScaleSynchronization = YES;
|
|
280
|
+
PDFPage *currentPage = self.pdfView.currentPage;
|
|
281
|
+
self.pdfView.autoScales = YES;
|
|
282
|
+
[self.pdfView layoutDocumentView];
|
|
283
|
+
[self updateScaleLimitsForFitScale:self.pdfView.scaleFactorForSizeToFit];
|
|
284
|
+
self.pdfView.autoScales = NO;
|
|
285
|
+
_zoom = desiredZoom;
|
|
286
|
+
[self applyNormalizedZoom];
|
|
287
|
+
|
|
288
|
+
if (currentPage) {
|
|
289
|
+
[self.pdfView goToPage:currentPage];
|
|
290
|
+
} else {
|
|
291
|
+
[self applyCurrentPage];
|
|
292
|
+
}
|
|
293
|
+
_zoom = desiredZoom;
|
|
294
|
+
[self applyNormalizedZoom];
|
|
295
|
+
self.suppressScaleSynchronization = wasSuppressingScaleSynchronization;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
- (void)updateScaleLimitsForFitScale:(CGFloat)fitScale {
|
|
299
|
+
if (!isfinite(fitScale) || fitScale <= 0) return;
|
|
300
|
+
self.pdfView.minScaleFactor = fitScale * PapyrusPdfMinimumZoom;
|
|
301
|
+
self.pdfView.maxScaleFactor = fitScale * PapyrusPdfMaximumZoom;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
- (void)applyNormalizedZoom {
|
|
305
|
+
CGFloat fitScale = self.pdfView.scaleFactorForSizeToFit;
|
|
306
|
+
if (!isfinite(fitScale) || fitScale <= 0) return;
|
|
307
|
+
[self updateScaleLimitsForFitScale:fitScale];
|
|
308
|
+
|
|
309
|
+
CGFloat targetScale = fitScale * [self clampedZoom:self.zoom];
|
|
310
|
+
targetScale = MAX(self.pdfView.minScaleFactor,
|
|
311
|
+
MIN(self.pdfView.maxScaleFactor, targetScale));
|
|
312
|
+
if (fabs(self.pdfView.scaleFactor - targetScale) > 0.001) {
|
|
313
|
+
self.pdfView.scaleFactor = targetScale;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
- (CGFloat)normalizedZoomForPdfView {
|
|
318
|
+
CGFloat fitScale = self.pdfView.scaleFactorForSizeToFit;
|
|
319
|
+
if (!isfinite(fitScale) || fitScale <= 0) return 0;
|
|
320
|
+
CGFloat normalized = self.pdfView.scaleFactor / fitScale;
|
|
321
|
+
return isfinite(normalized) ? [self clampedZoom:normalized] : 0;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
- (CGFloat)clampedZoom:(CGFloat)zoom {
|
|
325
|
+
return MAX(PapyrusPdfMinimumZoom, MIN(PapyrusPdfMaximumZoom, zoom));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
- (void)applyCurrentPage {
|
|
329
|
+
PDFDocument *document = self.pdfView.document;
|
|
330
|
+
if (!document || document.pageCount <= 0 || self.currentPage <= 0) return;
|
|
331
|
+
NSInteger targetPage = MAX(1, MIN(self.currentPage, document.pageCount));
|
|
332
|
+
_currentPage = targetPage;
|
|
333
|
+
PDFPage *page = [document pageAtIndex:targetPage - 1];
|
|
334
|
+
if (page && self.pdfView.currentPage != page) {
|
|
335
|
+
[self.pdfView goToPage:page];
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
- (void)handlePdfPageChanged:(NSNotification *)notification {
|
|
340
|
+
if (notification.object != self.pdfView) return;
|
|
341
|
+
[self emitCurrentPageIfNeeded];
|
|
342
|
+
[self emitVisiblePagesIfNeeded];
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
- (void)emitCurrentPageIfNeeded {
|
|
346
|
+
PDFDocument *document = self.pdfView.document;
|
|
347
|
+
PDFPage *page = self.pdfView.currentPage;
|
|
348
|
+
if (!document || !page) return;
|
|
349
|
+
NSInteger pageNumber = [document indexForPage:page] + 1;
|
|
350
|
+
if (pageNumber <= 0 || pageNumber == self.lastEmittedPage) return;
|
|
351
|
+
|
|
352
|
+
_currentPage = pageNumber;
|
|
353
|
+
self.lastEmittedPage = pageNumber;
|
|
354
|
+
if (self.onPageChange) {
|
|
355
|
+
self.onPageChange(@{@"page" : @(pageNumber)});
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
- (void)handlePdfScaleChanged:(NSNotification *)notification {
|
|
360
|
+
if (notification.object != self.pdfView) return;
|
|
361
|
+
if (self.suppressScaleSynchronization) return;
|
|
362
|
+
CGFloat normalized = [self normalizedZoomForPdfView];
|
|
363
|
+
if (normalized <= 0) return;
|
|
364
|
+
_zoom = normalized;
|
|
365
|
+
if (self.zoomEventScheduled) return;
|
|
366
|
+
|
|
367
|
+
self.zoomEventScheduled = YES;
|
|
368
|
+
__weak typeof(self) weakSelf = self;
|
|
369
|
+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
|
|
370
|
+
(int64_t)(PapyrusPdfZoomEventInterval * NSEC_PER_SEC)),
|
|
371
|
+
dispatch_get_main_queue(), ^{
|
|
372
|
+
typeof(self) strongSelf = weakSelf;
|
|
373
|
+
if (!strongSelf) return;
|
|
374
|
+
strongSelf.zoomEventScheduled = NO;
|
|
375
|
+
[strongSelf emitZoomIfNeeded];
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
- (void)emitZoomIfNeeded {
|
|
380
|
+
CGFloat normalized = [self normalizedZoomForPdfView];
|
|
381
|
+
if (normalized <= 0) return;
|
|
382
|
+
if (isfinite(self.lastEmittedZoom) &&
|
|
383
|
+
fabs(normalized - self.lastEmittedZoom) <= PapyrusPdfZoomEventTolerance) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
_zoom = normalized;
|
|
388
|
+
self.lastEmittedZoom = normalized;
|
|
389
|
+
if (self.onZoomChange) {
|
|
390
|
+
self.onZoomChange(@{@"zoom" : @(normalized)});
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
- (void)handlePdfVisiblePagesChanged:(NSNotification *)notification {
|
|
395
|
+
if (notification.object != self.pdfView) return;
|
|
396
|
+
[self emitVisiblePagesIfNeeded];
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
- (void)handlePdfSelectionChanged:(NSNotification *)notification {
|
|
400
|
+
if (notification.object != self.pdfView) return;
|
|
401
|
+
[self emitCurrentSelectionIfNeeded];
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
- (void)emitCurrentSelectionIfNeeded {
|
|
405
|
+
PDFSelection *selection = self.pdfView.currentSelection;
|
|
406
|
+
NSString *text = selection.string ?: @"";
|
|
407
|
+
PDFDocument *document = self.pdfView.document;
|
|
408
|
+
PDFPage *page = selection.pages.firstObject;
|
|
409
|
+
NSInteger pageIndex = document && page ? [document indexForPage:page] : NSNotFound;
|
|
410
|
+
if (!document || !page || pageIndex == NSNotFound || text.length == 0 ||
|
|
411
|
+
[text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet].length == 0) {
|
|
412
|
+
[self emitSelectionClearedIfNeeded];
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
CGRect pageBounds = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
417
|
+
if (CGRectIsEmpty(pageBounds)) {
|
|
418
|
+
[self emitSelectionClearedIfNeeded];
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
NSMutableArray<NSDictionary *> *rects = [NSMutableArray array];
|
|
423
|
+
for (PDFSelection *line in selection.selectionsByLine) {
|
|
424
|
+
NSDictionary *normalizedRect = PapyrusNormalizedSelectionRect(
|
|
425
|
+
[line boundsForPage:page], pageBounds);
|
|
426
|
+
if (normalizedRect) [rects addObject:normalizedRect];
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (rects.count == 0) {
|
|
430
|
+
NSDictionary *normalizedRect = PapyrusNormalizedSelectionRect(
|
|
431
|
+
[selection boundsForPage:page], pageBounds);
|
|
432
|
+
if (normalizedRect) [rects addObject:normalizedRect];
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
NSString *signature = [NSString stringWithFormat:@"%ld|%@|%@", (long)pageIndex, text, rects];
|
|
436
|
+
if (self.lastSelectionWasActive && [signature isEqualToString:self.lastSelectionSignature]) return;
|
|
437
|
+
self.lastSelectionWasActive = YES;
|
|
438
|
+
self.lastSelectionPageIndex = pageIndex;
|
|
439
|
+
self.lastSelectionSignature = signature;
|
|
440
|
+
if (self.onTextSelected) {
|
|
441
|
+
self.onTextSelected(@{@"text" : text, @"pageIndex" : @(pageIndex), @"rects" : rects});
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
- (void)emitSelectionClearedIfNeeded {
|
|
446
|
+
if (!self.lastSelectionWasActive) return;
|
|
447
|
+
NSInteger pageIndex = self.lastSelectionPageIndex;
|
|
448
|
+
if (pageIndex == NSNotFound) pageIndex = MAX(0, self.currentPage - 1);
|
|
449
|
+
self.lastSelectionWasActive = NO;
|
|
450
|
+
self.lastSelectionSignature = nil;
|
|
451
|
+
self.lastSelectionPageIndex = NSNotFound;
|
|
452
|
+
if (self.onTextSelected) {
|
|
453
|
+
self.onTextSelected(@{@"text" : @"", @"pageIndex" : @(pageIndex), @"rects" : @[]});
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
- (void)clearCurrentSelection {
|
|
458
|
+
if (self.pdfView.currentSelection) {
|
|
459
|
+
self.pdfView.currentSelection = nil;
|
|
460
|
+
}
|
|
461
|
+
[self emitSelectionClearedIfNeeded];
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
- (void)emitVisiblePagesIfNeeded {
|
|
465
|
+
PDFDocument *document = self.pdfView.document;
|
|
466
|
+
if (!document || self.pdfView.bounds.size.width <= 0 || self.pdfView.bounds.size.height <= 0) return;
|
|
467
|
+
|
|
468
|
+
NSMutableArray<NSDictionary *> *pages = [NSMutableArray array];
|
|
469
|
+
NSMutableArray<NSString *> *signatureParts = [NSMutableArray array];
|
|
470
|
+
for (PDFPage *page in self.pdfView.visiblePages) {
|
|
471
|
+
NSInteger pageIndex = [document indexForPage:page];
|
|
472
|
+
if (pageIndex == NSNotFound) continue;
|
|
473
|
+
CGRect pageBounds = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
474
|
+
CGRect viewBounds = [self.pdfView convertRect:pageBounds fromPage:page];
|
|
475
|
+
CGRect intersection = CGRectIntersection(self.pdfView.bounds, viewBounds);
|
|
476
|
+
CGFloat pageArea = CGRectGetWidth(viewBounds) * CGRectGetHeight(viewBounds);
|
|
477
|
+
CGFloat visibleArea = CGRectIsNull(intersection)
|
|
478
|
+
? 0
|
|
479
|
+
: CGRectGetWidth(intersection) * CGRectGetHeight(intersection);
|
|
480
|
+
if (!isfinite(pageArea) || pageArea <= 0 || visibleArea <= 0) continue;
|
|
481
|
+
|
|
482
|
+
CGFloat visibleRatio = MIN(1.0, MAX(0.0, visibleArea / pageArea));
|
|
483
|
+
if (visibleRatio <= 0) continue;
|
|
484
|
+
[pages addObject:@{@"pageIndex" : @(pageIndex), @"visibleRatio" : @(visibleRatio)}];
|
|
485
|
+
NSInteger bucket = (NSInteger)llround(visibleRatio * 20.0);
|
|
486
|
+
[signatureParts addObject:[NSString stringWithFormat:@"%ld:%ld", (long)pageIndex, (long)bucket]];
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
if (pages.count == 0) return;
|
|
490
|
+
NSString *signature = [signatureParts componentsJoinedByString:@"|"];
|
|
491
|
+
if ([signature isEqualToString:self.lastVisiblePagesSignature]) return;
|
|
492
|
+
self.lastVisiblePagesSignature = signature;
|
|
493
|
+
if (self.onVisiblePagesChange) {
|
|
494
|
+
self.onVisiblePagesChange(@{@"pages" : pages});
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
- (UIScrollView *)findScrollViewInView:(UIView *)view {
|
|
499
|
+
for (UIView *subview in view.subviews) {
|
|
500
|
+
if ([subview isKindOfClass:[UIScrollView class]]) {
|
|
501
|
+
return (UIScrollView *)subview;
|
|
502
|
+
}
|
|
503
|
+
UIScrollView *nested = [self findScrollViewInView:subview];
|
|
504
|
+
if (nested) return nested;
|
|
505
|
+
}
|
|
506
|
+
return nil;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
- (void)refreshScrollViewObservation {
|
|
510
|
+
UIScrollView *scrollView = [self findScrollViewInView:self.pdfView];
|
|
511
|
+
if (scrollView == self.observedScrollView) return;
|
|
512
|
+
[self stopObservingScrollView];
|
|
513
|
+
self.observedScrollView = scrollView;
|
|
514
|
+
if (!scrollView) return;
|
|
515
|
+
|
|
516
|
+
@try {
|
|
517
|
+
[scrollView addObserver:self
|
|
518
|
+
forKeyPath:@"contentOffset"
|
|
519
|
+
options:NSKeyValueObservingOptionNew
|
|
520
|
+
context:PapyrusPdfScrollObservationContext];
|
|
521
|
+
} @catch (__unused NSException *exception) {
|
|
522
|
+
self.observedScrollView = nil;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
- (void)stopObservingScrollView {
|
|
527
|
+
UIScrollView *scrollView = self.observedScrollView;
|
|
528
|
+
if (!scrollView) return;
|
|
529
|
+
@try {
|
|
530
|
+
[scrollView removeObserver:self
|
|
531
|
+
forKeyPath:@"contentOffset"
|
|
532
|
+
context:PapyrusPdfScrollObservationContext];
|
|
533
|
+
} @catch (__unused NSException *exception) {
|
|
534
|
+
}
|
|
535
|
+
self.observedScrollView = nil;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
- (void)observeValueForKeyPath:(NSString *)keyPath
|
|
539
|
+
ofObject:(id)object
|
|
540
|
+
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
|
|
541
|
+
context:(void *)context {
|
|
542
|
+
if (context != PapyrusPdfScrollObservationContext) {
|
|
543
|
+
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
if (![keyPath isEqualToString:@"contentOffset"]) return;
|
|
547
|
+
if (!NSThread.isMainThread) {
|
|
548
|
+
__weak typeof(self) weakSelf = self;
|
|
549
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
550
|
+
typeof(self) strongSelf = weakSelf;
|
|
551
|
+
if (strongSelf && object == strongSelf.observedScrollView) {
|
|
552
|
+
UIScrollView *scrollView = strongSelf.observedScrollView;
|
|
553
|
+
[strongSelf scheduleScrollEventWithOffsetY:scrollView.contentOffset.y];
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
if (object != self.observedScrollView) return;
|
|
559
|
+
UIScrollView *scrollView = self.observedScrollView;
|
|
560
|
+
CGFloat offsetY = scrollView.contentOffset.y;
|
|
561
|
+
[self scheduleScrollEventWithOffsetY:offsetY];
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
- (void)scheduleScrollEventWithOffsetY:(CGFloat)offsetY {
|
|
565
|
+
self.pendingScrollOffsetY = offsetY;
|
|
566
|
+
CFTimeInterval now = CACurrentMediaTime();
|
|
567
|
+
CFTimeInterval elapsed = now - self.lastScrollEventTime;
|
|
568
|
+
if (elapsed >= PapyrusPdfScrollEventInterval && !self.scrollEventScheduled) {
|
|
569
|
+
[self emitScrollEvent];
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
if (self.scrollEventScheduled) return;
|
|
573
|
+
|
|
574
|
+
self.scrollEventScheduled = YES;
|
|
575
|
+
CFTimeInterval delay = MAX(0, PapyrusPdfScrollEventInterval - elapsed);
|
|
576
|
+
__weak typeof(self) weakSelf = self;
|
|
577
|
+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)),
|
|
578
|
+
dispatch_get_main_queue(), ^{
|
|
579
|
+
typeof(self) strongSelf = weakSelf;
|
|
580
|
+
if (!strongSelf) return;
|
|
581
|
+
strongSelf.scrollEventScheduled = NO;
|
|
582
|
+
[strongSelf emitScrollEvent];
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
- (void)emitScrollEvent {
|
|
587
|
+
self.lastScrollEventTime = CACurrentMediaTime();
|
|
588
|
+
if (self.onScroll) {
|
|
589
|
+
self.onScroll(@{@"offsetY" : @(self.pendingScrollOffsetY)});
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
- (void)handleDocumentTap:(UITapGestureRecognizer *)recognizer {
|
|
594
|
+
if (recognizer.state != UIGestureRecognizerStateEnded) return;
|
|
595
|
+
PDFDocument *document = self.pdfView.document;
|
|
596
|
+
if (!document) return;
|
|
597
|
+
|
|
598
|
+
CGPoint viewPoint = [recognizer locationInView:self.pdfView];
|
|
599
|
+
if (self.pdfView.currentSelection && ![self selectionContainsViewPoint:viewPoint]) {
|
|
600
|
+
[self clearCurrentSelection];
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
PDFPage *page = [self.pdfView pageForPoint:viewPoint nearest:YES];
|
|
604
|
+
if (!page) return;
|
|
605
|
+
CGRect pageBounds = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
606
|
+
if (CGRectGetWidth(pageBounds) <= 0 || CGRectGetHeight(pageBounds) <= 0) return;
|
|
607
|
+
CGPoint pagePoint = [self.pdfView convertPoint:viewPoint toPage:page];
|
|
608
|
+
CGFloat x = (pagePoint.x - CGRectGetMinX(pageBounds)) / CGRectGetWidth(pageBounds);
|
|
609
|
+
CGFloat y = 1.0 - ((pagePoint.y - CGRectGetMinY(pageBounds)) / CGRectGetHeight(pageBounds));
|
|
610
|
+
NSInteger pageIndex = [document indexForPage:page];
|
|
611
|
+
if (pageIndex == NSNotFound) return;
|
|
612
|
+
|
|
613
|
+
if (self.onTap) {
|
|
614
|
+
self.onTap(@{
|
|
615
|
+
@"pageIndex" : @(pageIndex),
|
|
616
|
+
@"x" : @(MIN(1.0, MAX(0.0, x))),
|
|
617
|
+
@"y" : @(MIN(1.0, MAX(0.0, y)))
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
- (void)handleDocumentDoubleTap:(UITapGestureRecognizer *)recognizer {
|
|
623
|
+
if (recognizer.state != UIGestureRecognizerStateEnded) return;
|
|
624
|
+
PDFDocument *document = self.pdfView.document;
|
|
625
|
+
if (!document) return;
|
|
626
|
+
|
|
627
|
+
CGPoint viewPoint = [recognizer locationInView:self.pdfView];
|
|
628
|
+
PDFPage *page = [self.pdfView pageForPoint:viewPoint nearest:YES];
|
|
629
|
+
if (!page) return;
|
|
630
|
+
CGPoint pagePoint = [self.pdfView convertPoint:viewPoint toPage:page];
|
|
631
|
+
PDFSelection *selection = [page selectionForWordAtPoint:pagePoint];
|
|
632
|
+
self.pdfView.currentSelection = selection;
|
|
633
|
+
[self emitCurrentSelectionIfNeeded];
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
- (BOOL)selectionContainsViewPoint:(CGPoint)viewPoint {
|
|
637
|
+
PDFSelection *selection = self.pdfView.currentSelection;
|
|
638
|
+
if (!selection) return NO;
|
|
639
|
+
|
|
640
|
+
NSArray<PDFSelection *> *lines = selection.selectionsByLine;
|
|
641
|
+
if (lines.count == 0) lines = @[selection];
|
|
642
|
+
for (PDFPage *page in selection.pages) {
|
|
643
|
+
for (PDFSelection *line in lines) {
|
|
644
|
+
CGRect pageRect = [line boundsForPage:page];
|
|
645
|
+
if (CGRectIsNull(pageRect) || CGRectIsEmpty(pageRect)) continue;
|
|
646
|
+
CGRect viewRect = [self.pdfView convertRect:pageRect fromPage:page];
|
|
647
|
+
if (CGRectContainsPoint(CGRectInset(viewRect, -6, -6), viewPoint)) return YES;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return NO;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
|
|
654
|
+
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
|
|
655
|
+
return YES;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
@end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#import <React/RCTViewManager.h>
|
|
2
|
+
|
|
3
|
+
#import "PapyrusPdfDocumentView.h"
|
|
4
|
+
|
|
5
|
+
@interface PapyrusPdfDocumentViewManager : RCTViewManager
|
|
6
|
+
@end
|
|
7
|
+
|
|
8
|
+
@implementation PapyrusPdfDocumentViewManager
|
|
9
|
+
|
|
10
|
+
RCT_EXPORT_MODULE(PapyrusPdfDocumentView)
|
|
11
|
+
RCT_EXPORT_VIEW_PROPERTY(engineId, NSString)
|
|
12
|
+
RCT_EXPORT_VIEW_PROPERTY(pageTheme, NSString)
|
|
13
|
+
RCT_EXPORT_VIEW_PROPERTY(zoom, CGFloat)
|
|
14
|
+
RCT_EXPORT_VIEW_PROPERTY(currentPage, NSInteger)
|
|
15
|
+
RCT_EXPORT_VIEW_PROPERTY(viewMode, NSString)
|
|
16
|
+
RCT_EXPORT_VIEW_PROPERTY(selectionActive, BOOL)
|
|
17
|
+
RCT_EXPORT_VIEW_PROPERTY(onPageChange, RCTBubblingEventBlock)
|
|
18
|
+
RCT_EXPORT_VIEW_PROPERTY(onZoomChange, RCTBubblingEventBlock)
|
|
19
|
+
RCT_EXPORT_VIEW_PROPERTY(onVisiblePagesChange, RCTBubblingEventBlock)
|
|
20
|
+
RCT_EXPORT_VIEW_PROPERTY(onTextSelected, RCTBubblingEventBlock)
|
|
21
|
+
RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock)
|
|
22
|
+
RCT_EXPORT_VIEW_PROPERTY(onTap, RCTBubblingEventBlock)
|
|
23
|
+
|
|
24
|
+
+ (BOOL)requiresMainQueueSetup {
|
|
25
|
+
return YES;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
- (UIView *)view {
|
|
29
|
+
return [[PapyrusPdfDocumentView alloc] initWithFrame:CGRectZero];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@end
|