comparison mupdf-source/platform/java/jni/pdfpage.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 /* PDFPage interface */
24
25 JNIEXPORT jobject JNICALL
26 FUN(PDFPage_createAnnotation)(JNIEnv *env, jobject self, jint type)
27 {
28 fz_context *ctx = get_context(env);
29 pdf_page *page = from_PDFPage(env, self);
30 pdf_annot *annot = NULL;
31
32 if (!ctx || !page) return NULL;
33
34 fz_try(ctx)
35 annot = pdf_create_annot(ctx, page, type);
36 fz_catch(ctx)
37 jni_rethrow(env, ctx);
38
39 return to_PDFAnnotation_safe_own(ctx, env, annot);
40 }
41
42 JNIEXPORT void JNICALL
43 FUN(PDFPage_deleteAnnotation)(JNIEnv *env, jobject self, jobject jannot)
44 {
45 fz_context *ctx = get_context(env);
46 pdf_page *page = from_PDFPage(env, self);
47 pdf_annot *annot = from_PDFAnnotation(env, jannot);
48
49 if (!ctx || !page) return;
50
51 fz_try(ctx)
52 pdf_delete_annot(ctx, page, annot);
53 fz_catch(ctx)
54 jni_rethrow_void(env, ctx);
55 }
56
57 JNIEXPORT jboolean JNICALL
58 FUN(PDFPage_update)(JNIEnv *env, jobject self)
59 {
60 fz_context *ctx = get_context(env);
61 pdf_page *page = from_PDFPage(env, self);
62 jboolean changed = JNI_FALSE;
63
64 if (!ctx || !page) return JNI_FALSE;
65
66 fz_try(ctx)
67 changed = pdf_update_page(ctx, page);
68 fz_catch(ctx)
69 jni_rethrow(env, ctx);
70
71 return changed;
72 }
73
74 JNIEXPORT jboolean JNICALL
75 FUN(PDFPage_applyRedactions)(JNIEnv *env, jobject self, jboolean blackBoxes, jint imageMethod, jint lineArt, jint text)
76 {
77 fz_context *ctx = get_context(env);
78 pdf_page *page = from_PDFPage(env, self);
79 jboolean redacted = JNI_FALSE;
80 pdf_redact_options opts = { blackBoxes, imageMethod, lineArt, text };
81
82 if (!ctx || !page) return JNI_FALSE;
83
84 fz_try(ctx)
85 redacted = pdf_redact_page(ctx, page->doc, page, &opts);
86 fz_catch(ctx)
87 jni_rethrow(env, ctx);
88
89 return redacted;
90 }
91
92 JNIEXPORT jobject JNICALL
93 FUN(PDFPage_getAnnotations)(JNIEnv *env, jobject self)
94 {
95 fz_context *ctx = get_context(env);
96 pdf_page *page = from_PDFPage(env, self);
97 pdf_annot *annot = NULL;
98 pdf_annot *annots = NULL;
99 jobject jannots = NULL;
100 int count;
101 int i;
102
103 if (!ctx || !page) return NULL;
104
105 /* count the annotations */
106 fz_try(ctx)
107 {
108 annots = pdf_first_annot(ctx, page);
109
110 annot = annots;
111 for (count = 0; annot; count++)
112 annot = pdf_next_annot(ctx, annot);
113 }
114 fz_catch(ctx)
115 jni_rethrow(env, ctx);
116
117 /* no annotations, return NULL instead of empty array */
118 if (count == 0)
119 return NULL;
120
121 /* now run through actually creating the annotation objects */
122 jannots = (*env)->NewObjectArray(env, count, cls_PDFAnnotation, NULL);
123 if (!jannots || (*env)->ExceptionCheck(env)) jni_throw_null(env, "cannot wrap page annotations in object array");
124
125 annot = annots;
126 for (i = 0; annot && i < count; i++)
127 {
128 jobject jannot = to_PDFAnnotation_safe(ctx, env, annot);
129 if (!jannot) return NULL;
130
131 (*env)->SetObjectArrayElement(env, jannots, i, jannot);
132 if ((*env)->ExceptionCheck(env)) return NULL;
133
134 (*env)->DeleteLocalRef(env, jannot);
135
136 fz_try(ctx)
137 annot = pdf_next_annot(ctx, annot);
138 fz_catch(ctx)
139 jni_rethrow(env, ctx);
140 }
141
142 return jannots;
143 }
144
145 JNIEXPORT jobjectArray JNICALL
146 FUN(PDFPage_getWidgets)(JNIEnv *env, jobject self)
147 {
148 fz_context *ctx = get_context(env);
149 pdf_page *page = from_PDFPage(env, self);
150 pdf_annot *widget = NULL;
151 pdf_annot *widgets = NULL;
152 jobjectArray jwidgets = NULL;
153 int count;
154 int i;
155
156 if (!ctx || !page) return NULL;
157
158 /* count the widgets */
159 fz_try(ctx)
160 {
161 widgets = pdf_first_widget(ctx, page);
162
163 widget = widgets;
164 for (count = 0; widget; count++)
165 widget = pdf_next_widget(ctx, widget);
166 }
167 fz_catch(ctx)
168 jni_rethrow(env, ctx);
169
170 /* no widgets, return NULL instead of empty array */
171 if (count == 0)
172 return NULL;
173
174 /* now run through actually creating the widget objects */
175 jwidgets = (*env)->NewObjectArray(env, count, cls_PDFWidget, NULL);
176 if (!jwidgets || (*env)->ExceptionCheck(env)) jni_throw_null(env, "cannot wrap page widgets in object array");
177
178 widget = widgets;
179 for (i = 0; widget && i < count; i++)
180 {
181 jobject jwidget = NULL;
182
183 if (widget)
184 {
185 jwidget = to_PDFWidget_safe(ctx, env, widget);
186 if (!jwidget) return NULL;
187 }
188
189 (*env)->SetObjectArrayElement(env, jwidgets, i, jwidget);
190 if ((*env)->ExceptionCheck(env)) return NULL;
191
192 (*env)->DeleteLocalRef(env, jwidget);
193
194 fz_try(ctx)
195 widget = pdf_next_widget(ctx, widget);
196 fz_catch(ctx)
197 jni_rethrow(env, ctx);
198 }
199
200 return jwidgets;
201 }
202
203 JNIEXPORT jobject JNICALL
204 FUN(PDFPage_createSignature)(JNIEnv *env, jobject self)
205 {
206 fz_context *ctx = get_context(env);
207 pdf_page *page = from_PDFPage(env, self);
208 pdf_annot *widget = NULL;
209 char name[80];
210
211 if (!ctx || !page)
212 return NULL;
213
214 fz_try(ctx)
215 {
216 pdf_create_field_name(ctx, page->doc, "Signature", name, sizeof name);
217 widget = pdf_create_signature_widget(ctx, page, name);
218 }
219 fz_catch(ctx)
220 {
221 jni_rethrow(env, ctx);
222 }
223
224 return to_PDFWidget_safe_own(ctx, env, widget);
225 }
226
227 JNIEXPORT jobject JNICALL
228 FUN(PDFPage_getTransform)(JNIEnv *env, jobject self)
229 {
230 fz_context *ctx = get_context(env);
231 pdf_page *page = from_PDFPage(env, self);
232 fz_matrix ctm;
233
234 if (!ctx || !page)
235 return NULL;
236
237 fz_try(ctx)
238 pdf_page_transform(ctx, page, NULL, &ctm);
239 fz_catch(ctx)
240 jni_rethrow(env, ctx);
241
242 return to_Matrix_safe(ctx, env, ctm);
243 }
244
245 JNIEXPORT jobject JNICALL
246 FUN(PDFPage_getObject)(JNIEnv *env, jobject self)
247 {
248 fz_context *ctx = get_context(env);
249 pdf_page *page = from_PDFPage(env, self);
250
251 if (!ctx || !page)
252 return NULL;
253
254 return to_PDFObject_safe_own(ctx, env, pdf_keep_obj(ctx, page->obj));
255 }
256
257 JNIEXPORT void JNICALL
258 FUN(PDFPage_setPageBox)(JNIEnv *env, jobject self, jint box, jobject jrect)
259 {
260 fz_context *ctx = get_context(env);
261 pdf_page *page = from_PDFPage(env, self);
262 fz_rect rect = from_Rect(env, jrect);
263
264 if (!ctx || !page)
265 return;
266
267 fz_try(ctx)
268 pdf_set_page_box(ctx, page, box, rect);
269 fz_catch(ctx)
270 jni_rethrow_void(env, ctx);
271 }
272
273 JNIEXPORT jint JNICALL
274 FUN(PDFPage_countAssociatedFiles)(JNIEnv *env, jobject self)
275 {
276 fz_context *ctx = get_context(env);
277 pdf_page *page = from_PDFPage(env, self);
278 int n;
279
280 fz_try(ctx)
281 n = pdf_count_page_associated_files(ctx, page);
282 fz_catch(ctx)
283 jni_rethrow(env, ctx);
284
285 return n;
286 }
287
288 JNIEXPORT jobject JNICALL
289 FUN(PDFPage_associatedFile)(JNIEnv *env, jobject self, jint idx)
290 {
291 fz_context *ctx = get_context(env);
292 pdf_page *page = from_PDFPage(env, self);
293 pdf_obj *af;
294
295 fz_try(ctx)
296 af = pdf_page_associated_file(ctx, page, idx);
297 fz_catch(ctx)
298 jni_rethrow(env, ctx);
299
300 return to_PDFObject_safe_own(ctx, env, af);
301 }
302
303 JNIEXPORT void JNICALL
304 FUN(PDFPage_process)(JNIEnv *env, jobject self, jobject jproc)
305 {
306 fz_context *ctx = get_context(env);
307 pdf_page *page = from_PDFPage(env, self);
308 pdf_processor *proc = make_pdf_processor(env, ctx, jproc);
309 pdf_obj *resources;
310 pdf_obj *contents;
311
312 if (!ctx || !page) return;
313 if (!proc) jni_throw_arg_void(env, "processor must not be null");
314
315 fz_try(ctx)
316 {
317 resources = pdf_page_resources(ctx, page);
318 contents = pdf_page_contents(ctx, page);
319 pdf_process_contents(ctx, proc, page->doc, resources, contents, NULL, NULL);
320 pdf_close_processor(ctx, proc);
321 }
322 fz_always(ctx)
323 {
324 pdf_drop_processor(ctx, proc);
325 }
326 fz_catch(ctx)
327 jni_rethrow_void(env, ctx);
328 }
329
330 JNIEXPORT jobject JNICALL
331 FUN(PDFPage_toPixmap)(JNIEnv *env, jobject self, jobject jctm, jobject jcs, jboolean alpha, jboolean showExtra, jstring jusage, jint box)
332 {
333 fz_context *ctx = get_context(env);
334 pdf_page *page = from_PDFPage(env, self);
335 fz_colorspace *cs = from_ColorSpace(env, jcs);
336 fz_matrix ctm = from_Matrix(env, jctm);
337 fz_pixmap *pixmap = NULL;
338 const char *usage = NULL;
339
340 if (!ctx || !page) return NULL;
341 if (jusage)
342 {
343 usage = (*env)->GetStringUTFChars(env, jusage, NULL);
344 if (!usage) return NULL;
345 }
346
347 fz_try(ctx)
348 {
349 if (showExtra)
350 pixmap = pdf_new_pixmap_from_page_with_usage(ctx, page, ctm, cs, alpha, usage, box);
351 else
352 pixmap = pdf_new_pixmap_from_page_contents_with_usage(ctx, page, ctm, cs, alpha, usage, box);
353 }
354 fz_always(ctx)
355 if (usage)
356 (*env)->ReleaseStringUTFChars(env, jusage, usage);
357 fz_catch(ctx)
358 jni_rethrow(env, ctx);
359
360 return to_Pixmap_safe_own(ctx, env, pixmap);
361 }