@papyrus-sdk/engine-native 0.2.20 → 0.2.22-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/android/src/main/cpp/CMakeLists.txt +12 -1
- package/android/src/main/cpp/papyrus_outline_loader_test.cpp +240 -0
- package/android/src/main/cpp/papyrus_text_search.cpp +162 -24
- package/android/src/main/cpp/papyrus_text_word.cpp +33 -0
- package/android/src/main/cpp/papyrus_text_word.h +19 -0
- package/android/src/main/cpp/papyrus_text_word_test.cpp +72 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +3 -3
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.kt +11 -3
- package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +279 -138
- package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerViewManager.java +9 -7
- package/android/src/main/java/com/papyrus/engine/PapyrusTextSelect.java +2 -2
- package/android/src/main/java/com/papyrus/engine/PapyrusTextSelectionGesture.java +44 -2
- package/dist/index.d.mts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +43 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +42 -11
- package/dist/index.mjs.map +1 -1
- package/ios/PapyrusEngineStore.h +3 -0
- package/ios/PapyrusEngineStore.m +29 -7
- package/ios/PapyrusNativeEngine.m +184 -0
- package/ios/PapyrusPdfDocumentView.h +24 -0
- package/ios/PapyrusPdfDocumentView.m +658 -0
- package/ios/PapyrusPdfDocumentViewManager.m +32 -0
- package/package.json +5 -5
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.
|
|
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.18.1)
|
|
|
2
2
|
project(papyrus_text)
|
|
3
3
|
|
|
4
4
|
option(PAPYRUS_OUTLINE_HOST_TESTS "Build host tests for the outline loader" OFF)
|
|
5
|
+
# Alias geral para todos os testes C++ puros (inclui o outline loader).
|
|
6
|
+
option(PAPYRUS_CPP_HOST_TESTS "Build all pure C++ host tests" OFF)
|
|
5
7
|
|
|
6
8
|
function(papyrus_set_cxx17 target_name)
|
|
7
9
|
set_target_properties(${target_name} PROPERTIES
|
|
@@ -15,6 +17,7 @@ if(ANDROID)
|
|
|
15
17
|
papyrus_text_search.cpp
|
|
16
18
|
papyrus_outline.cpp
|
|
17
19
|
papyrus_outline_loader.cpp
|
|
20
|
+
papyrus_text_word.cpp
|
|
18
21
|
)
|
|
19
22
|
|
|
20
23
|
papyrus_set_cxx17(papyrus_text)
|
|
@@ -32,7 +35,7 @@ if(ANDROID)
|
|
|
32
35
|
)
|
|
33
36
|
endif()
|
|
34
37
|
|
|
35
|
-
if(PAPYRUS_OUTLINE_HOST_TESTS)
|
|
38
|
+
if(PAPYRUS_OUTLINE_HOST_TESTS OR PAPYRUS_CPP_HOST_TESTS)
|
|
36
39
|
enable_testing()
|
|
37
40
|
|
|
38
41
|
add_executable(papyrus_outline_loader_test
|
|
@@ -42,4 +45,12 @@ if(PAPYRUS_OUTLINE_HOST_TESTS)
|
|
|
42
45
|
|
|
43
46
|
papyrus_set_cxx17(papyrus_outline_loader_test)
|
|
44
47
|
add_test(NAME papyrus_outline_loader_test COMMAND papyrus_outline_loader_test)
|
|
48
|
+
|
|
49
|
+
add_executable(papyrus_text_word_test
|
|
50
|
+
papyrus_text_word.cpp
|
|
51
|
+
papyrus_text_word_test.cpp
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
papyrus_set_cxx17(papyrus_text_word_test)
|
|
55
|
+
add_test(NAME papyrus_text_word_test COMMAND papyrus_text_word_test)
|
|
45
56
|
endif()
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
#include "papyrus_outline_loader.h"
|
|
2
|
+
|
|
3
|
+
#include <cstdlib>
|
|
4
|
+
#include <cstring>
|
|
5
|
+
#include <iostream>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <unordered_map>
|
|
8
|
+
|
|
9
|
+
namespace {
|
|
10
|
+
|
|
11
|
+
using PdfiumSymbol = OutlineLoaderDeps::PdfiumSymbol;
|
|
12
|
+
|
|
13
|
+
struct FakeLoaderEnv {
|
|
14
|
+
void *library_handle = reinterpret_cast<void *>(0x1);
|
|
15
|
+
int dlopen_calls = 0;
|
|
16
|
+
int dlsym_calls = 0;
|
|
17
|
+
int dlclose_calls = 0;
|
|
18
|
+
std::unordered_map<std::string, PdfiumSymbol> symbols;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
FPDF_DOCUMENT FakeLoadDocument(const char *, const char *) {
|
|
22
|
+
return nullptr;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
void FakeCloseDocument(FPDF_DOCUMENT) {}
|
|
26
|
+
|
|
27
|
+
FPDF_BOOKMARK FakeBookmarkGetFirstChild(FPDF_DOCUMENT, FPDF_BOOKMARK) {
|
|
28
|
+
return nullptr;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
FPDF_BOOKMARK FakeBookmarkGetNextSibling(FPDF_DOCUMENT, FPDF_BOOKMARK) {
|
|
32
|
+
return nullptr;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
unsigned long FakeBookmarkGetTitle(FPDF_BOOKMARK, void *, unsigned long) {
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
FPDF_DEST FakeBookmarkGetDest(FPDF_DOCUMENT, FPDF_BOOKMARK) {
|
|
40
|
+
return nullptr;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
int FakeDestGetPageIndex(FPDF_DOCUMENT, FPDF_DEST) {
|
|
44
|
+
return -1;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
template <typename FnType>
|
|
48
|
+
PdfiumSymbol ToPdfiumSymbol(FnType fn) {
|
|
49
|
+
PdfiumSymbol symbol = nullptr;
|
|
50
|
+
static_assert(sizeof(symbol) == sizeof(fn),
|
|
51
|
+
"Expected function pointers to share the same size");
|
|
52
|
+
std::memcpy(&symbol, &fn, sizeof(symbol));
|
|
53
|
+
return symbol;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
PdfiumOutlineFns MakeCompleteOutlineFns() {
|
|
57
|
+
PdfiumOutlineFns fns = {};
|
|
58
|
+
fns.loadDocument = &FakeLoadDocument;
|
|
59
|
+
fns.closeDocument = &FakeCloseDocument;
|
|
60
|
+
fns.bookmarkGetFirstChild = &FakeBookmarkGetFirstChild;
|
|
61
|
+
fns.bookmarkGetNextSibling = &FakeBookmarkGetNextSibling;
|
|
62
|
+
fns.bookmarkGetTitle = &FakeBookmarkGetTitle;
|
|
63
|
+
fns.bookmarkGetDest = &FakeBookmarkGetDest;
|
|
64
|
+
fns.destGetPageIndex = &FakeDestGetPageIndex;
|
|
65
|
+
return fns;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
FakeLoaderEnv *GetEnv(void *user_data) {
|
|
69
|
+
return static_cast<FakeLoaderEnv *>(user_data);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void *FakeDlopen(void *user_data, const char *, int) {
|
|
73
|
+
FakeLoaderEnv *env = GetEnv(user_data);
|
|
74
|
+
env->dlopen_calls += 1;
|
|
75
|
+
return env->library_handle;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
PdfiumSymbol FakeDlsym(void *user_data, void *, const char *symbol_name) {
|
|
79
|
+
FakeLoaderEnv *env = GetEnv(user_data);
|
|
80
|
+
env->dlsym_calls += 1;
|
|
81
|
+
|
|
82
|
+
const auto it = env->symbols.find(symbol_name);
|
|
83
|
+
if (it == env->symbols.end()) {
|
|
84
|
+
return nullptr;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return it->second;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
int FakeDlclose(void *user_data, void *) {
|
|
91
|
+
FakeLoaderEnv *env = GetEnv(user_data);
|
|
92
|
+
env->dlclose_calls += 1;
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
OutlineLoaderDeps MakeDeps(FakeLoaderEnv *env) {
|
|
97
|
+
OutlineLoaderDeps deps = {};
|
|
98
|
+
deps.user_data = env;
|
|
99
|
+
deps.dlopen_fn = &FakeDlopen;
|
|
100
|
+
deps.dlsym_fn = &FakeDlsym;
|
|
101
|
+
deps.dlclose_fn = &FakeDlclose;
|
|
102
|
+
deps.library_name = "libmodpdfium.so";
|
|
103
|
+
deps.dlopen_flags = 0;
|
|
104
|
+
return deps;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
void InstallSymbols(FakeLoaderEnv *env, const PdfiumOutlineFns &fns) {
|
|
108
|
+
env->symbols["FPDF_LoadDocument"] = ToPdfiumSymbol(fns.loadDocument);
|
|
109
|
+
env->symbols["FPDF_CloseDocument"] = ToPdfiumSymbol(fns.closeDocument);
|
|
110
|
+
env->symbols["FPDFBookmark_GetFirstChild"] = ToPdfiumSymbol(fns.bookmarkGetFirstChild);
|
|
111
|
+
env->symbols["FPDFBookmark_GetNextSibling"] = ToPdfiumSymbol(fns.bookmarkGetNextSibling);
|
|
112
|
+
env->symbols["FPDFBookmark_GetTitle"] = ToPdfiumSymbol(fns.bookmarkGetTitle);
|
|
113
|
+
env->symbols["FPDFBookmark_GetDest"] = ToPdfiumSymbol(fns.bookmarkGetDest);
|
|
114
|
+
env->symbols["FPDFDest_GetPageIndex"] = ToPdfiumSymbol(fns.destGetPageIndex);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
[[noreturn]] void Fail(const char *expression, const char *file, int line) {
|
|
118
|
+
std::cerr << file << ":" << line << ": assertion failed: " << expression << std::endl;
|
|
119
|
+
std::exit(1);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
#define EXPECT_TRUE(condition) \
|
|
123
|
+
do { \
|
|
124
|
+
if (!(condition)) { \
|
|
125
|
+
Fail(#condition, __FILE__, __LINE__); \
|
|
126
|
+
} \
|
|
127
|
+
} while (false)
|
|
128
|
+
|
|
129
|
+
#define EXPECT_FALSE(condition) EXPECT_TRUE(!(condition))
|
|
130
|
+
|
|
131
|
+
#define EXPECT_EQ(expected, actual) \
|
|
132
|
+
do { \
|
|
133
|
+
const auto expected_value = (expected); \
|
|
134
|
+
const auto actual_value = (actual); \
|
|
135
|
+
if (!(expected_value == actual_value)) { \
|
|
136
|
+
std::cerr << __FILE__ << ":" << __LINE__ \
|
|
137
|
+
<< ": expected equality for " << #expected << " and " << #actual << std::endl; \
|
|
138
|
+
std::exit(1); \
|
|
139
|
+
} \
|
|
140
|
+
} while (false)
|
|
141
|
+
|
|
142
|
+
void TestFullSymbolTableReturnsSupported() {
|
|
143
|
+
FakeLoaderEnv env = {};
|
|
144
|
+
const PdfiumOutlineFns complete_fns = MakeCompleteOutlineFns();
|
|
145
|
+
InstallSymbols(&env, complete_fns);
|
|
146
|
+
|
|
147
|
+
OutlineLoaderState state = {};
|
|
148
|
+
const OutlineLoaderDeps deps = MakeDeps(&env);
|
|
149
|
+
|
|
150
|
+
const OutlineLoadAttemptResult result = EnsureOutlinePdfiumLoaded(&state, &deps);
|
|
151
|
+
|
|
152
|
+
EXPECT_TRUE(result.loaded);
|
|
153
|
+
EXPECT_FALSE(result.transitioned_to_unsupported);
|
|
154
|
+
EXPECT_EQ(OutlineLoadState::kSupported, state.load_state);
|
|
155
|
+
EXPECT_TRUE(state.handle != nullptr);
|
|
156
|
+
EXPECT_TRUE(HasCompleteOutlineFns(state.fns));
|
|
157
|
+
EXPECT_EQ(1, env.dlopen_calls);
|
|
158
|
+
EXPECT_EQ(7, env.dlsym_calls);
|
|
159
|
+
EXPECT_EQ(0, env.dlclose_calls);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
void TestMissingLibraryReturnsUnsupported() {
|
|
163
|
+
FakeLoaderEnv env = {};
|
|
164
|
+
env.library_handle = nullptr;
|
|
165
|
+
|
|
166
|
+
OutlineLoaderState state = {};
|
|
167
|
+
const OutlineLoaderDeps deps = MakeDeps(&env);
|
|
168
|
+
|
|
169
|
+
const OutlineLoadAttemptResult result = EnsureOutlinePdfiumLoaded(&state, &deps);
|
|
170
|
+
|
|
171
|
+
EXPECT_FALSE(result.loaded);
|
|
172
|
+
EXPECT_TRUE(result.transitioned_to_unsupported);
|
|
173
|
+
EXPECT_EQ(OutlineLoadState::kUnsupported, state.load_state);
|
|
174
|
+
EXPECT_TRUE(state.handle == nullptr);
|
|
175
|
+
EXPECT_FALSE(HasCompleteOutlineFns(state.fns));
|
|
176
|
+
EXPECT_EQ(1, env.dlopen_calls);
|
|
177
|
+
EXPECT_EQ(0, env.dlsym_calls);
|
|
178
|
+
EXPECT_EQ(0, env.dlclose_calls);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
void TestMissingSymbolReturnsUnsupportedAndClosesHandle() {
|
|
182
|
+
FakeLoaderEnv env = {};
|
|
183
|
+
const PdfiumOutlineFns partial_fns = MakeCompleteOutlineFns();
|
|
184
|
+
InstallSymbols(&env, partial_fns);
|
|
185
|
+
env.symbols.erase("FPDFBookmark_GetDest");
|
|
186
|
+
|
|
187
|
+
OutlineLoaderState state = {};
|
|
188
|
+
const OutlineLoaderDeps deps = MakeDeps(&env);
|
|
189
|
+
|
|
190
|
+
const OutlineLoadAttemptResult result = EnsureOutlinePdfiumLoaded(&state, &deps);
|
|
191
|
+
|
|
192
|
+
EXPECT_FALSE(result.loaded);
|
|
193
|
+
EXPECT_TRUE(result.transitioned_to_unsupported);
|
|
194
|
+
EXPECT_EQ(OutlineLoadState::kUnsupported, state.load_state);
|
|
195
|
+
EXPECT_TRUE(state.handle == nullptr);
|
|
196
|
+
EXPECT_FALSE(HasCompleteOutlineFns(state.fns));
|
|
197
|
+
EXPECT_EQ(1, env.dlopen_calls);
|
|
198
|
+
EXPECT_EQ(7, env.dlsym_calls);
|
|
199
|
+
EXPECT_EQ(1, env.dlclose_calls);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
void TestRepeatedCallsAfterFailureStayUnsupported() {
|
|
203
|
+
FakeLoaderEnv env = {};
|
|
204
|
+
const PdfiumOutlineFns partial_fns = MakeCompleteOutlineFns();
|
|
205
|
+
InstallSymbols(&env, partial_fns);
|
|
206
|
+
env.symbols.erase("FPDFDest_GetPageIndex");
|
|
207
|
+
|
|
208
|
+
OutlineLoaderState state = {};
|
|
209
|
+
const OutlineLoaderDeps deps = MakeDeps(&env);
|
|
210
|
+
|
|
211
|
+
const OutlineLoadAttemptResult first = EnsureOutlinePdfiumLoaded(&state, &deps);
|
|
212
|
+
EXPECT_FALSE(first.loaded);
|
|
213
|
+
EXPECT_TRUE(first.transitioned_to_unsupported);
|
|
214
|
+
EXPECT_EQ(OutlineLoadState::kUnsupported, state.load_state);
|
|
215
|
+
EXPECT_TRUE(state.handle == nullptr);
|
|
216
|
+
EXPECT_FALSE(HasCompleteOutlineFns(state.fns));
|
|
217
|
+
EXPECT_EQ(1, env.dlopen_calls);
|
|
218
|
+
EXPECT_EQ(7, env.dlsym_calls);
|
|
219
|
+
EXPECT_EQ(1, env.dlclose_calls);
|
|
220
|
+
|
|
221
|
+
const OutlineLoadAttemptResult second = EnsureOutlinePdfiumLoaded(&state, &deps);
|
|
222
|
+
EXPECT_FALSE(second.loaded);
|
|
223
|
+
EXPECT_FALSE(second.transitioned_to_unsupported);
|
|
224
|
+
EXPECT_EQ(OutlineLoadState::kUnsupported, state.load_state);
|
|
225
|
+
EXPECT_TRUE(state.handle == nullptr);
|
|
226
|
+
EXPECT_FALSE(HasCompleteOutlineFns(state.fns));
|
|
227
|
+
EXPECT_EQ(1, env.dlopen_calls);
|
|
228
|
+
EXPECT_EQ(7, env.dlsym_calls);
|
|
229
|
+
EXPECT_EQ(1, env.dlclose_calls);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
} // namespace
|
|
233
|
+
|
|
234
|
+
int main() {
|
|
235
|
+
TestFullSymbolTableReturnsSupported();
|
|
236
|
+
TestMissingLibraryReturnsUnsupported();
|
|
237
|
+
TestMissingSymbolReturnsUnsupportedAndClosesHandle();
|
|
238
|
+
TestRepeatedCallsAfterFailureStayUnsupported();
|
|
239
|
+
return 0;
|
|
240
|
+
}
|
|
@@ -4,9 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
#include <algorithm>
|
|
6
6
|
#include <cmath>
|
|
7
|
+
#include <limits>
|
|
7
8
|
#include <string>
|
|
8
9
|
#include <vector>
|
|
9
10
|
|
|
11
|
+
#include "papyrus_text_word.h"
|
|
12
|
+
|
|
10
13
|
#define LOG_TAG "PapyrusText"
|
|
11
14
|
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
|
|
12
15
|
|
|
@@ -212,7 +215,8 @@ static jobjectArray SearchDocument(JNIEnv *env, FPDF_DOCUMENT doc, int pageCount
|
|
|
212
215
|
return result;
|
|
213
216
|
}
|
|
214
217
|
|
|
215
|
-
static jobject BuildSelection(JNIEnv *env, FPDF_DOCUMENT doc, int pageIndex, double normX, double normY, double normW, double normH
|
|
218
|
+
static jobject BuildSelection(JNIEnv *env, FPDF_DOCUMENT doc, int pageIndex, double normX, double normY, double normW, double normH,
|
|
219
|
+
double startNormX, double startNormY, double endNormX, double endNormY) {
|
|
216
220
|
if (!doc || pageIndex < 0 || normW <= 0 || normH <= 0) return nullptr;
|
|
217
221
|
|
|
218
222
|
jclass selectionClass = env->FindClass("com/papyrus/engine/PapyrusTextSelection");
|
|
@@ -241,11 +245,23 @@ static jobject BuildSelection(JNIEnv *env, FPDF_DOCUMENT doc, int pageIndex, dou
|
|
|
241
245
|
double rectRight = rectLeft + (normW * pageWidth);
|
|
242
246
|
double rectBottom = rectTop - (normH * pageHeight);
|
|
243
247
|
|
|
248
|
+
std::vector<char16_t> pageChars;
|
|
249
|
+
|
|
250
|
+
struct CharBox {
|
|
251
|
+
int index;
|
|
252
|
+
double left;
|
|
253
|
+
double right;
|
|
254
|
+
double top;
|
|
255
|
+
double bottom;
|
|
256
|
+
unsigned short value;
|
|
257
|
+
};
|
|
258
|
+
|
|
244
259
|
struct LineRect {
|
|
245
260
|
double left;
|
|
246
261
|
double right;
|
|
247
262
|
double top;
|
|
248
263
|
double bottom;
|
|
264
|
+
std::vector<int> chars;
|
|
249
265
|
};
|
|
250
266
|
|
|
251
267
|
std::vector<LineRect> lines;
|
|
@@ -258,8 +274,8 @@ static jobject BuildSelection(JNIEnv *env, FPDF_DOCUMENT doc, int pageIndex, dou
|
|
|
258
274
|
return nullptr;
|
|
259
275
|
}
|
|
260
276
|
|
|
261
|
-
const double lineTolerance = 2.5;
|
|
262
277
|
std::vector<unsigned short> charBuffer(2, 0);
|
|
278
|
+
std::vector<CharBox> chars;
|
|
263
279
|
|
|
264
280
|
for (int i = 0; i < charCount; i++) {
|
|
265
281
|
double cLeft = 0;
|
|
@@ -268,33 +284,155 @@ static jobject BuildSelection(JNIEnv *env, FPDF_DOCUMENT doc, int pageIndex, dou
|
|
|
268
284
|
double cBottom = 0;
|
|
269
285
|
if (!g_fns.textGetCharBox(textPage, i, &cLeft, &cRight, &cBottom, &cTop)) continue;
|
|
270
286
|
|
|
271
|
-
bool intersects = !(cRight < rectLeft || cLeft > rectRight || cTop < rectBottom || cBottom > rectTop);
|
|
272
|
-
if (!intersects) continue;
|
|
273
|
-
|
|
274
287
|
int written = g_fns.textGetText(textPage, i, 1, charBuffer.data());
|
|
275
|
-
if (written
|
|
276
|
-
|
|
288
|
+
if (written <= 0 || charBuffer[0] == 0) continue;
|
|
289
|
+
|
|
290
|
+
chars.push_back({i, cLeft, cRight, cTop, cBottom, charBuffer[0]});
|
|
291
|
+
pageChars.push_back(static_cast<char16_t>(charBuffer[0]));
|
|
292
|
+
int lineIndex = -1;
|
|
293
|
+
const double charHeight = std::max(0.001, cTop - cBottom);
|
|
294
|
+
for (int line = 0; line < static_cast<int>(lines.size()); line++) {
|
|
295
|
+
const auto &candidate = lines[line];
|
|
296
|
+
const double overlap = std::min(candidate.top, cTop) - std::max(candidate.bottom, cBottom);
|
|
297
|
+
const double candidateHeight = std::max(0.001, candidate.top - candidate.bottom);
|
|
298
|
+
const double minHeight = std::min(charHeight, candidateHeight);
|
|
299
|
+
const double centerDistance = std::abs((candidate.top + candidate.bottom) / 2.0 - (cTop + cBottom) / 2.0);
|
|
300
|
+
if (overlap >= minHeight * 0.2 || centerDistance <= minHeight * 0.5) {
|
|
301
|
+
lineIndex = line;
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
277
304
|
}
|
|
305
|
+
if (lineIndex < 0) {
|
|
306
|
+
lines.push_back({cLeft, cRight, cTop, cBottom, {}});
|
|
307
|
+
lineIndex = static_cast<int>(lines.size()) - 1;
|
|
308
|
+
}
|
|
309
|
+
lines[lineIndex].left = std::min(lines[lineIndex].left, cLeft);
|
|
310
|
+
lines[lineIndex].right = std::max(lines[lineIndex].right, cRight);
|
|
311
|
+
lines[lineIndex].top = std::max(lines[lineIndex].top, cTop);
|
|
312
|
+
lines[lineIndex].bottom = std::min(lines[lineIndex].bottom, cBottom);
|
|
313
|
+
lines[lineIndex].chars.push_back(static_cast<int>(chars.size()) - 1);
|
|
314
|
+
}
|
|
278
315
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
316
|
+
std::sort(lines.begin(), lines.end(), [](const LineRect &a, const LineRect &b) {
|
|
317
|
+
if (a.top != b.top) return a.top > b.top;
|
|
318
|
+
return a.left < b.left;
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
const bool hasEndpoints = std::isfinite(startNormX) && std::isfinite(startNormY) &&
|
|
322
|
+
std::isfinite(endNormX) && std::isfinite(endNormY) &&
|
|
323
|
+
startNormX >= 0 && startNormY >= 0 && endNormX >= 0 && endNormY >= 0;
|
|
324
|
+
std::vector<bool> selected(chars.size(), false);
|
|
325
|
+
if (hasEndpoints && !lines.empty()) {
|
|
326
|
+
const double startX = std::max(0.0, std::min(1.0, startNormX)) * pageWidth;
|
|
327
|
+
const double endX = std::max(0.0, std::min(1.0, endNormX)) * pageWidth;
|
|
328
|
+
const double startY = std::max(0.0, std::min(1.0, startNormY)) * pageHeight;
|
|
329
|
+
const double endY = std::max(0.0, std::min(1.0, endNormY)) * pageHeight;
|
|
330
|
+
|
|
331
|
+
const bool isPointSelection =
|
|
332
|
+
startNormX == endNormX && startNormY == endNormY;
|
|
333
|
+
if (isPointSelection) {
|
|
334
|
+
const double pointX = startX;
|
|
335
|
+
const double pointY = pageHeight - startY;
|
|
336
|
+
int pointChar = -1;
|
|
337
|
+
double bestDistance = std::numeric_limits<double>::max();
|
|
338
|
+
for (int i = 0; i < static_cast<int>(chars.size()); i++) {
|
|
339
|
+
const auto &character = chars[i];
|
|
340
|
+
const bool inside = pointX >= character.left && pointX <= character.right &&
|
|
341
|
+
pointY >= character.bottom && pointY <= character.top;
|
|
342
|
+
if (inside) {
|
|
343
|
+
pointChar = i;
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
const double dx = std::max({character.left - pointX, pointX - character.right, 0.0});
|
|
347
|
+
const double dy = std::max({character.bottom - pointY, pointY - character.top, 0.0});
|
|
348
|
+
const double distance = dx * dx + dy * dy;
|
|
349
|
+
if (distance < bestDistance) {
|
|
350
|
+
bestDistance = distance;
|
|
351
|
+
pointChar = i;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (pointChar >= 0) {
|
|
355
|
+
const auto wordRange = papyrus::ResolveWordRange(pageChars, pointChar);
|
|
356
|
+
for (int i = wordRange.first; i < wordRange.second; i++) {
|
|
357
|
+
selected[i] = true;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
} else {
|
|
361
|
+
auto findLine = [&](double pdfY) { int best = 0;
|
|
362
|
+
double bestDistance = std::numeric_limits<double>::max();
|
|
363
|
+
for (int i = 0; i < static_cast<int>(lines.size()); i++) {
|
|
364
|
+
const auto &line = lines[i];
|
|
365
|
+
double distance = 0.0;
|
|
366
|
+
if (pdfY > line.top) distance = pdfY - line.top;
|
|
367
|
+
else if (pdfY < line.bottom) distance = line.bottom - pdfY;
|
|
368
|
+
if (distance < bestDistance) {
|
|
369
|
+
best = i;
|
|
370
|
+
bestDistance = distance;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return best;
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const int startLine = findLine(pageHeight - startY);
|
|
377
|
+
const int endLine = findLine(pageHeight - endY);
|
|
378
|
+
const bool forward = startLine < endLine || (startLine == endLine && startX <= endX);
|
|
379
|
+
const int firstLine = std::min(startLine, endLine);
|
|
380
|
+
const int lastLine = std::max(startLine, endLine);
|
|
381
|
+
|
|
382
|
+
for (int lineIndex = firstLine; lineIndex <= lastLine; lineIndex++) {
|
|
383
|
+
const auto &line = lines[lineIndex];
|
|
384
|
+
for (int charIndex : line.chars) {
|
|
385
|
+
const auto &character = chars[charIndex];
|
|
386
|
+
bool include = true;
|
|
387
|
+
if (startLine == endLine) {
|
|
388
|
+
const double left = std::min(startX, endX);
|
|
389
|
+
const double right = std::max(startX, endX);
|
|
390
|
+
include = character.right >= left && character.left <= right;
|
|
391
|
+
} else if (forward) {
|
|
392
|
+
if (lineIndex == startLine) include = character.right >= startX;
|
|
393
|
+
else if (lineIndex == endLine) include = character.left <= endX;
|
|
394
|
+
} else {
|
|
395
|
+
if (lineIndex == startLine) include = character.left <= startX;
|
|
396
|
+
else if (lineIndex == endLine) include = character.right >= endX;
|
|
397
|
+
}
|
|
398
|
+
selected[charIndex] = include;
|
|
399
|
+
}
|
|
288
400
|
}
|
|
289
401
|
}
|
|
290
|
-
|
|
291
|
-
|
|
402
|
+
} else {
|
|
403
|
+
for (int charIndex = 0; charIndex < static_cast<int>(chars.size()); charIndex++) {
|
|
404
|
+
const auto &character = chars[charIndex];
|
|
405
|
+
selected[charIndex] = !(character.right < rectLeft || character.left > rectRight ||
|
|
406
|
+
character.top < rectBottom || character.bottom > rectTop);
|
|
292
407
|
}
|
|
293
408
|
}
|
|
294
409
|
|
|
295
|
-
std::vector<
|
|
296
|
-
rects.reserve(lines.size() * 4);
|
|
410
|
+
std::vector<LineRect> selectedLines;
|
|
297
411
|
for (const auto &line : lines) {
|
|
412
|
+
LineRect selectedLine = {0, 0, 0, 0, {}};
|
|
413
|
+
for (int charIndex : line.chars) {
|
|
414
|
+
if (!selected[charIndex]) continue;
|
|
415
|
+
const auto &character = chars[charIndex];
|
|
416
|
+
if (selectedLine.chars.empty()) {
|
|
417
|
+
selectedLine.left = character.left;
|
|
418
|
+
selectedLine.right = character.right;
|
|
419
|
+
selectedLine.top = character.top;
|
|
420
|
+
selectedLine.bottom = character.bottom;
|
|
421
|
+
} else {
|
|
422
|
+
selectedLine.left = std::min(selectedLine.left, character.left);
|
|
423
|
+
selectedLine.right = std::max(selectedLine.right, character.right);
|
|
424
|
+
selectedLine.top = std::max(selectedLine.top, character.top);
|
|
425
|
+
selectedLine.bottom = std::min(selectedLine.bottom, character.bottom);
|
|
426
|
+
}
|
|
427
|
+
selectedLine.chars.push_back(charIndex);
|
|
428
|
+
selectedText.push_back(static_cast<char16_t>(character.value));
|
|
429
|
+
}
|
|
430
|
+
if (!selectedLine.chars.empty()) selectedLines.push_back(selectedLine);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
std::vector<jfloat> rects;
|
|
434
|
+
rects.reserve(selectedLines.size() * 4);
|
|
435
|
+
for (const auto &line : selectedLines) {
|
|
298
436
|
float x = static_cast<float>(line.left / pageWidth);
|
|
299
437
|
float y = static_cast<float>((pageHeight - line.top) / pageHeight);
|
|
300
438
|
float w = static_cast<float>((line.right - line.left) / pageWidth);
|
|
@@ -382,16 +520,16 @@ Java_com_papyrus_engine_PapyrusTextSearch_nativeSearchFile(JNIEnv *env, jclass,
|
|
|
382
520
|
}
|
|
383
521
|
|
|
384
522
|
extern "C" JNIEXPORT jobject JNICALL
|
|
385
|
-
Java_com_papyrus_engine_PapyrusTextSelect_nativeSelectText(JNIEnv *env, jclass, jlong docPtr, jint pageIndex, jfloat x, jfloat y, jfloat width, jfloat height) {
|
|
523
|
+
Java_com_papyrus_engine_PapyrusTextSelect_nativeSelectText(JNIEnv *env, jclass, jlong docPtr, jint pageIndex, jfloat x, jfloat y, jfloat width, jfloat height, jfloat startX, jfloat startY, jfloat endX, jfloat endY) {
|
|
386
524
|
if (!LoadPdfium()) return nullptr;
|
|
387
525
|
if (!docPtr) return nullptr;
|
|
388
526
|
|
|
389
527
|
FPDF_DOCUMENT doc = reinterpret_cast<FPDF_DOCUMENT>(docPtr);
|
|
390
|
-
return BuildSelection(env, doc, pageIndex, x, y, width, height);
|
|
528
|
+
return BuildSelection(env, doc, pageIndex, x, y, width, height, startX, startY, endX, endY);
|
|
391
529
|
}
|
|
392
530
|
|
|
393
531
|
extern "C" JNIEXPORT jobject JNICALL
|
|
394
|
-
Java_com_papyrus_engine_PapyrusTextSelect_nativeSelectTextFile(JNIEnv *env, jclass, jstring filePath, jint pageIndex, jfloat x, jfloat y, jfloat width, jfloat height) {
|
|
532
|
+
Java_com_papyrus_engine_PapyrusTextSelect_nativeSelectTextFile(JNIEnv *env, jclass, jstring filePath, jint pageIndex, jfloat x, jfloat y, jfloat width, jfloat height, jfloat startX, jfloat startY, jfloat endX, jfloat endY) {
|
|
395
533
|
if (!LoadPdfium()) return nullptr;
|
|
396
534
|
if (!filePath) return nullptr;
|
|
397
535
|
|
|
@@ -402,7 +540,7 @@ Java_com_papyrus_engine_PapyrusTextSelect_nativeSelectTextFile(JNIEnv *env, jcla
|
|
|
402
540
|
env->ReleaseStringUTFChars(filePath, path);
|
|
403
541
|
if (!doc) return nullptr;
|
|
404
542
|
|
|
405
|
-
jobject selection = BuildSelection(env, doc, pageIndex, x, y, width, height);
|
|
543
|
+
jobject selection = BuildSelection(env, doc, pageIndex, x, y, width, height, startX, startY, endX, endY);
|
|
406
544
|
g_fns.closeDocument(doc);
|
|
407
545
|
return selection;
|
|
408
546
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#include "papyrus_text_word.h"
|
|
2
|
+
|
|
3
|
+
namespace papyrus {
|
|
4
|
+
|
|
5
|
+
bool IsWordCharacter(char16_t value) {
|
|
6
|
+
if (value >= u'0' && value <= u'9') return true;
|
|
7
|
+
if (value >= u'A' && value <= u'Z') return true;
|
|
8
|
+
if (value >= u'a' && value <= u'z') return true;
|
|
9
|
+
if (value == u'\'' || value == 0x2019) return true;
|
|
10
|
+
if (value == u'-') return true;
|
|
11
|
+
if (value >= 0x00C0 && value <= 0x00D6) return true;
|
|
12
|
+
if (value >= 0x00D8 && value <= 0x00F6) return true;
|
|
13
|
+
if (value >= 0x00F8 && value <= 0x00FF) return true;
|
|
14
|
+
if (value >= 0x0100 && value <= 0x017F) return true;
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
std::pair<int, int> ResolveWordRange(const std::vector<char16_t> &text,
|
|
19
|
+
int index) {
|
|
20
|
+
const int size = static_cast<int>(text.size());
|
|
21
|
+
if (index < 0 || index >= size) return {index, index};
|
|
22
|
+
if (!IsWordCharacter(text[index])) return {index, index};
|
|
23
|
+
|
|
24
|
+
int start = index;
|
|
25
|
+
while (start > 0 && IsWordCharacter(text[start - 1])) start -= 1;
|
|
26
|
+
|
|
27
|
+
int end = index + 1;
|
|
28
|
+
while (end < size && IsWordCharacter(text[end])) end += 1;
|
|
29
|
+
|
|
30
|
+
return {start, end};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
} // namespace papyrus
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#ifndef PAPYRUS_TEXT_WORD_H
|
|
2
|
+
#define PAPYRUS_TEXT_WORD_H
|
|
3
|
+
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <utility>
|
|
6
|
+
#include <vector>
|
|
7
|
+
|
|
8
|
+
namespace papyrus {
|
|
9
|
+
|
|
10
|
+
bool IsWordCharacter(char16_t value);
|
|
11
|
+
|
|
12
|
+
// Returns [start, end) of the word that contains |index|. When |index| is out
|
|
13
|
+
// of bounds or not a word character, returns {index, index}.
|
|
14
|
+
std::pair<int, int> ResolveWordRange(const std::vector<char16_t> &text,
|
|
15
|
+
int index);
|
|
16
|
+
|
|
17
|
+
} // namespace papyrus
|
|
18
|
+
|
|
19
|
+
#endif // PAPYRUS_TEXT_WORD_H
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#include "papyrus_text_word.h"
|
|
2
|
+
|
|
3
|
+
#include <iostream>
|
|
4
|
+
#include <vector>
|
|
5
|
+
|
|
6
|
+
namespace {
|
|
7
|
+
|
|
8
|
+
int failures = 0;
|
|
9
|
+
|
|
10
|
+
void ExpectTrue(bool value, const char *name) {
|
|
11
|
+
if (!value) {
|
|
12
|
+
std::cerr << "FAIL: " << name << std::endl;
|
|
13
|
+
failures += 1;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
void ExpectRange(const std::vector<char16_t> &text, int index, int expectedStart,
|
|
18
|
+
int expectedEnd, const char *name) {
|
|
19
|
+
const auto range = papyrus::ResolveWordRange(text, index);
|
|
20
|
+
if (range.first != expectedStart || range.second != expectedEnd) {
|
|
21
|
+
std::cerr << "FAIL: " << name << " expected [" << expectedStart << ", "
|
|
22
|
+
<< expectedEnd << ") got [" << range.first << ", " << range.second
|
|
23
|
+
<< ")" << std::endl;
|
|
24
|
+
failures += 1;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
} // namespace
|
|
29
|
+
|
|
30
|
+
int main() {
|
|
31
|
+
ExpectTrue(papyrus::IsWordCharacter(u'a'), "ascii letter");
|
|
32
|
+
ExpectTrue(papyrus::IsWordCharacter(u'Z'), "ascii upper");
|
|
33
|
+
ExpectTrue(papyrus::IsWordCharacter(u'7'), "digit");
|
|
34
|
+
ExpectTrue(papyrus::IsWordCharacter(0x00E7), "c-cedilla");
|
|
35
|
+
ExpectTrue(papyrus::IsWordCharacter(0x00E3), "a-tilde");
|
|
36
|
+
ExpectTrue(papyrus::IsWordCharacter(u'\''), "apostrophe");
|
|
37
|
+
ExpectTrue(papyrus::IsWordCharacter(0x2019), "curly apostrophe");
|
|
38
|
+
ExpectTrue(papyrus::IsWordCharacter(u'-'), "hyphen");
|
|
39
|
+
ExpectTrue(!papyrus::IsWordCharacter(u' '), "space is not a word char");
|
|
40
|
+
ExpectTrue(!papyrus::IsWordCharacter(u'.'), "dot is not a word char");
|
|
41
|
+
ExpectTrue(!papyrus::IsWordCharacter(u'\n'), "newline is not a word char");
|
|
42
|
+
|
|
43
|
+
const std::vector<char16_t> simple = {u'd', u'i', u'n', u'a'};
|
|
44
|
+
ExpectRange(simple, 0, 0, 4, "word from first char");
|
|
45
|
+
ExpectRange(simple, 3, 0, 4, "word from last char");
|
|
46
|
+
ExpectRange(simple, -1, -1, -1, "out of bounds start");
|
|
47
|
+
ExpectRange(simple, 4, 4, 4, "out of bounds end");
|
|
48
|
+
|
|
49
|
+
const std::vector<char16_t> accented = {u'c', u'o', u'm', u'p', u'i',
|
|
50
|
+
u'l', u'a', 0x00E7, 0x00E3, u'o'};
|
|
51
|
+
ExpectRange(accented, 7, 0, 10, "accented word");
|
|
52
|
+
|
|
53
|
+
const std::vector<char16_t> hyphenated = {u'j', u'u', u's', u't', u'-',
|
|
54
|
+
u'i', u'n', u'-', u't', u'i',
|
|
55
|
+
u'm', u'e'};
|
|
56
|
+
ExpectRange(hyphenated, 5, 0, 12, "hyphenated word");
|
|
57
|
+
ExpectRange(hyphenated, 4, 0, 12, "hyphen is word char");
|
|
58
|
+
|
|
59
|
+
const std::vector<char16_t> apostrophe = {u'l', u'\'', 0x00E9, u't'};
|
|
60
|
+
ExpectRange(apostrophe, 1, 0, 4, "apostrophe joins the word");
|
|
61
|
+
|
|
62
|
+
const std::vector<char16_t> sentence = {u'a', u' ', u'b', u'.', u'c'};
|
|
63
|
+
ExpectRange(sentence, 1, 1, 1, "space yields empty range");
|
|
64
|
+
ExpectRange(sentence, 3, 3, 3, "dot yields empty range");
|
|
65
|
+
|
|
66
|
+
if (failures > 0) {
|
|
67
|
+
std::cerr << failures << " failure(s)" << std::endl;
|
|
68
|
+
return 1;
|
|
69
|
+
}
|
|
70
|
+
std::cout << "papyrus_text_word_test: OK" << std::endl;
|
|
71
|
+
return 0;
|
|
72
|
+
}
|