| 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.IntRange; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.graphics.Canvas; |
| 24 |
import android.graphics.Paint; |
| 25 |
import android.graphics.Paint.FontMetricsInt; |
| 26 |
import android.graphics.text.PositionedGlyphs; |
| 27 |
import android.graphics.text.TextRunShaper; |
| 28 |
import android.os.Build; |
| 29 |
import android.text.Layout.Directions; |
| 30 |
import android.text.Layout.TabStops; |
| 31 |
import android.text.style.CharacterStyle; |
| 32 |
import android.text.style.MetricAffectingSpan; |
| 33 |
import android.text.style.ReplacementSpan; |
| 34 |
import android.util.Log; |
| 35 |
|
| 36 |
import com.android.internal.annotations.VisibleForTesting; |
| 37 |
import com.android.internal.util.ArrayUtils; |
| 38 |
|
| 39 |
import java.util.ArrayList; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 54 |
public class TextLine { |
| 55 |
private static final boolean DEBUG = false; |
| 56 |
|
| 57 |
private static final char TAB_CHAR = '\t'; |
| 58 |
|
| 59 |
private TextPaint mPaint; |
| 60 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 61 |
private CharSequence mText; |
| 62 |
private int mStart; |
| 63 |
private int mLen; |
| 64 |
private int mDir; |
| 65 |
private Directions mDirections; |
| 66 |
private boolean mHasTabs; |
| 67 |
private TabStops mTabs; |
| 68 |
private char[] mChars; |
| 69 |
private boolean mCharsValid; |
| 70 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 71 |
private Spanned mSpanned; |
| 72 |
private PrecomputedText mComputed; |
| 73 |
|
| 74 |
private boolean mUseFallbackExtent = false; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private int mEllipsisStart; |
| 79 |
private int mEllipsisEnd; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private float mAddedWidthForJustify; |
| 84 |
private boolean mIsJustifying; |
| 85 |
|
| 86 |
private final TextPaint mWorkPaint = new TextPaint(); |
| 87 |
private final TextPaint mActivePaint = new TextPaint(); |
| 88 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 89 |
private final SpanSet<MetricAffectingSpan> mMetricAffectingSpanSpanSet = |
| 90 |
new SpanSet<MetricAffectingSpan>(MetricAffectingSpan.class); |
| 91 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 92 |
private final SpanSet<CharacterStyle> mCharacterStyleSpanSet = |
| 93 |
new SpanSet<CharacterStyle>(CharacterStyle.class); |
| 94 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 95 |
private final SpanSet<ReplacementSpan> mReplacementSpanSpanSet = |
| 96 |
new SpanSet<ReplacementSpan>(ReplacementSpan.class); |
| 97 |
|
| 98 |
private final DecorationInfo mDecorationInfo = new DecorationInfo(); |
| 99 |
private final ArrayList<DecorationInfo> mDecorations = new ArrayList<>(); |
| 100 |
|
| 101 |
|
| 102 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 103 |
private static final TextLine[] sCached = new TextLine[3]; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 111 |
@UnsupportedAppUsage |
| 112 |
public static TextLine obtain() { |
| 113 |
TextLine tl; |
| 114 |
synchronized (sCached) { |
| 115 |
for (int i = sCached.length; --i >= 0;) { |
| 116 |
if (sCached[i] != null) { |
| 117 |
tl = sCached[i]; |
| 118 |
sCached[i] = null; |
| 119 |
return tl; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
tl = new TextLine(); |
| 124 |
if (DEBUG) { |
| 125 |
Log.v("TLINE", "new: " + tl); |
| 126 |
} |
| 127 |
return tl; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 138 |
public static TextLine recycle(TextLine tl) { |
| 139 |
tl.mText = null; |
| 140 |
tl.mPaint = null; |
| 141 |
tl.mDirections = null; |
| 142 |
tl.mSpanned = null; |
| 143 |
tl.mTabs = null; |
| 144 |
tl.mChars = null; |
| 145 |
tl.mComputed = null; |
| 146 |
tl.mUseFallbackExtent = false; |
| 147 |
|
| 148 |
tl.mMetricAffectingSpanSpanSet.recycle(); |
| 149 |
tl.mCharacterStyleSpanSet.recycle(); |
| 150 |
tl.mReplacementSpanSpanSet.recycle(); |
| 151 |
|
| 152 |
synchronized(sCached) { |
| 153 |
for (int i = 0; i < sCached.length; ++i) { |
| 154 |
if (sCached[i] == null) { |
| 155 |
sCached[i] = tl; |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
return null; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 181 |
public void set(TextPaint paint, CharSequence text, int start, int limit, int dir, |
| 182 |
Directions directions, boolean hasTabs, TabStops tabStops, |
| 183 |
int ellipsisStart, int ellipsisEnd, boolean useFallbackLineSpacing) { |
| 184 |
mPaint = paint; |
| 185 |
mText = text; |
| 186 |
mStart = start; |
| 187 |
mLen = limit - start; |
| 188 |
mDir = dir; |
| 189 |
mDirections = directions; |
| 190 |
mUseFallbackExtent = useFallbackLineSpacing; |
| 191 |
if (mDirections == null) { |
| 192 |
throw new IllegalArgumentException("Directions cannot be null"); |
| 193 |
} |
| 194 |
mHasTabs = hasTabs; |
| 195 |
mSpanned = null; |
| 196 |
|
| 197 |
boolean hasReplacement = false; |
| 198 |
if (text instanceof Spanned) { |
| 199 |
mSpanned = (Spanned) text; |
| 200 |
mReplacementSpanSpanSet.init(mSpanned, start, limit); |
| 201 |
hasReplacement = mReplacementSpanSpanSet.numberOfSpans > 0; |
| 202 |
} |
| 203 |
|
| 204 |
mComputed = null; |
| 205 |
if (text instanceof PrecomputedText) { |
| 206 |
|
| 207 |
|
| 208 |
mComputed = (PrecomputedText) text; |
| 209 |
if (!mComputed.getParams().getTextPaint().equalsForTextMeasurement(paint)) { |
| 210 |
mComputed = null; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
mCharsValid = hasReplacement; |
| 215 |
|
| 216 |
if (mCharsValid) { |
| 217 |
if (mChars == null || mChars.length < mLen) { |
| 218 |
mChars = ArrayUtils.newUnpaddedCharArray(mLen); |
| 219 |
} |
| 220 |
TextUtils.getChars(text, start, limit, mChars, 0); |
| 221 |
if (hasReplacement) { |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
char[] chars = mChars; |
| 228 |
for (int i = start, inext; i < limit; i = inext) { |
| 229 |
inext = mReplacementSpanSpanSet.getNextTransition(i, limit); |
| 230 |
if (mReplacementSpanSpanSet.hasSpansIntersecting(i, inext) |
| 231 |
&& (i - start >= ellipsisEnd || inext - start <= ellipsisStart)) { |
| 232 |
|
| 233 |
chars[i - start] = '\ufffc'; |
| 234 |
for (int j = i - start + 1, e = inext - start; j < e; ++j) { |
| 235 |
chars[j] = '\ufeff'; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
mTabs = tabStops; |
| 242 |
mAddedWidthForJustify = 0; |
| 243 |
mIsJustifying = false; |
| 244 |
|
| 245 |
mEllipsisStart = ellipsisStart != ellipsisEnd ? ellipsisStart : 0; |
| 246 |
mEllipsisEnd = ellipsisStart != ellipsisEnd ? ellipsisEnd : 0; |
| 247 |
} |
| 248 |
|
| 249 |
private char charAt(int i) { |
| 250 |
return mCharsValid ? mChars[i] : mText.charAt(i + mStart); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 257 |
public void justify(float justifyWidth) { |
| 258 |
int end = mLen; |
| 259 |
while (end > 0 && isLineEndSpace(mText.charAt(mStart + end - 1))) { |
| 260 |
end--; |
| 261 |
} |
| 262 |
final int spaces = countStretchableSpaces(0, end); |
| 263 |
if (spaces == 0) { |
| 264 |
|
| 265 |
|
| 266 |
return; |
| 267 |
} |
| 268 |
final float width = Math.abs(measure(end, false, null)); |
| 269 |
mAddedWidthForJustify = (justifyWidth - width) / spaces; |
| 270 |
mIsJustifying = true; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
void draw(Canvas c, float x, int top, int y, int bottom) { |
| 283 |
float h = 0; |
| 284 |
final int runCount = mDirections.getRunCount(); |
| 285 |
for (int runIndex = 0; runIndex < runCount; runIndex++) { |
| 286 |
final int runStart = mDirections.getRunStart(runIndex); |
| 287 |
if (runStart > mLen) break; |
| 288 |
final int runLimit = Math.min(runStart + mDirections.getRunLength(runIndex), mLen); |
| 289 |
final boolean runIsRtl = mDirections.isRunRtl(runIndex); |
| 290 |
|
| 291 |
int segStart = runStart; |
| 292 |
for (int j = mHasTabs ? runStart : runLimit; j <= runLimit; j++) { |
| 293 |
if (j == runLimit || charAt(j) == TAB_CHAR) { |
| 294 |
h += drawRun(c, segStart, j, runIsRtl, x + h, top, y, bottom, |
| 295 |
runIndex != (runCount - 1) || j != mLen); |
| 296 |
|
| 297 |
if (j != runLimit) { |
| 298 |
h = mDir * nextTab(h * mDir); |
| 299 |
} |
| 300 |
segStart = j + 1; |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 313 |
public float metrics(FontMetricsInt fmi) { |
| 314 |
return measure(mLen, false, fmi); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
void shape(TextShaper.GlyphsConsumer consumer) { |
| 321 |
float horizontal = 0; |
| 322 |
float x = 0; |
| 323 |
final int runCount = mDirections.getRunCount(); |
| 324 |
for (int runIndex = 0; runIndex < runCount; runIndex++) { |
| 325 |
final int runStart = mDirections.getRunStart(runIndex); |
| 326 |
if (runStart > mLen) break; |
| 327 |
final int runLimit = Math.min(runStart + mDirections.getRunLength(runIndex), mLen); |
| 328 |
final boolean runIsRtl = mDirections.isRunRtl(runIndex); |
| 329 |
|
| 330 |
int segStart = runStart; |
| 331 |
for (int j = mHasTabs ? runStart : runLimit; j <= runLimit; j++) { |
| 332 |
if (j == runLimit || charAt(j) == TAB_CHAR) { |
| 333 |
horizontal += shapeRun(consumer, segStart, j, runIsRtl, x + horizontal, |
| 334 |
runIndex != (runCount - 1) || j != mLen); |
| 335 |
|
| 336 |
if (j != runLimit) { |
| 337 |
horizontal = mDir * nextTab(horizontal * mDir); |
| 338 |
} |
| 339 |
segStart = j + 1; |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
public float measure(@IntRange(from = 0) int offset, boolean trailing, |
| 387 |
@NonNull FontMetricsInt fmi) { |
| 388 |
if (offset > mLen) { |
| 389 |
throw new IndexOutOfBoundsException( |
| 390 |
"offset(" + offset + ") should be less than line limit(" + mLen + ")"); |
| 391 |
} |
| 392 |
final int target = trailing ? offset - 1 : offset; |
| 393 |
if (target < 0) { |
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
float h = 0; |
| 398 |
for (int runIndex = 0; runIndex < mDirections.getRunCount(); runIndex++) { |
| 399 |
final int runStart = mDirections.getRunStart(runIndex); |
| 400 |
if (runStart > mLen) break; |
| 401 |
final int runLimit = Math.min(runStart + mDirections.getRunLength(runIndex), mLen); |
| 402 |
final boolean runIsRtl = mDirections.isRunRtl(runIndex); |
| 403 |
|
| 404 |
int segStart = runStart; |
| 405 |
for (int j = mHasTabs ? runStart : runLimit; j <= runLimit; j++) { |
| 406 |
if (j == runLimit || charAt(j) == TAB_CHAR) { |
| 407 |
final boolean targetIsInThisSegment = target >= segStart && target < j; |
| 408 |
final boolean sameDirection = (mDir == Layout.DIR_RIGHT_TO_LEFT) == runIsRtl; |
| 409 |
|
| 410 |
if (targetIsInThisSegment && sameDirection) { |
| 411 |
return h + measureRun(segStart, offset, j, runIsRtl, fmi); |
| 412 |
} |
| 413 |
|
| 414 |
final float segmentWidth = measureRun(segStart, j, j, runIsRtl, fmi); |
| 415 |
h += sameDirection ? segmentWidth : -segmentWidth; |
| 416 |
|
| 417 |
if (targetIsInThisSegment) { |
| 418 |
return h + measureRun(segStart, offset, j, runIsRtl, null); |
| 419 |
} |
| 420 |
|
| 421 |
if (j != runLimit) { |
| 422 |
if (offset == j) { |
| 423 |
return h; |
| 424 |
} |
| 425 |
h = mDir * nextTab(h * mDir); |
| 426 |
if (target == j) { |
| 427 |
return h; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
segStart = j + 1; |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
return h; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
@VisibleForTesting |
| 444 |
public float[] measureAllOffsets(boolean[] trailing, FontMetricsInt fmi) { |
| 445 |
float[] measurement = new float[mLen + 1]; |
| 446 |
|
| 447 |
int[] target = new int[mLen + 1]; |
| 448 |
for (int offset = 0; offset < target.length; ++offset) { |
| 449 |
target[offset] = trailing[offset] ? offset - 1 : offset; |
| 450 |
} |
| 451 |
if (target[0] < 0) { |
| 452 |
measurement[0] = 0; |
| 453 |
} |
| 454 |
|
| 455 |
float h = 0; |
| 456 |
for (int runIndex = 0; runIndex < mDirections.getRunCount(); runIndex++) { |
| 457 |
final int runStart = mDirections.getRunStart(runIndex); |
| 458 |
if (runStart > mLen) break; |
| 459 |
final int runLimit = Math.min(runStart + mDirections.getRunLength(runIndex), mLen); |
| 460 |
final boolean runIsRtl = mDirections.isRunRtl(runIndex); |
| 461 |
|
| 462 |
int segStart = runStart; |
| 463 |
for (int j = mHasTabs ? runStart : runLimit; j <= runLimit; ++j) { |
| 464 |
if (j == runLimit || charAt(j) == TAB_CHAR) { |
| 465 |
final float oldh = h; |
| 466 |
final boolean advance = (mDir == Layout.DIR_RIGHT_TO_LEFT) == runIsRtl; |
| 467 |
final float w = measureRun(segStart, j, j, runIsRtl, fmi); |
| 468 |
h += advance ? w : -w; |
| 469 |
|
| 470 |
final float baseh = advance ? oldh : h; |
| 471 |
FontMetricsInt crtfmi = advance ? fmi : null; |
| 472 |
for (int offset = segStart; offset <= j && offset <= mLen; ++offset) { |
| 473 |
if (target[offset] >= segStart && target[offset] < j) { |
| 474 |
measurement[offset] = |
| 475 |
baseh + measureRun(segStart, offset, j, runIsRtl, crtfmi); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
if (j != runLimit) { |
| 480 |
if (target[j] == j) { |
| 481 |
measurement[j] = h; |
| 482 |
} |
| 483 |
h = mDir * nextTab(h * mDir); |
| 484 |
if (target[j + 1] == j) { |
| 485 |
measurement[j + 1] = h; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
segStart = j + 1; |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
if (target[mLen] == mLen) { |
| 494 |
measurement[mLen] = h; |
| 495 |
} |
| 496 |
|
| 497 |
return measurement; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
private float drawRun(Canvas c, int start, |
| 517 |
int limit, boolean runIsRtl, float x, int top, int y, int bottom, |
| 518 |
boolean needWidth) { |
| 519 |
|
| 520 |
if ((mDir == Layout.DIR_LEFT_TO_RIGHT) == runIsRtl) { |
| 521 |
float w = -measureRun(start, limit, limit, runIsRtl, null); |
| 522 |
handleRun(start, limit, limit, runIsRtl, c, null, x + w, top, |
| 523 |
y, bottom, null, false); |
| 524 |
return w; |
| 525 |
} |
| 526 |
|
| 527 |
return handleRun(start, limit, limit, runIsRtl, c, null, x, top, |
| 528 |
y, bottom, null, needWidth); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
private float measureRun(int start, int offset, int limit, boolean runIsRtl, |
| 545 |
FontMetricsInt fmi) { |
| 546 |
return handleRun(start, offset, limit, runIsRtl, null, null, 0, 0, 0, 0, fmi, true); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
private float shapeRun(TextShaper.GlyphsConsumer consumer, int start, |
| 562 |
int limit, boolean runIsRtl, float x, boolean needWidth) { |
| 563 |
|
| 564 |
if ((mDir == Layout.DIR_LEFT_TO_RIGHT) == runIsRtl) { |
| 565 |
float w = -measureRun(start, limit, limit, runIsRtl, null); |
| 566 |
handleRun(start, limit, limit, runIsRtl, null, consumer, x + w, 0, 0, 0, null, false); |
| 567 |
return w; |
| 568 |
} |
| 569 |
|
| 570 |
return handleRun(start, limit, limit, runIsRtl, null, consumer, x, 0, 0, 0, null, |
| 571 |
needWidth); |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
int getOffsetToLeftRightOf(int cursor, boolean toLeft) { |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
int lineStart = 0; |
| 606 |
int lineEnd = mLen; |
| 607 |
boolean paraIsRtl = mDir == -1; |
| 608 |
int[] runs = mDirections.mDirections; |
| 609 |
|
| 610 |
int runIndex, runLevel = 0, runStart = lineStart, runLimit = lineEnd, newCaret = -1; |
| 611 |
boolean trailing = false; |
| 612 |
|
| 613 |
if (cursor == lineStart) { |
| 614 |
runIndex = -2; |
| 615 |
} else if (cursor == lineEnd) { |
| 616 |
runIndex = runs.length; |
| 617 |
} else { |
| 618 |
|
| 619 |
|
| 620 |
for (runIndex = 0; runIndex < runs.length; runIndex += 2) { |
| 621 |
runStart = lineStart + runs[runIndex]; |
| 622 |
if (cursor >= runStart) { |
| 623 |
runLimit = runStart + (runs[runIndex+1] & Layout.RUN_LENGTH_MASK); |
| 624 |
if (runLimit > lineEnd) { |
| 625 |
runLimit = lineEnd; |
| 626 |
} |
| 627 |
if (cursor < runLimit) { |
| 628 |
runLevel = (runs[runIndex+1] >>> Layout.RUN_LEVEL_SHIFT) & |
| 629 |
Layout.RUN_LEVEL_MASK; |
| 630 |
if (cursor == runStart) { |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
int prevRunIndex, prevRunLevel, prevRunStart, prevRunLimit; |
| 635 |
int pos = cursor - 1; |
| 636 |
for (prevRunIndex = 0; prevRunIndex < runs.length; prevRunIndex += 2) { |
| 637 |
prevRunStart = lineStart + runs[prevRunIndex]; |
| 638 |
if (pos >= prevRunStart) { |
| 639 |
prevRunLimit = prevRunStart + |
| 640 |
(runs[prevRunIndex+1] & Layout.RUN_LENGTH_MASK); |
| 641 |
if (prevRunLimit > lineEnd) { |
| 642 |
prevRunLimit = lineEnd; |
| 643 |
} |
| 644 |
if (pos < prevRunLimit) { |
| 645 |
prevRunLevel = (runs[prevRunIndex+1] >>> Layout.RUN_LEVEL_SHIFT) |
| 646 |
& Layout.RUN_LEVEL_MASK; |
| 647 |
if (prevRunLevel < runLevel) { |
| 648 |
|
| 649 |
runIndex = prevRunIndex; |
| 650 |
runLevel = prevRunLevel; |
| 651 |
runStart = prevRunStart; |
| 652 |
runLimit = prevRunLimit; |
| 653 |
trailing = true; |
| 654 |
break; |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
break; |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
if (runIndex != runs.length) { |
| 671 |
boolean runIsRtl = (runLevel & 0x1) != 0; |
| 672 |
boolean advance = toLeft == runIsRtl; |
| 673 |
if (cursor != (advance ? runLimit : runStart) || advance != trailing) { |
| 674 |
|
| 675 |
newCaret = getOffsetBeforeAfter(runIndex, runStart, runLimit, |
| 676 |
runIsRtl, cursor, advance); |
| 677 |
|
| 678 |
|
| 679 |
if (newCaret != (advance ? runLimit : runStart)) { |
| 680 |
return newCaret; |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
while (true) { |
| 692 |
boolean advance = toLeft == paraIsRtl; |
| 693 |
int otherRunIndex = runIndex + (advance ? 2 : -2); |
| 694 |
if (otherRunIndex >= 0 && otherRunIndex < runs.length) { |
| 695 |
int otherRunStart = lineStart + runs[otherRunIndex]; |
| 696 |
int otherRunLimit = otherRunStart + |
| 697 |
(runs[otherRunIndex+1] & Layout.RUN_LENGTH_MASK); |
| 698 |
if (otherRunLimit > lineEnd) { |
| 699 |
otherRunLimit = lineEnd; |
| 700 |
} |
| 701 |
int otherRunLevel = (runs[otherRunIndex+1] >>> Layout.RUN_LEVEL_SHIFT) & |
| 702 |
Layout.RUN_LEVEL_MASK; |
| 703 |
boolean otherRunIsRtl = (otherRunLevel & 1) != 0; |
| 704 |
|
| 705 |
advance = toLeft == otherRunIsRtl; |
| 706 |
if (newCaret == -1) { |
| 707 |
newCaret = getOffsetBeforeAfter(otherRunIndex, otherRunStart, |
| 708 |
otherRunLimit, otherRunIsRtl, |
| 709 |
advance ? otherRunStart : otherRunLimit, advance); |
| 710 |
if (newCaret == (advance ? otherRunLimit : otherRunStart)) { |
| 711 |
|
| 712 |
|
| 713 |
runIndex = otherRunIndex; |
| 714 |
runLevel = otherRunLevel; |
| 715 |
continue; |
| 716 |
} |
| 717 |
break; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
if (otherRunLevel < runLevel) { |
| 722 |
|
| 723 |
newCaret = advance ? otherRunStart : otherRunLimit; |
| 724 |
} |
| 725 |
break; |
| 726 |
} |
| 727 |
|
| 728 |
if (newCaret == -1) { |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
newCaret = advance ? mLen + 1 : -1; |
| 733 |
break; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
if (newCaret <= lineEnd) { |
| 745 |
newCaret = advance ? lineEnd : lineStart; |
| 746 |
} |
| 747 |
break; |
| 748 |
} |
| 749 |
|
| 750 |
return newCaret; |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
private int getOffsetBeforeAfter(int runIndex, int runStart, int runLimit, |
| 771 |
boolean runIsRtl, int offset, boolean after) { |
| 772 |
|
| 773 |
if (runIndex < 0 || offset == (after ? mLen : 0)) { |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
if (after) { |
| 778 |
return TextUtils.getOffsetAfter(mText, offset + mStart) - mStart; |
| 779 |
} |
| 780 |
return TextUtils.getOffsetBefore(mText, offset + mStart) - mStart; |
| 781 |
} |
| 782 |
|
| 783 |
TextPaint wp = mWorkPaint; |
| 784 |
wp.set(mPaint); |
| 785 |
if (mIsJustifying) { |
| 786 |
wp.setWordSpacing(mAddedWidthForJustify); |
| 787 |
} |
| 788 |
|
| 789 |
int spanStart = runStart; |
| 790 |
int spanLimit; |
| 791 |
if (mSpanned == null || runStart == runLimit) { |
| 792 |
spanLimit = runLimit; |
| 793 |
} else { |
| 794 |
int target = after ? offset + 1 : offset; |
| 795 |
int limit = mStart + runLimit; |
| 796 |
while (true) { |
| 797 |
spanLimit = mSpanned.nextSpanTransition(mStart + spanStart, limit, |
| 798 |
MetricAffectingSpan.class) - mStart; |
| 799 |
if (spanLimit >= target) { |
| 800 |
break; |
| 801 |
} |
| 802 |
spanStart = spanLimit; |
| 803 |
} |
| 804 |
|
| 805 |
MetricAffectingSpan[] spans = mSpanned.getSpans(mStart + spanStart, |
| 806 |
mStart + spanLimit, MetricAffectingSpan.class); |
| 807 |
spans = TextUtils.removeEmptySpans(spans, mSpanned, MetricAffectingSpan.class); |
| 808 |
|
| 809 |
if (spans.length > 0) { |
| 810 |
ReplacementSpan replacement = null; |
| 811 |
for (int j = 0; j < spans.length; j++) { |
| 812 |
MetricAffectingSpan span = spans[j]; |
| 813 |
if (span instanceof ReplacementSpan) { |
| 814 |
replacement = (ReplacementSpan)span; |
| 815 |
} else { |
| 816 |
span.updateMeasureState(wp); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
if (replacement != null) { |
| 821 |
|
| 822 |
|
| 823 |
return after ? spanLimit : spanStart; |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
int cursorOpt = after ? Paint.CURSOR_AFTER : Paint.CURSOR_BEFORE; |
| 829 |
if (mCharsValid) { |
| 830 |
return wp.getTextRunCursor(mChars, spanStart, spanLimit - spanStart, |
| 831 |
runIsRtl, offset, cursorOpt); |
| 832 |
} else { |
| 833 |
return wp.getTextRunCursor(mText, mStart + spanStart, |
| 834 |
mStart + spanLimit, runIsRtl, mStart + offset, cursorOpt) - mStart; |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
private static void expandMetricsFromPaint(FontMetricsInt fmi, TextPaint wp) { |
| 842 |
final int previousTop = fmi.top; |
| 843 |
final int previousAscent = fmi.ascent; |
| 844 |
final int previousDescent = fmi.descent; |
| 845 |
final int previousBottom = fmi.bottom; |
| 846 |
final int previousLeading = fmi.leading; |
| 847 |
|
| 848 |
wp.getFontMetricsInt(fmi); |
| 849 |
|
| 850 |
updateMetrics(fmi, previousTop, previousAscent, previousDescent, previousBottom, |
| 851 |
previousLeading); |
| 852 |
} |
| 853 |
|
| 854 |
private void expandMetricsFromPaint(TextPaint wp, int start, int end, |
| 855 |
int contextStart, int contextEnd, boolean runIsRtl, FontMetricsInt fmi) { |
| 856 |
|
| 857 |
final int previousTop = fmi.top; |
| 858 |
final int previousAscent = fmi.ascent; |
| 859 |
final int previousDescent = fmi.descent; |
| 860 |
final int previousBottom = fmi.bottom; |
| 861 |
final int previousLeading = fmi.leading; |
| 862 |
|
| 863 |
int count = end - start; |
| 864 |
int contextCount = contextEnd - contextStart; |
| 865 |
if (mCharsValid) { |
| 866 |
wp.getFontMetricsInt(mChars, start, count, contextStart, contextCount, runIsRtl, |
| 867 |
fmi); |
| 868 |
} else { |
| 869 |
if (mComputed == null) { |
| 870 |
wp.getFontMetricsInt(mText, mStart + start, count, mStart + contextStart, |
| 871 |
contextCount, runIsRtl, fmi); |
| 872 |
} else { |
| 873 |
mComputed.getFontMetricsInt(mStart + start, mStart + end, fmi); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
updateMetrics(fmi, previousTop, previousAscent, previousDescent, previousBottom, |
| 878 |
previousLeading); |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
static void updateMetrics(FontMetricsInt fmi, int previousTop, int previousAscent, |
| 883 |
int previousDescent, int previousBottom, int previousLeading) { |
| 884 |
fmi.top = Math.min(fmi.top, previousTop); |
| 885 |
fmi.ascent = Math.min(fmi.ascent, previousAscent); |
| 886 |
fmi.descent = Math.max(fmi.descent, previousDescent); |
| 887 |
fmi.bottom = Math.max(fmi.bottom, previousBottom); |
| 888 |
fmi.leading = Math.max(fmi.leading, previousLeading); |
| 889 |
} |
| 890 |
|
| 891 |
private static void drawStroke(TextPaint wp, Canvas c, int color, float position, |
| 892 |
float thickness, float xleft, float xright, float baseline) { |
| 893 |
final float strokeTop = baseline + wp.baselineShift + position; |
| 894 |
|
| 895 |
final int previousColor = wp.getColor(); |
| 896 |
final Paint.Style previousStyle = wp.getStyle(); |
| 897 |
final boolean previousAntiAlias = wp.isAntiAlias(); |
| 898 |
|
| 899 |
wp.setStyle(Paint.Style.FILL); |
| 900 |
wp.setAntiAlias(true); |
| 901 |
|
| 902 |
wp.setColor(color); |
| 903 |
c.drawRect(xleft, strokeTop, xright, strokeTop + thickness, wp); |
| 904 |
|
| 905 |
wp.setStyle(previousStyle); |
| 906 |
wp.setColor(previousColor); |
| 907 |
wp.setAntiAlias(previousAntiAlias); |
| 908 |
} |
| 909 |
|
| 910 |
private float getRunAdvance(TextPaint wp, int start, int end, int contextStart, int contextEnd, |
| 911 |
boolean runIsRtl, int offset) { |
| 912 |
if (mCharsValid) { |
| 913 |
return wp.getRunAdvance(mChars, start, end, contextStart, contextEnd, runIsRtl, offset); |
| 914 |
} else { |
| 915 |
final int delta = mStart; |
| 916 |
if (mComputed == null) { |
| 917 |
return wp.getRunAdvance(mText, delta + start, delta + end, |
| 918 |
delta + contextStart, delta + contextEnd, runIsRtl, delta + offset); |
| 919 |
} else { |
| 920 |
return mComputed.getWidth(start + delta, end + delta); |
| 921 |
} |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
private float handleText(TextPaint wp, int start, int end, |
| 947 |
int contextStart, int contextEnd, boolean runIsRtl, |
| 948 |
Canvas c, TextShaper.GlyphsConsumer consumer, float x, int top, int y, int bottom, |
| 949 |
FontMetricsInt fmi, boolean needWidth, int offset, |
| 950 |
@Nullable ArrayList<DecorationInfo> decorations) { |
| 951 |
|
| 952 |
if (mIsJustifying) { |
| 953 |
wp.setWordSpacing(mAddedWidthForJustify); |
| 954 |
} |
| 955 |
|
| 956 |
if (fmi != null) { |
| 957 |
expandMetricsFromPaint(fmi, wp); |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
if (end == start) { |
| 962 |
return 0f; |
| 963 |
} |
| 964 |
|
| 965 |
float totalWidth = 0; |
| 966 |
|
| 967 |
final int numDecorations = decorations == null ? 0 : decorations.size(); |
| 968 |
if (needWidth || ((c != null || consumer != null) && (wp.bgColor != 0 |
| 969 |
|| numDecorations != 0 || runIsRtl))) { |
| 970 |
totalWidth = getRunAdvance(wp, start, end, contextStart, contextEnd, runIsRtl, offset); |
| 971 |
} |
| 972 |
|
| 973 |
final float leftX, rightX; |
| 974 |
if (runIsRtl) { |
| 975 |
leftX = x - totalWidth; |
| 976 |
rightX = x; |
| 977 |
} else { |
| 978 |
leftX = x; |
| 979 |
rightX = x + totalWidth; |
| 980 |
} |
| 981 |
|
| 982 |
if (consumer != null) { |
| 983 |
shapeTextRun(consumer, wp, start, end, contextStart, contextEnd, runIsRtl, leftX); |
| 984 |
} |
| 985 |
|
| 986 |
if (mUseFallbackExtent && fmi != null) { |
| 987 |
expandMetricsFromPaint(wp, start, end, contextStart, contextEnd, runIsRtl, fmi); |
| 988 |
} |
| 989 |
|
| 990 |
if (c != null) { |
| 991 |
if (wp.bgColor != 0) { |
| 992 |
int previousColor = wp.getColor(); |
| 993 |
Paint.Style previousStyle = wp.getStyle(); |
| 994 |
|
| 995 |
wp.setColor(wp.bgColor); |
| 996 |
wp.setStyle(Paint.Style.FILL); |
| 997 |
c.drawRect(leftX, top, rightX, bottom, wp); |
| 998 |
|
| 999 |
wp.setStyle(previousStyle); |
| 1000 |
wp.setColor(previousColor); |
| 1001 |
} |
| 1002 |
|
| 1003 |
drawTextRun(c, wp, start, end, contextStart, contextEnd, runIsRtl, |
| 1004 |
leftX, y + wp.baselineShift); |
| 1005 |
|
| 1006 |
if (numDecorations != 0) { |
| 1007 |
for (int i = 0; i < numDecorations; i++) { |
| 1008 |
final DecorationInfo info = decorations.get(i); |
| 1009 |
|
| 1010 |
final int decorationStart = Math.max(info.start, start); |
| 1011 |
final int decorationEnd = Math.min(info.end, offset); |
| 1012 |
float decorationStartAdvance = getRunAdvance( |
| 1013 |
wp, start, end, contextStart, contextEnd, runIsRtl, decorationStart); |
| 1014 |
float decorationEndAdvance = getRunAdvance( |
| 1015 |
wp, start, end, contextStart, contextEnd, runIsRtl, decorationEnd); |
| 1016 |
final float decorationXLeft, decorationXRight; |
| 1017 |
if (runIsRtl) { |
| 1018 |
decorationXLeft = rightX - decorationEndAdvance; |
| 1019 |
decorationXRight = rightX - decorationStartAdvance; |
| 1020 |
} else { |
| 1021 |
decorationXLeft = leftX + decorationStartAdvance; |
| 1022 |
decorationXRight = leftX + decorationEndAdvance; |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
if (info.underlineColor != 0) { |
| 1029 |
drawStroke(wp, c, info.underlineColor, wp.getUnderlinePosition(), |
| 1030 |
info.underlineThickness, decorationXLeft, decorationXRight, y); |
| 1031 |
} |
| 1032 |
if (info.isUnderlineText) { |
| 1033 |
final float thickness = |
| 1034 |
Math.max(wp.getUnderlineThickness(), 1.0f); |
| 1035 |
drawStroke(wp, c, wp.getColor(), wp.getUnderlinePosition(), thickness, |
| 1036 |
decorationXLeft, decorationXRight, y); |
| 1037 |
} |
| 1038 |
|
| 1039 |
if (info.isStrikeThruText) { |
| 1040 |
final float thickness = |
| 1041 |
Math.max(wp.getStrikeThruThickness(), 1.0f); |
| 1042 |
drawStroke(wp, c, wp.getColor(), wp.getStrikeThruPosition(), thickness, |
| 1043 |
decorationXLeft, decorationXRight, y); |
| 1044 |
} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
} |
| 1049 |
|
| 1050 |
return runIsRtl ? -totalWidth : totalWidth; |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
private float handleReplacement(ReplacementSpan replacement, TextPaint wp, |
| 1073 |
int start, int limit, boolean runIsRtl, Canvas c, |
| 1074 |
float x, int top, int y, int bottom, FontMetricsInt fmi, |
| 1075 |
boolean needWidth) { |
| 1076 |
|
| 1077 |
float ret = 0; |
| 1078 |
|
| 1079 |
int textStart = mStart + start; |
| 1080 |
int textLimit = mStart + limit; |
| 1081 |
|
| 1082 |
if (needWidth || (c != null && runIsRtl)) { |
| 1083 |
int previousTop = 0; |
| 1084 |
int previousAscent = 0; |
| 1085 |
int previousDescent = 0; |
| 1086 |
int previousBottom = 0; |
| 1087 |
int previousLeading = 0; |
| 1088 |
|
| 1089 |
boolean needUpdateMetrics = (fmi != null); |
| 1090 |
|
| 1091 |
if (needUpdateMetrics) { |
| 1092 |
previousTop = fmi.top; |
| 1093 |
previousAscent = fmi.ascent; |
| 1094 |
previousDescent = fmi.descent; |
| 1095 |
previousBottom = fmi.bottom; |
| 1096 |
previousLeading = fmi.leading; |
| 1097 |
} |
| 1098 |
|
| 1099 |
ret = replacement.getSize(wp, mText, textStart, textLimit, fmi); |
| 1100 |
|
| 1101 |
if (needUpdateMetrics) { |
| 1102 |
updateMetrics(fmi, previousTop, previousAscent, previousDescent, previousBottom, |
| 1103 |
previousLeading); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
if (c != null) { |
| 1108 |
if (runIsRtl) { |
| 1109 |
x -= ret; |
| 1110 |
} |
| 1111 |
replacement.draw(c, mText, textStart, textLimit, |
| 1112 |
x, top, y, bottom, wp); |
| 1113 |
} |
| 1114 |
|
| 1115 |
return runIsRtl ? -ret : ret; |
| 1116 |
} |
| 1117 |
|
| 1118 |
private int adjustStartHyphenEdit(int start, @Paint.StartHyphenEdit int startHyphenEdit) { |
| 1119 |
|
| 1120 |
return start > 0 ? Paint.START_HYPHEN_EDIT_NO_EDIT : startHyphenEdit; |
| 1121 |
} |
| 1122 |
|
| 1123 |
private int adjustEndHyphenEdit(int limit, @Paint.EndHyphenEdit int endHyphenEdit) { |
| 1124 |
|
| 1125 |
return limit < mLen ? Paint.END_HYPHEN_EDIT_NO_EDIT : endHyphenEdit; |
| 1126 |
} |
| 1127 |
|
| 1128 |
private static final class DecorationInfo { |
| 1129 |
public boolean isStrikeThruText; |
| 1130 |
public boolean isUnderlineText; |
| 1131 |
public int underlineColor; |
| 1132 |
public float underlineThickness; |
| 1133 |
public int start = -1; |
| 1134 |
public int end = -1; |
| 1135 |
|
| 1136 |
public boolean hasDecoration() { |
| 1137 |
return isStrikeThruText || isUnderlineText || underlineColor != 0; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
public DecorationInfo copyInfo() { |
| 1142 |
final DecorationInfo copy = new DecorationInfo(); |
| 1143 |
copy.isStrikeThruText = isStrikeThruText; |
| 1144 |
copy.isUnderlineText = isUnderlineText; |
| 1145 |
copy.underlineColor = underlineColor; |
| 1146 |
copy.underlineThickness = underlineThickness; |
| 1147 |
return copy; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
private void extractDecorationInfo(@NonNull TextPaint paint, @NonNull DecorationInfo info) { |
| 1152 |
info.isStrikeThruText = paint.isStrikeThruText(); |
| 1153 |
if (info.isStrikeThruText) { |
| 1154 |
paint.setStrikeThruText(false); |
| 1155 |
} |
| 1156 |
info.isUnderlineText = paint.isUnderlineText(); |
| 1157 |
if (info.isUnderlineText) { |
| 1158 |
paint.setUnderlineText(false); |
| 1159 |
} |
| 1160 |
info.underlineColor = paint.underlineColor; |
| 1161 |
info.underlineThickness = paint.underlineThickness; |
| 1162 |
paint.setUnderlineText(0, 0.0f); |
| 1163 |
} |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
private float handleRun(int start, int measureLimit, |
| 1186 |
int limit, boolean runIsRtl, Canvas c, |
| 1187 |
TextShaper.GlyphsConsumer consumer, float x, int top, int y, |
| 1188 |
int bottom, FontMetricsInt fmi, boolean needWidth) { |
| 1189 |
|
| 1190 |
if (measureLimit < start || measureLimit > limit) { |
| 1191 |
throw new IndexOutOfBoundsException("measureLimit (" + measureLimit + ") is out of " |
| 1192 |
+ "start (" + start + ") and limit (" + limit + ") bounds"); |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
if (start == measureLimit) { |
| 1197 |
final TextPaint wp = mWorkPaint; |
| 1198 |
wp.set(mPaint); |
| 1199 |
if (fmi != null) { |
| 1200 |
expandMetricsFromPaint(fmi, wp); |
| 1201 |
} |
| 1202 |
return 0f; |
| 1203 |
} |
| 1204 |
|
| 1205 |
final boolean needsSpanMeasurement; |
| 1206 |
if (mSpanned == null) { |
| 1207 |
needsSpanMeasurement = false; |
| 1208 |
} else { |
| 1209 |
mMetricAffectingSpanSpanSet.init(mSpanned, mStart + start, mStart + limit); |
| 1210 |
mCharacterStyleSpanSet.init(mSpanned, mStart + start, mStart + limit); |
| 1211 |
needsSpanMeasurement = mMetricAffectingSpanSpanSet.numberOfSpans != 0 |
| 1212 |
|| mCharacterStyleSpanSet.numberOfSpans != 0; |
| 1213 |
} |
| 1214 |
|
| 1215 |
if (!needsSpanMeasurement) { |
| 1216 |
final TextPaint wp = mWorkPaint; |
| 1217 |
wp.set(mPaint); |
| 1218 |
wp.setStartHyphenEdit(adjustStartHyphenEdit(start, wp.getStartHyphenEdit())); |
| 1219 |
wp.setEndHyphenEdit(adjustEndHyphenEdit(limit, wp.getEndHyphenEdit())); |
| 1220 |
return handleText(wp, start, limit, start, limit, runIsRtl, c, consumer, x, top, |
| 1221 |
y, bottom, fmi, needWidth, measureLimit, null); |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
final float originalX = x; |
| 1230 |
for (int i = start, inext; i < measureLimit; i = inext) { |
| 1231 |
final TextPaint wp = mWorkPaint; |
| 1232 |
wp.set(mPaint); |
| 1233 |
|
| 1234 |
inext = mMetricAffectingSpanSpanSet.getNextTransition(mStart + i, mStart + limit) - |
| 1235 |
mStart; |
| 1236 |
int mlimit = Math.min(inext, measureLimit); |
| 1237 |
|
| 1238 |
ReplacementSpan replacement = null; |
| 1239 |
|
| 1240 |
for (int j = 0; j < mMetricAffectingSpanSpanSet.numberOfSpans; j++) { |
| 1241 |
|
| 1242 |
|
| 1243 |
if ((mMetricAffectingSpanSpanSet.spanStarts[j] >= mStart + mlimit) |
| 1244 |
|| (mMetricAffectingSpanSpanSet.spanEnds[j] <= mStart + i)) continue; |
| 1245 |
|
| 1246 |
boolean insideEllipsis = |
| 1247 |
mStart + mEllipsisStart <= mMetricAffectingSpanSpanSet.spanStarts[j] |
| 1248 |
&& mMetricAffectingSpanSpanSet.spanEnds[j] <= mStart + mEllipsisEnd; |
| 1249 |
final MetricAffectingSpan span = mMetricAffectingSpanSpanSet.spans[j]; |
| 1250 |
if (span instanceof ReplacementSpan) { |
| 1251 |
replacement = !insideEllipsis ? (ReplacementSpan) span : null; |
| 1252 |
} else { |
| 1253 |
|
| 1254 |
|
| 1255 |
span.updateDrawState(wp); |
| 1256 |
} |
| 1257 |
} |
| 1258 |
|
| 1259 |
if (replacement != null) { |
| 1260 |
x += handleReplacement(replacement, wp, i, mlimit, runIsRtl, c, x, top, y, |
| 1261 |
bottom, fmi, needWidth || mlimit < measureLimit); |
| 1262 |
continue; |
| 1263 |
} |
| 1264 |
|
| 1265 |
final TextPaint activePaint = mActivePaint; |
| 1266 |
activePaint.set(mPaint); |
| 1267 |
int activeStart = i; |
| 1268 |
int activeEnd = mlimit; |
| 1269 |
final DecorationInfo decorationInfo = mDecorationInfo; |
| 1270 |
mDecorations.clear(); |
| 1271 |
for (int j = i, jnext; j < mlimit; j = jnext) { |
| 1272 |
jnext = mCharacterStyleSpanSet.getNextTransition(mStart + j, mStart + inext) - |
| 1273 |
mStart; |
| 1274 |
|
| 1275 |
final int offset = Math.min(jnext, mlimit); |
| 1276 |
wp.set(mPaint); |
| 1277 |
for (int k = 0; k < mCharacterStyleSpanSet.numberOfSpans; k++) { |
| 1278 |
|
| 1279 |
if ((mCharacterStyleSpanSet.spanStarts[k] >= mStart + offset) || |
| 1280 |
(mCharacterStyleSpanSet.spanEnds[k] <= mStart + j)) continue; |
| 1281 |
|
| 1282 |
final CharacterStyle span = mCharacterStyleSpanSet.spans[k]; |
| 1283 |
span.updateDrawState(wp); |
| 1284 |
} |
| 1285 |
|
| 1286 |
extractDecorationInfo(wp, decorationInfo); |
| 1287 |
|
| 1288 |
if (j == i) { |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
activePaint.set(wp); |
| 1293 |
} else if (!equalAttributes(wp, activePaint)) { |
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
activePaint.setStartHyphenEdit( |
| 1298 |
adjustStartHyphenEdit(activeStart, mPaint.getStartHyphenEdit())); |
| 1299 |
activePaint.setEndHyphenEdit( |
| 1300 |
adjustEndHyphenEdit(activeEnd, mPaint.getEndHyphenEdit())); |
| 1301 |
x += handleText(activePaint, activeStart, activeEnd, i, inext, runIsRtl, c, |
| 1302 |
consumer, x, top, y, bottom, fmi, needWidth || activeEnd < measureLimit, |
| 1303 |
Math.min(activeEnd, mlimit), mDecorations); |
| 1304 |
|
| 1305 |
activeStart = j; |
| 1306 |
activePaint.set(wp); |
| 1307 |
mDecorations.clear(); |
| 1308 |
} else { |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
} |
| 1314 |
|
| 1315 |
activeEnd = jnext; |
| 1316 |
if (decorationInfo.hasDecoration()) { |
| 1317 |
final DecorationInfo copy = decorationInfo.copyInfo(); |
| 1318 |
copy.start = j; |
| 1319 |
copy.end = jnext; |
| 1320 |
mDecorations.add(copy); |
| 1321 |
} |
| 1322 |
} |
| 1323 |
|
| 1324 |
activePaint.setStartHyphenEdit( |
| 1325 |
adjustStartHyphenEdit(activeStart, mPaint.getStartHyphenEdit())); |
| 1326 |
activePaint.setEndHyphenEdit( |
| 1327 |
adjustEndHyphenEdit(activeEnd, mPaint.getEndHyphenEdit())); |
| 1328 |
x += handleText(activePaint, activeStart, activeEnd, i, inext, runIsRtl, c, consumer, x, |
| 1329 |
top, y, bottom, fmi, needWidth || activeEnd < measureLimit, |
| 1330 |
Math.min(activeEnd, mlimit), mDecorations); |
| 1331 |
} |
| 1332 |
|
| 1333 |
return x - originalX; |
| 1334 |
} |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
private void drawTextRun(Canvas c, TextPaint wp, int start, int end, |
| 1350 |
int contextStart, int contextEnd, boolean runIsRtl, float x, int y) { |
| 1351 |
|
| 1352 |
if (mCharsValid) { |
| 1353 |
int count = end - start; |
| 1354 |
int contextCount = contextEnd - contextStart; |
| 1355 |
c.drawTextRun(mChars, start, count, contextStart, contextCount, |
| 1356 |
x, y, runIsRtl, wp); |
| 1357 |
} else { |
| 1358 |
int delta = mStart; |
| 1359 |
c.drawTextRun(mText, delta + start, delta + end, |
| 1360 |
delta + contextStart, delta + contextEnd, x, y, runIsRtl, wp); |
| 1361 |
} |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
private void shapeTextRun(TextShaper.GlyphsConsumer consumer, TextPaint paint, |
| 1377 |
int start, int end, int contextStart, int contextEnd, boolean runIsRtl, float x) { |
| 1378 |
|
| 1379 |
int count = end - start; |
| 1380 |
int contextCount = contextEnd - contextStart; |
| 1381 |
PositionedGlyphs glyphs; |
| 1382 |
if (mCharsValid) { |
| 1383 |
glyphs = TextRunShaper.shapeTextRun( |
| 1384 |
mChars, |
| 1385 |
start, count, |
| 1386 |
contextStart, contextCount, |
| 1387 |
x, 0f, |
| 1388 |
runIsRtl, |
| 1389 |
paint |
| 1390 |
); |
| 1391 |
} else { |
| 1392 |
glyphs = TextRunShaper.shapeTextRun( |
| 1393 |
mText, |
| 1394 |
mStart + start, count, |
| 1395 |
mStart + contextStart, contextCount, |
| 1396 |
x, 0f, |
| 1397 |
runIsRtl, |
| 1398 |
paint |
| 1399 |
); |
| 1400 |
} |
| 1401 |
consumer.accept(start, count, glyphs, paint); |
| 1402 |
} |
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
float nextTab(float h) { |
| 1412 |
if (mTabs != null) { |
| 1413 |
return mTabs.nextTab(h); |
| 1414 |
} |
| 1415 |
return TabStops.nextDefaultStop(h, TAB_INCREMENT); |
| 1416 |
} |
| 1417 |
|
| 1418 |
private boolean isStretchableWhitespace(int ch) { |
| 1419 |
|
| 1420 |
return ch == 0x0020; |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
private int countStretchableSpaces(int start, int end) { |
| 1425 |
int count = 0; |
| 1426 |
for (int i = start; i < end; i++) { |
| 1427 |
final char c = mCharsValid ? mChars[i] : mText.charAt(i + mStart); |
| 1428 |
if (isStretchableWhitespace(c)) { |
| 1429 |
count++; |
| 1430 |
} |
| 1431 |
} |
| 1432 |
return count; |
| 1433 |
} |
| 1434 |
|
| 1435 |
|
| 1436 |
public static boolean isLineEndSpace(char ch) { |
| 1437 |
return ch == ' ' || ch == '\t' || ch == 0x1680 |
| 1438 |
|| (0x2000 <= ch && ch <= 0x200A && ch != 0x2007) |
| 1439 |
|| ch == 0x205F || ch == 0x3000; |
| 1440 |
} |
| 1441 |
|
| 1442 |
private static final int TAB_INCREMENT = 20; |
| 1443 |
|
| 1444 |
private static boolean equalAttributes(@NonNull TextPaint lp, @NonNull TextPaint rp) { |
| 1445 |
return lp.getColorFilter() == rp.getColorFilter() |
| 1446 |
&& lp.getMaskFilter() == rp.getMaskFilter() |
| 1447 |
&& lp.getShader() == rp.getShader() |
| 1448 |
&& lp.getTypeface() == rp.getTypeface() |
| 1449 |
&& lp.getXfermode() == rp.getXfermode() |
| 1450 |
&& lp.getTextLocales().equals(rp.getTextLocales()) |
| 1451 |
&& TextUtils.equals(lp.getFontFeatureSettings(), rp.getFontFeatureSettings()) |
| 1452 |
&& TextUtils.equals(lp.getFontVariationSettings(), rp.getFontVariationSettings()) |
| 1453 |
&& lp.getShadowLayerRadius() == rp.getShadowLayerRadius() |
| 1454 |
&& lp.getShadowLayerDx() == rp.getShadowLayerDx() |
| 1455 |
&& lp.getShadowLayerDy() == rp.getShadowLayerDy() |
| 1456 |
&& lp.getShadowLayerColor() == rp.getShadowLayerColor() |
| 1457 |
&& lp.getFlags() == rp.getFlags() |
| 1458 |
&& lp.getHinting() == rp.getHinting() |
| 1459 |
&& lp.getStyle() == rp.getStyle() |
| 1460 |
&& lp.getColor() == rp.getColor() |
| 1461 |
&& lp.getStrokeWidth() == rp.getStrokeWidth() |
| 1462 |
&& lp.getStrokeMiter() == rp.getStrokeMiter() |
| 1463 |
&& lp.getStrokeCap() == rp.getStrokeCap() |
| 1464 |
&& lp.getStrokeJoin() == rp.getStrokeJoin() |
| 1465 |
&& lp.getTextAlign() == rp.getTextAlign() |
| 1466 |
&& lp.isElegantTextHeight() == rp.isElegantTextHeight() |
| 1467 |
&& lp.getTextSize() == rp.getTextSize() |
| 1468 |
&& lp.getTextScaleX() == rp.getTextScaleX() |
| 1469 |
&& lp.getTextSkewX() == rp.getTextSkewX() |
| 1470 |
&& lp.getLetterSpacing() == rp.getLetterSpacing() |
| 1471 |
&& lp.getWordSpacing() == rp.getWordSpacing() |
| 1472 |
&& lp.getStartHyphenEdit() == rp.getStartHyphenEdit() |
| 1473 |
&& lp.getEndHyphenEdit() == rp.getEndHyphenEdit() |
| 1474 |
&& lp.bgColor == rp.bgColor |
| 1475 |
&& lp.baselineShift == rp.baselineShift |
| 1476 |
&& lp.linkColor == rp.linkColor |
| 1477 |
&& lp.drawableState == rp.drawableState |
| 1478 |
&& lp.density == rp.density |
| 1479 |
&& lp.underlineColor == rp.underlineColor |
| 1480 |
&& lp.underlineThickness == rp.underlineThickness; |
| 1481 |
} |
| 1482 |
} |