@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,125 +1,125 @@
|
|
|
1
|
-
#include "papyrus_outline_loader.h"
|
|
2
|
-
|
|
3
|
-
#include <cstring>
|
|
4
|
-
|
|
5
|
-
namespace {
|
|
6
|
-
|
|
7
|
-
void ClearOutlineFns(PdfiumOutlineFns *fns) {
|
|
8
|
-
if (fns == nullptr) {
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
*fns = {};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
void CloseHandleIfPresent(OutlineLoaderState *state, const OutlineLoaderDeps *deps) {
|
|
16
|
-
if (state == nullptr || state->handle == nullptr) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (deps != nullptr && deps->dlclose_fn != nullptr) {
|
|
21
|
-
deps->dlclose_fn(deps->user_data, state->handle);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
state->handle = nullptr;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
void TransitionToUnsupported(OutlineLoaderState *state, const OutlineLoaderDeps *deps) {
|
|
28
|
-
if (state == nullptr) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
ClearOutlineFns(&state->fns);
|
|
33
|
-
CloseHandleIfPresent(state, deps);
|
|
34
|
-
state->load_state = OutlineLoadState::kUnsupported;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
template <typename FnType>
|
|
38
|
-
FnType ResolveOutlineSymbol(const OutlineLoaderDeps *deps, void *handle, const char *symbol_name) {
|
|
39
|
-
const OutlineLoaderDeps::PdfiumSymbol raw_symbol =
|
|
40
|
-
deps->dlsym_fn(deps->user_data, handle, symbol_name);
|
|
41
|
-
|
|
42
|
-
FnType resolved_symbol = nullptr;
|
|
43
|
-
static_assert(sizeof(resolved_symbol) == sizeof(raw_symbol),
|
|
44
|
-
"Expected function pointers to share the same size");
|
|
45
|
-
std::memcpy(&resolved_symbol, &raw_symbol, sizeof(resolved_symbol));
|
|
46
|
-
return resolved_symbol;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
PdfiumOutlineFns LoadOutlineFns(void *handle, const OutlineLoaderDeps *deps) {
|
|
50
|
-
PdfiumOutlineFns fns = {};
|
|
51
|
-
fns.loadDocument =
|
|
52
|
-
ResolveOutlineSymbol<decltype(fns.loadDocument)>(deps, handle, "FPDF_LoadDocument");
|
|
53
|
-
fns.closeDocument =
|
|
54
|
-
ResolveOutlineSymbol<decltype(fns.closeDocument)>(deps, handle, "FPDF_CloseDocument");
|
|
55
|
-
fns.bookmarkGetFirstChild = ResolveOutlineSymbol<decltype(fns.bookmarkGetFirstChild)>(
|
|
56
|
-
deps, handle, "FPDFBookmark_GetFirstChild");
|
|
57
|
-
fns.bookmarkGetNextSibling = ResolveOutlineSymbol<decltype(fns.bookmarkGetNextSibling)>(
|
|
58
|
-
deps, handle, "FPDFBookmark_GetNextSibling");
|
|
59
|
-
fns.bookmarkGetTitle =
|
|
60
|
-
ResolveOutlineSymbol<decltype(fns.bookmarkGetTitle)>(deps, handle, "FPDFBookmark_GetTitle");
|
|
61
|
-
fns.bookmarkGetDest =
|
|
62
|
-
ResolveOutlineSymbol<decltype(fns.bookmarkGetDest)>(deps, handle, "FPDFBookmark_GetDest");
|
|
63
|
-
fns.destGetPageIndex =
|
|
64
|
-
ResolveOutlineSymbol<decltype(fns.destGetPageIndex)>(deps, handle, "FPDFDest_GetPageIndex");
|
|
65
|
-
return fns;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
bool HasRequiredDeps(const OutlineLoaderDeps *deps) {
|
|
69
|
-
return deps != nullptr && deps->dlopen_fn != nullptr && deps->dlsym_fn != nullptr &&
|
|
70
|
-
deps->dlclose_fn != nullptr && deps->library_name != nullptr;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
} // namespace
|
|
74
|
-
|
|
75
|
-
bool HasCompleteOutlineFns(const PdfiumOutlineFns &fns) {
|
|
76
|
-
return fns.loadDocument != nullptr && fns.closeDocument != nullptr &&
|
|
77
|
-
fns.bookmarkGetFirstChild != nullptr && fns.bookmarkGetNextSibling != nullptr &&
|
|
78
|
-
fns.bookmarkGetTitle != nullptr && fns.bookmarkGetDest != nullptr &&
|
|
79
|
-
fns.destGetPageIndex != nullptr;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
OutlineLoadAttemptResult EnsureOutlinePdfiumLoaded(OutlineLoaderState *state,
|
|
83
|
-
const OutlineLoaderDeps *deps) {
|
|
84
|
-
if (state == nullptr) {
|
|
85
|
-
return {};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (state->load_state == OutlineLoadState::kSupported) {
|
|
89
|
-
return {true, false};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (state->load_state == OutlineLoadState::kUnsupported) {
|
|
93
|
-
return {false, false};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (!HasRequiredDeps(deps)) {
|
|
97
|
-
TransitionToUnsupported(state, deps);
|
|
98
|
-
return {false, true};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
state->handle = deps->dlopen_fn(deps->user_data, deps->library_name, deps->dlopen_flags);
|
|
102
|
-
if (state->handle == nullptr) {
|
|
103
|
-
TransitionToUnsupported(state, deps);
|
|
104
|
-
return {false, true};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
state->fns = LoadOutlineFns(state->handle, deps);
|
|
108
|
-
if (!HasCompleteOutlineFns(state->fns)) {
|
|
109
|
-
TransitionToUnsupported(state, deps);
|
|
110
|
-
return {false, true};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
state->load_state = OutlineLoadState::kSupported;
|
|
114
|
-
return {true, false};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
void ResetOutlineLoaderState(OutlineLoaderState *state, const OutlineLoaderDeps *deps) {
|
|
118
|
-
if (state == nullptr) {
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
ClearOutlineFns(&state->fns);
|
|
123
|
-
CloseHandleIfPresent(state, deps);
|
|
124
|
-
state->load_state = OutlineLoadState::kUninitialized;
|
|
125
|
-
}
|
|
1
|
+
#include "papyrus_outline_loader.h"
|
|
2
|
+
|
|
3
|
+
#include <cstring>
|
|
4
|
+
|
|
5
|
+
namespace {
|
|
6
|
+
|
|
7
|
+
void ClearOutlineFns(PdfiumOutlineFns *fns) {
|
|
8
|
+
if (fns == nullptr) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
*fns = {};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
void CloseHandleIfPresent(OutlineLoaderState *state, const OutlineLoaderDeps *deps) {
|
|
16
|
+
if (state == nullptr || state->handle == nullptr) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (deps != nullptr && deps->dlclose_fn != nullptr) {
|
|
21
|
+
deps->dlclose_fn(deps->user_data, state->handle);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
state->handle = nullptr;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
void TransitionToUnsupported(OutlineLoaderState *state, const OutlineLoaderDeps *deps) {
|
|
28
|
+
if (state == nullptr) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
ClearOutlineFns(&state->fns);
|
|
33
|
+
CloseHandleIfPresent(state, deps);
|
|
34
|
+
state->load_state = OutlineLoadState::kUnsupported;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
template <typename FnType>
|
|
38
|
+
FnType ResolveOutlineSymbol(const OutlineLoaderDeps *deps, void *handle, const char *symbol_name) {
|
|
39
|
+
const OutlineLoaderDeps::PdfiumSymbol raw_symbol =
|
|
40
|
+
deps->dlsym_fn(deps->user_data, handle, symbol_name);
|
|
41
|
+
|
|
42
|
+
FnType resolved_symbol = nullptr;
|
|
43
|
+
static_assert(sizeof(resolved_symbol) == sizeof(raw_symbol),
|
|
44
|
+
"Expected function pointers to share the same size");
|
|
45
|
+
std::memcpy(&resolved_symbol, &raw_symbol, sizeof(resolved_symbol));
|
|
46
|
+
return resolved_symbol;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
PdfiumOutlineFns LoadOutlineFns(void *handle, const OutlineLoaderDeps *deps) {
|
|
50
|
+
PdfiumOutlineFns fns = {};
|
|
51
|
+
fns.loadDocument =
|
|
52
|
+
ResolveOutlineSymbol<decltype(fns.loadDocument)>(deps, handle, "FPDF_LoadDocument");
|
|
53
|
+
fns.closeDocument =
|
|
54
|
+
ResolveOutlineSymbol<decltype(fns.closeDocument)>(deps, handle, "FPDF_CloseDocument");
|
|
55
|
+
fns.bookmarkGetFirstChild = ResolveOutlineSymbol<decltype(fns.bookmarkGetFirstChild)>(
|
|
56
|
+
deps, handle, "FPDFBookmark_GetFirstChild");
|
|
57
|
+
fns.bookmarkGetNextSibling = ResolveOutlineSymbol<decltype(fns.bookmarkGetNextSibling)>(
|
|
58
|
+
deps, handle, "FPDFBookmark_GetNextSibling");
|
|
59
|
+
fns.bookmarkGetTitle =
|
|
60
|
+
ResolveOutlineSymbol<decltype(fns.bookmarkGetTitle)>(deps, handle, "FPDFBookmark_GetTitle");
|
|
61
|
+
fns.bookmarkGetDest =
|
|
62
|
+
ResolveOutlineSymbol<decltype(fns.bookmarkGetDest)>(deps, handle, "FPDFBookmark_GetDest");
|
|
63
|
+
fns.destGetPageIndex =
|
|
64
|
+
ResolveOutlineSymbol<decltype(fns.destGetPageIndex)>(deps, handle, "FPDFDest_GetPageIndex");
|
|
65
|
+
return fns;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
bool HasRequiredDeps(const OutlineLoaderDeps *deps) {
|
|
69
|
+
return deps != nullptr && deps->dlopen_fn != nullptr && deps->dlsym_fn != nullptr &&
|
|
70
|
+
deps->dlclose_fn != nullptr && deps->library_name != nullptr;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
} // namespace
|
|
74
|
+
|
|
75
|
+
bool HasCompleteOutlineFns(const PdfiumOutlineFns &fns) {
|
|
76
|
+
return fns.loadDocument != nullptr && fns.closeDocument != nullptr &&
|
|
77
|
+
fns.bookmarkGetFirstChild != nullptr && fns.bookmarkGetNextSibling != nullptr &&
|
|
78
|
+
fns.bookmarkGetTitle != nullptr && fns.bookmarkGetDest != nullptr &&
|
|
79
|
+
fns.destGetPageIndex != nullptr;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
OutlineLoadAttemptResult EnsureOutlinePdfiumLoaded(OutlineLoaderState *state,
|
|
83
|
+
const OutlineLoaderDeps *deps) {
|
|
84
|
+
if (state == nullptr) {
|
|
85
|
+
return {};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (state->load_state == OutlineLoadState::kSupported) {
|
|
89
|
+
return {true, false};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (state->load_state == OutlineLoadState::kUnsupported) {
|
|
93
|
+
return {false, false};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!HasRequiredDeps(deps)) {
|
|
97
|
+
TransitionToUnsupported(state, deps);
|
|
98
|
+
return {false, true};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
state->handle = deps->dlopen_fn(deps->user_data, deps->library_name, deps->dlopen_flags);
|
|
102
|
+
if (state->handle == nullptr) {
|
|
103
|
+
TransitionToUnsupported(state, deps);
|
|
104
|
+
return {false, true};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
state->fns = LoadOutlineFns(state->handle, deps);
|
|
108
|
+
if (!HasCompleteOutlineFns(state->fns)) {
|
|
109
|
+
TransitionToUnsupported(state, deps);
|
|
110
|
+
return {false, true};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
state->load_state = OutlineLoadState::kSupported;
|
|
114
|
+
return {true, false};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
void ResetOutlineLoaderState(OutlineLoaderState *state, const OutlineLoaderDeps *deps) {
|
|
118
|
+
if (state == nullptr) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
ClearOutlineFns(&state->fns);
|
|
123
|
+
CloseHandleIfPresent(state, deps);
|
|
124
|
+
state->load_state = OutlineLoadState::kUninitialized;
|
|
125
|
+
}
|
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
using FPDF_DOCUMENT = void *;
|
|
4
|
-
using FPDF_BOOKMARK = void *;
|
|
5
|
-
using FPDF_DEST = void *;
|
|
6
|
-
|
|
7
|
-
enum class OutlineLoadState {
|
|
8
|
-
kUninitialized,
|
|
9
|
-
kSupported,
|
|
10
|
-
kUnsupported,
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
struct PdfiumOutlineFns {
|
|
14
|
-
FPDF_DOCUMENT (*loadDocument)(const char *, const char *) = nullptr;
|
|
15
|
-
void (*closeDocument)(FPDF_DOCUMENT) = nullptr;
|
|
16
|
-
FPDF_BOOKMARK (*bookmarkGetFirstChild)(FPDF_DOCUMENT, FPDF_BOOKMARK) = nullptr;
|
|
17
|
-
FPDF_BOOKMARK (*bookmarkGetNextSibling)(FPDF_DOCUMENT, FPDF_BOOKMARK) = nullptr;
|
|
18
|
-
unsigned long (*bookmarkGetTitle)(FPDF_BOOKMARK, void *, unsigned long) = nullptr;
|
|
19
|
-
FPDF_DEST (*bookmarkGetDest)(FPDF_DOCUMENT, FPDF_BOOKMARK) = nullptr;
|
|
20
|
-
int (*destGetPageIndex)(FPDF_DOCUMENT, FPDF_DEST) = nullptr;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
struct OutlineLoaderDeps {
|
|
24
|
-
using PdfiumSymbol = void (*)();
|
|
25
|
-
|
|
26
|
-
void *user_data = nullptr;
|
|
27
|
-
void *(*dlopen_fn)(void *user_data, const char *filename, int flags) = nullptr;
|
|
28
|
-
PdfiumSymbol (*dlsym_fn)(void *user_data, void *handle, const char *symbol_name) = nullptr;
|
|
29
|
-
int (*dlclose_fn)(void *user_data, void *handle) = nullptr;
|
|
30
|
-
const char *library_name = nullptr;
|
|
31
|
-
int dlopen_flags = 0;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
struct OutlineLoaderState {
|
|
35
|
-
void *handle = nullptr;
|
|
36
|
-
PdfiumOutlineFns fns = {};
|
|
37
|
-
OutlineLoadState load_state = OutlineLoadState::kUninitialized;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
struct OutlineLoadAttemptResult {
|
|
41
|
-
bool loaded = false;
|
|
42
|
-
bool transitioned_to_unsupported = false;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
bool HasCompleteOutlineFns(const PdfiumOutlineFns &fns);
|
|
46
|
-
OutlineLoadAttemptResult EnsureOutlinePdfiumLoaded(OutlineLoaderState *state,
|
|
47
|
-
const OutlineLoaderDeps *deps);
|
|
48
|
-
void ResetOutlineLoaderState(OutlineLoaderState *state, const OutlineLoaderDeps *deps);
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
using FPDF_DOCUMENT = void *;
|
|
4
|
+
using FPDF_BOOKMARK = void *;
|
|
5
|
+
using FPDF_DEST = void *;
|
|
6
|
+
|
|
7
|
+
enum class OutlineLoadState {
|
|
8
|
+
kUninitialized,
|
|
9
|
+
kSupported,
|
|
10
|
+
kUnsupported,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
struct PdfiumOutlineFns {
|
|
14
|
+
FPDF_DOCUMENT (*loadDocument)(const char *, const char *) = nullptr;
|
|
15
|
+
void (*closeDocument)(FPDF_DOCUMENT) = nullptr;
|
|
16
|
+
FPDF_BOOKMARK (*bookmarkGetFirstChild)(FPDF_DOCUMENT, FPDF_BOOKMARK) = nullptr;
|
|
17
|
+
FPDF_BOOKMARK (*bookmarkGetNextSibling)(FPDF_DOCUMENT, FPDF_BOOKMARK) = nullptr;
|
|
18
|
+
unsigned long (*bookmarkGetTitle)(FPDF_BOOKMARK, void *, unsigned long) = nullptr;
|
|
19
|
+
FPDF_DEST (*bookmarkGetDest)(FPDF_DOCUMENT, FPDF_BOOKMARK) = nullptr;
|
|
20
|
+
int (*destGetPageIndex)(FPDF_DOCUMENT, FPDF_DEST) = nullptr;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
struct OutlineLoaderDeps {
|
|
24
|
+
using PdfiumSymbol = void (*)();
|
|
25
|
+
|
|
26
|
+
void *user_data = nullptr;
|
|
27
|
+
void *(*dlopen_fn)(void *user_data, const char *filename, int flags) = nullptr;
|
|
28
|
+
PdfiumSymbol (*dlsym_fn)(void *user_data, void *handle, const char *symbol_name) = nullptr;
|
|
29
|
+
int (*dlclose_fn)(void *user_data, void *handle) = nullptr;
|
|
30
|
+
const char *library_name = nullptr;
|
|
31
|
+
int dlopen_flags = 0;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
struct OutlineLoaderState {
|
|
35
|
+
void *handle = nullptr;
|
|
36
|
+
PdfiumOutlineFns fns = {};
|
|
37
|
+
OutlineLoadState load_state = OutlineLoadState::kUninitialized;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
struct OutlineLoadAttemptResult {
|
|
41
|
+
bool loaded = false;
|
|
42
|
+
bool transitioned_to_unsupported = false;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
bool HasCompleteOutlineFns(const PdfiumOutlineFns &fns);
|
|
46
|
+
OutlineLoadAttemptResult EnsureOutlinePdfiumLoaded(OutlineLoaderState *state,
|
|
47
|
+
const OutlineLoaderDeps *deps);
|
|
48
|
+
void ResetOutlineLoaderState(OutlineLoaderState *state, const OutlineLoaderDeps *deps);
|
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
package com.papyrus.engine;
|
|
2
|
-
|
|
3
|
-
import android.content.Context;
|
|
4
|
-
import android.os.ParcelFileDescriptor;
|
|
5
|
-
|
|
6
|
-
import com.shockwave.pdfium.PdfDocument;
|
|
7
|
-
import com.shockwave.pdfium.PdfiumCore;
|
|
8
|
-
|
|
9
|
-
import java.io.File;
|
|
10
|
-
import java.io.IOException;
|
|
11
|
-
import java.util.Map;
|
|
12
|
-
import java.util.UUID;
|
|
13
|
-
import java.util.concurrent.ConcurrentHashMap;
|
|
14
|
-
|
|
15
|
-
final class PapyrusEngineStore {
|
|
16
|
-
static final class EngineState {
|
|
17
|
-
final PdfiumCore pdfium;
|
|
18
|
-
final Object pdfiumLock = new Object();
|
|
19
|
-
volatile boolean isSearching = false;
|
|
20
|
-
PdfDocument document;
|
|
21
|
-
ParcelFileDescriptor fileDescriptor;
|
|
22
|
-
String sourcePath;
|
|
23
|
-
|
|
24
|
-
EngineState(PdfiumCore pdfium) {
|
|
25
|
-
this.pdfium = pdfium;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
private static final Map<String, EngineState> ENGINES = new ConcurrentHashMap<>();
|
|
30
|
-
|
|
31
|
-
static String createEngine(Context context) {
|
|
32
|
-
String engineId = UUID.randomUUID().toString();
|
|
33
|
-
ENGINES.put(engineId, new EngineState(new PdfiumCore(context)));
|
|
34
|
-
return engineId;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
static EngineState getEngine(String engineId) {
|
|
38
|
-
return ENGINES.get(engineId);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
static void destroyEngine(String engineId) {
|
|
42
|
-
EngineState state = ENGINES.remove(engineId);
|
|
43
|
-
if (state == null) return;
|
|
44
|
-
if (state.document != null) {
|
|
45
|
-
state.pdfium.closeDocument(state.document);
|
|
46
|
-
state.document = null;
|
|
47
|
-
}
|
|
48
|
-
if (state.fileDescriptor != null) {
|
|
49
|
-
try {
|
|
50
|
-
state.fileDescriptor.close();
|
|
51
|
-
} catch (IOException ignored) {
|
|
52
|
-
}
|
|
53
|
-
state.fileDescriptor = null;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
static void setDocument(EngineState state, PdfDocument document, ParcelFileDescriptor fd, String sourcePath) {
|
|
58
|
-
if (state.document != null) {
|
|
59
|
-
state.pdfium.closeDocument(state.document);
|
|
60
|
-
}
|
|
61
|
-
if (state.fileDescriptor != null) {
|
|
62
|
-
try {
|
|
63
|
-
state.fileDescriptor.close();
|
|
64
|
-
} catch (IOException ignored) {
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
state.document = document;
|
|
68
|
-
state.fileDescriptor = fd;
|
|
69
|
-
state.sourcePath = sourcePath;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
1
|
+
package com.papyrus.engine;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.os.ParcelFileDescriptor;
|
|
5
|
+
|
|
6
|
+
import com.shockwave.pdfium.PdfDocument;
|
|
7
|
+
import com.shockwave.pdfium.PdfiumCore;
|
|
8
|
+
|
|
9
|
+
import java.io.File;
|
|
10
|
+
import java.io.IOException;
|
|
11
|
+
import java.util.Map;
|
|
12
|
+
import java.util.UUID;
|
|
13
|
+
import java.util.concurrent.ConcurrentHashMap;
|
|
14
|
+
|
|
15
|
+
final class PapyrusEngineStore {
|
|
16
|
+
static final class EngineState {
|
|
17
|
+
final PdfiumCore pdfium;
|
|
18
|
+
final Object pdfiumLock = new Object();
|
|
19
|
+
volatile boolean isSearching = false;
|
|
20
|
+
PdfDocument document;
|
|
21
|
+
ParcelFileDescriptor fileDescriptor;
|
|
22
|
+
String sourcePath;
|
|
23
|
+
|
|
24
|
+
EngineState(PdfiumCore pdfium) {
|
|
25
|
+
this.pdfium = pdfium;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private static final Map<String, EngineState> ENGINES = new ConcurrentHashMap<>();
|
|
30
|
+
|
|
31
|
+
static String createEngine(Context context) {
|
|
32
|
+
String engineId = UUID.randomUUID().toString();
|
|
33
|
+
ENGINES.put(engineId, new EngineState(new PdfiumCore(context)));
|
|
34
|
+
return engineId;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static EngineState getEngine(String engineId) {
|
|
38
|
+
return ENGINES.get(engineId);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static void destroyEngine(String engineId) {
|
|
42
|
+
EngineState state = ENGINES.remove(engineId);
|
|
43
|
+
if (state == null) return;
|
|
44
|
+
if (state.document != null) {
|
|
45
|
+
state.pdfium.closeDocument(state.document);
|
|
46
|
+
state.document = null;
|
|
47
|
+
}
|
|
48
|
+
if (state.fileDescriptor != null) {
|
|
49
|
+
try {
|
|
50
|
+
state.fileDescriptor.close();
|
|
51
|
+
} catch (IOException ignored) {
|
|
52
|
+
}
|
|
53
|
+
state.fileDescriptor = null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static void setDocument(EngineState state, PdfDocument document, ParcelFileDescriptor fd, String sourcePath) {
|
|
58
|
+
if (state.document != null) {
|
|
59
|
+
state.pdfium.closeDocument(state.document);
|
|
60
|
+
}
|
|
61
|
+
if (state.fileDescriptor != null) {
|
|
62
|
+
try {
|
|
63
|
+
state.fileDescriptor.close();
|
|
64
|
+
} catch (IOException ignored) {
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
state.document = document;
|
|
68
|
+
state.fileDescriptor = fd;
|
|
69
|
+
state.sourcePath = sourcePath;
|
|
70
|
+
}
|
|
71
|
+
}
|