blob: f46f45cc5e5d5b4a9ee3d7b790c1efcdbbb7048f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* libs/android_runtime/android/graphics/Paint.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
Elliott Hughes8451b252011-04-07 19:17:57 -07005** 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 Project9066cfe2009-03-03 19:31:44 -08008**
Elliott Hughes8451b252011-04-07 19:17:57 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Elliott Hughes8451b252011-04-07 19:17:57 -070011** 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 Project9066cfe2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
Dianne Hackbornf43fa572011-08-12 18:59:39 -070018#define LOG_TAG "Paint"
19
20#include <utils/Log.h>
21
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include "jni.h"
23#include "GraphicsJNI.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080024#include "core_jni_helpers.h"
Raph Levienf7f969e62015-04-01 14:41:21 -070025#include <ScopedStringChars.h>
Raph Leviena027ec52015-04-06 16:21:59 -070026#include <ScopedUtfChars.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
28#include "SkBlurDrawLooper.h"
29#include "SkColorFilter.h"
30#include "SkMaskFilter.h"
Derek Sollenbergereba81d02015-10-26 10:22:37 -040031#include "SkPath.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032#include "SkRasterizer.h"
33#include "SkShader.h"
34#include "SkTypeface.h"
35#include "SkXfermode.h"
Billy Hewlettac1cbaf2012-07-18 09:51:45 -070036#include "unicode/uloc.h"
Doug Felt0c702b82010-05-14 10:55:42 -070037#include "unicode/ushape.h"
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -040038#include "utils/Blur.h"
Doug Felt0c702b82010-05-14 10:55:42 -070039
sergeyvdccca442016-03-21 15:38:21 -070040#include <hwui/MinikinSkia.h>
41#include <hwui/MinikinUtils.h>
42#include <hwui/Paint.h>
sergeyvbad99182016-03-17 11:24:22 -070043#include <hwui/Typeface.h>
Raph Leviene368b6b2014-06-15 17:37:57 -070044#include <minikin/GraphemeBreak.h>
Raph Leviena027ec52015-04-06 16:21:59 -070045#include <minikin/Measurement.h>
Seigo Nonakabb1a9662015-10-21 23:24:25 +090046#include <unicode/utf16.h>
Raph Levien1a73f7322014-01-30 16:06:28 -080047
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -070048#include <cassert>
49#include <cstring>
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -070050#include <memory>
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -070051#include <vector>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53namespace android {
54
55struct JMetricsID {
56 jfieldID top;
57 jfieldID ascent;
58 jfieldID descent;
59 jfieldID bottom;
60 jfieldID leading;
61};
62
63static jclass gFontMetrics_class;
64static JMetricsID gFontMetrics_fieldID;
65
66static jclass gFontMetricsInt_class;
67static JMetricsID gFontMetricsInt_fieldID;
68
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040069static void defaultSettingsForAndroid(Paint* paint) {
Fabrice Di Meglioc511bee82012-01-05 13:30:54 -080070 // GlyphID encoding is required because we are using Harfbuzz shaping
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040071 paint->setTextEncoding(Paint::kGlyphID_TextEncoding);
Mike Reed3d63e012009-07-27 09:50:31 -040072}
73
John Reckdbffd252015-10-01 14:46:12 -070074namespace PaintGlue {
Doug Felt0c702b82010-05-14 10:55:42 -070075 enum MoveOpt {
76 AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT
77 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
Richard Uhler775873a2015-12-29 12:37:39 -080079 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 Project9066cfe2009-03-03 19:31:44 -080085 }
86
John Reckdbffd252015-10-01 14:46:12 -070087 static jlong init(JNIEnv* env, jobject) {
Ben Wagnere3a40ea2015-08-19 15:49:37 -040088 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 Sollenberger0b860622015-03-25 10:08:30 -040097
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040098 Paint* obj = new Paint();
Mike Reed3d63e012009-07-27 09:50:31 -040099 defaultSettingsForAndroid(obj);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000100 return reinterpret_cast<jlong>(obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 }
102
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000103 static jlong initWithPaint(JNIEnv* env, jobject clazz, jlong paintHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400104 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
105 Paint* obj = new Paint(*paint);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000106 return reinterpret_cast<jlong>(obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700108
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000109 static void reset(JNIEnv* env, jobject clazz, jlong objHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400110 Paint* obj = reinterpret_cast<Paint*>(objHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 obj->reset();
Mike Reed3d63e012009-07-27 09:50:31 -0400112 defaultSettingsForAndroid(obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700114
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000115 static void assign(JNIEnv* env, jobject clazz, jlong dstPaintHandle, jlong srcPaintHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400116 Paint* dst = reinterpret_cast<Paint*>(dstPaintHandle);
117 const Paint* src = reinterpret_cast<Paint*>(srcPaintHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 *dst = *src;
119 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700120
Mike Reed4c9355c2014-05-07 11:48:37 -0400121 // Equivalent to the Java Paint's FILTER_BITMAP_FLAG.
122 static const uint32_t sFilterBitmapFlag = 0x02;
123
John Reckdbffd252015-10-01 14:46:12 -0700124 static jint getFlags(JNIEnv* env, jobject, jlong paintHandle) {
125 Paint* nativePaint = reinterpret_cast<Paint*>(paintHandle);
Mike Reed4c9355c2014-05-07 11:48:37 -0400126 uint32_t result = nativePaint->getFlags();
127 result &= ~sFilterBitmapFlag; // Filtering no longer stored in this bit. Mask away.
Mike Reed2a1ce8a2015-03-16 11:16:09 -0400128 if (nativePaint->getFilterQuality() != kNone_SkFilterQuality) {
Mike Reed4c9355c2014-05-07 11:48:37 -0400129 result |= sFilterBitmapFlag;
130 }
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000131 return static_cast<jint>(result);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700133
John Reckdbffd252015-10-01 14:46:12 -0700134 static void setFlags(JNIEnv* env, jobject, jlong paintHandle, jint flags) {
135 Paint* nativePaint = reinterpret_cast<Paint*>(paintHandle);
Mike Reed4c9355c2014-05-07 11:48:37 -0400136 // Instead of modifying 0x02, change the filter level.
Mike Reed2a1ce8a2015-03-16 11:16:09 -0400137 nativePaint->setFilterQuality(flags & sFilterBitmapFlag
138 ? kLow_SkFilterQuality
139 : kNone_SkFilterQuality);
Mike Reed4c9355c2014-05-07 11:48:37 -0400140 // Don't pass through filter flag, which is no longer stored in paint's flags.
141 flags &= ~sFilterBitmapFlag;
142 // Use the existing value for 0x02.
143 const uint32_t existing0x02Flag = nativePaint->getFlags() & sFilterBitmapFlag;
144 flags |= existing0x02Flag;
145 nativePaint->setFlags(flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700147
John Reckdbffd252015-10-01 14:46:12 -0700148 static jint getHinting(JNIEnv* env, jobject, jlong paintHandle) {
149 return reinterpret_cast<Paint*>(paintHandle)->getHinting()
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400150 == Paint::kNo_Hinting ? 0 : 1;
Dianne Hackbornf43fa572011-08-12 18:59:39 -0700151 }
152
John Reckdbffd252015-10-01 14:46:12 -0700153 static void setHinting(JNIEnv* env, jobject, jlong paintHandle, jint mode) {
154 reinterpret_cast<Paint*>(paintHandle)->setHinting(
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400155 mode == 0 ? Paint::kNo_Hinting : Paint::kNormal_Hinting);
Dianne Hackbornf43fa572011-08-12 18:59:39 -0700156 }
157
John Reckdbffd252015-10-01 14:46:12 -0700158 static void setAntiAlias(JNIEnv* env, jobject, jlong paintHandle, jboolean aa) {
159 reinterpret_cast<Paint*>(paintHandle)->setAntiAlias(aa);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700161
John Reckdbffd252015-10-01 14:46:12 -0700162 static void setLinearText(JNIEnv* env, jobject, jlong paintHandle, jboolean linearText) {
163 reinterpret_cast<Paint*>(paintHandle)->setLinearText(linearText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700165
John Reckdbffd252015-10-01 14:46:12 -0700166 static void setSubpixelText(JNIEnv* env, jobject, jlong paintHandle, jboolean subpixelText) {
167 reinterpret_cast<Paint*>(paintHandle)->setSubpixelText(subpixelText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700169
John Reckdbffd252015-10-01 14:46:12 -0700170 static void setUnderlineText(JNIEnv* env, jobject, jlong paintHandle, jboolean underlineText) {
171 reinterpret_cast<Paint*>(paintHandle)->setUnderlineText(underlineText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700173
John Reckdbffd252015-10-01 14:46:12 -0700174 static void setStrikeThruText(JNIEnv* env, jobject, jlong paintHandle, jboolean strikeThruText) {
175 reinterpret_cast<Paint*>(paintHandle)->setStrikeThruText(strikeThruText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700177
John Reckdbffd252015-10-01 14:46:12 -0700178 static void setFakeBoldText(JNIEnv* env, jobject, jlong paintHandle, jboolean fakeBoldText) {
179 reinterpret_cast<Paint*>(paintHandle)->setFakeBoldText(fakeBoldText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700181
John Reckdbffd252015-10-01 14:46:12 -0700182 static void setFilterBitmap(JNIEnv* env, jobject, jlong paintHandle, jboolean filterBitmap) {
183 reinterpret_cast<Paint*>(paintHandle)->setFilterQuality(
Mike Reed2a1ce8a2015-03-16 11:16:09 -0400184 filterBitmap ? kLow_SkFilterQuality : kNone_SkFilterQuality);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700186
John Reckdbffd252015-10-01 14:46:12 -0700187 static void setDither(JNIEnv* env, jobject, jlong paintHandle, jboolean dither) {
188 reinterpret_cast<Paint*>(paintHandle)->setDither(dither);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700190
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000191 static jint getStyle(JNIEnv* env, jobject clazz,jlong objHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400192 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000193 return static_cast<jint>(obj->getStyle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700195
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000196 static void setStyle(JNIEnv* env, jobject clazz, jlong objHandle, jint styleHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400197 Paint* obj = reinterpret_cast<Paint*>(objHandle);
198 Paint::Style style = static_cast<Paint::Style>(styleHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 obj->setStyle(style);
200 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700201
John Reckdbffd252015-10-01 14:46:12 -0700202 static jint getColor(JNIEnv* env, jobject, jlong paintHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000203 int color;
John Reckdbffd252015-10-01 14:46:12 -0700204 color = reinterpret_cast<Paint*>(paintHandle)->getColor();
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000205 return static_cast<jint>(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700207
John Reckdbffd252015-10-01 14:46:12 -0700208 static jint getAlpha(JNIEnv* env, jobject, jlong paintHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000209 int alpha;
John Reckdbffd252015-10-01 14:46:12 -0700210 alpha = reinterpret_cast<Paint*>(paintHandle)->getAlpha();
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000211 return static_cast<jint>(alpha);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700213
John Reckdbffd252015-10-01 14:46:12 -0700214 static void setColor(JNIEnv* env, jobject, jlong paintHandle, jint color) {
215 reinterpret_cast<Paint*>(paintHandle)->setColor(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700217
John Reckdbffd252015-10-01 14:46:12 -0700218 static void setAlpha(JNIEnv* env, jobject, jlong paintHandle, jint a) {
219 reinterpret_cast<Paint*>(paintHandle)->setAlpha(a);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700221
John Reckdbffd252015-10-01 14:46:12 -0700222 static jfloat getStrokeWidth(JNIEnv* env, jobject, jlong paintHandle) {
223 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeWidth());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700225
John Reckdbffd252015-10-01 14:46:12 -0700226 static void setStrokeWidth(JNIEnv* env, jobject, jlong paintHandle, jfloat width) {
227 reinterpret_cast<Paint*>(paintHandle)->setStrokeWidth(width);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700229
John Reckdbffd252015-10-01 14:46:12 -0700230 static jfloat getStrokeMiter(JNIEnv* env, jobject, jlong paintHandle) {
231 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeMiter());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700233
John Reckdbffd252015-10-01 14:46:12 -0700234 static void setStrokeMiter(JNIEnv* env, jobject, jlong paintHandle, jfloat miter) {
235 reinterpret_cast<Paint*>(paintHandle)->setStrokeMiter(miter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700237
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000238 static jint getStrokeCap(JNIEnv* env, jobject clazz, jlong objHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400239 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000240 return static_cast<jint>(obj->getStrokeCap());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700242
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000243 static void setStrokeCap(JNIEnv* env, jobject clazz, jlong objHandle, jint capHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400244 Paint* obj = reinterpret_cast<Paint*>(objHandle);
245 Paint::Cap cap = static_cast<Paint::Cap>(capHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 obj->setStrokeCap(cap);
247 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700248
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000249 static jint getStrokeJoin(JNIEnv* env, jobject clazz, jlong objHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400250 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000251 return static_cast<jint>(obj->getStrokeJoin());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700253
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000254 static void setStrokeJoin(JNIEnv* env, jobject clazz, jlong objHandle, jint joinHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400255 Paint* obj = reinterpret_cast<Paint*>(objHandle);
256 Paint::Join join = (Paint::Join) joinHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 obj->setStrokeJoin(join);
258 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700259
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000260 static jboolean getFillPath(JNIEnv* env, jobject clazz, jlong objHandle, jlong srcHandle, jlong dstHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400261 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000262 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
263 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
264 return obj->getFillPath(*src, dst) ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700266
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000267 static jlong setShader(JNIEnv* env, jobject clazz, jlong objHandle, jlong shaderHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400268 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000269 SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle);
270 return reinterpret_cast<jlong>(obj->setShader(shader));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700272
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000273 static jlong setColorFilter(JNIEnv* env, jobject clazz, jlong objHandle, jlong filterHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400274 Paint* obj = reinterpret_cast<Paint *>(objHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000275 SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(filterHandle);
276 return reinterpret_cast<jlong>(obj->setColorFilter(filter));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700278
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000279 static jlong setXfermode(JNIEnv* env, jobject clazz, jlong objHandle, jlong xfermodeHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400280 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000281 SkXfermode* xfermode = reinterpret_cast<SkXfermode*>(xfermodeHandle);
282 return reinterpret_cast<jlong>(obj->setXfermode(xfermode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700284
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000285 static jlong setPathEffect(JNIEnv* env, jobject clazz, jlong objHandle, jlong effectHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400286 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000287 SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle);
288 return reinterpret_cast<jlong>(obj->setPathEffect(effect));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700290
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000291 static jlong setMaskFilter(JNIEnv* env, jobject clazz, jlong objHandle, jlong maskfilterHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400292 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000293 SkMaskFilter* maskfilter = reinterpret_cast<SkMaskFilter*>(maskfilterHandle);
294 return reinterpret_cast<jlong>(obj->setMaskFilter(maskfilter));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700296
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000297 static jlong setTypeface(JNIEnv* env, jobject clazz, jlong objHandle, jlong typefaceHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400298 // TODO: in Paint refactoring, set typeface on android Paint, not Paint
Raph Leviena0336302013-05-22 16:16:59 -0700299 return NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700301
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000302 static jlong setRasterizer(JNIEnv* env, jobject clazz, jlong objHandle, jlong rasterizerHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400303 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Mike Reed4c9355c2014-05-07 11:48:37 -0400304 SkAutoTUnref<SkRasterizer> rasterizer(GraphicsJNI::refNativeRasterizer(rasterizerHandle));
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000305 return reinterpret_cast<jlong>(obj->setRasterizer(rasterizer));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700307
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000308 static jint getTextAlign(JNIEnv* env, jobject clazz, jlong objHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400309 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000310 return static_cast<jint>(obj->getTextAlign());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700312
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000313 static void setTextAlign(JNIEnv* env, jobject clazz, jlong objHandle, jint alignHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400314 Paint* obj = reinterpret_cast<Paint*>(objHandle);
315 Paint::Align align = static_cast<Paint::Align>(alignHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 obj->setTextAlign(align);
317 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700318
Seigo Nonakacfc607c2015-12-02 10:53:18 +0900319 static jint setTextLocales(JNIEnv* env, jobject clazz, jlong objHandle, jstring locales) {
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -0700320 Paint* obj = reinterpret_cast<Paint*>(objHandle);
321 ScopedUtfChars localesChars(env, locales);
Seigo Nonaka4ea7d1d2015-12-15 10:03:36 -0800322 jint minikinLangListId = FontStyle::registerLanguageList(localesChars.c_str());
Seigo Nonakacfc607c2015-12-02 10:53:18 +0900323 obj->setMinikinLangListId(minikinLangListId);
324 return minikinLangListId;
325 }
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -0700326
Seigo Nonakacfc607c2015-12-02 10:53:18 +0900327 static void setTextLocalesByMinikinLangListId(JNIEnv* env, jobject clazz, jlong objHandle,
328 jint minikinLangListId) {
329 Paint* obj = reinterpret_cast<Paint*>(objHandle);
330 obj->setMinikinLangListId(minikinLangListId);
Fabrice Di Meglio517825f2012-04-06 16:53:48 -0700331 }
332
John Reckdbffd252015-10-01 14:46:12 -0700333 static jboolean isElegantTextHeight(JNIEnv* env, jobject, jlong paintHandle) {
334 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
Derek Sollenberger6f030232014-08-07 08:10:39 -0400335 return obj->getFontVariant() == VARIANT_ELEGANT;
Raph Levien53c00772014-04-14 14:11:02 -0700336 }
337
John Reckdbffd252015-10-01 14:46:12 -0700338 static void setElegantTextHeight(JNIEnv* env, jobject, jlong paintHandle, jboolean aa) {
339 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
Derek Sollenberger6f030232014-08-07 08:10:39 -0400340 obj->setFontVariant(aa ? VARIANT_ELEGANT : VARIANT_DEFAULT);
Raph Levien53c00772014-04-14 14:11:02 -0700341 }
342
John Reckdbffd252015-10-01 14:46:12 -0700343 static jfloat getTextSize(JNIEnv* env, jobject, jlong paintHandle) {
344 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextSize());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700346
John Reckdbffd252015-10-01 14:46:12 -0700347 static void setTextSize(JNIEnv* env, jobject, jlong paintHandle, jfloat textSize) {
348 reinterpret_cast<Paint*>(paintHandle)->setTextSize(textSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700350
John Reckdbffd252015-10-01 14:46:12 -0700351 static jfloat getTextScaleX(JNIEnv* env, jobject, jlong paintHandle) {
352 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextScaleX());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700354
John Reckdbffd252015-10-01 14:46:12 -0700355 static void setTextScaleX(JNIEnv* env, jobject, jlong paintHandle, jfloat scaleX) {
356 reinterpret_cast<Paint*>(paintHandle)->setTextScaleX(scaleX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700358
John Reckdbffd252015-10-01 14:46:12 -0700359 static jfloat getTextSkewX(JNIEnv* env, jobject, jlong paintHandle) {
360 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextSkewX());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700362
John Reckdbffd252015-10-01 14:46:12 -0700363 static void setTextSkewX(JNIEnv* env, jobject, jlong paintHandle, jfloat skewX) {
364 reinterpret_cast<Paint*>(paintHandle)->setTextSkewX(skewX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700366
Behdad Esfahbodfa80f742014-07-17 19:10:39 -0400367 static jfloat getLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle) {
368 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
369 return paint->getLetterSpacing();
370 }
371
372 static void setLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle, jfloat letterSpacing) {
373 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
374 paint->setLetterSpacing(letterSpacing);
375 }
376
Behdad Esfahbod805f6eb2014-07-29 18:43:03 -0400377 static void setFontFeatureSettings(JNIEnv* env, jobject clazz, jlong paintHandle, jstring settings) {
378 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Behdad Esfahbode9ad3932014-07-30 19:46:53 -0400379 if (!settings) {
Behdad Esfahbod805f6eb2014-07-29 18:43:03 -0400380 paint->setFontFeatureSettings(std::string());
Behdad Esfahbode9ad3932014-07-30 19:46:53 -0400381 } else {
Behdad Esfahbod805f6eb2014-07-29 18:43:03 -0400382 ScopedUtfChars settingsChars(env, settings);
383 paint->setFontFeatureSettings(std::string(settingsChars.c_str(), settingsChars.size()));
384 }
385 }
386
Raph Levien210a1892015-03-09 14:42:14 -0700387 static jint getHyphenEdit(JNIEnv* env, jobject clazz, jlong paintHandle, jint hyphen) {
388 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
389 return paint->getHyphenEdit();
390 }
391
392 static void setHyphenEdit(JNIEnv* env, jobject clazz, jlong paintHandle, jint hyphen) {
393 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
394 paint->setHyphenEdit((uint32_t)hyphen);
395 }
396
John Reckdbffd252015-10-01 14:46:12 -0700397 static SkScalar getMetricsInternal(jlong paintHandle, jlong typefaceHandle,
398 Paint::FontMetrics *metrics) {
Raph Levien53c00772014-04-14 14:11:02 -0700399 const int kElegantTop = 2500;
400 const int kElegantBottom = -1000;
Raph Levien817ef4a2014-06-05 22:38:08 -0700401 const int kElegantAscent = 1900;
402 const int kElegantDescent = -500;
Raph Levien53c00772014-04-14 14:11:02 -0700403 const int kElegantLeading = 0;
John Reckdbffd252015-10-01 14:46:12 -0700404 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700405 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
406 typeface = Typeface::resolveDefault(typeface);
Raph Levien1fc0fa82014-06-06 18:05:22 -0700407 FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle);
Raph Levien0627ec02014-06-06 22:31:15 -0700408 float saveSkewX = paint->getTextSkewX();
409 bool savefakeBold = paint->isFakeBoldText();
Raph Levien1fc0fa82014-06-06 18:05:22 -0700410 MinikinFontSkia::populateSkPaint(paint, baseFont.font, baseFont.fakery);
Raph Levien53c00772014-04-14 14:11:02 -0700411 SkScalar spacing = paint->getFontMetrics(metrics);
Raph Levien0627ec02014-06-06 22:31:15 -0700412 // The populateSkPaint call may have changed fake bold / text skew
413 // because we want to measure with those effects applied, so now
414 // restore the original settings.
415 paint->setTextSkewX(saveSkewX);
416 paint->setFakeBoldText(savefakeBold);
Derek Sollenberger6f030232014-08-07 08:10:39 -0400417 if (paint->getFontVariant() == VARIANT_ELEGANT) {
Raph Levien53c00772014-04-14 14:11:02 -0700418 SkScalar size = paint->getTextSize();
419 metrics->fTop = -size * kElegantTop / 2048;
420 metrics->fBottom = -size * kElegantBottom / 2048;
421 metrics->fAscent = -size * kElegantAscent / 2048;
422 metrics->fDescent = -size * kElegantDescent / 2048;
423 metrics->fLeading = size * kElegantLeading / 2048;
424 spacing = metrics->fDescent - metrics->fAscent + metrics->fLeading;
425 }
426 return spacing;
427 }
428
John Reckdbffd252015-10-01 14:46:12 -0700429 static jfloat ascent(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400430 Paint::FontMetrics metrics;
John Reckdbffd252015-10-01 14:46:12 -0700431 getMetricsInternal(paintHandle, typefaceHandle, &metrics);
Raph Levien817ef4a2014-06-05 22:38:08 -0700432 return SkScalarToFloat(metrics.fAscent);
433 }
434
John Reckdbffd252015-10-01 14:46:12 -0700435 static jfloat descent(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400436 Paint::FontMetrics metrics;
John Reckdbffd252015-10-01 14:46:12 -0700437 getMetricsInternal(paintHandle, typefaceHandle, &metrics);
Raph Levien817ef4a2014-06-05 22:38:08 -0700438 return SkScalarToFloat(metrics.fDescent);
439 }
440
John Reckdbffd252015-10-01 14:46:12 -0700441 static jfloat getFontMetrics(JNIEnv* env, jobject, jlong paintHandle,
442 jlong typefaceHandle, jobject metricsObj) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400443 Paint::FontMetrics metrics;
John Reckdbffd252015-10-01 14:46:12 -0700444 SkScalar spacing = getMetricsInternal(paintHandle, typefaceHandle, &metrics);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
446 if (metricsObj) {
447 SkASSERT(env->IsInstanceOf(metricsObj, gFontMetrics_class));
448 env->SetFloatField(metricsObj, gFontMetrics_fieldID.top, SkScalarToFloat(metrics.fTop));
449 env->SetFloatField(metricsObj, gFontMetrics_fieldID.ascent, SkScalarToFloat(metrics.fAscent));
450 env->SetFloatField(metricsObj, gFontMetrics_fieldID.descent, SkScalarToFloat(metrics.fDescent));
451 env->SetFloatField(metricsObj, gFontMetrics_fieldID.bottom, SkScalarToFloat(metrics.fBottom));
452 env->SetFloatField(metricsObj, gFontMetrics_fieldID.leading, SkScalarToFloat(metrics.fLeading));
453 }
454 return SkScalarToFloat(spacing);
455 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700456
John Reckdbffd252015-10-01 14:46:12 -0700457 static jint getFontMetricsInt(JNIEnv* env, jobject, jlong paintHandle,
458 jlong typefaceHandle, jobject metricsObj) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400459 Paint::FontMetrics metrics;
Elliott Hughes8451b252011-04-07 19:17:57 -0700460
John Reckdbffd252015-10-01 14:46:12 -0700461 getMetricsInternal(paintHandle, typefaceHandle, &metrics);
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500462 int ascent = SkScalarRoundToInt(metrics.fAscent);
463 int descent = SkScalarRoundToInt(metrics.fDescent);
464 int leading = SkScalarRoundToInt(metrics.fLeading);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465
466 if (metricsObj) {
467 SkASSERT(env->IsInstanceOf(metricsObj, gFontMetricsInt_class));
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500468 env->SetIntField(metricsObj, gFontMetricsInt_fieldID.top, SkScalarFloorToInt(metrics.fTop));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 env->SetIntField(metricsObj, gFontMetricsInt_fieldID.ascent, ascent);
470 env->SetIntField(metricsObj, gFontMetricsInt_fieldID.descent, descent);
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500471 env->SetIntField(metricsObj, gFontMetricsInt_fieldID.bottom, SkScalarCeilToInt(metrics.fBottom));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 env->SetIntField(metricsObj, gFontMetricsInt_fieldID.leading, leading);
473 }
474 return descent - ascent + leading;
475 }
476
sergeyvbad99182016-03-17 11:24:22 -0700477 static jfloat doTextAdvances(JNIEnv *env, Paint *paint, Typeface* typeface,
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700478 const jchar *text, jint start, jint count, jint contextCount, jint bidiFlags,
479 jfloatArray advances, jint advancesIndex) {
Fabrice Di Meglio6ab90ed2011-08-08 16:19:38 -0700480 NPE_CHECK_RETURN_ZERO(env, text);
481
482 if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) {
483 doThrowAIOOBE(env);
484 return 0;
485 }
486 if (count == 0) {
487 return 0;
488 }
489 if (advances) {
490 size_t advancesLength = env->GetArrayLength(advances);
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700491 if ((size_t)(count + advancesIndex) > advancesLength) {
Fabrice Di Meglio6ab90ed2011-08-08 16:19:38 -0700492 doThrowAIOOBE(env);
493 return 0;
494 }
495 }
Keisuke Kuroyanagia3024bd2015-09-16 20:02:15 -0700496 std::unique_ptr<jfloat[]> advancesArray;
497 if (advances) {
498 advancesArray.reset(new jfloat[count]);
499 }
500 const float advance = MinikinUtils::measureText(paint, bidiFlags, typeface, text,
501 start, count, contextCount, advancesArray.get());
502 if (advances) {
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700503 env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray.get());
Doug Felt0c702b82010-05-14 10:55:42 -0700504 }
Keisuke Kuroyanagia3024bd2015-09-16 20:02:15 -0700505 return advance;
Doug Felt0c702b82010-05-14 10:55:42 -0700506 }
507
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700508 static jfloat getTextAdvances___CIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle,
Raph Levien1a73f7322014-01-30 16:06:28 -0800509 jlong typefaceHandle,
Doug Felt0c702b82010-05-14 10:55:42 -0700510 jcharArray text, jint index, jint count, jint contextIndex, jint contextCount,
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700511 jint bidiFlags, jfloatArray advances, jint advancesIndex) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400512 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700513 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
Doug Felt0c702b82010-05-14 10:55:42 -0700514 jchar* textArray = env->GetCharArrayElements(text, NULL);
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700515 jfloat result = doTextAdvances(env, paint, typeface, textArray + contextIndex,
516 index - contextIndex, count, contextCount, bidiFlags, advances, advancesIndex);
Doug Felt0c702b82010-05-14 10:55:42 -0700517 env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
518 return result;
519 }
520
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700521 static jfloat getTextAdvances__StringIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle,
Raph Levien1a73f7322014-01-30 16:06:28 -0800522 jlong typefaceHandle,
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700523 jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint bidiFlags,
Fabrice Di Meglio665f02c2013-03-20 14:56:05 -0700524 jfloatArray advances, jint advancesIndex) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400525 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700526 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
Doug Felt0c702b82010-05-14 10:55:42 -0700527 const jchar* textArray = env->GetStringChars(text, NULL);
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700528 jfloat result = doTextAdvances(env, paint, typeface, textArray + contextStart,
529 start - contextStart, end - start, contextEnd - contextStart, bidiFlags,
Fabrice Di Meglio665f02c2013-03-20 14:56:05 -0700530 advances, advancesIndex);
Fabrice Di Meglioeee49c62011-03-24 17:21:23 -0700531 env->ReleaseStringChars(text, textArray);
532 return result;
533 }
534
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400535 static jint doTextRunCursor(JNIEnv *env, Paint* paint, const jchar *text, jint start,
Fabrice Di Meglioa4bf8112013-03-15 11:22:29 -0700536 jint count, jint flags, jint offset, jint opt) {
Raph Leviene368b6b2014-06-15 17:37:57 -0700537 GraphemeBreak::MoveOpt moveOpt = GraphemeBreak::MoveOpt(opt);
538 size_t result = GraphemeBreak::getTextRunCursor(text, start, count, offset, moveOpt);
539 return static_cast<jint>(result);
Doug Felt0c702b82010-05-14 10:55:42 -0700540 }
541
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000542 static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text,
Raph Levien051910b2014-06-15 18:25:29 -0700543 jint contextStart, jint contextCount, jint dir, jint offset, jint cursorOpt) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400544 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Doug Felt0c702b82010-05-14 10:55:42 -0700545 jchar* textArray = env->GetCharArrayElements(text, NULL);
Raph Levien051910b2014-06-15 18:25:29 -0700546 jint result = doTextRunCursor(env, paint, textArray, contextStart, contextCount, dir,
Doug Felt0c702b82010-05-14 10:55:42 -0700547 offset, cursorOpt);
548 env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
549 return result;
550 }
551
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000552 static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, jlong paintHandle, jstring text,
Raph Levien051910b2014-06-15 18:25:29 -0700553 jint contextStart, jint contextEnd, jint dir, jint offset, jint cursorOpt) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400554 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Doug Felt0c702b82010-05-14 10:55:42 -0700555 const jchar* textArray = env->GetStringChars(text, NULL);
556 jint result = doTextRunCursor(env, paint, textArray, contextStart,
Raph Levien051910b2014-06-15 18:25:29 -0700557 contextEnd - contextStart, dir, offset, cursorOpt);
Doug Felt0c702b82010-05-14 10:55:42 -0700558 env->ReleaseStringChars(text, textArray);
559 return result;
560 }
561
Raph Levienf2114d52014-06-01 15:54:47 -0700562 class GetTextFunctor {
563 public:
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400564 GetTextFunctor(const Layout& layout, SkPath* path, jfloat x, jfloat y, Paint* paint,
Raph Levienf2114d52014-06-01 15:54:47 -0700565 uint16_t* glyphs, SkPoint* pos)
566 : layout(layout), path(path), x(x), y(y), paint(paint), glyphs(glyphs), pos(pos) {
567 }
568
Raph Levien1fc0fa82014-06-06 18:05:22 -0700569 void operator()(size_t start, size_t end) {
Raph Levienf2114d52014-06-01 15:54:47 -0700570 for (size_t i = start; i < end; i++) {
571 glyphs[i] = layout.getGlyphId(i);
572 pos[i].fX = x + layout.getX(i);
573 pos[i].fY = y + layout.getY(i);
574 }
Raph Levienf2114d52014-06-01 15:54:47 -0700575 if (start == 0) {
576 paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, path);
577 } else {
578 paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, &tmpPath);
579 path->addPath(tmpPath);
580 }
581 }
582 private:
583 const Layout& layout;
584 SkPath* path;
585 jfloat x;
586 jfloat y;
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400587 Paint* paint;
Raph Levienf2114d52014-06-01 15:54:47 -0700588 uint16_t* glyphs;
589 SkPoint* pos;
590 SkPath tmpPath;
591 };
Raph Levienf2114d52014-06-01 15:54:47 -0700592
sergeyvbad99182016-03-17 11:24:22 -0700593 static void getTextPath(JNIEnv* env, Paint* paint, Typeface* typeface, const jchar* text,
Raph Levienf2114d52014-06-01 15:54:47 -0700594 jint count, jint bidiFlags, jfloat x, jfloat y, SkPath* path) {
Raph Levienf2114d52014-06-01 15:54:47 -0700595 Layout layout;
Behdad Esfahbod63c5c782014-07-25 14:54:46 -0400596 MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, 0, count, count);
Raph Levienf2114d52014-06-01 15:54:47 -0700597 size_t nGlyphs = layout.nGlyphs();
598 uint16_t* glyphs = new uint16_t[nGlyphs];
599 SkPoint* pos = new SkPoint[nGlyphs];
600
601 x += MinikinUtils::xOffsetForTextAlign(paint, layout);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400602 Paint::Align align = paint->getTextAlign();
603 paint->setTextAlign(Paint::kLeft_Align);
604 paint->setTextEncoding(Paint::kGlyphID_TextEncoding);
Raph Levienf2114d52014-06-01 15:54:47 -0700605 GetTextFunctor f(layout, path, x, y, paint, glyphs, pos);
Raph Levien1fc0fa82014-06-06 18:05:22 -0700606 MinikinUtils::forFontRun(layout, paint, f);
Raph Levienf2114d52014-06-01 15:54:47 -0700607 paint->setTextAlign(align);
608 delete[] glyphs;
609 delete[] pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
Doug Feltf7cb1f72010-07-01 16:20:43 -0700611
Raph Levienf2114d52014-06-01 15:54:47 -0700612 static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle,
613 jlong typefaceHandle, jint bidiFlags,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000614 jcharArray text, jint index, jint count, jfloat x, jfloat y, jlong pathHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400615 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700616 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000617 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
Doug Feltf7cb1f72010-07-01 16:20:43 -0700618 const jchar* textArray = env->GetCharArrayElements(text, NULL);
Raph Levienf2114d52014-06-01 15:54:47 -0700619 getTextPath(env, paint, typeface, textArray + index, count, bidiFlags, x, y, path);
Doug Feltf7cb1f72010-07-01 16:20:43 -0700620 env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
621 }
622
Raph Levienf2114d52014-06-01 15:54:47 -0700623 static void getTextPath__String(JNIEnv* env, jobject clazz, jlong paintHandle,
624 jlong typefaceHandle, jint bidiFlags,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000625 jstring text, jint start, jint end, jfloat x, jfloat y, jlong pathHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400626 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700627 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000628 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 const jchar* textArray = env->GetStringChars(text, NULL);
Raph Levienf2114d52014-06-01 15:54:47 -0700630 getTextPath(env, paint, typeface, textArray + start, end - start, bidiFlags, x, y, path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 env->ReleaseStringChars(text, textArray);
632 }
Doug Feltf7cb1f72010-07-01 16:20:43 -0700633
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -0400634 static void setShadowLayer(JNIEnv* env, jobject clazz, jlong paintHandle, jfloat radius,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000635 jfloat dx, jfloat dy, jint color) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400636 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 if (radius <= 0) {
638 paint->setLooper(NULL);
639 }
640 else {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -0400641 SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius);
Victoria Lease49f0db32014-05-19 16:00:17 -0700642 paint->setLooper(SkBlurDrawLooper::Create((SkColor)color, sigma, dx, dy))->unref();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 }
644 }
645
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -0400646 static jboolean hasShadowLayer(JNIEnv* env, jobject clazz, jlong paintHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400647 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -0400648 return paint->getLooper() && paint->getLooper()->asABlurShadow(NULL);
649 }
650
sergeyvbad99182016-03-17 11:24:22 -0700651 static int breakText(JNIEnv* env, const Paint& paint, Typeface* typeface, const jchar text[],
Victoria Lease626d3c22013-03-27 15:35:53 -0700652 int count, float maxWidth, jint bidiFlags, jfloatArray jmeasured,
Derek Sollenberger57c4fed2014-10-08 09:23:32 -0400653 const bool forwardScan) {
Anish Athalyea7aa1b02014-06-12 12:53:09 -0700654 size_t measuredCount = 0;
655 float measured = 0;
656
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800657 std::unique_ptr<float[]> advancesArray(new float[count]);
658 MinikinUtils::measureText(&paint, bidiFlags, typeface, text, 0, count, count,
659 advancesArray.get());
Derek Sollenberger57c4fed2014-10-08 09:23:32 -0400660
Anish Athalyea7aa1b02014-06-12 12:53:09 -0700661 for (int i = 0; i < count; i++) {
662 // traverse in the given direction
663 int index = forwardScan ? i : (count - i - 1);
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800664 float width = advancesArray[index];
Anish Athalyea7aa1b02014-06-12 12:53:09 -0700665 if (measured + width > maxWidth) {
666 break;
667 }
668 // properly handle clusters when scanning backwards
669 if (forwardScan || width != 0.0f) {
670 measuredCount = i + 1;
671 }
672 measured += width;
673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674
675 if (jmeasured && env->GetArrayLength(jmeasured) > 0) {
676 AutoJavaFloatArray autoMeasured(env, jmeasured, 1);
677 jfloat* array = autoMeasured.ptr();
Anish Athalyea7aa1b02014-06-12 12:53:09 -0700678 array[0] = measured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 }
Anish Athalyea7aa1b02014-06-12 12:53:09 -0700680 return measuredCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682
Anish Athalyea7aa1b02014-06-12 12:53:09 -0700683 static jint breakTextC(JNIEnv* env, jobject clazz, jlong paintHandle, jlong typefaceHandle, jcharArray jtext,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000684 jint index, jint count, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 NPE_CHECK_RETURN_ZERO(env, jtext);
686
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400687 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700688 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
Anish Athalyea7aa1b02014-06-12 12:53:09 -0700689
Derek Sollenberger57c4fed2014-10-08 09:23:32 -0400690 bool forwardTextDirection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 if (count < 0) {
Derek Sollenberger57c4fed2014-10-08 09:23:32 -0400692 forwardTextDirection = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 count = -count;
694 }
695 else {
Derek Sollenberger57c4fed2014-10-08 09:23:32 -0400696 forwardTextDirection = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
698
699 if ((index < 0) || (index + count > env->GetArrayLength(jtext))) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700700 doThrowAIOOBE(env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 return 0;
702 }
703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 const jchar* text = env->GetCharArrayElements(jtext, NULL);
Anish Athalyea7aa1b02014-06-12 12:53:09 -0700705 count = breakText(env, *paint, typeface, text + index, count, maxWidth,
Derek Sollenberger57c4fed2014-10-08 09:23:32 -0400706 bidiFlags, jmeasuredWidth, forwardTextDirection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text),
708 JNI_ABORT);
709 return count;
710 }
711
Anish Athalyea7aa1b02014-06-12 12:53:09 -0700712 static jint breakTextS(JNIEnv* env, jobject clazz, jlong paintHandle, jlong typefaceHandle, jstring jtext,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000713 jboolean forwards, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 NPE_CHECK_RETURN_ZERO(env, jtext);
715
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400716 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700717 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
Anish Athalyea7aa1b02014-06-12 12:53:09 -0700718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 int count = env->GetStringLength(jtext);
720 const jchar* text = env->GetStringChars(jtext, NULL);
Derek Sollenberger57c4fed2014-10-08 09:23:32 -0400721 count = breakText(env, *paint, typeface, text, count, maxWidth, bidiFlags, jmeasuredWidth, forwards);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 env->ReleaseStringChars(jtext, text);
723 return count;
724 }
725
Raph Levien854363e2014-06-03 19:56:05 -0700726 static void doTextBounds(JNIEnv* env, const jchar* text, int count, jobject bounds,
sergeyvbad99182016-03-17 11:24:22 -0700727 const Paint& paint, Typeface* typeface, jint bidiFlags) {
Romain Guy059e12c2012-11-28 17:35:51 -0800728 SkRect r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 SkIRect ir;
Elliott Hughes8451b252011-04-07 19:17:57 -0700730
Raph Levien854363e2014-06-03 19:56:05 -0700731 Layout layout;
Behdad Esfahbod63c5c782014-07-25 14:54:46 -0400732 MinikinUtils::doLayout(&layout, &paint, bidiFlags, typeface, text, 0, count, count);
Raph Levien854363e2014-06-03 19:56:05 -0700733 MinikinRect rect;
734 layout.getBounds(&rect);
735 r.fLeft = rect.mLeft;
736 r.fTop = rect.mTop;
737 r.fRight = rect.mRight;
738 r.fBottom = rect.mBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 r.roundOut(&ir);
740 GraphicsJNI::irect_to_jrect(ir, env, bounds);
741 }
742
Raph Levien854363e2014-06-03 19:56:05 -0700743 static void getStringBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000744 jstring text, jint start, jint end, jint bidiFlags, jobject bounds) {
John Reckdbffd252015-10-01 14:46:12 -0700745 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700746 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 const jchar* textArray = env->GetStringChars(text, NULL);
Raph Levien854363e2014-06-03 19:56:05 -0700748 doTextBounds(env, textArray + start, end - start, bounds, *paint, typeface, bidiFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 env->ReleaseStringChars(text, textArray);
750 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700751
Raph Levien854363e2014-06-03 19:56:05 -0700752 static void getCharArrayBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000753 jcharArray text, jint index, jint count, jint bidiFlags, jobject bounds) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400754 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700755 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 const jchar* textArray = env->GetCharArrayElements(text, NULL);
Raph Levien854363e2014-06-03 19:56:05 -0700757 doTextBounds(env, textArray + index, count, bounds, *paint, typeface, bidiFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray),
759 JNI_ABORT);
760 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700761
Raph Levienf7f969e62015-04-01 14:41:21 -0700762 static jboolean layoutContainsNotdef(const Layout& layout) {
763 for (size_t i = 0; i < layout.nGlyphs(); i++) {
764 if (layout.getGlyphId(i) == 0) {
765 return true;
766 }
767 }
768 return false;
769 }
770
Seigo Nonaka589cddc2016-04-05 14:43:14 +0900771 // Returns true if the given string is exact one pair of regional indicators.
772 static bool isFlag(const jchar* str, size_t length) {
773 const jchar RI_LEAD_SURROGATE = 0xD83C;
774 const jchar RI_TRAIL_SURROGATE_MIN = 0xDDE6;
775 const jchar RI_TRAIL_SURROGATE_MAX = 0xDDFF;
776
777 if (length != 4) {
778 return false;
779 }
780 if (str[0] != RI_LEAD_SURROGATE || str[2] != RI_LEAD_SURROGATE) {
781 return false;
782 }
783 return RI_TRAIL_SURROGATE_MIN <= str[1] && str[1] <= RI_TRAIL_SURROGATE_MAX &&
784 RI_TRAIL_SURROGATE_MIN <= str[3] && str[3] <= RI_TRAIL_SURROGATE_MAX;
785 }
786
Raph Levienf7f969e62015-04-01 14:41:21 -0700787 static jboolean hasGlyph(JNIEnv *env, jclass, jlong paintHandle, jlong typefaceHandle,
788 jint bidiFlags, jstring string) {
789 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700790 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
Raph Levienf7f969e62015-04-01 14:41:21 -0700791 ScopedStringChars str(env, string);
792
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900793 /* Start by rejecting unsupported base code point and variation selector pairs. */
Raph Levienf7f969e62015-04-01 14:41:21 -0700794 size_t nChars = 0;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900795 const uint32_t kStartOfString = 0xFFFFFFFF;
796 uint32_t prevCp = kStartOfString;
Raph Levienf7f969e62015-04-01 14:41:21 -0700797 for (size_t i = 0; i < str.size(); i++) {
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900798 jchar cu = str[i];
799 uint32_t cp = cu;
800 if (U16_IS_TRAIL(cu)) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700801 // invalid UTF-16, unpaired trailing surrogate
802 return false;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900803 } else if (U16_IS_LEAD(cu)) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700804 if (i + 1 == str.size()) {
805 // invalid UTF-16, unpaired leading surrogate at end of string
806 return false;
807 }
808 i++;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900809 jchar cu2 = str[i];
810 if (!U16_IS_TRAIL(cu2)) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700811 // invalid UTF-16, unpaired leading surrogate
812 return false;
813 }
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900814 cp = U16_GET_SUPPLEMENTARY(cu, cu2);
815 }
816
817 if (prevCp != kStartOfString &&
818 ((0xFE00 <= cp && cp <= 0xFE0F) || (0xE0100 <= cp && cp <= 0xE01EF)) &&
819 !MinikinUtils::hasVariationSelector(typeface, prevCp, cp)) {
820 // No font has a glyph for the code point and variation selector pair.
821 return false;
Raph Levienf7f969e62015-04-01 14:41:21 -0700822 }
823 nChars++;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900824 prevCp = cp;
Raph Levienf7f969e62015-04-01 14:41:21 -0700825 }
826 Layout layout;
827 MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, str.get(), 0, str.size(),
828 str.size());
829 size_t nGlyphs = layout.nGlyphs();
830 if (nGlyphs != 1 && nChars > 1) {
831 // multiple-character input, and was not a ligature
832 // TODO: handle ZWJ/ZWNJ characters specially so we can detect certain ligatures
833 // in joining scripts, such as Arabic and Mongolian.
834 return false;
835 }
Seigo Nonaka589cddc2016-04-05 14:43:14 +0900836
837 if (nGlyphs == 0 || layoutContainsNotdef(layout)) {
838 return false; // The collection doesn't have a glyph.
839 }
840
841 if (nChars == 2 && isFlag(str.get(), str.size())) {
842 // Some font may have a special glyph for unsupported regional indicator pairs.
843 // To return false for this case, need to compare the glyph id with the one of ZZ
844 // since ZZ is reserved for unknown or invalid territory.
845 // U+1F1FF (REGIONAL INDICATOR SYMBOL LETTER Z) is \uD83C\uDDFF in UTF16.
846 static const jchar ZZ_FLAG_STR[] = { 0xD83C, 0xDDFF, 0xD83C, 0xDDFF };
847 Layout zzLayout;
848 MinikinUtils::doLayout(&zzLayout, paint, bidiFlags, typeface, ZZ_FLAG_STR, 0, 4, 4);
849 if (zzLayout.nGlyphs() != 1 || layoutContainsNotdef(zzLayout)) {
850 // The font collection doesn't have a glyph for unknown flag. Just return true.
851 return true;
852 }
853 return zzLayout.getGlyphId(0) != layout.getGlyphId(0);
854 }
855 return true;
Raph Levienf7f969e62015-04-01 14:41:21 -0700856 }
857
sergeyvbad99182016-03-17 11:24:22 -0700858 static jfloat doRunAdvance(const Paint* paint, Typeface* typeface, const jchar buf[],
Raph Leviena027ec52015-04-06 16:21:59 -0700859 jint start, jint count, jint bufSize, jboolean isRtl, jint offset) {
Raph Leviena027ec52015-04-06 16:21:59 -0700860 int bidiFlags = isRtl ? kBidi_Force_RTL : kBidi_Force_LTR;
Keisuke Kuroyanagib3677712016-03-28 19:16:30 +0900861 if (offset == start + count) {
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800862 return MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count,
863 bufSize, nullptr);
864 }
865 std::unique_ptr<float[]> advancesArray(new float[count]);
866 MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize,
867 advancesArray.get());
868 return getRunAdvance(advancesArray.get(), buf, start, count, offset);
Raph Leviena027ec52015-04-06 16:21:59 -0700869 }
870
871 static jfloat getRunAdvance___CIIIIZI_F(JNIEnv *env, jclass, jlong paintHandle,
872 jlong typefaceHandle, jcharArray text, jint start, jint end, jint contextStart,
873 jint contextEnd, jboolean isRtl, jint offset) {
874 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700875 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
John Reckc6024cd2015-07-10 10:55:49 -0700876 jchar* textArray = (jchar*) env->GetPrimitiveArrayCritical(text, NULL);
Raph Leviena027ec52015-04-06 16:21:59 -0700877 jfloat result = doRunAdvance(paint, typeface, textArray + contextStart,
Raph Levien36ff86c2015-06-03 10:58:33 -0700878 start - contextStart, end - start, contextEnd - contextStart, isRtl,
879 offset - contextStart);
John Reckc6024cd2015-07-10 10:55:49 -0700880 env->ReleasePrimitiveArrayCritical(text, textArray, JNI_ABORT);
Raph Leviena027ec52015-04-06 16:21:59 -0700881 return result;
882 }
883
sergeyvbad99182016-03-17 11:24:22 -0700884 static jint doOffsetForAdvance(const Paint* paint, Typeface* typeface, const jchar buf[],
Raph Leviena027ec52015-04-06 16:21:59 -0700885 jint start, jint count, jint bufSize, jboolean isRtl, jfloat advance) {
Raph Leviena027ec52015-04-06 16:21:59 -0700886 int bidiFlags = isRtl ? kBidi_Force_RTL : kBidi_Force_LTR;
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800887 std::unique_ptr<float[]> advancesArray(new float[count]);
888 MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize,
889 advancesArray.get());
890 return getOffsetForAdvance(advancesArray.get(), buf, start, count, advance);
Raph Leviena027ec52015-04-06 16:21:59 -0700891 }
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800892
Raph Leviena027ec52015-04-06 16:21:59 -0700893 static jint getOffsetForAdvance___CIIIIZF_I(JNIEnv *env, jclass, jlong paintHandle,
894 jlong typefaceHandle, jcharArray text, jint start, jint end, jint contextStart,
895 jint contextEnd, jboolean isRtl, jfloat advance) {
896 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
sergeyvbad99182016-03-17 11:24:22 -0700897 Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
John Reckc6024cd2015-07-10 10:55:49 -0700898 jchar* textArray = (jchar*) env->GetPrimitiveArrayCritical(text, NULL);
Raph Leviena027ec52015-04-06 16:21:59 -0700899 jint result = doOffsetForAdvance(paint, typeface, textArray + contextStart,
900 start - contextStart, end - start, contextEnd - contextStart, isRtl, advance);
901 result += contextStart;
John Reckc6024cd2015-07-10 10:55:49 -0700902 env->ReleasePrimitiveArrayCritical(text, textArray, JNI_ABORT);
Raph Leviena027ec52015-04-06 16:21:59 -0700903 return result;
904 }
905
John Reckdbffd252015-10-01 14:46:12 -0700906}; // namespace PaintGlue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907
Daniel Micay76f6a862015-09-19 17:31:01 -0400908static const JNINativeMethod methods[] = {
Richard Uhler775873a2015-12-29 12:37:39 -0800909 {"nGetNativeFinalizer", "()J", (void*) PaintGlue::getNativeFinalizer},
John Reckdbffd252015-10-01 14:46:12 -0700910 {"nInit","()J", (void*) PaintGlue::init},
911 {"nInitWithPaint","(J)J", (void*) PaintGlue::initWithPaint},
Chris Craik4136a0a2014-10-07 15:09:46 -0700912
John Reckdbffd252015-10-01 14:46:12 -0700913 {"nReset","!(J)V", (void*) PaintGlue::reset},
914 {"nSet","!(JJ)V", (void*) PaintGlue::assign},
915 {"nGetFlags","!(J)I", (void*) PaintGlue::getFlags},
916 {"nSetFlags","!(JI)V", (void*) PaintGlue::setFlags},
917 {"nGetHinting","!(J)I", (void*) PaintGlue::getHinting},
918 {"nSetHinting","!(JI)V", (void*) PaintGlue::setHinting},
919 {"nSetAntiAlias","!(JZ)V", (void*) PaintGlue::setAntiAlias},
920 {"nSetSubpixelText","!(JZ)V", (void*) PaintGlue::setSubpixelText},
921 {"nSetLinearText","!(JZ)V", (void*) PaintGlue::setLinearText},
922 {"nSetUnderlineText","!(JZ)V", (void*) PaintGlue::setUnderlineText},
923 {"nSetStrikeThruText","!(JZ)V", (void*) PaintGlue::setStrikeThruText},
924 {"nSetFakeBoldText","!(JZ)V", (void*) PaintGlue::setFakeBoldText},
925 {"nSetFilterBitmap","!(JZ)V", (void*) PaintGlue::setFilterBitmap},
926 {"nSetDither","!(JZ)V", (void*) PaintGlue::setDither},
927 {"nGetStyle","!(J)I", (void*) PaintGlue::getStyle},
928 {"nSetStyle","!(JI)V", (void*) PaintGlue::setStyle},
929 {"nGetColor","!(J)I", (void*) PaintGlue::getColor},
930 {"nSetColor","!(JI)V", (void*) PaintGlue::setColor},
931 {"nGetAlpha","!(J)I", (void*) PaintGlue::getAlpha},
932 {"nSetAlpha","!(JI)V", (void*) PaintGlue::setAlpha},
933 {"nGetStrokeWidth","!(J)F", (void*) PaintGlue::getStrokeWidth},
934 {"nSetStrokeWidth","!(JF)V", (void*) PaintGlue::setStrokeWidth},
935 {"nGetStrokeMiter","!(J)F", (void*) PaintGlue::getStrokeMiter},
936 {"nSetStrokeMiter","!(JF)V", (void*) PaintGlue::setStrokeMiter},
937 {"nGetStrokeCap","!(J)I", (void*) PaintGlue::getStrokeCap},
938 {"nSetStrokeCap","!(JI)V", (void*) PaintGlue::setStrokeCap},
939 {"nGetStrokeJoin","!(J)I", (void*) PaintGlue::getStrokeJoin},
940 {"nSetStrokeJoin","!(JI)V", (void*) PaintGlue::setStrokeJoin},
941 {"nGetFillPath","!(JJJ)Z", (void*) PaintGlue::getFillPath},
942 {"nSetShader","!(JJ)J", (void*) PaintGlue::setShader},
943 {"nSetColorFilter","!(JJ)J", (void*) PaintGlue::setColorFilter},
944 {"nSetXfermode","!(JJ)J", (void*) PaintGlue::setXfermode},
945 {"nSetPathEffect","!(JJ)J", (void*) PaintGlue::setPathEffect},
946 {"nSetMaskFilter","!(JJ)J", (void*) PaintGlue::setMaskFilter},
947 {"nSetTypeface","!(JJ)J", (void*) PaintGlue::setTypeface},
948 {"nSetRasterizer","!(JJ)J", (void*) PaintGlue::setRasterizer},
949 {"nGetTextAlign","!(J)I", (void*) PaintGlue::getTextAlign},
950 {"nSetTextAlign","!(JI)V", (void*) PaintGlue::setTextAlign},
Seigo Nonakacfc607c2015-12-02 10:53:18 +0900951 {"nSetTextLocales","!(JLjava/lang/String;)I", (void*) PaintGlue::setTextLocales},
952 {"nSetTextLocalesByMinikinLangListId","!(JI)V",
953 (void*) PaintGlue::setTextLocalesByMinikinLangListId},
John Reckdbffd252015-10-01 14:46:12 -0700954 {"nIsElegantTextHeight","!(J)Z", (void*) PaintGlue::isElegantTextHeight},
955 {"nSetElegantTextHeight","!(JZ)V", (void*) PaintGlue::setElegantTextHeight},
956 {"nGetTextSize","!(J)F", (void*) PaintGlue::getTextSize},
957 {"nSetTextSize","!(JF)V", (void*) PaintGlue::setTextSize},
958 {"nGetTextScaleX","!(J)F", (void*) PaintGlue::getTextScaleX},
959 {"nSetTextScaleX","!(JF)V", (void*) PaintGlue::setTextScaleX},
960 {"nGetTextSkewX","!(J)F", (void*) PaintGlue::getTextSkewX},
961 {"nSetTextSkewX","!(JF)V", (void*) PaintGlue::setTextSkewX},
962 {"nGetLetterSpacing","!(J)F", (void*) PaintGlue::getLetterSpacing},
963 {"nSetLetterSpacing","!(JF)V", (void*) PaintGlue::setLetterSpacing},
964 {"nSetFontFeatureSettings","(JLjava/lang/String;)V",
Raph Leviena027ec52015-04-06 16:21:59 -0700965 (void*) PaintGlue::setFontFeatureSettings},
John Reckdbffd252015-10-01 14:46:12 -0700966 {"nGetHyphenEdit", "!(J)I", (void*) PaintGlue::getHyphenEdit},
967 {"nSetHyphenEdit", "!(JI)V", (void*) PaintGlue::setHyphenEdit},
968 {"nAscent","!(JJ)F", (void*) PaintGlue::ascent},
969 {"nDescent","!(JJ)F", (void*) PaintGlue::descent},
Chris Craik4136a0a2014-10-07 15:09:46 -0700970
John Reckdbffd252015-10-01 14:46:12 -0700971 {"nGetFontMetrics", "!(JJLandroid/graphics/Paint$FontMetrics;)F",
Raph Leviena027ec52015-04-06 16:21:59 -0700972 (void*)PaintGlue::getFontMetrics},
John Reckdbffd252015-10-01 14:46:12 -0700973 {"nGetFontMetricsInt", "!(JJLandroid/graphics/Paint$FontMetricsInt;)I",
Raph Leviena027ec52015-04-06 16:21:59 -0700974 (void*)PaintGlue::getFontMetricsInt},
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700975
John Reckdbffd252015-10-01 14:46:12 -0700976 {"nBreakText","(JJ[CIIFI[F)I", (void*) PaintGlue::breakTextC},
977 {"nBreakText","(JJLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS},
978 {"nGetTextAdvances","(JJ[CIIIII[FI)F",
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700979 (void*) PaintGlue::getTextAdvances___CIIIII_FI},
John Reckdbffd252015-10-01 14:46:12 -0700980 {"nGetTextAdvances","(JJLjava/lang/String;IIIII[FI)F",
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700981 (void*) PaintGlue::getTextAdvances__StringIIIII_FI},
Fabrice Di Meglioda12f382013-03-15 11:26:56 -0700982
John Reckdbffd252015-10-01 14:46:12 -0700983 {"nGetTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C},
984 {"nGetTextRunCursor", "(JLjava/lang/String;IIIII)I",
Raph Leviena027ec52015-04-06 16:21:59 -0700985 (void*) PaintGlue::getTextRunCursor__String},
John Reckdbffd252015-10-01 14:46:12 -0700986 {"nGetTextPath", "(JJI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C},
987 {"nGetTextPath", "(JJILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String},
988 {"nGetStringBounds", "(JJLjava/lang/String;IIILandroid/graphics/Rect;)V",
Raph Leviena027ec52015-04-06 16:21:59 -0700989 (void*) PaintGlue::getStringBounds },
John Reckdbffd252015-10-01 14:46:12 -0700990 {"nGetCharArrayBounds", "(JJ[CIIILandroid/graphics/Rect;)V",
Raph Leviena027ec52015-04-06 16:21:59 -0700991 (void*) PaintGlue::getCharArrayBounds },
John Reckdbffd252015-10-01 14:46:12 -0700992 {"nHasGlyph", "(JJILjava/lang/String;)Z", (void*) PaintGlue::hasGlyph },
993 {"nGetRunAdvance", "(JJ[CIIIIZI)F", (void*) PaintGlue::getRunAdvance___CIIIIZI_F},
994 {"nGetOffsetForAdvance", "(JJ[CIIIIZF)I",
Raph Leviena027ec52015-04-06 16:21:59 -0700995 (void*) PaintGlue::getOffsetForAdvance___CIIIIZF_I},
Chris Craik4136a0a2014-10-07 15:09:46 -0700996
John Reckdbffd252015-10-01 14:46:12 -0700997 {"nSetShadowLayer", "!(JFFFI)V", (void*)PaintGlue::setShadowLayer},
998 {"nHasShadowLayer", "!(J)Z", (void*)PaintGlue::hasShadowLayer}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999};
1000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001int register_android_graphics_Paint(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001002 gFontMetrics_class = FindClassOrDie(env, "android/graphics/Paint$FontMetrics");
1003 gFontMetrics_class = MakeGlobalRefOrDie(env, gFontMetrics_class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001005 gFontMetrics_fieldID.top = GetFieldIDOrDie(env, gFontMetrics_class, "top", "F");
1006 gFontMetrics_fieldID.ascent = GetFieldIDOrDie(env, gFontMetrics_class, "ascent", "F");
1007 gFontMetrics_fieldID.descent = GetFieldIDOrDie(env, gFontMetrics_class, "descent", "F");
1008 gFontMetrics_fieldID.bottom = GetFieldIDOrDie(env, gFontMetrics_class, "bottom", "F");
1009 gFontMetrics_fieldID.leading = GetFieldIDOrDie(env, gFontMetrics_class, "leading", "F");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001011 gFontMetricsInt_class = FindClassOrDie(env, "android/graphics/Paint$FontMetricsInt");
1012 gFontMetricsInt_class = MakeGlobalRefOrDie(env, gFontMetricsInt_class);
Derek Sollenbergerab22c1c2014-09-03 14:12:47 -04001013
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001014 gFontMetricsInt_fieldID.top = GetFieldIDOrDie(env, gFontMetricsInt_class, "top", "I");
1015 gFontMetricsInt_fieldID.ascent = GetFieldIDOrDie(env, gFontMetricsInt_class, "ascent", "I");
1016 gFontMetricsInt_fieldID.descent = GetFieldIDOrDie(env, gFontMetricsInt_class, "descent", "I");
1017 gFontMetricsInt_fieldID.bottom = GetFieldIDOrDie(env, gFontMetricsInt_class, "bottom", "I");
1018 gFontMetricsInt_fieldID.leading = GetFieldIDOrDie(env, gFontMetricsInt_class, "leading", "I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001020 return RegisterMethodsOrDie(env, "android/graphics/Paint", methods, NELEM(methods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021}
1022
1023}