blob: bcec0fa8a1cc04d0a73535d7a49d7c8b7a32f640 [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
John Reckf2285972016-09-30 14:10:05 -0700544 // ------------------ @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);
Seigo Nonaka20866c12017-10-26 16:02:01 -0700549 jint minikinLocaleListId = minikin::registerLocaleList(localesChars.c_str());
550 obj->setMinikinLocaleListId(minikinLocaleListId);
551 return minikinLocaleListId;
John Reckf2285972016-09-30 14:10:05 -0700552 }
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) {
John Reckf2285972016-09-30 14:10:05 -0700666 reinterpret_cast<Paint*>(paintHandle)->setFilterQuality(
667 filterBitmap ? kLow_SkFilterQuality : kNone_SkFilterQuality);
668 }
669
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100670 static void setDither(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean dither) {
John Reckf2285972016-09-30 14:10:05 -0700671 reinterpret_cast<Paint*>(paintHandle)->setDither(dither);
672 }
673
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100674 static jint getStyle(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700675 Paint* obj = reinterpret_cast<Paint*>(objHandle);
676 return static_cast<jint>(obj->getStyle());
677 }
678
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100679 static void setStyle(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint styleHandle) {
John Reckf2285972016-09-30 14:10:05 -0700680 Paint* obj = reinterpret_cast<Paint*>(objHandle);
681 Paint::Style style = static_cast<Paint::Style>(styleHandle);
682 obj->setStyle(style);
683 }
684
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100685 static void setColorLong(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jlong colorSpaceHandle,
Leon Scroggins III10965f32019-03-12 11:13:27 -0400686 jlong colorLong) {
687 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500688 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500689 reinterpret_cast<Paint*>(paintHandle)->setColor4f(color, cs.get());
690 }
691
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100692 static void setColor(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint color) {
Leon Scroggins III03b3e2362019-03-12 10:15:46 -0400693 reinterpret_cast<Paint*>(paintHandle)->setColor(color);
694 }
695
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100696 static void setAlpha(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint a) {
John Reckf2285972016-09-30 14:10:05 -0700697 reinterpret_cast<Paint*>(paintHandle)->setAlpha(a);
698 }
699
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100700 static jfloat getStrokeWidth(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700701 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeWidth());
702 }
703
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100704 static void setStrokeWidth(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat width) {
John Reckf2285972016-09-30 14:10:05 -0700705 reinterpret_cast<Paint*>(paintHandle)->setStrokeWidth(width);
706 }
707
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100708 static jfloat getStrokeMiter(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700709 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeMiter());
710 }
711
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100712 static void setStrokeMiter(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat miter) {
John Reckf2285972016-09-30 14:10:05 -0700713 reinterpret_cast<Paint*>(paintHandle)->setStrokeMiter(miter);
714 }
715
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100716 static jint getStrokeCap(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700717 Paint* obj = reinterpret_cast<Paint*>(objHandle);
718 return static_cast<jint>(obj->getStrokeCap());
719 }
720
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100721 static void setStrokeCap(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint capHandle) {
John Reckf2285972016-09-30 14:10:05 -0700722 Paint* obj = reinterpret_cast<Paint*>(objHandle);
723 Paint::Cap cap = static_cast<Paint::Cap>(capHandle);
724 obj->setStrokeCap(cap);
725 }
726
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100727 static jint getStrokeJoin(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700728 Paint* obj = reinterpret_cast<Paint*>(objHandle);
729 return static_cast<jint>(obj->getStrokeJoin());
730 }
731
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100732 static void setStrokeJoin(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint joinHandle) {
John Reckf2285972016-09-30 14:10:05 -0700733 Paint* obj = reinterpret_cast<Paint*>(objHandle);
734 Paint::Join join = (Paint::Join) joinHandle;
735 obj->setStrokeJoin(join);
736 }
737
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100738 static jboolean getFillPath(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong srcHandle, jlong dstHandle) {
John Reckf2285972016-09-30 14:10:05 -0700739 Paint* obj = reinterpret_cast<Paint*>(objHandle);
740 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
741 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
742 return obj->getFillPath(*src, dst) ? JNI_TRUE : JNI_FALSE;
743 }
744
Nader Jawad5bed1f52020-09-25 00:27:29 -0700745 static jlong setShader(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong shaderHandle) {
746 Paint* obj = reinterpret_cast<Paint*>(objHandle);
747 SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle);
748 obj->setShader(sk_ref_sp(shader));
749 return reinterpret_cast<jlong>(obj->getShader());
John Reckf2285972016-09-30 14:10:05 -0700750 }
751
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100752 static jlong setColorFilter(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong filterHandle) {
John Reckf2285972016-09-30 14:10:05 -0700753 Paint* obj = reinterpret_cast<Paint *>(objHandle);
754 SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(filterHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400755 obj->setColorFilter(sk_ref_sp(filter));
756 return reinterpret_cast<jlong>(obj->getColorFilter());
John Reckf2285972016-09-30 14:10:05 -0700757 }
758
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100759 static void setXfermode(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint xfermodeHandle) {
John Reckf2285972016-09-30 14:10:05 -0700760 // validate that the Java enum values match our expectations
Mike Reed260ab722016-10-07 15:59:20 -0400761 static_assert(0 == static_cast<int>(SkBlendMode::kClear), "xfermode_mismatch");
762 static_assert(1 == static_cast<int>(SkBlendMode::kSrc), "xfermode_mismatch");
763 static_assert(2 == static_cast<int>(SkBlendMode::kDst), "xfermode_mismatch");
764 static_assert(3 == static_cast<int>(SkBlendMode::kSrcOver), "xfermode_mismatch");
765 static_assert(4 == static_cast<int>(SkBlendMode::kDstOver), "xfermode_mismatch");
766 static_assert(5 == static_cast<int>(SkBlendMode::kSrcIn), "xfermode_mismatch");
767 static_assert(6 == static_cast<int>(SkBlendMode::kDstIn), "xfermode_mismatch");
768 static_assert(7 == static_cast<int>(SkBlendMode::kSrcOut), "xfermode_mismatch");
769 static_assert(8 == static_cast<int>(SkBlendMode::kDstOut), "xfermode_mismatch");
770 static_assert(9 == static_cast<int>(SkBlendMode::kSrcATop), "xfermode_mismatch");
771 static_assert(10 == static_cast<int>(SkBlendMode::kDstATop), "xfermode_mismatch");
772 static_assert(11 == static_cast<int>(SkBlendMode::kXor), "xfermode_mismatch");
Nader Jawad55e49d82018-11-16 11:22:32 -0800773 static_assert(12 == static_cast<int>(SkBlendMode::kPlus), "xfermode_mismatch");
Mike Reed260ab722016-10-07 15:59:20 -0400774 static_assert(13 == static_cast<int>(SkBlendMode::kModulate), "xfermode_mismatch");
775 static_assert(14 == static_cast<int>(SkBlendMode::kScreen), "xfermode_mismatch");
Mike Reed260ab722016-10-07 15:59:20 -0400776 static_assert(15 == static_cast<int>(SkBlendMode::kOverlay), "xfermode_mismatch");
Nader Jawad55e49d82018-11-16 11:22:32 -0800777 static_assert(16 == static_cast<int>(SkBlendMode::kDarken), "xfermode_mismatch");
778 static_assert(17 == static_cast<int>(SkBlendMode::kLighten), "xfermode_mismatch");
779 static_assert(18 == static_cast<int>(SkBlendMode::kColorDodge), "xfermode mismatch");
780 static_assert(19 == static_cast<int>(SkBlendMode::kColorBurn), "xfermode mismatch");
781 static_assert(20 == static_cast<int>(SkBlendMode::kHardLight), "xfermode mismatch");
782 static_assert(21 == static_cast<int>(SkBlendMode::kSoftLight), "xfermode mismatch");
783 static_assert(22 == static_cast<int>(SkBlendMode::kDifference), "xfermode mismatch");
784 static_assert(23 == static_cast<int>(SkBlendMode::kExclusion), "xfermode mismatch");
785 static_assert(24 == static_cast<int>(SkBlendMode::kMultiply), "xfermode mismatch");
786 static_assert(25 == static_cast<int>(SkBlendMode::kHue), "xfermode mismatch");
787 static_assert(26 == static_cast<int>(SkBlendMode::kSaturation), "xfermode mismatch");
788 static_assert(27 == static_cast<int>(SkBlendMode::kColor), "xfermode mismatch");
789 static_assert(28 == static_cast<int>(SkBlendMode::kLuminosity), "xfermode mismatch");
John Reckf2285972016-09-30 14:10:05 -0700790
Mike Reed260ab722016-10-07 15:59:20 -0400791 SkBlendMode mode = static_cast<SkBlendMode>(xfermodeHandle);
John Reckf2285972016-09-30 14:10:05 -0700792 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400793 paint->setBlendMode(mode);
John Reckf2285972016-09-30 14:10:05 -0700794 }
795
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100796 static jlong setPathEffect(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong effectHandle) {
John Reckf2285972016-09-30 14:10:05 -0700797 Paint* obj = reinterpret_cast<Paint*>(objHandle);
798 SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400799 obj->setPathEffect(sk_ref_sp(effect));
800 return reinterpret_cast<jlong>(obj->getPathEffect());
John Reckf2285972016-09-30 14:10:05 -0700801 }
802
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100803 static jlong setMaskFilter(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong maskfilterHandle) {
John Reckf2285972016-09-30 14:10:05 -0700804 Paint* obj = reinterpret_cast<Paint*>(objHandle);
805 SkMaskFilter* maskfilter = reinterpret_cast<SkMaskFilter*>(maskfilterHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400806 obj->setMaskFilter(sk_ref_sp(maskfilter));
807 return reinterpret_cast<jlong>(obj->getMaskFilter());
John Reckf2285972016-09-30 14:10:05 -0700808 }
809
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100810 static void setTypeface(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong typefaceHandle) {
Seigo Nonaka318ca042017-08-01 16:36:18 -0700811 Paint* paint = reinterpret_cast<Paint*>(objHandle);
812 paint->setAndroidTypeface(reinterpret_cast<Typeface*>(typefaceHandle));
John Reckf2285972016-09-30 14:10:05 -0700813 }
814
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100815 static jint getTextAlign(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700816 Paint* obj = reinterpret_cast<Paint*>(objHandle);
817 return static_cast<jint>(obj->getTextAlign());
818 }
819
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100820 static void setTextAlign(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint alignHandle) {
John Reckf2285972016-09-30 14:10:05 -0700821 Paint* obj = reinterpret_cast<Paint*>(objHandle);
822 Paint::Align align = static_cast<Paint::Align>(alignHandle);
823 obj->setTextAlign(align);
824 }
825
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100826 static void setTextLocalesByMinikinLocaleListId(CRITICAL_JNI_PARAMS_COMMA jlong objHandle,
Seigo Nonaka20866c12017-10-26 16:02:01 -0700827 jint minikinLocaleListId) {
John Reckf2285972016-09-30 14:10:05 -0700828 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Seigo Nonaka20866c12017-10-26 16:02:01 -0700829 obj->setMinikinLocaleListId(minikinLocaleListId);
John Reckf2285972016-09-30 14:10:05 -0700830 }
831
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100832 static jboolean isElegantTextHeight(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700833 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700834 return obj->getFamilyVariant() == minikin::FamilyVariant::ELEGANT;
John Reckf2285972016-09-30 14:10:05 -0700835 }
836
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100837 static void setElegantTextHeight(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean aa) {
John Reckf2285972016-09-30 14:10:05 -0700838 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakac52075e2017-11-17 15:40:06 -0800839 obj->setFamilyVariant(
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700840 aa ? minikin::FamilyVariant::ELEGANT : minikin::FamilyVariant::DEFAULT);
John Reckf2285972016-09-30 14:10:05 -0700841 }
842
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100843 static jfloat getTextSize(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500844 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize());
John Reckf2285972016-09-30 14:10:05 -0700845 }
846
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100847 static void setTextSize(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat textSize) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500848 if (textSize >= 0) {
849 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSize(textSize);
850 }
John Reckf2285972016-09-30 14:10:05 -0700851 }
852
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100853 static jfloat getTextScaleX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500854 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getScaleX());
John Reckf2285972016-09-30 14:10:05 -0700855 }
856
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100857 static void setTextScaleX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat scaleX) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500858 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setScaleX(scaleX);
John Reckf2285972016-09-30 14:10:05 -0700859 }
860
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100861 static jfloat getTextSkewX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500862 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSkewX());
John Reckf2285972016-09-30 14:10:05 -0700863 }
864
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100865 static void setTextSkewX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat skewX) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500866 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSkewX(skewX);
John Reckf2285972016-09-30 14:10:05 -0700867 }
868
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100869 static jfloat getLetterSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700870 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
871 return paint->getLetterSpacing();
872 }
873
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100874 static void setLetterSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat letterSpacing) {
John Reckf2285972016-09-30 14:10:05 -0700875 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
876 paint->setLetterSpacing(letterSpacing);
877 }
878
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100879 static jfloat getWordSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Seigo Nonaka219e2c792016-11-15 19:01:45 +0900880 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
881 return paint->getWordSpacing();
882 }
883
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100884 static void setWordSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat wordSpacing) {
Seigo Nonaka219e2c792016-11-15 19:01:45 +0900885 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
886 paint->setWordSpacing(wordSpacing);
887 }
888
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100889 static jint getStartHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
John Reckf2285972016-09-30 14:10:05 -0700890 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800891 return static_cast<jint>(paint->getStartHyphenEdit());
John Reckf2285972016-09-30 14:10:05 -0700892 }
893
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100894 static jint getEndHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
John Reckf2285972016-09-30 14:10:05 -0700895 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800896 return static_cast<jint>(paint->getEndHyphenEdit());
897 }
898
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100899 static void setStartHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800900 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
901 paint->setStartHyphenEdit((uint32_t)hyphen);
902 }
903
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100904 static void setEndHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800905 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
906 paint->setEndHyphenEdit((uint32_t)hyphen);
John Reckf2285972016-09-30 14:10:05 -0700907 }
908
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100909 static jfloat ascent(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500910 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700911 getMetricsInternal(paintHandle, &metrics);
John Reckf2285972016-09-30 14:10:05 -0700912 return SkScalarToFloat(metrics.fAscent);
913 }
914
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100915 static jfloat descent(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500916 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700917 getMetricsInternal(paintHandle, &metrics);
John Reckf2285972016-09-30 14:10:05 -0700918 return SkScalarToFloat(metrics.fDescent);
919 }
920
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100921 static jfloat getUnderlinePosition(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500922 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700923 getMetricsInternal(paintHandle, &metrics);
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700924 SkScalar position;
925 if (metrics.hasUnderlinePosition(&position)) {
926 return SkScalarToFloat(position);
927 } else {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500928 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700929 return SkScalarToFloat(Paint::kStdUnderline_Top * textSize);
930 }
931 }
932
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100933 static jfloat getUnderlineThickness(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500934 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700935 getMetricsInternal(paintHandle, &metrics);
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700936 SkScalar thickness;
937 if (metrics.hasUnderlineThickness(&thickness)) {
938 return SkScalarToFloat(thickness);
939 } else {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500940 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700941 return SkScalarToFloat(Paint::kStdUnderline_Thickness * textSize);
942 }
943 }
944
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100945 static jfloat getStrikeThruPosition(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500946 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournader1378a9d2017-07-13 12:45:20 -0700947 return SkScalarToFloat(Paint::kStdStrikeThru_Top * textSize);
948 }
949
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100950 static jfloat getStrikeThruThickness(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500951 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournader1378a9d2017-07-13 12:45:20 -0700952 return SkScalarToFloat(Paint::kStdStrikeThru_Thickness * textSize);
953 }
954
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100955 static void setShadowLayer(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat radius,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500956 jfloat dx, jfloat dy, jlong colorSpaceHandle,
Leon Scroggins III10965f32019-03-12 11:13:27 -0400957 jlong colorLong) {
958 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500959 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500960
961 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
962 if (radius <= 0) {
963 paint->setLooper(nullptr);
964 }
965 else {
966 SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius);
Mike Reed0f9dce72021-02-12 21:20:33 -0500967 paint->setLooper(BlurDrawLooper::Make(color, cs.get(), sigma, {dx, dy}));
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500968 }
969 }
970
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100971 static jboolean hasShadowLayer(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700972 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Mike Reed0f9dce72021-02-12 21:20:33 -0500973 return paint->getLooper() != nullptr;
John Reckf2285972016-09-30 14:10:05 -0700974 }
975
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100976 static jboolean equalsForTextMeasurement(CRITICAL_JNI_PARAMS_COMMA jlong lPaint, jlong rPaint) {
Seigo Nonakabeafa1f2018-02-01 21:39:24 -0800977 if (lPaint == rPaint) {
978 return true;
979 }
980 Paint* leftPaint = reinterpret_cast<Paint*>(lPaint);
981 Paint* rightPaint = reinterpret_cast<Paint*>(rPaint);
982
983 const Typeface* leftTypeface = Typeface::resolveDefault(leftPaint->getAndroidTypeface());
984 const Typeface* rightTypeface = Typeface::resolveDefault(rightPaint->getAndroidTypeface());
985 minikin::MinikinPaint leftMinikinPaint
986 = MinikinUtils::prepareMinikinPaint(leftPaint, leftTypeface);
987 minikin::MinikinPaint rightMinikinPaint
988 = MinikinUtils::prepareMinikinPaint(rightPaint, rightTypeface);
989
990 return leftMinikinPaint == rightMinikinPaint;
991 }
992
John Reckdbffd252015-10-01 14:46:12 -0700993}; // namespace PaintGlue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994
Daniel Micay76f6a862015-09-19 17:31:01 -0400995static const JNINativeMethod methods[] = {
Richard Uhler775873a2015-12-29 12:37:39 -0800996 {"nGetNativeFinalizer", "()J", (void*) PaintGlue::getNativeFinalizer},
John Reckdbffd252015-10-01 14:46:12 -0700997 {"nInit","()J", (void*) PaintGlue::init},
998 {"nInitWithPaint","(J)J", (void*) PaintGlue::initWithPaint},
Seigo Nonaka318ca042017-08-01 16:36:18 -0700999 {"nBreakText","(J[CIIFI[F)I", (void*) PaintGlue::breakTextC},
1000 {"nBreakText","(JLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS},
1001 {"nGetTextAdvances","(J[CIIIII[FI)F",
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -07001002 (void*) PaintGlue::getTextAdvances___CIIIII_FI},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001003 {"nGetTextAdvances","(JLjava/lang/String;IIIII[FI)F",
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -07001004 (void*) PaintGlue::getTextAdvances__StringIIIII_FI},
Fabrice Di Meglioda12f382013-03-15 11:26:56 -07001005
Seigo Nonaka318ca042017-08-01 16:36:18 -07001006 {"nGetTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C},
1007 {"nGetTextRunCursor", "(JLjava/lang/String;IIIII)I",
Raph Leviena027ec52015-04-06 16:21:59 -07001008 (void*) PaintGlue::getTextRunCursor__String},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001009 {"nGetTextPath", "(JI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C},
1010 {"nGetTextPath", "(JILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String},
1011 {"nGetStringBounds", "(JLjava/lang/String;IIILandroid/graphics/Rect;)V",
Raph Leviena027ec52015-04-06 16:21:59 -07001012 (void*) PaintGlue::getStringBounds },
Seigo Nonaka318ca042017-08-01 16:36:18 -07001013 {"nGetCharArrayBounds", "(J[CIIILandroid/graphics/Rect;)V",
Raph Leviena027ec52015-04-06 16:21:59 -07001014 (void*) PaintGlue::getCharArrayBounds },
Seigo Nonaka318ca042017-08-01 16:36:18 -07001015 {"nHasGlyph", "(JILjava/lang/String;)Z", (void*) PaintGlue::hasGlyph },
1016 {"nGetRunAdvance", "(J[CIIIIZI)F", (void*) PaintGlue::getRunAdvance___CIIIIZI_F},
1017 {"nGetOffsetForAdvance", "(J[CIIIIZF)I",
Raph Leviena027ec52015-04-06 16:21:59 -07001018 (void*) PaintGlue::getOffsetForAdvance___CIIIIZF_I},
Chris Craik4136a0a2014-10-07 15:09:46 -07001019
John Reckf2285972016-09-30 14:10:05 -07001020 // --------------- @FastNative ----------------------
1021
1022 {"nSetTextLocales","(JLjava/lang/String;)I", (void*) PaintGlue::setTextLocales},
1023 {"nSetFontFeatureSettings","(JLjava/lang/String;)V",
1024 (void*) PaintGlue::setFontFeatureSettings},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001025 {"nGetFontMetrics", "(JLandroid/graphics/Paint$FontMetrics;)F",
John Reckf2285972016-09-30 14:10:05 -07001026 (void*)PaintGlue::getFontMetrics},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001027 {"nGetFontMetricsInt", "(JLandroid/graphics/Paint$FontMetricsInt;)I",
John Reckf2285972016-09-30 14:10:05 -07001028 (void*)PaintGlue::getFontMetricsInt},
1029
1030 // --------------- @CriticalNative ------------------
1031
1032 {"nReset","(J)V", (void*) PaintGlue::reset},
1033 {"nSet","(JJ)V", (void*) PaintGlue::assign},
1034 {"nGetFlags","(J)I", (void*) PaintGlue::getFlags},
1035 {"nSetFlags","(JI)V", (void*) PaintGlue::setFlags},
1036 {"nGetHinting","(J)I", (void*) PaintGlue::getHinting},
1037 {"nSetHinting","(JI)V", (void*) PaintGlue::setHinting},
1038 {"nSetAntiAlias","(JZ)V", (void*) PaintGlue::setAntiAlias},
1039 {"nSetSubpixelText","(JZ)V", (void*) PaintGlue::setSubpixelText},
1040 {"nSetLinearText","(JZ)V", (void*) PaintGlue::setLinearText},
1041 {"nSetUnderlineText","(JZ)V", (void*) PaintGlue::setUnderlineText},
1042 {"nSetStrikeThruText","(JZ)V", (void*) PaintGlue::setStrikeThruText},
1043 {"nSetFakeBoldText","(JZ)V", (void*) PaintGlue::setFakeBoldText},
1044 {"nSetFilterBitmap","(JZ)V", (void*) PaintGlue::setFilterBitmap},
1045 {"nSetDither","(JZ)V", (void*) PaintGlue::setDither},
1046 {"nGetStyle","(J)I", (void*) PaintGlue::getStyle},
1047 {"nSetStyle","(JI)V", (void*) PaintGlue::setStyle},
Leon Scroggins III03b3e2362019-03-12 10:15:46 -04001048 {"nSetColor","(JI)V", (void*) PaintGlue::setColor},
Leon Scroggins III10965f32019-03-12 11:13:27 -04001049 {"nSetColor","(JJJ)V", (void*) PaintGlue::setColorLong},
John Reckf2285972016-09-30 14:10:05 -07001050 {"nSetAlpha","(JI)V", (void*) PaintGlue::setAlpha},
1051 {"nGetStrokeWidth","(J)F", (void*) PaintGlue::getStrokeWidth},
1052 {"nSetStrokeWidth","(JF)V", (void*) PaintGlue::setStrokeWidth},
1053 {"nGetStrokeMiter","(J)F", (void*) PaintGlue::getStrokeMiter},
1054 {"nSetStrokeMiter","(JF)V", (void*) PaintGlue::setStrokeMiter},
1055 {"nGetStrokeCap","(J)I", (void*) PaintGlue::getStrokeCap},
1056 {"nSetStrokeCap","(JI)V", (void*) PaintGlue::setStrokeCap},
1057 {"nGetStrokeJoin","(J)I", (void*) PaintGlue::getStrokeJoin},
1058 {"nSetStrokeJoin","(JI)V", (void*) PaintGlue::setStrokeJoin},
1059 {"nGetFillPath","(JJJ)Z", (void*) PaintGlue::getFillPath},
Nader Jawad5bed1f52020-09-25 00:27:29 -07001060 {"nSetShader","(JJ)J", (void*) PaintGlue::setShader},
John Reckf2285972016-09-30 14:10:05 -07001061 {"nSetColorFilter","(JJ)J", (void*) PaintGlue::setColorFilter},
1062 {"nSetXfermode","(JI)V", (void*) PaintGlue::setXfermode},
1063 {"nSetPathEffect","(JJ)J", (void*) PaintGlue::setPathEffect},
1064 {"nSetMaskFilter","(JJ)J", (void*) PaintGlue::setMaskFilter},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001065 {"nSetTypeface","(JJ)V", (void*) PaintGlue::setTypeface},
John Reckf2285972016-09-30 14:10:05 -07001066 {"nGetTextAlign","(J)I", (void*) PaintGlue::getTextAlign},
1067 {"nSetTextAlign","(JI)V", (void*) PaintGlue::setTextAlign},
Seigo Nonaka20866c12017-10-26 16:02:01 -07001068 {"nSetTextLocalesByMinikinLocaleListId","(JI)V",
1069 (void*) PaintGlue::setTextLocalesByMinikinLocaleListId},
John Reckf2285972016-09-30 14:10:05 -07001070 {"nIsElegantTextHeight","(J)Z", (void*) PaintGlue::isElegantTextHeight},
1071 {"nSetElegantTextHeight","(JZ)V", (void*) PaintGlue::setElegantTextHeight},
1072 {"nGetTextSize","(J)F", (void*) PaintGlue::getTextSize},
1073 {"nSetTextSize","(JF)V", (void*) PaintGlue::setTextSize},
1074 {"nGetTextScaleX","(J)F", (void*) PaintGlue::getTextScaleX},
1075 {"nSetTextScaleX","(JF)V", (void*) PaintGlue::setTextScaleX},
1076 {"nGetTextSkewX","(J)F", (void*) PaintGlue::getTextSkewX},
1077 {"nSetTextSkewX","(JF)V", (void*) PaintGlue::setTextSkewX},
1078 {"nGetLetterSpacing","(J)F", (void*) PaintGlue::getLetterSpacing},
1079 {"nSetLetterSpacing","(JF)V", (void*) PaintGlue::setLetterSpacing},
Seigo Nonaka219e2c792016-11-15 19:01:45 +09001080 {"nGetWordSpacing","(J)F", (void*) PaintGlue::getWordSpacing},
1081 {"nSetWordSpacing","(JF)V", (void*) PaintGlue::setWordSpacing},
Seigo Nonakafb1b4792019-03-08 14:05:08 -08001082 {"nGetStartHyphenEdit", "(J)I", (void*) PaintGlue::getStartHyphenEdit},
1083 {"nGetEndHyphenEdit", "(J)I", (void*) PaintGlue::getEndHyphenEdit},
1084 {"nSetStartHyphenEdit", "(JI)V", (void*) PaintGlue::setStartHyphenEdit},
1085 {"nSetEndHyphenEdit", "(JI)V", (void*) PaintGlue::setEndHyphenEdit},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001086 {"nAscent","(J)F", (void*) PaintGlue::ascent},
1087 {"nDescent","(J)F", (void*) PaintGlue::descent},
1088 {"nGetUnderlinePosition","(J)F", (void*) PaintGlue::getUnderlinePosition},
1089 {"nGetUnderlineThickness","(J)F", (void*) PaintGlue::getUnderlineThickness},
1090 {"nGetStrikeThruPosition","(J)F", (void*) PaintGlue::getStrikeThruPosition},
1091 {"nGetStrikeThruThickness","(J)F", (void*) PaintGlue::getStrikeThruThickness},
Leon Scroggins III10965f32019-03-12 11:13:27 -04001092 {"nSetShadowLayer", "(JFFFJJ)V", (void*)PaintGlue::setShadowLayer},
Seigo Nonakabeafa1f2018-02-01 21:39:24 -08001093 {"nHasShadowLayer", "(J)Z", (void*)PaintGlue::hasShadowLayer},
1094 {"nEqualsForTextMeasurement", "(JJ)Z", (void*)PaintGlue::equalsForTextMeasurement},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095};
1096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097int register_android_graphics_Paint(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001098 return RegisterMethodsOrDie(env, "android/graphics/Paint", methods, NELEM(methods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099}
1100
1101}