From 1fa9a65166acb4077d2b0974ad579dc11d1a92cc Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 10 Feb 2017 14:05:45 +0100 Subject: java: Make PDFDocument a subclass of Document. Requires use of Document.openDocument(path) to open a document. No more new Document(path) since we may need to return a PDFDocument. Create a new blank PDF with new PDFDocument() constructor. --- platform/java/example/TraceDevice.java | 2 +- platform/java/example/Viewer.java | 2 +- platform/java/mupdf_native.c | 126 +++++++++------------ platform/java/mupdf_native.h | 48 ++------ .../java/src/com/artifex/mupdf/fitz/Document.java | 38 +++---- .../src/com/artifex/mupdf/fitz/PDFDocument.java | 25 ++-- 6 files changed, 96 insertions(+), 145 deletions(-) (limited to 'platform/java') diff --git a/platform/java/example/TraceDevice.java b/platform/java/example/TraceDevice.java index d6bb2953..3739948a 100644 --- a/platform/java/example/TraceDevice.java +++ b/platform/java/example/TraceDevice.java @@ -124,7 +124,7 @@ public class TraceDevice extends Device implements PathWalker, TextWalker } public static void main(String[] args) { - Document doc = new Document("pdfref17.pdf"); + Document doc = Document.openDocument("pdfref17.pdf"); Page page = doc.loadPage(1144); TraceDevice dev = new TraceDevice(); page.run(dev, new Matrix()); diff --git a/platform/java/example/Viewer.java b/platform/java/example/Viewer.java index a924cf4b..e5c45be4 100644 --- a/platform/java/example/Viewer.java +++ b/platform/java/example/Viewer.java @@ -452,7 +452,7 @@ public class Viewer extends Frame implements WindowListener, ActionListener, Ite } try { - Document doc = new Document(selectedFile.getAbsolutePath()); + Document doc = Document.openDocument(selectedFile.getAbsolutePath()); Viewer app = new Viewer(doc); app.setVisible(true); return; diff --git a/platform/java/mupdf_native.c b/platform/java/mupdf_native.c index aec932c4..e547482c 100644 --- a/platform/java/mupdf_native.c +++ b/platform/java/mupdf_native.c @@ -911,14 +911,6 @@ static inline jobject to_ColorSpace_safe(fz_context *ctx, JNIEnv *env, fz_colors return jcs; } -static inline jobject to_Document_safe(fz_context *ctx, JNIEnv *env, fz_document *doc) -{ - if (!ctx || !doc) return NULL; - - fz_keep_document(ctx, doc); - return (*env)->NewObject(env, cls_Document, mid_Document_init, jlong_cast(doc)); -} - static inline jobject to_Font_safe(fz_context *ctx, JNIEnv *env, fz_font *font) { jobject jfont; @@ -1008,14 +1000,6 @@ static inline jobject to_Outline_safe(fz_context *ctx, JNIEnv *env, fz_document return jarr; } -static inline jobject to_PDFDocument_safe(fz_context *ctx, JNIEnv *env, pdf_document *pdf) -{ - if (!ctx || !pdf) return NULL; - - fz_keep_document(ctx, (fz_document *) pdf); - return (*env)->NewObject(env, cls_PDFDocument, mid_PDFDocument_init, jlong_cast(pdf)); -} - static inline jobject to_PDFObject_safe(fz_context *ctx, JNIEnv *env, jobject pdf, pdf_obj *obj) { if (!ctx || !pdf) return NULL; @@ -1066,6 +1050,32 @@ static inline jobjectArray to_jRectArray_safe(fz_context *ctx, JNIEnv *env, cons /* Conversion functions: C to Java. Take ownership of fitz object. None of these throw fitz exceptions. */ +static inline jobject to_Document_safe_own(fz_context *ctx, JNIEnv *env, fz_document *doc) +{ + jobject obj; + + if (!ctx || !doc) return NULL; + + obj = (*env)->NewObject(env, cls_Document, mid_Document_init, jlong_cast(doc)); + if (!obj) + fz_drop_document(ctx, doc); + + return obj; +} + +static inline jobject to_PDFDocument_safe_own(fz_context *ctx, JNIEnv *env, pdf_document *pdf) +{ + jobject obj; + + if (!ctx || !pdf) return NULL; + + obj = (*env)->NewObject(env, cls_PDFDocument, mid_PDFDocument_init, jlong_cast(pdf)); + if (!obj) + fz_drop_document(ctx, (fz_document*)pdf); + + return obj; +} + static inline jobject to_Device_safe_own(fz_context *ctx, JNIEnv *env, fz_device *device) { jobject jdev; @@ -3996,11 +4006,12 @@ FUN(Document_finalize)(JNIEnv *env, jobject self) Memento_fin(); } -JNIEXPORT jlong JNICALL -FUN(Document_newNativeWithPath)(JNIEnv *env, jobject self, jstring jfilename) +JNIEXPORT jobject JNICALL +FUN(Document_openNativeWithPath)(JNIEnv *env, jclass cls, jstring jfilename) { fz_context *ctx = get_context(env); fz_document *doc = NULL; + pdf_document *pdf = NULL; const char *filename = NULL; if (!ctx) return 0; @@ -4011,17 +4022,24 @@ FUN(Document_newNativeWithPath)(JNIEnv *env, jobject self, jstring jfilename) } fz_try(ctx) + { doc = fz_open_document(ctx, filename); + pdf = pdf_document_from_fz_document(ctx, doc); + } fz_always(ctx) + { if (filename) (*env)->ReleaseStringUTFChars(env, jfilename, filename); + } fz_catch(ctx) { jni_rethrow(env, ctx); return 0; } - return jlong_cast(doc); + if (pdf) + return to_PDFDocument_safe_own(ctx, env, pdf); + return to_Document_safe_own(ctx, env, doc); } JNIEXPORT jboolean JNICALL @@ -4258,26 +4276,6 @@ FUN(Document_findBookmark)(JNIEnv *env, jobject self, jlong mark) return page; } -JNIEXPORT jobject JNICALL -FUN(Document_toPDFDocument)(JNIEnv *env, jobject self) -{ - fz_context *ctx = get_context(env); - fz_document *doc = from_Document(env, self); - pdf_document *pdf = NULL; - - if (!ctx || !doc) return NULL; - - fz_try(ctx) - pdf = pdf_specifics(ctx, doc); - fz_catch(ctx) - { - jni_rethrow(env, ctx); - return NULL; - } - - return to_PDFDocument_safe(ctx, env, pdf); -} - /* Page interface */ JNIEXPORT void JNICALL @@ -5640,6 +5638,25 @@ FUN(StructuredText_getBlocks)(JNIEnv *env, jobject self) /* PDFDocument interface */ +JNIEXPORT jlong JNICALL +FUN(PDFDocument_newNative)(JNIEnv *env, jclass cls) +{ + fz_context *ctx = get_context(env); + pdf_document *doc = NULL; + + if (!ctx) return 0; + + fz_try(ctx) + doc = pdf_create_document(ctx); + fz_catch(ctx) + { + jni_rethrow(env, ctx); + return 0; + } + + return jlong_cast(doc); +} + JNIEXPORT void JNICALL FUN(PDFDocument_finalize)(JNIEnv *env, jobject self) { @@ -5865,37 +5882,6 @@ FUN(PDFDocument_newDictionary)(JNIEnv *env, jobject self) return (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj), self); } -JNIEXPORT jobject JNICALL -FUN(PDFDocument_toDocument)(JNIEnv *env, jobject self) -{ - fz_context *ctx = get_context(env); - pdf_document *pdf = from_PDFDocument(env, self); - - if (!ctx || !pdf) return NULL; - - return to_Document_safe(ctx, env, (fz_document *) pdf); -} - -JNIEXPORT jint JNICALL -FUN(PDFDocument_countPages)(JNIEnv *env, jobject self) -{ - fz_context *ctx = get_context(env); - pdf_document *pdf = from_PDFDocument(env, self); - int count = 0; - - if (!ctx || !pdf) return 0; - - fz_try(ctx) - count = pdf_count_pages(ctx, pdf); - fz_catch(ctx) - { - jni_rethrow(env, ctx); - return 0; - } - - return count; -} - JNIEXPORT jobject JNICALL FUN(PDFDocument_findPage)(JNIEnv *env, jobject self, jint jat) { diff --git a/platform/java/mupdf_native.h b/platform/java/mupdf_native.h index 8b0b2fe3..19fe1502 100644 --- a/platform/java/mupdf_native.h +++ b/platform/java/mupdf_native.h @@ -603,19 +603,19 @@ JNIEXPORT void JNICALL Java_com_artifex_mupdf_fitz_Document_finalize /* * Class: com_artifex_mupdf_fitz_Document - * Method: newNativeWithPath - * Signature: (Ljava/lang/String;)J + * Method: openNativeWithPath + * Signature: (Ljava/lang/String;)Lcom/artifex/mupdf/fitz/Document; */ -JNIEXPORT jlong JNICALL Java_com_artifex_mupdf_fitz_Document_newNativeWithPath - (JNIEnv *, jobject, jstring); +JNIEXPORT jobject JNICALL Java_com_artifex_mupdf_fitz_Document_openNativeWithPath + (JNIEnv *, jclass, jstring); /* * Class: com_artifex_mupdf_fitz_Document - * Method: newNativeWithBuffer - * Signature: ([BLjava/lang/String;)J + * Method: openNativeWithBuffer + * Signature: ([BLjava/lang/String;)Lcom/artifex/mupdf/fitz/Document; */ -JNIEXPORT jlong JNICALL Java_com_artifex_mupdf_fitz_Document_newNativeWithBuffer - (JNIEnv *, jobject, jbyteArray, jstring); +JNIEXPORT jobject JNICALL Java_com_artifex_mupdf_fitz_Document_openNativeWithBuffer + (JNIEnv *, jclass, jbyteArray, jstring); /* * Class: com_artifex_mupdf_fitz_Document @@ -705,14 +705,6 @@ JNIEXPORT jint JNICALL Java_com_artifex_mupdf_fitz_Document_findBookmark JNIEXPORT jboolean JNICALL Java_com_artifex_mupdf_fitz_Document_isUnencryptedPDF (JNIEnv *, jobject); -/* - * Class: com_artifex_mupdf_fitz_Document - * Method: toPDFDocument - * Signature: ()Lcom/artifex/mupdf/fitz/PDFDocument; - */ -JNIEXPORT jobject JNICALL Java_com_artifex_mupdf_fitz_Document_toPDFDocument - (JNIEnv *, jobject); - /* * Class: com_artifex_mupdf_fitz_Document * Method: proofNative @@ -1326,27 +1318,11 @@ extern "C" { #endif /* * Class: com_artifex_mupdf_fitz_PDFDocument - * Method: finalize - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_artifex_mupdf_fitz_PDFDocument_finalize - (JNIEnv *, jobject); - -/* - * Class: com_artifex_mupdf_fitz_PDFDocument - * Method: toDocument - * Signature: ()Lcom/artifex/mupdf/fitz/Document; - */ -JNIEXPORT jobject JNICALL Java_com_artifex_mupdf_fitz_PDFDocument_toDocument - (JNIEnv *, jobject); - -/* - * Class: com_artifex_mupdf_fitz_PDFDocument - * Method: countPages - * Signature: ()I + * Method: newNative + * Signature: ()J */ -JNIEXPORT jint JNICALL Java_com_artifex_mupdf_fitz_PDFDocument_countPages - (JNIEnv *, jobject); +JNIEXPORT jlong JNICALL Java_com_artifex_mupdf_fitz_PDFDocument_newNative + (JNIEnv *, jclass); /* * Class: com_artifex_mupdf_fitz_PDFDocument diff --git a/platform/java/src/com/artifex/mupdf/fitz/Document.java b/platform/java/src/com/artifex/mupdf/fitz/Document.java index 1fc21c13..52010a02 100644 --- a/platform/java/src/com/artifex/mupdf/fitz/Document.java +++ b/platform/java/src/com/artifex/mupdf/fitz/Document.java @@ -11,7 +11,8 @@ public class Document public static final String META_INFO_AUTHOR = "info:Author"; public static final String META_INFO_TITLE = "info:Title"; - private long pointer; + protected long pointer; + protected String path; /* for proofing */ protected native void finalize(); @@ -20,23 +21,21 @@ public class Document pointer = 0; } - private native long newNativeWithPath(String filename); - private native long newNativeWithBuffer(byte buffer[], String magic); - // private native long newNativeWithRandomAccessFile(RandomAccessFile file, String magic); - - private String mPath=null; - public String getPath() {return mPath;} - public Document(String filename) { - mPath = filename; - pointer = newNativeWithPath(filename); + protected Document(long p) { + pointer = p; } - public Document(byte buffer[], String magic) { - pointer = newNativeWithBuffer(buffer, magic); + protected native static Document openNativeWithPath(String filename); + protected native static Document openNativeWithBuffer(byte buffer[], String magic); + + public static Document openDocument(String filename) { + Document doc = openNativeWithPath(filename); + doc.path = filename; + return doc; } - private Document(long p) { - pointer = p; + public static Document openDocument(byte buffer[], String magic) { + return openNativeWithBuffer(buffer, magic); } public native boolean needsPassword(); @@ -54,17 +53,14 @@ public class Document public native boolean isUnencryptedPDF(); - public native PDFDocument toPDFDocument(); - public boolean isPDF() { - return toPDFDocument() != null; + return false; } - public String makeProof (String currentPath, String printProfile, String displayProfile, int resolution) - { + public String getPath() { return path; } + protected native String proofNative (String currentPath, String printProfile, String displayProfile, int resolution); + public String makeProof (String currentPath, String printProfile, String displayProfile, int resolution) { String proofFile = proofNative( currentPath, printProfile, displayProfile, resolution); return proofFile; } - - public native String proofNative (String currentPath, String printProfile, String displayProfile, int resolution); } diff --git a/platform/java/src/com/artifex/mupdf/fitz/PDFDocument.java b/platform/java/src/com/artifex/mupdf/fitz/PDFDocument.java index f866cb86..a925ac90 100644 --- a/platform/java/src/com/artifex/mupdf/fitz/PDFDocument.java +++ b/platform/java/src/com/artifex/mupdf/fitz/PDFDocument.java @@ -1,27 +1,21 @@ package com.artifex.mupdf.fitz; -public class PDFDocument +public class PDFDocument extends Document { - static { - Context.init(); - } - - private long pointer; - - protected native void finalize(); + private static native long newNative(); - public void destroy() { - finalize(); - pointer = 0; + protected PDFDocument(long p) { + super(p); } - private PDFDocument(long p) { - pointer = p; + public PDFDocument() { + super(newNative()); } - public native Document toDocument(); + public boolean isPDF() { + return true; + } - public native int countPages(); public native PDFObject findPage(int at); public native PDFObject getTrailer(); @@ -102,5 +96,4 @@ public class PDFDocument public native boolean hasUnsavedChanges(); public native boolean canBeSavedIncrementally(); public native int save(String filename, String options); - } -- cgit v1.2.3