text.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (C) 2004-2021 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. /* Text interface */
  23. JNIEXPORT void JNICALL
  24. FUN(Text_finalize)(JNIEnv *env, jobject self)
  25. {
  26. fz_context *ctx = get_context(env);
  27. fz_text *text = from_Text_safe(env, self);
  28. if (!ctx || !text) return;
  29. (*env)->SetLongField(env, self, fid_Text_pointer, 0);
  30. fz_drop_text(ctx, text);
  31. }
  32. JNIEXPORT jlong JNICALL
  33. FUN(Text_newNative)(JNIEnv *env, jobject self)
  34. {
  35. fz_context *ctx = get_context(env);
  36. fz_text *text = NULL;
  37. if (!ctx) return 0;
  38. fz_try(ctx)
  39. text = fz_new_text(ctx);
  40. fz_catch(ctx)
  41. jni_rethrow(env, ctx);
  42. return jlong_cast(text);
  43. }
  44. JNIEXPORT jobject JNICALL
  45. FUN(Text_getBounds)(JNIEnv *env, jobject self, jobject jstroke, jobject jctm)
  46. {
  47. fz_context *ctx = get_context(env);
  48. fz_text *text = from_Text(env, self);
  49. fz_stroke_state *stroke = from_StrokeState(env, jstroke);
  50. fz_matrix ctm = from_Matrix(env, jctm);
  51. fz_rect rect;
  52. if (!ctx || !text) return NULL;
  53. if (!stroke) jni_throw_arg(env, "stroke must not be null");
  54. fz_try(ctx)
  55. rect = fz_bound_text(ctx, text, stroke, ctm);
  56. fz_catch(ctx)
  57. jni_rethrow(env, ctx);
  58. return to_Rect_safe(ctx, env, rect);
  59. }
  60. JNIEXPORT void JNICALL
  61. FUN(Text_showGlyph)(JNIEnv *env, jobject self, jobject jfont, jobject jtrm, jint glyph, jint unicode, jboolean wmode)
  62. {
  63. fz_context *ctx = get_context(env);
  64. fz_text *text = from_Text(env, self);
  65. fz_font *font = from_Font(env, jfont);
  66. fz_matrix trm = from_Matrix(env, jtrm);
  67. if (!ctx || !text) return;
  68. if (!font) jni_throw_arg_void(env, "font must not be null");
  69. fz_try(ctx)
  70. fz_show_glyph(ctx, text, font, trm, glyph, unicode, wmode, 0, FZ_BIDI_NEUTRAL, FZ_LANG_UNSET);
  71. fz_catch(ctx)
  72. jni_rethrow_void(env, ctx);
  73. }
  74. JNIEXPORT void JNICALL
  75. FUN(Text_showString)(JNIEnv *env, jobject self, jobject jfont, jobject jtrm, jstring jstr, jboolean wmode)
  76. {
  77. fz_context *ctx = get_context(env);
  78. fz_text *text = from_Text(env, self);
  79. fz_font *font = from_Font(env, jfont);
  80. fz_matrix trm = from_Matrix(env, jtrm);
  81. const char *str = NULL;
  82. if (!ctx || !text) return;
  83. if (!jfont) jni_throw_arg_void(env, "font must not be null");
  84. if (!jstr) jni_throw_arg_void(env, "string must not be null");
  85. str = (*env)->GetStringUTFChars(env, jstr, NULL);
  86. if (!str) return;
  87. fz_try(ctx)
  88. trm = fz_show_string(ctx, text, font, trm, str, wmode, 0, FZ_BIDI_NEUTRAL, FZ_LANG_UNSET);
  89. fz_always(ctx)
  90. (*env)->ReleaseStringUTFChars(env, jstr, str);
  91. fz_catch(ctx)
  92. jni_rethrow_void(env, ctx);
  93. (*env)->SetFloatField(env, jtrm, fid_Matrix_e, trm.e);
  94. (*env)->SetFloatField(env, jtrm, fid_Matrix_f, trm.f);
  95. }
  96. JNIEXPORT void JNICALL
  97. FUN(Text_walk)(JNIEnv *env, jobject self, jobject walker)
  98. {
  99. fz_context *ctx = get_context(env);
  100. fz_text *text = from_Text(env, self);
  101. fz_text_span *span;
  102. fz_font *font = NULL;
  103. jobject jfont = NULL;
  104. jobject jtrm = NULL;
  105. int i;
  106. if (!ctx || !text) return;
  107. if (!walker) jni_throw_arg_void(env, "walker must not be null");
  108. if (text->head == NULL)
  109. return; /* text has no spans to walk */
  110. for (span = text->head; span; span = span->next)
  111. {
  112. if (font != span->font)
  113. {
  114. if (jfont)
  115. (*env)->DeleteLocalRef(env, jfont);
  116. font = span->font;
  117. jfont = to_Font_safe(ctx, env, font);
  118. if (!jfont)
  119. return;
  120. }
  121. for (i = 0; i < span->len; ++i)
  122. {
  123. jtrm = (*env)->NewObject(env, cls_Matrix, mid_Matrix_init,
  124. span->trm.a, span->trm.b, span->trm.c, span->trm.d,
  125. span->items[i].x, span->items[i].y);
  126. if (!jtrm) return;
  127. (*env)->CallVoidMethod(env, walker, mid_TextWalker_showGlyph,
  128. jfont, jtrm,
  129. (jint)span->items[i].gid,
  130. (jint)span->items[i].ucs,
  131. (jint)span->wmode);
  132. if ((*env)->ExceptionCheck(env)) return;
  133. (*env)->DeleteLocalRef(env, jtrm);
  134. }
  135. }
  136. }