Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/platform/java/jni/pdfdocument.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2025 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 /* PDFDocument interface */ | |
| 24 | |
| 25 static void free_event_cb_data(fz_context *ctx, void *data) | |
| 26 { | |
| 27 jobject jlistener = (jobject)data; | |
| 28 jboolean detach = JNI_FALSE; | |
| 29 JNIEnv *env; | |
| 30 | |
| 31 env = jni_attach_thread(&detach); | |
| 32 if (env == NULL) | |
| 33 fz_warn(ctx, "cannot attach to JVM in free_event_cb_data"); | |
| 34 else | |
| 35 { | |
| 36 (*env)->DeleteGlobalRef(env, jlistener); | |
| 37 jni_detach_thread(detach); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 static void event_cb(fz_context *ctx, pdf_document *pdf, pdf_doc_event *event, void *data) | |
| 42 { | |
| 43 jobject jlistener = (jobject)data; | |
| 44 jboolean detach = JNI_FALSE; | |
| 45 JNIEnv *env; | |
| 46 | |
| 47 env = jni_attach_thread(&detach); | |
| 48 if (env == NULL) | |
| 49 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot attach to JVM in event_cb"); | |
| 50 | |
| 51 switch (event->type) | |
| 52 { | |
| 53 case PDF_DOCUMENT_EVENT_ALERT: | |
| 54 { | |
| 55 pdf_alert_event *alert; | |
| 56 jstring jtitle = NULL; | |
| 57 jstring jmessage = NULL; | |
| 58 jstring jcheckboxmsg = NULL; | |
| 59 jobject jalertresult; | |
| 60 jobject jpdf; | |
| 61 | |
| 62 alert = pdf_access_alert_event(ctx, event); | |
| 63 | |
| 64 jpdf = to_PDFDocument_safe(ctx, env, pdf); | |
| 65 if (!jpdf || (*env)->ExceptionCheck(env)) | |
| 66 fz_throw_java_and_detach_thread(ctx, env, detach); | |
| 67 | |
| 68 jtitle = (*env)->NewStringUTF(env, alert->title); | |
| 69 if (!jtitle || (*env)->ExceptionCheck(env)) | |
| 70 fz_throw_java_and_detach_thread(ctx, env, detach); | |
| 71 | |
| 72 jmessage = (*env)->NewStringUTF(env, alert->message); | |
| 73 if (!jmessage || (*env)->ExceptionCheck(env)) | |
| 74 fz_throw_java_and_detach_thread(ctx, env, detach); | |
| 75 | |
| 76 if (alert->has_check_box) { | |
| 77 jcheckboxmsg = (*env)->NewStringUTF(env, alert->check_box_message); | |
| 78 if (!jcheckboxmsg || (*env)->ExceptionCheck(env)) | |
| 79 fz_throw_java_and_detach_thread(ctx, env, detach); | |
| 80 } | |
| 81 | |
| 82 jalertresult = (*env)->CallObjectMethod(env, jlistener, mid_PDFDocument_JsEventListener_onAlert, jpdf, jtitle, jmessage, alert->icon_type, alert->button_group_type, alert->has_check_box, jcheckboxmsg, alert->initially_checked); | |
| 83 if ((*env)->ExceptionCheck(env)) | |
| 84 fz_throw_java_and_detach_thread(ctx, env, detach); | |
| 85 | |
| 86 if (jalertresult) | |
| 87 { | |
| 88 alert->button_pressed = (*env)->GetIntField(env, jalertresult, fid_AlertResult_buttonPressed); | |
| 89 alert->finally_checked = (*env)->GetBooleanField(env, jalertresult, fid_AlertResult_checkboxChecked); | |
| 90 } | |
| 91 } | |
| 92 break; | |
| 93 | |
| 94 default: | |
| 95 fz_throw_and_detach_thread(ctx, detach, FZ_ERROR_GENERIC, "event not yet implemented"); | |
| 96 break; | |
| 97 } | |
| 98 | |
| 99 jni_detach_thread(detach); | |
| 100 } | |
| 101 | |
| 102 JNIEXPORT jlong JNICALL | |
| 103 FUN(PDFDocument_newNative)(JNIEnv *env, jclass cls) | |
| 104 { | |
| 105 fz_context *ctx = get_context(env); | |
| 106 pdf_document *doc = NULL; | |
| 107 | |
| 108 if (!ctx) return 0; | |
| 109 | |
| 110 fz_try(ctx) | |
| 111 doc = pdf_create_document(ctx); | |
| 112 fz_catch(ctx) | |
| 113 jni_rethrow(env, ctx); | |
| 114 | |
| 115 return jlong_cast(doc); | |
| 116 } | |
| 117 | |
| 118 JNIEXPORT void JNICALL | |
| 119 FUN(PDFDocument_finalize)(JNIEnv *env, jobject self) | |
| 120 { | |
| 121 fz_context *ctx = get_context(env); | |
| 122 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 123 if (!ctx || !pdf) return; | |
| 124 FUN(Document_finalize)(env, self); /* Call super.finalize() */ | |
| 125 } | |
| 126 | |
| 127 JNIEXPORT jint JNICALL | |
| 128 FUN(PDFDocument_countObjects)(JNIEnv *env, jobject self) | |
| 129 { | |
| 130 fz_context *ctx = get_context(env); | |
| 131 pdf_document *pdf = from_PDFDocument(env, self); | |
| 132 int count = 0; | |
| 133 | |
| 134 if (!ctx || !pdf) return 0; | |
| 135 | |
| 136 fz_try(ctx) | |
| 137 count = pdf_xref_len(ctx, pdf); | |
| 138 fz_catch(ctx) | |
| 139 jni_rethrow(env, ctx); | |
| 140 | |
| 141 return count; | |
| 142 } | |
| 143 | |
| 144 JNIEXPORT jobject JNICALL | |
| 145 FUN(PDFDocument_newNull)(JNIEnv *env, jobject self) | |
| 146 { | |
| 147 fz_context *ctx = get_context(env); | |
| 148 pdf_obj *obj = NULL; | |
| 149 jobject jobj; | |
| 150 | |
| 151 if (!ctx) return NULL; | |
| 152 | |
| 153 obj = PDF_NULL; | |
| 154 | |
| 155 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 156 if (!jobj) | |
| 157 pdf_drop_obj(ctx, obj); | |
| 158 return jobj; | |
| 159 } | |
| 160 | |
| 161 JNIEXPORT jobject JNICALL | |
| 162 FUN(PDFDocument_newBoolean)(JNIEnv *env, jobject self, jboolean b) | |
| 163 { | |
| 164 fz_context *ctx = get_context(env); | |
| 165 pdf_obj *obj = NULL; | |
| 166 jobject jobj; | |
| 167 | |
| 168 if (!ctx) return NULL; | |
| 169 | |
| 170 obj = b ? PDF_TRUE : PDF_FALSE; | |
| 171 | |
| 172 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 173 if (!jobj) | |
| 174 pdf_drop_obj(ctx, obj); | |
| 175 return jobj; | |
| 176 } | |
| 177 | |
| 178 JNIEXPORT jobject JNICALL | |
| 179 FUN(PDFDocument_newInteger)(JNIEnv *env, jobject self, jint i) | |
| 180 { | |
| 181 fz_context *ctx = get_context(env); | |
| 182 pdf_obj *obj = NULL; | |
| 183 jobject jobj; | |
| 184 | |
| 185 if (!ctx) return NULL; | |
| 186 | |
| 187 fz_try(ctx) | |
| 188 obj = pdf_new_int(ctx, i); | |
| 189 fz_catch(ctx) | |
| 190 jni_rethrow(env, ctx); | |
| 191 | |
| 192 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 193 if (!jobj) | |
| 194 pdf_drop_obj(ctx, obj); | |
| 195 return jobj; | |
| 196 } | |
| 197 | |
| 198 JNIEXPORT jobject JNICALL | |
| 199 FUN(PDFDocument_newReal)(JNIEnv *env, jobject self, jfloat f) | |
| 200 { | |
| 201 fz_context *ctx = get_context(env); | |
| 202 pdf_obj *obj = NULL; | |
| 203 jobject jobj; | |
| 204 | |
| 205 if (!ctx) return NULL; | |
| 206 | |
| 207 fz_try(ctx) | |
| 208 obj = pdf_new_real(ctx, f); | |
| 209 fz_catch(ctx) | |
| 210 jni_rethrow(env, ctx); | |
| 211 | |
| 212 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 213 if (!jobj) | |
| 214 pdf_drop_obj(ctx, obj); | |
| 215 return jobj; | |
| 216 } | |
| 217 | |
| 218 JNIEXPORT jobject JNICALL | |
| 219 FUN(PDFDocument_newString)(JNIEnv *env, jobject self, jstring jstring) | |
| 220 { | |
| 221 fz_context *ctx = get_context(env); | |
| 222 pdf_obj *obj = NULL; | |
| 223 const char *s = NULL; | |
| 224 jobject jobj; | |
| 225 | |
| 226 if (!ctx) return NULL; | |
| 227 if (!jstring) jni_throw_arg(env, "string must not be null"); | |
| 228 | |
| 229 s = (*env)->GetStringUTFChars(env, jstring, NULL); | |
| 230 if (!s) return NULL; | |
| 231 | |
| 232 fz_try(ctx) | |
| 233 obj = pdf_new_text_string(ctx, s); | |
| 234 fz_always(ctx) | |
| 235 (*env)->ReleaseStringUTFChars(env, jstring, s); | |
| 236 fz_catch(ctx) | |
| 237 jni_rethrow(env, ctx); | |
| 238 | |
| 239 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 240 if (!jobj) | |
| 241 pdf_drop_obj(ctx, obj); | |
| 242 return jobj; | |
| 243 } | |
| 244 | |
| 245 JNIEXPORT jobject JNICALL | |
| 246 FUN(PDFDocument_newByteString)(JNIEnv *env, jobject self, jobject jbs) | |
| 247 { | |
| 248 fz_context *ctx = get_context(env); | |
| 249 pdf_obj *obj = NULL; | |
| 250 jbyte *bs; | |
| 251 size_t bslen; | |
| 252 jobject jobj; | |
| 253 | |
| 254 if (!ctx) return NULL; | |
| 255 if (!jbs) jni_throw_arg(env, "bs must not be null"); | |
| 256 | |
| 257 bslen = (*env)->GetArrayLength(env, jbs); | |
| 258 | |
| 259 fz_try(ctx) | |
| 260 bs = Memento_label(fz_malloc(ctx, bslen), "PDFDocument_newByteString"); | |
| 261 fz_catch(ctx) | |
| 262 jni_rethrow(env, ctx); | |
| 263 | |
| 264 (*env)->GetByteArrayRegion(env, jbs, 0, bslen, bs); | |
| 265 if ((*env)->ExceptionCheck(env)) | |
| 266 { | |
| 267 fz_free(ctx, bs); | |
| 268 return NULL; | |
| 269 } | |
| 270 | |
| 271 fz_try(ctx) | |
| 272 obj = pdf_new_string(ctx, (char *) bs, bslen); | |
| 273 fz_always(ctx) | |
| 274 fz_free(ctx, bs); | |
| 275 fz_catch(ctx) | |
| 276 jni_rethrow(env, ctx); | |
| 277 | |
| 278 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 279 if (!jobj) | |
| 280 pdf_drop_obj(ctx, obj); | |
| 281 return jobj; | |
| 282 } | |
| 283 | |
| 284 JNIEXPORT jobject JNICALL | |
| 285 FUN(PDFDocument_newName)(JNIEnv *env, jobject self, jstring jname) | |
| 286 { | |
| 287 fz_context *ctx = get_context(env); | |
| 288 pdf_obj *obj = NULL; | |
| 289 const char *name = NULL; | |
| 290 jobject jobj; | |
| 291 | |
| 292 if (!ctx) return NULL; | |
| 293 if (!jname) jni_throw_arg(env, "name must not be null"); | |
| 294 | |
| 295 name = (*env)->GetStringUTFChars(env, jname, NULL); | |
| 296 if (!name) return NULL; | |
| 297 | |
| 298 fz_try(ctx) | |
| 299 obj = pdf_new_name(ctx, name); | |
| 300 fz_always(ctx) | |
| 301 (*env)->ReleaseStringUTFChars(env, jname, name); | |
| 302 fz_catch(ctx) | |
| 303 jni_rethrow(env, ctx); | |
| 304 | |
| 305 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 306 if (!jobj) | |
| 307 pdf_drop_obj(ctx, obj); | |
| 308 return jobj; | |
| 309 } | |
| 310 | |
| 311 JNIEXPORT jobject JNICALL | |
| 312 FUN(PDFDocument_newIndirect)(JNIEnv *env, jobject self, jint num, jint gen) | |
| 313 { | |
| 314 fz_context *ctx = get_context(env); | |
| 315 pdf_document *pdf = from_PDFDocument(env, self); | |
| 316 pdf_obj *obj = NULL; | |
| 317 jobject jobj; | |
| 318 | |
| 319 if (!ctx || !pdf) return NULL; | |
| 320 | |
| 321 fz_try(ctx) | |
| 322 obj = pdf_new_indirect(ctx, pdf, num, gen); | |
| 323 fz_catch(ctx) | |
| 324 jni_rethrow(env, ctx); | |
| 325 | |
| 326 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 327 if (!jobj) | |
| 328 pdf_drop_obj(ctx, obj); | |
| 329 return jobj; | |
| 330 } | |
| 331 | |
| 332 JNIEXPORT jobject JNICALL | |
| 333 FUN(PDFDocument_newArray)(JNIEnv *env, jobject self) | |
| 334 { | |
| 335 fz_context *ctx = get_context(env); | |
| 336 pdf_document *pdf = from_PDFDocument(env, self); | |
| 337 pdf_obj *obj = NULL; | |
| 338 jobject jobj; | |
| 339 | |
| 340 if (!ctx || !pdf) return NULL; | |
| 341 | |
| 342 fz_try(ctx) | |
| 343 obj = pdf_new_array(ctx, pdf, 0); | |
| 344 fz_catch(ctx) | |
| 345 jni_rethrow(env, ctx); | |
| 346 | |
| 347 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 348 if (!jobj) | |
| 349 pdf_drop_obj(ctx, obj); | |
| 350 return jobj; | |
| 351 } | |
| 352 | |
| 353 JNIEXPORT jobject JNICALL | |
| 354 FUN(PDFDocument_newDictionary)(JNIEnv *env, jobject self) | |
| 355 { | |
| 356 fz_context *ctx = get_context(env); | |
| 357 pdf_document *pdf = from_PDFDocument(env, self); | |
| 358 pdf_obj *obj = NULL; | |
| 359 jobject jobj; | |
| 360 | |
| 361 if (!ctx || !pdf) return NULL; | |
| 362 | |
| 363 fz_try(ctx) | |
| 364 obj = pdf_new_dict(ctx, pdf, 0); | |
| 365 fz_catch(ctx) | |
| 366 jni_rethrow(env, ctx); | |
| 367 | |
| 368 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 369 if (!jobj) | |
| 370 pdf_drop_obj(ctx, obj); | |
| 371 return jobj; | |
| 372 } | |
| 373 | |
| 374 JNIEXPORT jobject JNICALL | |
| 375 FUN(PDFDocument_findPage)(JNIEnv *env, jobject self, jint jat) | |
| 376 { | |
| 377 fz_context *ctx = get_context(env); | |
| 378 pdf_document *pdf = from_PDFDocument(env, self); | |
| 379 pdf_obj *obj = NULL; | |
| 380 | |
| 381 if (!ctx || !pdf) return NULL; | |
| 382 | |
| 383 fz_try(ctx) | |
| 384 obj = pdf_lookup_page_obj(ctx, pdf, jat); | |
| 385 fz_catch(ctx) | |
| 386 jni_rethrow(env, ctx); | |
| 387 | |
| 388 return to_PDFObject_safe(ctx, env, obj); | |
| 389 } | |
| 390 | |
| 391 JNIEXPORT jobject JNICALL | |
| 392 FUN(PDFDocument_getTrailer)(JNIEnv *env, jobject self) | |
| 393 { | |
| 394 fz_context *ctx = get_context(env); | |
| 395 pdf_document *pdf = from_PDFDocument(env, self); | |
| 396 pdf_obj *obj = NULL; | |
| 397 | |
| 398 if (!ctx || !pdf) return NULL; | |
| 399 | |
| 400 fz_try(ctx) | |
| 401 obj = pdf_trailer(ctx, pdf); | |
| 402 fz_catch(ctx) | |
| 403 jni_rethrow(env, ctx); | |
| 404 | |
| 405 return to_PDFObject_safe(ctx, env, obj); | |
| 406 } | |
| 407 | |
| 408 JNIEXPORT jobject JNICALL | |
| 409 FUN(PDFDocument_addObject)(JNIEnv *env, jobject self, jobject jobj) | |
| 410 { | |
| 411 fz_context *ctx = get_context(env); | |
| 412 pdf_document *pdf = from_PDFDocument(env, self); | |
| 413 pdf_obj *obj = from_PDFObject(env, jobj); | |
| 414 | |
| 415 if (!ctx || !pdf) return NULL; | |
| 416 if (!jobj) jni_throw_arg(env, "object must not be null"); | |
| 417 | |
| 418 fz_try(ctx) | |
| 419 obj = pdf_add_object_drop(ctx, pdf, obj); | |
| 420 fz_catch(ctx) | |
| 421 jni_rethrow(env, ctx); | |
| 422 | |
| 423 return jobj; | |
| 424 } | |
| 425 | |
| 426 JNIEXPORT jobject JNICALL | |
| 427 FUN(PDFDocument_createObject)(JNIEnv *env, jobject self) | |
| 428 { | |
| 429 fz_context *ctx = get_context(env); | |
| 430 pdf_document *pdf = from_PDFDocument(env, self); | |
| 431 pdf_obj *ind = NULL; | |
| 432 | |
| 433 if (!ctx || !pdf) return NULL; | |
| 434 | |
| 435 fz_try(ctx) | |
| 436 ind = pdf_new_indirect(ctx, pdf, pdf_create_object(ctx, pdf), 0); | |
| 437 fz_catch(ctx) | |
| 438 jni_rethrow(env, ctx); | |
| 439 | |
| 440 return to_PDFObject_safe_own(ctx, env, ind); | |
| 441 } | |
| 442 | |
| 443 JNIEXPORT void JNICALL | |
| 444 FUN(PDFDocument_deleteObject)(JNIEnv *env, jobject self, jint num) | |
| 445 { | |
| 446 fz_context *ctx = get_context(env); | |
| 447 pdf_document *pdf = from_PDFDocument(env, self); | |
| 448 | |
| 449 if (!ctx || !pdf) return; | |
| 450 | |
| 451 fz_try(ctx) | |
| 452 pdf_delete_object(ctx, pdf, num); | |
| 453 fz_catch(ctx) | |
| 454 jni_rethrow_void(env, ctx); | |
| 455 } | |
| 456 | |
| 457 JNIEXPORT jobject JNICALL | |
| 458 FUN(PDFDocument_newPDFGraftMap)(JNIEnv *env, jobject self) | |
| 459 { | |
| 460 fz_context *ctx = get_context(env); | |
| 461 pdf_document *pdf = from_PDFDocument(env, self); | |
| 462 pdf_graft_map *map = NULL; | |
| 463 | |
| 464 if (!ctx || !pdf) return NULL; | |
| 465 | |
| 466 fz_try(ctx) | |
| 467 map = pdf_new_graft_map(ctx, pdf); | |
| 468 fz_catch(ctx) | |
| 469 jni_rethrow(env, ctx); | |
| 470 | |
| 471 return to_PDFGraftMap_safe_own(ctx, env, self, map); | |
| 472 } | |
| 473 | |
| 474 JNIEXPORT jobject JNICALL | |
| 475 FUN(PDFDocument_graftObject)(JNIEnv *env, jobject self, jobject jobj) | |
| 476 { | |
| 477 fz_context *ctx = get_context(env); | |
| 478 pdf_obj *obj = from_PDFObject(env, jobj); | |
| 479 pdf_document *dst = from_PDFDocument(env, self); | |
| 480 | |
| 481 if (!ctx || !dst) return NULL; | |
| 482 if (!dst) jni_throw_arg(env, "dst must not be null"); | |
| 483 if (!obj) jni_throw_arg(env, "object must not be null"); | |
| 484 | |
| 485 fz_try(ctx) | |
| 486 obj = pdf_graft_object(ctx, dst, obj); | |
| 487 fz_catch(ctx) | |
| 488 jni_rethrow(env, ctx); | |
| 489 | |
| 490 return to_PDFObject_safe_own(ctx, env, obj); | |
| 491 } | |
| 492 | |
| 493 JNIEXPORT jobject JNICALL | |
| 494 FUN(PDFDocument_addStreamBuffer)(JNIEnv *env, jobject self, jobject jbuf, jobject jobj, jboolean compressed) | |
| 495 { | |
| 496 fz_context *ctx = get_context(env); | |
| 497 pdf_document *pdf = from_PDFDocument(env, self); | |
| 498 pdf_obj *obj = from_PDFObject(env, jobj); | |
| 499 fz_buffer *buf = from_Buffer(env, jbuf); | |
| 500 pdf_obj *ind = NULL; | |
| 501 | |
| 502 if (!ctx || !pdf) return NULL; | |
| 503 if (!jbuf) jni_throw_arg(env, "buffer must not be null"); | |
| 504 | |
| 505 fz_try(ctx) | |
| 506 ind = pdf_add_stream(ctx, pdf, buf, obj, compressed); | |
| 507 fz_catch(ctx) | |
| 508 jni_rethrow(env, ctx); | |
| 509 | |
| 510 return to_PDFObject_safe_own(ctx, env, ind); | |
| 511 } | |
| 512 | |
| 513 JNIEXPORT jobject JNICALL | |
| 514 FUN(PDFDocument_addStreamString)(JNIEnv *env, jobject self, jstring jbuf, jobject jobj, jboolean compressed) | |
| 515 { | |
| 516 fz_context *ctx = get_context(env); | |
| 517 pdf_document *pdf = from_PDFDocument(env, self); | |
| 518 pdf_obj *obj = from_PDFObject(env, jobj); | |
| 519 fz_buffer *buf = NULL; | |
| 520 const char *sbuf = NULL; | |
| 521 pdf_obj *ind = NULL; | |
| 522 | |
| 523 if (!ctx || !pdf) return NULL; | |
| 524 if (!jbuf) jni_throw_arg(env, "buffer must not be null"); | |
| 525 | |
| 526 sbuf = (*env)->GetStringUTFChars(env, jbuf, NULL); | |
| 527 if (!sbuf) return NULL; | |
| 528 | |
| 529 fz_var(buf); | |
| 530 | |
| 531 fz_try(ctx) | |
| 532 { | |
| 533 buf = fz_new_buffer_from_copied_data(ctx, (const unsigned char *)sbuf, strlen(sbuf)); | |
| 534 ind = pdf_add_stream(ctx, pdf, buf, obj, compressed); | |
| 535 } | |
| 536 fz_always(ctx) | |
| 537 { | |
| 538 fz_drop_buffer(ctx, buf); | |
| 539 (*env)->ReleaseStringUTFChars(env, jbuf, sbuf); | |
| 540 } | |
| 541 fz_catch(ctx) | |
| 542 jni_rethrow(env, ctx); | |
| 543 | |
| 544 return to_PDFObject_safe_own(ctx, env, ind); | |
| 545 } | |
| 546 | |
| 547 JNIEXPORT jobject JNICALL | |
| 548 FUN(PDFDocument_addPageBuffer)(JNIEnv *env, jobject self, jobject jmediabox, jint rotate, jobject jresources, jobject jcontents) | |
| 549 { | |
| 550 fz_context *ctx = get_context(env); | |
| 551 pdf_document *pdf = from_PDFDocument(env, self); | |
| 552 fz_rect mediabox = from_Rect(env, jmediabox); | |
| 553 pdf_obj *resources = from_PDFObject(env, jresources); | |
| 554 fz_buffer *contents = from_Buffer(env, jcontents); | |
| 555 pdf_obj *ind = NULL; | |
| 556 | |
| 557 if (!ctx || !pdf) return NULL; | |
| 558 | |
| 559 fz_try(ctx) | |
| 560 ind = pdf_add_page(ctx, pdf, mediabox, rotate, resources, contents); | |
| 561 fz_catch(ctx) | |
| 562 jni_rethrow(env, ctx); | |
| 563 | |
| 564 return to_PDFObject_safe_own(ctx, env, ind); | |
| 565 } | |
| 566 | |
| 567 JNIEXPORT jobject JNICALL | |
| 568 FUN(PDFDocument_addPageString)(JNIEnv *env, jobject self, jobject jmediabox, jint rotate, jobject jresources, jstring jcontents) | |
| 569 { | |
| 570 fz_context *ctx = get_context(env); | |
| 571 pdf_document *pdf = from_PDFDocument(env, self); | |
| 572 fz_rect mediabox = from_Rect(env, jmediabox); | |
| 573 pdf_obj *resources = from_PDFObject(env, jresources); | |
| 574 const char *scontents = NULL; | |
| 575 fz_buffer *contents = NULL; | |
| 576 pdf_obj *ind = NULL; | |
| 577 | |
| 578 if (!ctx || !pdf) return NULL; | |
| 579 | |
| 580 scontents = (*env)->GetStringUTFChars(env, jcontents, NULL); | |
| 581 if (!scontents) return NULL; | |
| 582 | |
| 583 fz_var(contents); | |
| 584 | |
| 585 fz_try(ctx) | |
| 586 { | |
| 587 contents = fz_new_buffer_from_copied_data(ctx, (const unsigned char *)scontents, strlen(scontents)); | |
| 588 ind = pdf_add_page(ctx, pdf, mediabox, rotate, resources, contents); | |
| 589 } | |
| 590 fz_always(ctx) | |
| 591 { | |
| 592 fz_drop_buffer(ctx, contents); | |
| 593 (*env)->ReleaseStringUTFChars(env, jcontents, scontents); | |
| 594 } | |
| 595 fz_catch(ctx) | |
| 596 jni_rethrow(env, ctx); | |
| 597 | |
| 598 return to_PDFObject_safe_own(ctx, env, ind); | |
| 599 } | |
| 600 | |
| 601 JNIEXPORT void JNICALL | |
| 602 FUN(PDFDocument_insertPage)(JNIEnv *env, jobject self, jint jat, jobject jpage) | |
| 603 { | |
| 604 fz_context *ctx = get_context(env); | |
| 605 pdf_document *pdf = from_PDFDocument(env, self); | |
| 606 int at = jat; | |
| 607 pdf_obj *page = from_PDFObject(env, jpage); | |
| 608 | |
| 609 if (!ctx || !pdf) return; | |
| 610 if (!page) jni_throw_arg_void(env, "page must not be null"); | |
| 611 | |
| 612 fz_try(ctx) | |
| 613 pdf_insert_page(ctx, pdf, at, page); | |
| 614 fz_catch(ctx) | |
| 615 jni_rethrow_void(env, ctx); | |
| 616 } | |
| 617 | |
| 618 JNIEXPORT void JNICALL | |
| 619 FUN(PDFDocument_deletePage)(JNIEnv *env, jobject self, jint jat) | |
| 620 { | |
| 621 fz_context *ctx = get_context(env); | |
| 622 pdf_document *pdf = from_PDFDocument(env, self); | |
| 623 int at = jat; | |
| 624 | |
| 625 if (!ctx || !pdf) return; | |
| 626 | |
| 627 fz_try(ctx) | |
| 628 pdf_delete_page(ctx, pdf, at); | |
| 629 fz_catch(ctx) | |
| 630 jni_rethrow_void(env, ctx); | |
| 631 } | |
| 632 | |
| 633 JNIEXPORT jobject JNICALL | |
| 634 FUN(PDFDocument_addImage)(JNIEnv *env, jobject self, jobject jimage) | |
| 635 { | |
| 636 fz_context *ctx = get_context(env); | |
| 637 pdf_document *pdf = from_PDFDocument(env, self); | |
| 638 fz_image *image = from_Image(env, jimage); | |
| 639 pdf_obj *ind = NULL; | |
| 640 | |
| 641 if (!ctx || !pdf) return NULL; | |
| 642 if (!image) jni_throw_arg(env, "image must not be null"); | |
| 643 | |
| 644 fz_try(ctx) | |
| 645 ind = pdf_add_image(ctx, pdf, image); | |
| 646 fz_catch(ctx) | |
| 647 jni_rethrow(env, ctx); | |
| 648 | |
| 649 return to_PDFObject_safe_own(ctx, env, ind); | |
| 650 } | |
| 651 | |
| 652 JNIEXPORT jobject JNICALL | |
| 653 FUN(PDFDocument_addFont)(JNIEnv *env, jobject self, jobject jfont) | |
| 654 { | |
| 655 fz_context *ctx = get_context(env); | |
| 656 pdf_document *pdf = from_PDFDocument(env, self); | |
| 657 fz_font *font = from_Font(env, jfont); | |
| 658 pdf_obj *ind = NULL; | |
| 659 | |
| 660 if (!ctx || !pdf) return NULL; | |
| 661 if (!font) jni_throw_arg(env, "font must not be null"); | |
| 662 | |
| 663 fz_try(ctx) | |
| 664 ind = pdf_add_cid_font(ctx, pdf, font); | |
| 665 fz_catch(ctx) | |
| 666 jni_rethrow(env, ctx); | |
| 667 | |
| 668 return to_PDFObject_safe_own(ctx, env, ind); | |
| 669 } | |
| 670 | |
| 671 JNIEXPORT jobject JNICALL | |
| 672 FUN(PDFDocument_addCJKFont)(JNIEnv *env, jobject self, jobject jfont, jint ordering, jint wmode, jboolean serif) | |
| 673 { | |
| 674 fz_context *ctx = get_context(env); | |
| 675 pdf_document *pdf = from_PDFDocument(env, self); | |
| 676 fz_font *font = from_Font(env, jfont); | |
| 677 pdf_obj *ind = NULL; | |
| 678 | |
| 679 if (!ctx || !pdf) return NULL; | |
| 680 if (!font) jni_throw_arg(env, "font must not be null"); | |
| 681 | |
| 682 fz_try(ctx) | |
| 683 ind = pdf_add_cjk_font(ctx, pdf, font, ordering, wmode, serif); | |
| 684 fz_catch(ctx) | |
| 685 jni_rethrow(env, ctx); | |
| 686 | |
| 687 return to_PDFObject_safe_own(ctx, env, ind); | |
| 688 } | |
| 689 | |
| 690 JNIEXPORT jobject JNICALL | |
| 691 FUN(PDFDocument_addSimpleFont)(JNIEnv *env, jobject self, jobject jfont, jint encoding) | |
| 692 { | |
| 693 fz_context *ctx = get_context(env); | |
| 694 pdf_document *pdf = from_PDFDocument(env, self); | |
| 695 fz_font *font = from_Font(env, jfont); | |
| 696 pdf_obj *ind = NULL; | |
| 697 | |
| 698 if (!ctx || !pdf) return NULL; | |
| 699 if (!font) jni_throw_arg(env, "font must not be null"); | |
| 700 | |
| 701 fz_try(ctx) | |
| 702 ind = pdf_add_simple_font(ctx, pdf, font, encoding); | |
| 703 fz_catch(ctx) | |
| 704 jni_rethrow(env, ctx); | |
| 705 | |
| 706 return to_PDFObject_safe_own(ctx, env, ind); | |
| 707 } | |
| 708 | |
| 709 JNIEXPORT jboolean JNICALL | |
| 710 FUN(PDFDocument_hasUnsavedChanges)(JNIEnv *env, jobject self) | |
| 711 { | |
| 712 fz_context *ctx = get_context(env); | |
| 713 pdf_document *pdf = from_PDFDocument(env, self); | |
| 714 if (!ctx || !pdf) return JNI_FALSE; | |
| 715 return pdf_has_unsaved_changes(ctx, pdf) ? JNI_TRUE : JNI_FALSE; | |
| 716 } | |
| 717 | |
| 718 JNIEXPORT jboolean JNICALL | |
| 719 FUN(PDFDocument_wasRepaired)(JNIEnv *env, jobject self) | |
| 720 { | |
| 721 fz_context *ctx = get_context(env); | |
| 722 pdf_document *pdf = from_PDFDocument(env, self); | |
| 723 if (!ctx || !pdf) return JNI_FALSE; | |
| 724 return pdf_was_repaired(ctx, pdf) ? JNI_TRUE : JNI_FALSE; | |
| 725 } | |
| 726 | |
| 727 JNIEXPORT jboolean JNICALL | |
| 728 FUN(PDFDocument_canBeSavedIncrementally)(JNIEnv *env, jobject self) | |
| 729 { | |
| 730 fz_context *ctx = get_context(env); | |
| 731 pdf_document *pdf = from_PDFDocument(env, self); | |
| 732 if (!ctx || !pdf) return JNI_FALSE; | |
| 733 return pdf_can_be_saved_incrementally(ctx, pdf) ? JNI_TRUE : JNI_FALSE; | |
| 734 } | |
| 735 | |
| 736 static fz_stream * | |
| 737 SeekableOutputStream_as_stream(fz_context *ctx, void *opaque) | |
| 738 { | |
| 739 SeekableStreamState *in_state = opaque; | |
| 740 SeekableStreamState *state; | |
| 741 fz_stream *stm; | |
| 742 | |
| 743 state = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_state"); | |
| 744 state->stream = in_state->stream; | |
| 745 state->array = in_state->array; | |
| 746 | |
| 747 stm = fz_new_stream(ctx, state, SeekableInputStream_next, fz_free); | |
| 748 stm->seek = SeekableInputStream_seek; | |
| 749 | |
| 750 return stm; | |
| 751 } | |
| 752 | |
| 753 JNIEXPORT void JNICALL | |
| 754 FUN(PDFDocument_nativeSaveWithStream)(JNIEnv *env, jobject self, jobject jstream, jstring joptions) | |
| 755 { | |
| 756 fz_context *ctx = get_context(env); | |
| 757 pdf_document *pdf = from_PDFDocument(env, self); | |
| 758 SeekableStreamState *state = NULL; | |
| 759 jobject stream = NULL; | |
| 760 jbyteArray array = NULL; | |
| 761 fz_output *out = NULL; | |
| 762 const char *options = NULL; | |
| 763 pdf_write_options pwo; | |
| 764 | |
| 765 fz_var(state); | |
| 766 fz_var(out); | |
| 767 fz_var(stream); | |
| 768 fz_var(array); | |
| 769 | |
| 770 if (!ctx || !pdf) return; | |
| 771 if (!jstream) jni_throw_arg_void(env, "stream must not be null"); | |
| 772 | |
| 773 if (joptions) | |
| 774 { | |
| 775 options = (*env)->GetStringUTFChars(env, joptions, NULL); | |
| 776 if (!options) return; | |
| 777 } | |
| 778 | |
| 779 stream = (*env)->NewGlobalRef(env, jstream); | |
| 780 if (!stream) | |
| 781 { | |
| 782 if (options) (*env)->ReleaseStringUTFChars(env, joptions, options); | |
| 783 return; | |
| 784 } | |
| 785 | |
| 786 array = (*env)->NewByteArray(env, sizeof state->buffer); | |
| 787 if ((*env)->ExceptionCheck(env)) | |
| 788 { | |
| 789 if (options) (*env)->ReleaseStringUTFChars(env, joptions, options); | |
| 790 (*env)->DeleteGlobalRef(env, stream); | |
| 791 return; | |
| 792 } | |
| 793 if (!array) | |
| 794 { | |
| 795 if (options) (*env)->ReleaseStringUTFChars(env, joptions, options); | |
| 796 (*env)->DeleteGlobalRef(env, stream); | |
| 797 jni_throw_run_void(env, "cannot create byte array"); | |
| 798 } | |
| 799 | |
| 800 array = (*env)->NewGlobalRef(env, array); | |
| 801 if (!array) | |
| 802 { | |
| 803 if (options) (*env)->ReleaseStringUTFChars(env, joptions, options); | |
| 804 (*env)->DeleteGlobalRef(env, stream); | |
| 805 jni_throw_run_void(env, "cannot create global reference"); | |
| 806 } | |
| 807 | |
| 808 fz_try(ctx) | |
| 809 { | |
| 810 if (jstream) | |
| 811 { | |
| 812 /* No exceptions can occur from here to stream owning state, so we must not free state. */ | |
| 813 state = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_state"); | |
| 814 state->stream = stream; | |
| 815 state->array = array; | |
| 816 | |
| 817 /* Ownership transferred to state. */ | |
| 818 stream = NULL; | |
| 819 array = NULL; | |
| 820 | |
| 821 /* Stream takes ownership of state. */ | |
| 822 out = fz_new_output(ctx, sizeof state->buffer, state, SeekableOutputStream_write, NULL, SeekableOutputStream_drop); | |
| 823 out->seek = SeekableOutputStream_seek; | |
| 824 out->tell = SeekableOutputStream_tell; | |
| 825 out->truncate = SeekableOutputStream_truncate; | |
| 826 out->as_stream = SeekableOutputStream_as_stream; | |
| 827 | |
| 828 /* these are now owned by 'out' */ | |
| 829 state = NULL; | |
| 830 } | |
| 831 | |
| 832 pdf_parse_write_options(ctx, &pwo, options); | |
| 833 pdf_write_document(ctx, pdf, out, &pwo); | |
| 834 fz_close_output(ctx, out); | |
| 835 } | |
| 836 fz_always(ctx) | |
| 837 { | |
| 838 fz_drop_output(ctx, out); | |
| 839 if (options) | |
| 840 (*env)->ReleaseStringUTFChars(env, joptions, options); | |
| 841 } | |
| 842 fz_catch(ctx) | |
| 843 { | |
| 844 (*env)->DeleteGlobalRef(env, array); | |
| 845 (*env)->DeleteGlobalRef(env, stream); | |
| 846 jni_rethrow_void(env, ctx); | |
| 847 } | |
| 848 } | |
| 849 | |
| 850 JNIEXPORT void JNICALL | |
| 851 FUN(PDFDocument_save)(JNIEnv *env, jobject self, jstring jfilename, jstring joptions) | |
| 852 { | |
| 853 fz_context *ctx = get_context(env); | |
| 854 pdf_document *pdf = from_PDFDocument(env, self); | |
| 855 const char *filename = NULL; | |
| 856 const char *options = NULL; | |
| 857 pdf_write_options pwo; | |
| 858 | |
| 859 if (!ctx || !pdf) return; | |
| 860 if (!jfilename) jni_throw_arg_void(env, "filename must not be null"); | |
| 861 | |
| 862 filename = (*env)->GetStringUTFChars(env, jfilename, NULL); | |
| 863 if (!filename) return; | |
| 864 | |
| 865 if (joptions) | |
| 866 { | |
| 867 options = (*env)->GetStringUTFChars(env, joptions, NULL); | |
| 868 if (!options) | |
| 869 { | |
| 870 (*env)->ReleaseStringUTFChars(env, jfilename, filename); | |
| 871 return; | |
| 872 } | |
| 873 } | |
| 874 | |
| 875 fz_try(ctx) | |
| 876 { | |
| 877 pdf_parse_write_options(ctx, &pwo, options); | |
| 878 pdf_save_document(ctx, pdf, filename, &pwo); | |
| 879 } | |
| 880 fz_always(ctx) | |
| 881 { | |
| 882 if (options) | |
| 883 (*env)->ReleaseStringUTFChars(env, joptions, options); | |
| 884 (*env)->ReleaseStringUTFChars(env, jfilename, filename); | |
| 885 } | |
| 886 fz_catch(ctx) | |
| 887 jni_rethrow_void(env, ctx); | |
| 888 } | |
| 889 | |
| 890 JNIEXPORT void JNICALL | |
| 891 FUN(PDFDocument_enableJs)(JNIEnv *env, jobject self) | |
| 892 { | |
| 893 fz_context *ctx = get_context(env); | |
| 894 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 895 | |
| 896 if (!ctx || !pdf) return; | |
| 897 | |
| 898 fz_try(ctx) | |
| 899 pdf_enable_js(ctx, pdf); | |
| 900 fz_catch(ctx) | |
| 901 jni_rethrow_void(env, ctx); | |
| 902 } | |
| 903 | |
| 904 JNIEXPORT void JNICALL | |
| 905 FUN(PDFDocument_disableJs)(JNIEnv *env, jobject self) | |
| 906 { | |
| 907 fz_context *ctx = get_context(env); | |
| 908 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 909 | |
| 910 if (!ctx || !pdf) return; | |
| 911 | |
| 912 fz_try(ctx) | |
| 913 pdf_disable_js(ctx, pdf); | |
| 914 fz_catch(ctx) | |
| 915 jni_rethrow_void(env, ctx); | |
| 916 } | |
| 917 | |
| 918 JNIEXPORT jboolean JNICALL | |
| 919 FUN(PDFDocument_isJsSupported)(JNIEnv *env, jobject self) | |
| 920 { | |
| 921 fz_context *ctx = get_context(env); | |
| 922 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 923 jboolean supported = JNI_FALSE; | |
| 924 | |
| 925 if (!ctx || !pdf) return JNI_FALSE; | |
| 926 | |
| 927 fz_try(ctx) | |
| 928 supported = pdf_js_supported(ctx, pdf); | |
| 929 fz_catch(ctx) | |
| 930 jni_rethrow(env, ctx); | |
| 931 | |
| 932 return supported; | |
| 933 } | |
| 934 | |
| 935 JNIEXPORT void JNICALL | |
| 936 FUN(PDFDocument_setJsEventListener)(JNIEnv *env, jobject self, jobject jlistener) | |
| 937 { | |
| 938 fz_context *ctx = get_context(env); | |
| 939 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 940 | |
| 941 if (!ctx || !pdf) return; | |
| 942 if (!jlistener) jni_throw_arg_void(env, "listener must not be null"); | |
| 943 | |
| 944 jlistener = (*env)->NewGlobalRef(env, jlistener); | |
| 945 if (!jlistener) jni_throw_arg_void(env, "unable to get reference to listener"); | |
| 946 | |
| 947 fz_try(ctx) | |
| 948 pdf_set_doc_event_callback(ctx, pdf, event_cb, free_event_cb_data, jlistener); | |
| 949 fz_catch(ctx) | |
| 950 jni_rethrow_void(env, ctx); | |
| 951 } | |
| 952 | |
| 953 JNIEXPORT void JNICALL | |
| 954 FUN(PDFDocument_calculate)(JNIEnv *env, jobject self) | |
| 955 { | |
| 956 fz_context *ctx = get_context(env); | |
| 957 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 958 | |
| 959 if (!ctx || !pdf) return; | |
| 960 | |
| 961 fz_try(ctx) | |
| 962 if (pdf->recalculate) | |
| 963 pdf_calculate_form(ctx, pdf); | |
| 964 fz_catch(ctx) | |
| 965 jni_rethrow_void(env, ctx); | |
| 966 } | |
| 967 | |
| 968 JNIEXPORT jint JNICALL | |
| 969 FUN(PDFDocument_countVersions)(JNIEnv *env, jobject self) | |
| 970 { | |
| 971 fz_context *ctx = get_context(env); | |
| 972 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 973 int val = 0; | |
| 974 | |
| 975 if (!ctx || !pdf) return 0; | |
| 976 | |
| 977 fz_try(ctx) | |
| 978 val = pdf_count_versions(ctx, pdf); | |
| 979 fz_catch(ctx) | |
| 980 jni_rethrow(env, ctx); | |
| 981 | |
| 982 return val; | |
| 983 } | |
| 984 | |
| 985 JNIEXPORT jint JNICALL | |
| 986 FUN(PDFDocument_countUnsavedVersions)(JNIEnv *env, jobject self) | |
| 987 { | |
| 988 fz_context *ctx = get_context(env); | |
| 989 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 990 int val = 0; | |
| 991 | |
| 992 if (!ctx || !pdf) return 0; | |
| 993 | |
| 994 fz_try(ctx) | |
| 995 val = pdf_count_unsaved_versions(ctx, pdf); | |
| 996 fz_catch(ctx) | |
| 997 jni_rethrow(env, ctx); | |
| 998 | |
| 999 return val; | |
| 1000 } | |
| 1001 | |
| 1002 JNIEXPORT jint JNICALL | |
| 1003 FUN(PDFDocument_validateChangeHistory)(JNIEnv *env, jobject self) | |
| 1004 { | |
| 1005 fz_context *ctx = get_context(env); | |
| 1006 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 1007 int val = 0; | |
| 1008 | |
| 1009 if (!ctx || !pdf) return 0; | |
| 1010 | |
| 1011 fz_try(ctx) | |
| 1012 val = pdf_validate_change_history(ctx, pdf); | |
| 1013 fz_catch(ctx) | |
| 1014 jni_rethrow(env, ctx); | |
| 1015 | |
| 1016 return val; | |
| 1017 } | |
| 1018 | |
| 1019 JNIEXPORT jboolean JNICALL | |
| 1020 FUN(PDFDocument_wasPureXFA)(JNIEnv *env, jobject self) | |
| 1021 { | |
| 1022 fz_context *ctx = get_context(env); | |
| 1023 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 1024 jboolean val = JNI_FALSE; | |
| 1025 | |
| 1026 if (!ctx || !pdf) return JNI_FALSE; | |
| 1027 | |
| 1028 fz_try(ctx) | |
| 1029 val = pdf_was_pure_xfa(ctx, pdf); | |
| 1030 fz_catch(ctx) | |
| 1031 jni_rethrow(env, ctx); | |
| 1032 | |
| 1033 return val; | |
| 1034 } | |
| 1035 | |
| 1036 JNIEXPORT jboolean JNICALL | |
| 1037 FUN(PDFDocument_wasLinearized)(JNIEnv *env, jobject self) | |
| 1038 { | |
| 1039 fz_context *ctx = get_context(env); | |
| 1040 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 1041 jboolean val = JNI_FALSE; | |
| 1042 | |
| 1043 if (!ctx || !pdf) return JNI_FALSE; | |
| 1044 | |
| 1045 fz_try(ctx) | |
| 1046 val = pdf_doc_was_linearized(ctx, pdf); | |
| 1047 fz_catch(ctx) | |
| 1048 jni_rethrow(env, ctx); | |
| 1049 | |
| 1050 return val; | |
| 1051 } | |
| 1052 | |
| 1053 JNIEXPORT void JNICALL | |
| 1054 FUN(PDFDocument_graftPage)(JNIEnv *env, jobject self, jint pageTo, jobject jobj, jint pageFrom) | |
| 1055 { | |
| 1056 fz_context *ctx = get_context(env); | |
| 1057 pdf_document *src = from_PDFDocument(env, jobj); | |
| 1058 pdf_document *dst = from_PDFDocument(env, self); | |
| 1059 | |
| 1060 if (!ctx || !dst) return; | |
| 1061 if (!src) jni_throw_arg_void(env, "Source Document must not be null"); | |
| 1062 | |
| 1063 fz_try(ctx) | |
| 1064 pdf_graft_page(ctx, dst, pageTo, src, pageFrom); | |
| 1065 fz_catch(ctx) | |
| 1066 jni_rethrow_void(env, ctx); | |
| 1067 } | |
| 1068 | |
| 1069 JNIEXPORT void JNICALL | |
| 1070 FUN(PDFDocument_enableJournal)(JNIEnv *env, jobject self) | |
| 1071 { | |
| 1072 fz_context *ctx = get_context(env); | |
| 1073 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1074 | |
| 1075 if (!ctx || !pdf) return; | |
| 1076 | |
| 1077 pdf_enable_journal(ctx, pdf); | |
| 1078 } | |
| 1079 | |
| 1080 JNIEXPORT void JNICALL | |
| 1081 FUN(PDFDocument_saveJournalWithStream)(JNIEnv *env, jobject self, jobject jstream) | |
| 1082 { | |
| 1083 fz_context *ctx = get_context(env); | |
| 1084 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1085 SeekableStreamState *state = NULL; | |
| 1086 jobject stream = NULL; | |
| 1087 jbyteArray array = NULL; | |
| 1088 fz_output *out = NULL; | |
| 1089 | |
| 1090 if (!ctx || !pdf) | |
| 1091 return; | |
| 1092 if (!jstream) | |
| 1093 jni_throw_arg_void(env, "stream must not be null"); | |
| 1094 | |
| 1095 fz_var(state); | |
| 1096 fz_var(out); | |
| 1097 fz_var(stream); | |
| 1098 fz_var(array); | |
| 1099 | |
| 1100 stream = (*env)->NewGlobalRef(env, jstream); | |
| 1101 if (!stream) | |
| 1102 jni_throw_run_void(env, "invalid stream"); | |
| 1103 | |
| 1104 array = (*env)->NewByteArray(env, sizeof state->buffer); | |
| 1105 if ((*env)->ExceptionCheck(env)) | |
| 1106 { | |
| 1107 (*env)->DeleteGlobalRef(env, stream); | |
| 1108 return; | |
| 1109 } | |
| 1110 if (!array) | |
| 1111 { | |
| 1112 (*env)->DeleteGlobalRef(env, stream); | |
| 1113 jni_throw_run_void(env, "cannot create byte array"); | |
| 1114 } | |
| 1115 | |
| 1116 array = (*env)->NewGlobalRef(env, array); | |
| 1117 if (!array) | |
| 1118 { | |
| 1119 (*env)->DeleteGlobalRef(env, stream); | |
| 1120 jni_throw_run_void(env, "cannot create global reference"); | |
| 1121 } | |
| 1122 | |
| 1123 fz_try(ctx) | |
| 1124 { | |
| 1125 /* No exceptions can occur from here to stream owning state, so we must not free state. */ | |
| 1126 state = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_state"); | |
| 1127 state->stream = stream; | |
| 1128 state->array = array; | |
| 1129 | |
| 1130 /* Ownership transferred to state. */ | |
| 1131 stream = NULL; | |
| 1132 array = NULL; | |
| 1133 | |
| 1134 /* Stream takes ownership of state. */ | |
| 1135 out = fz_new_output(ctx, sizeof state->buffer, state, SeekableOutputStream_write, NULL, SeekableOutputStream_drop); | |
| 1136 out->seek = SeekableOutputStream_seek; | |
| 1137 out->tell = SeekableOutputStream_tell; | |
| 1138 out->truncate = SeekableOutputStream_truncate; | |
| 1139 out->as_stream = SeekableOutputStream_as_stream; | |
| 1140 | |
| 1141 /* these are now owned by 'out' */ | |
| 1142 state = NULL; | |
| 1143 | |
| 1144 pdf_write_journal(ctx, pdf, out); | |
| 1145 fz_close_output(ctx, out); | |
| 1146 } | |
| 1147 fz_always(ctx) | |
| 1148 { | |
| 1149 fz_drop_output(ctx, out); | |
| 1150 } | |
| 1151 fz_catch(ctx) | |
| 1152 { | |
| 1153 (*env)->DeleteGlobalRef(env, array); | |
| 1154 (*env)->DeleteGlobalRef(env, stream); | |
| 1155 jni_rethrow_void(env, ctx); | |
| 1156 } | |
| 1157 } | |
| 1158 | |
| 1159 JNIEXPORT void JNICALL | |
| 1160 FUN(PDFDocument_loadJournalWithStream)(JNIEnv *env, jobject self, jobject jstream) | |
| 1161 { | |
| 1162 fz_context *ctx = get_context(env); | |
| 1163 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1164 SeekableStreamState *state = NULL; | |
| 1165 jobject stream = NULL; | |
| 1166 jbyteArray array = NULL; | |
| 1167 fz_stream *stm = NULL; | |
| 1168 | |
| 1169 if (!ctx || !pdf) | |
| 1170 return; | |
| 1171 if (!jstream) | |
| 1172 jni_throw_arg_void(env, "stream must not be null"); | |
| 1173 | |
| 1174 fz_var(state); | |
| 1175 fz_var(stm); | |
| 1176 fz_var(stream); | |
| 1177 fz_var(array); | |
| 1178 | |
| 1179 stream = (*env)->NewGlobalRef(env, jstream); | |
| 1180 if (!stream) | |
| 1181 jni_throw_run_void(env, "invalid stream"); | |
| 1182 | |
| 1183 array = (*env)->NewByteArray(env, sizeof state->buffer); | |
| 1184 if ((*env)->ExceptionCheck(env)) | |
| 1185 { | |
| 1186 (*env)->DeleteGlobalRef(env, stream); | |
| 1187 return; | |
| 1188 } | |
| 1189 if (!array) | |
| 1190 { | |
| 1191 (*env)->DeleteGlobalRef(env, stream); | |
| 1192 jni_throw_run_void(env, "cannot create byte array"); | |
| 1193 } | |
| 1194 | |
| 1195 array = (*env)->NewGlobalRef(env, array); | |
| 1196 if (!array) | |
| 1197 { | |
| 1198 (*env)->DeleteGlobalRef(env, stream); | |
| 1199 jni_throw_run_void(env, "cannot create global reference"); | |
| 1200 } | |
| 1201 | |
| 1202 fz_try(ctx) | |
| 1203 { | |
| 1204 /* No exceptions can occur from here to stream owning state, so we must not free state. */ | |
| 1205 state = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_state"); | |
| 1206 state->stream = stream; | |
| 1207 state->array = array; | |
| 1208 | |
| 1209 /* Ownership transferred to state. */ | |
| 1210 stream = NULL; | |
| 1211 array = NULL; | |
| 1212 | |
| 1213 /* Stream takes ownership of accstate. */ | |
| 1214 stm = fz_new_stream(ctx, state, SeekableInputStream_next, SeekableInputStream_drop); | |
| 1215 stm->seek = SeekableInputStream_seek; | |
| 1216 | |
| 1217 pdf_read_journal(ctx, pdf, stm); | |
| 1218 } | |
| 1219 fz_always(ctx) | |
| 1220 { | |
| 1221 fz_drop_stream(ctx, stm); | |
| 1222 } | |
| 1223 fz_catch(ctx) | |
| 1224 { | |
| 1225 (*env)->DeleteGlobalRef(env, array); | |
| 1226 (*env)->DeleteGlobalRef(env, stream); | |
| 1227 jni_rethrow_void(env, ctx); | |
| 1228 } | |
| 1229 } | |
| 1230 | |
| 1231 JNIEXPORT void JNICALL | |
| 1232 FUN(PDFDocument_saveJournal)(JNIEnv *env, jobject self, jstring jfilename) | |
| 1233 { | |
| 1234 fz_context *ctx = get_context(env); | |
| 1235 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1236 const char *filename = NULL; | |
| 1237 | |
| 1238 if (!ctx || !pdf) | |
| 1239 return; | |
| 1240 if (!jfilename) | |
| 1241 jni_throw_arg_void(env, "filename must not be null"); | |
| 1242 | |
| 1243 filename = (*env)->GetStringUTFChars(env, jfilename, NULL); | |
| 1244 if (!filename) | |
| 1245 jni_throw_run_void(env, "cannot get characters in filename"); | |
| 1246 | |
| 1247 fz_try(ctx) | |
| 1248 pdf_save_journal(ctx, pdf, filename); | |
| 1249 fz_always(ctx) | |
| 1250 (*env)->ReleaseStringUTFChars(env, jfilename, filename); | |
| 1251 fz_catch(ctx) | |
| 1252 jni_rethrow_void(env, ctx); | |
| 1253 } | |
| 1254 | |
| 1255 JNIEXPORT void JNICALL | |
| 1256 FUN(PDFDocument_loadJournal)(JNIEnv *env, jobject self, jstring jfilename) | |
| 1257 { | |
| 1258 fz_context *ctx = get_context(env); | |
| 1259 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1260 const char *filename = NULL; | |
| 1261 | |
| 1262 if (!ctx || !pdf) | |
| 1263 return; | |
| 1264 if (!jfilename) | |
| 1265 jni_throw_arg_void(env, "filename must not be null"); | |
| 1266 | |
| 1267 filename = (*env)->GetStringUTFChars(env, jfilename, NULL); | |
| 1268 if (!filename) | |
| 1269 jni_throw_run_void(env, "cannot get characters in filename"); | |
| 1270 | |
| 1271 fz_try(ctx) | |
| 1272 pdf_load_journal(ctx, pdf, filename); | |
| 1273 fz_always(ctx) | |
| 1274 (*env)->ReleaseStringUTFChars(env, jfilename, filename); | |
| 1275 fz_catch(ctx) | |
| 1276 jni_rethrow_void(env, ctx); | |
| 1277 } | |
| 1278 | |
| 1279 JNIEXPORT jint JNICALL | |
| 1280 FUN(PDFDocument_undoRedoPosition)(JNIEnv *env, jobject self) | |
| 1281 { | |
| 1282 fz_context *ctx = get_context(env); | |
| 1283 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1284 int steps; | |
| 1285 | |
| 1286 if (!ctx || !pdf) return 0; | |
| 1287 | |
| 1288 return pdf_undoredo_state(ctx, pdf, &steps); | |
| 1289 } | |
| 1290 | |
| 1291 JNIEXPORT jint JNICALL | |
| 1292 FUN(PDFDocument_undoRedoSteps)(JNIEnv *env, jobject self) | |
| 1293 { | |
| 1294 fz_context *ctx = get_context(env); | |
| 1295 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1296 int steps; | |
| 1297 | |
| 1298 if (!ctx || !pdf) return 0; | |
| 1299 | |
| 1300 (void)pdf_undoredo_state(ctx, pdf, &steps); | |
| 1301 | |
| 1302 return steps; | |
| 1303 } | |
| 1304 | |
| 1305 JNIEXPORT jstring JNICALL | |
| 1306 FUN(PDFDocument_undoRedoStep)(JNIEnv *env, jobject self, jint n) | |
| 1307 { | |
| 1308 fz_context *ctx = get_context(env); | |
| 1309 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1310 const char *step; | |
| 1311 | |
| 1312 if (!ctx || !pdf) return NULL; | |
| 1313 | |
| 1314 step = pdf_undoredo_step(ctx, pdf, n); | |
| 1315 | |
| 1316 return (*env)->NewStringUTF(env, step); | |
| 1317 } | |
| 1318 | |
| 1319 JNIEXPORT jboolean JNICALL | |
| 1320 FUN(PDFDocument_canUndo)(JNIEnv *env, jobject self) | |
| 1321 { | |
| 1322 fz_context *ctx = get_context(env); | |
| 1323 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1324 jboolean can = JNI_FALSE; | |
| 1325 | |
| 1326 if (!ctx || !pdf) return JNI_FALSE; | |
| 1327 | |
| 1328 fz_try(ctx) | |
| 1329 can = pdf_can_undo(ctx, pdf); | |
| 1330 fz_catch(ctx) | |
| 1331 jni_rethrow(env, ctx); | |
| 1332 | |
| 1333 return can; | |
| 1334 } | |
| 1335 | |
| 1336 JNIEXPORT jboolean JNICALL | |
| 1337 FUN(PDFDocument_canRedo)(JNIEnv *env, jobject self) | |
| 1338 { | |
| 1339 fz_context *ctx = get_context(env); | |
| 1340 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1341 jboolean can = JNI_FALSE; | |
| 1342 | |
| 1343 if (!ctx || !pdf) return JNI_FALSE; | |
| 1344 | |
| 1345 fz_try(ctx) | |
| 1346 can = pdf_can_redo(ctx, pdf); | |
| 1347 fz_catch(ctx) | |
| 1348 jni_rethrow(env, ctx); | |
| 1349 | |
| 1350 return can; | |
| 1351 } | |
| 1352 | |
| 1353 JNIEXPORT void JNICALL | |
| 1354 FUN(PDFDocument_undo)(JNIEnv *env, jobject self) | |
| 1355 { | |
| 1356 fz_context *ctx = get_context(env); | |
| 1357 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1358 | |
| 1359 if (!ctx || !pdf) return; | |
| 1360 | |
| 1361 fz_try(ctx) | |
| 1362 pdf_undo(ctx, pdf); | |
| 1363 fz_catch(ctx) | |
| 1364 jni_rethrow_void(env, ctx); | |
| 1365 } | |
| 1366 | |
| 1367 JNIEXPORT void JNICALL | |
| 1368 FUN(PDFDocument_redo)(JNIEnv *env, jobject self) | |
| 1369 { | |
| 1370 fz_context *ctx = get_context(env); | |
| 1371 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1372 | |
| 1373 if (!ctx || !pdf) return; | |
| 1374 | |
| 1375 fz_try(ctx) | |
| 1376 pdf_redo(ctx, pdf); | |
| 1377 fz_catch(ctx) | |
| 1378 jni_rethrow_void(env, ctx); | |
| 1379 } | |
| 1380 | |
| 1381 JNIEXPORT void JNICALL | |
| 1382 FUN(PDFDocument_beginOperation)(JNIEnv *env, jobject self, jstring joperation) | |
| 1383 { | |
| 1384 fz_context *ctx = get_context(env); | |
| 1385 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1386 const char *operation = NULL; | |
| 1387 | |
| 1388 if (!ctx || !pdf) return; | |
| 1389 if (!joperation) jni_throw_arg_void(env, "operation must not be null"); | |
| 1390 | |
| 1391 operation = (*env)->GetStringUTFChars(env, joperation, NULL); | |
| 1392 if (!operation) return; | |
| 1393 | |
| 1394 fz_try(ctx) | |
| 1395 pdf_begin_operation(ctx, pdf, operation); | |
| 1396 fz_always(ctx) | |
| 1397 (*env)->ReleaseStringUTFChars(env, joperation, operation); | |
| 1398 fz_catch(ctx) | |
| 1399 jni_rethrow_void(env, ctx); | |
| 1400 } | |
| 1401 | |
| 1402 JNIEXPORT void JNICALL | |
| 1403 FUN(PDFDocument_beginImplicitOperation)(JNIEnv *env, jobject self) | |
| 1404 { | |
| 1405 fz_context *ctx = get_context(env); | |
| 1406 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1407 | |
| 1408 if (!ctx || !pdf) return; | |
| 1409 | |
| 1410 fz_try(ctx) | |
| 1411 pdf_begin_implicit_operation(ctx, pdf); | |
| 1412 fz_catch(ctx) | |
| 1413 jni_rethrow_void(env, ctx); | |
| 1414 } | |
| 1415 | |
| 1416 JNIEXPORT void JNICALL | |
| 1417 FUN(PDFDocument_endOperation)(JNIEnv *env, jobject self) | |
| 1418 { | |
| 1419 fz_context *ctx = get_context(env); | |
| 1420 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1421 | |
| 1422 if (!ctx || !pdf) return; | |
| 1423 | |
| 1424 fz_try(ctx) | |
| 1425 pdf_end_operation(ctx, pdf); | |
| 1426 fz_catch(ctx) | |
| 1427 jni_rethrow_void(env, ctx); | |
| 1428 } | |
| 1429 | |
| 1430 JNIEXPORT void JNICALL | |
| 1431 FUN(PDFDocument_abandonOperation)(JNIEnv *env, jobject self) | |
| 1432 { | |
| 1433 fz_context *ctx = get_context(env); | |
| 1434 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1435 | |
| 1436 if (!ctx || !pdf) return; | |
| 1437 | |
| 1438 fz_try(ctx) | |
| 1439 pdf_abandon_operation(ctx, pdf); | |
| 1440 fz_catch(ctx) | |
| 1441 jni_rethrow_void(env, ctx); | |
| 1442 } | |
| 1443 | |
| 1444 JNIEXPORT jboolean JNICALL | |
| 1445 FUN(PDFDocument_isRedacted)(JNIEnv *env, jobject self) | |
| 1446 { | |
| 1447 fz_context *ctx = get_context(env); | |
| 1448 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1449 | |
| 1450 if (!ctx || !pdf) return JNI_FALSE; | |
| 1451 return pdf->redacted; | |
| 1452 } | |
| 1453 | |
| 1454 JNIEXPORT jint JNICALL | |
| 1455 FUN(PDFDocument_getLanguage)(JNIEnv *env, jobject self) | |
| 1456 { | |
| 1457 fz_context *ctx = get_context(env); | |
| 1458 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1459 int lang; | |
| 1460 | |
| 1461 if (!ctx || !pdf) return FZ_LANG_UNSET; | |
| 1462 | |
| 1463 fz_try(ctx) | |
| 1464 lang = pdf_document_language(ctx, pdf); | |
| 1465 fz_catch(ctx) | |
| 1466 jni_rethrow(env, ctx); | |
| 1467 | |
| 1468 return lang; | |
| 1469 } | |
| 1470 | |
| 1471 JNIEXPORT void JNICALL | |
| 1472 FUN(PDFDocument_setLanguage)(JNIEnv *env, jobject self, jint lang) | |
| 1473 { | |
| 1474 fz_context *ctx = get_context(env); | |
| 1475 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1476 | |
| 1477 if (!ctx || !pdf) return; | |
| 1478 | |
| 1479 fz_try(ctx) | |
| 1480 pdf_set_document_language(ctx, pdf, lang); | |
| 1481 fz_catch(ctx) | |
| 1482 jni_rethrow_void(env, ctx); | |
| 1483 } | |
| 1484 | |
| 1485 JNIEXPORT jint JNICALL | |
| 1486 FUN(PDFDocument_countSignatures)(JNIEnv *env, jobject self) | |
| 1487 { | |
| 1488 fz_context *ctx = get_context(env); | |
| 1489 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 1490 jint val = -1; | |
| 1491 | |
| 1492 if (!ctx || !pdf) return -1; | |
| 1493 | |
| 1494 fz_try(ctx) | |
| 1495 val = pdf_count_signatures(ctx, pdf); | |
| 1496 fz_catch(ctx) | |
| 1497 jni_rethrow(env, ctx); | |
| 1498 | |
| 1499 return val; | |
| 1500 } | |
| 1501 | |
| 1502 JNIEXPORT jobject JNICALL | |
| 1503 FUN(PDFDocument_addEmbeddedFile)(JNIEnv *env, jobject self, jstring jfilename, jstring jmimetype, jobject jcontents, jlong created, jlong modified, jboolean addChecksum) | |
| 1504 { | |
| 1505 fz_context *ctx = get_context(env); | |
| 1506 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 1507 const char *filename = NULL; | |
| 1508 const char *mimetype = NULL; | |
| 1509 fz_buffer *contents = from_Buffer(env, jcontents); | |
| 1510 pdf_obj *fs = NULL; | |
| 1511 | |
| 1512 if (!ctx || !pdf) return NULL; | |
| 1513 if (!jfilename) jni_throw_arg(env, "filename must not be null"); | |
| 1514 | |
| 1515 filename = (*env)->GetStringUTFChars(env, jfilename, NULL); | |
| 1516 if (!filename) return NULL; | |
| 1517 | |
| 1518 if (jmimetype) | |
| 1519 { | |
| 1520 mimetype = (*env)->GetStringUTFChars(env, jmimetype, NULL); | |
| 1521 if (!mimetype) | |
| 1522 { | |
| 1523 (*env)->ReleaseStringUTFChars(env, jfilename, filename); | |
| 1524 return NULL; | |
| 1525 } | |
| 1526 } | |
| 1527 | |
| 1528 fz_try(ctx) | |
| 1529 fs = pdf_add_embedded_file(ctx, pdf, filename, mimetype, contents, created, modified, addChecksum); | |
| 1530 fz_always(ctx) | |
| 1531 { | |
| 1532 if (mimetype) | |
| 1533 (*env)->ReleaseStringUTFChars(env, jmimetype, mimetype); | |
| 1534 (*env)->ReleaseStringUTFChars(env, jfilename, filename); | |
| 1535 } | |
| 1536 fz_catch(ctx) | |
| 1537 jni_rethrow(env, ctx); | |
| 1538 | |
| 1539 return to_PDFObject_safe(ctx, env, fs); | |
| 1540 } | |
| 1541 | |
| 1542 JNIEXPORT jstring JNICALL | |
| 1543 FUN(PDFDocument_getFilespecParams)(JNIEnv *env, jobject self, jobject jfs) | |
| 1544 { | |
| 1545 fz_context *ctx = get_context(env); | |
| 1546 pdf_obj *fs = from_PDFObject_safe(env, jfs); | |
| 1547 pdf_filespec_params params; | |
| 1548 jstring jfilename = NULL; | |
| 1549 jstring jmimetype = NULL; | |
| 1550 | |
| 1551 fz_try(ctx) | |
| 1552 pdf_get_filespec_params(ctx, fs, ¶ms); | |
| 1553 fz_catch(ctx) | |
| 1554 jni_rethrow(env, ctx); | |
| 1555 | |
| 1556 jfilename = (*env)->NewStringUTF(env, params.filename); | |
| 1557 if (!jfilename || (*env)->ExceptionCheck(env)) | |
| 1558 return NULL; | |
| 1559 | |
| 1560 jmimetype = (*env)->NewStringUTF(env, params.mimetype); | |
| 1561 if (!jmimetype || (*env)->ExceptionCheck(env)) | |
| 1562 return NULL; | |
| 1563 | |
| 1564 return (*env)->NewObject(env, cls_PDFDocument_PDFEmbeddedFileParams, mid_PDFDocument_PDFEmbeddedFileParams_init, | |
| 1565 jfilename, jmimetype, params.size, params.created * 1000, params.modified * 1000); | |
| 1566 } | |
| 1567 | |
| 1568 JNIEXPORT jobject JNICALL | |
| 1569 FUN(PDFDocument_loadEmbeddedFileContents)(JNIEnv *env, jobject self, jobject jfs) | |
| 1570 { | |
| 1571 fz_context *ctx = get_context(env); | |
| 1572 pdf_obj *fs = from_PDFObject_safe(env, jfs); | |
| 1573 fz_buffer *contents = NULL; | |
| 1574 | |
| 1575 fz_try(ctx) | |
| 1576 contents = pdf_load_embedded_file_contents(ctx, fs); | |
| 1577 fz_catch(ctx) | |
| 1578 jni_rethrow(env, ctx); | |
| 1579 | |
| 1580 return to_Buffer_safe(ctx, env, contents); | |
| 1581 } | |
| 1582 | |
| 1583 JNIEXPORT jboolean JNICALL | |
| 1584 FUN(PDFDocument_verifyEmbeddedFileChecksum)(JNIEnv *env, jobject self, jobject jfs) | |
| 1585 { | |
| 1586 fz_context *ctx = get_context(env); | |
| 1587 pdf_obj *fs = from_PDFObject_safe(env, jfs); | |
| 1588 int valid = 0; | |
| 1589 | |
| 1590 fz_try(ctx) | |
| 1591 valid = pdf_verify_embedded_file_checksum(ctx, fs); | |
| 1592 fz_catch(ctx) | |
| 1593 jni_rethrow(env, ctx); | |
| 1594 | |
| 1595 return valid ? JNI_TRUE : JNI_FALSE; | |
| 1596 } | |
| 1597 | |
| 1598 JNIEXPORT jboolean JNICALL | |
| 1599 FUN(PDFDocument_isEmbeddedFile)(JNIEnv *env, jobject self, jobject jfs) | |
| 1600 { | |
| 1601 fz_context *ctx = get_context(env); | |
| 1602 pdf_obj *fs = from_PDFObject_safe(env, jfs); | |
| 1603 int embedded = 0; | |
| 1604 | |
| 1605 fz_try(ctx) | |
| 1606 embedded = pdf_is_embedded_file(ctx, fs); | |
| 1607 fz_catch(ctx) | |
| 1608 jni_rethrow(env, ctx); | |
| 1609 | |
| 1610 return embedded ? JNI_TRUE : JNI_FALSE; | |
| 1611 } | |
| 1612 | |
| 1613 JNIEXPORT void JNICALL | |
| 1614 FUN(PDFDocument_setPageLabels)(JNIEnv *env, jobject self, jint index, jint style, jstring jprefix, jint start) | |
| 1615 { | |
| 1616 fz_context *ctx = get_context(env); | |
| 1617 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 1618 const char *prefix = NULL; | |
| 1619 | |
| 1620 if (jprefix) | |
| 1621 { | |
| 1622 prefix = (*env)->GetStringUTFChars(env, jprefix, NULL); | |
| 1623 if (!prefix) return; | |
| 1624 } | |
| 1625 | |
| 1626 fz_try(ctx) | |
| 1627 { | |
| 1628 pdf_set_page_labels(ctx, pdf, index, style, prefix, start); | |
| 1629 } | |
| 1630 fz_always(ctx) | |
| 1631 { | |
| 1632 if (jprefix) | |
| 1633 (*env)->ReleaseStringUTFChars(env, jprefix, prefix); | |
| 1634 } | |
| 1635 fz_catch(ctx) | |
| 1636 { | |
| 1637 jni_rethrow_void(env, ctx); | |
| 1638 } | |
| 1639 } | |
| 1640 | |
| 1641 JNIEXPORT void JNICALL | |
| 1642 FUN(PDFDocument_deletePageLabels)(JNIEnv *env, jobject self, jint index) | |
| 1643 { | |
| 1644 fz_context *ctx = get_context(env); | |
| 1645 pdf_document *pdf = from_PDFDocument_safe(env, self); | |
| 1646 fz_try(ctx) | |
| 1647 pdf_delete_page_labels(ctx, pdf, index); | |
| 1648 fz_catch(ctx) | |
| 1649 jni_rethrow_void(env, ctx); | |
| 1650 } | |
| 1651 | |
| 1652 JNIEXPORT jint JNICALL | |
| 1653 FUN(PDFDocument_getVersion)(JNIEnv *env, jobject self) | |
| 1654 { | |
| 1655 fz_context *ctx = get_context(env); | |
| 1656 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1657 int version = 0; | |
| 1658 | |
| 1659 if (!ctx || !pdf) return 0; | |
| 1660 | |
| 1661 fz_try(ctx) | |
| 1662 version = pdf_version(ctx, pdf); | |
| 1663 fz_catch(ctx) | |
| 1664 jni_rethrow(env, ctx); | |
| 1665 | |
| 1666 return version; | |
| 1667 } | |
| 1668 | |
| 1669 JNIEXPORT jstring JNICALL | |
| 1670 FUN(PDFDocument_formatURIFromPathAndNamedDest)(JNIEnv *env, jclass cls, jstring jpath, jstring jname) | |
| 1671 { | |
| 1672 fz_context *ctx = get_context(env); | |
| 1673 char *uri = NULL; | |
| 1674 jobject juri; | |
| 1675 const char *path = NULL; | |
| 1676 const char *name = NULL; | |
| 1677 | |
| 1678 if (jpath) | |
| 1679 { | |
| 1680 path = (*env)->GetStringUTFChars(env, jpath, NULL); | |
| 1681 if (!path) return NULL; | |
| 1682 } | |
| 1683 if (jname) | |
| 1684 { | |
| 1685 name = (*env)->GetStringUTFChars(env, jname, NULL); | |
| 1686 if (!name) return NULL; | |
| 1687 } | |
| 1688 | |
| 1689 fz_try(ctx) | |
| 1690 uri = pdf_new_uri_from_path_and_named_dest(ctx, path, name); | |
| 1691 fz_always(ctx) | |
| 1692 { | |
| 1693 if (jname) | |
| 1694 (*env)->ReleaseStringUTFChars(env, jname, name); | |
| 1695 if (jpath) | |
| 1696 (*env)->ReleaseStringUTFChars(env, jpath, path); | |
| 1697 } | |
| 1698 fz_catch(ctx) | |
| 1699 jni_rethrow(env, ctx); | |
| 1700 | |
| 1701 juri = (*env)->NewStringUTF(env, uri); | |
| 1702 fz_free(ctx, uri); | |
| 1703 return juri; | |
| 1704 } | |
| 1705 | |
| 1706 JNIEXPORT jstring JNICALL | |
| 1707 FUN(PDFDocument_formatURIFromPathAndExplicitDest)(JNIEnv *env, jclass cls, jstring jpath, jobject jdest) | |
| 1708 { | |
| 1709 fz_context *ctx = get_context(env); | |
| 1710 fz_link_dest dest = from_LinkDestination(env, jdest); | |
| 1711 char *uri = NULL; | |
| 1712 jobject juri; | |
| 1713 const char *path = NULL; | |
| 1714 | |
| 1715 if (jpath) | |
| 1716 { | |
| 1717 path = (*env)->GetStringUTFChars(env, jpath, NULL); | |
| 1718 if (!path) return NULL; | |
| 1719 } | |
| 1720 | |
| 1721 fz_try(ctx) | |
| 1722 uri = pdf_new_uri_from_path_and_explicit_dest(ctx, path, dest); | |
| 1723 fz_always(ctx) | |
| 1724 { | |
| 1725 if (jpath) | |
| 1726 (*env)->ReleaseStringUTFChars(env, jpath, path); | |
| 1727 } | |
| 1728 fz_catch(ctx) | |
| 1729 jni_rethrow(env, ctx); | |
| 1730 | |
| 1731 juri = (*env)->NewStringUTF(env, uri); | |
| 1732 fz_free(ctx, uri); | |
| 1733 return juri; | |
| 1734 } | |
| 1735 | |
| 1736 JNIEXPORT jstring JNICALL | |
| 1737 FUN(PDFDocument_appendNamedDestToURI)(JNIEnv *env, jclass cls, jstring jurl, jstring jname) | |
| 1738 { | |
| 1739 fz_context *ctx = get_context(env); | |
| 1740 char *uri = NULL; | |
| 1741 jobject juri; | |
| 1742 const char *url = NULL; | |
| 1743 const char *name = NULL; | |
| 1744 | |
| 1745 if (jurl) | |
| 1746 { | |
| 1747 url = (*env)->GetStringUTFChars(env, jurl, NULL); | |
| 1748 if (!url) return NULL; | |
| 1749 } | |
| 1750 if (jname) | |
| 1751 { | |
| 1752 name = (*env)->GetStringUTFChars(env, jname, NULL); | |
| 1753 if (!name) return NULL; | |
| 1754 } | |
| 1755 | |
| 1756 fz_try(ctx) | |
| 1757 uri = pdf_append_named_dest_to_uri(ctx, url, name); | |
| 1758 fz_always(ctx) | |
| 1759 { | |
| 1760 if (jname) | |
| 1761 (*env)->ReleaseStringUTFChars(env, jname, name); | |
| 1762 if (jurl) | |
| 1763 (*env)->ReleaseStringUTFChars(env, jurl, url); | |
| 1764 } | |
| 1765 fz_catch(ctx) | |
| 1766 jni_rethrow(env, ctx); | |
| 1767 | |
| 1768 juri = (*env)->NewStringUTF(env, uri); | |
| 1769 fz_free(ctx, uri); | |
| 1770 return juri; | |
| 1771 } | |
| 1772 | |
| 1773 JNIEXPORT jstring JNICALL | |
| 1774 FUN(PDFDocument_appendExplicitDestToURI)(JNIEnv *env, jclass cls, jstring jurl, jobject jdest) | |
| 1775 { | |
| 1776 fz_context *ctx = get_context(env); | |
| 1777 fz_link_dest dest = from_LinkDestination(env, jdest); | |
| 1778 char *uri = NULL; | |
| 1779 jobject juri; | |
| 1780 const char *url = NULL; | |
| 1781 | |
| 1782 if (jurl) | |
| 1783 { | |
| 1784 url = (*env)->GetStringUTFChars(env, jurl, NULL); | |
| 1785 if (!url) return NULL; | |
| 1786 } | |
| 1787 | |
| 1788 fz_try(ctx) | |
| 1789 uri = pdf_append_explicit_dest_to_uri(ctx, url, dest); | |
| 1790 fz_always(ctx) | |
| 1791 { | |
| 1792 if (jurl) | |
| 1793 (*env)->ReleaseStringUTFChars(env, jurl, url); | |
| 1794 } | |
| 1795 fz_catch(ctx) | |
| 1796 jni_rethrow(env, ctx); | |
| 1797 | |
| 1798 juri = (*env)->NewStringUTF(env, uri); | |
| 1799 fz_free(ctx, uri); | |
| 1800 return juri; | |
| 1801 } | |
| 1802 | |
| 1803 JNIEXPORT void JNICALL | |
| 1804 FUN(PDFDocument_rearrangePages)(JNIEnv *env, jobject self, jobject jpages) | |
| 1805 { | |
| 1806 fz_context *ctx = get_context(env); | |
| 1807 pdf_document *pdf = from_PDFDocument(env, self); | |
| 1808 jsize len = 0; | |
| 1809 int *pages = NULL; | |
| 1810 | |
| 1811 if (!ctx || !pdf) return; | |
| 1812 | |
| 1813 len = (*env)->GetArrayLength(env, jpages); | |
| 1814 fz_try(ctx) | |
| 1815 pages = fz_malloc_array(ctx, len, int); | |
| 1816 fz_catch(ctx) | |
| 1817 jni_rethrow_void(env, ctx); | |
| 1818 | |
| 1819 (*env)->GetIntArrayRegion(env, jpages, 0, len, pages); | |
| 1820 if ((*env)->ExceptionCheck(env)) | |
| 1821 { | |
| 1822 fz_free(ctx, pages); | |
| 1823 return; | |
| 1824 } | |
| 1825 | |
| 1826 fz_try(ctx) | |
| 1827 pdf_rearrange_pages(ctx, pdf, len, pages, PDF_CLEAN_STRUCTURE_DROP); | |
| 1828 fz_always(ctx) | |
| 1829 fz_free(ctx, pages); | |
| 1830 fz_catch(ctx) | |
| 1831 jni_rethrow_void(env, ctx); | |
| 1832 } | |
| 1833 | |
| 1834 JNIEXPORT jint JNICALL | |
| 1835 FUN(PDFDocument_countAssociatedFiles)(JNIEnv *env, jobject self) | |
| 1836 { | |
| 1837 fz_context *ctx = get_context(env); | |
| 1838 pdf_document *doc = from_PDFDocument(env, self); | |
| 1839 int n; | |
| 1840 | |
| 1841 if (!ctx) return 0; | |
| 1842 | |
| 1843 fz_try(ctx) | |
| 1844 n = pdf_count_document_associated_files(ctx, doc); | |
| 1845 fz_catch(ctx) | |
| 1846 jni_rethrow(env, ctx); | |
| 1847 | |
| 1848 return n; | |
| 1849 } | |
| 1850 | |
| 1851 JNIEXPORT jobject JNICALL | |
| 1852 FUN(PDFDocument_associatedFile)(JNIEnv *env, jobject self, jint idx) | |
| 1853 { | |
| 1854 fz_context *ctx = get_context(env); | |
| 1855 pdf_document *doc = from_PDFDocument(env, self); | |
| 1856 pdf_obj *af; | |
| 1857 | |
| 1858 if (!ctx) return NULL; | |
| 1859 | |
| 1860 fz_try(ctx) | |
| 1861 af = pdf_document_associated_file(ctx, doc, idx); | |
| 1862 fz_catch(ctx) | |
| 1863 jni_rethrow(env, ctx); | |
| 1864 | |
| 1865 return to_PDFObject_safe_own(ctx, env, af); | |
| 1866 } | |
| 1867 | |
| 1868 JNIEXPORT jint JNICALL | |
| 1869 FUN(PDFDocument_zugferdProfile)(JNIEnv *env, jobject self) | |
| 1870 { | |
| 1871 fz_context *ctx = get_context(env); | |
| 1872 pdf_document *doc = from_PDFDocument(env, self); | |
| 1873 enum pdf_zugferd_profile p; | |
| 1874 float version; | |
| 1875 | |
| 1876 if (!ctx) return 0; | |
| 1877 | |
| 1878 fz_try(ctx) | |
| 1879 p = pdf_zugferd_profile(ctx, doc, &version); | |
| 1880 fz_catch(ctx) | |
| 1881 jni_rethrow(env, ctx); | |
| 1882 | |
| 1883 return (int)p; | |
| 1884 } | |
| 1885 | |
| 1886 JNIEXPORT jfloat JNICALL | |
| 1887 FUN(PDFDocument_zugferdVersion)(JNIEnv *env, jobject self) | |
| 1888 { | |
| 1889 fz_context *ctx = get_context(env); | |
| 1890 pdf_document *doc = from_PDFDocument(env, self); | |
| 1891 float version; | |
| 1892 | |
| 1893 if (!ctx) return 0; | |
| 1894 | |
| 1895 fz_try(ctx) | |
| 1896 (void) pdf_zugferd_profile(ctx, doc, &version); | |
| 1897 fz_catch(ctx) | |
| 1898 jni_rethrow(env, ctx); | |
| 1899 | |
| 1900 return (jfloat)version; | |
| 1901 } | |
| 1902 | |
| 1903 JNIEXPORT jobject JNICALL | |
| 1904 FUN(PDFDocument_zugferdXML)(JNIEnv *env, jobject self) | |
| 1905 { | |
| 1906 fz_context *ctx = get_context(env); | |
| 1907 pdf_document *doc = from_PDFDocument(env, self); | |
| 1908 fz_buffer *buf; | |
| 1909 | |
| 1910 if (!ctx) return NULL; | |
| 1911 | |
| 1912 fz_try(ctx) | |
| 1913 buf = pdf_zugferd_xml(ctx, doc); | |
| 1914 fz_catch(ctx) | |
| 1915 jni_rethrow(env, ctx); | |
| 1916 | |
| 1917 return to_Buffer_safe_own(ctx, env, buf); | |
| 1918 } | |
| 1919 | |
| 1920 JNIEXPORT jobject JNICALL | |
| 1921 FUN(PDFDocument_loadImage)(JNIEnv *env, jobject self, jobject jobj) | |
| 1922 { | |
| 1923 fz_context *ctx = get_context(env); | |
| 1924 pdf_document *doc = from_PDFDocument(env, self); | |
| 1925 pdf_obj *obj = from_PDFObject(env, jobj); | |
| 1926 fz_image *img; | |
| 1927 | |
| 1928 if (!ctx) return NULL; | |
| 1929 | |
| 1930 fz_try(ctx) | |
| 1931 img = pdf_load_image(ctx, doc, obj); | |
| 1932 fz_catch(ctx) | |
| 1933 jni_rethrow(env, ctx); | |
| 1934 | |
| 1935 return to_Image_safe_own(ctx, env, img); | |
| 1936 } | |
| 1937 | |
| 1938 JNIEXPORT jobject JNICALL | |
| 1939 FUN(PDFDocument_lookupDest)(JNIEnv *env, jobject self, jobject jdest) | |
| 1940 { | |
| 1941 fz_context *ctx = get_context(env); | |
| 1942 pdf_document *doc = from_PDFDocument(env, self); | |
| 1943 pdf_obj *dest = from_PDFObject(env, jdest); | |
| 1944 pdf_obj *obj = NULL; | |
| 1945 | |
| 1946 if (!ctx) return NULL; | |
| 1947 | |
| 1948 fz_try(ctx) | |
| 1949 obj = pdf_lookup_dest(ctx, doc, dest); | |
| 1950 fz_catch(ctx) | |
| 1951 jni_rethrow(env, ctx); | |
| 1952 | |
| 1953 return to_PDFObject_safe_own(ctx, env, obj); | |
| 1954 } | |
| 1955 | |
| 1956 JNIEXPORT jint JNICALL | |
| 1957 FUN(PDFDocument_countLayers)(JNIEnv *env, jobject self) | |
| 1958 { | |
| 1959 fz_context *ctx = get_context(env); | |
| 1960 pdf_document *doc = from_PDFDocument(env, self); | |
| 1961 jint layers = 0; | |
| 1962 | |
| 1963 fz_try(ctx) | |
| 1964 layers = pdf_count_layers(ctx, doc); | |
| 1965 fz_catch(ctx) | |
| 1966 jni_rethrow(env, ctx); | |
| 1967 | |
| 1968 return layers; | |
| 1969 } | |
| 1970 | |
| 1971 JNIEXPORT jboolean JNICALL | |
| 1972 FUN(PDFDocument_isLayerVisible)(JNIEnv *env, jobject self, jint layer) | |
| 1973 { | |
| 1974 fz_context *ctx = get_context(env); | |
| 1975 pdf_document *doc = from_PDFDocument(env, self); | |
| 1976 jboolean visible = JNI_FALSE; | |
| 1977 | |
| 1978 fz_try(ctx) | |
| 1979 visible = pdf_layer_is_enabled(ctx, doc, layer); | |
| 1980 fz_catch(ctx) | |
| 1981 jni_rethrow(env, ctx); | |
| 1982 | |
| 1983 return visible; | |
| 1984 } | |
| 1985 | |
| 1986 JNIEXPORT void JNICALL | |
| 1987 FUN(PDFDocument_setLayerVisible)(JNIEnv *env, jobject self, jint layer, jboolean visible) | |
| 1988 { | |
| 1989 fz_context *ctx = get_context(env); | |
| 1990 pdf_document *doc = from_PDFDocument(env, self); | |
| 1991 | |
| 1992 fz_try(ctx) | |
| 1993 pdf_enable_layer(ctx, doc, layer, visible); | |
| 1994 fz_catch(ctx) | |
| 1995 jni_rethrow_void(env, ctx); | |
| 1996 } | |
| 1997 | |
| 1998 JNIEXPORT jstring JNICALL | |
| 1999 FUN(PDFDocument_getLayerName)(JNIEnv *env, jobject self, jint layer) | |
| 2000 { | |
| 2001 fz_context *ctx = get_context(env); | |
| 2002 pdf_document *doc = from_PDFDocument(env, self); | |
| 2003 const char *name = NULL; | |
| 2004 | |
| 2005 fz_try(ctx) | |
| 2006 name = pdf_layer_name(ctx, doc, layer); | |
| 2007 fz_catch(ctx) | |
| 2008 jni_rethrow(env, ctx); | |
| 2009 | |
| 2010 return (*env)->NewStringUTF(env, name); | |
| 2011 } | |
| 2012 | |
| 2013 JNIEXPORT void JNICALL | |
| 2014 FUN(PDFDocument_subsetFonts)(JNIEnv *env, jobject self) | |
| 2015 { | |
| 2016 fz_context *ctx = get_context(env); | |
| 2017 pdf_document *doc = from_PDFDocument(env, self); | |
| 2018 | |
| 2019 fz_try(ctx) | |
| 2020 pdf_subset_fonts(ctx, doc, 0, NULL); | |
| 2021 fz_catch(ctx) | |
| 2022 jni_rethrow_void(env, ctx); | |
| 2023 } | |
| 2024 | |
| 2025 JNIEXPORT void JNICALL | |
| 2026 FUN(PDFDocument_bake)(JNIEnv *env, jobject self, jboolean bake_annots, jboolean bake_widgets) | |
| 2027 { | |
| 2028 fz_context *ctx = get_context(env); | |
| 2029 pdf_document *doc = from_PDFDocument(env, self); | |
| 2030 | |
| 2031 fz_try(ctx) | |
| 2032 pdf_bake_document(ctx, doc, bake_annots, bake_widgets); | |
| 2033 fz_catch(ctx) | |
| 2034 jni_rethrow_void(env, ctx); | |
| 2035 } |
