outlineiterator.c 6.4 KB

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