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" |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 32 | #include "SkPathEffect.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #include "SkRasterizer.h" |
| 34 | #include "SkShader.h" |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 35 | #include "SkBlendMode.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 | |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 40 | #include <hwui/MinikinSkia.h> |
| 41 | #include <hwui/MinikinUtils.h> |
| 42 | #include <hwui/Paint.h> |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 43 | #include <hwui/Typeface.h> |
Raph Levien | e368b6b | 2014-06-15 17:37:57 -0700 | [diff] [blame] | 44 | #include <minikin/GraphemeBreak.h> |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 45 | #include <minikin/Measurement.h> |
Seigo Nonaka | bb1a966 | 2015-10-21 23:24:25 +0900 | [diff] [blame] | 46 | #include <unicode/utf16.h> |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 47 | |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 48 | #include <cassert> |
| 49 | #include <cstring> |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 50 | #include <memory> |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 51 | #include <vector> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | namespace android { |
| 54 | |
| 55 | struct JMetricsID { |
| 56 | jfieldID top; |
| 57 | jfieldID ascent; |
| 58 | jfieldID descent; |
| 59 | jfieldID bottom; |
| 60 | jfieldID leading; |
| 61 | }; |
| 62 | |
| 63 | static jclass gFontMetrics_class; |
| 64 | static JMetricsID gFontMetrics_fieldID; |
| 65 | |
| 66 | static jclass gFontMetricsInt_class; |
| 67 | static JMetricsID gFontMetricsInt_fieldID; |
| 68 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 69 | static void defaultSettingsForAndroid(Paint* paint) { |
Fabrice Di Meglio | c511bee8 | 2012-01-05 13:30:54 -0800 | [diff] [blame] | 70 | // GlyphID encoding is required because we are using Harfbuzz shaping |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 71 | paint->setTextEncoding(Paint::kGlyphID_TextEncoding); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 72 | } |
| 73 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 74 | namespace PaintGlue { |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 75 | enum MoveOpt { |
| 76 | AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT |
| 77 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
Richard Uhler | 775873a | 2015-12-29 12:37:39 -0800 | [diff] [blame] | 79 | static void deletePaint(Paint* paint) { |
| 80 | delete paint; |
| 81 | } |
| 82 | |
| 83 | static jlong getNativeFinalizer(JNIEnv*, jobject) { |
| 84 | return static_cast<jlong>(reinterpret_cast<uintptr_t>(&deletePaint)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | } |
| 86 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 87 | static jlong init(JNIEnv* env, jobject) { |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 88 | static_assert(1 << 0 == SkPaint::kAntiAlias_Flag, "paint_flags_mismatch"); |
| 89 | static_assert(1 << 2 == SkPaint::kDither_Flag, "paint_flags_mismatch"); |
| 90 | static_assert(1 << 3 == SkPaint::kUnderlineText_Flag, "paint_flags_mismatch"); |
| 91 | static_assert(1 << 4 == SkPaint::kStrikeThruText_Flag, "paint_flags_mismatch"); |
| 92 | static_assert(1 << 5 == SkPaint::kFakeBoldText_Flag, "paint_flags_mismatch"); |
| 93 | static_assert(1 << 6 == SkPaint::kLinearText_Flag, "paint_flags_mismatch"); |
| 94 | static_assert(1 << 7 == SkPaint::kSubpixelText_Flag, "paint_flags_mismatch"); |
| 95 | static_assert(1 << 8 == SkPaint::kDevKernText_Flag, "paint_flags_mismatch"); |
| 96 | static_assert(1 << 10 == SkPaint::kEmbeddedBitmapText_Flag, "paint_flags_mismatch"); |
Derek Sollenberger | 0b86062 | 2015-03-25 10:08:30 -0400 | [diff] [blame] | 97 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 98 | Paint* obj = new Paint(); |
Mike Reed | 3d63e01 | 2009-07-27 09:50:31 -0400 | [diff] [blame] | 99 | defaultSettingsForAndroid(obj); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 100 | return reinterpret_cast<jlong>(obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 103 | static jlong initWithPaint(JNIEnv* env, jobject clazz, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 104 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 105 | Paint* obj = new Paint(*paint); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 106 | return reinterpret_cast<jlong>(obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 108 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 109 | static int breakText(JNIEnv* env, const Paint& paint, Typeface* typeface, const jchar text[], |
| 110 | int count, float maxWidth, jint bidiFlags, jfloatArray jmeasured, |
| 111 | const bool forwardScan) { |
| 112 | size_t measuredCount = 0; |
| 113 | float measured = 0; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 114 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 115 | std::unique_ptr<float[]> advancesArray(new float[count]); |
| 116 | MinikinUtils::measureText(&paint, bidiFlags, typeface, text, 0, count, count, |
| 117 | advancesArray.get()); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 118 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 119 | for (int i = 0; i < count; i++) { |
| 120 | // traverse in the given direction |
| 121 | int index = forwardScan ? i : (count - i - 1); |
| 122 | float width = advancesArray[index]; |
| 123 | if (measured + width > maxWidth) { |
| 124 | break; |
| 125 | } |
| 126 | // properly handle clusters when scanning backwards |
| 127 | if (forwardScan || width != 0.0f) { |
| 128 | measuredCount = i + 1; |
| 129 | } |
| 130 | measured += width; |
Mike Reed | 4c9355c | 2014-05-07 11:48:37 -0400 | [diff] [blame] | 131 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 132 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 133 | if (jmeasured && env->GetArrayLength(jmeasured) > 0) { |
| 134 | AutoJavaFloatArray autoMeasured(env, jmeasured, 1); |
| 135 | jfloat* array = autoMeasured.ptr(); |
| 136 | array[0] = measured; |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 137 | } |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 138 | return measuredCount; |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 139 | } |
| 140 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 141 | static jint breakTextC(JNIEnv* env, jobject clazz, jlong paintHandle, jlong typefaceHandle, |
| 142 | jcharArray jtext, jint index, jint count, jfloat maxWidth, jint bidiFlags, |
| 143 | jfloatArray jmeasuredWidth) { |
| 144 | NPE_CHECK_RETURN_ZERO(env, jtext); |
Raph Levien | 210a189 | 2015-03-09 14:42:14 -0700 | [diff] [blame] | 145 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 146 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 147 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 148 | |
| 149 | bool forwardTextDirection; |
| 150 | if (count < 0) { |
| 151 | forwardTextDirection = false; |
| 152 | count = -count; |
Raph Levien | 53c0077 | 2014-04-14 14:11:02 -0700 | [diff] [blame] | 153 | } |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 154 | else { |
| 155 | forwardTextDirection = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | } |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 157 | |
| 158 | if ((index < 0) || (index + count > env->GetArrayLength(jtext))) { |
| 159 | doThrowAIOOBE(env); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | const jchar* text = env->GetCharArrayElements(jtext, nullptr); |
| 164 | count = breakText(env, *paint, typeface, text + index, count, maxWidth, |
| 165 | bidiFlags, jmeasuredWidth, forwardTextDirection); |
| 166 | env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text), |
| 167 | JNI_ABORT); |
| 168 | return count; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 170 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 171 | static jint breakTextS(JNIEnv* env, jobject clazz, jlong paintHandle, jlong typefaceHandle, jstring jtext, |
| 172 | jboolean forwards, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) { |
| 173 | NPE_CHECK_RETURN_ZERO(env, jtext); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 174 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 175 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 176 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 178 | int count = env->GetStringLength(jtext); |
| 179 | const jchar* text = env->GetStringChars(jtext, nullptr); |
| 180 | count = breakText(env, *paint, typeface, text, count, maxWidth, bidiFlags, jmeasuredWidth, forwards); |
| 181 | env->ReleaseStringChars(jtext, text); |
| 182 | return count; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
| 184 | |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 185 | static jfloat doTextAdvances(JNIEnv *env, Paint *paint, Typeface* typeface, |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 186 | const jchar *text, jint start, jint count, jint contextCount, jint bidiFlags, |
| 187 | jfloatArray advances, jint advancesIndex) { |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 188 | NPE_CHECK_RETURN_ZERO(env, text); |
| 189 | |
| 190 | if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) { |
| 191 | doThrowAIOOBE(env); |
| 192 | return 0; |
| 193 | } |
| 194 | if (count == 0) { |
| 195 | return 0; |
| 196 | } |
| 197 | if (advances) { |
| 198 | size_t advancesLength = env->GetArrayLength(advances); |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 199 | if ((size_t)(count + advancesIndex) > advancesLength) { |
Fabrice Di Meglio | 6ab90ed | 2011-08-08 16:19:38 -0700 | [diff] [blame] | 200 | doThrowAIOOBE(env); |
| 201 | return 0; |
| 202 | } |
| 203 | } |
Keisuke Kuroyanagi | a3024bd | 2015-09-16 20:02:15 -0700 | [diff] [blame] | 204 | std::unique_ptr<jfloat[]> advancesArray; |
| 205 | if (advances) { |
| 206 | advancesArray.reset(new jfloat[count]); |
| 207 | } |
| 208 | const float advance = MinikinUtils::measureText(paint, bidiFlags, typeface, text, |
| 209 | start, count, contextCount, advancesArray.get()); |
| 210 | if (advances) { |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 211 | env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray.get()); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 212 | } |
Keisuke Kuroyanagi | a3024bd | 2015-09-16 20:02:15 -0700 | [diff] [blame] | 213 | return advance; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 216 | static jfloat getTextAdvances___CIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle, |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 217 | jlong typefaceHandle, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 218 | jcharArray text, jint index, jint count, jint contextIndex, jint contextCount, |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 219 | jint bidiFlags, jfloatArray advances, jint advancesIndex) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 220 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 221 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 222 | jchar* textArray = env->GetCharArrayElements(text, nullptr); |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 223 | jfloat result = doTextAdvances(env, paint, typeface, textArray + contextIndex, |
| 224 | index - contextIndex, count, contextCount, bidiFlags, advances, advancesIndex); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 225 | env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); |
| 226 | return result; |
| 227 | } |
| 228 | |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 229 | static jfloat getTextAdvances__StringIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle, |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 230 | jlong typefaceHandle, |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 231 | 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] | 232 | jfloatArray advances, jint advancesIndex) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 233 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 234 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 235 | const jchar* textArray = env->GetStringChars(text, nullptr); |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 236 | jfloat result = doTextAdvances(env, paint, typeface, textArray + contextStart, |
| 237 | start - contextStart, end - start, contextEnd - contextStart, bidiFlags, |
Fabrice Di Meglio | 665f02c | 2013-03-20 14:56:05 -0700 | [diff] [blame] | 238 | advances, advancesIndex); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 239 | env->ReleaseStringChars(text, textArray); |
| 240 | return result; |
| 241 | } |
| 242 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 243 | 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] | 244 | jint count, jint flags, jint offset, jint opt) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 245 | minikin::GraphemeBreak::MoveOpt moveOpt = minikin::GraphemeBreak::MoveOpt(opt); |
| 246 | size_t result = minikin::GraphemeBreak::getTextRunCursor(text, start, count, offset, |
| 247 | moveOpt); |
Raph Levien | e368b6b | 2014-06-15 17:37:57 -0700 | [diff] [blame] | 248 | return static_cast<jint>(result); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 251 | static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 252 | jint contextStart, jint contextCount, jint dir, jint offset, jint cursorOpt) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 253 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 254 | jchar* textArray = env->GetCharArrayElements(text, nullptr); |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 255 | jint result = doTextRunCursor(env, paint, textArray, contextStart, contextCount, dir, |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 256 | offset, cursorOpt); |
| 257 | env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); |
| 258 | return result; |
| 259 | } |
| 260 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 261 | static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, jlong paintHandle, jstring text, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 262 | jint contextStart, jint contextEnd, jint dir, jint offset, jint cursorOpt) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 263 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 264 | const jchar* textArray = env->GetStringChars(text, nullptr); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 265 | jint result = doTextRunCursor(env, paint, textArray, contextStart, |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 266 | contextEnd - contextStart, dir, offset, cursorOpt); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 267 | env->ReleaseStringChars(text, textArray); |
| 268 | return result; |
| 269 | } |
| 270 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 271 | class GetTextFunctor { |
| 272 | public: |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 273 | GetTextFunctor(const minikin::Layout& layout, SkPath* path, jfloat x, jfloat y, |
| 274 | Paint* paint, uint16_t* glyphs, SkPoint* pos) |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 275 | : layout(layout), path(path), x(x), y(y), paint(paint), glyphs(glyphs), pos(pos) { |
| 276 | } |
| 277 | |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 278 | void operator()(size_t start, size_t end) { |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 279 | for (size_t i = start; i < end; i++) { |
| 280 | glyphs[i] = layout.getGlyphId(i); |
| 281 | pos[i].fX = x + layout.getX(i); |
| 282 | pos[i].fY = y + layout.getY(i); |
| 283 | } |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 284 | if (start == 0) { |
| 285 | paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, path); |
| 286 | } else { |
| 287 | paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, &tmpPath); |
| 288 | path->addPath(tmpPath); |
| 289 | } |
| 290 | } |
| 291 | private: |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 292 | const minikin::Layout& layout; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 293 | SkPath* path; |
| 294 | jfloat x; |
| 295 | jfloat y; |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 296 | Paint* paint; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 297 | uint16_t* glyphs; |
| 298 | SkPoint* pos; |
| 299 | SkPath tmpPath; |
| 300 | }; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 301 | |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 302 | static void getTextPath(JNIEnv* env, Paint* paint, Typeface* typeface, const jchar* text, |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 303 | jint count, jint bidiFlags, jfloat x, jfloat y, SkPath* path) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 304 | minikin::Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 305 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, 0, count, count); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 306 | size_t nGlyphs = layout.nGlyphs(); |
| 307 | uint16_t* glyphs = new uint16_t[nGlyphs]; |
| 308 | SkPoint* pos = new SkPoint[nGlyphs]; |
| 309 | |
| 310 | x += MinikinUtils::xOffsetForTextAlign(paint, layout); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 311 | Paint::Align align = paint->getTextAlign(); |
| 312 | paint->setTextAlign(Paint::kLeft_Align); |
| 313 | paint->setTextEncoding(Paint::kGlyphID_TextEncoding); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 314 | GetTextFunctor f(layout, path, x, y, paint, glyphs, pos); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 315 | MinikinUtils::forFontRun(layout, paint, f); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 316 | paint->setTextAlign(align); |
| 317 | delete[] glyphs; |
| 318 | delete[] pos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 320 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 321 | static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle, |
| 322 | jlong typefaceHandle, jint bidiFlags, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 323 | jcharArray text, jint index, jint count, jfloat x, jfloat y, jlong pathHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 324 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 325 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 326 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 327 | const jchar* textArray = env->GetCharArrayElements(text, nullptr); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 328 | getTextPath(env, paint, typeface, textArray + index, count, bidiFlags, x, y, path); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 329 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT); |
| 330 | } |
| 331 | |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 332 | static void getTextPath__String(JNIEnv* env, jobject clazz, jlong paintHandle, |
| 333 | jlong typefaceHandle, jint bidiFlags, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 334 | jstring text, jint start, jint end, jfloat x, jfloat y, jlong pathHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 335 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 336 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 337 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 338 | const jchar* textArray = env->GetStringChars(text, nullptr); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 339 | 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] | 340 | env->ReleaseStringChars(text, textArray); |
| 341 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 342 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 343 | static void doTextBounds(JNIEnv* env, const jchar* text, int count, jobject bounds, |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 344 | const Paint& paint, Typeface* typeface, jint bidiFlags) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 345 | SkRect r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | SkIRect ir; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 347 | |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 348 | minikin::Layout layout; |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 349 | MinikinUtils::doLayout(&layout, &paint, bidiFlags, typeface, text, 0, count, count); |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 350 | minikin::MinikinRect rect; |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 351 | layout.getBounds(&rect); |
| 352 | r.fLeft = rect.mLeft; |
| 353 | r.fTop = rect.mTop; |
| 354 | r.fRight = rect.mRight; |
| 355 | r.fBottom = rect.mBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | r.roundOut(&ir); |
| 357 | GraphicsJNI::irect_to_jrect(ir, env, bounds); |
| 358 | } |
| 359 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 360 | static void getStringBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 361 | jstring text, jint start, jint end, jint bidiFlags, jobject bounds) { |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 362 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 363 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 364 | const jchar* textArray = env->GetStringChars(text, nullptr); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 365 | 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] | 366 | env->ReleaseStringChars(text, textArray); |
| 367 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 368 | |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 369 | static void getCharArrayBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 370 | jcharArray text, jint index, jint count, jint bidiFlags, jobject bounds) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 371 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 372 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 373 | const jchar* textArray = env->GetCharArrayElements(text, nullptr); |
Raph Levien | 854363e | 2014-06-03 19:56:05 -0700 | [diff] [blame] | 374 | doTextBounds(env, textArray + index, count, bounds, *paint, typeface, bidiFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), |
| 376 | JNI_ABORT); |
| 377 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 378 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 379 | // Returns true if the given string is exact one pair of regional indicators. |
| 380 | static bool isFlag(const jchar* str, size_t length) { |
| 381 | const jchar RI_LEAD_SURROGATE = 0xD83C; |
| 382 | const jchar RI_TRAIL_SURROGATE_MIN = 0xDDE6; |
| 383 | const jchar RI_TRAIL_SURROGATE_MAX = 0xDDFF; |
| 384 | |
| 385 | if (length != 4) { |
| 386 | return false; |
| 387 | } |
| 388 | if (str[0] != RI_LEAD_SURROGATE || str[2] != RI_LEAD_SURROGATE) { |
| 389 | return false; |
| 390 | } |
| 391 | return RI_TRAIL_SURROGATE_MIN <= str[1] && str[1] <= RI_TRAIL_SURROGATE_MAX && |
| 392 | RI_TRAIL_SURROGATE_MIN <= str[3] && str[3] <= RI_TRAIL_SURROGATE_MAX; |
| 393 | } |
| 394 | |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 395 | static jboolean layoutContainsNotdef(const minikin::Layout& layout) { |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 396 | for (size_t i = 0; i < layout.nGlyphs(); i++) { |
| 397 | if (layout.getGlyphId(i) == 0) { |
| 398 | return true; |
| 399 | } |
| 400 | } |
| 401 | return false; |
| 402 | } |
| 403 | |
Raph Levien | 6141db3 | 2016-07-11 13:59:58 -0700 | [diff] [blame] | 404 | // Don't count glyphs that are the recommended "space" glyph and are zero width. |
| 405 | // This logic makes assumptions about HarfBuzz layout, but does correctly handle |
| 406 | // cases where ligatures form and zero width space glyphs are left in as |
| 407 | // placeholders. |
| 408 | static size_t countNonSpaceGlyphs(const minikin::Layout& layout) { |
| 409 | size_t count = 0; |
| 410 | static unsigned int kSpaceGlyphId = 3; |
| 411 | for (size_t i = 0; i < layout.nGlyphs(); i++) { |
| 412 | if (layout.getGlyphId(i) != kSpaceGlyphId || layout.getCharAdvance(i) != 0.0) { |
| 413 | count++; |
| 414 | } |
| 415 | } |
| 416 | return count; |
| 417 | } |
| 418 | |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 419 | static jboolean hasGlyph(JNIEnv *env, jclass, jlong paintHandle, jlong typefaceHandle, |
| 420 | jint bidiFlags, jstring string) { |
| 421 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 422 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 423 | ScopedStringChars str(env, string); |
| 424 | |
Seigo Nonaka | bb1a966 | 2015-10-21 23:24:25 +0900 | [diff] [blame] | 425 | /* Start by rejecting unsupported base code point and variation selector pairs. */ |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 426 | size_t nChars = 0; |
Seigo Nonaka | bb1a966 | 2015-10-21 23:24:25 +0900 | [diff] [blame] | 427 | const uint32_t kStartOfString = 0xFFFFFFFF; |
| 428 | uint32_t prevCp = kStartOfString; |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 429 | for (size_t i = 0; i < str.size(); i++) { |
Seigo Nonaka | bb1a966 | 2015-10-21 23:24:25 +0900 | [diff] [blame] | 430 | jchar cu = str[i]; |
| 431 | uint32_t cp = cu; |
| 432 | if (U16_IS_TRAIL(cu)) { |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 433 | // invalid UTF-16, unpaired trailing surrogate |
| 434 | return false; |
Seigo Nonaka | bb1a966 | 2015-10-21 23:24:25 +0900 | [diff] [blame] | 435 | } else if (U16_IS_LEAD(cu)) { |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 436 | if (i + 1 == str.size()) { |
| 437 | // invalid UTF-16, unpaired leading surrogate at end of string |
| 438 | return false; |
| 439 | } |
| 440 | i++; |
Seigo Nonaka | bb1a966 | 2015-10-21 23:24:25 +0900 | [diff] [blame] | 441 | jchar cu2 = str[i]; |
| 442 | if (!U16_IS_TRAIL(cu2)) { |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 443 | // invalid UTF-16, unpaired leading surrogate |
| 444 | return false; |
| 445 | } |
Seigo Nonaka | bb1a966 | 2015-10-21 23:24:25 +0900 | [diff] [blame] | 446 | cp = U16_GET_SUPPLEMENTARY(cu, cu2); |
| 447 | } |
| 448 | |
| 449 | if (prevCp != kStartOfString && |
Raph Levien | 032a359 | 2016-04-14 21:22:37 -0700 | [diff] [blame] | 450 | ((0xFE00 <= cp && cp <= 0xFE0F) || (0xE0100 <= cp && cp <= 0xE01EF))) { |
| 451 | bool hasVS = MinikinUtils::hasVariationSelector(typeface, prevCp, cp); |
| 452 | if (!hasVS) { |
| 453 | // No font has a glyph for the code point and variation selector pair. |
| 454 | return false; |
| 455 | } else if (nChars == 1 && i + 1 == str.size()) { |
| 456 | // The string is just a codepoint and a VS, we have an authoritative answer |
| 457 | return true; |
| 458 | } |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 459 | } |
| 460 | nChars++; |
Seigo Nonaka | bb1a966 | 2015-10-21 23:24:25 +0900 | [diff] [blame] | 461 | prevCp = cp; |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 462 | } |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 463 | minikin::Layout layout; |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 464 | MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, str.get(), 0, str.size(), |
| 465 | str.size()); |
Raph Levien | 6141db3 | 2016-07-11 13:59:58 -0700 | [diff] [blame] | 466 | size_t nGlyphs = countNonSpaceGlyphs(layout); |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 467 | if (nGlyphs != 1 && nChars > 1) { |
| 468 | // multiple-character input, and was not a ligature |
| 469 | // TODO: handle ZWJ/ZWNJ characters specially so we can detect certain ligatures |
| 470 | // in joining scripts, such as Arabic and Mongolian. |
| 471 | return false; |
| 472 | } |
Seigo Nonaka | 589cddc | 2016-04-05 14:43:14 +0900 | [diff] [blame] | 473 | |
| 474 | if (nGlyphs == 0 || layoutContainsNotdef(layout)) { |
| 475 | return false; // The collection doesn't have a glyph. |
| 476 | } |
| 477 | |
| 478 | if (nChars == 2 && isFlag(str.get(), str.size())) { |
| 479 | // Some font may have a special glyph for unsupported regional indicator pairs. |
| 480 | // To return false for this case, need to compare the glyph id with the one of ZZ |
| 481 | // since ZZ is reserved for unknown or invalid territory. |
| 482 | // U+1F1FF (REGIONAL INDICATOR SYMBOL LETTER Z) is \uD83C\uDDFF in UTF16. |
| 483 | static const jchar ZZ_FLAG_STR[] = { 0xD83C, 0xDDFF, 0xD83C, 0xDDFF }; |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 484 | minikin::Layout zzLayout; |
Seigo Nonaka | 589cddc | 2016-04-05 14:43:14 +0900 | [diff] [blame] | 485 | MinikinUtils::doLayout(&zzLayout, paint, bidiFlags, typeface, ZZ_FLAG_STR, 0, 4, 4); |
| 486 | if (zzLayout.nGlyphs() != 1 || layoutContainsNotdef(zzLayout)) { |
| 487 | // The font collection doesn't have a glyph for unknown flag. Just return true. |
| 488 | return true; |
| 489 | } |
| 490 | return zzLayout.getGlyphId(0) != layout.getGlyphId(0); |
| 491 | } |
| 492 | return true; |
Raph Levien | f7f969e6 | 2015-04-01 14:41:21 -0700 | [diff] [blame] | 493 | } |
| 494 | |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 495 | static jfloat doRunAdvance(const Paint* paint, Typeface* typeface, const jchar buf[], |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 496 | jint start, jint count, jint bufSize, jboolean isRtl, jint offset) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 497 | int bidiFlags = isRtl ? minikin::kBidi_Force_RTL : minikin::kBidi_Force_LTR; |
Keisuke Kuroyanagi | b367771 | 2016-03-28 19:16:30 +0900 | [diff] [blame] | 498 | if (offset == start + count) { |
Keisuke Kuroyanagi | 4c8a526 | 2016-02-18 11:31:56 -0800 | [diff] [blame] | 499 | return MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, |
| 500 | bufSize, nullptr); |
| 501 | } |
| 502 | std::unique_ptr<float[]> advancesArray(new float[count]); |
| 503 | MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize, |
| 504 | advancesArray.get()); |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 505 | return minikin::getRunAdvance(advancesArray.get(), buf, start, count, offset); |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static jfloat getRunAdvance___CIIIIZI_F(JNIEnv *env, jclass, jlong paintHandle, |
| 509 | jlong typefaceHandle, jcharArray text, jint start, jint end, jint contextStart, |
| 510 | jint contextEnd, jboolean isRtl, jint offset) { |
| 511 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 512 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 513 | jchar* textArray = (jchar*) env->GetPrimitiveArrayCritical(text, nullptr); |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 514 | jfloat result = doRunAdvance(paint, typeface, textArray + contextStart, |
Raph Levien | 36ff86c | 2015-06-03 10:58:33 -0700 | [diff] [blame] | 515 | start - contextStart, end - start, contextEnd - contextStart, isRtl, |
| 516 | offset - contextStart); |
John Reck | c6024cd | 2015-07-10 10:55:49 -0700 | [diff] [blame] | 517 | env->ReleasePrimitiveArrayCritical(text, textArray, JNI_ABORT); |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 518 | return result; |
| 519 | } |
| 520 | |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 521 | static jint doOffsetForAdvance(const Paint* paint, Typeface* typeface, const jchar buf[], |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 522 | jint start, jint count, jint bufSize, jboolean isRtl, jfloat advance) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 523 | int bidiFlags = isRtl ? minikin::kBidi_Force_RTL : minikin::kBidi_Force_LTR; |
Keisuke Kuroyanagi | 4c8a526 | 2016-02-18 11:31:56 -0800 | [diff] [blame] | 524 | std::unique_ptr<float[]> advancesArray(new float[count]); |
| 525 | MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize, |
| 526 | advancesArray.get()); |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 527 | return minikin::getOffsetForAdvance(advancesArray.get(), buf, start, count, advance); |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 528 | } |
Keisuke Kuroyanagi | 4c8a526 | 2016-02-18 11:31:56 -0800 | [diff] [blame] | 529 | |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 530 | static jint getOffsetForAdvance___CIIIIZF_I(JNIEnv *env, jclass, jlong paintHandle, |
| 531 | jlong typefaceHandle, jcharArray text, jint start, jint end, jint contextStart, |
| 532 | jint contextEnd, jboolean isRtl, jfloat advance) { |
| 533 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 534 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 535 | jchar* textArray = (jchar*) env->GetPrimitiveArrayCritical(text, nullptr); |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 536 | jint result = doOffsetForAdvance(paint, typeface, textArray + contextStart, |
| 537 | start - contextStart, end - start, contextEnd - contextStart, isRtl, advance); |
| 538 | result += contextStart; |
John Reck | c6024cd | 2015-07-10 10:55:49 -0700 | [diff] [blame] | 539 | env->ReleasePrimitiveArrayCritical(text, textArray, JNI_ABORT); |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 540 | return result; |
| 541 | } |
| 542 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 543 | // ------------------ @FastNative --------------------------- |
| 544 | |
| 545 | static jint setTextLocales(JNIEnv* env, jobject clazz, jlong objHandle, jstring locales) { |
| 546 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 547 | ScopedUtfChars localesChars(env, locales); |
| 548 | jint minikinLangListId = minikin::FontStyle::registerLanguageList(localesChars.c_str()); |
| 549 | obj->setMinikinLangListId(minikinLangListId); |
| 550 | return minikinLangListId; |
| 551 | } |
| 552 | |
| 553 | static void setFontFeatureSettings(JNIEnv* env, jobject clazz, jlong paintHandle, jstring settings) { |
| 554 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 555 | if (!settings) { |
| 556 | paint->setFontFeatureSettings(std::string()); |
| 557 | } else { |
| 558 | ScopedUtfChars settingsChars(env, settings); |
| 559 | paint->setFontFeatureSettings(std::string(settingsChars.c_str(), settingsChars.size())); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | static SkScalar getMetricsInternal(jlong paintHandle, jlong typefaceHandle, |
| 564 | Paint::FontMetrics *metrics) { |
| 565 | const int kElegantTop = 2500; |
| 566 | const int kElegantBottom = -1000; |
| 567 | const int kElegantAscent = 1900; |
| 568 | const int kElegantDescent = -500; |
| 569 | const int kElegantLeading = 0; |
| 570 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 571 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
| 572 | typeface = Typeface::resolveDefault(typeface); |
| 573 | minikin::FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle); |
| 574 | float saveSkewX = paint->getTextSkewX(); |
| 575 | bool savefakeBold = paint->isFakeBoldText(); |
| 576 | MinikinFontSkia::populateSkPaint(paint, baseFont.font, baseFont.fakery); |
| 577 | SkScalar spacing = paint->getFontMetrics(metrics); |
| 578 | // The populateSkPaint call may have changed fake bold / text skew |
| 579 | // because we want to measure with those effects applied, so now |
| 580 | // restore the original settings. |
| 581 | paint->setTextSkewX(saveSkewX); |
| 582 | paint->setFakeBoldText(savefakeBold); |
| 583 | if (paint->getFontVariant() == minikin::VARIANT_ELEGANT) { |
| 584 | SkScalar size = paint->getTextSize(); |
| 585 | metrics->fTop = -size * kElegantTop / 2048; |
| 586 | metrics->fBottom = -size * kElegantBottom / 2048; |
| 587 | metrics->fAscent = -size * kElegantAscent / 2048; |
| 588 | metrics->fDescent = -size * kElegantDescent / 2048; |
| 589 | metrics->fLeading = size * kElegantLeading / 2048; |
| 590 | spacing = metrics->fDescent - metrics->fAscent + metrics->fLeading; |
| 591 | } |
| 592 | return spacing; |
| 593 | } |
| 594 | |
| 595 | static jfloat getFontMetrics(JNIEnv* env, jobject, jlong paintHandle, |
| 596 | jlong typefaceHandle, jobject metricsObj) { |
| 597 | Paint::FontMetrics metrics; |
| 598 | SkScalar spacing = getMetricsInternal(paintHandle, typefaceHandle, &metrics); |
| 599 | |
| 600 | if (metricsObj) { |
| 601 | SkASSERT(env->IsInstanceOf(metricsObj, gFontMetrics_class)); |
| 602 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.top, SkScalarToFloat(metrics.fTop)); |
| 603 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.ascent, SkScalarToFloat(metrics.fAscent)); |
| 604 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.descent, SkScalarToFloat(metrics.fDescent)); |
| 605 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.bottom, SkScalarToFloat(metrics.fBottom)); |
| 606 | env->SetFloatField(metricsObj, gFontMetrics_fieldID.leading, SkScalarToFloat(metrics.fLeading)); |
| 607 | } |
| 608 | return SkScalarToFloat(spacing); |
| 609 | } |
| 610 | |
| 611 | static jint getFontMetricsInt(JNIEnv* env, jobject, jlong paintHandle, |
| 612 | jlong typefaceHandle, jobject metricsObj) { |
| 613 | Paint::FontMetrics metrics; |
| 614 | |
| 615 | getMetricsInternal(paintHandle, typefaceHandle, &metrics); |
| 616 | int ascent = SkScalarRoundToInt(metrics.fAscent); |
| 617 | int descent = SkScalarRoundToInt(metrics.fDescent); |
| 618 | int leading = SkScalarRoundToInt(metrics.fLeading); |
| 619 | |
| 620 | if (metricsObj) { |
| 621 | SkASSERT(env->IsInstanceOf(metricsObj, gFontMetricsInt_class)); |
| 622 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.top, SkScalarFloorToInt(metrics.fTop)); |
| 623 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.ascent, ascent); |
| 624 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.descent, descent); |
| 625 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.bottom, SkScalarCeilToInt(metrics.fBottom)); |
| 626 | env->SetIntField(metricsObj, gFontMetricsInt_fieldID.leading, leading); |
| 627 | } |
| 628 | return descent - ascent + leading; |
| 629 | } |
| 630 | |
| 631 | |
| 632 | // ------------------ @CriticalNative --------------------------- |
| 633 | |
| 634 | static void reset(jlong objHandle) { |
| 635 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 636 | obj->reset(); |
| 637 | defaultSettingsForAndroid(obj); |
| 638 | } |
| 639 | |
| 640 | static void assign(jlong dstPaintHandle, jlong srcPaintHandle) { |
| 641 | Paint* dst = reinterpret_cast<Paint*>(dstPaintHandle); |
| 642 | const Paint* src = reinterpret_cast<Paint*>(srcPaintHandle); |
| 643 | *dst = *src; |
| 644 | } |
| 645 | |
| 646 | // Equivalent to the Java Paint's FILTER_BITMAP_FLAG. |
| 647 | static const uint32_t sFilterBitmapFlag = 0x02; |
| 648 | |
| 649 | static jint getFlags(jlong paintHandle) { |
| 650 | Paint* nativePaint = reinterpret_cast<Paint*>(paintHandle); |
| 651 | uint32_t result = nativePaint->getFlags(); |
| 652 | result &= ~sFilterBitmapFlag; // Filtering no longer stored in this bit. Mask away. |
| 653 | if (nativePaint->getFilterQuality() != kNone_SkFilterQuality) { |
| 654 | result |= sFilterBitmapFlag; |
| 655 | } |
| 656 | return static_cast<jint>(result); |
| 657 | } |
| 658 | |
| 659 | static void setFlags(jlong paintHandle, jint flags) { |
| 660 | Paint* nativePaint = reinterpret_cast<Paint*>(paintHandle); |
| 661 | // Instead of modifying 0x02, change the filter level. |
| 662 | nativePaint->setFilterQuality(flags & sFilterBitmapFlag |
| 663 | ? kLow_SkFilterQuality |
| 664 | : kNone_SkFilterQuality); |
| 665 | // Don't pass through filter flag, which is no longer stored in paint's flags. |
| 666 | flags &= ~sFilterBitmapFlag; |
| 667 | // Use the existing value for 0x02. |
| 668 | const uint32_t existing0x02Flag = nativePaint->getFlags() & sFilterBitmapFlag; |
| 669 | flags |= existing0x02Flag; |
| 670 | nativePaint->setFlags(flags); |
| 671 | } |
| 672 | |
| 673 | static jint getHinting(jlong paintHandle) { |
| 674 | return reinterpret_cast<Paint*>(paintHandle)->getHinting() |
| 675 | == Paint::kNo_Hinting ? 0 : 1; |
| 676 | } |
| 677 | |
| 678 | static void setHinting(jlong paintHandle, jint mode) { |
| 679 | reinterpret_cast<Paint*>(paintHandle)->setHinting( |
| 680 | mode == 0 ? Paint::kNo_Hinting : Paint::kNormal_Hinting); |
| 681 | } |
| 682 | |
| 683 | static void setAntiAlias(jlong paintHandle, jboolean aa) { |
| 684 | reinterpret_cast<Paint*>(paintHandle)->setAntiAlias(aa); |
| 685 | } |
| 686 | |
| 687 | static void setLinearText(jlong paintHandle, jboolean linearText) { |
| 688 | reinterpret_cast<Paint*>(paintHandle)->setLinearText(linearText); |
| 689 | } |
| 690 | |
| 691 | static void setSubpixelText(jlong paintHandle, jboolean subpixelText) { |
| 692 | reinterpret_cast<Paint*>(paintHandle)->setSubpixelText(subpixelText); |
| 693 | } |
| 694 | |
| 695 | static void setUnderlineText(jlong paintHandle, jboolean underlineText) { |
| 696 | reinterpret_cast<Paint*>(paintHandle)->setUnderlineText(underlineText); |
| 697 | } |
| 698 | |
| 699 | static void setStrikeThruText(jlong paintHandle, jboolean strikeThruText) { |
| 700 | reinterpret_cast<Paint*>(paintHandle)->setStrikeThruText(strikeThruText); |
| 701 | } |
| 702 | |
| 703 | static void setFakeBoldText(jlong paintHandle, jboolean fakeBoldText) { |
| 704 | reinterpret_cast<Paint*>(paintHandle)->setFakeBoldText(fakeBoldText); |
| 705 | } |
| 706 | |
| 707 | static void setFilterBitmap(jlong paintHandle, jboolean filterBitmap) { |
| 708 | reinterpret_cast<Paint*>(paintHandle)->setFilterQuality( |
| 709 | filterBitmap ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
| 710 | } |
| 711 | |
| 712 | static void setDither(jlong paintHandle, jboolean dither) { |
| 713 | reinterpret_cast<Paint*>(paintHandle)->setDither(dither); |
| 714 | } |
| 715 | |
| 716 | static jint getStyle(jlong objHandle) { |
| 717 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 718 | return static_cast<jint>(obj->getStyle()); |
| 719 | } |
| 720 | |
| 721 | static void setStyle(jlong objHandle, jint styleHandle) { |
| 722 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 723 | Paint::Style style = static_cast<Paint::Style>(styleHandle); |
| 724 | obj->setStyle(style); |
| 725 | } |
| 726 | |
| 727 | static jint getColor(jlong paintHandle) { |
| 728 | int color; |
| 729 | color = reinterpret_cast<Paint*>(paintHandle)->getColor(); |
| 730 | return static_cast<jint>(color); |
| 731 | } |
| 732 | |
| 733 | static jint getAlpha(jlong paintHandle) { |
| 734 | int alpha; |
| 735 | alpha = reinterpret_cast<Paint*>(paintHandle)->getAlpha(); |
| 736 | return static_cast<jint>(alpha); |
| 737 | } |
| 738 | |
| 739 | static void setColor(jlong paintHandle, jint color) { |
| 740 | reinterpret_cast<Paint*>(paintHandle)->setColor(color); |
| 741 | } |
| 742 | |
| 743 | static void setAlpha(jlong paintHandle, jint a) { |
| 744 | reinterpret_cast<Paint*>(paintHandle)->setAlpha(a); |
| 745 | } |
| 746 | |
| 747 | static jfloat getStrokeWidth(jlong paintHandle) { |
| 748 | return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeWidth()); |
| 749 | } |
| 750 | |
| 751 | static void setStrokeWidth(jlong paintHandle, jfloat width) { |
| 752 | reinterpret_cast<Paint*>(paintHandle)->setStrokeWidth(width); |
| 753 | } |
| 754 | |
| 755 | static jfloat getStrokeMiter(jlong paintHandle) { |
| 756 | return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeMiter()); |
| 757 | } |
| 758 | |
| 759 | static void setStrokeMiter(jlong paintHandle, jfloat miter) { |
| 760 | reinterpret_cast<Paint*>(paintHandle)->setStrokeMiter(miter); |
| 761 | } |
| 762 | |
| 763 | static jint getStrokeCap(jlong objHandle) { |
| 764 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 765 | return static_cast<jint>(obj->getStrokeCap()); |
| 766 | } |
| 767 | |
| 768 | static void setStrokeCap(jlong objHandle, jint capHandle) { |
| 769 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 770 | Paint::Cap cap = static_cast<Paint::Cap>(capHandle); |
| 771 | obj->setStrokeCap(cap); |
| 772 | } |
| 773 | |
| 774 | static jint getStrokeJoin(jlong objHandle) { |
| 775 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 776 | return static_cast<jint>(obj->getStrokeJoin()); |
| 777 | } |
| 778 | |
| 779 | static void setStrokeJoin(jlong objHandle, jint joinHandle) { |
| 780 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 781 | Paint::Join join = (Paint::Join) joinHandle; |
| 782 | obj->setStrokeJoin(join); |
| 783 | } |
| 784 | |
| 785 | static jboolean getFillPath(jlong objHandle, jlong srcHandle, jlong dstHandle) { |
| 786 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 787 | SkPath* src = reinterpret_cast<SkPath*>(srcHandle); |
| 788 | SkPath* dst = reinterpret_cast<SkPath*>(dstHandle); |
| 789 | return obj->getFillPath(*src, dst) ? JNI_TRUE : JNI_FALSE; |
| 790 | } |
| 791 | |
| 792 | static jlong setShader(jlong objHandle, jlong shaderHandle) { |
| 793 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 794 | SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 795 | obj->setShader(sk_ref_sp(shader)); |
| 796 | return reinterpret_cast<jlong>(obj->getShader()); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | static jlong setColorFilter(jlong objHandle, jlong filterHandle) { |
| 800 | Paint* obj = reinterpret_cast<Paint *>(objHandle); |
| 801 | SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(filterHandle); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 802 | obj->setColorFilter(sk_ref_sp(filter)); |
| 803 | return reinterpret_cast<jlong>(obj->getColorFilter()); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static void setXfermode(jlong paintHandle, jint xfermodeHandle) { |
| 807 | // validate that the Java enum values match our expectations |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 808 | static_assert(0 == static_cast<int>(SkBlendMode::kClear), "xfermode_mismatch"); |
| 809 | static_assert(1 == static_cast<int>(SkBlendMode::kSrc), "xfermode_mismatch"); |
| 810 | static_assert(2 == static_cast<int>(SkBlendMode::kDst), "xfermode_mismatch"); |
| 811 | static_assert(3 == static_cast<int>(SkBlendMode::kSrcOver), "xfermode_mismatch"); |
| 812 | static_assert(4 == static_cast<int>(SkBlendMode::kDstOver), "xfermode_mismatch"); |
| 813 | static_assert(5 == static_cast<int>(SkBlendMode::kSrcIn), "xfermode_mismatch"); |
| 814 | static_assert(6 == static_cast<int>(SkBlendMode::kDstIn), "xfermode_mismatch"); |
| 815 | static_assert(7 == static_cast<int>(SkBlendMode::kSrcOut), "xfermode_mismatch"); |
| 816 | static_assert(8 == static_cast<int>(SkBlendMode::kDstOut), "xfermode_mismatch"); |
| 817 | static_assert(9 == static_cast<int>(SkBlendMode::kSrcATop), "xfermode_mismatch"); |
| 818 | static_assert(10 == static_cast<int>(SkBlendMode::kDstATop), "xfermode_mismatch"); |
| 819 | static_assert(11 == static_cast<int>(SkBlendMode::kXor), "xfermode_mismatch"); |
| 820 | static_assert(16 == static_cast<int>(SkBlendMode::kDarken), "xfermode_mismatch"); |
| 821 | static_assert(17 == static_cast<int>(SkBlendMode::kLighten), "xfermode_mismatch"); |
| 822 | static_assert(13 == static_cast<int>(SkBlendMode::kModulate), "xfermode_mismatch"); |
| 823 | static_assert(14 == static_cast<int>(SkBlendMode::kScreen), "xfermode_mismatch"); |
| 824 | static_assert(12 == static_cast<int>(SkBlendMode::kPlus), "xfermode_mismatch"); |
| 825 | static_assert(15 == static_cast<int>(SkBlendMode::kOverlay), "xfermode_mismatch"); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 826 | |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 827 | SkBlendMode mode = static_cast<SkBlendMode>(xfermodeHandle); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 828 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 829 | paint->setBlendMode(mode); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | static jlong setPathEffect(jlong objHandle, jlong effectHandle) { |
| 833 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 834 | SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 835 | obj->setPathEffect(sk_ref_sp(effect)); |
| 836 | return reinterpret_cast<jlong>(obj->getPathEffect()); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static jlong setMaskFilter(jlong objHandle, jlong maskfilterHandle) { |
| 840 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 841 | SkMaskFilter* maskfilter = reinterpret_cast<SkMaskFilter*>(maskfilterHandle); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 842 | obj->setMaskFilter(sk_ref_sp(maskfilter)); |
| 843 | return reinterpret_cast<jlong>(obj->getMaskFilter()); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static jlong setTypeface(jlong objHandle, jlong typefaceHandle) { |
| 847 | // TODO: in Paint refactoring, set typeface on android Paint, not Paint |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static jlong setRasterizer(jlong objHandle, jlong rasterizerHandle) { |
| 852 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 853 | obj->setRasterizer(GraphicsJNI::refNativeRasterizer(rasterizerHandle)); |
| 854 | return reinterpret_cast<jlong>(obj->getRasterizer()); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | static jint getTextAlign(jlong objHandle) { |
| 858 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 859 | return static_cast<jint>(obj->getTextAlign()); |
| 860 | } |
| 861 | |
| 862 | static void setTextAlign(jlong objHandle, jint alignHandle) { |
| 863 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 864 | Paint::Align align = static_cast<Paint::Align>(alignHandle); |
| 865 | obj->setTextAlign(align); |
| 866 | } |
| 867 | |
| 868 | static void setTextLocalesByMinikinLangListId(jlong objHandle, |
| 869 | jint minikinLangListId) { |
| 870 | Paint* obj = reinterpret_cast<Paint*>(objHandle); |
| 871 | obj->setMinikinLangListId(minikinLangListId); |
| 872 | } |
| 873 | |
| 874 | static jboolean isElegantTextHeight(jlong paintHandle) { |
| 875 | Paint* obj = reinterpret_cast<Paint*>(paintHandle); |
| 876 | return obj->getFontVariant() == minikin::VARIANT_ELEGANT; |
| 877 | } |
| 878 | |
| 879 | static void setElegantTextHeight(jlong paintHandle, jboolean aa) { |
| 880 | Paint* obj = reinterpret_cast<Paint*>(paintHandle); |
| 881 | obj->setFontVariant(aa ? minikin::VARIANT_ELEGANT : minikin::VARIANT_DEFAULT); |
| 882 | } |
| 883 | |
| 884 | static jfloat getTextSize(jlong paintHandle) { |
| 885 | return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextSize()); |
| 886 | } |
| 887 | |
| 888 | static void setTextSize(jlong paintHandle, jfloat textSize) { |
| 889 | reinterpret_cast<Paint*>(paintHandle)->setTextSize(textSize); |
| 890 | } |
| 891 | |
| 892 | static jfloat getTextScaleX(jlong paintHandle) { |
| 893 | return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextScaleX()); |
| 894 | } |
| 895 | |
| 896 | static void setTextScaleX(jlong paintHandle, jfloat scaleX) { |
| 897 | reinterpret_cast<Paint*>(paintHandle)->setTextScaleX(scaleX); |
| 898 | } |
| 899 | |
| 900 | static jfloat getTextSkewX(jlong paintHandle) { |
| 901 | return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextSkewX()); |
| 902 | } |
| 903 | |
| 904 | static void setTextSkewX(jlong paintHandle, jfloat skewX) { |
| 905 | reinterpret_cast<Paint*>(paintHandle)->setTextSkewX(skewX); |
| 906 | } |
| 907 | |
| 908 | static jfloat getLetterSpacing(jlong paintHandle) { |
| 909 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 910 | return paint->getLetterSpacing(); |
| 911 | } |
| 912 | |
| 913 | static void setLetterSpacing(jlong paintHandle, jfloat letterSpacing) { |
| 914 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 915 | paint->setLetterSpacing(letterSpacing); |
| 916 | } |
| 917 | |
| 918 | static jint getHyphenEdit(jlong paintHandle, jint hyphen) { |
| 919 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 920 | return paint->getHyphenEdit(); |
| 921 | } |
| 922 | |
| 923 | static void setHyphenEdit(jlong paintHandle, jint hyphen) { |
| 924 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 925 | paint->setHyphenEdit((uint32_t)hyphen); |
| 926 | } |
| 927 | |
| 928 | static jfloat ascent(jlong paintHandle, jlong typefaceHandle) { |
| 929 | Paint::FontMetrics metrics; |
| 930 | getMetricsInternal(paintHandle, typefaceHandle, &metrics); |
| 931 | return SkScalarToFloat(metrics.fAscent); |
| 932 | } |
| 933 | |
| 934 | static jfloat descent(jlong paintHandle, jlong typefaceHandle) { |
| 935 | Paint::FontMetrics metrics; |
| 936 | getMetricsInternal(paintHandle, typefaceHandle, &metrics); |
| 937 | return SkScalarToFloat(metrics.fDescent); |
| 938 | } |
| 939 | |
| 940 | static void setShadowLayer(jlong paintHandle, jfloat radius, |
| 941 | jfloat dx, jfloat dy, jint color) { |
| 942 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 943 | if (radius <= 0) { |
| 944 | paint->setLooper(nullptr); |
| 945 | } |
| 946 | else { |
| 947 | SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 948 | paint->setLooper(SkBlurDrawLooper::Make((SkColor)color, sigma, dx, dy)); |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
| 952 | static jboolean hasShadowLayer(jlong paintHandle) { |
| 953 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 954 | return paint->getLooper() && paint->getLooper()->asABlurShadow(nullptr); |
| 955 | } |
| 956 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 957 | }; // namespace PaintGlue |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 959 | static const JNINativeMethod methods[] = { |
Richard Uhler | 775873a | 2015-12-29 12:37:39 -0800 | [diff] [blame] | 960 | {"nGetNativeFinalizer", "()J", (void*) PaintGlue::getNativeFinalizer}, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 961 | {"nInit","()J", (void*) PaintGlue::init}, |
| 962 | {"nInitWithPaint","(J)J", (void*) PaintGlue::initWithPaint}, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 963 | {"nBreakText","(JJ[CIIFI[F)I", (void*) PaintGlue::breakTextC}, |
| 964 | {"nBreakText","(JJLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS}, |
| 965 | {"nGetTextAdvances","(JJ[CIIIII[FI)F", |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 966 | (void*) PaintGlue::getTextAdvances___CIIIII_FI}, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 967 | {"nGetTextAdvances","(JJLjava/lang/String;IIIII[FI)F", |
Keisuke Kuroyanagi | 536afe6 | 2015-09-29 13:52:45 -0700 | [diff] [blame] | 968 | (void*) PaintGlue::getTextAdvances__StringIIIII_FI}, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 969 | |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 970 | {"nGetTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C}, |
| 971 | {"nGetTextRunCursor", "(JLjava/lang/String;IIIII)I", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 972 | (void*) PaintGlue::getTextRunCursor__String}, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 973 | {"nGetTextPath", "(JJI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C}, |
| 974 | {"nGetTextPath", "(JJILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String}, |
| 975 | {"nGetStringBounds", "(JJLjava/lang/String;IIILandroid/graphics/Rect;)V", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 976 | (void*) PaintGlue::getStringBounds }, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 977 | {"nGetCharArrayBounds", "(JJ[CIIILandroid/graphics/Rect;)V", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 978 | (void*) PaintGlue::getCharArrayBounds }, |
John Reck | dbffd25 | 2015-10-01 14:46:12 -0700 | [diff] [blame] | 979 | {"nHasGlyph", "(JJILjava/lang/String;)Z", (void*) PaintGlue::hasGlyph }, |
| 980 | {"nGetRunAdvance", "(JJ[CIIIIZI)F", (void*) PaintGlue::getRunAdvance___CIIIIZI_F}, |
| 981 | {"nGetOffsetForAdvance", "(JJ[CIIIIZF)I", |
Raph Levien | a027ec5 | 2015-04-06 16:21:59 -0700 | [diff] [blame] | 982 | (void*) PaintGlue::getOffsetForAdvance___CIIIIZF_I}, |
Chris Craik | 4136a0a | 2014-10-07 15:09:46 -0700 | [diff] [blame] | 983 | |
John Reck | f228597 | 2016-09-30 14:10:05 -0700 | [diff] [blame] | 984 | // --------------- @FastNative ---------------------- |
| 985 | |
| 986 | {"nSetTextLocales","(JLjava/lang/String;)I", (void*) PaintGlue::setTextLocales}, |
| 987 | {"nSetFontFeatureSettings","(JLjava/lang/String;)V", |
| 988 | (void*) PaintGlue::setFontFeatureSettings}, |
| 989 | {"nGetFontMetrics", "(JJLandroid/graphics/Paint$FontMetrics;)F", |
| 990 | (void*)PaintGlue::getFontMetrics}, |
| 991 | {"nGetFontMetricsInt", "(JJLandroid/graphics/Paint$FontMetricsInt;)I", |
| 992 | (void*)PaintGlue::getFontMetricsInt}, |
| 993 | |
| 994 | // --------------- @CriticalNative ------------------ |
| 995 | |
| 996 | {"nReset","(J)V", (void*) PaintGlue::reset}, |
| 997 | {"nSet","(JJ)V", (void*) PaintGlue::assign}, |
| 998 | {"nGetFlags","(J)I", (void*) PaintGlue::getFlags}, |
| 999 | {"nSetFlags","(JI)V", (void*) PaintGlue::setFlags}, |
| 1000 | {"nGetHinting","(J)I", (void*) PaintGlue::getHinting}, |
| 1001 | {"nSetHinting","(JI)V", (void*) PaintGlue::setHinting}, |
| 1002 | {"nSetAntiAlias","(JZ)V", (void*) PaintGlue::setAntiAlias}, |
| 1003 | {"nSetSubpixelText","(JZ)V", (void*) PaintGlue::setSubpixelText}, |
| 1004 | {"nSetLinearText","(JZ)V", (void*) PaintGlue::setLinearText}, |
| 1005 | {"nSetUnderlineText","(JZ)V", (void*) PaintGlue::setUnderlineText}, |
| 1006 | {"nSetStrikeThruText","(JZ)V", (void*) PaintGlue::setStrikeThruText}, |
| 1007 | {"nSetFakeBoldText","(JZ)V", (void*) PaintGlue::setFakeBoldText}, |
| 1008 | {"nSetFilterBitmap","(JZ)V", (void*) PaintGlue::setFilterBitmap}, |
| 1009 | {"nSetDither","(JZ)V", (void*) PaintGlue::setDither}, |
| 1010 | {"nGetStyle","(J)I", (void*) PaintGlue::getStyle}, |
| 1011 | {"nSetStyle","(JI)V", (void*) PaintGlue::setStyle}, |
| 1012 | {"nGetColor","(J)I", (void*) PaintGlue::getColor}, |
| 1013 | {"nSetColor","(JI)V", (void*) PaintGlue::setColor}, |
| 1014 | {"nGetAlpha","(J)I", (void*) PaintGlue::getAlpha}, |
| 1015 | {"nSetAlpha","(JI)V", (void*) PaintGlue::setAlpha}, |
| 1016 | {"nGetStrokeWidth","(J)F", (void*) PaintGlue::getStrokeWidth}, |
| 1017 | {"nSetStrokeWidth","(JF)V", (void*) PaintGlue::setStrokeWidth}, |
| 1018 | {"nGetStrokeMiter","(J)F", (void*) PaintGlue::getStrokeMiter}, |
| 1019 | {"nSetStrokeMiter","(JF)V", (void*) PaintGlue::setStrokeMiter}, |
| 1020 | {"nGetStrokeCap","(J)I", (void*) PaintGlue::getStrokeCap}, |
| 1021 | {"nSetStrokeCap","(JI)V", (void*) PaintGlue::setStrokeCap}, |
| 1022 | {"nGetStrokeJoin","(J)I", (void*) PaintGlue::getStrokeJoin}, |
| 1023 | {"nSetStrokeJoin","(JI)V", (void*) PaintGlue::setStrokeJoin}, |
| 1024 | {"nGetFillPath","(JJJ)Z", (void*) PaintGlue::getFillPath}, |
| 1025 | {"nSetShader","(JJ)J", (void*) PaintGlue::setShader}, |
| 1026 | {"nSetColorFilter","(JJ)J", (void*) PaintGlue::setColorFilter}, |
| 1027 | {"nSetXfermode","(JI)V", (void*) PaintGlue::setXfermode}, |
| 1028 | {"nSetPathEffect","(JJ)J", (void*) PaintGlue::setPathEffect}, |
| 1029 | {"nSetMaskFilter","(JJ)J", (void*) PaintGlue::setMaskFilter}, |
| 1030 | {"nSetTypeface","(JJ)J", (void*) PaintGlue::setTypeface}, |
| 1031 | {"nSetRasterizer","(JJ)J", (void*) PaintGlue::setRasterizer}, |
| 1032 | {"nGetTextAlign","(J)I", (void*) PaintGlue::getTextAlign}, |
| 1033 | {"nSetTextAlign","(JI)V", (void*) PaintGlue::setTextAlign}, |
| 1034 | {"nSetTextLocalesByMinikinLangListId","(JI)V", |
| 1035 | (void*) PaintGlue::setTextLocalesByMinikinLangListId}, |
| 1036 | {"nIsElegantTextHeight","(J)Z", (void*) PaintGlue::isElegantTextHeight}, |
| 1037 | {"nSetElegantTextHeight","(JZ)V", (void*) PaintGlue::setElegantTextHeight}, |
| 1038 | {"nGetTextSize","(J)F", (void*) PaintGlue::getTextSize}, |
| 1039 | {"nSetTextSize","(JF)V", (void*) PaintGlue::setTextSize}, |
| 1040 | {"nGetTextScaleX","(J)F", (void*) PaintGlue::getTextScaleX}, |
| 1041 | {"nSetTextScaleX","(JF)V", (void*) PaintGlue::setTextScaleX}, |
| 1042 | {"nGetTextSkewX","(J)F", (void*) PaintGlue::getTextSkewX}, |
| 1043 | {"nSetTextSkewX","(JF)V", (void*) PaintGlue::setTextSkewX}, |
| 1044 | {"nGetLetterSpacing","(J)F", (void*) PaintGlue::getLetterSpacing}, |
| 1045 | {"nSetLetterSpacing","(JF)V", (void*) PaintGlue::setLetterSpacing}, |
| 1046 | {"nGetHyphenEdit", "(J)I", (void*) PaintGlue::getHyphenEdit}, |
| 1047 | {"nSetHyphenEdit", "(JI)V", (void*) PaintGlue::setHyphenEdit}, |
| 1048 | {"nAscent","(JJ)F", (void*) PaintGlue::ascent}, |
| 1049 | {"nDescent","(JJ)F", (void*) PaintGlue::descent}, |
| 1050 | {"nSetShadowLayer", "(JFFFI)V", (void*)PaintGlue::setShadowLayer}, |
| 1051 | {"nHasShadowLayer", "(J)Z", (void*)PaintGlue::hasShadowLayer} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | }; |
| 1053 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | int register_android_graphics_Paint(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1055 | gFontMetrics_class = FindClassOrDie(env, "android/graphics/Paint$FontMetrics"); |
| 1056 | gFontMetrics_class = MakeGlobalRefOrDie(env, gFontMetrics_class); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1058 | gFontMetrics_fieldID.top = GetFieldIDOrDie(env, gFontMetrics_class, "top", "F"); |
| 1059 | gFontMetrics_fieldID.ascent = GetFieldIDOrDie(env, gFontMetrics_class, "ascent", "F"); |
| 1060 | gFontMetrics_fieldID.descent = GetFieldIDOrDie(env, gFontMetrics_class, "descent", "F"); |
| 1061 | gFontMetrics_fieldID.bottom = GetFieldIDOrDie(env, gFontMetrics_class, "bottom", "F"); |
| 1062 | gFontMetrics_fieldID.leading = GetFieldIDOrDie(env, gFontMetrics_class, "leading", "F"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1064 | gFontMetricsInt_class = FindClassOrDie(env, "android/graphics/Paint$FontMetricsInt"); |
| 1065 | gFontMetricsInt_class = MakeGlobalRefOrDie(env, gFontMetricsInt_class); |
Derek Sollenberger | ab22c1c | 2014-09-03 14:12:47 -0400 | [diff] [blame] | 1066 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1067 | gFontMetricsInt_fieldID.top = GetFieldIDOrDie(env, gFontMetricsInt_class, "top", "I"); |
| 1068 | gFontMetricsInt_fieldID.ascent = GetFieldIDOrDie(env, gFontMetricsInt_class, "ascent", "I"); |
| 1069 | gFontMetricsInt_fieldID.descent = GetFieldIDOrDie(env, gFontMetricsInt_class, "descent", "I"); |
| 1070 | gFontMetricsInt_fieldID.bottom = GetFieldIDOrDie(env, gFontMetricsInt_class, "bottom", "I"); |
| 1071 | gFontMetricsInt_fieldID.leading = GetFieldIDOrDie(env, gFontMetricsInt_class, "leading", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1073 | return RegisterMethodsOrDie(env, "android/graphics/Paint", methods, NELEM(methods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | } |