-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathPdfiumCore.java
More file actions
156 lines (136 loc) · 5.69 KB
/
PdfiumCore.java
File metadata and controls
156 lines (136 loc) · 5.69 KB
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
package com.shockwave.pdfium;
import android.content.Context;
import android.util.Log;
import android.view.Surface;
import android.graphics.RectF;
import android.graphics.PointF;
import java.io.FileDescriptor;
import java.lang.reflect.Field;
public class PdfiumCore {
private static final String TAG = PdfiumCore.class.getName();
static{
System.loadLibrary("jniPdfium");
}
private native long nativeOpenDocument(int fd);
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 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);
// Text Module
public static native int textLoadPage(long page);
public static native int textFindStart(int textPage, String findWhat, long flags, int startIndex);
public static native int textFindNext(int handle);
public static native int textFindPrev(int handle);
public static native int textGetSchResultIndex(int handle);
public static native int textGetSchCount(int handle);
public static native String textGetText(int textPage, int start, int count);
public static native RectF textGetRect(int textPage, int index);
public static native int textCountRects(int textPage, int start, int count);
public static native int textCountChars(int textPage);
public static native void textFindClose(int handle);
public static native void textClosePage(int textPage);
private static final Class FD_CLASS = FileDescriptor.class;
private static final String FD_FIELD_NAME = "descriptor";
private static Field mFdField = null;
private int mCurrentDpi;
public PdfiumCore(Context ctx){
mCurrentDpi = ctx.getResources().getDisplayMetrics().densityDpi;
}
public static int getNumFd(FileDescriptor fdObj){
try{
if(mFdField == null){
mFdField = FD_CLASS.getDeclaredField(FD_FIELD_NAME);
mFdField.setAccessible(true);
}
return mFdField.getInt(fdObj);
}catch(NoSuchFieldException e){
e.printStackTrace();
return -1;
} catch (IllegalAccessException e) {
e.printStackTrace();
return -1;
}
}
public PdfDocument newDocument(FileDescriptor fd){
PdfDocument document = new PdfDocument();
document.mNativeDocPtr = nativeOpenDocument(getNumFd(fd));
if(document.mNativeDocPtr <= 0) Log.e(TAG, "Open document failed");
return document;
}
public int getPageCount(PdfDocument doc){
synchronized (doc.Lock){
return nativeGetPageCount(doc.mNativeDocPtr);
}
}
public long openPage(PdfDocument doc, int pageIndex){
synchronized (doc.Lock){
long pagePtr = nativeLoadPage(doc.mNativeDocPtr, pageIndex);
doc.mNativePagesPtr.put(pageIndex, pagePtr);
return pagePtr;
}
}
public long[] openPage(PdfDocument doc, int fromIndex, int toIndex){
synchronized (doc.Lock){
long[] 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;
}
}
public int getPageWidth(PdfDocument doc, int index){
synchronized (doc.Lock){
Long pagePtr;
if( (pagePtr = doc.mNativePagesPtr.get(index)) != null ){
return nativeGetPageWidthPixel(pagePtr, mCurrentDpi);
}
return 0;
}
}
public int getPageHeight(PdfDocument doc, int index){
synchronized (doc.Lock){
Long pagePtr;
if( (pagePtr = doc.mNativePagesPtr.get(index)) != null ){
return nativeGetPageHeightPixel(pagePtr, mCurrentDpi);
}
return 0;
}
}
public void renderPage(PdfDocument doc, Surface surface, int pageIndex,
int startX, int startY, int drawSizeX, int drawSizeY){
synchronized (doc.Lock){
try{
//nativeRenderPage(doc.mNativePagesPtr.get(pageIndex), surface, mCurrentDpi);
nativeRenderPage(doc.mNativePagesPtr.get(pageIndex), surface, mCurrentDpi,
startX, startY, drawSizeX, drawSizeY);
}catch(NullPointerException e){
Log.e(TAG, "mContext may be null");
e.printStackTrace();
}catch(Exception e){
Log.e(TAG, "Exception throw from native");
e.printStackTrace();
}
}
}
public void closeDocument(PdfDocument doc){
synchronized (doc.Lock){
for(Integer index : doc.mNativePagesPtr.keySet()){
nativeClosePage(doc.mNativePagesPtr.get(index));
}
doc.mNativePagesPtr.clear();
nativeCloseDocument(doc.mNativeDocPtr);
}
}
}