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