displaylist.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /* DisplayList interface */
  23. JNIEXPORT jlong JNICALL
  24. FUN(DisplayList_newNative)(JNIEnv *env, jobject self, jobject jmediabox)
  25. {
  26. fz_context *ctx = get_context(env);
  27. fz_rect mediabox = from_Rect(env, jmediabox);
  28. fz_display_list *list = NULL;
  29. if (!ctx) return 0;
  30. fz_try(ctx)
  31. list = fz_new_display_list(ctx, mediabox);
  32. fz_catch(ctx)
  33. jni_rethrow(env, ctx);
  34. return jlong_cast(list);
  35. }
  36. JNIEXPORT void JNICALL
  37. FUN(DisplayList_run)(JNIEnv *env, jobject self, jobject jdev, jobject jctm, jobject jrect, jobject jcookie)
  38. {
  39. fz_context *ctx = get_context(env);
  40. fz_display_list *list = from_DisplayList(env, self);
  41. fz_device *dev = from_Device(env, jdev);
  42. fz_matrix ctm = from_Matrix(env, jctm);
  43. fz_cookie *cookie = from_Cookie(env, jcookie);
  44. NativeDeviceInfo *info;
  45. fz_rect rect;
  46. int err;
  47. if (!ctx || !list) return;
  48. if (!dev) jni_throw_arg_void(env, "device must not be null");
  49. /* Use a scissor rectangle if one is supplied */
  50. if (jrect)
  51. rect = from_Rect(env, jrect);
  52. else
  53. rect = fz_infinite_rect;
  54. info = lockNativeDevice(env, jdev, &err);
  55. if (err)
  56. return;
  57. fz_try(ctx)
  58. fz_run_display_list(ctx, list, dev, ctm, rect, cookie);
  59. fz_always(ctx)
  60. unlockNativeDevice(env, info);
  61. fz_catch(ctx)
  62. jni_rethrow_void(env, ctx);
  63. }
  64. JNIEXPORT void JNICALL
  65. FUN(DisplayList_finalize)(JNIEnv *env, jobject self)
  66. {
  67. fz_context *ctx = get_context(env);
  68. fz_display_list *list = from_DisplayList_safe(env, self);
  69. if (!ctx || !list) return;
  70. (*env)->SetLongField(env, self, fid_DisplayList_pointer, 0);
  71. fz_drop_display_list(ctx, list);
  72. }
  73. JNIEXPORT jobject JNICALL
  74. FUN(DisplayList_toPixmap)(JNIEnv *env, jobject self, jobject jctm, jobject jcs, jboolean alpha)
  75. {
  76. fz_context *ctx = get_context(env);
  77. fz_display_list *list = from_DisplayList(env, self);
  78. fz_matrix ctm = from_Matrix(env, jctm);
  79. fz_colorspace *cs = from_ColorSpace(env, jcs);
  80. fz_pixmap *pixmap = NULL;
  81. if (!ctx || !list) return NULL;
  82. fz_try(ctx)
  83. pixmap = fz_new_pixmap_from_display_list(ctx, list, ctm, cs, alpha);
  84. fz_catch(ctx)
  85. jni_rethrow(env, ctx);
  86. return to_Pixmap_safe_own(ctx, env, pixmap);
  87. }
  88. JNIEXPORT jobject JNICALL
  89. FUN(DisplayList_toStructuredText)(JNIEnv *env, jobject self, jstring joptions)
  90. {
  91. fz_context *ctx = get_context(env);
  92. fz_display_list *list = from_DisplayList(env, self);
  93. fz_stext_page *text = NULL;
  94. const char *options= NULL;
  95. fz_stext_options opts;
  96. if (!ctx || !list) return NULL;
  97. if (joptions)
  98. {
  99. options = (*env)->GetStringUTFChars(env, joptions, NULL);
  100. if (!options) return NULL;
  101. }
  102. fz_try(ctx)
  103. {
  104. fz_parse_stext_options(ctx, &opts, options);
  105. text = fz_new_stext_page_from_display_list(ctx, list, &opts);
  106. }
  107. fz_always(ctx)
  108. {
  109. if (options)
  110. (*env)->ReleaseStringUTFChars(env, joptions, options);
  111. }
  112. fz_catch(ctx)
  113. jni_rethrow(env, ctx);
  114. return to_StructuredText_safe_own(ctx, env, text);
  115. }
  116. JNIEXPORT jobjectArray JNICALL
  117. FUN(DisplayList_search)(JNIEnv *env, jobject self, jstring jneedle)
  118. {
  119. fz_context *ctx = get_context(env);
  120. fz_display_list *list = from_DisplayList(env, self);
  121. const char *needle = NULL;
  122. search_state state = { env, NULL, 0 };
  123. jobject jsample = NULL;
  124. if (!ctx || !list) return NULL;
  125. if (!jneedle) jni_throw_arg(env, "needle must not be null");
  126. needle = (*env)->GetStringUTFChars(env, jneedle, NULL);
  127. if (!needle) return NULL;
  128. state.hits = (*env)->NewObject(env, cls_ArrayList, mid_ArrayList_init);
  129. if (!state.hits || (*env)->ExceptionCheck(env)) return NULL;
  130. fz_try(ctx)
  131. fz_search_display_list_cb(ctx, list, needle, hit_callback, &state);
  132. fz_always(ctx)
  133. {
  134. (*env)->ReleaseStringUTFChars(env, jneedle, needle);
  135. }
  136. fz_catch(ctx)
  137. jni_rethrow(env, ctx);
  138. if (state.error)
  139. return NULL;
  140. jsample = (*env)->NewObjectArray(env, 0, cls_ArrayOfQuad, NULL);
  141. return (*env)->CallObjectMethod(env, state.hits, mid_ArrayList_toArray, jsample);
  142. }
  143. JNIEXPORT jobject JNICALL
  144. FUN(DisplayList_getBounds)(JNIEnv *env, jobject self)
  145. {
  146. fz_context *ctx = get_context(env);
  147. fz_display_list *list = from_DisplayList(env, self);
  148. fz_rect bounds;
  149. if (!ctx || !list) return NULL;
  150. fz_try(ctx)
  151. bounds = fz_bound_display_list(ctx, list);
  152. fz_catch(ctx)
  153. jni_rethrow(env, ctx);
  154. return to_Rect(ctx, env, bounds);
  155. }
  156. JNIEXPORT jobject JNICALL
  157. FUN(DisplayList_decodeBarcode)(JNIEnv *env, jobject self, jobject jsubarea, jfloat rotate)
  158. {
  159. fz_context *ctx = get_context(env);
  160. fz_display_list *list = from_DisplayList(env, self);
  161. fz_rect subarea = from_Rect(env, jsubarea);
  162. fz_barcode_type type = FZ_BARCODE_NONE;
  163. char *contents = NULL;
  164. jobject jcontents;
  165. jobject jbarcodeinfo;
  166. if (!ctx || !list)
  167. return NULL;
  168. fz_try(ctx)
  169. contents = fz_decode_barcode_from_display_list(ctx, &type, list, subarea, rotate);
  170. fz_catch(ctx)
  171. jni_rethrow(env, ctx);
  172. jcontents = (*env)->NewStringUTF(env, contents);
  173. fz_free(ctx, contents);
  174. if (!jcontents || (*env)->ExceptionCheck(env))
  175. return NULL;
  176. jbarcodeinfo = (*env)->NewObject(env, cls_BarcodeInfo, mid_BarcodeInfo_init, type, jcontents);
  177. if (!jbarcodeinfo || (*env)->ExceptionCheck(env))
  178. return NULL;
  179. return jbarcodeinfo;
  180. }