]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/mupdf/MuPDFCore.java
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / src / main / java / cz / cvut / fel / dce / qrscanner / mupdf / MuPDFCore.java
1 package cz.cvut.fel.dce.qrscanner.mupdf;
2 import java.util.ArrayList;
3
4 import android.content.Context;
5 import android.graphics.Bitmap;
6 import android.graphics.PointF;
7 import android.graphics.RectF;
8
9 import cz.cvut.fel.dce.qrscanner.R;
10
11 public class MuPDFCore
12 {
13         /* load our native library */
14         static {
15                 System.loadLibrary("mupdf");
16         }
17
18         /* Readable members */
19         private int numPages = -1;
20         private float pageWidth;
21         private float pageHeight;
22         private long globals;
23         private byte fileBuffer[];
24         private String file_format;
25         private boolean isUnencryptedPDF;
26         private final boolean wasOpenedFromBuffer;
27
28         /* The native functions */
29         private native long openFile(String filename);
30         private native long openBuffer(String magic);
31         private native String fileFormatInternal();
32         private native boolean isUnencryptedPDFInternal();
33         private native int countPagesInternal();
34         private native void gotoPageInternal(int localActionPageNum);
35         private native float getPageWidth();
36         private native float getPageHeight();
37         private native void drawPage(Bitmap bitmap,
38                         int pageW, int pageH,
39                         int patchX, int patchY,
40                         int patchW, int patchH,
41                         long cookiePtr);
42         private native void updatePageInternal(Bitmap bitmap,
43                         int page,
44                         int pageW, int pageH,
45                         int patchX, int patchY,
46                         int patchW, int patchH,
47                         long cookiePtr);
48         private native RectF[] searchPage(String text);
49         private native TextChar[][][][] text();
50         private native byte[] textAsHtml();
51         private native void addMarkupAnnotationInternal(PointF[] quadPoints, int type);
52         private native void addInkAnnotationInternal(PointF[][] arcs);
53         private native void deleteAnnotationInternal(int annot_index);
54         private native int passClickEventInternal(int page, float x, float y);
55         private native void setFocusedWidgetChoiceSelectedInternal(String [] selected);
56         private native String [] getFocusedWidgetChoiceSelected();
57         private native String [] getFocusedWidgetChoiceOptions();
58         private native int getFocusedWidgetSignatureState();
59         private native String checkFocusedSignatureInternal();
60         private native boolean signFocusedSignatureInternal(String keyFile, String password);
61         private native int setFocusedWidgetTextInternal(String text);
62         private native String getFocusedWidgetTextInternal();
63         private native int getFocusedWidgetTypeInternal();
64         private native LinkInfo [] getPageLinksInternal(int page);
65         private native RectF[] getWidgetAreasInternal(int page);
66         private native Annotation[] getAnnotationsInternal(int page);
67         private native OutlineItem [] getOutlineInternal();
68         private native boolean hasOutlineInternal();
69         private native boolean needsPasswordInternal();
70         private native boolean authenticatePasswordInternal(String password);
71         private native MuPDFAlertInternal waitForAlertInternal();
72         private native void replyToAlertInternal(MuPDFAlertInternal alert);
73         private native void startAlertsInternal();
74         private native void stopAlertsInternal();
75         private native void destroying();
76         private native boolean hasChangesInternal();
77         private native void saveInternal();
78         private native long createCookie();
79         private native void destroyCookie(long cookie);
80         private native void abortCookie(long cookie);
81
82         public native boolean javascriptSupported();
83
84         public class Cookie
85         {
86                 private final long cookiePtr;
87
88                 public Cookie()
89                 {
90                         cookiePtr = createCookie();
91                         if (cookiePtr == 0)
92                                 throw new OutOfMemoryError();
93                 }
94
95                 public void abort()
96                 {
97                         abortCookie(cookiePtr);
98                 }
99
100                 public void destroy()
101                 {
102                         // We could do this in finalize, but there's no guarantee that
103                         // a finalize will occur before the muPDF context occurs.
104                         destroyCookie(cookiePtr);
105                 }
106         }
107
108         public MuPDFCore(Context context, String filename) throws Exception
109         {
110                 globals = openFile(filename);
111                 if (globals == 0)
112                 {
113                         throw new Exception(String.format(context.getString(R.string.cannot_open_file_Path), filename));
114                 }
115                 file_format = fileFormatInternal();
116                 isUnencryptedPDF = isUnencryptedPDFInternal();
117                 wasOpenedFromBuffer = false;
118         }
119
120         public MuPDFCore(Context context, byte buffer[], String magic) throws Exception {
121                 fileBuffer = buffer;
122                 globals = openBuffer(magic != null ? magic : "");
123                 if (globals == 0)
124                 {
125                         throw new Exception(context.getString(R.string.cannot_open_buffer));
126                 }
127                 file_format = fileFormatInternal();
128                 isUnencryptedPDF = isUnencryptedPDFInternal();
129                 wasOpenedFromBuffer = true;
130         }
131
132         public  int countPages()
133         {
134                 if (numPages < 0)
135                         numPages = countPagesSynchronized();
136
137                 return numPages;
138         }
139
140         public String fileFormat()
141         {
142                 return file_format;
143         }
144
145         public boolean isUnencryptedPDF()
146         {
147                 return isUnencryptedPDF;
148         }
149
150         public boolean wasOpenedFromBuffer()
151         {
152                 return wasOpenedFromBuffer;
153         }
154
155         private synchronized int countPagesSynchronized() {
156                 return countPagesInternal();
157         }
158
159         /* Shim function */
160         private void gotoPage(int page)
161         {
162                 if (page > numPages-1)
163                         page = numPages-1;
164                 else if (page < 0)
165                         page = 0;
166                 gotoPageInternal(page);
167                 this.pageWidth = getPageWidth();
168                 this.pageHeight = getPageHeight();
169         }
170
171         public synchronized PointF getPageSize(int page) {
172                 gotoPage(page);
173                 return new PointF(pageWidth, pageHeight);
174         }
175
176         public MuPDFAlert waitForAlert() {
177                 MuPDFAlertInternal alert = waitForAlertInternal();
178                 return alert != null ? alert.toAlert() : null;
179         }
180
181         public void replyToAlert(MuPDFAlert alert) {
182                 replyToAlertInternal(new MuPDFAlertInternal(alert));
183         }
184
185         public void stopAlerts() {
186                 stopAlertsInternal();
187         }
188
189         public void startAlerts() {
190                 startAlertsInternal();
191         }
192
193         public synchronized void onDestroy() {
194                 destroying();
195                 globals = 0;
196         }
197
198         public synchronized void drawPage(Bitmap bm, int page,
199                         int pageW, int pageH,
200                         int patchX, int patchY,
201                         int patchW, int patchH,
202                         MuPDFCore.Cookie cookie) {
203                 gotoPage(page);
204                 drawPage(bm, pageW, pageH, patchX, patchY, patchW, patchH, cookie.cookiePtr);
205         }
206
207         public synchronized void updatePage(Bitmap bm, int page,
208                         int pageW, int pageH,
209                         int patchX, int patchY,
210                         int patchW, int patchH,
211                         MuPDFCore.Cookie cookie) {
212                 updatePageInternal(bm, page, pageW, pageH, patchX, patchY, patchW, patchH, cookie.cookiePtr);
213         }
214
215         public synchronized PassClickResult passClickEvent(int page, float x, float y) {
216                 boolean changed = passClickEventInternal(page, x, y) != 0;
217
218                 switch (WidgetType.values()[getFocusedWidgetTypeInternal()])
219                 {
220                 case TEXT:
221                         return new PassClickResultText(changed, getFocusedWidgetTextInternal());
222                 case LISTBOX:
223                 case COMBOBOX:
224                         return new PassClickResultChoice(changed, getFocusedWidgetChoiceOptions(), getFocusedWidgetChoiceSelected());
225                 case SIGNATURE:
226                         return new PassClickResultSignature(changed, getFocusedWidgetSignatureState());
227                 default:
228                         return new PassClickResult(changed);
229                 }
230
231         }
232
233         public synchronized boolean setFocusedWidgetText(int page, String text) {
234                 boolean success;
235                 gotoPage(page);
236                 success = setFocusedWidgetTextInternal(text) != 0 ? true : false;
237
238                 return success;
239         }
240
241         public synchronized void setFocusedWidgetChoiceSelected(String [] selected) {
242                 setFocusedWidgetChoiceSelectedInternal(selected);
243         }
244
245         public synchronized String checkFocusedSignature() {
246                 return checkFocusedSignatureInternal();
247         }
248
249         public synchronized boolean signFocusedSignature(String keyFile, String password) {
250                 return signFocusedSignatureInternal(keyFile, password);
251         }
252
253         public synchronized LinkInfo [] getPageLinks(int page) {
254                 return getPageLinksInternal(page);
255         }
256
257         public synchronized RectF [] getWidgetAreas(int page) {
258                 return getWidgetAreasInternal(page);
259         }
260
261         public synchronized Annotation [] getAnnoations(int page) {
262                 return getAnnotationsInternal(page);
263         }
264
265         public synchronized RectF [] searchPage(int page, String text) {
266                 gotoPage(page);
267                 return searchPage(text);
268         }
269
270         public synchronized byte[] html(int page) {
271                 gotoPage(page);
272                 return textAsHtml();
273         }
274
275         public synchronized TextWord [][] textLines(int page) {
276                 gotoPage(page);
277                 TextChar[][][][] chars = text();
278
279                 // The text of the page held in a hierarchy (blocks, lines, spans).
280                 // Currently we don't need to distinguish the blocks level or
281                 // the spans, and we need to collect the text into words.
282                 ArrayList<TextWord[]> lns = new ArrayList<TextWord[]>();
283
284                 for (TextChar[][][] bl: chars) {
285                         if (bl == null)
286                                 continue;
287                         for (TextChar[][] ln: bl) {
288                                 ArrayList<TextWord> wds = new ArrayList<TextWord>();
289                                 TextWord wd = new TextWord();
290
291                                 for (TextChar[] sp: ln) {
292                                         for (TextChar tc: sp) {
293                                                 if (tc.c != ' ') {
294                                                         wd.Add(tc);
295                                                 } else if (wd.w.length() > 0) {
296                                                         wds.add(wd);
297                                                         wd = new TextWord();
298                                                 }
299                                         }
300                                 }
301
302                                 if (wd.w.length() > 0)
303                                         wds.add(wd);
304
305                                 if (wds.size() > 0)
306                                         lns.add(wds.toArray(new TextWord[wds.size()]));
307                         }
308                 }
309
310                 return lns.toArray(new TextWord[lns.size()][]);
311         }
312
313         public synchronized void addMarkupAnnotation(int page, PointF[] quadPoints, Annotation.Type type) {
314                 gotoPage(page);
315                 addMarkupAnnotationInternal(quadPoints, type.ordinal());
316         }
317
318         public synchronized void addInkAnnotation(int page, PointF[][] arcs) {
319                 gotoPage(page);
320                 addInkAnnotationInternal(arcs);
321         }
322
323         public synchronized void deleteAnnotation(int page, int annot_index) {
324                 gotoPage(page);
325                 deleteAnnotationInternal(annot_index);
326         }
327
328         public synchronized boolean hasOutline() {
329                 return hasOutlineInternal();
330         }
331
332         public synchronized OutlineItem [] getOutline() {
333                 return getOutlineInternal();
334         }
335
336         public synchronized boolean needsPassword() {
337                 return needsPasswordInternal();
338         }
339
340         public synchronized boolean authenticatePassword(String password) {
341                 return authenticatePasswordInternal(password);
342         }
343
344         public synchronized boolean hasChanges() {
345                 return hasChangesInternal();
346         }
347
348         public synchronized void save() {
349                 saveInternal();
350         }
351 }