@papyrus-sdk/engine-native 0.2.11 → 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +42 -42
- package/android/src/main/AndroidManifest.xml +1 -1
- package/android/src/main/cpp/CMakeLists.txt +45 -45
- package/android/src/main/cpp/papyrus_outline.cpp +173 -173
- package/android/src/main/cpp/papyrus_outline_loader.cpp +125 -125
- package/android/src/main/cpp/papyrus_outline_loader.h +48 -48
- package/android/src/main/java/com/papyrus/engine/PapyrusEngineStore.java +71 -71
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +520 -516
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.kt +527 -527
- package/android/src/main/java/com/papyrus/engine/PapyrusOutline.java +34 -34
- package/android/src/main/java/com/papyrus/engine/PapyrusOutlineItem.java +13 -13
- package/android/src/main/java/com/papyrus/engine/PapyrusPackage.java +24 -24
- package/android/src/main/java/com/papyrus/engine/PapyrusPageView.java +197 -128
- package/android/src/main/java/com/papyrus/engine/PapyrusPageViewManager.java +22 -16
- package/android/src/main/java/com/papyrus/engine/PapyrusPageViewModule.kt +15 -12
- package/android/src/main/java/com/papyrus/engine/PapyrusTextHit.java +15 -15
- package/android/src/main/java/com/papyrus/engine/PapyrusTextSearch.java +20 -20
- package/android/src/main/java/com/papyrus/engine/PapyrusTextSelect.java +20 -20
- package/android/src/main/java/com/papyrus/engine/PapyrusTextSelection.java +11 -11
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +32 -44
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +30 -41
- package/dist/index.mjs.map +1 -1
- package/ios/PapyrusNativeEngine.m +447 -447
- package/ios/PapyrusNativeEngine.podspec +15 -15
- package/ios/PapyrusPageView.h +21 -19
- package/ios/PapyrusPageView.m +194 -63
- package/ios/PapyrusPageViewManager.m +20 -19
- package/package.json +14 -10
- package/react-native.config.js +10 -10
- package/LICENSE +0 -21
- package/android/src/main/cpp/papyrus_outline_loader_test.cpp +0 -240
- package/android/src/test/java/com/papyrus/engine/PapyrusOutlineTest.java +0 -40
- package/android/src/test/java/com/papyrus/engine/PapyrusPageViewTest.java +0 -30
|
@@ -1,447 +1,447 @@
|
|
|
1
|
-
#import <Foundation/Foundation.h>
|
|
2
|
-
#import <React/RCTBridge.h>
|
|
3
|
-
#import <React/RCTBridgeModule.h>
|
|
4
|
-
#import <React/RCTUIManager.h>
|
|
5
|
-
#import <PDFKit/PDFKit.h>
|
|
6
|
-
|
|
7
|
-
#import "PapyrusEngineStore.h"
|
|
8
|
-
#import "PapyrusPageView.h"
|
|
9
|
-
|
|
10
|
-
NS_ASSUME_NONNULL_BEGIN
|
|
11
|
-
|
|
12
|
-
@interface PapyrusNativeEngine : NSObject <RCTBridgeModule>
|
|
13
|
-
@property (nonatomic, weak) RCTBridge *bridge;
|
|
14
|
-
@end
|
|
15
|
-
|
|
16
|
-
NS_ASSUME_NONNULL_END
|
|
17
|
-
|
|
18
|
-
@implementation PapyrusNativeEngine
|
|
19
|
-
|
|
20
|
-
RCT_EXPORT_MODULE(PapyrusNativeEngine)
|
|
21
|
-
|
|
22
|
-
static NSMutableDictionary<NSString *, NSNumber *> *PapyrusPageViewTags;
|
|
23
|
-
|
|
24
|
-
static NSString *PapyrusViewKeyFor(NSString *engineId, NSInteger pageIndex) {
|
|
25
|
-
return [NSString stringWithFormat:@"%@:%ld", engineId ?: @"", (long)pageIndex];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
static void PapyrusEnsureViewTagStore(void) {
|
|
29
|
-
static dispatch_once_t onceToken;
|
|
30
|
-
dispatch_once(&onceToken, ^{
|
|
31
|
-
PapyrusPageViewTags = [NSMutableDictionary dictionary];
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
static void PapyrusSyncOnMain(void (^block)(void)) {
|
|
36
|
-
if ([NSThread isMainThread]) {
|
|
37
|
-
block();
|
|
38
|
-
} else {
|
|
39
|
-
dispatch_sync(dispatch_get_main_queue(), block);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
static NSArray<NSDictionary *> *PapyrusBuildOutlineItems(PDFOutline *outline, PDFDocument *document) {
|
|
44
|
-
if (!outline || !document) return @[];
|
|
45
|
-
|
|
46
|
-
NSMutableArray<NSDictionary *> *items = [NSMutableArray array];
|
|
47
|
-
NSInteger count = outline.numberOfChildren;
|
|
48
|
-
for (NSInteger i = 0; i < count; i++) {
|
|
49
|
-
PDFOutline *child = [outline childAtIndex:i];
|
|
50
|
-
if (!child) continue;
|
|
51
|
-
|
|
52
|
-
NSString *title = child.label ?: @"";
|
|
53
|
-
NSInteger pageIndex = -1;
|
|
54
|
-
PDFDestination *dest = child.destination;
|
|
55
|
-
if (!dest && [child.action isKindOfClass:[PDFActionGoTo class]]) {
|
|
56
|
-
dest = ((PDFActionGoTo *)child.action).destination;
|
|
57
|
-
}
|
|
58
|
-
if (dest.page) {
|
|
59
|
-
pageIndex = [document indexForPage:dest.page];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
NSArray<NSDictionary *> *children = PapyrusBuildOutlineItems(child, document);
|
|
63
|
-
NSMutableDictionary *item = [NSMutableDictionary dictionaryWithCapacity:3];
|
|
64
|
-
item[@"title"] = title;
|
|
65
|
-
item[@"pageIndex"] = @(pageIndex);
|
|
66
|
-
if (children.count > 0) {
|
|
67
|
-
item[@"children"] = children;
|
|
68
|
-
}
|
|
69
|
-
[items addObject:item];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return items;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
+ (BOOL)requiresMainQueueSetup {
|
|
76
|
-
return YES;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(createEngine) {
|
|
80
|
-
return [[PapyrusEngineStore shared] createEngine];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
RCT_EXPORT_METHOD(destroyEngine:(NSString *)engineId) {
|
|
84
|
-
[[PapyrusEngineStore shared] destroyEngine:engineId];
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
RCT_EXPORT_METHOD(load:(NSString *)engineId
|
|
88
|
-
source:(NSDictionary *)source
|
|
89
|
-
resolver:(RCTPromiseResolveBlock)resolve
|
|
90
|
-
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
91
|
-
NSString *uri = source[@"uri"];
|
|
92
|
-
id dataObject = source[@"data"];
|
|
93
|
-
|
|
94
|
-
void (^loadFromData)(NSData *) = ^(NSData *data) {
|
|
95
|
-
PDFDocument *document = [[PDFDocument alloc] initWithData:data];
|
|
96
|
-
if (!document) {
|
|
97
|
-
reject(@"papyrus_load_failed", @"Failed to open PDF document", nil);
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
[[PapyrusEngineStore shared] setDocument:document forEngine:engineId];
|
|
101
|
-
resolve(@{@"pageCount": @(document.pageCount)});
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
if (uri && [uri isKindOfClass:[NSString class]]) {
|
|
105
|
-
NSURL *url = [NSURL URLWithString:uri];
|
|
106
|
-
if (!url) {
|
|
107
|
-
reject(@"papyrus_invalid_uri", @"Invalid PDF URI", nil);
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if ([uri hasPrefix:@"http://"] || [uri hasPrefix:@"https://"]) {
|
|
112
|
-
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
|
|
113
|
-
if (error || !data) {
|
|
114
|
-
reject(@"papyrus_download_failed", @"Failed to download PDF", error);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
loadFromData(data);
|
|
118
|
-
}];
|
|
119
|
-
[task resume];
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
NSData *data = [NSData dataWithContentsOfURL:url];
|
|
124
|
-
if (!data) {
|
|
125
|
-
reject(@"papyrus_read_failed", @"Failed to read PDF data from URI", nil);
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
loadFromData(data);
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if ([dataObject isKindOfClass:[NSData class]]) {
|
|
133
|
-
loadFromData((NSData *)dataObject);
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if ([dataObject isKindOfClass:[NSArray class]]) {
|
|
138
|
-
NSArray *array = (NSArray *)dataObject;
|
|
139
|
-
NSUInteger length = array.count;
|
|
140
|
-
NSMutableData *data = [NSMutableData dataWithLength:length];
|
|
141
|
-
uint8_t *bytes = (uint8_t *)data.mutableBytes;
|
|
142
|
-
for (NSUInteger i = 0; i < length; i++) {
|
|
143
|
-
bytes[i] = (uint8_t)[array[i] intValue];
|
|
144
|
-
}
|
|
145
|
-
loadFromData(data);
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
reject(@"papyrus_invalid_source", @"Unsupported PDF source", nil);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getPageCount:(NSString *)engineId) {
|
|
153
|
-
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
154
|
-
if (!document) return @(0);
|
|
155
|
-
return @(document.pageCount);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
RCT_EXPORT_METHOD(renderPage:(NSString *)engineId
|
|
159
|
-
pageIndex:(NSInteger)pageIndex
|
|
160
|
-
target:(nonnull NSNumber *)target
|
|
161
|
-
scale:(CGFloat)scale
|
|
162
|
-
zoom:(CGFloat)zoom
|
|
163
|
-
rotation:(NSInteger)rotation) {
|
|
164
|
-
dispatch_async(dispatch_get_main_queue(), ^{
|
|
165
|
-
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
166
|
-
UIView *view = [self.bridge.uiManager viewForReactTag:target];
|
|
167
|
-
if ([view isKindOfClass:[PapyrusPageView class]]) {
|
|
168
|
-
PapyrusPageView *pageView = (PapyrusPageView *)view;
|
|
169
|
-
PapyrusEnsureViewTagStore();
|
|
170
|
-
NSString *key = PapyrusViewKeyFor(engineId, pageIndex);
|
|
171
|
-
@synchronized (PapyrusPageViewTags) {
|
|
172
|
-
PapyrusPageViewTags[key] = target;
|
|
173
|
-
}
|
|
174
|
-
[pageView renderWithDocument:document pageIndex:pageIndex scale:scale zoom:zoom rotation:rotation];
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
RCT_EXPORT_METHOD(renderTextLayer:(NSString *)engineId
|
|
180
|
-
pageIndex:(NSInteger)pageIndex
|
|
181
|
-
target:(nonnull NSNumber *)target
|
|
182
|
-
scale:(CGFloat)scale
|
|
183
|
-
zoom:(CGFloat)zoom
|
|
184
|
-
rotation:(NSInteger)rotation) {
|
|
185
|
-
#pragma unused(engineId, pageIndex, target, scale, zoom, rotation)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
RCT_EXPORT_METHOD(getTextContent:(NSString *)engineId
|
|
189
|
-
pageIndex:(NSInteger)pageIndex
|
|
190
|
-
resolver:(RCTPromiseResolveBlock)resolve
|
|
191
|
-
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
192
|
-
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
193
|
-
PDFPage *page = [document pageAtIndex:pageIndex];
|
|
194
|
-
if (!page) {
|
|
195
|
-
resolve(@[]);
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
CGRect pageBounds = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
200
|
-
PDFSelection *selection = [page selectionForRect:pageBounds];
|
|
201
|
-
if (!selection) {
|
|
202
|
-
resolve(@[]);
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
NSArray<PDFSelection *> *lines = [selection selectionsByLine];
|
|
207
|
-
if (!lines || lines.count == 0) {
|
|
208
|
-
lines = @[selection];
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
NSMutableArray *items = [NSMutableArray arrayWithCapacity:lines.count];
|
|
212
|
-
for (PDFSelection *line in lines) {
|
|
213
|
-
NSString *text = line.string ?: @"";
|
|
214
|
-
if (text.length == 0) continue;
|
|
215
|
-
CGRect bounds = [line boundsForPage:page];
|
|
216
|
-
NSDictionary *item = @{
|
|
217
|
-
@"str": text,
|
|
218
|
-
@"dir": @"ltr",
|
|
219
|
-
@"width": @(bounds.size.width),
|
|
220
|
-
@"height": @(bounds.size.height),
|
|
221
|
-
@"transform": @[@1, @0, @0, @1, @(bounds.origin.x), @(bounds.origin.y)],
|
|
222
|
-
@"fontName": @""
|
|
223
|
-
};
|
|
224
|
-
[items addObject:item];
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
resolve(items);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
RCT_EXPORT_METHOD(getPageDimensions:(NSString *)engineId
|
|
231
|
-
pageIndex:(NSInteger)pageIndex
|
|
232
|
-
resolver:(RCTPromiseResolveBlock)resolve
|
|
233
|
-
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
234
|
-
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
235
|
-
PDFPage *page = [document pageAtIndex:pageIndex];
|
|
236
|
-
if (!page) {
|
|
237
|
-
resolve(@{@"width": @(0), @"height": @(0)});
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
CGRect box = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
241
|
-
resolve(@{@"width": @(box.size.width), @"height": @(box.size.height)});
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
RCT_EXPORT_METHOD(getOutline:(NSString *)engineId
|
|
245
|
-
resolver:(RCTPromiseResolveBlock)resolve
|
|
246
|
-
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
247
|
-
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
248
|
-
if (!document) {
|
|
249
|
-
resolve(@[]);
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
PDFOutline *root = document.outlineRoot;
|
|
254
|
-
if (!root) {
|
|
255
|
-
resolve(@[]);
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
resolve(PapyrusBuildOutlineItems(root, document));
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
RCT_EXPORT_METHOD(getPageIndex:(NSString *)engineId
|
|
263
|
-
dest:(id)dest
|
|
264
|
-
resolver:(RCTPromiseResolveBlock)resolve
|
|
265
|
-
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
266
|
-
#pragma unused(engineId, dest)
|
|
267
|
-
resolve([NSNull null]);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
RCT_EXPORT_METHOD(searchText:(NSString *)engineId
|
|
271
|
-
query:(NSString *)query
|
|
272
|
-
resolver:(RCTPromiseResolveBlock)resolve
|
|
273
|
-
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
274
|
-
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
275
|
-
if (!document || !query || query.length < 2) {
|
|
276
|
-
resolve(@[]);
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
NSArray<PDFSelection *> *matches = [document findString:query withOptions:NSCaseInsensitiveSearch];
|
|
281
|
-
if (!matches || matches.count == 0) {
|
|
282
|
-
resolve(@[]);
|
|
283
|
-
return;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
NSMutableDictionary<NSNumber *, NSNumber *> *pageCounts = [NSMutableDictionary dictionary];
|
|
287
|
-
NSMutableArray *results = [NSMutableArray arrayWithCapacity:matches.count];
|
|
288
|
-
|
|
289
|
-
for (PDFSelection *selection in matches) {
|
|
290
|
-
NSArray<PDFPage *> *pages = selection.pages;
|
|
291
|
-
PDFPage *page = pages.firstObject;
|
|
292
|
-
if (!page) continue;
|
|
293
|
-
|
|
294
|
-
NSInteger pageIndex = [document indexForPage:page];
|
|
295
|
-
NSNumber *pageKey = @(pageIndex);
|
|
296
|
-
NSInteger matchIndex = [pageCounts[pageKey] integerValue];
|
|
297
|
-
pageCounts[pageKey] = @(matchIndex + 1);
|
|
298
|
-
|
|
299
|
-
CGRect pageBounds = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
300
|
-
CGFloat pageWidth = pageBounds.size.width;
|
|
301
|
-
CGFloat pageHeight = pageBounds.size.height;
|
|
302
|
-
CGFloat pageOriginX = pageBounds.origin.x;
|
|
303
|
-
CGFloat pageOriginY = pageBounds.origin.y;
|
|
304
|
-
|
|
305
|
-
NSArray<PDFSelection *> *lineSelections = [selection selectionsByLine];
|
|
306
|
-
if (!lineSelections || lineSelections.count == 0) {
|
|
307
|
-
lineSelections = @[selection];
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
NSMutableArray *rects = [NSMutableArray arrayWithCapacity:lineSelections.count];
|
|
311
|
-
for (PDFSelection *line in lineSelections) {
|
|
312
|
-
CGRect bounds = [line boundsForPage:page];
|
|
313
|
-
if (CGRectIsEmpty(bounds) || pageWidth <= 0 || pageHeight <= 0) continue;
|
|
314
|
-
|
|
315
|
-
CGFloat topLeftY = (pageOriginY + pageHeight) - (bounds.origin.y + bounds.size.height);
|
|
316
|
-
NSDictionary *rect = @{
|
|
317
|
-
@"x": @((bounds.origin.x - pageOriginX) / pageWidth),
|
|
318
|
-
@"y": @(topLeftY / pageHeight),
|
|
319
|
-
@"width": @(bounds.size.width / pageWidth),
|
|
320
|
-
@"height": @(bounds.size.height / pageHeight)
|
|
321
|
-
};
|
|
322
|
-
[rects addObject:rect];
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
NSDictionary *result = @{
|
|
326
|
-
@"pageIndex": @(pageIndex),
|
|
327
|
-
@"text": selection.string ?: @"",
|
|
328
|
-
@"matchIndex": @(matchIndex),
|
|
329
|
-
@"rects": rects
|
|
330
|
-
};
|
|
331
|
-
[results addObject:result];
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
resolve(results);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
RCT_EXPORT_METHOD(selectText:(NSString *)engineId
|
|
338
|
-
pageIndex:(NSInteger)pageIndex
|
|
339
|
-
x:(CGFloat)x
|
|
340
|
-
y:(CGFloat)y
|
|
341
|
-
width:(CGFloat)width
|
|
342
|
-
height:(CGFloat)height
|
|
343
|
-
resolver:(RCTPromiseResolveBlock)resolve
|
|
344
|
-
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
345
|
-
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
346
|
-
if (!document || pageIndex < 0 || width <= 0 || height <= 0) {
|
|
347
|
-
resolve([NSNull null]);
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
PDFPage *page = [document pageAtIndex:pageIndex];
|
|
352
|
-
if (!page) {
|
|
353
|
-
resolve([NSNull null]);
|
|
354
|
-
return;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
CGRect pageBounds = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
358
|
-
CGFloat pageWidth = pageBounds.size.width;
|
|
359
|
-
CGFloat pageHeight = pageBounds.size.height;
|
|
360
|
-
CGFloat pageOriginX = pageBounds.origin.x;
|
|
361
|
-
CGFloat pageOriginY = pageBounds.origin.y;
|
|
362
|
-
if (pageWidth <= 0 || pageHeight <= 0) {
|
|
363
|
-
resolve([NSNull null]);
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
CGRect selectionRect = CGRectZero;
|
|
368
|
-
__block PapyrusPageView *pageView = nil;
|
|
369
|
-
__block CGSize viewSize = CGSizeZero;
|
|
370
|
-
PapyrusEnsureViewTagStore();
|
|
371
|
-
NSString *key = PapyrusViewKeyFor(engineId, pageIndex);
|
|
372
|
-
NSNumber *viewTag = nil;
|
|
373
|
-
@synchronized (PapyrusPageViewTags) {
|
|
374
|
-
viewTag = PapyrusPageViewTags[key];
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
if (viewTag) {
|
|
378
|
-
PapyrusSyncOnMain(^{
|
|
379
|
-
UIView *view = [self.bridge.uiManager viewForReactTag:viewTag];
|
|
380
|
-
if ([view isKindOfClass:[PapyrusPageView class]]) {
|
|
381
|
-
pageView = (PapyrusPageView *)view;
|
|
382
|
-
viewSize = pageView.bounds.size;
|
|
383
|
-
}
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
if (pageView && viewSize.width > 0 && viewSize.height > 0) {
|
|
388
|
-
CGFloat rectX = x * viewSize.width;
|
|
389
|
-
CGFloat rectY = y * viewSize.height;
|
|
390
|
-
CGFloat rectW = width * viewSize.width;
|
|
391
|
-
CGFloat rectH = height * viewSize.height;
|
|
392
|
-
CGRect viewRect = CGRectMake(rectX, rectY, rectW, rectH);
|
|
393
|
-
selectionRect = [pageView convertRectToPage:viewRect page:page];
|
|
394
|
-
} else {
|
|
395
|
-
CGFloat rectX = pageOriginX + (x * pageWidth);
|
|
396
|
-
CGFloat rectW = width * pageWidth;
|
|
397
|
-
CGFloat rectH = height * pageHeight;
|
|
398
|
-
CGFloat rectTop = y * pageHeight;
|
|
399
|
-
CGFloat rectY = pageOriginY + (pageHeight - rectTop - rectH);
|
|
400
|
-
selectionRect = CGRectMake(rectX, rectY, rectW, rectH);
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
PDFSelection *selection = [page selectionForRect:selectionRect];
|
|
404
|
-
if (!selection) {
|
|
405
|
-
resolve([NSNull null]);
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
NSArray<PDFSelection *> *lineSelections = [selection selectionsByLine];
|
|
410
|
-
if (!lineSelections || lineSelections.count == 0) {
|
|
411
|
-
lineSelections = @[selection];
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
NSMutableArray *rects = [NSMutableArray arrayWithCapacity:lineSelections.count];
|
|
415
|
-
for (PDFSelection *line in lineSelections) {
|
|
416
|
-
CGRect bounds = [line boundsForPage:page];
|
|
417
|
-
if (CGRectIsEmpty(bounds)) continue;
|
|
418
|
-
|
|
419
|
-
NSDictionary *rect = nil;
|
|
420
|
-
if (pageView && viewSize.width > 0 && viewSize.height > 0) {
|
|
421
|
-
CGRect viewBounds = [pageView convertRectFromPage:bounds page:page];
|
|
422
|
-
rect = @{
|
|
423
|
-
@"x": @(viewBounds.origin.x / viewSize.width),
|
|
424
|
-
@"y": @(viewBounds.origin.y / viewSize.height),
|
|
425
|
-
@"width": @(viewBounds.size.width / viewSize.width),
|
|
426
|
-
@"height": @(viewBounds.size.height / viewSize.height)
|
|
427
|
-
};
|
|
428
|
-
} else {
|
|
429
|
-
CGFloat topLeftY = (pageOriginY + pageHeight) - (bounds.origin.y + bounds.size.height);
|
|
430
|
-
rect = @{
|
|
431
|
-
@"x": @((bounds.origin.x - pageOriginX) / pageWidth),
|
|
432
|
-
@"y": @(topLeftY / pageHeight),
|
|
433
|
-
@"width": @(bounds.size.width / pageWidth),
|
|
434
|
-
@"height": @(bounds.size.height / pageHeight)
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
[rects addObject:rect];
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
NSDictionary *result = @{
|
|
441
|
-
@"text": selection.string ?: @"",
|
|
442
|
-
@"rects": rects
|
|
443
|
-
};
|
|
444
|
-
resolve(result);
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
@end
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
#import <React/RCTBridge.h>
|
|
3
|
+
#import <React/RCTBridgeModule.h>
|
|
4
|
+
#import <React/RCTUIManager.h>
|
|
5
|
+
#import <PDFKit/PDFKit.h>
|
|
6
|
+
|
|
7
|
+
#import "PapyrusEngineStore.h"
|
|
8
|
+
#import "PapyrusPageView.h"
|
|
9
|
+
|
|
10
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
11
|
+
|
|
12
|
+
@interface PapyrusNativeEngine : NSObject <RCTBridgeModule>
|
|
13
|
+
@property (nonatomic, weak) RCTBridge *bridge;
|
|
14
|
+
@end
|
|
15
|
+
|
|
16
|
+
NS_ASSUME_NONNULL_END
|
|
17
|
+
|
|
18
|
+
@implementation PapyrusNativeEngine
|
|
19
|
+
|
|
20
|
+
RCT_EXPORT_MODULE(PapyrusNativeEngine)
|
|
21
|
+
|
|
22
|
+
static NSMutableDictionary<NSString *, NSNumber *> *PapyrusPageViewTags;
|
|
23
|
+
|
|
24
|
+
static NSString *PapyrusViewKeyFor(NSString *engineId, NSInteger pageIndex) {
|
|
25
|
+
return [NSString stringWithFormat:@"%@:%ld", engineId ?: @"", (long)pageIndex];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static void PapyrusEnsureViewTagStore(void) {
|
|
29
|
+
static dispatch_once_t onceToken;
|
|
30
|
+
dispatch_once(&onceToken, ^{
|
|
31
|
+
PapyrusPageViewTags = [NSMutableDictionary dictionary];
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static void PapyrusSyncOnMain(void (^block)(void)) {
|
|
36
|
+
if ([NSThread isMainThread]) {
|
|
37
|
+
block();
|
|
38
|
+
} else {
|
|
39
|
+
dispatch_sync(dispatch_get_main_queue(), block);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static NSArray<NSDictionary *> *PapyrusBuildOutlineItems(PDFOutline *outline, PDFDocument *document) {
|
|
44
|
+
if (!outline || !document) return @[];
|
|
45
|
+
|
|
46
|
+
NSMutableArray<NSDictionary *> *items = [NSMutableArray array];
|
|
47
|
+
NSInteger count = outline.numberOfChildren;
|
|
48
|
+
for (NSInteger i = 0; i < count; i++) {
|
|
49
|
+
PDFOutline *child = [outline childAtIndex:i];
|
|
50
|
+
if (!child) continue;
|
|
51
|
+
|
|
52
|
+
NSString *title = child.label ?: @"";
|
|
53
|
+
NSInteger pageIndex = -1;
|
|
54
|
+
PDFDestination *dest = child.destination;
|
|
55
|
+
if (!dest && [child.action isKindOfClass:[PDFActionGoTo class]]) {
|
|
56
|
+
dest = ((PDFActionGoTo *)child.action).destination;
|
|
57
|
+
}
|
|
58
|
+
if (dest.page) {
|
|
59
|
+
pageIndex = [document indexForPage:dest.page];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
NSArray<NSDictionary *> *children = PapyrusBuildOutlineItems(child, document);
|
|
63
|
+
NSMutableDictionary *item = [NSMutableDictionary dictionaryWithCapacity:3];
|
|
64
|
+
item[@"title"] = title;
|
|
65
|
+
item[@"pageIndex"] = @(pageIndex);
|
|
66
|
+
if (children.count > 0) {
|
|
67
|
+
item[@"children"] = children;
|
|
68
|
+
}
|
|
69
|
+
[items addObject:item];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return items;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
+ (BOOL)requiresMainQueueSetup {
|
|
76
|
+
return YES;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(createEngine) {
|
|
80
|
+
return [[PapyrusEngineStore shared] createEngine];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
RCT_EXPORT_METHOD(destroyEngine:(NSString *)engineId) {
|
|
84
|
+
[[PapyrusEngineStore shared] destroyEngine:engineId];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
RCT_EXPORT_METHOD(load:(NSString *)engineId
|
|
88
|
+
source:(NSDictionary *)source
|
|
89
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
90
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
91
|
+
NSString *uri = source[@"uri"];
|
|
92
|
+
id dataObject = source[@"data"];
|
|
93
|
+
|
|
94
|
+
void (^loadFromData)(NSData *) = ^(NSData *data) {
|
|
95
|
+
PDFDocument *document = [[PDFDocument alloc] initWithData:data];
|
|
96
|
+
if (!document) {
|
|
97
|
+
reject(@"papyrus_load_failed", @"Failed to open PDF document", nil);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
[[PapyrusEngineStore shared] setDocument:document forEngine:engineId];
|
|
101
|
+
resolve(@{@"pageCount": @(document.pageCount)});
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
if (uri && [uri isKindOfClass:[NSString class]]) {
|
|
105
|
+
NSURL *url = [NSURL URLWithString:uri];
|
|
106
|
+
if (!url) {
|
|
107
|
+
reject(@"papyrus_invalid_uri", @"Invalid PDF URI", nil);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if ([uri hasPrefix:@"http://"] || [uri hasPrefix:@"https://"]) {
|
|
112
|
+
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
|
|
113
|
+
if (error || !data) {
|
|
114
|
+
reject(@"papyrus_download_failed", @"Failed to download PDF", error);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
loadFromData(data);
|
|
118
|
+
}];
|
|
119
|
+
[task resume];
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
NSData *data = [NSData dataWithContentsOfURL:url];
|
|
124
|
+
if (!data) {
|
|
125
|
+
reject(@"papyrus_read_failed", @"Failed to read PDF data from URI", nil);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
loadFromData(data);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if ([dataObject isKindOfClass:[NSData class]]) {
|
|
133
|
+
loadFromData((NSData *)dataObject);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if ([dataObject isKindOfClass:[NSArray class]]) {
|
|
138
|
+
NSArray *array = (NSArray *)dataObject;
|
|
139
|
+
NSUInteger length = array.count;
|
|
140
|
+
NSMutableData *data = [NSMutableData dataWithLength:length];
|
|
141
|
+
uint8_t *bytes = (uint8_t *)data.mutableBytes;
|
|
142
|
+
for (NSUInteger i = 0; i < length; i++) {
|
|
143
|
+
bytes[i] = (uint8_t)[array[i] intValue];
|
|
144
|
+
}
|
|
145
|
+
loadFromData(data);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
reject(@"papyrus_invalid_source", @"Unsupported PDF source", nil);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getPageCount:(NSString *)engineId) {
|
|
153
|
+
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
154
|
+
if (!document) return @(0);
|
|
155
|
+
return @(document.pageCount);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
RCT_EXPORT_METHOD(renderPage:(NSString *)engineId
|
|
159
|
+
pageIndex:(NSInteger)pageIndex
|
|
160
|
+
target:(nonnull NSNumber *)target
|
|
161
|
+
scale:(CGFloat)scale
|
|
162
|
+
zoom:(CGFloat)zoom
|
|
163
|
+
rotation:(NSInteger)rotation) {
|
|
164
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
165
|
+
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
166
|
+
UIView *view = [self.bridge.uiManager viewForReactTag:target];
|
|
167
|
+
if ([view isKindOfClass:[PapyrusPageView class]]) {
|
|
168
|
+
PapyrusPageView *pageView = (PapyrusPageView *)view;
|
|
169
|
+
PapyrusEnsureViewTagStore();
|
|
170
|
+
NSString *key = PapyrusViewKeyFor(engineId, pageIndex);
|
|
171
|
+
@synchronized (PapyrusPageViewTags) {
|
|
172
|
+
PapyrusPageViewTags[key] = target;
|
|
173
|
+
}
|
|
174
|
+
[pageView renderWithDocument:document pageIndex:pageIndex scale:scale zoom:zoom rotation:rotation];
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
RCT_EXPORT_METHOD(renderTextLayer:(NSString *)engineId
|
|
180
|
+
pageIndex:(NSInteger)pageIndex
|
|
181
|
+
target:(nonnull NSNumber *)target
|
|
182
|
+
scale:(CGFloat)scale
|
|
183
|
+
zoom:(CGFloat)zoom
|
|
184
|
+
rotation:(NSInteger)rotation) {
|
|
185
|
+
#pragma unused(engineId, pageIndex, target, scale, zoom, rotation)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
RCT_EXPORT_METHOD(getTextContent:(NSString *)engineId
|
|
189
|
+
pageIndex:(NSInteger)pageIndex
|
|
190
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
191
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
192
|
+
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
193
|
+
PDFPage *page = [document pageAtIndex:pageIndex];
|
|
194
|
+
if (!page) {
|
|
195
|
+
resolve(@[]);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
CGRect pageBounds = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
200
|
+
PDFSelection *selection = [page selectionForRect:pageBounds];
|
|
201
|
+
if (!selection) {
|
|
202
|
+
resolve(@[]);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
NSArray<PDFSelection *> *lines = [selection selectionsByLine];
|
|
207
|
+
if (!lines || lines.count == 0) {
|
|
208
|
+
lines = @[selection];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
NSMutableArray *items = [NSMutableArray arrayWithCapacity:lines.count];
|
|
212
|
+
for (PDFSelection *line in lines) {
|
|
213
|
+
NSString *text = line.string ?: @"";
|
|
214
|
+
if (text.length == 0) continue;
|
|
215
|
+
CGRect bounds = [line boundsForPage:page];
|
|
216
|
+
NSDictionary *item = @{
|
|
217
|
+
@"str": text,
|
|
218
|
+
@"dir": @"ltr",
|
|
219
|
+
@"width": @(bounds.size.width),
|
|
220
|
+
@"height": @(bounds.size.height),
|
|
221
|
+
@"transform": @[@1, @0, @0, @1, @(bounds.origin.x), @(bounds.origin.y)],
|
|
222
|
+
@"fontName": @""
|
|
223
|
+
};
|
|
224
|
+
[items addObject:item];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
resolve(items);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
RCT_EXPORT_METHOD(getPageDimensions:(NSString *)engineId
|
|
231
|
+
pageIndex:(NSInteger)pageIndex
|
|
232
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
233
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
234
|
+
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
235
|
+
PDFPage *page = [document pageAtIndex:pageIndex];
|
|
236
|
+
if (!page) {
|
|
237
|
+
resolve(@{@"width": @(0), @"height": @(0)});
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
CGRect box = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
241
|
+
resolve(@{@"width": @(box.size.width), @"height": @(box.size.height)});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
RCT_EXPORT_METHOD(getOutline:(NSString *)engineId
|
|
245
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
246
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
247
|
+
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
248
|
+
if (!document) {
|
|
249
|
+
resolve(@[]);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
PDFOutline *root = document.outlineRoot;
|
|
254
|
+
if (!root) {
|
|
255
|
+
resolve(@[]);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
resolve(PapyrusBuildOutlineItems(root, document));
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
RCT_EXPORT_METHOD(getPageIndex:(NSString *)engineId
|
|
263
|
+
dest:(id)dest
|
|
264
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
265
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
266
|
+
#pragma unused(engineId, dest)
|
|
267
|
+
resolve([NSNull null]);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
RCT_EXPORT_METHOD(searchText:(NSString *)engineId
|
|
271
|
+
query:(NSString *)query
|
|
272
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
273
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
274
|
+
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
275
|
+
if (!document || !query || query.length < 2) {
|
|
276
|
+
resolve(@[]);
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
NSArray<PDFSelection *> *matches = [document findString:query withOptions:NSCaseInsensitiveSearch];
|
|
281
|
+
if (!matches || matches.count == 0) {
|
|
282
|
+
resolve(@[]);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
NSMutableDictionary<NSNumber *, NSNumber *> *pageCounts = [NSMutableDictionary dictionary];
|
|
287
|
+
NSMutableArray *results = [NSMutableArray arrayWithCapacity:matches.count];
|
|
288
|
+
|
|
289
|
+
for (PDFSelection *selection in matches) {
|
|
290
|
+
NSArray<PDFPage *> *pages = selection.pages;
|
|
291
|
+
PDFPage *page = pages.firstObject;
|
|
292
|
+
if (!page) continue;
|
|
293
|
+
|
|
294
|
+
NSInteger pageIndex = [document indexForPage:page];
|
|
295
|
+
NSNumber *pageKey = @(pageIndex);
|
|
296
|
+
NSInteger matchIndex = [pageCounts[pageKey] integerValue];
|
|
297
|
+
pageCounts[pageKey] = @(matchIndex + 1);
|
|
298
|
+
|
|
299
|
+
CGRect pageBounds = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
300
|
+
CGFloat pageWidth = pageBounds.size.width;
|
|
301
|
+
CGFloat pageHeight = pageBounds.size.height;
|
|
302
|
+
CGFloat pageOriginX = pageBounds.origin.x;
|
|
303
|
+
CGFloat pageOriginY = pageBounds.origin.y;
|
|
304
|
+
|
|
305
|
+
NSArray<PDFSelection *> *lineSelections = [selection selectionsByLine];
|
|
306
|
+
if (!lineSelections || lineSelections.count == 0) {
|
|
307
|
+
lineSelections = @[selection];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
NSMutableArray *rects = [NSMutableArray arrayWithCapacity:lineSelections.count];
|
|
311
|
+
for (PDFSelection *line in lineSelections) {
|
|
312
|
+
CGRect bounds = [line boundsForPage:page];
|
|
313
|
+
if (CGRectIsEmpty(bounds) || pageWidth <= 0 || pageHeight <= 0) continue;
|
|
314
|
+
|
|
315
|
+
CGFloat topLeftY = (pageOriginY + pageHeight) - (bounds.origin.y + bounds.size.height);
|
|
316
|
+
NSDictionary *rect = @{
|
|
317
|
+
@"x": @((bounds.origin.x - pageOriginX) / pageWidth),
|
|
318
|
+
@"y": @(topLeftY / pageHeight),
|
|
319
|
+
@"width": @(bounds.size.width / pageWidth),
|
|
320
|
+
@"height": @(bounds.size.height / pageHeight)
|
|
321
|
+
};
|
|
322
|
+
[rects addObject:rect];
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
NSDictionary *result = @{
|
|
326
|
+
@"pageIndex": @(pageIndex),
|
|
327
|
+
@"text": selection.string ?: @"",
|
|
328
|
+
@"matchIndex": @(matchIndex),
|
|
329
|
+
@"rects": rects
|
|
330
|
+
};
|
|
331
|
+
[results addObject:result];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
resolve(results);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
RCT_EXPORT_METHOD(selectText:(NSString *)engineId
|
|
338
|
+
pageIndex:(NSInteger)pageIndex
|
|
339
|
+
x:(CGFloat)x
|
|
340
|
+
y:(CGFloat)y
|
|
341
|
+
width:(CGFloat)width
|
|
342
|
+
height:(CGFloat)height
|
|
343
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
344
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
345
|
+
PDFDocument *document = [[PapyrusEngineStore shared] documentForEngine:engineId];
|
|
346
|
+
if (!document || pageIndex < 0 || width <= 0 || height <= 0) {
|
|
347
|
+
resolve([NSNull null]);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
PDFPage *page = [document pageAtIndex:pageIndex];
|
|
352
|
+
if (!page) {
|
|
353
|
+
resolve([NSNull null]);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
CGRect pageBounds = [page boundsForBox:kPDFDisplayBoxCropBox];
|
|
358
|
+
CGFloat pageWidth = pageBounds.size.width;
|
|
359
|
+
CGFloat pageHeight = pageBounds.size.height;
|
|
360
|
+
CGFloat pageOriginX = pageBounds.origin.x;
|
|
361
|
+
CGFloat pageOriginY = pageBounds.origin.y;
|
|
362
|
+
if (pageWidth <= 0 || pageHeight <= 0) {
|
|
363
|
+
resolve([NSNull null]);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
CGRect selectionRect = CGRectZero;
|
|
368
|
+
__block PapyrusPageView *pageView = nil;
|
|
369
|
+
__block CGSize viewSize = CGSizeZero;
|
|
370
|
+
PapyrusEnsureViewTagStore();
|
|
371
|
+
NSString *key = PapyrusViewKeyFor(engineId, pageIndex);
|
|
372
|
+
NSNumber *viewTag = nil;
|
|
373
|
+
@synchronized (PapyrusPageViewTags) {
|
|
374
|
+
viewTag = PapyrusPageViewTags[key];
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (viewTag) {
|
|
378
|
+
PapyrusSyncOnMain(^{
|
|
379
|
+
UIView *view = [self.bridge.uiManager viewForReactTag:viewTag];
|
|
380
|
+
if ([view isKindOfClass:[PapyrusPageView class]]) {
|
|
381
|
+
pageView = (PapyrusPageView *)view;
|
|
382
|
+
viewSize = pageView.bounds.size;
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (pageView && viewSize.width > 0 && viewSize.height > 0) {
|
|
388
|
+
CGFloat rectX = x * viewSize.width;
|
|
389
|
+
CGFloat rectY = y * viewSize.height;
|
|
390
|
+
CGFloat rectW = width * viewSize.width;
|
|
391
|
+
CGFloat rectH = height * viewSize.height;
|
|
392
|
+
CGRect viewRect = CGRectMake(rectX, rectY, rectW, rectH);
|
|
393
|
+
selectionRect = [pageView convertRectToPage:viewRect page:page];
|
|
394
|
+
} else {
|
|
395
|
+
CGFloat rectX = pageOriginX + (x * pageWidth);
|
|
396
|
+
CGFloat rectW = width * pageWidth;
|
|
397
|
+
CGFloat rectH = height * pageHeight;
|
|
398
|
+
CGFloat rectTop = y * pageHeight;
|
|
399
|
+
CGFloat rectY = pageOriginY + (pageHeight - rectTop - rectH);
|
|
400
|
+
selectionRect = CGRectMake(rectX, rectY, rectW, rectH);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
PDFSelection *selection = [page selectionForRect:selectionRect];
|
|
404
|
+
if (!selection) {
|
|
405
|
+
resolve([NSNull null]);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
NSArray<PDFSelection *> *lineSelections = [selection selectionsByLine];
|
|
410
|
+
if (!lineSelections || lineSelections.count == 0) {
|
|
411
|
+
lineSelections = @[selection];
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
NSMutableArray *rects = [NSMutableArray arrayWithCapacity:lineSelections.count];
|
|
415
|
+
for (PDFSelection *line in lineSelections) {
|
|
416
|
+
CGRect bounds = [line boundsForPage:page];
|
|
417
|
+
if (CGRectIsEmpty(bounds)) continue;
|
|
418
|
+
|
|
419
|
+
NSDictionary *rect = nil;
|
|
420
|
+
if (pageView && viewSize.width > 0 && viewSize.height > 0) {
|
|
421
|
+
CGRect viewBounds = [pageView convertRectFromPage:bounds page:page];
|
|
422
|
+
rect = @{
|
|
423
|
+
@"x": @(viewBounds.origin.x / viewSize.width),
|
|
424
|
+
@"y": @(viewBounds.origin.y / viewSize.height),
|
|
425
|
+
@"width": @(viewBounds.size.width / viewSize.width),
|
|
426
|
+
@"height": @(viewBounds.size.height / viewSize.height)
|
|
427
|
+
};
|
|
428
|
+
} else {
|
|
429
|
+
CGFloat topLeftY = (pageOriginY + pageHeight) - (bounds.origin.y + bounds.size.height);
|
|
430
|
+
rect = @{
|
|
431
|
+
@"x": @((bounds.origin.x - pageOriginX) / pageWidth),
|
|
432
|
+
@"y": @(topLeftY / pageHeight),
|
|
433
|
+
@"width": @(bounds.size.width / pageWidth),
|
|
434
|
+
@"height": @(bounds.size.height / pageHeight)
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
[rects addObject:rect];
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
NSDictionary *result = @{
|
|
441
|
+
@"text": selection.string ?: @"",
|
|
442
|
+
@"rects": rects
|
|
443
|
+
};
|
|
444
|
+
resolve(result);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
@end
|