comparison mupdf-source/platform/java/jni/outlineiterator.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) 2021-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 /* OutlineIterator interface */
24
25 JNIEXPORT void JNICALL
26 FUN(OutlineIterator_finalize)(JNIEnv *env, jobject self)
27 {
28 fz_context *ctx = get_context(env);
29 fz_outline_iterator *iterator = from_OutlineIterator(env, self);
30 if (!ctx || !iterator) return;
31 (*env)->SetLongField(env, self, fid_OutlineIterator_pointer, 0);
32 fz_drop_outline_iterator(ctx, iterator);
33 }
34
35 JNIEXPORT jint JNICALL
36 FUN(OutlineIterator_next)(JNIEnv *env, jobject self)
37 {
38 fz_context *ctx = get_context(env);
39 fz_outline_iterator *iterator = from_OutlineIterator(env, self);
40 int okay = -1;
41
42 if (!ctx || !iterator) return -1;
43
44 fz_try(ctx)
45 okay = fz_outline_iterator_next(ctx, iterator);
46 fz_catch(ctx)
47 jni_rethrow(env, ctx);
48
49 return okay;
50 }
51
52 JNIEXPORT jint JNICALL
53 FUN(OutlineIterator_prev)(JNIEnv *env, jobject self)
54 {
55 fz_context *ctx = get_context(env);
56 fz_outline_iterator *iterator = from_OutlineIterator(env, self);
57 int okay = -1;
58
59 if (!ctx || !iterator) return -1;
60
61 fz_try(ctx)
62 okay = fz_outline_iterator_prev(ctx, iterator);
63 fz_catch(ctx)
64 jni_rethrow(env, ctx);
65
66 return okay;
67 }
68
69 JNIEXPORT jint JNICALL
70 FUN(OutlineIterator_up)(JNIEnv *env, jobject self)
71 {
72 fz_context *ctx = get_context(env);
73 fz_outline_iterator *iterator = from_OutlineIterator(env, self);
74 int okay = -1;
75
76 if (!ctx || !iterator) return -1;
77
78 fz_try(ctx)
79 okay = fz_outline_iterator_up(ctx, iterator);
80 fz_catch(ctx)
81 jni_rethrow(env, ctx);
82
83 return okay;
84 }
85
86 JNIEXPORT jint JNICALL
87 FUN(OutlineIterator_down)(JNIEnv *env, jobject self)
88 {
89 fz_context *ctx = get_context(env);
90 fz_outline_iterator *iterator = from_OutlineIterator(env, self);
91 int okay = -1;
92
93 if (!ctx || !iterator) return -1;
94
95 fz_try(ctx)
96 okay = fz_outline_iterator_down(ctx, iterator);
97 fz_catch(ctx)
98 jni_rethrow(env, ctx);
99
100 return okay;
101 }
102
103 JNIEXPORT jint JNICALL
104 FUN(OutlineIterator_delete)(JNIEnv *env, jobject self)
105 {
106 fz_context *ctx = get_context(env);
107 fz_outline_iterator *iterator = from_OutlineIterator(env, self);
108 int okay = -1;
109
110 if (!ctx || !iterator) return -1;
111
112 fz_try(ctx)
113 okay = fz_outline_iterator_delete(ctx, iterator);
114 fz_catch(ctx)
115 jni_rethrow(env, ctx);
116
117 return okay;
118 }
119
120 static unsigned int to255(float x)
121 {
122 if (x < 0)
123 return 0;
124 if (x > 1)
125 return 255;
126 return (int)(x * 255 + 0.5);
127 }
128
129 JNIEXPORT void JNICALL
130 FUN(OutlineIterator_update)(JNIEnv *env, jobject self, jstring jtitle, jstring juri, jboolean is_open, jfloat r, jfloat g, jfloat b, int flags)
131 {
132 fz_context *ctx = get_context(env);
133 fz_outline_iterator *iterator = from_OutlineIterator(env, self);
134 fz_outline_item item = { 0 };
135
136 if (!ctx || !iterator) return;
137
138 item.is_open = is_open;
139
140 fz_try(ctx)
141 {
142 item.title = jtitle ? (char *)((*env)->GetStringUTFChars(env, jtitle, NULL)) : NULL;
143 if (item.title == NULL && jtitle != NULL)
144 fz_throw(ctx, FZ_ERROR_GENERIC, "OutlineIterator_update failed to get title as string");
145 item.uri = juri ? (char *)((*env)->GetStringUTFChars(env, juri, NULL)) : NULL;
146 if (item.uri == NULL && juri != NULL)
147 fz_throw(ctx, FZ_ERROR_GENERIC, "OutlineIterator_update failed to get uri as string");
148
149 item.r = to255(r);
150 item.g = to255(g);
151 item.b = to255(b);
152 item.flags = flags & 127;
153
154 fz_outline_iterator_update(ctx, iterator, &item);
155 }
156 fz_always(ctx)
157 {
158 if (item.title)
159 (*env)->ReleaseStringUTFChars(env, jtitle, item.title);
160 if (item.uri)
161 (*env)->ReleaseStringUTFChars(env, juri, item.uri);
162 }
163 fz_catch(ctx)
164 jni_rethrow_void(env, ctx);
165 }
166
167 JNIEXPORT jint JNICALL
168 FUN(OutlineIterator_insert)(JNIEnv *env, jobject self, jstring jtitle, jstring juri, jboolean is_open, jfloat r, jfloat g, jfloat b, int flags)
169 {
170 fz_context *ctx = get_context(env);
171 fz_outline_iterator *iterator = from_OutlineIterator(env, self);
172 int okay = -1;
173 fz_outline_item item = { 0 };
174
175 if (!ctx || !iterator) return -1;
176
177 item.is_open = is_open;
178
179 fz_try(ctx)
180 {
181 item.title = jtitle ? (char *)((*env)->GetStringUTFChars(env, jtitle, NULL)) : NULL;
182 if (item.title == NULL && jtitle != NULL)
183 fz_throw(ctx, FZ_ERROR_GENERIC, "OutlineIterator_insert failed to get title as string");
184 item.uri = juri ? (char *)((*env)->GetStringUTFChars(env, juri, NULL)) : NULL;
185 if (item.uri == NULL && juri != NULL)
186 fz_throw(ctx, FZ_ERROR_GENERIC, "OutlineIterator_insert failed to get uri as string");
187
188 item.r = to255(r);
189 item.g = to255(g);
190 item.b = to255(b);
191 item.flags = flags & 127;
192
193 okay = fz_outline_iterator_insert(ctx, iterator, &item);
194 }
195 fz_always(ctx)
196 {
197 if (item.title)
198 (*env)->ReleaseStringUTFChars(env, jtitle, item.title);
199 if (item.uri)
200 (*env)->ReleaseStringUTFChars(env, juri, item.uri);
201 }
202 fz_catch(ctx)
203 jni_rethrow(env, ctx);
204
205 return okay;
206 }
207
208 JNIEXPORT jobject JNICALL
209 FUN(OutlineIterator_item)(JNIEnv *env, jobject self)
210 {
211 fz_context *ctx = get_context(env);
212 fz_outline_iterator *iterator = from_OutlineIterator(env, self);
213 fz_outline_item *item = NULL;
214 jstring jtitle = NULL;
215 jstring juri = NULL;
216 float r, g, b;
217
218 if (!ctx || !iterator) return NULL;
219
220 fz_try(ctx)
221 item = fz_outline_iterator_item(ctx, iterator);
222 fz_catch(ctx)
223 jni_rethrow(env, ctx);
224
225 if (!item)
226 return NULL;
227
228 if (item->title)
229 {
230 jtitle = (*env)->NewStringUTF(env, item->title);
231 if (!jtitle || (*env)->ExceptionCheck(env))
232 return NULL;
233 }
234 if (item->uri)
235 {
236 juri = (*env)->NewStringUTF(env, item->uri);
237 if (!juri || (*env)->ExceptionCheck(env))
238 return NULL;
239 }
240 r = item->r / 255.0f;
241 g = item->g / 255.0f;
242 b = item->b / 255.0f;
243 return (*env)->NewObject(env, cls_OutlineItem, mid_OutlineItem_init, jtitle, juri, item->is_open, r, g, b, item->flags);
244 }