context.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. /* Context interface */
  23. /* Put the fz_context in thread-local storage */
  24. #ifdef _WIN32
  25. static CRITICAL_SECTION mutexes[FZ_LOCK_MAX];
  26. #else
  27. static pthread_mutex_t mutexes[FZ_LOCK_MAX];
  28. #endif
  29. static void lock(void *user, int lock)
  30. {
  31. #ifdef _WIN32
  32. EnterCriticalSection(&mutexes[lock]);
  33. #else
  34. (void)pthread_mutex_lock(&mutexes[lock]);
  35. #endif
  36. }
  37. static void unlock(void *user, int lock)
  38. {
  39. #ifdef _WIN32
  40. LeaveCriticalSection(&mutexes[lock]);
  41. #else
  42. (void)pthread_mutex_unlock(&mutexes[lock]);
  43. #endif
  44. }
  45. static const fz_locks_context locks =
  46. {
  47. NULL, /* user */
  48. lock,
  49. unlock
  50. };
  51. static void fin_base_context(JNIEnv *env)
  52. {
  53. int i;
  54. fz_drop_context(base_context);
  55. base_context = NULL;
  56. for (i = 0; i < FZ_LOCK_MAX; i++)
  57. #ifdef _WIN32
  58. DeleteCriticalSection(&mutexes[i]);
  59. #else
  60. (void)pthread_mutex_destroy(&mutexes[i]);
  61. #endif
  62. }
  63. #ifndef _WIN32
  64. static void drop_tls_context(void *arg)
  65. {
  66. fz_context *ctx = (fz_context *)arg;
  67. fz_drop_context(ctx);
  68. }
  69. #endif
  70. static void log_callback(void *user, const char *message)
  71. {
  72. jboolean detach = JNI_FALSE;
  73. JNIEnv *env = NULL;
  74. jobject jcallback;
  75. jstring jmessage;
  76. jobject jlock;
  77. jmethodID mid;
  78. env = jni_attach_thread(&detach);
  79. if (env == NULL)
  80. return;
  81. if (user != NULL)
  82. mid = mid_Context_Log_error;
  83. else
  84. mid = mid_Context_Log_warning;
  85. jcallback = (*env)->GetStaticObjectField(env, cls_Context, fid_Context_log);
  86. if (jcallback)
  87. {
  88. jlock = (*env)->GetStaticObjectField(env, cls_Context, fid_Context_lock);
  89. (*env)->MonitorEnter(env, jlock);
  90. jmessage = (*env)->NewStringUTF(env, message);
  91. (*env)->CallVoidMethod(env, jcallback, mid, jmessage);
  92. (*env)->DeleteLocalRef(env, jmessage);
  93. (*env)->MonitorExit(env, jlock);
  94. (*env)->DeleteLocalRef(env, jcallback);
  95. (*env)->DeleteLocalRef(env, jlock);
  96. }
  97. jni_detach_thread(detach);
  98. }
  99. static int init_base_context(JNIEnv *env)
  100. {
  101. int i;
  102. #ifdef FZ_JAVA_STORE_SIZE
  103. size_t fz_store_size = FZ_JAVA_STORE_SIZE;
  104. #else
  105. size_t fz_store_size = FZ_STORE_DEFAULT;
  106. #endif
  107. char *env_fz_store_size;
  108. #ifdef _WIN32
  109. /* No destructor on windows. We will leak contexts.
  110. * There is no easy way around this, but this page:
  111. * http://stackoverflow.com/questions/3241732/is-there-anyway-to-dynamically-free-thread-local-storage-in-the-win32-apis/3245082#3245082
  112. * suggests a workaround that we can implement if we
  113. * need to. */
  114. context_key = TlsAlloc();
  115. if (context_key == TLS_OUT_OF_INDEXES)
  116. {
  117. LOGE("cannot get thread local storage for storing base context");
  118. return -1;
  119. }
  120. #else
  121. int ret = pthread_key_create(&context_key, drop_tls_context);
  122. if (ret < 0)
  123. {
  124. LOGE("cannot get thread local storage for storing base context");
  125. return -1;
  126. }
  127. #endif
  128. for (i = 0; i < FZ_LOCK_MAX; i++)
  129. #ifdef _WIN32
  130. InitializeCriticalSection(&mutexes[i]);
  131. #else
  132. (void)pthread_mutex_init(&mutexes[i], NULL);
  133. #endif
  134. env_fz_store_size = getenv("FZ_JAVA_STORE_SIZE");
  135. if (env_fz_store_size)
  136. fz_store_size = fz_atoz(env_fz_store_size);
  137. base_context = fz_new_context(NULL, &locks, fz_store_size);
  138. if (!base_context)
  139. {
  140. LOGE("cannot create base context");
  141. fin_base_context(env);
  142. return -1;
  143. }
  144. fz_set_error_callback(base_context, log_callback, (void *) 1);
  145. fz_set_warning_callback(base_context, log_callback, (void *) 0);
  146. fz_try(base_context)
  147. fz_register_document_handlers(base_context);
  148. fz_catch(base_context)
  149. {
  150. fz_report_error(base_context);
  151. LOGE("cannot register document handlers");
  152. fin_base_context(env);
  153. return -1;
  154. }
  155. #ifdef HAVE_ANDROID
  156. fz_install_load_system_font_funcs(base_context,
  157. load_droid_font,
  158. load_droid_cjk_font,
  159. load_droid_fallback_font);
  160. #endif
  161. return 0;
  162. }
  163. static fz_context *get_context(JNIEnv *env)
  164. {
  165. fz_context *ctx = (fz_context *)
  166. #ifdef _WIN32
  167. TlsGetValue(context_key);
  168. if (ctx == NULL && GetLastError() != ERROR_SUCCESS) jni_throw_run(env, "cannot get context");
  169. #else
  170. pthread_getspecific(context_key);
  171. #endif
  172. if (ctx)
  173. return ctx;
  174. ctx = fz_clone_context(base_context);
  175. if (!ctx) jni_throw_oom(env, "failed to clone fz_context");
  176. #ifdef _WIN32
  177. if (TlsSetValue(context_key, ctx) == 0) jni_throw_run(env, "cannot store context");
  178. #else
  179. if (pthread_setspecific(context_key, ctx) != 0) jni_throw_run(env, "cannot store context");
  180. #endif
  181. return ctx;
  182. }
  183. JNIEXPORT jint JNICALL
  184. FUN(Context_initNative)(JNIEnv *env, jclass cls)
  185. {
  186. if (!check_enums())
  187. return -1;
  188. /* Must init the context before find_finds, because the act of
  189. * finding the fids can cause classes to load. This causes
  190. * statics to be setup, which can in turn call JNI code, which
  191. * requires the context. (For example see ColorSpace) */
  192. if (init_base_context(env) < 0)
  193. return -1;
  194. if (find_fids(env) < 0)
  195. {
  196. fin_base_context(env);
  197. return -1;
  198. }
  199. return 0;
  200. }
  201. JNIEXPORT void JNICALL
  202. FUN(Context_emptyStore)(JNIEnv *env, jclass cls)
  203. {
  204. fz_context *ctx = get_context(env);
  205. if (!ctx) return;
  206. fz_empty_store(ctx);
  207. }
  208. JNIEXPORT void JNICALL
  209. FUN(Context_enableICC)(JNIEnv *env, jclass cls)
  210. {
  211. fz_context *ctx = get_context(env);
  212. if (!ctx) return;
  213. fz_enable_icc(ctx);
  214. }
  215. JNIEXPORT void JNICALL
  216. FUN(Context_disableICC)(JNIEnv *env, jclass cls)
  217. {
  218. fz_context *ctx = get_context(env);
  219. if (!ctx) return;
  220. fz_disable_icc(ctx);
  221. }
  222. JNIEXPORT void JNICALL
  223. FUN(Context_setAntiAliasLevel)(JNIEnv *env, jclass cls, jint level)
  224. {
  225. fz_context *ctx = get_context(env);
  226. if (!ctx) return;
  227. fz_set_aa_level(ctx, level);
  228. }
  229. JNIEXPORT jobject JNICALL
  230. FUN(Context_getVersion)(JNIEnv *env, jclass cls)
  231. {
  232. fz_context *ctx = get_context(env);
  233. jobject jversion = NULL;
  234. jobject jvs = NULL;
  235. if (!ctx) return NULL;
  236. jvs = (*env)->NewStringUTF(env, FZ_VERSION);
  237. if (!jvs || (*env)->ExceptionCheck(env))
  238. return NULL;
  239. jversion = (*env)->NewObject(env, cls_Context_Version, mid_Context_Version_init);
  240. if (!jversion || (*env)->ExceptionCheck(env))
  241. return NULL;
  242. (*env)->SetIntField(env, jversion, fid_Context_Version_major, FZ_VERSION_MAJOR);
  243. (*env)->SetIntField(env, jversion, fid_Context_Version_minor, FZ_VERSION_MINOR);
  244. (*env)->SetIntField(env, jversion, fid_Context_Version_patch, FZ_VERSION_PATCH);
  245. (*env)->SetObjectField(env, jversion, fid_Context_Version_version, jvs);
  246. return jversion;
  247. }
  248. JNIEXPORT void JNICALL
  249. FUN(Context_setUserCSS)(JNIEnv *env, jclass cls, jstring jcss)
  250. {
  251. fz_context *ctx = get_context(env);
  252. const char *css = NULL;
  253. if (jcss)
  254. css = (*env)->GetStringUTFChars(env, jcss, NULL);
  255. fz_try(ctx)
  256. fz_set_user_css(ctx, css);
  257. fz_always(ctx)
  258. if (jcss)
  259. (*env)->ReleaseStringUTFChars(env, jcss, css);
  260. fz_catch(ctx)
  261. jni_rethrow_void(env, ctx);
  262. }
  263. JNIEXPORT void JNICALL
  264. FUN(Context_useDocumentCSS)(JNIEnv *env, jclass cls, jboolean state)
  265. {
  266. fz_context *ctx = get_context(env);
  267. fz_try(ctx)
  268. fz_set_use_document_css(ctx, state);
  269. fz_catch(ctx)
  270. jni_rethrow_void(env, ctx);
  271. }
  272. JNIEXPORT jboolean JNICALL
  273. FUN(Context_shrinkStore)(JNIEnv *env, jclass cls, jint percent)
  274. {
  275. fz_context *ctx = get_context(env);
  276. int success = 0;
  277. if (!ctx) return JNI_FALSE;
  278. if (percent < 0) jni_throw_arg(env, "percent must not be negative");
  279. if (percent > 100) jni_throw_arg(env, "percent must not be more than 100");
  280. fz_try(ctx)
  281. success = fz_shrink_store(ctx, percent);
  282. fz_catch(ctx)
  283. jni_rethrow(env, ctx);
  284. return success != 0;
  285. }