@papyrus-sdk/engine-native 0.2.9 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/android/build.gradle +1 -0
- package/android/src/main/cpp/CMakeLists.txt +45 -24
- package/android/src/main/cpp/papyrus_outline.cpp +173 -139
- package/android/src/main/cpp/papyrus_outline_loader.cpp +125 -0
- package/android/src/main/cpp/papyrus_outline_loader.h +48 -0
- package/android/src/main/cpp/papyrus_outline_loader_test.cpp +240 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusOutline.java +19 -5
- package/android/src/main/java/com/papyrus/engine/PapyrusPageView.java +45 -3
- package/android/src/test/java/com/papyrus/engine/PapyrusOutlineTest.java +40 -0
- package/android/src/test/java/com/papyrus/engine/PapyrusPageViewTest.java +30 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Papyrus Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/android/build.gradle
CHANGED
|
@@ -1,24 +1,45 @@
|
|
|
1
|
-
cmake_minimum_required(VERSION 3.18.1)
|
|
2
|
-
project(papyrus_text)
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
)
|
|
1
|
+
cmake_minimum_required(VERSION 3.18.1)
|
|
2
|
+
project(papyrus_text)
|
|
3
|
+
|
|
4
|
+
option(PAPYRUS_OUTLINE_HOST_TESTS "Build host tests for the outline loader" OFF)
|
|
5
|
+
|
|
6
|
+
function(papyrus_set_cxx17 target_name)
|
|
7
|
+
set_target_properties(${target_name} PROPERTIES
|
|
8
|
+
CXX_STANDARD 17
|
|
9
|
+
CXX_STANDARD_REQUIRED YES
|
|
10
|
+
)
|
|
11
|
+
endfunction()
|
|
12
|
+
|
|
13
|
+
if(ANDROID)
|
|
14
|
+
add_library(papyrus_text SHARED
|
|
15
|
+
papyrus_text_search.cpp
|
|
16
|
+
papyrus_outline.cpp
|
|
17
|
+
papyrus_outline_loader.cpp
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
papyrus_set_cxx17(papyrus_text)
|
|
21
|
+
target_compile_options(papyrus_text PRIVATE -Wall -Werror)
|
|
22
|
+
target_link_options(papyrus_text PRIVATE "-Wl,-z,max-page-size=16384")
|
|
23
|
+
|
|
24
|
+
find_library(log-lib log)
|
|
25
|
+
find_library(android-lib android)
|
|
26
|
+
find_library(dl-lib dl)
|
|
27
|
+
|
|
28
|
+
target_link_libraries(papyrus_text
|
|
29
|
+
${log-lib}
|
|
30
|
+
${android-lib}
|
|
31
|
+
${dl-lib}
|
|
32
|
+
)
|
|
33
|
+
endif()
|
|
34
|
+
|
|
35
|
+
if(PAPYRUS_OUTLINE_HOST_TESTS)
|
|
36
|
+
enable_testing()
|
|
37
|
+
|
|
38
|
+
add_executable(papyrus_outline_loader_test
|
|
39
|
+
papyrus_outline_loader.cpp
|
|
40
|
+
papyrus_outline_loader_test.cpp
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
papyrus_set_cxx17(papyrus_outline_loader_test)
|
|
44
|
+
add_test(NAME papyrus_outline_loader_test COMMAND papyrus_outline_loader_test)
|
|
45
|
+
endif()
|
|
@@ -1,139 +1,173 @@
|
|
|
1
|
-
#include
|
|
2
|
-
|
|
3
|
-
#include <
|
|
4
|
-
|
|
5
|
-
#include <
|
|
6
|
-
|
|
7
|
-
#include <
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
#define
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
1
|
+
#include "papyrus_outline_loader.h"
|
|
2
|
+
|
|
3
|
+
#include <jni.h>
|
|
4
|
+
#include <android/log.h>
|
|
5
|
+
#include <dlfcn.h>
|
|
6
|
+
|
|
7
|
+
#include <cstring>
|
|
8
|
+
#include <vector>
|
|
9
|
+
|
|
10
|
+
#define LOG_TAG "PapyrusOutline"
|
|
11
|
+
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
|
|
12
|
+
|
|
13
|
+
namespace {
|
|
14
|
+
|
|
15
|
+
OutlineLoaderState g_outline_loader = {};
|
|
16
|
+
|
|
17
|
+
void *OutlineDlopen(void *, const char *filename, int flags) {
|
|
18
|
+
return dlopen(filename, flags);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
OutlineLoaderDeps::PdfiumSymbol OutlineDlsym(void *, void *handle, const char *symbol_name) {
|
|
22
|
+
void *raw_symbol = dlsym(handle, symbol_name);
|
|
23
|
+
OutlineLoaderDeps::PdfiumSymbol symbol = nullptr;
|
|
24
|
+
static_assert(sizeof(symbol) == sizeof(raw_symbol),
|
|
25
|
+
"Expected dlsym results to match function pointer size");
|
|
26
|
+
std::memcpy(&symbol, &raw_symbol, sizeof(symbol));
|
|
27
|
+
return symbol;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
int OutlineDlclose(void *, void *handle) {
|
|
31
|
+
return dlclose(handle);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const OutlineLoaderDeps kOutlineLoaderDeps = {
|
|
35
|
+
nullptr,
|
|
36
|
+
&OutlineDlopen,
|
|
37
|
+
&OutlineDlsym,
|
|
38
|
+
&OutlineDlclose,
|
|
39
|
+
"libmodpdfium.so",
|
|
40
|
+
RTLD_LAZY,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
bool LoadPdfium() {
|
|
44
|
+
const OutlineLoadAttemptResult result =
|
|
45
|
+
EnsureOutlinePdfiumLoaded(&g_outline_loader, &kOutlineLoaderDeps);
|
|
46
|
+
if (!result.loaded && result.transitioned_to_unsupported) {
|
|
47
|
+
LOGE("Failed to load required PDFium symbols for outline");
|
|
48
|
+
}
|
|
49
|
+
return result.loaded;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
PdfiumOutlineFns &OutlineFns() {
|
|
53
|
+
return g_outline_loader.fns;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
jstring GetBookmarkTitle(JNIEnv *env, FPDF_BOOKMARK bookmark) {
|
|
57
|
+
unsigned long length = OutlineFns().bookmarkGetTitle(bookmark, nullptr, 0);
|
|
58
|
+
if (length <= 2) {
|
|
59
|
+
return env->NewStringUTF("");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
std::vector<unsigned short> buffer((length / 2) + 1, 0);
|
|
63
|
+
unsigned long written = OutlineFns().bookmarkGetTitle(bookmark, buffer.data(), length);
|
|
64
|
+
int chars = written > 0 ? static_cast<int>(written / 2) - 1
|
|
65
|
+
: static_cast<int>(length / 2) - 1;
|
|
66
|
+
if (chars <= 0) {
|
|
67
|
+
return env->NewStringUTF("");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return env->NewString(reinterpret_cast<const jchar *>(buffer.data()), chars);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
jobjectArray BuildOutlineItems(JNIEnv *env,
|
|
74
|
+
FPDF_DOCUMENT doc,
|
|
75
|
+
FPDF_BOOKMARK parent,
|
|
76
|
+
jclass item_class,
|
|
77
|
+
jmethodID ctor) {
|
|
78
|
+
std::vector<jobject> items;
|
|
79
|
+
|
|
80
|
+
for (FPDF_BOOKMARK child = OutlineFns().bookmarkGetFirstChild(doc, parent); child;
|
|
81
|
+
child = OutlineFns().bookmarkGetNextSibling(doc, child)) {
|
|
82
|
+
jstring title = GetBookmarkTitle(env, child);
|
|
83
|
+
int page_index = -1;
|
|
84
|
+
FPDF_DEST dest = OutlineFns().bookmarkGetDest(doc, child);
|
|
85
|
+
if (dest != nullptr) {
|
|
86
|
+
page_index = OutlineFns().destGetPageIndex(doc, dest);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
jobjectArray children = BuildOutlineItems(env, doc, child, item_class, ctor);
|
|
90
|
+
jobject item = env->NewObject(item_class, ctor, title, page_index, children);
|
|
91
|
+
|
|
92
|
+
if (title != nullptr) {
|
|
93
|
+
env->DeleteLocalRef(title);
|
|
94
|
+
}
|
|
95
|
+
if (children != nullptr) {
|
|
96
|
+
env->DeleteLocalRef(children);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (item != nullptr) {
|
|
100
|
+
items.push_back(item);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
jobjectArray array =
|
|
105
|
+
env->NewObjectArray(static_cast<jsize>(items.size()), item_class, nullptr);
|
|
106
|
+
for (jsize i = 0; i < static_cast<jsize>(items.size()); i++) {
|
|
107
|
+
env->SetObjectArrayElement(array, i, items[i]);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return array;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
jobjectArray BuildOutline(JNIEnv *env, FPDF_DOCUMENT doc) {
|
|
114
|
+
jclass item_class = env->FindClass("com/papyrus/engine/PapyrusOutlineItem");
|
|
115
|
+
if (item_class == nullptr) {
|
|
116
|
+
return nullptr;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
jmethodID ctor = env->GetMethodID(
|
|
120
|
+
item_class, "<init>", "(Ljava/lang/String;I[Lcom/papyrus/engine/PapyrusOutlineItem;)V");
|
|
121
|
+
if (ctor == nullptr) {
|
|
122
|
+
return nullptr;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return BuildOutlineItems(env, doc, nullptr, item_class, ctor);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
} // namespace
|
|
129
|
+
|
|
130
|
+
extern "C" JNIEXPORT jboolean JNICALL
|
|
131
|
+
Java_com_papyrus_engine_PapyrusOutline_nativeIsOutlineSupported(JNIEnv *, jclass) {
|
|
132
|
+
return LoadPdfium() ? JNI_TRUE : JNI_FALSE;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
extern "C" JNIEXPORT jobjectArray JNICALL
|
|
136
|
+
Java_com_papyrus_engine_PapyrusOutline_nativeGetOutline(JNIEnv *env, jclass, jlong docPtr) {
|
|
137
|
+
if (!LoadPdfium()) {
|
|
138
|
+
return nullptr;
|
|
139
|
+
}
|
|
140
|
+
if (!docPtr) {
|
|
141
|
+
return nullptr;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
FPDF_DOCUMENT doc = reinterpret_cast<FPDF_DOCUMENT>(docPtr);
|
|
145
|
+
return BuildOutline(env, doc);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
extern "C" JNIEXPORT jobjectArray JNICALL
|
|
149
|
+
Java_com_papyrus_engine_PapyrusOutline_nativeGetOutlineFile(JNIEnv *env,
|
|
150
|
+
jclass,
|
|
151
|
+
jstring filePath) {
|
|
152
|
+
if (!LoadPdfium()) {
|
|
153
|
+
return nullptr;
|
|
154
|
+
}
|
|
155
|
+
if (filePath == nullptr) {
|
|
156
|
+
return nullptr;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const char *path = env->GetStringUTFChars(filePath, nullptr);
|
|
160
|
+
if (path == nullptr) {
|
|
161
|
+
return nullptr;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
FPDF_DOCUMENT doc = OutlineFns().loadDocument(path, nullptr);
|
|
165
|
+
env->ReleaseStringUTFChars(filePath, path);
|
|
166
|
+
if (doc == nullptr) {
|
|
167
|
+
return nullptr;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
jobjectArray result = BuildOutline(env, doc);
|
|
171
|
+
OutlineFns().closeDocument(doc);
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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);
|