2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import android.emoji.EmojiFactory;
20 import android.graphics.Canvas;
21 import android.graphics.Paint;
22 import android.graphics.Path;
23 import android.graphics.Rect;
24 import android.text.method.TextKeyListener;
25 import android.text.style.AlignmentSpan;
26 import android.text.style.LeadingMarginSpan;
27 import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
28 import android.text.style.LineBackgroundSpan;
29 import android.text.style.ParagraphStyle;
30 import android.text.style.ReplacementSpan;
31 import android.text.style.TabStopSpan;
33 import com.android.internal.util.ArrayUtils;
34 import com.android.internal.util.GrowingArrayUtils;
36 import java.util.Arrays;
39 * A base class that manages text layout in visual elements on
41 * <p>For text that will be edited, use a {@link DynamicLayout},
42 * which will be updated as the text changes.
43 * For text that will not change, use a {@link StaticLayout}.
45 public abstract class Layout {
46 private static final ParagraphStyle[] NO_PARA_SPANS =
47 ArrayUtils.emptyArray(ParagraphStyle.class);
49 /* package */ static final EmojiFactory EMOJI_FACTORY = EmojiFactory.newAvailableInstance();
50 /* package */ static final int MIN_EMOJI, MAX_EMOJI;
53 if (EMOJI_FACTORY != null) {
54 MIN_EMOJI = EMOJI_FACTORY.getMinimumAndroidPua();
55 MAX_EMOJI = EMOJI_FACTORY.getMaximumAndroidPua();
63 * Return how wide a layout must be in order to display the
64 * specified text with one line per paragraph.
66 public static float getDesiredWidth(CharSequence source,
68 return getDesiredWidth(source, 0, source.length(), paint);
72 * Return how wide a layout must be in order to display the
73 * specified text slice with one line per paragraph.
75 public static float getDesiredWidth(CharSequence source,
81 for (int i = start; i <= end; i = next) {
82 next = TextUtils.indexOf(source, '\n', i, end);
87 // note, omits trailing paragraph char
88 float w = measurePara(paint, source, i, next);
100 * Subclasses of Layout use this constructor to set the display text,
101 * width, and other standard properties.
102 * @param text the text to render
103 * @param paint the default paint for the layout. Styles can override
104 * various attributes of the paint.
105 * @param width the wrapping width for the text.
106 * @param align whether to left, right, or center the text. Styles can
107 * override the alignment.
108 * @param spacingMult factor by which to scale the font size to get the
109 * default line spacing
110 * @param spacingAdd amount to add to the default line spacing
112 protected Layout(CharSequence text, TextPaint paint,
113 int width, Alignment align,
114 float spacingMult, float spacingAdd) {
115 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
116 spacingMult, spacingAdd);
120 * Subclasses of Layout use this constructor to set the display text,
121 * width, and other standard properties.
122 * @param text the text to render
123 * @param paint the default paint for the layout. Styles can override
124 * various attributes of the paint.
125 * @param width the wrapping width for the text.
126 * @param align whether to left, right, or center the text. Styles can
127 * override the alignment.
128 * @param spacingMult factor by which to scale the font size to get the
129 * default line spacing
130 * @param spacingAdd amount to add to the default line spacing
134 protected Layout(CharSequence text, TextPaint paint,
135 int width, Alignment align, TextDirectionHeuristic textDir,
136 float spacingMult, float spacingAdd) {
139 throw new IllegalArgumentException("Layout: " + width + " < 0");
141 // Ensure paint doesn't have baselineShift set.
142 // While normally we don't modify the paint the user passed in,
143 // we were already doing this in Styled.drawUniformRun with both
144 // baselineShift and bgColor. We probably should reevaluate bgColor.
147 paint.baselineShift = 0;
152 mWorkPaint = new TextPaint();
155 mSpacingMult = spacingMult;
156 mSpacingAdd = spacingAdd;
157 mSpannedText = text instanceof Spanned;
162 * Replace constructor properties of this Layout with new ones. Be careful.
164 /* package */ void replaceWith(CharSequence text, TextPaint paint,
165 int width, Alignment align,
166 float spacingmult, float spacingadd) {
168 throw new IllegalArgumentException("Layout: " + width + " < 0");
175 mSpacingMult = spacingmult;
176 mSpacingAdd = spacingadd;
177 mSpannedText = text instanceof Spanned;
181 * Draw this Layout on the specified Canvas.
183 public void draw(Canvas c) {
184 draw(c, null, null, 0);
188 * Draw this Layout on the specified canvas, with the highlight path drawn
189 * between the background and the text.
191 * @param canvas the canvas
192 * @param highlight the path of the highlight or cursor; can be null
193 * @param highlightPaint the paint for the highlight
194 * @param cursorOffsetVertical the amount to temporarily translate the
195 * canvas while rendering the highlight
197 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
198 int cursorOffsetVertical) {
199 final long lineRange = getLineRangeForDraw(canvas);
200 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
201 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
202 if (lastLine < 0) return;
204 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
205 firstLine, lastLine);
206 drawText(canvas, firstLine, lastLine);
212 public void drawText(Canvas canvas, int firstLine, int lastLine) {
213 int previousLineBottom = getLineTop(firstLine);
214 int previousLineEnd = getLineStart(firstLine);
215 ParagraphStyle[] spans = NO_PARA_SPANS;
217 TextPaint paint = mPaint;
218 CharSequence buf = mText;
220 Alignment paraAlign = mAlignment;
221 TabStops tabStops = null;
222 boolean tabStopsIsInitialized = false;
224 TextLine tl = TextLine.obtain();
226 // Draw the lines, one at a time.
227 // The baseline is the top of the following line minus the current line's descent.
228 for (int i = firstLine; i <= lastLine; i++) {
229 int start = previousLineEnd;
230 previousLineEnd = getLineStart(i + 1);
231 int end = getLineVisibleEnd(i, start, previousLineEnd);
233 int ltop = previousLineBottom;
234 int lbottom = getLineTop(i+1);
235 previousLineBottom = lbottom;
236 int lbaseline = lbottom - getLineDescent(i);
238 int dir = getParagraphDirection(i);
243 Spanned sp = (Spanned) buf;
244 int textLength = buf.length();
245 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
247 // New batch of paragraph styles, collect into spans array.
248 // Compute the alignment, last alignment style wins.
249 // Reset tabStops, we'll rebuild if we encounter a line with
251 // We expect paragraph spans to be relatively infrequent, use
252 // spanEnd so that we can check less frequently. Since
253 // paragraph styles ought to apply to entire paragraphs, we can
254 // just collect the ones present at the start of the paragraph.
255 // If spanEnd is before the end of the paragraph, that's not
257 if (start >= spanEnd && (i == firstLine || isFirstParaLine)) {
258 spanEnd = sp.nextSpanTransition(start, textLength,
259 ParagraphStyle.class);
260 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
262 paraAlign = mAlignment;
263 for (int n = spans.length - 1; n >= 0; n--) {
264 if (spans[n] instanceof AlignmentSpan) {
265 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
270 tabStopsIsInitialized = false;
273 // Draw all leading margin spans. Adjust left or right according
274 // to the paragraph direction of the line.
275 final int length = spans.length;
276 boolean useFirstLineMargin = isFirstParaLine;
277 for (int n = 0; n < length; n++) {
278 if (spans[n] instanceof LeadingMarginSpan2) {
279 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
280 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
281 // if there is more than one LeadingMarginSpan2, use
282 // the count that is greatest
283 if (i < startLine + count) {
284 useFirstLineMargin = true;
289 for (int n = 0; n < length; n++) {
290 if (spans[n] instanceof LeadingMarginSpan) {
291 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
292 if (dir == DIR_RIGHT_TO_LEFT) {
293 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
294 lbaseline, lbottom, buf,
295 start, end, isFirstParaLine, this);
296 right -= margin.getLeadingMargin(useFirstLineMargin);
298 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
299 lbaseline, lbottom, buf,
300 start, end, isFirstParaLine, this);
301 left += margin.getLeadingMargin(useFirstLineMargin);
307 boolean hasTabOrEmoji = getLineContainsTab(i);
308 // Can't tell if we have tabs for sure, currently
309 if (hasTabOrEmoji && !tabStopsIsInitialized) {
310 if (tabStops == null) {
311 tabStops = new TabStops(TAB_INCREMENT, spans);
313 tabStops.reset(TAB_INCREMENT, spans);
315 tabStopsIsInitialized = true;
318 // Determine whether the line aligns to normal, opposite, or center.
319 Alignment align = paraAlign;
320 if (align == Alignment.ALIGN_LEFT) {
321 align = (dir == DIR_LEFT_TO_RIGHT) ?
322 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
323 } else if (align == Alignment.ALIGN_RIGHT) {
324 align = (dir == DIR_LEFT_TO_RIGHT) ?
325 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
329 if (align == Alignment.ALIGN_NORMAL) {
330 if (dir == DIR_LEFT_TO_RIGHT) {
336 int max = (int)getLineExtent(i, tabStops, false);
337 if (align == Alignment.ALIGN_OPPOSITE) {
338 if (dir == DIR_LEFT_TO_RIGHT) {
343 } else { // Alignment.ALIGN_CENTER
345 x = (right + left - max) >> 1;
349 Directions directions = getLineDirections(i);
350 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTabOrEmoji) {
351 // XXX: assumes there's nothing additional to be done
352 canvas.drawText(buf, start, end, x, lbaseline, paint);
354 tl.set(paint, buf, start, end, dir, directions, hasTabOrEmoji, tabStops);
355 tl.draw(canvas, x, ltop, lbaseline, lbottom);
359 TextLine.recycle(tl);
365 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
366 int cursorOffsetVertical, int firstLine, int lastLine) {
367 // First, draw LineBackgroundSpans.
368 // LineBackgroundSpans know nothing about the alignment, margins, or
369 // direction of the layout or line. XXX: Should they?
370 // They are evaluated at each line.
372 if (mLineBackgroundSpans == null) {
373 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
376 Spanned buffer = (Spanned) mText;
377 int textLength = buffer.length();
378 mLineBackgroundSpans.init(buffer, 0, textLength);
380 if (mLineBackgroundSpans.numberOfSpans > 0) {
381 int previousLineBottom = getLineTop(firstLine);
382 int previousLineEnd = getLineStart(firstLine);
383 ParagraphStyle[] spans = NO_PARA_SPANS;
385 TextPaint paint = mPaint;
387 final int width = mWidth;
388 for (int i = firstLine; i <= lastLine; i++) {
389 int start = previousLineEnd;
390 int end = getLineStart(i + 1);
391 previousLineEnd = end;
393 int ltop = previousLineBottom;
394 int lbottom = getLineTop(i + 1);
395 previousLineBottom = lbottom;
396 int lbaseline = lbottom - getLineDescent(i);
398 if (start >= spanEnd) {
399 // These should be infrequent, so we'll use this so that
400 // we don't have to check as often.
401 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
402 // All LineBackgroundSpans on a line contribute to its background.
404 // Duplication of the logic of getParagraphSpans
405 if (start != end || start == 0) {
406 // Equivalent to a getSpans(start, end), but filling the 'spans' local
407 // array instead to reduce memory allocation
408 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
409 // equal test is valid since both intervals are not empty by
411 if (mLineBackgroundSpans.spanStarts[j] >= end ||
412 mLineBackgroundSpans.spanEnds[j] <= start) continue;
413 spans = GrowingArrayUtils.append(
414 spans, spansLength, mLineBackgroundSpans.spans[j]);
420 for (int n = 0; n < spansLength; n++) {
421 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
422 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
423 ltop, lbaseline, lbottom,
424 buffer, start, end, i);
428 mLineBackgroundSpans.recycle();
431 // There can be a highlight even without spans if we are drawing
432 // a non-spanned transformation of a spanned editing buffer.
433 if (highlight != null) {
434 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
435 canvas.drawPath(highlight, highlightPaint);
436 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
442 * @return The range of lines that need to be drawn, possibly empty.
445 public long getLineRangeForDraw(Canvas canvas) {
448 synchronized (sTempRect) {
449 if (!canvas.getClipBounds(sTempRect)) {
450 // Negative range end used as a special flag
451 return TextUtils.packRangeInLong(0, -1);
454 dtop = sTempRect.top;
455 dbottom = sTempRect.bottom;
458 final int top = Math.max(dtop, 0);
459 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
461 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
462 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
466 * Return the start position of the line, given the left and right bounds
469 * @param line the line index
470 * @param left the left bounds (0, or leading margin if ltr para)
471 * @param right the right bounds (width, minus leading margin if rtl para)
472 * @return the start position of the line (to right of line if rtl para)
474 private int getLineStartPos(int line, int left, int right) {
475 // Adjust the point at which to start rendering depending on the
476 // alignment of the paragraph.
477 Alignment align = getParagraphAlignment(line);
478 int dir = getParagraphDirection(line);
480 if (align == Alignment.ALIGN_LEFT) {
481 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
482 } else if (align == Alignment.ALIGN_RIGHT) {
483 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
487 if (align == Alignment.ALIGN_NORMAL) {
488 if (dir == DIR_LEFT_TO_RIGHT) {
494 TabStops tabStops = null;
495 if (mSpannedText && getLineContainsTab(line)) {
496 Spanned spanned = (Spanned) mText;
497 int start = getLineStart(line);
498 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
500 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
502 if (tabSpans.length > 0) {
503 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
506 int max = (int)getLineExtent(line, tabStops, false);
507 if (align == Alignment.ALIGN_OPPOSITE) {
508 if (dir == DIR_LEFT_TO_RIGHT) {
511 // max is negative here
514 } else { // Alignment.ALIGN_CENTER
516 x = (left + right - max) >> 1;
523 * Return the text that is displayed by this Layout.
525 public final CharSequence getText() {
530 * Return the base Paint properties for this layout.
531 * Do NOT change the paint, which may result in funny
532 * drawing for this layout.
534 public final TextPaint getPaint() {
539 * Return the width of this layout.
541 public final int getWidth() {
546 * Return the width to which this Layout is ellipsizing, or
547 * {@link #getWidth} if it is not doing anything special.
549 public int getEllipsizedWidth() {
554 * Increase the width of this layout to the specified width.
555 * Be careful to use this only when you know it is appropriate—
556 * it does not cause the text to reflow to use the full new width.
558 public final void increaseWidthTo(int wid) {
560 throw new RuntimeException("attempted to reduce Layout width");
567 * Return the total height of this layout.
569 public int getHeight() {
570 return getLineTop(getLineCount());
574 * Return the base alignment of this layout.
576 public final Alignment getAlignment() {
581 * Return what the text height is multiplied by to get the line height.
583 public final float getSpacingMultiplier() {
588 * Return the number of units of leading that are added to each line.
590 public final float getSpacingAdd() {
595 * Return the heuristic used to determine paragraph text direction.
598 public final TextDirectionHeuristic getTextDirectionHeuristic() {
603 * Return the number of lines of text in this layout.
605 public abstract int getLineCount();
608 * Return the baseline for the specified line (0…getLineCount() - 1)
609 * If bounds is not null, return the top, left, right, bottom extents
610 * of the specified line in it.
611 * @param line which line to examine (0..getLineCount() - 1)
612 * @param bounds Optional. If not null, it returns the extent of the line
613 * @return the Y-coordinate of the baseline
615 public int getLineBounds(int line, Rect bounds) {
616 if (bounds != null) {
617 bounds.left = 0; // ???
618 bounds.top = getLineTop(line);
619 bounds.right = mWidth; // ???
620 bounds.bottom = getLineTop(line + 1);
622 return getLineBaseline(line);
626 * Return the vertical position of the top of the specified line
627 * (0…getLineCount()).
628 * If the specified line is equal to the line count, returns the
629 * bottom of the last line.
631 public abstract int getLineTop(int line);
634 * Return the descent of the specified line(0…getLineCount() - 1).
636 public abstract int getLineDescent(int line);
639 * Return the text offset of the beginning of the specified line (
640 * 0…getLineCount()). If the specified line is equal to the line
641 * count, returns the length of the text.
643 public abstract int getLineStart(int line);
646 * Returns the primary directionality of the paragraph containing the
647 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
648 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
650 public abstract int getParagraphDirection(int line);
653 * Returns whether the specified line contains one or more
654 * characters that need to be handled specially, like tabs
657 public abstract boolean getLineContainsTab(int line);
660 * Returns the directional run information for the specified line.
661 * The array alternates counts of characters in left-to-right
662 * and right-to-left segments of the line.
664 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
666 public abstract Directions getLineDirections(int line);
669 * Returns the (negative) number of extra pixels of ascent padding in the
670 * top line of the Layout.
672 public abstract int getTopPadding();
675 * Returns the number of extra pixels of descent padding in the
676 * bottom line of the Layout.
678 public abstract int getBottomPadding();
682 * Returns true if the character at offset and the preceding character
683 * are at different run levels (and thus there's a split caret).
684 * @param offset the offset
685 * @return true if at a level boundary
688 public boolean isLevelBoundary(int offset) {
689 int line = getLineForOffset(offset);
690 Directions dirs = getLineDirections(line);
691 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
695 int[] runs = dirs.mDirections;
696 int lineStart = getLineStart(line);
697 int lineEnd = getLineEnd(line);
698 if (offset == lineStart || offset == lineEnd) {
699 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
700 int runIndex = offset == lineStart ? 0 : runs.length - 2;
701 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
705 for (int i = 0; i < runs.length; i += 2) {
706 if (offset == runs[i]) {
714 * Returns true if the character at offset is right to left (RTL).
715 * @param offset the offset
716 * @return true if the character is RTL, false if it is LTR
718 public boolean isRtlCharAt(int offset) {
719 int line = getLineForOffset(offset);
720 Directions dirs = getLineDirections(line);
721 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
724 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
727 int[] runs = dirs.mDirections;
728 int lineStart = getLineStart(line);
729 for (int i = 0; i < runs.length; i += 2) {
730 int start = lineStart + runs[i];
731 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
732 if (offset >= start && offset < limit) {
733 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
734 return ((level & 1) != 0);
737 // Should happen only if the offset is "out of bounds"
741 private boolean primaryIsTrailingPrevious(int offset) {
742 int line = getLineForOffset(offset);
743 int lineStart = getLineStart(line);
744 int lineEnd = getLineEnd(line);
745 int[] runs = getLineDirections(line).mDirections;
748 for (int i = 0; i < runs.length; i += 2) {
749 int start = lineStart + runs[i];
750 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
751 if (limit > lineEnd) {
754 if (offset >= start && offset < limit) {
755 if (offset > start) {
756 // Previous character is at same level, so don't use trailing.
759 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
764 // Offset was limit of line.
765 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
768 // At level boundary, check previous level.
769 int levelBefore = -1;
770 if (offset == lineStart) {
771 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
774 for (int i = 0; i < runs.length; i += 2) {
775 int start = lineStart + runs[i];
776 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
777 if (limit > lineEnd) {
780 if (offset >= start && offset < limit) {
781 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
787 return levelBefore < levelAt;
791 * Get the primary horizontal position for the specified text offset.
792 * This is the location where a new character would be inserted in
793 * the paragraph's primary direction.
795 public float getPrimaryHorizontal(int offset) {
796 return getPrimaryHorizontal(offset, false /* not clamped */);
800 * Get the primary horizontal position for the specified text offset, but
801 * optionally clamp it so that it doesn't exceed the width of the layout.
804 public float getPrimaryHorizontal(int offset, boolean clamped) {
805 boolean trailing = primaryIsTrailingPrevious(offset);
806 return getHorizontal(offset, trailing, clamped);
810 * Get the secondary horizontal position for the specified text offset.
811 * This is the location where a new character would be inserted in
812 * the direction other than the paragraph's primary direction.
814 public float getSecondaryHorizontal(int offset) {
815 return getSecondaryHorizontal(offset, false /* not clamped */);
819 * Get the secondary horizontal position for the specified text offset, but
820 * optionally clamp it so that it doesn't exceed the width of the layout.
823 public float getSecondaryHorizontal(int offset, boolean clamped) {
824 boolean trailing = primaryIsTrailingPrevious(offset);
825 return getHorizontal(offset, !trailing, clamped);
828 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
829 int line = getLineForOffset(offset);
831 return getHorizontal(offset, trailing, line, clamped);
834 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
835 int start = getLineStart(line);
836 int end = getLineEnd(line);
837 int dir = getParagraphDirection(line);
838 boolean hasTabOrEmoji = getLineContainsTab(line);
839 Directions directions = getLineDirections(line);
841 TabStops tabStops = null;
842 if (hasTabOrEmoji && mText instanceof Spanned) {
843 // Just checking this line should be good enough, tabs should be
844 // consistent across all lines in a paragraph.
845 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
846 if (tabs.length > 0) {
847 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
851 TextLine tl = TextLine.obtain();
852 tl.set(mPaint, mText, start, end, dir, directions, hasTabOrEmoji, tabStops);
853 float wid = tl.measure(offset - start, trailing, null);
854 TextLine.recycle(tl);
856 if (clamped && wid > mWidth) {
859 int left = getParagraphLeft(line);
860 int right = getParagraphRight(line);
862 return getLineStartPos(line, left, right) + wid;
866 * Get the leftmost position that should be exposed for horizontal
867 * scrolling on the specified line.
869 public float getLineLeft(int line) {
870 int dir = getParagraphDirection(line);
871 Alignment align = getParagraphAlignment(line);
873 if (align == Alignment.ALIGN_LEFT) {
875 } else if (align == Alignment.ALIGN_NORMAL) {
876 if (dir == DIR_RIGHT_TO_LEFT)
877 return getParagraphRight(line) - getLineMax(line);
880 } else if (align == Alignment.ALIGN_RIGHT) {
881 return mWidth - getLineMax(line);
882 } else if (align == Alignment.ALIGN_OPPOSITE) {
883 if (dir == DIR_RIGHT_TO_LEFT)
886 return mWidth - getLineMax(line);
887 } else { /* align == Alignment.ALIGN_CENTER */
888 int left = getParagraphLeft(line);
889 int right = getParagraphRight(line);
890 int max = ((int) getLineMax(line)) & ~1;
892 return left + ((right - left) - max) / 2;
897 * Get the rightmost position that should be exposed for horizontal
898 * scrolling on the specified line.
900 public float getLineRight(int line) {
901 int dir = getParagraphDirection(line);
902 Alignment align = getParagraphAlignment(line);
904 if (align == Alignment.ALIGN_LEFT) {
905 return getParagraphLeft(line) + getLineMax(line);
906 } else if (align == Alignment.ALIGN_NORMAL) {
907 if (dir == DIR_RIGHT_TO_LEFT)
910 return getParagraphLeft(line) + getLineMax(line);
911 } else if (align == Alignment.ALIGN_RIGHT) {
913 } else if (align == Alignment.ALIGN_OPPOSITE) {
914 if (dir == DIR_RIGHT_TO_LEFT)
915 return getLineMax(line);
918 } else { /* align == Alignment.ALIGN_CENTER */
919 int left = getParagraphLeft(line);
920 int right = getParagraphRight(line);
921 int max = ((int) getLineMax(line)) & ~1;
923 return right - ((right - left) - max) / 2;
928 * Gets the unsigned horizontal extent of the specified line, including
929 * leading margin indent, but excluding trailing whitespace.
931 public float getLineMax(int line) {
932 float margin = getParagraphLeadingMargin(line);
933 float signedExtent = getLineExtent(line, false);
934 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
938 * Gets the unsigned horizontal extent of the specified line, including
939 * leading margin indent and trailing whitespace.
941 public float getLineWidth(int line) {
942 float margin = getParagraphLeadingMargin(line);
943 float signedExtent = getLineExtent(line, true);
944 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
948 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
949 * tab stops instead of using the ones passed in.
950 * @param line the index of the line
951 * @param full whether to include trailing whitespace
952 * @return the extent of the line
954 private float getLineExtent(int line, boolean full) {
955 int start = getLineStart(line);
956 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
958 boolean hasTabsOrEmoji = getLineContainsTab(line);
959 TabStops tabStops = null;
960 if (hasTabsOrEmoji && mText instanceof Spanned) {
961 // Just checking this line should be good enough, tabs should be
962 // consistent across all lines in a paragraph.
963 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
964 if (tabs.length > 0) {
965 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
968 Directions directions = getLineDirections(line);
969 // Returned directions can actually be null
970 if (directions == null) {
973 int dir = getParagraphDirection(line);
975 TextLine tl = TextLine.obtain();
976 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
977 float width = tl.metrics(null);
978 TextLine.recycle(tl);
983 * Returns the signed horizontal extent of the specified line, excluding
984 * leading margin. If full is false, excludes trailing whitespace.
985 * @param line the index of the line
986 * @param tabStops the tab stops, can be null if we know they're not used.
987 * @param full whether to include trailing whitespace
988 * @return the extent of the text on this line
990 private float getLineExtent(int line, TabStops tabStops, boolean full) {
991 int start = getLineStart(line);
992 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
993 boolean hasTabsOrEmoji = getLineContainsTab(line);
994 Directions directions = getLineDirections(line);
995 int dir = getParagraphDirection(line);
997 TextLine tl = TextLine.obtain();
998 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
999 float width = tl.metrics(null);
1000 TextLine.recycle(tl);
1005 * Get the line number corresponding to the specified vertical position.
1006 * If you ask for a position above 0, you get 0; if you ask for a position
1007 * below the bottom of the text, you get the last line.
1009 // FIXME: It may be faster to do a linear search for layouts without many lines.
1010 public int getLineForVertical(int vertical) {
1011 int high = getLineCount(), low = -1, guess;
1013 while (high - low > 1) {
1014 guess = (high + low) / 2;
1016 if (getLineTop(guess) > vertical)
1029 * Get the line number on which the specified text offset appears.
1030 * If you ask for a position before 0, you get 0; if you ask for a position
1031 * beyond the end of the text, you get the last line.
1033 public int getLineForOffset(int offset) {
1034 int high = getLineCount(), low = -1, guess;
1036 while (high - low > 1) {
1037 guess = (high + low) / 2;
1039 if (getLineStart(guess) > offset)
1052 * Get the character offset on the specified line whose position is
1053 * closest to the specified horizontal position.
1055 public int getOffsetForHorizontal(int line, float horiz) {
1056 int max = getLineEnd(line) - 1;
1057 int min = getLineStart(line);
1058 Directions dirs = getLineDirections(line);
1060 if (line == getLineCount() - 1)
1064 float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz);
1066 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1067 int here = min + dirs.mDirections[i];
1068 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1069 int swap = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0 ? -1 : 1;
1073 int high = there - 1 + 1, low = here + 1 - 1, guess;
1075 while (high - low > 1) {
1076 guess = (high + low) / 2;
1077 int adguess = getOffsetAtStartOf(guess);
1079 if (getPrimaryHorizontal(adguess) * swap >= horiz * swap)
1089 low = getOffsetAtStartOf(low);
1091 float dist = Math.abs(getPrimaryHorizontal(low) - horiz);
1093 int aft = TextUtils.getOffsetAfter(mText, low);
1095 float other = Math.abs(getPrimaryHorizontal(aft) - horiz);
1103 if (dist < bestdist) {
1109 float dist = Math.abs(getPrimaryHorizontal(here) - horiz);
1111 if (dist < bestdist) {
1117 float dist = Math.abs(getPrimaryHorizontal(max) - horiz);
1119 if (dist <= bestdist) {
1128 * Return the text offset after the last character on the specified line.
1130 public final int getLineEnd(int line) {
1131 return getLineStart(line + 1);
1135 * Return the text offset after the last visible character (so whitespace
1136 * is not counted) on the specified line.
1138 public int getLineVisibleEnd(int line) {
1139 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1142 private int getLineVisibleEnd(int line, int start, int end) {
1143 CharSequence text = mText;
1145 if (line == getLineCount() - 1) {
1149 for (; end > start; end--) {
1150 ch = text.charAt(end - 1);
1156 if (ch != ' ' && ch != '\t') {
1166 * Return the vertical position of the bottom of the specified line.
1168 public final int getLineBottom(int line) {
1169 return getLineTop(line + 1);
1173 * Return the vertical position of the baseline of the specified line.
1175 public final int getLineBaseline(int line) {
1176 // getLineTop(line+1) == getLineTop(line)
1177 return getLineTop(line+1) - getLineDescent(line);
1181 * Get the ascent of the text on the specified line.
1182 * The return value is negative to match the Paint.ascent() convention.
1184 public final int getLineAscent(int line) {
1185 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1186 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1189 public int getOffsetToLeftOf(int offset) {
1190 return getOffsetToLeftRightOf(offset, true);
1193 public int getOffsetToRightOf(int offset) {
1194 return getOffsetToLeftRightOf(offset, false);
1197 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1198 int line = getLineForOffset(caret);
1199 int lineStart = getLineStart(line);
1200 int lineEnd = getLineEnd(line);
1201 int lineDir = getParagraphDirection(line);
1203 boolean lineChanged = false;
1204 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
1205 // if walking off line, look at the line we're headed to
1207 if (caret == lineEnd) {
1208 if (line < getLineCount() - 1) {
1212 return caret; // at very end, don't move
1216 if (caret == lineStart) {
1221 return caret; // at very start, don't move
1227 lineStart = getLineStart(line);
1228 lineEnd = getLineEnd(line);
1229 int newDir = getParagraphDirection(line);
1230 if (newDir != lineDir) {
1231 // unusual case. we want to walk onto the line, but it runs
1232 // in a different direction than this one, so we fake movement
1233 // in the opposite direction.
1239 Directions directions = getLineDirections(line);
1241 TextLine tl = TextLine.obtain();
1242 // XXX: we don't care about tabs
1243 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1244 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1245 tl = TextLine.recycle(tl);
1249 private int getOffsetAtStartOf(int offset) {
1250 // XXX this probably should skip local reorderings and
1251 // zero-width characters, look at callers
1255 CharSequence text = mText;
1256 char c = text.charAt(offset);
1258 if (c >= '\uDC00' && c <= '\uDFFF') {
1259 char c1 = text.charAt(offset - 1);
1261 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1266 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1267 ReplacementSpan.class);
1269 for (int i = 0; i < spans.length; i++) {
1270 int start = ((Spanned) text).getSpanStart(spans[i]);
1271 int end = ((Spanned) text).getSpanEnd(spans[i]);
1273 if (start < offset && end > offset)
1282 * Determine whether we should clamp cursor position. Currently it's
1283 * only robust for left-aligned displays.
1286 public boolean shouldClampCursor(int line) {
1287 // Only clamp cursor position in left-aligned displays.
1288 switch (getParagraphAlignment(line)) {
1292 return getParagraphDirection(line) > 0;
1299 * Fills in the specified Path with a representation of a cursor
1300 * at the specified offset. This will often be a vertical line
1301 * but can be multiple discontinuous lines in text with multiple
1304 public void getCursorPath(int point, Path dest,
1305 CharSequence editingBuffer) {
1308 int line = getLineForOffset(point);
1309 int top = getLineTop(line);
1310 int bottom = getLineTop(line+1);
1312 boolean clamped = shouldClampCursor(line);
1313 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1314 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
1316 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1317 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1318 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
1321 if (caps != 0 || fn != 0) {
1322 dist = (bottom - top) >> 2;
1335 if (Float.compare(h1, h2) == 0) {
1336 dest.moveTo(h1, top);
1337 dest.lineTo(h1, bottom);
1339 dest.moveTo(h1, top);
1340 dest.lineTo(h1, (top + bottom) >> 1);
1342 dest.moveTo(h2, (top + bottom) >> 1);
1343 dest.lineTo(h2, bottom);
1347 dest.moveTo(h2, bottom);
1348 dest.lineTo(h2 - dist, bottom + dist);
1349 dest.lineTo(h2, bottom);
1350 dest.lineTo(h2 + dist, bottom + dist);
1351 } else if (caps == 1) {
1352 dest.moveTo(h2, bottom);
1353 dest.lineTo(h2 - dist, bottom + dist);
1355 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1356 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1358 dest.moveTo(h2 + dist, bottom + dist);
1359 dest.lineTo(h2, bottom);
1363 dest.moveTo(h1, top);
1364 dest.lineTo(h1 - dist, top - dist);
1365 dest.lineTo(h1, top);
1366 dest.lineTo(h1 + dist, top - dist);
1367 } else if (fn == 1) {
1368 dest.moveTo(h1, top);
1369 dest.lineTo(h1 - dist, top - dist);
1371 dest.moveTo(h1 - dist, top - dist + 0.5f);
1372 dest.lineTo(h1 + dist, top - dist + 0.5f);
1374 dest.moveTo(h1 + dist, top - dist);
1375 dest.lineTo(h1, top);
1379 private void addSelection(int line, int start, int end,
1380 int top, int bottom, Path dest) {
1381 int linestart = getLineStart(line);
1382 int lineend = getLineEnd(line);
1383 Directions dirs = getLineDirections(line);
1385 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1388 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1389 int here = linestart + dirs.mDirections[i];
1390 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1392 if (there > lineend)
1395 if (start <= there && end >= here) {
1396 int st = Math.max(start, here);
1397 int en = Math.min(end, there);
1400 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1401 float h2 = getHorizontal(en, true, line, false /* not clamped */);
1403 float left = Math.min(h1, h2);
1404 float right = Math.max(h1, h2);
1406 dest.addRect(left, top, right, bottom, Path.Direction.CW);
1413 * Fills in the specified Path with a representation of a highlight
1414 * between the specified offsets. This will often be a rectangle
1415 * or a potentially discontinuous set of rectangles. If the start
1416 * and end are the same, the returned path is empty.
1418 public void getSelectionPath(int start, int end, Path dest) {
1430 int startline = getLineForOffset(start);
1431 int endline = getLineForOffset(end);
1433 int top = getLineTop(startline);
1434 int bottom = getLineBottom(endline);
1436 if (startline == endline) {
1437 addSelection(startline, start, end, top, bottom, dest);
1439 final float width = mWidth;
1441 addSelection(startline, start, getLineEnd(startline),
1442 top, getLineBottom(startline), dest);
1444 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1445 dest.addRect(getLineLeft(startline), top,
1446 0, getLineBottom(startline), Path.Direction.CW);
1448 dest.addRect(getLineRight(startline), top,
1449 width, getLineBottom(startline), Path.Direction.CW);
1451 for (int i = startline + 1; i < endline; i++) {
1452 top = getLineTop(i);
1453 bottom = getLineBottom(i);
1454 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1457 top = getLineTop(endline);
1458 bottom = getLineBottom(endline);
1460 addSelection(endline, getLineStart(endline), end,
1463 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1464 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1466 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1471 * Get the alignment of the specified paragraph, taking into account
1472 * markup attached to it.
1474 public final Alignment getParagraphAlignment(int line) {
1475 Alignment align = mAlignment;
1478 Spanned sp = (Spanned) mText;
1479 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
1481 AlignmentSpan.class);
1483 int spanLength = spans.length;
1484 if (spanLength > 0) {
1485 align = spans[spanLength-1].getAlignment();
1493 * Get the left edge of the specified paragraph, inset by left margins.
1495 public final int getParagraphLeft(int line) {
1497 int dir = getParagraphDirection(line);
1498 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1499 return left; // leading margin has no impact, or no styles
1501 return getParagraphLeadingMargin(line);
1505 * Get the right edge of the specified paragraph, inset by right margins.
1507 public final int getParagraphRight(int line) {
1509 int dir = getParagraphDirection(line);
1510 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1511 return right; // leading margin has no impact, or no styles
1513 return right - getParagraphLeadingMargin(line);
1517 * Returns the effective leading margin (unsigned) for this line,
1518 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1519 * @param line the line index
1520 * @return the leading margin of this line
1522 private int getParagraphLeadingMargin(int line) {
1523 if (!mSpannedText) {
1526 Spanned spanned = (Spanned) mText;
1528 int lineStart = getLineStart(line);
1529 int lineEnd = getLineEnd(line);
1530 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
1531 LeadingMarginSpan.class);
1532 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
1533 LeadingMarginSpan.class);
1534 if (spans.length == 0) {
1535 return 0; // no leading margin span;
1540 boolean isFirstParaLine = lineStart == 0 ||
1541 spanned.charAt(lineStart - 1) == '\n';
1543 boolean useFirstLineMargin = isFirstParaLine;
1544 for (int i = 0; i < spans.length; i++) {
1545 if (spans[i] instanceof LeadingMarginSpan2) {
1546 int spStart = spanned.getSpanStart(spans[i]);
1547 int spanLine = getLineForOffset(spStart);
1548 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1549 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1550 useFirstLineMargin |= line < spanLine + count;
1553 for (int i = 0; i < spans.length; i++) {
1554 LeadingMarginSpan span = spans[i];
1555 margin += span.getLeadingMargin(useFirstLineMargin);
1562 static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
1564 MeasuredText mt = MeasuredText.obtain();
1565 TextLine tl = TextLine.obtain();
1567 mt.setPara(text, start, end, TextDirectionHeuristics.LTR);
1568 Directions directions;
1571 directions = DIRS_ALL_LEFT_TO_RIGHT;
1572 dir = Layout.DIR_LEFT_TO_RIGHT;
1574 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1575 0, mt.mChars, 0, mt.mLen);
1578 char[] chars = mt.mChars;
1580 boolean hasTabs = false;
1581 TabStops tabStops = null;
1582 // leading margins should be taken into account when measuring a paragraph
1584 if (text instanceof Spanned) {
1585 Spanned spanned = (Spanned) text;
1586 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1587 LeadingMarginSpan.class);
1588 for (LeadingMarginSpan lms : spans) {
1589 margin += lms.getLeadingMargin(true);
1592 for (int i = 0; i < len; ++i) {
1593 if (chars[i] == '\t') {
1595 if (text instanceof Spanned) {
1596 Spanned spanned = (Spanned) text;
1597 int spanEnd = spanned.nextSpanTransition(start, end,
1599 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
1601 if (spans.length > 0) {
1602 tabStops = new TabStops(TAB_INCREMENT, spans);
1608 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
1609 return margin + tl.metrics(null);
1611 TextLine.recycle(tl);
1612 MeasuredText.recycle(mt);
1619 /* package */ static class TabStops {
1620 private int[] mStops;
1621 private int mNumStops;
1622 private int mIncrement;
1624 TabStops(int increment, Object[] spans) {
1625 reset(increment, spans);
1628 void reset(int increment, Object[] spans) {
1629 this.mIncrement = increment;
1632 if (spans != null) {
1633 int[] stops = this.mStops;
1634 for (Object o : spans) {
1635 if (o instanceof TabStopSpan) {
1636 if (stops == null) {
1637 stops = new int[10];
1638 } else if (ns == stops.length) {
1639 int[] nstops = new int[ns * 2];
1640 for (int i = 0; i < ns; ++i) {
1641 nstops[i] = stops[i];
1645 stops[ns++] = ((TabStopSpan) o).getTabStop();
1649 Arrays.sort(stops, 0, ns);
1651 if (stops != this.mStops) {
1652 this.mStops = stops;
1655 this.mNumStops = ns;
1658 float nextTab(float h) {
1659 int ns = this.mNumStops;
1661 int[] stops = this.mStops;
1662 for (int i = 0; i < ns; ++i) {
1663 int stop = stops[i];
1669 return nextDefaultStop(h, mIncrement);
1672 public static float nextDefaultStop(float h, int inc) {
1673 return ((int) ((h + inc) / inc)) * inc;
1678 * Returns the position of the next tab stop after h on the line.
1680 * @param text the text
1681 * @param start start of the line
1682 * @param end limit of the line
1683 * @param h the current horizontal offset
1684 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1685 * on the line will be used. If there are no tabs, a default offset
1686 * will be used to compute the tab stop.
1687 * @return the offset of the next tab stop.
1689 /* package */ static float nextTab(CharSequence text, int start, int end,
1690 float h, Object[] tabs) {
1691 float nh = Float.MAX_VALUE;
1692 boolean alltabs = false;
1694 if (text instanceof Spanned) {
1696 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
1700 for (int i = 0; i < tabs.length; i++) {
1702 if (!(tabs[i] instanceof TabStopSpan))
1706 int where = ((TabStopSpan) tabs[i]).getTabStop();
1708 if (where < nh && where > h)
1712 if (nh != Float.MAX_VALUE)
1716 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1719 protected final boolean isSpanned() {
1720 return mSpannedText;
1724 * Returns the same as <code>text.getSpans()</code>, except where
1725 * <code>start</code> and <code>end</code> are the same and are not
1726 * at the very beginning of the text, in which case an empty array
1727 * is returned instead.
1729 * This is needed because of the special case that <code>getSpans()</code>
1730 * on an empty range returns the spans adjacent to that range, which is
1731 * primarily for the sake of <code>TextWatchers</code> so they will get
1732 * notifications when text goes from empty to non-empty. But it also
1733 * has the unfortunate side effect that if the text ends with an empty
1734 * paragraph, that paragraph accidentally picks up the styles of the
1735 * preceding paragraph (even though those styles will not be picked up
1736 * by new text that is inserted into the empty paragraph).
1738 * The reason it just checks whether <code>start</code> and <code>end</code>
1739 * is the same is that the only time a line can contain 0 characters
1740 * is if it is the final paragraph of the Layout; otherwise any line will
1741 * contain at least one printing or newline character. The reason for the
1742 * additional check if <code>start</code> is greater than 0 is that
1743 * if the empty paragraph is the entire content of the buffer, paragraph
1744 * styles that are already applied to the buffer will apply to text that
1745 * is inserted into it.
1747 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
1748 if (start == end && start > 0) {
1749 return ArrayUtils.emptyArray(type);
1752 return text.getSpans(start, end, type);
1755 private char getEllipsisChar(TextUtils.TruncateAt method) {
1756 return (method == TextUtils.TruncateAt.END_SMALL) ?
1757 TextUtils.ELLIPSIS_TWO_DOTS[0] :
1758 TextUtils.ELLIPSIS_NORMAL[0];
1761 private void ellipsize(int start, int end, int line,
1762 char[] dest, int destoff, TextUtils.TruncateAt method) {
1763 int ellipsisCount = getEllipsisCount(line);
1765 if (ellipsisCount == 0) {
1769 int ellipsisStart = getEllipsisStart(line);
1770 int linestart = getLineStart(line);
1772 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1775 if (i == ellipsisStart) {
1776 c = getEllipsisChar(method); // ellipsis
1778 c = '\uFEFF'; // 0-width space
1781 int a = i + linestart;
1783 if (a >= start && a < end) {
1784 dest[destoff + a - start] = c;
1790 * Stores information about bidirectional (left-to-right or right-to-left)
1791 * text within the layout of a line.
1793 public static class Directions {
1794 // Directions represents directional runs within a line of text.
1795 // Runs are pairs of ints listed in visual order, starting from the
1796 // leading margin. The first int of each pair is the offset from
1797 // the first character of the line to the start of the run. The
1798 // second int represents both the length and level of the run.
1799 // The length is in the lower bits, accessed by masking with
1800 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1801 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1802 // To simply test for an RTL direction, test the bit using
1803 // DIR_RTL_FLAG, if set then the direction is rtl.
1805 /* package */ int[] mDirections;
1806 /* package */ Directions(int[] dirs) {
1812 * Return the offset of the first character to be ellipsized away,
1813 * relative to the start of the line. (So 0 if the beginning of the
1814 * line is ellipsized, not getLineStart().)
1816 public abstract int getEllipsisStart(int line);
1819 * Returns the number of characters to be ellipsized away, or 0 if
1820 * no ellipsis is to take place.
1822 public abstract int getEllipsisCount(int line);
1824 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1825 /* package */ CharSequence mText;
1826 /* package */ Layout mLayout;
1827 /* package */ int mWidth;
1828 /* package */ TextUtils.TruncateAt mMethod;
1830 public Ellipsizer(CharSequence s) {
1834 public char charAt(int off) {
1835 char[] buf = TextUtils.obtain(1);
1836 getChars(off, off + 1, buf, 0);
1839 TextUtils.recycle(buf);
1843 public void getChars(int start, int end, char[] dest, int destoff) {
1844 int line1 = mLayout.getLineForOffset(start);
1845 int line2 = mLayout.getLineForOffset(end);
1847 TextUtils.getChars(mText, start, end, dest, destoff);
1849 for (int i = line1; i <= line2; i++) {
1850 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
1854 public int length() {
1855 return mText.length();
1858 public CharSequence subSequence(int start, int end) {
1859 char[] s = new char[end - start];
1860 getChars(start, end, s, 0);
1861 return new String(s);
1865 public String toString() {
1866 char[] s = new char[length()];
1867 getChars(0, length(), s, 0);
1868 return new String(s);
1873 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
1874 private Spanned mSpanned;
1876 public SpannedEllipsizer(CharSequence display) {
1878 mSpanned = (Spanned) display;
1881 public <T> T[] getSpans(int start, int end, Class<T> type) {
1882 return mSpanned.getSpans(start, end, type);
1885 public int getSpanStart(Object tag) {
1886 return mSpanned.getSpanStart(tag);
1889 public int getSpanEnd(Object tag) {
1890 return mSpanned.getSpanEnd(tag);
1893 public int getSpanFlags(Object tag) {
1894 return mSpanned.getSpanFlags(tag);
1897 @SuppressWarnings("rawtypes")
1898 public int nextSpanTransition(int start, int limit, Class type) {
1899 return mSpanned.nextSpanTransition(start, limit, type);
1903 public CharSequence subSequence(int start, int end) {
1904 char[] s = new char[end - start];
1905 getChars(start, end, s, 0);
1907 SpannableString ss = new SpannableString(new String(s));
1908 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
1913 private CharSequence mText;
1914 private TextPaint mPaint;
1915 /* package */ TextPaint mWorkPaint;
1917 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
1918 private float mSpacingMult;
1919 private float mSpacingAdd;
1920 private static final Rect sTempRect = new Rect();
1921 private boolean mSpannedText;
1922 private TextDirectionHeuristic mTextDir;
1923 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
1925 public static final int DIR_LEFT_TO_RIGHT = 1;
1926 public static final int DIR_RIGHT_TO_LEFT = -1;
1928 /* package */ static final int DIR_REQUEST_LTR = 1;
1929 /* package */ static final int DIR_REQUEST_RTL = -1;
1930 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
1931 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
1933 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
1934 /* package */ static final int RUN_LEVEL_SHIFT = 26;
1935 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
1936 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
1938 public enum Alignment {
1948 private static final int TAB_INCREMENT = 20;
1950 /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
1951 new Directions(new int[] { 0, RUN_LENGTH_MASK });
1952 /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
1953 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });