strokestate.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /* StrokeState interface */
  23. JNIEXPORT void JNICALL
  24. FUN(StrokeState_finalize)(JNIEnv *env, jobject self)
  25. {
  26. fz_context *ctx = get_context(env);
  27. fz_stroke_state *stroke = from_StrokeState_safe(env, self);
  28. if (!ctx || !stroke) return;
  29. (*env)->SetLongField(env, self, fid_StrokeState_pointer, 0);
  30. fz_drop_stroke_state(ctx, stroke);
  31. }
  32. JNIEXPORT jlong JNICALL
  33. FUN(StrokeState_newNativeStrokeState)(JNIEnv *env, jobject self, jint lineCap, jint lineJoin, jfloat lineWidth, jfloat miterLimit, jfloat dashPhase, jfloatArray dash)
  34. {
  35. fz_context *ctx = get_context(env);
  36. fz_stroke_state *stroke = NULL;
  37. jsize len = 0;
  38. if (!ctx) return 0;
  39. if (dash)
  40. len = (*env)->GetArrayLength(env, dash);
  41. fz_try(ctx)
  42. {
  43. stroke = fz_new_stroke_state_with_dash_len(ctx, len);
  44. stroke->start_cap = lineCap;
  45. stroke->dash_cap = lineCap;
  46. stroke->end_cap = lineCap;
  47. stroke->linejoin = lineJoin;
  48. stroke->linewidth = lineWidth;
  49. stroke->miterlimit = miterLimit;
  50. stroke->dash_phase = dashPhase;
  51. stroke->dash_len = len;
  52. }
  53. fz_catch(ctx)
  54. jni_rethrow(env, ctx);
  55. if (dash)
  56. {
  57. (*env)->GetFloatArrayRegion(env, dash, 0, len, &stroke->dash_list[0]);
  58. if ((*env)->ExceptionCheck(env)) return 0;
  59. }
  60. return jlong_cast(stroke);
  61. }
  62. JNIEXPORT jint JNICALL
  63. FUN(StrokeState_getLineCap)(JNIEnv *env, jobject self)
  64. {
  65. fz_stroke_state *stroke = from_StrokeState(env, self);
  66. return stroke ? stroke->start_cap : 0;
  67. }
  68. JNIEXPORT jint JNICALL
  69. FUN(StrokeState_getLineJoin)(JNIEnv *env, jobject self)
  70. {
  71. fz_stroke_state *stroke = from_StrokeState(env, self);
  72. return stroke ? stroke->linejoin : 0;
  73. }
  74. JNIEXPORT float JNICALL
  75. FUN(StrokeState_getLineWidth)(JNIEnv *env, jobject self)
  76. {
  77. fz_stroke_state *stroke = from_StrokeState(env, self);
  78. return stroke ? stroke->linewidth : 0;
  79. }
  80. JNIEXPORT float JNICALL
  81. FUN(StrokeState_getMiterLimit)(JNIEnv *env, jobject self)
  82. {
  83. fz_stroke_state *stroke = from_StrokeState(env, self);
  84. return stroke ? stroke->miterlimit : 0;
  85. }
  86. JNIEXPORT float JNICALL
  87. FUN(StrokeState_getDashPhase)(JNIEnv *env, jobject self)
  88. {
  89. fz_stroke_state *stroke = from_StrokeState(env, self);
  90. return stroke ? stroke->dash_phase : 0;
  91. }
  92. JNIEXPORT jfloatArray JNICALL
  93. FUN(StrokeState_getDashPattern)(JNIEnv *env, jobject self)
  94. {
  95. fz_context *ctx = get_context(env);
  96. fz_stroke_state *stroke = from_StrokeState(env, self);
  97. jfloatArray arr;
  98. if (!ctx || !stroke) return NULL;
  99. if (stroke->dash_len == 0)
  100. return NULL; /* there are no dashes, so return NULL instead of empty array */
  101. arr = (*env)->NewFloatArray(env, stroke->dash_len);
  102. if (!arr || (*env)->ExceptionCheck(env)) return NULL;
  103. (*env)->SetFloatArrayRegion(env, arr, 0, stroke->dash_len, &stroke->dash_list[0]);
  104. if ((*env)->ExceptionCheck(env)) return NULL;
  105. return arr;
  106. }