blob: 22a1e1fd94b94ac5b8c73f01df327b00c5f6a9c8 [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
Derek Sollenberger5368eda2019-10-25 11:20:03 -040018#undef LOG_TAG
Dianne Hackbornf43fa572011-08-12 18:59:39 -070019#define LOG_TAG "Paint"
20
21#include <utils/Log.h>
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include "GraphicsJNI.h"
Steven Moreland2279b252017-07-19 09:50:45 -070024#include <nativehelper/ScopedStringChars.h>
25#include <nativehelper/ScopedUtfChars.h>
Seigo Nonakaee23f612018-01-27 15:08:25 -080026#include <nativehelper/ScopedPrimitiveArray.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include "SkColorFilter.h"
Mike Reedf6d86ac2019-01-18 14:13:23 -050029#include "SkFont.h"
Mike Reed0ea09a42019-01-25 13:04:14 -050030#include "SkFontMetrics.h"
Mike Reeddad2f892018-11-05 16:13:54 -050031#include "SkFontTypes.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032#include "SkMaskFilter.h"
Derek Sollenbergereba81d02015-10-26 10:22:37 -040033#include "SkPath.h"
Mike Reed260ab722016-10-07 15:59:20 -040034#include "SkPathEffect.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include "SkShader.h"
Mike Reedc2f31df2016-10-28 17:21:45 -040036#include "SkBlendMode.h"
Billy Hewlettac1cbaf2012-07-18 09:51:45 -070037#include "unicode/uloc.h"
Doug Felt0c702b82010-05-14 10:55:42 -070038#include "unicode/ushape.h"
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -040039#include "utils/Blur.h"
Doug Felt0c702b82010-05-14 10:55:42 -070040
Mike Reed0f9dce72021-02-12 21:20:33 -050041#include <hwui/BlurDrawLooper.h>
sergeyvdccca442016-03-21 15:38:21 -070042#include <hwui/MinikinSkia.h>
43#include <hwui/MinikinUtils.h>
44#include <hwui/Paint.h>
sergeyvbad99182016-03-17 11:24:22 -070045#include <hwui/Typeface.h>
Raph Leviene368b6b2014-06-15 17:37:57 -070046#include <minikin/GraphemeBreak.h>
Seigo Nonaka20866c12017-10-26 16:02:01 -070047#include <minikin/LocaleList.h>
Raph Leviena027ec52015-04-06 16:21:59 -070048#include <minikin/Measurement.h>
Seigo Nonaka0ca492f2018-05-25 14:52:22 -070049#include <minikin/MinikinPaint.h>
Seigo Nonakabb1a9662015-10-21 23:24:25 +090050#include <unicode/utf16.h>
Raph Levien1a73f7322014-01-30 16:06:28 -080051
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -070052#include <cassert>
53#include <cstring>
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -070054#include <memory>
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -070055#include <vector>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57namespace android {
58
Mike Reedf6d86ac2019-01-18 14:13:23 -050059static void getPosTextPath(const SkFont& font, const uint16_t glyphs[], int count,
60 const SkPoint pos[], SkPath* dst) {
Mike Reedbacaa1d2019-01-29 08:07:50 -050061 dst->reset();
Mike Reedf6d86ac2019-01-18 14:13:23 -050062 struct Rec {
63 SkPath* fDst;
64 const SkPoint* fPos;
65 } rec = { dst, pos };
66 font.getPaths(glyphs, count, [](const SkPath* src, const SkMatrix& mx, void* ctx) {
67 Rec* rec = (Rec*)ctx;
68 if (src) {
69 SkMatrix tmp(mx);
70 tmp.postTranslate(rec->fPos->fX, rec->fPos->fY);
71 rec->fDst->addPath(*src, tmp);
72 }
73 rec->fPos += 1;
74 }, &rec);
Mike Reed3d63e012009-07-27 09:50:31 -040075}
76
John Reckdbffd252015-10-01 14:46:12 -070077namespace PaintGlue {
Doug Felt0c702b82010-05-14 10:55:42 -070078 enum MoveOpt {
79 AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT
80 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
Richard Uhler775873a2015-12-29 12:37:39 -080082 static void deletePaint(Paint* paint) {
83 delete paint;
84 }
85
86 static jlong getNativeFinalizer(JNIEnv*, jobject) {
87 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&deletePaint));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 }
89
John Reckdbffd252015-10-01 14:46:12 -070090 static jlong init(JNIEnv* env, jobject) {
Mike Reedf6d86ac2019-01-18 14:13:23 -050091 return reinterpret_cast<jlong>(new Paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 }
93
Ashok Bhat36bef0b2014-01-20 20:08:01 +000094 static jlong initWithPaint(JNIEnv* env, jobject clazz, jlong paintHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040095 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
96 Paint* obj = new Paint(*paint);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000097 return reinterpret_cast<jlong>(obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 }
Elliott Hughes8451b252011-04-07 19:17:57 -070099
Seigo Nonaka318ca042017-08-01 16:36:18 -0700100 static int breakText(JNIEnv* env, const Paint& paint, const Typeface* typeface,
101 const jchar text[], int count, float maxWidth, jint bidiFlags, jfloatArray jmeasured,
John Reckf2285972016-09-30 14:10:05 -0700102 const bool forwardScan) {
103 size_t measuredCount = 0;
104 float measured = 0;
Elliott Hughes8451b252011-04-07 19:17:57 -0700105
John Reckf2285972016-09-30 14:10:05 -0700106 std::unique_ptr<float[]> advancesArray(new float[count]);
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700107 MinikinUtils::measureText(&paint, static_cast<minikin::Bidi>(bidiFlags), typeface, text,
108 0, count, count, advancesArray.get());
Elliott Hughes8451b252011-04-07 19:17:57 -0700109
John Reckf2285972016-09-30 14:10:05 -0700110 for (int i = 0; i < count; i++) {
111 // traverse in the given direction
112 int index = forwardScan ? i : (count - i - 1);
113 float width = advancesArray[index];
114 if (measured + width > maxWidth) {
115 break;
116 }
117 // properly handle clusters when scanning backwards
118 if (forwardScan || width != 0.0f) {
119 measuredCount = i + 1;
120 }
121 measured += width;
Mike Reed4c9355c2014-05-07 11:48:37 -0400122 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700123
John Reckf2285972016-09-30 14:10:05 -0700124 if (jmeasured && env->GetArrayLength(jmeasured) > 0) {
125 AutoJavaFloatArray autoMeasured(env, jmeasured, 1);
126 jfloat* array = autoMeasured.ptr();
127 array[0] = measured;
Behdad Esfahbod805f6eb2014-07-29 18:43:03 -0400128 }
John Reckf2285972016-09-30 14:10:05 -0700129 return measuredCount;
Behdad Esfahbod805f6eb2014-07-29 18:43:03 -0400130 }
131
Seigo Nonaka318ca042017-08-01 16:36:18 -0700132 static jint breakTextC(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray jtext,
133 jint index, jint count, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) {
John Reckf2285972016-09-30 14:10:05 -0700134 NPE_CHECK_RETURN_ZERO(env, jtext);
Raph Levien210a1892015-03-09 14:42:14 -0700135
John Reckdbffd252015-10-01 14:46:12 -0700136 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700137 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700138
139 bool forwardTextDirection;
140 if (count < 0) {
141 forwardTextDirection = false;
142 count = -count;
Raph Levien53c00772014-04-14 14:11:02 -0700143 }
John Reckf2285972016-09-30 14:10:05 -0700144 else {
145 forwardTextDirection = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
John Reckf2285972016-09-30 14:10:05 -0700147
148 if ((index < 0) || (index + count > env->GetArrayLength(jtext))) {
149 doThrowAIOOBE(env);
150 return 0;
151 }
152
153 const jchar* text = env->GetCharArrayElements(jtext, nullptr);
154 count = breakText(env, *paint, typeface, text + index, count, maxWidth,
155 bidiFlags, jmeasuredWidth, forwardTextDirection);
156 env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text),
157 JNI_ABORT);
158 return count;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700160
Seigo Nonaka318ca042017-08-01 16:36:18 -0700161 static jint breakTextS(JNIEnv* env, jobject clazz, jlong paintHandle, jstring jtext,
162 jboolean forwards, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) {
John Reckf2285972016-09-30 14:10:05 -0700163 NPE_CHECK_RETURN_ZERO(env, jtext);
Elliott Hughes8451b252011-04-07 19:17:57 -0700164
John Reckf2285972016-09-30 14:10:05 -0700165 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700166 const Typeface* typeface = paint->getAndroidTypeface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
John Reckf2285972016-09-30 14:10:05 -0700168 int count = env->GetStringLength(jtext);
169 const jchar* text = env->GetStringChars(jtext, nullptr);
170 count = breakText(env, *paint, typeface, text, count, maxWidth, bidiFlags, jmeasuredWidth, forwards);
171 env->ReleaseStringChars(jtext, text);
172 return count;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 }
174
Seigo Nonaka318ca042017-08-01 16:36:18 -0700175 static jfloat doTextAdvances(JNIEnv *env, Paint *paint, const Typeface* typeface,
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700176 const jchar *text, jint start, jint count, jint contextCount, jint bidiFlags,
177 jfloatArray advances, jint advancesIndex) {
Fabrice Di Meglio6ab90ed2011-08-08 16:19:38 -0700178 NPE_CHECK_RETURN_ZERO(env, text);
179
180 if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) {
181 doThrowAIOOBE(env);
182 return 0;
183 }
184 if (count == 0) {
185 return 0;
186 }
187 if (advances) {
188 size_t advancesLength = env->GetArrayLength(advances);
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700189 if ((size_t)(count + advancesIndex) > advancesLength) {
Fabrice Di Meglio6ab90ed2011-08-08 16:19:38 -0700190 doThrowAIOOBE(env);
191 return 0;
192 }
193 }
Keisuke Kuroyanagia3024bd2015-09-16 20:02:15 -0700194 std::unique_ptr<jfloat[]> advancesArray;
195 if (advances) {
196 advancesArray.reset(new jfloat[count]);
197 }
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700198 const float advance = MinikinUtils::measureText(paint,
199 static_cast<minikin::Bidi>(bidiFlags), typeface, text, start, count, contextCount,
200 advancesArray.get());
Keisuke Kuroyanagia3024bd2015-09-16 20:02:15 -0700201 if (advances) {
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700202 env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray.get());
Doug Felt0c702b82010-05-14 10:55:42 -0700203 }
Keisuke Kuroyanagia3024bd2015-09-16 20:02:15 -0700204 return advance;
Doug Felt0c702b82010-05-14 10:55:42 -0700205 }
206
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700207 static jfloat getTextAdvances___CIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle,
Doug Felt0c702b82010-05-14 10:55:42 -0700208 jcharArray text, jint index, jint count, jint contextIndex, jint contextCount,
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700209 jint bidiFlags, jfloatArray advances, jint advancesIndex) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400210 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700211 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700212 jchar* textArray = env->GetCharArrayElements(text, nullptr);
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700213 jfloat result = doTextAdvances(env, paint, typeface, textArray + contextIndex,
214 index - contextIndex, count, contextCount, bidiFlags, advances, advancesIndex);
Doug Felt0c702b82010-05-14 10:55:42 -0700215 env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
216 return result;
217 }
218
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700219 static jfloat getTextAdvances__StringIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle,
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700220 jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint bidiFlags,
Fabrice Di Meglio665f02c2013-03-20 14:56:05 -0700221 jfloatArray advances, jint advancesIndex) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400222 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700223 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700224 const jchar* textArray = env->GetStringChars(text, nullptr);
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700225 jfloat result = doTextAdvances(env, paint, typeface, textArray + contextStart,
226 start - contextStart, end - start, contextEnd - contextStart, bidiFlags,
Fabrice Di Meglio665f02c2013-03-20 14:56:05 -0700227 advances, advancesIndex);
Fabrice Di Meglioeee49c62011-03-24 17:21:23 -0700228 env->ReleaseStringChars(text, textArray);
229 return result;
230 }
231
Seigo Nonaka318ca042017-08-01 16:36:18 -0700232 static jint doTextRunCursor(JNIEnv *env, Paint* paint, const Typeface* typeface,
233 const jchar *text, jint start, jint count, jint dir, jint offset, jint opt) {
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900234 minikin::GraphemeBreak::MoveOpt moveOpt = minikin::GraphemeBreak::MoveOpt(opt);
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700235 minikin::Bidi bidiFlags = dir == 1 ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
Roozbeh Pournader5d74b1c2017-03-13 21:41:15 -0700236 std::unique_ptr<float[]> advancesArray(new float[count]);
237 MinikinUtils::measureText(paint, bidiFlags, typeface, text, start, count, start + count,
238 advancesArray.get());
239 size_t result = minikin::GraphemeBreak::getTextRunCursor(advancesArray.get(), text,
240 start, count, offset, moveOpt);
Raph Leviene368b6b2014-06-15 17:37:57 -0700241 return static_cast<jint>(result);
Doug Felt0c702b82010-05-14 10:55:42 -0700242 }
243
Seigo Nonaka318ca042017-08-01 16:36:18 -0700244 static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text,
245 jint contextStart, jint contextCount, jint dir, jint offset, jint cursorOpt) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400246 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700247 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700248 jchar* textArray = env->GetCharArrayElements(text, nullptr);
Roozbeh Pournader5d74b1c2017-03-13 21:41:15 -0700249 jint result = doTextRunCursor(env, paint, typeface, textArray,
250 contextStart, contextCount, dir, offset, cursorOpt);
Doug Felt0c702b82010-05-14 10:55:42 -0700251 env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
252 return result;
253 }
254
Roozbeh Pournader5d74b1c2017-03-13 21:41:15 -0700255 static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, jlong paintHandle,
Seigo Nonaka318ca042017-08-01 16:36:18 -0700256 jstring text, jint contextStart, jint contextEnd, jint dir, jint offset,
257 jint cursorOpt) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400258 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700259 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700260 const jchar* textArray = env->GetStringChars(text, nullptr);
Roozbeh Pournader5d74b1c2017-03-13 21:41:15 -0700261 jint result = doTextRunCursor(env, paint, typeface, textArray,
262 contextStart, contextEnd - contextStart, dir, offset, cursorOpt);
Doug Felt0c702b82010-05-14 10:55:42 -0700263 env->ReleaseStringChars(text, textArray);
264 return result;
265 }
266
Raph Levienf2114d52014-06-01 15:54:47 -0700267 class GetTextFunctor {
268 public:
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900269 GetTextFunctor(const minikin::Layout& layout, SkPath* path, jfloat x, jfloat y,
270 Paint* paint, uint16_t* glyphs, SkPoint* pos)
Raph Levienf2114d52014-06-01 15:54:47 -0700271 : layout(layout), path(path), x(x), y(y), paint(paint), glyphs(glyphs), pos(pos) {
272 }
273
Raph Levien1fc0fa82014-06-06 18:05:22 -0700274 void operator()(size_t start, size_t end) {
Raph Levienf2114d52014-06-01 15:54:47 -0700275 for (size_t i = start; i < end; i++) {
276 glyphs[i] = layout.getGlyphId(i);
277 pos[i].fX = x + layout.getX(i);
278 pos[i].fY = y + layout.getY(i);
279 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500280 const SkFont& font = paint->getSkFont();
Raph Levienf2114d52014-06-01 15:54:47 -0700281 if (start == 0) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500282 getPosTextPath(font, glyphs, end, pos, path);
Raph Levienf2114d52014-06-01 15:54:47 -0700283 } else {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500284 getPosTextPath(font, glyphs + start, end - start, pos + start, &tmpPath);
Raph Levienf2114d52014-06-01 15:54:47 -0700285 path->addPath(tmpPath);
286 }
287 }
288 private:
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900289 const minikin::Layout& layout;
Raph Levienf2114d52014-06-01 15:54:47 -0700290 SkPath* path;
291 jfloat x;
292 jfloat y;
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400293 Paint* paint;
Raph Levienf2114d52014-06-01 15:54:47 -0700294 uint16_t* glyphs;
295 SkPoint* pos;
296 SkPath tmpPath;
297 };
Raph Levienf2114d52014-06-01 15:54:47 -0700298
Seigo Nonaka318ca042017-08-01 16:36:18 -0700299 static void getTextPath(JNIEnv* env, Paint* paint, const Typeface* typeface, const jchar* text,
Raph Levienf2114d52014-06-01 15:54:47 -0700300 jint count, jint bidiFlags, jfloat x, jfloat y, SkPath* path) {
Seigo Nonakac7064142017-02-10 16:53:31 +0900301 minikin::Layout layout = MinikinUtils::doLayout(
Seigo Nonaka3a4217f2018-05-02 12:56:16 -0700302 paint, static_cast<minikin::Bidi>(bidiFlags), typeface,
303 text, count, // text buffer
304 0, count, // draw range
305 0, count, // context range
Seigo Nonaka83143d02018-03-14 17:08:28 -0700306 nullptr);
Raph Levienf2114d52014-06-01 15:54:47 -0700307 size_t nGlyphs = layout.nGlyphs();
308 uint16_t* glyphs = new uint16_t[nGlyphs];
309 SkPoint* pos = new SkPoint[nGlyphs];
310
311 x += MinikinUtils::xOffsetForTextAlign(paint, layout);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400312 Paint::Align align = paint->getTextAlign();
313 paint->setTextAlign(Paint::kLeft_Align);
Raph Levienf2114d52014-06-01 15:54:47 -0700314 GetTextFunctor f(layout, path, x, y, paint, glyphs, pos);
Raph Levien1fc0fa82014-06-06 18:05:22 -0700315 MinikinUtils::forFontRun(layout, paint, f);
Raph Levienf2114d52014-06-01 15:54:47 -0700316 paint->setTextAlign(align);
317 delete[] glyphs;
318 delete[] pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 }
Doug Feltf7cb1f72010-07-01 16:20:43 -0700320
Seigo Nonaka318ca042017-08-01 16:36:18 -0700321 static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle, jint bidiFlags,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000322 jcharArray text, jint index, jint count, jfloat x, jfloat y, jlong pathHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400323 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700324 const Typeface* typeface = paint->getAndroidTypeface();
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000325 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
John Reckf2285972016-09-30 14:10:05 -0700326 const jchar* textArray = env->GetCharArrayElements(text, nullptr);
Raph Levienf2114d52014-06-01 15:54:47 -0700327 getTextPath(env, paint, typeface, textArray + index, count, bidiFlags, x, y, path);
Doug Feltf7cb1f72010-07-01 16:20:43 -0700328 env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
329 }
330
Seigo Nonaka318ca042017-08-01 16:36:18 -0700331 static void getTextPath__String(JNIEnv* env, jobject clazz, jlong paintHandle, jint bidiFlags,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000332 jstring text, jint start, jint end, jfloat x, jfloat y, jlong pathHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400333 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700334 const Typeface* typeface = paint->getAndroidTypeface();
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000335 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
John Reckf2285972016-09-30 14:10:05 -0700336 const jchar* textArray = env->GetStringChars(text, nullptr);
Raph Levienf2114d52014-06-01 15:54:47 -0700337 getTextPath(env, paint, typeface, textArray + start, end - start, bidiFlags, x, y, path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 env->ReleaseStringChars(text, textArray);
339 }
Doug Feltf7cb1f72010-07-01 16:20:43 -0700340
Raph Levien854363e2014-06-03 19:56:05 -0700341 static void doTextBounds(JNIEnv* env, const jchar* text, int count, jobject bounds,
Seigo Nonaka96860de2020-09-22 23:40:18 -0700342 const Paint& paint, const Typeface* typeface, jint bidiFlagsInt) {
Romain Guy059e12c2012-11-28 17:35:51 -0800343 SkRect r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 SkIRect ir;
Elliott Hughes8451b252011-04-07 19:17:57 -0700345
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900346 minikin::MinikinRect rect;
Seigo Nonaka96860de2020-09-22 23:40:18 -0700347 minikin::Bidi bidiFlags = static_cast<minikin::Bidi>(bidiFlagsInt);
348 MinikinUtils::getBounds(&paint, bidiFlags, typeface, text, count, &rect);
Raph Levien854363e2014-06-03 19:56:05 -0700349 r.fLeft = rect.mLeft;
350 r.fTop = rect.mTop;
351 r.fRight = rect.mRight;
352 r.fBottom = rect.mBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 r.roundOut(&ir);
354 GraphicsJNI::irect_to_jrect(ir, env, bounds);
355 }
356
Seigo Nonaka318ca042017-08-01 16:36:18 -0700357 static void getStringBounds(JNIEnv* env, jobject, jlong paintHandle, jstring text, jint start,
358 jint end, jint bidiFlags, jobject bounds) {
John Reckdbffd252015-10-01 14:46:12 -0700359 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700360 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700361 const jchar* textArray = env->GetStringChars(text, nullptr);
Raph Levien854363e2014-06-03 19:56:05 -0700362 doTextBounds(env, textArray + start, end - start, bounds, *paint, typeface, bidiFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 env->ReleaseStringChars(text, textArray);
364 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700365
Seigo Nonaka318ca042017-08-01 16:36:18 -0700366 static void getCharArrayBounds(JNIEnv* env, jobject, jlong paintHandle, jcharArray text,
367 jint index, jint count, jint bidiFlags, jobject bounds) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400368 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700369 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700370 const jchar* textArray = env->GetCharArrayElements(text, nullptr);
Raph Levien854363e2014-06-03 19:56:05 -0700371 doTextBounds(env, textArray + index, count, bounds, *paint, typeface, bidiFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray),
373 JNI_ABORT);
374 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700375
John Reckf2285972016-09-30 14:10:05 -0700376 // Returns true if the given string is exact one pair of regional indicators.
377 static bool isFlag(const jchar* str, size_t length) {
378 const jchar RI_LEAD_SURROGATE = 0xD83C;
379 const jchar RI_TRAIL_SURROGATE_MIN = 0xDDE6;
380 const jchar RI_TRAIL_SURROGATE_MAX = 0xDDFF;
381
382 if (length != 4) {
383 return false;
384 }
385 if (str[0] != RI_LEAD_SURROGATE || str[2] != RI_LEAD_SURROGATE) {
386 return false;
387 }
388 return RI_TRAIL_SURROGATE_MIN <= str[1] && str[1] <= RI_TRAIL_SURROGATE_MAX &&
389 RI_TRAIL_SURROGATE_MIN <= str[3] && str[3] <= RI_TRAIL_SURROGATE_MAX;
390 }
391
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900392 static jboolean layoutContainsNotdef(const minikin::Layout& layout) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700393 for (size_t i = 0; i < layout.nGlyphs(); i++) {
394 if (layout.getGlyphId(i) == 0) {
395 return true;
396 }
397 }
398 return false;
399 }
400
Raph Levien6141db32016-07-11 13:59:58 -0700401 // Don't count glyphs that are the recommended "space" glyph and are zero width.
402 // This logic makes assumptions about HarfBuzz layout, but does correctly handle
403 // cases where ligatures form and zero width space glyphs are left in as
404 // placeholders.
405 static size_t countNonSpaceGlyphs(const minikin::Layout& layout) {
406 size_t count = 0;
407 static unsigned int kSpaceGlyphId = 3;
408 for (size_t i = 0; i < layout.nGlyphs(); i++) {
409 if (layout.getGlyphId(i) != kSpaceGlyphId || layout.getCharAdvance(i) != 0.0) {
410 count++;
411 }
412 }
413 return count;
414 }
415
Seigo Nonaka318ca042017-08-01 16:36:18 -0700416 static jboolean hasGlyph(JNIEnv *env, jclass, jlong paintHandle, jint bidiFlags,
417 jstring string) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700418 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700419 const Typeface* typeface = paint->getAndroidTypeface();
Raph Levienf7f969e62015-04-01 14:41:21 -0700420 ScopedStringChars str(env, string);
421
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900422 /* Start by rejecting unsupported base code point and variation selector pairs. */
Raph Levienf7f969e62015-04-01 14:41:21 -0700423 size_t nChars = 0;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900424 const uint32_t kStartOfString = 0xFFFFFFFF;
425 uint32_t prevCp = kStartOfString;
Raph Levienf7f969e62015-04-01 14:41:21 -0700426 for (size_t i = 0; i < str.size(); i++) {
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900427 jchar cu = str[i];
428 uint32_t cp = cu;
429 if (U16_IS_TRAIL(cu)) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700430 // invalid UTF-16, unpaired trailing surrogate
431 return false;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900432 } else if (U16_IS_LEAD(cu)) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700433 if (i + 1 == str.size()) {
434 // invalid UTF-16, unpaired leading surrogate at end of string
435 return false;
436 }
437 i++;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900438 jchar cu2 = str[i];
439 if (!U16_IS_TRAIL(cu2)) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700440 // invalid UTF-16, unpaired leading surrogate
441 return false;
442 }
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900443 cp = U16_GET_SUPPLEMENTARY(cu, cu2);
444 }
445
446 if (prevCp != kStartOfString &&
Raph Levien032a3592016-04-14 21:22:37 -0700447 ((0xFE00 <= cp && cp <= 0xFE0F) || (0xE0100 <= cp && cp <= 0xE01EF))) {
448 bool hasVS = MinikinUtils::hasVariationSelector(typeface, prevCp, cp);
449 if (!hasVS) {
450 // No font has a glyph for the code point and variation selector pair.
451 return false;
452 } else if (nChars == 1 && i + 1 == str.size()) {
453 // The string is just a codepoint and a VS, we have an authoritative answer
454 return true;
455 }
Raph Levienf7f969e62015-04-01 14:41:21 -0700456 }
457 nChars++;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900458 prevCp = cp;
Raph Levienf7f969e62015-04-01 14:41:21 -0700459 }
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700460 minikin::Layout layout = MinikinUtils::doLayout(paint,
Seigo Nonaka3a4217f2018-05-02 12:56:16 -0700461 static_cast<minikin::Bidi>(bidiFlags), typeface,
462 str.get(), str.size(), // text buffer
463 0, str.size(), // draw range
464 0, str.size(), // context range
465 nullptr);
Raph Levien6141db32016-07-11 13:59:58 -0700466 size_t nGlyphs = countNonSpaceGlyphs(layout);
Raph Levienf7f969e62015-04-01 14:41:21 -0700467 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 Nonaka589cddc2016-04-05 14:43:14 +0900473
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 Nonaka7c93e862017-10-25 16:34:48 -0700484 minikin::Layout zzLayout = MinikinUtils::doLayout(paint,
Seigo Nonaka3a4217f2018-05-02 12:56:16 -0700485 static_cast<minikin::Bidi>(bidiFlags), typeface,
486 ZZ_FLAG_STR, 4, // text buffer
487 0, 4, // draw range
488 0, 4, // context range
Seigo Nonaka83143d02018-03-14 17:08:28 -0700489 nullptr);
Seigo Nonaka589cddc2016-04-05 14:43:14 +0900490 if (zzLayout.nGlyphs() != 1 || layoutContainsNotdef(zzLayout)) {
491 // The font collection doesn't have a glyph for unknown flag. Just return true.
492 return true;
493 }
494 return zzLayout.getGlyphId(0) != layout.getGlyphId(0);
495 }
496 return true;
Raph Levienf7f969e62015-04-01 14:41:21 -0700497 }
498
Seigo Nonaka318ca042017-08-01 16:36:18 -0700499 static jfloat doRunAdvance(const Paint* paint, const Typeface* typeface, const jchar buf[],
Raph Leviena027ec52015-04-06 16:21:59 -0700500 jint start, jint count, jint bufSize, jboolean isRtl, jint offset) {
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700501 minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
Keisuke Kuroyanagib3677712016-03-28 19:16:30 +0900502 if (offset == start + count) {
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800503 return MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count,
504 bufSize, nullptr);
505 }
506 std::unique_ptr<float[]> advancesArray(new float[count]);
507 MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize,
508 advancesArray.get());
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900509 return minikin::getRunAdvance(advancesArray.get(), buf, start, count, offset);
Raph Leviena027ec52015-04-06 16:21:59 -0700510 }
511
Seigo Nonaka318ca042017-08-01 16:36:18 -0700512 static jfloat getRunAdvance___CIIIIZI_F(JNIEnv *env, jclass, jlong paintHandle, jcharArray text,
513 jint start, jint end, jint contextStart, jint contextEnd, jboolean isRtl, jint offset) {
Raph Leviena027ec52015-04-06 16:21:59 -0700514 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700515 const Typeface* typeface = paint->getAndroidTypeface();
Seigo Nonakaee23f612018-01-27 15:08:25 -0800516 ScopedCharArrayRO textArray(env, text);
517 jfloat result = doRunAdvance(paint, typeface, textArray.get() + contextStart,
Raph Levien36ff86c2015-06-03 10:58:33 -0700518 start - contextStart, end - start, contextEnd - contextStart, isRtl,
519 offset - contextStart);
Raph Leviena027ec52015-04-06 16:21:59 -0700520 return result;
521 }
522
Seigo Nonaka318ca042017-08-01 16:36:18 -0700523 static jint doOffsetForAdvance(const Paint* paint, const Typeface* typeface, const jchar buf[],
Raph Leviena027ec52015-04-06 16:21:59 -0700524 jint start, jint count, jint bufSize, jboolean isRtl, jfloat advance) {
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700525 minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800526 std::unique_ptr<float[]> advancesArray(new float[count]);
527 MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize,
528 advancesArray.get());
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900529 return minikin::getOffsetForAdvance(advancesArray.get(), buf, start, count, advance);
Raph Leviena027ec52015-04-06 16:21:59 -0700530 }
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800531
Raph Leviena027ec52015-04-06 16:21:59 -0700532 static jint getOffsetForAdvance___CIIIIZF_I(JNIEnv *env, jclass, jlong paintHandle,
Seigo Nonaka318ca042017-08-01 16:36:18 -0700533 jcharArray text, jint start, jint end, jint contextStart, jint contextEnd,
534 jboolean isRtl, jfloat advance) {
Raph Leviena027ec52015-04-06 16:21:59 -0700535 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700536 const Typeface* typeface = paint->getAndroidTypeface();
Seigo Nonakaee23f612018-01-27 15:08:25 -0800537 ScopedCharArrayRO textArray(env, text);
538 jint result = doOffsetForAdvance(paint, typeface, textArray.get() + contextStart,
Raph Leviena027ec52015-04-06 16:21:59 -0700539 start - contextStart, end - start, contextEnd - contextStart, isRtl, advance);
540 result += contextStart;
Raph Leviena027ec52015-04-06 16:21:59 -0700541 return result;
542 }
543
Shubham Dubeyda511942022-01-10 05:17:52 +0000544 // ------------------ @FastNative ---------------------------
545
546 static jint setTextLocales(JNIEnv* env, jobject clazz, jlong objHandle, jstring locales) {
547 Paint* obj = reinterpret_cast<Paint*>(objHandle);
548 ScopedUtfChars localesChars(env, locales);
549 jint minikinLocaleListId = minikin::registerLocaleList(localesChars.c_str());
550 obj->setMinikinLocaleListId(minikinLocaleListId);
551 return minikinLocaleListId;
552 }
553
554 static void setFontFeatureSettings(JNIEnv* env, jobject clazz, jlong paintHandle, jstring settings) {
555 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
556 if (!settings) {
557 paint->setFontFeatureSettings(std::string());
558 } else {
559 ScopedUtfChars settingsChars(env, settings);
560 paint->setFontFeatureSettings(std::string(settingsChars.c_str(), settingsChars.size()));
561 }
562 }
563
Mike Reedda3488a2018-12-14 12:08:55 -0500564 static SkScalar getMetricsInternal(jlong paintHandle, SkFontMetrics *metrics) {
John Reckf2285972016-09-30 14:10:05 -0700565 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);
Mike Reedf6d86ac2019-01-18 14:13:23 -0500571 SkFont* font = &paint->getSkFont();
Seigo Nonaka318ca042017-08-01 16:36:18 -0700572 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700573 typeface = Typeface::resolveDefault(typeface);
574 minikin::FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle);
Mike Reedf6d86ac2019-01-18 14:13:23 -0500575 float saveSkewX = font->getSkewX();
576 bool savefakeBold = font->isEmbolden();
577 MinikinFontSkia::populateSkFont(font, baseFont.font->typeface().get(), baseFont.fakery);
578 SkScalar spacing = font->getMetrics(metrics);
John Reckf2285972016-09-30 14:10:05 -0700579 // The populateSkPaint call may have changed fake bold / text skew
580 // because we want to measure with those effects applied, so now
581 // restore the original settings.
Mike Reedf6d86ac2019-01-18 14:13:23 -0500582 font->setSkewX(saveSkewX);
583 font->setEmbolden(savefakeBold);
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700584 if (paint->getFamilyVariant() == minikin::FamilyVariant::ELEGANT) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500585 SkScalar size = font->getSize();
John Reckf2285972016-09-30 14:10:05 -0700586 metrics->fTop = -size * kElegantTop / 2048;
587 metrics->fBottom = -size * kElegantBottom / 2048;
588 metrics->fAscent = -size * kElegantAscent / 2048;
589 metrics->fDescent = -size * kElegantDescent / 2048;
590 metrics->fLeading = size * kElegantLeading / 2048;
591 spacing = metrics->fDescent - metrics->fAscent + metrics->fLeading;
592 }
593 return spacing;
594 }
595
Seigo Nonaka318ca042017-08-01 16:36:18 -0700596 static jfloat getFontMetrics(JNIEnv* env, jobject, jlong paintHandle, jobject metricsObj) {
Mike Reedda3488a2018-12-14 12:08:55 -0500597 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700598 SkScalar spacing = getMetricsInternal(paintHandle, &metrics);
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700599 GraphicsJNI::set_metrics(env, metricsObj, metrics);
John Reckf2285972016-09-30 14:10:05 -0700600 return SkScalarToFloat(spacing);
601 }
602
Seigo Nonaka318ca042017-08-01 16:36:18 -0700603 static jint getFontMetricsInt(JNIEnv* env, jobject, jlong paintHandle, jobject metricsObj) {
Mike Reedda3488a2018-12-14 12:08:55 -0500604 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700605 getMetricsInternal(paintHandle, &metrics);
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700606 return GraphicsJNI::set_metrics_int(env, metricsObj, metrics);
John Reckf2285972016-09-30 14:10:05 -0700607 }
608
609
610 // ------------------ @CriticalNative ---------------------------
611
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100612 static void reset(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500613 reinterpret_cast<Paint*>(objHandle)->reset();
John Reckf2285972016-09-30 14:10:05 -0700614 }
615
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100616 static void assign(CRITICAL_JNI_PARAMS_COMMA jlong dstPaintHandle, jlong srcPaintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700617 Paint* dst = reinterpret_cast<Paint*>(dstPaintHandle);
618 const Paint* src = reinterpret_cast<Paint*>(srcPaintHandle);
619 *dst = *src;
620 }
621
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100622 static jint getFlags(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500623 uint32_t flags = reinterpret_cast<Paint*>(paintHandle)->getJavaFlags();
624 return static_cast<jint>(flags);
John Reckf2285972016-09-30 14:10:05 -0700625 }
626
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100627 static void setFlags(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint flags) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500628 reinterpret_cast<Paint*>(paintHandle)->setJavaFlags(flags);
John Reckf2285972016-09-30 14:10:05 -0700629 }
630
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100631 static jint getHinting(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reed03acdb12019-01-26 10:17:31 -0500632 return (SkFontHinting)reinterpret_cast<Paint*>(paintHandle)->getSkFont().getHinting()
Ben Wagner0ab44392019-05-09 18:44:34 -0400633 == SkFontHinting::kNone ? 0 : 1;
John Reckf2285972016-09-30 14:10:05 -0700634 }
635
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100636 static void setHinting(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint mode) {
Mike Reed03acdb12019-01-26 10:17:31 -0500637 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setHinting(
Ben Wagner0ab44392019-05-09 18:44:34 -0400638 mode == 0 ? SkFontHinting::kNone : SkFontHinting::kNormal);
John Reckf2285972016-09-30 14:10:05 -0700639 }
640
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100641 static void setAntiAlias(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean aa) {
John Reckf2285972016-09-30 14:10:05 -0700642 reinterpret_cast<Paint*>(paintHandle)->setAntiAlias(aa);
643 }
644
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100645 static void setLinearText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean linearText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500646 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setLinearMetrics(linearText);
John Reckf2285972016-09-30 14:10:05 -0700647 }
648
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100649 static void setSubpixelText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean subpixelText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500650 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSubpixel(subpixelText);
John Reckf2285972016-09-30 14:10:05 -0700651 }
652
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100653 static void setUnderlineText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean underlineText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500654 reinterpret_cast<Paint*>(paintHandle)->setUnderline(underlineText);
John Reckf2285972016-09-30 14:10:05 -0700655 }
656
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100657 static void setStrikeThruText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean strikeThruText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500658 reinterpret_cast<Paint*>(paintHandle)->setStrikeThru(strikeThruText);
John Reckf2285972016-09-30 14:10:05 -0700659 }
660
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100661 static void setFakeBoldText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean fakeBoldText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500662 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setEmbolden(fakeBoldText);
John Reckf2285972016-09-30 14:10:05 -0700663 }
664
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100665 static void setFilterBitmap(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean filterBitmap) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400666 reinterpret_cast<Paint*>(paintHandle)->setFilterBitmap(filterBitmap);
John Reckf2285972016-09-30 14:10:05 -0700667 }
668
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100669 static void setDither(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean dither) {
John Reckf2285972016-09-30 14:10:05 -0700670 reinterpret_cast<Paint*>(paintHandle)->setDither(dither);
671 }
672
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100673 static jint getStyle(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700674 Paint* obj = reinterpret_cast<Paint*>(objHandle);
675 return static_cast<jint>(obj->getStyle());
676 }
677
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100678 static void setStyle(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint styleHandle) {
John Reckf2285972016-09-30 14:10:05 -0700679 Paint* obj = reinterpret_cast<Paint*>(objHandle);
680 Paint::Style style = static_cast<Paint::Style>(styleHandle);
681 obj->setStyle(style);
682 }
683
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100684 static void setColorLong(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jlong colorSpaceHandle,
Leon Scroggins III10965f32019-03-12 11:13:27 -0400685 jlong colorLong) {
686 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500687 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500688 reinterpret_cast<Paint*>(paintHandle)->setColor4f(color, cs.get());
689 }
690
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100691 static void setColor(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint color) {
Leon Scroggins III03b3e2362019-03-12 10:15:46 -0400692 reinterpret_cast<Paint*>(paintHandle)->setColor(color);
693 }
694
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100695 static void setAlpha(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint a) {
John Reckf2285972016-09-30 14:10:05 -0700696 reinterpret_cast<Paint*>(paintHandle)->setAlpha(a);
697 }
698
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100699 static jfloat getStrokeWidth(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700700 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeWidth());
701 }
702
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100703 static void setStrokeWidth(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat width) {
John Reckf2285972016-09-30 14:10:05 -0700704 reinterpret_cast<Paint*>(paintHandle)->setStrokeWidth(width);
705 }
706
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100707 static jfloat getStrokeMiter(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700708 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeMiter());
709 }
710
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100711 static void setStrokeMiter(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat miter) {
John Reckf2285972016-09-30 14:10:05 -0700712 reinterpret_cast<Paint*>(paintHandle)->setStrokeMiter(miter);
713 }
714
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100715 static jint getStrokeCap(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700716 Paint* obj = reinterpret_cast<Paint*>(objHandle);
717 return static_cast<jint>(obj->getStrokeCap());
718 }
719
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100720 static void setStrokeCap(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint capHandle) {
John Reckf2285972016-09-30 14:10:05 -0700721 Paint* obj = reinterpret_cast<Paint*>(objHandle);
722 Paint::Cap cap = static_cast<Paint::Cap>(capHandle);
723 obj->setStrokeCap(cap);
724 }
725
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100726 static jint getStrokeJoin(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700727 Paint* obj = reinterpret_cast<Paint*>(objHandle);
728 return static_cast<jint>(obj->getStrokeJoin());
729 }
730
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100731 static void setStrokeJoin(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint joinHandle) {
John Reckf2285972016-09-30 14:10:05 -0700732 Paint* obj = reinterpret_cast<Paint*>(objHandle);
733 Paint::Join join = (Paint::Join) joinHandle;
734 obj->setStrokeJoin(join);
735 }
736
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100737 static jboolean getFillPath(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong srcHandle, jlong dstHandle) {
John Reckf2285972016-09-30 14:10:05 -0700738 Paint* obj = reinterpret_cast<Paint*>(objHandle);
739 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
740 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
741 return obj->getFillPath(*src, dst) ? JNI_TRUE : JNI_FALSE;
742 }
743
Nader Jawad5bed1f52020-09-25 00:27:29 -0700744 static jlong setShader(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong shaderHandle) {
745 Paint* obj = reinterpret_cast<Paint*>(objHandle);
746 SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle);
747 obj->setShader(sk_ref_sp(shader));
748 return reinterpret_cast<jlong>(obj->getShader());
John Reckf2285972016-09-30 14:10:05 -0700749 }
750
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100751 static jlong setColorFilter(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong filterHandle) {
John Reckf2285972016-09-30 14:10:05 -0700752 Paint* obj = reinterpret_cast<Paint *>(objHandle);
753 SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(filterHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400754 obj->setColorFilter(sk_ref_sp(filter));
755 return reinterpret_cast<jlong>(obj->getColorFilter());
John Reckf2285972016-09-30 14:10:05 -0700756 }
757
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100758 static void setXfermode(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint xfermodeHandle) {
John Reckf2285972016-09-30 14:10:05 -0700759 // validate that the Java enum values match our expectations
Mike Reed260ab722016-10-07 15:59:20 -0400760 static_assert(0 == static_cast<int>(SkBlendMode::kClear), "xfermode_mismatch");
761 static_assert(1 == static_cast<int>(SkBlendMode::kSrc), "xfermode_mismatch");
762 static_assert(2 == static_cast<int>(SkBlendMode::kDst), "xfermode_mismatch");
763 static_assert(3 == static_cast<int>(SkBlendMode::kSrcOver), "xfermode_mismatch");
764 static_assert(4 == static_cast<int>(SkBlendMode::kDstOver), "xfermode_mismatch");
765 static_assert(5 == static_cast<int>(SkBlendMode::kSrcIn), "xfermode_mismatch");
766 static_assert(6 == static_cast<int>(SkBlendMode::kDstIn), "xfermode_mismatch");
767 static_assert(7 == static_cast<int>(SkBlendMode::kSrcOut), "xfermode_mismatch");
768 static_assert(8 == static_cast<int>(SkBlendMode::kDstOut), "xfermode_mismatch");
769 static_assert(9 == static_cast<int>(SkBlendMode::kSrcATop), "xfermode_mismatch");
770 static_assert(10 == static_cast<int>(SkBlendMode::kDstATop), "xfermode_mismatch");
771 static_assert(11 == static_cast<int>(SkBlendMode::kXor), "xfermode_mismatch");
Nader Jawad55e49d82018-11-16 11:22:32 -0800772 static_assert(12 == static_cast<int>(SkBlendMode::kPlus), "xfermode_mismatch");
Mike Reed260ab722016-10-07 15:59:20 -0400773 static_assert(13 == static_cast<int>(SkBlendMode::kModulate), "xfermode_mismatch");
774 static_assert(14 == static_cast<int>(SkBlendMode::kScreen), "xfermode_mismatch");
Mike Reed260ab722016-10-07 15:59:20 -0400775 static_assert(15 == static_cast<int>(SkBlendMode::kOverlay), "xfermode_mismatch");
Nader Jawad55e49d82018-11-16 11:22:32 -0800776 static_assert(16 == static_cast<int>(SkBlendMode::kDarken), "xfermode_mismatch");
777 static_assert(17 == static_cast<int>(SkBlendMode::kLighten), "xfermode_mismatch");
778 static_assert(18 == static_cast<int>(SkBlendMode::kColorDodge), "xfermode mismatch");
779 static_assert(19 == static_cast<int>(SkBlendMode::kColorBurn), "xfermode mismatch");
780 static_assert(20 == static_cast<int>(SkBlendMode::kHardLight), "xfermode mismatch");
781 static_assert(21 == static_cast<int>(SkBlendMode::kSoftLight), "xfermode mismatch");
782 static_assert(22 == static_cast<int>(SkBlendMode::kDifference), "xfermode mismatch");
783 static_assert(23 == static_cast<int>(SkBlendMode::kExclusion), "xfermode mismatch");
784 static_assert(24 == static_cast<int>(SkBlendMode::kMultiply), "xfermode mismatch");
785 static_assert(25 == static_cast<int>(SkBlendMode::kHue), "xfermode mismatch");
786 static_assert(26 == static_cast<int>(SkBlendMode::kSaturation), "xfermode mismatch");
787 static_assert(27 == static_cast<int>(SkBlendMode::kColor), "xfermode mismatch");
788 static_assert(28 == static_cast<int>(SkBlendMode::kLuminosity), "xfermode mismatch");
John Reckf2285972016-09-30 14:10:05 -0700789
Mike Reed260ab722016-10-07 15:59:20 -0400790 SkBlendMode mode = static_cast<SkBlendMode>(xfermodeHandle);
John Reckf2285972016-09-30 14:10:05 -0700791 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400792 paint->setBlendMode(mode);
John Reckf2285972016-09-30 14:10:05 -0700793 }
794
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100795 static jlong setPathEffect(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong effectHandle) {
John Reckf2285972016-09-30 14:10:05 -0700796 Paint* obj = reinterpret_cast<Paint*>(objHandle);
797 SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400798 obj->setPathEffect(sk_ref_sp(effect));
799 return reinterpret_cast<jlong>(obj->getPathEffect());
John Reckf2285972016-09-30 14:10:05 -0700800 }
801
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100802 static jlong setMaskFilter(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong maskfilterHandle) {
John Reckf2285972016-09-30 14:10:05 -0700803 Paint* obj = reinterpret_cast<Paint*>(objHandle);
804 SkMaskFilter* maskfilter = reinterpret_cast<SkMaskFilter*>(maskfilterHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400805 obj->setMaskFilter(sk_ref_sp(maskfilter));
806 return reinterpret_cast<jlong>(obj->getMaskFilter());
John Reckf2285972016-09-30 14:10:05 -0700807 }
808
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100809 static void setTypeface(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong typefaceHandle) {
Seigo Nonaka318ca042017-08-01 16:36:18 -0700810 Paint* paint = reinterpret_cast<Paint*>(objHandle);
811 paint->setAndroidTypeface(reinterpret_cast<Typeface*>(typefaceHandle));
John Reckf2285972016-09-30 14:10:05 -0700812 }
813
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100814 static jint getTextAlign(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700815 Paint* obj = reinterpret_cast<Paint*>(objHandle);
816 return static_cast<jint>(obj->getTextAlign());
817 }
818
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100819 static void setTextAlign(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint alignHandle) {
John Reckf2285972016-09-30 14:10:05 -0700820 Paint* obj = reinterpret_cast<Paint*>(objHandle);
821 Paint::Align align = static_cast<Paint::Align>(alignHandle);
822 obj->setTextAlign(align);
823 }
824
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100825 static void setTextLocalesByMinikinLocaleListId(CRITICAL_JNI_PARAMS_COMMA jlong objHandle,
Seigo Nonaka20866c12017-10-26 16:02:01 -0700826 jint minikinLocaleListId) {
John Reckf2285972016-09-30 14:10:05 -0700827 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Seigo Nonaka20866c12017-10-26 16:02:01 -0700828 obj->setMinikinLocaleListId(minikinLocaleListId);
John Reckf2285972016-09-30 14:10:05 -0700829 }
830
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100831 static jboolean isElegantTextHeight(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700832 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700833 return obj->getFamilyVariant() == minikin::FamilyVariant::ELEGANT;
John Reckf2285972016-09-30 14:10:05 -0700834 }
835
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100836 static void setElegantTextHeight(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean aa) {
John Reckf2285972016-09-30 14:10:05 -0700837 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakac52075e2017-11-17 15:40:06 -0800838 obj->setFamilyVariant(
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700839 aa ? minikin::FamilyVariant::ELEGANT : minikin::FamilyVariant::DEFAULT);
John Reckf2285972016-09-30 14:10:05 -0700840 }
841
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100842 static jfloat getTextSize(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500843 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize());
John Reckf2285972016-09-30 14:10:05 -0700844 }
845
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100846 static void setTextSize(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat textSize) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500847 if (textSize >= 0) {
848 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSize(textSize);
849 }
John Reckf2285972016-09-30 14:10:05 -0700850 }
851
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100852 static jfloat getTextScaleX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500853 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getScaleX());
John Reckf2285972016-09-30 14:10:05 -0700854 }
855
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100856 static void setTextScaleX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat scaleX) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500857 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setScaleX(scaleX);
John Reckf2285972016-09-30 14:10:05 -0700858 }
859
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100860 static jfloat getTextSkewX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500861 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSkewX());
John Reckf2285972016-09-30 14:10:05 -0700862 }
863
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100864 static void setTextSkewX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat skewX) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500865 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSkewX(skewX);
John Reckf2285972016-09-30 14:10:05 -0700866 }
867
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100868 static jfloat getLetterSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700869 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
870 return paint->getLetterSpacing();
871 }
872
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100873 static void setLetterSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat letterSpacing) {
John Reckf2285972016-09-30 14:10:05 -0700874 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
875 paint->setLetterSpacing(letterSpacing);
876 }
877
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100878 static jfloat getWordSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Seigo Nonaka219e2c792016-11-15 19:01:45 +0900879 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
880 return paint->getWordSpacing();
881 }
882
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100883 static void setWordSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat wordSpacing) {
Seigo Nonaka219e2c792016-11-15 19:01:45 +0900884 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
885 paint->setWordSpacing(wordSpacing);
886 }
887
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100888 static jint getStartHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
John Reckf2285972016-09-30 14:10:05 -0700889 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800890 return static_cast<jint>(paint->getStartHyphenEdit());
John Reckf2285972016-09-30 14:10:05 -0700891 }
892
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100893 static jint getEndHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
John Reckf2285972016-09-30 14:10:05 -0700894 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800895 return static_cast<jint>(paint->getEndHyphenEdit());
896 }
897
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100898 static void setStartHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800899 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
900 paint->setStartHyphenEdit((uint32_t)hyphen);
901 }
902
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100903 static void setEndHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800904 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
905 paint->setEndHyphenEdit((uint32_t)hyphen);
John Reckf2285972016-09-30 14:10:05 -0700906 }
907
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100908 static jfloat ascent(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500909 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700910 getMetricsInternal(paintHandle, &metrics);
John Reckf2285972016-09-30 14:10:05 -0700911 return SkScalarToFloat(metrics.fAscent);
912 }
913
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100914 static jfloat descent(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500915 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700916 getMetricsInternal(paintHandle, &metrics);
John Reckf2285972016-09-30 14:10:05 -0700917 return SkScalarToFloat(metrics.fDescent);
918 }
919
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100920 static jfloat getUnderlinePosition(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500921 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700922 getMetricsInternal(paintHandle, &metrics);
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700923 SkScalar position;
924 if (metrics.hasUnderlinePosition(&position)) {
925 return SkScalarToFloat(position);
926 } else {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500927 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700928 return SkScalarToFloat(Paint::kStdUnderline_Top * textSize);
929 }
930 }
931
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100932 static jfloat getUnderlineThickness(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500933 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700934 getMetricsInternal(paintHandle, &metrics);
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700935 SkScalar thickness;
936 if (metrics.hasUnderlineThickness(&thickness)) {
937 return SkScalarToFloat(thickness);
938 } else {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500939 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700940 return SkScalarToFloat(Paint::kStdUnderline_Thickness * textSize);
941 }
942 }
943
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100944 static jfloat getStrikeThruPosition(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500945 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournader1378a9d2017-07-13 12:45:20 -0700946 return SkScalarToFloat(Paint::kStdStrikeThru_Top * textSize);
947 }
948
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100949 static jfloat getStrikeThruThickness(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500950 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournader1378a9d2017-07-13 12:45:20 -0700951 return SkScalarToFloat(Paint::kStdStrikeThru_Thickness * textSize);
952 }
953
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100954 static void setShadowLayer(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat radius,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500955 jfloat dx, jfloat dy, jlong colorSpaceHandle,
Leon Scroggins III10965f32019-03-12 11:13:27 -0400956 jlong colorLong) {
957 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500958 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500959
960 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
961 if (radius <= 0) {
962 paint->setLooper(nullptr);
963 }
964 else {
965 SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius);
Mike Reed0f9dce72021-02-12 21:20:33 -0500966 paint->setLooper(BlurDrawLooper::Make(color, cs.get(), sigma, {dx, dy}));
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500967 }
968 }
969
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100970 static jboolean hasShadowLayer(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700971 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Mike Reed0f9dce72021-02-12 21:20:33 -0500972 return paint->getLooper() != nullptr;
John Reckf2285972016-09-30 14:10:05 -0700973 }
974
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100975 static jboolean equalsForTextMeasurement(CRITICAL_JNI_PARAMS_COMMA jlong lPaint, jlong rPaint) {
Seigo Nonakabeafa1f2018-02-01 21:39:24 -0800976 if (lPaint == rPaint) {
977 return true;
978 }
979 Paint* leftPaint = reinterpret_cast<Paint*>(lPaint);
980 Paint* rightPaint = reinterpret_cast<Paint*>(rPaint);
981
982 const Typeface* leftTypeface = Typeface::resolveDefault(leftPaint->getAndroidTypeface());
983 const Typeface* rightTypeface = Typeface::resolveDefault(rightPaint->getAndroidTypeface());
984 minikin::MinikinPaint leftMinikinPaint
985 = MinikinUtils::prepareMinikinPaint(leftPaint, leftTypeface);
986 minikin::MinikinPaint rightMinikinPaint
987 = MinikinUtils::prepareMinikinPaint(rightPaint, rightTypeface);
988
989 return leftMinikinPaint == rightMinikinPaint;
990 }
991
John Reckdbffd252015-10-01 14:46:12 -0700992}; // namespace PaintGlue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993
Daniel Micay76f6a862015-09-19 17:31:01 -0400994static const JNINativeMethod methods[] = {
Richard Uhler775873a2015-12-29 12:37:39 -0800995 {"nGetNativeFinalizer", "()J", (void*) PaintGlue::getNativeFinalizer},
John Reckdbffd252015-10-01 14:46:12 -0700996 {"nInit","()J", (void*) PaintGlue::init},
997 {"nInitWithPaint","(J)J", (void*) PaintGlue::initWithPaint},
Seigo Nonaka318ca042017-08-01 16:36:18 -0700998 {"nBreakText","(J[CIIFI[F)I", (void*) PaintGlue::breakTextC},
999 {"nBreakText","(JLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS},
1000 {"nGetTextAdvances","(J[CIIIII[FI)F",
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -07001001 (void*) PaintGlue::getTextAdvances___CIIIII_FI},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001002 {"nGetTextAdvances","(JLjava/lang/String;IIIII[FI)F",
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -07001003 (void*) PaintGlue::getTextAdvances__StringIIIII_FI},
Fabrice Di Meglioda12f382013-03-15 11:26:56 -07001004
Seigo Nonaka318ca042017-08-01 16:36:18 -07001005 {"nGetTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C},
1006 {"nGetTextRunCursor", "(JLjava/lang/String;IIIII)I",
Raph Leviena027ec52015-04-06 16:21:59 -07001007 (void*) PaintGlue::getTextRunCursor__String},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001008 {"nGetTextPath", "(JI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C},
1009 {"nGetTextPath", "(JILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String},
1010 {"nGetStringBounds", "(JLjava/lang/String;IIILandroid/graphics/Rect;)V",
Raph Leviena027ec52015-04-06 16:21:59 -07001011 (void*) PaintGlue::getStringBounds },
Seigo Nonaka318ca042017-08-01 16:36:18 -07001012 {"nGetCharArrayBounds", "(J[CIIILandroid/graphics/Rect;)V",
Raph Leviena027ec52015-04-06 16:21:59 -07001013 (void*) PaintGlue::getCharArrayBounds },
Seigo Nonaka318ca042017-08-01 16:36:18 -07001014 {"nHasGlyph", "(JILjava/lang/String;)Z", (void*) PaintGlue::hasGlyph },
1015 {"nGetRunAdvance", "(J[CIIIIZI)F", (void*) PaintGlue::getRunAdvance___CIIIIZI_F},
1016 {"nGetOffsetForAdvance", "(J[CIIIIZF)I",
Raph Leviena027ec52015-04-06 16:21:59 -07001017 (void*) PaintGlue::getOffsetForAdvance___CIIIIZF_I},
Chris Craik4136a0a2014-10-07 15:09:46 -07001018
John Reckf2285972016-09-30 14:10:05 -07001019 // --------------- @FastNative ----------------------
1020
1021 {"nSetTextLocales","(JLjava/lang/String;)I", (void*) PaintGlue::setTextLocales},
1022 {"nSetFontFeatureSettings","(JLjava/lang/String;)V",
1023 (void*) PaintGlue::setFontFeatureSettings},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001024 {"nGetFontMetrics", "(JLandroid/graphics/Paint$FontMetrics;)F",
John Reckf2285972016-09-30 14:10:05 -07001025 (void*)PaintGlue::getFontMetrics},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001026 {"nGetFontMetricsInt", "(JLandroid/graphics/Paint$FontMetricsInt;)I",
John Reckf2285972016-09-30 14:10:05 -07001027 (void*)PaintGlue::getFontMetricsInt},
1028
1029 // --------------- @CriticalNative ------------------
1030
1031 {"nReset","(J)V", (void*) PaintGlue::reset},
1032 {"nSet","(JJ)V", (void*) PaintGlue::assign},
1033 {"nGetFlags","(J)I", (void*) PaintGlue::getFlags},
1034 {"nSetFlags","(JI)V", (void*) PaintGlue::setFlags},
1035 {"nGetHinting","(J)I", (void*) PaintGlue::getHinting},
1036 {"nSetHinting","(JI)V", (void*) PaintGlue::setHinting},
1037 {"nSetAntiAlias","(JZ)V", (void*) PaintGlue::setAntiAlias},
1038 {"nSetSubpixelText","(JZ)V", (void*) PaintGlue::setSubpixelText},
1039 {"nSetLinearText","(JZ)V", (void*) PaintGlue::setLinearText},
1040 {"nSetUnderlineText","(JZ)V", (void*) PaintGlue::setUnderlineText},
1041 {"nSetStrikeThruText","(JZ)V", (void*) PaintGlue::setStrikeThruText},
1042 {"nSetFakeBoldText","(JZ)V", (void*) PaintGlue::setFakeBoldText},
1043 {"nSetFilterBitmap","(JZ)V", (void*) PaintGlue::setFilterBitmap},
1044 {"nSetDither","(JZ)V", (void*) PaintGlue::setDither},
1045 {"nGetStyle","(J)I", (void*) PaintGlue::getStyle},
1046 {"nSetStyle","(JI)V", (void*) PaintGlue::setStyle},
Leon Scroggins III03b3e2362019-03-12 10:15:46 -04001047 {"nSetColor","(JI)V", (void*) PaintGlue::setColor},
Leon Scroggins III10965f32019-03-12 11:13:27 -04001048 {"nSetColor","(JJJ)V", (void*) PaintGlue::setColorLong},
John Reckf2285972016-09-30 14:10:05 -07001049 {"nSetAlpha","(JI)V", (void*) PaintGlue::setAlpha},
1050 {"nGetStrokeWidth","(J)F", (void*) PaintGlue::getStrokeWidth},
1051 {"nSetStrokeWidth","(JF)V", (void*) PaintGlue::setStrokeWidth},
1052 {"nGetStrokeMiter","(J)F", (void*) PaintGlue::getStrokeMiter},
1053 {"nSetStrokeMiter","(JF)V", (void*) PaintGlue::setStrokeMiter},
1054 {"nGetStrokeCap","(J)I", (void*) PaintGlue::getStrokeCap},
1055 {"nSetStrokeCap","(JI)V", (void*) PaintGlue::setStrokeCap},
1056 {"nGetStrokeJoin","(J)I", (void*) PaintGlue::getStrokeJoin},
1057 {"nSetStrokeJoin","(JI)V", (void*) PaintGlue::setStrokeJoin},
1058 {"nGetFillPath","(JJJ)Z", (void*) PaintGlue::getFillPath},
Nader Jawad5bed1f52020-09-25 00:27:29 -07001059 {"nSetShader","(JJ)J", (void*) PaintGlue::setShader},
John Reckf2285972016-09-30 14:10:05 -07001060 {"nSetColorFilter","(JJ)J", (void*) PaintGlue::setColorFilter},
1061 {"nSetXfermode","(JI)V", (void*) PaintGlue::setXfermode},
1062 {"nSetPathEffect","(JJ)J", (void*) PaintGlue::setPathEffect},
1063 {"nSetMaskFilter","(JJ)J", (void*) PaintGlue::setMaskFilter},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001064 {"nSetTypeface","(JJ)V", (void*) PaintGlue::setTypeface},
John Reckf2285972016-09-30 14:10:05 -07001065 {"nGetTextAlign","(J)I", (void*) PaintGlue::getTextAlign},
1066 {"nSetTextAlign","(JI)V", (void*) PaintGlue::setTextAlign},
Seigo Nonaka20866c12017-10-26 16:02:01 -07001067 {"nSetTextLocalesByMinikinLocaleListId","(JI)V",
1068 (void*) PaintGlue::setTextLocalesByMinikinLocaleListId},
John Reckf2285972016-09-30 14:10:05 -07001069 {"nIsElegantTextHeight","(J)Z", (void*) PaintGlue::isElegantTextHeight},
1070 {"nSetElegantTextHeight","(JZ)V", (void*) PaintGlue::setElegantTextHeight},
1071 {"nGetTextSize","(J)F", (void*) PaintGlue::getTextSize},
1072 {"nSetTextSize","(JF)V", (void*) PaintGlue::setTextSize},
1073 {"nGetTextScaleX","(J)F", (void*) PaintGlue::getTextScaleX},
1074 {"nSetTextScaleX","(JF)V", (void*) PaintGlue::setTextScaleX},
1075 {"nGetTextSkewX","(J)F", (void*) PaintGlue::getTextSkewX},
1076 {"nSetTextSkewX","(JF)V", (void*) PaintGlue::setTextSkewX},
1077 {"nGetLetterSpacing","(J)F", (void*) PaintGlue::getLetterSpacing},
1078 {"nSetLetterSpacing","(JF)V", (void*) PaintGlue::setLetterSpacing},
Seigo Nonaka219e2c792016-11-15 19:01:45 +09001079 {"nGetWordSpacing","(J)F", (void*) PaintGlue::getWordSpacing},
1080 {"nSetWordSpacing","(JF)V", (void*) PaintGlue::setWordSpacing},
Seigo Nonakafb1b4792019-03-08 14:05:08 -08001081 {"nGetStartHyphenEdit", "(J)I", (void*) PaintGlue::getStartHyphenEdit},
1082 {"nGetEndHyphenEdit", "(J)I", (void*) PaintGlue::getEndHyphenEdit},
1083 {"nSetStartHyphenEdit", "(JI)V", (void*) PaintGlue::setStartHyphenEdit},
1084 {"nSetEndHyphenEdit", "(JI)V", (void*) PaintGlue::setEndHyphenEdit},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001085 {"nAscent","(J)F", (void*) PaintGlue::ascent},
1086 {"nDescent","(J)F", (void*) PaintGlue::descent},
1087 {"nGetUnderlinePosition","(J)F", (void*) PaintGlue::getUnderlinePosition},
1088 {"nGetUnderlineThickness","(J)F", (void*) PaintGlue::getUnderlineThickness},
1089 {"nGetStrikeThruPosition","(J)F", (void*) PaintGlue::getStrikeThruPosition},
1090 {"nGetStrikeThruThickness","(J)F", (void*) PaintGlue::getStrikeThruThickness},
Leon Scroggins III10965f32019-03-12 11:13:27 -04001091 {"nSetShadowLayer", "(JFFFJJ)V", (void*)PaintGlue::setShadowLayer},
Seigo Nonakabeafa1f2018-02-01 21:39:24 -08001092 {"nHasShadowLayer", "(J)Z", (void*)PaintGlue::hasShadowLayer},
1093 {"nEqualsForTextMeasurement", "(JJ)Z", (void*)PaintGlue::equalsForTextMeasurement},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094};
1095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096int register_android_graphics_Paint(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001097 return RegisterMethodsOrDie(env, "android/graphics/Paint", methods, NELEM(methods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098}
1099
1100}