blob: 0aa14655725c94a7ed9149970fe75e29dfb5037f [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"
Kevin Lubick07d6aae2022-04-01 14:03:11 -040029#include "SkColorSpace.h"
Mike Reedf6d86ac2019-01-18 14:13:23 -050030#include "SkFont.h"
Mike Reed0ea09a42019-01-25 13:04:14 -050031#include "SkFontMetrics.h"
Mike Reeddad2f892018-11-05 16:13:54 -050032#include "SkFontTypes.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033#include "SkMaskFilter.h"
Derek Sollenbergereba81d02015-10-26 10:22:37 -040034#include "SkPath.h"
Mike Reed260ab722016-10-07 15:59:20 -040035#include "SkPathEffect.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "SkShader.h"
Mike Reedc2f31df2016-10-28 17:21:45 -040037#include "SkBlendMode.h"
Billy Hewlettac1cbaf2012-07-18 09:51:45 -070038#include "unicode/uloc.h"
Doug Felt0c702b82010-05-14 10:55:42 -070039#include "unicode/ushape.h"
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -040040#include "utils/Blur.h"
Doug Felt0c702b82010-05-14 10:55:42 -070041
Mike Reed0f9dce72021-02-12 21:20:33 -050042#include <hwui/BlurDrawLooper.h>
sergeyvdccca442016-03-21 15:38:21 -070043#include <hwui/MinikinSkia.h>
44#include <hwui/MinikinUtils.h>
45#include <hwui/Paint.h>
sergeyvbad99182016-03-17 11:24:22 -070046#include <hwui/Typeface.h>
Raph Leviene368b6b2014-06-15 17:37:57 -070047#include <minikin/GraphemeBreak.h>
Seigo Nonaka20866c12017-10-26 16:02:01 -070048#include <minikin/LocaleList.h>
Raph Leviena027ec52015-04-06 16:21:59 -070049#include <minikin/Measurement.h>
Seigo Nonaka0ca492f2018-05-25 14:52:22 -070050#include <minikin/MinikinPaint.h>
Seigo Nonakabb1a9662015-10-21 23:24:25 +090051#include <unicode/utf16.h>
Raph Levien1a73f7322014-01-30 16:06:28 -080052
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -070053#include <cassert>
54#include <cstring>
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -070055#include <memory>
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -070056#include <vector>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58namespace android {
59
Mike Reedf6d86ac2019-01-18 14:13:23 -050060static void getPosTextPath(const SkFont& font, const uint16_t glyphs[], int count,
61 const SkPoint pos[], SkPath* dst) {
Mike Reedbacaa1d2019-01-29 08:07:50 -050062 dst->reset();
Mike Reedf6d86ac2019-01-18 14:13:23 -050063 struct Rec {
64 SkPath* fDst;
65 const SkPoint* fPos;
66 } rec = { dst, pos };
67 font.getPaths(glyphs, count, [](const SkPath* src, const SkMatrix& mx, void* ctx) {
68 Rec* rec = (Rec*)ctx;
69 if (src) {
70 SkMatrix tmp(mx);
71 tmp.postTranslate(rec->fPos->fX, rec->fPos->fY);
72 rec->fDst->addPath(*src, tmp);
73 }
74 rec->fPos += 1;
75 }, &rec);
Mike Reed3d63e012009-07-27 09:50:31 -040076}
77
John Reckdbffd252015-10-01 14:46:12 -070078namespace PaintGlue {
Doug Felt0c702b82010-05-14 10:55:42 -070079 enum MoveOpt {
80 AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT
81 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
Richard Uhler775873a2015-12-29 12:37:39 -080083 static void deletePaint(Paint* paint) {
84 delete paint;
85 }
86
87 static jlong getNativeFinalizer(JNIEnv*, jobject) {
88 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&deletePaint));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 }
90
John Reckdbffd252015-10-01 14:46:12 -070091 static jlong init(JNIEnv* env, jobject) {
Mike Reedf6d86ac2019-01-18 14:13:23 -050092 return reinterpret_cast<jlong>(new Paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 }
94
Ashok Bhat36bef0b2014-01-20 20:08:01 +000095 static jlong initWithPaint(JNIEnv* env, jobject clazz, jlong paintHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040096 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
97 Paint* obj = new Paint(*paint);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000098 return reinterpret_cast<jlong>(obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700100
Seigo Nonaka318ca042017-08-01 16:36:18 -0700101 static int breakText(JNIEnv* env, const Paint& paint, const Typeface* typeface,
102 const jchar text[], int count, float maxWidth, jint bidiFlags, jfloatArray jmeasured,
John Reckf2285972016-09-30 14:10:05 -0700103 const bool forwardScan) {
104 size_t measuredCount = 0;
105 float measured = 0;
Elliott Hughes8451b252011-04-07 19:17:57 -0700106
John Reckf2285972016-09-30 14:10:05 -0700107 std::unique_ptr<float[]> advancesArray(new float[count]);
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700108 MinikinUtils::measureText(&paint, static_cast<minikin::Bidi>(bidiFlags), typeface, text,
109 0, count, count, advancesArray.get());
Elliott Hughes8451b252011-04-07 19:17:57 -0700110
John Reckf2285972016-09-30 14:10:05 -0700111 for (int i = 0; i < count; i++) {
112 // traverse in the given direction
113 int index = forwardScan ? i : (count - i - 1);
114 float width = advancesArray[index];
115 if (measured + width > maxWidth) {
116 break;
117 }
118 // properly handle clusters when scanning backwards
119 if (forwardScan || width != 0.0f) {
120 measuredCount = i + 1;
121 }
122 measured += width;
Mike Reed4c9355c2014-05-07 11:48:37 -0400123 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700124
John Reckf2285972016-09-30 14:10:05 -0700125 if (jmeasured && env->GetArrayLength(jmeasured) > 0) {
126 AutoJavaFloatArray autoMeasured(env, jmeasured, 1);
127 jfloat* array = autoMeasured.ptr();
128 array[0] = measured;
Behdad Esfahbod805f6eb2014-07-29 18:43:03 -0400129 }
John Reckf2285972016-09-30 14:10:05 -0700130 return measuredCount;
Behdad Esfahbod805f6eb2014-07-29 18:43:03 -0400131 }
132
Seigo Nonaka318ca042017-08-01 16:36:18 -0700133 static jint breakTextC(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray jtext,
134 jint index, jint count, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) {
John Reckf2285972016-09-30 14:10:05 -0700135 NPE_CHECK_RETURN_ZERO(env, jtext);
Raph Levien210a1892015-03-09 14:42:14 -0700136
John Reckdbffd252015-10-01 14:46:12 -0700137 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700138 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700139
140 bool forwardTextDirection;
141 if (count < 0) {
142 forwardTextDirection = false;
143 count = -count;
Raph Levien53c00772014-04-14 14:11:02 -0700144 }
John Reckf2285972016-09-30 14:10:05 -0700145 else {
146 forwardTextDirection = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
John Reckf2285972016-09-30 14:10:05 -0700148
149 if ((index < 0) || (index + count > env->GetArrayLength(jtext))) {
150 doThrowAIOOBE(env);
151 return 0;
152 }
153
154 const jchar* text = env->GetCharArrayElements(jtext, nullptr);
155 count = breakText(env, *paint, typeface, text + index, count, maxWidth,
156 bidiFlags, jmeasuredWidth, forwardTextDirection);
157 env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text),
158 JNI_ABORT);
159 return count;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700161
Seigo Nonaka318ca042017-08-01 16:36:18 -0700162 static jint breakTextS(JNIEnv* env, jobject clazz, jlong paintHandle, jstring jtext,
163 jboolean forwards, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) {
John Reckf2285972016-09-30 14:10:05 -0700164 NPE_CHECK_RETURN_ZERO(env, jtext);
Elliott Hughes8451b252011-04-07 19:17:57 -0700165
John Reckf2285972016-09-30 14:10:05 -0700166 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700167 const Typeface* typeface = paint->getAndroidTypeface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
John Reckf2285972016-09-30 14:10:05 -0700169 int count = env->GetStringLength(jtext);
170 const jchar* text = env->GetStringChars(jtext, nullptr);
171 count = breakText(env, *paint, typeface, text, count, maxWidth, bidiFlags, jmeasuredWidth, forwards);
172 env->ReleaseStringChars(jtext, text);
173 return count;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 }
175
Seigo Nonaka318ca042017-08-01 16:36:18 -0700176 static jfloat doTextAdvances(JNIEnv *env, Paint *paint, const Typeface* typeface,
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700177 const jchar *text, jint start, jint count, jint contextCount, jint bidiFlags,
178 jfloatArray advances, jint advancesIndex) {
Fabrice Di Meglio6ab90ed2011-08-08 16:19:38 -0700179 NPE_CHECK_RETURN_ZERO(env, text);
180
181 if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) {
182 doThrowAIOOBE(env);
183 return 0;
184 }
185 if (count == 0) {
186 return 0;
187 }
188 if (advances) {
189 size_t advancesLength = env->GetArrayLength(advances);
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700190 if ((size_t)(count + advancesIndex) > advancesLength) {
Fabrice Di Meglio6ab90ed2011-08-08 16:19:38 -0700191 doThrowAIOOBE(env);
192 return 0;
193 }
194 }
Keisuke Kuroyanagia3024bd2015-09-16 20:02:15 -0700195 std::unique_ptr<jfloat[]> advancesArray;
196 if (advances) {
197 advancesArray.reset(new jfloat[count]);
198 }
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700199 const float advance = MinikinUtils::measureText(paint,
200 static_cast<minikin::Bidi>(bidiFlags), typeface, text, start, count, contextCount,
201 advancesArray.get());
Keisuke Kuroyanagia3024bd2015-09-16 20:02:15 -0700202 if (advances) {
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700203 env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray.get());
Doug Felt0c702b82010-05-14 10:55:42 -0700204 }
Keisuke Kuroyanagia3024bd2015-09-16 20:02:15 -0700205 return advance;
Doug Felt0c702b82010-05-14 10:55:42 -0700206 }
207
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700208 static jfloat getTextAdvances___CIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle,
Doug Felt0c702b82010-05-14 10:55:42 -0700209 jcharArray text, jint index, jint count, jint contextIndex, jint contextCount,
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700210 jint bidiFlags, jfloatArray advances, jint advancesIndex) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400211 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700212 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700213 jchar* textArray = env->GetCharArrayElements(text, nullptr);
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700214 jfloat result = doTextAdvances(env, paint, typeface, textArray + contextIndex,
215 index - contextIndex, count, contextCount, bidiFlags, advances, advancesIndex);
Doug Felt0c702b82010-05-14 10:55:42 -0700216 env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
217 return result;
218 }
219
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700220 static jfloat getTextAdvances__StringIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle,
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700221 jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint bidiFlags,
Fabrice Di Meglio665f02c2013-03-20 14:56:05 -0700222 jfloatArray advances, jint advancesIndex) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400223 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700224 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700225 const jchar* textArray = env->GetStringChars(text, nullptr);
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -0700226 jfloat result = doTextAdvances(env, paint, typeface, textArray + contextStart,
227 start - contextStart, end - start, contextEnd - contextStart, bidiFlags,
Fabrice Di Meglio665f02c2013-03-20 14:56:05 -0700228 advances, advancesIndex);
Fabrice Di Meglioeee49c62011-03-24 17:21:23 -0700229 env->ReleaseStringChars(text, textArray);
230 return result;
231 }
232
Seigo Nonaka318ca042017-08-01 16:36:18 -0700233 static jint doTextRunCursor(JNIEnv *env, Paint* paint, const Typeface* typeface,
234 const jchar *text, jint start, jint count, jint dir, jint offset, jint opt) {
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900235 minikin::GraphemeBreak::MoveOpt moveOpt = minikin::GraphemeBreak::MoveOpt(opt);
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700236 minikin::Bidi bidiFlags = dir == 1 ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
Roozbeh Pournader5d74b1c2017-03-13 21:41:15 -0700237 std::unique_ptr<float[]> advancesArray(new float[count]);
238 MinikinUtils::measureText(paint, bidiFlags, typeface, text, start, count, start + count,
239 advancesArray.get());
240 size_t result = minikin::GraphemeBreak::getTextRunCursor(advancesArray.get(), text,
241 start, count, offset, moveOpt);
Raph Leviene368b6b2014-06-15 17:37:57 -0700242 return static_cast<jint>(result);
Doug Felt0c702b82010-05-14 10:55:42 -0700243 }
244
Seigo Nonaka318ca042017-08-01 16:36:18 -0700245 static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text,
246 jint contextStart, jint contextCount, jint dir, jint offset, jint cursorOpt) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400247 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700248 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700249 jchar* textArray = env->GetCharArrayElements(text, nullptr);
Roozbeh Pournader5d74b1c2017-03-13 21:41:15 -0700250 jint result = doTextRunCursor(env, paint, typeface, textArray,
251 contextStart, contextCount, dir, offset, cursorOpt);
Doug Felt0c702b82010-05-14 10:55:42 -0700252 env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
253 return result;
254 }
255
Roozbeh Pournader5d74b1c2017-03-13 21:41:15 -0700256 static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, jlong paintHandle,
Seigo Nonaka318ca042017-08-01 16:36:18 -0700257 jstring text, jint contextStart, jint contextEnd, jint dir, jint offset,
258 jint cursorOpt) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400259 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700260 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700261 const jchar* textArray = env->GetStringChars(text, nullptr);
Roozbeh Pournader5d74b1c2017-03-13 21:41:15 -0700262 jint result = doTextRunCursor(env, paint, typeface, textArray,
263 contextStart, contextEnd - contextStart, dir, offset, cursorOpt);
Doug Felt0c702b82010-05-14 10:55:42 -0700264 env->ReleaseStringChars(text, textArray);
265 return result;
266 }
267
Raph Levienf2114d52014-06-01 15:54:47 -0700268 class GetTextFunctor {
269 public:
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900270 GetTextFunctor(const minikin::Layout& layout, SkPath* path, jfloat x, jfloat y,
271 Paint* paint, uint16_t* glyphs, SkPoint* pos)
Raph Levienf2114d52014-06-01 15:54:47 -0700272 : layout(layout), path(path), x(x), y(y), paint(paint), glyphs(glyphs), pos(pos) {
273 }
274
Raph Levien1fc0fa82014-06-06 18:05:22 -0700275 void operator()(size_t start, size_t end) {
Raph Levienf2114d52014-06-01 15:54:47 -0700276 for (size_t i = start; i < end; i++) {
277 glyphs[i] = layout.getGlyphId(i);
278 pos[i].fX = x + layout.getX(i);
279 pos[i].fY = y + layout.getY(i);
280 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500281 const SkFont& font = paint->getSkFont();
Raph Levienf2114d52014-06-01 15:54:47 -0700282 if (start == 0) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500283 getPosTextPath(font, glyphs, end, pos, path);
Raph Levienf2114d52014-06-01 15:54:47 -0700284 } else {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500285 getPosTextPath(font, glyphs + start, end - start, pos + start, &tmpPath);
Raph Levienf2114d52014-06-01 15:54:47 -0700286 path->addPath(tmpPath);
287 }
288 }
289 private:
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900290 const minikin::Layout& layout;
Raph Levienf2114d52014-06-01 15:54:47 -0700291 SkPath* path;
292 jfloat x;
293 jfloat y;
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400294 Paint* paint;
Raph Levienf2114d52014-06-01 15:54:47 -0700295 uint16_t* glyphs;
296 SkPoint* pos;
297 SkPath tmpPath;
298 };
Raph Levienf2114d52014-06-01 15:54:47 -0700299
Seigo Nonaka318ca042017-08-01 16:36:18 -0700300 static void getTextPath(JNIEnv* env, Paint* paint, const Typeface* typeface, const jchar* text,
Raph Levienf2114d52014-06-01 15:54:47 -0700301 jint count, jint bidiFlags, jfloat x, jfloat y, SkPath* path) {
Seigo Nonakac7064142017-02-10 16:53:31 +0900302 minikin::Layout layout = MinikinUtils::doLayout(
Seigo Nonaka3a4217f2018-05-02 12:56:16 -0700303 paint, static_cast<minikin::Bidi>(bidiFlags), typeface,
304 text, count, // text buffer
305 0, count, // draw range
306 0, count, // context range
Seigo Nonaka83143d02018-03-14 17:08:28 -0700307 nullptr);
Raph Levienf2114d52014-06-01 15:54:47 -0700308 size_t nGlyphs = layout.nGlyphs();
309 uint16_t* glyphs = new uint16_t[nGlyphs];
310 SkPoint* pos = new SkPoint[nGlyphs];
311
312 x += MinikinUtils::xOffsetForTextAlign(paint, layout);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400313 Paint::Align align = paint->getTextAlign();
314 paint->setTextAlign(Paint::kLeft_Align);
Raph Levienf2114d52014-06-01 15:54:47 -0700315 GetTextFunctor f(layout, path, x, y, paint, glyphs, pos);
Raph Levien1fc0fa82014-06-06 18:05:22 -0700316 MinikinUtils::forFontRun(layout, paint, f);
Raph Levienf2114d52014-06-01 15:54:47 -0700317 paint->setTextAlign(align);
318 delete[] glyphs;
319 delete[] pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
Doug Feltf7cb1f72010-07-01 16:20:43 -0700321
Seigo Nonaka318ca042017-08-01 16:36:18 -0700322 static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle, jint bidiFlags,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000323 jcharArray text, jint index, jint count, jfloat x, jfloat y, jlong pathHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400324 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700325 const Typeface* typeface = paint->getAndroidTypeface();
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000326 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
John Reckf2285972016-09-30 14:10:05 -0700327 const jchar* textArray = env->GetCharArrayElements(text, nullptr);
Raph Levienf2114d52014-06-01 15:54:47 -0700328 getTextPath(env, paint, typeface, textArray + index, count, bidiFlags, x, y, path);
Doug Feltf7cb1f72010-07-01 16:20:43 -0700329 env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
330 }
331
Seigo Nonaka318ca042017-08-01 16:36:18 -0700332 static void getTextPath__String(JNIEnv* env, jobject clazz, jlong paintHandle, jint bidiFlags,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000333 jstring text, jint start, jint end, jfloat x, jfloat y, jlong pathHandle) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400334 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700335 const Typeface* typeface = paint->getAndroidTypeface();
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000336 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
John Reckf2285972016-09-30 14:10:05 -0700337 const jchar* textArray = env->GetStringChars(text, nullptr);
Raph Levienf2114d52014-06-01 15:54:47 -0700338 getTextPath(env, paint, typeface, textArray + start, end - start, bidiFlags, x, y, path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 env->ReleaseStringChars(text, textArray);
340 }
Doug Feltf7cb1f72010-07-01 16:20:43 -0700341
Raph Levien854363e2014-06-03 19:56:05 -0700342 static void doTextBounds(JNIEnv* env, const jchar* text, int count, jobject bounds,
Seigo Nonaka96860de2020-09-22 23:40:18 -0700343 const Paint& paint, const Typeface* typeface, jint bidiFlagsInt) {
Romain Guy059e12c2012-11-28 17:35:51 -0800344 SkRect r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 SkIRect ir;
Elliott Hughes8451b252011-04-07 19:17:57 -0700346
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900347 minikin::MinikinRect rect;
Seigo Nonaka96860de2020-09-22 23:40:18 -0700348 minikin::Bidi bidiFlags = static_cast<minikin::Bidi>(bidiFlagsInt);
349 MinikinUtils::getBounds(&paint, bidiFlags, typeface, text, count, &rect);
Raph Levien854363e2014-06-03 19:56:05 -0700350 r.fLeft = rect.mLeft;
351 r.fTop = rect.mTop;
352 r.fRight = rect.mRight;
353 r.fBottom = rect.mBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 r.roundOut(&ir);
355 GraphicsJNI::irect_to_jrect(ir, env, bounds);
356 }
357
Seigo Nonaka318ca042017-08-01 16:36:18 -0700358 static void getStringBounds(JNIEnv* env, jobject, jlong paintHandle, jstring text, jint start,
359 jint end, jint bidiFlags, jobject bounds) {
John Reckdbffd252015-10-01 14:46:12 -0700360 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700361 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700362 const jchar* textArray = env->GetStringChars(text, nullptr);
Raph Levien854363e2014-06-03 19:56:05 -0700363 doTextBounds(env, textArray + start, end - start, bounds, *paint, typeface, bidiFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 env->ReleaseStringChars(text, textArray);
365 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700366
Seigo Nonaka318ca042017-08-01 16:36:18 -0700367 static void getCharArrayBounds(JNIEnv* env, jobject, jlong paintHandle, jcharArray text,
368 jint index, jint count, jint bidiFlags, jobject bounds) {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400369 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700370 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700371 const jchar* textArray = env->GetCharArrayElements(text, nullptr);
Raph Levien854363e2014-06-03 19:56:05 -0700372 doTextBounds(env, textArray + index, count, bounds, *paint, typeface, bidiFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray),
374 JNI_ABORT);
375 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700376
John Reckf2285972016-09-30 14:10:05 -0700377 // Returns true if the given string is exact one pair of regional indicators.
378 static bool isFlag(const jchar* str, size_t length) {
379 const jchar RI_LEAD_SURROGATE = 0xD83C;
380 const jchar RI_TRAIL_SURROGATE_MIN = 0xDDE6;
381 const jchar RI_TRAIL_SURROGATE_MAX = 0xDDFF;
382
383 if (length != 4) {
384 return false;
385 }
386 if (str[0] != RI_LEAD_SURROGATE || str[2] != RI_LEAD_SURROGATE) {
387 return false;
388 }
389 return RI_TRAIL_SURROGATE_MIN <= str[1] && str[1] <= RI_TRAIL_SURROGATE_MAX &&
390 RI_TRAIL_SURROGATE_MIN <= str[3] && str[3] <= RI_TRAIL_SURROGATE_MAX;
391 }
392
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900393 static jboolean layoutContainsNotdef(const minikin::Layout& layout) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700394 for (size_t i = 0; i < layout.nGlyphs(); i++) {
395 if (layout.getGlyphId(i) == 0) {
396 return true;
397 }
398 }
399 return false;
400 }
401
Raph Levien6141db32016-07-11 13:59:58 -0700402 // Don't count glyphs that are the recommended "space" glyph and are zero width.
403 // This logic makes assumptions about HarfBuzz layout, but does correctly handle
404 // cases where ligatures form and zero width space glyphs are left in as
405 // placeholders.
406 static size_t countNonSpaceGlyphs(const minikin::Layout& layout) {
407 size_t count = 0;
408 static unsigned int kSpaceGlyphId = 3;
409 for (size_t i = 0; i < layout.nGlyphs(); i++) {
410 if (layout.getGlyphId(i) != kSpaceGlyphId || layout.getCharAdvance(i) != 0.0) {
411 count++;
412 }
413 }
414 return count;
415 }
416
Seigo Nonaka318ca042017-08-01 16:36:18 -0700417 static jboolean hasGlyph(JNIEnv *env, jclass, jlong paintHandle, jint bidiFlags,
418 jstring string) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700419 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700420 const Typeface* typeface = paint->getAndroidTypeface();
Raph Levienf7f969e62015-04-01 14:41:21 -0700421 ScopedStringChars str(env, string);
422
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900423 /* Start by rejecting unsupported base code point and variation selector pairs. */
Raph Levienf7f969e62015-04-01 14:41:21 -0700424 size_t nChars = 0;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900425 const uint32_t kStartOfString = 0xFFFFFFFF;
426 uint32_t prevCp = kStartOfString;
Raph Levienf7f969e62015-04-01 14:41:21 -0700427 for (size_t i = 0; i < str.size(); i++) {
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900428 jchar cu = str[i];
429 uint32_t cp = cu;
430 if (U16_IS_TRAIL(cu)) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700431 // invalid UTF-16, unpaired trailing surrogate
432 return false;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900433 } else if (U16_IS_LEAD(cu)) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700434 if (i + 1 == str.size()) {
435 // invalid UTF-16, unpaired leading surrogate at end of string
436 return false;
437 }
438 i++;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900439 jchar cu2 = str[i];
440 if (!U16_IS_TRAIL(cu2)) {
Raph Levienf7f969e62015-04-01 14:41:21 -0700441 // invalid UTF-16, unpaired leading surrogate
442 return false;
443 }
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900444 cp = U16_GET_SUPPLEMENTARY(cu, cu2);
445 }
446
447 if (prevCp != kStartOfString &&
Raph Levien032a3592016-04-14 21:22:37 -0700448 ((0xFE00 <= cp && cp <= 0xFE0F) || (0xE0100 <= cp && cp <= 0xE01EF))) {
449 bool hasVS = MinikinUtils::hasVariationSelector(typeface, prevCp, cp);
450 if (!hasVS) {
451 // No font has a glyph for the code point and variation selector pair.
452 return false;
453 } else if (nChars == 1 && i + 1 == str.size()) {
454 // The string is just a codepoint and a VS, we have an authoritative answer
455 return true;
456 }
Raph Levienf7f969e62015-04-01 14:41:21 -0700457 }
458 nChars++;
Seigo Nonakabb1a9662015-10-21 23:24:25 +0900459 prevCp = cp;
Raph Levienf7f969e62015-04-01 14:41:21 -0700460 }
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700461 minikin::Layout layout = MinikinUtils::doLayout(paint,
Seigo Nonaka3a4217f2018-05-02 12:56:16 -0700462 static_cast<minikin::Bidi>(bidiFlags), typeface,
463 str.get(), str.size(), // text buffer
464 0, str.size(), // draw range
465 0, str.size(), // context range
466 nullptr);
Raph Levien6141db32016-07-11 13:59:58 -0700467 size_t nGlyphs = countNonSpaceGlyphs(layout);
Raph Levienf7f969e62015-04-01 14:41:21 -0700468 if (nGlyphs != 1 && nChars > 1) {
469 // multiple-character input, and was not a ligature
470 // TODO: handle ZWJ/ZWNJ characters specially so we can detect certain ligatures
471 // in joining scripts, such as Arabic and Mongolian.
472 return false;
473 }
Seigo Nonaka589cddc2016-04-05 14:43:14 +0900474
475 if (nGlyphs == 0 || layoutContainsNotdef(layout)) {
476 return false; // The collection doesn't have a glyph.
477 }
478
479 if (nChars == 2 && isFlag(str.get(), str.size())) {
480 // Some font may have a special glyph for unsupported regional indicator pairs.
481 // To return false for this case, need to compare the glyph id with the one of ZZ
482 // since ZZ is reserved for unknown or invalid territory.
483 // U+1F1FF (REGIONAL INDICATOR SYMBOL LETTER Z) is \uD83C\uDDFF in UTF16.
484 static const jchar ZZ_FLAG_STR[] = { 0xD83C, 0xDDFF, 0xD83C, 0xDDFF };
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700485 minikin::Layout zzLayout = MinikinUtils::doLayout(paint,
Seigo Nonaka3a4217f2018-05-02 12:56:16 -0700486 static_cast<minikin::Bidi>(bidiFlags), typeface,
487 ZZ_FLAG_STR, 4, // text buffer
488 0, 4, // draw range
489 0, 4, // context range
Seigo Nonaka83143d02018-03-14 17:08:28 -0700490 nullptr);
Seigo Nonaka589cddc2016-04-05 14:43:14 +0900491 if (zzLayout.nGlyphs() != 1 || layoutContainsNotdef(zzLayout)) {
492 // The font collection doesn't have a glyph for unknown flag. Just return true.
493 return true;
494 }
495 return zzLayout.getGlyphId(0) != layout.getGlyphId(0);
496 }
497 return true;
Raph Levienf7f969e62015-04-01 14:41:21 -0700498 }
499
Seigo Nonaka318ca042017-08-01 16:36:18 -0700500 static jfloat doRunAdvance(const Paint* paint, const Typeface* typeface, const jchar buf[],
Raph Leviena027ec52015-04-06 16:21:59 -0700501 jint start, jint count, jint bufSize, jboolean isRtl, jint offset) {
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700502 minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
Keisuke Kuroyanagib3677712016-03-28 19:16:30 +0900503 if (offset == start + count) {
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800504 return MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count,
505 bufSize, nullptr);
506 }
507 std::unique_ptr<float[]> advancesArray(new float[count]);
508 MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize,
509 advancesArray.get());
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900510 return minikin::getRunAdvance(advancesArray.get(), buf, start, count, offset);
Raph Leviena027ec52015-04-06 16:21:59 -0700511 }
512
Seigo Nonaka318ca042017-08-01 16:36:18 -0700513 static jfloat getRunAdvance___CIIIIZI_F(JNIEnv *env, jclass, jlong paintHandle, jcharArray text,
514 jint start, jint end, jint contextStart, jint contextEnd, jboolean isRtl, jint offset) {
Raph Leviena027ec52015-04-06 16:21:59 -0700515 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700516 const Typeface* typeface = paint->getAndroidTypeface();
Seigo Nonakaee23f612018-01-27 15:08:25 -0800517 ScopedCharArrayRO textArray(env, text);
518 jfloat result = doRunAdvance(paint, typeface, textArray.get() + contextStart,
Raph Levien36ff86c2015-06-03 10:58:33 -0700519 start - contextStart, end - start, contextEnd - contextStart, isRtl,
520 offset - contextStart);
Raph Leviena027ec52015-04-06 16:21:59 -0700521 return result;
522 }
523
Seigo Nonaka318ca042017-08-01 16:36:18 -0700524 static jint doOffsetForAdvance(const Paint* paint, const Typeface* typeface, const jchar buf[],
Raph Leviena027ec52015-04-06 16:21:59 -0700525 jint start, jint count, jint bufSize, jboolean isRtl, jfloat advance) {
Seigo Nonaka7c93e862017-10-25 16:34:48 -0700526 minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800527 std::unique_ptr<float[]> advancesArray(new float[count]);
528 MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize,
529 advancesArray.get());
Seigo Nonakaae1aa852016-06-09 19:42:51 +0900530 return minikin::getOffsetForAdvance(advancesArray.get(), buf, start, count, advance);
Raph Leviena027ec52015-04-06 16:21:59 -0700531 }
Keisuke Kuroyanagi4c8a5262016-02-18 11:31:56 -0800532
Raph Leviena027ec52015-04-06 16:21:59 -0700533 static jint getOffsetForAdvance___CIIIIZF_I(JNIEnv *env, jclass, jlong paintHandle,
Seigo Nonaka318ca042017-08-01 16:36:18 -0700534 jcharArray text, jint start, jint end, jint contextStart, jint contextEnd,
535 jboolean isRtl, jfloat advance) {
Raph Leviena027ec52015-04-06 16:21:59 -0700536 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka318ca042017-08-01 16:36:18 -0700537 const Typeface* typeface = paint->getAndroidTypeface();
Seigo Nonakaee23f612018-01-27 15:08:25 -0800538 ScopedCharArrayRO textArray(env, text);
539 jint result = doOffsetForAdvance(paint, typeface, textArray.get() + contextStart,
Raph Leviena027ec52015-04-06 16:21:59 -0700540 start - contextStart, end - start, contextEnd - contextStart, isRtl, advance);
541 result += contextStart;
Raph Leviena027ec52015-04-06 16:21:59 -0700542 return result;
543 }
544
Mike Reedda3488a2018-12-14 12:08:55 -0500545 static SkScalar getMetricsInternal(jlong paintHandle, SkFontMetrics *metrics) {
John Reckf2285972016-09-30 14:10:05 -0700546 const int kElegantTop = 2500;
547 const int kElegantBottom = -1000;
548 const int kElegantAscent = 1900;
549 const int kElegantDescent = -500;
550 const int kElegantLeading = 0;
551 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Mike Reedf6d86ac2019-01-18 14:13:23 -0500552 SkFont* font = &paint->getSkFont();
Seigo Nonaka318ca042017-08-01 16:36:18 -0700553 const Typeface* typeface = paint->getAndroidTypeface();
John Reckf2285972016-09-30 14:10:05 -0700554 typeface = Typeface::resolveDefault(typeface);
555 minikin::FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle);
Mike Reedf6d86ac2019-01-18 14:13:23 -0500556 float saveSkewX = font->getSkewX();
557 bool savefakeBold = font->isEmbolden();
558 MinikinFontSkia::populateSkFont(font, baseFont.font->typeface().get(), baseFont.fakery);
559 SkScalar spacing = font->getMetrics(metrics);
John Reckf2285972016-09-30 14:10:05 -0700560 // The populateSkPaint call may have changed fake bold / text skew
561 // because we want to measure with those effects applied, so now
562 // restore the original settings.
Mike Reedf6d86ac2019-01-18 14:13:23 -0500563 font->setSkewX(saveSkewX);
564 font->setEmbolden(savefakeBold);
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700565 if (paint->getFamilyVariant() == minikin::FamilyVariant::ELEGANT) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500566 SkScalar size = font->getSize();
John Reckf2285972016-09-30 14:10:05 -0700567 metrics->fTop = -size * kElegantTop / 2048;
568 metrics->fBottom = -size * kElegantBottom / 2048;
569 metrics->fAscent = -size * kElegantAscent / 2048;
570 metrics->fDescent = -size * kElegantDescent / 2048;
571 metrics->fLeading = size * kElegantLeading / 2048;
572 spacing = metrics->fDescent - metrics->fAscent + metrics->fLeading;
573 }
574 return spacing;
575 }
576
Seigo Nonaka78c774d2021-12-10 10:49:43 -0800577 static void doFontExtent(JNIEnv* env, jlong paintHandle, const jchar buf[], jint start,
578 jint count, jint bufSize, jboolean isRtl, jobject fmi) {
579 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
580 const Typeface* typeface = paint->getAndroidTypeface();
581 minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
582 minikin::MinikinExtent extent =
583 MinikinUtils::getFontExtent(paint, bidiFlags, typeface, buf, start, count, bufSize);
584
585 SkFontMetrics metrics;
586 getMetricsInternal(paintHandle, &metrics);
587
588 metrics.fAscent = extent.ascent;
589 metrics.fDescent = extent.descent;
590
591 // If top/bottom is narrower than ascent/descent, adjust top/bottom to ascent/descent.
592 metrics.fTop = std::min(metrics.fAscent, metrics.fTop);
593 metrics.fBottom = std::max(metrics.fDescent, metrics.fBottom);
594
595 GraphicsJNI::set_metrics_int(env, fmi, metrics);
596 }
597
598 static void getFontMetricsIntForText___C(JNIEnv* env, jclass, jlong paintHandle,
599 jcharArray text, jint start, jint count, jint ctxStart,
600 jint ctxCount, jboolean isRtl, jobject fmi) {
601 ScopedCharArrayRO textArray(env, text);
602
603 doFontExtent(env, paintHandle, textArray.get() + ctxStart, start - ctxStart, count,
604 ctxCount, isRtl, fmi);
605 }
606
607 static void getFontMetricsIntForText___String(JNIEnv* env, jclass, jlong paintHandle,
608 jstring text, jint start, jint count,
609 jint ctxStart, jint ctxCount, jboolean isRtl,
610 jobject fmi) {
611 ScopedStringChars textChars(env, text);
612
613 doFontExtent(env, paintHandle, textChars.get() + ctxStart, start - ctxStart, count,
614 ctxCount, isRtl, fmi);
615 }
616
617 // ------------------ @FastNative ---------------------------
618
619 static jint setTextLocales(JNIEnv* env, jobject clazz, jlong objHandle, jstring locales) {
620 Paint* obj = reinterpret_cast<Paint*>(objHandle);
621 ScopedUtfChars localesChars(env, locales);
622 jint minikinLocaleListId = minikin::registerLocaleList(localesChars.c_str());
623 obj->setMinikinLocaleListId(minikinLocaleListId);
624 return minikinLocaleListId;
625 }
626
627 static void setFontFeatureSettings(JNIEnv* env, jobject clazz, jlong paintHandle,
628 jstring settings) {
629 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
630 if (!settings) {
631 paint->setFontFeatureSettings(std::string());
632 } else {
633 ScopedUtfChars settingsChars(env, settings);
634 paint->setFontFeatureSettings(std::string(settingsChars.c_str(), settingsChars.size()));
635 }
636 }
637
Seigo Nonaka318ca042017-08-01 16:36:18 -0700638 static jfloat getFontMetrics(JNIEnv* env, jobject, jlong paintHandle, jobject metricsObj) {
Mike Reedda3488a2018-12-14 12:08:55 -0500639 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700640 SkScalar spacing = getMetricsInternal(paintHandle, &metrics);
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700641 GraphicsJNI::set_metrics(env, metricsObj, metrics);
John Reckf2285972016-09-30 14:10:05 -0700642 return SkScalarToFloat(spacing);
643 }
644
Seigo Nonaka318ca042017-08-01 16:36:18 -0700645 static jint getFontMetricsInt(JNIEnv* env, jobject, jlong paintHandle, jobject metricsObj) {
Mike Reedda3488a2018-12-14 12:08:55 -0500646 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700647 getMetricsInternal(paintHandle, &metrics);
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700648 return GraphicsJNI::set_metrics_int(env, metricsObj, metrics);
John Reckf2285972016-09-30 14:10:05 -0700649 }
650
651
652 // ------------------ @CriticalNative ---------------------------
653
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100654 static void reset(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500655 reinterpret_cast<Paint*>(objHandle)->reset();
John Reckf2285972016-09-30 14:10:05 -0700656 }
657
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100658 static void assign(CRITICAL_JNI_PARAMS_COMMA jlong dstPaintHandle, jlong srcPaintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700659 Paint* dst = reinterpret_cast<Paint*>(dstPaintHandle);
660 const Paint* src = reinterpret_cast<Paint*>(srcPaintHandle);
661 *dst = *src;
662 }
663
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100664 static jint getFlags(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500665 uint32_t flags = reinterpret_cast<Paint*>(paintHandle)->getJavaFlags();
666 return static_cast<jint>(flags);
John Reckf2285972016-09-30 14:10:05 -0700667 }
668
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100669 static void setFlags(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint flags) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500670 reinterpret_cast<Paint*>(paintHandle)->setJavaFlags(flags);
John Reckf2285972016-09-30 14:10:05 -0700671 }
672
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100673 static jint getHinting(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reed03acdb12019-01-26 10:17:31 -0500674 return (SkFontHinting)reinterpret_cast<Paint*>(paintHandle)->getSkFont().getHinting()
Ben Wagner0ab44392019-05-09 18:44:34 -0400675 == SkFontHinting::kNone ? 0 : 1;
John Reckf2285972016-09-30 14:10:05 -0700676 }
677
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100678 static void setHinting(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint mode) {
Mike Reed03acdb12019-01-26 10:17:31 -0500679 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setHinting(
Ben Wagner0ab44392019-05-09 18:44:34 -0400680 mode == 0 ? SkFontHinting::kNone : SkFontHinting::kNormal);
John Reckf2285972016-09-30 14:10:05 -0700681 }
682
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100683 static void setAntiAlias(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean aa) {
John Reckf2285972016-09-30 14:10:05 -0700684 reinterpret_cast<Paint*>(paintHandle)->setAntiAlias(aa);
685 }
686
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100687 static void setLinearText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean linearText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500688 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setLinearMetrics(linearText);
John Reckf2285972016-09-30 14:10:05 -0700689 }
690
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100691 static void setSubpixelText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean subpixelText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500692 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSubpixel(subpixelText);
John Reckf2285972016-09-30 14:10:05 -0700693 }
694
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100695 static void setUnderlineText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean underlineText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500696 reinterpret_cast<Paint*>(paintHandle)->setUnderline(underlineText);
John Reckf2285972016-09-30 14:10:05 -0700697 }
698
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100699 static void setStrikeThruText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean strikeThruText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500700 reinterpret_cast<Paint*>(paintHandle)->setStrikeThru(strikeThruText);
John Reckf2285972016-09-30 14:10:05 -0700701 }
702
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100703 static void setFakeBoldText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean fakeBoldText) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500704 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setEmbolden(fakeBoldText);
John Reckf2285972016-09-30 14:10:05 -0700705 }
706
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100707 static void setFilterBitmap(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean filterBitmap) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400708 reinterpret_cast<Paint*>(paintHandle)->setFilterBitmap(filterBitmap);
John Reckf2285972016-09-30 14:10:05 -0700709 }
710
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100711 static void setDither(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean dither) {
John Reckf2285972016-09-30 14:10:05 -0700712 reinterpret_cast<Paint*>(paintHandle)->setDither(dither);
713 }
714
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100715 static jint getStyle(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700716 Paint* obj = reinterpret_cast<Paint*>(objHandle);
717 return static_cast<jint>(obj->getStyle());
718 }
719
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100720 static void setStyle(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint styleHandle) {
John Reckf2285972016-09-30 14:10:05 -0700721 Paint* obj = reinterpret_cast<Paint*>(objHandle);
722 Paint::Style style = static_cast<Paint::Style>(styleHandle);
723 obj->setStyle(style);
724 }
725
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100726 static void setColorLong(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jlong colorSpaceHandle,
Leon Scroggins III10965f32019-03-12 11:13:27 -0400727 jlong colorLong) {
728 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500729 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500730 reinterpret_cast<Paint*>(paintHandle)->setColor4f(color, cs.get());
731 }
732
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100733 static void setColor(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint color) {
Leon Scroggins III03b3e2362019-03-12 10:15:46 -0400734 reinterpret_cast<Paint*>(paintHandle)->setColor(color);
735 }
736
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100737 static void setAlpha(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint a) {
John Reckf2285972016-09-30 14:10:05 -0700738 reinterpret_cast<Paint*>(paintHandle)->setAlpha(a);
739 }
740
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100741 static jfloat getStrokeWidth(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700742 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeWidth());
743 }
744
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100745 static void setStrokeWidth(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat width) {
John Reckf2285972016-09-30 14:10:05 -0700746 reinterpret_cast<Paint*>(paintHandle)->setStrokeWidth(width);
747 }
748
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100749 static jfloat getStrokeMiter(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700750 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeMiter());
751 }
752
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100753 static void setStrokeMiter(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat miter) {
John Reckf2285972016-09-30 14:10:05 -0700754 reinterpret_cast<Paint*>(paintHandle)->setStrokeMiter(miter);
755 }
756
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100757 static jint getStrokeCap(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700758 Paint* obj = reinterpret_cast<Paint*>(objHandle);
759 return static_cast<jint>(obj->getStrokeCap());
760 }
761
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100762 static void setStrokeCap(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint capHandle) {
John Reckf2285972016-09-30 14:10:05 -0700763 Paint* obj = reinterpret_cast<Paint*>(objHandle);
764 Paint::Cap cap = static_cast<Paint::Cap>(capHandle);
765 obj->setStrokeCap(cap);
766 }
767
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100768 static jint getStrokeJoin(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700769 Paint* obj = reinterpret_cast<Paint*>(objHandle);
770 return static_cast<jint>(obj->getStrokeJoin());
771 }
772
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100773 static void setStrokeJoin(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint joinHandle) {
John Reckf2285972016-09-30 14:10:05 -0700774 Paint* obj = reinterpret_cast<Paint*>(objHandle);
775 Paint::Join join = (Paint::Join) joinHandle;
776 obj->setStrokeJoin(join);
777 }
778
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100779 static jboolean getFillPath(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong srcHandle, jlong dstHandle) {
John Reckf2285972016-09-30 14:10:05 -0700780 Paint* obj = reinterpret_cast<Paint*>(objHandle);
781 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
782 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
783 return obj->getFillPath(*src, dst) ? JNI_TRUE : JNI_FALSE;
784 }
785
Nader Jawad5bed1f52020-09-25 00:27:29 -0700786 static jlong setShader(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong shaderHandle) {
787 Paint* obj = reinterpret_cast<Paint*>(objHandle);
788 SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle);
789 obj->setShader(sk_ref_sp(shader));
790 return reinterpret_cast<jlong>(obj->getShader());
John Reckf2285972016-09-30 14:10:05 -0700791 }
792
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100793 static jlong setColorFilter(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong filterHandle) {
John Reckf2285972016-09-30 14:10:05 -0700794 Paint* obj = reinterpret_cast<Paint *>(objHandle);
795 SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(filterHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400796 obj->setColorFilter(sk_ref_sp(filter));
797 return reinterpret_cast<jlong>(obj->getColorFilter());
John Reckf2285972016-09-30 14:10:05 -0700798 }
799
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100800 static void setXfermode(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint xfermodeHandle) {
John Reckf2285972016-09-30 14:10:05 -0700801 // validate that the Java enum values match our expectations
Mike Reed260ab722016-10-07 15:59:20 -0400802 static_assert(0 == static_cast<int>(SkBlendMode::kClear), "xfermode_mismatch");
803 static_assert(1 == static_cast<int>(SkBlendMode::kSrc), "xfermode_mismatch");
804 static_assert(2 == static_cast<int>(SkBlendMode::kDst), "xfermode_mismatch");
805 static_assert(3 == static_cast<int>(SkBlendMode::kSrcOver), "xfermode_mismatch");
806 static_assert(4 == static_cast<int>(SkBlendMode::kDstOver), "xfermode_mismatch");
807 static_assert(5 == static_cast<int>(SkBlendMode::kSrcIn), "xfermode_mismatch");
808 static_assert(6 == static_cast<int>(SkBlendMode::kDstIn), "xfermode_mismatch");
809 static_assert(7 == static_cast<int>(SkBlendMode::kSrcOut), "xfermode_mismatch");
810 static_assert(8 == static_cast<int>(SkBlendMode::kDstOut), "xfermode_mismatch");
811 static_assert(9 == static_cast<int>(SkBlendMode::kSrcATop), "xfermode_mismatch");
812 static_assert(10 == static_cast<int>(SkBlendMode::kDstATop), "xfermode_mismatch");
813 static_assert(11 == static_cast<int>(SkBlendMode::kXor), "xfermode_mismatch");
Nader Jawad55e49d82018-11-16 11:22:32 -0800814 static_assert(12 == static_cast<int>(SkBlendMode::kPlus), "xfermode_mismatch");
Mike Reed260ab722016-10-07 15:59:20 -0400815 static_assert(13 == static_cast<int>(SkBlendMode::kModulate), "xfermode_mismatch");
816 static_assert(14 == static_cast<int>(SkBlendMode::kScreen), "xfermode_mismatch");
Mike Reed260ab722016-10-07 15:59:20 -0400817 static_assert(15 == static_cast<int>(SkBlendMode::kOverlay), "xfermode_mismatch");
Nader Jawad55e49d82018-11-16 11:22:32 -0800818 static_assert(16 == static_cast<int>(SkBlendMode::kDarken), "xfermode_mismatch");
819 static_assert(17 == static_cast<int>(SkBlendMode::kLighten), "xfermode_mismatch");
820 static_assert(18 == static_cast<int>(SkBlendMode::kColorDodge), "xfermode mismatch");
821 static_assert(19 == static_cast<int>(SkBlendMode::kColorBurn), "xfermode mismatch");
822 static_assert(20 == static_cast<int>(SkBlendMode::kHardLight), "xfermode mismatch");
823 static_assert(21 == static_cast<int>(SkBlendMode::kSoftLight), "xfermode mismatch");
824 static_assert(22 == static_cast<int>(SkBlendMode::kDifference), "xfermode mismatch");
825 static_assert(23 == static_cast<int>(SkBlendMode::kExclusion), "xfermode mismatch");
826 static_assert(24 == static_cast<int>(SkBlendMode::kMultiply), "xfermode mismatch");
827 static_assert(25 == static_cast<int>(SkBlendMode::kHue), "xfermode mismatch");
828 static_assert(26 == static_cast<int>(SkBlendMode::kSaturation), "xfermode mismatch");
829 static_assert(27 == static_cast<int>(SkBlendMode::kColor), "xfermode mismatch");
830 static_assert(28 == static_cast<int>(SkBlendMode::kLuminosity), "xfermode mismatch");
John Reckf2285972016-09-30 14:10:05 -0700831
Mike Reed260ab722016-10-07 15:59:20 -0400832 SkBlendMode mode = static_cast<SkBlendMode>(xfermodeHandle);
John Reckf2285972016-09-30 14:10:05 -0700833 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400834 paint->setBlendMode(mode);
John Reckf2285972016-09-30 14:10:05 -0700835 }
836
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100837 static jlong setPathEffect(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong effectHandle) {
John Reckf2285972016-09-30 14:10:05 -0700838 Paint* obj = reinterpret_cast<Paint*>(objHandle);
839 SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400840 obj->setPathEffect(sk_ref_sp(effect));
841 return reinterpret_cast<jlong>(obj->getPathEffect());
John Reckf2285972016-09-30 14:10:05 -0700842 }
843
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100844 static jlong setMaskFilter(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong maskfilterHandle) {
John Reckf2285972016-09-30 14:10:05 -0700845 Paint* obj = reinterpret_cast<Paint*>(objHandle);
846 SkMaskFilter* maskfilter = reinterpret_cast<SkMaskFilter*>(maskfilterHandle);
Mike Reed260ab722016-10-07 15:59:20 -0400847 obj->setMaskFilter(sk_ref_sp(maskfilter));
848 return reinterpret_cast<jlong>(obj->getMaskFilter());
John Reckf2285972016-09-30 14:10:05 -0700849 }
850
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100851 static void setTypeface(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong typefaceHandle) {
Seigo Nonaka318ca042017-08-01 16:36:18 -0700852 Paint* paint = reinterpret_cast<Paint*>(objHandle);
853 paint->setAndroidTypeface(reinterpret_cast<Typeface*>(typefaceHandle));
John Reckf2285972016-09-30 14:10:05 -0700854 }
855
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100856 static jint getTextAlign(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
John Reckf2285972016-09-30 14:10:05 -0700857 Paint* obj = reinterpret_cast<Paint*>(objHandle);
858 return static_cast<jint>(obj->getTextAlign());
859 }
860
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100861 static void setTextAlign(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint alignHandle) {
John Reckf2285972016-09-30 14:10:05 -0700862 Paint* obj = reinterpret_cast<Paint*>(objHandle);
863 Paint::Align align = static_cast<Paint::Align>(alignHandle);
864 obj->setTextAlign(align);
865 }
866
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100867 static void setTextLocalesByMinikinLocaleListId(CRITICAL_JNI_PARAMS_COMMA jlong objHandle,
Seigo Nonaka20866c12017-10-26 16:02:01 -0700868 jint minikinLocaleListId) {
John Reckf2285972016-09-30 14:10:05 -0700869 Paint* obj = reinterpret_cast<Paint*>(objHandle);
Seigo Nonaka20866c12017-10-26 16:02:01 -0700870 obj->setMinikinLocaleListId(minikinLocaleListId);
John Reckf2285972016-09-30 14:10:05 -0700871 }
872
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100873 static jboolean isElegantTextHeight(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700874 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700875 return obj->getFamilyVariant() == minikin::FamilyVariant::ELEGANT;
John Reckf2285972016-09-30 14:10:05 -0700876 }
877
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100878 static void setElegantTextHeight(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean aa) {
John Reckf2285972016-09-30 14:10:05 -0700879 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakac52075e2017-11-17 15:40:06 -0800880 obj->setFamilyVariant(
Seigo Nonaka0ca492f2018-05-25 14:52:22 -0700881 aa ? minikin::FamilyVariant::ELEGANT : minikin::FamilyVariant::DEFAULT);
John Reckf2285972016-09-30 14:10:05 -0700882 }
883
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100884 static jfloat getTextSize(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500885 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize());
John Reckf2285972016-09-30 14:10:05 -0700886 }
887
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100888 static void setTextSize(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat textSize) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500889 if (textSize >= 0) {
890 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSize(textSize);
891 }
John Reckf2285972016-09-30 14:10:05 -0700892 }
893
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100894 static jfloat getTextScaleX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500895 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getScaleX());
John Reckf2285972016-09-30 14:10:05 -0700896 }
897
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100898 static void setTextScaleX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat scaleX) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500899 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setScaleX(scaleX);
John Reckf2285972016-09-30 14:10:05 -0700900 }
901
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100902 static jfloat getTextSkewX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500903 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSkewX());
John Reckf2285972016-09-30 14:10:05 -0700904 }
905
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100906 static void setTextSkewX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat skewX) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500907 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSkewX(skewX);
John Reckf2285972016-09-30 14:10:05 -0700908 }
909
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100910 static jfloat getLetterSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -0700911 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
912 return paint->getLetterSpacing();
913 }
914
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100915 static void setLetterSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat letterSpacing) {
John Reckf2285972016-09-30 14:10:05 -0700916 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
917 paint->setLetterSpacing(letterSpacing);
918 }
919
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100920 static jfloat getWordSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Seigo Nonaka219e2c792016-11-15 19:01:45 +0900921 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
922 return paint->getWordSpacing();
923 }
924
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100925 static void setWordSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat wordSpacing) {
Seigo Nonaka219e2c792016-11-15 19:01:45 +0900926 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
927 paint->setWordSpacing(wordSpacing);
928 }
929
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100930 static jint getStartHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
John Reckf2285972016-09-30 14:10:05 -0700931 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800932 return static_cast<jint>(paint->getStartHyphenEdit());
John Reckf2285972016-09-30 14:10:05 -0700933 }
934
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100935 static jint getEndHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
John Reckf2285972016-09-30 14:10:05 -0700936 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800937 return static_cast<jint>(paint->getEndHyphenEdit());
938 }
939
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100940 static void setStartHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800941 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
942 paint->setStartHyphenEdit((uint32_t)hyphen);
943 }
944
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100945 static void setEndHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
Seigo Nonakafb1b4792019-03-08 14:05:08 -0800946 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
947 paint->setEndHyphenEdit((uint32_t)hyphen);
John Reckf2285972016-09-30 14:10:05 -0700948 }
949
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100950 static jfloat ascent(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500951 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700952 getMetricsInternal(paintHandle, &metrics);
John Reckf2285972016-09-30 14:10:05 -0700953 return SkScalarToFloat(metrics.fAscent);
954 }
955
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100956 static jfloat descent(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500957 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700958 getMetricsInternal(paintHandle, &metrics);
John Reckf2285972016-09-30 14:10:05 -0700959 return SkScalarToFloat(metrics.fDescent);
960 }
961
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100962 static jfloat getUnderlinePosition(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500963 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700964 getMetricsInternal(paintHandle, &metrics);
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700965 SkScalar position;
966 if (metrics.hasUnderlinePosition(&position)) {
967 return SkScalarToFloat(position);
968 } else {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500969 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700970 return SkScalarToFloat(Paint::kStdUnderline_Top * textSize);
971 }
972 }
973
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100974 static jfloat getUnderlineThickness(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedda3488a2018-12-14 12:08:55 -0500975 SkFontMetrics metrics;
Seigo Nonaka318ca042017-08-01 16:36:18 -0700976 getMetricsInternal(paintHandle, &metrics);
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700977 SkScalar thickness;
978 if (metrics.hasUnderlineThickness(&thickness)) {
979 return SkScalarToFloat(thickness);
980 } else {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500981 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournaderca8a04a2017-06-06 18:30:29 -0700982 return SkScalarToFloat(Paint::kStdUnderline_Thickness * textSize);
983 }
984 }
985
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100986 static jfloat getStrikeThruPosition(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500987 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournader1378a9d2017-07-13 12:45:20 -0700988 return SkScalarToFloat(Paint::kStdStrikeThru_Top * textSize);
989 }
990
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100991 static jfloat getStrikeThruThickness(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500992 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
Roozbeh Pournader1378a9d2017-07-13 12:45:20 -0700993 return SkScalarToFloat(Paint::kStdStrikeThru_Thickness * textSize);
994 }
995
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100996 static void setShadowLayer(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat radius,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500997 jfloat dx, jfloat dy, jlong colorSpaceHandle,
Leon Scroggins III10965f32019-03-12 11:13:27 -0400998 jlong colorLong) {
999 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
Leon Scroggins III0e443d12018-12-19 11:38:35 -05001000 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Leon Scroggins III0e443d12018-12-19 11:38:35 -05001001
1002 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
1003 if (radius <= 0) {
1004 paint->setLooper(nullptr);
1005 }
1006 else {
1007 SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius);
Mike Reed0f9dce72021-02-12 21:20:33 -05001008 paint->setLooper(BlurDrawLooper::Make(color, cs.get(), sigma, {dx, dy}));
Leon Scroggins III0e443d12018-12-19 11:38:35 -05001009 }
1010 }
1011
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +01001012 static jboolean hasShadowLayer(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
John Reckf2285972016-09-30 14:10:05 -07001013 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
Mike Reed0f9dce72021-02-12 21:20:33 -05001014 return paint->getLooper() != nullptr;
John Reckf2285972016-09-30 14:10:05 -07001015 }
1016
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +01001017 static jboolean equalsForTextMeasurement(CRITICAL_JNI_PARAMS_COMMA jlong lPaint, jlong rPaint) {
Seigo Nonakabeafa1f2018-02-01 21:39:24 -08001018 if (lPaint == rPaint) {
1019 return true;
1020 }
1021 Paint* leftPaint = reinterpret_cast<Paint*>(lPaint);
1022 Paint* rightPaint = reinterpret_cast<Paint*>(rPaint);
1023
1024 const Typeface* leftTypeface = Typeface::resolveDefault(leftPaint->getAndroidTypeface());
1025 const Typeface* rightTypeface = Typeface::resolveDefault(rightPaint->getAndroidTypeface());
1026 minikin::MinikinPaint leftMinikinPaint
1027 = MinikinUtils::prepareMinikinPaint(leftPaint, leftTypeface);
1028 minikin::MinikinPaint rightMinikinPaint
1029 = MinikinUtils::prepareMinikinPaint(rightPaint, rightTypeface);
1030
1031 return leftMinikinPaint == rightMinikinPaint;
1032 }
1033
John Reckdbffd252015-10-01 14:46:12 -07001034}; // namespace PaintGlue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035
Daniel Micay76f6a862015-09-19 17:31:01 -04001036static const JNINativeMethod methods[] = {
Richard Uhler775873a2015-12-29 12:37:39 -08001037 {"nGetNativeFinalizer", "()J", (void*) PaintGlue::getNativeFinalizer},
John Reckdbffd252015-10-01 14:46:12 -07001038 {"nInit","()J", (void*) PaintGlue::init},
1039 {"nInitWithPaint","(J)J", (void*) PaintGlue::initWithPaint},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001040 {"nBreakText","(J[CIIFI[F)I", (void*) PaintGlue::breakTextC},
1041 {"nBreakText","(JLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS},
1042 {"nGetTextAdvances","(J[CIIIII[FI)F",
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -07001043 (void*) PaintGlue::getTextAdvances___CIIIII_FI},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001044 {"nGetTextAdvances","(JLjava/lang/String;IIIII[FI)F",
Keisuke Kuroyanagi536afe62015-09-29 13:52:45 -07001045 (void*) PaintGlue::getTextAdvances__StringIIIII_FI},
Fabrice Di Meglioda12f382013-03-15 11:26:56 -07001046
Seigo Nonaka318ca042017-08-01 16:36:18 -07001047 {"nGetTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C},
1048 {"nGetTextRunCursor", "(JLjava/lang/String;IIIII)I",
Raph Leviena027ec52015-04-06 16:21:59 -07001049 (void*) PaintGlue::getTextRunCursor__String},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001050 {"nGetTextPath", "(JI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C},
1051 {"nGetTextPath", "(JILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String},
1052 {"nGetStringBounds", "(JLjava/lang/String;IIILandroid/graphics/Rect;)V",
Raph Leviena027ec52015-04-06 16:21:59 -07001053 (void*) PaintGlue::getStringBounds },
Seigo Nonaka318ca042017-08-01 16:36:18 -07001054 {"nGetCharArrayBounds", "(J[CIIILandroid/graphics/Rect;)V",
Raph Leviena027ec52015-04-06 16:21:59 -07001055 (void*) PaintGlue::getCharArrayBounds },
Seigo Nonaka318ca042017-08-01 16:36:18 -07001056 {"nHasGlyph", "(JILjava/lang/String;)Z", (void*) PaintGlue::hasGlyph },
1057 {"nGetRunAdvance", "(J[CIIIIZI)F", (void*) PaintGlue::getRunAdvance___CIIIIZI_F},
1058 {"nGetOffsetForAdvance", "(J[CIIIIZF)I",
Raph Leviena027ec52015-04-06 16:21:59 -07001059 (void*) PaintGlue::getOffsetForAdvance___CIIIIZF_I},
Seigo Nonaka78c774d2021-12-10 10:49:43 -08001060 {"nGetFontMetricsIntForText", "(J[CIIIIZLandroid/graphics/Paint$FontMetricsInt;)V",
1061 (void*)PaintGlue::getFontMetricsIntForText___C},
1062 {"nGetFontMetricsIntForText",
1063 "(JLjava/lang/String;IIIIZLandroid/graphics/Paint$FontMetricsInt;)V",
1064 (void*)PaintGlue::getFontMetricsIntForText___String},
Chris Craik4136a0a2014-10-07 15:09:46 -07001065
John Reckf2285972016-09-30 14:10:05 -07001066 // --------------- @FastNative ----------------------
1067
1068 {"nSetTextLocales","(JLjava/lang/String;)I", (void*) PaintGlue::setTextLocales},
1069 {"nSetFontFeatureSettings","(JLjava/lang/String;)V",
1070 (void*) PaintGlue::setFontFeatureSettings},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001071 {"nGetFontMetrics", "(JLandroid/graphics/Paint$FontMetrics;)F",
John Reckf2285972016-09-30 14:10:05 -07001072 (void*)PaintGlue::getFontMetrics},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001073 {"nGetFontMetricsInt", "(JLandroid/graphics/Paint$FontMetricsInt;)I",
John Reckf2285972016-09-30 14:10:05 -07001074 (void*)PaintGlue::getFontMetricsInt},
1075
1076 // --------------- @CriticalNative ------------------
1077
1078 {"nReset","(J)V", (void*) PaintGlue::reset},
1079 {"nSet","(JJ)V", (void*) PaintGlue::assign},
1080 {"nGetFlags","(J)I", (void*) PaintGlue::getFlags},
1081 {"nSetFlags","(JI)V", (void*) PaintGlue::setFlags},
1082 {"nGetHinting","(J)I", (void*) PaintGlue::getHinting},
1083 {"nSetHinting","(JI)V", (void*) PaintGlue::setHinting},
1084 {"nSetAntiAlias","(JZ)V", (void*) PaintGlue::setAntiAlias},
1085 {"nSetSubpixelText","(JZ)V", (void*) PaintGlue::setSubpixelText},
1086 {"nSetLinearText","(JZ)V", (void*) PaintGlue::setLinearText},
1087 {"nSetUnderlineText","(JZ)V", (void*) PaintGlue::setUnderlineText},
1088 {"nSetStrikeThruText","(JZ)V", (void*) PaintGlue::setStrikeThruText},
1089 {"nSetFakeBoldText","(JZ)V", (void*) PaintGlue::setFakeBoldText},
1090 {"nSetFilterBitmap","(JZ)V", (void*) PaintGlue::setFilterBitmap},
1091 {"nSetDither","(JZ)V", (void*) PaintGlue::setDither},
1092 {"nGetStyle","(J)I", (void*) PaintGlue::getStyle},
1093 {"nSetStyle","(JI)V", (void*) PaintGlue::setStyle},
Leon Scroggins III03b3e2362019-03-12 10:15:46 -04001094 {"nSetColor","(JI)V", (void*) PaintGlue::setColor},
Leon Scroggins III10965f32019-03-12 11:13:27 -04001095 {"nSetColor","(JJJ)V", (void*) PaintGlue::setColorLong},
John Reckf2285972016-09-30 14:10:05 -07001096 {"nSetAlpha","(JI)V", (void*) PaintGlue::setAlpha},
1097 {"nGetStrokeWidth","(J)F", (void*) PaintGlue::getStrokeWidth},
1098 {"nSetStrokeWidth","(JF)V", (void*) PaintGlue::setStrokeWidth},
1099 {"nGetStrokeMiter","(J)F", (void*) PaintGlue::getStrokeMiter},
1100 {"nSetStrokeMiter","(JF)V", (void*) PaintGlue::setStrokeMiter},
1101 {"nGetStrokeCap","(J)I", (void*) PaintGlue::getStrokeCap},
1102 {"nSetStrokeCap","(JI)V", (void*) PaintGlue::setStrokeCap},
1103 {"nGetStrokeJoin","(J)I", (void*) PaintGlue::getStrokeJoin},
1104 {"nSetStrokeJoin","(JI)V", (void*) PaintGlue::setStrokeJoin},
1105 {"nGetFillPath","(JJJ)Z", (void*) PaintGlue::getFillPath},
Nader Jawad5bed1f52020-09-25 00:27:29 -07001106 {"nSetShader","(JJ)J", (void*) PaintGlue::setShader},
John Reckf2285972016-09-30 14:10:05 -07001107 {"nSetColorFilter","(JJ)J", (void*) PaintGlue::setColorFilter},
1108 {"nSetXfermode","(JI)V", (void*) PaintGlue::setXfermode},
1109 {"nSetPathEffect","(JJ)J", (void*) PaintGlue::setPathEffect},
1110 {"nSetMaskFilter","(JJ)J", (void*) PaintGlue::setMaskFilter},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001111 {"nSetTypeface","(JJ)V", (void*) PaintGlue::setTypeface},
John Reckf2285972016-09-30 14:10:05 -07001112 {"nGetTextAlign","(J)I", (void*) PaintGlue::getTextAlign},
1113 {"nSetTextAlign","(JI)V", (void*) PaintGlue::setTextAlign},
Seigo Nonaka20866c12017-10-26 16:02:01 -07001114 {"nSetTextLocalesByMinikinLocaleListId","(JI)V",
1115 (void*) PaintGlue::setTextLocalesByMinikinLocaleListId},
John Reckf2285972016-09-30 14:10:05 -07001116 {"nIsElegantTextHeight","(J)Z", (void*) PaintGlue::isElegantTextHeight},
1117 {"nSetElegantTextHeight","(JZ)V", (void*) PaintGlue::setElegantTextHeight},
1118 {"nGetTextSize","(J)F", (void*) PaintGlue::getTextSize},
1119 {"nSetTextSize","(JF)V", (void*) PaintGlue::setTextSize},
1120 {"nGetTextScaleX","(J)F", (void*) PaintGlue::getTextScaleX},
1121 {"nSetTextScaleX","(JF)V", (void*) PaintGlue::setTextScaleX},
1122 {"nGetTextSkewX","(J)F", (void*) PaintGlue::getTextSkewX},
1123 {"nSetTextSkewX","(JF)V", (void*) PaintGlue::setTextSkewX},
1124 {"nGetLetterSpacing","(J)F", (void*) PaintGlue::getLetterSpacing},
1125 {"nSetLetterSpacing","(JF)V", (void*) PaintGlue::setLetterSpacing},
Seigo Nonaka219e2c792016-11-15 19:01:45 +09001126 {"nGetWordSpacing","(J)F", (void*) PaintGlue::getWordSpacing},
1127 {"nSetWordSpacing","(JF)V", (void*) PaintGlue::setWordSpacing},
Seigo Nonakafb1b4792019-03-08 14:05:08 -08001128 {"nGetStartHyphenEdit", "(J)I", (void*) PaintGlue::getStartHyphenEdit},
1129 {"nGetEndHyphenEdit", "(J)I", (void*) PaintGlue::getEndHyphenEdit},
1130 {"nSetStartHyphenEdit", "(JI)V", (void*) PaintGlue::setStartHyphenEdit},
1131 {"nSetEndHyphenEdit", "(JI)V", (void*) PaintGlue::setEndHyphenEdit},
Seigo Nonaka318ca042017-08-01 16:36:18 -07001132 {"nAscent","(J)F", (void*) PaintGlue::ascent},
1133 {"nDescent","(J)F", (void*) PaintGlue::descent},
1134 {"nGetUnderlinePosition","(J)F", (void*) PaintGlue::getUnderlinePosition},
1135 {"nGetUnderlineThickness","(J)F", (void*) PaintGlue::getUnderlineThickness},
1136 {"nGetStrikeThruPosition","(J)F", (void*) PaintGlue::getStrikeThruPosition},
1137 {"nGetStrikeThruThickness","(J)F", (void*) PaintGlue::getStrikeThruThickness},
Leon Scroggins III10965f32019-03-12 11:13:27 -04001138 {"nSetShadowLayer", "(JFFFJJ)V", (void*)PaintGlue::setShadowLayer},
Seigo Nonakabeafa1f2018-02-01 21:39:24 -08001139 {"nHasShadowLayer", "(J)Z", (void*)PaintGlue::hasShadowLayer},
1140 {"nEqualsForTextMeasurement", "(JJ)Z", (void*)PaintGlue::equalsForTextMeasurement},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141};
1142
Seigo Nonaka78c774d2021-12-10 10:49:43 -08001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144int register_android_graphics_Paint(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001145 return RegisterMethodsOrDie(env, "android/graphics/Paint", methods, NELEM(methods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146}
1147
1148}