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