Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 17 | #include "utils/misc.h" |
| 18 | #include "utils/Log.h" |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 19 | #include "graphics_jni_helpers.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 20 | #include <nativehelper/ScopedStringChars.h> |
| 21 | #include <nativehelper/ScopedPrimitiveArray.h> |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 22 | #include "scoped_nullable_primitive_array.h" |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 23 | #include <cstdint> |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 24 | #include <vector> |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 25 | #include <list> |
| 26 | #include <algorithm> |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 27 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 28 | #include "SkPaint.h" |
| 29 | #include "SkTypeface.h" |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 30 | #include <hwui/MinikinSkia.h> |
| 31 | #include <hwui/MinikinUtils.h> |
| 32 | #include <hwui/Paint.h> |
| 33 | #include <minikin/FontCollection.h> |
Seigo Nonaka | 3e7deec | 2017-11-15 12:06:24 -0800 | [diff] [blame] | 34 | #include <minikin/AndroidLineBreakerHelper.h> |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 35 | #include <minikin/MinikinFont.h> |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 36 | |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | |
Roozbeh Pournader | 0bba742 | 2017-09-28 12:35:10 -0700 | [diff] [blame] | 39 | static inline std::vector<float> jintArrayToFloatVector(JNIEnv* env, jintArray javaArray) { |
| 40 | if (javaArray == nullptr) { |
| 41 | return std::vector<float>(); |
| 42 | } else { |
| 43 | ScopedIntArrayRO intArr(env, javaArray); |
| 44 | return std::vector<float>(intArr.get(), intArr.get() + intArr.size()); |
| 45 | } |
| 46 | } |
| 47 | |
Seigo Nonaka | 3e7deec | 2017-11-15 12:06:24 -0800 | [diff] [blame] | 48 | static inline minikin::android::StaticLayoutNative* toNative(jlong ptr) { |
| 49 | return reinterpret_cast<minikin::android::StaticLayoutNative*>(ptr); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 52 | // set text and set a number of parameters for creating a layout (width, tabstops, strategy, |
| 53 | // hyphenFrequency) |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame^] | 54 | static jlong nInit(JNIEnv* env, jclass /* unused */, jint breakStrategy, jint hyphenationFrequency, |
| 55 | jboolean isJustified, jintArray indents, jboolean useBoundsForWidth) { |
Seigo Nonaka | 3e7deec | 2017-11-15 12:06:24 -0800 | [diff] [blame] | 56 | return reinterpret_cast<jlong>(new minikin::android::StaticLayoutNative( |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 57 | static_cast<minikin::BreakStrategy>(breakStrategy), |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame^] | 58 | static_cast<minikin::HyphenationFrequency>(hyphenationFrequency), isJustified, |
| 59 | jintArrayToFloatVector(env, indents), useBoundsForWidth)); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 60 | } |
Seigo Nonaka | bafe197 | 2017-08-24 15:30:29 -0700 | [diff] [blame] | 61 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 62 | static void nFinish(jlong nativePtr) { |
| 63 | delete toNative(nativePtr); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 64 | } |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 65 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 66 | // CriticalNative |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 67 | static jlong nGetReleaseFunc(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 68 | return reinterpret_cast<jlong>(nFinish); |
| 69 | } |
| 70 | |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 71 | static jlong nComputeLineBreaks(JNIEnv* env, jclass, jlong nativePtr, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 72 | // Inputs |
Seigo Nonaka | ef94f49 | 2017-11-10 16:11:33 -0800 | [diff] [blame] | 73 | jcharArray javaText, |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 74 | jlong measuredTextPtr, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 75 | jint length, |
| 76 | jfloat firstWidth, |
| 77 | jint firstWidthLineCount, |
| 78 | jfloat restWidth, |
Seigo Nonaka | d5621ed | 2019-03-08 16:04:37 -0800 | [diff] [blame] | 79 | jfloatArray variableTabStops, |
| 80 | jfloat defaultTabStop, |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 81 | jint indentsOffset) { |
Seigo Nonaka | 3e7deec | 2017-11-15 12:06:24 -0800 | [diff] [blame] | 82 | minikin::android::StaticLayoutNative* builder = toNative(nativePtr); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 83 | |
Seigo Nonaka | ef94f49 | 2017-11-10 16:11:33 -0800 | [diff] [blame] | 84 | ScopedCharArrayRO text(env, javaText); |
Seigo Nonaka | d5621ed | 2019-03-08 16:04:37 -0800 | [diff] [blame] | 85 | ScopedNullableFloatArrayRO tabStops(env, variableTabStops); |
Seigo Nonaka | ef94f49 | 2017-11-10 16:11:33 -0800 | [diff] [blame] | 86 | |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 87 | minikin::U16StringPiece u16Text(text.get(), length); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 88 | minikin::MeasuredText* measuredText = reinterpret_cast<minikin::MeasuredText*>(measuredTextPtr); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 89 | |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 90 | std::unique_ptr<minikin::LineBreakResult> result = |
| 91 | std::make_unique<minikin::LineBreakResult>(builder->computeBreaks( |
| 92 | u16Text, *measuredText, firstWidth, firstWidthLineCount, restWidth, indentsOffset, |
| 93 | tabStops.get(), tabStops.size(), defaultTabStop)); |
| 94 | return reinterpret_cast<jlong>(result.release()); |
| 95 | } |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 96 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 97 | static jint nGetLineCount(CRITICAL_JNI_PARAMS_COMMA jlong ptr) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 98 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->breakPoints.size(); |
| 99 | } |
| 100 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 101 | static jint nGetLineBreakOffset(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 102 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->breakPoints[i]; |
| 103 | } |
| 104 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 105 | static jfloat nGetLineWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 106 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->widths[i]; |
| 107 | } |
| 108 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 109 | static jfloat nGetLineAscent(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 110 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->ascents[i]; |
| 111 | } |
| 112 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 113 | static jfloat nGetLineDescent(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 114 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->descents[i]; |
| 115 | } |
| 116 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 117 | static jint nGetLineFlag(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 118 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->flags[i]; |
| 119 | } |
| 120 | |
| 121 | static void nReleaseResult(jlong ptr) { |
| 122 | delete reinterpret_cast<minikin::LineBreakResult*>(ptr); |
| 123 | } |
| 124 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 125 | static jlong nGetReleaseResultFunc(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 126 | return reinterpret_cast<jlong>(nReleaseResult); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 129 | static const JNINativeMethod gMethods[] = { |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame^] | 130 | // Fast Natives |
| 131 | {"nInit", |
| 132 | "(" |
| 133 | "I" // breakStrategy |
| 134 | "I" // hyphenationFrequency |
| 135 | "Z" // isJustified |
| 136 | "[I" // indents |
| 137 | "Z" // useBoundsForWidth |
| 138 | ")J", |
| 139 | (void*)nInit}, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 140 | |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame^] | 141 | // Critical Natives |
| 142 | {"nGetReleaseFunc", "()J", (void*)nGetReleaseFunc}, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 143 | |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame^] | 144 | // Regular JNI |
| 145 | {"nComputeLineBreaks", |
| 146 | "(" |
| 147 | "J" // nativePtr |
| 148 | "[C" // text |
| 149 | "J" // MeasuredParagraph ptr. |
| 150 | "I" // length |
| 151 | "F" // firstWidth |
| 152 | "I" // firstWidthLineCount |
| 153 | "F" // restWidth |
| 154 | "[F" // variableTabStops |
| 155 | "F" // defaultTabStop |
| 156 | "I" // indentsOffset |
| 157 | ")J", |
| 158 | (void*)nComputeLineBreaks}, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 159 | |
Seigo Nonaka | 6e39daf | 2023-08-19 11:36:26 +0900 | [diff] [blame^] | 160 | // Result accessors, CriticalNatives |
| 161 | {"nGetLineCount", "(J)I", (void*)nGetLineCount}, |
| 162 | {"nGetLineBreakOffset", "(JI)I", (void*)nGetLineBreakOffset}, |
| 163 | {"nGetLineWidth", "(JI)F", (void*)nGetLineWidth}, |
| 164 | {"nGetLineAscent", "(JI)F", (void*)nGetLineAscent}, |
| 165 | {"nGetLineDescent", "(JI)F", (void*)nGetLineDescent}, |
| 166 | {"nGetLineFlag", "(JI)I", (void*)nGetLineFlag}, |
| 167 | {"nGetReleaseResultFunc", "()J", (void*)nGetReleaseResultFunc}, |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Seigo Nonaka | 70200b0 | 2018-10-01 16:04:11 -0700 | [diff] [blame] | 170 | int register_android_graphics_text_LineBreaker(JNIEnv* env) { |
| 171 | return RegisterMethodsOrDie(env, "android/graphics/text/LineBreaker", gMethods, |
| 172 | NELEM(gMethods)); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | } |