Debian Codegraph @

libs/hwui/jni/Paint.cpp

raw symbols SCIP
1 /* libs/android_runtime/android/graphics/Paint.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** 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
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** 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
15 ** limitations under the License.
16 */
17
18 #undef LOG_TAG
19 #define LOG_TAG "Paint"
20
21 #include <utils/Log.h>
22
23 #include "GraphicsJNI.h"
24 #include <nativehelper/ScopedStringChars.h>
25 #include <nativehelper/ScopedUtfChars.h>
26 #include <nativehelper/ScopedPrimitiveArray.h>
27
28 #include "SkColorFilter.h"
29 #include "SkFont.h"
30 #include "SkFontMetrics.h"
31 #include "SkFontTypes.h"
32 #include "SkMaskFilter.h"
33 #include "SkPath.h"
34 #include "SkPathEffect.h"
35 #include "SkShader.h"
36 #include "SkBlendMode.h"
37 #include "unicode/uloc.h"
38 #include "utils/Blur.h"
39
40 #include <hwui/BlurDrawLooper.h>
41 #include <hwui/MinikinSkia.h>
42 #include <hwui/MinikinUtils.h>
43 #include <hwui/Paint.h>
44 #include <hwui/Typeface.h>
45 #include <minikin/GraphemeBreak.h>
46 #include <minikin/LocaleList.h>
47 #include <minikin/Measurement.h>
48 #include <minikin/MinikinPaint.h>
49 #include <unicode/utf16.h>
50
51 #include <cassert>
52 #include <cstring>
53 #include <memory>
54 #include <vector>
55
56 namespace android {
57
58 static void getPosTextPath(const SkFont& font, const uint16_t glyphs[], int count,
59 const SkPoint pos[], SkPath* dst) {
60 dst->reset();
61 struct Rec {
62 SkPath* fDst;
63 const SkPoint* fPos;
64 } rec = { dst, pos };
65 font.getPaths(glyphs, count, [](const SkPath* src, const SkMatrix& mx, void* ctx) {
66 Rec* rec = (Rec*)ctx;
67 if (src) {
68 SkMatrix tmp(mx);
69 tmp.postTranslate(rec->fPos->fX, rec->fPos->fY);
70 rec->fDst->addPath(*src, tmp);
71 }
72 rec->fPos += 1;
73 }, &rec);
74 }
75
76 namespace PaintGlue {
77 enum MoveOpt {
78 AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT
79 };
80
81 static void deletePaint(Paint* paint) {
82 delete paint;
83 }
84
85 static jlong getNativeFinalizer(JNIEnv*, jobject) {
86 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&deletePaint));
87 }
88
89 static jlong init(JNIEnv* env, jobject) {
90 return reinterpret_cast<jlong>(new Paint);
91 }
92
93 static jlong initWithPaint(JNIEnv* env, jobject clazz, jlong paintHandle) {
94 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
95 Paint* obj = new Paint(*paint);
96 return reinterpret_cast<jlong>(obj);
97 }
98
99 static int breakText(JNIEnv* env, const Paint& paint, const Typeface* typeface,
100 const jchar text[], int count, float maxWidth, jint bidiFlags, jfloatArray jmeasured,
101 const bool forwardScan) {
102 size_t measuredCount = 0;
103 float measured = 0;
104
105 std::unique_ptr<float[]> advancesArray(new float[count]);
106 MinikinUtils::measureText(&paint, static_cast<minikin::Bidi>(bidiFlags), typeface, text,
107 0, count, count, advancesArray.get());
108
109 for (int i = 0; i < count; i++) {
110 // traverse in the given direction
111 int index = forwardScan ? i : (count - i - 1);
112 float width = advancesArray[index];
113 if (measured + width > maxWidth) {
114 break;
115 }
116 // properly handle clusters when scanning backwards
117 if (forwardScan || width != 0.0f) {
118 measuredCount = i + 1;
119 }
120 measured += width;
121 }
122
123 if (jmeasured && env->GetArrayLength(jmeasured) > 0) {
124 AutoJavaFloatArray autoMeasured(env, jmeasured, 1);
125 jfloat* array = autoMeasured.ptr();
126 array[0] = measured;
127 }
128 return measuredCount;
129 }
130
131 static jint breakTextC(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray jtext,
132 jint index, jint count, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) {
133 NPE_CHECK_RETURN_ZERO(env, jtext);
134
135 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
136 const Typeface* typeface = paint->getAndroidTypeface();
137
138 bool forwardTextDirection;
139 if (count < 0) {
140 forwardTextDirection = false;
141 count = -count;
142 }
143 else {
144 forwardTextDirection = true;
145 }
146
147 if ((index < 0) || (index + count > env->GetArrayLength(jtext))) {
148 doThrowAIOOBE(env);
149 return 0;
150 }
151
152 const jchar* text = env->GetCharArrayElements(jtext, nullptr);
153 count = breakText(env, *paint, typeface, text + index, count, maxWidth,
154 bidiFlags, jmeasuredWidth, forwardTextDirection);
155 env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text),
156 JNI_ABORT);
157 return count;
158 }
159
160 static jint breakTextS(JNIEnv* env, jobject clazz, jlong paintHandle, jstring jtext,
161 jboolean forwards, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) {
162 NPE_CHECK_RETURN_ZERO(env, jtext);
163
164 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
165 const Typeface* typeface = paint->getAndroidTypeface();
166
167 int count = env->GetStringLength(jtext);
168 const jchar* text = env->GetStringChars(jtext, nullptr);
169 count = breakText(env, *paint, typeface, text, count, maxWidth, bidiFlags, jmeasuredWidth, forwards);
170 env->ReleaseStringChars(jtext, text);
171 return count;
172 }
173
174 static jfloat doTextAdvances(JNIEnv *env, Paint *paint, const Typeface* typeface,
175 const jchar *text, jint start, jint count, jint contextCount, jint bidiFlags,
176 jfloatArray advances, jint advancesIndex) {
177 NPE_CHECK_RETURN_ZERO(env, text);
178
179 if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) {
180 doThrowAIOOBE(env);
181 return 0;
182 }
183 if (count == 0) {
184 return 0;
185 }
186 if (advances) {
187 size_t advancesLength = env->GetArrayLength(advances);
188 if ((size_t)(count + advancesIndex) > advancesLength) {
189 doThrowAIOOBE(env);
190 return 0;
191 }
192 }
193 std::unique_ptr<jfloat[]> advancesArray;
194 if (advances) {
195 advancesArray.reset(new jfloat[count]);
196 }
197 const float advance = MinikinUtils::measureText(paint,
198 static_cast<minikin::Bidi>(bidiFlags), typeface, text, start, count, contextCount,
199 advancesArray.get());
200 if (advances) {
201 env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray.get());
202 }
203 return advance;
204 }
205
206 static jfloat getTextAdvances___CIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle,
207 jcharArray text, jint index, jint count, jint contextIndex, jint contextCount,
208 jint bidiFlags, jfloatArray advances, jint advancesIndex) {
209 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
210 const Typeface* typeface = paint->getAndroidTypeface();
211 jchar* textArray = env->GetCharArrayElements(text, nullptr);
212 jfloat result = doTextAdvances(env, paint, typeface, textArray + contextIndex,
213 index - contextIndex, count, contextCount, bidiFlags, advances, advancesIndex);
214 env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
215 return result;
216 }
217
218 static jfloat getTextAdvances__StringIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle,
219 jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint bidiFlags,
220 jfloatArray advances, jint advancesIndex) {
221 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
222 const Typeface* typeface = paint->getAndroidTypeface();
223 const jchar* textArray = env->GetStringChars(text, nullptr);
224 jfloat result = doTextAdvances(env, paint, typeface, textArray + contextStart,
225 start - contextStart, end - start, contextEnd - contextStart, bidiFlags,
226 advances, advancesIndex);
227 env->ReleaseStringChars(text, textArray);
228 return result;
229 }
230
231 static jint doTextRunCursor(JNIEnv *env, Paint* paint, const Typeface* typeface,
232 const jchar *text, jint start, jint count, jint dir, jint offset, jint opt) {
233 minikin::GraphemeBreak::MoveOpt moveOpt = minikin::GraphemeBreak::MoveOpt(opt);
234 minikin::Bidi bidiFlags = dir == 1 ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
235 std::unique_ptr<float[]> advancesArray(new float[count]);
236 MinikinUtils::measureText(paint, bidiFlags, typeface, text, start, count, start + count,
237 advancesArray.get());
238 size_t result = minikin::GraphemeBreak::getTextRunCursor(advancesArray.get(), text,
239 start, count, offset, moveOpt);
240 return static_cast<jint>(result);
241 }
242
243 static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text,
244 jint contextStart, jint contextCount, jint dir, jint offset, jint cursorOpt) {
245 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
246 const Typeface* typeface = paint->getAndroidTypeface();
247 jchar* textArray = env->GetCharArrayElements(text, nullptr);
248 jint result = doTextRunCursor(env, paint, typeface, textArray,
249 contextStart, contextCount, dir, offset, cursorOpt);
250 env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
251 return result;
252 }
253
254 static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, jlong paintHandle,
255 jstring text, jint contextStart, jint contextEnd, jint dir, jint offset,
256 jint cursorOpt) {
257 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
258 const Typeface* typeface = paint->getAndroidTypeface();
259 const jchar* textArray = env->GetStringChars(text, nullptr);
260 jint result = doTextRunCursor(env, paint, typeface, textArray,
261 contextStart, contextEnd - contextStart, dir, offset, cursorOpt);
262 env->ReleaseStringChars(text, textArray);
263 return result;
264 }
265
266 class GetTextFunctor {
267 public:
268 GetTextFunctor(const minikin::Layout& layout, SkPath* path, jfloat x, jfloat y,
269 Paint* paint, uint16_t* glyphs, SkPoint* pos)
270 : layout(layout), path(path), x(x), y(y), paint(paint), glyphs(glyphs), pos(pos) {
271 }
272
273 void operator()(size_t start, size_t end) {
274 for (size_t i = start; i < end; i++) {
275 glyphs[i] = layout.getGlyphId(i);
276 pos[i].fX = x + layout.getX(i);
277 pos[i].fY = y + layout.getY(i);
278 }
279 const SkFont& font = paint->getSkFont();
280 if (start == 0) {
281 getPosTextPath(font, glyphs, end, pos, path);
282 } else {
283 getPosTextPath(font, glyphs + start, end - start, pos + start, &tmpPath);
284 path->addPath(tmpPath);
285 }
286 }
287 private:
288 const minikin::Layout& layout;
289 SkPath* path;
290 jfloat x;
291 jfloat y;
292 Paint* paint;
293 uint16_t* glyphs;
294 SkPoint* pos;
295 SkPath tmpPath;
296 };
297
298 static void getTextPath(JNIEnv* env, Paint* paint, const Typeface* typeface, const jchar* text,
299 jint count, jint bidiFlags, jfloat x, jfloat y, SkPath* path) {
300 minikin::Layout layout = MinikinUtils::doLayout(
301 paint, static_cast<minikin::Bidi>(bidiFlags), typeface,
302 text, count, // text buffer
303 0, count, // draw range
304 0, count, // context range
305 nullptr);
306 size_t nGlyphs = layout.nGlyphs();
307 uint16_t* glyphs = new uint16_t[nGlyphs];
308 SkPoint* pos = new SkPoint[nGlyphs];
309
310 x += MinikinUtils::xOffsetForTextAlign(paint, layout);
311 Paint::Align align = paint->getTextAlign();
312 paint->setTextAlign(Paint::kLeft_Align);
313 GetTextFunctor f(layout, path, x, y, paint, glyphs, pos);
314 MinikinUtils::forFontRun(layout, paint, f);
315 paint->setTextAlign(align);
316 delete[] glyphs;
317 delete[] pos;
318 }
319
320 static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle, jint bidiFlags,
321 jcharArray text, jint index, jint count, jfloat x, jfloat y, jlong pathHandle) {
322 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
323 const Typeface* typeface = paint->getAndroidTypeface();
324 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
325 const jchar* textArray = env->GetCharArrayElements(text, nullptr);
326 getTextPath(env, paint, typeface, textArray + index, count, bidiFlags, x, y, path);
327 env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
328 }
329
330 static void getTextPath__String(JNIEnv* env, jobject clazz, jlong paintHandle, jint bidiFlags,
331 jstring text, jint start, jint end, jfloat x, jfloat y, jlong pathHandle) {
332 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
333 const Typeface* typeface = paint->getAndroidTypeface();
334 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
335 const jchar* textArray = env->GetStringChars(text, nullptr);
336 getTextPath(env, paint, typeface, textArray + start, end - start, bidiFlags, x, y, path);
337 env->ReleaseStringChars(text, textArray);
338 }
339
340 static void doTextBounds(JNIEnv* env, const jchar* text, int count, jobject bounds,
341 const Paint& paint, const Typeface* typeface, jint bidiFlagsInt) {
342 SkRect r;
343 SkIRect ir;
344
345 minikin::MinikinRect rect;
346 minikin::Bidi bidiFlags = static_cast<minikin::Bidi>(bidiFlagsInt);
347 MinikinUtils::getBounds(&paint, bidiFlags, typeface, text, count, &rect);
348 r.fLeft = rect.mLeft;
349 r.fTop = rect.mTop;
350 r.fRight = rect.mRight;
351 r.fBottom = rect.mBottom;
352 r.roundOut(&ir);
353 GraphicsJNI::irect_to_jrect(ir, env, bounds);
354 }
355
356 static void getStringBounds(JNIEnv* env, jobject, jlong paintHandle, jstring text, jint start,
357 jint end, jint bidiFlags, jobject bounds) {
358 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
359 const Typeface* typeface = paint->getAndroidTypeface();
360 const jchar* textArray = env->GetStringChars(text, nullptr);
361 doTextBounds(env, textArray + start, end - start, bounds, *paint, typeface, bidiFlags);
362 env->ReleaseStringChars(text, textArray);
363 }
364
365 static void getCharArrayBounds(JNIEnv* env, jobject, jlong paintHandle, jcharArray text,
366 jint index, jint count, jint bidiFlags, jobject bounds) {
367 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
368 const Typeface* typeface = paint->getAndroidTypeface();
369 const jchar* textArray = env->GetCharArrayElements(text, nullptr);
370 doTextBounds(env, textArray + index, count, bounds, *paint, typeface, bidiFlags);
371 env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray),
372 JNI_ABORT);
373 }
374
375 // Returns true if the given string is exact one pair of regional indicators.
376 static bool isFlag(const jchar* str, size_t length) {
377 const jchar RI_LEAD_SURROGATE = 0xD83C;
378 const jchar RI_TRAIL_SURROGATE_MIN = 0xDDE6;
379 const jchar RI_TRAIL_SURROGATE_MAX = 0xDDFF;
380
381 if (length != 4) {
382 return false;
383 }
384 if (str[0] != RI_LEAD_SURROGATE || str[2] != RI_LEAD_SURROGATE) {
385 return false;
386 }
387 return RI_TRAIL_SURROGATE_MIN <= str[1] && str[1] <= RI_TRAIL_SURROGATE_MAX &&
388 RI_TRAIL_SURROGATE_MIN <= str[3] && str[3] <= RI_TRAIL_SURROGATE_MAX;
389 }
390
391 static jboolean layoutContainsNotdef(const minikin::Layout& layout) {
392 for (size_t i = 0; i < layout.nGlyphs(); i++) {
393 if (layout.getGlyphId(i) == 0) {
394 return true;
395 }
396 }
397 return false;
398 }
399
400 // Don't count glyphs that are the recommended "space" glyph and are zero width.
401 // This logic makes assumptions about HarfBuzz layout, but does correctly handle
402 // cases where ligatures form and zero width space glyphs are left in as
403 // placeholders.
404 static size_t countNonSpaceGlyphs(const minikin::Layout& layout) {
405 size_t count = 0;
406 static unsigned int kSpaceGlyphId = 3;
407 for (size_t i = 0; i < layout.nGlyphs(); i++) {
408 if (layout.getGlyphId(i) != kSpaceGlyphId || layout.getCharAdvance(i) != 0.0) {
409 count++;
410 }
411 }
412 return count;
413 }
414
415 static jboolean hasGlyph(JNIEnv *env, jclass, jlong paintHandle, jint bidiFlags,
416 jstring string) {
417 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
418 const Typeface* typeface = paint->getAndroidTypeface();
419 ScopedStringChars str(env, string);
420
421 /* Start by rejecting unsupported base code point and variation selector pairs. */
422 size_t nChars = 0;
423 const uint32_t kStartOfString = 0xFFFFFFFF;
424 uint32_t prevCp = kStartOfString;
425 for (size_t i = 0; i < str.size(); i++) {
426 jchar cu = str[i];
427 uint32_t cp = cu;
428 if (U16_IS_TRAIL(cu)) {
429 // invalid UTF-16, unpaired trailing surrogate
430 return false;
431 } else if (U16_IS_LEAD(cu)) {
432 if (i + 1 == str.size()) {
433 // invalid UTF-16, unpaired leading surrogate at end of string
434 return false;
435 }
436 i++;
437 jchar cu2 = str[i];
438 if (!U16_IS_TRAIL(cu2)) {
439 // invalid UTF-16, unpaired leading surrogate
440 return false;
441 }
442 cp = U16_GET_SUPPLEMENTARY(cu, cu2);
443 }
444
445 if (prevCp != kStartOfString &&
446 ((0xFE00 <= cp && cp <= 0xFE0F) || (0xE0100 <= cp && cp <= 0xE01EF))) {
447 bool hasVS = MinikinUtils::hasVariationSelector(typeface, prevCp, cp);
448 if (!hasVS) {
449 // No font has a glyph for the code point and variation selector pair.
450 return false;
451 } else if (nChars == 1 && i + 1 == str.size()) {
452 // The string is just a codepoint and a VS, we have an authoritative answer
453 return true;
454 }
455 }
456 nChars++;
457 prevCp = cp;
458 }
459 minikin::Layout layout = MinikinUtils::doLayout(paint,
460 static_cast<minikin::Bidi>(bidiFlags), typeface,
461 str.get(), str.size(), // text buffer
462 0, str.size(), // draw range
463 0, str.size(), // context range
464 nullptr);
465 size_t nGlyphs = countNonSpaceGlyphs(layout);
466 if (nGlyphs != 1 && nChars > 1) {
467 // multiple-character input, and was not a ligature
468 // TODO: handle ZWJ/ZWNJ characters specially so we can detect certain ligatures
469 // in joining scripts, such as Arabic and Mongolian.
470 return false;
471 }
472
473 if (nGlyphs == 0 || layoutContainsNotdef(layout)) {
474 return false; // The collection doesn't have a glyph.
475 }
476
477 if (nChars == 2 && isFlag(str.get(), str.size())) {
478 // Some font may have a special glyph for unsupported regional indicator pairs.
479 // To return false for this case, need to compare the glyph id with the one of ZZ
480 // since ZZ is reserved for unknown or invalid territory.
481 // U+1F1FF (REGIONAL INDICATOR SYMBOL LETTER Z) is \uD83C\uDDFF in UTF16.
482 static const jchar ZZ_FLAG_STR[] = { 0xD83C, 0xDDFF, 0xD83C, 0xDDFF };
483 minikin::Layout zzLayout = MinikinUtils::doLayout(paint,
484 static_cast<minikin::Bidi>(bidiFlags), typeface,
485 ZZ_FLAG_STR, 4, // text buffer
486 0, 4, // draw range
487 0, 4, // context range
488 nullptr);
489 if (zzLayout.nGlyphs() != 1 || layoutContainsNotdef(zzLayout)) {
490 // The font collection doesn't have a glyph for unknown flag. Just return true.
491 return true;
492 }
493 return zzLayout.getGlyphId(0) != layout.getGlyphId(0);
494 }
495 return true;
496 }
497
498 static jfloat doRunAdvance(const Paint* paint, const Typeface* typeface, const jchar buf[],
499 jint start, jint count, jint bufSize, jboolean isRtl, jint offset) {
500 minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
501 if (offset == start + count) {
502 return MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count,
503 bufSize, nullptr);
504 }
505 std::unique_ptr<float[]> advancesArray(new float[count]);
506 MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize,
507 advancesArray.get());
508 return minikin::getRunAdvance(advancesArray.get(), buf, start, count, offset);
509 }
510
511 static jfloat getRunAdvance___CIIIIZI_F(JNIEnv *env, jclass, jlong paintHandle, jcharArray text,
512 jint start, jint end, jint contextStart, jint contextEnd, jboolean isRtl, jint offset) {
513 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
514 const Typeface* typeface = paint->getAndroidTypeface();
515 ScopedCharArrayRO textArray(env, text);
516 jfloat result = doRunAdvance(paint, typeface, textArray.get() + contextStart,
517 start - contextStart, end - start, contextEnd - contextStart, isRtl,
518 offset - contextStart);
519 return result;
520 }
521
522 static jint doOffsetForAdvance(const Paint* paint, const Typeface* typeface, const jchar buf[],
523 jint start, jint count, jint bufSize, jboolean isRtl, jfloat advance) {
524 minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
525 std::unique_ptr<float[]> advancesArray(new float[count]);
526 MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize,
527 advancesArray.get());
528 return minikin::getOffsetForAdvance(advancesArray.get(), buf, start, count, advance);
529 }
530
531 static jint getOffsetForAdvance___CIIIIZF_I(JNIEnv *env, jclass, jlong paintHandle,
532 jcharArray text, jint start, jint end, jint contextStart, jint contextEnd,
533 jboolean isRtl, jfloat advance) {
534 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
535 const Typeface* typeface = paint->getAndroidTypeface();
536 ScopedCharArrayRO textArray(env, text);
537 jint result = doOffsetForAdvance(paint, typeface, textArray.get() + contextStart,
538 start - contextStart, end - start, contextEnd - contextStart, isRtl, advance);
539 result += contextStart;
540 return result;
541 }
542
543 static SkScalar getMetricsInternal(jlong paintHandle, SkFontMetrics *metrics) {
544 const int kElegantTop = 2500;
545 const int kElegantBottom = -1000;
546 const int kElegantAscent = 1900;
547 const int kElegantDescent = -500;
548 const int kElegantLeading = 0;
549 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
550 SkFont* font = &paint->getSkFont();
551 const Typeface* typeface = paint->getAndroidTypeface();
552 typeface = Typeface::resolveDefault(typeface);
553 minikin::FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle);
554 float saveSkewX = font->getSkewX();
555 bool savefakeBold = font->isEmbolden();
556 MinikinFontSkia::populateSkFont(font, baseFont.font->typeface().get(), baseFont.fakery);
557 SkScalar spacing = font->getMetrics(metrics);
558 // The populateSkPaint call may have changed fake bold / text skew
559 // because we want to measure with those effects applied, so now
560 // restore the original settings.
561 font->setSkewX(saveSkewX);
562 font->setEmbolden(savefakeBold);
563 if (paint->getFamilyVariant() == minikin::FamilyVariant::ELEGANT) {
564 SkScalar size = font->getSize();
565 metrics->fTop = -size * kElegantTop / 2048;
566 metrics->fBottom = -size * kElegantBottom / 2048;
567 metrics->fAscent = -size * kElegantAscent / 2048;
568 metrics->fDescent = -size * kElegantDescent / 2048;
569 metrics->fLeading = size * kElegantLeading / 2048;
570 spacing = metrics->fDescent - metrics->fAscent + metrics->fLeading;
571 }
572 return spacing;
573 }
574
575 static void doFontExtent(JNIEnv* env, jlong paintHandle, const jchar buf[], jint start,
576 jint count, jint bufSize, jboolean isRtl, jobject fmi) {
577 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
578 const Typeface* typeface = paint->getAndroidTypeface();
579 minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
580 minikin::MinikinExtent extent =
581 MinikinUtils::getFontExtent(paint, bidiFlags, typeface, buf, start, count, bufSize);
582
583 SkFontMetrics metrics;
584 getMetricsInternal(paintHandle, &metrics);
585
586 metrics.fAscent = extent.ascent;
587 metrics.fDescent = extent.descent;
588
589 // If top/bottom is narrower than ascent/descent, adjust top/bottom to ascent/descent.
590 metrics.fTop = std::min(metrics.fAscent, metrics.fTop);
591 metrics.fBottom = std::max(metrics.fDescent, metrics.fBottom);
592
593 GraphicsJNI::set_metrics_int(env, fmi, metrics);
594 }
595
596 static void getFontMetricsIntForText___C(JNIEnv* env, jclass, jlong paintHandle,
597 jcharArray text, jint start, jint count, jint ctxStart,
598 jint ctxCount, jboolean isRtl, jobject fmi) {
599 ScopedCharArrayRO textArray(env, text);
600
601 doFontExtent(env, paintHandle, textArray.get() + ctxStart, start - ctxStart, count,
602 ctxCount, isRtl, fmi);
603 }
604
605 static void getFontMetricsIntForText___String(JNIEnv* env, jclass, jlong paintHandle,
606 jstring text, jint start, jint count,
607 jint ctxStart, jint ctxCount, jboolean isRtl,
608 jobject fmi) {
609 ScopedStringChars textChars(env, text);
610
611 doFontExtent(env, paintHandle, textChars.get() + ctxStart, start - ctxStart, count,
612 ctxCount, isRtl, fmi);
613 }
614
615 // ------------------ @FastNative ---------------------------
616
617 static jint setTextLocales(JNIEnv* env, jobject clazz, jlong objHandle, jstring locales) {
618 Paint* obj = reinterpret_cast<Paint*>(objHandle);
619 ScopedUtfChars localesChars(env, locales);
620 jint minikinLocaleListId = minikin::registerLocaleList(localesChars.c_str());
621 obj->setMinikinLocaleListId(minikinLocaleListId);
622 return minikinLocaleListId;
623 }
624
625 static void setFontFeatureSettings(JNIEnv* env, jobject clazz, jlong paintHandle,
626 jstring settings) {
627 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
628 if (!settings) {
629 paint->setFontFeatureSettings(std::string());
630 } else {
631 ScopedUtfChars settingsChars(env, settings);
632 paint->setFontFeatureSettings(std::string(settingsChars.c_str(), settingsChars.size()));
633 }
634 }
635
636 static jfloat getFontMetrics(JNIEnv* env, jobject, jlong paintHandle, jobject metricsObj) {
637 SkFontMetrics metrics;
638 SkScalar spacing = getMetricsInternal(paintHandle, &metrics);
639 GraphicsJNI::set_metrics(env, metricsObj, metrics);
640 return SkScalarToFloat(spacing);
641 }
642
643 static jint getFontMetricsInt(JNIEnv* env, jobject, jlong paintHandle, jobject metricsObj) {
644 SkFontMetrics metrics;
645 getMetricsInternal(paintHandle, &metrics);
646 return GraphicsJNI::set_metrics_int(env, metricsObj, metrics);
647 }
648
649
650 // ------------------ @CriticalNative ---------------------------
651
652 static void reset(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
653 reinterpret_cast<Paint*>(objHandle)->reset();
654 }
655
656 static void assign(CRITICAL_JNI_PARAMS_COMMA jlong dstPaintHandle, jlong srcPaintHandle) {
657 Paint* dst = reinterpret_cast<Paint*>(dstPaintHandle);
658 const Paint* src = reinterpret_cast<Paint*>(srcPaintHandle);
659 *dst = *src;
660 }
661
662 static jint getFlags(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
663 uint32_t flags = reinterpret_cast<Paint*>(paintHandle)->getJavaFlags();
664 return static_cast<jint>(flags);
665 }
666
667 static void setFlags(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint flags) {
668 reinterpret_cast<Paint*>(paintHandle)->setJavaFlags(flags);
669 }
670
671 static jint getHinting(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
672 return (SkFontHinting)reinterpret_cast<Paint*>(paintHandle)->getSkFont().getHinting()
673 == SkFontHinting::kNone ? 0 : 1;
674 }
675
676 static void setHinting(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint mode) {
677 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setHinting(
678 mode == 0 ? SkFontHinting::kNone : SkFontHinting::kNormal);
679 }
680
681 static void setAntiAlias(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean aa) {
682 reinterpret_cast<Paint*>(paintHandle)->setAntiAlias(aa);
683 }
684
685 static void setLinearText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean linearText) {
686 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setLinearMetrics(linearText);
687 }
688
689 static void setSubpixelText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean subpixelText) {
690 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSubpixel(subpixelText);
691 }
692
693 static void setUnderlineText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean underlineText) {
694 reinterpret_cast<Paint*>(paintHandle)->setUnderline(underlineText);
695 }
696
697 static void setStrikeThruText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean strikeThruText) {
698 reinterpret_cast<Paint*>(paintHandle)->setStrikeThru(strikeThruText);
699 }
700
701 static void setFakeBoldText(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean fakeBoldText) {
702 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setEmbolden(fakeBoldText);
703 }
704
705 static void setFilterBitmap(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean filterBitmap) {
706 reinterpret_cast<Paint*>(paintHandle)->setFilterBitmap(filterBitmap);
707 }
708
709 static void setDither(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean dither) {
710 reinterpret_cast<Paint*>(paintHandle)->setDither(dither);
711 }
712
713 static jint getStyle(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
714 Paint* obj = reinterpret_cast<Paint*>(objHandle);
715 return static_cast<jint>(obj->getStyle());
716 }
717
718 static void setStyle(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint styleHandle) {
719 Paint* obj = reinterpret_cast<Paint*>(objHandle);
720 Paint::Style style = static_cast<Paint::Style>(styleHandle);
721 obj->setStyle(style);
722 }
723
724 static void setColorLong(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jlong colorSpaceHandle,
725 jlong colorLong) {
726 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
727 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
728 reinterpret_cast<Paint*>(paintHandle)->setColor4f(color, cs.get());
729 }
730
731 static void setColor(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint color) {
732 reinterpret_cast<Paint*>(paintHandle)->setColor(color);
733 }
734
735 static void setAlpha(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint a) {
736 reinterpret_cast<Paint*>(paintHandle)->setAlpha(a);
737 }
738
739 static jfloat getStrokeWidth(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
740 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeWidth());
741 }
742
743 static void setStrokeWidth(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat width) {
744 reinterpret_cast<Paint*>(paintHandle)->setStrokeWidth(width);
745 }
746
747 static jfloat getStrokeMiter(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
748 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeMiter());
749 }
750
751 static void setStrokeMiter(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat miter) {
752 reinterpret_cast<Paint*>(paintHandle)->setStrokeMiter(miter);
753 }
754
755 static jint getStrokeCap(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
756 Paint* obj = reinterpret_cast<Paint*>(objHandle);
757 return static_cast<jint>(obj->getStrokeCap());
758 }
759
760 static void setStrokeCap(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint capHandle) {
761 Paint* obj = reinterpret_cast<Paint*>(objHandle);
762 Paint::Cap cap = static_cast<Paint::Cap>(capHandle);
763 obj->setStrokeCap(cap);
764 }
765
766 static jint getStrokeJoin(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
767 Paint* obj = reinterpret_cast<Paint*>(objHandle);
768 return static_cast<jint>(obj->getStrokeJoin());
769 }
770
771 static void setStrokeJoin(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint joinHandle) {
772 Paint* obj = reinterpret_cast<Paint*>(objHandle);
773 Paint::Join join = (Paint::Join) joinHandle;
774 obj->setStrokeJoin(join);
775 }
776
777 static jboolean getFillPath(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong srcHandle, jlong dstHandle) {
778 Paint* obj = reinterpret_cast<Paint*>(objHandle);
779 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
780 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
781 return obj->getFillPath(*src, dst) ? JNI_TRUE : JNI_FALSE;
782 }
783
784 static jlong setShader(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong shaderHandle) {
785 Paint* obj = reinterpret_cast<Paint*>(objHandle);
786 SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle);
787 obj->setShader(sk_ref_sp(shader));
788 return reinterpret_cast<jlong>(obj->getShader());
789 }
790
791 static jlong setColorFilter(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong filterHandle) {
792 Paint* obj = reinterpret_cast<Paint *>(objHandle);
793 SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(filterHandle);
794 obj->setColorFilter(sk_ref_sp(filter));
795 return reinterpret_cast<jlong>(obj->getColorFilter());
796 }
797
798 static void setXfermode(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint xfermodeHandle) {
799 // validate that the Java enum values match our expectations
800 static_assert(0 == static_cast<int>(SkBlendMode::kClear), "xfermode_mismatch");
801 static_assert(1 == static_cast<int>(SkBlendMode::kSrc), "xfermode_mismatch");
802 static_assert(2 == static_cast<int>(SkBlendMode::kDst), "xfermode_mismatch");
803 static_assert(3 == static_cast<int>(SkBlendMode::kSrcOver), "xfermode_mismatch");
804 static_assert(4 == static_cast<int>(SkBlendMode::kDstOver), "xfermode_mismatch");
805 static_assert(5 == static_cast<int>(SkBlendMode::kSrcIn), "xfermode_mismatch");
806 static_assert(6 == static_cast<int>(SkBlendMode::kDstIn), "xfermode_mismatch");
807 static_assert(7 == static_cast<int>(SkBlendMode::kSrcOut), "xfermode_mismatch");
808 static_assert(8 == static_cast<int>(SkBlendMode::kDstOut), "xfermode_mismatch");
809 static_assert(9 == static_cast<int>(SkBlendMode::kSrcATop), "xfermode_mismatch");
810 static_assert(10 == static_cast<int>(SkBlendMode::kDstATop), "xfermode_mismatch");
811 static_assert(11 == static_cast<int>(SkBlendMode::kXor), "xfermode_mismatch");
812 static_assert(12 == static_cast<int>(SkBlendMode::kPlus), "xfermode_mismatch");
813 static_assert(13 == static_cast<int>(SkBlendMode::kModulate), "xfermode_mismatch");
814 static_assert(14 == static_cast<int>(SkBlendMode::kScreen), "xfermode_mismatch");
815 static_assert(15 == static_cast<int>(SkBlendMode::kOverlay), "xfermode_mismatch");
816 static_assert(16 == static_cast<int>(SkBlendMode::kDarken), "xfermode_mismatch");
817 static_assert(17 == static_cast<int>(SkBlendMode::kLighten), "xfermode_mismatch");
818 static_assert(18 == static_cast<int>(SkBlendMode::kColorDodge), "xfermode mismatch");
819 static_assert(19 == static_cast<int>(SkBlendMode::kColorBurn), "xfermode mismatch");
820 static_assert(20 == static_cast<int>(SkBlendMode::kHardLight), "xfermode mismatch");
821 static_assert(21 == static_cast<int>(SkBlendMode::kSoftLight), "xfermode mismatch");
822 static_assert(22 == static_cast<int>(SkBlendMode::kDifference), "xfermode mismatch");
823 static_assert(23 == static_cast<int>(SkBlendMode::kExclusion), "xfermode mismatch");
824 static_assert(24 == static_cast<int>(SkBlendMode::kMultiply), "xfermode mismatch");
825 static_assert(25 == static_cast<int>(SkBlendMode::kHue), "xfermode mismatch");
826 static_assert(26 == static_cast<int>(SkBlendMode::kSaturation), "xfermode mismatch");
827 static_assert(27 == static_cast<int>(SkBlendMode::kColor), "xfermode mismatch");
828 static_assert(28 == static_cast<int>(SkBlendMode::kLuminosity), "xfermode mismatch");
829
830 SkBlendMode mode = static_cast<SkBlendMode>(xfermodeHandle);
831 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
832 paint->setBlendMode(mode);
833 }
834
835 static jlong setPathEffect(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong effectHandle) {
836 Paint* obj = reinterpret_cast<Paint*>(objHandle);
837 SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle);
838 obj->setPathEffect(sk_ref_sp(effect));
839 return reinterpret_cast<jlong>(obj->getPathEffect());
840 }
841
842 static jlong setMaskFilter(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong maskfilterHandle) {
843 Paint* obj = reinterpret_cast<Paint*>(objHandle);
844 SkMaskFilter* maskfilter = reinterpret_cast<SkMaskFilter*>(maskfilterHandle);
845 obj->setMaskFilter(sk_ref_sp(maskfilter));
846 return reinterpret_cast<jlong>(obj->getMaskFilter());
847 }
848
849 static void setTypeface(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jlong typefaceHandle) {
850 Paint* paint = reinterpret_cast<Paint*>(objHandle);
851 paint->setAndroidTypeface(reinterpret_cast<Typeface*>(typefaceHandle));
852 }
853
854 static jint getTextAlign(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
855 Paint* obj = reinterpret_cast<Paint*>(objHandle);
856 return static_cast<jint>(obj->getTextAlign());
857 }
858
859 static void setTextAlign(CRITICAL_JNI_PARAMS_COMMA jlong objHandle, jint alignHandle) {
860 Paint* obj = reinterpret_cast<Paint*>(objHandle);
861 Paint::Align align = static_cast<Paint::Align>(alignHandle);
862 obj->setTextAlign(align);
863 }
864
865 static void setTextLocalesByMinikinLocaleListId(CRITICAL_JNI_PARAMS_COMMA jlong objHandle,
866 jint minikinLocaleListId) {
867 Paint* obj = reinterpret_cast<Paint*>(objHandle);
868 obj->setMinikinLocaleListId(minikinLocaleListId);
869 }
870
871 static jboolean isElegantTextHeight(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
872 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
873 return obj->getFamilyVariant() == minikin::FamilyVariant::ELEGANT;
874 }
875
876 static void setElegantTextHeight(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jboolean aa) {
877 Paint* obj = reinterpret_cast<Paint*>(paintHandle);
878 obj->setFamilyVariant(
879 aa ? minikin::FamilyVariant::ELEGANT : minikin::FamilyVariant::DEFAULT);
880 }
881
882 static jfloat getTextSize(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
883 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize());
884 }
885
886 static void setTextSize(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat textSize) {
887 if (textSize >= 0) {
888 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSize(textSize);
889 }
890 }
891
892 static jfloat getTextScaleX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
893 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getScaleX());
894 }
895
896 static void setTextScaleX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat scaleX) {
897 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setScaleX(scaleX);
898 }
899
900 static jfloat getTextSkewX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
901 return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSkewX());
902 }
903
904 static void setTextSkewX(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat skewX) {
905 reinterpret_cast<Paint*>(paintHandle)->getSkFont().setSkewX(skewX);
906 }
907
908 static jfloat getLetterSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
909 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
910 return paint->getLetterSpacing();
911 }
912
913 static void setLetterSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat letterSpacing) {
914 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
915 paint->setLetterSpacing(letterSpacing);
916 }
917
918 static jfloat getWordSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
919 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
920 return paint->getWordSpacing();
921 }
922
923 static void setWordSpacing(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat wordSpacing) {
924 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
925 paint->setWordSpacing(wordSpacing);
926 }
927
928 static jint getStartHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
929 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
930 return static_cast<jint>(paint->getStartHyphenEdit());
931 }
932
933 static jint getEndHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
934 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
935 return static_cast<jint>(paint->getEndHyphenEdit());
936 }
937
938 static void setStartHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
939 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
940 paint->setStartHyphenEdit((uint32_t)hyphen);
941 }
942
943 static void setEndHyphenEdit(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jint hyphen) {
944 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
945 paint->setEndHyphenEdit((uint32_t)hyphen);
946 }
947
948 static jfloat ascent(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
949 SkFontMetrics metrics;
950 getMetricsInternal(paintHandle, &metrics);
951 return SkScalarToFloat(metrics.fAscent);
952 }
953
954 static jfloat descent(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
955 SkFontMetrics metrics;
956 getMetricsInternal(paintHandle, &metrics);
957 return SkScalarToFloat(metrics.fDescent);
958 }
959
960 static jfloat getUnderlinePosition(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
961 SkFontMetrics metrics;
962 getMetricsInternal(paintHandle, &metrics);
963 SkScalar position;
964 if (metrics.hasUnderlinePosition(&position)) {
965 return SkScalarToFloat(position);
966 } else {
967 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
968 return SkScalarToFloat(Paint::kStdUnderline_Top * textSize);
969 }
970 }
971
972 static jfloat getUnderlineThickness(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
973 SkFontMetrics metrics;
974 getMetricsInternal(paintHandle, &metrics);
975 SkScalar thickness;
976 if (metrics.hasUnderlineThickness(&thickness)) {
977 return SkScalarToFloat(thickness);
978 } else {
979 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
980 return SkScalarToFloat(Paint::kStdUnderline_Thickness * textSize);
981 }
982 }
983
984 static jfloat getStrikeThruPosition(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
985 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
986 return SkScalarToFloat(Paint::kStdStrikeThru_Top * textSize);
987 }
988
989 static jfloat getStrikeThruThickness(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
990 const SkScalar textSize = reinterpret_cast<Paint*>(paintHandle)->getSkFont().getSize();
991 return SkScalarToFloat(Paint::kStdStrikeThru_Thickness * textSize);
992 }
993
994 static void setShadowLayer(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle, jfloat radius,
995 jfloat dx, jfloat dy, jlong colorSpaceHandle,
996 jlong colorLong) {
997 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
998 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
999
1000 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
1001 if (radius <= 0) {
1002 paint->setLooper(nullptr);
1003 }
1004 else {
1005 SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius);
1006 paint->setLooper(BlurDrawLooper::Make(color, cs.get(), sigma, {dx, dy}));
1007 }
1008 }
1009
1010 static jboolean hasShadowLayer(CRITICAL_JNI_PARAMS_COMMA jlong paintHandle) {
1011 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
1012 return paint->getLooper() != nullptr;
1013 }
1014
1015 static jboolean equalsForTextMeasurement(CRITICAL_JNI_PARAMS_COMMA jlong lPaint, jlong rPaint) {
1016 if (lPaint == rPaint) {
1017 return true;
1018 }
1019 Paint* leftPaint = reinterpret_cast<Paint*>(lPaint);
1020 Paint* rightPaint = reinterpret_cast<Paint*>(rPaint);
1021
1022 const Typeface* leftTypeface = Typeface::resolveDefault(leftPaint->getAndroidTypeface());
1023 const Typeface* rightTypeface = Typeface::resolveDefault(rightPaint->getAndroidTypeface());
1024 minikin::MinikinPaint leftMinikinPaint
1025 = MinikinUtils::prepareMinikinPaint(leftPaint, leftTypeface);
1026 minikin::MinikinPaint rightMinikinPaint
1027 = MinikinUtils::prepareMinikinPaint(rightPaint, rightTypeface);
1028
1029 return leftMinikinPaint == rightMinikinPaint;
1030 }
1031
1032 }; // namespace PaintGlue
1033
1034 static const JNINativeMethod methods[] = {
1035 {"nGetNativeFinalizer", "()J", (void*) PaintGlue::getNativeFinalizer},
1036 {"nInit","()J", (void*) PaintGlue::init},
1037 {"nInitWithPaint","(J)J", (void*) PaintGlue::initWithPaint},
1038 {"nBreakText","(J[CIIFI[F)I", (void*) PaintGlue::breakTextC},
1039 {"nBreakText","(JLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS},
1040 {"nGetTextAdvances","(J[CIIIII[FI)F",
1041 (void*) PaintGlue::getTextAdvances___CIIIII_FI},
1042 {"nGetTextAdvances","(JLjava/lang/String;IIIII[FI)F",
1043 (void*) PaintGlue::getTextAdvances__StringIIIII_FI},
1044
1045 {"nGetTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C},
1046 {"nGetTextRunCursor", "(JLjava/lang/String;IIIII)I",
1047 (void*) PaintGlue::getTextRunCursor__String},
1048 {"nGetTextPath", "(JI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C},
1049 {"nGetTextPath", "(JILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String},
1050 {"nGetStringBounds", "(JLjava/lang/String;IIILandroid/graphics/Rect;)V",
1051 (void*) PaintGlue::getStringBounds },
1052 {"nGetCharArrayBounds", "(J[CIIILandroid/graphics/Rect;)V",
1053 (void*) PaintGlue::getCharArrayBounds },
1054 {"nHasGlyph", "(JILjava/lang/String;)Z", (void*) PaintGlue::hasGlyph },
1055 {"nGetRunAdvance", "(J[CIIIIZI)F", (void*) PaintGlue::getRunAdvance___CIIIIZI_F},
1056 {"nGetOffsetForAdvance", "(J[CIIIIZF)I",
1057 (void*) PaintGlue::getOffsetForAdvance___CIIIIZF_I},
1058 {"nGetFontMetricsIntForText", "(J[CIIIIZLandroid/graphics/Paint$FontMetricsInt;)V",
1059 (void*)PaintGlue::getFontMetricsIntForText___C},
1060 {"nGetFontMetricsIntForText",
1061 "(JLjava/lang/String;IIIIZLandroid/graphics/Paint$FontMetricsInt;)V",
1062 (void*)PaintGlue::getFontMetricsIntForText___String},
1063
1064 // --------------- @FastNative ----------------------
1065
1066 {"nSetTextLocales","(JLjava/lang/String;)I", (void*) PaintGlue::setTextLocales},
1067 {"nSetFontFeatureSettings","(JLjava/lang/String;)V",
1068 (void*) PaintGlue::setFontFeatureSettings},
1069 {"nGetFontMetrics", "(JLandroid/graphics/Paint$FontMetrics;)F",
1070 (void*)PaintGlue::getFontMetrics},
1071 {"nGetFontMetricsInt", "(JLandroid/graphics/Paint$FontMetricsInt;)I",
1072 (void*)PaintGlue::getFontMetricsInt},
1073
1074 // --------------- @CriticalNative ------------------
1075
1076 {"nReset","(J)V", (void*) PaintGlue::reset},
1077 {"nSet","(JJ)V", (void*) PaintGlue::assign},
1078 {"nGetFlags","(J)I", (void*) PaintGlue::getFlags},
1079 {"nSetFlags","(JI)V", (void*) PaintGlue::setFlags},
1080 {"nGetHinting","(J)I", (void*) PaintGlue::getHinting},
1081 {"nSetHinting","(JI)V", (void*) PaintGlue::setHinting},
1082 {"nSetAntiAlias","(JZ)V", (void*) PaintGlue::setAntiAlias},
1083 {"nSetSubpixelText","(JZ)V", (void*) PaintGlue::setSubpixelText},
1084 {"nSetLinearText","(JZ)V", (void*) PaintGlue::setLinearText},
1085 {"nSetUnderlineText","(JZ)V", (void*) PaintGlue::setUnderlineText},
1086 {"nSetStrikeThruText","(JZ)V", (void*) PaintGlue::setStrikeThruText},
1087 {"nSetFakeBoldText","(JZ)V", (void*) PaintGlue::setFakeBoldText},
1088 {"nSetFilterBitmap","(JZ)V", (void*) PaintGlue::setFilterBitmap},
1089 {"nSetDither","(JZ)V", (void*) PaintGlue::setDither},
1090 {"nGetStyle","(J)I", (void*) PaintGlue::getStyle},
1091 {"nSetStyle","(JI)V", (void*) PaintGlue::setStyle},
1092 {"nSetColor","(JI)V", (void*) PaintGlue::setColor},
1093 {"nSetColor","(JJJ)V", (void*) PaintGlue::setColorLong},
1094 {"nSetAlpha","(JI)V", (void*) PaintGlue::setAlpha},
1095 {"nGetStrokeWidth","(J)F", (void*) PaintGlue::getStrokeWidth},
1096 {"nSetStrokeWidth","(JF)V", (void*) PaintGlue::setStrokeWidth},
1097 {"nGetStrokeMiter","(J)F", (void*) PaintGlue::getStrokeMiter},
1098 {"nSetStrokeMiter","(JF)V", (void*) PaintGlue::setStrokeMiter},
1099 {"nGetStrokeCap","(J)I", (void*) PaintGlue::getStrokeCap},
1100 {"nSetStrokeCap","(JI)V", (void*) PaintGlue::setStrokeCap},
1101 {"nGetStrokeJoin","(J)I", (void*) PaintGlue::getStrokeJoin},
1102 {"nSetStrokeJoin","(JI)V", (void*) PaintGlue::setStrokeJoin},
1103 {"nGetFillPath","(JJJ)Z", (void*) PaintGlue::getFillPath},
1104 {"nSetShader","(JJ)J", (void*) PaintGlue::setShader},
1105 {"nSetColorFilter","(JJ)J", (void*) PaintGlue::setColorFilter},
1106 {"nSetXfermode","(JI)V", (void*) PaintGlue::setXfermode},
1107 {"nSetPathEffect","(JJ)J", (void*) PaintGlue::setPathEffect},
1108 {"nSetMaskFilter","(JJ)J", (void*) PaintGlue::setMaskFilter},
1109 {"nSetTypeface","(JJ)V", (void*) PaintGlue::setTypeface},
1110 {"nGetTextAlign","(J)I", (void*) PaintGlue::getTextAlign},
1111 {"nSetTextAlign","(JI)V", (void*) PaintGlue::setTextAlign},
1112 {"nSetTextLocalesByMinikinLocaleListId","(JI)V",
1113 (void*) PaintGlue::setTextLocalesByMinikinLocaleListId},
1114 {"nIsElegantTextHeight","(J)Z", (void*) PaintGlue::isElegantTextHeight},
1115 {"nSetElegantTextHeight","(JZ)V", (void*) PaintGlue::setElegantTextHeight},
1116 {"nGetTextSize","(J)F", (void*) PaintGlue::getTextSize},
1117 {"nSetTextSize","(JF)V", (void*) PaintGlue::setTextSize},
1118 {"nGetTextScaleX","(J)F", (void*) PaintGlue::getTextScaleX},
1119 {"nSetTextScaleX","(JF)V", (void*) PaintGlue::setTextScaleX},
1120 {"nGetTextSkewX","(J)F", (void*) PaintGlue::getTextSkewX},
1121 {"nSetTextSkewX","(JF)V", (void*) PaintGlue::setTextSkewX},
1122 {"nGetLetterSpacing","(J)F", (void*) PaintGlue::getLetterSpacing},
1123 {"nSetLetterSpacing","(JF)V", (void*) PaintGlue::setLetterSpacing},
1124 {"nGetWordSpacing","(J)F", (void*) PaintGlue::getWordSpacing},
1125 {"nSetWordSpacing","(JF)V", (void*) PaintGlue::setWordSpacing},
1126 {"nGetStartHyphenEdit", "(J)I", (void*) PaintGlue::getStartHyphenEdit},
1127 {"nGetEndHyphenEdit", "(J)I", (void*) PaintGlue::getEndHyphenEdit},
1128 {"nSetStartHyphenEdit", "(JI)V", (void*) PaintGlue::setStartHyphenEdit},
1129 {"nSetEndHyphenEdit", "(JI)V", (void*) PaintGlue::setEndHyphenEdit},
1130 {"nAscent","(J)F", (void*) PaintGlue::ascent},
1131 {"nDescent","(J)F", (void*) PaintGlue::descent},
1132 {"nGetUnderlinePosition","(J)F", (void*) PaintGlue::getUnderlinePosition},
1133 {"nGetUnderlineThickness","(J)F", (void*) PaintGlue::getUnderlineThickness},
1134 {"nGetStrikeThruPosition","(J)F", (void*) PaintGlue::getStrikeThruPosition},
1135 {"nGetStrikeThruThickness","(J)F", (void*) PaintGlue::getStrikeThruThickness},
1136 {"nSetShadowLayer", "(JFFFJJ)V", (void*)PaintGlue::setShadowLayer},
1137 {"nHasShadowLayer", "(J)Z", (void*)PaintGlue::hasShadowLayer},
1138 {"nEqualsForTextMeasurement", "(JJ)Z", (void*)PaintGlue::equalsForTextMeasurement},
1139 };
1140
1141
1142 int register_android_graphics_Paint(JNIEnv* env) {
1143 return RegisterMethodsOrDie(env, "android/graphics/Paint", methods, NELEM(methods));
1144 }
1145
1146 }