From 3d6cf4e4e543ce0d204db71ef8d0b9277219f250 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Tue, 20 Jun 2017 19:43:09 +0100 Subject: Rejig page separations interface. Specifically this is aimed so we can efficiently get separation details for PDF files (which may require a lengthy search process). --- platform/java/mupdf_native.c | 156 +++++++++++++++++++++++++++++-------------- 1 file changed, 106 insertions(+), 50 deletions(-) (limited to 'platform/java/mupdf_native.c') diff --git a/platform/java/mupdf_native.c b/platform/java/mupdf_native.c index 69208f56..7d068d85 100644 --- a/platform/java/mupdf_native.c +++ b/platform/java/mupdf_native.c @@ -85,6 +85,7 @@ static jclass cls_Point; static jclass cls_Rect; static jclass cls_RuntimeException; static jclass cls_Separation; +static jclass cls_Separations; static jclass cls_Shade; static jclass cls_StrokeState; static jclass cls_StructuredText; @@ -130,6 +131,7 @@ static jfieldID fid_Rect_x0; static jfieldID fid_Rect_x1; static jfieldID fid_Rect_y0; static jfieldID fid_Rect_y1; +static jfieldID fid_Separations_pointer; static jfieldID fid_Shade_pointer; static jfieldID fid_StrokeState_pointer; static jfieldID fid_StructuredText_pointer; @@ -191,6 +193,7 @@ static jmethodID mid_Point_init; static jmethodID mid_Rect_init; static jmethodID mid_Shade_init; static jmethodID mid_Separation_init; +static jmethodID mid_Separations_init; static jmethodID mid_StrokeState_init; static jmethodID mid_StructuredText_init; static jmethodID mid_TextBlock_init; @@ -556,6 +559,10 @@ static int find_fids(JNIEnv *env) cls_Separation = get_class(&err, env, PKG"Separation"); mid_Separation_init = get_method(&err, env, "", "(Ljava/lang/String;II)V"); + cls_Separations = get_class(&err, env, PKG"Separations"); + fid_Separations_pointer = get_field(&err, env, "pointer", "J"); + mid_Separations_init = get_method(&err, env, "", "(J)V"); + cls_StrokeState = get_class(&err, env, PKG"StrokeState"); fid_StrokeState_pointer = get_field(&err, env, "pointer", "J"); mid_StrokeState_init = get_method(&err, env, "", "(J)V"); @@ -649,6 +656,7 @@ static void lose_fids(JNIEnv *env) (*env)->DeleteGlobalRef(env, cls_Rect); (*env)->DeleteGlobalRef(env, cls_RuntimeException); (*env)->DeleteGlobalRef(env, cls_Separation); + (*env)->DeleteGlobalRef(env, cls_Separations); (*env)->DeleteGlobalRef(env, cls_Shade); (*env)->DeleteGlobalRef(env, cls_StrokeState); (*env)->DeleteGlobalRef(env, cls_StructuredText); @@ -1428,6 +1436,19 @@ static inline jobject to_Pixmap_safe_own(fz_context *ctx, JNIEnv *env, fz_pixmap return jobj; } +static inline jobject to_Separations_safe_own(fz_context *ctx, JNIEnv *env, fz_separations *seps) +{ + jobject jseps; + + if (!ctx || !seps) return NULL; + + jseps = (*env)->NewObject(env, cls_Separations, mid_Separations_init, jlong_cast(seps)); + if (!jseps) + fz_drop_separations(ctx, seps); + + return jseps; +} + static inline jobject to_StructuredText_safe_own(fz_context *ctx, JNIEnv *env, fz_stext_page *text) { jobject jtext; @@ -1605,6 +1626,15 @@ static inline fz_pixmap *from_Pixmap(JNIEnv *env, jobject jobj) return pixmap; } +static inline fz_separations *from_Separations(JNIEnv *env, jobject jobj) +{ + fz_separations *seps; + if (!jobj) return NULL; + seps = CAST(fz_separations *, (*env)->GetLongField(env, jobj, fid_Separations_pointer)); + if (!seps) jni_throw_null(env, "cannot use already destroyed Separations"); + return seps; +} + static inline fz_shade *from_Shade(JNIEnv *env, jobject jobj) { fz_shade *shd; @@ -4718,63 +4748,18 @@ FUN(Page_finalize)(JNIEnv *env, jobject self) fz_drop_page(ctx, page); } -JNIEXPORT jint JNICALL -FUN(Page_countSeparations)(JNIEnv *env, jobject self) -{ - fz_context *ctx = get_context(env); - fz_page *page = from_Page(env, self); - int nSep; - - if (!ctx || !page) return 0; - - fz_try(ctx) - nSep = fz_count_separations_on_page(ctx, page); - fz_catch(ctx) - { - jni_rethrow(env, ctx); - return 0; - } - - return nSep; -} - -JNIEXPORT void JNICALL -FUN(Page_enableSeparation)(JNIEnv *env, jobject self, jint sep, jboolean enable) -{ - fz_context *ctx = get_context(env); - fz_page *page = from_Page(env, self); - - if (!ctx || !page) return; - - fz_try(ctx) - fz_control_separation_on_page(ctx, page, sep, !enable); - fz_catch(ctx) - jni_rethrow(env, ctx); -} - JNIEXPORT jobject JNICALL -FUN(Page_getSeparation)(JNIEnv *env, jobject self, jint sep) +FUN(Page_getSeparations)(JNIEnv *env, jobject self) { fz_context *ctx = get_context(env); fz_page *page = from_Page(env, self); - const char *name; - char rgba[4]; - unsigned int bgra; - unsigned int cmyk; - jobject jname = NULL; + fz_separations *seps; - if (!ctx || !page) return NULL; + if (!ctx || !page) return 0; - /* MuPDF returns RGBA as bytes. Android wants a packed BGRA int. */ - name = fz_get_separation_on_page(ctx, page, sep, (unsigned int *)(&rgba[0]), &cmyk); - bgra = (rgba[0] << 16) | (rgba[1]<<8) | rgba[2] | (rgba[3]<<24); - if (name) - { - jname = (*env)->NewStringUTF(env, name); - if (!jname) return NULL; - } + seps = fz_page_separations(ctx, page); - return (*env)->NewObject(env, cls_Separation, mid_Separation_init, jname, bgra, cmyk); + return to_Separations_safe_own(ctx, env, seps); } JNIEXPORT jobject JNICALL @@ -8849,3 +8834,74 @@ FUN(PDFAnnotation_setLineEndingStyles)(JNIEnv *env, jobject self, jint start_sty fz_catch(ctx) jni_rethrow(env, ctx); } + +/* Separations interface */ + +JNIEXPORT jint JNICALL +FUN(Separations_getNumberOfSeparations)(JNIEnv *env, jobject self) +{ + fz_context *ctx = get_context(env); + fz_separations *seps = from_Separations(env, self); + int nSep; + + fz_try(ctx) + nSep = fz_count_separations(ctx, seps); + fz_catch(ctx) + { + jni_rethrow(env, ctx); + return 0; + } + + return nSep; +} + +JNIEXPORT void JNICALL +FUN(Separations_controlSeparation)(JNIEnv *env, jobject self, jint sep, jboolean disable) +{ + fz_context *ctx = get_context(env); + fz_separations *seps = from_Separations(env, self); + + if (!ctx || !seps) return; + + fz_try(ctx) + fz_control_separation(ctx, seps, sep, disable); + fz_catch(ctx) + jni_rethrow(env, ctx); +} + +JNIEXPORT jobject JNICALL +FUN(Separations_getSeparation)(JNIEnv *env, jobject self, jint sep) +{ + fz_context *ctx = get_context(env); + fz_separations *seps = from_Separations(env, self); + const char *name; + char rgba[4]; + unsigned int bgra; + unsigned int cmyk; + jobject jname = NULL; + + if (!ctx || !seps) return NULL; + + /* MuPDF returns RGBA as bytes. Android wants a packed BGRA int. */ + name = fz_get_separation(ctx, seps, sep, (unsigned int *)(&rgba[0]), &cmyk); + bgra = (rgba[0] << 16) | (rgba[1]<<8) | rgba[2] | (rgba[3]<<24); + if (name) + { + jname = (*env)->NewStringUTF(env, name); + if (!jname) return NULL; + } + + return (*env)->NewObject(env, cls_Separation, mid_Separation_init, jname, bgra, cmyk); +} + +JNIEXPORT jboolean JNICALL +FUN(Separations_areSeparationsControllable)(JNIEnv *env, jobject self) +{ + fz_context *ctx = get_context(env); + fz_separations *seps = from_Separations(env, self); + jobject jname = NULL; + + if (!ctx || !seps) return 0; + + return fz_separations_controllable(ctx, seps); +} -- cgit v1.2.3