page.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. /* Page interface */
  23. JNIEXPORT void JNICALL
  24. FUN(Page_finalize)(JNIEnv *env, jobject self)
  25. {
  26. fz_context *ctx = get_context(env);
  27. fz_page *page = from_Page_safe(env, self);
  28. if (!ctx || !page) return;
  29. (*env)->SetLongField(env, self, fid_Page_pointer, 0);
  30. fz_drop_page(ctx, page);
  31. }
  32. JNIEXPORT jobject JNICALL
  33. FUN(Page_toPixmap)(JNIEnv *env, jobject self, jobject jctm, jobject jcs, jboolean alpha, jboolean showExtra)
  34. {
  35. fz_context *ctx = get_context(env);
  36. fz_page *page = from_Page(env, self);
  37. fz_colorspace *cs = from_ColorSpace(env, jcs);
  38. fz_matrix ctm = from_Matrix(env, jctm);
  39. fz_pixmap *pixmap = NULL;
  40. if (!ctx || !page) return NULL;
  41. fz_try(ctx)
  42. {
  43. if (showExtra)
  44. pixmap = fz_new_pixmap_from_page(ctx, page, ctm, cs, alpha);
  45. else
  46. pixmap = fz_new_pixmap_from_page_contents(ctx, page, ctm, cs, alpha);
  47. }
  48. fz_catch(ctx)
  49. jni_rethrow(env, ctx);
  50. return to_Pixmap_safe_own(ctx, env, pixmap);
  51. }
  52. JNIEXPORT jobject JNICALL
  53. FUN(Page_getBoundsNative)(JNIEnv *env, jobject self, jint box)
  54. {
  55. fz_context *ctx = get_context(env);
  56. fz_page *page = from_Page(env, self);
  57. fz_rect rect;
  58. if (!ctx || !page) return NULL;
  59. fz_try(ctx)
  60. rect = fz_bound_page_box(ctx, page, box);
  61. fz_catch(ctx)
  62. jni_rethrow(env, ctx);
  63. return to_Rect_safe(ctx, env, rect);
  64. }
  65. JNIEXPORT void JNICALL
  66. FUN(Page_run)(JNIEnv *env, jobject self, jobject jdev, jobject jctm, jobject jcookie)
  67. {
  68. fz_context *ctx = get_context(env);
  69. fz_page *page = from_Page(env, self);
  70. fz_device *dev = from_Device(env, jdev);
  71. fz_matrix ctm = from_Matrix(env, jctm);
  72. fz_cookie *cookie = from_Cookie(env, jcookie);
  73. NativeDeviceInfo *info;
  74. int err;
  75. if (!ctx || !page) return;
  76. if (!dev) jni_throw_arg_void(env, "device must not be null");
  77. info = lockNativeDevice(env, jdev, &err);
  78. if (err)
  79. return;
  80. fz_try(ctx)
  81. fz_run_page(ctx, page, dev, ctm, cookie);
  82. fz_always(ctx)
  83. unlockNativeDevice(env, info);
  84. fz_catch(ctx)
  85. jni_rethrow_void(env, ctx);
  86. }
  87. JNIEXPORT void JNICALL
  88. FUN(Page_runPageContents)(JNIEnv *env, jobject self, jobject jdev, jobject jctm, jobject jcookie)
  89. {
  90. fz_context *ctx = get_context(env);
  91. fz_page *page = from_Page(env, self);
  92. fz_device *dev = from_Device(env, jdev);
  93. fz_matrix ctm = from_Matrix(env, jctm);
  94. fz_cookie *cookie = from_Cookie(env, jcookie);
  95. NativeDeviceInfo *info;
  96. int err;
  97. if (!ctx || !page) return;
  98. if (!dev) jni_throw_arg_void(env, "device must not be null");
  99. info = lockNativeDevice(env, jdev, &err);
  100. if (err)
  101. return;
  102. fz_try(ctx)
  103. fz_run_page_contents(ctx, page, dev, ctm, cookie);
  104. fz_always(ctx)
  105. unlockNativeDevice(env, info);
  106. fz_catch(ctx)
  107. jni_rethrow_void(env, ctx);
  108. }
  109. JNIEXPORT void JNICALL
  110. FUN(Page_runPageAnnots)(JNIEnv *env, jobject self, jobject jdev, jobject jctm, jobject jcookie)
  111. {
  112. fz_context *ctx = get_context(env);
  113. fz_page *page = from_Page(env, self);
  114. fz_device *dev = from_Device(env, jdev);
  115. fz_matrix ctm = from_Matrix(env, jctm);
  116. fz_cookie *cookie = from_Cookie(env, jcookie);
  117. NativeDeviceInfo *info;
  118. int err;
  119. if (!ctx || !page) return;
  120. if (!dev) jni_throw_arg_void(env, "device must not be null");
  121. info = lockNativeDevice(env, jdev, &err);
  122. if (err)
  123. return;
  124. fz_try(ctx)
  125. fz_run_page_annots(ctx, page, dev, ctm, cookie);
  126. fz_always(ctx)
  127. unlockNativeDevice(env, info);
  128. fz_catch(ctx)
  129. jni_rethrow_void(env, ctx);
  130. }
  131. JNIEXPORT void JNICALL
  132. FUN(Page_runPageWidgets)(JNIEnv *env, jobject self, jobject jdev, jobject jctm, jobject jcookie)
  133. {
  134. fz_context *ctx = get_context(env);
  135. fz_page *page = from_Page(env, self);
  136. fz_device *dev = from_Device(env, jdev);
  137. fz_matrix ctm = from_Matrix(env, jctm);
  138. fz_cookie *cookie = from_Cookie(env, jcookie);
  139. NativeDeviceInfo *info;
  140. int err;
  141. if (!ctx || !page) return;
  142. if (!dev) jni_throw_arg_void(env, "device must not be null");
  143. info = lockNativeDevice(env, jdev, &err);
  144. if (err)
  145. return;
  146. fz_try(ctx)
  147. fz_run_page_widgets(ctx, page, dev, ctm, cookie);
  148. fz_always(ctx)
  149. unlockNativeDevice(env, info);
  150. fz_catch(ctx)
  151. jni_rethrow_void(env, ctx);
  152. }
  153. JNIEXPORT jobject JNICALL
  154. FUN(Page_getLinks)(JNIEnv *env, jobject self)
  155. {
  156. fz_context *ctx = get_context(env);
  157. fz_page *page = from_Page(env, self);
  158. fz_link *link = NULL;
  159. fz_link *links = NULL;
  160. jobject jlinks = NULL;
  161. int link_count;
  162. int i;
  163. if (!ctx || !page) return NULL;
  164. fz_var(links);
  165. fz_try(ctx)
  166. links = fz_load_links(ctx, page);
  167. fz_catch(ctx)
  168. {
  169. fz_drop_link(ctx, links);
  170. jni_rethrow(env, ctx);
  171. }
  172. /* count the links */
  173. link = links;
  174. for (link_count = 0; link; link_count++)
  175. link = link->next;
  176. /* no links, return NULL instead of empty array */
  177. if (link_count == 0)
  178. {
  179. fz_drop_link(ctx, links);
  180. return NULL;
  181. }
  182. /* now run through actually creating the link objects */
  183. jlinks = (*env)->NewObjectArray(env, link_count, cls_Link, NULL);
  184. if (!jlinks || (*env)->ExceptionCheck(env))
  185. {
  186. fz_drop_link(ctx, links);
  187. return NULL;
  188. }
  189. link = links;
  190. for (i = 0; link && i < link_count; i++)
  191. {
  192. jobject jlink = NULL;
  193. jlink = to_Link_safe(ctx, env, link);
  194. if (!jlink || (*env)->ExceptionCheck(env))
  195. {
  196. fz_drop_link(ctx, links);
  197. return NULL;
  198. }
  199. (*env)->SetObjectArrayElement(env, jlinks, i, jlink);
  200. if ((*env)->ExceptionCheck(env))
  201. {
  202. fz_drop_link(ctx, links);
  203. return NULL;
  204. }
  205. (*env)->DeleteLocalRef(env, jlink);
  206. link = link->next;
  207. }
  208. fz_drop_link(ctx, links);
  209. return jlinks;
  210. }
  211. JNIEXPORT jobjectArray JNICALL
  212. FUN(Page_search)(JNIEnv *env, jobject self, jstring jneedle)
  213. {
  214. fz_context *ctx = get_context(env);
  215. fz_page *page = from_Page(env, self);
  216. const char *needle = NULL;
  217. search_state state = { env, NULL, 0 };
  218. jobject jsample = NULL;
  219. if (!ctx || !page) return NULL;
  220. if (!jneedle) jni_throw_arg(env, "needle must not be null");
  221. needle = (*env)->GetStringUTFChars(env, jneedle, NULL);
  222. if (!needle) return NULL;
  223. state.hits = (*env)->NewObject(env, cls_ArrayList, mid_ArrayList_init);
  224. if (!state.hits || (*env)->ExceptionCheck(env)) return NULL;
  225. fz_try(ctx)
  226. fz_search_page_cb(ctx, page, needle, hit_callback, &state);
  227. fz_always(ctx)
  228. {
  229. (*env)->ReleaseStringUTFChars(env, jneedle, needle);
  230. }
  231. fz_catch(ctx)
  232. jni_rethrow(env, ctx);
  233. if (state.error)
  234. return NULL;
  235. jsample = (*env)->NewObjectArray(env, 0, cls_ArrayOfQuad, NULL);
  236. return (*env)->CallObjectMethod(env, state.hits, mid_ArrayList_toArray, jsample);
  237. }
  238. JNIEXPORT jobject JNICALL
  239. FUN(Page_toDisplayList)(JNIEnv *env, jobject self, jboolean showExtra)
  240. {
  241. fz_context *ctx = get_context(env);
  242. fz_page *page = from_Page(env, self);
  243. fz_display_list *list = NULL;
  244. if (!ctx || !page) return NULL;
  245. fz_try(ctx)
  246. if (showExtra)
  247. list = fz_new_display_list_from_page(ctx, page);
  248. else
  249. list = fz_new_display_list_from_page_contents(ctx, page);
  250. fz_catch(ctx)
  251. jni_rethrow(env, ctx);
  252. return to_DisplayList_safe_own(ctx, env, list);
  253. }
  254. JNIEXPORT jobject JNICALL
  255. FUN(Page_toStructuredText)(JNIEnv *env, jobject self, jstring joptions)
  256. {
  257. fz_context *ctx = get_context(env);
  258. fz_page *page = from_Page(env, self);
  259. fz_stext_page *text = NULL;
  260. const char *options= NULL;
  261. fz_stext_options opts;
  262. if (!ctx || !page) return NULL;
  263. if (joptions)
  264. {
  265. options = (*env)->GetStringUTFChars(env, joptions, NULL);
  266. if (!options) return NULL;
  267. }
  268. fz_try(ctx)
  269. {
  270. fz_parse_stext_options(ctx, &opts, options);
  271. text = fz_new_stext_page_from_page(ctx, page, &opts);
  272. }
  273. fz_always(ctx)
  274. {
  275. if (options)
  276. (*env)->ReleaseStringUTFChars(env, joptions, options);
  277. }
  278. fz_catch(ctx)
  279. jni_rethrow(env, ctx);
  280. return to_StructuredText_safe_own(ctx, env, text);
  281. }
  282. JNIEXPORT jbyteArray JNICALL
  283. FUN(Page_textAsHtml)(JNIEnv *env, jobject self)
  284. {
  285. fz_context *ctx = get_context(env);
  286. fz_page *page = from_Page(env, self);
  287. fz_stext_page *text = NULL;
  288. fz_device *dev = NULL;
  289. fz_matrix ctm;
  290. jbyteArray arr = NULL;
  291. fz_buffer *buf = NULL;
  292. fz_output *out = NULL;
  293. unsigned char *data;
  294. size_t len;
  295. if (!ctx || !page) return NULL;
  296. fz_var(text);
  297. fz_var(dev);
  298. fz_var(buf);
  299. fz_var(out);
  300. fz_try(ctx)
  301. {
  302. ctm = fz_identity;
  303. text = fz_new_stext_page(ctx, fz_bound_page(ctx, page));
  304. dev = fz_new_stext_device(ctx, text, NULL);
  305. fz_run_page(ctx, page, dev, ctm, NULL);
  306. fz_close_device(ctx, dev);
  307. buf = fz_new_buffer(ctx, 256);
  308. out = fz_new_output_with_buffer(ctx, buf);
  309. fz_print_stext_header_as_html(ctx, out);
  310. fz_print_stext_page_as_html(ctx, out, text, page->number);
  311. fz_print_stext_trailer_as_html(ctx, out);
  312. fz_close_output(ctx, out);
  313. len = fz_buffer_storage(ctx, buf, &data);
  314. arr = (*env)->NewByteArray(env, (jsize)len);
  315. if ((*env)->ExceptionCheck(env))
  316. fz_throw_java(ctx, env);
  317. if (!arr)
  318. fz_throw(ctx, FZ_ERROR_GENERIC, "cannot create byte array");
  319. (*env)->SetByteArrayRegion(env, arr, 0, (jsize)len, (jbyte *)data);
  320. if ((*env)->ExceptionCheck(env))
  321. fz_throw_java(ctx, env);
  322. }
  323. fz_always(ctx)
  324. {
  325. fz_drop_output(ctx, out);
  326. fz_drop_buffer(ctx, buf);
  327. fz_drop_device(ctx, dev);
  328. fz_drop_stext_page(ctx, text);
  329. }
  330. fz_catch(ctx)
  331. jni_rethrow(env, ctx);
  332. return arr;
  333. }
  334. JNIEXPORT jobject JNICALL
  335. FUN(Page_createLink)(JNIEnv *env, jobject self, jobject jrect, jstring juri)
  336. {
  337. fz_context *ctx = get_context(env);
  338. fz_page *page = from_Page(env, self);
  339. fz_rect rect = from_Rect(env, jrect);
  340. fz_link *link = NULL;
  341. const char *uri = NULL;
  342. if (!ctx || !page)
  343. return NULL;
  344. fz_try(ctx)
  345. {
  346. if (juri != NULL)
  347. {
  348. uri = (*env)->GetStringUTFChars(env, juri, NULL);
  349. if (!uri)
  350. fz_throw(ctx, FZ_ERROR_GENERIC, "cannot not get UTF string");
  351. }
  352. link = fz_create_link(ctx, page, rect, uri);
  353. }
  354. fz_always(ctx)
  355. {
  356. if (uri)
  357. (*env)->ReleaseStringUTFChars(env, juri, uri);
  358. }
  359. fz_catch(ctx)
  360. {
  361. jni_rethrow(env, ctx);
  362. }
  363. return to_Link_safe_own(ctx, env, link);
  364. }
  365. JNIEXPORT void JNICALL
  366. FUN(Page_deleteLink)(JNIEnv *env, jobject self, jobject jlink)
  367. {
  368. fz_context *ctx = get_context(env);
  369. fz_page *page = from_Page(env, self);
  370. fz_link *link = from_Link(env, jlink);
  371. if (!ctx || !page)
  372. return;
  373. fz_try(ctx)
  374. fz_delete_link(ctx, page, link);
  375. fz_catch(ctx)
  376. jni_rethrow_void(env, ctx);
  377. }
  378. JNIEXPORT jobject JNICALL
  379. FUN(Page_getDocument)(JNIEnv *env, jobject self)
  380. {
  381. fz_context *ctx = get_context(env);
  382. fz_page *page = from_Page(env, self);
  383. fz_document *doc = page ? page->doc : NULL;
  384. if (!ctx || !page || !doc) return NULL;
  385. return to_Document_safe_own(ctx, env, fz_keep_document(ctx, doc));
  386. }
  387. JNIEXPORT jstring JNICALL
  388. FUN(Page_getLabel)(JNIEnv *env, jobject self)
  389. {
  390. fz_context *ctx = get_context(env);
  391. fz_page *page = from_Page(env, self);
  392. char buf[100];
  393. if (!ctx || !page)
  394. return NULL;
  395. fz_try(ctx)
  396. fz_page_label(ctx, page, buf, sizeof buf);
  397. fz_catch(ctx)
  398. jni_rethrow(env, ctx);
  399. return (*env)->NewStringUTF(env, buf);
  400. }
  401. JNIEXPORT jobject JNICALL
  402. FUN(Page_decodeBarcode)(JNIEnv *env, jobject self, jobject jsubarea, jfloat rotate)
  403. {
  404. fz_context *ctx = get_context(env);
  405. fz_page *page = from_Page(env, self);
  406. fz_rect subarea = from_Rect(env, jsubarea);
  407. fz_barcode_type type = FZ_BARCODE_NONE;
  408. char *contents = NULL;
  409. jobject jcontents;
  410. jobject jbarcodeinfo;
  411. if (!ctx || !page)
  412. return NULL;
  413. fz_try(ctx)
  414. contents = fz_decode_barcode_from_page(ctx, &type, page, subarea, rotate);
  415. fz_catch(ctx)
  416. jni_rethrow(env, ctx);
  417. jcontents = (*env)->NewStringUTF(env, contents);
  418. fz_free(ctx, contents);
  419. if (!jcontents || (*env)->ExceptionCheck(env))
  420. return NULL;
  421. jbarcodeinfo = (*env)->NewObject(env, cls_BarcodeInfo, mid_BarcodeInfo_init, type, jcontents);
  422. if (!jbarcodeinfo || (*env)->ExceptionCheck(env))
  423. return NULL;
  424. return jbarcodeinfo;
  425. }