From 07a60a0ef205eaf037135107085519616697f43e Mon Sep 17 00:00:00 2001 From: fred ross-perry Date: Sun, 14 Aug 2016 20:00:19 -0700 Subject: Android java - ad textAsHtml() to the Page class. The JNI implementation is taken from core.textAsHtml() in the old viewer. --- platform/java/mupdf_native.c | 85 ++++++++++++++++++++++ platform/java/src/com/artifex/mupdf/fitz/Page.java | 2 + 2 files changed, 87 insertions(+) (limited to 'platform/java') diff --git a/platform/java/mupdf_native.c b/platform/java/mupdf_native.c index fc90b6e8..62855ac0 100644 --- a/platform/java/mupdf_native.c +++ b/platform/java/mupdf_native.c @@ -4450,6 +4450,91 @@ FUN(Page_toStructuredText)(JNIEnv *env, jobject self) return to_StructuredText_safe_own(ctx, env, text); } +JNIEXPORT jbyteArray JNICALL +FUN(Page_textAsHtml)(JNIEnv *env, jobject self) +{ + fz_context *ctx = get_context(env); + fz_page *page = from_Page(env, self); + + fz_stext_sheet *sheet = NULL; + fz_stext_page *text = NULL; + fz_device *dev = NULL; + fz_matrix ctm; + jbyteArray bArray = NULL; + fz_buffer *buf = NULL; + fz_output *out = NULL; + + fz_var(sheet); + fz_var(text); + fz_var(dev); + fz_var(buf); + fz_var(out); + + fz_try(ctx) + { + fz_rect mediabox; + int b, l, s, c; + + ctm = fz_identity; + sheet = fz_new_stext_sheet(ctx); + text = fz_new_stext_page(ctx, fz_bound_page(ctx, page, &mediabox)); + dev = fz_new_stext_device(ctx, sheet, text); + fz_run_page(ctx, page, dev, &ctm, NULL); + fz_close_device(ctx, dev); + fz_drop_device(ctx, dev); + dev = NULL; + + fz_analyze_text(ctx, sheet, text); + + buf = fz_new_buffer(ctx, 256); + out = fz_new_output_with_buffer(ctx, buf); + fz_printf(ctx, out, "\n"); + fz_printf(ctx, out, "\n"); + fz_printf(ctx, out, "
"); + fz_print_stext_page_html(ctx, out, text); + fz_printf(ctx, out, "
\n"); + fz_printf(ctx, out, "\n\n"); + fz_drop_output(ctx, out); + out = NULL; + + bArray = (*env)->NewByteArray(env, buf->len); + if (bArray == NULL) + fz_throw(ctx, FZ_ERROR_GENERIC, "Failed to make byteArray"); + (*env)->SetByteArrayRegion(env, bArray, 0, buf->len, buf->data); + + } + fz_always(ctx) + { + fz_drop_stext_page(ctx, text); + fz_drop_stext_sheet(ctx, sheet); + fz_drop_device(ctx, dev); + fz_drop_output(ctx, out); + fz_drop_buffer(ctx, buf); + } + fz_catch(ctx) + { + jclass cls = (*env)->FindClass(env, "java/lang/OutOfMemoryError"); + if (cls != NULL) + (*env)->ThrowNew(env, cls, "Out of memory in MuPDFCore_textAsHtml"); + (*env)->DeleteLocalRef(env, cls); + + return NULL; + } + + return bArray; +} + + /* Cookie interface */ JNIEXPORT void JNICALL diff --git a/platform/java/src/com/artifex/mupdf/fitz/Page.java b/platform/java/src/com/artifex/mupdf/fitz/Page.java index 91bd5067..301b77df 100644 --- a/platform/java/src/com/artifex/mupdf/fitz/Page.java +++ b/platform/java/src/com/artifex/mupdf/fitz/Page.java @@ -40,4 +40,6 @@ public class Page public native StructuredText toStructuredText(); public native Rect[] search(String needle); + + public native byte[] textAsHtml(); } -- cgit v1.2.3