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, |
Seigo Nonaka | 6c9956e | 2023-06-29 17:45:00 +0900 | [diff] [blame] | 65 | jlong paintPtr, jint lbStyle, jint lbWordStyle, jboolean hyphenation, |
| 66 | jint start, jint end, 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) |
Seigo Nonaka | 6c9956e | 2023-06-29 17:45:00 +0900 | [diff] [blame] | 71 | ->addStyleRun(start, end, std::move(minikinPaint), lbStyle, lbWordStyle, hyphenation, |
| 72 | isRtl); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Regular JNI |
| 76 | static void nAddReplacementRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr, |
| 77 | jlong paintPtr, jint start, jint end, jfloat width) { |
| 78 | toBuilder(builderPtr)->addReplacementRun(start, end, width, |
| 79 | toPaint(paintPtr)->getMinikinLocaleListId()); |
| 80 | } |
| 81 | |
| 82 | // Regular JNI |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 83 | static jlong nBuildMeasuredText(JNIEnv* env, jclass /* unused */, jlong builderPtr, jlong hintPtr, |
| 84 | jcharArray javaText, jboolean computeHyphenation, |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame] | 85 | jboolean computeLayout, jboolean computeBounds, |
| 86 | jboolean fastHyphenationMode) { |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 87 | ScopedCharArrayRO text(env, javaText); |
| 88 | const minikin::U16StringPiece textBuffer(text.get(), text.size()); |
| 89 | |
| 90 | // Pass the ownership to Java. |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 91 | return toJLong(toBuilder(builderPtr) |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame] | 92 | ->build(textBuffer, computeHyphenation, computeLayout, computeBounds, |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 93 | fastHyphenationMode, toMeasuredParagraph(hintPtr)) |
| 94 | .release()); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Regular JNI |
| 98 | static void nFreeBuilder(JNIEnv* env, jclass /* unused */, jlong builderPtr) { |
| 99 | delete toBuilder(builderPtr); |
| 100 | } |
| 101 | |
| 102 | // CriticalNative |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 103 | 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] | 104 | minikin::MeasuredText* mt = toMeasuredParagraph(ptr); |
| 105 | float r = 0.0f; |
| 106 | for (int i = start; i < end; ++i) { |
| 107 | r += mt->widths[i]; |
| 108 | } |
| 109 | return r; |
| 110 | } |
| 111 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 112 | static jfloat nGetCharWidthAt(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint offset) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 113 | return toMeasuredParagraph(ptr)->widths[offset]; |
| 114 | } |
| 115 | |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 116 | // Regular JNI |
Seigo Nonaka | fb0abe1 | 2018-04-02 23:25:38 -0700 | [diff] [blame] | 117 | static void nGetBounds(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start, jint end, |
| 118 | jobject bounds) { |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 119 | ScopedCharArrayRO text(env, javaText); |
| 120 | const minikin::U16StringPiece textBuffer(text.get(), text.size()); |
Seigo Nonaka | fb0abe1 | 2018-04-02 23:25:38 -0700 | [diff] [blame] | 121 | const minikin::Range range(start, end); |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 122 | |
Seigo Nonaka | fb0abe1 | 2018-04-02 23:25:38 -0700 | [diff] [blame] | 123 | minikin::MinikinRect rect = toMeasuredParagraph(ptr)->getBounds(textBuffer, range); |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 124 | |
| 125 | SkRect r; |
| 126 | r.fLeft = rect.mLeft; |
| 127 | r.fTop = rect.mTop; |
| 128 | r.fRight = rect.mRight; |
| 129 | r.fBottom = rect.mBottom; |
| 130 | |
| 131 | SkIRect ir; |
| 132 | r.roundOut(&ir); |
| 133 | GraphicsJNI::irect_to_jrect(ir, env, bounds); |
| 134 | } |
| 135 | |
Seigo Nonaka | 6b9b516 | 2022-02-03 15:20:20 +0900 | [diff] [blame] | 136 | // Regular JNI |
| 137 | static jlong nGetExtent(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start, |
| 138 | jint end) { |
| 139 | ScopedCharArrayRO text(env, javaText); |
| 140 | const minikin::U16StringPiece textBuffer(text.get(), text.size()); |
| 141 | const minikin::Range range(start, end); |
| 142 | |
| 143 | minikin::MinikinExtent extent = toMeasuredParagraph(ptr)->getExtent(textBuffer, range); |
| 144 | |
| 145 | int32_t ascent = SkScalarRoundToInt(extent.ascent); |
| 146 | int32_t descent = SkScalarRoundToInt(extent.descent); |
| 147 | |
| 148 | return (((jlong)(ascent)) << 32) | ((jlong)descent); |
| 149 | } |
| 150 | |
Seigo Nonaka | 783f961 | 2018-01-20 12:11:13 -0800 | [diff] [blame] | 151 | // CriticalNative |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 152 | static jlong nGetReleaseFunc(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 153 | return toJLong(&releaseMeasuredParagraph); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 156 | static jint nGetMemoryUsage(CRITICAL_JNI_PARAMS_COMMA jlong ptr) { |
Seigo Nonaka | 49ca024 | 2018-01-24 16:46:14 -0800 | [diff] [blame] | 157 | return static_cast<jint>(toMeasuredParagraph(ptr)->getMemoryUsage()); |
| 158 | } |
| 159 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 160 | static const JNINativeMethod gMTBuilderMethods[] = { |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 161 | // MeasuredParagraphBuilder native functions. |
| 162 | {"nInitBuilder", "()J", (void*)nInitBuilder}, |
Seigo Nonaka | 6c9956e | 2023-06-29 17:45:00 +0900 | [diff] [blame] | 163 | {"nAddStyleRun", "(JJIIZIIZ)V", (void*)nAddStyleRun}, |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 164 | {"nAddReplacementRun", "(JJIIF)V", (void*)nAddReplacementRun}, |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame] | 165 | {"nBuildMeasuredText", "(JJ[CZZZZ)J", (void*)nBuildMeasuredText}, |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 166 | {"nFreeBuilder", "(J)V", (void*)nFreeBuilder}, |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 167 | }; |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 168 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 169 | static const JNINativeMethod gMTMethods[] = { |
Seigo Nonaka | 6b9b516 | 2022-02-03 15:20:20 +0900 | [diff] [blame] | 170 | // MeasuredParagraph native functions. |
| 171 | {"nGetWidth", "(JII)F", (void*)nGetWidth}, // Critical Natives |
| 172 | {"nGetBounds", "(J[CIILandroid/graphics/Rect;)V", (void*)nGetBounds}, // Regular JNI |
| 173 | {"nGetExtent", "(J[CII)J", (void*)nGetExtent}, // Regular JNI |
| 174 | {"nGetReleaseFunc", "()J", (void*)nGetReleaseFunc}, // Critical Natives |
| 175 | {"nGetMemoryUsage", "(J)I", (void*)nGetMemoryUsage}, // Critical Native |
| 176 | {"nGetCharWidthAt", "(JI)F", (void*)nGetCharWidthAt}, // Critical Native |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
Seigo Nonaka | 70200b0 | 2018-10-01 16:04:11 -0700 | [diff] [blame] | 179 | int register_android_graphics_text_MeasuredText(JNIEnv* env) { |
| 180 | return RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText", |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 181 | gMTMethods, NELEM(gMTMethods)) |
Seigo Nonaka | 70200b0 | 2018-10-01 16:04:11 -0700 | [diff] [blame] | 182 | + RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText$Builder", |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 183 | gMTBuilderMethods, NELEM(gMTBuilderMethods)); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | } |