forked from mshockwave/PdfiumAndroid
/
PdfiumCore.java
449 lines (379 loc) · 16.8 KB
/
PdfiumCore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package com.shockwave.pdfium;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.RectF;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.view.Surface;
import com.shockwave.pdfium.util.Size;
import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class PdfiumCore {
private static final String TAG = PdfiumCore.class.getName();
private static final Class FD_CLASS = FileDescriptor.class;
private static final String FD_FIELD_NAME = "descriptor";
static {
try {
System.loadLibrary("c++_shared");
System.loadLibrary("modpng");
System.loadLibrary("modft2");
System.loadLibrary("modpdfium");
System.loadLibrary("jniPdfium");
} catch (UnsatisfiedLinkError e) {
Log.e(TAG, "Native libraries failed to load - " + e);
}
}
private native long nativeOpenDocument(int fd, String password);
private native long nativeOpenMemDocument(byte[] data, String password);
private native void nativeCloseDocument(long docPtr);
private native int nativeGetPageCount(long docPtr);
private native long nativeLoadPage(long docPtr, int pageIndex);
private native long[] nativeLoadPages(long docPtr, int fromIndex, int toIndex);
private native void nativeClosePage(long pagePtr);
private native void nativeClosePages(long[] pagesPtr);
private native int nativeGetPageWidthPixel(long pagePtr, int dpi);
private native int nativeGetPageHeightPixel(long pagePtr, int dpi);
private native int nativeGetPageWidthPoint(long pagePtr);
private native int nativeGetPageHeightPoint(long pagePtr);
//private native long nativeGetNativeWindow(Surface surface);
//private native void nativeRenderPage(long pagePtr, long nativeWindowPtr);
private native void nativeRenderPage(long pagePtr, Surface surface, int dpi,
int startX, int startY,
int drawSizeHor, int drawSizeVer,
boolean renderAnnot);
private native void nativeRenderPageBitmap(long pagePtr, Bitmap bitmap, int dpi,
int startX, int startY,
int drawSizeHor, int drawSizeVer,
boolean renderAnnot);
private native String nativeGetDocumentMetaText(long docPtr, String tag);
private native Long nativeGetFirstChildBookmark(long docPtr, Long bookmarkPtr);
private native Long nativeGetSiblingBookmark(long docPtr, long bookmarkPtr);
private native String nativeGetBookmarkTitle(long bookmarkPtr);
private native long nativeGetBookmarkDestIndex(long docPtr, long bookmarkPtr);
private native Size nativeGetPageSizeByIndex(long docPtr, int pageIndex, int dpi);
private native long[] nativeGetPageLinks(long pagePtr);
private native Integer nativeGetDestPageIndex(long docPtr, long linkPtr);
private native String nativeGetLinkURI(long docPtr, long linkPtr);
private native RectF nativeGetLinkRect(long linkPtr);
private native Point nativePageCoordsToDevice(long pagePtr, int startX, int startY, int sizeX,
int sizeY, int rotate, double pageX, double pageY);
/* synchronize native methods */
private static final Object lock = new Object();
private static Field mFdField = null;
private int mCurrentDpi;
public static int getNumFd(ParcelFileDescriptor fdObj) {
try {
if (mFdField == null) {
mFdField = FD_CLASS.getDeclaredField(FD_FIELD_NAME);
mFdField.setAccessible(true);
}
return mFdField.getInt(fdObj.getFileDescriptor());
} catch (NoSuchFieldException e) {
e.printStackTrace();
return -1;
} catch (IllegalAccessException e) {
e.printStackTrace();
return -1;
}
}
/** Context needed to get screen density */
public PdfiumCore(Context ctx) {
mCurrentDpi = ctx.getResources().getDisplayMetrics().densityDpi;
Log.d(TAG, "Starting PdfiumAndroid " + BuildConfig.VERSION_NAME);
}
/** Create new document from file */
public PdfDocument newDocument(ParcelFileDescriptor fd) throws IOException {
return newDocument(fd, null);
}
/** Create new document from file with password */
public PdfDocument newDocument(ParcelFileDescriptor fd, String password) throws IOException {
PdfDocument document = new PdfDocument();
document.parcelFileDescriptor = fd;
synchronized (lock) {
document.mNativeDocPtr = nativeOpenDocument(getNumFd(fd), password);
}
return document;
}
/** Create new document from bytearray */
public PdfDocument newDocument(byte[] data) throws IOException {
return newDocument(data, null);
}
/** Create new document from bytearray with password */
public PdfDocument newDocument(byte[] data, String password) throws IOException {
PdfDocument document = new PdfDocument();
synchronized (lock) {
document.mNativeDocPtr = nativeOpenMemDocument(data, password);
}
return document;
}
/** Get total numer of pages in document */
public int getPageCount(PdfDocument doc) {
synchronized (lock) {
return nativeGetPageCount(doc.mNativeDocPtr);
}
}
/** Open page and store native pointer in {@link PdfDocument} */
public long openPage(PdfDocument doc, int pageIndex) {
long pagePtr;
synchronized (lock) {
pagePtr = nativeLoadPage(doc.mNativeDocPtr, pageIndex);
doc.mNativePagesPtr.put(pageIndex, pagePtr);
return pagePtr;
}
}
/** Open range of pages and store native pointers in {@link PdfDocument} */
public long[] openPage(PdfDocument doc, int fromIndex, int toIndex) {
long[] pagesPtr;
synchronized (lock) {
pagesPtr = nativeLoadPages(doc.mNativeDocPtr, fromIndex, toIndex);
int pageIndex = fromIndex;
for (long page : pagesPtr) {
if (pageIndex > toIndex) break;
doc.mNativePagesPtr.put(pageIndex, page);
pageIndex++;
}
return pagesPtr;
}
}
/**
* Get page width in pixels. <br>
* This method requires page to be opened.
*/
public int getPageWidth(PdfDocument doc, int index) {
synchronized (lock) {
Long pagePtr;
if ((pagePtr = doc.mNativePagesPtr.get(index)) != null) {
return nativeGetPageWidthPixel(pagePtr, mCurrentDpi);
}
return 0;
}
}
/**
* Get page height in pixels. <br>
* This method requires page to be opened.
*/
public int getPageHeight(PdfDocument doc, int index) {
synchronized (lock) {
Long pagePtr;
if ((pagePtr = doc.mNativePagesPtr.get(index)) != null) {
return nativeGetPageHeightPixel(pagePtr, mCurrentDpi);
}
return 0;
}
}
/**
* Get page width in PostScript points (1/72th of an inch).<br>
* This method requires page to be opened.
*/
public int getPageWidthPoint(PdfDocument doc, int index) {
synchronized (lock) {
Long pagePtr;
if ((pagePtr = doc.mNativePagesPtr.get(index)) != null) {
return nativeGetPageWidthPoint(pagePtr);
}
return 0;
}
}
/**
* Get page height in PostScript points (1/72th of an inch).<br>
* This method requires page to be opened.
*/
public int getPageHeightPoint(PdfDocument doc, int index) {
synchronized (lock) {
Long pagePtr;
if ((pagePtr = doc.mNativePagesPtr.get(index)) != null) {
return nativeGetPageHeightPoint(pagePtr);
}
return 0;
}
}
/**
* Get size of page in pixels.<br>
* This method does not require given page to be opened.
*/
public Size getPageSize(PdfDocument doc, int index) {
synchronized (lock) {
return nativeGetPageSizeByIndex(doc.mNativeDocPtr, index, mCurrentDpi);
}
}
/**
* Render page fragment on {@link Surface}.<br>
* Page must be opened before rendering.
*/
public void renderPage(PdfDocument doc, Surface surface, int pageIndex,
int startX, int startY, int drawSizeX, int drawSizeY) {
renderPage(doc, surface, pageIndex, startX, startY, drawSizeX, drawSizeY, false);
}
/**
* Render page fragment on {@link Surface}. This method allows to render annotations.<br>
* Page must be opened before rendering.
*/
public void renderPage(PdfDocument doc, Surface surface, int pageIndex,
int startX, int startY, int drawSizeX, int drawSizeY,
boolean renderAnnot) {
synchronized (lock) {
try {
//nativeRenderPage(doc.mNativePagesPtr.get(pageIndex), surface, mCurrentDpi);
nativeRenderPage(doc.mNativePagesPtr.get(pageIndex), surface, mCurrentDpi,
startX, startY, drawSizeX, drawSizeY, renderAnnot);
} catch (NullPointerException e) {
Log.e(TAG, "mContext may be null");
e.printStackTrace();
} catch (Exception e) {
Log.e(TAG, "Exception throw from native");
e.printStackTrace();
}
}
}
/**
* Render page fragment on {@link Bitmap}.<br>
* Page must be opened before rendering.
* <p>
* Supported bitmap configurations:
* <ul>
* <li>ARGB_8888 - best quality, high memory usage, higher possibility of OutOfMemoryError
* <li>RGB_565 - little worse quality, twice less memory usage
* </ul>
*/
public void renderPageBitmap(PdfDocument doc, Bitmap bitmap, int pageIndex,
int startX, int startY, int drawSizeX, int drawSizeY) {
renderPageBitmap(doc, bitmap, pageIndex, startX, startY, drawSizeX, drawSizeY, false);
}
/**
* Render page fragment on {@link Bitmap}. This method allows to render annotations.<br>
* Page must be opened before rendering.
* <p>
* For more info see {@link PdfiumCore#renderPageBitmap(PdfDocument, Bitmap, int, int, int, int, int)}
*/
public void renderPageBitmap(PdfDocument doc, Bitmap bitmap, int pageIndex,
int startX, int startY, int drawSizeX, int drawSizeY,
boolean renderAnnot) {
synchronized (lock) {
try {
nativeRenderPageBitmap(doc.mNativePagesPtr.get(pageIndex), bitmap, mCurrentDpi,
startX, startY, drawSizeX, drawSizeY, renderAnnot);
} catch (NullPointerException e) {
Log.e(TAG, "mContext may be null");
e.printStackTrace();
} catch (Exception e) {
Log.e(TAG, "Exception throw from native");
e.printStackTrace();
}
}
}
/** Release native resources and opened file */
public void closeDocument(PdfDocument doc) {
synchronized (lock) {
for (Integer index : doc.mNativePagesPtr.keySet()) {
nativeClosePage(doc.mNativePagesPtr.get(index));
}
doc.mNativePagesPtr.clear();
nativeCloseDocument(doc.mNativeDocPtr);
if (doc.parcelFileDescriptor != null) { //if document was loaded from file
try {
doc.parcelFileDescriptor.close();
} catch (IOException e) {
/* ignore */
}
doc.parcelFileDescriptor = null;
}
}
}
/** Get metadata for given document */
public PdfDocument.Meta getDocumentMeta(PdfDocument doc) {
synchronized (lock) {
PdfDocument.Meta meta = new PdfDocument.Meta();
meta.title = nativeGetDocumentMetaText(doc.mNativeDocPtr, "Title");
meta.author = nativeGetDocumentMetaText(doc.mNativeDocPtr, "Author");
meta.subject = nativeGetDocumentMetaText(doc.mNativeDocPtr, "Subject");
meta.keywords = nativeGetDocumentMetaText(doc.mNativeDocPtr, "Keywords");
meta.creator = nativeGetDocumentMetaText(doc.mNativeDocPtr, "Creator");
meta.producer = nativeGetDocumentMetaText(doc.mNativeDocPtr, "Producer");
meta.creationDate = nativeGetDocumentMetaText(doc.mNativeDocPtr, "CreationDate");
meta.modDate = nativeGetDocumentMetaText(doc.mNativeDocPtr, "ModDate");
return meta;
}
}
/** Get table of contents (bookmarks) for given document */
public List<PdfDocument.Bookmark> getTableOfContents(PdfDocument doc) {
synchronized (lock) {
List<PdfDocument.Bookmark> topLevel = new ArrayList<>();
Long first = nativeGetFirstChildBookmark(doc.mNativeDocPtr, null);
if (first != null) {
recursiveGetBookmark(topLevel, doc, first);
}
return topLevel;
}
}
private void recursiveGetBookmark(List<PdfDocument.Bookmark> tree, PdfDocument doc, long bookmarkPtr) {
PdfDocument.Bookmark bookmark = new PdfDocument.Bookmark();
bookmark.mNativePtr = bookmarkPtr;
bookmark.title = nativeGetBookmarkTitle(bookmarkPtr);
bookmark.pageIdx = nativeGetBookmarkDestIndex(doc.mNativeDocPtr, bookmarkPtr);
tree.add(bookmark);
Long child = nativeGetFirstChildBookmark(doc.mNativeDocPtr, bookmarkPtr);
if (child != null) {
recursiveGetBookmark(bookmark.getChildren(), doc, child);
}
Long sibling = nativeGetSiblingBookmark(doc.mNativeDocPtr, bookmarkPtr);
if (sibling != null) {
recursiveGetBookmark(tree, doc, sibling);
}
}
/** Get all links from given page */
public List<PdfDocument.Link> getPageLinks(PdfDocument doc, int pageIndex) {
synchronized (lock) {
List<PdfDocument.Link> links = new ArrayList<>();
Long nativePagePtr = doc.mNativePagesPtr.get(pageIndex);
if (nativePagePtr == null) {
return links;
}
long[] linkPtrs = nativeGetPageLinks(nativePagePtr);
for (long linkPtr : linkPtrs) {
Integer index = nativeGetDestPageIndex(doc.mNativeDocPtr, linkPtr);
String uri = nativeGetLinkURI(doc.mNativeDocPtr, linkPtr);
RectF rect = nativeGetLinkRect(linkPtr);
if (rect != null && (index != null || uri != null)) {
links.add(new PdfDocument.Link(rect, index, uri));
}
}
return links;
}
}
/**
* Map page coordinates to device screen coordinates
*
* @param doc pdf document
* @param pageIndex index of page
* @param startX left pixel position of the display area in device coordinates
* @param startY top pixel position of the display area in device coordinates
* @param sizeX horizontal size (in pixels) for displaying the page
* @param sizeY vertical size (in pixels) for displaying the page
* @param rotate page orientation: 0 (normal), 1 (rotated 90 degrees clockwise),
* 2 (rotated 180 degrees), 3 (rotated 90 degrees counter-clockwise)
* @param pageX X value in page coordinates
* @param pageY Y value in page coordinate
* @return mapped coordinates
*/
public Point mapPageCoordsToDevice(PdfDocument doc, int pageIndex, int startX, int startY, int sizeX,
int sizeY, int rotate, double pageX, double pageY) {
long pagePtr = doc.mNativePagesPtr.get(pageIndex);
return nativePageCoordsToDevice(pagePtr, startX, startY, sizeX, sizeY, rotate, pageX, pageY);
}
/**
* @return mapped coordinates
* @see PdfiumCore#mapPageCoordsToDevice(PdfDocument, int, int, int, int, int, int, double, double)
*/
public RectF mapRectToDevice(PdfDocument doc, int pageIndex, int startX, int startY, int sizeX,
int sizeY, int rotate, RectF coords) {
Point leftTop = mapPageCoordsToDevice(doc, pageIndex, startX, startY, sizeX, sizeY, rotate,
coords.left, coords.top);
Point rightBottom = mapPageCoordsToDevice(doc, pageIndex, startX, startY, sizeX, sizeY, rotate,
coords.right, coords.bottom);
return new RectF(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y);
}
}