@papyrus-sdk/engine-native 0.2.18 → 0.2.19
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/src/main/java/com/papyrus/engine/PapyrusBitmapOwnership.java +9 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusEngineStore.java +12 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +66 -6
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.kt +14 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeRenderTelemetry.java +118 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusPageView.java +260 -47
- package/android/src/main/java/com/papyrus/engine/PapyrusPageViewManager.java +6 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +35 -19
- package/android/src/main/java/com/papyrus/engine/PapyrusRenderCompletion.java +33 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusRenderMath.java +13 -2
- package/dist/index.d.mts +10 -4
- package/dist/index.d.ts +10 -4
- package/dist/index.js +100 -45
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +100 -45
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ComponentType, RefAttributes } from 'react';
|
|
2
2
|
import { ViewProps, View } from 'react-native';
|
|
3
3
|
import { BaseDocumentEngine } from '@papyrus-sdk/core';
|
|
4
|
-
import { RenderTargetType, DocumentLoadInput, TextItem, SearchResult, TextSelection, OutlineItem, PageDestination, PageTheme, Annotation, PdfVisiblePage } from '@papyrus-sdk/types';
|
|
4
|
+
import { RenderTargetType, DocumentLoadInput, RenderPageTelemetryContext, TextItem, SearchResult, TextSelection, OutlineItem, PageDestination, RenderPageResult, PageTheme, Annotation, PdfVisiblePage } from '@papyrus-sdk/types';
|
|
5
5
|
|
|
6
6
|
type PapyrusPageViewProps = ViewProps & {
|
|
7
7
|
engineId?: string;
|
|
@@ -93,9 +93,11 @@ declare class NativeDocumentEngine extends BaseDocumentEngine {
|
|
|
93
93
|
private currentPage;
|
|
94
94
|
private zoom;
|
|
95
95
|
private rotation;
|
|
96
|
+
private renderRequestCounter;
|
|
96
97
|
constructor();
|
|
97
98
|
load(input: DocumentLoadInput): Promise<void>;
|
|
98
99
|
getPageCount(): number;
|
|
100
|
+
getLifecycleStats(): Record<string, number>;
|
|
99
101
|
getCurrentPage(): number;
|
|
100
102
|
goToPage(page: number): void;
|
|
101
103
|
setZoom(zoom: number): void;
|
|
@@ -103,7 +105,7 @@ declare class NativeDocumentEngine extends BaseDocumentEngine {
|
|
|
103
105
|
rotate(direction: "clockwise" | "counterclockwise"): void;
|
|
104
106
|
getRotation(): number;
|
|
105
107
|
getNativeEngineId(): string;
|
|
106
|
-
renderPage(pageIndex: number, target: any, scale: number): Promise<void>;
|
|
108
|
+
renderPage(pageIndex: number, target: any, scale: number, telemetry?: RenderPageTelemetryContext): Promise<void | RenderPageResult>;
|
|
107
109
|
renderTextLayer(pageIndex: number, target: any, scale: number): Promise<void>;
|
|
108
110
|
getTextContent(pageIndex: number): Promise<TextItem[]>;
|
|
109
111
|
getPageDimensions(pageIndex: number): Promise<{
|
|
@@ -157,7 +159,9 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
157
159
|
getWebViewRuntimeSource(): WebViewRuntimeSource | undefined;
|
|
158
160
|
getWebViewRuntimeConfig(): WebViewRuntimeConfig | undefined;
|
|
159
161
|
getRenderTargetType(): "webview";
|
|
162
|
+
getLifecycleStats(): Record<string, number>;
|
|
160
163
|
attachBridge(bridge: WebViewBridge): void;
|
|
164
|
+
detachBridge(): void;
|
|
161
165
|
handleMessage(raw: string): void;
|
|
162
166
|
private handleAssetRequest;
|
|
163
167
|
private handleFileChunkRequest;
|
|
@@ -169,7 +173,7 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
169
173
|
getZoom(): number;
|
|
170
174
|
rotate(direction: "clockwise" | "counterclockwise"): void;
|
|
171
175
|
getRotation(): number;
|
|
172
|
-
renderPage(pageIndex: number, target: any, scale: number): Promise<void>;
|
|
176
|
+
renderPage(pageIndex: number, target: any, scale: number, _telemetry?: RenderPageTelemetryContext): Promise<void>;
|
|
173
177
|
renderTextLayer(pageIndex: number, container: any, scale: number): Promise<void>;
|
|
174
178
|
getTextContent(pageIndex: number): Promise<TextItem[]>;
|
|
175
179
|
getPageDimensions(pageIndex: number): Promise<{
|
|
@@ -207,7 +211,9 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
207
211
|
getWebViewRuntimeConfig(): WebViewRuntimeConfig | undefined;
|
|
208
212
|
getRenderTargetType(): RenderTargetType;
|
|
209
213
|
getNativeEngineId(): string | null;
|
|
214
|
+
getLifecycleStats(): Record<string, number>;
|
|
210
215
|
attachWebView(bridge: WebViewBridge): void;
|
|
216
|
+
detachWebView(): void;
|
|
211
217
|
handleWebViewMessage(data: string): void;
|
|
212
218
|
load(input: DocumentLoadInput): Promise<void>;
|
|
213
219
|
getPageCount(): number;
|
|
@@ -217,7 +223,7 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
217
223
|
getZoom(): number;
|
|
218
224
|
rotate(direction: "clockwise" | "counterclockwise"): void;
|
|
219
225
|
getRotation(): number;
|
|
220
|
-
renderPage(pageIndex: number, target: any, scale: number): Promise<void>;
|
|
226
|
+
renderPage(pageIndex: number, target: any, scale: number, telemetry?: RenderPageTelemetryContext): Promise<void>;
|
|
221
227
|
renderTextLayer(pageIndex: number, container: any, scale: number): Promise<void>;
|
|
222
228
|
getTextContent(pageIndex: number): Promise<TextItem[]>;
|
|
223
229
|
getPageDimensions(pageIndex: number): Promise<{
|
package/dist/index.js
CHANGED
|
@@ -28,8 +28,6 @@ __export(engine_native_exports, {
|
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(engine_native_exports);
|
|
30
30
|
var import_react_native = require("react-native");
|
|
31
|
-
var import_NativeViewManagerAdapter = require("expo-modules-core/src/NativeViewManagerAdapter");
|
|
32
|
-
var import_requireNativeModule = require("expo-modules-core/src/requireNativeModule");
|
|
33
31
|
var import_core = require("@papyrus-sdk/core");
|
|
34
32
|
var import_types = require("@papyrus-sdk/types");
|
|
35
33
|
|
|
@@ -82,6 +80,15 @@ var inferDocumentType = (source) => {
|
|
|
82
80
|
return "pdf";
|
|
83
81
|
};
|
|
84
82
|
|
|
83
|
+
// nativeModuleResolution.ts
|
|
84
|
+
var resolvePapyrusNativeModule = (registries) => {
|
|
85
|
+
const turboModule = registries.turboModuleRegistry.get(
|
|
86
|
+
"PapyrusNativeEngine"
|
|
87
|
+
);
|
|
88
|
+
if (turboModule) return turboModule;
|
|
89
|
+
return registries.nativeModules.PapyrusNativeEngine ?? null;
|
|
90
|
+
};
|
|
91
|
+
|
|
85
92
|
// index.ts
|
|
86
93
|
var MODULE_NAME = "PapyrusNativeEngine";
|
|
87
94
|
var BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
@@ -102,7 +109,7 @@ var parseDataUri2 = (value) => {
|
|
|
102
109
|
data: match[3] ?? ""
|
|
103
110
|
};
|
|
104
111
|
};
|
|
105
|
-
var looksLikeUri = (value) => value.startsWith("http://") || value.startsWith("https://") || value.startsWith("/") || value.startsWith("./") || value.startsWith("../") || value.startsWith("file://") || value.startsWith("content://");
|
|
112
|
+
var looksLikeUri = (value) => value.startsWith("http://") || value.startsWith("https://") || value.startsWith("/") || value.startsWith("./") || value.startsWith("../") || value.startsWith("file://") || value.startsWith("content://") || value.startsWith("android.resource://");
|
|
106
113
|
var isLikelyBase64 = (value) => {
|
|
107
114
|
if (looksLikeUri(value)) return false;
|
|
108
115
|
if (value.includes(".")) return false;
|
|
@@ -110,7 +117,15 @@ var isLikelyBase64 = (value) => {
|
|
|
110
117
|
return /^[A-Za-z0-9+/=]+$/.test(value);
|
|
111
118
|
};
|
|
112
119
|
var isHttpUri = (value) => value.startsWith("http://") || value.startsWith("https://");
|
|
113
|
-
var
|
|
120
|
+
var isMetroAssetUri = (value) => {
|
|
121
|
+
try {
|
|
122
|
+
const url = new URL(value);
|
|
123
|
+
return (url.protocol === "http:" || url.protocol === "https:") && url.pathname.startsWith("/assets/") && url.searchParams.has("platform") && url.searchParams.has("hash");
|
|
124
|
+
} catch {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
var isLocalUri = (value) => value.startsWith("content://") || value.startsWith("file://") || value.startsWith("android.resource://");
|
|
114
129
|
var decodeBase64 = (value) => {
|
|
115
130
|
const clean = value.replace(/[^A-Za-z0-9+/=]/g, "");
|
|
116
131
|
const padding = clean.endsWith("==") ? 2 : clean.endsWith("=") ? 1 : 0;
|
|
@@ -166,14 +181,10 @@ var normalizeNativeDestination = (dest) => {
|
|
|
166
181
|
return null;
|
|
167
182
|
};
|
|
168
183
|
var resolveNativeModule = () => {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
);
|
|
174
|
-
if (turboModule) return turboModule;
|
|
175
|
-
const rnModule = import_react_native.NativeModules[MODULE_NAME];
|
|
176
|
-
return rnModule ?? null;
|
|
184
|
+
return resolvePapyrusNativeModule({
|
|
185
|
+
nativeModules: import_react_native.NativeModules,
|
|
186
|
+
turboModuleRegistry: import_react_native.TurboModuleRegistry
|
|
187
|
+
});
|
|
177
188
|
};
|
|
178
189
|
var resolvePapyrusPageView = () => {
|
|
179
190
|
try {
|
|
@@ -181,13 +192,7 @@ var resolvePapyrusPageView = () => {
|
|
|
181
192
|
"PapyrusPageView"
|
|
182
193
|
);
|
|
183
194
|
} catch {
|
|
184
|
-
|
|
185
|
-
return (0, import_NativeViewManagerAdapter.requireNativeViewManager)(
|
|
186
|
-
"PapyrusPageView"
|
|
187
|
-
);
|
|
188
|
-
} catch {
|
|
189
|
-
return import_react_native.View;
|
|
190
|
-
}
|
|
195
|
+
return import_react_native.View;
|
|
191
196
|
}
|
|
192
197
|
};
|
|
193
198
|
var resolvePapyrusPdfViewerView = () => {
|
|
@@ -197,13 +202,7 @@ var resolvePapyrusPdfViewerView = () => {
|
|
|
197
202
|
componentName
|
|
198
203
|
);
|
|
199
204
|
} catch {
|
|
200
|
-
|
|
201
|
-
return (0, import_NativeViewManagerAdapter.requireNativeViewManager)(
|
|
202
|
-
componentName
|
|
203
|
-
);
|
|
204
|
-
} catch {
|
|
205
|
-
return import_react_native.View;
|
|
206
|
-
}
|
|
205
|
+
return import_react_native.View;
|
|
207
206
|
}
|
|
208
207
|
};
|
|
209
208
|
var PapyrusPageView = resolvePapyrusPageView();
|
|
@@ -218,6 +217,7 @@ var NativeDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
218
217
|
this.currentPage = 1;
|
|
219
218
|
this.zoom = 1;
|
|
220
219
|
this.rotation = 0;
|
|
220
|
+
this.renderRequestCounter = 0;
|
|
221
221
|
this.nativeModule = resolveNativeModule();
|
|
222
222
|
this.engineId = this.nativeModule?.createEngine ? this.nativeModule.createEngine() : "default";
|
|
223
223
|
}
|
|
@@ -230,7 +230,17 @@ var NativeDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
230
230
|
}
|
|
231
231
|
const native = this.assertNativeModule();
|
|
232
232
|
const normalized = await this.normalizeSource(source);
|
|
233
|
-
|
|
233
|
+
let result;
|
|
234
|
+
try {
|
|
235
|
+
result = native.load ? await native.load(this.engineId, normalized) : void 0;
|
|
236
|
+
} catch (error) {
|
|
237
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
238
|
+
if (!/engine not found|papyrus_no_engine/i.test(message) || !native.createEngine) {
|
|
239
|
+
throw error;
|
|
240
|
+
}
|
|
241
|
+
this.engineId = native.createEngine();
|
|
242
|
+
result = native.load ? await native.load(this.engineId, normalized) : void 0;
|
|
243
|
+
}
|
|
234
244
|
if (result && typeof result.pageCount === "number") {
|
|
235
245
|
this.pageCount = result.pageCount;
|
|
236
246
|
} else if (native.getPageCount) {
|
|
@@ -243,6 +253,9 @@ var NativeDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
243
253
|
getPageCount() {
|
|
244
254
|
return this.pageCount;
|
|
245
255
|
}
|
|
256
|
+
getLifecycleStats() {
|
|
257
|
+
return this.nativeModule?.getLifecycleStats?.() ?? {};
|
|
258
|
+
}
|
|
246
259
|
getCurrentPage() {
|
|
247
260
|
return this.currentPage;
|
|
248
261
|
}
|
|
@@ -271,18 +284,21 @@ var NativeDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
271
284
|
getNativeEngineId() {
|
|
272
285
|
return this.engineId;
|
|
273
286
|
}
|
|
274
|
-
async renderPage(pageIndex, target, scale) {
|
|
287
|
+
async renderPage(pageIndex, target, scale, telemetry) {
|
|
275
288
|
const native = this.assertNativeModule();
|
|
276
289
|
if (!native.renderPage) return;
|
|
277
290
|
const viewTag = this.toNativeViewTag(target);
|
|
278
291
|
if (viewTag === null) return;
|
|
279
|
-
native.
|
|
292
|
+
const requestId = `native-render-${++this.renderRequestCounter}`;
|
|
293
|
+
return native.renderPage(
|
|
280
294
|
this.engineId,
|
|
281
295
|
pageIndex,
|
|
282
296
|
viewTag,
|
|
283
297
|
scale,
|
|
284
298
|
this.zoom,
|
|
285
|
-
this.rotation
|
|
299
|
+
this.rotation,
|
|
300
|
+
requestId,
|
|
301
|
+
telemetry
|
|
286
302
|
);
|
|
287
303
|
}
|
|
288
304
|
async renderTextLayer(pageIndex, target, scale) {
|
|
@@ -422,11 +438,29 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
422
438
|
getRenderTargetType() {
|
|
423
439
|
return "webview";
|
|
424
440
|
}
|
|
441
|
+
getLifecycleStats() {
|
|
442
|
+
return {
|
|
443
|
+
webViewCount: this.bridge ? 1 : 0,
|
|
444
|
+
pendingBridgeRequests: this.pending.size
|
|
445
|
+
};
|
|
446
|
+
}
|
|
425
447
|
attachBridge(bridge) {
|
|
426
448
|
this.bridge = bridge;
|
|
427
|
-
this.
|
|
449
|
+
this.ready = false;
|
|
450
|
+
this.bridgeResolvers.forEach(({ resolve }) => resolve(bridge));
|
|
428
451
|
this.bridgeResolvers = [];
|
|
429
452
|
}
|
|
453
|
+
detachBridge() {
|
|
454
|
+
const error = new Error("[Papyrus] WebView detached");
|
|
455
|
+
this.pending.forEach(({ reject }) => reject(error));
|
|
456
|
+
this.pending.clear();
|
|
457
|
+
this.bridgeResolvers.forEach(({ reject }) => reject(error));
|
|
458
|
+
this.readyResolvers.forEach(({ reject }) => reject(error));
|
|
459
|
+
this.bridgeResolvers = [];
|
|
460
|
+
this.readyResolvers = [];
|
|
461
|
+
this.bridge = null;
|
|
462
|
+
this.ready = false;
|
|
463
|
+
}
|
|
430
464
|
handleMessage(raw) {
|
|
431
465
|
let message = null;
|
|
432
466
|
try {
|
|
@@ -445,7 +479,7 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
445
479
|
}
|
|
446
480
|
if (message.type === "ready") {
|
|
447
481
|
this.ready = true;
|
|
448
|
-
this.readyResolvers.forEach((resolve) => resolve());
|
|
482
|
+
this.readyResolvers.forEach(({ resolve }) => resolve());
|
|
449
483
|
this.readyResolvers = [];
|
|
450
484
|
return;
|
|
451
485
|
}
|
|
@@ -625,7 +659,7 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
625
659
|
getRotation() {
|
|
626
660
|
return this.rotation;
|
|
627
661
|
}
|
|
628
|
-
async renderPage(pageIndex, target, scale) {
|
|
662
|
+
async renderPage(pageIndex, target, scale, _telemetry) {
|
|
629
663
|
}
|
|
630
664
|
async renderTextLayer(pageIndex, container, scale) {
|
|
631
665
|
}
|
|
@@ -671,23 +705,18 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
671
705
|
return await this.request("get-page-index", { dest });
|
|
672
706
|
}
|
|
673
707
|
destroy() {
|
|
674
|
-
this.
|
|
675
|
-
({ reject }) => reject(new Error("[Papyrus] WebView engine destroyed"))
|
|
676
|
-
);
|
|
677
|
-
this.pending.clear();
|
|
678
|
-
this.bridge = null;
|
|
679
|
-
this.ready = false;
|
|
708
|
+
this.detachBridge();
|
|
680
709
|
}
|
|
681
710
|
async ensureBridge() {
|
|
682
711
|
if (this.bridge) return this.bridge;
|
|
683
|
-
return new Promise((resolve) => {
|
|
684
|
-
this.bridgeResolvers.push(resolve);
|
|
712
|
+
return new Promise((resolve, reject) => {
|
|
713
|
+
this.bridgeResolvers.push({ resolve, reject });
|
|
685
714
|
});
|
|
686
715
|
}
|
|
687
716
|
async ensureReady() {
|
|
688
717
|
if (this.ready) return;
|
|
689
|
-
return new Promise((resolve) => {
|
|
690
|
-
this.readyResolvers.push(resolve);
|
|
718
|
+
return new Promise((resolve, reject) => {
|
|
719
|
+
this.readyResolvers.push({ resolve, reject });
|
|
691
720
|
});
|
|
692
721
|
}
|
|
693
722
|
async request(kind, payload) {
|
|
@@ -728,6 +757,9 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
728
757
|
return { kind: "text", text };
|
|
729
758
|
}
|
|
730
759
|
if (looksLikeUri(source)) {
|
|
760
|
+
if (isMetroAssetUri(source)) {
|
|
761
|
+
return { kind: "uri", uri: source };
|
|
762
|
+
}
|
|
731
763
|
if (isHttpUri(source)) {
|
|
732
764
|
const fetched = await this.fetchRemoteSource(type, source);
|
|
733
765
|
if (fetched) return fetched;
|
|
@@ -747,6 +779,20 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
747
779
|
}
|
|
748
780
|
if (this.isUriSource(source)) {
|
|
749
781
|
const uri = source.uri;
|
|
782
|
+
const dataUri = parseDataUri2(uri);
|
|
783
|
+
if (dataUri) {
|
|
784
|
+
if (dataUri.isBase64) {
|
|
785
|
+
return {
|
|
786
|
+
kind: "base64",
|
|
787
|
+
data: dataUri.data,
|
|
788
|
+
mime: dataUri.mime || void 0
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
return { kind: "text", text: decodeURIComponent(dataUri.data) };
|
|
792
|
+
}
|
|
793
|
+
if (isMetroAssetUri(uri)) {
|
|
794
|
+
return { kind: "uri", uri };
|
|
795
|
+
}
|
|
750
796
|
if (isHttpUri(uri)) {
|
|
751
797
|
const fetched = await this.fetchRemoteSource(type, uri);
|
|
752
798
|
if (fetched) return fetched;
|
|
@@ -856,9 +902,18 @@ var MobileDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
856
902
|
getNativeEngineId() {
|
|
857
903
|
return this.activeEngine === this.pdfEngine ? this.pdfEngine.getNativeEngineId() : null;
|
|
858
904
|
}
|
|
905
|
+
getLifecycleStats() {
|
|
906
|
+
return {
|
|
907
|
+
...this.pdfEngine.getLifecycleStats(),
|
|
908
|
+
...this.webEngine.getLifecycleStats()
|
|
909
|
+
};
|
|
910
|
+
}
|
|
859
911
|
attachWebView(bridge) {
|
|
860
912
|
this.webEngine.attachBridge(bridge);
|
|
861
913
|
}
|
|
914
|
+
detachWebView() {
|
|
915
|
+
this.webEngine.detachBridge();
|
|
916
|
+
}
|
|
862
917
|
handleWebViewMessage(data) {
|
|
863
918
|
this.webEngine.handleMessage(data);
|
|
864
919
|
}
|
|
@@ -893,8 +948,8 @@ var MobileDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
893
948
|
getRotation() {
|
|
894
949
|
return this.activeEngine.getRotation();
|
|
895
950
|
}
|
|
896
|
-
async renderPage(pageIndex, target, scale) {
|
|
897
|
-
await this.activeEngine.renderPage(pageIndex, target, scale);
|
|
951
|
+
async renderPage(pageIndex, target, scale, telemetry) {
|
|
952
|
+
await this.activeEngine.renderPage(pageIndex, target, scale, telemetry);
|
|
898
953
|
}
|
|
899
954
|
async renderTextLayer(pageIndex, container, scale) {
|
|
900
955
|
await this.activeEngine.renderTextLayer(pageIndex, container, scale);
|