Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/platform/java/jni/wrap.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 /* Conversion functions: C to Java. These all throw fitz exceptions. */ | |
| 24 | |
| 25 static inline jobject to_ColorSpace(fz_context *ctx, JNIEnv *env, fz_colorspace *cs) | |
| 26 { | |
| 27 jobject jcs; | |
| 28 | |
| 29 if (!ctx || !cs) return NULL; | |
| 30 | |
| 31 fz_keep_colorspace(ctx, cs); | |
| 32 jcs = (*env)->CallStaticObjectMethod(env, cls_ColorSpace, mid_ColorSpace_fromPointer, jlong_cast(cs)); | |
| 33 if (!jcs) | |
| 34 fz_drop_colorspace(ctx, cs); | |
| 35 if ((*env)->ExceptionCheck(env)) | |
| 36 fz_throw_java(ctx, env); | |
| 37 | |
| 38 return jcs; | |
| 39 } | |
| 40 | |
| 41 static inline jobject to_DefaultColorSpaces(fz_context *ctx, JNIEnv *env, fz_default_colorspaces *dcs) | |
| 42 { | |
| 43 jobject jdcs; | |
| 44 | |
| 45 if (!ctx || !dcs) return NULL; | |
| 46 | |
| 47 fz_keep_default_colorspaces(ctx, dcs); | |
| 48 jdcs = (*env)->NewObject(env, cls_DefaultColorSpaces, mid_DefaultColorSpaces_init, jlong_cast(dcs)); | |
| 49 if (!jdcs) | |
| 50 fz_drop_default_colorspaces(ctx, dcs); | |
| 51 if ((*env)->ExceptionCheck(env)) | |
| 52 fz_throw_java(ctx, env); | |
| 53 | |
| 54 return jdcs; | |
| 55 } | |
| 56 | |
| 57 static inline jobject to_FitzInputStream(fz_context *ctx, JNIEnv *env, fz_stream *stm) | |
| 58 { | |
| 59 jobject jstm; | |
| 60 | |
| 61 if (!ctx || !stm) return NULL; | |
| 62 | |
| 63 fz_keep_stream(ctx, stm); | |
| 64 jstm = (*env)->NewObject(env, cls_FitzInputStream, mid_FitzInputStream_init, jlong_cast(stm)); | |
| 65 if (!jstm) | |
| 66 fz_drop_stream(ctx, stm); | |
| 67 if ((*env)->ExceptionCheck(env)) | |
| 68 fz_throw_java(ctx, env); | |
| 69 | |
| 70 return jstm; | |
| 71 } | |
| 72 | |
| 73 static inline jobject to_Image(fz_context *ctx, JNIEnv *env, fz_image *img) | |
| 74 { | |
| 75 jobject jimg; | |
| 76 | |
| 77 if (!ctx || !img) return NULL; | |
| 78 | |
| 79 fz_keep_image(ctx, img); | |
| 80 jimg = (*env)->NewObject(env, cls_Image, mid_Image_init, jlong_cast(img)); | |
| 81 if (!jimg) | |
| 82 fz_drop_image(ctx, img); | |
| 83 if ((*env)->ExceptionCheck(env)) | |
| 84 fz_throw_java(ctx, env); | |
| 85 | |
| 86 return jimg; | |
| 87 } | |
| 88 | |
| 89 static inline jobject to_Matrix(fz_context *ctx, JNIEnv *env, fz_matrix mat) | |
| 90 { | |
| 91 jobject jctm; | |
| 92 | |
| 93 if (!ctx) return NULL; | |
| 94 | |
| 95 jctm = (*env)->NewObject(env, cls_Matrix, mid_Matrix_init, mat.a, mat.b, mat.c, mat.d, mat.e, mat.f); | |
| 96 if ((*env)->ExceptionCheck(env)) | |
| 97 fz_throw_java(ctx, env); | |
| 98 | |
| 99 return jctm; | |
| 100 } | |
| 101 | |
| 102 static inline jobject to_Path(fz_context *ctx, JNIEnv *env, const fz_path *path) | |
| 103 { | |
| 104 jobject jpath; | |
| 105 | |
| 106 if (!ctx || !path) return NULL; | |
| 107 | |
| 108 fz_keep_path(ctx, path); | |
| 109 jpath = (*env)->NewObject(env, cls_Path, mid_Path_init, jlong_cast(path)); | |
| 110 if (!jpath) | |
| 111 fz_drop_path(ctx, path); | |
| 112 if ((*env)->ExceptionCheck(env)) | |
| 113 fz_throw_java(ctx, env); | |
| 114 | |
| 115 return jpath; | |
| 116 } | |
| 117 | |
| 118 static inline jobject to_Rect(fz_context *ctx, JNIEnv *env, fz_rect rect) | |
| 119 { | |
| 120 jobject jrect; | |
| 121 | |
| 122 if (!ctx) return NULL; | |
| 123 | |
| 124 jrect = (*env)->NewObject(env, cls_Rect, mid_Rect_init, rect.x0, rect.y0, rect.x1, rect.y1); | |
| 125 if ((*env)->ExceptionCheck(env)) | |
| 126 fz_throw_java(ctx, env); | |
| 127 | |
| 128 return jrect; | |
| 129 } | |
| 130 | |
| 131 static inline jobject to_Shade(fz_context *ctx, JNIEnv *env, fz_shade *shd) | |
| 132 { | |
| 133 jobject jshd; | |
| 134 | |
| 135 if (!ctx || !shd) return NULL; | |
| 136 | |
| 137 fz_keep_shade(ctx, shd); | |
| 138 jshd = (*env)->NewObject(env, cls_Shade, mid_Shade_init, jlong_cast(shd)); | |
| 139 if (!jshd) | |
| 140 fz_drop_shade(ctx, shd); | |
| 141 if ((*env)->ExceptionCheck(env)) | |
| 142 fz_throw_java(ctx, env); | |
| 143 | |
| 144 return jshd; | |
| 145 } | |
| 146 | |
| 147 static inline jobject to_StrokeState(fz_context *ctx, JNIEnv *env, const fz_stroke_state *state) | |
| 148 { | |
| 149 jobject jstate; | |
| 150 | |
| 151 if (!ctx || !state) return NULL; | |
| 152 | |
| 153 fz_keep_stroke_state(ctx, state); | |
| 154 jstate = (*env)->NewObject(env, cls_StrokeState, mid_StrokeState_init, jlong_cast(state)); | |
| 155 if (!jstate) | |
| 156 fz_drop_stroke_state(ctx, state); | |
| 157 if ((*env)->ExceptionCheck(env)) | |
| 158 fz_throw_java(ctx, env); | |
| 159 | |
| 160 return jstate; | |
| 161 } | |
| 162 | |
| 163 static inline jobject to_Text(fz_context *ctx, JNIEnv *env, const fz_text *text) | |
| 164 { | |
| 165 jobject jtext; | |
| 166 | |
| 167 if (!ctx) return NULL; | |
| 168 | |
| 169 fz_keep_text(ctx, text); | |
| 170 jtext = (*env)->NewObject(env, cls_Text, mid_Text_init, jlong_cast(text)); | |
| 171 if (!jtext) | |
| 172 fz_drop_text(ctx, text); | |
| 173 if ((*env)->ExceptionCheck(env)) | |
| 174 fz_throw_java(ctx, env); | |
| 175 | |
| 176 return jtext; | |
| 177 } | |
| 178 | |
| 179 static inline jbyteArray to_byteArray(fz_context *ctx, JNIEnv *env, const unsigned char *arr, jint n) | |
| 180 { | |
| 181 jbyteArray jarr; | |
| 182 | |
| 183 if (!ctx) return NULL; | |
| 184 | |
| 185 jarr = (*env)->NewByteArray(env, n); | |
| 186 if ((*env)->ExceptionCheck(env)) | |
| 187 fz_throw_java(ctx, env); | |
| 188 if (!jarr) | |
| 189 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot allocate byte array"); | |
| 190 | |
| 191 (*env)->SetByteArrayRegion(env, jarr, 0, n, (jbyte *) arr); | |
| 192 if ((*env)->ExceptionCheck(env)) | |
| 193 fz_throw_java(ctx, env); | |
| 194 | |
| 195 return jarr; | |
| 196 } | |
| 197 | |
| 198 static inline jfloatArray to_floatArray(fz_context *ctx, JNIEnv *env, const float *arr, jint n) | |
| 199 { | |
| 200 jfloatArray jarr; | |
| 201 | |
| 202 if (!ctx) return NULL; | |
| 203 | |
| 204 jarr = (*env)->NewFloatArray(env, n); | |
| 205 if ((*env)->ExceptionCheck(env)) | |
| 206 fz_throw_java(ctx, env); | |
| 207 if (!jarr) | |
| 208 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot allocate float array"); | |
| 209 | |
| 210 (*env)->SetFloatArrayRegion(env, jarr, 0, n, arr); | |
| 211 if ((*env)->ExceptionCheck(env)) | |
| 212 fz_throw_java(ctx, env); | |
| 213 | |
| 214 return jarr; | |
| 215 } | |
| 216 | |
| 217 static inline jintArray to_intArray(fz_context *ctx, JNIEnv *env, const int *arr, jint n) | |
| 218 { | |
| 219 jintArray jarr; | |
| 220 | |
| 221 if (!ctx) return NULL; | |
| 222 | |
| 223 jarr = (*env)->NewIntArray(env, n); | |
| 224 if ((*env)->ExceptionCheck(env)) | |
| 225 fz_throw_java(ctx, env); | |
| 226 if (!jarr) | |
| 227 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot allocate int array"); | |
| 228 | |
| 229 (*env)->SetIntArrayRegion(env, jarr, 0, n, (jint *) arr); | |
| 230 if ((*env)->ExceptionCheck(env)) | |
| 231 fz_throw_java(ctx, env); | |
| 232 | |
| 233 return jarr; | |
| 234 } | |
| 235 | |
| 236 /* Conversion functions: C to Java. None of these throw fitz exceptions. */ | |
| 237 | |
| 238 static inline jobject to_Buffer_safe(fz_context *ctx, JNIEnv *env, fz_buffer *buf) | |
| 239 { | |
| 240 jobject jbuf; | |
| 241 | |
| 242 if (!ctx || !buf) return NULL; | |
| 243 | |
| 244 fz_keep_buffer(ctx, buf); | |
| 245 jbuf = (*env)->NewObject(env, cls_Buffer, mid_Buffer_init, jlong_cast(buf)); | |
| 246 if (!jbuf) | |
| 247 fz_drop_buffer(ctx, buf); | |
| 248 | |
| 249 return jbuf; | |
| 250 } | |
| 251 | |
| 252 static inline jint to_ColorParams_safe(fz_context *ctx, JNIEnv *env, fz_color_params cp) | |
| 253 { | |
| 254 if (!ctx) return 0; | |
| 255 return (((int) (!!cp.bp)<<5) | ((int) (!!cp.op)<<6) | ((int) (!!cp.opm)<<7) | (cp.ri & 31)); | |
| 256 } | |
| 257 | |
| 258 static inline jobject to_ColorSpace_safe(fz_context *ctx, JNIEnv *env, fz_colorspace *cs) | |
| 259 { | |
| 260 jobject jcs; | |
| 261 | |
| 262 if (!ctx || !cs) return NULL; | |
| 263 | |
| 264 fz_keep_colorspace(ctx, cs); | |
| 265 jcs = (*env)->CallStaticObjectMethod(env, cls_ColorSpace, mid_ColorSpace_fromPointer, jlong_cast(cs)); | |
| 266 if (!jcs) fz_drop_colorspace(ctx, cs); | |
| 267 if ((*env)->ExceptionCheck(env)) return NULL; | |
| 268 | |
| 269 return jcs; | |
| 270 } | |
| 271 | |
| 272 static inline jobject to_Font_safe(fz_context *ctx, JNIEnv *env, fz_font *font) | |
| 273 { | |
| 274 jobject jfont; | |
| 275 | |
| 276 if (!ctx || !font) return NULL; | |
| 277 | |
| 278 fz_keep_font(ctx, font); | |
| 279 jfont = (*env)->NewObject(env, cls_Font, mid_Font_init, jlong_cast(font)); | |
| 280 if (!jfont) | |
| 281 fz_drop_font(ctx, font); | |
| 282 | |
| 283 return jfont; | |
| 284 } | |
| 285 | |
| 286 static inline jobject to_Image_safe(fz_context *ctx, JNIEnv *env, fz_image *img) | |
| 287 { | |
| 288 jobject jimg; | |
| 289 | |
| 290 if (!ctx || !img) return NULL; | |
| 291 | |
| 292 fz_keep_image(ctx, img); | |
| 293 jimg = (*env)->NewObject(env, cls_Image, mid_Image_init, jlong_cast(img)); | |
| 294 if (!jimg) | |
| 295 fz_drop_image(ctx, img); | |
| 296 | |
| 297 return jimg; | |
| 298 } | |
| 299 | |
| 300 static inline jobject to_Link_safe(fz_context *ctx, JNIEnv *env, fz_link *link) | |
| 301 { | |
| 302 jobject jlink; | |
| 303 | |
| 304 if (!ctx || !link) return NULL; | |
| 305 | |
| 306 fz_keep_link(ctx, link); | |
| 307 jlink = (*env)->NewObject(env, cls_Link, mid_Link_init, jlong_cast(link)); | |
| 308 if (!jlink) | |
| 309 fz_drop_link(ctx, link); | |
| 310 | |
| 311 return jlink; | |
| 312 } | |
| 313 | |
| 314 static inline jobject to_Matrix_safe(fz_context *ctx, JNIEnv *env, fz_matrix mat) | |
| 315 { | |
| 316 if (!ctx) return NULL; | |
| 317 | |
| 318 return (*env)->NewObject(env, cls_Matrix, mid_Matrix_init, mat.a, mat.b, mat.c, mat.d, mat.e, mat.f); | |
| 319 } | |
| 320 | |
| 321 static inline jobject to_Outline_safe(fz_context *ctx, JNIEnv *env, fz_document *doc, fz_outline *outline) | |
| 322 { | |
| 323 jobject joutline = NULL; | |
| 324 jobject jarr = NULL; | |
| 325 jsize jindex = 0; | |
| 326 jsize count = 0; | |
| 327 fz_outline *counter = outline; | |
| 328 | |
| 329 if (!ctx || !outline) return NULL; | |
| 330 | |
| 331 while (counter) | |
| 332 { | |
| 333 count++; | |
| 334 counter = counter->next; | |
| 335 } | |
| 336 | |
| 337 jarr = (*env)->NewObjectArray(env, count, cls_Outline, NULL); | |
| 338 if (!jarr || (*env)->ExceptionCheck(env)) return NULL; | |
| 339 | |
| 340 while (outline) | |
| 341 { | |
| 342 jstring jtitle = NULL; | |
| 343 jstring juri = NULL; | |
| 344 jobject jdown = NULL; | |
| 345 | |
| 346 if (outline->title) | |
| 347 { | |
| 348 jtitle = (*env)->NewStringUTF(env, outline->title); | |
| 349 if (!jtitle || (*env)->ExceptionCheck(env)) return NULL; | |
| 350 } | |
| 351 | |
| 352 if (outline->uri) | |
| 353 { | |
| 354 juri = (*env)->NewStringUTF(env, outline->uri); | |
| 355 if (!juri || (*env)->ExceptionCheck(env)) return NULL; | |
| 356 } | |
| 357 | |
| 358 if (outline->down) | |
| 359 { | |
| 360 jdown = to_Outline_safe(ctx, env, doc, outline->down); | |
| 361 if (!jdown) return NULL; | |
| 362 } | |
| 363 | |
| 364 joutline = (*env)->NewObject(env, cls_Outline, mid_Outline_init, jtitle, juri, jdown); | |
| 365 if (!joutline) return NULL; | |
| 366 | |
| 367 if (jdown) | |
| 368 (*env)->DeleteLocalRef(env, jdown); | |
| 369 if (juri) | |
| 370 (*env)->DeleteLocalRef(env, juri); | |
| 371 if (jtitle) | |
| 372 (*env)->DeleteLocalRef(env, jtitle); | |
| 373 | |
| 374 (*env)->SetObjectArrayElement(env, jarr, jindex++, joutline); | |
| 375 if ((*env)->ExceptionCheck(env)) return NULL; | |
| 376 | |
| 377 (*env)->DeleteLocalRef(env, joutline); | |
| 378 outline = outline->next; | |
| 379 } | |
| 380 | |
| 381 return jarr; | |
| 382 } | |
| 383 | |
| 384 static inline jobject to_OutlineIterator_safe(fz_context *ctx, JNIEnv *env, fz_outline_iterator *iterator) | |
| 385 { | |
| 386 jobject jiterator = NULL; | |
| 387 | |
| 388 if (!ctx || !iterator) return NULL; | |
| 389 | |
| 390 jiterator = (*env)->NewObject(env, cls_OutlineIterator, mid_OutlineIterator_init, (jlong)iterator); | |
| 391 if (!jiterator || (*env)->ExceptionCheck(env)) return NULL; | |
| 392 | |
| 393 return jiterator; | |
| 394 } | |
| 395 | |
| 396 static inline jobject to_PDFAnnotation_safe(fz_context *ctx, JNIEnv *env, pdf_annot *annot) | |
| 397 { | |
| 398 jobject jannot; | |
| 399 | |
| 400 if (!ctx || !annot) return NULL; | |
| 401 | |
| 402 pdf_keep_annot(ctx, annot); | |
| 403 jannot = (*env)->NewObject(env, cls_PDFAnnotation, mid_PDFAnnotation_init, jlong_cast(annot)); | |
| 404 if (!jannot) | |
| 405 pdf_drop_annot(ctx, annot); | |
| 406 | |
| 407 return jannot; | |
| 408 } | |
| 409 | |
| 410 static inline jobject to_PDFDocument_safe(fz_context *ctx, JNIEnv *env, pdf_document *pdf) | |
| 411 { | |
| 412 jobject jpdf; | |
| 413 | |
| 414 if (!ctx || !pdf) return NULL; | |
| 415 | |
| 416 pdf_keep_document(ctx, pdf); | |
| 417 jpdf = (*env)->NewObject(env, cls_PDFDocument, mid_PDFDocument_init, jlong_cast(pdf)); | |
| 418 if (!jpdf) | |
| 419 pdf_drop_document(ctx, pdf); | |
| 420 | |
| 421 return jpdf; | |
| 422 } | |
| 423 | |
| 424 static inline jobject to_PDFObject_safe(fz_context *ctx, JNIEnv *env, pdf_obj *obj) | |
| 425 { | |
| 426 jobject jobj; | |
| 427 | |
| 428 if (!ctx) return NULL; | |
| 429 | |
| 430 if (obj == NULL) | |
| 431 return (*env)->GetStaticObjectField(env, cls_PDFObject, fid_PDFObject_Null); | |
| 432 | |
| 433 pdf_keep_obj(ctx, obj); | |
| 434 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 435 if (!jobj) | |
| 436 pdf_drop_obj(ctx, obj); | |
| 437 | |
| 438 return jobj; | |
| 439 } | |
| 440 | |
| 441 static inline jobject to_Point_safe(fz_context *ctx, JNIEnv *env, fz_point point) | |
| 442 { | |
| 443 if (!ctx) return NULL; | |
| 444 | |
| 445 return (*env)->NewObject(env, cls_Point, mid_Point_init, point.x, point.y); | |
| 446 } | |
| 447 | |
| 448 static inline jobject to_Quad_safe(fz_context *ctx, JNIEnv *env, fz_quad quad) | |
| 449 { | |
| 450 if (!ctx) return NULL; | |
| 451 | |
| 452 return (*env)->NewObject(env, cls_Quad, mid_Quad_init, | |
| 453 quad.ul.x, quad.ul.y, | |
| 454 quad.ur.x, quad.ur.y, | |
| 455 quad.ll.x, quad.ll.y, | |
| 456 quad.lr.x, quad.lr.y); | |
| 457 } | |
| 458 | |
| 459 static inline jobjectArray to_QuadArray_safe(fz_context *ctx, JNIEnv *env, const fz_quad *quads, jint n) | |
| 460 { | |
| 461 jobjectArray arr; | |
| 462 int i; | |
| 463 | |
| 464 if (!ctx || !quads) return NULL; | |
| 465 | |
| 466 arr = (*env)->NewObjectArray(env, n, cls_Quad, NULL); | |
| 467 if (!arr || (*env)->ExceptionCheck(env)) return NULL; | |
| 468 | |
| 469 for (i = 0; i < n; i++) | |
| 470 { | |
| 471 jobject jquad = to_Quad_safe(ctx, env, quads[i]); | |
| 472 if (!jquad) return NULL; | |
| 473 | |
| 474 (*env)->SetObjectArrayElement(env, arr, i, jquad); | |
| 475 if ((*env)->ExceptionCheck(env)) return NULL; | |
| 476 | |
| 477 (*env)->DeleteLocalRef(env, jquad); | |
| 478 } | |
| 479 | |
| 480 return arr; | |
| 481 } | |
| 482 | |
| 483 static inline jobject to_DOM_safe(fz_context *ctx, JNIEnv *env, fz_xml *xml) | |
| 484 { | |
| 485 jobject jxml; | |
| 486 | |
| 487 if (!ctx || !xml) return NULL; | |
| 488 | |
| 489 fz_keep_xml(ctx, xml); | |
| 490 jxml = (*env)->NewObject(env, cls_DOM, mid_DOM_init, jlong_cast(xml)); | |
| 491 if (!jxml) fz_drop_xml(ctx, xml); | |
| 492 if ((*env)->ExceptionCheck(env)) return NULL; | |
| 493 | |
| 494 return jxml; | |
| 495 } | |
| 496 | |
| 497 static inline jobject to_Shade_safe(fz_context *ctx, JNIEnv *env, fz_shade *sh) | |
| 498 { | |
| 499 jobject jsh; | |
| 500 | |
| 501 if (!ctx || !sh) return NULL; | |
| 502 | |
| 503 fz_keep_shade(ctx, sh); | |
| 504 jsh = (*env)->NewObject(env, cls_Shade, mid_Shade_init, jlong_cast(sh)); | |
| 505 if (!jsh) fz_drop_shade(ctx, sh); | |
| 506 if ((*env)->ExceptionCheck(env)) return NULL; | |
| 507 | |
| 508 return jsh; | |
| 509 } | |
| 510 | |
| 511 static inline jobject to_String_safe(fz_context *ctx, JNIEnv *env, const char *val) | |
| 512 { | |
| 513 jstring jval; | |
| 514 if (!ctx) return NULL; | |
| 515 | |
| 516 jval = (*env)->NewStringUTF(env, val); | |
| 517 if (!jval || (*env)->ExceptionCheck(env)) | |
| 518 fz_throw_java(ctx, env); | |
| 519 | |
| 520 return jval; | |
| 521 } | |
| 522 | |
| 523 static inline jobject to_Rect_safe(fz_context *ctx, JNIEnv *env, fz_rect rect) | |
| 524 { | |
| 525 if (!ctx) return NULL; | |
| 526 | |
| 527 return (*env)->NewObject(env, cls_Rect, mid_Rect_init, rect.x0, rect.y0, rect.x1, rect.y1); | |
| 528 } | |
| 529 | |
| 530 static inline jobjectArray to_StringArray_safe(fz_context *ctx, JNIEnv *env, const char **strings, int n) | |
| 531 { | |
| 532 jobjectArray arr; | |
| 533 int i; | |
| 534 | |
| 535 if (!ctx || !strings) return NULL; | |
| 536 | |
| 537 arr = (*env)->NewObjectArray(env, n, cls_String, NULL); | |
| 538 if (!arr || (*env)->ExceptionCheck(env)) return NULL; | |
| 539 | |
| 540 for (i = 0; i < n; i++) | |
| 541 { | |
| 542 jstring jstring; | |
| 543 | |
| 544 jstring = (*env)->NewStringUTF(env, strings[i]); | |
| 545 if (!jstring || (*env)->ExceptionCheck(env)) return NULL; | |
| 546 | |
| 547 (*env)->SetObjectArrayElement(env, arr, i, jstring); | |
| 548 if ((*env)->ExceptionCheck(env)) return NULL; | |
| 549 | |
| 550 (*env)->DeleteLocalRef(env, jstring); | |
| 551 } | |
| 552 | |
| 553 return arr; | |
| 554 } | |
| 555 | |
| 556 static inline jobject to_PDFWidget_safe(fz_context *ctx, JNIEnv *env, pdf_annot *widget) | |
| 557 { | |
| 558 jobject jwidget; | |
| 559 int nopts; | |
| 560 char **opts = NULL; | |
| 561 jobjectArray jopts = NULL; | |
| 562 | |
| 563 fz_var(opts); | |
| 564 | |
| 565 pdf_keep_annot(ctx, widget); | |
| 566 jwidget = (*env)->NewObject(env, cls_PDFWidget, mid_PDFWidget_init, jlong_cast(widget)); | |
| 567 if (!jwidget || (*env)->ExceptionCheck(env)) | |
| 568 { | |
| 569 pdf_drop_annot(ctx, widget); | |
| 570 jni_throw_null(env, "cannot wrap PDF widget in java object"); | |
| 571 } | |
| 572 | |
| 573 fz_try(ctx) | |
| 574 { | |
| 575 int fieldType = pdf_widget_type(ctx, widget); | |
| 576 int fieldFlags = pdf_field_flags(ctx, pdf_annot_obj(ctx, widget)); | |
| 577 (*env)->SetIntField(env, jwidget, fid_PDFWidget_fieldType, fieldType); | |
| 578 (*env)->SetIntField(env, jwidget, fid_PDFWidget_fieldFlags, fieldFlags); | |
| 579 if (fieldType == PDF_WIDGET_TYPE_TEXT) | |
| 580 { | |
| 581 (*env)->SetIntField(env, jwidget, fid_PDFWidget_maxLen, pdf_text_widget_max_len(ctx, widget)); | |
| 582 (*env)->SetIntField(env, jwidget, fid_PDFWidget_textFormat, pdf_text_widget_format(ctx, widget)); | |
| 583 } | |
| 584 if (fieldType == PDF_WIDGET_TYPE_COMBOBOX || fieldType == PDF_WIDGET_TYPE_LISTBOX) | |
| 585 { | |
| 586 nopts = pdf_choice_widget_options(ctx, widget, 0, NULL); | |
| 587 if (nopts > 0) | |
| 588 { | |
| 589 opts = Memento_label(fz_malloc(ctx, nopts * sizeof(*opts)), "to_PDFWidget"); | |
| 590 pdf_choice_widget_options(ctx, widget, 0, (const char **)opts); | |
| 591 jopts = to_StringArray_safe(ctx, env, (const char **)opts, nopts); | |
| 592 if (!jopts || (*env)->ExceptionCheck(env)) | |
| 593 fz_throw_java(ctx, env); | |
| 594 } | |
| 595 } | |
| 596 } | |
| 597 fz_always(ctx) | |
| 598 { | |
| 599 fz_free(ctx, opts); | |
| 600 } | |
| 601 fz_catch(ctx) | |
| 602 { | |
| 603 jni_rethrow(env, ctx); | |
| 604 } | |
| 605 | |
| 606 (*env)->SetObjectField(env, jwidget, fid_PDFWidget_options, jopts); | |
| 607 | |
| 608 return jwidget; | |
| 609 } | |
| 610 | |
| 611 static inline jobject to_VectorInfo_safe(fz_context *ctx, JNIEnv *env, int flags) | |
| 612 { | |
| 613 jobject jvecinfo; | |
| 614 if (!ctx) return NULL; | |
| 615 jvecinfo = (*env)->NewObject(env, cls_StructuredTextWalker_VectorInfo, mid_StructuredTextWalker_VectorInfo_init); | |
| 616 (*env)->SetBooleanField(env, jvecinfo, fid_StructuredTextWalker_VectorInfo_isStroked, flags & FZ_STEXT_VECTOR_IS_STROKED); | |
| 617 (*env)->SetBooleanField(env, jvecinfo, fid_StructuredTextWalker_VectorInfo_isRectangle, flags & FZ_STEXT_VECTOR_IS_RECTANGLE); | |
| 618 return jvecinfo; | |
| 619 } | |
| 620 | |
| 621 /* Conversion functions: C to Java. Take ownership of fitz object. None of these throw fitz exceptions. */ | |
| 622 | |
| 623 static inline jobject to_Buffer_safe_own(fz_context *ctx, JNIEnv *env, fz_buffer *buf) | |
| 624 { | |
| 625 jobject jobj; | |
| 626 | |
| 627 if (!ctx || !buf) return NULL; | |
| 628 | |
| 629 jobj = (*env)->NewObject(env, cls_Buffer, mid_Buffer_init, jlong_cast(buf)); | |
| 630 if (!jobj) | |
| 631 fz_drop_buffer(ctx, buf); | |
| 632 | |
| 633 return jobj; | |
| 634 } | |
| 635 | |
| 636 static inline jobject to_ColorSpace_safe_own(fz_context *ctx, JNIEnv *env, fz_colorspace *cs) | |
| 637 { | |
| 638 jobject jobj; | |
| 639 | |
| 640 if (!ctx || !cs) return NULL; | |
| 641 | |
| 642 jobj = (*env)->NewObject(env, cls_ColorSpace, mid_ColorSpace_init, jlong_cast(cs)); | |
| 643 if (!jobj) | |
| 644 fz_drop_colorspace(ctx, cs); | |
| 645 | |
| 646 return jobj; | |
| 647 } | |
| 648 | |
| 649 static inline jobject to_Document_safe_own(fz_context *ctx, JNIEnv *env, fz_document *doc) | |
| 650 { | |
| 651 jobject obj; | |
| 652 pdf_document *pdf; | |
| 653 | |
| 654 if (!ctx || !doc) return NULL; | |
| 655 | |
| 656 pdf = pdf_document_from_fz_document(ctx, doc); | |
| 657 if (pdf) | |
| 658 /* This relies on the fact that pdf == doc! */ | |
| 659 obj = (*env)->NewObject(env, cls_PDFDocument, mid_PDFDocument_init, jlong_cast(pdf)); | |
| 660 else | |
| 661 obj = (*env)->NewObject(env, cls_Document, mid_Document_init, jlong_cast(doc)); | |
| 662 if (!obj) | |
| 663 fz_drop_document(ctx, doc); | |
| 664 | |
| 665 return obj; | |
| 666 } | |
| 667 | |
| 668 static inline jobject to_DisplayList_safe_own(fz_context *ctx, JNIEnv *env, fz_display_list *list) | |
| 669 { | |
| 670 jobject jlist; | |
| 671 | |
| 672 if (!ctx || !list) return NULL; | |
| 673 | |
| 674 jlist = (*env)->NewObject(env, cls_DisplayList, mid_DisplayList_init, jlong_cast(list)); | |
| 675 if (!jlist) | |
| 676 fz_drop_display_list(ctx, list); | |
| 677 | |
| 678 return jlist; | |
| 679 } | |
| 680 | |
| 681 static inline jobject to_Image_safe_own(fz_context *ctx, JNIEnv *env, fz_image *img) | |
| 682 { | |
| 683 jobject jimg; | |
| 684 | |
| 685 if (!ctx || !img) return NULL; | |
| 686 | |
| 687 jimg = (*env)->NewObject(env, cls_Image, mid_Image_init, jlong_cast(img)); | |
| 688 if (!jimg) | |
| 689 fz_drop_image(ctx, img); | |
| 690 | |
| 691 return jimg; | |
| 692 } | |
| 693 | |
| 694 static inline jobject to_NativeDevice_safe_own(fz_context *ctx, JNIEnv *env, fz_device *device) | |
| 695 { | |
| 696 jobject jdev; | |
| 697 | |
| 698 if (!ctx || !device) return NULL; | |
| 699 | |
| 700 jdev = (*env)->NewObject(env, cls_NativeDevice, mid_NativeDevice_init, jlong_cast(device)); | |
| 701 if (!jdev) | |
| 702 fz_drop_device(ctx, device); | |
| 703 | |
| 704 return jdev; | |
| 705 } | |
| 706 | |
| 707 static inline jobject to_Page_safe_own(fz_context *ctx, JNIEnv *env, fz_page *page) | |
| 708 { | |
| 709 jobject jobj; | |
| 710 pdf_page *ppage; | |
| 711 | |
| 712 if (!ctx || !page) return NULL; | |
| 713 | |
| 714 ppage = pdf_page_from_fz_page(ctx, page); | |
| 715 if (ppage) | |
| 716 jobj = (*env)->NewObject(env, cls_PDFPage, mid_PDFPage_init, jlong_cast(page)); | |
| 717 else | |
| 718 jobj = (*env)->NewObject(env, cls_Page, mid_Page_init, jlong_cast(page)); | |
| 719 if (!jobj) | |
| 720 fz_drop_page(ctx, page); | |
| 721 | |
| 722 return jobj; | |
| 723 } | |
| 724 | |
| 725 static inline jobject to_PDFDocument_safe_own(fz_context *ctx, JNIEnv *env, pdf_document *pdf) | |
| 726 { | |
| 727 jobject obj; | |
| 728 | |
| 729 if (!ctx || !pdf) return NULL; | |
| 730 | |
| 731 obj = (*env)->NewObject(env, cls_PDFDocument, mid_PDFDocument_init, jlong_cast(pdf)); | |
| 732 if (!obj) | |
| 733 fz_drop_document(ctx, &pdf->super); | |
| 734 | |
| 735 return obj; | |
| 736 } | |
| 737 | |
| 738 static inline jobject to_Link_safe_own(fz_context *ctx, JNIEnv *env, fz_link *link) | |
| 739 { | |
| 740 jobject jobj; | |
| 741 | |
| 742 if (!ctx || !link) return NULL; | |
| 743 | |
| 744 jobj = (*env)->NewObject(env, cls_Link, mid_Link_init, jlong_cast(link)); | |
| 745 if (!jobj) | |
| 746 fz_drop_link(ctx, link); | |
| 747 | |
| 748 return jobj; | |
| 749 } | |
| 750 | |
| 751 static inline jobject to_PDFAnnotation_safe_own(fz_context *ctx, JNIEnv *env, pdf_annot *annot) | |
| 752 { | |
| 753 jobject jannot; | |
| 754 | |
| 755 if (!ctx || !annot) return NULL; | |
| 756 | |
| 757 jannot = (*env)->NewObject(env, cls_PDFAnnotation, mid_PDFAnnotation_init, jlong_cast(annot)); | |
| 758 if (!jannot) | |
| 759 pdf_drop_annot(ctx, annot); | |
| 760 | |
| 761 return jannot; | |
| 762 } | |
| 763 | |
| 764 static inline jobject to_PDFWidget_safe_own(fz_context *ctx, JNIEnv *env, pdf_annot *widget) | |
| 765 { | |
| 766 jobject jwidget; | |
| 767 | |
| 768 if (!ctx || !widget) return NULL; | |
| 769 | |
| 770 jwidget = (*env)->NewObject(env, cls_PDFWidget, mid_PDFWidget_init, jlong_cast(widget)); | |
| 771 if (!jwidget) | |
| 772 pdf_drop_annot(ctx, widget); | |
| 773 | |
| 774 return jwidget; | |
| 775 } | |
| 776 | |
| 777 static inline jobject to_PDFGraftMap_safe_own(fz_context *ctx, JNIEnv *env, jobject pdf, pdf_graft_map *map) | |
| 778 { | |
| 779 jobject jmap; | |
| 780 | |
| 781 if (!ctx || !map || !pdf) return NULL; | |
| 782 | |
| 783 jmap = (*env)->NewObject(env, cls_PDFGraftMap, mid_PDFGraftMap_init, jlong_cast(map), pdf); | |
| 784 if (!jmap) | |
| 785 pdf_drop_graft_map(ctx, map); | |
| 786 | |
| 787 return jmap; | |
| 788 } | |
| 789 | |
| 790 static inline jobject to_PDFObject_safe_own(fz_context *ctx, JNIEnv *env, pdf_obj *obj) | |
| 791 { | |
| 792 jobject jobj; | |
| 793 | |
| 794 if (!ctx || !obj) return NULL; | |
| 795 | |
| 796 jobj = (*env)->NewObject(env, cls_PDFObject, mid_PDFObject_init, jlong_cast(obj)); | |
| 797 if (!jobj) | |
| 798 pdf_drop_obj(ctx, obj); | |
| 799 | |
| 800 return jobj; | |
| 801 } | |
| 802 | |
| 803 static inline jobject to_Pixmap_safe_own(fz_context *ctx, JNIEnv *env, fz_pixmap *pixmap) | |
| 804 { | |
| 805 jobject jobj; | |
| 806 | |
| 807 if (!ctx || !pixmap) return NULL; | |
| 808 | |
| 809 jobj = (*env)->NewObject(env, cls_Pixmap, mid_Pixmap_init, jlong_cast(pixmap)); | |
| 810 if (!jobj) | |
| 811 fz_drop_pixmap(ctx, pixmap); | |
| 812 | |
| 813 return jobj; | |
| 814 } | |
| 815 | |
| 816 static inline jobject to_String_safe_own(fz_context *ctx, JNIEnv *env, char *val) | |
| 817 { | |
| 818 jstring jval; | |
| 819 if (!ctx) return NULL; | |
| 820 | |
| 821 jval = (*env)->NewStringUTF(env, val); | |
| 822 | |
| 823 fz_free(ctx, val); | |
| 824 | |
| 825 return jval; | |
| 826 } | |
| 827 | |
| 828 static inline jobject to_StructuredText_safe_own(fz_context *ctx, JNIEnv *env, fz_stext_page *text) | |
| 829 { | |
| 830 jobject jtext; | |
| 831 | |
| 832 if (!ctx || !text) return NULL; | |
| 833 | |
| 834 jtext = (*env)->NewObject(env, cls_StructuredText, mid_StructuredText_init, jlong_cast(text)); | |
| 835 if (!jtext) | |
| 836 fz_drop_stext_page(ctx, text); | |
| 837 | |
| 838 return jtext; | |
| 839 } | |
| 840 | |
| 841 /* Conversion functions: Java to C. These all throw java exceptions. */ | |
| 842 | |
| 843 static inline fz_archive *from_Archive(JNIEnv *env, jobject jobj) | |
| 844 { | |
| 845 fz_archive *arch; | |
| 846 if (!jobj) return NULL; | |
| 847 arch = CAST(fz_archive *, (*env)->GetLongField(env, jobj, fid_Archive_pointer)); | |
| 848 if (!arch) jni_throw_null(env, "cannot use already destroyed Archive"); | |
| 849 return arch; | |
| 850 } | |
| 851 | |
| 852 static inline fz_buffer *from_Buffer(JNIEnv *env, jobject jobj) | |
| 853 { | |
| 854 fz_buffer *buffer; | |
| 855 if (!jobj) return NULL; | |
| 856 buffer = CAST(fz_buffer *, (*env)->GetLongField(env, jobj, fid_Buffer_pointer)); | |
| 857 if (!buffer) jni_throw_null(env, "cannot use already destroyed Buffer"); | |
| 858 return buffer; | |
| 859 } | |
| 860 | |
| 861 static inline fz_colorspace *from_ColorSpace(JNIEnv *env, jobject jobj) | |
| 862 { | |
| 863 fz_colorspace *cs; | |
| 864 if (!jobj) return NULL; | |
| 865 cs = CAST(fz_colorspace *, (*env)->GetLongField(env, jobj, fid_ColorSpace_pointer)); | |
| 866 if (!cs) jni_throw_null(env, "cannot use already destroyed ColorSpace"); | |
| 867 return cs; | |
| 868 } | |
| 869 | |
| 870 static inline fz_cookie *from_Cookie(JNIEnv *env, jobject jobj) | |
| 871 { | |
| 872 fz_cookie *cookie; | |
| 873 if (!jobj) return NULL; | |
| 874 cookie = CAST(fz_cookie *, (*env)->GetLongField(env, jobj, fid_Cookie_pointer)); | |
| 875 if (!cookie) jni_throw_null(env, "cannot use already destroyed Cookie"); | |
| 876 return cookie; | |
| 877 } | |
| 878 | |
| 879 static inline fz_default_colorspaces *from_DefaultColorSpaces(JNIEnv *env, jobject jobj) | |
| 880 { | |
| 881 fz_default_colorspaces *dcs; | |
| 882 if (!jobj) return NULL; | |
| 883 dcs = CAST(fz_default_colorspaces *, (*env)->GetLongField(env, jobj, fid_DefaultColorSpaces_pointer)); | |
| 884 if (!dcs) jni_throw_null(env, "cannot use already destroyed DefaultColorSpaces"); | |
| 885 return dcs; | |
| 886 } | |
| 887 | |
| 888 static fz_device *from_Device(JNIEnv *env, jobject jobj) | |
| 889 { | |
| 890 fz_device *dev; | |
| 891 if (!jobj) return NULL; | |
| 892 dev = CAST(fz_device *, (*env)->GetLongField(env, jobj, fid_Device_pointer)); | |
| 893 if (!dev) jni_throw_null(env, "cannot use already destroyed Device"); | |
| 894 return dev; | |
| 895 } | |
| 896 | |
| 897 static inline fz_display_list *from_DisplayList(JNIEnv *env, jobject jobj) | |
| 898 { | |
| 899 fz_display_list *list; | |
| 900 if (!jobj) return NULL; | |
| 901 list = CAST(fz_display_list *, (*env)->GetLongField(env, jobj, fid_DisplayList_pointer)); | |
| 902 if (!list) jni_throw_null(env, "cannot use already destroyed DisplayList"); | |
| 903 return list; | |
| 904 } | |
| 905 | |
| 906 static inline fz_document *from_Document(JNIEnv *env, jobject jobj) | |
| 907 { | |
| 908 fz_document *doc; | |
| 909 if (!jobj) return NULL; | |
| 910 doc = CAST(fz_document *, (*env)->GetLongField(env, jobj, fid_Document_pointer)); | |
| 911 if (!doc) jni_throw_null(env, "cannot use already destroyed Document"); | |
| 912 return doc; | |
| 913 } | |
| 914 | |
| 915 static inline fz_document_writer *from_DocumentWriter(JNIEnv *env, jobject jobj) | |
| 916 { | |
| 917 fz_document_writer *wri; | |
| 918 if (!jobj) return NULL; | |
| 919 wri = CAST(fz_document_writer *, (*env)->GetLongField(env, jobj, fid_DocumentWriter_pointer)); | |
| 920 if (!wri) jni_throw_null(env, "cannot use already destroyed DocumentWriter"); | |
| 921 return wri; | |
| 922 } | |
| 923 | |
| 924 static inline fz_font *from_Font(JNIEnv *env, jobject jobj) | |
| 925 { | |
| 926 fz_font *font; | |
| 927 if (!jobj) return NULL; | |
| 928 font = CAST(fz_font *, (*env)->GetLongField(env, jobj, fid_Font_pointer)); | |
| 929 if (!font) jni_throw_null(env, "cannot use already destroyed Font"); | |
| 930 return font; | |
| 931 } | |
| 932 | |
| 933 static inline fz_image *from_Image(JNIEnv *env, jobject jobj) | |
| 934 { | |
| 935 fz_image *image; | |
| 936 if (!jobj) return NULL; | |
| 937 image = CAST(fz_image *, (*env)->GetLongField(env, jobj, fid_Image_pointer)); | |
| 938 if (!image) jni_throw_null(env, "cannot use already destroyed Image"); | |
| 939 return image; | |
| 940 } | |
| 941 | |
| 942 static inline fz_link *from_Link(JNIEnv *env, jobject jobj) | |
| 943 { | |
| 944 fz_link *link; | |
| 945 if (!jobj) return NULL; | |
| 946 link = CAST(fz_link *, (*env)->GetLongField(env, jobj, fid_Link_pointer)); | |
| 947 if (!link) jni_throw_null(env, "cannot use already destroyed Link"); | |
| 948 return link; | |
| 949 } | |
| 950 | |
| 951 static inline fz_outline_iterator *from_OutlineIterator(JNIEnv *env, jobject jobj) | |
| 952 { | |
| 953 fz_outline_iterator *iterator; | |
| 954 if (!jobj) return NULL; | |
| 955 iterator = CAST(fz_outline_iterator *, (*env)->GetLongField(env, jobj, fid_OutlineIterator_pointer)); | |
| 956 if (!iterator) jni_throw_null(env, "cannot use already destroyed OutlineIterator"); | |
| 957 return iterator; | |
| 958 } | |
| 959 | |
| 960 static inline fz_page *from_Page(JNIEnv *env, jobject jobj) | |
| 961 { | |
| 962 fz_page *page; | |
| 963 if (!jobj) return NULL; | |
| 964 page = CAST(fz_page *, (*env)->GetLongField(env, jobj, fid_Page_pointer)); | |
| 965 if (!page) jni_throw_null(env, "cannot use already destroyed Page"); | |
| 966 return page; | |
| 967 } | |
| 968 | |
| 969 static inline fz_path *from_Path(JNIEnv *env, jobject jobj) | |
| 970 { | |
| 971 fz_path *path; | |
| 972 if (!jobj) return NULL; | |
| 973 path = CAST(fz_path *, (*env)->GetLongField(env, jobj, fid_Path_pointer)); | |
| 974 if (!path) jni_throw_null(env, "cannot use already destroyed Path"); | |
| 975 return path; | |
| 976 } | |
| 977 | |
| 978 static inline pdf_annot *from_PDFAnnotation(JNIEnv *env, jobject jobj) | |
| 979 { | |
| 980 pdf_annot *annot; | |
| 981 if (!jobj) return NULL; | |
| 982 annot = CAST(pdf_annot *, (*env)->GetLongField(env, jobj, fid_PDFAnnotation_pointer)); | |
| 983 if (!annot) jni_throw_null(env, "cannot use already destroyed PDFAnnotation"); | |
| 984 return annot; | |
| 985 } | |
| 986 | |
| 987 static inline pdf_document *from_PDFDocument(JNIEnv *env, jobject jobj) | |
| 988 { | |
| 989 pdf_document *pdf; | |
| 990 if (!jobj) return NULL; | |
| 991 pdf = CAST(pdf_document *, (*env)->GetLongField(env, jobj, fid_PDFDocument_pointer)); | |
| 992 if (!pdf) jni_throw_null(env, "cannot use already destroyed PDFDocument"); | |
| 993 return pdf; | |
| 994 } | |
| 995 | |
| 996 static inline pdf_graft_map *from_PDFGraftMap(JNIEnv *env, jobject jobj) | |
| 997 { | |
| 998 pdf_graft_map *map; | |
| 999 if (!jobj) return NULL; | |
| 1000 map = CAST(pdf_graft_map *, (*env)->GetLongField(env, jobj, fid_PDFGraftMap_pointer)); | |
| 1001 if (!map) jni_throw_null(env, "cannot use already destroyed PDFGraftMap"); | |
| 1002 return map; | |
| 1003 } | |
| 1004 | |
| 1005 static inline pdf_obj *from_PDFObject(JNIEnv *env, jobject jobj) | |
| 1006 { | |
| 1007 pdf_obj *obj; | |
| 1008 if (!jobj) return NULL; | |
| 1009 obj = CAST(pdf_obj *, (*env)->GetLongField(env, jobj, fid_PDFObject_pointer)); | |
| 1010 return obj; | |
| 1011 } | |
| 1012 | |
| 1013 static inline pdf_page *from_PDFPage(JNIEnv *env, jobject jobj) | |
| 1014 { | |
| 1015 pdf_page *page; | |
| 1016 if (!jobj) return NULL; | |
| 1017 page = CAST(pdf_page *, (*env)->GetLongField(env, jobj, fid_PDFPage_pointer)); | |
| 1018 if (!page) jni_throw_null(env, "cannot use already destroyed PDFPage"); | |
| 1019 return page; | |
| 1020 } | |
| 1021 | |
| 1022 static inline fz_pixmap *from_Pixmap(JNIEnv *env, jobject jobj) | |
| 1023 { | |
| 1024 fz_pixmap *pixmap; | |
| 1025 if (!jobj) return NULL; | |
| 1026 pixmap = CAST(fz_pixmap *, (*env)->GetLongField(env, jobj, fid_Pixmap_pointer)); | |
| 1027 if (!pixmap) jni_throw_null(env, "cannot use already destroyed Pixmap"); | |
| 1028 return pixmap; | |
| 1029 } | |
| 1030 | |
| 1031 static inline fz_shade *from_Shade(JNIEnv *env, jobject jobj) | |
| 1032 { | |
| 1033 fz_shade *shd; | |
| 1034 if (!jobj) return NULL; | |
| 1035 shd = CAST(fz_shade *, (*env)->GetLongField(env, jobj, fid_Shade_pointer)); | |
| 1036 if (!shd) jni_throw_null(env, "cannot use already destroyed Shade"); | |
| 1037 return shd; | |
| 1038 } | |
| 1039 | |
| 1040 static inline fz_stroke_state *from_StrokeState(JNIEnv *env, jobject jobj) | |
| 1041 { | |
| 1042 fz_stroke_state *stroke; | |
| 1043 if (!jobj) return NULL; | |
| 1044 stroke = CAST(fz_stroke_state *, (*env)->GetLongField(env, jobj, fid_StrokeState_pointer)); | |
| 1045 if (!stroke) jni_throw_null(env, "cannot use already destroyed StrokeState"); | |
| 1046 return stroke; | |
| 1047 } | |
| 1048 | |
| 1049 static inline fz_stext_page *from_StructuredText(JNIEnv *env, jobject jobj) | |
| 1050 { | |
| 1051 fz_stext_page *stext; | |
| 1052 if (!jobj) return NULL; | |
| 1053 stext = CAST(fz_stext_page *, (*env)->GetLongField(env, jobj, fid_StructuredText_pointer)); | |
| 1054 if (!stext) jni_throw_null(env, "cannot use already destroyed StructuredText"); | |
| 1055 return stext; | |
| 1056 } | |
| 1057 | |
| 1058 static inline fz_text *from_Text(JNIEnv *env, jobject jobj) | |
| 1059 { | |
| 1060 fz_text *text; | |
| 1061 if (!jobj) return NULL; | |
| 1062 text = CAST(fz_text *, (*env)->GetLongField(env, jobj, fid_Text_pointer)); | |
| 1063 if (!text) jni_throw_null(env, "cannot use already destroyed Text"); | |
| 1064 return text; | |
| 1065 } | |
| 1066 | |
| 1067 static inline int from_jfloatArray(JNIEnv *env, float *color, jint n, jfloatArray jcolor) | |
| 1068 { | |
| 1069 jsize len; | |
| 1070 | |
| 1071 if (!jcolor) | |
| 1072 len = 0; | |
| 1073 else | |
| 1074 { | |
| 1075 len = (*env)->GetArrayLength(env, jcolor); | |
| 1076 if (len > n) | |
| 1077 len = n; | |
| 1078 (*env)->GetFloatArrayRegion(env, jcolor, 0, len, color); | |
| 1079 if ((*env)->ExceptionCheck(env)) return 0; | |
| 1080 } | |
| 1081 | |
| 1082 if (len < n) | |
| 1083 memset(color+len, 0, (n - len) * sizeof(float)); | |
| 1084 | |
| 1085 return 1; | |
| 1086 } | |
| 1087 | |
| 1088 static inline fz_matrix from_Matrix(JNIEnv *env, jobject jmat) | |
| 1089 { | |
| 1090 fz_matrix mat; | |
| 1091 | |
| 1092 if (!jmat) | |
| 1093 return fz_identity; | |
| 1094 | |
| 1095 mat.a = (*env)->GetFloatField(env, jmat, fid_Matrix_a); | |
| 1096 mat.b = (*env)->GetFloatField(env, jmat, fid_Matrix_b); | |
| 1097 mat.c = (*env)->GetFloatField(env, jmat, fid_Matrix_c); | |
| 1098 mat.d = (*env)->GetFloatField(env, jmat, fid_Matrix_d); | |
| 1099 mat.e = (*env)->GetFloatField(env, jmat, fid_Matrix_e); | |
| 1100 mat.f = (*env)->GetFloatField(env, jmat, fid_Matrix_f); | |
| 1101 | |
| 1102 return mat; | |
| 1103 } | |
| 1104 | |
| 1105 static inline fz_point from_Point(JNIEnv *env, jobject jpt) | |
| 1106 { | |
| 1107 fz_point pt; | |
| 1108 | |
| 1109 if (!jpt) | |
| 1110 { | |
| 1111 pt.x = pt.y = 0; | |
| 1112 return pt; | |
| 1113 } | |
| 1114 | |
| 1115 pt.x = (*env)->GetFloatField(env, jpt, fid_Point_x); | |
| 1116 pt.y = (*env)->GetFloatField(env, jpt, fid_Point_y); | |
| 1117 | |
| 1118 return pt; | |
| 1119 } | |
| 1120 | |
| 1121 static inline fz_rect from_Rect(JNIEnv *env, jobject jrect) | |
| 1122 { | |
| 1123 fz_rect rect; | |
| 1124 | |
| 1125 if (!jrect) | |
| 1126 return fz_empty_rect; | |
| 1127 | |
| 1128 rect.x0 = (*env)->GetFloatField(env, jrect, fid_Rect_x0); | |
| 1129 rect.x1 = (*env)->GetFloatField(env, jrect, fid_Rect_x1); | |
| 1130 rect.y0 = (*env)->GetFloatField(env, jrect, fid_Rect_y0); | |
| 1131 rect.y1 = (*env)->GetFloatField(env, jrect, fid_Rect_y1); | |
| 1132 | |
| 1133 return rect; | |
| 1134 } | |
| 1135 | |
| 1136 static inline fz_quad from_Quad(JNIEnv *env, jobject jquad) | |
| 1137 { | |
| 1138 fz_quad quad; | |
| 1139 | |
| 1140 if (!jquad) | |
| 1141 return fz_make_quad(0, 0, 0, 0, 0, 0, 0, 0); | |
| 1142 | |
| 1143 quad.ul.x = (*env)->GetFloatField(env, jquad, fid_Quad_ul_x); | |
| 1144 quad.ul.y = (*env)->GetFloatField(env, jquad, fid_Quad_ul_y); | |
| 1145 quad.ur.x = (*env)->GetFloatField(env, jquad, fid_Quad_ur_x); | |
| 1146 quad.ur.y = (*env)->GetFloatField(env, jquad, fid_Quad_ur_y); | |
| 1147 quad.ll.x = (*env)->GetFloatField(env, jquad, fid_Quad_ll_x); | |
| 1148 quad.ll.y = (*env)->GetFloatField(env, jquad, fid_Quad_ll_y); | |
| 1149 quad.lr.x = (*env)->GetFloatField(env, jquad, fid_Quad_lr_x); | |
| 1150 quad.lr.y = (*env)->GetFloatField(env, jquad, fid_Quad_lr_y); | |
| 1151 | |
| 1152 return quad; | |
| 1153 } | |
| 1154 | |
| 1155 static fz_link_dest from_LinkDestination(JNIEnv *env, jobject jdest) | |
| 1156 { | |
| 1157 fz_link_dest dest; | |
| 1158 | |
| 1159 if (!jdest) | |
| 1160 return fz_make_link_dest_none(); | |
| 1161 | |
| 1162 dest.loc.chapter = (*env)->GetIntField(env, jdest, fid_LinkDestination_chapter); | |
| 1163 dest.loc.page = (*env)->GetIntField(env, jdest, fid_LinkDestination_page); | |
| 1164 dest.type = (*env)->GetIntField(env, jdest, fid_LinkDestination_type); | |
| 1165 dest.x = (*env)->GetFloatField(env, jdest, fid_LinkDestination_x); | |
| 1166 dest.y = (*env)->GetFloatField(env, jdest, fid_LinkDestination_y); | |
| 1167 dest.w = (*env)->GetFloatField(env, jdest, fid_LinkDestination_width); | |
| 1168 dest.h = (*env)->GetFloatField(env, jdest, fid_LinkDestination_height); | |
| 1169 dest.zoom = (*env)->GetFloatField(env, jdest, fid_LinkDestination_zoom); | |
| 1170 | |
| 1171 return dest; | |
| 1172 } | |
| 1173 | |
| 1174 /* Conversion functions: Java to C. None of these throw java exceptions. */ | |
| 1175 | |
| 1176 static inline fz_archive *from_Archive_safe(JNIEnv *env, jobject jobj) | |
| 1177 { | |
| 1178 if (!jobj) return NULL; | |
| 1179 return CAST(fz_archive *, (*env)->GetLongField(env, jobj, fid_Archive_pointer)); | |
| 1180 } | |
| 1181 | |
| 1182 static inline fz_buffer *from_Buffer_safe(JNIEnv *env, jobject jobj) | |
| 1183 { | |
| 1184 if (!jobj) return NULL; | |
| 1185 return CAST(fz_buffer *, (*env)->GetLongField(env, jobj, fid_Buffer_pointer)); | |
| 1186 } | |
| 1187 | |
| 1188 static inline fz_color_params from_ColorParams_safe(JNIEnv *env, jint params) | |
| 1189 { | |
| 1190 fz_color_params p; | |
| 1191 | |
| 1192 p.bp = (params>>5) & 1; | |
| 1193 p.op = (params>>6) & 1; | |
| 1194 p.opm = (params>>7) & 1; | |
| 1195 p.ri = (params & 31); | |
| 1196 | |
| 1197 return p; | |
| 1198 } | |
| 1199 | |
| 1200 static inline fz_colorspace *from_ColorSpace_safe(JNIEnv *env, jobject jobj) | |
| 1201 { | |
| 1202 if (!jobj) return NULL; | |
| 1203 return CAST(fz_colorspace *, (*env)->GetLongField(env, jobj, fid_ColorSpace_pointer)); | |
| 1204 } | |
| 1205 | |
| 1206 static inline fz_cookie *from_Cookie_safe(JNIEnv *env, jobject jobj) | |
| 1207 { | |
| 1208 if (!jobj) return NULL; | |
| 1209 return CAST(fz_cookie *, (*env)->GetLongField(env, jobj, fid_Cookie_pointer)); | |
| 1210 } | |
| 1211 | |
| 1212 static inline fz_default_colorspaces *from_DefaultColorSpaces_safe(JNIEnv *env, jobject jobj) | |
| 1213 { | |
| 1214 if (!jobj) return NULL; | |
| 1215 return CAST(fz_default_colorspaces *, (*env)->GetLongField(env, jobj, fid_DefaultColorSpaces_pointer)); | |
| 1216 } | |
| 1217 | |
| 1218 static fz_device *from_Device_safe(JNIEnv *env, jobject jobj) | |
| 1219 { | |
| 1220 if (!jobj) return NULL; | |
| 1221 return CAST(fz_device *, (*env)->GetLongField(env, jobj, fid_Device_pointer)); | |
| 1222 } | |
| 1223 | |
| 1224 static inline fz_display_list *from_DisplayList_safe(JNIEnv *env, jobject jobj) | |
| 1225 { | |
| 1226 if (!jobj) return NULL; | |
| 1227 return CAST(fz_display_list *, (*env)->GetLongField(env, jobj, fid_DisplayList_pointer)); | |
| 1228 } | |
| 1229 | |
| 1230 static inline fz_document *from_Document_safe(JNIEnv *env, jobject jobj) | |
| 1231 { | |
| 1232 if (!jobj) return NULL; | |
| 1233 return CAST(fz_document *, (*env)->GetLongField(env, jobj, fid_Document_pointer)); | |
| 1234 } | |
| 1235 | |
| 1236 static inline fz_document_writer *from_DocumentWriter_safe(JNIEnv *env, jobject jobj) | |
| 1237 { | |
| 1238 if (!jobj) return NULL; | |
| 1239 return CAST(fz_document_writer *, (*env)->GetLongField(env, jobj, fid_DocumentWriter_pointer)); | |
| 1240 } | |
| 1241 | |
| 1242 static inline fz_font *from_Font_safe(JNIEnv *env, jobject jobj) | |
| 1243 { | |
| 1244 if (!jobj) return NULL; | |
| 1245 return CAST(fz_font *, (*env)->GetLongField(env, jobj, fid_Font_pointer)); | |
| 1246 } | |
| 1247 | |
| 1248 static inline fz_story *from_Story_safe(JNIEnv *env, jobject jobj) | |
| 1249 { | |
| 1250 if (!jobj) return NULL; | |
| 1251 return CAST(fz_story *, (*env)->GetLongField(env, jobj, fid_Story_pointer)); | |
| 1252 } | |
| 1253 | |
| 1254 static inline fz_image *from_Image_safe(JNIEnv *env, jobject jobj) | |
| 1255 { | |
| 1256 if (!jobj) return NULL; | |
| 1257 return CAST(fz_image *, (*env)->GetLongField(env, jobj, fid_Image_pointer)); | |
| 1258 } | |
| 1259 | |
| 1260 static inline fz_link *from_Link_safe(JNIEnv *env, jobject jobj) | |
| 1261 { | |
| 1262 if (!jobj) return NULL; | |
| 1263 return CAST(fz_link *, (*env)->GetLongField(env, jobj, fid_Link_pointer)); | |
| 1264 } | |
| 1265 | |
| 1266 static inline fz_page *from_Page_safe(JNIEnv *env, jobject jobj) | |
| 1267 { | |
| 1268 if (!jobj) return NULL; | |
| 1269 return CAST(fz_page *, (*env)->GetLongField(env, jobj, fid_Page_pointer)); | |
| 1270 } | |
| 1271 | |
| 1272 static inline fz_path *from_Path_safe(JNIEnv *env, jobject jobj) | |
| 1273 { | |
| 1274 if (!jobj) return NULL; | |
| 1275 return CAST(fz_path *, (*env)->GetLongField(env, jobj, fid_Path_pointer)); | |
| 1276 } | |
| 1277 | |
| 1278 static inline pdf_annot *from_PDFAnnotation_safe(JNIEnv *env, jobject jobj) | |
| 1279 { | |
| 1280 if (!jobj) return NULL; | |
| 1281 return CAST(pdf_annot *, (*env)->GetLongField(env, jobj, fid_PDFAnnotation_pointer)); | |
| 1282 } | |
| 1283 | |
| 1284 static inline pdf_document *from_PDFDocument_safe(JNIEnv *env, jobject jobj) | |
| 1285 { | |
| 1286 if (!jobj) return NULL; | |
| 1287 return CAST(pdf_document *, (*env)->GetLongField(env, jobj, fid_PDFDocument_pointer)); | |
| 1288 } | |
| 1289 | |
| 1290 static inline pdf_graft_map *from_PDFGraftMap_safe(JNIEnv *env, jobject jobj) | |
| 1291 { | |
| 1292 if (!jobj) return NULL; | |
| 1293 return CAST(pdf_graft_map *, (*env)->GetLongField(env, jobj, fid_PDFGraftMap_pointer)); | |
| 1294 } | |
| 1295 | |
| 1296 static inline pdf_obj *from_PDFObject_safe(JNIEnv *env, jobject jobj) | |
| 1297 { | |
| 1298 if (!jobj) return NULL; | |
| 1299 return CAST(pdf_obj *, (*env)->GetLongField(env, jobj, fid_PDFObject_pointer)); | |
| 1300 } | |
| 1301 | |
| 1302 static inline pdf_annot *from_PDFWidget_safe(JNIEnv *env, jobject jobj) | |
| 1303 { | |
| 1304 if (!jobj) return NULL; | |
| 1305 return CAST(pdf_annot *, (*env)->GetLongField(env, jobj, fid_PDFWidget_pointer)); | |
| 1306 } | |
| 1307 | |
| 1308 static inline pdf_pkcs7_signer *from_PKCS7Signer_safe(JNIEnv *env, jobject jobj) | |
| 1309 { | |
| 1310 if (!jobj) return NULL; | |
| 1311 return CAST(pdf_pkcs7_signer *, (*env)->GetLongField(env, jobj, fid_PKCS7Signer_pointer)); | |
| 1312 } | |
| 1313 | |
| 1314 static inline java_pkcs7_verifier *from_PKCS7Verifier_safe(JNIEnv *env, jobject jobj) | |
| 1315 { | |
| 1316 if (!jobj) return NULL; | |
| 1317 return CAST(java_pkcs7_verifier *, (*env)->GetLongField(env, jobj, fid_PKCS7Verifier_pointer)); | |
| 1318 } | |
| 1319 | |
| 1320 static inline fz_stream *from_FitzInputStream_safe(JNIEnv *env, jobject jobj) | |
| 1321 { | |
| 1322 if (!jobj) return NULL; | |
| 1323 return CAST(fz_stream *, (*env)->GetLongField(env, jobj, fid_FitzInputStream_pointer)); | |
| 1324 } | |
| 1325 | |
| 1326 static inline fz_pixmap *from_Pixmap_safe(JNIEnv *env, jobject jobj) | |
| 1327 { | |
| 1328 if (!jobj) return NULL; | |
| 1329 return CAST(fz_pixmap *, (*env)->GetLongField(env, jobj, fid_Pixmap_pointer)); | |
| 1330 } | |
| 1331 | |
| 1332 static inline fz_shade *from_Shade_safe(JNIEnv *env, jobject jobj) | |
| 1333 { | |
| 1334 if (!jobj) return NULL; | |
| 1335 return CAST(fz_shade *, (*env)->GetLongField(env, jobj, fid_Shade_pointer)); | |
| 1336 } | |
| 1337 | |
| 1338 static inline fz_stroke_state *from_StrokeState_safe(JNIEnv *env, jobject jobj) | |
| 1339 { | |
| 1340 if (!jobj) return NULL; | |
| 1341 return CAST(fz_stroke_state *, (*env)->GetLongField(env, jobj, fid_StrokeState_pointer)); | |
| 1342 } | |
| 1343 | |
| 1344 static inline fz_stext_page *from_StructuredText_safe(JNIEnv *env, jobject jobj) | |
| 1345 { | |
| 1346 if (!jobj) return NULL; | |
| 1347 return CAST(fz_stext_page *, (*env)->GetLongField(env, jobj, fid_StructuredText_pointer)); | |
| 1348 } | |
| 1349 | |
| 1350 static inline fz_text *from_Text_safe(JNIEnv *env, jobject jobj) | |
| 1351 { | |
| 1352 if (!jobj) return NULL; | |
| 1353 return CAST(fz_text *, (*env)->GetLongField(env, jobj, fid_Text_pointer)); | |
| 1354 } | |
| 1355 | |
| 1356 static inline fz_xml *from_DOM_safe(JNIEnv *env, jobject jobj) | |
| 1357 { | |
| 1358 if (!jobj) return NULL; | |
| 1359 return CAST(fz_xml *, (*env)->GetLongField(env, jobj, fid_DOM_pointer)); | |
| 1360 } |
