@papyrus-sdk/engine-native 0.2.25 → 0.2.27
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/consumer-rules.pro +13 -1
- package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +14 -3
- package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +1 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusEngineStore.java +371 -12
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +77 -12
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.kt +4 -9
- package/android/src/main/java/com/papyrus/engine/PapyrusPageView.java +13 -4
- package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +10 -8
- package/android/src/main/java/com/papyrus/engine/PapyrusThumbnail.java +227 -0
- package/dist/index.d.mts +61 -1
- package/dist/index.d.ts +61 -1
- package/dist/index.js +46 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +43 -1
- package/dist/index.mjs.map +1 -1
- package/ios/PapyrusComicDocumentView.h +1 -0
- package/ios/PapyrusComicDocumentView.m +15 -0
- package/ios/PapyrusComicDocumentViewManager.m +1 -0
- package/package.json +9 -9
- package/android/src/main/cpp/papyrus_outline_loader_test.cpp +0 -240
- package/android/src/main/cpp/papyrus_text_word_test.cpp +0 -72
|
@@ -1 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# The engine reaches three private members of pdfium-android by reflection
|
|
2
|
+
# (the library exposes no public way to close a page or to hand its native
|
|
3
|
+
# document to our own JNI). Without these rules R8 renames them in a minified
|
|
4
|
+
# app and the lookups fail silently: pages would stay open until the document
|
|
5
|
+
# closes, and outline/text search through the native pointer would be off.
|
|
6
|
+
-keepclassmembers class com.shockwave.pdfium.PdfDocument {
|
|
7
|
+
long mNativeDocPtr;
|
|
8
|
+
java.util.Map mNativePagesPtr;
|
|
9
|
+
}
|
|
10
|
+
-keepclassmembers class com.shockwave.pdfium.PdfiumCore {
|
|
11
|
+
private native void nativeClosePage(long);
|
|
12
|
+
*** getPageText(...);
|
|
13
|
+
}
|
|
@@ -95,6 +95,11 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
95
95
|
emitZoom();
|
|
96
96
|
return true;
|
|
97
97
|
}
|
|
98
|
+
@Override public boolean onSingleTapConfirmed(MotionEvent event) {
|
|
99
|
+
if (scaleDetector.isInProgress() || pageCount <= 0) return false;
|
|
100
|
+
emitPageTap();
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
98
103
|
});
|
|
99
104
|
setLayoutMode(layoutMode);
|
|
100
105
|
applyTheme();
|
|
@@ -228,9 +233,9 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
228
233
|
|
|
229
234
|
@Override public boolean dispatchTouchEvent(android.view.MotionEvent event) {
|
|
230
235
|
scaleDetector.onTouchEvent(event);
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
236
|
+
// Feed pointer transitions too so a pinch cancels a pending single tap,
|
|
237
|
+
// while the detector still arbitrates a single tap against double-tap zoom.
|
|
238
|
+
tapDetector.onTouchEvent(event);
|
|
234
239
|
if (event.getActionMasked() == android.view.MotionEvent.ACTION_POINTER_UP && event.getPointerCount() > 1) {
|
|
235
240
|
int remaining = event.getActionIndex() == 0 ? 1 : 0;
|
|
236
241
|
lastPanX = event.getX(remaining); lastPanY = event.getY(remaining);
|
|
@@ -310,6 +315,12 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
310
315
|
payload.putDouble("zoom", zoom);
|
|
311
316
|
context.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onZoomChanged", payload);
|
|
312
317
|
}
|
|
318
|
+
private void emitPageTap() {
|
|
319
|
+
ReactContext context = (ReactContext) getContext();
|
|
320
|
+
com.facebook.react.bridge.WritableMap payload = Arguments.createMap();
|
|
321
|
+
payload.putInt("page", currentPage);
|
|
322
|
+
context.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onPageTap", payload);
|
|
323
|
+
}
|
|
313
324
|
private void emitError(String key, String message, String sourceEngineId, int sourceGeneration) {
|
|
314
325
|
synchronized (emittedErrors) {
|
|
315
326
|
if (!emittedErrors.add(key)) return;
|
|
@@ -16,6 +16,7 @@ public final class PapyrusComicDocumentViewManager extends SimpleViewManager<Pap
|
|
|
16
16
|
@Nullable @Override public Map<String, Object> getExportedCustomBubblingEventTypeConstants() {
|
|
17
17
|
return MapBuilder.<String, Object>builder()
|
|
18
18
|
.put("onPageChanged", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onPageChanged")))
|
|
19
|
+
.put("onPageTap", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onPageTap")))
|
|
19
20
|
.put("onZoomChanged", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onZoomChanged")))
|
|
20
21
|
.put("onError", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onError")))
|
|
21
22
|
.build();
|
|
@@ -8,6 +8,9 @@ import com.shockwave.pdfium.PdfiumCore;
|
|
|
8
8
|
|
|
9
9
|
import java.io.File;
|
|
10
10
|
import java.io.IOException;
|
|
11
|
+
import java.lang.reflect.Field;
|
|
12
|
+
import java.lang.reflect.Method;
|
|
13
|
+
import java.util.LinkedHashMap;
|
|
11
14
|
import java.util.Map;
|
|
12
15
|
import java.util.UUID;
|
|
13
16
|
import java.util.concurrent.ConcurrentHashMap;
|
|
@@ -20,9 +23,36 @@ final class PapyrusEngineStore {
|
|
|
20
23
|
PdfDocument document;
|
|
21
24
|
ParcelFileDescriptor fileDescriptor;
|
|
22
25
|
String sourcePath;
|
|
26
|
+
/** Pages this engine opened for drawing or text, least recently used first. */
|
|
27
|
+
final LinkedHashMap<Integer, Boolean> openPages = new LinkedHashMap<>(16, 0.75f, true);
|
|
28
|
+
/**
|
|
29
|
+
* Pages that are still open in pdfium because closing them failed (or is
|
|
30
|
+
* not possible on this pdfium build). Counted apart so the stats never
|
|
31
|
+
* claim memory that was not released.
|
|
32
|
+
*/
|
|
33
|
+
final java.util.Set<Integer> retainedPages = new java.util.HashSet<>();
|
|
34
|
+
/** True while the document was released on purpose and can be reopened from {@code sourcePath}. */
|
|
35
|
+
boolean suspended = false;
|
|
36
|
+
/**
|
|
37
|
+
* Changes when a different document is set or the document is closed for
|
|
38
|
+
* good, NOT when the same file is reopened (suspend/resume, recycling):
|
|
39
|
+
* views use it to tell "another document" from "same document, new
|
|
40
|
+
* pdfium handle".
|
|
41
|
+
*/
|
|
42
|
+
volatile int documentGeneration = 0;
|
|
43
|
+
/** Pages loaded since the pdfium document was (re)opened, and the native heap at that moment. */
|
|
44
|
+
int pagesLoadedSinceOpen = 0;
|
|
45
|
+
long nativeHeapAtOpen = 0;
|
|
46
|
+
/** Page sizes in points, so a layout never needs the page open again. */
|
|
47
|
+
final Map<Integer, int[]> pageSizes = new ConcurrentHashMap<>();
|
|
23
48
|
|
|
24
|
-
|
|
49
|
+
/** Approximate sizes (see {@link #layoutPageSizePoints}). */
|
|
50
|
+
final Map<Integer, int[]> layoutSizes = new ConcurrentHashMap<>();
|
|
51
|
+
final int densityDpi;
|
|
52
|
+
|
|
53
|
+
EngineState(PdfiumCore pdfium, int densityDpi) {
|
|
25
54
|
this.pdfium = pdfium;
|
|
55
|
+
this.densityDpi = densityDpi > 0 ? densityDpi : 160;
|
|
26
56
|
}
|
|
27
57
|
}
|
|
28
58
|
|
|
@@ -30,7 +60,7 @@ final class PapyrusEngineStore {
|
|
|
30
60
|
|
|
31
61
|
static String createEngine(Context context) {
|
|
32
62
|
String engineId = UUID.randomUUID().toString();
|
|
33
|
-
ENGINES.put(engineId, new EngineState(new PdfiumCore(context)));
|
|
63
|
+
ENGINES.put(engineId, new EngineState(new PdfiumCore(context), context.getResources().getDisplayMetrics().densityDpi));
|
|
34
64
|
return engineId;
|
|
35
65
|
}
|
|
36
66
|
|
|
@@ -51,34 +81,363 @@ final class PapyrusEngineStore {
|
|
|
51
81
|
return count;
|
|
52
82
|
}
|
|
53
83
|
|
|
84
|
+
/** Every page still open in pdfium, including the ones that could not be closed. */
|
|
85
|
+
static int openPageCount() {
|
|
86
|
+
int count = 0;
|
|
87
|
+
for (EngineState state : ENGINES.values()) {
|
|
88
|
+
synchronized (state.pdfiumLock) {
|
|
89
|
+
count += state.openPages.size() + state.retainedPages.size();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return count;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Pages left open because closing failed: above zero means memory is being held. */
|
|
96
|
+
static int retainedPageCount() {
|
|
97
|
+
int count = 0;
|
|
98
|
+
for (EngineState state : ENGINES.values()) {
|
|
99
|
+
synchronized (state.pdfiumLock) {
|
|
100
|
+
count += state.retainedPages.size();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return count;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static int suspendedDocumentCount() {
|
|
107
|
+
int count = 0;
|
|
108
|
+
for (EngineState state : ENGINES.values()) {
|
|
109
|
+
if (state.suspended) count++;
|
|
110
|
+
}
|
|
111
|
+
return count;
|
|
112
|
+
}
|
|
113
|
+
|
|
54
114
|
static void destroyEngine(String engineId) {
|
|
55
115
|
EngineState state = ENGINES.remove(engineId);
|
|
56
116
|
if (state == null) return;
|
|
117
|
+
closeDocument(state);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Closes the document and its file under the engine lock: a render in progress finishes first. */
|
|
121
|
+
static void closeDocument(EngineState state) {
|
|
122
|
+
synchronized (state.pdfiumLock) {
|
|
123
|
+
releaseDocumentLocked(state);
|
|
124
|
+
state.pageSizes.clear();
|
|
125
|
+
state.layoutSizes.clear();
|
|
126
|
+
state.sourcePath = null;
|
|
127
|
+
state.suspended = false;
|
|
128
|
+
state.documentGeneration++;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Documents whose close threw: their native memory may still be held. */
|
|
133
|
+
private static final java.util.concurrent.atomic.AtomicInteger FAILED_DOCUMENT_CLOSES = new java.util.concurrent.atomic.AtomicInteger();
|
|
134
|
+
/** Pages that were open in a document whose close threw. */
|
|
135
|
+
private static final java.util.concurrent.atomic.AtomicInteger LEAKED_PAGES = new java.util.concurrent.atomic.AtomicInteger();
|
|
136
|
+
|
|
137
|
+
static int failedDocumentCloseCount() {
|
|
138
|
+
return FAILED_DOCUMENT_CLOSES.get();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
static int leakedPageCount() {
|
|
142
|
+
return LEAKED_PAGES.get();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Closes the pdfium document and its file; pdfium frees every page with it.
|
|
147
|
+
* The page counters are cleared only after the close succeeded. If it
|
|
148
|
+
* throws, the document is dropped anyway (it cannot be trusted any more),
|
|
149
|
+
* but its pages are added to the leak counters instead of being reported
|
|
150
|
+
* as released. Returns whether everything was released.
|
|
151
|
+
*/
|
|
152
|
+
private static boolean releaseDocumentLocked(EngineState state) {
|
|
153
|
+
boolean released = true;
|
|
57
154
|
if (state.document != null) {
|
|
58
|
-
|
|
155
|
+
try {
|
|
156
|
+
state.pdfium.closeDocument(state.document);
|
|
157
|
+
} catch (Throwable error) {
|
|
158
|
+
released = false;
|
|
159
|
+
FAILED_DOCUMENT_CLOSES.incrementAndGet();
|
|
160
|
+
LEAKED_PAGES.addAndGet(state.openPages.size() + state.retainedPages.size());
|
|
161
|
+
android.util.Log.w("PapyrusEngine", "closeDocument failed; native memory may be held", error);
|
|
162
|
+
}
|
|
59
163
|
state.document = null;
|
|
60
164
|
}
|
|
165
|
+
// Reached with the document gone either way: these now describe nothing.
|
|
166
|
+
state.openPages.clear();
|
|
167
|
+
state.retainedPages.clear();
|
|
61
168
|
if (state.fileDescriptor != null) {
|
|
62
169
|
try {
|
|
170
|
+
// pdfium-android closes the descriptor with the document; closing it
|
|
171
|
+
// again is harmless and covers a close that threw half-way.
|
|
63
172
|
state.fileDescriptor.close();
|
|
64
|
-
} catch (IOException
|
|
173
|
+
} catch (IOException error) {
|
|
174
|
+
android.util.Log.w("PapyrusEngine", "closing the document file failed", error);
|
|
65
175
|
}
|
|
66
176
|
state.fileDescriptor = null;
|
|
67
177
|
}
|
|
178
|
+
return released;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private static void markOpenedLocked(EngineState state) {
|
|
182
|
+
state.pagesLoadedSinceOpen = 0;
|
|
183
|
+
state.nativeHeapAtOpen = android.os.Debug.getNativeHeapAllocatedSize();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* pdfium keeps what every page it has loaded parsed (and the image streams
|
|
188
|
+
* it decoded) in a document-level cache with no limit and no public way to
|
|
189
|
+
* trim it: closing pages does not shrink it. Reading 300 scanned pages kept
|
|
190
|
+
* 385 MB with six pages open. The only release is closing the document, so
|
|
191
|
+
* a long session reopens it from its file when the cache has grown past
|
|
192
|
+
* these limits (2–30 ms, between two page loads, under the engine lock).
|
|
193
|
+
*/
|
|
194
|
+
static final int RECYCLE_MIN_PAGES = 24;
|
|
195
|
+
static final int RECYCLE_AFTER_PAGES = 400;
|
|
196
|
+
static final long RECYCLE_AFTER_BYTES = 96L * 1024 * 1024;
|
|
197
|
+
private static final java.util.concurrent.atomic.AtomicInteger DOCUMENT_RECYCLES = new java.util.concurrent.atomic.AtomicInteger();
|
|
198
|
+
|
|
199
|
+
static int documentRecycleCount() {
|
|
200
|
+
return DOCUMENT_RECYCLES.get();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Call with {@code state.pdfiumLock} held and no page of this document in use. */
|
|
204
|
+
private static void recycleIfGrownLocked(EngineState state) {
|
|
205
|
+
if (state.document == null || state.sourcePath == null) return;
|
|
206
|
+
if (state.pagesLoadedSinceOpen < RECYCLE_MIN_PAGES) return;
|
|
207
|
+
long grown = android.os.Debug.getNativeHeapAllocatedSize() - state.nativeHeapAtOpen;
|
|
208
|
+
if (state.pagesLoadedSinceOpen < RECYCLE_AFTER_PAGES && grown < RECYCLE_AFTER_BYTES) return;
|
|
209
|
+
releaseDocumentLocked(state);
|
|
210
|
+
try {
|
|
211
|
+
ParcelFileDescriptor fd = ParcelFileDescriptor.open(new File(state.sourcePath), ParcelFileDescriptor.MODE_READ_ONLY);
|
|
212
|
+
try {
|
|
213
|
+
state.document = state.pdfium.newDocument(fd);
|
|
214
|
+
state.fileDescriptor = fd;
|
|
215
|
+
} catch (IOException | RuntimeException error) {
|
|
216
|
+
fd.close();
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
markOpenedLocked(state);
|
|
220
|
+
DOCUMENT_RECYCLES.incrementAndGet();
|
|
221
|
+
} catch (Throwable error) {
|
|
222
|
+
// The file is gone or unreadable: leave the engine as "suspended" so a
|
|
223
|
+
// later resume can try again, instead of pretending a document is open.
|
|
224
|
+
state.suspended = true;
|
|
225
|
+
android.util.Log.w("PapyrusEngine", "could not reopen the document while recycling", error);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Releases the document while the reader is not on screen, keeping what is
|
|
231
|
+
* needed to open it again: the file path and the measured page sizes. The
|
|
232
|
+
* reading position, progress and annotations live outside the engine and
|
|
233
|
+
* are not touched.
|
|
234
|
+
*/
|
|
235
|
+
static boolean suspendDocument(EngineState state) {
|
|
236
|
+
synchronized (state.pdfiumLock) {
|
|
237
|
+
if (state.document == null || state.sourcePath == null) return false;
|
|
238
|
+
boolean released = releaseDocumentLocked(state);
|
|
239
|
+
state.suspended = true;
|
|
240
|
+
return released;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Reopens a suspended document. Returns false when there is nothing to reopen. */
|
|
245
|
+
static boolean resumeDocument(EngineState state) throws IOException {
|
|
246
|
+
synchronized (state.pdfiumLock) {
|
|
247
|
+
if (state.document != null) return true;
|
|
248
|
+
if (!state.suspended || state.sourcePath == null) return false;
|
|
249
|
+
ParcelFileDescriptor fd = ParcelFileDescriptor.open(new File(state.sourcePath), ParcelFileDescriptor.MODE_READ_ONLY);
|
|
250
|
+
try {
|
|
251
|
+
state.document = state.pdfium.newDocument(fd);
|
|
252
|
+
state.fileDescriptor = fd;
|
|
253
|
+
state.suspended = false;
|
|
254
|
+
markOpenedLocked(state);
|
|
255
|
+
return true;
|
|
256
|
+
} catch (IOException | RuntimeException error) {
|
|
257
|
+
try {
|
|
258
|
+
fd.close();
|
|
259
|
+
} catch (IOException ignored) {
|
|
260
|
+
}
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
68
264
|
}
|
|
69
265
|
|
|
70
266
|
static void setDocument(EngineState state, PdfDocument document, ParcelFileDescriptor fd, String sourcePath) {
|
|
71
|
-
|
|
72
|
-
|
|
267
|
+
synchronized (state.pdfiumLock) {
|
|
268
|
+
closeDocument(state);
|
|
269
|
+
state.document = document;
|
|
270
|
+
state.fileDescriptor = fd;
|
|
271
|
+
state.sourcePath = sourcePath;
|
|
272
|
+
markOpenedLocked(state);
|
|
73
273
|
}
|
|
74
|
-
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* A page opened in pdfium stays in native memory until the document closes:
|
|
278
|
+
* the library has no public way to close one. A reader that opened every
|
|
279
|
+
* page to lay the document out (or to search it) kept all of them, hundreds
|
|
280
|
+
* of megabytes for a long book. Pages are now opened through
|
|
281
|
+
* {@link #acquirePage} and {@link #pageSizePoints}, which keep at most
|
|
282
|
+
* {@link #MAX_OPEN_PAGES} of them.
|
|
283
|
+
*/
|
|
284
|
+
static final int MAX_OPEN_PAGES = 6;
|
|
285
|
+
|
|
286
|
+
private static volatile boolean closeLookupDone = false;
|
|
287
|
+
private static Field nativePagesField;
|
|
288
|
+
private static Method nativeClosePageMethod;
|
|
289
|
+
|
|
290
|
+
private static void lookupClose() {
|
|
291
|
+
if (closeLookupDone) return;
|
|
292
|
+
synchronized (PapyrusEngineStore.class) {
|
|
293
|
+
if (closeLookupDone) return;
|
|
75
294
|
try {
|
|
76
|
-
|
|
77
|
-
|
|
295
|
+
Field field = PdfDocument.class.getDeclaredField("mNativePagesPtr");
|
|
296
|
+
field.setAccessible(true);
|
|
297
|
+
Method method = PdfiumCore.class.getDeclaredMethod("nativeClosePage", long.class);
|
|
298
|
+
method.setAccessible(true);
|
|
299
|
+
nativePagesField = field;
|
|
300
|
+
nativeClosePageMethod = method;
|
|
301
|
+
} catch (Throwable ignored) {
|
|
302
|
+
// A pdfium build without these names: pages stay open until the
|
|
303
|
+
// document closes, as before.
|
|
304
|
+
}
|
|
305
|
+
closeLookupDone = true;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
static boolean canClosePages() {
|
|
310
|
+
lookupClose();
|
|
311
|
+
return nativeClosePageMethod != null;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Call with {@code state.pdfiumLock} held. Returns whether the page is
|
|
316
|
+
* really closed in pdfium. A page that could not be closed moves to
|
|
317
|
+
* {@code retainedPages}: it is still counted, and never reported as freed.
|
|
318
|
+
*/
|
|
319
|
+
@SuppressWarnings("unchecked")
|
|
320
|
+
private static boolean closePageLocked(EngineState state, int pageIndex) {
|
|
321
|
+
if (state.document == null) {
|
|
322
|
+
state.openPages.remove(pageIndex);
|
|
323
|
+
state.retainedPages.remove(pageIndex);
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
boolean closed = false;
|
|
327
|
+
lookupClose();
|
|
328
|
+
if (nativeClosePageMethod != null) {
|
|
329
|
+
try {
|
|
330
|
+
Map<Integer, Long> pages = (Map<Integer, Long>) nativePagesField.get(state.document);
|
|
331
|
+
Long pointer = pages == null ? null : pages.get(pageIndex);
|
|
332
|
+
if (pointer == null) {
|
|
333
|
+
// pdfium does not have it open: nothing to release.
|
|
334
|
+
closed = pages != null;
|
|
335
|
+
} else {
|
|
336
|
+
nativeClosePageMethod.invoke(state.pdfium, pointer.longValue());
|
|
337
|
+
// Forget the pointer only after pdfium released it.
|
|
338
|
+
pages.remove(pageIndex);
|
|
339
|
+
closed = true;
|
|
340
|
+
}
|
|
341
|
+
} catch (Throwable ignored) {
|
|
342
|
+
closed = false;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
state.openPages.remove(pageIndex);
|
|
346
|
+
if (closed) state.retainedPages.remove(pageIndex);
|
|
347
|
+
else state.retainedPages.add(pageIndex);
|
|
348
|
+
return closed;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Opens a page for drawing or for reading its text, and closes the least
|
|
353
|
+
* recently used ones beyond the limit. Hold {@code state.pdfiumLock} from
|
|
354
|
+
* this call until the page has been used, and read {@code state.document}
|
|
355
|
+
* AFTER it: a long session may have reopened the document here.
|
|
356
|
+
*/
|
|
357
|
+
static void acquirePage(EngineState state, int pageIndex) {
|
|
358
|
+
synchronized (state.pdfiumLock) {
|
|
359
|
+
if (state.document == null) return;
|
|
360
|
+
if (!state.openPages.containsKey(pageIndex)) recycleIfGrownLocked(state);
|
|
361
|
+
if (state.document == null) return;
|
|
362
|
+
boolean wasOpen = state.document.hasPage(pageIndex);
|
|
363
|
+
try {
|
|
364
|
+
state.pdfium.openPage(state.document, pageIndex);
|
|
365
|
+
} catch (Throwable ignored) {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (!wasOpen) state.pagesLoadedSinceOpen++;
|
|
369
|
+
state.retainedPages.remove(pageIndex);
|
|
370
|
+
state.openPages.put(pageIndex, Boolean.TRUE);
|
|
371
|
+
while (state.openPages.size() > MAX_OPEN_PAGES) {
|
|
372
|
+
Integer candidate = null;
|
|
373
|
+
for (Integer index : state.openPages.keySet()) {
|
|
374
|
+
if (index != pageIndex) {
|
|
375
|
+
candidate = index;
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (candidate == null) break;
|
|
380
|
+
// On failure the page leaves the LRU for `retainedPages`, so this
|
|
381
|
+
// loop always ends and the page is not asked to close again.
|
|
382
|
+
closePageLocked(state, candidate);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Width and height of a page in points; the page is not left open. */
|
|
388
|
+
static int[] pageSizePoints(EngineState state, int pageIndex) {
|
|
389
|
+
int[] cached = state.pageSizes.get(pageIndex);
|
|
390
|
+
if (cached != null) return cached;
|
|
391
|
+
synchronized (state.pdfiumLock) {
|
|
392
|
+
if (state.document == null) return new int[] {0, 0};
|
|
393
|
+
cached = state.pageSizes.get(pageIndex);
|
|
394
|
+
if (cached != null) return cached;
|
|
395
|
+
boolean wasOpen = state.document.hasPage(pageIndex);
|
|
396
|
+
int width = 0;
|
|
397
|
+
int height = 0;
|
|
398
|
+
try {
|
|
399
|
+
if (!wasOpen) state.pdfium.openPage(state.document, pageIndex);
|
|
400
|
+
width = state.pdfium.getPageWidthPoint(state.document, pageIndex);
|
|
401
|
+
height = state.pdfium.getPageHeightPoint(state.document, pageIndex);
|
|
402
|
+
} catch (Throwable ignored) {
|
|
403
|
+
} finally {
|
|
404
|
+
if (!wasOpen) closePageLocked(state, pageIndex);
|
|
405
|
+
}
|
|
406
|
+
int[] size = new int[] {width, height};
|
|
407
|
+
if (width > 0 && height > 0) state.pageSizes.put(pageIndex, size);
|
|
408
|
+
return size;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Page size for laying the whole document out, WITHOUT loading the page:
|
|
414
|
+
* pdfium reads it from the page dictionary. Loading every page to measure
|
|
415
|
+
* it (even closing it right after) leaves each page's parsed objects in the
|
|
416
|
+
* document's cache until it closes, ~200 MB for a 1,400-page book.
|
|
417
|
+
*
|
|
418
|
+
* The library reports this size in device pixels, so the value in points
|
|
419
|
+
* can be off by one. A page that has already been measured exactly (it was
|
|
420
|
+
* drawn, or asked for by {@link #pageSizePoints}) uses the exact value.
|
|
421
|
+
*/
|
|
422
|
+
static int[] layoutPageSizePoints(EngineState state, int pageIndex) {
|
|
423
|
+
int[] exact = state.pageSizes.get(pageIndex);
|
|
424
|
+
if (exact != null) return exact;
|
|
425
|
+
int[] cached = state.layoutSizes.get(pageIndex);
|
|
426
|
+
if (cached != null) return cached;
|
|
427
|
+
synchronized (state.pdfiumLock) {
|
|
428
|
+
if (state.document == null) return new int[] {0, 0};
|
|
429
|
+
int width = 0;
|
|
430
|
+
int height = 0;
|
|
431
|
+
try {
|
|
432
|
+
com.shockwave.pdfium.util.Size size = state.pdfium.getPageSize(state.document, pageIndex);
|
|
433
|
+
width = Math.round(size.getWidth() * 72f / state.densityDpi);
|
|
434
|
+
height = Math.round(size.getHeight() * 72f / state.densityDpi);
|
|
435
|
+
} catch (Throwable ignored) {
|
|
78
436
|
}
|
|
437
|
+
if (width <= 0 || height <= 0) return pageSizePoints(state, pageIndex);
|
|
438
|
+
int[] result = new int[] {width, height};
|
|
439
|
+
state.layoutSizes.put(pageIndex, result);
|
|
440
|
+
return result;
|
|
79
441
|
}
|
|
80
|
-
state.document = document;
|
|
81
|
-
state.fileDescriptor = fd;
|
|
82
|
-
state.sourcePath = sourcePath;
|
|
83
442
|
}
|
|
84
443
|
}
|
|
@@ -48,6 +48,15 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
|
|
|
48
48
|
private static final long MAX_COMIC_ARCHIVE_BYTES = 512L * 1024L * 1024L;
|
|
49
49
|
private final ReactApplicationContext reactContext;
|
|
50
50
|
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
51
|
+
/** One thumbnail at a time, below the priority of anything on screen. */
|
|
52
|
+
private static final java.util.concurrent.ExecutorService thumbnailExecutor =
|
|
53
|
+
java.util.concurrent.Executors.newSingleThreadExecutor(task -> {
|
|
54
|
+
Thread thread = new Thread(() -> {
|
|
55
|
+
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
|
|
56
|
+
task.run();
|
|
57
|
+
}, "papyrus-thumbnail");
|
|
58
|
+
return thread;
|
|
59
|
+
});
|
|
51
60
|
private final ThreadPoolExecutor comicPreviewExecutor = PapyrusComicPreviewExecutor.create();
|
|
52
61
|
private final ConcurrentHashMap<String, File> comicSourceFiles = new ConcurrentHashMap<>();
|
|
53
62
|
private long renderRequestCounter = 0;
|
|
@@ -403,6 +412,63 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
|
|
|
403
412
|
});
|
|
404
413
|
}
|
|
405
414
|
|
|
415
|
+
/**
|
|
416
|
+
* Releases the PDF while the reader is not on screen (the document, its
|
|
417
|
+
* file and every open page). {@link #resumeDocument} opens it again.
|
|
418
|
+
*/
|
|
419
|
+
@ReactMethod
|
|
420
|
+
public void suspendDocument(final String engineId, final Promise promise) {
|
|
421
|
+
executor.execute(() -> {
|
|
422
|
+
PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
|
|
423
|
+
promise.resolve(state != null && PapyrusEngineStore.suspendDocument(state));
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
@ReactMethod
|
|
428
|
+
public void resumeDocument(final String engineId, final Promise promise) {
|
|
429
|
+
executor.execute(() -> {
|
|
430
|
+
PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
|
|
431
|
+
if (state == null) {
|
|
432
|
+
promise.resolve(false);
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
try {
|
|
436
|
+
promise.resolve(PapyrusEngineStore.resumeDocument(state));
|
|
437
|
+
} catch (Throwable error) {
|
|
438
|
+
promise.reject("papyrus_resume_failed", error);
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* One page of a PDF, or the cover of an EPUB, as an image file. Options:
|
|
445
|
+
* uri (local file), type, pageIndex, maxWidth, format ("jpeg" | "png"),
|
|
446
|
+
* quality, outputPath. Nothing stays open after it resolves.
|
|
447
|
+
*/
|
|
448
|
+
@ReactMethod
|
|
449
|
+
public void generateThumbnail(final ReadableMap options, final Promise promise) {
|
|
450
|
+
thumbnailExecutor.execute(() -> {
|
|
451
|
+
try {
|
|
452
|
+
PapyrusThumbnail.Result result = PapyrusThumbnail.generate(
|
|
453
|
+
reactContext,
|
|
454
|
+
options.hasKey("uri") ? options.getString("uri") : null,
|
|
455
|
+
options.hasKey("type") ? options.getString("type") : null,
|
|
456
|
+
options.hasKey("pageIndex") ? options.getInt("pageIndex") : 0,
|
|
457
|
+
options.hasKey("maxWidth") ? options.getInt("maxWidth") : 480,
|
|
458
|
+
options.hasKey("format") ? options.getString("format") : "jpeg",
|
|
459
|
+
options.hasKey("quality") ? options.getInt("quality") : 85,
|
|
460
|
+
options.hasKey("outputPath") ? options.getString("outputPath") : null);
|
|
461
|
+
WritableMap map = Arguments.createMap();
|
|
462
|
+
map.putString("uri", Uri.fromFile(new File(result.path)).toString());
|
|
463
|
+
map.putInt("width", result.width);
|
|
464
|
+
map.putInt("height", result.height);
|
|
465
|
+
promise.resolve(map);
|
|
466
|
+
} catch (Throwable error) {
|
|
467
|
+
promise.reject("papyrus_thumbnail_failed", error.getMessage() == null ? error.getClass().getSimpleName() : error.getMessage(), error);
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
|
|
406
472
|
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
407
473
|
public int getPageCount(String engineId) {
|
|
408
474
|
PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
|
|
@@ -415,6 +481,13 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
|
|
|
415
481
|
WritableMap result = Arguments.createMap();
|
|
416
482
|
result.putInt("engineStates", PapyrusEngineStore.engineCount());
|
|
417
483
|
result.putInt("loadedDocuments", PapyrusEngineStore.loadedDocumentCount());
|
|
484
|
+
result.putInt("openPdfPages", PapyrusEngineStore.openPageCount());
|
|
485
|
+
result.putBoolean("canClosePdfPages", PapyrusEngineStore.canClosePages());
|
|
486
|
+
result.putInt("retainedPdfPages", PapyrusEngineStore.retainedPageCount());
|
|
487
|
+
result.putInt("suspendedDocuments", PapyrusEngineStore.suspendedDocumentCount());
|
|
488
|
+
result.putInt("failedDocumentCloses", PapyrusEngineStore.failedDocumentCloseCount());
|
|
489
|
+
result.putInt("documentRecycles", PapyrusEngineStore.documentRecycleCount());
|
|
490
|
+
result.putInt("leakedPdfPages", PapyrusEngineStore.leakedPageCount());
|
|
418
491
|
for (java.util.Map.Entry<String, Integer> entry : PapyrusPageView.lifecycleStats().entrySet()) {
|
|
419
492
|
result.putInt(entry.getKey(), entry.getValue());
|
|
420
493
|
}
|
|
@@ -531,14 +604,9 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
|
|
|
531
604
|
}
|
|
532
605
|
int width;
|
|
533
606
|
int height;
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
} catch (Throwable ignored) {
|
|
538
|
-
}
|
|
539
|
-
width = state.pdfium.getPageWidthPoint(state.document, pageIndex);
|
|
540
|
-
height = state.pdfium.getPageHeightPoint(state.document, pageIndex);
|
|
541
|
-
}
|
|
607
|
+
int[] pageSize = PapyrusEngineStore.pageSizePoints(state, pageIndex);
|
|
608
|
+
width = pageSize[0];
|
|
609
|
+
height = pageSize[1];
|
|
542
610
|
WritableMap result = Arguments.createMap();
|
|
543
611
|
result.putInt("width", width);
|
|
544
612
|
result.putInt("height", height);
|
|
@@ -743,10 +811,7 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
|
|
|
743
811
|
}
|
|
744
812
|
|
|
745
813
|
private String extractPageText(PapyrusEngineStore.EngineState state, int pageIndex) {
|
|
746
|
-
|
|
747
|
-
state.pdfium.openPage(state.document, pageIndex);
|
|
748
|
-
} catch (Throwable ignored) {
|
|
749
|
-
}
|
|
814
|
+
PapyrusEngineStore.acquirePage(state, pageIndex);
|
|
750
815
|
|
|
751
816
|
try {
|
|
752
817
|
java.lang.reflect.Method method = null;
|
|
@@ -176,11 +176,9 @@ class PapyrusNativeEngineModule : Module() {
|
|
|
176
176
|
promise.resolve(mapOf("width" to 0, "height" to 0))
|
|
177
177
|
return@execute
|
|
178
178
|
}
|
|
179
|
-
val
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
Pair(w, h)
|
|
183
|
-
}
|
|
179
|
+
val pageSize = PapyrusEngineStore.pageSizePoints(state, pageIndex)
|
|
180
|
+
val width = pageSize[0]
|
|
181
|
+
val height = pageSize[1]
|
|
184
182
|
promise.resolve(mapOf("width" to width, "height" to height))
|
|
185
183
|
}
|
|
186
184
|
}
|
|
@@ -417,10 +415,7 @@ class PapyrusNativeEngineModule : Module() {
|
|
|
417
415
|
}
|
|
418
416
|
|
|
419
417
|
private fun extractPageText(state: PapyrusEngineStore.EngineState, pageIndex: Int): String? {
|
|
420
|
-
|
|
421
|
-
state.pdfium.openPage(state.document, pageIndex)
|
|
422
|
-
} catch (_: Throwable) {
|
|
423
|
-
}
|
|
418
|
+
PapyrusEngineStore.acquirePage(state, pageIndex)
|
|
424
419
|
|
|
425
420
|
try {
|
|
426
421
|
var method: Method? = null
|
|
@@ -167,6 +167,7 @@ public class PapyrusPageView extends View {
|
|
|
167
167
|
try {
|
|
168
168
|
telemetry.emit("native.render.worker.start");
|
|
169
169
|
PdfDocument doc = state.document;
|
|
170
|
+
final int docGeneration = state.documentGeneration;
|
|
170
171
|
if (doc == null) {
|
|
171
172
|
telemetry.emit("native.render.stale");
|
|
172
173
|
completion.complete(PapyrusRenderCompletion.Status.STALE);
|
|
@@ -188,21 +189,29 @@ public class PapyrusPageView extends View {
|
|
|
188
189
|
completion.complete(PapyrusRenderCompletion.Status.STALE);
|
|
189
190
|
return;
|
|
190
191
|
}
|
|
191
|
-
|
|
192
|
+
// Another document was set (the same file reopened does not count).
|
|
193
|
+
if (docGeneration != state.documentGeneration || state.document == null) {
|
|
192
194
|
telemetry.emit("native.render.stale");
|
|
193
195
|
completion.complete(PapyrusRenderCompletion.Status.STALE);
|
|
194
196
|
return;
|
|
195
197
|
}
|
|
196
|
-
int pageCount = state.pdfium.getPageCount(
|
|
198
|
+
int pageCount = state.pdfium.getPageCount(state.document);
|
|
197
199
|
if (pageIndex < 0 || pageIndex >= pageCount) {
|
|
198
200
|
telemetry.emit("native.render.stale");
|
|
199
201
|
completion.complete(PapyrusRenderCompletion.Status.STALE);
|
|
200
202
|
return;
|
|
201
203
|
}
|
|
202
|
-
|
|
204
|
+
PapyrusEngineStore.acquirePage(state, pageIndex);
|
|
205
|
+
// Read after acquiring: a long session may have reopened the document.
|
|
206
|
+
PdfDocument current = state.document;
|
|
207
|
+
if (current == null) {
|
|
208
|
+
telemetry.emit("native.render.stale");
|
|
209
|
+
completion.complete(PapyrusRenderCompletion.Status.STALE);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
203
212
|
rendered = Bitmap.createBitmap(renderWidth, renderHeight, Bitmap.Config.ARGB_8888);
|
|
204
213
|
telemetry.emit("native.render.raster.start");
|
|
205
|
-
state.pdfium.renderPageBitmap(
|
|
214
|
+
state.pdfium.renderPageBitmap(current, rendered, pageIndex, 0, 0, renderWidth, renderHeight, true);
|
|
206
215
|
telemetry.emit("native.render.raster.end");
|
|
207
216
|
}
|
|
208
217
|
|