summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2011-12-21 17:57:53 +0000
committerRobin Watts <robin.watts@artifex.com>2011-12-21 17:57:53 +0000
commite465b579a4137757ca5239a9ca4c51a930a6a363 (patch)
tree292a9168cd459987d2a74365502fd291a259b98e
parent5474e8ea6bcc637357911264358085704855ffad (diff)
downloadmupdf-e465b579a4137757ca5239a9ca4c51a930a6a363.tar.xz
Bring the Android demo port up to date.
Hadn't been updated to allow for context/exceptions.
-rw-r--r--android/jni/Core.mk5
-rw-r--r--android/jni/mupdf.c207
2 files changed, 129 insertions, 83 deletions
diff --git a/android/jni/Core.mk b/android/jni/Core.mk
index 7116d57a..259f0398 100644
--- a/android/jni/Core.mk
+++ b/android/jni/Core.mk
@@ -20,6 +20,7 @@ LOCAL_C_INCLUDES := \
LOCAL_MODULE := mupdfcore
LOCAL_SRC_FILES := \
+ $(MY_ROOT)/fitz/base_context.c \
$(MY_ROOT)/fitz/base_error.c \
$(MY_ROOT)/fitz/base_geometry.c \
$(MY_ROOT)/fitz/base_getopt.c \
@@ -37,6 +38,7 @@ LOCAL_SRC_FILES := \
$(MY_ROOT)/fitz/dev_null.c \
$(MY_ROOT)/fitz/dev_text.c \
$(MY_ROOT)/fitz/dev_trace.c \
+ $(MY_ROOT)/fitz/doc_outline.c \
$(MY_ROOT)/fitz/filt_basic.c \
$(MY_ROOT)/fitz/filt_dctd.c \
$(MY_ROOT)/fitz/filt_faxd.c \
@@ -45,12 +47,12 @@ LOCAL_SRC_FILES := \
$(MY_ROOT)/fitz/filt_jpxd.c \
$(MY_ROOT)/fitz/filt_lzwd.c \
$(MY_ROOT)/fitz/filt_predict.c \
- $(MY_ROOT)/fitz/obj_print.c \
$(MY_ROOT)/fitz/res_colorspace.c \
$(MY_ROOT)/fitz/res_font.c \
$(MY_ROOT)/fitz/res_path.c \
$(MY_ROOT)/fitz/res_pixmap.c \
$(MY_ROOT)/fitz/res_shade.c \
+ $(MY_ROOT)/fitz/res_store.c \
$(MY_ROOT)/fitz/res_text.c \
$(MY_ROOT)/fitz/stm_buffer.c \
$(MY_ROOT)/fitz/stm_open.c \
@@ -88,7 +90,6 @@ LOCAL_SRC_FILES := \
$(MY_ROOT)/pdf/pdf_pattern.c \
$(MY_ROOT)/pdf/pdf_repair.c \
$(MY_ROOT)/pdf/pdf_shade.c \
- $(MY_ROOT)/pdf/pdf_store.c \
$(MY_ROOT)/pdf/pdf_stream.c \
$(MY_ROOT)/pdf/pdf_type3.c \
$(MY_ROOT)/pdf/pdf_unicode.c \
diff --git a/android/jni/mupdf.c b/android/jni/mupdf.c
index a3d45d8d..c7f06252 100644
--- a/android/jni/mupdf.c
+++ b/android/jni/mupdf.c
@@ -31,6 +31,7 @@ float pageHeight = 100;
fz_display_list *currentPageList;
fz_rect currentMediabox;
int currentRotate;
+fz_context *ctx;
JNIEXPORT int JNICALL
Java_com_artifex_mupdf_MuPDFCore_openFile(JNIEnv * env, jobject thiz, jstring jfilename)
@@ -38,8 +39,7 @@ Java_com_artifex_mupdf_MuPDFCore_openFile(JNIEnv * env, jobject thiz, jstring jf
const char *filename;
char *password = "";
int accelerate = 1;
- fz_error error;
- int pages;
+ int pages = 0;
filename = (*env)->GetStringUTFChars(env, jfilename, NULL);
if (filename == NULL)
@@ -50,26 +50,53 @@ Java_com_artifex_mupdf_MuPDFCore_openFile(JNIEnv * env, jobject thiz, jstring jf
if (accelerate)
fz_accelerate();
- glyphcache = fz_new_glyph_cache();
- colorspace = fz_device_rgb;
- LOGE("Opening document...");
- error = pdf_open_xref(&xref, filename, password);
- if (error)
+ ctx = fz_new_context(&fz_alloc_default, FZ_STORE_UNLIMITED);
+ if (!ctx)
{
- LOGE("Cannot open document: '%s'\n", filename);
+ LOGE("Failed to initialise context");
return 0;
}
- LOGE("Loading page tree...");
- error = pdf_load_page_tree(xref);
- if (error)
+ xref = NULL;
+ glyphcache = NULL;
+ fz_try(ctx)
{
- LOGE("Cannot load page tree: '%s'\n", filename);
- return 0;
+ glyphcache = fz_new_glyph_cache(ctx);
+ colorspace = fz_device_rgb;
+
+ LOGE("Opening document...");
+ fz_try(ctx)
+ {
+ xref = pdf_open_xref(ctx, filename, password);
+ }
+ fz_catch(ctx)
+ {
+ fz_throw(ctx, "Cannot open document: '%s'\n", filename);
+ }
+
+ LOGE("Loading page tree...");
+ fz_try(ctx)
+ {
+ pdf_load_page_tree(xref);
+ }
+ fz_catch(ctx)
+ {
+ fz_throw(ctx, "Cannot load page tree: '%s'\n", filename);
+ }
+ pages = pdf_count_pages(xref);
+ LOGE("Done! %d pages", pages);
+ }
+ fz_catch(ctx)
+ {
+ LOGE("Failed: %s", ctx->error->message);
+ pdf_free_xref(xref);
+ xref = NULL;
+ fz_free_glyph_cache(ctx, glyphcache);
+ glyphcache = NULL;
+ fz_free_context(ctx);
+ ctx = NULL;
}
- pages = pdf_count_pages(xref);
- LOGE("Done! %d pages", pages);
return pages;
}
@@ -80,41 +107,48 @@ Java_com_artifex_mupdf_MuPDFCore_gotoPageInternal(JNIEnv *env, jobject thiz, int
float zoom;
fz_matrix ctm;
fz_bbox bbox;
- fz_error error;
- fz_device *dev;
- pdf_page *currentPage;
+ fz_device *dev = NULL;
+ pdf_page *currentPage = NULL;
+
+ fz_var(dev);
+ fz_var(currentPage);
/* In the event of an error, ensure we give a non-empty page */
pageWidth = 100;
pageHeight = 100;
LOGE("Goto page %d...", page);
- if (currentPageList != NULL)
+ fz_try(ctx)
{
- fz_free_display_list(currentPageList);
- currentPageList = NULL;
+ if (currentPageList != NULL)
+ {
+ fz_free_display_list(ctx, currentPageList);
+ currentPageList = NULL;
+ }
+ pagenum = page;
+ currentPage = pdf_load_page(xref, pagenum);
+ zoom = resolution / 72;
+ currentMediabox = currentPage->mediabox;
+ currentRotate = currentPage->rotate;
+ ctm = fz_translate(0, -currentMediabox.y1);
+ ctm = fz_concat(ctm, fz_scale(zoom, -zoom));
+ ctm = fz_concat(ctm, fz_rotate(currentRotate));
+ bbox = fz_round_rect(fz_transform_rect(ctm, currentMediabox));
+ pageWidth = bbox.x1-bbox.x0;
+ pageHeight = bbox.y1-bbox.y0;
+ /* Render to list */
+ currentPageList = fz_new_display_list(ctx);
+ dev = fz_new_list_device(ctx, currentPageList);
+ pdf_run_page(xref, currentPage, dev, fz_identity);
}
- pagenum = page;
- error = pdf_load_page(&currentPage, xref, pagenum);
- if (error)
- return;
- zoom = resolution / 72;
- currentMediabox = currentPage->mediabox;
- currentRotate = currentPage->rotate;
- ctm = fz_translate(0, -currentMediabox.y1);
- ctm = fz_concat(ctm, fz_scale(zoom, -zoom));
- ctm = fz_concat(ctm, fz_rotate(currentRotate));
- bbox = fz_round_rect(fz_transform_rect(ctm, currentMediabox));
- pageWidth = bbox.x1-bbox.x0;
- pageHeight = bbox.y1-bbox.y0;
- /* Render to list */
- currentPageList = fz_new_display_list();
- dev = fz_new_list_device(currentPageList);
- error = pdf_run_page(xref, currentPage, dev, fz_identity);
- pdf_free_page(currentPage);
- if (error)
+ fz_catch(ctx)
+ {
LOGE("cannot make displaylist from page %d", pagenum);
+ }
+ pdf_free_page(ctx, currentPage);
+ currentPage = NULL;
fz_free_device(dev);
+ dev = NULL;
}
JNIEXPORT float JNICALL
@@ -138,15 +172,17 @@ Java_com_artifex_mupdf_MuPDFCore_drawPage(JNIEnv *env, jobject thiz, jobject bit
AndroidBitmapInfo info;
void *pixels;
int ret;
- fz_error error;
- fz_device *dev;
+ fz_device *dev = NULL;
float zoom;
fz_matrix ctm;
fz_bbox bbox;
- fz_pixmap *pix;
+ fz_pixmap *pix = NULL;
float xscale, yscale;
fz_bbox rect;
+ fz_var(pix);
+ fz_var(dev);
+
LOGI("In native method\n");
if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
@@ -169,49 +205,58 @@ Java_com_artifex_mupdf_MuPDFCore_drawPage(JNIEnv *env, jobject thiz, jobject bit
LOGE("Rendering page=%dx%d patch=[%d,%d,%d,%d]",
pageW, pageH, patchX, patchY, patchW, patchH);
- rect.x0 = patchX;
- rect.y0 = patchY;
- rect.x1 = patchX + patchW;
- rect.y1 = patchY + patchH;
- pix = fz_new_pixmap_with_rect_and_data(colorspace, rect, pixels);
- if (currentPageList == NULL)
+ fz_try(ctx)
{
- fz_clear_pixmap_with_color(pix, 0xd0);
- return 0;
- }
- fz_clear_pixmap_with_color(pix, 0xff);
-
- zoom = resolution / 72;
- ctm = fz_translate(-currentMediabox.x0, -currentMediabox.y1);
- ctm = fz_concat(ctm, fz_scale(zoom, -zoom));
- ctm = fz_concat(ctm, fz_rotate(currentRotate));
- bbox = fz_round_rect(fz_transform_rect(ctm,currentMediabox));
- /* Now, adjust ctm so that it would give the correct page width
- * heights. */
- xscale = (float)pageW/(float)(bbox.x1-bbox.x0);
- yscale = (float)pageH/(float)(bbox.y1-bbox.y0);
- ctm = fz_concat(ctm, fz_scale(xscale, yscale));
- bbox = fz_round_rect(fz_transform_rect(ctm,currentMediabox));
- dev = fz_new_draw_device(glyphcache, pix);
+ rect.x0 = patchX;
+ rect.y0 = patchY;
+ rect.x1 = patchX + patchW;
+ rect.y1 = patchY + patchH;
+ pix = fz_new_pixmap_with_rect_and_data(ctx, colorspace, rect, pixels);
+ if (currentPageList == NULL)
+ {
+ fz_clear_pixmap_with_color(pix, 0xd0);
+ break;
+ }
+ fz_clear_pixmap_with_color(pix, 0xff);
+
+ zoom = resolution / 72;
+ ctm = fz_translate(-currentMediabox.x0, -currentMediabox.y1);
+ ctm = fz_concat(ctm, fz_scale(zoom, -zoom));
+ ctm = fz_concat(ctm, fz_rotate(currentRotate));
+ bbox = fz_round_rect(fz_transform_rect(ctm,currentMediabox));
+ /* Now, adjust ctm so that it would give the correct page width
+ * heights. */
+ xscale = (float)pageW/(float)(bbox.x1-bbox.x0);
+ yscale = (float)pageH/(float)(bbox.y1-bbox.y0);
+ ctm = fz_concat(ctm, fz_scale(xscale, yscale));
+ bbox = fz_round_rect(fz_transform_rect(ctm,currentMediabox));
+ dev = fz_new_draw_device(ctx, glyphcache, pix);
#ifdef TIME_DISPLAY_LIST
- {
- clock_t time;
- int i;
+ {
+ clock_t time;
+ int i;
- LOGE("Executing display list");
- time = clock();
- for (i=0; i<100;i++) {
+ LOGE("Executing display list");
+ time = clock();
+ for (i=0; i<100;i++) {
#endif
- fz_execute_display_list(currentPageList, dev, ctm, bbox);
+ fz_execute_display_list(currentPageList, dev, ctm, bbox);
#ifdef TIME_DISPLAY_LIST
+ }
+ time = clock() - time;
+ LOGE("100 renders in %d (%d per sec)", time, CLOCKS_PER_SEC);
}
- time = clock() - time;
- LOGE("100 renders in %d (%d per sec)", time, CLOCKS_PER_SEC);
- }
#endif
- fz_free_device(dev);
- fz_drop_pixmap(pix);
- LOGE("Rendered");
+ fz_free_device(dev);
+ dev = NULL;
+ fz_drop_pixmap(ctx, pix);
+ LOGE("Rendered");
+ }
+ fz_catch(ctx)
+ {
+ fz_free_device(dev);
+ LOGE("Render failed");
+ }
AndroidBitmap_unlockPixels(env, bitmap);
@@ -221,10 +266,10 @@ Java_com_artifex_mupdf_MuPDFCore_drawPage(JNIEnv *env, jobject thiz, jobject bit
JNIEXPORT void JNICALL
Java_com_artifex_mupdf_MuPDFCore_destroying(JNIEnv * env, jobject thiz)
{
- fz_free_display_list(currentPageList);
+ fz_free_display_list(ctx, currentPageList);
currentPageList = NULL;
pdf_free_xref(xref);
xref = NULL;
- fz_free_glyph_cache(glyphcache);
+ fz_free_glyph_cache(ctx, glyphcache);
glyphcache = NULL;
}