story.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2004-2024 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. /* Story interface */
  23. JNIEXPORT void JNICALL
  24. FUN(Story_finalize)(JNIEnv *env, jobject self)
  25. {
  26. fz_context *ctx = get_context(env);
  27. fz_story *story = from_Story_safe(env, self);
  28. if (!ctx || !story) return;
  29. (*env)->SetLongField(env, self, fid_Story_pointer, 0);
  30. fz_drop_story(ctx, story);
  31. }
  32. JNIEXPORT jlong JNICALL
  33. FUN(Story_newStory)(JNIEnv *env, jclass cls, jbyteArray content, jbyteArray css, float em, jobject jarch)
  34. {
  35. fz_context *ctx = get_context(env);
  36. int content_len, css_len;
  37. jbyte *content_bytes = NULL;
  38. jbyte *css_bytes = NULL;
  39. fz_story *story = NULL;
  40. fz_buffer *content_buf = NULL;
  41. fz_buffer *css_buf = NULL;
  42. fz_archive *arch = from_Archive(env, jarch);
  43. if (!ctx) return 0;
  44. if (content)
  45. {
  46. content_len = (*env)->GetArrayLength(env, content);
  47. content_bytes = (*env)->GetByteArrayElements(env, content, NULL);
  48. }
  49. else
  50. {
  51. content_len = 0;
  52. content_bytes = NULL;
  53. }
  54. if (css)
  55. {
  56. css_len = (*env)->GetArrayLength(env, css);
  57. css_bytes = (*env)->GetByteArrayElements(env, css, NULL);
  58. }
  59. else
  60. {
  61. css_len = 0;
  62. css_bytes = NULL;
  63. }
  64. fz_var(content_buf);
  65. fz_var(css_buf);
  66. fz_var(story);
  67. fz_try(ctx)
  68. {
  69. content_buf = fz_new_buffer_from_copied_data(ctx, (const unsigned char *)content_bytes, content_len);
  70. css_buf = fz_new_buffer_from_copied_data(ctx, (const unsigned char *)css_bytes, css_len);
  71. fz_terminate_buffer(ctx, css_buf);
  72. story = fz_new_story(ctx, content_buf, (const char *)css_buf->data, em, arch);
  73. }
  74. fz_always(ctx)
  75. {
  76. fz_drop_buffer(ctx, content_buf);
  77. }
  78. fz_catch(ctx)
  79. {
  80. jni_rethrow(env, ctx);
  81. }
  82. return jlong_cast(story);
  83. }
  84. JNIEXPORT jint JNICALL
  85. FUN(Story_place)(JNIEnv *env, jobject self, jobject jrect, jobject jfilled, jint flags)
  86. {
  87. fz_context *ctx = get_context(env);
  88. fz_story *story = from_Story_safe(env, self);
  89. fz_rect rect = from_Rect(env, jrect);
  90. fz_rect filled = fz_empty_rect;
  91. int more;
  92. fz_try(ctx)
  93. {
  94. more = fz_place_story_flags(ctx, story, rect, &filled, flags);
  95. (*env)->SetFloatField(env, jfilled, fid_Rect_x0, filled.x0);
  96. (*env)->SetFloatField(env, jfilled, fid_Rect_x1, filled.x1);
  97. (*env)->SetFloatField(env, jfilled, fid_Rect_y0, filled.y0);
  98. (*env)->SetFloatField(env, jfilled, fid_Rect_y1, filled.y1);
  99. }
  100. fz_catch(ctx)
  101. jni_rethrow(env, ctx);
  102. return more;
  103. }
  104. JNIEXPORT void JNICALL
  105. FUN(Story_draw)(JNIEnv *env, jobject self, jobject jdev, jobject jctm)
  106. {
  107. fz_context *ctx = get_context(env);
  108. fz_story *story = from_Story_safe(env, self);
  109. fz_device *dev = from_Device(env, jdev);
  110. fz_matrix ctm = from_Matrix(env, jctm);
  111. NativeDeviceInfo *info;
  112. int err;
  113. if (!ctx || !story) return;
  114. if (!dev) jni_throw_arg_void(env, "device must not be null");
  115. info = lockNativeDevice(env, jdev, &err);
  116. if (err)
  117. return;
  118. fz_try(ctx)
  119. fz_draw_story(ctx, story, dev, ctm);
  120. fz_always(ctx)
  121. unlockNativeDevice(env, info);
  122. fz_catch(ctx)
  123. jni_rethrow_void(env, ctx);
  124. }
  125. JNIEXPORT jobject JNICALL
  126. FUN(Story_document)(JNIEnv *env, jobject self)
  127. {
  128. fz_context *ctx = get_context(env);
  129. fz_story *story = from_Story_safe(env, self);
  130. return to_DOM_safe(ctx, env, fz_story_document(ctx, story));
  131. }