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 | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 54 | static jlong nInit(JNIEnv* env, jclass /* unused */, |
Seigo Nonaka | 9155fa6 | 2018-06-05 18:22:03 -0700 | [diff] [blame] | 55 | jint breakStrategy, jint hyphenationFrequency, jboolean isJustified, jintArray indents) { |
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), |
| 58 | static_cast<minikin::HyphenationFrequency>(hyphenationFrequency), |
| 59 | isJustified, |
Seigo Nonaka | 9155fa6 | 2018-06-05 18:22:03 -0700 | [diff] [blame] | 60 | jintArrayToFloatVector(env, indents))); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 61 | } |
Seigo Nonaka | bafe197 | 2017-08-24 15:30:29 -0700 | [diff] [blame] | 62 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 63 | static void nFinish(jlong nativePtr) { |
| 64 | delete toNative(nativePtr); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 65 | } |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 66 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 67 | // CriticalNative |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 68 | static jlong nGetReleaseFunc(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 69 | return reinterpret_cast<jlong>(nFinish); |
| 70 | } |
| 71 | |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 72 | static jlong nComputeLineBreaks(JNIEnv* env, jclass, jlong nativePtr, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 73 | // Inputs |
Seigo Nonaka | ef94f49 | 2017-11-10 16:11:33 -0800 | [diff] [blame] | 74 | jcharArray javaText, |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 75 | jlong measuredTextPtr, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 76 | jint length, |
| 77 | jfloat firstWidth, |
| 78 | jint firstWidthLineCount, |
| 79 | jfloat restWidth, |
Seigo Nonaka | d5621ed | 2019-03-08 16:04:37 -0800 | [diff] [blame] | 80 | jfloatArray variableTabStops, |
| 81 | jfloat defaultTabStop, |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 82 | jint indentsOffset) { |
Seigo Nonaka | 3e7deec | 2017-11-15 12:06:24 -0800 | [diff] [blame] | 83 | minikin::android::StaticLayoutNative* builder = toNative(nativePtr); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 84 | |
Seigo Nonaka | ef94f49 | 2017-11-10 16:11:33 -0800 | [diff] [blame] | 85 | ScopedCharArrayRO text(env, javaText); |
Seigo Nonaka | d5621ed | 2019-03-08 16:04:37 -0800 | [diff] [blame] | 86 | ScopedNullableFloatArrayRO tabStops(env, variableTabStops); |
Seigo Nonaka | ef94f49 | 2017-11-10 16:11:33 -0800 | [diff] [blame] | 87 | |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 88 | minikin::U16StringPiece u16Text(text.get(), length); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 89 | minikin::MeasuredText* measuredText = reinterpret_cast<minikin::MeasuredText*>(measuredTextPtr); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 90 | |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 91 | std::unique_ptr<minikin::LineBreakResult> result = |
| 92 | std::make_unique<minikin::LineBreakResult>(builder->computeBreaks( |
| 93 | u16Text, *measuredText, firstWidth, firstWidthLineCount, restWidth, indentsOffset, |
| 94 | tabStops.get(), tabStops.size(), defaultTabStop)); |
| 95 | return reinterpret_cast<jlong>(result.release()); |
| 96 | } |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 97 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 98 | static jint nGetLineCount(CRITICAL_JNI_PARAMS_COMMA jlong ptr) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 99 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->breakPoints.size(); |
| 100 | } |
| 101 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 102 | static jint nGetLineBreakOffset(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 103 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->breakPoints[i]; |
| 104 | } |
| 105 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 106 | static jfloat nGetLineWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 107 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->widths[i]; |
| 108 | } |
| 109 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 110 | static jfloat nGetLineAscent(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 111 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->ascents[i]; |
| 112 | } |
| 113 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 114 | static jfloat nGetLineDescent(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 115 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->descents[i]; |
| 116 | } |
| 117 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 118 | static jint nGetLineFlag(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 119 | return reinterpret_cast<minikin::LineBreakResult*>(ptr)->flags[i]; |
| 120 | } |
| 121 | |
| 122 | static void nReleaseResult(jlong ptr) { |
| 123 | delete reinterpret_cast<minikin::LineBreakResult*>(ptr); |
| 124 | } |
| 125 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 126 | static jlong nGetReleaseResultFunc(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 127 | return reinterpret_cast<jlong>(nReleaseResult); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 130 | static const JNINativeMethod gMethods[] = { |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 131 | // Fast Natives |
| 132 | {"nInit", "(" |
| 133 | "I" // breakStrategy |
| 134 | "I" // hyphenationFrequency |
| 135 | "Z" // isJustified |
| 136 | "[I" // indents |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 137 | ")J", (void*) nInit}, |
| 138 | |
| 139 | // Critical Natives |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 140 | {"nGetReleaseFunc", "()J", (void*) nGetReleaseFunc}, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 141 | |
| 142 | // Regular JNI |
| 143 | {"nComputeLineBreaks", "(" |
| 144 | "J" // nativePtr |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 145 | "[C" // text |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 146 | "J" // MeasuredParagraph ptr. |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 147 | "I" // length |
| 148 | "F" // firstWidth |
| 149 | "I" // firstWidthLineCount |
| 150 | "F" // restWidth |
Seigo Nonaka | d5621ed | 2019-03-08 16:04:37 -0800 | [diff] [blame] | 151 | "[F" // variableTabStops |
| 152 | "F" // defaultTabStop |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 153 | "I" // indentsOffset |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 154 | ")J", (void*) nComputeLineBreaks}, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 155 | |
Seigo Nonaka | ab9b479 | 2018-09-24 17:37:15 -0700 | [diff] [blame] | 156 | // Result accessors, CriticalNatives |
| 157 | {"nGetLineCount", "(J)I", (void*)nGetLineCount}, |
| 158 | {"nGetLineBreakOffset", "(JI)I", (void*)nGetLineBreakOffset}, |
| 159 | {"nGetLineWidth", "(JI)F", (void*)nGetLineWidth}, |
| 160 | {"nGetLineAscent", "(JI)F", (void*)nGetLineAscent}, |
| 161 | {"nGetLineDescent", "(JI)F", (void*)nGetLineDescent}, |
| 162 | {"nGetLineFlag", "(JI)I", (void*)nGetLineFlag}, |
| 163 | {"nGetReleaseResultFunc", "()J", (void*)nGetReleaseResultFunc}, |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Seigo Nonaka | 70200b0 | 2018-10-01 16:04:11 -0700 | [diff] [blame] | 166 | int register_android_graphics_text_LineBreaker(JNIEnv* env) { |
| 167 | return RegisterMethodsOrDie(env, "android/graphics/text/LineBreaker", gMethods, |
| 168 | NELEM(gMethods)); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | } |