Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/platform/java/jni/text.c @ 3:2c135c81b16c
MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:44:09 +0200 |
| parents | b50eed0cc0ef |
| children |
comparison
equal
deleted
inserted
replaced
| 0:6015a75abc2d | 3:2c135c81b16c |
|---|---|
| 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 | |
| 23 /* Text interface */ | |
| 24 | |
| 25 JNIEXPORT void JNICALL | |
| 26 FUN(Text_finalize)(JNIEnv *env, jobject self) | |
| 27 { | |
| 28 fz_context *ctx = get_context(env); | |
| 29 fz_text *text = from_Text_safe(env, self); | |
| 30 if (!ctx || !text) return; | |
| 31 (*env)->SetLongField(env, self, fid_Text_pointer, 0); | |
| 32 fz_drop_text(ctx, text); | |
| 33 } | |
| 34 | |
| 35 JNIEXPORT jlong JNICALL | |
| 36 FUN(Text_newNative)(JNIEnv *env, jobject self) | |
| 37 { | |
| 38 fz_context *ctx = get_context(env); | |
| 39 fz_text *text = NULL; | |
| 40 | |
| 41 if (!ctx) return 0; | |
| 42 | |
| 43 fz_try(ctx) | |
| 44 text = fz_new_text(ctx); | |
| 45 fz_catch(ctx) | |
| 46 jni_rethrow(env, ctx); | |
| 47 | |
| 48 return jlong_cast(text); | |
| 49 } | |
| 50 | |
| 51 JNIEXPORT jobject JNICALL | |
| 52 FUN(Text_getBounds)(JNIEnv *env, jobject self, jobject jstroke, jobject jctm) | |
| 53 { | |
| 54 fz_context *ctx = get_context(env); | |
| 55 fz_text *text = from_Text(env, self); | |
| 56 fz_stroke_state *stroke = from_StrokeState(env, jstroke); | |
| 57 fz_matrix ctm = from_Matrix(env, jctm); | |
| 58 fz_rect rect; | |
| 59 | |
| 60 if (!ctx || !text) return NULL; | |
| 61 if (!stroke) jni_throw_arg(env, "stroke must not be null"); | |
| 62 | |
| 63 fz_try(ctx) | |
| 64 rect = fz_bound_text(ctx, text, stroke, ctm); | |
| 65 fz_catch(ctx) | |
| 66 jni_rethrow(env, ctx); | |
| 67 | |
| 68 return to_Rect_safe(ctx, env, rect); | |
| 69 } | |
| 70 | |
| 71 JNIEXPORT void JNICALL | |
| 72 FUN(Text_showGlyph)(JNIEnv *env, jobject self, jobject jfont, jobject jtrm, jint glyph, jint unicode, jboolean wmode) | |
| 73 { | |
| 74 fz_context *ctx = get_context(env); | |
| 75 fz_text *text = from_Text(env, self); | |
| 76 fz_font *font = from_Font(env, jfont); | |
| 77 fz_matrix trm = from_Matrix(env, jtrm); | |
| 78 | |
| 79 if (!ctx || !text) return; | |
| 80 if (!font) jni_throw_arg_void(env, "font must not be null"); | |
| 81 | |
| 82 fz_try(ctx) | |
| 83 fz_show_glyph(ctx, text, font, trm, glyph, unicode, wmode, 0, FZ_BIDI_NEUTRAL, FZ_LANG_UNSET); | |
| 84 fz_catch(ctx) | |
| 85 jni_rethrow_void(env, ctx); | |
| 86 } | |
| 87 | |
| 88 JNIEXPORT void JNICALL | |
| 89 FUN(Text_showString)(JNIEnv *env, jobject self, jobject jfont, jobject jtrm, jstring jstr, jboolean wmode) | |
| 90 { | |
| 91 fz_context *ctx = get_context(env); | |
| 92 fz_text *text = from_Text(env, self); | |
| 93 fz_font *font = from_Font(env, jfont); | |
| 94 fz_matrix trm = from_Matrix(env, jtrm); | |
| 95 const char *str = NULL; | |
| 96 | |
| 97 if (!ctx || !text) return; | |
| 98 if (!jfont) jni_throw_arg_void(env, "font must not be null"); | |
| 99 if (!jstr) jni_throw_arg_void(env, "string must not be null"); | |
| 100 | |
| 101 str = (*env)->GetStringUTFChars(env, jstr, NULL); | |
| 102 if (!str) return; | |
| 103 | |
| 104 fz_try(ctx) | |
| 105 trm = fz_show_string(ctx, text, font, trm, str, wmode, 0, FZ_BIDI_NEUTRAL, FZ_LANG_UNSET); | |
| 106 fz_always(ctx) | |
| 107 (*env)->ReleaseStringUTFChars(env, jstr, str); | |
| 108 fz_catch(ctx) | |
| 109 jni_rethrow_void(env, ctx); | |
| 110 | |
| 111 (*env)->SetFloatField(env, jtrm, fid_Matrix_e, trm.e); | |
| 112 (*env)->SetFloatField(env, jtrm, fid_Matrix_f, trm.f); | |
| 113 } | |
| 114 | |
| 115 JNIEXPORT void JNICALL | |
| 116 FUN(Text_walk)(JNIEnv *env, jobject self, jobject walker) | |
| 117 { | |
| 118 fz_context *ctx = get_context(env); | |
| 119 fz_text *text = from_Text(env, self); | |
| 120 fz_text_span *span; | |
| 121 fz_font *font = NULL; | |
| 122 jobject jfont = NULL; | |
| 123 jobject jtrm = NULL; | |
| 124 int i; | |
| 125 | |
| 126 if (!ctx || !text) return; | |
| 127 if (!walker) jni_throw_arg_void(env, "walker must not be null"); | |
| 128 | |
| 129 if (text->head == NULL) | |
| 130 return; /* text has no spans to walk */ | |
| 131 | |
| 132 for (span = text->head; span; span = span->next) | |
| 133 { | |
| 134 if (font != span->font) | |
| 135 { | |
| 136 if (jfont) | |
| 137 (*env)->DeleteLocalRef(env, jfont); | |
| 138 font = span->font; | |
| 139 jfont = to_Font_safe(ctx, env, font); | |
| 140 if (!jfont) | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 for (i = 0; i < span->len; ++i) | |
| 145 { | |
| 146 jtrm = (*env)->NewObject(env, cls_Matrix, mid_Matrix_init, | |
| 147 span->trm.a, span->trm.b, span->trm.c, span->trm.d, | |
| 148 span->items[i].x, span->items[i].y); | |
| 149 if (!jtrm) return; | |
| 150 | |
| 151 (*env)->CallVoidMethod(env, walker, mid_TextWalker_showGlyph, | |
| 152 jfont, jtrm, | |
| 153 (jint)span->items[i].gid, | |
| 154 (jint)span->items[i].ucs, | |
| 155 (jint)span->wmode); | |
| 156 if ((*env)->ExceptionCheck(env)) return; | |
| 157 | |
| 158 (*env)->DeleteLocalRef(env, jtrm); | |
| 159 } | |
| 160 } | |
| 161 } |
