Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 17 | #include "GraphicsJNI.h" |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 18 | #include "utils/misc.h" |
| 19 | #include "utils/Log.h" |
| 20 | #include <nativehelper/ScopedStringChars.h> |
| 21 | #include <nativehelper/ScopedPrimitiveArray.h> |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 22 | #include <cstdint> |
| 23 | #include <vector> |
| 24 | #include <list> |
| 25 | #include <algorithm> |
| 26 | |
| 27 | #include "SkPaint.h" |
| 28 | #include "SkTypeface.h" |
| 29 | #include <hwui/MinikinSkia.h> |
| 30 | #include <hwui/MinikinUtils.h> |
| 31 | #include <hwui/Paint.h> |
| 32 | #include <minikin/FontCollection.h> |
| 33 | #include <minikin/AndroidLineBreakerHelper.h> |
| 34 | #include <minikin/MinikinFont.h> |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | static inline minikin::MeasuredTextBuilder* toBuilder(jlong ptr) { |
| 39 | return reinterpret_cast<minikin::MeasuredTextBuilder*>(ptr); |
| 40 | } |
| 41 | |
| 42 | static inline Paint* toPaint(jlong ptr) { |
| 43 | return reinterpret_cast<Paint*>(ptr); |
| 44 | } |
| 45 | |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 46 | static inline minikin::MeasuredText* toMeasuredParagraph(jlong ptr) { |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 47 | return reinterpret_cast<minikin::MeasuredText*>(ptr); |
| 48 | } |
| 49 | |
| 50 | template<typename Ptr> static inline jlong toJLong(Ptr ptr) { |
| 51 | return reinterpret_cast<jlong>(ptr); |
| 52 | } |
| 53 | |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 54 | static void releaseMeasuredParagraph(jlong measuredTextPtr) { |
| 55 | delete toMeasuredParagraph(measuredTextPtr); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Regular JNI |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 59 | static jlong nInitBuilder(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 60 | return toJLong(new minikin::MeasuredTextBuilder()); |
| 61 | } |
| 62 | |
| 63 | // Regular JNI |
| 64 | static void nAddStyleRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr, |
James.cf Lin | 664a75b | 2022-01-19 16:56:34 +0800 | [diff] [blame] | 65 | jlong paintPtr, jint lbStyle, jint lbWordStyle, jint start, jint end, |
| 66 | jboolean isRtl) { |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 67 | Paint* paint = toPaint(paintPtr); |
| 68 | const Typeface* typeface = Typeface::resolveDefault(paint->getAndroidTypeface()); |
| 69 | minikin::MinikinPaint minikinPaint = MinikinUtils::prepareMinikinPaint(paint, typeface); |
James.cf Lin | 664a75b | 2022-01-19 16:56:34 +0800 | [diff] [blame] | 70 | toBuilder(builderPtr) |
| 71 | ->addStyleRun(start, end, std::move(minikinPaint), lbStyle, lbWordStyle, isRtl); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Regular JNI |
| 75 | static void nAddReplacementRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr, |
| 76 | jlong paintPtr, jint start, jint end, jfloat width) { |
| 77 | toBuilder(builderPtr)->addReplacementRun(start, end, width, |
| 78 | toPaint(paintPtr)->getMinikinLocaleListId()); |
| 79 | } |
| 80 | |
| 81 | // Regular JNI |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 82 | static jlong nBuildMeasuredText(JNIEnv* env, jclass /* unused */, jlong builderPtr, jlong hintPtr, |
| 83 | jcharArray javaText, jboolean computeHyphenation, |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame^] | 84 | jboolean computeLayout, jboolean computeBounds, |
| 85 | jboolean fastHyphenationMode) { |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 86 | ScopedCharArrayRO text(env, javaText); |
| 87 | const minikin::U16StringPiece textBuffer(text.get(), text.size()); |
| 88 | |
| 89 | // Pass the ownership to Java. |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 90 | return toJLong(toBuilder(builderPtr) |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame^] | 91 | ->build(textBuffer, computeHyphenation, computeLayout, computeBounds, |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 92 | fastHyphenationMode, toMeasuredParagraph(hintPtr)) |
| 93 | .release()); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Regular JNI |
| 97 | static void nFreeBuilder(JNIEnv* env, jclass /* unused */, jlong builderPtr) { |
| 98 | delete toBuilder(builderPtr); |
| 99 | } |
| 100 | |
| 101 | // CriticalNative |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 102 | static jfloat nGetWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint start, jint end) { |
Seigo Nonaka | 783f961 | 2018-01-20 12:11:13 -0800 | [diff] [blame] | 103 | minikin::MeasuredText* mt = toMeasuredParagraph(ptr); |
| 104 | float r = 0.0f; |
| 105 | for (int i = start; i < end; ++i) { |
| 106 | r += mt->widths[i]; |
| 107 | } |
| 108 | return r; |
| 109 | } |
| 110 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 111 | static jfloat nGetCharWidthAt(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint offset) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 112 | return toMeasuredParagraph(ptr)->widths[offset]; |
| 113 | } |
| 114 | |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 115 | // Regular JNI |
Seigo Nonaka | fb0abe1 | 2018-04-02 23:25:38 -0700 | [diff] [blame] | 116 | static void nGetBounds(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start, jint end, |
| 117 | jobject bounds) { |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 118 | ScopedCharArrayRO text(env, javaText); |
| 119 | const minikin::U16StringPiece textBuffer(text.get(), text.size()); |
Seigo Nonaka | fb0abe1 | 2018-04-02 23:25:38 -0700 | [diff] [blame] | 120 | const minikin::Range range(start, end); |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 121 | |
Seigo Nonaka | fb0abe1 | 2018-04-02 23:25:38 -0700 | [diff] [blame] | 122 | minikin::MinikinRect rect = toMeasuredParagraph(ptr)->getBounds(textBuffer, range); |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 123 | |
| 124 | SkRect r; |
| 125 | r.fLeft = rect.mLeft; |
| 126 | r.fTop = rect.mTop; |
| 127 | r.fRight = rect.mRight; |
| 128 | r.fBottom = rect.mBottom; |
| 129 | |
| 130 | SkIRect ir; |
| 131 | r.roundOut(&ir); |
| 132 | GraphicsJNI::irect_to_jrect(ir, env, bounds); |
| 133 | } |
| 134 | |
Seigo Nonaka | 6b9b516 | 2022-02-03 15:20:20 +0900 | [diff] [blame] | 135 | // Regular JNI |
| 136 | static jlong nGetExtent(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start, |
| 137 | jint end) { |
| 138 | ScopedCharArrayRO text(env, javaText); |
| 139 | const minikin::U16StringPiece textBuffer(text.get(), text.size()); |
| 140 | const minikin::Range range(start, end); |
| 141 | |
| 142 | minikin::MinikinExtent extent = toMeasuredParagraph(ptr)->getExtent(textBuffer, range); |
| 143 | |
| 144 | int32_t ascent = SkScalarRoundToInt(extent.ascent); |
| 145 | int32_t descent = SkScalarRoundToInt(extent.descent); |
| 146 | |
| 147 | return (((jlong)(ascent)) << 32) | ((jlong)descent); |
| 148 | } |
| 149 | |
Seigo Nonaka | 783f961 | 2018-01-20 12:11:13 -0800 | [diff] [blame] | 150 | // CriticalNative |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 151 | static jlong nGetReleaseFunc(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 152 | return toJLong(&releaseMeasuredParagraph); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 155 | static jint nGetMemoryUsage(CRITICAL_JNI_PARAMS_COMMA jlong ptr) { |
Seigo Nonaka | 49ca024 | 2018-01-24 16:46:14 -0800 | [diff] [blame] | 156 | return static_cast<jint>(toMeasuredParagraph(ptr)->getMemoryUsage()); |
| 157 | } |
| 158 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 159 | static const JNINativeMethod gMTBuilderMethods[] = { |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 160 | // MeasuredParagraphBuilder native functions. |
| 161 | {"nInitBuilder", "()J", (void*)nInitBuilder}, |
James.cf Lin | 664a75b | 2022-01-19 16:56:34 +0800 | [diff] [blame] | 162 | {"nAddStyleRun", "(JJIIIIZ)V", (void*)nAddStyleRun}, |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 163 | {"nAddReplacementRun", "(JJIIF)V", (void*)nAddReplacementRun}, |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame^] | 164 | {"nBuildMeasuredText", "(JJ[CZZZZ)J", (void*)nBuildMeasuredText}, |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 165 | {"nFreeBuilder", "(J)V", (void*)nFreeBuilder}, |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 166 | }; |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 167 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 168 | static const JNINativeMethod gMTMethods[] = { |
Seigo Nonaka | 6b9b516 | 2022-02-03 15:20:20 +0900 | [diff] [blame] | 169 | // MeasuredParagraph native functions. |
| 170 | {"nGetWidth", "(JII)F", (void*)nGetWidth}, // Critical Natives |
| 171 | {"nGetBounds", "(J[CIILandroid/graphics/Rect;)V", (void*)nGetBounds}, // Regular JNI |
| 172 | {"nGetExtent", "(J[CII)J", (void*)nGetExtent}, // Regular JNI |
| 173 | {"nGetReleaseFunc", "()J", (void*)nGetReleaseFunc}, // Critical Natives |
| 174 | {"nGetMemoryUsage", "(J)I", (void*)nGetMemoryUsage}, // Critical Native |
| 175 | {"nGetCharWidthAt", "(JI)F", (void*)nGetCharWidthAt}, // Critical Native |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
Seigo Nonaka | 70200b0 | 2018-10-01 16:04:11 -0700 | [diff] [blame] | 178 | int register_android_graphics_text_MeasuredText(JNIEnv* env) { |
| 179 | return RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText", |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 180 | gMTMethods, NELEM(gMTMethods)) |
Seigo Nonaka | 70200b0 | 2018-10-01 16:04:11 -0700 | [diff] [blame] | 181 | + RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText$Builder", |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 182 | gMTBuilderMethods, NELEM(gMTBuilderMethods)); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } |