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 | |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 368 | obj->setTextLocale(langTag); |
Fabrice Di Meglio | 517825f | 2012-04-06 16:53:48 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 371 | static jboolean isElegantTextHeight(JNIEnv* env, jobject paint) { |
| 372 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 373 | Paint* obj = GraphicsJNI::getNativePaint(env, paint); |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 374 | return obj->getFontVariant() == VARIANT_ELEGANT; |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static void setElegantTextHeight(JNIEnv* env, jobject paint, jboolean aa) { |
| 378 | NPE_CHECK_RETURN_VOID(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 379 | Paint* obj = GraphicsJNI::getNativePaint(env, paint); |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 380 | obj->setFontVariant(aa ? VARIANT_ELEGANT : VARIANT_DEFAULT); |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 381 | } |
| 382 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | static jfloat getTextSize(JNIEnv* env, jobject paint) { |
| 384 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 385 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSize()); |
| 386 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 387 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | static void setTextSize(JNIEnv* env, jobject paint, jfloat textSize) { |
| 389 | NPE_CHECK_RETURN_VOID(env, paint); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 390 | GraphicsJNI::getNativePaint(env, paint)->setTextSize(textSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 392 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | static jfloat getTextScaleX(JNIEnv* env, jobject paint) { |
| 394 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 395 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextScaleX()); |
| 396 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 397 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | static void setTextScaleX(JNIEnv* env, jobject paint, jfloat scaleX) { |
| 399 | NPE_CHECK_RETURN_VOID(env, paint); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 400 | GraphicsJNI::getNativePaint(env, paint)->setTextScaleX(scaleX); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 402 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | static jfloat getTextSkewX(JNIEnv* env, jobject paint) { |
| 404 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 405 | return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSkewX()); |
| 406 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 407 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | static void setTextSkewX(JNIEnv* env, jobject paint, jfloat skewX) { |
| 409 | NPE_CHECK_RETURN_VOID(env, paint); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 410 | GraphicsJNI::getNativePaint(env, paint)->setTextSkewX(skewX); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 412 | |
Behdad Esfahbod | fa80f74 | 2014-07-17 19:10:39 -0400 | [diff] [blame] | 413 | static jfloat getLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle) { |
| 414 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 415 | return paint->getLetterSpacing(); |
| 416 | } |
| 417 | |
| 418 | static void setLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle, jfloat letterSpacing) { |
| 419 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 420 | paint->setLetterSpacing(letterSpacing); |
| 421 | } |
| 422 | |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 423 | static void setFontFeatureSettings(JNIEnv* env, jobject clazz, jlong paintHandle, jstring settings) { |
| 424 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Behdad Esfahbod | e9ad393 | 2014-07-30 19:46:53 -0400 | [diff] [blame] | 425 | if (!settings) { |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 426 | paint->setFontFeatureSettings(std::string()); |
Behdad Esfahbod | e9ad393 | 2014-07-30 19:46:53 -0400 | [diff] [blame] | 427 | } else { |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 428 | ScopedUtfChars settingsChars(env, settings); |
| 429 | paint->setFontFeatureSettings(std::string(settingsChars.c_str(), settingsChars.size())); |
| 430 | } |
| 431 | } |
| 432 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 433 | static SkScalar getMetricsInternal(JNIEnv* env, jobject jpaint, Paint::FontMetrics *metrics) { |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 434 | const int kElegantTop = 2500; |
| 435 | const int kElegantBottom = -1000; |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 436 | const int kElegantAscent = 1900; |
| 437 | const int kElegantDescent = -500; |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 438 | const int kElegantLeading = 0; |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 439 | Paint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 440 | TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint); |
| 441 | typeface = TypefaceImpl_resolveDefault(typeface); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 442 | FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle); |
Raph Levien | 0627ec0 | 2014-06-06 22:31:15 -0700 | [diff] [blame] | 443 | float saveSkewX = paint->getTextSkewX(); |
| 444 | bool savefakeBold = paint->isFakeBoldText(); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 445 | MinikinFontSkia::populateSkPaint(paint, baseFont.font, baseFont.fakery); |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 446 | SkScalar spacing = paint->getFontMetrics(metrics); |
Raph Levien | 0627ec0 | 2014-06-06 22:31:15 -0700 | [diff] [blame] | 447 | // The populateSkPaint call may have changed fake bold / text skew |
| 448 | // because we want to measure with those effects applied, so now |
| 449 | // restore the original settings. |
| 450 | paint->setTextSkewX(saveSkewX); |
| 451 | paint->setFakeBoldText(savefakeBold); |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 452 | if (paint->getFontVariant() == VARIANT_ELEGANT) { |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 453 | SkScalar size = paint->getTextSize(); |
| 454 | metrics->fTop = -size * kElegantTop / 2048; |
| 455 | metrics->fBottom = -size * kElegantBottom / 2048; |
| 456 | metrics->fAscent = -size * kElegantAscent / 2048; |
| 457 | metrics->fDescent = -size * kElegantDescent / 2048; |
| 458 | metrics->fLeading = size * kElegantLeading / 2048; |
| 459 | spacing = metrics->fDescent - metrics->fAscent + metrics->fLeading; |
| 460 | } |
| 461 | return spacing; |
| 462 | } |
| 463 | |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 464 | static jfloat ascent(JNIEnv* env, jobject paint) { |
| 465 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 466 | Paint::FontMetrics metrics; |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 467 | getMetricsInternal(env, paint, &metrics); |
| 468 | return SkScalarToFloat(metrics.fAscent); |
| 469 | } |
| 470 | |
| 471 | static jfloat descent(JNIEnv* env, jobject paint) { |
| 472 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 473 | Paint::FontMetrics metrics; |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 474 | getMetricsInternal(env, paint, &metrics); |
| 475 | return SkScalarToFloat(metrics.fDescent); |
| 476 | } |
| 477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | static jfloat getFontMetrics(JNIEnv* env, jobject paint, jobject metricsObj) { |
| 479 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 480 | Paint::FontMetrics metrics; |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 481 | SkScalar spacing = getMetricsInternal(env, paint, &metrics); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | |
| 483 | if (metricsObj) { |
| 484 | SkASSERT(env->IsInstanceOf(metricsObj, gFontMetrics_class)); |
| 485 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.top, SkScalarToFloat(metrics.fTop)); |
| 486 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.ascent, SkScalarToFloat(metrics.fAscent)); |
| 487 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.descent, SkScalarToFloat(metrics.fDescent)); |
| 488 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.bottom, SkScalarToFloat(metrics.fBottom)); |
| 489 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.leading, SkScalarToFloat(metrics.fLeading)); |
| 490 | } |
| 491 | return SkScalarToFloat(spacing); |
| 492 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 493 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | static jint getFontMetricsInt(JNIEnv* env, jobject paint, jobject metricsObj) { |
| 495 | NPE_CHECK_RETURN_ZERO(env, paint); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 496 | Paint::FontMetrics metrics; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 497 | |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 498 | getMetricsInternal(env, paint, &metrics); |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 499 | int ascent = SkScalarRoundToInt(metrics.fAscent); |
| 500 | int descent = SkScalarRoundToInt(metrics.fDescent); |
| 501 | int leading = SkScalarRoundToInt(metrics.fLeading); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | |
| 503 | if (metricsObj) { |
| 504 | SkASSERT(env->IsInstanceOf(metricsObj, gFontMetricsInt_class)); |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 505 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.top, SkScalarFloorToInt(metrics.fTop)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.ascent, ascent); |
| 507 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.descent, descent); |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 508 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.bottom, SkScalarCeilToInt(metrics.fBottom)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.leading, leading); |
| 510 | } |
| 511 | return descent - ascent + leading; |
| 512 | } |
| 513 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 514 | 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] | 515 | jint bidiFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 517 | NPE_CHECK_RETURN_ZERO(env, text); |
| 518 | |
| 519 | size_t textLength = env->GetArrayLength(text); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | if ((index | count) < 0 || (size_t)(index + count) > textLength) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 521 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | return 0; |
| 523 | } |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 524 | if (count == 0) { |
| 525 | return 0; |
| 526 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 528 | Paint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 530 | jfloat result = 0; |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 531 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 532 | Layout layout; |
| 533 | TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint); |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 534 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, textArray, index, count, textLength); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 535 | result = layout.getAdvance(); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 536 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT); |
| 537 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 539 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 540 | 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] | 541 | jint bidiFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 543 | NPE_CHECK_RETURN_ZERO(env, text); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 544 | |
Fabrice Di Meglio | 51f383d | 2011-09-13 15:57:58 -0700 | [diff] [blame] | 545 | size_t textLength = env->GetStringLength(text); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | int count = end - start; |
Fabrice Di Meglio | 51f383d | 2011-09-13 15:57:58 -0700 | [diff] [blame] | 547 | if ((start | count) < 0 || (size_t)end > textLength) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 548 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | return 0; |
| 550 | } |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 551 | if (count == 0) { |
| 552 | return 0; |
| 553 | } |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 554 | |
| 555 | const jchar* textArray = env->GetStringChars(text, NULL); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 556 | Paint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 557 | jfloat width = 0; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 558 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 559 | Layout layout; |
| 560 | TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint); |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 561 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, textArray, start, count, textLength); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 562 | width = layout.getAdvance(); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 563 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | env->ReleaseStringChars(text, textArray); |
| 565 | return width; |
| 566 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 567 | |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 568 | 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] | 569 | NPE_CHECK_RETURN_ZERO(env, jpaint); |
| 570 | NPE_CHECK_RETURN_ZERO(env, text); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 571 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | size_t textLength = env->GetStringLength(text); |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 573 | if (textLength == 0) { |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | const jchar* textArray = env->GetStringChars(text, NULL); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 578 | Paint* paint = GraphicsJNI::getNativePaint(env, jpaint); |
Fabrice Di Meglio | 4f810c8 | 2011-04-19 14:53:58 -0700 | [diff] [blame] | 579 | jfloat width = 0; |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 580 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 581 | Layout layout; |
| 582 | TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint); |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 583 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, textArray, 0, textLength, textLength); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 584 | width = layout.getAdvance(); |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 585 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | env->ReleaseStringChars(text, textArray); |
| 587 | return width; |
| 588 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 589 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 590 | 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] | 591 | jfloatArray widths, jint bidiFlags) { |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 592 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 593 | NPE_CHECK_RETURN_ZERO(env, text); |
| 594 | |
| 595 | if (count < 0 || !widths) { |
| 596 | doThrowAIOOBE(env); |
| 597 | return 0; |
| 598 | } |
| 599 | if (count == 0) { |
| 600 | return 0; |
| 601 | } |
| 602 | size_t widthsLength = env->GetArrayLength(widths); |
| 603 | if ((size_t)count > widthsLength) { |
| 604 | doThrowAIOOBE(env); |
| 605 | return 0; |
| 606 | } |
| 607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | AutoJavaFloatArray autoWidths(env, widths, count); |
| 609 | jfloat* widthsArray = autoWidths.ptr(); |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 610 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 611 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 612 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, 0, count, count); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 613 | layout.getAdvances(widthsArray); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | return count; |
| 616 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 617 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 618 | 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] | 619 | jint index, jint count, jint bidiFlags, jfloatArray widths) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 620 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 621 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 623 | 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] | 624 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 625 | JNI_ABORT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | return count; |
| 627 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 628 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 629 | 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] | 630 | jint start, jint end, jint bidiFlags, jfloatArray widths) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 631 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 632 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | const jchar* textArray = env->GetStringChars(text, NULL); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 634 | 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] | 635 | env->ReleaseStringChars(text, textArray); |
| 636 | return count; |
| 637 | } |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 638 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 639 | static jfloat doTextRunAdvances(JNIEnv *env, Paint *paint, TypefaceImpl* typeface, const jchar *text, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 640 | jint start, jint count, jint contextCount, jboolean isRtl, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 641 | jfloatArray advances, jint advancesIndex) { |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 642 | NPE_CHECK_RETURN_ZERO(env, paint); |
| 643 | NPE_CHECK_RETURN_ZERO(env, text); |
| 644 | |
| 645 | if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) { |
| 646 | doThrowAIOOBE(env); |
| 647 | return 0; |
| 648 | } |
| 649 | if (count == 0) { |
| 650 | return 0; |
| 651 | } |
| 652 | if (advances) { |
| 653 | size_t advancesLength = env->GetArrayLength(advances); |
| 654 | if ((size_t)count > advancesLength) { |
| 655 | doThrowAIOOBE(env); |
| 656 | return 0; |
| 657 | } |
| 658 | } |
Victoria Lease | 3af2a37 | 2014-03-17 16:03:28 -0700 | [diff] [blame] | 659 | jfloat* advancesArray = new jfloat[count]; |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 660 | jfloat totalAdvance = 0; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 661 | |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 662 | int bidiFlags = isRtl ? kBidi_Force_RTL : kBidi_Force_LTR; |
| 663 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 664 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 665 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, start, count, contextCount); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 666 | layout.getAdvances(advancesArray); |
| 667 | totalAdvance = layout.getAdvance(); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 668 | |
| 669 | if (advances != NULL) { |
| 670 | env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray); |
| 671 | } |
Victoria Lease | 3af2a37 | 2014-03-17 16:03:28 -0700 | [diff] [blame] | 672 | delete [] advancesArray; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 673 | return totalAdvance; |
| 674 | } |
| 675 | |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 676 | static jfloat getTextRunAdvances___CIIIIZ_FI(JNIEnv* env, jobject clazz, jlong paintHandle, |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 677 | jlong typefaceHandle, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 678 | jcharArray text, jint index, jint count, jint contextIndex, jint contextCount, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 679 | jboolean isRtl, jfloatArray advances, jint advancesIndex) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 680 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 681 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 682 | jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 683 | jfloat result = doTextRunAdvances(env, paint, typeface, textArray + contextIndex, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 684 | index - contextIndex, count, contextCount, isRtl, advances, advancesIndex); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 685 | env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); |
| 686 | return result; |
| 687 | } |
| 688 | |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 689 | static jfloat getTextRunAdvances__StringIIIIZ_FI(JNIEnv* env, jobject clazz, jlong paintHandle, |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 690 | jlong typefaceHandle, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 691 | 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] | 692 | jfloatArray advances, jint advancesIndex) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 693 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 694 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 695 | const jchar* textArray = env->GetStringChars(text, NULL); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 696 | jfloat result = doTextRunAdvances(env, paint, typeface, textArray + contextStart, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 697 | start - contextStart, end - start, contextEnd - contextStart, isRtl, |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 698 | advances, advancesIndex); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 699 | env->ReleaseStringChars(text, textArray); |
| 700 | return result; |
| 701 | } |
| 702 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 703 | 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] | 704 | jint count, jint flags, jint offset, jint opt) { |
Raph Levien | e368b6b | 2014-06-15 17:37:57 -0700 | [diff] [blame] | 705 | GraphemeBreak::MoveOpt moveOpt = GraphemeBreak::MoveOpt(opt); |
| 706 | size_t result = GraphemeBreak::getTextRunCursor(text, start, count, offset, moveOpt); |
| 707 | return static_cast<jint>(result); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 710 | static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 711 | jint contextStart, jint contextCount, jint dir, jint offset, jint cursorOpt) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 712 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 713 | jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 714 | jint result = doTextRunCursor(env, paint, textArray, contextStart, contextCount, dir, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 715 | offset, cursorOpt); |
| 716 | env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); |
| 717 | return result; |
| 718 | } |
| 719 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 720 | static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, jlong paintHandle, jstring text, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 721 | jint contextStart, jint contextEnd, jint dir, jint offset, jint cursorOpt) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 722 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 723 | const jchar* textArray = env->GetStringChars(text, NULL); |
| 724 | jint result = doTextRunCursor(env, paint, textArray, contextStart, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 725 | contextEnd - contextStart, dir, offset, cursorOpt); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 726 | env->ReleaseStringChars(text, textArray); |
| 727 | return result; |
| 728 | } |
| 729 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 730 | class GetTextFunctor { |
| 731 | public: |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 732 | GetTextFunctor(const Layout& layout, SkPath* path, jfloat x, jfloat y, Paint* paint, |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 733 | uint16_t* glyphs, SkPoint* pos) |
| 734 | : layout(layout), path(path), x(x), y(y), paint(paint), glyphs(glyphs), pos(pos) { |
| 735 | } |
| 736 | |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 737 | void operator()(size_t start, size_t end) { |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 738 | for (size_t i = start; i < end; i++) { |
| 739 | glyphs[i] = layout.getGlyphId(i); |
| 740 | pos[i].fX = x + layout.getX(i); |
| 741 | pos[i].fY = y + layout.getY(i); |
| 742 | } |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 743 | if (start == 0) { |
| 744 | paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, path); |
| 745 | } else { |
| 746 | paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, &tmpPath); |
| 747 | path->addPath(tmpPath); |
| 748 | } |
| 749 | } |
| 750 | private: |
| 751 | const Layout& layout; |
| 752 | SkPath* path; |
| 753 | jfloat x; |
| 754 | jfloat y; |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 755 | Paint* paint; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 756 | uint16_t* glyphs; |
| 757 | SkPoint* pos; |
| 758 | SkPath tmpPath; |
| 759 | }; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 760 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 761 | static void getTextPath(JNIEnv* env, Paint* paint, TypefaceImpl* typeface, const jchar* text, |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 762 | jint count, jint bidiFlags, jfloat x, jfloat y, SkPath* path) { |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 763 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 764 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, 0, count, count); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 765 | size_t nGlyphs = layout.nGlyphs(); |
| 766 | uint16_t* glyphs = new uint16_t[nGlyphs]; |
| 767 | SkPoint* pos = new SkPoint[nGlyphs]; |
| 768 | |
| 769 | x += MinikinUtils::xOffsetForTextAlign(paint, layout); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 770 | Paint::Align align = paint->getTextAlign(); |
| 771 | paint->setTextAlign(Paint::kLeft_Align); |
| 772 | paint->setTextEncoding(Paint::kGlyphID_TextEncoding); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 773 | GetTextFunctor f(layout, path, x, y, paint, glyphs, pos); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 774 | MinikinUtils::forFontRun(layout, paint, f); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 775 | paint->setTextAlign(align); |
| 776 | delete[] glyphs; |
| 777 | delete[] pos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 779 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 780 | static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle, |
| 781 | jlong typefaceHandle, jint bidiFlags, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 782 | jcharArray text, jint index, jint count, jfloat x, jfloat y, jlong pathHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 783 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 784 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 785 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 786 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 787 | getTextPath(env, paint, typeface, textArray + index, count, bidiFlags, x, y, path); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 788 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT); |
| 789 | } |
| 790 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 791 | static void getTextPath__String(JNIEnv* env, jobject clazz, jlong paintHandle, |
| 792 | jlong typefaceHandle, jint bidiFlags, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 793 | jstring text, jint start, jint end, jfloat x, jfloat y, jlong pathHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 794 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 795 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 796 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | const jchar* textArray = env->GetStringChars(text, NULL); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 798 | 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] | 799 | env->ReleaseStringChars(text, textArray); |
| 800 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 801 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 802 | static void setShadowLayer(JNIEnv* env, jobject clazz, jlong paintHandle, jfloat radius, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 803 | jfloat dx, jfloat dy, jint color) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 804 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | if (radius <= 0) { |
| 806 | paint->setLooper(NULL); |
| 807 | } |
| 808 | else { |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 809 | SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius); |
Victoria Lease | 49f0db3 | 2014-05-19 16:00:17 -0700 | [diff] [blame] | 810 | 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] | 811 | } |
| 812 | } |
| 813 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 814 | static jboolean hasShadowLayer(JNIEnv* env, jobject clazz, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 815 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 816 | return paint->getLooper() && paint->getLooper()->asABlurShadow(NULL); |
| 817 | } |
| 818 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 819 | 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] | 820 | int count, float maxWidth, jint bidiFlags, jfloatArray jmeasured, |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 821 | Paint::TextBufferDirection textBufferDirection) { |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 822 | size_t measuredCount = 0; |
| 823 | float measured = 0; |
| 824 | |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 825 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 826 | MinikinUtils::doLayout(&layout, &paint, bidiFlags, typeface, text, 0, count, count); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 827 | float* advances = new float[count]; |
| 828 | layout.getAdvances(advances); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 829 | const bool forwardScan = (textBufferDirection == Paint::kForward_TextBufferDirection); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 830 | for (int i = 0; i < count; i++) { |
| 831 | // traverse in the given direction |
| 832 | int index = forwardScan ? i : (count - i - 1); |
| 833 | float width = advances[index]; |
| 834 | if (measured + width > maxWidth) { |
| 835 | break; |
| 836 | } |
| 837 | // properly handle clusters when scanning backwards |
| 838 | if (forwardScan || width != 0.0f) { |
| 839 | measuredCount = i + 1; |
| 840 | } |
| 841 | measured += width; |
| 842 | } |
| 843 | delete[] advances; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | |
| 845 | if (jmeasured && env->GetArrayLength(jmeasured) > 0) { |
| 846 | AutoJavaFloatArray autoMeasured(env, jmeasured, 1); |
| 847 | jfloat* array = autoMeasured.ptr(); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 848 | array[0] = measured; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | } |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 850 | return measuredCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 853 | 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] | 854 | 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] | 855 | NPE_CHECK_RETURN_ZERO(env, jtext); |
| 856 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 857 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 858 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
| 859 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 860 | Paint::TextBufferDirection tbd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | if (count < 0) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 862 | tbd = Paint::kBackward_TextBufferDirection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | count = -count; |
| 864 | } |
| 865 | else { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 866 | tbd = Paint::kForward_TextBufferDirection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | if ((index < 0) || (index + count > env->GetArrayLength(jtext))) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 870 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | const jchar* text = env->GetCharArrayElements(jtext, NULL); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 875 | count = breakText(env, *paint, typeface, text + index, count, maxWidth, |
Victoria Lease | 626d3c2 | 2013-03-27 15:35:53 -0700 | [diff] [blame] | 876 | bidiFlags, jmeasuredWidth, tbd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text), |
| 878 | JNI_ABORT); |
| 879 | return count; |
| 880 | } |
| 881 | |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 882 | 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] | 883 | jboolean forwards, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | NPE_CHECK_RETURN_ZERO(env, jtext); |
| 885 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 886 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 887 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
| 888 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 889 | Paint::TextBufferDirection tbd = forwards ? |
| 890 | Paint::kForward_TextBufferDirection : |
| 891 | Paint::kBackward_TextBufferDirection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | int count = env->GetStringLength(jtext); |
| 894 | const jchar* text = env->GetStringChars(jtext, NULL); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 895 | 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] | 896 | env->ReleaseStringChars(jtext, text); |
| 897 | return count; |
| 898 | } |
| 899 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 900 | static void doTextBounds(JNIEnv* env, const jchar* text, int count, jobject bounds, |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 901 | const Paint& paint, TypefaceImpl* typeface, jint bidiFlags) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 902 | SkRect r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | SkIRect ir; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 904 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 905 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 906 | MinikinUtils::doLayout(&layout, &paint, bidiFlags, typeface, text, 0, count, count); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 907 | MinikinRect rect; |
| 908 | layout.getBounds(&rect); |
| 909 | r.fLeft = rect.mLeft; |
| 910 | r.fTop = rect.mTop; |
| 911 | r.fRight = rect.mRight; |
| 912 | r.fBottom = rect.mBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | r.roundOut(&ir); |
| 914 | GraphicsJNI::irect_to_jrect(ir, env, bounds); |
| 915 | } |
| 916 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 917 | static void getStringBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 918 | jstring text, jint start, jint end, jint bidiFlags, jobject bounds) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 919 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle);; |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 920 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | const jchar* textArray = env->GetStringChars(text, NULL); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 922 | 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] | 923 | env->ReleaseStringChars(text, textArray); |
| 924 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 925 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 926 | static void getCharArrayBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 927 | jcharArray text, jint index, jint count, 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->GetCharArrayElements(text, NULL); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 931 | doTextBounds(env, textArray + index, count, bounds, *paint, typeface, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), |
| 933 | JNI_ABORT); |
| 934 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 935 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | }; |
| 937 | |
| 938 | static JNINativeMethod methods[] = { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 939 | {"finalizer", "(J)V", (void*) PaintGlue::finalizer}, |
| 940 | {"native_init","()J", (void*) PaintGlue::init}, |
| 941 | {"native_initWithPaint","(J)J", (void*) PaintGlue::initWithPaint}, |
| 942 | {"native_reset","(J)V", (void*) PaintGlue::reset}, |
| 943 | {"native_set","(JJ)V", (void*) PaintGlue::assign}, |
| 944 | {"getFlags","()I", (void*) PaintGlue::getFlags}, |
| 945 | {"setFlags","(I)V", (void*) PaintGlue::setFlags}, |
| 946 | {"getHinting","()I", (void*) PaintGlue::getHinting}, |
| 947 | {"setHinting","(I)V", (void*) PaintGlue::setHinting}, |
| 948 | {"setAntiAlias","(Z)V", (void*) PaintGlue::setAntiAlias}, |
| 949 | {"setSubpixelText","(Z)V", (void*) PaintGlue::setSubpixelText}, |
| 950 | {"setLinearText","(Z)V", (void*) PaintGlue::setLinearText}, |
| 951 | {"setUnderlineText","(Z)V", (void*) PaintGlue::setUnderlineText}, |
| 952 | {"setStrikeThruText","(Z)V", (void*) PaintGlue::setStrikeThruText}, |
| 953 | {"setFakeBoldText","(Z)V", (void*) PaintGlue::setFakeBoldText}, |
| 954 | {"setFilterBitmap","(Z)V", (void*) PaintGlue::setFilterBitmap}, |
| 955 | {"setDither","(Z)V", (void*) PaintGlue::setDither}, |
| 956 | {"native_getStyle","(J)I", (void*) PaintGlue::getStyle}, |
| 957 | {"native_setStyle","(JI)V", (void*) PaintGlue::setStyle}, |
| 958 | {"getColor","()I", (void*) PaintGlue::getColor}, |
| 959 | {"setColor","(I)V", (void*) PaintGlue::setColor}, |
| 960 | {"getAlpha","()I", (void*) PaintGlue::getAlpha}, |
| 961 | {"setAlpha","(I)V", (void*) PaintGlue::setAlpha}, |
| 962 | {"getStrokeWidth","()F", (void*) PaintGlue::getStrokeWidth}, |
| 963 | {"setStrokeWidth","(F)V", (void*) PaintGlue::setStrokeWidth}, |
| 964 | {"getStrokeMiter","()F", (void*) PaintGlue::getStrokeMiter}, |
| 965 | {"setStrokeMiter","(F)V", (void*) PaintGlue::setStrokeMiter}, |
| 966 | {"native_getStrokeCap","(J)I", (void*) PaintGlue::getStrokeCap}, |
| 967 | {"native_setStrokeCap","(JI)V", (void*) PaintGlue::setStrokeCap}, |
| 968 | {"native_getStrokeJoin","(J)I", (void*) PaintGlue::getStrokeJoin}, |
| 969 | {"native_setStrokeJoin","(JI)V", (void*) PaintGlue::setStrokeJoin}, |
| 970 | {"native_getFillPath","(JJJ)Z", (void*) PaintGlue::getFillPath}, |
| 971 | {"native_setShader","(JJ)J", (void*) PaintGlue::setShader}, |
| 972 | {"native_setColorFilter","(JJ)J", (void*) PaintGlue::setColorFilter}, |
| 973 | {"native_setXfermode","(JJ)J", (void*) PaintGlue::setXfermode}, |
| 974 | {"native_setPathEffect","(JJ)J", (void*) PaintGlue::setPathEffect}, |
| 975 | {"native_setMaskFilter","(JJ)J", (void*) PaintGlue::setMaskFilter}, |
| 976 | {"native_setTypeface","(JJ)J", (void*) PaintGlue::setTypeface}, |
| 977 | {"native_setRasterizer","(JJ)J", (void*) PaintGlue::setRasterizer}, |
| 978 | {"native_getTextAlign","(J)I", (void*) PaintGlue::getTextAlign}, |
| 979 | {"native_setTextAlign","(JI)V", (void*) PaintGlue::setTextAlign}, |
| 980 | {"native_setTextLocale","(JLjava/lang/String;)V", (void*) PaintGlue::setTextLocale}, |
| 981 | {"isElegantTextHeight","()Z", (void*) PaintGlue::isElegantTextHeight}, |
| 982 | {"setElegantTextHeight","(Z)V", (void*) PaintGlue::setElegantTextHeight}, |
| 983 | {"getTextSize","()F", (void*) PaintGlue::getTextSize}, |
| 984 | {"setTextSize","(F)V", (void*) PaintGlue::setTextSize}, |
| 985 | {"getTextScaleX","()F", (void*) PaintGlue::getTextScaleX}, |
| 986 | {"setTextScaleX","(F)V", (void*) PaintGlue::setTextScaleX}, |
| 987 | {"getTextSkewX","()F", (void*) PaintGlue::getTextSkewX}, |
| 988 | {"setTextSkewX","(F)V", (void*) PaintGlue::setTextSkewX}, |
Behdad Esfahbod | fa80f74 | 2014-07-17 19:10:39 -0400 | [diff] [blame] | 989 | {"native_getLetterSpacing","(J)F", (void*) PaintGlue::getLetterSpacing}, |
| 990 | {"native_setLetterSpacing","(JF)V", (void*) PaintGlue::setLetterSpacing}, |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 991 | {"native_setFontFeatureSettings","(JLjava/lang/String;)V", (void*) PaintGlue::setFontFeatureSettings}, |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 992 | {"ascent","()F", (void*) PaintGlue::ascent}, |
| 993 | {"descent","()F", (void*) PaintGlue::descent}, |
| 994 | {"getFontMetrics", "(Landroid/graphics/Paint$FontMetrics;)F", (void*)PaintGlue::getFontMetrics}, |
| 995 | {"getFontMetricsInt", "(Landroid/graphics/Paint$FontMetricsInt;)I", (void*)PaintGlue::getFontMetricsInt}, |
| 996 | {"native_measureText","([CIII)F", (void*) PaintGlue::measureText_CIII}, |
| 997 | {"native_measureText","(Ljava/lang/String;I)F", (void*) PaintGlue::measureText_StringI}, |
| 998 | {"native_measureText","(Ljava/lang/String;III)F", (void*) PaintGlue::measureText_StringIII}, |
| 999 | {"native_breakText","(JJ[CIIFI[F)I", (void*) PaintGlue::breakTextC}, |
| 1000 | {"native_breakText","(JJLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS}, |
| 1001 | {"native_getTextWidths","(JJ[CIII[F)I", (void*) PaintGlue::getTextWidths___CIII_F}, |
| 1002 | {"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] | 1003 | {"native_getTextRunAdvances","(JJ[CIIIIZ[FI)F", |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1004 | (void*) PaintGlue::getTextRunAdvances___CIIIIZ_FI}, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 1005 | {"native_getTextRunAdvances","(JJLjava/lang/String;IIIIZ[FI)F", |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1006 | (void*) PaintGlue::getTextRunAdvances__StringIIIIZ_FI}, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 1007 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1008 | {"native_getTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C}, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 1009 | {"native_getTextRunCursor", "(JLjava/lang/String;IIIII)I", |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1010 | (void*) PaintGlue::getTextRunCursor__String}, |
| 1011 | {"native_getTextPath","(JJI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C}, |
| 1012 | {"native_getTextPath","(JJILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String}, |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 1013 | {"nativeGetStringBounds", "(JJLjava/lang/String;IIILandroid/graphics/Rect;)V", |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1014 | (void*) PaintGlue::getStringBounds }, |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 1015 | {"nativeGetCharArrayBounds", "(JJ[CIIILandroid/graphics/Rect;)V", |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1016 | (void*) PaintGlue::getCharArrayBounds }, |
| 1017 | {"native_setShadowLayer", "(JFFFI)V", (void*)PaintGlue::setShadowLayer}, |
| 1018 | {"native_hasShadowLayer", "(J)Z", (void*)PaintGlue::hasShadowLayer} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | }; |
| 1020 | |
| 1021 | static jfieldID req_fieldID(jfieldID id) { |
| 1022 | SkASSERT(id); |
| 1023 | return id; |
| 1024 | } |
| 1025 | |
| 1026 | int register_android_graphics_Paint(JNIEnv* env) { |
| 1027 | gFontMetrics_class = env->FindClass("android/graphics/Paint$FontMetrics"); |
| 1028 | SkASSERT(gFontMetrics_class); |
| 1029 | gFontMetrics_class = (jclass)env->NewGlobalRef(gFontMetrics_class); |
| 1030 | |
| 1031 | gFontMetrics_fieldID.top = req_fieldID(env->GetFieldID(gFontMetrics_class, "top", "F")); |
| 1032 | gFontMetrics_fieldID.ascent = req_fieldID(env->GetFieldID(gFontMetrics_class, "ascent", "F")); |
| 1033 | gFontMetrics_fieldID.descent = req_fieldID(env->GetFieldID(gFontMetrics_class, "descent", "F")); |
| 1034 | gFontMetrics_fieldID.bottom = req_fieldID(env->GetFieldID(gFontMetrics_class, "bottom", "F")); |
| 1035 | gFontMetrics_fieldID.leading = req_fieldID(env->GetFieldID(gFontMetrics_class, "leading", "F")); |
| 1036 | |
| 1037 | gFontMetricsInt_class = env->FindClass("android/graphics/Paint$FontMetricsInt"); |
| 1038 | SkASSERT(gFontMetricsInt_class); |
| 1039 | gFontMetricsInt_class = (jclass)env->NewGlobalRef(gFontMetricsInt_class); |
| 1040 | |
| 1041 | gFontMetricsInt_fieldID.top = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "top", "I")); |
| 1042 | gFontMetricsInt_fieldID.ascent = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "ascent", "I")); |
| 1043 | gFontMetricsInt_fieldID.descent = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "descent", "I")); |
| 1044 | gFontMetricsInt_fieldID.bottom = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "bottom", "I")); |
| 1045 | gFontMetricsInt_fieldID.leading = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "leading", "I")); |
| 1046 | |
| 1047 | int result = AndroidRuntime::registerNativeMethods(env, "android/graphics/Paint", methods, |
| 1048 | sizeof(methods) / sizeof(methods[0])); |
| 1049 | return result; |
| 1050 | } |
| 1051 | |
| 1052 | } |