Layout.java revision 60e3b3fa60a672583fe6294139feb940845efc9b
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text;
18
19import android.emoji.EmojiFactory;
20import android.graphics.Canvas;
21import android.graphics.Paint;
22import android.graphics.Path;
23import android.graphics.Rect;
24import android.text.method.TextKeyListener;
25import android.text.style.AlignmentSpan;
26import android.text.style.LeadingMarginSpan;
27import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
28import android.text.style.LineBackgroundSpan;
29import android.text.style.ParagraphStyle;
30import android.text.style.ReplacementSpan;
31import android.text.style.TabStopSpan;
32
33import com.android.internal.util.ArrayUtils;
34
35import java.util.Arrays;
36
37/**
38 * A base class that manages text layout in visual elements on
39 * the screen.
40 * <p>For text that will be edited, use a {@link DynamicLayout},
41 * which will be updated as the text changes.
42 * For text that will not change, use a {@link StaticLayout}.
43 */
44public abstract class Layout {
45    private static final ParagraphStyle[] NO_PARA_SPANS =
46        ArrayUtils.emptyArray(ParagraphStyle.class);
47
48    /* package */ static final EmojiFactory EMOJI_FACTORY =
49        EmojiFactory.newAvailableInstance();
50    /* package */ static final int MIN_EMOJI, MAX_EMOJI;
51
52    static {
53        if (EMOJI_FACTORY != null) {
54            MIN_EMOJI = EMOJI_FACTORY.getMinimumAndroidPua();
55            MAX_EMOJI = EMOJI_FACTORY.getMaximumAndroidPua();
56        } else {
57            MIN_EMOJI = -1;
58            MAX_EMOJI = -1;
59        }
60    }
61
62    /**
63     * Return how wide a layout must be in order to display the
64     * specified text with one line per paragraph.
65     */
66    public static float getDesiredWidth(CharSequence source,
67                                        TextPaint paint) {
68        return getDesiredWidth(source, 0, source.length(), paint);
69    }
70
71    /**
72     * Return how wide a layout must be in order to display the
73     * specified text slice with one line per paragraph.
74     */
75    public static float getDesiredWidth(CharSequence source,
76                                        int start, int end,
77                                        TextPaint paint) {
78        float need = 0;
79
80        int next;
81        for (int i = start; i <= end; i = next) {
82            next = TextUtils.indexOf(source, '\n', i, end);
83
84            if (next < 0)
85                next = end;
86
87            // note, omits trailing paragraph char
88            float w = measurePara(paint, source, i, next);
89
90            if (w > need)
91                need = w;
92
93            next++;
94        }
95
96        return need;
97    }
98
99    /**
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
111     */
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);
117    }
118
119    /**
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
131     *
132     * @hide
133     */
134    protected Layout(CharSequence text, TextPaint paint,
135                     int width, Alignment align, TextDirectionHeuristic textDir,
136                     float spacingMult, float spacingAdd) {
137
138        if (width < 0)
139            throw new IllegalArgumentException("Layout: " + width + " < 0");
140
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.
145        if (paint != null) {
146            paint.bgColor = 0;
147            paint.baselineShift = 0;
148        }
149
150        mText = text;
151        mPaint = paint;
152        mWorkPaint = new TextPaint();
153        mWidth = width;
154        mAlignment = align;
155        mSpacingMult = spacingMult;
156        mSpacingAdd = spacingAdd;
157        mSpannedText = text instanceof Spanned;
158        mTextDir = textDir;
159    }
160
161    /**
162     * Replace constructor properties of this Layout with new ones.  Be careful.
163     */
164    /* package */ void replaceWith(CharSequence text, TextPaint paint,
165                              int width, Alignment align,
166                              float spacingmult, float spacingadd) {
167        if (width < 0) {
168            throw new IllegalArgumentException("Layout: " + width + " < 0");
169        }
170
171        mText = text;
172        mPaint = paint;
173        mWidth = width;
174        mAlignment = align;
175        mSpacingMult = spacingmult;
176        mSpacingAdd = spacingadd;
177        mSpannedText = text instanceof Spanned;
178    }
179
180    /**
181     * Draw this Layout on the specified Canvas.
182     */
183    public void draw(Canvas c) {
184        draw(c, null, null, 0);
185    }
186
187    /**
188     * Draw this Layout on the specified canvas, with the highlight path drawn
189     * between the background and the text.
190     *
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
196     */
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;
203
204        drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
205                firstLine, lastLine);
206        drawText(canvas, firstLine, lastLine);
207    }
208
209    /**
210     * @hide
211     */
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;
216        int spanEnd = 0;
217        TextPaint paint = mPaint;
218        CharSequence buf = mText;
219
220        Alignment paraAlign = mAlignment;
221        TabStops tabStops = null;
222        boolean tabStopsIsInitialized = false;
223
224        TextLine tl = TextLine.obtain();
225
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);
232
233            int ltop = previousLineBottom;
234            int lbottom = getLineTop(i+1);
235            previousLineBottom = lbottom;
236            int lbaseline = lbottom - getLineDescent(i);
237
238            int dir = getParagraphDirection(i);
239            int left = 0;
240            int right = mWidth;
241
242            if (mSpannedText) {
243                Spanned sp = (Spanned) buf;
244                int textLength = buf.length();
245                boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
246
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
250                // tabs.
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
256                // our problem.
257                if (start >= spanEnd && (i == firstLine || isFirstParaLine)) {
258                    spanEnd = sp.nextSpanTransition(start, textLength,
259                                                    ParagraphStyle.class);
260                    spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
261
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();
266                            break;
267                        }
268                    }
269
270                    tabStopsIsInitialized = false;
271                }
272
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                for (int n = 0; n < length; n++) {
277                    if (spans[n] instanceof LeadingMarginSpan) {
278                        LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
279                        boolean useFirstLineMargin = isFirstParaLine;
280                        if (margin instanceof LeadingMarginSpan2) {
281                            int count = ((LeadingMarginSpan2) margin).getLeadingMarginLineCount();
282                            int startLine = getLineForOffset(sp.getSpanStart(margin));
283                            useFirstLineMargin = i < startLine + count;
284                        }
285
286                        if (dir == DIR_RIGHT_TO_LEFT) {
287                            margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
288                                                     lbaseline, lbottom, buf,
289                                                     start, end, isFirstParaLine, this);
290                            right -= margin.getLeadingMargin(useFirstLineMargin);
291                        } else {
292                            margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
293                                                     lbaseline, lbottom, buf,
294                                                     start, end, isFirstParaLine, this);
295                            left += margin.getLeadingMargin(useFirstLineMargin);
296                        }
297                    }
298                }
299            }
300
301            boolean hasTabOrEmoji = getLineContainsTab(i);
302            // Can't tell if we have tabs for sure, currently
303            if (hasTabOrEmoji && !tabStopsIsInitialized) {
304                if (tabStops == null) {
305                    tabStops = new TabStops(TAB_INCREMENT, spans);
306                } else {
307                    tabStops.reset(TAB_INCREMENT, spans);
308                }
309                tabStopsIsInitialized = true;
310            }
311
312            // Determine whether the line aligns to normal, opposite, or center.
313            Alignment align = paraAlign;
314            if (align == Alignment.ALIGN_LEFT) {
315                align = (dir == DIR_LEFT_TO_RIGHT) ?
316                    Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
317            } else if (align == Alignment.ALIGN_RIGHT) {
318                align = (dir == DIR_LEFT_TO_RIGHT) ?
319                    Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
320            }
321
322            int x;
323            if (align == Alignment.ALIGN_NORMAL) {
324                if (dir == DIR_LEFT_TO_RIGHT) {
325                    x = left;
326                } else {
327                    x = right;
328                }
329            } else {
330                int max = (int)getLineExtent(i, tabStops, false);
331                if (align == Alignment.ALIGN_OPPOSITE) {
332                    if (dir == DIR_LEFT_TO_RIGHT) {
333                        x = right - max;
334                    } else {
335                        x = left - max;
336                    }
337                } else { // Alignment.ALIGN_CENTER
338                    max = max & ~1;
339                    x = (right + left - max) >> 1;
340                }
341            }
342
343            Directions directions = getLineDirections(i);
344            if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTabOrEmoji) {
345                // XXX: assumes there's nothing additional to be done
346                canvas.drawText(buf, start, end, x, lbaseline, paint);
347            } else {
348                tl.set(paint, buf, start, end, dir, directions, hasTabOrEmoji, tabStops);
349                tl.draw(canvas, x, ltop, lbaseline, lbottom);
350            }
351        }
352
353        TextLine.recycle(tl);
354    }
355
356    /**
357     * @hide
358     */
359    public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
360            int cursorOffsetVertical, int firstLine, int lastLine) {
361        // First, draw LineBackgroundSpans.
362        // LineBackgroundSpans know nothing about the alignment, margins, or
363        // direction of the layout or line.  XXX: Should they?
364        // They are evaluated at each line.
365        if (mSpannedText) {
366            if (lineBackgroundSpans == null) {
367                lineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
368            }
369
370            Spanned buffer = (Spanned) mText;
371            int textLength = buffer.length();
372            lineBackgroundSpans.init(buffer, 0, textLength);
373
374            if (lineBackgroundSpans.numberOfSpans > 0) {
375                int previousLineBottom = getLineTop(firstLine);
376                int previousLineEnd = getLineStart(firstLine);
377                ParagraphStyle[] spans = NO_PARA_SPANS;
378                int spansLength = 0;
379                TextPaint paint = mPaint;
380                int spanEnd = 0;
381                final int width = mWidth;
382                for (int i = firstLine; i <= lastLine; i++) {
383                    int start = previousLineEnd;
384                    int end = getLineStart(i + 1);
385                    previousLineEnd = end;
386
387                    int ltop = previousLineBottom;
388                    int lbottom = getLineTop(i + 1);
389                    previousLineBottom = lbottom;
390                    int lbaseline = lbottom - getLineDescent(i);
391
392                    if (start >= spanEnd) {
393                        // These should be infrequent, so we'll use this so that
394                        // we don't have to check as often.
395                        spanEnd = lineBackgroundSpans.getNextTransition(start, textLength);
396                        // All LineBackgroundSpans on a line contribute to its background.
397                        spansLength = 0;
398                        // Duplication of the logic of getParagraphSpans
399                        if (start != end || start == 0) {
400                            // Equivalent to a getSpans(start, end), but filling the 'spans' local
401                            // array instead to reduce memory allocation
402                            for (int j = 0; j < lineBackgroundSpans.numberOfSpans; j++) {
403                                // equal test is valid since both intervals are not empty by construction
404                                if (lineBackgroundSpans.spanStarts[j] >= end ||
405                                        lineBackgroundSpans.spanEnds[j] <= start) continue;
406                                if (spansLength == spans.length) {
407                                    // The spans array needs to be expanded
408                                    int newSize = ArrayUtils.idealObjectArraySize(2 * spansLength);
409                                    ParagraphStyle[] newSpans = new ParagraphStyle[newSize];
410                                    System.arraycopy(spans, 0, newSpans, 0, spansLength);
411                                    spans = newSpans;
412                                }
413                                spans[spansLength++] = lineBackgroundSpans.spans[j];
414                            }
415                        }
416                    }
417
418                    for (int n = 0; n < spansLength; n++) {
419                        LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
420                        lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
421                                ltop, lbaseline, lbottom,
422                                buffer, start, end, i);
423                    }
424                }
425            }
426            lineBackgroundSpans.recycle();
427        }
428
429        // There can be a highlight even without spans if we are drawing
430        // a non-spanned transformation of a spanned editing buffer.
431        if (highlight != null) {
432            if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
433            canvas.drawPath(highlight, highlightPaint);
434            if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
435        }
436    }
437
438    /**
439     * @param canvas
440     * @return The range of lines that need to be drawn, possibly empty.
441     * @hide
442     */
443    public long getLineRangeForDraw(Canvas canvas) {
444        int dtop, dbottom;
445
446        synchronized (sTempRect) {
447            if (!canvas.getClipBounds(sTempRect)) {
448                // Negative range end used as a special flag
449                return TextUtils.packRangeInLong(0, -1);
450            }
451
452            dtop = sTempRect.top;
453            dbottom = sTempRect.bottom;
454        }
455
456        final int top = Math.max(dtop, 0);
457        final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
458
459        return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
460    }
461
462    /**
463     * Return the start position of the line, given the left and right bounds
464     * of the margins.
465     *
466     * @param line the line index
467     * @param left the left bounds (0, or leading margin if ltr para)
468     * @param right the right bounds (width, minus leading margin if rtl para)
469     * @return the start position of the line (to right of line if rtl para)
470     */
471    private int getLineStartPos(int line, int left, int right) {
472        // Adjust the point at which to start rendering depending on the
473        // alignment of the paragraph.
474        Alignment align = getParagraphAlignment(line);
475        int dir = getParagraphDirection(line);
476
477        int x;
478        if (align == Alignment.ALIGN_LEFT) {
479            x = left;
480        } else if (align == Alignment.ALIGN_NORMAL) {
481            if (dir == DIR_LEFT_TO_RIGHT) {
482                x = left;
483            } else {
484                x = right;
485            }
486        } else {
487            TabStops tabStops = null;
488            if (mSpannedText && getLineContainsTab(line)) {
489                Spanned spanned = (Spanned) mText;
490                int start = getLineStart(line);
491                int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
492                        TabStopSpan.class);
493                TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
494                        TabStopSpan.class);
495                if (tabSpans.length > 0) {
496                    tabStops = new TabStops(TAB_INCREMENT, tabSpans);
497                }
498            }
499            int max = (int)getLineExtent(line, tabStops, false);
500            if (align == Alignment.ALIGN_RIGHT) {
501                x = right - max;
502            } else if (align == Alignment.ALIGN_OPPOSITE) {
503                if (dir == DIR_LEFT_TO_RIGHT) {
504                    x = right - max;
505                } else {
506                    x = left - max;
507                }
508            } else { // Alignment.ALIGN_CENTER
509                max = max & ~1;
510                x = (left + right - max) >> 1;
511            }
512        }
513        return x;
514    }
515
516    /**
517     * Return the text that is displayed by this Layout.
518     */
519    public final CharSequence getText() {
520        return mText;
521    }
522
523    /**
524     * Return the base Paint properties for this layout.
525     * Do NOT change the paint, which may result in funny
526     * drawing for this layout.
527     */
528    public final TextPaint getPaint() {
529        return mPaint;
530    }
531
532    /**
533     * Return the width of this layout.
534     */
535    public final int getWidth() {
536        return mWidth;
537    }
538
539    /**
540     * Return the width to which this Layout is ellipsizing, or
541     * {@link #getWidth} if it is not doing anything special.
542     */
543    public int getEllipsizedWidth() {
544        return mWidth;
545    }
546
547    /**
548     * Increase the width of this layout to the specified width.
549     * Be careful to use this only when you know it is appropriate&mdash;
550     * it does not cause the text to reflow to use the full new width.
551     */
552    public final void increaseWidthTo(int wid) {
553        if (wid < mWidth) {
554            throw new RuntimeException("attempted to reduce Layout width");
555        }
556
557        mWidth = wid;
558    }
559
560    /**
561     * Return the total height of this layout.
562     */
563    public int getHeight() {
564        return getLineTop(getLineCount());
565    }
566
567    /**
568     * Return the base alignment of this layout.
569     */
570    public final Alignment getAlignment() {
571        return mAlignment;
572    }
573
574    /**
575     * Return what the text height is multiplied by to get the line height.
576     */
577    public final float getSpacingMultiplier() {
578        return mSpacingMult;
579    }
580
581    /**
582     * Return the number of units of leading that are added to each line.
583     */
584    public final float getSpacingAdd() {
585        return mSpacingAdd;
586    }
587
588    /**
589     * Return the heuristic used to determine paragraph text direction.
590     * @hide
591     */
592    public final TextDirectionHeuristic getTextDirectionHeuristic() {
593        return mTextDir;
594    }
595
596    /**
597     * Return the number of lines of text in this layout.
598     */
599    public abstract int getLineCount();
600
601    /**
602     * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
603     * If bounds is not null, return the top, left, right, bottom extents
604     * of the specified line in it.
605     * @param line which line to examine (0..getLineCount() - 1)
606     * @param bounds Optional. If not null, it returns the extent of the line
607     * @return the Y-coordinate of the baseline
608     */
609    public int getLineBounds(int line, Rect bounds) {
610        if (bounds != null) {
611            bounds.left = 0;     // ???
612            bounds.top = getLineTop(line);
613            bounds.right = mWidth;   // ???
614            bounds.bottom = getLineTop(line + 1);
615        }
616        return getLineBaseline(line);
617    }
618
619    /**
620     * Return the vertical position of the top of the specified line
621     * (0&hellip;getLineCount()).
622     * If the specified line is equal to the line count, returns the
623     * bottom of the last line.
624     */
625    public abstract int getLineTop(int line);
626
627    /**
628     * Return the descent of the specified line(0&hellip;getLineCount() - 1).
629     */
630    public abstract int getLineDescent(int line);
631
632    /**
633     * Return the text offset of the beginning of the specified line (
634     * 0&hellip;getLineCount()). If the specified line is equal to the line
635     * count, returns the length of the text.
636     */
637    public abstract int getLineStart(int line);
638
639    /**
640     * Returns the primary directionality of the paragraph containing the
641     * specified line, either 1 for left-to-right lines, or -1 for right-to-left
642     * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
643     */
644    public abstract int getParagraphDirection(int line);
645
646    /**
647     * Returns whether the specified line contains one or more
648     * characters that need to be handled specially, like tabs
649     * or emoji.
650     */
651    public abstract boolean getLineContainsTab(int line);
652
653    /**
654     * Returns the directional run information for the specified line.
655     * The array alternates counts of characters in left-to-right
656     * and right-to-left segments of the line.
657     *
658     * <p>NOTE: this is inadequate to support bidirectional text, and will change.
659     */
660    public abstract Directions getLineDirections(int line);
661
662    /**
663     * Returns the (negative) number of extra pixels of ascent padding in the
664     * top line of the Layout.
665     */
666    public abstract int getTopPadding();
667
668    /**
669     * Returns the number of extra pixels of descent padding in the
670     * bottom line of the Layout.
671     */
672    public abstract int getBottomPadding();
673
674
675    /**
676     * Returns true if the character at offset and the preceding character
677     * are at different run levels (and thus there's a split caret).
678     * @param offset the offset
679     * @return true if at a level boundary
680     * @hide
681     */
682    public boolean isLevelBoundary(int offset) {
683        int line = getLineForOffset(offset);
684        Directions dirs = getLineDirections(line);
685        if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
686            return false;
687        }
688
689        int[] runs = dirs.mDirections;
690        int lineStart = getLineStart(line);
691        int lineEnd = getLineEnd(line);
692        if (offset == lineStart || offset == lineEnd) {
693            int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
694            int runIndex = offset == lineStart ? 0 : runs.length - 2;
695            return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
696        }
697
698        offset -= lineStart;
699        for (int i = 0; i < runs.length; i += 2) {
700            if (offset == runs[i]) {
701                return true;
702            }
703        }
704        return false;
705    }
706
707    /**
708     * Returns true if the character at offset is right to left (RTL).
709     * @param offset the offset
710     * @return true if the character is RTL, false if it is LTR
711     */
712    public boolean isRtlCharAt(int offset) {
713        int line = getLineForOffset(offset);
714        Directions dirs = getLineDirections(line);
715        if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
716            return false;
717        }
718        if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
719            return  true;
720        }
721        int[] runs = dirs.mDirections;
722        int lineStart = getLineStart(line);
723        for (int i = 0; i < runs.length; i += 2) {
724            int start = lineStart + (runs[i] & RUN_LENGTH_MASK);
725            // No need to test the end as an offset after the last run should return the value
726            // corresponding of the last run
727            if (offset >= start) {
728                int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
729                return ((level & 1) != 0);
730            }
731        }
732        // Should happen only if the offset is "out of bounds"
733        return false;
734    }
735
736    private boolean primaryIsTrailingPrevious(int offset) {
737        int line = getLineForOffset(offset);
738        int lineStart = getLineStart(line);
739        int lineEnd = getLineEnd(line);
740        int[] runs = getLineDirections(line).mDirections;
741
742        int levelAt = -1;
743        for (int i = 0; i < runs.length; i += 2) {
744            int start = lineStart + runs[i];
745            int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
746            if (limit > lineEnd) {
747                limit = lineEnd;
748            }
749            if (offset >= start && offset < limit) {
750                if (offset > start) {
751                    // Previous character is at same level, so don't use trailing.
752                    return false;
753                }
754                levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
755                break;
756            }
757        }
758        if (levelAt == -1) {
759            // Offset was limit of line.
760            levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
761        }
762
763        // At level boundary, check previous level.
764        int levelBefore = -1;
765        if (offset == lineStart) {
766            levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
767        } else {
768            offset -= 1;
769            for (int i = 0; i < runs.length; i += 2) {
770                int start = lineStart + runs[i];
771                int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
772                if (limit > lineEnd) {
773                    limit = lineEnd;
774                }
775                if (offset >= start && offset < limit) {
776                    levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
777                    break;
778                }
779            }
780        }
781
782        return levelBefore < levelAt;
783    }
784
785    /**
786     * Get the primary horizontal position for the specified text offset.
787     * This is the location where a new character would be inserted in
788     * the paragraph's primary direction.
789     */
790    public float getPrimaryHorizontal(int offset) {
791        boolean trailing = primaryIsTrailingPrevious(offset);
792        return getHorizontal(offset, trailing);
793    }
794
795    /**
796     * Get the secondary horizontal position for the specified text offset.
797     * This is the location where a new character would be inserted in
798     * the direction other than the paragraph's primary direction.
799     */
800    public float getSecondaryHorizontal(int offset) {
801        boolean trailing = primaryIsTrailingPrevious(offset);
802        return getHorizontal(offset, !trailing);
803    }
804
805    private float getHorizontal(int offset, boolean trailing) {
806        int line = getLineForOffset(offset);
807
808        return getHorizontal(offset, trailing, line);
809    }
810
811    private float getHorizontal(int offset, boolean trailing, int line) {
812        int start = getLineStart(line);
813        int end = getLineEnd(line);
814        int dir = getParagraphDirection(line);
815        boolean hasTabOrEmoji = getLineContainsTab(line);
816        Directions directions = getLineDirections(line);
817
818        TabStops tabStops = null;
819        if (hasTabOrEmoji && mText instanceof Spanned) {
820            // Just checking this line should be good enough, tabs should be
821            // consistent across all lines in a paragraph.
822            TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
823            if (tabs.length > 0) {
824                tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
825            }
826        }
827
828        TextLine tl = TextLine.obtain();
829        tl.set(mPaint, mText, start, end, dir, directions, hasTabOrEmoji, tabStops);
830        float wid = tl.measure(offset - start, trailing, null);
831        TextLine.recycle(tl);
832
833        int left = getParagraphLeft(line);
834        int right = getParagraphRight(line);
835
836        return getLineStartPos(line, left, right) + wid;
837    }
838
839    /**
840     * Get the leftmost position that should be exposed for horizontal
841     * scrolling on the specified line.
842     */
843    public float getLineLeft(int line) {
844        int dir = getParagraphDirection(line);
845        Alignment align = getParagraphAlignment(line);
846
847        if (align == Alignment.ALIGN_LEFT) {
848            return 0;
849        } else if (align == Alignment.ALIGN_NORMAL) {
850            if (dir == DIR_RIGHT_TO_LEFT)
851                return getParagraphRight(line) - getLineMax(line);
852            else
853                return 0;
854        } else if (align == Alignment.ALIGN_RIGHT) {
855            return mWidth - getLineMax(line);
856        } else if (align == Alignment.ALIGN_OPPOSITE) {
857            if (dir == DIR_RIGHT_TO_LEFT)
858                return 0;
859            else
860                return mWidth - getLineMax(line);
861        } else { /* align == Alignment.ALIGN_CENTER */
862            int left = getParagraphLeft(line);
863            int right = getParagraphRight(line);
864            int max = ((int) getLineMax(line)) & ~1;
865
866            return left + ((right - left) - max) / 2;
867        }
868    }
869
870    /**
871     * Get the rightmost position that should be exposed for horizontal
872     * scrolling on the specified line.
873     */
874    public float getLineRight(int line) {
875        int dir = getParagraphDirection(line);
876        Alignment align = getParagraphAlignment(line);
877
878        if (align == Alignment.ALIGN_LEFT) {
879            return getParagraphLeft(line) + getLineMax(line);
880        } else if (align == Alignment.ALIGN_NORMAL) {
881            if (dir == DIR_RIGHT_TO_LEFT)
882                return mWidth;
883            else
884                return getParagraphLeft(line) + getLineMax(line);
885        } else if (align == Alignment.ALIGN_RIGHT) {
886            return mWidth;
887        } else if (align == Alignment.ALIGN_OPPOSITE) {
888            if (dir == DIR_RIGHT_TO_LEFT)
889                return getLineMax(line);
890            else
891                return mWidth;
892        } else { /* align == Alignment.ALIGN_CENTER */
893            int left = getParagraphLeft(line);
894            int right = getParagraphRight(line);
895            int max = ((int) getLineMax(line)) & ~1;
896
897            return right - ((right - left) - max) / 2;
898        }
899    }
900
901    /**
902     * Gets the unsigned horizontal extent of the specified line, including
903     * leading margin indent, but excluding trailing whitespace.
904     */
905    public float getLineMax(int line) {
906        float margin = getParagraphLeadingMargin(line);
907        float signedExtent = getLineExtent(line, false);
908        return margin + signedExtent >= 0 ? signedExtent : -signedExtent;
909    }
910
911    /**
912     * Gets the unsigned horizontal extent of the specified line, including
913     * leading margin indent and trailing whitespace.
914     */
915    public float getLineWidth(int line) {
916        float margin = getParagraphLeadingMargin(line);
917        float signedExtent = getLineExtent(line, true);
918        return margin + signedExtent >= 0 ? signedExtent : -signedExtent;
919    }
920
921    /**
922     * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
923     * tab stops instead of using the ones passed in.
924     * @param line the index of the line
925     * @param full whether to include trailing whitespace
926     * @return the extent of the line
927     */
928    private float getLineExtent(int line, boolean full) {
929        int start = getLineStart(line);
930        int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
931
932        boolean hasTabsOrEmoji = getLineContainsTab(line);
933        TabStops tabStops = null;
934        if (hasTabsOrEmoji && mText instanceof Spanned) {
935            // Just checking this line should be good enough, tabs should be
936            // consistent across all lines in a paragraph.
937            TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
938            if (tabs.length > 0) {
939                tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
940            }
941        }
942        Directions directions = getLineDirections(line);
943        // Returned directions can actually be null
944        if (directions == null) {
945            return 0f;
946        }
947        int dir = getParagraphDirection(line);
948
949        TextLine tl = TextLine.obtain();
950        tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
951        float width = tl.metrics(null);
952        TextLine.recycle(tl);
953        return width;
954    }
955
956    /**
957     * Returns the signed horizontal extent of the specified line, excluding
958     * leading margin.  If full is false, excludes trailing whitespace.
959     * @param line the index of the line
960     * @param tabStops the tab stops, can be null if we know they're not used.
961     * @param full whether to include trailing whitespace
962     * @return the extent of the text on this line
963     */
964    private float getLineExtent(int line, TabStops tabStops, boolean full) {
965        int start = getLineStart(line);
966        int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
967        boolean hasTabsOrEmoji = getLineContainsTab(line);
968        Directions directions = getLineDirections(line);
969        int dir = getParagraphDirection(line);
970
971        TextLine tl = TextLine.obtain();
972        tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
973        float width = tl.metrics(null);
974        TextLine.recycle(tl);
975        return width;
976    }
977
978    /**
979     * Get the line number corresponding to the specified vertical position.
980     * If you ask for a position above 0, you get 0; if you ask for a position
981     * below the bottom of the text, you get the last line.
982     */
983    // FIXME: It may be faster to do a linear search for layouts without many lines.
984    public int getLineForVertical(int vertical) {
985        int high = getLineCount(), low = -1, guess;
986
987        while (high - low > 1) {
988            guess = (high + low) / 2;
989
990            if (getLineTop(guess) > vertical)
991                high = guess;
992            else
993                low = guess;
994        }
995
996        if (low < 0)
997            return 0;
998        else
999            return low;
1000    }
1001
1002    /**
1003     * Get the line number on which the specified text offset appears.
1004     * If you ask for a position before 0, you get 0; if you ask for a position
1005     * beyond the end of the text, you get the last line.
1006     */
1007    public int getLineForOffset(int offset) {
1008        int high = getLineCount(), low = -1, guess;
1009
1010        while (high - low > 1) {
1011            guess = (high + low) / 2;
1012
1013            if (getLineStart(guess) > offset)
1014                high = guess;
1015            else
1016                low = guess;
1017        }
1018
1019        if (low < 0)
1020            return 0;
1021        else
1022            return low;
1023    }
1024
1025    /**
1026     * Get the character offset on the specified line whose position is
1027     * closest to the specified horizontal position.
1028     */
1029    public int getOffsetForHorizontal(int line, float horiz) {
1030        int max = getLineEnd(line) - 1;
1031        int min = getLineStart(line);
1032        Directions dirs = getLineDirections(line);
1033
1034        if (line == getLineCount() - 1)
1035            max++;
1036
1037        int best = min;
1038        float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz);
1039
1040        for (int i = 0; i < dirs.mDirections.length; i += 2) {
1041            int here = min + dirs.mDirections[i];
1042            int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1043            int swap = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0 ? -1 : 1;
1044
1045            if (there > max)
1046                there = max;
1047            int high = there - 1 + 1, low = here + 1 - 1, guess;
1048
1049            while (high - low > 1) {
1050                guess = (high + low) / 2;
1051                int adguess = getOffsetAtStartOf(guess);
1052
1053                if (getPrimaryHorizontal(adguess) * swap >= horiz * swap)
1054                    high = guess;
1055                else
1056                    low = guess;
1057            }
1058
1059            if (low < here + 1)
1060                low = here + 1;
1061
1062            if (low < there) {
1063                low = getOffsetAtStartOf(low);
1064
1065                float dist = Math.abs(getPrimaryHorizontal(low) - horiz);
1066
1067                int aft = TextUtils.getOffsetAfter(mText, low);
1068                if (aft < there) {
1069                    float other = Math.abs(getPrimaryHorizontal(aft) - horiz);
1070
1071                    if (other < dist) {
1072                        dist = other;
1073                        low = aft;
1074                    }
1075                }
1076
1077                if (dist < bestdist) {
1078                    bestdist = dist;
1079                    best = low;
1080                }
1081            }
1082
1083            float dist = Math.abs(getPrimaryHorizontal(here) - horiz);
1084
1085            if (dist < bestdist) {
1086                bestdist = dist;
1087                best = here;
1088            }
1089        }
1090
1091        float dist = Math.abs(getPrimaryHorizontal(max) - horiz);
1092
1093        if (dist < bestdist) {
1094            bestdist = dist;
1095            best = max;
1096        }
1097
1098        return best;
1099    }
1100
1101    /**
1102     * Return the text offset after the last character on the specified line.
1103     */
1104    public final int getLineEnd(int line) {
1105        return getLineStart(line + 1);
1106    }
1107
1108    /**
1109     * Return the text offset after the last visible character (so whitespace
1110     * is not counted) on the specified line.
1111     */
1112    public int getLineVisibleEnd(int line) {
1113        return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1114    }
1115
1116    private int getLineVisibleEnd(int line, int start, int end) {
1117        CharSequence text = mText;
1118        char ch;
1119        if (line == getLineCount() - 1) {
1120            return end;
1121        }
1122
1123        for (; end > start; end--) {
1124            ch = text.charAt(end - 1);
1125
1126            if (ch == '\n') {
1127                return end - 1;
1128            }
1129
1130            if (ch != ' ' && ch != '\t') {
1131                break;
1132            }
1133
1134        }
1135
1136        return end;
1137    }
1138
1139    /**
1140     * Return the vertical position of the bottom of the specified line.
1141     */
1142    public final int getLineBottom(int line) {
1143        return getLineTop(line + 1);
1144    }
1145
1146    /**
1147     * Return the vertical position of the baseline of the specified line.
1148     */
1149    public final int getLineBaseline(int line) {
1150        // getLineTop(line+1) == getLineTop(line)
1151        return getLineTop(line+1) - getLineDescent(line);
1152    }
1153
1154    /**
1155     * Get the ascent of the text on the specified line.
1156     * The return value is negative to match the Paint.ascent() convention.
1157     */
1158    public final int getLineAscent(int line) {
1159        // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1160        return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1161    }
1162
1163    public int getOffsetToLeftOf(int offset) {
1164        return getOffsetToLeftRightOf(offset, true);
1165    }
1166
1167    public int getOffsetToRightOf(int offset) {
1168        return getOffsetToLeftRightOf(offset, false);
1169    }
1170
1171    private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1172        int line = getLineForOffset(caret);
1173        int lineStart = getLineStart(line);
1174        int lineEnd = getLineEnd(line);
1175        int lineDir = getParagraphDirection(line);
1176
1177        boolean lineChanged = false;
1178        boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
1179        // if walking off line, look at the line we're headed to
1180        if (advance) {
1181            if (caret == lineEnd) {
1182                if (line < getLineCount() - 1) {
1183                    lineChanged = true;
1184                    ++line;
1185                } else {
1186                    return caret; // at very end, don't move
1187                }
1188            }
1189        } else {
1190            if (caret == lineStart) {
1191                if (line > 0) {
1192                    lineChanged = true;
1193                    --line;
1194                } else {
1195                    return caret; // at very start, don't move
1196                }
1197            }
1198        }
1199
1200        if (lineChanged) {
1201            lineStart = getLineStart(line);
1202            lineEnd = getLineEnd(line);
1203            int newDir = getParagraphDirection(line);
1204            if (newDir != lineDir) {
1205                // unusual case.  we want to walk onto the line, but it runs
1206                // in a different direction than this one, so we fake movement
1207                // in the opposite direction.
1208                toLeft = !toLeft;
1209                lineDir = newDir;
1210            }
1211        }
1212
1213        Directions directions = getLineDirections(line);
1214
1215        TextLine tl = TextLine.obtain();
1216        // XXX: we don't care about tabs
1217        tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1218        caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1219        tl = TextLine.recycle(tl);
1220        return caret;
1221    }
1222
1223    private int getOffsetAtStartOf(int offset) {
1224        // XXX this probably should skip local reorderings and
1225        // zero-width characters, look at callers
1226        if (offset == 0)
1227            return 0;
1228
1229        CharSequence text = mText;
1230        char c = text.charAt(offset);
1231
1232        if (c >= '\uDC00' && c <= '\uDFFF') {
1233            char c1 = text.charAt(offset - 1);
1234
1235            if (c1 >= '\uD800' && c1 <= '\uDBFF')
1236                offset -= 1;
1237        }
1238
1239        if (mSpannedText) {
1240            ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1241                                                       ReplacementSpan.class);
1242
1243            for (int i = 0; i < spans.length; i++) {
1244                int start = ((Spanned) text).getSpanStart(spans[i]);
1245                int end = ((Spanned) text).getSpanEnd(spans[i]);
1246
1247                if (start < offset && end > offset)
1248                    offset = start;
1249            }
1250        }
1251
1252        return offset;
1253    }
1254
1255    /**
1256     * Fills in the specified Path with a representation of a cursor
1257     * at the specified offset.  This will often be a vertical line
1258     * but can be multiple discontinuous lines in text with multiple
1259     * directionalities.
1260     */
1261    public void getCursorPath(int point, Path dest,
1262                              CharSequence editingBuffer) {
1263        dest.reset();
1264
1265        int line = getLineForOffset(point);
1266        int top = getLineTop(line);
1267        int bottom = getLineTop(line+1);
1268
1269        float h1 = getPrimaryHorizontal(point) - 0.5f;
1270        float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point) - 0.5f : h1;
1271
1272        int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1273                   TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1274        int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
1275        int dist = 0;
1276
1277        if (caps != 0 || fn != 0) {
1278            dist = (bottom - top) >> 2;
1279
1280            if (fn != 0)
1281                top += dist;
1282            if (caps != 0)
1283                bottom -= dist;
1284        }
1285
1286        if (h1 < 0.5f)
1287            h1 = 0.5f;
1288        if (h2 < 0.5f)
1289            h2 = 0.5f;
1290
1291        if (Float.compare(h1, h2) == 0) {
1292            dest.moveTo(h1, top);
1293            dest.lineTo(h1, bottom);
1294        } else {
1295            dest.moveTo(h1, top);
1296            dest.lineTo(h1, (top + bottom) >> 1);
1297
1298            dest.moveTo(h2, (top + bottom) >> 1);
1299            dest.lineTo(h2, bottom);
1300        }
1301
1302        if (caps == 2) {
1303            dest.moveTo(h2, bottom);
1304            dest.lineTo(h2 - dist, bottom + dist);
1305            dest.lineTo(h2, bottom);
1306            dest.lineTo(h2 + dist, bottom + dist);
1307        } else if (caps == 1) {
1308            dest.moveTo(h2, bottom);
1309            dest.lineTo(h2 - dist, bottom + dist);
1310
1311            dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1312            dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1313
1314            dest.moveTo(h2 + dist, bottom + dist);
1315            dest.lineTo(h2, bottom);
1316        }
1317
1318        if (fn == 2) {
1319            dest.moveTo(h1, top);
1320            dest.lineTo(h1 - dist, top - dist);
1321            dest.lineTo(h1, top);
1322            dest.lineTo(h1 + dist, top - dist);
1323        } else if (fn == 1) {
1324            dest.moveTo(h1, top);
1325            dest.lineTo(h1 - dist, top - dist);
1326
1327            dest.moveTo(h1 - dist, top - dist + 0.5f);
1328            dest.lineTo(h1 + dist, top - dist + 0.5f);
1329
1330            dest.moveTo(h1 + dist, top - dist);
1331            dest.lineTo(h1, top);
1332        }
1333    }
1334
1335    private void addSelection(int line, int start, int end,
1336                              int top, int bottom, Path dest) {
1337        int linestart = getLineStart(line);
1338        int lineend = getLineEnd(line);
1339        Directions dirs = getLineDirections(line);
1340
1341        if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1342            lineend--;
1343
1344        for (int i = 0; i < dirs.mDirections.length; i += 2) {
1345            int here = linestart + dirs.mDirections[i];
1346            int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1347
1348            if (there > lineend)
1349                there = lineend;
1350
1351            if (start <= there && end >= here) {
1352                int st = Math.max(start, here);
1353                int en = Math.min(end, there);
1354
1355                if (st != en) {
1356                    float h1 = getHorizontal(st, false, line);
1357                    float h2 = getHorizontal(en, true, line);
1358
1359                    float left = Math.min(h1, h2);
1360                    float right = Math.max(h1, h2);
1361
1362                    dest.addRect(left, top, right, bottom, Path.Direction.CW);
1363                }
1364            }
1365        }
1366    }
1367
1368    /**
1369     * Fills in the specified Path with a representation of a highlight
1370     * between the specified offsets.  This will often be a rectangle
1371     * or a potentially discontinuous set of rectangles.  If the start
1372     * and end are the same, the returned path is empty.
1373     */
1374    public void getSelectionPath(int start, int end, Path dest) {
1375        dest.reset();
1376
1377        if (start == end)
1378            return;
1379
1380        if (end < start) {
1381            int temp = end;
1382            end = start;
1383            start = temp;
1384        }
1385
1386        int startline = getLineForOffset(start);
1387        int endline = getLineForOffset(end);
1388
1389        int top = getLineTop(startline);
1390        int bottom = getLineBottom(endline);
1391
1392        if (startline == endline) {
1393            addSelection(startline, start, end, top, bottom, dest);
1394        } else {
1395            final float width = mWidth;
1396
1397            addSelection(startline, start, getLineEnd(startline),
1398                         top, getLineBottom(startline), dest);
1399
1400            if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1401                dest.addRect(getLineLeft(startline), top,
1402                              0, getLineBottom(startline), Path.Direction.CW);
1403            else
1404                dest.addRect(getLineRight(startline), top,
1405                              width, getLineBottom(startline), Path.Direction.CW);
1406
1407            for (int i = startline + 1; i < endline; i++) {
1408                top = getLineTop(i);
1409                bottom = getLineBottom(i);
1410                dest.addRect(0, top, width, bottom, Path.Direction.CW);
1411            }
1412
1413            top = getLineTop(endline);
1414            bottom = getLineBottom(endline);
1415
1416            addSelection(endline, getLineStart(endline), end,
1417                         top, bottom, dest);
1418
1419            if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1420                dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1421            else
1422                dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1423        }
1424    }
1425
1426    /**
1427     * Get the alignment of the specified paragraph, taking into account
1428     * markup attached to it.
1429     */
1430    public final Alignment getParagraphAlignment(int line) {
1431        Alignment align = mAlignment;
1432
1433        if (mSpannedText) {
1434            Spanned sp = (Spanned) mText;
1435            AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
1436                                                getLineEnd(line),
1437                                                AlignmentSpan.class);
1438
1439            int spanLength = spans.length;
1440            if (spanLength > 0) {
1441                align = spans[spanLength-1].getAlignment();
1442            }
1443        }
1444
1445        return align;
1446    }
1447
1448    /**
1449     * Get the left edge of the specified paragraph, inset by left margins.
1450     */
1451    public final int getParagraphLeft(int line) {
1452        int left = 0;
1453        int dir = getParagraphDirection(line);
1454        if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1455            return left; // leading margin has no impact, or no styles
1456        }
1457        return getParagraphLeadingMargin(line);
1458    }
1459
1460    /**
1461     * Get the right edge of the specified paragraph, inset by right margins.
1462     */
1463    public final int getParagraphRight(int line) {
1464        int right = mWidth;
1465        int dir = getParagraphDirection(line);
1466        if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1467            return right; // leading margin has no impact, or no styles
1468        }
1469        return right - getParagraphLeadingMargin(line);
1470    }
1471
1472    /**
1473     * Returns the effective leading margin (unsigned) for this line,
1474     * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1475     * @param line the line index
1476     * @return the leading margin of this line
1477     */
1478    private int getParagraphLeadingMargin(int line) {
1479        if (!mSpannedText) {
1480            return 0;
1481        }
1482        Spanned spanned = (Spanned) mText;
1483
1484        int lineStart = getLineStart(line);
1485        int lineEnd = getLineEnd(line);
1486        int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
1487                LeadingMarginSpan.class);
1488        LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
1489                                                LeadingMarginSpan.class);
1490        if (spans.length == 0) {
1491            return 0; // no leading margin span;
1492        }
1493
1494        int margin = 0;
1495
1496        boolean isFirstParaLine = lineStart == 0 ||
1497            spanned.charAt(lineStart - 1) == '\n';
1498
1499        for (int i = 0; i < spans.length; i++) {
1500            LeadingMarginSpan span = spans[i];
1501            boolean useFirstLineMargin = isFirstParaLine;
1502            if (span instanceof LeadingMarginSpan2) {
1503                int spStart = spanned.getSpanStart(span);
1504                int spanLine = getLineForOffset(spStart);
1505                int count = ((LeadingMarginSpan2)span).getLeadingMarginLineCount();
1506                useFirstLineMargin = line < spanLine + count;
1507            }
1508            margin += span.getLeadingMargin(useFirstLineMargin);
1509        }
1510
1511        return margin;
1512    }
1513
1514    /* package */
1515    static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
1516
1517        MeasuredText mt = MeasuredText.obtain();
1518        TextLine tl = TextLine.obtain();
1519        try {
1520            mt.setPara(text, start, end, TextDirectionHeuristics.LTR);
1521            Directions directions;
1522            int dir;
1523            if (mt.mEasy) {
1524                directions = DIRS_ALL_LEFT_TO_RIGHT;
1525                dir = Layout.DIR_LEFT_TO_RIGHT;
1526            } else {
1527                directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1528                    0, mt.mChars, 0, mt.mLen);
1529                dir = mt.mDir;
1530            }
1531            char[] chars = mt.mChars;
1532            int len = mt.mLen;
1533            boolean hasTabs = false;
1534            TabStops tabStops = null;
1535            for (int i = 0; i < len; ++i) {
1536                if (chars[i] == '\t') {
1537                    hasTabs = true;
1538                    if (text instanceof Spanned) {
1539                        Spanned spanned = (Spanned) text;
1540                        int spanEnd = spanned.nextSpanTransition(start, end,
1541                                TabStopSpan.class);
1542                        TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
1543                                TabStopSpan.class);
1544                        if (spans.length > 0) {
1545                            tabStops = new TabStops(TAB_INCREMENT, spans);
1546                        }
1547                    }
1548                    break;
1549                }
1550            }
1551            tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
1552            return tl.metrics(null);
1553        } finally {
1554            TextLine.recycle(tl);
1555            MeasuredText.recycle(mt);
1556        }
1557    }
1558
1559    /**
1560     * @hide
1561     */
1562    /* package */ static class TabStops {
1563        private int[] mStops;
1564        private int mNumStops;
1565        private int mIncrement;
1566
1567        TabStops(int increment, Object[] spans) {
1568            reset(increment, spans);
1569        }
1570
1571        void reset(int increment, Object[] spans) {
1572            this.mIncrement = increment;
1573
1574            int ns = 0;
1575            if (spans != null) {
1576                int[] stops = this.mStops;
1577                for (Object o : spans) {
1578                    if (o instanceof TabStopSpan) {
1579                        if (stops == null) {
1580                            stops = new int[10];
1581                        } else if (ns == stops.length) {
1582                            int[] nstops = new int[ns * 2];
1583                            for (int i = 0; i < ns; ++i) {
1584                                nstops[i] = stops[i];
1585                            }
1586                            stops = nstops;
1587                        }
1588                        stops[ns++] = ((TabStopSpan) o).getTabStop();
1589                    }
1590                }
1591                if (ns > 1) {
1592                    Arrays.sort(stops, 0, ns);
1593                }
1594                if (stops != this.mStops) {
1595                    this.mStops = stops;
1596                }
1597            }
1598            this.mNumStops = ns;
1599        }
1600
1601        float nextTab(float h) {
1602            int ns = this.mNumStops;
1603            if (ns > 0) {
1604                int[] stops = this.mStops;
1605                for (int i = 0; i < ns; ++i) {
1606                    int stop = stops[i];
1607                    if (stop > h) {
1608                        return stop;
1609                    }
1610                }
1611            }
1612            return nextDefaultStop(h, mIncrement);
1613        }
1614
1615        public static float nextDefaultStop(float h, int inc) {
1616            return ((int) ((h + inc) / inc)) * inc;
1617        }
1618    }
1619
1620    /**
1621     * Returns the position of the next tab stop after h on the line.
1622     *
1623     * @param text the text
1624     * @param start start of the line
1625     * @param end limit of the line
1626     * @param h the current horizontal offset
1627     * @param tabs the tabs, can be null.  If it is null, any tabs in effect
1628     * on the line will be used.  If there are no tabs, a default offset
1629     * will be used to compute the tab stop.
1630     * @return the offset of the next tab stop.
1631     */
1632    /* package */ static float nextTab(CharSequence text, int start, int end,
1633                                       float h, Object[] tabs) {
1634        float nh = Float.MAX_VALUE;
1635        boolean alltabs = false;
1636
1637        if (text instanceof Spanned) {
1638            if (tabs == null) {
1639                tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
1640                alltabs = true;
1641            }
1642
1643            for (int i = 0; i < tabs.length; i++) {
1644                if (!alltabs) {
1645                    if (!(tabs[i] instanceof TabStopSpan))
1646                        continue;
1647                }
1648
1649                int where = ((TabStopSpan) tabs[i]).getTabStop();
1650
1651                if (where < nh && where > h)
1652                    nh = where;
1653            }
1654
1655            if (nh != Float.MAX_VALUE)
1656                return nh;
1657        }
1658
1659        return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1660    }
1661
1662    protected final boolean isSpanned() {
1663        return mSpannedText;
1664    }
1665
1666    /**
1667     * Returns the same as <code>text.getSpans()</code>, except where
1668     * <code>start</code> and <code>end</code> are the same and are not
1669     * at the very beginning of the text, in which case an empty array
1670     * is returned instead.
1671     * <p>
1672     * This is needed because of the special case that <code>getSpans()</code>
1673     * on an empty range returns the spans adjacent to that range, which is
1674     * primarily for the sake of <code>TextWatchers</code> so they will get
1675     * notifications when text goes from empty to non-empty.  But it also
1676     * has the unfortunate side effect that if the text ends with an empty
1677     * paragraph, that paragraph accidentally picks up the styles of the
1678     * preceding paragraph (even though those styles will not be picked up
1679     * by new text that is inserted into the empty paragraph).
1680     * <p>
1681     * The reason it just checks whether <code>start</code> and <code>end</code>
1682     * is the same is that the only time a line can contain 0 characters
1683     * is if it is the final paragraph of the Layout; otherwise any line will
1684     * contain at least one printing or newline character.  The reason for the
1685     * additional check if <code>start</code> is greater than 0 is that
1686     * if the empty paragraph is the entire content of the buffer, paragraph
1687     * styles that are already applied to the buffer will apply to text that
1688     * is inserted into it.
1689     */
1690    /* package */ static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
1691        if (start == end && start > 0) {
1692            return ArrayUtils.emptyArray(type);
1693        }
1694
1695        return text.getSpans(start, end, type);
1696    }
1697
1698    private void ellipsize(int start, int end, int line,
1699                           char[] dest, int destoff) {
1700        int ellipsisCount = getEllipsisCount(line);
1701
1702        if (ellipsisCount == 0) {
1703            return;
1704        }
1705
1706        int ellipsisStart = getEllipsisStart(line);
1707        int linestart = getLineStart(line);
1708
1709        for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1710            char c;
1711
1712            if (i == ellipsisStart) {
1713                c = '\u2026'; // ellipsis
1714            } else {
1715                c = '\uFEFF'; // 0-width space
1716            }
1717
1718            int a = i + linestart;
1719
1720            if (a >= start && a < end) {
1721                dest[destoff + a - start] = c;
1722            }
1723        }
1724    }
1725
1726    /**
1727     * Stores information about bidirectional (left-to-right or right-to-left)
1728     * text within the layout of a line.
1729     */
1730    public static class Directions {
1731        // Directions represents directional runs within a line of text.
1732        // Runs are pairs of ints listed in visual order, starting from the
1733        // leading margin.  The first int of each pair is the offset from
1734        // the first character of the line to the start of the run.  The
1735        // second int represents both the length and level of the run.
1736        // The length is in the lower bits, accessed by masking with
1737        // DIR_LENGTH_MASK.  The level is in the higher bits, accessed
1738        // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1739        // To simply test for an RTL direction, test the bit using
1740        // DIR_RTL_FLAG, if set then the direction is rtl.
1741
1742        /* package */ int[] mDirections;
1743        /* package */ Directions(int[] dirs) {
1744            mDirections = dirs;
1745        }
1746    }
1747
1748    /**
1749     * Return the offset of the first character to be ellipsized away,
1750     * relative to the start of the line.  (So 0 if the beginning of the
1751     * line is ellipsized, not getLineStart().)
1752     */
1753    public abstract int getEllipsisStart(int line);
1754
1755    /**
1756     * Returns the number of characters to be ellipsized away, or 0 if
1757     * no ellipsis is to take place.
1758     */
1759    public abstract int getEllipsisCount(int line);
1760
1761    /* package */ static class Ellipsizer implements CharSequence, GetChars {
1762        /* package */ CharSequence mText;
1763        /* package */ Layout mLayout;
1764        /* package */ int mWidth;
1765        /* package */ TextUtils.TruncateAt mMethod;
1766
1767        public Ellipsizer(CharSequence s) {
1768            mText = s;
1769        }
1770
1771        public char charAt(int off) {
1772            char[] buf = TextUtils.obtain(1);
1773            getChars(off, off + 1, buf, 0);
1774            char ret = buf[0];
1775
1776            TextUtils.recycle(buf);
1777            return ret;
1778        }
1779
1780        public void getChars(int start, int end, char[] dest, int destoff) {
1781            int line1 = mLayout.getLineForOffset(start);
1782            int line2 = mLayout.getLineForOffset(end);
1783
1784            TextUtils.getChars(mText, start, end, dest, destoff);
1785
1786            for (int i = line1; i <= line2; i++) {
1787                mLayout.ellipsize(start, end, i, dest, destoff);
1788            }
1789        }
1790
1791        public int length() {
1792            return mText.length();
1793        }
1794
1795        public CharSequence subSequence(int start, int end) {
1796            char[] s = new char[end - start];
1797            getChars(start, end, s, 0);
1798            return new String(s);
1799        }
1800
1801        @Override
1802        public String toString() {
1803            char[] s = new char[length()];
1804            getChars(0, length(), s, 0);
1805            return new String(s);
1806        }
1807
1808    }
1809
1810    /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
1811        private Spanned mSpanned;
1812
1813        public SpannedEllipsizer(CharSequence display) {
1814            super(display);
1815            mSpanned = (Spanned) display;
1816        }
1817
1818        public <T> T[] getSpans(int start, int end, Class<T> type) {
1819            return mSpanned.getSpans(start, end, type);
1820        }
1821
1822        public int getSpanStart(Object tag) {
1823            return mSpanned.getSpanStart(tag);
1824        }
1825
1826        public int getSpanEnd(Object tag) {
1827            return mSpanned.getSpanEnd(tag);
1828        }
1829
1830        public int getSpanFlags(Object tag) {
1831            return mSpanned.getSpanFlags(tag);
1832        }
1833
1834        @SuppressWarnings("rawtypes")
1835        public int nextSpanTransition(int start, int limit, Class type) {
1836            return mSpanned.nextSpanTransition(start, limit, type);
1837        }
1838
1839        @Override
1840        public CharSequence subSequence(int start, int end) {
1841            char[] s = new char[end - start];
1842            getChars(start, end, s, 0);
1843
1844            SpannableString ss = new SpannableString(new String(s));
1845            TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
1846            return ss;
1847        }
1848    }
1849
1850    private CharSequence mText;
1851    private TextPaint mPaint;
1852    /* package */ TextPaint mWorkPaint;
1853    private int mWidth;
1854    private Alignment mAlignment = Alignment.ALIGN_NORMAL;
1855    private float mSpacingMult;
1856    private float mSpacingAdd;
1857    private static final Rect sTempRect = new Rect();
1858    private boolean mSpannedText;
1859    private TextDirectionHeuristic mTextDir;
1860    private SpanSet<LineBackgroundSpan> lineBackgroundSpans;
1861
1862    public static final int DIR_LEFT_TO_RIGHT = 1;
1863    public static final int DIR_RIGHT_TO_LEFT = -1;
1864
1865    /* package */ static final int DIR_REQUEST_LTR = 1;
1866    /* package */ static final int DIR_REQUEST_RTL = -1;
1867    /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
1868    /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
1869
1870    /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
1871    /* package */ static final int RUN_LEVEL_SHIFT = 26;
1872    /* package */ static final int RUN_LEVEL_MASK = 0x3f;
1873    /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
1874
1875    public enum Alignment {
1876        ALIGN_NORMAL,
1877        ALIGN_OPPOSITE,
1878        ALIGN_CENTER,
1879        /** @hide */
1880        ALIGN_LEFT,
1881        /** @hide */
1882        ALIGN_RIGHT,
1883    }
1884
1885    private static final int TAB_INCREMENT = 20;
1886
1887    /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
1888        new Directions(new int[] { 0, RUN_LENGTH_MASK });
1889    /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
1890        new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
1891
1892}
1893