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" |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 36 | #include "utils/Blur.h" |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 37 | |
Raph Levien | e368b6b | 2014-06-15 17:37:57 -0700 | [diff] [blame] | 38 | #include <minikin/GraphemeBreak.h> |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 39 | #include "MinikinSkia.h" |
| 40 | #include "MinikinUtils.h" |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 41 | #include "Paint.h" |
Raph Levien | 3d528c40 | 2014-06-26 09:04:54 -0700 | [diff] [blame] | 42 | #include "TypefaceImpl.h" |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 43 | |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 44 | // temporary for debugging |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 45 | #include <Caches.h> |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 46 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | namespace android { |
| 49 | |
| 50 | struct JMetricsID { |
| 51 | jfieldID top; |
| 52 | jfieldID ascent; |
| 53 | jfieldID descent; |
| 54 | jfieldID bottom; |
| 55 | jfieldID leading; |
| 56 | }; |
| 57 | |
| 58 | static jclass gFontMetrics_class; |
| 59 | static JMetricsID gFontMetrics_fieldID; |
| 60 | |
| 61 | static jclass gFontMetricsInt_class; |
| 62 | static JMetricsID gFontMetricsInt_fieldID; |
| 63 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 64 | static void defaultSettingsForAndroid(Paint* paint) { |
Fabrice Di Meglio | c511bee8 | 2012-01-05 13:30:54 -0800 | [diff] [blame] | 65 | // GlyphID encoding is required because we are using Harfbuzz shaping |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 66 | paint->setTextEncoding(Paint::kGlyphID_TextEncoding); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 69 | class PaintGlue { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | public: |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 71 | enum MoveOpt { |
| 72 | AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT |
| 73 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 75 | static void finalizer(JNIEnv* env, jobject clazz, jlong objHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 76 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | delete obj; |
| 78 | } |
| 79 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 80 | static jlong init(JNIEnv* env, jobject clazz) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 81 | Paint* obj = new Paint(); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 82 | defaultSettingsForAndroid(obj); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 83 | return reinterpret_cast<jlong>(obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 86 | static jlong initWithPaint(JNIEnv* env, jobject clazz, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 87 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 88 | Paint* obj = new Paint(*paint); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 89 | return reinterpret_cast<jlong>(obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 91 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 92 | static void reset(JNIEnv* env, jobject clazz, jlong objHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 93 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | obj->reset(); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 95 | defaultSettingsForAndroid(obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 97 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 98 | static void assign(JNIEnv* env, jobject clazz, jlong dstPaintHandle, jlong srcPaintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 99 | Paint* dst = reinterpret_cast<Paint*>(dstPaintHandle); |
| 100 | const Paint* src = reinterpret_cast<Paint*>(srcPaintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | *dst = *src; |
| 102 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 103 | |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 104 | // Equivalent to the Java Paint's FILTER_BITMAP_FLAG. |
| 105 | static const uint32_t sFilterBitmapFlag = 0x02; |
| 106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | static jint getFlags(JNIEnv* env, jobject paint) { |
| 108 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 109 | Paint* nativePaint = GraphicsJNI::getNativePaint(env, paint); |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 110 | uint32_t result = nativePaint->getFlags(); |
| 111 | result &= ~sFilterBitmapFlag; // Filtering no longer stored in this bit. Mask away. |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 112 | if (nativePaint->getFilterLevel() != Paint::kNone_FilterLevel) { |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 113 | result |= sFilterBitmapFlag; |
| 114 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 115 | return static_cast<jint>(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | static void setFlags(JNIEnv* env, jobject paint, jint flags) { |
| 119 | NPE_CHECK_RETURN_VOID(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 120 | Paint* nativePaint = GraphicsJNI::getNativePaint(env, paint); |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 121 | // Instead of modifying 0x02, change the filter level. |
| 122 | nativePaint->setFilterLevel(flags & sFilterBitmapFlag |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 123 | ? Paint::kLow_FilterLevel |
| 124 | : Paint::kNone_FilterLevel); |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 125 | // Don't pass through filter flag, which is no longer stored in paint's flags. |
| 126 | flags &= ~sFilterBitmapFlag; |
| 127 | // Use the existing value for 0x02. |
| 128 | const uint32_t existing0x02Flag = nativePaint->getFlags() & sFilterBitmapFlag; |
| 129 | flags |= existing0x02Flag; |
| 130 | nativePaint->setFlags(flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 132 | |
Dianne Hackborn | f43fa57 | 2011-08-12 18:59:39 -0700 | [diff] [blame] | 133 | static jint getHinting(JNIEnv* env, jobject paint) { |
| 134 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 135 | return GraphicsJNI::getNativePaint(env, paint)->getHinting() |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 136 | == Paint::kNo_Hinting ? 0 : 1; |
Dianne Hackborn | f43fa57 | 2011-08-12 18:59:39 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void setHinting(JNIEnv* env, jobject paint, jint mode) { |
| 140 | NPE_CHECK_RETURN_VOID(env, paint); |
| 141 | GraphicsJNI::getNativePaint(env, paint)->setHinting( |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 142 | mode == 0 ? Paint::kNo_Hinting : Paint::kNormal_Hinting); |
Dianne Hackborn | f43fa57 | 2011-08-12 18:59:39 -0700 | [diff] [blame] | 143 | } |
| 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | static void setAntiAlias(JNIEnv* env, jobject paint, jboolean aa) { |
| 146 | NPE_CHECK_RETURN_VOID(env, paint); |
| 147 | GraphicsJNI::getNativePaint(env, paint)->setAntiAlias(aa); |
| 148 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | static void setLinearText(JNIEnv* env, jobject paint, jboolean linearText) { |
| 151 | NPE_CHECK_RETURN_VOID(env, paint); |
| 152 | GraphicsJNI::getNativePaint(env, paint)->setLinearText(linearText); |
| 153 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | static void setSubpixelText(JNIEnv* env, jobject paint, jboolean subpixelText) { |
| 156 | NPE_CHECK_RETURN_VOID(env, paint); |
| 157 | GraphicsJNI::getNativePaint(env, paint)->setSubpixelText(subpixelText); |
| 158 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | static void setUnderlineText(JNIEnv* env, jobject paint, jboolean underlineText) { |
| 161 | NPE_CHECK_RETURN_VOID(env, paint); |
| 162 | GraphicsJNI::getNativePaint(env, paint)->setUnderlineText(underlineText); |
| 163 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 164 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | static void setStrikeThruText(JNIEnv* env, jobject paint, jboolean strikeThruText) { |
| 166 | NPE_CHECK_RETURN_VOID(env, paint); |
| 167 | GraphicsJNI::getNativePaint(env, paint)->setStrikeThruText(strikeThruText); |
| 168 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | static void setFakeBoldText(JNIEnv* env, jobject paint, jboolean fakeBoldText) { |
| 171 | NPE_CHECK_RETURN_VOID(env, paint); |
| 172 | GraphicsJNI::getNativePaint(env, paint)->setFakeBoldText(fakeBoldText); |
| 173 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 174 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | static void setFilterBitmap(JNIEnv* env, jobject paint, jboolean filterBitmap) { |
| 176 | NPE_CHECK_RETURN_VOID(env, paint); |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 177 | GraphicsJNI::getNativePaint(env, paint)->setFilterLevel( |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 178 | filterBitmap ? Paint::kLow_FilterLevel : Paint::kNone_FilterLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 180 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | static void setDither(JNIEnv* env, jobject paint, jboolean dither) { |
| 182 | NPE_CHECK_RETURN_VOID(env, paint); |
| 183 | GraphicsJNI::getNativePaint(env, paint)->setDither(dither); |
| 184 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 185 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 186 | static jint getStyle(JNIEnv* env, jobject clazz,jlong objHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 187 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 188 | return static_cast<jint>(obj->getStyle()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 190 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 191 | static void setStyle(JNIEnv* env, jobject clazz, jlong objHandle, jint styleHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 192 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 193 | Paint::Style style = static_cast<Paint::Style>(styleHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | obj->setStyle(style); |
| 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 jint getColor(JNIEnv* env, jobject paint) { |
| 198 | NPE_CHECK_RETURN_ZERO(env, paint); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 199 | int color; |
| 200 | color = GraphicsJNI::getNativePaint(env, paint)->getColor(); |
| 201 | return static_cast<jint>(color); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 203 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | static jint getAlpha(JNIEnv* env, jobject paint) { |
| 205 | NPE_CHECK_RETURN_ZERO(env, paint); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 206 | int alpha; |
| 207 | alpha = GraphicsJNI::getNativePaint(env, paint)->getAlpha(); |
| 208 | return static_cast<jint>(alpha); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 210 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | static void setColor(JNIEnv* env, jobject paint, jint color) { |
| 212 | NPE_CHECK_RETURN_VOID(env, paint); |
| 213 | GraphicsJNI::getNativePaint(env, paint)->setColor(color); |
| 214 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | static void setAlpha(JNIEnv* env, jobject paint, jint a) { |
| 217 | NPE_CHECK_RETURN_VOID(env, paint); |
| 218 | GraphicsJNI::getNativePaint(env, paint)->setAlpha(a); |
| 219 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | static jfloat getStrokeWidth(JNIEnv* env, jobject paint) { |
| 222 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 223 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getStrokeWidth()); |
| 224 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 225 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | static void setStrokeWidth(JNIEnv* env, jobject paint, jfloat width) { |
| 227 | NPE_CHECK_RETURN_VOID(env, paint); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 228 | GraphicsJNI::getNativePaint(env, paint)->setStrokeWidth(width); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | static jfloat getStrokeMiter(JNIEnv* env, jobject paint) { |
| 232 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 233 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getStrokeMiter()); |
| 234 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 235 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | static void setStrokeMiter(JNIEnv* env, jobject paint, jfloat miter) { |
| 237 | NPE_CHECK_RETURN_VOID(env, paint); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 238 | GraphicsJNI::getNativePaint(env, paint)->setStrokeMiter(miter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 240 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 241 | static jint getStrokeCap(JNIEnv* env, jobject clazz, jlong objHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 242 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 243 | return static_cast<jint>(obj->getStrokeCap()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 245 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 246 | static void setStrokeCap(JNIEnv* env, jobject clazz, jlong objHandle, jint capHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 247 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 248 | Paint::Cap cap = static_cast<Paint::Cap>(capHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | obj->setStrokeCap(cap); |
| 250 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 251 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 252 | static jint getStrokeJoin(JNIEnv* env, jobject clazz, jlong objHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 253 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 254 | return static_cast<jint>(obj->getStrokeJoin()); |
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 void setStrokeJoin(JNIEnv* env, jobject clazz, jlong objHandle, jint joinHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 258 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 259 | Paint::Join join = (Paint::Join) joinHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | obj->setStrokeJoin(join); |
| 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 jboolean getFillPath(JNIEnv* env, jobject clazz, jlong objHandle, jlong srcHandle, jlong dstHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 264 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 265 | SkPath* src = reinterpret_cast<SkPath*>(srcHandle); |
| 266 | SkPath* dst = reinterpret_cast<SkPath*>(dstHandle); |
| 267 | return obj->getFillPath(*src, dst) ? JNI_TRUE : JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 269 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 270 | static jlong setShader(JNIEnv* env, jobject clazz, jlong objHandle, jlong shaderHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 271 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 272 | SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle); |
| 273 | return reinterpret_cast<jlong>(obj->setShader(shader)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 275 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 276 | static jlong setColorFilter(JNIEnv* env, jobject clazz, jlong objHandle, jlong filterHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 277 | Paint* obj = reinterpret_cast<Paint *>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 278 | SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(filterHandle); |
| 279 | return reinterpret_cast<jlong>(obj->setColorFilter(filter)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 281 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 282 | static jlong setXfermode(JNIEnv* env, jobject clazz, jlong objHandle, jlong xfermodeHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 283 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 284 | SkXfermode* xfermode = reinterpret_cast<SkXfermode*>(xfermodeHandle); |
| 285 | return reinterpret_cast<jlong>(obj->setXfermode(xfermode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 287 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 288 | static jlong setPathEffect(JNIEnv* env, jobject clazz, jlong objHandle, jlong effectHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 289 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 290 | SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle); |
| 291 | return reinterpret_cast<jlong>(obj->setPathEffect(effect)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 293 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 294 | static jlong setMaskFilter(JNIEnv* env, jobject clazz, jlong objHandle, jlong maskfilterHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 295 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 296 | SkMaskFilter* maskfilter = reinterpret_cast<SkMaskFilter*>(maskfilterHandle); |
| 297 | return reinterpret_cast<jlong>(obj->setMaskFilter(maskfilter)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 299 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 300 | static jlong setTypeface(JNIEnv* env, jobject clazz, jlong objHandle, jlong typefaceHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 301 | // TODO: in Paint refactoring, set typeface on android Paint, not Paint |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 302 | return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 304 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 305 | static jlong setRasterizer(JNIEnv* env, jobject clazz, jlong objHandle, jlong rasterizerHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 306 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 307 | SkAutoTUnref<SkRasterizer> rasterizer(GraphicsJNI::refNativeRasterizer(rasterizerHandle)); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 308 | return reinterpret_cast<jlong>(obj->setRasterizer(rasterizer)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 310 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 311 | static jint getTextAlign(JNIEnv* env, jobject clazz, jlong objHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 312 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 313 | return static_cast<jint>(obj->getTextAlign()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 315 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 316 | static void setTextAlign(JNIEnv* env, jobject clazz, jlong objHandle, jint alignHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 317 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 318 | Paint::Align align = static_cast<Paint::Align>(alignHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | obj->setTextAlign(align); |
| 320 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 321 | |
Billy Hewlett | ac1cbaf | 2012-07-18 09:51:45 -0700 | [diff] [blame] | 322 | // generate bcp47 identifier for the supplied locale |
| 323 | static void toLanguageTag(char* output, size_t outSize, |
| 324 | const char* locale) { |
| 325 | if (output == NULL || outSize <= 0) { |
| 326 | return; |
| 327 | } |
| 328 | if (locale == NULL) { |
| 329 | output[0] = '\0'; |
| 330 | return; |
| 331 | } |
| 332 | char canonicalChars[ULOC_FULLNAME_CAPACITY]; |
| 333 | UErrorCode uErr = U_ZERO_ERROR; |
| 334 | uloc_canonicalize(locale, canonicalChars, ULOC_FULLNAME_CAPACITY, |
| 335 | &uErr); |
| 336 | if (U_SUCCESS(uErr)) { |
| 337 | char likelyChars[ULOC_FULLNAME_CAPACITY]; |
| 338 | uErr = U_ZERO_ERROR; |
| 339 | uloc_addLikelySubtags(canonicalChars, likelyChars, |
| 340 | ULOC_FULLNAME_CAPACITY, &uErr); |
| 341 | if (U_SUCCESS(uErr)) { |
| 342 | uErr = U_ZERO_ERROR; |
| 343 | uloc_toLanguageTag(likelyChars, output, outSize, FALSE, &uErr); |
| 344 | if (U_SUCCESS(uErr)) { |
| 345 | return; |
| 346 | } else { |
| 347 | ALOGD("uloc_toLanguageTag(\"%s\") failed: %s", likelyChars, |
| 348 | u_errorName(uErr)); |
| 349 | } |
| 350 | } else { |
| 351 | ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s", |
| 352 | canonicalChars, u_errorName(uErr)); |
| 353 | } |
| 354 | } else { |
| 355 | ALOGD("uloc_canonicalize(\"%s\") failed: %s", locale, |
| 356 | u_errorName(uErr)); |
| 357 | } |
| 358 | // unable to build a proper language identifier |
| 359 | output[0] = '\0'; |
| 360 | } |
| 361 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 362 | static void setTextLocale(JNIEnv* env, jobject clazz, jlong objHandle, jstring locale) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 363 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Billy Hewlett | ac1cbaf | 2012-07-18 09:51:45 -0700 | [diff] [blame] | 364 | ScopedUtfChars localeChars(env, locale); |
| 365 | char langTag[ULOC_FULLNAME_CAPACITY]; |
| 366 | toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, localeChars.c_str()); |
Derek Sollenberger | d7a8077 | 2013-05-28 10:44:26 -0400 | [diff] [blame] | 367 | |
| 368 | SkPaintOptionsAndroid paintOpts = obj->getPaintOptionsAndroid(); |
| 369 | paintOpts.setLanguage(langTag); |
| 370 | obj->setPaintOptionsAndroid(paintOpts); |
Fabrice Di Meglio | 517825f | 2012-04-06 16:53:48 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 373 | static jboolean isElegantTextHeight(JNIEnv* env, jobject paint) { |
| 374 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 375 | Paint* obj = GraphicsJNI::getNativePaint(env, paint); |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 376 | SkPaintOptionsAndroid paintOpts = obj->getPaintOptionsAndroid(); |
| 377 | return paintOpts.getFontVariant() == SkPaintOptionsAndroid::kElegant_Variant; |
| 378 | } |
| 379 | |
| 380 | static void setElegantTextHeight(JNIEnv* env, jobject paint, jboolean aa) { |
| 381 | NPE_CHECK_RETURN_VOID(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 382 | Paint* obj = GraphicsJNI::getNativePaint(env, paint); |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 383 | SkPaintOptionsAndroid::FontVariant variant = |
| 384 | aa ? SkPaintOptionsAndroid::kElegant_Variant : |
| 385 | SkPaintOptionsAndroid::kDefault_Variant; |
| 386 | SkPaintOptionsAndroid paintOpts = obj->getPaintOptionsAndroid(); |
| 387 | paintOpts.setFontVariant(variant); |
| 388 | obj->setPaintOptionsAndroid(paintOpts); |
| 389 | } |
| 390 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | static jfloat getTextSize(JNIEnv* env, jobject paint) { |
| 392 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 393 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSize()); |
| 394 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 395 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | static void setTextSize(JNIEnv* env, jobject paint, jfloat textSize) { |
| 397 | NPE_CHECK_RETURN_VOID(env, paint); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 398 | GraphicsJNI::getNativePaint(env, paint)->setTextSize(textSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 400 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | static jfloat getTextScaleX(JNIEnv* env, jobject paint) { |
| 402 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 403 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextScaleX()); |
| 404 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 405 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | static void setTextScaleX(JNIEnv* env, jobject paint, jfloat scaleX) { |
| 407 | NPE_CHECK_RETURN_VOID(env, paint); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 408 | GraphicsJNI::getNativePaint(env, paint)->setTextScaleX(scaleX); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 410 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | static jfloat getTextSkewX(JNIEnv* env, jobject paint) { |
| 412 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 413 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSkewX()); |
| 414 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | static void setTextSkewX(JNIEnv* env, jobject paint, jfloat skewX) { |
| 417 | NPE_CHECK_RETURN_VOID(env, paint); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 418 | GraphicsJNI::getNativePaint(env, paint)->setTextSkewX(skewX); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 420 | |
Behdad Esfahbod | fa80f74 | 2014-07-17 19:10:39 -0400 | [diff] [blame] | 421 | static jfloat getLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle) { |
| 422 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 423 | return paint->getLetterSpacing(); |
| 424 | } |
| 425 | |
| 426 | static void setLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle, jfloat letterSpacing) { |
| 427 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 428 | paint->setLetterSpacing(letterSpacing); |
| 429 | } |
| 430 | |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame^] | 431 | static void setFontFeatureSettings(JNIEnv* env, jobject clazz, jlong paintHandle, jstring settings) { |
| 432 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 433 | if (!settings) |
| 434 | paint->setFontFeatureSettings(std::string()); |
| 435 | else { |
| 436 | ScopedUtfChars settingsChars(env, settings); |
| 437 | paint->setFontFeatureSettings(std::string(settingsChars.c_str(), settingsChars.size())); |
| 438 | } |
| 439 | } |
| 440 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 441 | static SkScalar getMetricsInternal(JNIEnv* env, jobject jpaint, Paint::FontMetrics *metrics) { |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 442 | const int kElegantTop = 2500; |
| 443 | const int kElegantBottom = -1000; |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 444 | const int kElegantAscent = 1900; |
| 445 | const int kElegantDescent = -500; |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 446 | const int kElegantLeading = 0; |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 447 | Paint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 448 | TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint); |
| 449 | typeface = TypefaceImpl_resolveDefault(typeface); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 450 | FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle); |
Raph Levien | 0627ec0 | 2014-06-06 22:31:15 -0700 | [diff] [blame] | 451 | float saveSkewX = paint->getTextSkewX(); |
| 452 | bool savefakeBold = paint->isFakeBoldText(); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 453 | MinikinFontSkia::populateSkPaint(paint, baseFont.font, baseFont.fakery); |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 454 | SkScalar spacing = paint->getFontMetrics(metrics); |
Raph Levien | 0627ec0 | 2014-06-06 22:31:15 -0700 | [diff] [blame] | 455 | // The populateSkPaint call may have changed fake bold / text skew |
| 456 | // because we want to measure with those effects applied, so now |
| 457 | // restore the original settings. |
| 458 | paint->setTextSkewX(saveSkewX); |
| 459 | paint->setFakeBoldText(savefakeBold); |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 460 | SkPaintOptionsAndroid paintOpts = paint->getPaintOptionsAndroid(); |
| 461 | if (paintOpts.getFontVariant() == SkPaintOptionsAndroid::kElegant_Variant) { |
| 462 | SkScalar size = paint->getTextSize(); |
| 463 | metrics->fTop = -size * kElegantTop / 2048; |
| 464 | metrics->fBottom = -size * kElegantBottom / 2048; |
| 465 | metrics->fAscent = -size * kElegantAscent / 2048; |
| 466 | metrics->fDescent = -size * kElegantDescent / 2048; |
| 467 | metrics->fLeading = size * kElegantLeading / 2048; |
| 468 | spacing = metrics->fDescent - metrics->fAscent + metrics->fLeading; |
| 469 | } |
| 470 | return spacing; |
| 471 | } |
| 472 | |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 473 | static jfloat ascent(JNIEnv* env, jobject paint) { |
| 474 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 475 | Paint::FontMetrics metrics; |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 476 | getMetricsInternal(env, paint, &metrics); |
| 477 | return SkScalarToFloat(metrics.fAscent); |
| 478 | } |
| 479 | |
| 480 | static jfloat descent(JNIEnv* env, jobject paint) { |
| 481 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 482 | Paint::FontMetrics metrics; |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 483 | getMetricsInternal(env, paint, &metrics); |
| 484 | return SkScalarToFloat(metrics.fDescent); |
| 485 | } |
| 486 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | static jfloat getFontMetrics(JNIEnv* env, jobject paint, jobject metricsObj) { |
| 488 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 489 | Paint::FontMetrics metrics; |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 490 | SkScalar spacing = getMetricsInternal(env, paint, &metrics); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | |
| 492 | if (metricsObj) { |
| 493 | SkASSERT(env->IsInstanceOf(metricsObj, gFontMetrics_class)); |
| 494 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.top, SkScalarToFloat(metrics.fTop)); |
| 495 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.ascent, SkScalarToFloat(metrics.fAscent)); |
| 496 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.descent, SkScalarToFloat(metrics.fDescent)); |
| 497 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.bottom, SkScalarToFloat(metrics.fBottom)); |
| 498 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.leading, SkScalarToFloat(metrics.fLeading)); |
| 499 | } |
| 500 | return SkScalarToFloat(spacing); |
| 501 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 502 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | static jint getFontMetricsInt(JNIEnv* env, jobject paint, jobject metricsObj) { |
| 504 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 505 | Paint::FontMetrics metrics; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 506 | |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 507 | getMetricsInternal(env, paint, &metrics); |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 508 | int ascent = SkScalarRoundToInt(metrics.fAscent); |
| 509 | int descent = SkScalarRoundToInt(metrics.fDescent); |
| 510 | int leading = SkScalarRoundToInt(metrics.fLeading); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | |
| 512 | if (metricsObj) { |
| 513 | SkASSERT(env->IsInstanceOf(metricsObj, gFontMetricsInt_class)); |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 514 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.top, SkScalarFloorToInt(metrics.fTop)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.ascent, ascent); |
| 516 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.descent, descent); |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 517 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.bottom, SkScalarCeilToInt(metrics.fBottom)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.leading, leading); |
| 519 | } |
| 520 | return descent - ascent + leading; |
| 521 | } |
| 522 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 523 | 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] | 524 | jint bidiFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 526 | NPE_CHECK_RETURN_ZERO(env, text); |
| 527 | |
| 528 | size_t textLength = env->GetArrayLength(text); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | if ((index | count) < 0 || (size_t)(index + count) > textLength) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 530 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | return 0; |
| 532 | } |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 533 | if (count == 0) { |
| 534 | return 0; |
| 535 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 537 | Paint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 539 | jfloat result = 0; |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 540 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 541 | Layout layout; |
| 542 | TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint); |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 543 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, textArray, index, count, textLength); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 544 | result = layout.getAdvance(); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 545 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT); |
| 546 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 548 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 549 | 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] | 550 | jint bidiFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 552 | NPE_CHECK_RETURN_ZERO(env, text); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 553 | |
Fabrice Di Meglio | 51f383d | 2011-09-13 15:57:58 -0700 | [diff] [blame] | 554 | size_t textLength = env->GetStringLength(text); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | int count = end - start; |
Fabrice Di Meglio | 51f383d | 2011-09-13 15:57:58 -0700 | [diff] [blame] | 556 | if ((start | count) < 0 || (size_t)end > textLength) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 557 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | return 0; |
| 559 | } |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 560 | if (count == 0) { |
| 561 | return 0; |
| 562 | } |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 563 | |
| 564 | const jchar* textArray = env->GetStringChars(text, NULL); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 565 | Paint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 566 | jfloat width = 0; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 567 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 568 | Layout layout; |
| 569 | TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint); |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 570 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, textArray, start, count, textLength); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 571 | width = layout.getAdvance(); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 572 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | env->ReleaseStringChars(text, textArray); |
| 574 | return width; |
| 575 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 576 | |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 577 | 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] | 578 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 579 | NPE_CHECK_RETURN_ZERO(env, text); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 580 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | size_t textLength = env->GetStringLength(text); |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 582 | if (textLength == 0) { |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | const jchar* textArray = env->GetStringChars(text, NULL); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 587 | Paint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 588 | jfloat width = 0; |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 589 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 590 | Layout layout; |
| 591 | TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint); |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 592 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, textArray, 0, textLength, textLength); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 593 | width = layout.getAdvance(); |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | env->ReleaseStringChars(text, textArray); |
| 596 | return width; |
| 597 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 598 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 599 | static int dotextwidths(JNIEnv* env, Paint* paint, TypefaceImpl* typeface, const jchar text[], int count, |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 600 | jfloatArray widths, jint bidiFlags) { |
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 (count < 0 || !widths) { |
| 605 | doThrowAIOOBE(env); |
| 606 | return 0; |
| 607 | } |
| 608 | if (count == 0) { |
| 609 | return 0; |
| 610 | } |
| 611 | size_t widthsLength = env->GetArrayLength(widths); |
| 612 | if ((size_t)count > widthsLength) { |
| 613 | doThrowAIOOBE(env); |
| 614 | return 0; |
| 615 | } |
| 616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | AutoJavaFloatArray autoWidths(env, widths, count); |
| 618 | jfloat* widthsArray = autoWidths.ptr(); |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 619 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 620 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 621 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, 0, count, count); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 622 | layout.getAdvances(widthsArray); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | return count; |
| 625 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 626 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 627 | static jint getTextWidths___CIII_F(JNIEnv* env, jobject clazz, jlong paintHandle, jlong typefaceHandle, jcharArray text, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 628 | jint index, jint count, jint bidiFlags, jfloatArray widths) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 629 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 630 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 632 | count = dotextwidths(env, paint, typeface, textArray + index, count, widths, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 634 | JNI_ABORT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | return count; |
| 636 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 637 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 638 | static jint getTextWidths__StringIII_F(JNIEnv* env, jobject clazz, jlong paintHandle, jlong typefaceHandle, jstring text, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 639 | jint start, jint end, jint bidiFlags, jfloatArray widths) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 640 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 641 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | const jchar* textArray = env->GetStringChars(text, NULL); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 643 | int count = dotextwidths(env, paint, typeface, textArray + start, end - start, widths, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | env->ReleaseStringChars(text, textArray); |
| 645 | return count; |
| 646 | } |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 647 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 648 | static jfloat doTextRunAdvances(JNIEnv *env, Paint *paint, TypefaceImpl* typeface, const jchar *text, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 649 | jint start, jint count, jint contextCount, jboolean isRtl, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 650 | jfloatArray advances, jint advancesIndex) { |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 651 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 652 | NPE_CHECK_RETURN_ZERO(env, text); |
| 653 | |
| 654 | if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) { |
| 655 | doThrowAIOOBE(env); |
| 656 | return 0; |
| 657 | } |
| 658 | if (count == 0) { |
| 659 | return 0; |
| 660 | } |
| 661 | if (advances) { |
| 662 | size_t advancesLength = env->GetArrayLength(advances); |
| 663 | if ((size_t)count > advancesLength) { |
| 664 | doThrowAIOOBE(env); |
| 665 | return 0; |
| 666 | } |
| 667 | } |
Victoria Lease | 3af2a37 | 2014-03-17 16:03:28 -0700 | [diff] [blame] | 668 | jfloat* advancesArray = new jfloat[count]; |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 669 | jfloat totalAdvance = 0; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 670 | |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 671 | int bidiFlags = isRtl ? kBidi_Force_RTL : kBidi_Force_LTR; |
| 672 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 673 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 674 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, start, count, contextCount); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 675 | layout.getAdvances(advancesArray); |
| 676 | totalAdvance = layout.getAdvance(); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 677 | |
| 678 | if (advances != NULL) { |
| 679 | env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray); |
| 680 | } |
Victoria Lease | 3af2a37 | 2014-03-17 16:03:28 -0700 | [diff] [blame] | 681 | delete [] advancesArray; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 682 | return totalAdvance; |
| 683 | } |
| 684 | |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 685 | static jfloat getTextRunAdvances___CIIIIZ_FI(JNIEnv* env, jobject clazz, jlong paintHandle, |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 686 | jlong typefaceHandle, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 687 | jcharArray text, jint index, jint count, jint contextIndex, jint contextCount, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 688 | jboolean isRtl, jfloatArray advances, jint advancesIndex) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 689 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 690 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 691 | jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 692 | jfloat result = doTextRunAdvances(env, paint, typeface, textArray + contextIndex, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 693 | index - contextIndex, count, contextCount, isRtl, advances, advancesIndex); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 694 | env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); |
| 695 | return result; |
| 696 | } |
| 697 | |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 698 | static jfloat getTextRunAdvances__StringIIIIZ_FI(JNIEnv* env, jobject clazz, jlong paintHandle, |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 699 | jlong typefaceHandle, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 700 | jstring text, jint start, jint end, jint contextStart, jint contextEnd, jboolean isRtl, |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 701 | jfloatArray advances, jint advancesIndex) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 702 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 703 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 704 | const jchar* textArray = env->GetStringChars(text, NULL); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 705 | jfloat result = doTextRunAdvances(env, paint, typeface, textArray + contextStart, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 706 | start - contextStart, end - start, contextEnd - contextStart, isRtl, |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 707 | advances, advancesIndex); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 708 | env->ReleaseStringChars(text, textArray); |
| 709 | return result; |
| 710 | } |
| 711 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 712 | static jint doTextRunCursor(JNIEnv *env, Paint* paint, const jchar *text, jint start, |
Fabrice Di Meglio | a4bf811 | 2013-03-15 11:22:29 -0700 | [diff] [blame] | 713 | jint count, jint flags, jint offset, jint opt) { |
Raph Levien | e368b6b | 2014-06-15 17:37:57 -0700 | [diff] [blame] | 714 | GraphemeBreak::MoveOpt moveOpt = GraphemeBreak::MoveOpt(opt); |
| 715 | size_t result = GraphemeBreak::getTextRunCursor(text, start, count, offset, moveOpt); |
| 716 | return static_cast<jint>(result); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 719 | static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 720 | jint contextStart, jint contextCount, jint dir, jint offset, jint cursorOpt) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 721 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 722 | jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 723 | jint result = doTextRunCursor(env, paint, textArray, contextStart, contextCount, dir, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 724 | offset, cursorOpt); |
| 725 | env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); |
| 726 | return result; |
| 727 | } |
| 728 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 729 | static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, jlong paintHandle, jstring text, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 730 | jint contextStart, jint contextEnd, jint dir, jint offset, jint cursorOpt) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 731 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 732 | const jchar* textArray = env->GetStringChars(text, NULL); |
| 733 | jint result = doTextRunCursor(env, paint, textArray, contextStart, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 734 | contextEnd - contextStart, dir, offset, cursorOpt); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 735 | env->ReleaseStringChars(text, textArray); |
| 736 | return result; |
| 737 | } |
| 738 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 739 | class GetTextFunctor { |
| 740 | public: |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 741 | GetTextFunctor(const Layout& layout, SkPath* path, jfloat x, jfloat y, Paint* paint, |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 742 | uint16_t* glyphs, SkPoint* pos) |
| 743 | : layout(layout), path(path), x(x), y(y), paint(paint), glyphs(glyphs), pos(pos) { |
| 744 | } |
| 745 | |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 746 | void operator()(size_t start, size_t end) { |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 747 | for (size_t i = start; i < end; i++) { |
| 748 | glyphs[i] = layout.getGlyphId(i); |
| 749 | pos[i].fX = x + layout.getX(i); |
| 750 | pos[i].fY = y + layout.getY(i); |
| 751 | } |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 752 | if (start == 0) { |
| 753 | paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, path); |
| 754 | } else { |
| 755 | paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, &tmpPath); |
| 756 | path->addPath(tmpPath); |
| 757 | } |
| 758 | } |
| 759 | private: |
| 760 | const Layout& layout; |
| 761 | SkPath* path; |
| 762 | jfloat x; |
| 763 | jfloat y; |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 764 | Paint* paint; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 765 | uint16_t* glyphs; |
| 766 | SkPoint* pos; |
| 767 | SkPath tmpPath; |
| 768 | }; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 769 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 770 | static void getTextPath(JNIEnv* env, Paint* paint, TypefaceImpl* typeface, const jchar* text, |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 771 | jint count, jint bidiFlags, jfloat x, jfloat y, SkPath* path) { |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 772 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 773 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, 0, count, count); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 774 | size_t nGlyphs = layout.nGlyphs(); |
| 775 | uint16_t* glyphs = new uint16_t[nGlyphs]; |
| 776 | SkPoint* pos = new SkPoint[nGlyphs]; |
| 777 | |
| 778 | x += MinikinUtils::xOffsetForTextAlign(paint, layout); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 779 | Paint::Align align = paint->getTextAlign(); |
| 780 | paint->setTextAlign(Paint::kLeft_Align); |
| 781 | paint->setTextEncoding(Paint::kGlyphID_TextEncoding); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 782 | GetTextFunctor f(layout, path, x, y, paint, glyphs, pos); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 783 | MinikinUtils::forFontRun(layout, paint, f); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 784 | paint->setTextAlign(align); |
| 785 | delete[] glyphs; |
| 786 | delete[] pos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 788 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 789 | static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle, |
| 790 | jlong typefaceHandle, jint bidiFlags, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 791 | jcharArray text, jint index, jint count, jfloat x, jfloat y, jlong pathHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 792 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 793 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 794 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 795 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 796 | getTextPath(env, paint, typeface, textArray + index, count, bidiFlags, x, y, path); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 797 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT); |
| 798 | } |
| 799 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 800 | static void getTextPath__String(JNIEnv* env, jobject clazz, jlong paintHandle, |
| 801 | jlong typefaceHandle, jint bidiFlags, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 802 | jstring text, jint start, jint end, jfloat x, jfloat y, jlong pathHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 803 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 804 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 805 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | const jchar* textArray = env->GetStringChars(text, NULL); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 807 | getTextPath(env, paint, typeface, textArray + start, end - start, bidiFlags, x, y, path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | env->ReleaseStringChars(text, textArray); |
| 809 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 810 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 811 | static void setShadowLayer(JNIEnv* env, jobject clazz, jlong paintHandle, jfloat radius, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 812 | jfloat dx, jfloat dy, jint color) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 813 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | if (radius <= 0) { |
| 815 | paint->setLooper(NULL); |
| 816 | } |
| 817 | else { |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 818 | SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius); |
Victoria Lease | 49f0db3 | 2014-05-19 16:00:17 -0700 | [diff] [blame] | 819 | paint->setLooper(SkBlurDrawLooper::Create((SkColor)color, sigma, dx, dy))->unref(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 823 | static jboolean hasShadowLayer(JNIEnv* env, jobject clazz, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 824 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 825 | return paint->getLooper() && paint->getLooper()->asABlurShadow(NULL); |
| 826 | } |
| 827 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 828 | static int breakText(JNIEnv* env, const Paint& paint, TypefaceImpl* typeface, const jchar text[], |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 829 | int count, float maxWidth, jint bidiFlags, jfloatArray jmeasured, |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 830 | Paint::TextBufferDirection textBufferDirection) { |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 831 | size_t measuredCount = 0; |
| 832 | float measured = 0; |
| 833 | |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 834 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 835 | MinikinUtils::doLayout(&layout, &paint, bidiFlags, typeface, text, 0, count, count); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 836 | float* advances = new float[count]; |
| 837 | layout.getAdvances(advances); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 838 | const bool forwardScan = (textBufferDirection == Paint::kForward_TextBufferDirection); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 839 | for (int i = 0; i < count; i++) { |
| 840 | // traverse in the given direction |
| 841 | int index = forwardScan ? i : (count - i - 1); |
| 842 | float width = advances[index]; |
| 843 | if (measured + width > maxWidth) { |
| 844 | break; |
| 845 | } |
| 846 | // properly handle clusters when scanning backwards |
| 847 | if (forwardScan || width != 0.0f) { |
| 848 | measuredCount = i + 1; |
| 849 | } |
| 850 | measured += width; |
| 851 | } |
| 852 | delete[] advances; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | |
| 854 | if (jmeasured && env->GetArrayLength(jmeasured) > 0) { |
| 855 | AutoJavaFloatArray autoMeasured(env, jmeasured, 1); |
| 856 | jfloat* array = autoMeasured.ptr(); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 857 | array[0] = measured; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | } |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 859 | return measuredCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 862 | static jint breakTextC(JNIEnv* env, jobject clazz, jlong paintHandle, jlong typefaceHandle, jcharArray jtext, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 863 | 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] | 864 | NPE_CHECK_RETURN_ZERO(env, jtext); |
| 865 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 866 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 867 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
| 868 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 869 | Paint::TextBufferDirection tbd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | if (count < 0) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 871 | tbd = Paint::kBackward_TextBufferDirection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | count = -count; |
| 873 | } |
| 874 | else { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 875 | tbd = Paint::kForward_TextBufferDirection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | if ((index < 0) || (index + count > env->GetArrayLength(jtext))) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 879 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | return 0; |
| 881 | } |
| 882 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | const jchar* text = env->GetCharArrayElements(jtext, NULL); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 884 | count = breakText(env, *paint, typeface, text + index, count, maxWidth, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 885 | bidiFlags, jmeasuredWidth, tbd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text), |
| 887 | JNI_ABORT); |
| 888 | return count; |
| 889 | } |
| 890 | |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 891 | static jint breakTextS(JNIEnv* env, jobject clazz, jlong paintHandle, jlong typefaceHandle, jstring jtext, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 892 | jboolean forwards, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | NPE_CHECK_RETURN_ZERO(env, jtext); |
| 894 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 895 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 896 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
| 897 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 898 | Paint::TextBufferDirection tbd = forwards ? |
| 899 | Paint::kForward_TextBufferDirection : |
| 900 | Paint::kBackward_TextBufferDirection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | int count = env->GetStringLength(jtext); |
| 903 | const jchar* text = env->GetStringChars(jtext, NULL); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 904 | count = breakText(env, *paint, typeface, text, count, maxWidth, bidiFlags, jmeasuredWidth, tbd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | env->ReleaseStringChars(jtext, text); |
| 906 | return count; |
| 907 | } |
| 908 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 909 | static void doTextBounds(JNIEnv* env, const jchar* text, int count, jobject bounds, |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 910 | const Paint& paint, TypefaceImpl* typeface, jint bidiFlags) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 911 | SkRect r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 912 | SkIRect ir; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 913 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 914 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 915 | MinikinUtils::doLayout(&layout, &paint, bidiFlags, typeface, text, 0, count, count); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 916 | MinikinRect rect; |
| 917 | layout.getBounds(&rect); |
| 918 | r.fLeft = rect.mLeft; |
| 919 | r.fTop = rect.mTop; |
| 920 | r.fRight = rect.mRight; |
| 921 | r.fBottom = rect.mBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | r.roundOut(&ir); |
| 923 | GraphicsJNI::irect_to_jrect(ir, env, bounds); |
| 924 | } |
| 925 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 926 | static void getStringBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 927 | jstring text, jint start, jint end, jint bidiFlags, jobject bounds) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 928 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle);; |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 929 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | const jchar* textArray = env->GetStringChars(text, NULL); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 931 | doTextBounds(env, textArray + start, end - start, bounds, *paint, typeface, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | env->ReleaseStringChars(text, textArray); |
| 933 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 934 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 935 | static void getCharArrayBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 936 | jcharArray text, jint index, jint count, jint bidiFlags, jobject bounds) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 937 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 938 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 940 | doTextBounds(env, textArray + index, count, bounds, *paint, typeface, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), |
| 942 | JNI_ABORT); |
| 943 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 944 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | }; |
| 946 | |
| 947 | static JNINativeMethod methods[] = { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 948 | {"finalizer", "(J)V", (void*) PaintGlue::finalizer}, |
| 949 | {"native_init","()J", (void*) PaintGlue::init}, |
| 950 | {"native_initWithPaint","(J)J", (void*) PaintGlue::initWithPaint}, |
| 951 | {"native_reset","(J)V", (void*) PaintGlue::reset}, |
| 952 | {"native_set","(JJ)V", (void*) PaintGlue::assign}, |
| 953 | {"getFlags","()I", (void*) PaintGlue::getFlags}, |
| 954 | {"setFlags","(I)V", (void*) PaintGlue::setFlags}, |
| 955 | {"getHinting","()I", (void*) PaintGlue::getHinting}, |
| 956 | {"setHinting","(I)V", (void*) PaintGlue::setHinting}, |
| 957 | {"setAntiAlias","(Z)V", (void*) PaintGlue::setAntiAlias}, |
| 958 | {"setSubpixelText","(Z)V", (void*) PaintGlue::setSubpixelText}, |
| 959 | {"setLinearText","(Z)V", (void*) PaintGlue::setLinearText}, |
| 960 | {"setUnderlineText","(Z)V", (void*) PaintGlue::setUnderlineText}, |
| 961 | {"setStrikeThruText","(Z)V", (void*) PaintGlue::setStrikeThruText}, |
| 962 | {"setFakeBoldText","(Z)V", (void*) PaintGlue::setFakeBoldText}, |
| 963 | {"setFilterBitmap","(Z)V", (void*) PaintGlue::setFilterBitmap}, |
| 964 | {"setDither","(Z)V", (void*) PaintGlue::setDither}, |
| 965 | {"native_getStyle","(J)I", (void*) PaintGlue::getStyle}, |
| 966 | {"native_setStyle","(JI)V", (void*) PaintGlue::setStyle}, |
| 967 | {"getColor","()I", (void*) PaintGlue::getColor}, |
| 968 | {"setColor","(I)V", (void*) PaintGlue::setColor}, |
| 969 | {"getAlpha","()I", (void*) PaintGlue::getAlpha}, |
| 970 | {"setAlpha","(I)V", (void*) PaintGlue::setAlpha}, |
| 971 | {"getStrokeWidth","()F", (void*) PaintGlue::getStrokeWidth}, |
| 972 | {"setStrokeWidth","(F)V", (void*) PaintGlue::setStrokeWidth}, |
| 973 | {"getStrokeMiter","()F", (void*) PaintGlue::getStrokeMiter}, |
| 974 | {"setStrokeMiter","(F)V", (void*) PaintGlue::setStrokeMiter}, |
| 975 | {"native_getStrokeCap","(J)I", (void*) PaintGlue::getStrokeCap}, |
| 976 | {"native_setStrokeCap","(JI)V", (void*) PaintGlue::setStrokeCap}, |
| 977 | {"native_getStrokeJoin","(J)I", (void*) PaintGlue::getStrokeJoin}, |
| 978 | {"native_setStrokeJoin","(JI)V", (void*) PaintGlue::setStrokeJoin}, |
| 979 | {"native_getFillPath","(JJJ)Z", (void*) PaintGlue::getFillPath}, |
| 980 | {"native_setShader","(JJ)J", (void*) PaintGlue::setShader}, |
| 981 | {"native_setColorFilter","(JJ)J", (void*) PaintGlue::setColorFilter}, |
| 982 | {"native_setXfermode","(JJ)J", (void*) PaintGlue::setXfermode}, |
| 983 | {"native_setPathEffect","(JJ)J", (void*) PaintGlue::setPathEffect}, |
| 984 | {"native_setMaskFilter","(JJ)J", (void*) PaintGlue::setMaskFilter}, |
| 985 | {"native_setTypeface","(JJ)J", (void*) PaintGlue::setTypeface}, |
| 986 | {"native_setRasterizer","(JJ)J", (void*) PaintGlue::setRasterizer}, |
| 987 | {"native_getTextAlign","(J)I", (void*) PaintGlue::getTextAlign}, |
| 988 | {"native_setTextAlign","(JI)V", (void*) PaintGlue::setTextAlign}, |
| 989 | {"native_setTextLocale","(JLjava/lang/String;)V", (void*) PaintGlue::setTextLocale}, |
| 990 | {"isElegantTextHeight","()Z", (void*) PaintGlue::isElegantTextHeight}, |
| 991 | {"setElegantTextHeight","(Z)V", (void*) PaintGlue::setElegantTextHeight}, |
| 992 | {"getTextSize","()F", (void*) PaintGlue::getTextSize}, |
| 993 | {"setTextSize","(F)V", (void*) PaintGlue::setTextSize}, |
| 994 | {"getTextScaleX","()F", (void*) PaintGlue::getTextScaleX}, |
| 995 | {"setTextScaleX","(F)V", (void*) PaintGlue::setTextScaleX}, |
| 996 | {"getTextSkewX","()F", (void*) PaintGlue::getTextSkewX}, |
| 997 | {"setTextSkewX","(F)V", (void*) PaintGlue::setTextSkewX}, |
Behdad Esfahbod | fa80f74 | 2014-07-17 19:10:39 -0400 | [diff] [blame] | 998 | {"native_getLetterSpacing","(J)F", (void*) PaintGlue::getLetterSpacing}, |
| 999 | {"native_setLetterSpacing","(JF)V", (void*) PaintGlue::setLetterSpacing}, |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame^] | 1000 | {"native_setFontFeatureSettings","(JLjava/lang/String;)V", (void*) PaintGlue::setFontFeatureSettings}, |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1001 | {"ascent","()F", (void*) PaintGlue::ascent}, |
| 1002 | {"descent","()F", (void*) PaintGlue::descent}, |
| 1003 | {"getFontMetrics", "(Landroid/graphics/Paint$FontMetrics;)F", (void*)PaintGlue::getFontMetrics}, |
| 1004 | {"getFontMetricsInt", "(Landroid/graphics/Paint$FontMetricsInt;)I", (void*)PaintGlue::getFontMetricsInt}, |
| 1005 | {"native_measureText","([CIII)F", (void*) PaintGlue::measureText_CIII}, |
| 1006 | {"native_measureText","(Ljava/lang/String;I)F", (void*) PaintGlue::measureText_StringI}, |
| 1007 | {"native_measureText","(Ljava/lang/String;III)F", (void*) PaintGlue::measureText_StringIII}, |
| 1008 | {"native_breakText","(JJ[CIIFI[F)I", (void*) PaintGlue::breakTextC}, |
| 1009 | {"native_breakText","(JJLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS}, |
| 1010 | {"native_getTextWidths","(JJ[CIII[F)I", (void*) PaintGlue::getTextWidths___CIII_F}, |
| 1011 | {"native_getTextWidths","(JJLjava/lang/String;III[F)I", (void*) PaintGlue::getTextWidths__StringIII_F}, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 1012 | {"native_getTextRunAdvances","(JJ[CIIIIZ[FI)F", |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1013 | (void*) PaintGlue::getTextRunAdvances___CIIIIZ_FI}, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 1014 | {"native_getTextRunAdvances","(JJLjava/lang/String;IIIIZ[FI)F", |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1015 | (void*) PaintGlue::getTextRunAdvances__StringIIIIZ_FI}, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 1016 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1017 | {"native_getTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C}, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 1018 | {"native_getTextRunCursor", "(JLjava/lang/String;IIIII)I", |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1019 | (void*) PaintGlue::getTextRunCursor__String}, |
| 1020 | {"native_getTextPath","(JJI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C}, |
| 1021 | {"native_getTextPath","(JJILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String}, |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 1022 | {"nativeGetStringBounds", "(JJLjava/lang/String;IIILandroid/graphics/Rect;)V", |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1023 | (void*) PaintGlue::getStringBounds }, |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 1024 | {"nativeGetCharArrayBounds", "(JJ[CIIILandroid/graphics/Rect;)V", |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1025 | (void*) PaintGlue::getCharArrayBounds }, |
| 1026 | {"native_setShadowLayer", "(JFFFI)V", (void*)PaintGlue::setShadowLayer}, |
| 1027 | {"native_hasShadowLayer", "(J)Z", (void*)PaintGlue::hasShadowLayer} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | }; |
| 1029 | |
| 1030 | static jfieldID req_fieldID(jfieldID id) { |
| 1031 | SkASSERT(id); |
| 1032 | return id; |
| 1033 | } |
| 1034 | |
| 1035 | int register_android_graphics_Paint(JNIEnv* env) { |
| 1036 | gFontMetrics_class = env->FindClass("android/graphics/Paint$FontMetrics"); |
| 1037 | SkASSERT(gFontMetrics_class); |
| 1038 | gFontMetrics_class = (jclass)env->NewGlobalRef(gFontMetrics_class); |
| 1039 | |
| 1040 | gFontMetrics_fieldID.top = req_fieldID(env->GetFieldID(gFontMetrics_class, "top", "F")); |
| 1041 | gFontMetrics_fieldID.ascent = req_fieldID(env->GetFieldID(gFontMetrics_class, "ascent", "F")); |
| 1042 | gFontMetrics_fieldID.descent = req_fieldID(env->GetFieldID(gFontMetrics_class, "descent", "F")); |
| 1043 | gFontMetrics_fieldID.bottom = req_fieldID(env->GetFieldID(gFontMetrics_class, "bottom", "F")); |
| 1044 | gFontMetrics_fieldID.leading = req_fieldID(env->GetFieldID(gFontMetrics_class, "leading", "F")); |
| 1045 | |
| 1046 | gFontMetricsInt_class = env->FindClass("android/graphics/Paint$FontMetricsInt"); |
| 1047 | SkASSERT(gFontMetricsInt_class); |
| 1048 | gFontMetricsInt_class = (jclass)env->NewGlobalRef(gFontMetricsInt_class); |
| 1049 | |
| 1050 | gFontMetricsInt_fieldID.top = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "top", "I")); |
| 1051 | gFontMetricsInt_fieldID.ascent = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "ascent", "I")); |
| 1052 | gFontMetricsInt_fieldID.descent = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "descent", "I")); |
| 1053 | gFontMetricsInt_fieldID.bottom = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "bottom", "I")); |
| 1054 | gFontMetricsInt_fieldID.leading = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "leading", "I")); |
| 1055 | |
| 1056 | int result = AndroidRuntime::registerNativeMethods(env, "android/graphics/Paint", methods, |
| 1057 | sizeof(methods) / sizeof(methods[0])); |
| 1058 | return result; |
| 1059 | } |
| 1060 | |
| 1061 | } |