@papyrus-sdk/engine-native 0.2.16 → 0.2.18
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/README.md +10 -2
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +53 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.kt +45 -0
- package/dist/index.d.mts +23 -2
- package/dist/index.d.ts +23 -2
- package/dist/index.js +201 -44
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +201 -44
- package/dist/index.mjs.map +1 -1
- package/ios/PapyrusNativeEngine.m +38 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Papyrus Engine Native
|
|
2
2
|
|
|
3
3
|
React Native native engine for PDF rendering (PDFKit on iOS, PDFium on Android),
|
|
4
|
-
plus a WebView runtime for EPUB/TXT via `MobileDocumentEngine`.
|
|
4
|
+
plus a WebView runtime for EPUB/TXT/CBZ via `MobileDocumentEngine`.
|
|
5
5
|
|
|
6
6
|
## Install
|
|
7
7
|
|
|
@@ -11,12 +11,20 @@ npm install @papyrus-sdk/engine-native @papyrus-sdk/core @papyrus-sdk/types
|
|
|
11
11
|
|
|
12
12
|
`@papyrus-sdk/core` and `@papyrus-sdk/types` are required peer dependencies.
|
|
13
13
|
|
|
14
|
-
For EPUB/TXT on mobile, also install:
|
|
14
|
+
For EPUB/TXT/CBZ on mobile, also install:
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
17
|
npm install react-native-webview
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
+
CBZ files use the same WebView dependency and are loaded with
|
|
21
|
+
`{ type: 'comic', source: { uri: 'file:///.../book.cbz' } }`. CBR is not part
|
|
22
|
+
of the base mobile runtime because its libarchive worker/WASM payload is
|
|
23
|
+
optional and substantially larger.
|
|
24
|
+
|
|
25
|
+
For CBR, install `@papyrus-sdk/engine-cbr-mobile` and pass its three runtime
|
|
26
|
+
asset URLs through `webViewRuntimeConfig` as shown in that package's README.
|
|
27
|
+
|
|
20
28
|
## Usage
|
|
21
29
|
|
|
22
30
|
```ts
|
|
@@ -4,6 +4,7 @@ import android.content.ContentResolver;
|
|
|
4
4
|
import android.content.Context;
|
|
5
5
|
import android.net.Uri;
|
|
6
6
|
import android.os.ParcelFileDescriptor;
|
|
7
|
+
import android.util.Base64;
|
|
7
8
|
import android.view.View;
|
|
8
9
|
|
|
9
10
|
import com.facebook.react.bridge.Arguments;
|
|
@@ -22,6 +23,7 @@ import com.facebook.react.uimanager.UIManagerModule;
|
|
|
22
23
|
import com.shockwave.pdfium.PdfDocument;
|
|
23
24
|
|
|
24
25
|
import java.io.File;
|
|
26
|
+
import java.io.FileInputStream;
|
|
25
27
|
import java.io.FileOutputStream;
|
|
26
28
|
import java.io.IOException;
|
|
27
29
|
import java.io.InputStream;
|
|
@@ -54,6 +56,57 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
|
|
|
54
56
|
PapyrusEngineStore.destroyEngine(engineId);
|
|
55
57
|
}
|
|
56
58
|
|
|
59
|
+
@ReactMethod
|
|
60
|
+
public void readFileChunk(String uriValue, double offsetValue, int length, Promise promise) {
|
|
61
|
+
executor.execute(() -> {
|
|
62
|
+
try {
|
|
63
|
+
if (length <= 0 || length > 4 * 1024 * 1024) {
|
|
64
|
+
throw new IOException("Invalid file chunk length");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
Uri uri = Uri.parse(uriValue);
|
|
68
|
+
InputStream inputStream;
|
|
69
|
+
if ("file".equalsIgnoreCase(uri.getScheme())) {
|
|
70
|
+
inputStream = new FileInputStream(new File(uri.getPath()));
|
|
71
|
+
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
|
|
72
|
+
ContentResolver resolver = reactContext.getContentResolver();
|
|
73
|
+
inputStream = resolver.openInputStream(uri);
|
|
74
|
+
if (inputStream == null) throw new IOException("Unable to read content URI");
|
|
75
|
+
} else {
|
|
76
|
+
throw new IOException("Unsupported local file URI");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
try (InputStream stream = inputStream) {
|
|
80
|
+
long remaining = Math.max(0L, (long) offsetValue);
|
|
81
|
+
while (remaining > 0) {
|
|
82
|
+
long skipped = stream.skip(remaining);
|
|
83
|
+
if (skipped <= 0) {
|
|
84
|
+
if (stream.read() < 0) break;
|
|
85
|
+
skipped = 1;
|
|
86
|
+
}
|
|
87
|
+
remaining -= skipped;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
byte[] buffer = new byte[length];
|
|
91
|
+
int read = 0;
|
|
92
|
+
while (read < length) {
|
|
93
|
+
int count = stream.read(buffer, read, length - read);
|
|
94
|
+
if (count < 0) break;
|
|
95
|
+
if (count == 0) continue;
|
|
96
|
+
read += count;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
WritableMap result = Arguments.createMap();
|
|
100
|
+
result.putString("data", Base64.encodeToString(buffer, 0, read, Base64.NO_WRAP));
|
|
101
|
+
result.putBoolean("done", read < length);
|
|
102
|
+
promise.resolve(result);
|
|
103
|
+
}
|
|
104
|
+
} catch (Throwable error) {
|
|
105
|
+
promise.reject("papyrus_file_chunk_failed", error);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
57
110
|
@ReactMethod
|
|
58
111
|
public void load(final String engineId, final ReadableMap source, final Promise promise) {
|
|
59
112
|
executor.execute(() -> {
|
|
@@ -11,7 +11,9 @@ import expo.modules.kotlin.Promise
|
|
|
11
11
|
import expo.modules.kotlin.modules.Module
|
|
12
12
|
import expo.modules.kotlin.modules.ModuleDefinition
|
|
13
13
|
import expo.modules.kotlin.typedarray.Uint8Array
|
|
14
|
+
import android.util.Base64
|
|
14
15
|
import java.io.File
|
|
16
|
+
import java.io.FileInputStream
|
|
15
17
|
import java.io.FileOutputStream
|
|
16
18
|
import java.io.IOException
|
|
17
19
|
import java.io.InputStream
|
|
@@ -36,6 +38,49 @@ class PapyrusNativeEngineModule : Module() {
|
|
|
36
38
|
PapyrusEngineStore.destroyEngine(engineId)
|
|
37
39
|
}
|
|
38
40
|
|
|
41
|
+
AsyncFunction("readFileChunk") { uriValue: String, offsetValue: Double, length: Int ->
|
|
42
|
+
if (length <= 0 || length > 4 * 1024 * 1024) {
|
|
43
|
+
throw IOException("Invalid file chunk length")
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
val uri = Uri.parse(uriValue)
|
|
47
|
+
val inputStream: InputStream = when (uri.scheme?.lowercase()) {
|
|
48
|
+
"file" -> FileInputStream(File(uri.path ?: throw IOException("Invalid file URI")))
|
|
49
|
+
"content" -> {
|
|
50
|
+
val context = appContext.reactContext ?: throw IOException("React context missing")
|
|
51
|
+
context.contentResolver.openInputStream(uri)
|
|
52
|
+
?: throw IOException("Unable to read content URI")
|
|
53
|
+
}
|
|
54
|
+
else -> throw IOException("Unsupported local file URI")
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
inputStream.use { stream ->
|
|
58
|
+
var remaining = maxOf(0L, offsetValue.toLong())
|
|
59
|
+
while (remaining > 0) {
|
|
60
|
+
val skipped = stream.skip(remaining)
|
|
61
|
+
if (skipped <= 0L) {
|
|
62
|
+
if (stream.read() < 0) break
|
|
63
|
+
remaining -= 1
|
|
64
|
+
} else {
|
|
65
|
+
remaining -= skipped
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
val buffer = ByteArray(length)
|
|
70
|
+
var read = 0
|
|
71
|
+
while (read < length) {
|
|
72
|
+
val count = stream.read(buffer, read, length - read)
|
|
73
|
+
if (count < 0) break
|
|
74
|
+
if (count > 0) read += count
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
mapOf(
|
|
78
|
+
"data" to Base64.encodeToString(buffer, 0, read, Base64.NO_WRAP),
|
|
79
|
+
"done" to (read < length),
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
39
84
|
AsyncFunction("load") { engineId: String, source: Map<String, Any?>, promise: Promise ->
|
|
40
85
|
executor.execute {
|
|
41
86
|
try {
|
package/dist/index.d.mts
CHANGED
|
@@ -130,7 +130,18 @@ declare class NativeDocumentEngine extends BaseDocumentEngine {
|
|
|
130
130
|
type WebViewBridge = {
|
|
131
131
|
postMessage: (message: string) => void;
|
|
132
132
|
};
|
|
133
|
+
type WebViewRuntimeSource = string | number | {
|
|
134
|
+
uri: string;
|
|
135
|
+
};
|
|
136
|
+
type WebViewRuntimeConfig = Record<string, string>;
|
|
137
|
+
type MobileDocumentEngineOptions = {
|
|
138
|
+
/** Optional asset URI/require result for a runtime extension such as CBR. */
|
|
139
|
+
webViewRuntimeSource?: WebViewRuntimeSource;
|
|
140
|
+
webViewRuntimeConfig?: WebViewRuntimeConfig;
|
|
141
|
+
};
|
|
133
142
|
declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
143
|
+
private readonly webViewRuntimeSource?;
|
|
144
|
+
private readonly webViewRuntimeConfig?;
|
|
134
145
|
private bridge;
|
|
135
146
|
private ready;
|
|
136
147
|
private requestId;
|
|
@@ -142,9 +153,14 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
142
153
|
private zoom;
|
|
143
154
|
private rotation;
|
|
144
155
|
private outline;
|
|
156
|
+
constructor(options?: MobileDocumentEngineOptions);
|
|
157
|
+
getWebViewRuntimeSource(): WebViewRuntimeSource | undefined;
|
|
158
|
+
getWebViewRuntimeConfig(): WebViewRuntimeConfig | undefined;
|
|
145
159
|
getRenderTargetType(): "webview";
|
|
146
160
|
attachBridge(bridge: WebViewBridge): void;
|
|
147
161
|
handleMessage(raw: string): void;
|
|
162
|
+
private handleAssetRequest;
|
|
163
|
+
private handleFileChunkRequest;
|
|
148
164
|
load(input: DocumentLoadInput): Promise<void>;
|
|
149
165
|
getPageCount(): number;
|
|
150
166
|
getCurrentPage(): number;
|
|
@@ -160,6 +176,7 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
160
176
|
width: number;
|
|
161
177
|
height: number;
|
|
162
178
|
}>;
|
|
179
|
+
getPagePreview(pageIndex: number): Promise<string | null>;
|
|
163
180
|
searchText(query: string): Promise<SearchResult[]>;
|
|
164
181
|
selectText(pageIndex: number, rect: {
|
|
165
182
|
x: number;
|
|
@@ -175,6 +192,7 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
175
192
|
private request;
|
|
176
193
|
private normalizeRuntimeSource;
|
|
177
194
|
private fetchRemoteSource;
|
|
195
|
+
private fetchLocalSource;
|
|
178
196
|
private readResponseBuffer;
|
|
179
197
|
private isUriSource;
|
|
180
198
|
private isDataSource;
|
|
@@ -184,7 +202,9 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
184
202
|
private pdfEngine;
|
|
185
203
|
private webEngine;
|
|
186
204
|
private activeEngine;
|
|
187
|
-
constructor();
|
|
205
|
+
constructor(options?: MobileDocumentEngineOptions);
|
|
206
|
+
getWebViewRuntimeSource(): WebViewRuntimeSource | undefined;
|
|
207
|
+
getWebViewRuntimeConfig(): WebViewRuntimeConfig | undefined;
|
|
188
208
|
getRenderTargetType(): RenderTargetType;
|
|
189
209
|
getNativeEngineId(): string | null;
|
|
190
210
|
attachWebView(bridge: WebViewBridge): void;
|
|
@@ -204,6 +224,7 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
204
224
|
width: number;
|
|
205
225
|
height: number;
|
|
206
226
|
}>;
|
|
227
|
+
getPagePreview(pageIndex: number): Promise<string | null>;
|
|
207
228
|
searchText(query: string): Promise<SearchResult[]>;
|
|
208
229
|
selectText(pageIndex: number, rect: {
|
|
209
230
|
x: number;
|
|
@@ -216,4 +237,4 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
216
237
|
destroy(): void;
|
|
217
238
|
}
|
|
218
239
|
|
|
219
|
-
export { MobileDocumentEngine, NativeDocumentEngine, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, WebViewDocumentEngine };
|
|
240
|
+
export { MobileDocumentEngine, type MobileDocumentEngineOptions, NativeDocumentEngine, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, WebViewDocumentEngine, type WebViewRuntimeConfig, type WebViewRuntimeSource };
|
package/dist/index.d.ts
CHANGED
|
@@ -130,7 +130,18 @@ declare class NativeDocumentEngine extends BaseDocumentEngine {
|
|
|
130
130
|
type WebViewBridge = {
|
|
131
131
|
postMessage: (message: string) => void;
|
|
132
132
|
};
|
|
133
|
+
type WebViewRuntimeSource = string | number | {
|
|
134
|
+
uri: string;
|
|
135
|
+
};
|
|
136
|
+
type WebViewRuntimeConfig = Record<string, string>;
|
|
137
|
+
type MobileDocumentEngineOptions = {
|
|
138
|
+
/** Optional asset URI/require result for a runtime extension such as CBR. */
|
|
139
|
+
webViewRuntimeSource?: WebViewRuntimeSource;
|
|
140
|
+
webViewRuntimeConfig?: WebViewRuntimeConfig;
|
|
141
|
+
};
|
|
133
142
|
declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
143
|
+
private readonly webViewRuntimeSource?;
|
|
144
|
+
private readonly webViewRuntimeConfig?;
|
|
134
145
|
private bridge;
|
|
135
146
|
private ready;
|
|
136
147
|
private requestId;
|
|
@@ -142,9 +153,14 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
142
153
|
private zoom;
|
|
143
154
|
private rotation;
|
|
144
155
|
private outline;
|
|
156
|
+
constructor(options?: MobileDocumentEngineOptions);
|
|
157
|
+
getWebViewRuntimeSource(): WebViewRuntimeSource | undefined;
|
|
158
|
+
getWebViewRuntimeConfig(): WebViewRuntimeConfig | undefined;
|
|
145
159
|
getRenderTargetType(): "webview";
|
|
146
160
|
attachBridge(bridge: WebViewBridge): void;
|
|
147
161
|
handleMessage(raw: string): void;
|
|
162
|
+
private handleAssetRequest;
|
|
163
|
+
private handleFileChunkRequest;
|
|
148
164
|
load(input: DocumentLoadInput): Promise<void>;
|
|
149
165
|
getPageCount(): number;
|
|
150
166
|
getCurrentPage(): number;
|
|
@@ -160,6 +176,7 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
160
176
|
width: number;
|
|
161
177
|
height: number;
|
|
162
178
|
}>;
|
|
179
|
+
getPagePreview(pageIndex: number): Promise<string | null>;
|
|
163
180
|
searchText(query: string): Promise<SearchResult[]>;
|
|
164
181
|
selectText(pageIndex: number, rect: {
|
|
165
182
|
x: number;
|
|
@@ -175,6 +192,7 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
175
192
|
private request;
|
|
176
193
|
private normalizeRuntimeSource;
|
|
177
194
|
private fetchRemoteSource;
|
|
195
|
+
private fetchLocalSource;
|
|
178
196
|
private readResponseBuffer;
|
|
179
197
|
private isUriSource;
|
|
180
198
|
private isDataSource;
|
|
@@ -184,7 +202,9 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
184
202
|
private pdfEngine;
|
|
185
203
|
private webEngine;
|
|
186
204
|
private activeEngine;
|
|
187
|
-
constructor();
|
|
205
|
+
constructor(options?: MobileDocumentEngineOptions);
|
|
206
|
+
getWebViewRuntimeSource(): WebViewRuntimeSource | undefined;
|
|
207
|
+
getWebViewRuntimeConfig(): WebViewRuntimeConfig | undefined;
|
|
188
208
|
getRenderTargetType(): RenderTargetType;
|
|
189
209
|
getNativeEngineId(): string | null;
|
|
190
210
|
attachWebView(bridge: WebViewBridge): void;
|
|
@@ -204,6 +224,7 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
204
224
|
width: number;
|
|
205
225
|
height: number;
|
|
206
226
|
}>;
|
|
227
|
+
getPagePreview(pageIndex: number): Promise<string | null>;
|
|
207
228
|
searchText(query: string): Promise<SearchResult[]>;
|
|
208
229
|
selectText(pageIndex: number, rect: {
|
|
209
230
|
x: number;
|
|
@@ -216,4 +237,4 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
216
237
|
destroy(): void;
|
|
217
238
|
}
|
|
218
239
|
|
|
219
|
-
export { MobileDocumentEngine, NativeDocumentEngine, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, WebViewDocumentEngine };
|
|
240
|
+
export { MobileDocumentEngine, type MobileDocumentEngineOptions, NativeDocumentEngine, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, WebViewDocumentEngine, type WebViewRuntimeConfig, type WebViewRuntimeSource };
|