The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* libs/android_runtime/android/graphics/Paint.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Dianne Hackborn | f43fa57 | 2011-08-12 18:59:39 -0700 | [diff] [blame] | 18 | #define LOG_TAG "Paint" |
| 19 | |
| 20 | #include <utils/Log.h> |
| 21 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include "jni.h" |
| 23 | #include "GraphicsJNI.h" |
| 24 | #include <android_runtime/AndroidRuntime.h> |
Billy Hewlett | ac1cbaf | 2012-07-18 09:51:45 -0700 | [diff] [blame] | 25 | #include <ScopedUtfChars.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | #include "SkBlurDrawLooper.h" |
| 28 | #include "SkColorFilter.h" |
| 29 | #include "SkMaskFilter.h" |
| 30 | #include "SkRasterizer.h" |
| 31 | #include "SkShader.h" |
| 32 | #include "SkTypeface.h" |
| 33 | #include "SkXfermode.h" |
Billy Hewlett | ac1cbaf | 2012-07-18 09:51:45 -0700 | [diff] [blame] | 34 | #include "unicode/uloc.h" |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 35 | #include "unicode/ushape.h" |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 36 | #include "TextLayout.h" |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 37 | |
| 38 | // temporary for debugging |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 39 | #include <Caches.h> |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 40 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | |
| 44 | struct JMetricsID { |
| 45 | jfieldID top; |
| 46 | jfieldID ascent; |
| 47 | jfieldID descent; |
| 48 | jfieldID bottom; |
| 49 | jfieldID leading; |
| 50 | }; |
| 51 | |
| 52 | static jclass gFontMetrics_class; |
| 53 | static JMetricsID gFontMetrics_fieldID; |
| 54 | |
| 55 | static jclass gFontMetricsInt_class; |
| 56 | static JMetricsID gFontMetricsInt_fieldID; |
| 57 | |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 58 | static void defaultSettingsForAndroid(SkPaint* paint) { |
Fabrice Di Meglio | c511bee8 | 2012-01-05 13:30:54 -0800 | [diff] [blame] | 59 | // GlyphID encoding is required because we are using Harfbuzz shaping |
| 60 | paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Derek Sollenberger | d7a8077 | 2013-05-28 10:44:26 -0400 | [diff] [blame] | 61 | |
| 62 | SkPaintOptionsAndroid paintOpts = paint->getPaintOptionsAndroid(); |
| 63 | paintOpts.setUseFontFallbacks(true); |
| 64 | paint->setPaintOptionsAndroid(paintOpts); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 65 | } |
| 66 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | class SkPaintGlue { |
| 68 | public: |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 69 | enum MoveOpt { |
| 70 | AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT |
| 71 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 73 | static void finalizer(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 74 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | delete obj; |
| 76 | } |
| 77 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 78 | static jlong init(JNIEnv* env, jobject clazz) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | SkPaint* obj = new SkPaint(); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 80 | defaultSettingsForAndroid(obj); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 81 | return reinterpret_cast<jlong>(obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 84 | static jlong initWithPaint(JNIEnv* env, jobject clazz, jlong paintHandle) { |
| 85 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | SkPaint* obj = new SkPaint(*paint); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 87 | return reinterpret_cast<jlong>(obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 89 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 90 | static void reset(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 91 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | obj->reset(); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 93 | defaultSettingsForAndroid(obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 95 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 96 | static void assign(JNIEnv* env, jobject clazz, jlong dstPaintHandle, jlong srcPaintHandle) { |
| 97 | SkPaint* dst = reinterpret_cast<SkPaint*>(dstPaintHandle); |
| 98 | const SkPaint* src = reinterpret_cast<SkPaint*>(srcPaintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | *dst = *src; |
| 100 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | static jint getFlags(JNIEnv* env, jobject paint) { |
| 103 | NPE_CHECK_RETURN_ZERO(env, paint); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 104 | int result; |
| 105 | result = GraphicsJNI::getNativePaint(env, paint)->getFlags(); |
| 106 | return static_cast<jint>(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | static void setFlags(JNIEnv* env, jobject paint, jint flags) { |
| 110 | NPE_CHECK_RETURN_VOID(env, paint); |
| 111 | GraphicsJNI::getNativePaint(env, paint)->setFlags(flags); |
| 112 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 113 | |
Dianne Hackborn | f43fa57 | 2011-08-12 18:59:39 -0700 | [diff] [blame] | 114 | static jint getHinting(JNIEnv* env, jobject paint) { |
| 115 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 116 | return GraphicsJNI::getNativePaint(env, paint)->getHinting() |
| 117 | == SkPaint::kNo_Hinting ? 0 : 1; |
| 118 | } |
| 119 | |
| 120 | static void setHinting(JNIEnv* env, jobject paint, jint mode) { |
| 121 | NPE_CHECK_RETURN_VOID(env, paint); |
| 122 | GraphicsJNI::getNativePaint(env, paint)->setHinting( |
Victoria Lease | 8870538 | 2013-06-14 16:15:41 -0700 | [diff] [blame] | 123 | mode == 0 ? SkPaint::kNo_Hinting : SkPaint::kNormal_Hinting); |
Dianne Hackborn | f43fa57 | 2011-08-12 18:59:39 -0700 | [diff] [blame] | 124 | } |
| 125 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | static void setAntiAlias(JNIEnv* env, jobject paint, jboolean aa) { |
| 127 | NPE_CHECK_RETURN_VOID(env, paint); |
| 128 | GraphicsJNI::getNativePaint(env, paint)->setAntiAlias(aa); |
| 129 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 130 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | static void setLinearText(JNIEnv* env, jobject paint, jboolean linearText) { |
| 132 | NPE_CHECK_RETURN_VOID(env, paint); |
| 133 | GraphicsJNI::getNativePaint(env, paint)->setLinearText(linearText); |
| 134 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | static void setSubpixelText(JNIEnv* env, jobject paint, jboolean subpixelText) { |
| 137 | NPE_CHECK_RETURN_VOID(env, paint); |
| 138 | GraphicsJNI::getNativePaint(env, paint)->setSubpixelText(subpixelText); |
| 139 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 140 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | static void setUnderlineText(JNIEnv* env, jobject paint, jboolean underlineText) { |
| 142 | NPE_CHECK_RETURN_VOID(env, paint); |
| 143 | GraphicsJNI::getNativePaint(env, paint)->setUnderlineText(underlineText); |
| 144 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | static void setStrikeThruText(JNIEnv* env, jobject paint, jboolean strikeThruText) { |
| 147 | NPE_CHECK_RETURN_VOID(env, paint); |
| 148 | GraphicsJNI::getNativePaint(env, paint)->setStrikeThruText(strikeThruText); |
| 149 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | static void setFakeBoldText(JNIEnv* env, jobject paint, jboolean fakeBoldText) { |
| 152 | NPE_CHECK_RETURN_VOID(env, paint); |
| 153 | GraphicsJNI::getNativePaint(env, paint)->setFakeBoldText(fakeBoldText); |
| 154 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | static void setFilterBitmap(JNIEnv* env, jobject paint, jboolean filterBitmap) { |
| 157 | NPE_CHECK_RETURN_VOID(env, paint); |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame^] | 158 | GraphicsJNI::getNativePaint(env, paint)->setFilterLevel( |
| 159 | filterBitmap ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | static void setDither(JNIEnv* env, jobject paint, jboolean dither) { |
| 163 | NPE_CHECK_RETURN_VOID(env, paint); |
| 164 | GraphicsJNI::getNativePaint(env, paint)->setDither(dither); |
| 165 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 166 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 167 | static jint getStyle(JNIEnv* env, jobject clazz,jlong objHandle) { |
| 168 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 169 | return static_cast<jint>(obj->getStyle()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 171 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 172 | static void setStyle(JNIEnv* env, jobject clazz, jlong objHandle, jint styleHandle) { |
| 173 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 174 | SkPaint::Style style = static_cast<SkPaint::Style>(styleHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | obj->setStyle(style); |
| 176 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | static jint getColor(JNIEnv* env, jobject paint) { |
| 179 | NPE_CHECK_RETURN_ZERO(env, paint); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 180 | int color; |
| 181 | color = GraphicsJNI::getNativePaint(env, paint)->getColor(); |
| 182 | return static_cast<jint>(color); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | static jint getAlpha(JNIEnv* env, jobject paint) { |
| 186 | NPE_CHECK_RETURN_ZERO(env, paint); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 187 | int alpha; |
| 188 | alpha = GraphicsJNI::getNativePaint(env, paint)->getAlpha(); |
| 189 | return static_cast<jint>(alpha); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 191 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | static void setColor(JNIEnv* env, jobject paint, jint color) { |
| 193 | NPE_CHECK_RETURN_VOID(env, paint); |
| 194 | GraphicsJNI::getNativePaint(env, paint)->setColor(color); |
| 195 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | static void setAlpha(JNIEnv* env, jobject paint, jint a) { |
| 198 | NPE_CHECK_RETURN_VOID(env, paint); |
| 199 | GraphicsJNI::getNativePaint(env, paint)->setAlpha(a); |
| 200 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | static jfloat getStrokeWidth(JNIEnv* env, jobject paint) { |
| 203 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 204 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getStrokeWidth()); |
| 205 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 206 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | static void setStrokeWidth(JNIEnv* env, jobject paint, jfloat width) { |
| 208 | NPE_CHECK_RETURN_VOID(env, paint); |
| 209 | GraphicsJNI::getNativePaint(env, paint)->setStrokeWidth(SkFloatToScalar(width)); |
| 210 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | static jfloat getStrokeMiter(JNIEnv* env, jobject paint) { |
| 213 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 214 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getStrokeMiter()); |
| 215 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | static void setStrokeMiter(JNIEnv* env, jobject paint, jfloat miter) { |
| 218 | NPE_CHECK_RETURN_VOID(env, paint); |
| 219 | GraphicsJNI::getNativePaint(env, paint)->setStrokeMiter(SkFloatToScalar(miter)); |
| 220 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 221 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 222 | static jint getStrokeCap(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 223 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 224 | return static_cast<jint>(obj->getStrokeCap()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 226 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 227 | static void setStrokeCap(JNIEnv* env, jobject clazz, jlong objHandle, jint capHandle) { |
| 228 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 229 | SkPaint::Cap cap = static_cast<SkPaint::Cap>(capHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | obj->setStrokeCap(cap); |
| 231 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 232 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 233 | static jint getStrokeJoin(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 234 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 235 | return static_cast<jint>(obj->getStrokeJoin()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 237 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 238 | static void setStrokeJoin(JNIEnv* env, jobject clazz, jlong objHandle, jint joinHandle) { |
| 239 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 240 | SkPaint::Join join = (SkPaint::Join) joinHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | obj->setStrokeJoin(join); |
| 242 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 243 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 244 | static jboolean getFillPath(JNIEnv* env, jobject clazz, jlong objHandle, jlong srcHandle, jlong dstHandle) { |
| 245 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 246 | SkPath* src = reinterpret_cast<SkPath*>(srcHandle); |
| 247 | SkPath* dst = reinterpret_cast<SkPath*>(dstHandle); |
| 248 | return obj->getFillPath(*src, dst) ? JNI_TRUE : JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 250 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 251 | static jlong setShader(JNIEnv* env, jobject clazz, jlong objHandle, jlong shaderHandle) { |
| 252 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 253 | SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle); |
| 254 | return reinterpret_cast<jlong>(obj->setShader(shader)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 256 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 257 | static jlong setColorFilter(JNIEnv* env, jobject clazz, jlong objHandle, jlong filterHandle) { |
| 258 | SkPaint* obj = reinterpret_cast<SkPaint *>(objHandle); |
| 259 | SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(filterHandle); |
| 260 | return reinterpret_cast<jlong>(obj->setColorFilter(filter)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 262 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 263 | static jlong setXfermode(JNIEnv* env, jobject clazz, jlong objHandle, jlong xfermodeHandle) { |
| 264 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 265 | SkXfermode* xfermode = reinterpret_cast<SkXfermode*>(xfermodeHandle); |
| 266 | return reinterpret_cast<jlong>(obj->setXfermode(xfermode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 268 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 269 | static jlong setPathEffect(JNIEnv* env, jobject clazz, jlong objHandle, jlong effectHandle) { |
| 270 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 271 | SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle); |
| 272 | return reinterpret_cast<jlong>(obj->setPathEffect(effect)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 274 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 275 | static jlong setMaskFilter(JNIEnv* env, jobject clazz, jlong objHandle, jlong maskfilterHandle) { |
| 276 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 277 | SkMaskFilter* maskfilter = reinterpret_cast<SkMaskFilter*>(maskfilterHandle); |
| 278 | return reinterpret_cast<jlong>(obj->setMaskFilter(maskfilter)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 280 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 281 | static jlong setTypeface(JNIEnv* env, jobject clazz, jlong objHandle, jlong typefaceHandle) { |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 282 | #ifndef USE_MINIKIN |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 283 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 284 | SkTypeface* typeface = reinterpret_cast<SkTypeface*>(typefaceHandle); |
| 285 | return reinterpret_cast<jlong>(obj->setTypeface(typeface)); |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 286 | #else |
| 287 | // TODO(raph): not yet implemented |
| 288 | return NULL; |
| 289 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 291 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 292 | static jlong setRasterizer(JNIEnv* env, jobject clazz, jlong objHandle, jlong rasterizerHandle) { |
| 293 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 294 | SkRasterizer* rasterizer = reinterpret_cast<SkRasterizer*>(rasterizerHandle); |
| 295 | return reinterpret_cast<jlong>(obj->setRasterizer(rasterizer)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 297 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 298 | static jint getTextAlign(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 299 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 300 | return static_cast<jint>(obj->getTextAlign()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 302 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 303 | static void setTextAlign(JNIEnv* env, jobject clazz, jlong objHandle, jint alignHandle) { |
| 304 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
| 305 | SkPaint::Align align = static_cast<SkPaint::Align>(alignHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | obj->setTextAlign(align); |
| 307 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 308 | |
Billy Hewlett | ac1cbaf | 2012-07-18 09:51:45 -0700 | [diff] [blame] | 309 | // generate bcp47 identifier for the supplied locale |
| 310 | static void toLanguageTag(char* output, size_t outSize, |
| 311 | const char* locale) { |
| 312 | if (output == NULL || outSize <= 0) { |
| 313 | return; |
| 314 | } |
| 315 | if (locale == NULL) { |
| 316 | output[0] = '\0'; |
| 317 | return; |
| 318 | } |
| 319 | char canonicalChars[ULOC_FULLNAME_CAPACITY]; |
| 320 | UErrorCode uErr = U_ZERO_ERROR; |
| 321 | uloc_canonicalize(locale, canonicalChars, ULOC_FULLNAME_CAPACITY, |
| 322 | &uErr); |
| 323 | if (U_SUCCESS(uErr)) { |
| 324 | char likelyChars[ULOC_FULLNAME_CAPACITY]; |
| 325 | uErr = U_ZERO_ERROR; |
| 326 | uloc_addLikelySubtags(canonicalChars, likelyChars, |
| 327 | ULOC_FULLNAME_CAPACITY, &uErr); |
| 328 | if (U_SUCCESS(uErr)) { |
| 329 | uErr = U_ZERO_ERROR; |
| 330 | uloc_toLanguageTag(likelyChars, output, outSize, FALSE, &uErr); |
| 331 | if (U_SUCCESS(uErr)) { |
| 332 | return; |
| 333 | } else { |
| 334 | ALOGD("uloc_toLanguageTag(\"%s\") failed: %s", likelyChars, |
| 335 | u_errorName(uErr)); |
| 336 | } |
| 337 | } else { |
| 338 | ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s", |
| 339 | canonicalChars, u_errorName(uErr)); |
| 340 | } |
| 341 | } else { |
| 342 | ALOGD("uloc_canonicalize(\"%s\") failed: %s", locale, |
| 343 | u_errorName(uErr)); |
| 344 | } |
| 345 | // unable to build a proper language identifier |
| 346 | output[0] = '\0'; |
| 347 | } |
| 348 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 349 | static void setTextLocale(JNIEnv* env, jobject clazz, jlong objHandle, jstring locale) { |
| 350 | SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle); |
Billy Hewlett | ac1cbaf | 2012-07-18 09:51:45 -0700 | [diff] [blame] | 351 | ScopedUtfChars localeChars(env, locale); |
| 352 | char langTag[ULOC_FULLNAME_CAPACITY]; |
| 353 | toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, localeChars.c_str()); |
Derek Sollenberger | d7a8077 | 2013-05-28 10:44:26 -0400 | [diff] [blame] | 354 | |
| 355 | SkPaintOptionsAndroid paintOpts = obj->getPaintOptionsAndroid(); |
| 356 | paintOpts.setLanguage(langTag); |
| 357 | obj->setPaintOptionsAndroid(paintOpts); |
Fabrice Di Meglio | 517825f | 2012-04-06 16:53:48 -0700 | [diff] [blame] | 358 | } |
| 359 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | static jfloat getTextSize(JNIEnv* env, jobject paint) { |
| 361 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 362 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSize()); |
| 363 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 364 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | static void setTextSize(JNIEnv* env, jobject paint, jfloat textSize) { |
| 366 | NPE_CHECK_RETURN_VOID(env, paint); |
| 367 | GraphicsJNI::getNativePaint(env, paint)->setTextSize(SkFloatToScalar(textSize)); |
| 368 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 369 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | static jfloat getTextScaleX(JNIEnv* env, jobject paint) { |
| 371 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 372 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextScaleX()); |
| 373 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 374 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | static void setTextScaleX(JNIEnv* env, jobject paint, jfloat scaleX) { |
| 376 | NPE_CHECK_RETURN_VOID(env, paint); |
| 377 | GraphicsJNI::getNativePaint(env, paint)->setTextScaleX(SkFloatToScalar(scaleX)); |
| 378 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 379 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | static jfloat getTextSkewX(JNIEnv* env, jobject paint) { |
| 381 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 382 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSkewX()); |
| 383 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 384 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | static void setTextSkewX(JNIEnv* env, jobject paint, jfloat skewX) { |
| 386 | NPE_CHECK_RETURN_VOID(env, paint); |
| 387 | GraphicsJNI::getNativePaint(env, paint)->setTextSkewX(SkFloatToScalar(skewX)); |
| 388 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 389 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | static jfloat ascent(JNIEnv* env, jobject paint) { |
| 391 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 392 | SkPaint::FontMetrics metrics; |
| 393 | (void)GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics); |
| 394 | return SkScalarToFloat(metrics.fAscent); |
| 395 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 396 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | static jfloat descent(JNIEnv* env, jobject paint) { |
| 398 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 399 | SkPaint::FontMetrics metrics; |
| 400 | (void)GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics); |
| 401 | return SkScalarToFloat(metrics.fDescent); |
| 402 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | static jfloat getFontMetrics(JNIEnv* env, jobject paint, jobject metricsObj) { |
| 405 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 406 | SkPaint::FontMetrics metrics; |
| 407 | SkScalar spacing = GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics); |
| 408 | |
| 409 | if (metricsObj) { |
| 410 | SkASSERT(env->IsInstanceOf(metricsObj, gFontMetrics_class)); |
| 411 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.top, SkScalarToFloat(metrics.fTop)); |
| 412 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.ascent, SkScalarToFloat(metrics.fAscent)); |
| 413 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.descent, SkScalarToFloat(metrics.fDescent)); |
| 414 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.bottom, SkScalarToFloat(metrics.fBottom)); |
| 415 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.leading, SkScalarToFloat(metrics.fLeading)); |
| 416 | } |
| 417 | return SkScalarToFloat(spacing); |
| 418 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 419 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | static jint getFontMetricsInt(JNIEnv* env, jobject paint, jobject metricsObj) { |
| 421 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 422 | SkPaint::FontMetrics metrics; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 423 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics); |
| 425 | int ascent = SkScalarRound(metrics.fAscent); |
| 426 | int descent = SkScalarRound(metrics.fDescent); |
| 427 | int leading = SkScalarRound(metrics.fLeading); |
| 428 | |
| 429 | if (metricsObj) { |
| 430 | SkASSERT(env->IsInstanceOf(metricsObj, gFontMetricsInt_class)); |
| 431 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.top, SkScalarFloor(metrics.fTop)); |
| 432 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.ascent, ascent); |
| 433 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.descent, descent); |
| 434 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.bottom, SkScalarCeil(metrics.fBottom)); |
| 435 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.leading, leading); |
| 436 | } |
| 437 | return descent - ascent + leading; |
| 438 | } |
| 439 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 440 | static jfloat measureText_CIII(JNIEnv* env, jobject jpaint, jcharArray text, jint index, jint count, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 441 | jint bidiFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 443 | NPE_CHECK_RETURN_ZERO(env, text); |
| 444 | |
| 445 | size_t textLength = env->GetArrayLength(text); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | if ((index | count) < 0 || (size_t)(index + count) > textLength) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 447 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | return 0; |
| 449 | } |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 450 | if (count == 0) { |
| 451 | return 0; |
| 452 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 454 | SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 456 | jfloat result = 0; |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 457 | |
Fabrice Di Meglio | 8fb5071 | 2011-05-13 18:51:21 -0700 | [diff] [blame] | 458 | TextLayout::getTextRunAdvances(paint, textArray, index, count, textLength, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 459 | bidiFlags, NULL /* dont need all advances */, &result); |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 460 | |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 461 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT); |
| 462 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 464 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 465 | static jfloat measureText_StringIII(JNIEnv* env, jobject jpaint, jstring text, jint start, jint end, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 466 | jint bidiFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 468 | NPE_CHECK_RETURN_ZERO(env, text); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 469 | |
Fabrice Di Meglio | 51f383d | 2011-09-13 15:57:58 -0700 | [diff] [blame] | 470 | size_t textLength = env->GetStringLength(text); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | int count = end - start; |
Fabrice Di Meglio | 51f383d | 2011-09-13 15:57:58 -0700 | [diff] [blame] | 472 | if ((start | count) < 0 || (size_t)end > textLength) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 473 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | return 0; |
| 475 | } |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 476 | if (count == 0) { |
| 477 | return 0; |
| 478 | } |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 479 | |
| 480 | const jchar* textArray = env->GetStringChars(text, NULL); |
| 481 | SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 482 | jfloat width = 0; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 483 | |
Fabrice Di Meglio | 51f383d | 2011-09-13 15:57:58 -0700 | [diff] [blame] | 484 | TextLayout::getTextRunAdvances(paint, textArray, start, count, textLength, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 485 | bidiFlags, NULL /* dont need all advances */, &width); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 486 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | env->ReleaseStringChars(text, textArray); |
| 488 | return width; |
| 489 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 490 | |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 491 | static jfloat measureText_StringI(JNIEnv* env, jobject jpaint, jstring text, jint bidiFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 493 | NPE_CHECK_RETURN_ZERO(env, text); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 494 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | size_t textLength = env->GetStringLength(text); |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 496 | if (textLength == 0) { |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | const jchar* textArray = env->GetStringChars(text, NULL); |
| 501 | SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 502 | jfloat width = 0; |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 503 | |
Fabrice Di Meglio | 8fb5071 | 2011-05-13 18:51:21 -0700 | [diff] [blame] | 504 | TextLayout::getTextRunAdvances(paint, textArray, 0, textLength, textLength, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 505 | bidiFlags, NULL /* dont need all advances */, &width); |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 506 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | env->ReleaseStringChars(text, textArray); |
| 508 | return width; |
| 509 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 510 | |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 511 | static int dotextwidths(JNIEnv* env, SkPaint* paint, const jchar text[], int count, jfloatArray widths, |
| 512 | jint bidiFlags) { |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 513 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 514 | NPE_CHECK_RETURN_ZERO(env, text); |
| 515 | |
| 516 | if (count < 0 || !widths) { |
| 517 | doThrowAIOOBE(env); |
| 518 | return 0; |
| 519 | } |
| 520 | if (count == 0) { |
| 521 | return 0; |
| 522 | } |
| 523 | size_t widthsLength = env->GetArrayLength(widths); |
| 524 | if ((size_t)count > widthsLength) { |
| 525 | doThrowAIOOBE(env); |
| 526 | return 0; |
| 527 | } |
| 528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | AutoJavaFloatArray autoWidths(env, widths, count); |
| 530 | jfloat* widthsArray = autoWidths.ptr(); |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 531 | |
Fabrice Di Meglio | 8fb5071 | 2011-05-13 18:51:21 -0700 | [diff] [blame] | 532 | TextLayout::getTextRunAdvances(paint, text, 0, count, count, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 533 | bidiFlags, widthsArray, NULL /* dont need totalAdvance */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | return count; |
| 536 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 537 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 538 | static jint getTextWidths___CIII_F(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text, |
| 539 | jint index, jint count, jint bidiFlags, jfloatArray widths) { |
| 540 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 542 | count = dotextwidths(env, paint, textArray + index, count, widths, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 544 | JNI_ABORT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | return count; |
| 546 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 547 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 548 | static jint getTextWidths__StringIII_F(JNIEnv* env, jobject clazz, jlong paintHandle, jstring text, |
| 549 | jint start, jint end, jint bidiFlags, jfloatArray widths) { |
| 550 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | const jchar* textArray = env->GetStringChars(text, NULL); |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 552 | int count = dotextwidths(env, paint, textArray + start, end - start, widths, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | env->ReleaseStringChars(text, textArray); |
| 554 | return count; |
| 555 | } |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 556 | |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 557 | static int doTextGlyphs(JNIEnv* env, SkPaint* paint, const jchar* text, jint start, jint count, |
| 558 | jint contextCount, jint flags, jcharArray glyphs) { |
| 559 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 560 | NPE_CHECK_RETURN_ZERO(env, text); |
| 561 | |
| 562 | if ((start | count | contextCount) < 0 || contextCount < count || !glyphs) { |
| 563 | doThrowAIOOBE(env); |
| 564 | return 0; |
| 565 | } |
| 566 | if (count == 0) { |
| 567 | return 0; |
| 568 | } |
| 569 | size_t glypthsLength = env->GetArrayLength(glyphs); |
| 570 | if ((size_t)count > glypthsLength) { |
| 571 | doThrowAIOOBE(env); |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | jchar* glyphsArray = env->GetCharArrayElements(glyphs, NULL); |
| 576 | |
| 577 | sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint, |
| 578 | text, start, count, contextCount, flags); |
| 579 | const jchar* shapedGlyphs = value->getGlyphs(); |
| 580 | size_t glyphsCount = value->getGlyphsCount(); |
| 581 | memcpy(glyphsArray, shapedGlyphs, sizeof(jchar) * glyphsCount); |
| 582 | |
| 583 | env->ReleaseCharArrayElements(glyphs, glyphsArray, JNI_ABORT); |
| 584 | return glyphsCount; |
| 585 | } |
| 586 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 587 | static jint getTextGlyphs__StringIIIII_C(JNIEnv* env, jobject clazz, jlong paintHandle, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 588 | jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint flags, |
| 589 | jcharArray glyphs) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 590 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 591 | const jchar* textArray = env->GetStringChars(text, NULL); |
| 592 | int count = doTextGlyphs(env, paint, textArray + contextStart, start - contextStart, |
| 593 | end - start, contextEnd - contextStart, flags, glyphs); |
| 594 | env->ReleaseStringChars(text, textArray); |
| 595 | return count; |
| 596 | } |
| 597 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 598 | static jfloat doTextRunAdvances(JNIEnv *env, SkPaint *paint, const jchar *text, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 599 | jint start, jint count, jint contextCount, jint flags, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 600 | jfloatArray advances, jint advancesIndex) { |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 601 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 602 | NPE_CHECK_RETURN_ZERO(env, text); |
| 603 | |
| 604 | if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) { |
| 605 | doThrowAIOOBE(env); |
| 606 | return 0; |
| 607 | } |
| 608 | if (count == 0) { |
| 609 | return 0; |
| 610 | } |
| 611 | if (advances) { |
| 612 | size_t advancesLength = env->GetArrayLength(advances); |
| 613 | if ((size_t)count > advancesLength) { |
| 614 | doThrowAIOOBE(env); |
| 615 | return 0; |
| 616 | } |
| 617 | } |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 618 | jfloat advancesArray[count]; |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 619 | jfloat totalAdvance = 0; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 620 | |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 621 | TextLayout::getTextRunAdvances(paint, text, start, count, contextCount, flags, |
Fabrice Di Meglio | 79df532 | 2011-09-19 15:17:56 -0700 | [diff] [blame] | 622 | advancesArray, &totalAdvance); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 623 | |
| 624 | if (advances != NULL) { |
| 625 | env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray); |
| 626 | } |
| 627 | return totalAdvance; |
| 628 | } |
| 629 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 630 | static jfloat getTextRunAdvances___CIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 631 | jcharArray text, jint index, jint count, jint contextIndex, jint contextCount, |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 632 | jint flags, jfloatArray advances, jint advancesIndex) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 633 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 634 | jchar* textArray = env->GetCharArrayElements(text, NULL); |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 635 | jfloat result = doTextRunAdvances(env, paint, textArray + contextIndex, |
| 636 | index - contextIndex, count, contextCount, flags, advances, advancesIndex); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 637 | env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); |
| 638 | return result; |
| 639 | } |
| 640 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 641 | static jfloat getTextRunAdvances__StringIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 642 | jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint flags, |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 643 | jfloatArray advances, jint advancesIndex) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 644 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 645 | const jchar* textArray = env->GetStringChars(text, NULL); |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 646 | jfloat result = doTextRunAdvances(env, paint, textArray + contextStart, |
| 647 | start - contextStart, end - start, contextEnd - contextStart, flags, |
| 648 | advances, advancesIndex); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 649 | env->ReleaseStringChars(text, textArray); |
| 650 | return result; |
| 651 | } |
| 652 | |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 653 | static jint doTextRunCursor(JNIEnv *env, SkPaint* paint, const jchar *text, jint start, |
Fabrice Di Meglio | a4bf811 | 2013-03-15 11:22:29 -0700 | [diff] [blame] | 654 | jint count, jint flags, jint offset, jint opt) { |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 655 | jfloat scalarArray[count]; |
Fabrice Di Meglio | 9c418db | 2011-09-18 12:54:38 -0700 | [diff] [blame] | 656 | |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 657 | TextLayout::getTextRunAdvances(paint, text, start, count, start + count, flags, |
Fabrice Di Meglio | 79df532 | 2011-09-19 15:17:56 -0700 | [diff] [blame] | 658 | scalarArray, NULL /* dont need totalAdvance */); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 659 | |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 660 | jint pos = offset - start; |
| 661 | switch (opt) { |
| 662 | case AFTER: |
| 663 | if (pos < count) { |
| 664 | pos += 1; |
| 665 | } |
| 666 | // fall through |
| 667 | case AT_OR_AFTER: |
| 668 | while (pos < count && scalarArray[pos] == 0) { |
| 669 | ++pos; |
| 670 | } |
| 671 | break; |
| 672 | case BEFORE: |
| 673 | if (pos > 0) { |
| 674 | --pos; |
| 675 | } |
| 676 | // fall through |
| 677 | case AT_OR_BEFORE: |
| 678 | while (pos > 0 && scalarArray[pos] == 0) { |
| 679 | --pos; |
| 680 | } |
| 681 | break; |
| 682 | case AT: |
| 683 | default: |
| 684 | if (scalarArray[pos] == 0) { |
| 685 | pos = -1; |
| 686 | } |
| 687 | break; |
| 688 | } |
| 689 | |
| 690 | if (pos != -1) { |
| 691 | pos += start; |
| 692 | } |
| 693 | |
| 694 | return pos; |
| 695 | } |
| 696 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 697 | static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text, |
Fabrice Di Meglio | a4bf811 | 2013-03-15 11:22:29 -0700 | [diff] [blame] | 698 | jint contextStart, jint contextCount, jint flags, jint offset, jint cursorOpt) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 699 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 700 | jchar* textArray = env->GetCharArrayElements(text, NULL); |
Fabrice Di Meglio | a4bf811 | 2013-03-15 11:22:29 -0700 | [diff] [blame] | 701 | jint result = doTextRunCursor(env, paint, textArray, contextStart, contextCount, flags, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 702 | offset, cursorOpt); |
| 703 | env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); |
| 704 | return result; |
| 705 | } |
| 706 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 707 | static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, jlong paintHandle, jstring text, |
Fabrice Di Meglio | a4bf811 | 2013-03-15 11:22:29 -0700 | [diff] [blame] | 708 | jint contextStart, jint contextEnd, jint flags, jint offset, jint cursorOpt) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 709 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 710 | const jchar* textArray = env->GetStringChars(text, NULL); |
| 711 | jint result = doTextRunCursor(env, paint, textArray, contextStart, |
Fabrice Di Meglio | a4bf811 | 2013-03-15 11:22:29 -0700 | [diff] [blame] | 712 | contextEnd - contextStart, flags, offset, cursorOpt); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 713 | env->ReleaseStringChars(text, textArray); |
| 714 | return result; |
| 715 | } |
| 716 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 717 | static void getTextPath(JNIEnv* env, SkPaint* paint, const jchar* text, jint count, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 718 | jint bidiFlags, jfloat x, jfloat y, SkPath *path) { |
| 719 | TextLayout::getTextPath(paint, text, count, bidiFlags, x, y, path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 721 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 722 | static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle, jint bidiFlags, |
| 723 | jcharArray text, jint index, jint count, jfloat x, jfloat y, jlong pathHandle) { |
| 724 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
| 725 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 726 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 727 | getTextPath(env, paint, textArray + index, count, bidiFlags, x, y, path); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 728 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT); |
| 729 | } |
| 730 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 731 | static void getTextPath__String(JNIEnv* env, jobject clazz, jlong paintHandle, jint bidiFlags, |
| 732 | jstring text, jint start, jint end, jfloat x, jfloat y, jlong pathHandle) { |
| 733 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
| 734 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | const jchar* textArray = env->GetStringChars(text, NULL); |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 736 | getTextPath(env, paint, textArray + start, end - start, bidiFlags, x, y, path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | env->ReleaseStringChars(text, textArray); |
| 738 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 739 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | static void setShadowLayer(JNIEnv* env, jobject jpaint, jfloat radius, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 741 | jfloat dx, jfloat dy, jint color) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | NPE_CHECK_RETURN_VOID(env, jpaint); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 743 | |
| 744 | SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | if (radius <= 0) { |
| 746 | paint->setLooper(NULL); |
| 747 | } |
| 748 | else { |
| 749 | paint->setLooper(new SkBlurDrawLooper(SkFloatToScalar(radius), |
| 750 | SkFloatToScalar(dx), |
| 751 | SkFloatToScalar(dy), |
| 752 | (SkColor)color))->unref(); |
| 753 | } |
| 754 | } |
| 755 | |
Fabrice Di Meglio | bd901de | 2012-01-20 17:41:55 -0800 | [diff] [blame] | 756 | static int breakText(JNIEnv* env, SkPaint& paint, const jchar text[], |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 757 | int count, float maxWidth, jint bidiFlags, jfloatArray jmeasured, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | SkPaint::TextBufferDirection tbd) { |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 759 | sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(&paint, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 760 | text, 0, count, count, bidiFlags); |
Fabrice Di Meglio | bd901de | 2012-01-20 17:41:55 -0800 | [diff] [blame] | 761 | if (value == NULL) { |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 762 | return 0; |
Fabrice Di Meglio | bd901de | 2012-01-20 17:41:55 -0800 | [diff] [blame] | 763 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | SkScalar measured; |
Fabrice Di Meglio | bd901de | 2012-01-20 17:41:55 -0800 | [diff] [blame] | 765 | size_t bytes = paint.breakText(value->getGlyphs(), value->getGlyphsCount() << 1, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | SkFloatToScalar(maxWidth), &measured, tbd); |
| 767 | SkASSERT((bytes & 1) == 0); |
| 768 | |
| 769 | if (jmeasured && env->GetArrayLength(jmeasured) > 0) { |
| 770 | AutoJavaFloatArray autoMeasured(env, jmeasured, 1); |
| 771 | jfloat* array = autoMeasured.ptr(); |
| 772 | array[0] = SkScalarToFloat(measured); |
| 773 | } |
| 774 | return bytes >> 1; |
| 775 | } |
| 776 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 777 | static jint breakTextC(JNIEnv* env, jobject jpaint, jcharArray jtext, |
| 778 | jint index, jint count, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 780 | NPE_CHECK_RETURN_ZERO(env, jtext); |
| 781 | |
| 782 | SkPaint::TextBufferDirection tbd; |
| 783 | if (count < 0) { |
| 784 | tbd = SkPaint::kBackward_TextBufferDirection; |
| 785 | count = -count; |
| 786 | } |
| 787 | else { |
| 788 | tbd = SkPaint::kForward_TextBufferDirection; |
| 789 | } |
| 790 | |
| 791 | if ((index < 0) || (index + count > env->GetArrayLength(jtext))) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 792 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
| 797 | const jchar* text = env->GetCharArrayElements(jtext, NULL); |
| 798 | count = breakText(env, *paint, text + index, count, maxWidth, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 799 | bidiFlags, jmeasuredWidth, tbd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text), |
| 801 | JNI_ABORT); |
| 802 | return count; |
| 803 | } |
| 804 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 805 | static jint breakTextS(JNIEnv* env, jobject jpaint, jstring jtext, |
| 806 | jboolean forwards, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 808 | NPE_CHECK_RETURN_ZERO(env, jtext); |
| 809 | |
| 810 | SkPaint::TextBufferDirection tbd = forwards ? |
| 811 | SkPaint::kForward_TextBufferDirection : |
| 812 | SkPaint::kBackward_TextBufferDirection; |
| 813 | |
| 814 | SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
| 815 | int count = env->GetStringLength(jtext); |
| 816 | const jchar* text = env->GetStringChars(jtext, NULL); |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 817 | count = breakText(env, *paint, text, count, maxWidth, bidiFlags, jmeasuredWidth, tbd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | env->ReleaseStringChars(jtext, text); |
| 819 | return count; |
| 820 | } |
| 821 | |
| 822 | static void doTextBounds(JNIEnv* env, const jchar* text, int count, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 823 | jobject bounds, const SkPaint& paint, jint bidiFlags) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 824 | SkRect r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | SkIRect ir; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 826 | |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 827 | sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(&paint, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 828 | text, 0, count, count, bidiFlags); |
Fabrice Di Meglio | bd901de | 2012-01-20 17:41:55 -0800 | [diff] [blame] | 829 | if (value == NULL) { |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 830 | return; |
Fabrice Di Meglio | bd901de | 2012-01-20 17:41:55 -0800 | [diff] [blame] | 831 | } |
Fabrice Di Meglio | bd901de | 2012-01-20 17:41:55 -0800 | [diff] [blame] | 832 | paint.measureText(value->getGlyphs(), value->getGlyphsCount() << 1, &r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | r.roundOut(&ir); |
| 834 | GraphicsJNI::irect_to_jrect(ir, env, bounds); |
| 835 | } |
| 836 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 837 | static void getStringBounds(JNIEnv* env, jobject, jlong paintHandle, |
| 838 | jstring text, jint start, jint end, jint bidiFlags, jobject bounds) { |
| 839 | const SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle);; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | const jchar* textArray = env->GetStringChars(text, NULL); |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 841 | doTextBounds(env, textArray + start, end - start, bounds, *paint, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | env->ReleaseStringChars(text, textArray); |
| 843 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 844 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 845 | static void getCharArrayBounds(JNIEnv* env, jobject, jlong paintHandle, |
| 846 | jcharArray text, jint index, jint count, jint bidiFlags, jobject bounds) { |
| 847 | const SkPaint* paint = reinterpret_cast<SkPaint*>(paintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 849 | doTextBounds(env, textArray + index, count, bounds, *paint, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), |
| 851 | JNI_ABORT); |
| 852 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 853 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | }; |
| 855 | |
| 856 | static JNINativeMethod methods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 857 | {"finalizer", "(J)V", (void*) SkPaintGlue::finalizer}, |
| 858 | {"native_init","()J", (void*) SkPaintGlue::init}, |
| 859 | {"native_initWithPaint","(J)J", (void*) SkPaintGlue::initWithPaint}, |
| 860 | {"native_reset","(J)V", (void*) SkPaintGlue::reset}, |
| 861 | {"native_set","(JJ)V", (void*) SkPaintGlue::assign}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | {"getFlags","()I", (void*) SkPaintGlue::getFlags}, |
| 863 | {"setFlags","(I)V", (void*) SkPaintGlue::setFlags}, |
Dianne Hackborn | f43fa57 | 2011-08-12 18:59:39 -0700 | [diff] [blame] | 864 | {"getHinting","()I", (void*) SkPaintGlue::getHinting}, |
| 865 | {"setHinting","(I)V", (void*) SkPaintGlue::setHinting}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | {"setAntiAlias","(Z)V", (void*) SkPaintGlue::setAntiAlias}, |
| 867 | {"setSubpixelText","(Z)V", (void*) SkPaintGlue::setSubpixelText}, |
| 868 | {"setLinearText","(Z)V", (void*) SkPaintGlue::setLinearText}, |
| 869 | {"setUnderlineText","(Z)V", (void*) SkPaintGlue::setUnderlineText}, |
| 870 | {"setStrikeThruText","(Z)V", (void*) SkPaintGlue::setStrikeThruText}, |
| 871 | {"setFakeBoldText","(Z)V", (void*) SkPaintGlue::setFakeBoldText}, |
| 872 | {"setFilterBitmap","(Z)V", (void*) SkPaintGlue::setFilterBitmap}, |
| 873 | {"setDither","(Z)V", (void*) SkPaintGlue::setDither}, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 874 | {"native_getStyle","(J)I", (void*) SkPaintGlue::getStyle}, |
| 875 | {"native_setStyle","(JI)V", (void*) SkPaintGlue::setStyle}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | {"getColor","()I", (void*) SkPaintGlue::getColor}, |
| 877 | {"setColor","(I)V", (void*) SkPaintGlue::setColor}, |
| 878 | {"getAlpha","()I", (void*) SkPaintGlue::getAlpha}, |
| 879 | {"setAlpha","(I)V", (void*) SkPaintGlue::setAlpha}, |
| 880 | {"getStrokeWidth","()F", (void*) SkPaintGlue::getStrokeWidth}, |
| 881 | {"setStrokeWidth","(F)V", (void*) SkPaintGlue::setStrokeWidth}, |
| 882 | {"getStrokeMiter","()F", (void*) SkPaintGlue::getStrokeMiter}, |
| 883 | {"setStrokeMiter","(F)V", (void*) SkPaintGlue::setStrokeMiter}, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 884 | {"native_getStrokeCap","(J)I", (void*) SkPaintGlue::getStrokeCap}, |
| 885 | {"native_setStrokeCap","(JI)V", (void*) SkPaintGlue::setStrokeCap}, |
| 886 | {"native_getStrokeJoin","(J)I", (void*) SkPaintGlue::getStrokeJoin}, |
| 887 | {"native_setStrokeJoin","(JI)V", (void*) SkPaintGlue::setStrokeJoin}, |
| 888 | {"native_getFillPath","(JJJ)Z", (void*) SkPaintGlue::getFillPath}, |
| 889 | {"native_setShader","(JJ)J", (void*) SkPaintGlue::setShader}, |
| 890 | {"native_setColorFilter","(JJ)J", (void*) SkPaintGlue::setColorFilter}, |
| 891 | {"native_setXfermode","(JJ)J", (void*) SkPaintGlue::setXfermode}, |
| 892 | {"native_setPathEffect","(JJ)J", (void*) SkPaintGlue::setPathEffect}, |
| 893 | {"native_setMaskFilter","(JJ)J", (void*) SkPaintGlue::setMaskFilter}, |
| 894 | {"native_setTypeface","(JJ)J", (void*) SkPaintGlue::setTypeface}, |
| 895 | {"native_setRasterizer","(JJ)J", (void*) SkPaintGlue::setRasterizer}, |
| 896 | {"native_getTextAlign","(J)I", (void*) SkPaintGlue::getTextAlign}, |
| 897 | {"native_setTextAlign","(JI)V", (void*) SkPaintGlue::setTextAlign}, |
| 898 | {"native_setTextLocale","(JLjava/lang/String;)V", (void*) SkPaintGlue::setTextLocale}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | {"getTextSize","()F", (void*) SkPaintGlue::getTextSize}, |
| 900 | {"setTextSize","(F)V", (void*) SkPaintGlue::setTextSize}, |
| 901 | {"getTextScaleX","()F", (void*) SkPaintGlue::getTextScaleX}, |
| 902 | {"setTextScaleX","(F)V", (void*) SkPaintGlue::setTextScaleX}, |
| 903 | {"getTextSkewX","()F", (void*) SkPaintGlue::getTextSkewX}, |
| 904 | {"setTextSkewX","(F)V", (void*) SkPaintGlue::setTextSkewX}, |
| 905 | {"ascent","()F", (void*) SkPaintGlue::ascent}, |
| 906 | {"descent","()F", (void*) SkPaintGlue::descent}, |
| 907 | {"getFontMetrics", "(Landroid/graphics/Paint$FontMetrics;)F", (void*)SkPaintGlue::getFontMetrics}, |
| 908 | {"getFontMetricsInt", "(Landroid/graphics/Paint$FontMetricsInt;)I", (void*)SkPaintGlue::getFontMetricsInt}, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 909 | {"native_measureText","([CIII)F", (void*) SkPaintGlue::measureText_CIII}, |
| 910 | {"native_measureText","(Ljava/lang/String;I)F", (void*) SkPaintGlue::measureText_StringI}, |
| 911 | {"native_measureText","(Ljava/lang/String;III)F", (void*) SkPaintGlue::measureText_StringIII}, |
| 912 | {"native_breakText","([CIIFI[F)I", (void*) SkPaintGlue::breakTextC}, |
| 913 | {"native_breakText","(Ljava/lang/String;ZFI[F)I", (void*) SkPaintGlue::breakTextS}, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 914 | {"native_getTextWidths","(J[CIII[F)I", (void*) SkPaintGlue::getTextWidths___CIII_F}, |
| 915 | {"native_getTextWidths","(JLjava/lang/String;III[F)I", (void*) SkPaintGlue::getTextWidths__StringIII_F}, |
| 916 | {"native_getTextRunAdvances","(J[CIIIII[FI)F", |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 917 | (void*) SkPaintGlue::getTextRunAdvances___CIIIII_FI}, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 918 | {"native_getTextRunAdvances","(JLjava/lang/String;IIIII[FI)F", |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 919 | (void*) SkPaintGlue::getTextRunAdvances__StringIIIII_FI}, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 920 | |
| 921 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 922 | {"native_getTextGlyphs","(JLjava/lang/String;IIIII[C)I", |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 923 | (void*) SkPaintGlue::getTextGlyphs__StringIIIII_C}, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 924 | {"native_getTextRunCursor", "(J[CIIIII)I", (void*) SkPaintGlue::getTextRunCursor___C}, |
| 925 | {"native_getTextRunCursor", "(JLjava/lang/String;IIIII)I", |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 926 | (void*) SkPaintGlue::getTextRunCursor__String}, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 927 | {"native_getTextPath","(JI[CIIFFJ)V", (void*) SkPaintGlue::getTextPath___C}, |
| 928 | {"native_getTextPath","(JILjava/lang/String;IIFFJ)V", (void*) SkPaintGlue::getTextPath__String}, |
| 929 | {"nativeGetStringBounds", "(JLjava/lang/String;IIILandroid/graphics/Rect;)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | (void*) SkPaintGlue::getStringBounds }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 931 | {"nativeGetCharArrayBounds", "(J[CIIILandroid/graphics/Rect;)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | (void*) SkPaintGlue::getCharArrayBounds }, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 933 | {"nSetShadowLayer", "(FFFI)V", (void*)SkPaintGlue::setShadowLayer} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | }; |
| 935 | |
| 936 | static jfieldID req_fieldID(jfieldID id) { |
| 937 | SkASSERT(id); |
| 938 | return id; |
| 939 | } |
| 940 | |
| 941 | int register_android_graphics_Paint(JNIEnv* env) { |
| 942 | gFontMetrics_class = env->FindClass("android/graphics/Paint$FontMetrics"); |
| 943 | SkASSERT(gFontMetrics_class); |
| 944 | gFontMetrics_class = (jclass)env->NewGlobalRef(gFontMetrics_class); |
| 945 | |
| 946 | gFontMetrics_fieldID.top = req_fieldID(env->GetFieldID(gFontMetrics_class, "top", "F")); |
| 947 | gFontMetrics_fieldID.ascent = req_fieldID(env->GetFieldID(gFontMetrics_class, "ascent", "F")); |
| 948 | gFontMetrics_fieldID.descent = req_fieldID(env->GetFieldID(gFontMetrics_class, "descent", "F")); |
| 949 | gFontMetrics_fieldID.bottom = req_fieldID(env->GetFieldID(gFontMetrics_class, "bottom", "F")); |
| 950 | gFontMetrics_fieldID.leading = req_fieldID(env->GetFieldID(gFontMetrics_class, "leading", "F")); |
| 951 | |
| 952 | gFontMetricsInt_class = env->FindClass("android/graphics/Paint$FontMetricsInt"); |
| 953 | SkASSERT(gFontMetricsInt_class); |
| 954 | gFontMetricsInt_class = (jclass)env->NewGlobalRef(gFontMetricsInt_class); |
| 955 | |
| 956 | gFontMetricsInt_fieldID.top = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "top", "I")); |
| 957 | gFontMetricsInt_fieldID.ascent = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "ascent", "I")); |
| 958 | gFontMetricsInt_fieldID.descent = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "descent", "I")); |
| 959 | gFontMetricsInt_fieldID.bottom = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "bottom", "I")); |
| 960 | gFontMetricsInt_fieldID.leading = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "leading", "I")); |
| 961 | |
| 962 | int result = AndroidRuntime::registerNativeMethods(env, "android/graphics/Paint", methods, |
| 963 | sizeof(methods) / sizeof(methods[0])); |
| 964 | return result; |
| 965 | } |
| 966 | |
| 967 | } |