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 | |
| 17 | #define LOG_TAG "StaticLayout" |
| 18 | |
| 19 | #include "ScopedIcuLocale.h" |
| 20 | #include "unicode/locid.h" |
| 21 | #include "unicode/brkiter.h" |
| 22 | #include "utils/misc.h" |
| 23 | #include "utils/Log.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 24 | #include <nativehelper/ScopedStringChars.h> |
| 25 | #include <nativehelper/ScopedPrimitiveArray.h> |
| 26 | #include <nativehelper/JNIHelp.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 27 | #include "core_jni_helpers.h" |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 28 | #include <cstdint> |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 29 | #include <vector> |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 30 | #include <list> |
| 31 | #include <algorithm> |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 32 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 33 | #include "SkPaint.h" |
| 34 | #include "SkTypeface.h" |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 35 | #include <hwui/MinikinSkia.h> |
| 36 | #include <hwui/MinikinUtils.h> |
| 37 | #include <hwui/Paint.h> |
| 38 | #include <minikin/FontCollection.h> |
| 39 | #include <minikin/LineBreaker.h> |
| 40 | #include <minikin/MinikinFont.h> |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 41 | |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 42 | namespace android { |
| 43 | |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 44 | struct JLineBreaksID { |
| 45 | jfieldID breaks; |
| 46 | jfieldID widths; |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 47 | jfieldID ascents; |
| 48 | jfieldID descents; |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 49 | jfieldID flags; |
| 50 | }; |
| 51 | |
| 52 | static jclass gLineBreaks_class; |
| 53 | static JLineBreaksID gLineBreaks_fieldID; |
| 54 | |
Seigo Nonaka | 749e57e | 2017-08-29 15:00:05 -0700 | [diff] [blame] | 55 | class JNILineBreakerLineWidth : public minikin::LineBreaker::LineWidthDelegate { |
| 56 | public: |
| 57 | JNILineBreakerLineWidth(float firstWidth, int32_t firstLineCount, float restWidth, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 58 | const std::vector<float>& indents, const std::vector<float>& leftPaddings, |
| 59 | const std::vector<float>& rightPaddings, int32_t indentsAndPaddingsOffset) |
Seigo Nonaka | 749e57e | 2017-08-29 15:00:05 -0700 | [diff] [blame] | 60 | : mFirstWidth(firstWidth), mFirstLineCount(firstLineCount), mRestWidth(restWidth), |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 61 | mIndents(indents), mLeftPaddings(leftPaddings), |
| 62 | mRightPaddings(rightPaddings), mOffset(indentsAndPaddingsOffset) {} |
Seigo Nonaka | 749e57e | 2017-08-29 15:00:05 -0700 | [diff] [blame] | 63 | |
| 64 | float getLineWidth(size_t lineNo) override { |
| 65 | const float width = ((ssize_t)lineNo < (ssize_t)mFirstLineCount) |
| 66 | ? mFirstWidth : mRestWidth; |
Roozbeh Pournader | 0bba742 | 2017-09-28 12:35:10 -0700 | [diff] [blame] | 67 | return width - get(mIndents, lineNo); |
| 68 | } |
Seigo Nonaka | 749e57e | 2017-08-29 15:00:05 -0700 | [diff] [blame] | 69 | |
Roozbeh Pournader | 0bba742 | 2017-09-28 12:35:10 -0700 | [diff] [blame] | 70 | float getLeftPadding(size_t lineNo) override { |
| 71 | return get(mLeftPaddings, lineNo); |
| 72 | } |
| 73 | |
| 74 | float getRightPadding(size_t lineNo) override { |
| 75 | return get(mRightPaddings, lineNo); |
Seigo Nonaka | 749e57e | 2017-08-29 15:00:05 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private: |
Roozbeh Pournader | 0bba742 | 2017-09-28 12:35:10 -0700 | [diff] [blame] | 79 | float get(const std::vector<float>& vec, size_t lineNo) { |
| 80 | if (vec.empty()) { |
| 81 | return 0; |
| 82 | } |
| 83 | const size_t index = lineNo + mOffset; |
| 84 | if (index < vec.size()) { |
| 85 | return vec[index]; |
| 86 | } else { |
| 87 | return vec.back(); |
| 88 | } |
| 89 | } |
| 90 | |
Seigo Nonaka | 749e57e | 2017-08-29 15:00:05 -0700 | [diff] [blame] | 91 | const float mFirstWidth; |
| 92 | const int32_t mFirstLineCount; |
| 93 | const float mRestWidth; |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 94 | const std::vector<float>& mIndents; |
| 95 | const std::vector<float>& mLeftPaddings; |
| 96 | const std::vector<float>& mRightPaddings; |
Roozbeh Pournader | 0bba742 | 2017-09-28 12:35:10 -0700 | [diff] [blame] | 97 | const int32_t mOffset; |
Seigo Nonaka | 749e57e | 2017-08-29 15:00:05 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Roozbeh Pournader | 0bba742 | 2017-09-28 12:35:10 -0700 | [diff] [blame] | 100 | static inline std::vector<float> jintArrayToFloatVector(JNIEnv* env, jintArray javaArray) { |
| 101 | if (javaArray == nullptr) { |
| 102 | return std::vector<float>(); |
| 103 | } else { |
| 104 | ScopedIntArrayRO intArr(env, javaArray); |
| 105 | return std::vector<float>(intArr.get(), intArr.get() + intArr.size()); |
| 106 | } |
| 107 | } |
| 108 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 109 | class Run { |
| 110 | public: |
| 111 | Run(int32_t start, int32_t end) : mStart(start), mEnd(end) {} |
| 112 | virtual ~Run() {} |
| 113 | |
| 114 | virtual void addTo(minikin::LineBreaker* lineBreaker) = 0; |
| 115 | |
| 116 | protected: |
| 117 | const int32_t mStart; |
| 118 | const int32_t mEnd; |
| 119 | |
| 120 | private: |
| 121 | // Forbid copy and assign. |
| 122 | Run(const Run&) = delete; |
| 123 | void operator=(const Run&) = delete; |
| 124 | }; |
| 125 | |
| 126 | class StyleRun : public Run { |
| 127 | public: |
| 128 | StyleRun(int32_t start, int32_t end, minikin::MinikinPaint&& paint, |
| 129 | std::shared_ptr<minikin::FontCollection>&& collection, |
| 130 | minikin::FontStyle&& style, bool isRtl) |
| 131 | : Run(start, end), mPaint(std::move(paint)), mCollection(std::move(collection)), |
| 132 | mStyle(std::move(style)), mIsRtl(isRtl) {} |
| 133 | |
| 134 | void addTo(minikin::LineBreaker* lineBreaker) override { |
| 135 | lineBreaker->addStyleRun(&mPaint, mCollection, mStyle, mStart, mEnd, mIsRtl); |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | minikin::MinikinPaint mPaint; |
| 140 | std::shared_ptr<minikin::FontCollection> mCollection; |
| 141 | minikin::FontStyle mStyle; |
| 142 | const bool mIsRtl; |
| 143 | }; |
| 144 | |
| 145 | class Replacement : public Run { |
| 146 | public: |
| 147 | Replacement(int32_t start, int32_t end, float width, uint32_t localeListId) |
| 148 | : Run(start, end), mWidth(width), mLocaleListId(localeListId) {} |
| 149 | |
| 150 | void addTo(minikin::LineBreaker* lineBreaker) override { |
| 151 | lineBreaker->addReplacement(mStart, mEnd, mWidth, mLocaleListId); |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | const float mWidth; |
| 156 | const uint32_t mLocaleListId; |
| 157 | }; |
| 158 | |
| 159 | class StaticLayoutNative { |
| 160 | public: |
| 161 | StaticLayoutNative( |
| 162 | minikin::BreakStrategy strategy, minikin::HyphenationFrequency frequency, |
| 163 | bool isJustified, std::vector<float>&& indents, std::vector<float>&& leftPaddings, |
| 164 | std::vector<float>&& rightPaddings) |
| 165 | : mStrategy(strategy), mFrequency(frequency), mIsJustified(isJustified), |
| 166 | mIndents(std::move(indents)), mLeftPaddings(std::move(leftPaddings)), |
| 167 | mRightPaddings(std::move(rightPaddings)) {} |
| 168 | |
| 169 | void addStyleRun(int32_t start, int32_t end, minikin::MinikinPaint&& paint, |
| 170 | std::shared_ptr<minikin::FontCollection> collection, |
| 171 | minikin::FontStyle&& style, bool isRtl) { |
| 172 | mRuns.emplace_back(std::make_unique<StyleRun>( |
| 173 | start, end, std::move(paint), std::move(collection), std::move(style), isRtl)); |
| 174 | } |
| 175 | |
| 176 | void addReplacementRun(int32_t start, int32_t end, float width, uint32_t localeListId) { |
| 177 | mRuns.emplace_back(std::make_unique<Replacement>(start, end, width, localeListId)); |
| 178 | } |
| 179 | |
| 180 | // Only valid while this instance is alive. |
| 181 | inline std::unique_ptr<minikin::LineBreaker::LineWidthDelegate> buildLineWidthDelegate( |
| 182 | float firstWidth, int32_t firstLineCount, float restWidth, |
| 183 | int32_t indentsAndPaddingsOffset) { |
| 184 | return std::make_unique<JNILineBreakerLineWidth>( |
| 185 | firstWidth, firstLineCount, restWidth, mIndents, mLeftPaddings, mRightPaddings, |
| 186 | indentsAndPaddingsOffset); |
| 187 | } |
| 188 | |
| 189 | void addRuns(minikin::LineBreaker* lineBreaker) { |
| 190 | for (const auto& run : mRuns) { |
| 191 | run->addTo(lineBreaker); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void clearRuns() { |
| 196 | mRuns.clear(); |
| 197 | } |
| 198 | |
| 199 | inline minikin::BreakStrategy getStrategy() const { return mStrategy; } |
| 200 | inline minikin::HyphenationFrequency getFrequency() const { return mFrequency; } |
| 201 | inline bool isJustified() const { return mIsJustified; } |
| 202 | |
| 203 | private: |
| 204 | const minikin::BreakStrategy mStrategy; |
| 205 | const minikin::HyphenationFrequency mFrequency; |
| 206 | const bool mIsJustified; |
| 207 | const std::vector<float> mIndents; |
| 208 | const std::vector<float> mLeftPaddings; |
| 209 | const std::vector<float> mRightPaddings; |
| 210 | |
| 211 | std::vector<std::unique_ptr<Run>> mRuns; |
| 212 | }; |
| 213 | |
| 214 | static inline StaticLayoutNative* toNative(jlong ptr) { |
| 215 | return reinterpret_cast<StaticLayoutNative*>(ptr); |
| 216 | } |
| 217 | |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 218 | // set text and set a number of parameters for creating a layout (width, tabstops, strategy, |
| 219 | // hyphenFrequency) |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 220 | static jlong nInit(JNIEnv* env, jclass /* unused */, |
| 221 | jint breakStrategy, jint hyphenationFrequency, jboolean isJustified, |
| 222 | jintArray indents, jintArray leftPaddings, jintArray rightPaddings) { |
| 223 | return reinterpret_cast<jlong>(new StaticLayoutNative( |
| 224 | static_cast<minikin::BreakStrategy>(breakStrategy), |
| 225 | static_cast<minikin::HyphenationFrequency>(hyphenationFrequency), |
| 226 | isJustified, |
| 227 | jintArrayToFloatVector(env, indents), |
| 228 | jintArrayToFloatVector(env, leftPaddings), |
| 229 | jintArrayToFloatVector(env, rightPaddings))); |
| 230 | } |
Seigo Nonaka | bafe197 | 2017-08-24 15:30:29 -0700 | [diff] [blame] | 231 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 232 | // CriticalNative |
| 233 | static void nFinish(jlong nativePtr) { |
| 234 | delete toNative(nativePtr); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 235 | } |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 236 | |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 237 | static void recycleCopy(JNIEnv* env, jobject recycle, jintArray recycleBreaks, |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 238 | jfloatArray recycleWidths, jfloatArray recycleAscents, |
| 239 | jfloatArray recycleDescents, jintArray recycleFlags, |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 240 | jint recycleLength, size_t nBreaks, const jint* breaks, |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 241 | const jfloat* widths, const jfloat* ascents, const jfloat* descents, |
| 242 | const jint* flags) { |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 243 | if ((size_t)recycleLength < nBreaks) { |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 244 | // have to reallocate buffers |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 245 | recycleBreaks = env->NewIntArray(nBreaks); |
| 246 | recycleWidths = env->NewFloatArray(nBreaks); |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 247 | recycleAscents = env->NewFloatArray(nBreaks); |
| 248 | recycleDescents = env->NewFloatArray(nBreaks); |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 249 | recycleFlags = env->NewIntArray(nBreaks); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 250 | |
| 251 | env->SetObjectField(recycle, gLineBreaks_fieldID.breaks, recycleBreaks); |
| 252 | env->SetObjectField(recycle, gLineBreaks_fieldID.widths, recycleWidths); |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 253 | env->SetObjectField(recycle, gLineBreaks_fieldID.ascents, recycleAscents); |
| 254 | env->SetObjectField(recycle, gLineBreaks_fieldID.descents, recycleDescents); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 255 | env->SetObjectField(recycle, gLineBreaks_fieldID.flags, recycleFlags); |
| 256 | } |
| 257 | // copy data |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 258 | env->SetIntArrayRegion(recycleBreaks, 0, nBreaks, breaks); |
| 259 | env->SetFloatArrayRegion(recycleWidths, 0, nBreaks, widths); |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 260 | env->SetFloatArrayRegion(recycleAscents, 0, nBreaks, ascents); |
| 261 | env->SetFloatArrayRegion(recycleDescents, 0, nBreaks, descents); |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 262 | env->SetIntArrayRegion(recycleFlags, 0, nBreaks, flags); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 265 | static jint nComputeLineBreaks(JNIEnv* env, jclass, jlong nativePtr, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 266 | // Inputs |
| 267 | jcharArray text, |
| 268 | jint length, |
| 269 | jfloat firstWidth, |
| 270 | jint firstWidthLineCount, |
| 271 | jfloat restWidth, |
| 272 | jintArray variableTabStops, |
| 273 | jint defaultTabStop, |
| 274 | jint indentsOffset, |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 275 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 276 | // Outputs |
| 277 | jobject recycle, |
| 278 | jint recycleLength, |
| 279 | jintArray recycleBreaks, |
| 280 | jfloatArray recycleWidths, |
| 281 | jfloatArray recycleAscents, |
| 282 | jfloatArray recycleDescents, |
| 283 | jintArray recycleFlags, |
| 284 | jfloatArray charWidths) { |
| 285 | |
| 286 | StaticLayoutNative* builder = toNative(nativePtr); |
| 287 | |
| 288 | // TODO: Reorganize minikin APIs. |
| 289 | minikin::LineBreaker b; |
| 290 | b.resize(length); |
| 291 | env->GetCharArrayRegion(text, 0, length, b.buffer()); |
| 292 | b.setText(); |
| 293 | if (variableTabStops == nullptr) { |
| 294 | b.setTabStops(nullptr, 0, defaultTabStop); |
| 295 | } else { |
| 296 | ScopedIntArrayRO stops(env, variableTabStops); |
| 297 | b.setTabStops(stops.get(), stops.size(), defaultTabStop); |
| 298 | } |
| 299 | b.setStrategy(builder->getStrategy()); |
| 300 | b.setHyphenationFrequency(builder->getFrequency()); |
| 301 | b.setJustified(builder->isJustified()); |
| 302 | b.setLineWidthDelegate(builder->buildLineWidthDelegate( |
| 303 | firstWidth, firstWidthLineCount, restWidth, indentsOffset)); |
| 304 | |
| 305 | builder->addRuns(&b); |
| 306 | |
| 307 | size_t nBreaks = b.computeBreaks(); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 308 | |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 309 | recycleCopy(env, recycle, recycleBreaks, recycleWidths, recycleAscents, recycleDescents, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 310 | recycleFlags, recycleLength, nBreaks, b.getBreaks(), b.getWidths(), b.getAscents(), |
| 311 | b.getDescents(), b.getFlags()); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 312 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 313 | env->SetFloatArrayRegion(charWidths, 0, b.size(), b.charWidths()); |
Seigo Nonaka | 2dd1a55 | 2017-10-06 11:41:00 -0700 | [diff] [blame] | 314 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 315 | b.finish(); |
| 316 | builder->clearRuns(); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 317 | |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 318 | return static_cast<jint>(nBreaks); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 321 | // Basically similar to Paint.getTextRunAdvances but with C++ interface |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 322 | // CriticalNative |
| 323 | static void nAddStyleRun(jlong nativePtr, jlong nativePaint, jint start, jint end, jboolean isRtl) { |
| 324 | StaticLayoutNative* builder = toNative(nativePtr); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 325 | Paint* paint = reinterpret_cast<Paint*>(nativePaint); |
Seigo Nonaka | 318ca04 | 2017-08-01 16:36:18 -0700 | [diff] [blame] | 326 | const Typeface* typeface = paint->getAndroidTypeface(); |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 327 | minikin::MinikinPaint minikinPaint; |
Seigo Nonaka | 318ca04 | 2017-08-01 16:36:18 -0700 | [diff] [blame] | 328 | const Typeface* resolvedTypeface = Typeface::resolveDefault(typeface); |
Seigo Nonaka | c706414 | 2017-02-10 16:53:31 +0900 | [diff] [blame] | 329 | minikin::FontStyle style = MinikinUtils::prepareMinikinPaint(&minikinPaint, paint, |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 330 | typeface); |
Seigo Nonaka | 5d518dc | 2017-08-31 14:01:54 -0700 | [diff] [blame] | 331 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 332 | builder->addStyleRun( |
| 333 | start, end, std::move(minikinPaint), resolvedTypeface->fFontCollection, std::move(style), |
| 334 | isRtl); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 337 | // CriticalNative |
| 338 | static void nAddReplacementRun(jlong nativePtr, jlong nativePaint, jint start, jint end, |
| 339 | jfloat width) { |
| 340 | StaticLayoutNative* builder = toNative(nativePtr); |
Seigo Nonaka | f90c9b6 | 2017-10-09 11:10:24 -0700 | [diff] [blame] | 341 | Paint* paint = reinterpret_cast<Paint*>(nativePaint); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 342 | builder->addReplacementRun(start, end, width, paint->getMinikinLangListId()); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 345 | static const JNINativeMethod gMethods[] = { |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 346 | // Fast Natives |
| 347 | {"nInit", "(" |
| 348 | "I" // breakStrategy |
| 349 | "I" // hyphenationFrequency |
| 350 | "Z" // isJustified |
| 351 | "[I" // indents |
| 352 | "[I" // left paddings |
| 353 | "[I" // right paddings |
| 354 | ")J", (void*) nInit}, |
| 355 | |
| 356 | // Critical Natives |
| 357 | {"nFinish", "(J)V", (void*) nFinish}, |
Seigo Nonaka | f90c9b6 | 2017-10-09 11:10:24 -0700 | [diff] [blame] | 358 | {"nAddStyleRun", "(JJIIZ)V", (void*) nAddStyleRun}, |
| 359 | {"nAddReplacementRun", "(JJIIF)V", (void*) nAddReplacementRun}, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 360 | |
| 361 | // Regular JNI |
| 362 | {"nComputeLineBreaks", "(" |
| 363 | "J" // nativePtr |
| 364 | |
| 365 | // Inputs |
| 366 | "[C" // text |
| 367 | "I" // length |
| 368 | "F" // firstWidth |
| 369 | "I" // firstWidthLineCount |
| 370 | "F" // restWidth |
| 371 | "[I" // variableTabStops |
| 372 | "I" // defaultTabStop |
| 373 | "I" // indentsOffset |
| 374 | |
| 375 | // Outputs |
| 376 | "Landroid/text/StaticLayout$LineBreaks;" // recycle |
| 377 | "I" // recycleLength |
| 378 | "[I" // recycleBreaks |
| 379 | "[F" // recycleWidths |
| 380 | "[F" // recycleAscents |
| 381 | "[F" // recycleDescents |
| 382 | "[I" // recycleFlags |
| 383 | "[F" // charWidths |
| 384 | ")I", (void*) nComputeLineBreaks} |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | int register_android_text_StaticLayout(JNIEnv* env) |
| 388 | { |
Andreas Gampe | 158b6c9 | 2014-11-21 14:03:34 -0800 | [diff] [blame] | 389 | gLineBreaks_class = MakeGlobalRefOrDie(env, |
| 390 | FindClassOrDie(env, "android/text/StaticLayout$LineBreaks")); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 391 | |
Andreas Gampe | 158b6c9 | 2014-11-21 14:03:34 -0800 | [diff] [blame] | 392 | gLineBreaks_fieldID.breaks = GetFieldIDOrDie(env, gLineBreaks_class, "breaks", "[I"); |
| 393 | gLineBreaks_fieldID.widths = GetFieldIDOrDie(env, gLineBreaks_class, "widths", "[F"); |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 394 | gLineBreaks_fieldID.ascents = GetFieldIDOrDie(env, gLineBreaks_class, "ascents", "[F"); |
| 395 | gLineBreaks_fieldID.descents = GetFieldIDOrDie(env, gLineBreaks_class, "descents", "[F"); |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 396 | gLineBreaks_fieldID.flags = GetFieldIDOrDie(env, gLineBreaks_class, "flags", "[I"); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 397 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 398 | return RegisterMethodsOrDie(env, "android/text/StaticLayout", gMethods, NELEM(gMethods)); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | } |