Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | package android.text; |
| 18 | |
| 19 | import android.annotation.FloatRange; |
| 20 | import android.annotation.IntRange; |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
| 23 | import android.graphics.Paint; |
| 24 | import android.text.AutoGrowArray.ByteArray; |
| 25 | import android.text.AutoGrowArray.FloatArray; |
| 26 | import android.text.AutoGrowArray.IntArray; |
| 27 | import android.text.Layout.Directions; |
| 28 | import android.text.style.MetricAffectingSpan; |
| 29 | import android.text.style.ReplacementSpan; |
| 30 | import android.util.Pools.SynchronizedPool; |
| 31 | |
| 32 | import dalvik.annotation.optimization.CriticalNative; |
| 33 | |
| 34 | import libcore.util.NativeAllocationRegistry; |
| 35 | |
| 36 | import java.util.Arrays; |
| 37 | |
| 38 | /** |
| 39 | * MeasuredParagraph provides text information for rendering purpose. |
| 40 | * |
| 41 | * The first motivation of this class is identify the text directions and retrieving individual |
| 42 | * character widths. However retrieving character widths is slower than identifying text directions. |
| 43 | * Thus, this class provides several builder methods for specific purposes. |
| 44 | * |
| 45 | * - buildForBidi: |
| 46 | * Compute only text directions. |
| 47 | * - buildForMeasurement: |
| 48 | * Compute text direction and all character widths. |
| 49 | * - buildForStaticLayout: |
| 50 | * This is bit special. StaticLayout also needs to know text direction and character widths for |
| 51 | * line breaking, but all things are done in native code. Similarly, text measurement is done |
| 52 | * in native code. So instead of storing result to Java array, this keeps the result in native |
| 53 | * code since there is no good reason to move the results to Java layer. |
| 54 | * |
| 55 | * In addition to the character widths, some additional information is computed for each purposes, |
| 56 | * e.g. whole text length for measurement or font metrics for static layout. |
| 57 | * |
| 58 | * MeasuredParagraph is NOT a thread safe object. |
| 59 | * @hide |
| 60 | */ |
| 61 | public class MeasuredParagraph { |
| 62 | private static final char OBJECT_REPLACEMENT_CHARACTER = '\uFFFC'; |
| 63 | |
| 64 | private static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry( |
| 65 | MeasuredParagraph.class.getClassLoader(), nGetReleaseFunc(), 1024); |
| 66 | |
| 67 | private MeasuredParagraph() {} // Use build static functions instead. |
| 68 | |
| 69 | private static final SynchronizedPool<MeasuredParagraph> sPool = new SynchronizedPool<>(1); |
| 70 | |
| 71 | private static @NonNull MeasuredParagraph obtain() { // Use build static functions instead. |
| 72 | final MeasuredParagraph mt = sPool.acquire(); |
| 73 | return mt != null ? mt : new MeasuredParagraph(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Recycle the MeasuredParagraph. |
| 78 | * |
| 79 | * Do not call any methods after you call this method. |
| 80 | */ |
| 81 | public void recycle() { |
| 82 | release(); |
| 83 | sPool.release(this); |
| 84 | } |
| 85 | |
| 86 | // The casted original text. |
| 87 | // |
| 88 | // This may be null if the passed text is not a Spanned. |
| 89 | private @Nullable Spanned mSpanned; |
| 90 | |
| 91 | // The start offset of the target range in the original text (mSpanned); |
| 92 | private @IntRange(from = 0) int mTextStart; |
| 93 | |
| 94 | // The length of the target range in the original text. |
| 95 | private @IntRange(from = 0) int mTextLength; |
| 96 | |
| 97 | // The copied character buffer for measuring text. |
| 98 | // |
| 99 | // The length of this array is mTextLength. |
| 100 | private @Nullable char[] mCopiedBuffer; |
| 101 | |
| 102 | // The whole paragraph direction. |
| 103 | private @Layout.Direction int mParaDir; |
| 104 | |
| 105 | // True if the text is LTR direction and doesn't contain any bidi characters. |
| 106 | private boolean mLtrWithoutBidi; |
| 107 | |
| 108 | // The bidi level for individual characters. |
| 109 | // |
| 110 | // This is empty if mLtrWithoutBidi is true. |
| 111 | private @NonNull ByteArray mLevels = new ByteArray(); |
| 112 | |
| 113 | // The whole width of the text. |
| 114 | // See getWholeWidth comments. |
| 115 | private @FloatRange(from = 0.0f) float mWholeWidth; |
| 116 | |
| 117 | // Individual characters' widths. |
| 118 | // See getWidths comments. |
| 119 | private @Nullable FloatArray mWidths = new FloatArray(); |
| 120 | |
| 121 | // The span end positions. |
| 122 | // See getSpanEndCache comments. |
| 123 | private @Nullable IntArray mSpanEndCache = new IntArray(4); |
| 124 | |
| 125 | // The font metrics. |
| 126 | // See getFontMetrics comments. |
| 127 | private @Nullable IntArray mFontMetrics = new IntArray(4 * 4); |
| 128 | |
| 129 | // The native MeasuredParagraph. |
| 130 | // See getNativePtr comments. |
| 131 | // Do not modify these members directly. Use bindNativeObject/unbindNativeObject instead. |
| 132 | private /* Maybe Zero */ long mNativePtr = 0; |
| 133 | private @Nullable Runnable mNativeObjectCleaner; |
| 134 | |
| 135 | // Associate the native object to this Java object. |
| 136 | private void bindNativeObject(/* Non Zero*/ long nativePtr) { |
| 137 | mNativePtr = nativePtr; |
| 138 | mNativeObjectCleaner = sRegistry.registerNativeAllocation(this, nativePtr); |
| 139 | } |
| 140 | |
| 141 | // Decouple the native object from this Java object and release the native object. |
| 142 | private void unbindNativeObject() { |
| 143 | if (mNativePtr != 0) { |
| 144 | mNativeObjectCleaner.run(); |
| 145 | mNativePtr = 0; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Following two objects are for avoiding object allocation. |
| 150 | private @NonNull TextPaint mCachedPaint = new TextPaint(); |
| 151 | private @Nullable Paint.FontMetricsInt mCachedFm; |
| 152 | |
| 153 | /** |
| 154 | * Releases internal buffers. |
| 155 | */ |
| 156 | public void release() { |
| 157 | reset(); |
| 158 | mLevels.clearWithReleasingLargeArray(); |
| 159 | mWidths.clearWithReleasingLargeArray(); |
| 160 | mFontMetrics.clearWithReleasingLargeArray(); |
| 161 | mSpanEndCache.clearWithReleasingLargeArray(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Resets the internal state for starting new text. |
| 166 | */ |
| 167 | private void reset() { |
| 168 | mSpanned = null; |
| 169 | mCopiedBuffer = null; |
| 170 | mWholeWidth = 0; |
| 171 | mLevels.clear(); |
| 172 | mWidths.clear(); |
| 173 | mFontMetrics.clear(); |
| 174 | mSpanEndCache.clear(); |
| 175 | unbindNativeObject(); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Returns the characters to be measured. |
| 180 | * |
| 181 | * This is always available. |
| 182 | */ |
| 183 | public @NonNull char[] getChars() { |
| 184 | return mCopiedBuffer; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns the paragraph direction. |
| 189 | * |
| 190 | * This is always available. |
| 191 | */ |
| 192 | public @Layout.Direction int getParagraphDir() { |
| 193 | return mParaDir; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Returns the directions. |
| 198 | * |
| 199 | * This is always available. |
| 200 | */ |
| 201 | public Directions getDirections(@IntRange(from = 0) int start, // inclusive |
| 202 | @IntRange(from = 0) int end) { // exclusive |
| 203 | if (mLtrWithoutBidi) { |
| 204 | return Layout.DIRS_ALL_LEFT_TO_RIGHT; |
| 205 | } |
| 206 | |
| 207 | final int length = end - start; |
| 208 | return AndroidBidi.directions(mParaDir, mLevels.getRawArray(), start, mCopiedBuffer, start, |
| 209 | length); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Returns the whole text width. |
| 214 | * |
| 215 | * This is available only if the MeasureText is computed with computeForMeasurement. |
| 216 | * Returns 0 in other cases. |
| 217 | */ |
| 218 | public @FloatRange(from = 0.0f) float getWholeWidth() { |
| 219 | return mWholeWidth; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Returns the individual character's width. |
| 224 | * |
| 225 | * This is available only if the MeasureText is computed with computeForMeasurement. |
| 226 | * Returns empty array in other cases. |
| 227 | */ |
| 228 | public @NonNull FloatArray getWidths() { |
| 229 | return mWidths; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Returns the MetricsAffectingSpan end indices. |
| 234 | * |
| 235 | * If the input text is not a spanned string, this has one value that is the length of the text. |
| 236 | * |
| 237 | * This is available only if the MeasureText is computed with computeForStaticLayout. |
| 238 | * Returns empty array in other cases. |
| 239 | */ |
| 240 | public @NonNull IntArray getSpanEndCache() { |
| 241 | return mSpanEndCache; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Returns the int array which holds FontMetrics. |
| 246 | * |
| 247 | * This array holds the repeat of top, bottom, ascent, descent of font metrics value. |
| 248 | * |
| 249 | * This is available only if the MeasureText is computed with computeForStaticLayout. |
| 250 | * Returns empty array in other cases. |
| 251 | */ |
| 252 | public @NonNull IntArray getFontMetrics() { |
| 253 | return mFontMetrics; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Returns the native ptr of the MeasuredParagraph. |
| 258 | * |
| 259 | * This is available only if the MeasureText is computed with computeForStaticLayout. |
| 260 | * Returns 0 in other cases. |
| 261 | */ |
| 262 | public /* Maybe Zero */ long getNativePtr() { |
| 263 | return mNativePtr; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Generates new MeasuredParagraph for Bidi computation. |
| 268 | * |
| 269 | * If recycle is null, this returns new instance. If recycle is not null, this fills computed |
| 270 | * result to recycle and returns recycle. |
| 271 | * |
| 272 | * @param text the character sequence to be measured |
| 273 | * @param start the inclusive start offset of the target region in the text |
| 274 | * @param end the exclusive end offset of the target region in the text |
| 275 | * @param textDir the text direction |
| 276 | * @param recycle pass existing MeasuredParagraph if you want to recycle it. |
| 277 | * |
| 278 | * @return measured text |
| 279 | */ |
| 280 | public static @NonNull MeasuredParagraph buildForBidi(@NonNull CharSequence text, |
| 281 | @IntRange(from = 0) int start, |
| 282 | @IntRange(from = 0) int end, |
| 283 | @NonNull TextDirectionHeuristic textDir, |
| 284 | @Nullable MeasuredParagraph recycle) { |
| 285 | final MeasuredParagraph mt = recycle == null ? obtain() : recycle; |
| 286 | mt.resetAndAnalyzeBidi(text, start, end, textDir); |
| 287 | return mt; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Generates new MeasuredParagraph for measuring texts. |
| 292 | * |
| 293 | * If recycle is null, this returns new instance. If recycle is not null, this fills computed |
| 294 | * result to recycle and returns recycle. |
| 295 | * |
| 296 | * @param paint the paint to be used for rendering the text. |
| 297 | * @param text the character sequence to be measured |
| 298 | * @param start the inclusive start offset of the target region in the text |
| 299 | * @param end the exclusive end offset of the target region in the text |
| 300 | * @param textDir the text direction |
| 301 | * @param recycle pass existing MeasuredParagraph if you want to recycle it. |
| 302 | * |
| 303 | * @return measured text |
| 304 | */ |
| 305 | public static @NonNull MeasuredParagraph buildForMeasurement(@NonNull TextPaint paint, |
| 306 | @NonNull CharSequence text, |
| 307 | @IntRange(from = 0) int start, |
| 308 | @IntRange(from = 0) int end, |
| 309 | @NonNull TextDirectionHeuristic textDir, |
| 310 | @Nullable MeasuredParagraph recycle) { |
| 311 | final MeasuredParagraph mt = recycle == null ? obtain() : recycle; |
| 312 | mt.resetAndAnalyzeBidi(text, start, end, textDir); |
| 313 | |
| 314 | mt.mWidths.resize(mt.mTextLength); |
| 315 | if (mt.mTextLength == 0) { |
| 316 | return mt; |
| 317 | } |
| 318 | |
| 319 | if (mt.mSpanned == null) { |
| 320 | // No style change by MetricsAffectingSpan. Just measure all text. |
| 321 | mt.applyMetricsAffectingSpan( |
| 322 | paint, null /* spans */, start, end, 0 /* native static layout ptr */); |
| 323 | } else { |
| 324 | // There may be a MetricsAffectingSpan. Split into span transitions and apply styles. |
| 325 | int spanEnd; |
| 326 | for (int spanStart = start; spanStart < end; spanStart = spanEnd) { |
| 327 | spanEnd = mt.mSpanned.nextSpanTransition(spanStart, end, MetricAffectingSpan.class); |
| 328 | MetricAffectingSpan[] spans = mt.mSpanned.getSpans(spanStart, spanEnd, |
| 329 | MetricAffectingSpan.class); |
| 330 | spans = TextUtils.removeEmptySpans(spans, mt.mSpanned, MetricAffectingSpan.class); |
| 331 | mt.applyMetricsAffectingSpan( |
| 332 | paint, spans, spanStart, spanEnd, 0 /* native static layout ptr */); |
| 333 | } |
| 334 | } |
| 335 | return mt; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Generates new MeasuredParagraph for StaticLayout. |
| 340 | * |
| 341 | * If recycle is null, this returns new instance. If recycle is not null, this fills computed |
| 342 | * result to recycle and returns recycle. |
| 343 | * |
| 344 | * @param paint the paint to be used for rendering the text. |
| 345 | * @param text the character sequence to be measured |
| 346 | * @param start the inclusive start offset of the target region in the text |
| 347 | * @param end the exclusive end offset of the target region in the text |
| 348 | * @param textDir the text direction |
| 349 | * @param recycle pass existing MeasuredParagraph if you want to recycle it. |
| 350 | * |
| 351 | * @return measured text |
| 352 | */ |
| 353 | public static @NonNull MeasuredParagraph buildForStaticLayout( |
| 354 | @NonNull TextPaint paint, |
| 355 | @NonNull CharSequence text, |
| 356 | @IntRange(from = 0) int start, |
| 357 | @IntRange(from = 0) int end, |
| 358 | @NonNull TextDirectionHeuristic textDir, |
Seigo Nonaka | 87b1547 | 2018-01-12 14:06:29 -0800 | [diff] [blame^] | 359 | boolean computeHyphenation, |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 360 | @Nullable MeasuredParagraph recycle) { |
| 361 | final MeasuredParagraph mt = recycle == null ? obtain() : recycle; |
| 362 | mt.resetAndAnalyzeBidi(text, start, end, textDir); |
| 363 | if (mt.mTextLength == 0) { |
| 364 | // Need to build empty native measured text for StaticLayout. |
| 365 | // TODO: Stop creating empty measured text for empty lines. |
| 366 | long nativeBuilderPtr = nInitBuilder(); |
| 367 | try { |
| 368 | mt.bindNativeObject( |
Seigo Nonaka | 87b1547 | 2018-01-12 14:06:29 -0800 | [diff] [blame^] | 369 | nBuildNativeMeasuredParagraph(nativeBuilderPtr, mt.mCopiedBuffer, |
| 370 | computeHyphenation)); |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 371 | } finally { |
| 372 | nFreeBuilder(nativeBuilderPtr); |
| 373 | } |
| 374 | return mt; |
| 375 | } |
| 376 | |
| 377 | long nativeBuilderPtr = nInitBuilder(); |
| 378 | try { |
| 379 | if (mt.mSpanned == null) { |
| 380 | // No style change by MetricsAffectingSpan. Just measure all text. |
| 381 | mt.applyMetricsAffectingSpan(paint, null /* spans */, start, end, nativeBuilderPtr); |
| 382 | mt.mSpanEndCache.append(end); |
| 383 | } else { |
| 384 | // There may be a MetricsAffectingSpan. Split into span transitions and apply |
| 385 | // styles. |
| 386 | int spanEnd; |
| 387 | for (int spanStart = start; spanStart < end; spanStart = spanEnd) { |
| 388 | spanEnd = mt.mSpanned.nextSpanTransition(spanStart, end, |
| 389 | MetricAffectingSpan.class); |
| 390 | MetricAffectingSpan[] spans = mt.mSpanned.getSpans(spanStart, spanEnd, |
| 391 | MetricAffectingSpan.class); |
| 392 | spans = TextUtils.removeEmptySpans(spans, mt.mSpanned, |
| 393 | MetricAffectingSpan.class); |
| 394 | mt.applyMetricsAffectingSpan(paint, spans, spanStart, spanEnd, |
| 395 | nativeBuilderPtr); |
| 396 | mt.mSpanEndCache.append(spanEnd); |
| 397 | } |
| 398 | } |
Seigo Nonaka | 87b1547 | 2018-01-12 14:06:29 -0800 | [diff] [blame^] | 399 | mt.bindNativeObject(nBuildNativeMeasuredParagraph(nativeBuilderPtr, mt.mCopiedBuffer, |
| 400 | computeHyphenation)); |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 401 | } finally { |
| 402 | nFreeBuilder(nativeBuilderPtr); |
| 403 | } |
| 404 | |
| 405 | return mt; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Reset internal state and analyzes text for bidirectional runs. |
| 410 | * |
| 411 | * @param text the character sequence to be measured |
| 412 | * @param start the inclusive start offset of the target region in the text |
| 413 | * @param end the exclusive end offset of the target region in the text |
| 414 | * @param textDir the text direction |
| 415 | */ |
| 416 | private void resetAndAnalyzeBidi(@NonNull CharSequence text, |
| 417 | @IntRange(from = 0) int start, // inclusive |
| 418 | @IntRange(from = 0) int end, // exclusive |
| 419 | @NonNull TextDirectionHeuristic textDir) { |
| 420 | reset(); |
| 421 | mSpanned = text instanceof Spanned ? (Spanned) text : null; |
| 422 | mTextStart = start; |
| 423 | mTextLength = end - start; |
| 424 | |
| 425 | if (mCopiedBuffer == null || mCopiedBuffer.length != mTextLength) { |
| 426 | mCopiedBuffer = new char[mTextLength]; |
| 427 | } |
| 428 | TextUtils.getChars(text, start, end, mCopiedBuffer, 0); |
| 429 | |
| 430 | // Replace characters associated with ReplacementSpan to U+FFFC. |
| 431 | if (mSpanned != null) { |
| 432 | ReplacementSpan[] spans = mSpanned.getSpans(start, end, ReplacementSpan.class); |
| 433 | |
| 434 | for (int i = 0; i < spans.length; i++) { |
| 435 | int startInPara = mSpanned.getSpanStart(spans[i]) - start; |
| 436 | int endInPara = mSpanned.getSpanEnd(spans[i]) - start; |
| 437 | // The span interval may be larger and must be restricted to [start, end) |
| 438 | if (startInPara < 0) startInPara = 0; |
| 439 | if (endInPara > mTextLength) endInPara = mTextLength; |
| 440 | Arrays.fill(mCopiedBuffer, startInPara, endInPara, OBJECT_REPLACEMENT_CHARACTER); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | if ((textDir == TextDirectionHeuristics.LTR |
| 445 | || textDir == TextDirectionHeuristics.FIRSTSTRONG_LTR |
| 446 | || textDir == TextDirectionHeuristics.ANYRTL_LTR) |
| 447 | && TextUtils.doesNotNeedBidi(mCopiedBuffer, 0, mTextLength)) { |
| 448 | mLevels.clear(); |
| 449 | mParaDir = Layout.DIR_LEFT_TO_RIGHT; |
| 450 | mLtrWithoutBidi = true; |
| 451 | } else { |
| 452 | final int bidiRequest; |
| 453 | if (textDir == TextDirectionHeuristics.LTR) { |
| 454 | bidiRequest = Layout.DIR_REQUEST_LTR; |
| 455 | } else if (textDir == TextDirectionHeuristics.RTL) { |
| 456 | bidiRequest = Layout.DIR_REQUEST_RTL; |
| 457 | } else if (textDir == TextDirectionHeuristics.FIRSTSTRONG_LTR) { |
| 458 | bidiRequest = Layout.DIR_REQUEST_DEFAULT_LTR; |
| 459 | } else if (textDir == TextDirectionHeuristics.FIRSTSTRONG_RTL) { |
| 460 | bidiRequest = Layout.DIR_REQUEST_DEFAULT_RTL; |
| 461 | } else { |
| 462 | final boolean isRtl = textDir.isRtl(mCopiedBuffer, 0, mTextLength); |
| 463 | bidiRequest = isRtl ? Layout.DIR_REQUEST_RTL : Layout.DIR_REQUEST_LTR; |
| 464 | } |
| 465 | mLevels.resize(mTextLength); |
| 466 | mParaDir = AndroidBidi.bidi(bidiRequest, mCopiedBuffer, mLevels.getRawArray()); |
| 467 | mLtrWithoutBidi = false; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | private void applyReplacementRun(@NonNull ReplacementSpan replacement, |
| 472 | @IntRange(from = 0) int start, // inclusive, in copied buffer |
| 473 | @IntRange(from = 0) int end, // exclusive, in copied buffer |
| 474 | /* Maybe Zero */ long nativeBuilderPtr) { |
| 475 | // Use original text. Shouldn't matter. |
| 476 | // TODO: passing uninitizlied FontMetrics to developers. Do we need to keep this for |
| 477 | // backward compatibility? or Should we initialize them for getFontMetricsInt? |
| 478 | final float width = replacement.getSize( |
| 479 | mCachedPaint, mSpanned, start + mTextStart, end + mTextStart, mCachedFm); |
| 480 | if (nativeBuilderPtr == 0) { |
| 481 | // Assigns all width to the first character. This is the same behavior as minikin. |
| 482 | mWidths.set(start, width); |
| 483 | if (end > start + 1) { |
| 484 | Arrays.fill(mWidths.getRawArray(), start + 1, end, 0.0f); |
| 485 | } |
| 486 | mWholeWidth += width; |
| 487 | } else { |
| 488 | nAddReplacementRun(nativeBuilderPtr, mCachedPaint.getNativeInstance(), start, end, |
| 489 | width); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | private void applyStyleRun(@IntRange(from = 0) int start, // inclusive, in copied buffer |
| 494 | @IntRange(from = 0) int end, // exclusive, in copied buffer |
| 495 | /* Maybe Zero */ long nativeBuilderPtr) { |
| 496 | if (nativeBuilderPtr != 0) { |
| 497 | mCachedPaint.getFontMetricsInt(mCachedFm); |
| 498 | } |
| 499 | |
| 500 | if (mLtrWithoutBidi) { |
| 501 | // If the whole text is LTR direction, just apply whole region. |
| 502 | if (nativeBuilderPtr == 0) { |
| 503 | mWholeWidth += mCachedPaint.getTextRunAdvances( |
| 504 | mCopiedBuffer, start, end - start, start, end - start, false /* isRtl */, |
| 505 | mWidths.getRawArray(), start); |
| 506 | } else { |
| 507 | nAddStyleRun(nativeBuilderPtr, mCachedPaint.getNativeInstance(), start, end, |
| 508 | false /* isRtl */); |
| 509 | } |
| 510 | } else { |
| 511 | // If there is multiple bidi levels, split into individual bidi level and apply style. |
| 512 | byte level = mLevels.get(start); |
| 513 | // Note that the empty text or empty range won't reach this method. |
| 514 | // Safe to search from start + 1. |
| 515 | for (int levelStart = start, levelEnd = start + 1;; ++levelEnd) { |
| 516 | if (levelEnd == end || mLevels.get(levelEnd) != level) { // transition point |
| 517 | final boolean isRtl = (level & 0x1) != 0; |
| 518 | if (nativeBuilderPtr == 0) { |
| 519 | final int levelLength = levelEnd - levelStart; |
| 520 | mWholeWidth += mCachedPaint.getTextRunAdvances( |
| 521 | mCopiedBuffer, levelStart, levelLength, levelStart, levelLength, |
| 522 | isRtl, mWidths.getRawArray(), levelStart); |
| 523 | } else { |
| 524 | nAddStyleRun(nativeBuilderPtr, mCachedPaint.getNativeInstance(), levelStart, |
| 525 | levelEnd, isRtl); |
| 526 | } |
| 527 | if (levelEnd == end) { |
| 528 | break; |
| 529 | } |
| 530 | levelStart = levelEnd; |
| 531 | level = mLevels.get(levelEnd); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | private void applyMetricsAffectingSpan( |
| 538 | @NonNull TextPaint paint, |
| 539 | @Nullable MetricAffectingSpan[] spans, |
| 540 | @IntRange(from = 0) int start, // inclusive, in original text buffer |
| 541 | @IntRange(from = 0) int end, // exclusive, in original text buffer |
| 542 | /* Maybe Zero */ long nativeBuilderPtr) { |
| 543 | mCachedPaint.set(paint); |
| 544 | // XXX paint should not have a baseline shift, but... |
| 545 | mCachedPaint.baselineShift = 0; |
| 546 | |
| 547 | final boolean needFontMetrics = nativeBuilderPtr != 0; |
| 548 | |
| 549 | if (needFontMetrics && mCachedFm == null) { |
| 550 | mCachedFm = new Paint.FontMetricsInt(); |
| 551 | } |
| 552 | |
| 553 | ReplacementSpan replacement = null; |
| 554 | if (spans != null) { |
| 555 | for (int i = 0; i < spans.length; i++) { |
| 556 | MetricAffectingSpan span = spans[i]; |
| 557 | if (span instanceof ReplacementSpan) { |
| 558 | // The last ReplacementSpan is effective for backward compatibility reasons. |
| 559 | replacement = (ReplacementSpan) span; |
| 560 | } else { |
| 561 | // TODO: No need to call updateMeasureState for ReplacementSpan as well? |
| 562 | span.updateMeasureState(mCachedPaint); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | final int startInCopiedBuffer = start - mTextStart; |
| 568 | final int endInCopiedBuffer = end - mTextStart; |
| 569 | |
| 570 | if (replacement != null) { |
| 571 | applyReplacementRun(replacement, startInCopiedBuffer, endInCopiedBuffer, |
| 572 | nativeBuilderPtr); |
| 573 | } else { |
| 574 | applyStyleRun(startInCopiedBuffer, endInCopiedBuffer, nativeBuilderPtr); |
| 575 | } |
| 576 | |
| 577 | if (needFontMetrics) { |
| 578 | if (mCachedPaint.baselineShift < 0) { |
| 579 | mCachedFm.ascent += mCachedPaint.baselineShift; |
| 580 | mCachedFm.top += mCachedPaint.baselineShift; |
| 581 | } else { |
| 582 | mCachedFm.descent += mCachedPaint.baselineShift; |
| 583 | mCachedFm.bottom += mCachedPaint.baselineShift; |
| 584 | } |
| 585 | |
| 586 | mFontMetrics.append(mCachedFm.top); |
| 587 | mFontMetrics.append(mCachedFm.bottom); |
| 588 | mFontMetrics.append(mCachedFm.ascent); |
| 589 | mFontMetrics.append(mCachedFm.descent); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Returns the maximum index that the accumulated width not exceeds the width. |
| 595 | * |
| 596 | * If forward=false is passed, returns the minimum index from the end instead. |
| 597 | * |
| 598 | * This only works if the MeasuredParagraph is computed with computeForMeasurement. |
| 599 | * Undefined behavior in other case. |
| 600 | */ |
| 601 | @IntRange(from = 0) int breakText(int limit, boolean forwards, float width) { |
| 602 | float[] w = mWidths.getRawArray(); |
| 603 | if (forwards) { |
| 604 | int i = 0; |
| 605 | while (i < limit) { |
| 606 | width -= w[i]; |
| 607 | if (width < 0.0f) break; |
| 608 | i++; |
| 609 | } |
| 610 | while (i > 0 && mCopiedBuffer[i - 1] == ' ') i--; |
| 611 | return i; |
| 612 | } else { |
| 613 | int i = limit - 1; |
| 614 | while (i >= 0) { |
| 615 | width -= w[i]; |
| 616 | if (width < 0.0f) break; |
| 617 | i--; |
| 618 | } |
| 619 | while (i < limit - 1 && (mCopiedBuffer[i + 1] == ' ' || w[i + 1] == 0.0f)) { |
| 620 | i++; |
| 621 | } |
| 622 | return limit - i - 1; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Returns the length of the substring. |
| 628 | * |
| 629 | * This only works if the MeasuredParagraph is computed with computeForMeasurement. |
| 630 | * Undefined behavior in other case. |
| 631 | */ |
| 632 | @FloatRange(from = 0.0f) float measure(int start, int limit) { |
| 633 | float width = 0; |
| 634 | float[] w = mWidths.getRawArray(); |
| 635 | for (int i = start; i < limit; ++i) { |
| 636 | width += w[i]; |
| 637 | } |
| 638 | return width; |
| 639 | } |
| 640 | |
| 641 | private static native /* Non Zero */ long nInitBuilder(); |
| 642 | |
| 643 | /** |
| 644 | * Apply style to make native measured text. |
| 645 | * |
| 646 | * @param nativeBuilderPtr The native MeasuredParagraph builder pointer. |
| 647 | * @param paintPtr The native paint pointer to be applied. |
| 648 | * @param start The start offset in the copied buffer. |
| 649 | * @param end The end offset in the copied buffer. |
| 650 | * @param isRtl True if the text is RTL. |
| 651 | */ |
| 652 | private static native void nAddStyleRun(/* Non Zero */ long nativeBuilderPtr, |
| 653 | /* Non Zero */ long paintPtr, |
| 654 | @IntRange(from = 0) int start, |
| 655 | @IntRange(from = 0) int end, |
| 656 | boolean isRtl); |
| 657 | |
| 658 | /** |
| 659 | * Apply ReplacementRun to make native measured text. |
| 660 | * |
| 661 | * @param nativeBuilderPtr The native MeasuredParagraph builder pointer. |
| 662 | * @param paintPtr The native paint pointer to be applied. |
| 663 | * @param start The start offset in the copied buffer. |
| 664 | * @param end The end offset in the copied buffer. |
| 665 | * @param width The width of the replacement. |
| 666 | */ |
| 667 | private static native void nAddReplacementRun(/* Non Zero */ long nativeBuilderPtr, |
| 668 | /* Non Zero */ long paintPtr, |
| 669 | @IntRange(from = 0) int start, |
| 670 | @IntRange(from = 0) int end, |
| 671 | @FloatRange(from = 0) float width); |
| 672 | |
| 673 | private static native long nBuildNativeMeasuredParagraph(/* Non Zero */ long nativeBuilderPtr, |
Seigo Nonaka | 87b1547 | 2018-01-12 14:06:29 -0800 | [diff] [blame^] | 674 | @NonNull char[] text, |
| 675 | boolean computeHyphenation); |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 676 | |
| 677 | private static native void nFreeBuilder(/* Non Zero */ long nativeBuilderPtr); |
| 678 | |
| 679 | @CriticalNative |
| 680 | private static native /* Non Zero */ long nGetReleaseFunc(); |
| 681 | } |