| 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.compat.annotation.UnsupportedAppUsage; |
| 24 |
import android.graphics.Paint; |
| 25 |
import android.graphics.text.LineBreakConfig; |
| 26 |
import android.graphics.text.LineBreaker; |
| 27 |
import android.os.Build; |
| 28 |
import android.text.style.LeadingMarginSpan; |
| 29 |
import android.text.style.LeadingMarginSpan.LeadingMarginSpan2; |
| 30 |
import android.text.style.LineHeightSpan; |
| 31 |
import android.text.style.TabStopSpan; |
| 32 |
import android.util.Log; |
| 33 |
import android.util.Pools.SynchronizedPool; |
| 34 |
|
| 35 |
import com.android.internal.util.ArrayUtils; |
| 36 |
import com.android.internal.util.GrowingArrayUtils; |
| 37 |
|
| 38 |
import java.util.Arrays; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class StaticLayout extends Layout { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
static final String TAG = "StaticLayout"; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public final static class Builder { |
| 76 |
private Builder() {} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@NonNull |
| 89 |
public static Builder obtain(@NonNull CharSequence source, @IntRange(from = 0) int start, |
| 90 |
@IntRange(from = 0) int end, @NonNull TextPaint paint, |
| 91 |
@IntRange(from = 0) int width) { |
| 92 |
Builder b = sPool.acquire(); |
| 93 |
if (b == null) { |
| 94 |
b = new Builder(); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
b.mText = source; |
| 99 |
b.mStart = start; |
| 100 |
b.mEnd = end; |
| 101 |
b.mPaint = paint; |
| 102 |
b.mWidth = width; |
| 103 |
b.mAlignment = Alignment.ALIGN_NORMAL; |
| 104 |
b.mTextDir = TextDirectionHeuristics.FIRSTSTRONG_LTR; |
| 105 |
b.mSpacingMult = DEFAULT_LINESPACING_MULTIPLIER; |
| 106 |
b.mSpacingAdd = DEFAULT_LINESPACING_ADDITION; |
| 107 |
b.mIncludePad = true; |
| 108 |
b.mFallbackLineSpacing = false; |
| 109 |
b.mEllipsizedWidth = width; |
| 110 |
b.mEllipsize = null; |
| 111 |
b.mMaxLines = Integer.MAX_VALUE; |
| 112 |
b.mBreakStrategy = Layout.BREAK_STRATEGY_SIMPLE; |
| 113 |
b.mHyphenationFrequency = Layout.HYPHENATION_FREQUENCY_NONE; |
| 114 |
b.mJustificationMode = Layout.JUSTIFICATION_MODE_NONE; |
| 115 |
b.mLineBreakConfig = LineBreakConfig.NONE; |
| 116 |
return b; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private static void recycle(@NonNull Builder b) { |
| 124 |
b.mPaint = null; |
| 125 |
b.mText = null; |
| 126 |
b.mLeftIndents = null; |
| 127 |
b.mRightIndents = null; |
| 128 |
sPool.release(b); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
void finish() { |
| 133 |
mText = null; |
| 134 |
mPaint = null; |
| 135 |
mLeftIndents = null; |
| 136 |
mRightIndents = null; |
| 137 |
} |
| 138 |
|
| 139 |
public Builder setText(CharSequence source) { |
| 140 |
return setText(source, 0, source.length()); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
@NonNull |
| 156 |
public Builder setText(@NonNull CharSequence source, int start, int end) { |
| 157 |
mText = source; |
| 158 |
mStart = start; |
| 159 |
mEnd = end; |
| 160 |
return this; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
@NonNull |
| 172 |
public Builder setPaint(@NonNull TextPaint paint) { |
| 173 |
mPaint = paint; |
| 174 |
return this; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
@NonNull |
| 186 |
public Builder setWidth(@IntRange(from = 0) int width) { |
| 187 |
mWidth = width; |
| 188 |
if (mEllipsize == null) { |
| 189 |
mEllipsizedWidth = width; |
| 190 |
} |
| 191 |
return this; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
@NonNull |
| 201 |
public Builder setAlignment(@NonNull Alignment alignment) { |
| 202 |
mAlignment = alignment; |
| 203 |
return this; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
@NonNull |
| 215 |
public Builder setTextDirection(@NonNull TextDirectionHeuristic textDir) { |
| 216 |
mTextDir = textDir; |
| 217 |
return this; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
@NonNull |
| 231 |
public Builder setLineSpacing(float spacingAdd, @FloatRange(from = 0.0) float spacingMult) { |
| 232 |
mSpacingAdd = spacingAdd; |
| 233 |
mSpacingMult = spacingMult; |
| 234 |
return this; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
@NonNull |
| 247 |
public Builder setIncludePad(boolean includePad) { |
| 248 |
mIncludePad = includePad; |
| 249 |
return this; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
@NonNull |
| 266 |
public Builder setUseLineSpacingFromFallbacks(boolean useLineSpacingFromFallbacks) { |
| 267 |
mFallbackLineSpacing = useLineSpacingFromFallbacks; |
| 268 |
return this; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
@NonNull |
| 281 |
public Builder setEllipsizedWidth(@IntRange(from = 0) int ellipsizedWidth) { |
| 282 |
mEllipsizedWidth = ellipsizedWidth; |
| 283 |
return this; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
@NonNull |
| 298 |
public Builder setEllipsize(@Nullable TextUtils.TruncateAt ellipsize) { |
| 299 |
mEllipsize = ellipsize; |
| 300 |
return this; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
@NonNull |
| 313 |
public Builder setMaxLines(@IntRange(from = 0) int maxLines) { |
| 314 |
mMaxLines = maxLines; |
| 315 |
return this; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
@NonNull |
| 334 |
public Builder setBreakStrategy(@BreakStrategy int breakStrategy) { |
| 335 |
mBreakStrategy = breakStrategy; |
| 336 |
return this; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
@NonNull |
| 357 |
public Builder setHyphenationFrequency(@HyphenationFrequency int hyphenationFrequency) { |
| 358 |
mHyphenationFrequency = hyphenationFrequency; |
| 359 |
return this; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
@NonNull |
| 371 |
public Builder setIndents(@Nullable int[] leftIndents, @Nullable int[] rightIndents) { |
| 372 |
mLeftIndents = leftIndents; |
| 373 |
mRightIndents = rightIndents; |
| 374 |
return this; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
@NonNull |
| 390 |
public Builder setJustificationMode(@JustificationMode int justificationMode) { |
| 391 |
mJustificationMode = justificationMode; |
| 392 |
return this; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
@NonNull |
| 402 |
Builder setAddLastLineLineSpacing(boolean value) { |
| 403 |
mAddLastLineLineSpacing = value; |
| 404 |
return this; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
@NonNull |
| 418 |
public Builder setLineBreakConfig(@NonNull LineBreakConfig lineBreakConfig) { |
| 419 |
mLineBreakConfig = lineBreakConfig; |
| 420 |
return this; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
@NonNull |
| 433 |
public StaticLayout build() { |
| 434 |
StaticLayout result = new StaticLayout(this); |
| 435 |
Builder.recycle(this); |
| 436 |
return result; |
| 437 |
} |
| 438 |
|
| 439 |
private CharSequence mText; |
| 440 |
private int mStart; |
| 441 |
private int mEnd; |
| 442 |
private TextPaint mPaint; |
| 443 |
private int mWidth; |
| 444 |
private Alignment mAlignment; |
| 445 |
private TextDirectionHeuristic mTextDir; |
| 446 |
private float mSpacingMult; |
| 447 |
private float mSpacingAdd; |
| 448 |
private boolean mIncludePad; |
| 449 |
private boolean mFallbackLineSpacing; |
| 450 |
private int mEllipsizedWidth; |
| 451 |
private TextUtils.TruncateAt mEllipsize; |
| 452 |
private int mMaxLines; |
| 453 |
private int mBreakStrategy; |
| 454 |
private int mHyphenationFrequency; |
| 455 |
@Nullable private int[] mLeftIndents; |
| 456 |
@Nullable private int[] mRightIndents; |
| 457 |
private int mJustificationMode; |
| 458 |
private boolean mAddLastLineLineSpacing; |
| 459 |
private LineBreakConfig mLineBreakConfig = LineBreakConfig.NONE; |
| 460 |
|
| 461 |
private final Paint.FontMetricsInt mFontMetricsInt = new Paint.FontMetricsInt(); |
| 462 |
|
| 463 |
private static final SynchronizedPool<Builder> sPool = new SynchronizedPool<>(3); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
@Deprecated |
| 470 |
public StaticLayout(CharSequence source, TextPaint paint, |
| 471 |
int width, |
| 472 |
Alignment align, float spacingmult, float spacingadd, |
| 473 |
boolean includepad) { |
| 474 |
this(source, 0, source.length(), paint, width, align, |
| 475 |
spacingmult, spacingadd, includepad); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
@Deprecated |
| 482 |
public StaticLayout(CharSequence source, int bufstart, int bufend, |
| 483 |
TextPaint paint, int outerwidth, |
| 484 |
Alignment align, |
| 485 |
float spacingmult, float spacingadd, |
| 486 |
boolean includepad) { |
| 487 |
this(source, bufstart, bufend, paint, outerwidth, align, |
| 488 |
spacingmult, spacingadd, includepad, null, 0); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
@Deprecated |
| 495 |
public StaticLayout(CharSequence source, int bufstart, int bufend, |
| 496 |
TextPaint paint, int outerwidth, |
| 497 |
Alignment align, |
| 498 |
float spacingmult, float spacingadd, |
| 499 |
boolean includepad, |
| 500 |
TextUtils.TruncateAt ellipsize, int ellipsizedWidth) { |
| 501 |
this(source, bufstart, bufend, paint, outerwidth, align, |
| 502 |
TextDirectionHeuristics.FIRSTSTRONG_LTR, |
| 503 |
spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth, Integer.MAX_VALUE); |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
@Deprecated |
| 511 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 117521430) |
| 512 |
public StaticLayout(CharSequence source, int bufstart, int bufend, |
| 513 |
TextPaint paint, int outerwidth, |
| 514 |
Alignment align, TextDirectionHeuristic textDir, |
| 515 |
float spacingmult, float spacingadd, |
| 516 |
boolean includepad, |
| 517 |
TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int maxLines) { |
| 518 |
super((ellipsize == null) |
| 519 |
? source |
| 520 |
: (source instanceof Spanned) |
| 521 |
? new SpannedEllipsizer(source) |
| 522 |
: new Ellipsizer(source), |
| 523 |
paint, outerwidth, align, textDir, spacingmult, spacingadd); |
| 524 |
|
| 525 |
Builder b = Builder.obtain(source, bufstart, bufend, paint, outerwidth) |
| 526 |
.setAlignment(align) |
| 527 |
.setTextDirection(textDir) |
| 528 |
.setLineSpacing(spacingadd, spacingmult) |
| 529 |
.setIncludePad(includepad) |
| 530 |
.setEllipsizedWidth(ellipsizedWidth) |
| 531 |
.setEllipsize(ellipsize) |
| 532 |
.setMaxLines(maxLines); |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
if (ellipsize != null) { |
| 547 |
Ellipsizer e = (Ellipsizer) getText(); |
| 548 |
|
| 549 |
e.mLayout = this; |
| 550 |
e.mWidth = ellipsizedWidth; |
| 551 |
e.mMethod = ellipsize; |
| 552 |
mEllipsizedWidth = ellipsizedWidth; |
| 553 |
|
| 554 |
mColumns = COLUMNS_ELLIPSIZE; |
| 555 |
} else { |
| 556 |
mColumns = COLUMNS_NORMAL; |
| 557 |
mEllipsizedWidth = outerwidth; |
| 558 |
} |
| 559 |
|
| 560 |
mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2); |
| 561 |
mLines = ArrayUtils.newUnpaddedIntArray(2 * mColumns); |
| 562 |
mMaximumVisibleLineCount = maxLines; |
| 563 |
|
| 564 |
generate(b, b.mIncludePad, b.mIncludePad); |
| 565 |
|
| 566 |
Builder.recycle(b); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
StaticLayout(@Nullable CharSequence text) { |
| 573 |
super(text, null, 0, null, 0, 0); |
| 574 |
|
| 575 |
mColumns = COLUMNS_ELLIPSIZE; |
| 576 |
mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2); |
| 577 |
mLines = ArrayUtils.newUnpaddedIntArray(2 * mColumns); |
| 578 |
} |
| 579 |
|
| 580 |
private StaticLayout(Builder b) { |
| 581 |
super((b.mEllipsize == null) |
| 582 |
? b.mText |
| 583 |
: (b.mText instanceof Spanned) |
| 584 |
? new SpannedEllipsizer(b.mText) |
| 585 |
: new Ellipsizer(b.mText), |
| 586 |
b.mPaint, b.mWidth, b.mAlignment, b.mTextDir, b.mSpacingMult, b.mSpacingAdd); |
| 587 |
|
| 588 |
if (b.mEllipsize != null) { |
| 589 |
Ellipsizer e = (Ellipsizer) getText(); |
| 590 |
|
| 591 |
e.mLayout = this; |
| 592 |
e.mWidth = b.mEllipsizedWidth; |
| 593 |
e.mMethod = b.mEllipsize; |
| 594 |
mEllipsizedWidth = b.mEllipsizedWidth; |
| 595 |
|
| 596 |
mColumns = COLUMNS_ELLIPSIZE; |
| 597 |
} else { |
| 598 |
mColumns = COLUMNS_NORMAL; |
| 599 |
mEllipsizedWidth = b.mWidth; |
| 600 |
} |
| 601 |
|
| 602 |
mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2); |
| 603 |
mLines = ArrayUtils.newUnpaddedIntArray(2 * mColumns); |
| 604 |
mMaximumVisibleLineCount = b.mMaxLines; |
| 605 |
|
| 606 |
mLeftIndents = b.mLeftIndents; |
| 607 |
mRightIndents = b.mRightIndents; |
| 608 |
setJustificationMode(b.mJustificationMode); |
| 609 |
|
| 610 |
generate(b, b.mIncludePad, b.mIncludePad); |
| 611 |
} |
| 612 |
|
| 613 |
private static int getBaseHyphenationFrequency(int frequency) { |
| 614 |
switch (frequency) { |
| 615 |
case Layout.HYPHENATION_FREQUENCY_FULL: |
| 616 |
case Layout.HYPHENATION_FREQUENCY_FULL_FAST: |
| 617 |
return LineBreaker.HYPHENATION_FREQUENCY_FULL; |
| 618 |
case Layout.HYPHENATION_FREQUENCY_NORMAL: |
| 619 |
case Layout.HYPHENATION_FREQUENCY_NORMAL_FAST: |
| 620 |
return LineBreaker.HYPHENATION_FREQUENCY_NORMAL; |
| 621 |
case Layout.HYPHENATION_FREQUENCY_NONE: |
| 622 |
default: |
| 623 |
return LineBreaker.HYPHENATION_FREQUENCY_NONE; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
void generate(Builder b, boolean includepad, boolean trackpad) { |
| 628 |
final CharSequence source = b.mText; |
| 629 |
final int bufStart = b.mStart; |
| 630 |
final int bufEnd = b.mEnd; |
| 631 |
TextPaint paint = b.mPaint; |
| 632 |
int outerWidth = b.mWidth; |
| 633 |
TextDirectionHeuristic textDir = b.mTextDir; |
| 634 |
float spacingmult = b.mSpacingMult; |
| 635 |
float spacingadd = b.mSpacingAdd; |
| 636 |
float ellipsizedWidth = b.mEllipsizedWidth; |
| 637 |
TextUtils.TruncateAt ellipsize = b.mEllipsize; |
| 638 |
final boolean addLastLineSpacing = b.mAddLastLineLineSpacing; |
| 639 |
|
| 640 |
int lineBreakCapacity = 0; |
| 641 |
int[] breaks = null; |
| 642 |
float[] lineWidths = null; |
| 643 |
float[] ascents = null; |
| 644 |
float[] descents = null; |
| 645 |
boolean[] hasTabs = null; |
| 646 |
int[] hyphenEdits = null; |
| 647 |
|
| 648 |
mLineCount = 0; |
| 649 |
mEllipsized = false; |
| 650 |
mMaxLineHeight = mMaximumVisibleLineCount < 1 ? 0 : DEFAULT_MAX_LINE_HEIGHT; |
| 651 |
mFallbackLineSpacing = b.mFallbackLineSpacing; |
| 652 |
|
| 653 |
int v = 0; |
| 654 |
boolean needMultiply = (spacingmult != 1 || spacingadd != 0); |
| 655 |
|
| 656 |
Paint.FontMetricsInt fm = b.mFontMetricsInt; |
| 657 |
int[] chooseHtv = null; |
| 658 |
|
| 659 |
final int[] indents; |
| 660 |
if (mLeftIndents != null || mRightIndents != null) { |
| 661 |
final int leftLen = mLeftIndents == null ? 0 : mLeftIndents.length; |
| 662 |
final int rightLen = mRightIndents == null ? 0 : mRightIndents.length; |
| 663 |
final int indentsLen = Math.max(leftLen, rightLen); |
| 664 |
indents = new int[indentsLen]; |
| 665 |
for (int i = 0; i < leftLen; i++) { |
| 666 |
indents[i] = mLeftIndents[i]; |
| 667 |
} |
| 668 |
for (int i = 0; i < rightLen; i++) { |
| 669 |
indents[i] += mRightIndents[i]; |
| 670 |
} |
| 671 |
} else { |
| 672 |
indents = null; |
| 673 |
} |
| 674 |
|
| 675 |
final LineBreaker lineBreaker = new LineBreaker.Builder() |
| 676 |
.setBreakStrategy(b.mBreakStrategy) |
| 677 |
.setHyphenationFrequency(getBaseHyphenationFrequency(b.mHyphenationFrequency)) |
| 678 |
|
| 679 |
.setJustificationMode(b.mJustificationMode) |
| 680 |
.setIndents(indents) |
| 681 |
.build(); |
| 682 |
|
| 683 |
LineBreaker.ParagraphConstraints constraints = |
| 684 |
new LineBreaker.ParagraphConstraints(); |
| 685 |
|
| 686 |
PrecomputedText.ParagraphInfo[] paragraphInfo = null; |
| 687 |
final Spanned spanned = (source instanceof Spanned) ? (Spanned) source : null; |
| 688 |
if (source instanceof PrecomputedText) { |
| 689 |
PrecomputedText precomputed = (PrecomputedText) source; |
| 690 |
final @PrecomputedText.Params.CheckResultUsableResult int checkResult = |
| 691 |
precomputed.checkResultUsable(bufStart, bufEnd, textDir, paint, |
| 692 |
b.mBreakStrategy, b.mHyphenationFrequency, b.mLineBreakConfig); |
| 693 |
switch (checkResult) { |
| 694 |
case PrecomputedText.Params.UNUSABLE: |
| 695 |
break; |
| 696 |
case PrecomputedText.Params.NEED_RECOMPUTE: |
| 697 |
final PrecomputedText.Params newParams = |
| 698 |
new PrecomputedText.Params.Builder(paint) |
| 699 |
.setBreakStrategy(b.mBreakStrategy) |
| 700 |
.setHyphenationFrequency(b.mHyphenationFrequency) |
| 701 |
.setTextDirection(textDir) |
| 702 |
.setLineBreakConfig(b.mLineBreakConfig) |
| 703 |
.build(); |
| 704 |
precomputed = PrecomputedText.create(precomputed, newParams); |
| 705 |
paragraphInfo = precomputed.getParagraphInfo(); |
| 706 |
break; |
| 707 |
case PrecomputedText.Params.USABLE: |
| 708 |
|
| 709 |
paragraphInfo = precomputed.getParagraphInfo(); |
| 710 |
break; |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
if (paragraphInfo == null) { |
| 715 |
final PrecomputedText.Params param = new PrecomputedText.Params(paint, |
| 716 |
b.mLineBreakConfig, textDir, b.mBreakStrategy, b.mHyphenationFrequency); |
| 717 |
paragraphInfo = PrecomputedText.createMeasuredParagraphs(source, param, bufStart, |
| 718 |
bufEnd, false ); |
| 719 |
} |
| 720 |
|
| 721 |
for (int paraIndex = 0; paraIndex < paragraphInfo.length; paraIndex++) { |
| 722 |
final int paraStart = paraIndex == 0 |
| 723 |
? bufStart : paragraphInfo[paraIndex - 1].paragraphEnd; |
| 724 |
final int paraEnd = paragraphInfo[paraIndex].paragraphEnd; |
| 725 |
|
| 726 |
int firstWidthLineCount = 1; |
| 727 |
int firstWidth = outerWidth; |
| 728 |
int restWidth = outerWidth; |
| 729 |
|
| 730 |
LineHeightSpan[] chooseHt = null; |
| 731 |
if (spanned != null) { |
| 732 |
LeadingMarginSpan[] sp = getParagraphSpans(spanned, paraStart, paraEnd, |
| 733 |
LeadingMarginSpan.class); |
| 734 |
for (int i = 0; i < sp.length; i++) { |
| 735 |
LeadingMarginSpan lms = sp[i]; |
| 736 |
firstWidth -= sp[i].getLeadingMargin(true); |
| 737 |
restWidth -= sp[i].getLeadingMargin(false); |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
if (lms instanceof LeadingMarginSpan2) { |
| 742 |
LeadingMarginSpan2 lms2 = (LeadingMarginSpan2) lms; |
| 743 |
firstWidthLineCount = Math.max(firstWidthLineCount, |
| 744 |
lms2.getLeadingMarginLineCount()); |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
chooseHt = getParagraphSpans(spanned, paraStart, paraEnd, LineHeightSpan.class); |
| 749 |
|
| 750 |
if (chooseHt.length == 0) { |
| 751 |
chooseHt = null; |
| 752 |
} else { |
| 753 |
if (chooseHtv == null || chooseHtv.length < chooseHt.length) { |
| 754 |
chooseHtv = ArrayUtils.newUnpaddedIntArray(chooseHt.length); |
| 755 |
} |
| 756 |
|
| 757 |
for (int i = 0; i < chooseHt.length; i++) { |
| 758 |
int o = spanned.getSpanStart(chooseHt[i]); |
| 759 |
|
| 760 |
if (o < paraStart) { |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
chooseHtv[i] = getLineTop(getLineForOffset(o)); |
| 765 |
} else { |
| 766 |
|
| 767 |
|
| 768 |
chooseHtv[i] = v; |
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
float[] variableTabStops = null; |
| 775 |
if (spanned != null) { |
| 776 |
TabStopSpan[] spans = getParagraphSpans(spanned, paraStart, |
| 777 |
paraEnd, TabStopSpan.class); |
| 778 |
if (spans.length > 0) { |
| 779 |
float[] stops = new float[spans.length]; |
| 780 |
for (int i = 0; i < spans.length; i++) { |
| 781 |
stops[i] = (float) spans[i].getTabStop(); |
| 782 |
} |
| 783 |
Arrays.sort(stops, 0, stops.length); |
| 784 |
variableTabStops = stops; |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
final MeasuredParagraph measuredPara = paragraphInfo[paraIndex].measured; |
| 789 |
final char[] chs = measuredPara.getChars(); |
| 790 |
final int[] spanEndCache = measuredPara.getSpanEndCache().getRawArray(); |
| 791 |
final int[] fmCache = measuredPara.getFontMetrics().getRawArray(); |
| 792 |
|
| 793 |
constraints.setWidth(restWidth); |
| 794 |
constraints.setIndent(firstWidth, firstWidthLineCount); |
| 795 |
constraints.setTabStops(variableTabStops, TAB_INCREMENT); |
| 796 |
|
| 797 |
LineBreaker.Result res = lineBreaker.computeLineBreaks( |
| 798 |
measuredPara.getMeasuredText(), constraints, mLineCount); |
| 799 |
int breakCount = res.getLineCount(); |
| 800 |
if (lineBreakCapacity < breakCount) { |
| 801 |
lineBreakCapacity = breakCount; |
| 802 |
breaks = new int[lineBreakCapacity]; |
| 803 |
lineWidths = new float[lineBreakCapacity]; |
| 804 |
ascents = new float[lineBreakCapacity]; |
| 805 |
descents = new float[lineBreakCapacity]; |
| 806 |
hasTabs = new boolean[lineBreakCapacity]; |
| 807 |
hyphenEdits = new int[lineBreakCapacity]; |
| 808 |
} |
| 809 |
|
| 810 |
for (int i = 0; i < breakCount; ++i) { |
| 811 |
breaks[i] = res.getLineBreakOffset(i); |
| 812 |
lineWidths[i] = res.getLineWidth(i); |
| 813 |
ascents[i] = res.getLineAscent(i); |
| 814 |
descents[i] = res.getLineDescent(i); |
| 815 |
hasTabs[i] = res.hasLineTab(i); |
| 816 |
hyphenEdits[i] = |
| 817 |
packHyphenEdit(res.getStartLineHyphenEdit(i), res.getEndLineHyphenEdit(i)); |
| 818 |
} |
| 819 |
|
| 820 |
final int remainingLineCount = mMaximumVisibleLineCount - mLineCount; |
| 821 |
final boolean ellipsisMayBeApplied = ellipsize != null |
| 822 |
&& (ellipsize == TextUtils.TruncateAt.END |
| 823 |
|| (mMaximumVisibleLineCount == 1 |
| 824 |
&& ellipsize != TextUtils.TruncateAt.MARQUEE)); |
| 825 |
if (0 < remainingLineCount && remainingLineCount < breakCount |
| 826 |
&& ellipsisMayBeApplied) { |
| 827 |
|
| 828 |
float width = 0; |
| 829 |
boolean hasTab = false; |
| 830 |
for (int i = remainingLineCount - 1; i < breakCount; i++) { |
| 831 |
if (i == breakCount - 1) { |
| 832 |
width += lineWidths[i]; |
| 833 |
} else { |
| 834 |
for (int j = (i == 0 ? 0 : breaks[i - 1]); j < breaks[i]; j++) { |
| 835 |
width += measuredPara.getCharWidthAt(j); |
| 836 |
} |
| 837 |
} |
| 838 |
hasTab |= hasTabs[i]; |
| 839 |
} |
| 840 |
|
| 841 |
breaks[remainingLineCount - 1] = breaks[breakCount - 1]; |
| 842 |
lineWidths[remainingLineCount - 1] = width; |
| 843 |
hasTabs[remainingLineCount - 1] = hasTab; |
| 844 |
|
| 845 |
breakCount = remainingLineCount; |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
int here = paraStart; |
| 851 |
|
| 852 |
int fmTop = 0, fmBottom = 0, fmAscent = 0, fmDescent = 0; |
| 853 |
int fmCacheIndex = 0; |
| 854 |
int spanEndCacheIndex = 0; |
| 855 |
int breakIndex = 0; |
| 856 |
for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) { |
| 857 |
|
| 858 |
spanEnd = spanEndCache[spanEndCacheIndex++]; |
| 859 |
|
| 860 |
|
| 861 |
fm.top = fmCache[fmCacheIndex * 4 + 0]; |
| 862 |
fm.bottom = fmCache[fmCacheIndex * 4 + 1]; |
| 863 |
fm.ascent = fmCache[fmCacheIndex * 4 + 2]; |
| 864 |
fm.descent = fmCache[fmCacheIndex * 4 + 3]; |
| 865 |
fmCacheIndex++; |
| 866 |
|
| 867 |
if (fm.top < fmTop) { |
| 868 |
fmTop = fm.top; |
| 869 |
} |
| 870 |
if (fm.ascent < fmAscent) { |
| 871 |
fmAscent = fm.ascent; |
| 872 |
} |
| 873 |
if (fm.descent > fmDescent) { |
| 874 |
fmDescent = fm.descent; |
| 875 |
} |
| 876 |
if (fm.bottom > fmBottom) { |
| 877 |
fmBottom = fm.bottom; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
while (breakIndex < breakCount && paraStart + breaks[breakIndex] < spanStart) { |
| 882 |
breakIndex++; |
| 883 |
} |
| 884 |
|
| 885 |
while (breakIndex < breakCount && paraStart + breaks[breakIndex] <= spanEnd) { |
| 886 |
int endPos = paraStart + breaks[breakIndex]; |
| 887 |
|
| 888 |
boolean moreChars = (endPos < bufEnd); |
| 889 |
|
| 890 |
final int ascent = mFallbackLineSpacing |
| 891 |
? Math.min(fmAscent, Math.round(ascents[breakIndex])) |
| 892 |
: fmAscent; |
| 893 |
final int descent = mFallbackLineSpacing |
| 894 |
? Math.max(fmDescent, Math.round(descents[breakIndex])) |
| 895 |
: fmDescent; |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
if (mFallbackLineSpacing) { |
| 901 |
if (ascent < fmTop) { |
| 902 |
fmTop = ascent; |
| 903 |
} |
| 904 |
if (descent > fmBottom) { |
| 905 |
fmBottom = descent; |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
v = out(source, here, endPos, |
| 910 |
ascent, descent, fmTop, fmBottom, |
| 911 |
v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, |
| 912 |
hasTabs[breakIndex], hyphenEdits[breakIndex], needMultiply, |
| 913 |
measuredPara, bufEnd, includepad, trackpad, addLastLineSpacing, chs, |
| 914 |
paraStart, ellipsize, ellipsizedWidth, lineWidths[breakIndex], |
| 915 |
paint, moreChars); |
| 916 |
|
| 917 |
if (endPos < spanEnd) { |
| 918 |
|
| 919 |
fmTop = fm.top; |
| 920 |
fmBottom = fm.bottom; |
| 921 |
fmAscent = fm.ascent; |
| 922 |
fmDescent = fm.descent; |
| 923 |
} else { |
| 924 |
fmTop = fmBottom = fmAscent = fmDescent = 0; |
| 925 |
} |
| 926 |
|
| 927 |
here = endPos; |
| 928 |
breakIndex++; |
| 929 |
|
| 930 |
if (mLineCount >= mMaximumVisibleLineCount && mEllipsized) { |
| 931 |
return; |
| 932 |
} |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
if (paraEnd == bufEnd) { |
| 937 |
break; |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
if ((bufEnd == bufStart || source.charAt(bufEnd - 1) == CHAR_NEW_LINE) |
| 942 |
&& mLineCount < mMaximumVisibleLineCount) { |
| 943 |
final MeasuredParagraph measuredPara = |
| 944 |
MeasuredParagraph.buildForBidi(source, bufEnd, bufEnd, textDir, null); |
| 945 |
paint.getFontMetricsInt(fm); |
| 946 |
v = out(source, |
| 947 |
bufEnd, bufEnd, fm.ascent, fm.descent, |
| 948 |
fm.top, fm.bottom, |
| 949 |
v, |
| 950 |
spacingmult, spacingadd, null, |
| 951 |
null, fm, false, 0, |
| 952 |
needMultiply, measuredPara, bufEnd, |
| 953 |
includepad, trackpad, addLastLineSpacing, null, |
| 954 |
bufStart, ellipsize, |
| 955 |
ellipsizedWidth, 0, paint, false); |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
private int out(final CharSequence text, final int start, final int end, int above, int below, |
| 960 |
int top, int bottom, int v, final float spacingmult, final float spacingadd, |
| 961 |
final LineHeightSpan[] chooseHt, final int[] chooseHtv, final Paint.FontMetricsInt fm, |
| 962 |
final boolean hasTab, final int hyphenEdit, final boolean needMultiply, |
| 963 |
@NonNull final MeasuredParagraph measured, |
| 964 |
final int bufEnd, final boolean includePad, final boolean trackPad, |
| 965 |
final boolean addLastLineLineSpacing, final char[] chs, |
| 966 |
final int widthStart, final TextUtils.TruncateAt ellipsize, final float ellipsisWidth, |
| 967 |
final float textWidth, final TextPaint paint, final boolean moreChars) { |
| 968 |
final int j = mLineCount; |
| 969 |
final int off = j * mColumns; |
| 970 |
final int want = off + mColumns + TOP; |
| 971 |
int[] lines = mLines; |
| 972 |
final int dir = measured.getParagraphDir(); |
| 973 |
|
| 974 |
if (want >= lines.length) { |
| 975 |
final int[] grow = ArrayUtils.newUnpaddedIntArray(GrowingArrayUtils.growSize(want)); |
| 976 |
System.arraycopy(lines, 0, grow, 0, lines.length); |
| 977 |
mLines = grow; |
| 978 |
lines = grow; |
| 979 |
} |
| 980 |
|
| 981 |
if (j >= mLineDirections.length) { |
| 982 |
final Directions[] grow = ArrayUtils.newUnpaddedArray(Directions.class, |
| 983 |
GrowingArrayUtils.growSize(j)); |
| 984 |
System.arraycopy(mLineDirections, 0, grow, 0, mLineDirections.length); |
| 985 |
mLineDirections = grow; |
| 986 |
} |
| 987 |
|
| 988 |
if (chooseHt != null) { |
| 989 |
fm.ascent = above; |
| 990 |
fm.descent = below; |
| 991 |
fm.top = top; |
| 992 |
fm.bottom = bottom; |
| 993 |
|
| 994 |
for (int i = 0; i < chooseHt.length; i++) { |
| 995 |
if (chooseHt[i] instanceof LineHeightSpan.WithDensity) { |
| 996 |
((LineHeightSpan.WithDensity) chooseHt[i]) |
| 997 |
.chooseHeight(text, start, end, chooseHtv[i], v, fm, paint); |
| 998 |
} else { |
| 999 |
chooseHt[i].chooseHeight(text, start, end, chooseHtv[i], v, fm); |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
above = fm.ascent; |
| 1004 |
below = fm.descent; |
| 1005 |
top = fm.top; |
| 1006 |
bottom = fm.bottom; |
| 1007 |
} |
| 1008 |
|
| 1009 |
boolean firstLine = (j == 0); |
| 1010 |
boolean currentLineIsTheLastVisibleOne = (j + 1 == mMaximumVisibleLineCount); |
| 1011 |
|
| 1012 |
if (ellipsize != null) { |
| 1013 |
|
| 1014 |
|
| 1015 |
boolean forceEllipsis = moreChars && (mLineCount + 1 == mMaximumVisibleLineCount); |
| 1016 |
|
| 1017 |
boolean doEllipsis = |
| 1018 |
(((mMaximumVisibleLineCount == 1 && moreChars) || (firstLine && !moreChars)) && |
| 1019 |
ellipsize != TextUtils.TruncateAt.MARQUEE) || |
| 1020 |
(!firstLine && (currentLineIsTheLastVisibleOne || !moreChars) && |
| 1021 |
ellipsize == TextUtils.TruncateAt.END); |
| 1022 |
if (doEllipsis) { |
| 1023 |
calculateEllipsis(start, end, measured, widthStart, |
| 1024 |
ellipsisWidth, ellipsize, j, |
| 1025 |
textWidth, paint, forceEllipsis); |
| 1026 |
} else { |
| 1027 |
mLines[mColumns * j + ELLIPSIS_START] = 0; |
| 1028 |
mLines[mColumns * j + ELLIPSIS_COUNT] = 0; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
final boolean lastLine; |
| 1033 |
if (mEllipsized) { |
| 1034 |
lastLine = true; |
| 1035 |
} else { |
| 1036 |
final boolean lastCharIsNewLine = widthStart != bufEnd && bufEnd > 0 |
| 1037 |
&& text.charAt(bufEnd - 1) == CHAR_NEW_LINE; |
| 1038 |
if (end == bufEnd && !lastCharIsNewLine) { |
| 1039 |
lastLine = true; |
| 1040 |
} else if (start == bufEnd && lastCharIsNewLine) { |
| 1041 |
lastLine = true; |
| 1042 |
} else { |
| 1043 |
lastLine = false; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
if (firstLine) { |
| 1048 |
if (trackPad) { |
| 1049 |
mTopPadding = top - above; |
| 1050 |
} |
| 1051 |
|
| 1052 |
if (includePad) { |
| 1053 |
above = top; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
int extra; |
| 1058 |
|
| 1059 |
if (lastLine) { |
| 1060 |
if (trackPad) { |
| 1061 |
mBottomPadding = bottom - below; |
| 1062 |
} |
| 1063 |
|
| 1064 |
if (includePad) { |
| 1065 |
below = bottom; |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
if (needMultiply && (addLastLineLineSpacing || !lastLine)) { |
| 1070 |
double ex = (below - above) * (spacingmult - 1) + spacingadd; |
| 1071 |
if (ex >= 0) { |
| 1072 |
extra = (int)(ex + EXTRA_ROUNDING); |
| 1073 |
} else { |
| 1074 |
extra = -(int)(-ex + EXTRA_ROUNDING); |
| 1075 |
} |
| 1076 |
} else { |
| 1077 |
extra = 0; |
| 1078 |
} |
| 1079 |
|
| 1080 |
lines[off + START] = start; |
| 1081 |
lines[off + TOP] = v; |
| 1082 |
lines[off + DESCENT] = below + extra; |
| 1083 |
lines[off + EXTRA] = extra; |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
if (!mEllipsized && currentLineIsTheLastVisibleOne) { |
| 1088 |
|
| 1089 |
int maxLineBelow = includePad ? bottom : below; |
| 1090 |
|
| 1091 |
mMaxLineHeight = v + (maxLineBelow - above); |
| 1092 |
} |
| 1093 |
|
| 1094 |
v += (below - above) + extra; |
| 1095 |
lines[off + mColumns + START] = end; |
| 1096 |
lines[off + mColumns + TOP] = v; |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
lines[off + TAB] |= hasTab ? TAB_MASK : 0; |
| 1101 |
lines[off + HYPHEN] = hyphenEdit; |
| 1102 |
lines[off + DIR] |= dir << DIR_SHIFT; |
| 1103 |
mLineDirections[j] = measured.getDirections(start - widthStart, end - widthStart); |
| 1104 |
|
| 1105 |
mLineCount++; |
| 1106 |
return v; |
| 1107 |
} |
| 1108 |
|
| 1109 |
private void calculateEllipsis(int lineStart, int lineEnd, |
| 1110 |
MeasuredParagraph measured, int widthStart, |
| 1111 |
float avail, TextUtils.TruncateAt where, |
| 1112 |
int line, float textWidth, TextPaint paint, |
| 1113 |
boolean forceEllipsis) { |
| 1114 |
avail -= getTotalInsets(line); |
| 1115 |
if (textWidth <= avail && !forceEllipsis) { |
| 1116 |
|
| 1117 |
mLines[mColumns * line + ELLIPSIS_START] = 0; |
| 1118 |
mLines[mColumns * line + ELLIPSIS_COUNT] = 0; |
| 1119 |
return; |
| 1120 |
} |
| 1121 |
|
| 1122 |
float ellipsisWidth = paint.measureText(TextUtils.getEllipsisString(where)); |
| 1123 |
int ellipsisStart = 0; |
| 1124 |
int ellipsisCount = 0; |
| 1125 |
int len = lineEnd - lineStart; |
| 1126 |
|
| 1127 |
|
| 1128 |
if (where == TextUtils.TruncateAt.START) { |
| 1129 |
if (mMaximumVisibleLineCount == 1) { |
| 1130 |
float sum = 0; |
| 1131 |
int i; |
| 1132 |
|
| 1133 |
for (i = len; i > 0; i--) { |
| 1134 |
float w = measured.getCharWidthAt(i - 1 + lineStart - widthStart); |
| 1135 |
if (w + sum + ellipsisWidth > avail) { |
| 1136 |
while (i < len |
| 1137 |
&& measured.getCharWidthAt(i + lineStart - widthStart) == 0.0f) { |
| 1138 |
i++; |
| 1139 |
} |
| 1140 |
break; |
| 1141 |
} |
| 1142 |
|
| 1143 |
sum += w; |
| 1144 |
} |
| 1145 |
|
| 1146 |
ellipsisStart = 0; |
| 1147 |
ellipsisCount = i; |
| 1148 |
} else { |
| 1149 |
if (Log.isLoggable(TAG, Log.WARN)) { |
| 1150 |
Log.w(TAG, "Start Ellipsis only supported with one line"); |
| 1151 |
} |
| 1152 |
} |
| 1153 |
} else if (where == TextUtils.TruncateAt.END || where == TextUtils.TruncateAt.MARQUEE || |
| 1154 |
where == TextUtils.TruncateAt.END_SMALL) { |
| 1155 |
float sum = 0; |
| 1156 |
int i; |
| 1157 |
|
| 1158 |
for (i = 0; i < len; i++) { |
| 1159 |
float w = measured.getCharWidthAt(i + lineStart - widthStart); |
| 1160 |
|
| 1161 |
if (w + sum + ellipsisWidth > avail) { |
| 1162 |
break; |
| 1163 |
} |
| 1164 |
|
| 1165 |
sum += w; |
| 1166 |
} |
| 1167 |
|
| 1168 |
ellipsisStart = i; |
| 1169 |
ellipsisCount = len - i; |
| 1170 |
if (forceEllipsis && ellipsisCount == 0 && len > 0) { |
| 1171 |
ellipsisStart = len - 1; |
| 1172 |
ellipsisCount = 1; |
| 1173 |
} |
| 1174 |
} else { |
| 1175 |
|
| 1176 |
if (mMaximumVisibleLineCount == 1) { |
| 1177 |
float lsum = 0, rsum = 0; |
| 1178 |
int left = 0, right = len; |
| 1179 |
|
| 1180 |
float ravail = (avail - ellipsisWidth) / 2; |
| 1181 |
for (right = len; right > 0; right--) { |
| 1182 |
float w = measured.getCharWidthAt(right - 1 + lineStart - widthStart); |
| 1183 |
|
| 1184 |
if (w + rsum > ravail) { |
| 1185 |
while (right < len |
| 1186 |
&& measured.getCharWidthAt(right + lineStart - widthStart) |
| 1187 |
== 0.0f) { |
| 1188 |
right++; |
| 1189 |
} |
| 1190 |
break; |
| 1191 |
} |
| 1192 |
rsum += w; |
| 1193 |
} |
| 1194 |
|
| 1195 |
float lavail = avail - ellipsisWidth - rsum; |
| 1196 |
for (left = 0; left < right; left++) { |
| 1197 |
float w = measured.getCharWidthAt(left + lineStart - widthStart); |
| 1198 |
|
| 1199 |
if (w + lsum > lavail) { |
| 1200 |
break; |
| 1201 |
} |
| 1202 |
|
| 1203 |
lsum += w; |
| 1204 |
} |
| 1205 |
|
| 1206 |
ellipsisStart = left; |
| 1207 |
ellipsisCount = right - left; |
| 1208 |
} else { |
| 1209 |
if (Log.isLoggable(TAG, Log.WARN)) { |
| 1210 |
Log.w(TAG, "Middle Ellipsis only supported with one line"); |
| 1211 |
} |
| 1212 |
} |
| 1213 |
} |
| 1214 |
mEllipsized = true; |
| 1215 |
mLines[mColumns * line + ELLIPSIS_START] = ellipsisStart; |
| 1216 |
mLines[mColumns * line + ELLIPSIS_COUNT] = ellipsisCount; |
| 1217 |
} |
| 1218 |
|
| 1219 |
private float getTotalInsets(int line) { |
| 1220 |
int totalIndent = 0; |
| 1221 |
if (mLeftIndents != null) { |
| 1222 |
totalIndent = mLeftIndents[Math.min(line, mLeftIndents.length - 1)]; |
| 1223 |
} |
| 1224 |
if (mRightIndents != null) { |
| 1225 |
totalIndent += mRightIndents[Math.min(line, mRightIndents.length - 1)]; |
| 1226 |
} |
| 1227 |
return totalIndent; |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
@Override |
| 1235 |
public int getLineForVertical(int vertical) { |
| 1236 |
int high = mLineCount; |
| 1237 |
int low = -1; |
| 1238 |
int guess; |
| 1239 |
int[] lines = mLines; |
| 1240 |
while (high - low > 1) { |
| 1241 |
guess = (high + low) >> 1; |
| 1242 |
if (lines[mColumns * guess + TOP] > vertical){ |
| 1243 |
high = guess; |
| 1244 |
} else { |
| 1245 |
low = guess; |
| 1246 |
} |
| 1247 |
} |
| 1248 |
if (low < 0) { |
| 1249 |
return 0; |
| 1250 |
} else { |
| 1251 |
return low; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
@Override |
| 1256 |
public int getLineCount() { |
| 1257 |
return mLineCount; |
| 1258 |
} |
| 1259 |
|
| 1260 |
@Override |
| 1261 |
public int getLineTop(int line) { |
| 1262 |
return mLines[mColumns * line + TOP]; |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
@Override |
| 1269 |
public int getLineExtra(int line) { |
| 1270 |
return mLines[mColumns * line + EXTRA]; |
| 1271 |
} |
| 1272 |
|
| 1273 |
@Override |
| 1274 |
public int getLineDescent(int line) { |
| 1275 |
return mLines[mColumns * line + DESCENT]; |
| 1276 |
} |
| 1277 |
|
| 1278 |
@Override |
| 1279 |
public int getLineStart(int line) { |
| 1280 |
return mLines[mColumns * line + START] & START_MASK; |
| 1281 |
} |
| 1282 |
|
| 1283 |
@Override |
| 1284 |
public int getParagraphDirection(int line) { |
| 1285 |
return mLines[mColumns * line + DIR] >> DIR_SHIFT; |
| 1286 |
} |
| 1287 |
|
| 1288 |
@Override |
| 1289 |
public boolean getLineContainsTab(int line) { |
| 1290 |
return (mLines[mColumns * line + TAB] & TAB_MASK) != 0; |
| 1291 |
} |
| 1292 |
|
| 1293 |
@Override |
| 1294 |
public final Directions getLineDirections(int line) { |
| 1295 |
if (line > getLineCount()) { |
| 1296 |
throw new ArrayIndexOutOfBoundsException(); |
| 1297 |
} |
| 1298 |
return mLineDirections[line]; |
| 1299 |
} |
| 1300 |
|
| 1301 |
@Override |
| 1302 |
public int getTopPadding() { |
| 1303 |
return mTopPadding; |
| 1304 |
} |
| 1305 |
|
| 1306 |
@Override |
| 1307 |
public int getBottomPadding() { |
| 1308 |
return mBottomPadding; |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
static int packHyphenEdit( |
| 1313 |
@Paint.StartHyphenEdit int start, @Paint.EndHyphenEdit int end) { |
| 1314 |
return start << START_HYPHEN_BITS_SHIFT | end; |
| 1315 |
} |
| 1316 |
|
| 1317 |
static int unpackStartHyphenEdit(int packedHyphenEdit) { |
| 1318 |
return (packedHyphenEdit & START_HYPHEN_MASK) >> START_HYPHEN_BITS_SHIFT; |
| 1319 |
} |
| 1320 |
|
| 1321 |
static int unpackEndHyphenEdit(int packedHyphenEdit) { |
| 1322 |
return packedHyphenEdit & END_HYPHEN_MASK; |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
@Override |
| 1333 |
public @Paint.StartHyphenEdit int getStartHyphenEdit(int lineNumber) { |
| 1334 |
return unpackStartHyphenEdit(mLines[mColumns * lineNumber + HYPHEN] & HYPHEN_MASK); |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
@Override |
| 1345 |
public @Paint.EndHyphenEdit int getEndHyphenEdit(int lineNumber) { |
| 1346 |
return unpackEndHyphenEdit(mLines[mColumns * lineNumber + HYPHEN] & HYPHEN_MASK); |
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
@Override |
| 1353 |
public int getIndentAdjust(int line, Alignment align) { |
| 1354 |
if (align == Alignment.ALIGN_LEFT) { |
| 1355 |
if (mLeftIndents == null) { |
| 1356 |
return 0; |
| 1357 |
} else { |
| 1358 |
return mLeftIndents[Math.min(line, mLeftIndents.length - 1)]; |
| 1359 |
} |
| 1360 |
} else if (align == Alignment.ALIGN_RIGHT) { |
| 1361 |
if (mRightIndents == null) { |
| 1362 |
return 0; |
| 1363 |
} else { |
| 1364 |
return -mRightIndents[Math.min(line, mRightIndents.length - 1)]; |
| 1365 |
} |
| 1366 |
} else if (align == Alignment.ALIGN_CENTER) { |
| 1367 |
int left = 0; |
| 1368 |
if (mLeftIndents != null) { |
| 1369 |
left = mLeftIndents[Math.min(line, mLeftIndents.length - 1)]; |
| 1370 |
} |
| 1371 |
int right = 0; |
| 1372 |
if (mRightIndents != null) { |
| 1373 |
right = mRightIndents[Math.min(line, mRightIndents.length - 1)]; |
| 1374 |
} |
| 1375 |
return (left - right) >> 1; |
| 1376 |
} else { |
| 1377 |
throw new AssertionError("unhandled alignment " + align); |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
@Override |
| 1382 |
public int getEllipsisCount(int line) { |
| 1383 |
if (mColumns < COLUMNS_ELLIPSIZE) { |
| 1384 |
return 0; |
| 1385 |
} |
| 1386 |
|
| 1387 |
return mLines[mColumns * line + ELLIPSIS_COUNT]; |
| 1388 |
} |
| 1389 |
|
| 1390 |
@Override |
| 1391 |
public int getEllipsisStart(int line) { |
| 1392 |
if (mColumns < COLUMNS_ELLIPSIZE) { |
| 1393 |
return 0; |
| 1394 |
} |
| 1395 |
|
| 1396 |
return mLines[mColumns * line + ELLIPSIS_START]; |
| 1397 |
} |
| 1398 |
|
| 1399 |
@Override |
| 1400 |
public int getEllipsizedWidth() { |
| 1401 |
return mEllipsizedWidth; |
| 1402 |
} |
| 1403 |
|
| 1404 |
@Override |
| 1405 |
public boolean isFallbackLineSpacingEnabled() { |
| 1406 |
return mFallbackLineSpacing; |
| 1407 |
} |
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 1417 |
public int getHeight(boolean cap) { |
| 1418 |
if (cap && mLineCount > mMaximumVisibleLineCount && mMaxLineHeight == -1 |
| 1419 |
&& Log.isLoggable(TAG, Log.WARN)) { |
| 1420 |
Log.w(TAG, "maxLineHeight should not be -1. " |
| 1421 |
+ " maxLines:" + mMaximumVisibleLineCount |
| 1422 |
+ " lineCount:" + mLineCount); |
| 1423 |
} |
| 1424 |
|
| 1425 |
return cap && mLineCount > mMaximumVisibleLineCount && mMaxLineHeight != -1 |
| 1426 |
? mMaxLineHeight : super.getHeight(); |
| 1427 |
} |
| 1428 |
|
| 1429 |
@UnsupportedAppUsage |
| 1430 |
private int mLineCount; |
| 1431 |
private int mTopPadding, mBottomPadding; |
| 1432 |
@UnsupportedAppUsage |
| 1433 |
private int mColumns; |
| 1434 |
private int mEllipsizedWidth; |
| 1435 |
private boolean mFallbackLineSpacing; |
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
private boolean mEllipsized; |
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
private int mMaxLineHeight = DEFAULT_MAX_LINE_HEIGHT; |
| 1451 |
|
| 1452 |
private static final int COLUMNS_NORMAL = 5; |
| 1453 |
private static final int COLUMNS_ELLIPSIZE = 7; |
| 1454 |
private static final int START = 0; |
| 1455 |
private static final int DIR = START; |
| 1456 |
private static final int TAB = START; |
| 1457 |
private static final int TOP = 1; |
| 1458 |
private static final int DESCENT = 2; |
| 1459 |
private static final int EXTRA = 3; |
| 1460 |
private static final int HYPHEN = 4; |
| 1461 |
@UnsupportedAppUsage |
| 1462 |
private static final int ELLIPSIS_START = 5; |
| 1463 |
private static final int ELLIPSIS_COUNT = 6; |
| 1464 |
|
| 1465 |
@UnsupportedAppUsage |
| 1466 |
private int[] mLines; |
| 1467 |
@UnsupportedAppUsage |
| 1468 |
private Directions[] mLineDirections; |
| 1469 |
@UnsupportedAppUsage |
| 1470 |
private int mMaximumVisibleLineCount = Integer.MAX_VALUE; |
| 1471 |
|
| 1472 |
private static final int START_MASK = 0x1FFFFFFF; |
| 1473 |
private static final int DIR_SHIFT = 30; |
| 1474 |
private static final int TAB_MASK = 0x20000000; |
| 1475 |
private static final int HYPHEN_MASK = 0xFF; |
| 1476 |
private static final int START_HYPHEN_BITS_SHIFT = 3; |
| 1477 |
private static final int START_HYPHEN_MASK = 0x18; |
| 1478 |
private static final int END_HYPHEN_MASK = 0x7; |
| 1479 |
|
| 1480 |
private static final float TAB_INCREMENT = 20; |
| 1481 |
|
| 1482 |
private static final char CHAR_NEW_LINE = '\n'; |
| 1483 |
|
| 1484 |
private static final double EXTRA_ROUNDING = 0.5; |
| 1485 |
|
| 1486 |
private static final int DEFAULT_MAX_LINE_HEIGHT = -1; |
| 1487 |
|
| 1488 |
|
| 1489 |
static class LineBreaks { |
| 1490 |
private static final int INITIAL_SIZE = 16; |
| 1491 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1492 |
public int[] breaks = new int[INITIAL_SIZE]; |
| 1493 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1494 |
public float[] widths = new float[INITIAL_SIZE]; |
| 1495 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1496 |
public float[] ascents = new float[INITIAL_SIZE]; |
| 1497 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1498 |
public float[] descents = new float[INITIAL_SIZE]; |
| 1499 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1500 |
public int[] flags = new int[INITIAL_SIZE]; |
| 1501 |
|
| 1502 |
} |
| 1503 |
|
| 1504 |
@Nullable private int[] mLeftIndents; |
| 1505 |
@Nullable private int[] mRightIndents; |
| 1506 |
} |