| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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.graphics.Rect; |
| 25 |
import android.graphics.text.LineBreakConfig; |
| 26 |
import android.graphics.text.MeasuredText; |
| 27 |
import android.text.AutoGrowArray.ByteArray; |
| 28 |
import android.text.AutoGrowArray.FloatArray; |
| 29 |
import android.text.AutoGrowArray.IntArray; |
| 30 |
import android.text.Layout.Directions; |
| 31 |
import android.text.style.MetricAffectingSpan; |
| 32 |
import android.text.style.ReplacementSpan; |
| 33 |
import android.util.Pools.SynchronizedPool; |
| 34 |
|
| 35 |
import java.util.Arrays; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public class MeasuredParagraph { |
| 61 |
private static final char OBJECT_REPLACEMENT_CHARACTER = '\uFFFC'; |
| 62 |
|
| 63 |
private MeasuredParagraph() {} |
| 64 |
|
| 65 |
private static final SynchronizedPool<MeasuredParagraph> sPool = new SynchronizedPool<>(1); |
| 66 |
|
| 67 |
private static @NonNull MeasuredParagraph obtain() { |
| 68 |
final MeasuredParagraph mt = sPool.acquire(); |
| 69 |
return mt != null ? mt : new MeasuredParagraph(); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public void recycle() { |
| 78 |
release(); |
| 79 |
sPool.release(this); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
private @Nullable Spanned mSpanned; |
| 86 |
|
| 87 |
|
| 88 |
private @IntRange(from = 0) int mTextStart; |
| 89 |
|
| 90 |
|
| 91 |
private @IntRange(from = 0) int mTextLength; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private @Nullable char[] mCopiedBuffer; |
| 97 |
|
| 98 |
|
| 99 |
private @Layout.Direction int mParaDir; |
| 100 |
|
| 101 |
|
| 102 |
private boolean mLtrWithoutBidi; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
private @NonNull ByteArray mLevels = new ByteArray(); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private @FloatRange(from = 0.0f) float mWholeWidth; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
private @Nullable FloatArray mWidths = new FloatArray(); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private @Nullable IntArray mSpanEndCache = new IntArray(4); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private @Nullable IntArray mFontMetrics = new IntArray(4 * 4); |
| 124 |
|
| 125 |
|
| 126 |
private @Nullable MeasuredText mMeasuredText; |
| 127 |
|
| 128 |
|
| 129 |
private @NonNull TextPaint mCachedPaint = new TextPaint(); |
| 130 |
private @Nullable Paint.FontMetricsInt mCachedFm; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public void release() { |
| 136 |
reset(); |
| 137 |
mLevels.clearWithReleasingLargeArray(); |
| 138 |
mWidths.clearWithReleasingLargeArray(); |
| 139 |
mFontMetrics.clearWithReleasingLargeArray(); |
| 140 |
mSpanEndCache.clearWithReleasingLargeArray(); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private void reset() { |
| 147 |
mSpanned = null; |
| 148 |
mCopiedBuffer = null; |
| 149 |
mWholeWidth = 0; |
| 150 |
mLevels.clear(); |
| 151 |
mWidths.clear(); |
| 152 |
mFontMetrics.clear(); |
| 153 |
mSpanEndCache.clear(); |
| 154 |
mMeasuredText = null; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public int getTextLength() { |
| 163 |
return mTextLength; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public @NonNull char[] getChars() { |
| 172 |
return mCopiedBuffer; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public @Layout.Direction int getParagraphDir() { |
| 181 |
return mParaDir; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public Directions getDirections(@IntRange(from = 0) int start, |
| 190 |
@IntRange(from = 0) int end) { |
| 191 |
if (mLtrWithoutBidi) { |
| 192 |
return Layout.DIRS_ALL_LEFT_TO_RIGHT; |
| 193 |
} |
| 194 |
|
| 195 |
final int length = end - start; |
| 196 |
return AndroidBidi.directions(mParaDir, mLevels.getRawArray(), start, mCopiedBuffer, start, |
| 197 |
length); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public @FloatRange(from = 0.0f) float getWholeWidth() { |
| 207 |
return mWholeWidth; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public @NonNull FloatArray getWidths() { |
| 217 |
return mWidths; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public @NonNull IntArray getSpanEndCache() { |
| 229 |
return mSpanEndCache; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public @NonNull IntArray getFontMetrics() { |
| 241 |
return mFontMetrics; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public MeasuredText getMeasuredText() { |
| 251 |
return mMeasuredText; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public float getWidth(int start, int end) { |
| 264 |
if (mMeasuredText == null) { |
| 265 |
|
| 266 |
final float[] widths = mWidths.getRawArray(); |
| 267 |
float r = 0.0f; |
| 268 |
for (int i = start; i < end; ++i) { |
| 269 |
r += widths[i]; |
| 270 |
} |
| 271 |
return r; |
| 272 |
} else { |
| 273 |
|
| 274 |
return mMeasuredText.getWidth(start, end); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public void getBounds(@IntRange(from = 0) int start, @IntRange(from = 0) int end, |
| 285 |
@NonNull Rect bounds) { |
| 286 |
mMeasuredText.getBounds(start, end, bounds); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public void getFontMetricsInt(@IntRange(from = 0) int start, @IntRange(from = 0) int end, |
| 295 |
@NonNull Paint.FontMetricsInt fmi) { |
| 296 |
mMeasuredText.getFontMetricsInt(start, end, fmi); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public float getCharWidthAt(@IntRange(from = 0) int offset) { |
| 305 |
return mMeasuredText.getCharWidthAt(offset); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public static @NonNull MeasuredParagraph buildForBidi(@NonNull CharSequence text, |
| 323 |
@IntRange(from = 0) int start, |
| 324 |
@IntRange(from = 0) int end, |
| 325 |
@NonNull TextDirectionHeuristic textDir, |
| 326 |
@Nullable MeasuredParagraph recycle) { |
| 327 |
final MeasuredParagraph mt = recycle == null ? obtain() : recycle; |
| 328 |
mt.resetAndAnalyzeBidi(text, start, end, textDir); |
| 329 |
return mt; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public static @NonNull MeasuredParagraph buildForMeasurement(@NonNull TextPaint paint, |
| 348 |
@NonNull CharSequence text, |
| 349 |
@IntRange(from = 0) int start, |
| 350 |
@IntRange(from = 0) int end, |
| 351 |
@NonNull TextDirectionHeuristic textDir, |
| 352 |
@Nullable MeasuredParagraph recycle) { |
| 353 |
final MeasuredParagraph mt = recycle == null ? obtain() : recycle; |
| 354 |
mt.resetAndAnalyzeBidi(text, start, end, textDir); |
| 355 |
|
| 356 |
mt.mWidths.resize(mt.mTextLength); |
| 357 |
if (mt.mTextLength == 0) { |
| 358 |
return mt; |
| 359 |
} |
| 360 |
|
| 361 |
if (mt.mSpanned == null) { |
| 362 |
|
| 363 |
mt.applyMetricsAffectingSpan( |
| 364 |
paint, null , null , start, end, |
| 365 |
null ); |
| 366 |
} else { |
| 367 |
|
| 368 |
int spanEnd; |
| 369 |
for (int spanStart = start; spanStart < end; spanStart = spanEnd) { |
| 370 |
spanEnd = mt.mSpanned.nextSpanTransition(spanStart, end, MetricAffectingSpan.class); |
| 371 |
MetricAffectingSpan[] spans = mt.mSpanned.getSpans(spanStart, spanEnd, |
| 372 |
MetricAffectingSpan.class); |
| 373 |
spans = TextUtils.removeEmptySpans(spans, mt.mSpanned, MetricAffectingSpan.class); |
| 374 |
mt.applyMetricsAffectingSpan( |
| 375 |
paint, null , spans, spanStart, spanEnd, |
| 376 |
null ); |
| 377 |
} |
| 378 |
} |
| 379 |
return mt; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public static @NonNull MeasuredParagraph buildForStaticLayout( |
| 402 |
@NonNull TextPaint paint, |
| 403 |
@Nullable LineBreakConfig lineBreakConfig, |
| 404 |
@NonNull CharSequence text, |
| 405 |
@IntRange(from = 0) int start, |
| 406 |
@IntRange(from = 0) int end, |
| 407 |
@NonNull TextDirectionHeuristic textDir, |
| 408 |
int hyphenationMode, |
| 409 |
boolean computeLayout, |
| 410 |
@Nullable MeasuredParagraph hint, |
| 411 |
@Nullable MeasuredParagraph recycle) { |
| 412 |
final MeasuredParagraph mt = recycle == null ? obtain() : recycle; |
| 413 |
mt.resetAndAnalyzeBidi(text, start, end, textDir); |
| 414 |
final MeasuredText.Builder builder; |
| 415 |
if (hint == null) { |
| 416 |
builder = new MeasuredText.Builder(mt.mCopiedBuffer) |
| 417 |
.setComputeHyphenation(hyphenationMode) |
| 418 |
.setComputeLayout(computeLayout); |
| 419 |
} else { |
| 420 |
builder = new MeasuredText.Builder(hint.mMeasuredText); |
| 421 |
} |
| 422 |
if (mt.mTextLength == 0) { |
| 423 |
|
| 424 |
|
| 425 |
mt.mMeasuredText = builder.build(); |
| 426 |
} else { |
| 427 |
if (mt.mSpanned == null) { |
| 428 |
|
| 429 |
mt.applyMetricsAffectingSpan(paint, lineBreakConfig, null , start, end, |
| 430 |
builder); |
| 431 |
mt.mSpanEndCache.append(end); |
| 432 |
} else { |
| 433 |
|
| 434 |
|
| 435 |
int spanEnd; |
| 436 |
for (int spanStart = start; spanStart < end; spanStart = spanEnd) { |
| 437 |
spanEnd = mt.mSpanned.nextSpanTransition(spanStart, end, |
| 438 |
MetricAffectingSpan.class); |
| 439 |
MetricAffectingSpan[] spans = mt.mSpanned.getSpans(spanStart, spanEnd, |
| 440 |
MetricAffectingSpan.class); |
| 441 |
spans = TextUtils.removeEmptySpans(spans, mt.mSpanned, |
| 442 |
MetricAffectingSpan.class); |
| 443 |
|
| 444 |
mt.applyMetricsAffectingSpan(paint, lineBreakConfig, spans, spanStart, spanEnd, |
| 445 |
builder); |
| 446 |
mt.mSpanEndCache.append(spanEnd); |
| 447 |
} |
| 448 |
} |
| 449 |
mt.mMeasuredText = builder.build(); |
| 450 |
} |
| 451 |
|
| 452 |
return mt; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
private void resetAndAnalyzeBidi(@NonNull CharSequence text, |
| 464 |
@IntRange(from = 0) int start, |
| 465 |
@IntRange(from = 0) int end, |
| 466 |
@NonNull TextDirectionHeuristic textDir) { |
| 467 |
reset(); |
| 468 |
mSpanned = text instanceof Spanned ? (Spanned) text : null; |
| 469 |
mTextStart = start; |
| 470 |
mTextLength = end - start; |
| 471 |
|
| 472 |
if (mCopiedBuffer == null || mCopiedBuffer.length != mTextLength) { |
| 473 |
mCopiedBuffer = new char[mTextLength]; |
| 474 |
} |
| 475 |
TextUtils.getChars(text, start, end, mCopiedBuffer, 0); |
| 476 |
|
| 477 |
|
| 478 |
if (mSpanned != null) { |
| 479 |
ReplacementSpan[] spans = mSpanned.getSpans(start, end, ReplacementSpan.class); |
| 480 |
|
| 481 |
for (int i = 0; i < spans.length; i++) { |
| 482 |
int startInPara = mSpanned.getSpanStart(spans[i]) - start; |
| 483 |
int endInPara = mSpanned.getSpanEnd(spans[i]) - start; |
| 484 |
|
| 485 |
if (startInPara < 0) startInPara = 0; |
| 486 |
if (endInPara > mTextLength) endInPara = mTextLength; |
| 487 |
Arrays.fill(mCopiedBuffer, startInPara, endInPara, OBJECT_REPLACEMENT_CHARACTER); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
if ((textDir == TextDirectionHeuristics.LTR |
| 492 |
|| textDir == TextDirectionHeuristics.FIRSTSTRONG_LTR |
| 493 |
|| textDir == TextDirectionHeuristics.ANYRTL_LTR) |
| 494 |
&& TextUtils.doesNotNeedBidi(mCopiedBuffer, 0, mTextLength)) { |
| 495 |
mLevels.clear(); |
| 496 |
mParaDir = Layout.DIR_LEFT_TO_RIGHT; |
| 497 |
mLtrWithoutBidi = true; |
| 498 |
} else { |
| 499 |
final int bidiRequest; |
| 500 |
if (textDir == TextDirectionHeuristics.LTR) { |
| 501 |
bidiRequest = Layout.DIR_REQUEST_LTR; |
| 502 |
} else if (textDir == TextDirectionHeuristics.RTL) { |
| 503 |
bidiRequest = Layout.DIR_REQUEST_RTL; |
| 504 |
} else if (textDir == TextDirectionHeuristics.FIRSTSTRONG_LTR) { |
| 505 |
bidiRequest = Layout.DIR_REQUEST_DEFAULT_LTR; |
| 506 |
} else if (textDir == TextDirectionHeuristics.FIRSTSTRONG_RTL) { |
| 507 |
bidiRequest = Layout.DIR_REQUEST_DEFAULT_RTL; |
| 508 |
} else { |
| 509 |
final boolean isRtl = textDir.isRtl(mCopiedBuffer, 0, mTextLength); |
| 510 |
bidiRequest = isRtl ? Layout.DIR_REQUEST_RTL : Layout.DIR_REQUEST_LTR; |
| 511 |
} |
| 512 |
mLevels.resize(mTextLength); |
| 513 |
mParaDir = AndroidBidi.bidi(bidiRequest, mCopiedBuffer, mLevels.getRawArray()); |
| 514 |
mLtrWithoutBidi = false; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
private void applyReplacementRun(@NonNull ReplacementSpan replacement, |
| 519 |
@IntRange(from = 0) int start, |
| 520 |
@IntRange(from = 0) int end, |
| 521 |
@NonNull TextPaint paint, |
| 522 |
@Nullable MeasuredText.Builder builder) { |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
final float width = replacement.getSize( |
| 527 |
paint, mSpanned, start + mTextStart, end + mTextStart, mCachedFm); |
| 528 |
if (builder == null) { |
| 529 |
|
| 530 |
mWidths.set(start, width); |
| 531 |
if (end > start + 1) { |
| 532 |
Arrays.fill(mWidths.getRawArray(), start + 1, end, 0.0f); |
| 533 |
} |
| 534 |
mWholeWidth += width; |
| 535 |
} else { |
| 536 |
builder.appendReplacementRun(paint, end - start, width); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
private void applyStyleRun(@IntRange(from = 0) int start, |
| 541 |
@IntRange(from = 0) int end, |
| 542 |
@NonNull TextPaint paint, |
| 543 |
@Nullable LineBreakConfig config, |
| 544 |
@Nullable MeasuredText.Builder builder) { |
| 545 |
|
| 546 |
if (mLtrWithoutBidi) { |
| 547 |
|
| 548 |
if (builder == null) { |
| 549 |
mWholeWidth += paint.getTextRunAdvances( |
| 550 |
mCopiedBuffer, start, end - start, start, end - start, false , |
| 551 |
mWidths.getRawArray(), start); |
| 552 |
} else { |
| 553 |
builder.appendStyleRun(paint, config, end - start, false ); |
| 554 |
} |
| 555 |
} else { |
| 556 |
|
| 557 |
byte level = mLevels.get(start); |
| 558 |
|
| 559 |
|
| 560 |
for (int levelStart = start, levelEnd = start + 1;; ++levelEnd) { |
| 561 |
if (levelEnd == end || mLevels.get(levelEnd) != level) { |
| 562 |
final boolean isRtl = (level & 0x1) != 0; |
| 563 |
if (builder == null) { |
| 564 |
final int levelLength = levelEnd - levelStart; |
| 565 |
mWholeWidth += paint.getTextRunAdvances( |
| 566 |
mCopiedBuffer, levelStart, levelLength, levelStart, levelLength, |
| 567 |
isRtl, mWidths.getRawArray(), levelStart); |
| 568 |
} else { |
| 569 |
builder.appendStyleRun(paint, config, levelEnd - levelStart, isRtl); |
| 570 |
} |
| 571 |
if (levelEnd == end) { |
| 572 |
break; |
| 573 |
} |
| 574 |
levelStart = levelEnd; |
| 575 |
level = mLevels.get(levelEnd); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
private void applyMetricsAffectingSpan( |
| 582 |
@NonNull TextPaint paint, |
| 583 |
@Nullable LineBreakConfig lineBreakConfig, |
| 584 |
@Nullable MetricAffectingSpan[] spans, |
| 585 |
@IntRange(from = 0) int start, |
| 586 |
@IntRange(from = 0) int end, |
| 587 |
@Nullable MeasuredText.Builder builder) { |
| 588 |
mCachedPaint.set(paint); |
| 589 |
|
| 590 |
mCachedPaint.baselineShift = 0; |
| 591 |
|
| 592 |
final boolean needFontMetrics = builder != null; |
| 593 |
|
| 594 |
if (needFontMetrics && mCachedFm == null) { |
| 595 |
mCachedFm = new Paint.FontMetricsInt(); |
| 596 |
} |
| 597 |
|
| 598 |
ReplacementSpan replacement = null; |
| 599 |
if (spans != null) { |
| 600 |
for (int i = 0; i < spans.length; i++) { |
| 601 |
MetricAffectingSpan span = spans[i]; |
| 602 |
if (span instanceof ReplacementSpan) { |
| 603 |
|
| 604 |
replacement = (ReplacementSpan) span; |
| 605 |
} else { |
| 606 |
|
| 607 |
span.updateMeasureState(mCachedPaint); |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
final int startInCopiedBuffer = start - mTextStart; |
| 613 |
final int endInCopiedBuffer = end - mTextStart; |
| 614 |
|
| 615 |
if (builder != null) { |
| 616 |
mCachedPaint.getFontMetricsInt(mCachedFm); |
| 617 |
} |
| 618 |
|
| 619 |
if (replacement != null) { |
| 620 |
applyReplacementRun(replacement, startInCopiedBuffer, endInCopiedBuffer, mCachedPaint, |
| 621 |
builder); |
| 622 |
} else { |
| 623 |
applyStyleRun(startInCopiedBuffer, endInCopiedBuffer, mCachedPaint, |
| 624 |
lineBreakConfig, builder); |
| 625 |
} |
| 626 |
|
| 627 |
if (needFontMetrics) { |
| 628 |
if (mCachedPaint.baselineShift < 0) { |
| 629 |
mCachedFm.ascent += mCachedPaint.baselineShift; |
| 630 |
mCachedFm.top += mCachedPaint.baselineShift; |
| 631 |
} else { |
| 632 |
mCachedFm.descent += mCachedPaint.baselineShift; |
| 633 |
mCachedFm.bottom += mCachedPaint.baselineShift; |
| 634 |
} |
| 635 |
|
| 636 |
mFontMetrics.append(mCachedFm.top); |
| 637 |
mFontMetrics.append(mCachedFm.bottom); |
| 638 |
mFontMetrics.append(mCachedFm.ascent); |
| 639 |
mFontMetrics.append(mCachedFm.descent); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
@IntRange(from = 0) int breakText(int limit, boolean forwards, float width) { |
| 652 |
float[] w = mWidths.getRawArray(); |
| 653 |
if (forwards) { |
| 654 |
int i = 0; |
| 655 |
while (i < limit) { |
| 656 |
width -= w[i]; |
| 657 |
if (width < 0.0f) break; |
| 658 |
i++; |
| 659 |
} |
| 660 |
while (i > 0 && mCopiedBuffer[i - 1] == ' ') i--; |
| 661 |
return i; |
| 662 |
} else { |
| 663 |
int i = limit - 1; |
| 664 |
while (i >= 0) { |
| 665 |
width -= w[i]; |
| 666 |
if (width < 0.0f) break; |
| 667 |
i--; |
| 668 |
} |
| 669 |
while (i < limit - 1 && (mCopiedBuffer[i + 1] == ' ' || w[i + 1] == 0.0f)) { |
| 670 |
i++; |
| 671 |
} |
| 672 |
return limit - i - 1; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
@FloatRange(from = 0.0f) float measure(int start, int limit) { |
| 683 |
float width = 0; |
| 684 |
float[] w = mWidths.getRawArray(); |
| 685 |
for (int i = start; i < limit; ++i) { |
| 686 |
width += w[i]; |
| 687 |
} |
| 688 |
return width; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
public @IntRange(from = 0) int getMemoryUsage() { |
| 695 |
return mMeasuredText.getMemoryUsage(); |
| 696 |
} |
| 697 |
} |