blob: f76863255153bef66bf081078e1746ec80a227c8 [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
Mike Reedda3488a2018-12-14 12:08:55 -0500544 static SkScalar getMetricsInternal(jlong paintHandle, SkFontMetrics *metrics) {
John Reckf2285972016-09-30 14:10:05 -0700545 const int kElegantTop = 2500;
546 const int kElegantBottom = -1000;
547 const int kElegantAscent = 1900;
548 const int kElegantDescent = -500;
549 const int kElegantLeading = 0;
550 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Mike Reedf6d86ac2019-01-18 14:13:23 -0500551 SkFont* font = &paint->getSkFont();
Seigo Nonaka318ca042017-08-01 16:36:18 -0700552 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700553 typeface = Typeface::resolveDefault(typeface);
554 minikin::FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle);
Mike Reedf6d86ac2019-01-18 14:13:23 -0500555 float saveSkewX = font->getSkewX();
556 bool savefakeBold = font->isEmbolden();
557 MinikinFontSkia::populateSkFont(font, baseFont.font->typeface().get(), baseFont.fakery);
558 SkScalar spacing = font->getMetrics(metrics);
John Reckf2285972016-09-30 14:10:05 -0700559 // The populateSkPaint call may have changed fake bold / text skew
560 // because we want to measure with those effects applied, so now
561 // restore the original settings.
Mike Reedf6d86ac2019-01-18 14:13:23 -0500562 font->setSkewX(saveSkewX);
563 font->setEmbolden(savefakeBold);
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700564 if (paint->getFamilyVariant() == minikin::FamilyVariant::ELEGANT) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500565 SkScalar size = font->getSize();
John Reckf2285972016-09-30 14:10:05 -0700566 metrics->fTop = -size * kElegantTop / 2048;
567 metrics->fBottom = -size * kElegantBottom / 2048;
568 metrics->fAscent = -size * kElegantAscent / 2048;
569 metrics->fDescent = -size * kElegantDescent / 2048;
570 metrics->fLeading = size * kElegantLeading / 2048;
571 spacing = metrics->fDescent - metrics->fAscent + metrics->fLeading;
572 }
573 return spacing;
574 }
575
Seigo Nonaka78c774d2021-12-10 10:49:43 -0800576 static void doFontExtent(JNIEnv* env, jlong paintHandle, const jchar buf[], jint start,
577 jint count, jint bufSize, jboolean isRtl, jobject fmi) {
578 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
579 const Typeface* typeface = paint->getAndroidTypeface();
580 minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
581 minikin::MinikinExtent extent =
582 MinikinUtils::getFontExtent(paint, bidiFlags, typeface, buf, start, count, bufSize);
583
584 SkFontMetrics metrics;
585 getMetricsInternal(paintHandle, &metrics);
586
587 metrics.fAscent = extent.ascent;
588 metrics.fDescent = extent.descent;
589
590 // If top/bottom is narrower than ascent/descent, adjust top/bottom to ascent/descent.
591 metrics.fTop = std::min(metrics.fAscent, metrics.fTop);
592 metrics.fBottom = std::max(metrics.fDescent, metrics.fBottom);
593
594 GraphicsJNI::set_metrics_int(env, fmi, metrics);
595 }
596
597 static void getFontMetricsIntForText___C(JNIEnv* env, jclass, jlong paintHandle,
598 jcharArray text, jint start, jint count, jint ctxStart,
599 jint ctxCount, jboolean isRtl, jobject fmi) {
600 ScopedCharArrayRO textArray(env, text);
601
602 doFontExtent(env, paintHandle, textArray.get() + ctxStart, start - ctxStart, count,
603 ctxCount, isRtl, fmi);
604 }
605
606 static void getFontMetricsIntForText___String(JNIEnv* env, jclass, jlong paintHandle,
607 jstring text, jint start, jint count,
608 jint ctxStart, jint ctxCount, jboolean isRtl,
609 jobject fmi) {
610 ScopedStringChars textChars(env, text);
611
612 doFontExtent(env, paintHandle, textChars.get() + ctxStart, start - ctxStart, count,
613 ctxCount, isRtl, fmi);
614 }
615
616 // ------------------ @FastNative ---------------------------
617
618 static jint setTextLocales(JNIEnv* env, jobject clazz, jlong objHandle, jstring locales) {
619 Paint* obj = reinterpret_cast<Paint*>(objHandle);
620 ScopedUtfChars localesChars(env, locales);
621 jint minikinLocaleListId = minikin::registerLocaleList(localesChars.c_str());
622 obj->setMinikinLocaleListId(minikinLocaleListId);
623 return minikinLocaleListId;
624 }
625
626 static void setFontFeatureSettings(JNIEnv* env, jobject clazz, jlong paintHandle,
627 jstring settings) {
628 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
629 if (!settings) {
630 paint->setFontFeatureSettings(std::string());
631 } else {
632 ScopedUtfChars settingsChars(env, settings);
633 paint->setFontFeatureSettings(std::string(settingsChars.c_str(), settingsChars.size()));
634 }
635 }
636
Seigo Nonaka318ca042017-08-01 16:36:18 -0700637 static jfloat getFontMetrics(JNIEnv* env, jobject, jlong paintHandle, jobject metricsObj) {
Mike Reedda3488a2018-12-14 12:08:55 -0500638 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700639 SkScalar spacing = getMetricsInternal(paintHandle, &metrics);
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700640 GraphicsJNI::set_metrics(env, metricsObj, metrics);
John Reckf2285972016-09-30 14:10:05 -0700641 return SkScalarToFloat(spacing);
642 }
643
Seigo Nonaka318ca042017-08-01 16:36:18 -0700644 static jint getFontMetricsInt(JNIEnv* env, jobject, jlong paintHandle, jobject metricsObj) {
Mike Reedda3488a2018-12-14 12:08:55 -0500645 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700646 getMetricsInternal(paintHandle, &metrics);
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700647 return GraphicsJNI::set_metrics_int(env, metricsObj, metrics);
John Reckf2285972016-09-30 14:10:05 -0700648 }
649
650
651 // ------------------ @CriticalNative ---------------------------
652
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100653 static void reset(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500654 reinterpret_cast<Paint*>(objHandle)->reset();
John Reckf2285972016-09-30 14:10:05 -0700655 }
656
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100657 static void assign(CRITICAL_JNI_PARAMS_COMMA jlong dstPaintHandle, jlong srcPaintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700658 Paint* dst = reinterpret_cast<Paint*>(dstPaintHandle);
659 const Paint* src = reinterpret_cast<Paint*>(srcPaintHandle);
660 *dst = *src;
661 }
662
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100663 static jint getFlags(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500664 uint32_t flags = reinterpret_cast<Paint*>(paintHandle)->getJavaFlags();
665 return static_cast<jint>(flags);
John Reckf2285972016-09-30 14:10:05 -0700666 }
667
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100668 static void setFlags(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint flags) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500669 reinterpret_cast<Paint*>(paintHandle)->setJavaFlags(flags);
John Reckf2285972016-09-30 14:10:05 -0700670 }
671
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100672 static jint getHinting(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reed03acdb12019-01-26 10:17:31 -0500673 return (SkFontHinting)reinterpret_cast<Paint*>(paintHandle)->getSkFont().getHinting()
Ben Wagner0ab44392019-05-09 18:44:34 -0400674 == SkFontHinting::kNone ? 0 : 1;
John Reckf2285972016-09-30 14:10:05 -0700675 }
676
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100677 static void setHinting(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint mode) {
Mike Reed03acdb12019-01-26 10:17:31 -0500678 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setHinting(
Ben Wagner0ab44392019-05-09 18:44:34 -0400679 mode == 0 ? SkFontHinting::kNone : SkFontHinting::kNormal);
John Reckf2285972016-09-30 14:10:05 -0700680 }
681
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100682 static void setAntiAlias(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean aa) {
John Reckf2285972016-09-30 14:10:05 -0700683 reinterpret_cast<Paint*>(paintHandle)->setAntiAlias(aa);
684 }
685
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100686 static void setLinearText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean linearText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500687 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setLinearMetrics(linearText);
John Reckf2285972016-09-30 14:10:05 -0700688 }
689
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100690 static void setSubpixelText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean subpixelText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500691 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSubpixel(subpixelText);
John Reckf2285972016-09-30 14:10:05 -0700692 }
693
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100694 static void setUnderlineText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean underlineText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500695 reinterpret_cast<Paint*>(paintHandle)->setUnderline(underlineText);
John Reckf2285972016-09-30 14:10:05 -0700696 }
697
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100698 static void setStrikeThruText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean strikeThruText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500699 reinterpret_cast<Paint*>(paintHandle)->setStrikeThru(strikeThruText);
John Reckf2285972016-09-30 14:10:05 -0700700 }
701
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100702 static void setFakeBoldText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean fakeBoldText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500703 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setEmbolden(fakeBoldText);
John Reckf2285972016-09-30 14:10:05 -0700704 }
705
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100706 static void setFilterBitmap(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean filterBitmap) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400707 reinterpret_cast<Paint*>(paintHandle)->setFilterBitmap(filterBitmap);
John Reckf2285972016-09-30 14:10:05 -0700708 }
709
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100710 static void setDither(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean dither) {
John Reckf2285972016-09-30 14:10:05 -0700711 reinterpret_cast<Paint*>(paintHandle)->setDither(dither);
712 }
713
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100714 static jint getStyle(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700715 Paint* obj = reinterpret_cast<Paint*>(objHandle);
716 return static_cast<jint>(obj->getStyle());
717 }
718
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100719 static void setStyle(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint styleHandle) {
John Reckf2285972016-09-30 14:10:05 -0700720 Paint* obj = reinterpret_cast<Paint*>(objHandle);
721 Paint::Style style = static_cast<Paint::Style>(styleHandle);
722 obj->setStyle(style);
723 }
724
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100725 static void setColorLong(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jlong colorSpaceHandle,
Leon Scroggins III10965f32019-03-12 11:13:27 -0400726 jlong colorLong) {
727 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500728 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500729 reinterpret_cast<Paint*>(paintHandle)->setColor4f(color, cs.get());
730 }
731
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100732 static void setColor(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint color) {
Leon Scroggins III03b3e2362019-03-12 10:15:46 -0400733 reinterpret_cast<Paint*>(paintHandle)->setColor(color);
734 }
735
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100736 static void setAlpha(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint a) {
John Reckf2285972016-09-30 14:10:05 -0700737 reinterpret_cast<Paint*>(paintHandle)->setAlpha(a);
738 }
739
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100740 static jfloat getStrokeWidth(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700741 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeWidth());
742 }
743
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100744 static void setStrokeWidth(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat width) {
John Reckf2285972016-09-30 14:10:05 -0700745 reinterpret_cast<Paint*>(paintHandle)->setStrokeWidth(width);
746 }
747
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100748 static jfloat getStrokeMiter(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700749 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeMiter());
750 }
751
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100752 static void setStrokeMiter(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat miter) {
John Reckf2285972016-09-30 14:10:05 -0700753 reinterpret_cast<Paint*>(paintHandle)->setStrokeMiter(miter);
754 }
755
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100756 static jint getStrokeCap(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700757 Paint* obj = reinterpret_cast<Paint*>(objHandle);
758 return static_cast<jint>(obj->getStrokeCap());
759 }
760
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100761 static void setStrokeCap(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint capHandle) {
John Reckf2285972016-09-30 14:10:05 -0700762 Paint* obj = reinterpret_cast<Paint*>(objHandle);
763 Paint::Cap cap = static_cast<Paint::Cap>(capHandle);
764 obj->setStrokeCap(cap);
765 }
766
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100767 static jint getStrokeJoin(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700768 Paint* obj = reinterpret_cast<Paint*>(objHandle);
769 return static_cast<jint>(obj->getStrokeJoin());
770 }
771
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100772 static void setStrokeJoin(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint joinHandle) {
John Reckf2285972016-09-30 14:10:05 -0700773 Paint* obj = reinterpret_cast<Paint*>(objHandle);
774 Paint::Join join = (Paint::Join) joinHandle;
775 obj->setStrokeJoin(join);
776 }
777
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100778 static jboolean getFillPath(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong srcHandle, jlong dstHandle) {
John Reckf2285972016-09-30 14:10:05 -0700779 Paint* obj = reinterpret_cast<Paint*>(objHandle);
780 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
781 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
782 return obj->getFillPath(*src, dst) ? JNI_TRUE : JNI_FALSE;
783 }
784
Nader Jawad5bed1f52020-09-25 00:27:29 -0700785 static jlong setShader(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong shaderHandle) {
786 Paint* obj = reinterpret_cast<Paint*>(objHandle);
787 SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle);
788 obj->setShader(sk_ref_sp(shader));
789 return reinterpret_cast<jlong>(obj->getShader());
John Reckf2285972016-09-30 14:10:05 -0700790 }
791
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100792 static jlong setColorFilter(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong filterHandle) {
John Reckf2285972016-09-30 14:10:05 -0700793 Paint* obj = reinterpret_cast<Paint *>(objHandle);
794 SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(filterHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400795 obj->setColorFilter(sk_ref_sp(filter));
796 return reinterpret_cast<jlong>(obj->getColorFilter());
John Reckf2285972016-09-30 14:10:05 -0700797 }
798
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100799 static void setXfermode(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint xfermodeHandle) {
John Reckf2285972016-09-30 14:10:05 -0700800 // validate that the Java enum values match our expectations
Mike Reed260ab722016-10-07 15:59:20 -0400801 static_assert(0 == static_cast<int>(SkBlendMode::kClear), "xfermode_mismatch");
802 static_assert(1 == static_cast<int>(SkBlendMode::kSrc), "xfermode_mismatch");
803 static_assert(2 == static_cast<int>(SkBlendMode::kDst), "xfermode_mismatch");
804 static_assert(3 == static_cast<int>(SkBlendMode::kSrcOver), "xfermode_mismatch");
805 static_assert(4 == static_cast<int>(SkBlendMode::kDstOver), "xfermode_mismatch");
806 static_assert(5 == static_cast<int>(SkBlendMode::kSrcIn), "xfermode_mismatch");
807 static_assert(6 == static_cast<int>(SkBlendMode::kDstIn), "xfermode_mismatch");
808 static_assert(7 == static_cast<int>(SkBlendMode::kSrcOut), "xfermode_mismatch");
809 static_assert(8 == static_cast<int>(SkBlendMode::kDstOut), "xfermode_mismatch");
810 static_assert(9 == static_cast<int>(SkBlendMode::kSrcATop), "xfermode_mismatch");
811 static_assert(10 == static_cast<int>(SkBlendMode::kDstATop), "xfermode_mismatch");
812 static_assert(11 == static_cast<int>(SkBlendMode::kXor), "xfermode_mismatch");
Nader Jawad55e49d82018-11-16 11:22:32 -0800813 static_assert(12 == static_cast<int>(SkBlendMode::kPlus), "xfermode_mismatch");
Mike Reed260ab722016-10-07 15:59:20 -0400814 static_assert(13 == static_cast<int>(SkBlendMode::kModulate), "xfermode_mismatch");
815 static_assert(14 == static_cast<int>(SkBlendMode::kScreen), "xfermode_mismatch");
Mike Reed260ab722016-10-07 15:59:20 -0400816 static_assert(15 == static_cast<int>(SkBlendMode::kOverlay), "xfermode_mismatch");
Nader Jawad55e49d82018-11-16 11:22:32 -0800817 static_assert(16 == static_cast<int>(SkBlendMode::kDarken), "xfermode_mismatch");
818 static_assert(17 == static_cast<int>(SkBlendMode::kLighten), "xfermode_mismatch");
819 static_assert(18 == static_cast<int>(SkBlendMode::kColorDodge), "xfermode mismatch");
820 static_assert(19 == static_cast<int>(SkBlendMode::kColorBurn), "xfermode mismatch");
821 static_assert(20 == static_cast<int>(SkBlendMode::kHardLight), "xfermode mismatch");
822 static_assert(21 == static_cast<int>(SkBlendMode::kSoftLight), "xfermode mismatch");
823 static_assert(22 == static_cast<int>(SkBlendMode::kDifference), "xfermode mismatch");
824 static_assert(23 == static_cast<int>(SkBlendMode::kExclusion), "xfermode mismatch");
825 static_assert(24 == static_cast<int>(SkBlendMode::kMultiply), "xfermode mismatch");
826 static_assert(25 == static_cast<int>(SkBlendMode::kHue), "xfermode mismatch");
827 static_assert(26 == static_cast<int>(SkBlendMode::kSaturation), "xfermode mismatch");
828 static_assert(27 == static_cast<int>(SkBlendMode::kColor), "xfermode mismatch");
829 static_assert(28 == static_cast<int>(SkBlendMode::kLuminosity), "xfermode mismatch");
John Reckf2285972016-09-30 14:10:05 -0700830
Mike Reed260ab722016-10-07 15:59:20 -0400831 SkBlendMode mode = static_cast<SkBlendMode>(xfermodeHandle);
John Reckf2285972016-09-30 14:10:05 -0700832 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400833 paint->setBlendMode(mode);
John Reckf2285972016-09-30 14:10:05 -0700834 }
835
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100836 static jlong setPathEffect(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong effectHandle) {
John Reckf2285972016-09-30 14:10:05 -0700837 Paint* obj = reinterpret_cast<Paint*>(objHandle);
838 SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400839 obj->setPathEffect(sk_ref_sp(effect));
840 return reinterpret_cast<jlong>(obj->getPathEffect());
John Reckf2285972016-09-30 14:10:05 -0700841 }
842
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100843 static jlong setMaskFilter(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong maskfilterHandle) {
John Reckf2285972016-09-30 14:10:05 -0700844 Paint* obj = reinterpret_cast<Paint*>(objHandle);
845 SkMaskFilter* maskfilter = reinterpret_cast<SkMaskFilter*>(maskfilterHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400846 obj->setMaskFilter(sk_ref_sp(maskfilter));
847 return reinterpret_cast<jlong>(obj->getMaskFilter());
John Reckf2285972016-09-30 14:10:05 -0700848 }
849
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100850 static void setTypeface(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong typefaceHandle) {
Seigo Nonaka318ca042017-08-01 16:36:18 -0700851 Paint* paint = reinterpret_cast<Paint*>(objHandle);
852 paint->setAndroidTypeface(reinterpret_cast<Typeface*>(typefaceHandle));
John Reckf2285972016-09-30 14:10:05 -0700853 }
854
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100855 static jint getTextAlign(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700856 Paint* obj = reinterpret_cast<Paint*>(objHandle);
857 return static_cast<jint>(obj->getTextAlign());
858 }
859
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100860 static void setTextAlign(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint alignHandle) {
John Reckf2285972016-09-30 14:10:05 -0700861 Paint* obj = reinterpret_cast<Paint*>(objHandle);
862 Paint::Align align = static_cast<Paint::Align>(alignHandle);
863 obj->setTextAlign(align);
864 }
865
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100866 static void setTextLocalesByMinikinLocaleListId(CRITICAL_JNI_PARAMS_COMMA jlong objHandle,
Seigo Nonaka20866c12017-10-26 16:02:01 -0700867 jint minikinLocaleListId) {
John Reckf2285972016-09-30 14:10:05 -0700868 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Seigo Nonaka20866c12017-10-26 16:02:01 -0700869 obj->setMinikinLocaleListId(minikinLocaleListId);
John Reckf2285972016-09-30 14:10:05 -0700870 }
871
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100872 static jboolean isElegantTextHeight(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700873 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700874 return obj->getFamilyVariant() == minikin::FamilyVariant::ELEGANT;
John Reckf2285972016-09-30 14:10:05 -0700875 }
876
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100877 static void setElegantTextHeight(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean aa) {
John Reckf2285972016-09-30 14:10:05 -0700878 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakac52075e2017-11-17 15:40:06 -0800879 obj->setFamilyVariant(
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700880 aa ? minikin::FamilyVariant::ELEGANT : minikin::FamilyVariant::DEFAULT);
John Reckf2285972016-09-30 14:10:05 -0700881 }
882
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100883 static jfloat getTextSize(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500884 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize());
John Reckf2285972016-09-30 14:10:05 -0700885 }
886
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100887 static void setTextSize(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat textSize) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500888 if (textSize >= 0) {
889 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSize(textSize);
890 }
John Reckf2285972016-09-30 14:10:05 -0700891 }
892
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100893 static jfloat getTextScaleX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500894 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getScaleX());
John Reckf2285972016-09-30 14:10:05 -0700895 }
896
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100897 static void setTextScaleX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat scaleX) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500898 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setScaleX(scaleX);
John Reckf2285972016-09-30 14:10:05 -0700899 }
900
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100901 static jfloat getTextSkewX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500902 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSkewX());
John Reckf2285972016-09-30 14:10:05 -0700903 }
904
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100905 static void setTextSkewX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat skewX) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500906 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSkewX(skewX);
John Reckf2285972016-09-30 14:10:05 -0700907 }
908
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100909 static jfloat getLetterSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700910 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
911 return paint->getLetterSpacing();
912 }
913
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100914 static void setLetterSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat letterSpacing) {
John Reckf2285972016-09-30 14:10:05 -0700915 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
916 paint->setLetterSpacing(letterSpacing);
917 }
918
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100919 static jfloat getWordSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Seigo Nonaka219e2c792016-11-15 19:01:45 +0900920 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
921 return paint->getWordSpacing();
922 }
923
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100924 static void setWordSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat wordSpacing) {
Seigo Nonaka219e2c792016-11-15 19:01:45 +0900925 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
926 paint->setWordSpacing(wordSpacing);
927 }
928
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100929 static jint getStartHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
John Reckf2285972016-09-30 14:10:05 -0700930 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800931 return static_cast<jint>(paint->getStartHyphenEdit());
John Reckf2285972016-09-30 14:10:05 -0700932 }
933
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100934 static jint getEndHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
John Reckf2285972016-09-30 14:10:05 -0700935 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800936 return static_cast<jint>(paint->getEndHyphenEdit());
937 }
938
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100939 static void setStartHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800940 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
941 paint->setStartHyphenEdit((uint32_t)hyphen);
942 }
943
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100944 static void setEndHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800945 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
946 paint->setEndHyphenEdit((uint32_t)hyphen);
John Reckf2285972016-09-30 14:10:05 -0700947 }
948
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100949 static jfloat ascent(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500950 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700951 getMetricsInternal(paintHandle, &metrics);
John Reckf2285972016-09-30 14:10:05 -0700952 return SkScalarToFloat(metrics.fAscent);
953 }
954
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100955 static jfloat descent(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500956 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700957 getMetricsInternal(paintHandle, &metrics);
John Reckf2285972016-09-30 14:10:05 -0700958 return SkScalarToFloat(metrics.fDescent);
959 }
960
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100961 static jfloat getUnderlinePosition(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500962 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700963 getMetricsInternal(paintHandle, &metrics);
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700964 SkScalar position;
965 if (metrics.hasUnderlinePosition(&position)) {
966 return SkScalarToFloat(position);
967 } else {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500968 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700969 return SkScalarToFloat(Paint::kStdUnderline_Top * textSize);
970 }
971 }
972
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100973 static jfloat getUnderlineThickness(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500974 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700975 getMetricsInternal(paintHandle, &metrics);
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700976 SkScalar thickness;
977 if (metrics.hasUnderlineThickness(&thickness)) {
978 return SkScalarToFloat(thickness);
979 } else {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500980 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700981 return SkScalarToFloat(Paint::kStdUnderline_Thickness * textSize);
982 }
983 }
984
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100985 static jfloat getStrikeThruPosition(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500986 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournader1378a9d2017-07-13 12:45:20 -0700987 return SkScalarToFloat(Paint::kStdStrikeThru_Top * textSize);
988 }
989
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100990 static jfloat getStrikeThruThickness(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500991 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournader1378a9d2017-07-13 12:45:20 -0700992 return SkScalarToFloat(Paint::kStdStrikeThru_Thickness * textSize);
993 }
994
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100995 static void setShadowLayer(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat radius,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500996 jfloat dx, jfloat dy, jlong colorSpaceHandle,
Leon Scroggins III10965f32019-03-12 11:13:27 -0400997 jlong colorLong) {
998 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500999 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Leon Scroggins III0e443d12018-12-19 11:38:35 -05001000
1001 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
1002 if (radius <= 0) {
1003 paint->setLooper(nullptr);
1004 }
1005 else {
1006 SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius);
Mike Reed0f9dce72021-02-12 21:20:33 -05001007 paint->setLooper(BlurDrawLooper::Make(color, cs.get(), sigma, {dx, dy}));
Leon Scroggins III0e443d12018-12-19 11:38:35 -05001008 }
1009 }
1010
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +01001011 static jboolean hasShadowLayer(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -07001012 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Mike Reed0f9dce72021-02-12 21:20:33 -05001013 return paint->getLooper() != nullptr;
John Reckf2285972016-09-30 14:10:05 -07001014 }
1015
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +01001016 static jboolean equalsForTextMeasurement(CRITICAL_JNI_PARAMS_COMMA jlong lPaint, jlong rPaint) {
Seigo Nonakabeafa1f2018-02-01 21:39:24 -08001017 if (lPaint == rPaint) {
1018 return true;
1019 }
1020 Paint* leftPaint = reinterpret_cast<Paint*>(lPaint);
1021 Paint* rightPaint = reinterpret_cast<Paint*>(rPaint);
1022
1023 const Typeface* leftTypeface = Typeface::resolveDefault(leftPaint->getAndroidTypeface());
1024 const Typeface* rightTypeface = Typeface::resolveDefault(rightPaint->getAndroidTypeface());
1025 minikin::MinikinPaint leftMinikinPaint
1026 = MinikinUtils::prepareMinikinPaint(leftPaint, leftTypeface);
1027 minikin::MinikinPaint rightMinikinPaint
1028 = MinikinUtils::prepareMinikinPaint(rightPaint, rightTypeface);
1029
1030 return leftMinikinPaint == rightMinikinPaint;
1031 }
1032
John Reckdbffd252015-10-01 14:46:12 -07001033}; // namespace PaintGlue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034
Daniel Micay76f6a862015-09-19 17:31:01 -04001035static const JNINativeMethod methods[] = {
Richard Uhler775873a2015-12-29 12:37:39 -08001036 {"nGetNativeFinalizer", "()J", (void*) PaintGlue::getNativeFinalizer},
John Reckdbffd252015-10-01 14:46:12 -07001037 {"nInit","()J", (void*) PaintGlue::init},
1038 {"nInitWithPaint","(J)J", (void*) PaintGlue::initWithPaint},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001039 {"nBreakText","(J[CIIFI[F)I", (void*) PaintGlue::breakTextC},
1040 {"nBreakText","(JLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS},
1041 {"nGetTextAdvances","(J[CIIIII[FI)F",
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -07001042 (void*) PaintGlue::getTextAdvances___CIIIII_FI},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001043 {"nGetTextAdvances","(JLjava/lang/String;IIIII[FI)F",
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -07001044 (void*) PaintGlue::getTextAdvances__StringIIIII_FI},
Fabrice Di Meglioda12f382013-03-15 11:26:56 -07001045
Seigo Nonaka318ca042017-08-01 16:36:18 -07001046 {"nGetTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C},
1047 {"nGetTextRunCursor", "(JLjava/lang/String;IIIII)I",
Raph Leviena027ec52015-04-06 16:21:59 -07001048 (void*) PaintGlue::getTextRunCursor__String},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001049 {"nGetTextPath", "(JI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C},
1050 {"nGetTextPath", "(JILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String},
1051 {"nGetStringBounds", "(JLjava/lang/String;IIILandroid/graphics/Rect;)V",
Raph Leviena027ec52015-04-06 16:21:59 -07001052 (void*) PaintGlue::getStringBounds },
Seigo Nonaka318ca042017-08-01 16:36:18 -07001053 {"nGetCharArrayBounds", "(J[CIIILandroid/graphics/Rect;)V",
Raph Leviena027ec52015-04-06 16:21:59 -07001054 (void*) PaintGlue::getCharArrayBounds },
Seigo Nonaka318ca042017-08-01 16:36:18 -07001055 {"nHasGlyph", "(JILjava/lang/String;)Z", (void*) PaintGlue::hasGlyph },
1056 {"nGetRunAdvance", "(J[CIIIIZI)F", (void*) PaintGlue::getRunAdvance___CIIIIZI_F},
1057 {"nGetOffsetForAdvance", "(J[CIIIIZF)I",
Raph Leviena027ec52015-04-06 16:21:59 -07001058 (void*) PaintGlue::getOffsetForAdvance___CIIIIZF_I},
Seigo Nonaka78c774d2021-12-10 10:49:43 -08001059 {"nGetFontMetricsIntForText", "(J[CIIIIZLandroid/graphics/Paint$FontMetricsInt;)V",
1060 (void*)PaintGlue::getFontMetricsIntForText___C},
1061 {"nGetFontMetricsIntForText",
1062 "(JLjava/lang/String;IIIIZLandroid/graphics/Paint$FontMetricsInt;)V",
1063 (void*)PaintGlue::getFontMetricsIntForText___String},
Chris Craik4136a0a2014-10-07 15:09:46 -07001064
John Reckf2285972016-09-30 14:10:05 -07001065 // --------------- @FastNative ----------------------
1066
1067 {"nSetTextLocales","(JLjava/lang/String;)I", (void*) PaintGlue::setTextLocales},
1068 {"nSetFontFeatureSettings","(JLjava/lang/String;)V",
1069 (void*) PaintGlue::setFontFeatureSettings},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001070 {"nGetFontMetrics", "(JLandroid/graphics/Paint$FontMetrics;)F",
John Reckf2285972016-09-30 14:10:05 -07001071 (void*)PaintGlue::getFontMetrics},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001072 {"nGetFontMetricsInt", "(JLandroid/graphics/Paint$FontMetricsInt;)I",
John Reckf2285972016-09-30 14:10:05 -07001073 (void*)PaintGlue::getFontMetricsInt},
1074
1075 // --------------- @CriticalNative ------------------
1076
1077 {"nReset","(J)V", (void*) PaintGlue::reset},
1078 {"nSet","(JJ)V", (void*) PaintGlue::assign},
1079 {"nGetFlags","(J)I", (void*) PaintGlue::getFlags},
1080 {"nSetFlags","(JI)V", (void*) PaintGlue::setFlags},
1081 {"nGetHinting","(J)I", (void*) PaintGlue::getHinting},
1082 {"nSetHinting","(JI)V", (void*) PaintGlue::setHinting},
1083 {"nSetAntiAlias","(JZ)V", (void*) PaintGlue::setAntiAlias},
1084 {"nSetSubpixelText","(JZ)V", (void*) PaintGlue::setSubpixelText},
1085 {"nSetLinearText","(JZ)V", (void*) PaintGlue::setLinearText},
1086 {"nSetUnderlineText","(JZ)V", (void*) PaintGlue::setUnderlineText},
1087 {"nSetStrikeThruText","(JZ)V", (void*) PaintGlue::setStrikeThruText},
1088 {"nSetFakeBoldText","(JZ)V", (void*) PaintGlue::setFakeBoldText},
1089 {"nSetFilterBitmap","(JZ)V", (void*) PaintGlue::setFilterBitmap},
1090 {"nSetDither","(JZ)V", (void*) PaintGlue::setDither},
1091 {"nGetStyle","(J)I", (void*) PaintGlue::getStyle},
1092 {"nSetStyle","(JI)V", (void*) PaintGlue::setStyle},
Leon Scroggins III03b3e2362019-03-12 10:15:46 -04001093 {"nSetColor","(JI)V", (void*) PaintGlue::setColor},
Leon Scroggins III10965f32019-03-12 11:13:27 -04001094 {"nSetColor","(JJJ)V", (void*) PaintGlue::setColorLong},
John Reckf2285972016-09-30 14:10:05 -07001095 {"nSetAlpha","(JI)V", (void*) PaintGlue::setAlpha},
1096 {"nGetStrokeWidth","(J)F", (void*) PaintGlue::getStrokeWidth},
1097 {"nSetStrokeWidth","(JF)V", (void*) PaintGlue::setStrokeWidth},
1098 {"nGetStrokeMiter","(J)F", (void*) PaintGlue::getStrokeMiter},
1099 {"nSetStrokeMiter","(JF)V", (void*) PaintGlue::setStrokeMiter},
1100 {"nGetStrokeCap","(J)I", (void*) PaintGlue::getStrokeCap},
1101 {"nSetStrokeCap","(JI)V", (void*) PaintGlue::setStrokeCap},
1102 {"nGetStrokeJoin","(J)I", (void*) PaintGlue::getStrokeJoin},
1103 {"nSetStrokeJoin","(JI)V", (void*) PaintGlue::setStrokeJoin},
1104 {"nGetFillPath","(JJJ)Z", (void*) PaintGlue::getFillPath},
Nader Jawad5bed1f52020-09-25 00:27:29 -07001105 {"nSetShader","(JJ)J", (void*) PaintGlue::setShader},
John Reckf2285972016-09-30 14:10:05 -07001106 {"nSetColorFilter","(JJ)J", (void*) PaintGlue::setColorFilter},
1107 {"nSetXfermode","(JI)V", (void*) PaintGlue::setXfermode},
1108 {"nSetPathEffect","(JJ)J", (void*) PaintGlue::setPathEffect},
1109 {"nSetMaskFilter","(JJ)J", (void*) PaintGlue::setMaskFilter},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001110 {"nSetTypeface","(JJ)V", (void*) PaintGlue::setTypeface},
John Reckf2285972016-09-30 14:10:05 -07001111 {"nGetTextAlign","(J)I", (void*) PaintGlue::getTextAlign},
1112 {"nSetTextAlign","(JI)V", (void*) PaintGlue::setTextAlign},
Seigo Nonaka20866c12017-10-26 16:02:01 -07001113 {"nSetTextLocalesByMinikinLocaleListId","(JI)V",
1114 (void*) PaintGlue::setTextLocalesByMinikinLocaleListId},
John Reckf2285972016-09-30 14:10:05 -07001115 {"nIsElegantTextHeight","(J)Z", (void*) PaintGlue::isElegantTextHeight},
1116 {"nSetElegantTextHeight","(JZ)V", (void*) PaintGlue::setElegantTextHeight},
1117 {"nGetTextSize","(J)F", (void*) PaintGlue::getTextSize},
1118 {"nSetTextSize","(JF)V", (void*) PaintGlue::setTextSize},
1119 {"nGetTextScaleX","(J)F", (void*) PaintGlue::getTextScaleX},
1120 {"nSetTextScaleX","(JF)V", (void*) PaintGlue::setTextScaleX},
1121 {"nGetTextSkewX","(J)F", (void*) PaintGlue::getTextSkewX},
1122 {"nSetTextSkewX","(JF)V", (void*) PaintGlue::setTextSkewX},
1123 {"nGetLetterSpacing","(J)F", (void*) PaintGlue::getLetterSpacing},
1124 {"nSetLetterSpacing","(JF)V", (void*) PaintGlue::setLetterSpacing},
Seigo Nonaka219e2c792016-11-15 19:01:45 +09001125 {"nGetWordSpacing","(J)F", (void*) PaintGlue::getWordSpacing},
1126 {"nSetWordSpacing","(JF)V", (void*) PaintGlue::setWordSpacing},
Seigo Nonakafb1b4792019-03-08 14:05:08 -08001127 {"nGetStartHyphenEdit", "(J)I", (void*) PaintGlue::getStartHyphenEdit},
1128 {"nGetEndHyphenEdit", "(J)I", (void*) PaintGlue::getEndHyphenEdit},
1129 {"nSetStartHyphenEdit", "(JI)V", (void*) PaintGlue::setStartHyphenEdit},
1130 {"nSetEndHyphenEdit", "(JI)V", (void*) PaintGlue::setEndHyphenEdit},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001131 {"nAscent","(J)F", (void*) PaintGlue::ascent},
1132 {"nDescent","(J)F", (void*) PaintGlue::descent},
1133 {"nGetUnderlinePosition","(J)F", (void*) PaintGlue::getUnderlinePosition},
1134 {"nGetUnderlineThickness","(J)F", (void*) PaintGlue::getUnderlineThickness},
1135 {"nGetStrikeThruPosition","(J)F", (void*) PaintGlue::getStrikeThruPosition},
1136 {"nGetStrikeThruThickness","(J)F", (void*) PaintGlue::getStrikeThruThickness},
Leon Scroggins III10965f32019-03-12 11:13:27 -04001137 {"nSetShadowLayer", "(JFFFJJ)V", (void*)PaintGlue::setShadowLayer},
Seigo Nonakabeafa1f2018-02-01 21:39:24 -08001138 {"nHasShadowLayer", "(J)Z", (void*)PaintGlue::hasShadowLayer},
1139 {"nEqualsForTextMeasurement", "(JJ)Z", (void*)PaintGlue::equalsForTextMeasurement},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140};
1141
Seigo Nonaka78c774d2021-12-10 10:49:43 -08001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143int register_android_graphics_Paint(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001144 return RegisterMethodsOrDie(env, "android/graphics/Paint", methods, NELEM(methods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145}
1146
1147}