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" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 24 | #include "core_jni_helpers.h" |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 25 | #include <ScopedStringChars.h> |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 26 | #include <ScopedUtfChars.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | #include "SkBlurDrawLooper.h" |
| 29 | #include "SkColorFilter.h" |
| 30 | #include "SkMaskFilter.h" |
Derek Sollenberger | eba81d0 | 2015-10-26 10:22:37 -0400 | [diff] [blame] | 31 | #include "SkPath.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include "SkRasterizer.h" |
| 33 | #include "SkShader.h" |
| 34 | #include "SkTypeface.h" |
| 35 | #include "SkXfermode.h" |
Billy Hewlett | ac1cbaf | 2012-07-18 09:51:45 -0700 | [diff] [blame] | 36 | #include "unicode/uloc.h" |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 37 | #include "unicode/ushape.h" |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 38 | #include "utils/Blur.h" |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 39 | |
Raph Levien | e368b6b | 2014-06-15 17:37:57 -0700 | [diff] [blame] | 40 | #include <minikin/GraphemeBreak.h> |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 41 | #include <minikin/Measurement.h> |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 42 | #include "MinikinSkia.h" |
| 43 | #include "MinikinUtils.h" |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 44 | #include "Paint.h" |
Raph Levien | 3d528c40 | 2014-06-26 09:04:54 -0700 | [diff] [blame] | 45 | #include "TypefaceImpl.h" |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 46 | |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 47 | #include <cassert> |
| 48 | #include <cstring> |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 49 | #include <memory> |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 50 | #include <vector> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
| 52 | namespace android { |
| 53 | |
| 54 | struct JMetricsID { |
| 55 | jfieldID top; |
| 56 | jfieldID ascent; |
| 57 | jfieldID descent; |
| 58 | jfieldID bottom; |
| 59 | jfieldID leading; |
| 60 | }; |
| 61 | |
| 62 | static jclass gFontMetrics_class; |
| 63 | static JMetricsID gFontMetrics_fieldID; |
| 64 | |
| 65 | static jclass gFontMetricsInt_class; |
| 66 | static JMetricsID gFontMetricsInt_fieldID; |
| 67 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 68 | static void defaultSettingsForAndroid(Paint* paint) { |
Fabrice Di Meglio | c511bee8 | 2012-01-05 13:30:54 -0800 | [diff] [blame] | 69 | // GlyphID encoding is required because we are using Harfbuzz shaping |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 70 | paint->setTextEncoding(Paint::kGlyphID_TextEncoding); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 73 | struct LocalesCacheEntry { |
| 74 | std::string javaLocales; |
| 75 | std::string languageTags; |
John Reck | 476f715 | 2015-07-10 09:56:34 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 78 | static thread_local LocalesCacheEntry sSingleEntryLocalesCache; |
John Reck | 476f715 | 2015-07-10 09:56:34 -0700 | [diff] [blame] | 79 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 80 | namespace PaintGlue { |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 81 | enum MoveOpt { |
| 82 | AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT |
| 83 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 85 | static void finalizer(JNIEnv* env, jobject clazz, jlong objHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 86 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | delete obj; |
| 88 | } |
| 89 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 90 | static jlong init(JNIEnv* env, jobject) { |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 91 | static_assert(1 << 0 == SkPaint::kAntiAlias_Flag, "paint_flags_mismatch"); |
| 92 | static_assert(1 << 2 == SkPaint::kDither_Flag, "paint_flags_mismatch"); |
| 93 | static_assert(1 << 3 == SkPaint::kUnderlineText_Flag, "paint_flags_mismatch"); |
| 94 | static_assert(1 << 4 == SkPaint::kStrikeThruText_Flag, "paint_flags_mismatch"); |
| 95 | static_assert(1 << 5 == SkPaint::kFakeBoldText_Flag, "paint_flags_mismatch"); |
| 96 | static_assert(1 << 6 == SkPaint::kLinearText_Flag, "paint_flags_mismatch"); |
| 97 | static_assert(1 << 7 == SkPaint::kSubpixelText_Flag, "paint_flags_mismatch"); |
| 98 | static_assert(1 << 8 == SkPaint::kDevKernText_Flag, "paint_flags_mismatch"); |
| 99 | static_assert(1 << 10 == SkPaint::kEmbeddedBitmapText_Flag, "paint_flags_mismatch"); |
Derek Sollenberger | 0b86062 | 2015-03-25 10:08:30 -0400 | [diff] [blame] | 100 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 101 | Paint* obj = new Paint(); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 102 | defaultSettingsForAndroid(obj); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 103 | return reinterpret_cast<jlong>(obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 106 | static jlong initWithPaint(JNIEnv* env, jobject clazz, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 107 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 108 | Paint* obj = new Paint(*paint); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 109 | return reinterpret_cast<jlong>(obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 111 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 112 | static void reset(JNIEnv* env, jobject clazz, jlong objHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 113 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | obj->reset(); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 115 | defaultSettingsForAndroid(obj); |
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 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 118 | static void assign(JNIEnv* env, jobject clazz, jlong dstPaintHandle, jlong srcPaintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 119 | Paint* dst = reinterpret_cast<Paint*>(dstPaintHandle); |
| 120 | const Paint* src = reinterpret_cast<Paint*>(srcPaintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | *dst = *src; |
| 122 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 123 | |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 124 | // Equivalent to the Java Paint's FILTER_BITMAP_FLAG. |
| 125 | static const uint32_t sFilterBitmapFlag = 0x02; |
| 126 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 127 | static jint getFlags(JNIEnv* env, jobject, jlong paintHandle) { |
| 128 | Paint* nativePaint = reinterpret_cast<Paint*>(paintHandle); |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 129 | uint32_t result = nativePaint->getFlags(); |
| 130 | result &= ~sFilterBitmapFlag; // Filtering no longer stored in this bit. Mask away. |
Mike Reed | 2a1ce8a | 2015-03-16 11:16:09 -0400 | [diff] [blame] | 131 | if (nativePaint->getFilterQuality() != kNone_SkFilterQuality) { |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 132 | result |= sFilterBitmapFlag; |
| 133 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 134 | return static_cast<jint>(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 136 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 137 | static void setFlags(JNIEnv* env, jobject, jlong paintHandle, jint flags) { |
| 138 | Paint* nativePaint = reinterpret_cast<Paint*>(paintHandle); |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 139 | // Instead of modifying 0x02, change the filter level. |
Mike Reed | 2a1ce8a | 2015-03-16 11:16:09 -0400 | [diff] [blame] | 140 | nativePaint->setFilterQuality(flags & sFilterBitmapFlag |
| 141 | ? kLow_SkFilterQuality |
| 142 | : kNone_SkFilterQuality); |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 143 | // Don't pass through filter flag, which is no longer stored in paint's flags. |
| 144 | flags &= ~sFilterBitmapFlag; |
| 145 | // Use the existing value for 0x02. |
| 146 | const uint32_t existing0x02Flag = nativePaint->getFlags() & sFilterBitmapFlag; |
| 147 | flags |= existing0x02Flag; |
| 148 | nativePaint->setFlags(flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 150 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 151 | static jint getHinting(JNIEnv* env, jobject, jlong paintHandle) { |
| 152 | return reinterpret_cast<Paint*>(paintHandle)->getHinting() |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 153 | == Paint::kNo_Hinting ? 0 : 1; |
Dianne Hackborn | f43fa57 | 2011-08-12 18:59:39 -0700 | [diff] [blame] | 154 | } |
| 155 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 156 | static void setHinting(JNIEnv* env, jobject, jlong paintHandle, jint mode) { |
| 157 | reinterpret_cast<Paint*>(paintHandle)->setHinting( |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 158 | mode == 0 ? Paint::kNo_Hinting : Paint::kNormal_Hinting); |
Dianne Hackborn | f43fa57 | 2011-08-12 18:59:39 -0700 | [diff] [blame] | 159 | } |
| 160 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 161 | static void setAntiAlias(JNIEnv* env, jobject, jlong paintHandle, jboolean aa) { |
| 162 | reinterpret_cast<Paint*>(paintHandle)->setAntiAlias(aa); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 164 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 165 | static void setLinearText(JNIEnv* env, jobject, jlong paintHandle, jboolean linearText) { |
| 166 | reinterpret_cast<Paint*>(paintHandle)->setLinearText(linearText); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 168 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 169 | static void setSubpixelText(JNIEnv* env, jobject, jlong paintHandle, jboolean subpixelText) { |
| 170 | reinterpret_cast<Paint*>(paintHandle)->setSubpixelText(subpixelText); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 172 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 173 | static void setUnderlineText(JNIEnv* env, jobject, jlong paintHandle, jboolean underlineText) { |
| 174 | reinterpret_cast<Paint*>(paintHandle)->setUnderlineText(underlineText); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 176 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 177 | static void setStrikeThruText(JNIEnv* env, jobject, jlong paintHandle, jboolean strikeThruText) { |
| 178 | reinterpret_cast<Paint*>(paintHandle)->setStrikeThruText(strikeThruText); |
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 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 181 | static void setFakeBoldText(JNIEnv* env, jobject, jlong paintHandle, jboolean fakeBoldText) { |
| 182 | reinterpret_cast<Paint*>(paintHandle)->setFakeBoldText(fakeBoldText); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 184 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 185 | static void setFilterBitmap(JNIEnv* env, jobject, jlong paintHandle, jboolean filterBitmap) { |
| 186 | reinterpret_cast<Paint*>(paintHandle)->setFilterQuality( |
Mike Reed | 2a1ce8a | 2015-03-16 11:16:09 -0400 | [diff] [blame] | 187 | filterBitmap ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 189 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 190 | static void setDither(JNIEnv* env, jobject, jlong paintHandle, jboolean dither) { |
| 191 | reinterpret_cast<Paint*>(paintHandle)->setDither(dither); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 193 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 194 | static jint getStyle(JNIEnv* env, jobject clazz,jlong objHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 195 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 196 | return static_cast<jint>(obj->getStyle()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 198 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 199 | static void setStyle(JNIEnv* env, jobject clazz, jlong objHandle, jint styleHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 200 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 201 | Paint::Style style = static_cast<Paint::Style>(styleHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | obj->setStyle(style); |
| 203 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 204 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 205 | static jint getColor(JNIEnv* env, jobject, jlong paintHandle) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 206 | int color; |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 207 | color = reinterpret_cast<Paint*>(paintHandle)->getColor(); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 208 | return static_cast<jint>(color); |
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 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 211 | static jint getAlpha(JNIEnv* env, jobject, jlong paintHandle) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 212 | int alpha; |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 213 | alpha = reinterpret_cast<Paint*>(paintHandle)->getAlpha(); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 214 | return static_cast<jint>(alpha); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 216 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 217 | static void setColor(JNIEnv* env, jobject, jlong paintHandle, jint color) { |
| 218 | reinterpret_cast<Paint*>(paintHandle)->setColor(color); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 220 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 221 | static void setAlpha(JNIEnv* env, jobject, jlong paintHandle, jint a) { |
| 222 | reinterpret_cast<Paint*>(paintHandle)->setAlpha(a); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 224 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 225 | static jfloat getStrokeWidth(JNIEnv* env, jobject, jlong paintHandle) { |
| 226 | return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeWidth()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 228 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 229 | static void setStrokeWidth(JNIEnv* env, jobject, jlong paintHandle, jfloat width) { |
| 230 | reinterpret_cast<Paint*>(paintHandle)->setStrokeWidth(width); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 232 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 233 | static jfloat getStrokeMiter(JNIEnv* env, jobject, jlong paintHandle) { |
| 234 | return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeMiter()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 236 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 237 | static void setStrokeMiter(JNIEnv* env, jobject, jlong paintHandle, jfloat miter) { |
| 238 | reinterpret_cast<Paint*>(paintHandle)->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 | |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 362 | static void toLanguageTags(std::string* output, const char* locales) { |
| 363 | if (output == NULL) { |
| 364 | return; |
| 365 | } |
| 366 | if (locales == NULL) { |
| 367 | output->clear(); |
| 368 | return; |
John Reck | 476f715 | 2015-07-10 09:56:34 -0700 | [diff] [blame] | 369 | } |
Derek Sollenberger | d7a8077 | 2013-05-28 10:44:26 -0400 | [diff] [blame] | 370 | |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 371 | char langTag[ULOC_FULLNAME_CAPACITY]; |
| 372 | const char* commaLoc = strchr(locales, ','); |
| 373 | if (commaLoc == NULL) { |
| 374 | assert(locales[0] != '\0'); // the string should not be empty |
| 375 | toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, locales); |
| 376 | *output = langTag; |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | size_t len = strlen(locales); |
| 381 | char locale[len]; |
| 382 | output->clear(); |
| 383 | output->reserve(len); |
| 384 | const char* lastStart = locales; |
| 385 | do { |
| 386 | assert(lastStart > commaLoc); // the substring should not be empty |
| 387 | strncpy(locale, lastStart, commaLoc - lastStart); |
| 388 | locale[commaLoc - lastStart] = '\0'; |
| 389 | toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, locale); |
| 390 | if (langTag[0] != '\0') { |
| 391 | output->append(langTag); |
| 392 | output->push_back(','); |
| 393 | } |
| 394 | lastStart = commaLoc + 1; |
| 395 | commaLoc = strchr(lastStart, ','); |
| 396 | } while (commaLoc != NULL); |
| 397 | assert(lastStart[0] != '\0'); // the final substring should not be empty |
| 398 | toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, lastStart); |
| 399 | if (langTag[0] != '\0') { |
| 400 | output->append(langTag); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | static void setTextLocales(JNIEnv* env, jobject clazz, jlong objHandle, jstring locales) { |
| 405 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 406 | ScopedUtfChars localesChars(env, locales); |
| 407 | if (sSingleEntryLocalesCache.javaLocales != localesChars.c_str()) { |
| 408 | sSingleEntryLocalesCache.javaLocales = localesChars.c_str(); |
| 409 | toLanguageTags(&sSingleEntryLocalesCache.languageTags, localesChars.c_str()); |
| 410 | } |
| 411 | |
| 412 | obj->setTextLocales(sSingleEntryLocalesCache.languageTags); |
Fabrice Di Meglio | 517825f | 2012-04-06 16:53:48 -0700 | [diff] [blame] | 413 | } |
| 414 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 415 | static jboolean isElegantTextHeight(JNIEnv* env, jobject, jlong paintHandle) { |
| 416 | Paint* obj = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 417 | return obj->getFontVariant() == VARIANT_ELEGANT; |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 418 | } |
| 419 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 420 | static void setElegantTextHeight(JNIEnv* env, jobject, jlong paintHandle, jboolean aa) { |
| 421 | Paint* obj = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 422 | obj->setFontVariant(aa ? VARIANT_ELEGANT : VARIANT_DEFAULT); |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 423 | } |
| 424 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 425 | static jfloat getTextSize(JNIEnv* env, jobject, jlong paintHandle) { |
| 426 | return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextSize()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 428 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 429 | static void setTextSize(JNIEnv* env, jobject, jlong paintHandle, jfloat textSize) { |
| 430 | reinterpret_cast<Paint*>(paintHandle)->setTextSize(textSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 432 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 433 | static jfloat getTextScaleX(JNIEnv* env, jobject, jlong paintHandle) { |
| 434 | return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextScaleX()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 436 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 437 | static void setTextScaleX(JNIEnv* env, jobject, jlong paintHandle, jfloat scaleX) { |
| 438 | reinterpret_cast<Paint*>(paintHandle)->setTextScaleX(scaleX); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 440 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 441 | static jfloat getTextSkewX(JNIEnv* env, jobject, jlong paintHandle) { |
| 442 | return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextSkewX()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 444 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 445 | static void setTextSkewX(JNIEnv* env, jobject, jlong paintHandle, jfloat skewX) { |
| 446 | reinterpret_cast<Paint*>(paintHandle)->setTextSkewX(skewX); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 448 | |
Behdad Esfahbod | fa80f74 | 2014-07-17 19:10:39 -0400 | [diff] [blame] | 449 | static jfloat getLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle) { |
| 450 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 451 | return paint->getLetterSpacing(); |
| 452 | } |
| 453 | |
| 454 | static void setLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle, jfloat letterSpacing) { |
| 455 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 456 | paint->setLetterSpacing(letterSpacing); |
| 457 | } |
| 458 | |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 459 | static void setFontFeatureSettings(JNIEnv* env, jobject clazz, jlong paintHandle, jstring settings) { |
| 460 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Behdad Esfahbod | e9ad393 | 2014-07-30 19:46:53 -0400 | [diff] [blame] | 461 | if (!settings) { |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 462 | paint->setFontFeatureSettings(std::string()); |
Behdad Esfahbod | e9ad393 | 2014-07-30 19:46:53 -0400 | [diff] [blame] | 463 | } else { |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 464 | ScopedUtfChars settingsChars(env, settings); |
| 465 | paint->setFontFeatureSettings(std::string(settingsChars.c_str(), settingsChars.size())); |
| 466 | } |
| 467 | } |
| 468 | |
Raph Levien | 210a189 | 2015-03-09 14:42:14 -0700 | [diff] [blame] | 469 | static jint getHyphenEdit(JNIEnv* env, jobject clazz, jlong paintHandle, jint hyphen) { |
| 470 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 471 | return paint->getHyphenEdit(); |
| 472 | } |
| 473 | |
| 474 | static void setHyphenEdit(JNIEnv* env, jobject clazz, jlong paintHandle, jint hyphen) { |
| 475 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 476 | paint->setHyphenEdit((uint32_t)hyphen); |
| 477 | } |
| 478 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 479 | static SkScalar getMetricsInternal(jlong paintHandle, jlong typefaceHandle, |
| 480 | Paint::FontMetrics *metrics) { |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 481 | const int kElegantTop = 2500; |
| 482 | const int kElegantBottom = -1000; |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 483 | const int kElegantAscent = 1900; |
| 484 | const int kElegantDescent = -500; |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 485 | const int kElegantLeading = 0; |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 486 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 487 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 488 | typeface = TypefaceImpl_resolveDefault(typeface); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 489 | FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle); |
Raph Levien | 0627ec0 | 2014-06-06 22:31:15 -0700 | [diff] [blame] | 490 | float saveSkewX = paint->getTextSkewX(); |
| 491 | bool savefakeBold = paint->isFakeBoldText(); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 492 | MinikinFontSkia::populateSkPaint(paint, baseFont.font, baseFont.fakery); |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 493 | SkScalar spacing = paint->getFontMetrics(metrics); |
Raph Levien | 0627ec0 | 2014-06-06 22:31:15 -0700 | [diff] [blame] | 494 | // The populateSkPaint call may have changed fake bold / text skew |
| 495 | // because we want to measure with those effects applied, so now |
| 496 | // restore the original settings. |
| 497 | paint->setTextSkewX(saveSkewX); |
| 498 | paint->setFakeBoldText(savefakeBold); |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 499 | if (paint->getFontVariant() == VARIANT_ELEGANT) { |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 500 | SkScalar size = paint->getTextSize(); |
| 501 | metrics->fTop = -size * kElegantTop / 2048; |
| 502 | metrics->fBottom = -size * kElegantBottom / 2048; |
| 503 | metrics->fAscent = -size * kElegantAscent / 2048; |
| 504 | metrics->fDescent = -size * kElegantDescent / 2048; |
| 505 | metrics->fLeading = size * kElegantLeading / 2048; |
| 506 | spacing = metrics->fDescent - metrics->fAscent + metrics->fLeading; |
| 507 | } |
| 508 | return spacing; |
| 509 | } |
| 510 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 511 | static jfloat ascent(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 512 | Paint::FontMetrics metrics; |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 513 | getMetricsInternal(paintHandle, typefaceHandle, &metrics); |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 514 | return SkScalarToFloat(metrics.fAscent); |
| 515 | } |
| 516 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 517 | static jfloat descent(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 518 | Paint::FontMetrics metrics; |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 519 | getMetricsInternal(paintHandle, typefaceHandle, &metrics); |
Raph Levien | 817ef4a | 2014-06-05 22:38:08 -0700 | [diff] [blame] | 520 | return SkScalarToFloat(metrics.fDescent); |
| 521 | } |
| 522 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 523 | static jfloat getFontMetrics(JNIEnv* env, jobject, jlong paintHandle, |
| 524 | jlong typefaceHandle, jobject metricsObj) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 525 | Paint::FontMetrics metrics; |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 526 | SkScalar spacing = getMetricsInternal(paintHandle, typefaceHandle, &metrics); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | |
| 528 | if (metricsObj) { |
| 529 | SkASSERT(env->IsInstanceOf(metricsObj, gFontMetrics_class)); |
| 530 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.top, SkScalarToFloat(metrics.fTop)); |
| 531 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.ascent, SkScalarToFloat(metrics.fAscent)); |
| 532 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.descent, SkScalarToFloat(metrics.fDescent)); |
| 533 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.bottom, SkScalarToFloat(metrics.fBottom)); |
| 534 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.leading, SkScalarToFloat(metrics.fLeading)); |
| 535 | } |
| 536 | return SkScalarToFloat(spacing); |
| 537 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 538 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 539 | static jint getFontMetricsInt(JNIEnv* env, jobject, jlong paintHandle, |
| 540 | jlong typefaceHandle, jobject metricsObj) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 541 | Paint::FontMetrics metrics; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 542 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 543 | getMetricsInternal(paintHandle, typefaceHandle, &metrics); |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 544 | int ascent = SkScalarRoundToInt(metrics.fAscent); |
| 545 | int descent = SkScalarRoundToInt(metrics.fDescent); |
| 546 | int leading = SkScalarRoundToInt(metrics.fLeading); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | |
| 548 | if (metricsObj) { |
| 549 | SkASSERT(env->IsInstanceOf(metricsObj, gFontMetricsInt_class)); |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 550 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.top, SkScalarFloorToInt(metrics.fTop)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.ascent, ascent); |
| 552 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.descent, descent); |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 553 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.bottom, SkScalarCeilToInt(metrics.fBottom)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.leading, leading); |
| 555 | } |
| 556 | return descent - ascent + leading; |
| 557 | } |
| 558 | |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 559 | static jfloat doTextAdvances(JNIEnv *env, Paint *paint, TypefaceImpl* typeface, |
| 560 | const jchar *text, jint start, jint count, jint contextCount, jint bidiFlags, |
| 561 | jfloatArray advances, jint advancesIndex) { |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 562 | NPE_CHECK_RETURN_ZERO(env, text); |
| 563 | |
| 564 | if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) { |
| 565 | doThrowAIOOBE(env); |
| 566 | return 0; |
| 567 | } |
| 568 | if (count == 0) { |
| 569 | return 0; |
| 570 | } |
| 571 | if (advances) { |
| 572 | size_t advancesLength = env->GetArrayLength(advances); |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 573 | if ((size_t)(count + advancesIndex) > advancesLength) { |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 574 | doThrowAIOOBE(env); |
| 575 | return 0; |
| 576 | } |
| 577 | } |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 578 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 579 | Layout layout; |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 580 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, start, count, |
| 581 | contextCount); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 582 | if (advances != NULL) { |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 583 | std::unique_ptr<jfloat> advancesArray(new jfloat[count]); |
| 584 | layout.getAdvances(advancesArray.get()); |
| 585 | env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray.get()); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 586 | } |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 587 | return layout.getAdvance(); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 590 | static jfloat getTextAdvances___CIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle, |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 591 | jlong typefaceHandle, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 592 | jcharArray text, jint index, jint count, jint contextIndex, jint contextCount, |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 593 | jint bidiFlags, jfloatArray advances, jint advancesIndex) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 594 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 595 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 596 | jchar* textArray = env->GetCharArrayElements(text, NULL); |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 597 | jfloat result = doTextAdvances(env, paint, typeface, textArray + contextIndex, |
| 598 | index - contextIndex, count, contextCount, bidiFlags, advances, advancesIndex); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 599 | env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); |
| 600 | return result; |
| 601 | } |
| 602 | |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 603 | static jfloat getTextAdvances__StringIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle, |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 604 | jlong typefaceHandle, |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 605 | jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint bidiFlags, |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 606 | jfloatArray advances, jint advancesIndex) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 607 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 608 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 609 | const jchar* textArray = env->GetStringChars(text, NULL); |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 610 | jfloat result = doTextAdvances(env, paint, typeface, textArray + contextStart, |
| 611 | start - contextStart, end - start, contextEnd - contextStart, bidiFlags, |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 612 | advances, advancesIndex); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 613 | env->ReleaseStringChars(text, textArray); |
| 614 | return result; |
| 615 | } |
| 616 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 617 | 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] | 618 | jint count, jint flags, jint offset, jint opt) { |
Raph Levien | e368b6b | 2014-06-15 17:37:57 -0700 | [diff] [blame] | 619 | GraphemeBreak::MoveOpt moveOpt = GraphemeBreak::MoveOpt(opt); |
| 620 | size_t result = GraphemeBreak::getTextRunCursor(text, start, count, offset, moveOpt); |
| 621 | return static_cast<jint>(result); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 624 | static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 625 | jint contextStart, jint contextCount, jint dir, jint offset, jint cursorOpt) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 626 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 627 | jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 628 | jint result = doTextRunCursor(env, paint, textArray, contextStart, contextCount, dir, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 629 | offset, cursorOpt); |
| 630 | env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); |
| 631 | return result; |
| 632 | } |
| 633 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 634 | static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, jlong paintHandle, jstring text, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 635 | jint contextStart, jint contextEnd, jint dir, jint offset, jint cursorOpt) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 636 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 637 | const jchar* textArray = env->GetStringChars(text, NULL); |
| 638 | jint result = doTextRunCursor(env, paint, textArray, contextStart, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 639 | contextEnd - contextStart, dir, offset, cursorOpt); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 640 | env->ReleaseStringChars(text, textArray); |
| 641 | return result; |
| 642 | } |
| 643 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 644 | class GetTextFunctor { |
| 645 | public: |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 646 | GetTextFunctor(const Layout& layout, SkPath* path, jfloat x, jfloat y, Paint* paint, |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 647 | uint16_t* glyphs, SkPoint* pos) |
| 648 | : layout(layout), path(path), x(x), y(y), paint(paint), glyphs(glyphs), pos(pos) { |
| 649 | } |
| 650 | |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 651 | void operator()(size_t start, size_t end) { |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 652 | for (size_t i = start; i < end; i++) { |
| 653 | glyphs[i] = layout.getGlyphId(i); |
| 654 | pos[i].fX = x + layout.getX(i); |
| 655 | pos[i].fY = y + layout.getY(i); |
| 656 | } |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 657 | if (start == 0) { |
| 658 | paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, path); |
| 659 | } else { |
| 660 | paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, &tmpPath); |
| 661 | path->addPath(tmpPath); |
| 662 | } |
| 663 | } |
| 664 | private: |
| 665 | const Layout& layout; |
| 666 | SkPath* path; |
| 667 | jfloat x; |
| 668 | jfloat y; |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 669 | Paint* paint; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 670 | uint16_t* glyphs; |
| 671 | SkPoint* pos; |
| 672 | SkPath tmpPath; |
| 673 | }; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 674 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 675 | static void getTextPath(JNIEnv* env, Paint* paint, TypefaceImpl* typeface, const jchar* text, |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 676 | jint count, jint bidiFlags, jfloat x, jfloat y, SkPath* path) { |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 677 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 678 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, 0, count, count); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 679 | size_t nGlyphs = layout.nGlyphs(); |
| 680 | uint16_t* glyphs = new uint16_t[nGlyphs]; |
| 681 | SkPoint* pos = new SkPoint[nGlyphs]; |
| 682 | |
| 683 | x += MinikinUtils::xOffsetForTextAlign(paint, layout); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 684 | Paint::Align align = paint->getTextAlign(); |
| 685 | paint->setTextAlign(Paint::kLeft_Align); |
| 686 | paint->setTextEncoding(Paint::kGlyphID_TextEncoding); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 687 | GetTextFunctor f(layout, path, x, y, paint, glyphs, pos); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 688 | MinikinUtils::forFontRun(layout, paint, f); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 689 | paint->setTextAlign(align); |
| 690 | delete[] glyphs; |
| 691 | delete[] pos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 693 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 694 | static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle, |
| 695 | jlong typefaceHandle, jint bidiFlags, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 696 | jcharArray text, jint index, jint count, jfloat x, jfloat y, jlong pathHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 697 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 698 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 699 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 700 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 701 | getTextPath(env, paint, typeface, textArray + index, count, bidiFlags, x, y, path); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 702 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT); |
| 703 | } |
| 704 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 705 | static void getTextPath__String(JNIEnv* env, jobject clazz, jlong paintHandle, |
| 706 | jlong typefaceHandle, jint bidiFlags, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 707 | jstring text, jint start, jint end, jfloat x, jfloat y, jlong pathHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 708 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 709 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 710 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | const jchar* textArray = env->GetStringChars(text, NULL); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 712 | 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] | 713 | env->ReleaseStringChars(text, textArray); |
| 714 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 715 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 716 | static void setShadowLayer(JNIEnv* env, jobject clazz, jlong paintHandle, jfloat radius, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 717 | jfloat dx, jfloat dy, jint color) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 718 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | if (radius <= 0) { |
| 720 | paint->setLooper(NULL); |
| 721 | } |
| 722 | else { |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 723 | SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius); |
Victoria Lease | 49f0db3 | 2014-05-19 16:00:17 -0700 | [diff] [blame] | 724 | 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] | 725 | } |
| 726 | } |
| 727 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 728 | static jboolean hasShadowLayer(JNIEnv* env, jobject clazz, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 729 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 730 | return paint->getLooper() && paint->getLooper()->asABlurShadow(NULL); |
| 731 | } |
| 732 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 733 | 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] | 734 | int count, float maxWidth, jint bidiFlags, jfloatArray jmeasured, |
Derek Sollenberger | 57c4fed | 2014-10-08 09:23:32 -0400 | [diff] [blame] | 735 | const bool forwardScan) { |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 736 | size_t measuredCount = 0; |
| 737 | float measured = 0; |
| 738 | |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 739 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 740 | MinikinUtils::doLayout(&layout, &paint, bidiFlags, typeface, text, 0, count, count); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 741 | float* advances = new float[count]; |
| 742 | layout.getAdvances(advances); |
Derek Sollenberger | 57c4fed | 2014-10-08 09:23:32 -0400 | [diff] [blame] | 743 | |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 744 | for (int i = 0; i < count; i++) { |
| 745 | // traverse in the given direction |
| 746 | int index = forwardScan ? i : (count - i - 1); |
| 747 | float width = advances[index]; |
| 748 | if (measured + width > maxWidth) { |
| 749 | break; |
| 750 | } |
| 751 | // properly handle clusters when scanning backwards |
| 752 | if (forwardScan || width != 0.0f) { |
| 753 | measuredCount = i + 1; |
| 754 | } |
| 755 | measured += width; |
| 756 | } |
| 757 | delete[] advances; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | |
| 759 | if (jmeasured && env->GetArrayLength(jmeasured) > 0) { |
| 760 | AutoJavaFloatArray autoMeasured(env, jmeasured, 1); |
| 761 | jfloat* array = autoMeasured.ptr(); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 762 | array[0] = measured; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | } |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 764 | return measuredCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | } |
| 766 | |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 767 | 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] | 768 | 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] | 769 | NPE_CHECK_RETURN_ZERO(env, jtext); |
| 770 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 771 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 772 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
| 773 | |
Derek Sollenberger | 57c4fed | 2014-10-08 09:23:32 -0400 | [diff] [blame] | 774 | bool forwardTextDirection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | if (count < 0) { |
Derek Sollenberger | 57c4fed | 2014-10-08 09:23:32 -0400 | [diff] [blame] | 776 | forwardTextDirection = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | count = -count; |
| 778 | } |
| 779 | else { |
Derek Sollenberger | 57c4fed | 2014-10-08 09:23:32 -0400 | [diff] [blame] | 780 | forwardTextDirection = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | if ((index < 0) || (index + count > env->GetArrayLength(jtext))) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 784 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | return 0; |
| 786 | } |
| 787 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | const jchar* text = env->GetCharArrayElements(jtext, NULL); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 789 | count = breakText(env, *paint, typeface, text + index, count, maxWidth, |
Derek Sollenberger | 57c4fed | 2014-10-08 09:23:32 -0400 | [diff] [blame] | 790 | bidiFlags, jmeasuredWidth, forwardTextDirection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text), |
| 792 | JNI_ABORT); |
| 793 | return count; |
| 794 | } |
| 795 | |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 796 | 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] | 797 | jboolean forwards, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | NPE_CHECK_RETURN_ZERO(env, jtext); |
| 799 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 800 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Anish Athalye | a7aa1b0 | 2014-06-12 12:53:09 -0700 | [diff] [blame] | 801 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
| 802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | int count = env->GetStringLength(jtext); |
| 804 | const jchar* text = env->GetStringChars(jtext, NULL); |
Derek Sollenberger | 57c4fed | 2014-10-08 09:23:32 -0400 | [diff] [blame] | 805 | count = breakText(env, *paint, typeface, text, count, maxWidth, bidiFlags, jmeasuredWidth, forwards); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | env->ReleaseStringChars(jtext, text); |
| 807 | return count; |
| 808 | } |
| 809 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 810 | static void doTextBounds(JNIEnv* env, const jchar* text, int count, jobject bounds, |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 811 | const Paint& paint, TypefaceImpl* typeface, jint bidiFlags) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 812 | SkRect r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 813 | SkIRect ir; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 814 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 815 | Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 816 | MinikinUtils::doLayout(&layout, &paint, bidiFlags, typeface, text, 0, count, count); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 817 | MinikinRect rect; |
| 818 | layout.getBounds(&rect); |
| 819 | r.fLeft = rect.mLeft; |
| 820 | r.fTop = rect.mTop; |
| 821 | r.fRight = rect.mRight; |
| 822 | r.fBottom = rect.mBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 823 | r.roundOut(&ir); |
| 824 | GraphicsJNI::irect_to_jrect(ir, env, bounds); |
| 825 | } |
| 826 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 827 | static void getStringBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 828 | jstring text, jint start, jint end, jint bidiFlags, jobject bounds) { |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 829 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 830 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | const jchar* textArray = env->GetStringChars(text, NULL); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 832 | 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] | 833 | env->ReleaseStringChars(text, textArray); |
| 834 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 835 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 836 | static void getCharArrayBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 837 | jcharArray text, jint index, jint count, jint bidiFlags, jobject bounds) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 838 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 839 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | const jchar* textArray = env->GetCharArrayElements(text, NULL); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 841 | doTextBounds(env, textArray + index, count, bounds, *paint, typeface, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), |
| 843 | JNI_ABORT); |
| 844 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 845 | |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 846 | static jboolean layoutContainsNotdef(const Layout& layout) { |
| 847 | for (size_t i = 0; i < layout.nGlyphs(); i++) { |
| 848 | if (layout.getGlyphId(i) == 0) { |
| 849 | return true; |
| 850 | } |
| 851 | } |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | static jboolean hasGlyphVariation(const Paint* paint, TypefaceImpl* typeface, jint bidiFlags, |
| 856 | const jchar* chars, size_t size) { |
| 857 | // TODO: query font for whether character has variation selector; requires a corresponding |
| 858 | // function in Minikin. |
| 859 | return false; |
| 860 | } |
| 861 | |
| 862 | static jboolean hasGlyph(JNIEnv *env, jclass, jlong paintHandle, jlong typefaceHandle, |
| 863 | jint bidiFlags, jstring string) { |
| 864 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 865 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
| 866 | ScopedStringChars str(env, string); |
| 867 | |
| 868 | /* start by rejecting variation selectors (not supported yet) */ |
| 869 | size_t nChars = 0; |
| 870 | for (size_t i = 0; i < str.size(); i++) { |
| 871 | jchar c = str[i]; |
| 872 | if (0xDC00 <= c && c <= 0xDFFF) { |
| 873 | // invalid UTF-16, unpaired trailing surrogate |
| 874 | return false; |
| 875 | } else if (0xD800 <= c && c <= 0xDBFF) { |
| 876 | if (i + 1 == str.size()) { |
| 877 | // invalid UTF-16, unpaired leading surrogate at end of string |
| 878 | return false; |
| 879 | } |
| 880 | i++; |
| 881 | jchar c2 = str[i]; |
| 882 | if (!(0xDC00 <= c2 && c2 <= 0xDFFF)) { |
| 883 | // invalid UTF-16, unpaired leading surrogate |
| 884 | return false; |
| 885 | } |
| 886 | // UTF-16 encoding of range U+E0100..U+E01EF is DB40 DD00 .. DB40 DDEF |
| 887 | if (c == 0xDB40 && 0xDD00 <= c2 && c2 <= 0xDDEF) { |
| 888 | return hasGlyphVariation(paint, typeface, bidiFlags, str.get(), str.size()); |
| 889 | } |
| 890 | } else if (0xFE00 <= c && c <= 0xFE0F) { |
| 891 | return hasGlyphVariation(paint, typeface, bidiFlags, str.get(), str.size()); |
| 892 | } |
| 893 | nChars++; |
| 894 | } |
| 895 | Layout layout; |
| 896 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, str.get(), 0, str.size(), |
| 897 | str.size()); |
| 898 | size_t nGlyphs = layout.nGlyphs(); |
| 899 | if (nGlyphs != 1 && nChars > 1) { |
| 900 | // multiple-character input, and was not a ligature |
| 901 | // TODO: handle ZWJ/ZWNJ characters specially so we can detect certain ligatures |
| 902 | // in joining scripts, such as Arabic and Mongolian. |
| 903 | return false; |
| 904 | } |
| 905 | return nGlyphs > 0 && !layoutContainsNotdef(layout); |
| 906 | } |
| 907 | |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 908 | static jfloat doRunAdvance(const Paint* paint, TypefaceImpl* typeface, const jchar buf[], |
| 909 | jint start, jint count, jint bufSize, jboolean isRtl, jint offset) { |
| 910 | Layout layout; |
| 911 | int bidiFlags = isRtl ? kBidi_Force_RTL : kBidi_Force_LTR; |
| 912 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, buf, start, count, bufSize); |
| 913 | return getRunAdvance(layout, buf, start, count, offset); |
| 914 | } |
| 915 | |
| 916 | static jfloat getRunAdvance___CIIIIZI_F(JNIEnv *env, jclass, jlong paintHandle, |
| 917 | jlong typefaceHandle, jcharArray text, jint start, jint end, jint contextStart, |
| 918 | jint contextEnd, jboolean isRtl, jint offset) { |
| 919 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 920 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
John Reck | c6024cd | 2015-07-10 10:55:49 -0700 | [diff] [blame] | 921 | jchar* textArray = (jchar*) env->GetPrimitiveArrayCritical(text, NULL); |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 922 | jfloat result = doRunAdvance(paint, typeface, textArray + contextStart, |
Raph Levien | 36ff86c | 2015-06-03 10:58:33 -0700 | [diff] [blame] | 923 | start - contextStart, end - start, contextEnd - contextStart, isRtl, |
| 924 | offset - contextStart); |
John Reck | c6024cd | 2015-07-10 10:55:49 -0700 | [diff] [blame] | 925 | env->ReleasePrimitiveArrayCritical(text, textArray, JNI_ABORT); |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 926 | return result; |
| 927 | } |
| 928 | |
| 929 | static jint doOffsetForAdvance(const Paint* paint, TypefaceImpl* typeface, const jchar buf[], |
| 930 | jint start, jint count, jint bufSize, jboolean isRtl, jfloat advance) { |
| 931 | Layout layout; |
| 932 | int bidiFlags = isRtl ? kBidi_Force_RTL : kBidi_Force_LTR; |
| 933 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, buf, start, count, bufSize); |
| 934 | return getOffsetForAdvance(layout, buf, start, count, advance); |
| 935 | } |
| 936 | static jint getOffsetForAdvance___CIIIIZF_I(JNIEnv *env, jclass, jlong paintHandle, |
| 937 | jlong typefaceHandle, jcharArray text, jint start, jint end, jint contextStart, |
| 938 | jint contextEnd, jboolean isRtl, jfloat advance) { |
| 939 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 940 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(typefaceHandle); |
John Reck | c6024cd | 2015-07-10 10:55:49 -0700 | [diff] [blame] | 941 | jchar* textArray = (jchar*) env->GetPrimitiveArrayCritical(text, NULL); |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 942 | jint result = doOffsetForAdvance(paint, typeface, textArray + contextStart, |
| 943 | start - contextStart, end - start, contextEnd - contextStart, isRtl, advance); |
| 944 | result += contextStart; |
John Reck | c6024cd | 2015-07-10 10:55:49 -0700 | [diff] [blame] | 945 | env->ReleasePrimitiveArrayCritical(text, textArray, JNI_ABORT); |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 946 | return result; |
| 947 | } |
| 948 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 949 | }; // namespace PaintGlue |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 951 | static const JNINativeMethod methods[] = { |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 952 | {"nFinalizer", "(J)V", (void*) PaintGlue::finalizer}, |
| 953 | {"nInit","()J", (void*) PaintGlue::init}, |
| 954 | {"nInitWithPaint","(J)J", (void*) PaintGlue::initWithPaint}, |
Chris Craik | 4136a0a | 2014-10-07 15:09:46 -0700 | [diff] [blame] | 955 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 956 | {"nReset","!(J)V", (void*) PaintGlue::reset}, |
| 957 | {"nSet","!(JJ)V", (void*) PaintGlue::assign}, |
| 958 | {"nGetFlags","!(J)I", (void*) PaintGlue::getFlags}, |
| 959 | {"nSetFlags","!(JI)V", (void*) PaintGlue::setFlags}, |
| 960 | {"nGetHinting","!(J)I", (void*) PaintGlue::getHinting}, |
| 961 | {"nSetHinting","!(JI)V", (void*) PaintGlue::setHinting}, |
| 962 | {"nSetAntiAlias","!(JZ)V", (void*) PaintGlue::setAntiAlias}, |
| 963 | {"nSetSubpixelText","!(JZ)V", (void*) PaintGlue::setSubpixelText}, |
| 964 | {"nSetLinearText","!(JZ)V", (void*) PaintGlue::setLinearText}, |
| 965 | {"nSetUnderlineText","!(JZ)V", (void*) PaintGlue::setUnderlineText}, |
| 966 | {"nSetStrikeThruText","!(JZ)V", (void*) PaintGlue::setStrikeThruText}, |
| 967 | {"nSetFakeBoldText","!(JZ)V", (void*) PaintGlue::setFakeBoldText}, |
| 968 | {"nSetFilterBitmap","!(JZ)V", (void*) PaintGlue::setFilterBitmap}, |
| 969 | {"nSetDither","!(JZ)V", (void*) PaintGlue::setDither}, |
| 970 | {"nGetStyle","!(J)I", (void*) PaintGlue::getStyle}, |
| 971 | {"nSetStyle","!(JI)V", (void*) PaintGlue::setStyle}, |
| 972 | {"nGetColor","!(J)I", (void*) PaintGlue::getColor}, |
| 973 | {"nSetColor","!(JI)V", (void*) PaintGlue::setColor}, |
| 974 | {"nGetAlpha","!(J)I", (void*) PaintGlue::getAlpha}, |
| 975 | {"nSetAlpha","!(JI)V", (void*) PaintGlue::setAlpha}, |
| 976 | {"nGetStrokeWidth","!(J)F", (void*) PaintGlue::getStrokeWidth}, |
| 977 | {"nSetStrokeWidth","!(JF)V", (void*) PaintGlue::setStrokeWidth}, |
| 978 | {"nGetStrokeMiter","!(J)F", (void*) PaintGlue::getStrokeMiter}, |
| 979 | {"nSetStrokeMiter","!(JF)V", (void*) PaintGlue::setStrokeMiter}, |
| 980 | {"nGetStrokeCap","!(J)I", (void*) PaintGlue::getStrokeCap}, |
| 981 | {"nSetStrokeCap","!(JI)V", (void*) PaintGlue::setStrokeCap}, |
| 982 | {"nGetStrokeJoin","!(J)I", (void*) PaintGlue::getStrokeJoin}, |
| 983 | {"nSetStrokeJoin","!(JI)V", (void*) PaintGlue::setStrokeJoin}, |
| 984 | {"nGetFillPath","!(JJJ)Z", (void*) PaintGlue::getFillPath}, |
| 985 | {"nSetShader","!(JJ)J", (void*) PaintGlue::setShader}, |
| 986 | {"nSetColorFilter","!(JJ)J", (void*) PaintGlue::setColorFilter}, |
| 987 | {"nSetXfermode","!(JJ)J", (void*) PaintGlue::setXfermode}, |
| 988 | {"nSetPathEffect","!(JJ)J", (void*) PaintGlue::setPathEffect}, |
| 989 | {"nSetMaskFilter","!(JJ)J", (void*) PaintGlue::setMaskFilter}, |
| 990 | {"nSetTypeface","!(JJ)J", (void*) PaintGlue::setTypeface}, |
| 991 | {"nSetRasterizer","!(JJ)J", (void*) PaintGlue::setRasterizer}, |
| 992 | {"nGetTextAlign","!(J)I", (void*) PaintGlue::getTextAlign}, |
| 993 | {"nSetTextAlign","!(JI)V", (void*) PaintGlue::setTextAlign}, |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 994 | {"nSetTextLocales","!(JLjava/lang/String;)V", (void*) PaintGlue::setTextLocales}, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 995 | {"nIsElegantTextHeight","!(J)Z", (void*) PaintGlue::isElegantTextHeight}, |
| 996 | {"nSetElegantTextHeight","!(JZ)V", (void*) PaintGlue::setElegantTextHeight}, |
| 997 | {"nGetTextSize","!(J)F", (void*) PaintGlue::getTextSize}, |
| 998 | {"nSetTextSize","!(JF)V", (void*) PaintGlue::setTextSize}, |
| 999 | {"nGetTextScaleX","!(J)F", (void*) PaintGlue::getTextScaleX}, |
| 1000 | {"nSetTextScaleX","!(JF)V", (void*) PaintGlue::setTextScaleX}, |
| 1001 | {"nGetTextSkewX","!(J)F", (void*) PaintGlue::getTextSkewX}, |
| 1002 | {"nSetTextSkewX","!(JF)V", (void*) PaintGlue::setTextSkewX}, |
| 1003 | {"nGetLetterSpacing","!(J)F", (void*) PaintGlue::getLetterSpacing}, |
| 1004 | {"nSetLetterSpacing","!(JF)V", (void*) PaintGlue::setLetterSpacing}, |
| 1005 | {"nSetFontFeatureSettings","(JLjava/lang/String;)V", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 1006 | (void*) PaintGlue::setFontFeatureSettings}, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 1007 | {"nGetHyphenEdit", "!(J)I", (void*) PaintGlue::getHyphenEdit}, |
| 1008 | {"nSetHyphenEdit", "!(JI)V", (void*) PaintGlue::setHyphenEdit}, |
| 1009 | {"nAscent","!(JJ)F", (void*) PaintGlue::ascent}, |
| 1010 | {"nDescent","!(JJ)F", (void*) PaintGlue::descent}, |
Chris Craik | 4136a0a | 2014-10-07 15:09:46 -0700 | [diff] [blame] | 1011 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 1012 | {"nGetFontMetrics", "!(JJLandroid/graphics/Paint$FontMetrics;)F", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 1013 | (void*)PaintGlue::getFontMetrics}, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 1014 | {"nGetFontMetricsInt", "!(JJLandroid/graphics/Paint$FontMetricsInt;)I", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 1015 | (void*)PaintGlue::getFontMetricsInt}, |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 1016 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 1017 | {"nBreakText","(JJ[CIIFI[F)I", (void*) PaintGlue::breakTextC}, |
| 1018 | {"nBreakText","(JJLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS}, |
| 1019 | {"nGetTextAdvances","(JJ[CIIIII[FI)F", |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 1020 | (void*) PaintGlue::getTextAdvances___CIIIII_FI}, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 1021 | {"nGetTextAdvances","(JJLjava/lang/String;IIIII[FI)F", |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 1022 | (void*) PaintGlue::getTextAdvances__StringIIIII_FI}, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 1023 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 1024 | {"nGetTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C}, |
| 1025 | {"nGetTextRunCursor", "(JLjava/lang/String;IIIII)I", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 1026 | (void*) PaintGlue::getTextRunCursor__String}, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 1027 | {"nGetTextPath", "(JJI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C}, |
| 1028 | {"nGetTextPath", "(JJILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String}, |
| 1029 | {"nGetStringBounds", "(JJLjava/lang/String;IIILandroid/graphics/Rect;)V", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 1030 | (void*) PaintGlue::getStringBounds }, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 1031 | {"nGetCharArrayBounds", "(JJ[CIIILandroid/graphics/Rect;)V", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 1032 | (void*) PaintGlue::getCharArrayBounds }, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 1033 | {"nHasGlyph", "(JJILjava/lang/String;)Z", (void*) PaintGlue::hasGlyph }, |
| 1034 | {"nGetRunAdvance", "(JJ[CIIIIZI)F", (void*) PaintGlue::getRunAdvance___CIIIIZI_F}, |
| 1035 | {"nGetOffsetForAdvance", "(JJ[CIIIIZF)I", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 1036 | (void*) PaintGlue::getOffsetForAdvance___CIIIIZF_I}, |
Chris Craik | 4136a0a | 2014-10-07 15:09:46 -0700 | [diff] [blame] | 1037 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 1038 | {"nSetShadowLayer", "!(JFFFI)V", (void*)PaintGlue::setShadowLayer}, |
| 1039 | {"nHasShadowLayer", "!(J)Z", (void*)PaintGlue::hasShadowLayer} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | }; |
| 1041 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | int register_android_graphics_Paint(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1043 | gFontMetrics_class = FindClassOrDie(env, "android/graphics/Paint$FontMetrics"); |
| 1044 | gFontMetrics_class = MakeGlobalRefOrDie(env, gFontMetrics_class); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1046 | gFontMetrics_fieldID.top = GetFieldIDOrDie(env, gFontMetrics_class, "top", "F"); |
| 1047 | gFontMetrics_fieldID.ascent = GetFieldIDOrDie(env, gFontMetrics_class, "ascent", "F"); |
| 1048 | gFontMetrics_fieldID.descent = GetFieldIDOrDie(env, gFontMetrics_class, "descent", "F"); |
| 1049 | gFontMetrics_fieldID.bottom = GetFieldIDOrDie(env, gFontMetrics_class, "bottom", "F"); |
| 1050 | gFontMetrics_fieldID.leading = GetFieldIDOrDie(env, gFontMetrics_class, "leading", "F"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1052 | gFontMetricsInt_class = FindClassOrDie(env, "android/graphics/Paint$FontMetricsInt"); |
| 1053 | gFontMetricsInt_class = MakeGlobalRefOrDie(env, gFontMetricsInt_class); |
Derek Sollenberger | ab22c1c | 2014-09-03 14:12:47 -0400 | [diff] [blame] | 1054 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1055 | gFontMetricsInt_fieldID.top = GetFieldIDOrDie(env, gFontMetricsInt_class, "top", "I"); |
| 1056 | gFontMetricsInt_fieldID.ascent = GetFieldIDOrDie(env, gFontMetricsInt_class, "ascent", "I"); |
| 1057 | gFontMetricsInt_fieldID.descent = GetFieldIDOrDie(env, gFontMetricsInt_class, "descent", "I"); |
| 1058 | gFontMetricsInt_fieldID.bottom = GetFieldIDOrDie(env, gFontMetricsInt_class, "bottom", "I"); |
| 1059 | gFontMetricsInt_fieldID.leading = GetFieldIDOrDie(env, gFontMetricsInt_class, "leading", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1061 | return RegisterMethodsOrDie(env, "android/graphics/Paint", methods, NELEM(methods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | } |