@papyrus-sdk/engine-native 0.2.27 → 0.2.29
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 +2 -0
- package/android/src/main/cpp/papyrus_outline_loader_test.cpp +240 -0
- package/android/src/main/cpp/papyrus_text_word_test.cpp +72 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeTextModel.java +105 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +6 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/ios/PapyrusNativeEngine.m +64 -0
- package/ios/PapyrusPdfDocumentView.h +3 -0
- package/ios/PapyrusPdfDocumentView.m +165 -128
- package/ios/PapyrusPdfDocumentViewManager.m +2 -0
- package/package.json +5 -5
package/android/build.gradle
CHANGED
|
@@ -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
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -76,12 +76,117 @@ final class PapyrusNativeTextModel {
|
|
|
76
76
|
if ((charset == StandardCharsets.UTF_16LE || charset == StandardCharsets.UTF_16BE) && (bytes.length - offset) % 2 != 0) {
|
|
77
77
|
throw new IllegalArgumentException("Incomplete UTF-16 code unit");
|
|
78
78
|
}
|
|
79
|
+
try {
|
|
80
|
+
return decodeStrict(bytes, offset, charset);
|
|
81
|
+
} catch (CharacterCodingException utf8Error) {
|
|
82
|
+
if (charset != StandardCharsets.UTF_8 || offset != 0) throw utf8Error;
|
|
83
|
+
String legacy = decodeLegacy(bytes);
|
|
84
|
+
if (legacy == null) throw utf8Error;
|
|
85
|
+
return legacy;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private static final java.util.Set<String> CJK_ENCODINGS = new java.util.HashSet<>(java.util.Arrays.asList(
|
|
90
|
+
"GB18030", "Big5", "Shift_JIS", "EUC-JP", "EUC-KR"));
|
|
91
|
+
private static final java.util.Set<String> LATIN_ENCODINGS = new java.util.HashSet<>(java.util.Arrays.asList(
|
|
92
|
+
"ISO-8859-1", "windows-1252"));
|
|
93
|
+
private static final int MIN_DETECTION_CONFIDENCE = 10;
|
|
94
|
+
private static final int DETECTION_SAMPLE_BYTES = 64 * 1024;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Text that is not UTF-8 and has no BOM (old Chinese, Japanese, Korean or
|
|
98
|
+
* Windows TXT files). The platform's ICU detector picks the encoding; Latin
|
|
99
|
+
* encodings are refused when the bytes look like double-byte CJK, and GB18030
|
|
100
|
+
* is the fallback when no detector is available. Returns null when nothing fits.
|
|
101
|
+
*/
|
|
102
|
+
static String decodeLegacy(byte[] bytes) {
|
|
103
|
+
boolean cjkPairs = looksLikeDoubleByteCjk(bytes);
|
|
104
|
+
for (String name : detectEncodings(bytes)) {
|
|
105
|
+
try {
|
|
106
|
+
if (CJK_ENCODINGS.contains(name)) return decodeStrict(bytes, 0, Charset.forName(name));
|
|
107
|
+
if (LATIN_ENCODINGS.contains(name) && !cjkPairs) {
|
|
108
|
+
try {
|
|
109
|
+
return decodeStrict(bytes, 0, Charset.forName("windows-1252"));
|
|
110
|
+
} catch (CharacterCodingException undefinedInWindows1252) {
|
|
111
|
+
return decodeStrict(bytes, 0, StandardCharsets.ISO_8859_1);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} catch (CharacterCodingException | IllegalArgumentException ignored) {
|
|
115
|
+
// Try the next candidate.
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (!cjkPairs) return null;
|
|
119
|
+
try {
|
|
120
|
+
return decodeStrict(bytes, 0, Charset.forName("GB18030"));
|
|
121
|
+
} catch (CharacterCodingException | IllegalArgumentException gbError) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Encoding names from ICU's CharsetDetector, most confident first. Loaded by
|
|
128
|
+
* reflection: android.icu exists from API 24, and plain JVM tests use ICU4J
|
|
129
|
+
* (same API). Empty when neither is available.
|
|
130
|
+
*/
|
|
131
|
+
static List<String> detectEncodings(byte[] bytes) {
|
|
132
|
+
List<String> names = new ArrayList<>();
|
|
133
|
+
byte[] sample = bytes.length > DETECTION_SAMPLE_BYTES ? java.util.Arrays.copyOf(bytes, DETECTION_SAMPLE_BYTES) : bytes;
|
|
134
|
+
for (String className : new String[] {"android.icu.text.CharsetDetector", "com.ibm.icu.text.CharsetDetector"}) {
|
|
135
|
+
try {
|
|
136
|
+
Class<?> detectorClass = Class.forName(className);
|
|
137
|
+
Object detector = detectorClass.getConstructor().newInstance();
|
|
138
|
+
detectorClass.getMethod("setText", byte[].class).invoke(detector, (Object) sample);
|
|
139
|
+
Object[] matches = (Object[]) detectorClass.getMethod("detectAll").invoke(detector);
|
|
140
|
+
if (matches == null) return names;
|
|
141
|
+
List<String> found = new ArrayList<>();
|
|
142
|
+
for (Object match : matches) {
|
|
143
|
+
int confidence = (Integer) match.getClass().getMethod("getConfidence").invoke(match);
|
|
144
|
+
if (confidence < MIN_DETECTION_CONFIDENCE) continue;
|
|
145
|
+
found.add((String) match.getClass().getMethod("getName").invoke(match));
|
|
146
|
+
}
|
|
147
|
+
return found;
|
|
148
|
+
} catch (ReflectiveOperationException | RuntimeException | LinkageError unavailable) {
|
|
149
|
+
// Missing, or a "Stub!" android.jar class in JVM tests: try the next implementation.
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return names;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private static String decodeStrict(byte[] bytes, int offset, Charset charset) throws CharacterCodingException {
|
|
79
156
|
return charset.newDecoder()
|
|
80
157
|
.onMalformedInput(CodingErrorAction.REPORT)
|
|
81
158
|
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
|
82
159
|
.decode(ByteBuffer.wrap(bytes, offset, bytes.length - offset)).toString();
|
|
83
160
|
}
|
|
84
161
|
|
|
162
|
+
/**
|
|
163
|
+
* True when the non-ASCII bytes come almost only in high/high pairs, as in
|
|
164
|
+
* GBK/GB18030 Chinese text. Latin-1 text (single accented bytes between
|
|
165
|
+
* ASCII letters) does not qualify, so it keeps failing instead of turning
|
|
166
|
+
* into Chinese mojibake. Mirrors looksLikeDoubleByteCjk in nativeTextModel.ts.
|
|
167
|
+
*/
|
|
168
|
+
static boolean looksLikeDoubleByteCjk(byte[] bytes) {
|
|
169
|
+
int highPairs = 0;
|
|
170
|
+
int other = 0;
|
|
171
|
+
int index = 0;
|
|
172
|
+
while (index < bytes.length) {
|
|
173
|
+
int lead = u(bytes[index]);
|
|
174
|
+
if (lead < 0x80) {
|
|
175
|
+
index += 1;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
int trail = index + 1 < bytes.length ? u(bytes[index + 1]) : -1;
|
|
179
|
+
if (lead >= 0x81 && lead <= 0xfe && trail >= 0x81 && trail <= 0xfe) {
|
|
180
|
+
highPairs += 1;
|
|
181
|
+
index += 2;
|
|
182
|
+
} else {
|
|
183
|
+
other += 1;
|
|
184
|
+
index += trail >= 0x40 && trail <= 0x7e ? 2 : 1;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return highPairs > 0 && highPairs * 5 >= (highPairs + other) * 4;
|
|
188
|
+
}
|
|
189
|
+
|
|
85
190
|
static List<SearchMatch> search(String text, String query) {
|
|
86
191
|
String normalizedQuery = normalize(query).value;
|
|
87
192
|
if (normalizedQuery.isEmpty()) return java.util.Collections.emptyList();
|
|
@@ -84,6 +84,8 @@ public class PapyrusPdfViewerView extends View {
|
|
|
84
84
|
private String annotationColor = "#111827";
|
|
85
85
|
private float inkStrokeWidth = 0.006f;
|
|
86
86
|
private float annotationOpacity = 0.85f;
|
|
87
|
+
/** The most a highlight fill covers the text it marks. */
|
|
88
|
+
static final float HIGHLIGHT_MAX_OPACITY = 0.4f;
|
|
87
89
|
private float zoom = 1.0f;
|
|
88
90
|
private String viewMode = "continuous";
|
|
89
91
|
private float offsetX = 0f;
|
|
@@ -2146,6 +2148,10 @@ public class PapyrusPdfViewerView extends View {
|
|
|
2146
2148
|
for(float x=rLeft+3;x<=rRight;x+=3)wave.lineTo(x,rBottom-2+(((int)((x-rLeft)/3))%2==0?-2:2));
|
|
2147
2149
|
canvas.drawPath(wave,overlayPaint);overlayPaint.setStyle(Paint.Style.FILL);
|
|
2148
2150
|
} else if ("highlight".equals(annotation.markupStyle)) {
|
|
2151
|
+
// A fill over the text: never opaque, whatever opacity the mark was
|
|
2152
|
+
// saved with (marks made after using the pen kept its 1.0).
|
|
2153
|
+
int fillAlpha = Math.round(Math.min(annotation.opacity, HIGHLIGHT_MAX_OPACITY) * 255);
|
|
2154
|
+
overlayPaint.setColor(Color.argb(fillAlpha, Color.red(color), Color.green(color), Color.blue(color)));
|
|
2149
2155
|
canvas.drawRect(rLeft, rTop, rRight, rBottom, overlayPaint);
|
|
2150
2156
|
}
|
|
2151
2157
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -140,6 +140,9 @@ type PapyrusPdfViewerViewProps = ViewProps & {
|
|
|
140
140
|
defineSelectionMode?: "selection" | "single-word";
|
|
141
141
|
annotateLabel?: string;
|
|
142
142
|
annotationHighlightLabel?: string;
|
|
143
|
+
/** iOS PDF: highlight colours (hex) offered in the selection menu. */
|
|
144
|
+
annotationHighlightColors?: readonly string[];
|
|
145
|
+
annotationHighlightColorLabels?: readonly string[];
|
|
143
146
|
annotationUnderlineLabel?: string;
|
|
144
147
|
annotationStrikeoutLabel?: string;
|
|
145
148
|
annotationSquigglyLabel?: string;
|
|
@@ -406,6 +409,9 @@ declare const PapyrusTextDocumentView: react.FunctionComponent<ViewProps & {
|
|
|
406
409
|
defineSelectionMode?: "selection" | "single-word";
|
|
407
410
|
annotateLabel?: string;
|
|
408
411
|
annotationHighlightLabel?: string;
|
|
412
|
+
/** iOS PDF: highlight colours (hex) offered in the selection menu. */
|
|
413
|
+
annotationHighlightColors?: readonly string[];
|
|
414
|
+
annotationHighlightColorLabels?: readonly string[];
|
|
409
415
|
annotationUnderlineLabel?: string;
|
|
410
416
|
annotationStrikeoutLabel?: string;
|
|
411
417
|
annotationSquigglyLabel?: string;
|
|
@@ -597,6 +603,9 @@ declare const PapyrusComicDocumentView: react.FunctionComponent<ViewProps & {
|
|
|
597
603
|
defineSelectionMode?: "selection" | "single-word";
|
|
598
604
|
annotateLabel?: string;
|
|
599
605
|
annotationHighlightLabel?: string;
|
|
606
|
+
/** iOS PDF: highlight colours (hex) offered in the selection menu. */
|
|
607
|
+
annotationHighlightColors?: readonly string[];
|
|
608
|
+
annotationHighlightColorLabels?: readonly string[];
|
|
600
609
|
annotationUnderlineLabel?: string;
|
|
601
610
|
annotationStrikeoutLabel?: string;
|
|
602
611
|
annotationSquigglyLabel?: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -140,6 +140,9 @@ type PapyrusPdfViewerViewProps = ViewProps & {
|
|
|
140
140
|
defineSelectionMode?: "selection" | "single-word";
|
|
141
141
|
annotateLabel?: string;
|
|
142
142
|
annotationHighlightLabel?: string;
|
|
143
|
+
/** iOS PDF: highlight colours (hex) offered in the selection menu. */
|
|
144
|
+
annotationHighlightColors?: readonly string[];
|
|
145
|
+
annotationHighlightColorLabels?: readonly string[];
|
|
143
146
|
annotationUnderlineLabel?: string;
|
|
144
147
|
annotationStrikeoutLabel?: string;
|
|
145
148
|
annotationSquigglyLabel?: string;
|
|
@@ -406,6 +409,9 @@ declare const PapyrusTextDocumentView: react.FunctionComponent<ViewProps & {
|
|
|
406
409
|
defineSelectionMode?: "selection" | "single-word";
|
|
407
410
|
annotateLabel?: string;
|
|
408
411
|
annotationHighlightLabel?: string;
|
|
412
|
+
/** iOS PDF: highlight colours (hex) offered in the selection menu. */
|
|
413
|
+
annotationHighlightColors?: readonly string[];
|
|
414
|
+
annotationHighlightColorLabels?: readonly string[];
|
|
409
415
|
annotationUnderlineLabel?: string;
|
|
410
416
|
annotationStrikeoutLabel?: string;
|
|
411
417
|
annotationSquigglyLabel?: string;
|
|
@@ -597,6 +603,9 @@ declare const PapyrusComicDocumentView: react.FunctionComponent<ViewProps & {
|
|
|
597
603
|
defineSelectionMode?: "selection" | "single-word";
|
|
598
604
|
annotateLabel?: string;
|
|
599
605
|
annotationHighlightLabel?: string;
|
|
606
|
+
/** iOS PDF: highlight colours (hex) offered in the selection menu. */
|
|
607
|
+
annotationHighlightColors?: readonly string[];
|
|
608
|
+
annotationHighlightColorLabels?: readonly string[];
|
|
600
609
|
annotationUnderlineLabel?: string;
|
|
601
610
|
annotationStrikeoutLabel?: string;
|
|
602
611
|
annotationSquigglyLabel?: string;
|