@papyrus-sdk/engine-native 0.2.26 → 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.
@@ -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;
@@ -1600,9 +1602,9 @@ public class PapyrusPdfViewerView extends View {
1600
1602
  if ("single".equals(viewMode)) {
1601
1603
  float cellHeight = getHeight();
1602
1604
  for (int i = 0; i < pageCount; i += 1) {
1603
- state.pdfium.openPage(document, i);
1604
- int pageWidth = Math.max(1, state.pdfium.getPageWidthPoint(document, i));
1605
- int pageHeight = Math.max(1, state.pdfium.getPageHeightPoint(document, i));
1605
+ int[] pageSize = PapyrusEngineStore.layoutPageSizePoints(state, i);
1606
+ int pageWidth = Math.max(1, pageSize[0]);
1607
+ int pageHeight = Math.max(1, pageSize[1]);
1606
1608
  float baseScale = availableWidth / (float) pageWidth;
1607
1609
  float width = pageWidth * baseScale * zoom;
1608
1610
  float height = pageHeight * baseScale * zoom;
@@ -1616,9 +1618,9 @@ public class PapyrusPdfViewerView extends View {
1616
1618
  } else {
1617
1619
  float y = PAGE_PADDING;
1618
1620
  for (int i = 0; i < pageCount; i += 1) {
1619
- state.pdfium.openPage(document, i);
1620
- int pageWidth = Math.max(1, state.pdfium.getPageWidthPoint(document, i));
1621
- int pageHeight = Math.max(1, state.pdfium.getPageHeightPoint(document, i));
1621
+ int[] pageSize = PapyrusEngineStore.layoutPageSizePoints(state, i);
1622
+ int pageWidth = Math.max(1, pageSize[0]);
1623
+ int pageHeight = Math.max(1, pageSize[1]);
1622
1624
  float baseScale = availableWidth / (float) pageWidth;
1623
1625
  float width = pageWidth * baseScale * zoom;
1624
1626
  float height = pageHeight * baseScale * zoom;
@@ -1768,7 +1770,8 @@ public class PapyrusPdfViewerView extends View {
1768
1770
  try {
1769
1771
  synchronized (state.pdfiumLock) {
1770
1772
  if (state.document == null) return;
1771
- state.pdfium.openPage(state.document, frame.index);
1773
+ PapyrusEngineStore.acquirePage(state, frame.index);
1774
+ if (state.document == null) return;
1772
1775
  rendered = Bitmap.createBitmap(renderWidth, renderHeight, Bitmap.Config.ARGB_8888);
1773
1776
  state.pdfium.renderPageBitmap(state.document, rendered, frame.index, 0, 0, renderWidth, renderHeight, true);
1774
1777
  }
@@ -1810,7 +1813,8 @@ public class PapyrusPdfViewerView extends View {
1810
1813
 
1811
1814
  private String buildRenderKey(PageFrame frame) {
1812
1815
  PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
1813
- int docIdentity = state == null || state.document == null ? 0 : System.identityHashCode(state.document);
1816
+ // The document as the reader sees it: stable while the same file is reopened underneath.
1817
+ int docIdentity = state == null || state.document == null ? 0 : 31 * System.identityHashCode(state) + state.documentGeneration + 1;
1814
1818
  String source = state == null || state.sourcePath == null ? "" : state.sourcePath;
1815
1819
  // Bucket zoom to prevent tiny fluctuations from changing the key during scroll/pan
1816
1820
  float zoomBucket = Math.round(zoom * 100f) / 100f;
@@ -2144,6 +2148,10 @@ public class PapyrusPdfViewerView extends View {
2144
2148
  for(float x=rLeft+3;x<=rRight;x+=3)wave.lineTo(x,rBottom-2+(((int)((x-rLeft)/3))%2==0?-2:2));
2145
2149
  canvas.drawPath(wave,overlayPaint);overlayPaint.setStyle(Paint.Style.FILL);
2146
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)));
2147
2155
  canvas.drawRect(rLeft, rTop, rRight, rBottom, overlayPaint);
2148
2156
  }
2149
2157
  }
@@ -0,0 +1,227 @@
1
+ package com.papyrus.engine;
2
+
3
+ import android.content.Context;
4
+ import android.graphics.Bitmap;
5
+ import android.graphics.BitmapFactory;
6
+ import android.graphics.Color;
7
+ import android.graphics.pdf.PdfRenderer;
8
+ import android.net.Uri;
9
+ import android.os.ParcelFileDescriptor;
10
+
11
+ import java.io.BufferedReader;
12
+ import java.io.File;
13
+ import java.io.FileOutputStream;
14
+ import java.io.IOException;
15
+ import java.io.InputStream;
16
+ import java.io.InputStreamReader;
17
+ import java.nio.charset.StandardCharsets;
18
+ import java.util.ArrayList;
19
+ import java.util.List;
20
+ import java.util.regex.Matcher;
21
+ import java.util.regex.Pattern;
22
+ import java.util.zip.ZipEntry;
23
+ import java.util.zip.ZipFile;
24
+
25
+ /**
26
+ * Draws one page of a PDF, or the cover image of an EPUB, into an image file.
27
+ *
28
+ * It opens the document, renders, and closes everything before returning: no
29
+ * engine, view or cache outlives the call. PDFs go through the platform's
30
+ * PdfRenderer, so a thumbnail never touches the reader's pdfium document.
31
+ * Only local sources are accepted: a thumbnail must not trigger a download.
32
+ */
33
+ final class PapyrusThumbnail {
34
+ static final class Result {
35
+ final String path;
36
+ final int width;
37
+ final int height;
38
+
39
+ Result(String path, int width, int height) {
40
+ this.path = path;
41
+ this.width = width;
42
+ this.height = height;
43
+ }
44
+ }
45
+
46
+ private PapyrusThumbnail() {}
47
+
48
+ static Result generate(Context context, String sourceUri, String type, int pageIndex, int maxWidth, String format, int quality, String outputPath) throws IOException {
49
+ if (sourceUri == null || sourceUri.isEmpty()) throw new IllegalArgumentException("uri is required");
50
+ int width = Math.max(32, Math.min(maxWidth <= 0 ? 480 : maxWidth, 2048));
51
+ boolean png = "png".equalsIgnoreCase(format);
52
+ String kind = kindOf(sourceUri, type);
53
+ Bitmap bitmap = null;
54
+ try {
55
+ if ("pdf".equals(kind)) bitmap = renderPdf(context, sourceUri, Math.max(0, pageIndex), width);
56
+ else if ("epub".equals(kind)) bitmap = renderEpubCover(sourceUri, width);
57
+ else throw new IllegalArgumentException("unsupported document type");
58
+
59
+ File destination = outputPath != null && !outputPath.isEmpty()
60
+ ? new File(stripFileScheme(outputPath))
61
+ : File.createTempFile("thumbnail-", png ? ".png" : ".jpg", ensureDirectory(new File(context.getCacheDir(), "papyrus-thumbnails")));
62
+ File parent = destination.getParentFile();
63
+ if (parent != null) ensureDirectory(parent);
64
+ // Written aside and renamed: a half-written file is never read as an image.
65
+ File partial = new File(destination.getPath() + ".part");
66
+ try (FileOutputStream out = new FileOutputStream(partial)) {
67
+ Bitmap.CompressFormat encoder = png ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG;
68
+ if (!bitmap.compress(encoder, Math.max(1, Math.min(quality <= 0 ? 85 : quality, 100)), out)) {
69
+ throw new IOException("could not encode the thumbnail");
70
+ }
71
+ }
72
+ if (!partial.renameTo(destination)) {
73
+ partial.delete();
74
+ throw new IOException("could not store the thumbnail");
75
+ }
76
+ return new Result(destination.getAbsolutePath(), bitmap.getWidth(), bitmap.getHeight());
77
+ } finally {
78
+ if (bitmap != null) bitmap.recycle();
79
+ }
80
+ }
81
+
82
+ private static File ensureDirectory(File directory) throws IOException {
83
+ if (!directory.isDirectory() && !directory.mkdirs() && !directory.isDirectory()) {
84
+ throw new IOException("could not create " + directory);
85
+ }
86
+ return directory;
87
+ }
88
+
89
+ private static String stripFileScheme(String value) {
90
+ return value.startsWith("file://") ? Uri.parse(value).getPath() : value;
91
+ }
92
+
93
+ static String kindOf(String sourceUri, String type) {
94
+ String declared = type == null ? "" : type.toLowerCase();
95
+ String path = sourceUri.toLowerCase();
96
+ int query = path.indexOf('?');
97
+ if (query >= 0) path = path.substring(0, query);
98
+ if (declared.contains("pdf") || path.endsWith(".pdf")) return "pdf";
99
+ if (declared.contains("epub") || path.endsWith(".epub")) return "epub";
100
+ return "unknown";
101
+ }
102
+
103
+ private static ParcelFileDescriptor openDescriptor(Context context, String sourceUri) throws IOException {
104
+ if (sourceUri.startsWith("content://")) {
105
+ ParcelFileDescriptor descriptor = context.getContentResolver().openFileDescriptor(Uri.parse(sourceUri), "r");
106
+ if (descriptor == null) throw new IOException("could not open the document");
107
+ return descriptor;
108
+ }
109
+ if (sourceUri.startsWith("file://") || sourceUri.startsWith("/")) {
110
+ return ParcelFileDescriptor.open(new File(stripFileScheme(sourceUri)), ParcelFileDescriptor.MODE_READ_ONLY);
111
+ }
112
+ throw new IllegalArgumentException("the document must be a local file");
113
+ }
114
+
115
+ private static Bitmap renderPdf(Context context, String sourceUri, int pageIndex, int maxWidth) throws IOException {
116
+ try (ParcelFileDescriptor descriptor = openDescriptor(context, sourceUri);
117
+ PdfRenderer renderer = new PdfRenderer(descriptor)) {
118
+ if (renderer.getPageCount() < 1) throw new IOException("the document has no pages");
119
+ try (PdfRenderer.Page page = renderer.openPage(Math.min(pageIndex, renderer.getPageCount() - 1))) {
120
+ float scale = maxWidth / (float) Math.max(1, page.getWidth());
121
+ int width = Math.max(1, Math.round(page.getWidth() * scale));
122
+ int height = Math.max(1, Math.round(page.getHeight() * scale));
123
+ Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
124
+ // PDF pages are transparent where nothing is drawn.
125
+ bitmap.eraseColor(Color.WHITE);
126
+ page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
127
+ return bitmap;
128
+ }
129
+ }
130
+ }
131
+
132
+ private static String readEntry(ZipFile zip, String name) throws IOException {
133
+ ZipEntry entry = zip.getEntry(name);
134
+ if (entry == null) return null;
135
+ StringBuilder text = new StringBuilder();
136
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(zip.getInputStream(entry), StandardCharsets.UTF_8))) {
137
+ char[] buffer = new char[8192];
138
+ int read;
139
+ while ((read = reader.read(buffer)) > 0) text.append(buffer, 0, read);
140
+ }
141
+ return text.toString();
142
+ }
143
+
144
+ private static String attribute(String tag, String name) {
145
+ Matcher matcher = Pattern.compile("\\b" + name + "\\s*=\\s*[\"']([^\"']*)[\"']").matcher(tag);
146
+ return matcher.find() ? matcher.group(1) : null;
147
+ }
148
+
149
+ private static List<String> tags(String xml, String name) {
150
+ List<String> found = new ArrayList<>();
151
+ Matcher matcher = Pattern.compile("<" + name + "\\b[^>]*>").matcher(xml);
152
+ while (matcher.find()) found.add(matcher.group());
153
+ return found;
154
+ }
155
+
156
+ /** The cover image named by the EPUB package (EPUB 3 `cover-image`, EPUB 2 `meta name="cover"`). */
157
+ static String epubCoverEntry(String opfPath, String opf) {
158
+ String coverId = null;
159
+ for (String meta : tags(opf, "meta")) {
160
+ if ("cover".equals(attribute(meta, "name"))) coverId = attribute(meta, "content");
161
+ }
162
+ String href = null;
163
+ List<String> items = tags(opf, "item");
164
+ for (String item : items) {
165
+ String properties = attribute(item, "properties");
166
+ if (properties != null && (" " + properties + " ").contains(" cover-image ")) href = attribute(item, "href");
167
+ }
168
+ if (href == null && coverId != null) {
169
+ for (String item : items) {
170
+ if (coverId.equals(attribute(item, "id"))) href = attribute(item, "href");
171
+ }
172
+ }
173
+ if (href == null) {
174
+ for (String item : items) {
175
+ String mediaType = attribute(item, "media-type");
176
+ String id = attribute(item, "id");
177
+ if (mediaType != null && mediaType.startsWith("image/") && id != null && id.toLowerCase().contains("cover")) {
178
+ href = attribute(item, "href");
179
+ break;
180
+ }
181
+ }
182
+ }
183
+ if (href == null) return null;
184
+ int slash = opfPath.lastIndexOf('/');
185
+ String base = slash >= 0 ? opfPath.substring(0, slash + 1) : "";
186
+ String normalized = new File("/" + base + Uri.decode(href)).toURI().normalize().getPath();
187
+ return normalized.startsWith("/") ? normalized.substring(1) : normalized;
188
+ }
189
+
190
+ private static Bitmap renderEpubCover(String sourceUri, int maxWidth) throws IOException {
191
+ if (!(sourceUri.startsWith("file://") || sourceUri.startsWith("/"))) {
192
+ throw new IllegalArgumentException("the document must be a local file");
193
+ }
194
+ try (ZipFile zip = new ZipFile(new File(stripFileScheme(sourceUri)))) {
195
+ String container = readEntry(zip, "META-INF/container.xml");
196
+ if (container == null) throw new IOException("not an EPUB");
197
+ Matcher rootfile = Pattern.compile("full-path\\s*=\\s*[\"']([^\"']+)[\"']").matcher(container);
198
+ if (!rootfile.find()) throw new IOException("EPUB without a package file");
199
+ String opfPath = rootfile.group(1);
200
+ String opf = readEntry(zip, opfPath);
201
+ if (opf == null) throw new IOException("EPUB without a package file");
202
+ String coverName = epubCoverEntry(opfPath, opf);
203
+ ZipEntry cover = coverName == null ? null : zip.getEntry(coverName);
204
+ if (cover == null) throw new IOException("EPUB without a cover image");
205
+
206
+ BitmapFactory.Options bounds = new BitmapFactory.Options();
207
+ bounds.inJustDecodeBounds = true;
208
+ try (InputStream stream = zip.getInputStream(cover)) {
209
+ BitmapFactory.decodeStream(stream, null, bounds);
210
+ }
211
+ if (bounds.outWidth <= 0 || bounds.outHeight <= 0) throw new IOException("EPUB cover image is not readable");
212
+ BitmapFactory.Options options = new BitmapFactory.Options();
213
+ options.inSampleSize = 1;
214
+ while (bounds.outWidth / (options.inSampleSize * 2) >= maxWidth) options.inSampleSize *= 2;
215
+ Bitmap decoded;
216
+ try (InputStream stream = zip.getInputStream(cover)) {
217
+ decoded = BitmapFactory.decodeStream(stream, null, options);
218
+ }
219
+ if (decoded == null) throw new IOException("EPUB cover image is not readable");
220
+ if (decoded.getWidth() <= maxWidth) return decoded;
221
+ float scale = maxWidth / (float) decoded.getWidth();
222
+ Bitmap scaled = Bitmap.createScaledBitmap(decoded, maxWidth, Math.max(1, Math.round(decoded.getHeight() * scale)), true);
223
+ if (scaled != decoded) decoded.recycle();
224
+ return scaled;
225
+ }
226
+ }
227
+ }
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;
@@ -233,6 +236,40 @@ type PapyrusComicDocumentViewProps = ViewProps & {
233
236
  };
234
237
  type PapyrusPageViewComponent = ComponentType<PapyrusPageViewProps & RefAttributes<View>>;
235
238
  type PapyrusPdfViewerViewComponent = ComponentType<PapyrusPdfViewerViewProps & RefAttributes<View>>;
239
+ type PapyrusThumbnailOptions = {
240
+ /** A local document: `file://`, an absolute path or (PDF only) `content://`. Remote URLs are refused. */
241
+ uri: string;
242
+ /** "pdf" | "epub" or a MIME type, when the file name does not say. */
243
+ type?: string;
244
+ /** PDF page to draw. EPUB always yields its cover image. */
245
+ pageIndex?: number;
246
+ maxWidth?: number;
247
+ format?: "jpeg" | "png";
248
+ /** 1–100, JPEG only. */
249
+ quality?: number;
250
+ /** Where to write the image. Defaults to a new file in the app's cache. */
251
+ outputPath?: string;
252
+ };
253
+ type PapyrusThumbnail = {
254
+ uri: string;
255
+ width: number;
256
+ height: number;
257
+ };
258
+ /**
259
+ * Whether `generateThumbnail` works on this build. Android only for now: the
260
+ * iOS implementation ships disabled until it has been validated on a device.
261
+ */
262
+ declare const isThumbnailGenerationSupported: () => boolean;
263
+ /**
264
+ * Draws one page of a PDF, or the cover image of an EPUB, into an image file
265
+ * and returns it. The document is opened, rendered and closed inside the
266
+ * call: no engine, view or WebView is involved and nothing stays in memory.
267
+ * Calls are served one at a time on a background-priority thread.
268
+ *
269
+ * The host owns the policy around it: when to ask, where to keep the files
270
+ * and how to show them.
271
+ */
272
+ declare function generateThumbnail(options: PapyrusThumbnailOptions): Promise<PapyrusThumbnail>;
236
273
  declare const isPapyrusTablet: () => boolean;
237
274
  declare const isPapyrusPdfDocumentViewAvailable: () => boolean;
238
275
  declare const PapyrusPageView: PapyrusPageViewComponent;
@@ -372,6 +409,9 @@ declare const PapyrusTextDocumentView: react.FunctionComponent<ViewProps & {
372
409
  defineSelectionMode?: "selection" | "single-word";
373
410
  annotateLabel?: string;
374
411
  annotationHighlightLabel?: string;
412
+ /** iOS PDF: highlight colours (hex) offered in the selection menu. */
413
+ annotationHighlightColors?: readonly string[];
414
+ annotationHighlightColorLabels?: readonly string[];
375
415
  annotationUnderlineLabel?: string;
376
416
  annotationStrikeoutLabel?: string;
377
417
  annotationSquigglyLabel?: string;
@@ -563,6 +603,9 @@ declare const PapyrusComicDocumentView: react.FunctionComponent<ViewProps & {
563
603
  defineSelectionMode?: "selection" | "single-word";
564
604
  annotateLabel?: string;
565
605
  annotationHighlightLabel?: string;
606
+ /** iOS PDF: highlight colours (hex) offered in the selection menu. */
607
+ annotationHighlightColors?: readonly string[];
608
+ annotationHighlightColorLabels?: readonly string[];
566
609
  annotationUnderlineLabel?: string;
567
610
  annotationStrikeoutLabel?: string;
568
611
  annotationSquigglyLabel?: string;
@@ -635,6 +678,15 @@ declare class NativeDocumentEngine extends BaseDocumentEngine {
635
678
  getOutline(): Promise<OutlineItem[]>;
636
679
  searchText(query: string): Promise<SearchResult[]>;
637
680
  getPageIndex(dest: PageDestination): Promise<number | null>;
681
+ /**
682
+ * Releases the native document (file, pages, parser caches) while the
683
+ * reader is not on screen. Position, zoom and everything the app keeps
684
+ * (progress, annotations) are untouched; call `resume()` before using the
685
+ * engine again. Resolves false where the platform has nothing to release.
686
+ */
687
+ suspend(): Promise<boolean>;
688
+ /** Reopens a document released by `suspend()`. Safe to call when it is already open. */
689
+ resume(): Promise<boolean>;
638
690
  destroy(): void;
639
691
  private assertNativeModule;
640
692
  private normalizeSource;
@@ -820,6 +872,13 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
820
872
  getCurrentTextOffset(): number;
821
873
  goToTextOffset(offset: number): void;
822
874
  getLifecycleStats(): Record<string, number>;
875
+ /**
876
+ * Releases the native PDF document while the reader is not on screen, and
877
+ * reopens it. Only the PDF engine holds a native document; for the other
878
+ * formats both resolve false and nothing changes.
879
+ */
880
+ suspend(): Promise<boolean>;
881
+ resume(): Promise<boolean>;
823
882
  resolveAnnotationTextRange(annotation: Annotation): Promise<{
824
883
  start: number;
825
884
  end: number;
@@ -864,4 +923,4 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
864
923
  destroy(): void;
865
924
  }
866
925
 
867
- export { MobileDocumentEngine, type MobileDocumentEngineOptions, NativeComicDocumentEngine, NativeDocumentEngine, NativeTextDocumentEngine, PapyrusComicDocumentView, type PapyrusComicDocumentViewProps, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, PapyrusTextDocumentView, type PapyrusTextDocumentViewProps, WebViewDocumentEngine, type WebViewRuntimeConfig, type WebViewRuntimeSource, isPapyrusComicDocumentViewAvailable, isPapyrusPdfDocumentViewAvailable, isPapyrusTablet, isPapyrusTextDocumentViewAvailable };
926
+ export { MobileDocumentEngine, type MobileDocumentEngineOptions, NativeComicDocumentEngine, NativeDocumentEngine, NativeTextDocumentEngine, PapyrusComicDocumentView, type PapyrusComicDocumentViewProps, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, PapyrusTextDocumentView, type PapyrusTextDocumentViewProps, type PapyrusThumbnail, type PapyrusThumbnailOptions, WebViewDocumentEngine, type WebViewRuntimeConfig, type WebViewRuntimeSource, generateThumbnail, isPapyrusComicDocumentViewAvailable, isPapyrusPdfDocumentViewAvailable, isPapyrusTablet, isPapyrusTextDocumentViewAvailable, isThumbnailGenerationSupported };
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;
@@ -233,6 +236,40 @@ type PapyrusComicDocumentViewProps = ViewProps & {
233
236
  };
234
237
  type PapyrusPageViewComponent = ComponentType<PapyrusPageViewProps & RefAttributes<View>>;
235
238
  type PapyrusPdfViewerViewComponent = ComponentType<PapyrusPdfViewerViewProps & RefAttributes<View>>;
239
+ type PapyrusThumbnailOptions = {
240
+ /** A local document: `file://`, an absolute path or (PDF only) `content://`. Remote URLs are refused. */
241
+ uri: string;
242
+ /** "pdf" | "epub" or a MIME type, when the file name does not say. */
243
+ type?: string;
244
+ /** PDF page to draw. EPUB always yields its cover image. */
245
+ pageIndex?: number;
246
+ maxWidth?: number;
247
+ format?: "jpeg" | "png";
248
+ /** 1–100, JPEG only. */
249
+ quality?: number;
250
+ /** Where to write the image. Defaults to a new file in the app's cache. */
251
+ outputPath?: string;
252
+ };
253
+ type PapyrusThumbnail = {
254
+ uri: string;
255
+ width: number;
256
+ height: number;
257
+ };
258
+ /**
259
+ * Whether `generateThumbnail` works on this build. Android only for now: the
260
+ * iOS implementation ships disabled until it has been validated on a device.
261
+ */
262
+ declare const isThumbnailGenerationSupported: () => boolean;
263
+ /**
264
+ * Draws one page of a PDF, or the cover image of an EPUB, into an image file
265
+ * and returns it. The document is opened, rendered and closed inside the
266
+ * call: no engine, view or WebView is involved and nothing stays in memory.
267
+ * Calls are served one at a time on a background-priority thread.
268
+ *
269
+ * The host owns the policy around it: when to ask, where to keep the files
270
+ * and how to show them.
271
+ */
272
+ declare function generateThumbnail(options: PapyrusThumbnailOptions): Promise<PapyrusThumbnail>;
236
273
  declare const isPapyrusTablet: () => boolean;
237
274
  declare const isPapyrusPdfDocumentViewAvailable: () => boolean;
238
275
  declare const PapyrusPageView: PapyrusPageViewComponent;
@@ -372,6 +409,9 @@ declare const PapyrusTextDocumentView: react.FunctionComponent<ViewProps & {
372
409
  defineSelectionMode?: "selection" | "single-word";
373
410
  annotateLabel?: string;
374
411
  annotationHighlightLabel?: string;
412
+ /** iOS PDF: highlight colours (hex) offered in the selection menu. */
413
+ annotationHighlightColors?: readonly string[];
414
+ annotationHighlightColorLabels?: readonly string[];
375
415
  annotationUnderlineLabel?: string;
376
416
  annotationStrikeoutLabel?: string;
377
417
  annotationSquigglyLabel?: string;
@@ -563,6 +603,9 @@ declare const PapyrusComicDocumentView: react.FunctionComponent<ViewProps & {
563
603
  defineSelectionMode?: "selection" | "single-word";
564
604
  annotateLabel?: string;
565
605
  annotationHighlightLabel?: string;
606
+ /** iOS PDF: highlight colours (hex) offered in the selection menu. */
607
+ annotationHighlightColors?: readonly string[];
608
+ annotationHighlightColorLabels?: readonly string[];
566
609
  annotationUnderlineLabel?: string;
567
610
  annotationStrikeoutLabel?: string;
568
611
  annotationSquigglyLabel?: string;
@@ -635,6 +678,15 @@ declare class NativeDocumentEngine extends BaseDocumentEngine {
635
678
  getOutline(): Promise<OutlineItem[]>;
636
679
  searchText(query: string): Promise<SearchResult[]>;
637
680
  getPageIndex(dest: PageDestination): Promise<number | null>;
681
+ /**
682
+ * Releases the native document (file, pages, parser caches) while the
683
+ * reader is not on screen. Position, zoom and everything the app keeps
684
+ * (progress, annotations) are untouched; call `resume()` before using the
685
+ * engine again. Resolves false where the platform has nothing to release.
686
+ */
687
+ suspend(): Promise<boolean>;
688
+ /** Reopens a document released by `suspend()`. Safe to call when it is already open. */
689
+ resume(): Promise<boolean>;
638
690
  destroy(): void;
639
691
  private assertNativeModule;
640
692
  private normalizeSource;
@@ -820,6 +872,13 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
820
872
  getCurrentTextOffset(): number;
821
873
  goToTextOffset(offset: number): void;
822
874
  getLifecycleStats(): Record<string, number>;
875
+ /**
876
+ * Releases the native PDF document while the reader is not on screen, and
877
+ * reopens it. Only the PDF engine holds a native document; for the other
878
+ * formats both resolve false and nothing changes.
879
+ */
880
+ suspend(): Promise<boolean>;
881
+ resume(): Promise<boolean>;
823
882
  resolveAnnotationTextRange(annotation: Annotation): Promise<{
824
883
  start: number;
825
884
  end: number;
@@ -864,4 +923,4 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
864
923
  destroy(): void;
865
924
  }
866
925
 
867
- export { MobileDocumentEngine, type MobileDocumentEngineOptions, NativeComicDocumentEngine, NativeDocumentEngine, NativeTextDocumentEngine, PapyrusComicDocumentView, type PapyrusComicDocumentViewProps, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, PapyrusTextDocumentView, type PapyrusTextDocumentViewProps, WebViewDocumentEngine, type WebViewRuntimeConfig, type WebViewRuntimeSource, isPapyrusComicDocumentViewAvailable, isPapyrusPdfDocumentViewAvailable, isPapyrusTablet, isPapyrusTextDocumentViewAvailable };
926
+ export { MobileDocumentEngine, type MobileDocumentEngineOptions, NativeComicDocumentEngine, NativeDocumentEngine, NativeTextDocumentEngine, PapyrusComicDocumentView, type PapyrusComicDocumentViewProps, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, PapyrusTextDocumentView, type PapyrusTextDocumentViewProps, type PapyrusThumbnail, type PapyrusThumbnailOptions, WebViewDocumentEngine, type WebViewRuntimeConfig, type WebViewRuntimeSource, generateThumbnail, isPapyrusComicDocumentViewAvailable, isPapyrusPdfDocumentViewAvailable, isPapyrusTablet, isPapyrusTextDocumentViewAvailable, isThumbnailGenerationSupported };
package/dist/index.js CHANGED
@@ -29,10 +29,12 @@ __export(engine_native_exports, {
29
29
  PapyrusPdfViewerView: () => PapyrusPdfViewerView,
30
30
  PapyrusTextDocumentView: () => PapyrusTextDocumentView,
31
31
  WebViewDocumentEngine: () => WebViewDocumentEngine,
32
+ generateThumbnail: () => generateThumbnail,
32
33
  isPapyrusComicDocumentViewAvailable: () => isPapyrusComicDocumentViewAvailable,
33
34
  isPapyrusPdfDocumentViewAvailable: () => isPapyrusPdfDocumentViewAvailable,
34
35
  isPapyrusTablet: () => isPapyrusTablet,
35
- isPapyrusTextDocumentViewAvailable: () => isPapyrusTextDocumentViewAvailable
36
+ isPapyrusTextDocumentViewAvailable: () => isPapyrusTextDocumentViewAvailable,
37
+ isThumbnailGenerationSupported: () => isThumbnailGenerationSupported
36
38
  });
37
39
  module.exports = __toCommonJS(engine_native_exports);
38
40
  var import_react_native = require("react-native");
@@ -252,6 +254,22 @@ var resolveNativeModule = () => {
252
254
  });
253
255
  };
254
256
  var cachedAndroidTabletFormFactor;
257
+ var isThumbnailGenerationSupported = () => import_react_native.Platform.OS === "android" && typeof resolveNativeModule()?.generateThumbnail === "function";
258
+ async function generateThumbnail(options) {
259
+ if (!isThumbnailGenerationSupported()) {
260
+ throw new Error("[Papyrus] generateThumbnail is not available on this platform or build.");
261
+ }
262
+ if (/^https?:/i.test(options.uri)) {
263
+ throw new Error("[Papyrus] generateThumbnail needs a local document.");
264
+ }
265
+ return resolveNativeModule().generateThumbnail({
266
+ pageIndex: 0,
267
+ maxWidth: 480,
268
+ format: "jpeg",
269
+ quality: 85,
270
+ ...options
271
+ });
272
+ }
255
273
  var isPapyrusTablet = () => {
256
274
  if (import_react_native.Platform.OS === "ios") return import_react_native.Platform.isPad;
257
275
  if (import_react_native.Platform.OS !== "android") return false;
@@ -478,6 +496,19 @@ var NativeDocumentEngine = class extends import_core.BaseDocumentEngine {
478
496
  if (!normalized || normalized.kind !== "named") return null;
479
497
  return native.getPageIndex(this.engineId, normalized);
480
498
  }
499
+ /**
500
+ * Releases the native document (file, pages, parser caches) while the
501
+ * reader is not on screen. Position, zoom and everything the app keeps
502
+ * (progress, annotations) are untouched; call `resume()` before using the
503
+ * engine again. Resolves false where the platform has nothing to release.
504
+ */
505
+ async suspend() {
506
+ return await this.nativeModule?.suspendDocument?.(this.engineId) ?? false;
507
+ }
508
+ /** Reopens a document released by `suspend()`. Safe to call when it is already open. */
509
+ async resume() {
510
+ return await this.nativeModule?.resumeDocument?.(this.engineId) ?? false;
511
+ }
481
512
  destroy() {
482
513
  this.nativeModule?.destroyEngine?.(this.engineId);
483
514
  }
@@ -1349,6 +1380,17 @@ var MobileDocumentEngine = class extends import_core.BaseDocumentEngine {
1349
1380
  ...this.webEngine.getLifecycleStats()
1350
1381
  };
1351
1382
  }
1383
+ /**
1384
+ * Releases the native PDF document while the reader is not on screen, and
1385
+ * reopens it. Only the PDF engine holds a native document; for the other
1386
+ * formats both resolve false and nothing changes.
1387
+ */
1388
+ async suspend() {
1389
+ return this.activeEngine === this.pdfEngine ? this.pdfEngine.suspend() : false;
1390
+ }
1391
+ async resume() {
1392
+ return this.activeEngine === this.pdfEngine ? this.pdfEngine.resume() : false;
1393
+ }
1352
1394
  resolveAnnotationTextRange(annotation) {
1353
1395
  return this.textEngine.resolveAnnotationTextRange(annotation);
1354
1396
  }
@@ -1488,9 +1530,11 @@ var MobileDocumentEngine = class extends import_core.BaseDocumentEngine {
1488
1530
  PapyrusPdfViewerView,
1489
1531
  PapyrusTextDocumentView,
1490
1532
  WebViewDocumentEngine,
1533
+ generateThumbnail,
1491
1534
  isPapyrusComicDocumentViewAvailable,
1492
1535
  isPapyrusPdfDocumentViewAvailable,
1493
1536
  isPapyrusTablet,
1494
- isPapyrusTextDocumentViewAvailable
1537
+ isPapyrusTextDocumentViewAvailable,
1538
+ isThumbnailGenerationSupported
1495
1539
  });
1496
1540
  //# sourceMappingURL=index.js.map