@papyrus-sdk/engine-native 0.2.27 → 0.2.28

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.
@@ -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
+ }
@@ -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;