blob: 16ae5e238e0e40aa4873dcea4d2103ec03a94761 [file] [log] [blame]
Gilles Debunne60e3b3f2012-03-13 11:26:05 -07001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002 * 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
Raph Levien39b4db72015-03-25 13:18:20 -070019import android.annotation.IntDef;
The Android Open Source Project10592532009-03-18 17:39:46 -070020import android.emoji.EmojiFactory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.graphics.Canvas;
22import android.graphics.Paint;
Doug Felt9f7a4442010-03-01 12:45:56 -080023import android.graphics.Path;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.text.method.TextKeyListener;
Doug Felt9f7a4442010-03-01 12:45:56 -080026import android.text.style.AlignmentSpan;
27import android.text.style.LeadingMarginSpan;
Gilles Debunne162bf0f2010-11-16 16:23:53 -080028import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
Doug Felt9f7a4442010-03-01 12:45:56 -080029import android.text.style.LineBackgroundSpan;
30import android.text.style.ParagraphStyle;
31import android.text.style.ReplacementSpan;
32import android.text.style.TabStopSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Doug Feltcb3791202011-07-07 11:57:48 -070034import com.android.internal.util.ArrayUtils;
Adam Lesinski776abc22014-03-07 11:30:59 -050035import com.android.internal.util.GrowingArrayUtils;
Doug Feltcb3791202011-07-07 11:57:48 -070036
Raph Levien39b4db72015-03-25 13:18:20 -070037import java.lang.annotation.Retention;
38import java.lang.annotation.RetentionPolicy;
Doug Feltc982f602010-05-25 11:51:40 -070039import java.util.Arrays;
Doug Felt9f7a4442010-03-01 12:45:56 -080040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/**
Doug Felt9f7a4442010-03-01 12:45:56 -080042 * A base class that manages text layout in visual elements on
43 * the screen.
44 * <p>For text that will be edited, use a {@link DynamicLayout},
45 * which will be updated as the text changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 * For text that will not change, use a {@link StaticLayout}.
47 */
48public abstract class Layout {
Raph Levien39b4db72015-03-25 13:18:20 -070049 /** @hide */
50 @IntDef({BREAK_STRATEGY_SIMPLE, BREAK_STRATEGY_HIGH_QUALITY, BREAK_STRATEGY_BALANCED})
51 @Retention(RetentionPolicy.SOURCE)
52 public @interface BreakStrategy {}
53
54 /**
55 * Value for break strategy indicating simple line breaking. Automatic hyphens are not added
56 * (though soft hyphens are respected), and modifying text generally doesn't affect the layout
57 * before it (which yields a more consistent user experience when editing), but layout may not
58 * be the highest quality.
59 */
60 public static final int BREAK_STRATEGY_SIMPLE = 0;
61
62 /**
63 * Value for break strategy indicating high quality line breaking, including automatic
64 * hyphenation and doing whole-paragraph optimization of line breaks.
65 */
66 public static final int BREAK_STRATEGY_HIGH_QUALITY = 1;
67
68 /**
69 * Value for break strategy indicating balanced line breaking. The breaks are chosen to
70 * make all lines as close to the same length as possible, including automatic hyphenation.
71 */
72 public static final int BREAK_STRATEGY_BALANCED = 2;
73
Doug Felt71b8dd72010-02-16 17:27:09 -080074 private static final ParagraphStyle[] NO_PARA_SPANS =
75 ArrayUtils.emptyArray(ParagraphStyle.class);
Dave Bort76c02262009-04-13 17:17:21 -070076
Gilles Debunneeca5b732012-04-25 18:48:42 -070077 /* package */ static final EmojiFactory EMOJI_FACTORY = EmojiFactory.newAvailableInstance();
The Android Open Source Project10592532009-03-18 17:39:46 -070078 /* package */ static final int MIN_EMOJI, MAX_EMOJI;
79
80 static {
81 if (EMOJI_FACTORY != null) {
82 MIN_EMOJI = EMOJI_FACTORY.getMinimumAndroidPua();
83 MAX_EMOJI = EMOJI_FACTORY.getMaximumAndroidPua();
84 } else {
85 MIN_EMOJI = -1;
86 MAX_EMOJI = -1;
87 }
Doug Felte8e45f22010-03-29 14:58:40 -070088 }
Eric Fischerc2d54f42009-03-27 15:52:38 -070089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /**
Doug Felt71b8dd72010-02-16 17:27:09 -080091 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * specified text with one line per paragraph.
93 */
94 public static float getDesiredWidth(CharSequence source,
95 TextPaint paint) {
96 return getDesiredWidth(source, 0, source.length(), paint);
97 }
Doug Felt9f7a4442010-03-01 12:45:56 -080098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800100 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 * specified text slice with one line per paragraph.
102 */
103 public static float getDesiredWidth(CharSequence source,
104 int start, int end,
105 TextPaint paint) {
106 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 int next;
109 for (int i = start; i <= end; i = next) {
110 next = TextUtils.indexOf(source, '\n', i, end);
111
112 if (next < 0)
113 next = end;
114
Doug Felt71b8dd72010-02-16 17:27:09 -0800115 // note, omits trailing paragraph char
Gilles Debunne6c488de2012-03-01 16:20:35 -0800116 float w = measurePara(paint, source, i, next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
118 if (w > need)
119 need = w;
120
121 next++;
122 }
123
124 return need;
125 }
126
127 /**
128 * Subclasses of Layout use this constructor to set the display text,
129 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800130 * @param text the text to render
131 * @param paint the default paint for the layout. Styles can override
132 * various attributes of the paint.
133 * @param width the wrapping width for the text.
134 * @param align whether to left, right, or center the text. Styles can
135 * override the alignment.
136 * @param spacingMult factor by which to scale the font size to get the
137 * default line spacing
138 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 */
140 protected Layout(CharSequence text, TextPaint paint,
141 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800142 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700143 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
144 spacingMult, spacingAdd);
145 }
146
147 /**
148 * Subclasses of Layout use this constructor to set the display text,
149 * width, and other standard properties.
150 * @param text the text to render
151 * @param paint the default paint for the layout. Styles can override
152 * various attributes of the paint.
153 * @param width the wrapping width for the text.
154 * @param align whether to left, right, or center the text. Styles can
155 * override the alignment.
156 * @param spacingMult factor by which to scale the font size to get the
157 * default line spacing
158 * @param spacingAdd amount to add to the default line spacing
159 *
160 * @hide
161 */
162 protected Layout(CharSequence text, TextPaint paint,
163 int width, Alignment align, TextDirectionHeuristic textDir,
164 float spacingMult, float spacingAdd) {
165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 if (width < 0)
167 throw new IllegalArgumentException("Layout: " + width + " < 0");
168
Doug Felte8e45f22010-03-29 14:58:40 -0700169 // Ensure paint doesn't have baselineShift set.
170 // While normally we don't modify the paint the user passed in,
171 // we were already doing this in Styled.drawUniformRun with both
172 // baselineShift and bgColor. We probably should reevaluate bgColor.
173 if (paint != null) {
174 paint.bgColor = 0;
175 paint.baselineShift = 0;
176 }
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 mText = text;
179 mPaint = paint;
180 mWorkPaint = new TextPaint();
181 mWidth = width;
182 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800183 mSpacingMult = spacingMult;
184 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700186 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
188
189 /**
190 * Replace constructor properties of this Layout with new ones. Be careful.
191 */
192 /* package */ void replaceWith(CharSequence text, TextPaint paint,
193 int width, Alignment align,
194 float spacingmult, float spacingadd) {
195 if (width < 0) {
196 throw new IllegalArgumentException("Layout: " + width + " < 0");
197 }
198
199 mText = text;
200 mPaint = paint;
201 mWidth = width;
202 mAlignment = align;
203 mSpacingMult = spacingmult;
204 mSpacingAdd = spacingadd;
205 mSpannedText = text instanceof Spanned;
206 }
207
208 /**
209 * Draw this Layout on the specified Canvas.
210 */
211 public void draw(Canvas c) {
212 draw(c, null, null, 0);
213 }
214
215 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800216 * Draw this Layout on the specified canvas, with the highlight path drawn
217 * between the background and the text.
218 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800219 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800220 * @param highlight the path of the highlight or cursor; can be null
221 * @param highlightPaint the paint for the highlight
222 * @param cursorOffsetVertical the amount to temporarily translate the
223 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800225 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
226 int cursorOffsetVertical) {
227 final long lineRange = getLineRangeForDraw(canvas);
228 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
229 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
230 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
Gilles Debunne6c488de2012-03-01 16:20:35 -0800232 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
233 firstLine, lastLine);
234 drawText(canvas, firstLine, lastLine);
235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Gilles Debunne6c488de2012-03-01 16:20:35 -0800237 /**
238 * @hide
239 */
240 public void drawText(Canvas canvas, int firstLine, int lastLine) {
241 int previousLineBottom = getLineTop(firstLine);
242 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800243 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700244 int spanEnd = 0;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800245 TextPaint paint = mPaint;
246 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700248 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700249 TabStops tabStops = null;
250 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800251
Doug Felte8e45f22010-03-29 14:58:40 -0700252 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700253
Gilles Debunne6c488de2012-03-01 16:20:35 -0800254 // Draw the lines, one at a time.
255 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700256 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700258 previousLineEnd = getLineStart(lineNum + 1);
259 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
261 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700262 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700264 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265
Raph Levien26d443a2015-03-30 14:18:32 -0700266 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700267 int left = 0;
268 int right = mWidth;
269
Gilles Debunne6c488de2012-03-01 16:20:35 -0800270 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700271 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800272 int textLength = buf.length();
273 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700274
Doug Feltc982f602010-05-25 11:51:40 -0700275 // New batch of paragraph styles, collect into spans array.
276 // Compute the alignment, last alignment style wins.
277 // Reset tabStops, we'll rebuild if we encounter a line with
278 // tabs.
279 // We expect paragraph spans to be relatively infrequent, use
280 // spanEnd so that we can check less frequently. Since
281 // paragraph styles ought to apply to entire paragraphs, we can
282 // just collect the ones present at the start of the paragraph.
283 // If spanEnd is before the end of the paragraph, that's not
284 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700285 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700286 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700288 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800289
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700290 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800291 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700293 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 break;
295 }
296 }
Doug Felt0c702b82010-05-14 10:55:42 -0700297
Doug Feltc982f602010-05-25 11:51:40 -0700298 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800300
Doug Feltc982f602010-05-25 11:51:40 -0700301 // Draw all leading margin spans. Adjust left or right according
302 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700304 boolean useFirstLineMargin = isFirstParaLine;
305 for (int n = 0; n < length; n++) {
306 if (spans[n] instanceof LeadingMarginSpan2) {
307 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
308 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
309 // if there is more than one LeadingMarginSpan2, use
310 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700311 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700312 useFirstLineMargin = true;
313 break;
314 }
315 }
316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 for (int n = 0; n < length; n++) {
318 if (spans[n] instanceof LeadingMarginSpan) {
319 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800321 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800323 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700324 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800326 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800328 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700329 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 }
331 }
332 }
333 }
334
Raph Levien26d443a2015-03-30 14:18:32 -0700335 boolean hasTabOrEmoji = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700336 // Can't tell if we have tabs for sure, currently
337 if (hasTabOrEmoji && !tabStopsIsInitialized) {
338 if (tabStops == null) {
339 tabStops = new TabStops(TAB_INCREMENT, spans);
340 } else {
341 tabStops.reset(TAB_INCREMENT, spans);
342 }
343 tabStopsIsInitialized = true;
344 }
345
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700346 // Determine whether the line aligns to normal, opposite, or center.
347 Alignment align = paraAlign;
348 if (align == Alignment.ALIGN_LEFT) {
349 align = (dir == DIR_LEFT_TO_RIGHT) ?
350 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
351 } else if (align == Alignment.ALIGN_RIGHT) {
352 align = (dir == DIR_LEFT_TO_RIGHT) ?
353 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
354 }
355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 int x;
357 if (align == Alignment.ALIGN_NORMAL) {
358 if (dir == DIR_LEFT_TO_RIGHT) {
359 x = left;
360 } else {
361 x = right;
362 }
363 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700364 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700366 if (dir == DIR_LEFT_TO_RIGHT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 x = right - max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700369 x = left - max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
Doug Feltc982f602010-05-25 11:51:40 -0700371 } else { // Alignment.ALIGN_CENTER
372 max = max & ~1;
373 x = (right + left - max) >> 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
375 }
376
Raph Levien26d443a2015-03-30 14:18:32 -0700377 paint.setHyphenEdit(getHyphen(lineNum));
378 Directions directions = getLineDirections(lineNum);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800379 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTabOrEmoji) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800380 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800381 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700383 tl.set(paint, buf, start, end, dir, directions, hasTabOrEmoji, tabStops);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800384 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 }
386 }
Doug Feltc982f602010-05-25 11:51:40 -0700387
Doug Felte8e45f22010-03-29 14:58:40 -0700388 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 }
390
391 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800392 * @hide
393 */
394 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
395 int cursorOffsetVertical, int firstLine, int lastLine) {
396 // First, draw LineBackgroundSpans.
397 // LineBackgroundSpans know nothing about the alignment, margins, or
398 // direction of the layout or line. XXX: Should they?
399 // They are evaluated at each line.
400 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700401 if (mLineBackgroundSpans == null) {
402 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700403 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800404
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700405 Spanned buffer = (Spanned) mText;
406 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700407 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800408
Gilles Debunneeca5b732012-04-25 18:48:42 -0700409 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700410 int previousLineBottom = getLineTop(firstLine);
411 int previousLineEnd = getLineStart(firstLine);
412 ParagraphStyle[] spans = NO_PARA_SPANS;
413 int spansLength = 0;
414 TextPaint paint = mPaint;
415 int spanEnd = 0;
416 final int width = mWidth;
417 for (int i = firstLine; i <= lastLine; i++) {
418 int start = previousLineEnd;
419 int end = getLineStart(i + 1);
420 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800421
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700422 int ltop = previousLineBottom;
423 int lbottom = getLineTop(i + 1);
424 previousLineBottom = lbottom;
425 int lbaseline = lbottom - getLineDescent(i);
426
427 if (start >= spanEnd) {
428 // These should be infrequent, so we'll use this so that
429 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700430 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700431 // All LineBackgroundSpans on a line contribute to its background.
432 spansLength = 0;
433 // Duplication of the logic of getParagraphSpans
434 if (start != end || start == 0) {
435 // Equivalent to a getSpans(start, end), but filling the 'spans' local
436 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700437 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
438 // equal test is valid since both intervals are not empty by
439 // construction
440 if (mLineBackgroundSpans.spanStarts[j] >= end ||
441 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500442 spans = GrowingArrayUtils.append(
443 spans, spansLength, mLineBackgroundSpans.spans[j]);
444 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700445 }
446 }
447 }
448
449 for (int n = 0; n < spansLength; n++) {
450 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
451 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
452 ltop, lbaseline, lbottom,
453 buffer, start, end, i);
454 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800455 }
456 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700457 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800458 }
459
460 // There can be a highlight even without spans if we are drawing
461 // a non-spanned transformation of a spanned editing buffer.
462 if (highlight != null) {
463 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
464 canvas.drawPath(highlight, highlightPaint);
465 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
466 }
467 }
468
469 /**
470 * @param canvas
471 * @return The range of lines that need to be drawn, possibly empty.
472 * @hide
473 */
474 public long getLineRangeForDraw(Canvas canvas) {
475 int dtop, dbottom;
476
477 synchronized (sTempRect) {
478 if (!canvas.getClipBounds(sTempRect)) {
479 // Negative range end used as a special flag
480 return TextUtils.packRangeInLong(0, -1);
481 }
482
483 dtop = sTempRect.top;
484 dbottom = sTempRect.bottom;
485 }
486
487 final int top = Math.max(dtop, 0);
488 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
489
Gilles Debunne2fba3382012-06-11 17:46:24 -0700490 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800491 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
492 }
493
494 /**
Doug Feltc982f602010-05-25 11:51:40 -0700495 * Return the start position of the line, given the left and right bounds
496 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700497 *
Doug Feltc982f602010-05-25 11:51:40 -0700498 * @param line the line index
499 * @param left the left bounds (0, or leading margin if ltr para)
500 * @param right the right bounds (width, minus leading margin if rtl para)
501 * @return the start position of the line (to right of line if rtl para)
502 */
503 private int getLineStartPos(int line, int left, int right) {
504 // Adjust the point at which to start rendering depending on the
505 // alignment of the paragraph.
506 Alignment align = getParagraphAlignment(line);
507 int dir = getParagraphDirection(line);
508
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700509 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700510 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
511 } else if (align == Alignment.ALIGN_RIGHT) {
512 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
513 }
514
515 int x;
516 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700517 if (dir == DIR_LEFT_TO_RIGHT) {
518 x = left;
519 } else {
520 x = right;
521 }
522 } else {
523 TabStops tabStops = null;
524 if (mSpannedText && getLineContainsTab(line)) {
525 Spanned spanned = (Spanned) mText;
526 int start = getLineStart(line);
527 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
528 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800529 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
530 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700531 if (tabSpans.length > 0) {
532 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
533 }
534 }
535 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700536 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700537 if (dir == DIR_LEFT_TO_RIGHT) {
538 x = right - max;
539 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700540 // max is negative here
Doug Feltc982f602010-05-25 11:51:40 -0700541 x = left - max;
542 }
543 } else { // Alignment.ALIGN_CENTER
544 max = max & ~1;
545 x = (left + right - max) >> 1;
546 }
547 }
548 return x;
549 }
550
551 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 * Return the text that is displayed by this Layout.
553 */
554 public final CharSequence getText() {
555 return mText;
556 }
557
558 /**
559 * Return the base Paint properties for this layout.
560 * Do NOT change the paint, which may result in funny
561 * drawing for this layout.
562 */
563 public final TextPaint getPaint() {
564 return mPaint;
565 }
566
567 /**
568 * Return the width of this layout.
569 */
570 public final int getWidth() {
571 return mWidth;
572 }
573
574 /**
575 * Return the width to which this Layout is ellipsizing, or
576 * {@link #getWidth} if it is not doing anything special.
577 */
578 public int getEllipsizedWidth() {
579 return mWidth;
580 }
581
582 /**
583 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800584 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 * it does not cause the text to reflow to use the full new width.
586 */
587 public final void increaseWidthTo(int wid) {
588 if (wid < mWidth) {
589 throw new RuntimeException("attempted to reduce Layout width");
590 }
591
592 mWidth = wid;
593 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 /**
596 * Return the total height of this layout.
597 */
598 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800599 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601
602 /**
603 * Return the base alignment of this layout.
604 */
605 public final Alignment getAlignment() {
606 return mAlignment;
607 }
608
609 /**
610 * Return what the text height is multiplied by to get the line height.
611 */
612 public final float getSpacingMultiplier() {
613 return mSpacingMult;
614 }
615
616 /**
617 * Return the number of units of leading that are added to each line.
618 */
619 public final float getSpacingAdd() {
620 return mSpacingAdd;
621 }
622
623 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700624 * Return the heuristic used to determine paragraph text direction.
625 * @hide
626 */
627 public final TextDirectionHeuristic getTextDirectionHeuristic() {
628 return mTextDir;
629 }
630
631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 * Return the number of lines of text in this layout.
633 */
634 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 /**
637 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
638 * If bounds is not null, return the top, left, right, bottom extents
639 * of the specified line in it.
640 * @param line which line to examine (0..getLineCount() - 1)
641 * @param bounds Optional. If not null, it returns the extent of the line
642 * @return the Y-coordinate of the baseline
643 */
644 public int getLineBounds(int line, Rect bounds) {
645 if (bounds != null) {
646 bounds.left = 0; // ???
647 bounds.top = getLineTop(line);
648 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800649 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651 return getLineBaseline(line);
652 }
653
654 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800655 * Return the vertical position of the top of the specified line
656 * (0&hellip;getLineCount()).
657 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 * bottom of the last line.
659 */
660 public abstract int getLineTop(int line);
661
662 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800663 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 */
665 public abstract int getLineDescent(int line);
666
667 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800668 * Return the text offset of the beginning of the specified line (
669 * 0&hellip;getLineCount()). If the specified line is equal to the line
670 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 */
672 public abstract int getLineStart(int line);
673
674 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800675 * Returns the primary directionality of the paragraph containing the
676 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
677 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
679 public abstract int getParagraphDirection(int line);
680
681 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700682 * Returns whether the specified line contains one or more
683 * characters that need to be handled specially, like tabs
684 * or emoji.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 */
686 public abstract boolean getLineContainsTab(int line);
687
688 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800689 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 * The array alternates counts of characters in left-to-right
691 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800692 *
693 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 */
695 public abstract Directions getLineDirections(int line);
696
697 /**
698 * Returns the (negative) number of extra pixels of ascent padding in the
699 * top line of the Layout.
700 */
701 public abstract int getTopPadding();
702
703 /**
704 * Returns the number of extra pixels of descent padding in the
705 * bottom line of the Layout.
706 */
707 public abstract int getBottomPadding();
708
Raph Levien26d443a2015-03-30 14:18:32 -0700709 /**
710 * Returns the hyphen edit for a line.
711 *
712 * @hide
713 */
714 public int getHyphen(int line) {
715 return 0;
716 }
717
Doug Felt4e0c5e52010-03-15 16:56:02 -0700718
719 /**
720 * Returns true if the character at offset and the preceding character
721 * are at different run levels (and thus there's a split caret).
722 * @param offset the offset
723 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800724 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700725 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800726 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800727 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700728 Directions dirs = getLineDirections(line);
729 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
730 return false;
731 }
732
733 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800734 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700735 int lineEnd = getLineEnd(line);
736 if (offset == lineStart || offset == lineEnd) {
737 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
738 int runIndex = offset == lineStart ? 0 : runs.length - 2;
739 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
740 }
741
742 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800743 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700744 if (offset == runs[i]) {
745 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800746 }
747 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700748 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800749 }
750
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700751 /**
752 * Returns true if the character at offset is right to left (RTL).
753 * @param offset the offset
754 * @return true if the character is RTL, false if it is LTR
755 */
756 public boolean isRtlCharAt(int offset) {
757 int line = getLineForOffset(offset);
758 Directions dirs = getLineDirections(line);
759 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
760 return false;
761 }
762 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
763 return true;
764 }
765 int[] runs = dirs.mDirections;
766 int lineStart = getLineStart(line);
767 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700768 int start = lineStart + runs[i];
769 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
770 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700771 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
772 return ((level & 1) != 0);
773 }
774 }
775 // Should happen only if the offset is "out of bounds"
776 return false;
777 }
778
Doug Felt9f7a4442010-03-01 12:45:56 -0800779 private boolean primaryIsTrailingPrevious(int offset) {
780 int line = getLineForOffset(offset);
781 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700782 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800783 int[] runs = getLineDirections(line).mDirections;
784
785 int levelAt = -1;
786 for (int i = 0; i < runs.length; i += 2) {
787 int start = lineStart + runs[i];
788 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
789 if (limit > lineEnd) {
790 limit = lineEnd;
791 }
792 if (offset >= start && offset < limit) {
793 if (offset > start) {
794 // Previous character is at same level, so don't use trailing.
795 return false;
796 }
797 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
798 break;
799 }
800 }
801 if (levelAt == -1) {
802 // Offset was limit of line.
803 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
804 }
805
806 // At level boundary, check previous level.
807 int levelBefore = -1;
808 if (offset == lineStart) {
809 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
810 } else {
811 offset -= 1;
812 for (int i = 0; i < runs.length; i += 2) {
813 int start = lineStart + runs[i];
814 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
815 if (limit > lineEnd) {
816 limit = lineEnd;
817 }
818 if (offset >= start && offset < limit) {
819 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
820 break;
821 }
822 }
823 }
824
825 return levelBefore < levelAt;
826 }
827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 /**
829 * Get the primary horizontal position for the specified text offset.
830 * This is the location where a new character would be inserted in
831 * the paragraph's primary direction.
832 */
833 public float getPrimaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800834 return getPrimaryHorizontal(offset, false /* not clamped */);
835 }
836
837 /**
838 * Get the primary horizontal position for the specified text offset, but
839 * optionally clamp it so that it doesn't exceed the width of the layout.
840 * @hide
841 */
842 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800843 boolean trailing = primaryIsTrailingPrevious(offset);
Raph Levienafe8e9b2012-12-19 16:09:32 -0800844 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 }
846
847 /**
848 * Get the secondary horizontal position for the specified text offset.
849 * This is the location where a new character would be inserted in
850 * the direction other than the paragraph's primary direction.
851 */
852 public float getSecondaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800853 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 }
855
Raph Levienafe8e9b2012-12-19 16:09:32 -0800856 /**
857 * Get the secondary horizontal position for the specified text offset, but
858 * optionally clamp it so that it doesn't exceed the width of the layout.
859 * @hide
860 */
861 public float getSecondaryHorizontal(int offset, boolean clamped) {
862 boolean trailing = primaryIsTrailingPrevious(offset);
863 return getHorizontal(offset, !trailing, clamped);
864 }
865
866 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 int line = getLineForOffset(offset);
868
Raph Levienafe8e9b2012-12-19 16:09:32 -0800869 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
871
Raph Levienafe8e9b2012-12-19 16:09:32 -0800872 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700874 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 int dir = getParagraphDirection(line);
Doug Feltc982f602010-05-25 11:51:40 -0700876 boolean hasTabOrEmoji = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 Directions directions = getLineDirections(line);
878
Doug Feltc982f602010-05-25 11:51:40 -0700879 TabStops tabStops = null;
880 if (hasTabOrEmoji && mText instanceof Spanned) {
881 // Just checking this line should be good enough, tabs should be
882 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700883 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700884 if (tabs.length > 0) {
885 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
888
Doug Felte8e45f22010-03-29 14:58:40 -0700889 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700890 tl.set(mPaint, mText, start, end, dir, directions, hasTabOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700891 float wid = tl.measure(offset - start, trailing, null);
892 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893
Raph Levienafe8e9b2012-12-19 16:09:32 -0800894 if (clamped && wid > mWidth) {
895 wid = mWidth;
896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 int left = getParagraphLeft(line);
898 int right = getParagraphRight(line);
899
Doug Feltc982f602010-05-25 11:51:40 -0700900 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 }
902
903 /**
904 * Get the leftmost position that should be exposed for horizontal
905 * scrolling on the specified line.
906 */
907 public float getLineLeft(int line) {
908 int dir = getParagraphDirection(line);
909 Alignment align = getParagraphAlignment(line);
910
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700911 if (align == Alignment.ALIGN_LEFT) {
912 return 0;
913 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 if (dir == DIR_RIGHT_TO_LEFT)
915 return getParagraphRight(line) - getLineMax(line);
916 else
917 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700918 } else if (align == Alignment.ALIGN_RIGHT) {
919 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 } else if (align == Alignment.ALIGN_OPPOSITE) {
921 if (dir == DIR_RIGHT_TO_LEFT)
922 return 0;
923 else
924 return mWidth - getLineMax(line);
925 } else { /* align == Alignment.ALIGN_CENTER */
926 int left = getParagraphLeft(line);
927 int right = getParagraphRight(line);
928 int max = ((int) getLineMax(line)) & ~1;
929
930 return left + ((right - left) - max) / 2;
931 }
932 }
933
934 /**
935 * Get the rightmost position that should be exposed for horizontal
936 * scrolling on the specified line.
937 */
938 public float getLineRight(int line) {
939 int dir = getParagraphDirection(line);
940 Alignment align = getParagraphAlignment(line);
941
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700942 if (align == Alignment.ALIGN_LEFT) {
943 return getParagraphLeft(line) + getLineMax(line);
944 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 if (dir == DIR_RIGHT_TO_LEFT)
946 return mWidth;
947 else
948 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700949 } else if (align == Alignment.ALIGN_RIGHT) {
950 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 } else if (align == Alignment.ALIGN_OPPOSITE) {
952 if (dir == DIR_RIGHT_TO_LEFT)
953 return getLineMax(line);
954 else
955 return mWidth;
956 } else { /* align == Alignment.ALIGN_CENTER */
957 int left = getParagraphLeft(line);
958 int right = getParagraphRight(line);
959 int max = ((int) getLineMax(line)) & ~1;
960
961 return right - ((right - left) - max) / 2;
962 }
963 }
964
965 /**
Doug Felt0c702b82010-05-14 10:55:42 -0700966 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -0700967 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 */
969 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700970 float margin = getParagraphLeadingMargin(line);
971 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -0700972 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 }
974
975 /**
Doug Feltc982f602010-05-25 11:51:40 -0700976 * Gets the unsigned horizontal extent of the specified line, including
977 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 */
979 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700980 float margin = getParagraphLeadingMargin(line);
981 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -0700982 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984
Doug Feltc982f602010-05-25 11:51:40 -0700985 /**
986 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
987 * tab stops instead of using the ones passed in.
988 * @param line the index of the line
989 * @param full whether to include trailing whitespace
990 * @return the extent of the line
991 */
992 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700994 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -0700995
996 boolean hasTabsOrEmoji = getLineContainsTab(line);
997 TabStops tabStops = null;
998 if (hasTabsOrEmoji && mText instanceof Spanned) {
999 // Just checking this line should be good enough, tabs should be
1000 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001001 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001002 if (tabs.length > 0) {
1003 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1004 }
1005 }
Doug Felte8e45f22010-03-29 14:58:40 -07001006 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001007 // Returned directions can actually be null
1008 if (directions == null) {
1009 return 0f;
1010 }
Doug Feltc982f602010-05-25 11:51:40 -07001011 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012
Doug Felte8e45f22010-03-29 14:58:40 -07001013 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -07001014 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
1015 float width = tl.metrics(null);
1016 TextLine.recycle(tl);
1017 return width;
1018 }
1019
1020 /**
1021 * Returns the signed horizontal extent of the specified line, excluding
1022 * leading margin. If full is false, excludes trailing whitespace.
1023 * @param line the index of the line
1024 * @param tabStops the tab stops, can be null if we know they're not used.
1025 * @param full whether to include trailing whitespace
1026 * @return the extent of the text on this line
1027 */
1028 private float getLineExtent(int line, TabStops tabStops, boolean full) {
1029 int start = getLineStart(line);
1030 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
1031 boolean hasTabsOrEmoji = getLineContainsTab(line);
1032 Directions directions = getLineDirections(line);
1033 int dir = getParagraphDirection(line);
1034
1035 TextLine tl = TextLine.obtain();
1036 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001037 float width = tl.metrics(null);
1038 TextLine.recycle(tl);
1039 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041
1042 /**
1043 * Get the line number corresponding to the specified vertical position.
1044 * If you ask for a position above 0, you get 0; if you ask for a position
1045 * below the bottom of the text, you get the last line.
1046 */
1047 // FIXME: It may be faster to do a linear search for layouts without many lines.
1048 public int getLineForVertical(int vertical) {
1049 int high = getLineCount(), low = -1, guess;
1050
1051 while (high - low > 1) {
1052 guess = (high + low) / 2;
1053
1054 if (getLineTop(guess) > vertical)
1055 high = guess;
1056 else
1057 low = guess;
1058 }
1059
1060 if (low < 0)
1061 return 0;
1062 else
1063 return low;
1064 }
1065
1066 /**
1067 * Get the line number on which the specified text offset appears.
1068 * If you ask for a position before 0, you get 0; if you ask for a position
1069 * beyond the end of the text, you get the last line.
1070 */
1071 public int getLineForOffset(int offset) {
1072 int high = getLineCount(), low = -1, guess;
1073
1074 while (high - low > 1) {
1075 guess = (high + low) / 2;
1076
1077 if (getLineStart(guess) > offset)
1078 high = guess;
1079 else
1080 low = guess;
1081 }
1082
1083 if (low < 0)
1084 return 0;
1085 else
1086 return low;
1087 }
1088
1089 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001090 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 * closest to the specified horizontal position.
1092 */
1093 public int getOffsetForHorizontal(int line, float horiz) {
1094 int max = getLineEnd(line) - 1;
1095 int min = getLineStart(line);
1096 Directions dirs = getLineDirections(line);
1097
1098 if (line == getLineCount() - 1)
1099 max++;
1100
1101 int best = min;
1102 float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz);
1103
Doug Felt9f7a4442010-03-01 12:45:56 -08001104 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1105 int here = min + dirs.mDirections[i];
1106 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1107 int swap = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0 ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108
1109 if (there > max)
1110 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 int high = there - 1 + 1, low = here + 1 - 1, guess;
1112
1113 while (high - low > 1) {
1114 guess = (high + low) / 2;
1115 int adguess = getOffsetAtStartOf(guess);
1116
1117 if (getPrimaryHorizontal(adguess) * swap >= horiz * swap)
1118 high = guess;
1119 else
1120 low = guess;
1121 }
1122
1123 if (low < here + 1)
1124 low = here + 1;
1125
1126 if (low < there) {
1127 low = getOffsetAtStartOf(low);
1128
1129 float dist = Math.abs(getPrimaryHorizontal(low) - horiz);
1130
1131 int aft = TextUtils.getOffsetAfter(mText, low);
1132 if (aft < there) {
1133 float other = Math.abs(getPrimaryHorizontal(aft) - horiz);
1134
1135 if (other < dist) {
1136 dist = other;
1137 low = aft;
1138 }
1139 }
1140
1141 if (dist < bestdist) {
1142 bestdist = dist;
Doug Felt9f7a4442010-03-01 12:45:56 -08001143 best = low;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145 }
1146
1147 float dist = Math.abs(getPrimaryHorizontal(here) - horiz);
1148
1149 if (dist < bestdist) {
1150 bestdist = dist;
1151 best = here;
1152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 }
1154
1155 float dist = Math.abs(getPrimaryHorizontal(max) - horiz);
1156
Raph Levien373b7a82013-09-20 15:11:52 -07001157 if (dist <= bestdist) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 bestdist = dist;
1159 best = max;
1160 }
1161
1162 return best;
1163 }
1164
1165 /**
1166 * Return the text offset after the last character on the specified line.
1167 */
1168 public final int getLineEnd(int line) {
1169 return getLineStart(line + 1);
1170 }
1171
Doug Felt9f7a4442010-03-01 12:45:56 -08001172 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 * Return the text offset after the last visible character (so whitespace
1174 * is not counted) on the specified line.
1175 */
1176 public int getLineVisibleEnd(int line) {
1177 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1178 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 CharSequence text = mText;
1182 char ch;
1183 if (line == getLineCount() - 1) {
1184 return end;
1185 }
1186
1187 for (; end > start; end--) {
1188 ch = text.charAt(end - 1);
1189
1190 if (ch == '\n') {
1191 return end - 1;
1192 }
1193
Raph Levienc94f7422015-03-06 19:19:48 -08001194 // Note: keep this in sync with Minikin LineBreaker::isLineEndSpace()
1195 if (!(ch == ' ' || ch == '\t' || ch == 0x1680 ||
1196 (0x2000 <= ch && ch <= 0x200A && ch != 0x2007) ||
1197 ch == 0x205F || ch == 0x3000)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 break;
1199 }
1200
1201 }
1202
1203 return end;
1204 }
1205
1206 /**
1207 * Return the vertical position of the bottom of the specified line.
1208 */
1209 public final int getLineBottom(int line) {
1210 return getLineTop(line + 1);
1211 }
1212
1213 /**
1214 * Return the vertical position of the baseline of the specified line.
1215 */
1216 public final int getLineBaseline(int line) {
1217 // getLineTop(line+1) == getLineTop(line)
1218 return getLineTop(line+1) - getLineDescent(line);
1219 }
1220
1221 /**
1222 * Get the ascent of the text on the specified line.
1223 * The return value is negative to match the Paint.ascent() convention.
1224 */
1225 public final int getLineAscent(int line) {
1226 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1227 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1228 }
1229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001231 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001235 return getOffsetToLeftRightOf(offset, false);
1236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237
Doug Felt9f7a4442010-03-01 12:45:56 -08001238 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1239 int line = getLineForOffset(caret);
1240 int lineStart = getLineStart(line);
1241 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001242 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001244 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001245 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001246 // if walking off line, look at the line we're headed to
1247 if (advance) {
1248 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001249 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001250 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001251 ++line;
1252 } else {
1253 return caret; // at very end, don't move
1254 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001255 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001256 } else {
1257 if (caret == lineStart) {
1258 if (line > 0) {
1259 lineChanged = true;
1260 --line;
1261 } else {
1262 return caret; // at very start, don't move
1263 }
1264 }
1265 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001266
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001267 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001268 lineStart = getLineStart(line);
1269 lineEnd = getLineEnd(line);
1270 int newDir = getParagraphDirection(line);
1271 if (newDir != lineDir) {
1272 // unusual case. we want to walk onto the line, but it runs
1273 // in a different direction than this one, so we fake movement
1274 // in the opposite direction.
1275 toLeft = !toLeft;
1276 lineDir = newDir;
1277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001279
Doug Felte8e45f22010-03-29 14:58:40 -07001280 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001281
Doug Felte8e45f22010-03-29 14:58:40 -07001282 TextLine tl = TextLine.obtain();
1283 // XXX: we don't care about tabs
1284 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1285 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1286 tl = TextLine.recycle(tl);
1287 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289
1290 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001291 // XXX this probably should skip local reorderings and
1292 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 if (offset == 0)
1294 return 0;
1295
1296 CharSequence text = mText;
1297 char c = text.charAt(offset);
1298
1299 if (c >= '\uDC00' && c <= '\uDFFF') {
1300 char c1 = text.charAt(offset - 1);
1301
1302 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1303 offset -= 1;
1304 }
1305
1306 if (mSpannedText) {
1307 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1308 ReplacementSpan.class);
1309
1310 for (int i = 0; i < spans.length; i++) {
1311 int start = ((Spanned) text).getSpanStart(spans[i]);
1312 int end = ((Spanned) text).getSpanEnd(spans[i]);
1313
1314 if (start < offset && end > offset)
1315 offset = start;
1316 }
1317 }
1318
1319 return offset;
1320 }
1321
1322 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001323 * Determine whether we should clamp cursor position. Currently it's
1324 * only robust for left-aligned displays.
1325 * @hide
1326 */
1327 public boolean shouldClampCursor(int line) {
1328 // Only clamp cursor position in left-aligned displays.
1329 switch (getParagraphAlignment(line)) {
1330 case ALIGN_LEFT:
1331 return true;
1332 case ALIGN_NORMAL:
1333 return getParagraphDirection(line) > 0;
1334 default:
1335 return false;
1336 }
1337
1338 }
1339 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 * Fills in the specified Path with a representation of a cursor
1341 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001342 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 * directionalities.
1344 */
1345 public void getCursorPath(int point, Path dest,
1346 CharSequence editingBuffer) {
1347 dest.reset();
1348
1349 int line = getLineForOffset(point);
1350 int top = getLineTop(line);
1351 int bottom = getLineTop(line+1);
1352
Raph Levienafe8e9b2012-12-19 16:09:32 -08001353 boolean clamped = shouldClampCursor(line);
1354 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1355 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356
Jeff Brown497a92c2010-09-12 17:55:08 -07001357 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1358 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1359 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 int dist = 0;
1361
1362 if (caps != 0 || fn != 0) {
1363 dist = (bottom - top) >> 2;
1364
1365 if (fn != 0)
1366 top += dist;
1367 if (caps != 0)
1368 bottom -= dist;
1369 }
1370
1371 if (h1 < 0.5f)
1372 h1 = 0.5f;
1373 if (h2 < 0.5f)
1374 h2 = 0.5f;
1375
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001376 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 dest.moveTo(h1, top);
1378 dest.lineTo(h1, bottom);
1379 } else {
1380 dest.moveTo(h1, top);
1381 dest.lineTo(h1, (top + bottom) >> 1);
1382
1383 dest.moveTo(h2, (top + bottom) >> 1);
1384 dest.lineTo(h2, bottom);
1385 }
1386
1387 if (caps == 2) {
1388 dest.moveTo(h2, bottom);
1389 dest.lineTo(h2 - dist, bottom + dist);
1390 dest.lineTo(h2, bottom);
1391 dest.lineTo(h2 + dist, bottom + dist);
1392 } else if (caps == 1) {
1393 dest.moveTo(h2, bottom);
1394 dest.lineTo(h2 - dist, bottom + dist);
1395
1396 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1397 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1398
1399 dest.moveTo(h2 + dist, bottom + dist);
1400 dest.lineTo(h2, bottom);
1401 }
1402
1403 if (fn == 2) {
1404 dest.moveTo(h1, top);
1405 dest.lineTo(h1 - dist, top - dist);
1406 dest.lineTo(h1, top);
1407 dest.lineTo(h1 + dist, top - dist);
1408 } else if (fn == 1) {
1409 dest.moveTo(h1, top);
1410 dest.lineTo(h1 - dist, top - dist);
1411
1412 dest.moveTo(h1 - dist, top - dist + 0.5f);
1413 dest.lineTo(h1 + dist, top - dist + 0.5f);
1414
1415 dest.moveTo(h1 + dist, top - dist);
1416 dest.lineTo(h1, top);
1417 }
1418 }
1419
1420 private void addSelection(int line, int start, int end,
1421 int top, int bottom, Path dest) {
1422 int linestart = getLineStart(line);
1423 int lineend = getLineEnd(line);
1424 Directions dirs = getLineDirections(line);
1425
1426 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1427 lineend--;
1428
Doug Felt9f7a4442010-03-01 12:45:56 -08001429 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1430 int here = linestart + dirs.mDirections[i];
1431 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 if (there > lineend)
1434 there = lineend;
1435
1436 if (start <= there && end >= here) {
1437 int st = Math.max(start, here);
1438 int en = Math.min(end, there);
1439
1440 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001441 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1442 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001444 float left = Math.min(h1, h2);
1445 float right = Math.max(h1, h2);
1446
1447 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 }
1449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 }
1451 }
1452
1453 /**
1454 * Fills in the specified Path with a representation of a highlight
1455 * between the specified offsets. This will often be a rectangle
1456 * or a potentially discontinuous set of rectangles. If the start
1457 * and end are the same, the returned path is empty.
1458 */
1459 public void getSelectionPath(int start, int end, Path dest) {
1460 dest.reset();
1461
1462 if (start == end)
1463 return;
1464
1465 if (end < start) {
1466 int temp = end;
1467 end = start;
1468 start = temp;
1469 }
1470
1471 int startline = getLineForOffset(start);
1472 int endline = getLineForOffset(end);
1473
1474 int top = getLineTop(startline);
1475 int bottom = getLineBottom(endline);
1476
1477 if (startline == endline) {
1478 addSelection(startline, start, end, top, bottom, dest);
1479 } else {
1480 final float width = mWidth;
1481
1482 addSelection(startline, start, getLineEnd(startline),
1483 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1486 dest.addRect(getLineLeft(startline), top,
1487 0, getLineBottom(startline), Path.Direction.CW);
1488 else
1489 dest.addRect(getLineRight(startline), top,
1490 width, getLineBottom(startline), Path.Direction.CW);
1491
1492 for (int i = startline + 1; i < endline; i++) {
1493 top = getLineTop(i);
1494 bottom = getLineBottom(i);
1495 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1496 }
1497
1498 top = getLineTop(endline);
1499 bottom = getLineBottom(endline);
1500
1501 addSelection(endline, getLineStart(endline), end,
1502 top, bottom, dest);
1503
1504 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1505 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1506 else
1507 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1508 }
1509 }
1510
1511 /**
1512 * Get the alignment of the specified paragraph, taking into account
1513 * markup attached to it.
1514 */
1515 public final Alignment getParagraphAlignment(int line) {
1516 Alignment align = mAlignment;
1517
1518 if (mSpannedText) {
1519 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001520 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 getLineEnd(line),
1522 AlignmentSpan.class);
1523
1524 int spanLength = spans.length;
1525 if (spanLength > 0) {
1526 align = spans[spanLength-1].getAlignment();
1527 }
1528 }
1529
1530 return align;
1531 }
1532
1533 /**
1534 * Get the left edge of the specified paragraph, inset by left margins.
1535 */
1536 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001538 int dir = getParagraphDirection(line);
1539 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1540 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
Doug Feltc982f602010-05-25 11:51:40 -07001542 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 }
1544
1545 /**
1546 * Get the right edge of the specified paragraph, inset by right margins.
1547 */
1548 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001550 int dir = getParagraphDirection(line);
1551 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1552 return right; // leading margin has no impact, or no styles
1553 }
1554 return right - getParagraphLeadingMargin(line);
1555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556
Doug Feltc982f602010-05-25 11:51:40 -07001557 /**
1558 * Returns the effective leading margin (unsigned) for this line,
1559 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1560 * @param line the line index
1561 * @return the leading margin of this line
1562 */
1563 private int getParagraphLeadingMargin(int line) {
1564 if (!mSpannedText) {
1565 return 0;
1566 }
1567 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001568
Doug Feltc982f602010-05-25 11:51:40 -07001569 int lineStart = getLineStart(line);
1570 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001571 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001572 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001573 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001574 LeadingMarginSpan.class);
1575 if (spans.length == 0) {
1576 return 0; // no leading margin span;
1577 }
Doug Felt0c702b82010-05-14 10:55:42 -07001578
Doug Feltc982f602010-05-25 11:51:40 -07001579 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001580
1581 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001582 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001583
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001584 boolean useFirstLineMargin = isFirstParaLine;
1585 for (int i = 0; i < spans.length; i++) {
1586 if (spans[i] instanceof LeadingMarginSpan2) {
1587 int spStart = spanned.getSpanStart(spans[i]);
1588 int spanLine = getLineForOffset(spStart);
1589 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1590 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1591 useFirstLineMargin |= line < spanLine + count;
1592 }
1593 }
Doug Feltc982f602010-05-25 11:51:40 -07001594 for (int i = 0; i < spans.length; i++) {
1595 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001596 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
1598
Doug Feltc982f602010-05-25 11:51:40 -07001599 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 }
1601
Doug Felte8e45f22010-03-29 14:58:40 -07001602 /* package */
Gilles Debunne6c488de2012-03-01 16:20:35 -08001603 static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
Doug Felte8e45f22010-03-29 14:58:40 -07001604
1605 MeasuredText mt = MeasuredText.obtain();
1606 TextLine tl = TextLine.obtain();
1607 try {
Raph Levien70616ec2015-03-04 10:41:30 -08001608 mt.setPara(text, start, end, TextDirectionHeuristics.LTR, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001609 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001610 int dir;
1611 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001612 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001613 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001614 } else {
1615 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1616 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001617 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 }
Doug Feltc982f602010-05-25 11:51:40 -07001619 char[] chars = mt.mChars;
1620 int len = mt.mLen;
1621 boolean hasTabs = false;
1622 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001623 // leading margins should be taken into account when measuring a paragraph
1624 int margin = 0;
1625 if (text instanceof Spanned) {
1626 Spanned spanned = (Spanned) text;
1627 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1628 LeadingMarginSpan.class);
1629 for (LeadingMarginSpan lms : spans) {
1630 margin += lms.getLeadingMargin(true);
1631 }
1632 }
Doug Feltc982f602010-05-25 11:51:40 -07001633 for (int i = 0; i < len; ++i) {
1634 if (chars[i] == '\t') {
1635 hasTabs = true;
1636 if (text instanceof Spanned) {
1637 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001638 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001639 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001640 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001641 TabStopSpan.class);
1642 if (spans.length > 0) {
1643 tabStops = new TabStops(TAB_INCREMENT, spans);
1644 }
1645 }
1646 break;
1647 }
1648 }
1649 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001650 return margin + tl.metrics(null);
Doug Felte8e45f22010-03-29 14:58:40 -07001651 } finally {
1652 TextLine.recycle(tl);
1653 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
1656
Doug Felt71b8dd72010-02-16 17:27:09 -08001657 /**
Doug Feltc982f602010-05-25 11:51:40 -07001658 * @hide
1659 */
1660 /* package */ static class TabStops {
1661 private int[] mStops;
1662 private int mNumStops;
1663 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001664
Doug Feltc982f602010-05-25 11:51:40 -07001665 TabStops(int increment, Object[] spans) {
1666 reset(increment, spans);
1667 }
Doug Felt0c702b82010-05-14 10:55:42 -07001668
Doug Feltc982f602010-05-25 11:51:40 -07001669 void reset(int increment, Object[] spans) {
1670 this.mIncrement = increment;
1671
1672 int ns = 0;
1673 if (spans != null) {
1674 int[] stops = this.mStops;
1675 for (Object o : spans) {
1676 if (o instanceof TabStopSpan) {
1677 if (stops == null) {
1678 stops = new int[10];
1679 } else if (ns == stops.length) {
1680 int[] nstops = new int[ns * 2];
1681 for (int i = 0; i < ns; ++i) {
1682 nstops[i] = stops[i];
1683 }
1684 stops = nstops;
1685 }
1686 stops[ns++] = ((TabStopSpan) o).getTabStop();
1687 }
1688 }
1689 if (ns > 1) {
1690 Arrays.sort(stops, 0, ns);
1691 }
1692 if (stops != this.mStops) {
1693 this.mStops = stops;
1694 }
1695 }
1696 this.mNumStops = ns;
1697 }
Doug Felt0c702b82010-05-14 10:55:42 -07001698
Doug Feltc982f602010-05-25 11:51:40 -07001699 float nextTab(float h) {
1700 int ns = this.mNumStops;
1701 if (ns > 0) {
1702 int[] stops = this.mStops;
1703 for (int i = 0; i < ns; ++i) {
1704 int stop = stops[i];
1705 if (stop > h) {
1706 return stop;
1707 }
1708 }
1709 }
1710 return nextDefaultStop(h, mIncrement);
1711 }
1712
1713 public static float nextDefaultStop(float h, int inc) {
1714 return ((int) ((h + inc) / inc)) * inc;
1715 }
1716 }
Doug Felt0c702b82010-05-14 10:55:42 -07001717
Doug Feltc982f602010-05-25 11:51:40 -07001718 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001719 * Returns the position of the next tab stop after h on the line.
1720 *
1721 * @param text the text
1722 * @param start start of the line
1723 * @param end limit of the line
1724 * @param h the current horizontal offset
1725 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1726 * on the line will be used. If there are no tabs, a default offset
1727 * will be used to compute the tab stop.
1728 * @return the offset of the next tab stop.
1729 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 /* package */ static float nextTab(CharSequence text, int start, int end,
1731 float h, Object[] tabs) {
1732 float nh = Float.MAX_VALUE;
1733 boolean alltabs = false;
1734
1735 if (text instanceof Spanned) {
1736 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001737 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 alltabs = true;
1739 }
1740
1741 for (int i = 0; i < tabs.length; i++) {
1742 if (!alltabs) {
1743 if (!(tabs[i] instanceof TabStopSpan))
1744 continue;
1745 }
1746
1747 int where = ((TabStopSpan) tabs[i]).getTabStop();
1748
1749 if (where < nh && where > h)
1750 nh = where;
1751 }
1752
1753 if (nh != Float.MAX_VALUE)
1754 return nh;
1755 }
1756
1757 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1758 }
1759
1760 protected final boolean isSpanned() {
1761 return mSpannedText;
1762 }
1763
Eric Fischer74d31ef2010-08-05 15:29:36 -07001764 /**
1765 * Returns the same as <code>text.getSpans()</code>, except where
1766 * <code>start</code> and <code>end</code> are the same and are not
1767 * at the very beginning of the text, in which case an empty array
1768 * is returned instead.
1769 * <p>
1770 * This is needed because of the special case that <code>getSpans()</code>
1771 * on an empty range returns the spans adjacent to that range, which is
1772 * primarily for the sake of <code>TextWatchers</code> so they will get
1773 * notifications when text goes from empty to non-empty. But it also
1774 * has the unfortunate side effect that if the text ends with an empty
1775 * paragraph, that paragraph accidentally picks up the styles of the
1776 * preceding paragraph (even though those styles will not be picked up
1777 * by new text that is inserted into the empty paragraph).
1778 * <p>
1779 * The reason it just checks whether <code>start</code> and <code>end</code>
1780 * is the same is that the only time a line can contain 0 characters
1781 * is if it is the final paragraph of the Layout; otherwise any line will
1782 * contain at least one printing or newline character. The reason for the
1783 * additional check if <code>start</code> is greater than 0 is that
1784 * if the empty paragraph is the entire content of the buffer, paragraph
1785 * styles that are already applied to the buffer will apply to text that
1786 * is inserted into it.
1787 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07001788 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001789 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08001790 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001791 }
1792
1793 return text.getSpans(start, end, type);
1794 }
1795
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001796 private char getEllipsisChar(TextUtils.TruncateAt method) {
1797 return (method == TextUtils.TruncateAt.END_SMALL) ?
Neil Fullerd29bdb22015-02-06 10:03:08 +00001798 TextUtils.ELLIPSIS_TWO_DOTS[0] :
1799 TextUtils.ELLIPSIS_NORMAL[0];
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001800 }
1801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001803 char[] dest, int destoff, TextUtils.TruncateAt method) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 int ellipsisCount = getEllipsisCount(line);
1805
1806 if (ellipsisCount == 0) {
1807 return;
1808 }
1809
1810 int ellipsisStart = getEllipsisStart(line);
1811 int linestart = getLineStart(line);
1812
1813 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1814 char c;
1815
1816 if (i == ellipsisStart) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001817 c = getEllipsisChar(method); // ellipsis
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 } else {
1819 c = '\uFEFF'; // 0-width space
1820 }
1821
1822 int a = i + linestart;
1823
1824 if (a >= start && a < end) {
1825 dest[destoff + a - start] = c;
1826 }
1827 }
1828 }
1829
1830 /**
1831 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08001832 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 */
1834 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08001835 // Directions represents directional runs within a line of text.
1836 // Runs are pairs of ints listed in visual order, starting from the
1837 // leading margin. The first int of each pair is the offset from
1838 // the first character of the line to the start of the run. The
1839 // second int represents both the length and level of the run.
1840 // The length is in the lower bits, accessed by masking with
1841 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1842 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1843 // To simply test for an RTL direction, test the bit using
1844 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845
Doug Felt9f7a4442010-03-01 12:45:56 -08001846 /* package */ int[] mDirections;
1847 /* package */ Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 mDirections = dirs;
1849 }
1850 }
1851
1852 /**
1853 * Return the offset of the first character to be ellipsized away,
1854 * relative to the start of the line. (So 0 if the beginning of the
1855 * line is ellipsized, not getLineStart().)
1856 */
1857 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07001858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 /**
1860 * Returns the number of characters to be ellipsized away, or 0 if
1861 * no ellipsis is to take place.
1862 */
1863 public abstract int getEllipsisCount(int line);
1864
1865 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1866 /* package */ CharSequence mText;
1867 /* package */ Layout mLayout;
1868 /* package */ int mWidth;
1869 /* package */ TextUtils.TruncateAt mMethod;
1870
1871 public Ellipsizer(CharSequence s) {
1872 mText = s;
1873 }
1874
1875 public char charAt(int off) {
1876 char[] buf = TextUtils.obtain(1);
1877 getChars(off, off + 1, buf, 0);
1878 char ret = buf[0];
1879
1880 TextUtils.recycle(buf);
1881 return ret;
1882 }
1883
1884 public void getChars(int start, int end, char[] dest, int destoff) {
1885 int line1 = mLayout.getLineForOffset(start);
1886 int line2 = mLayout.getLineForOffset(end);
1887
1888 TextUtils.getChars(mText, start, end, dest, destoff);
1889
1890 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001891 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 }
1893 }
1894
1895 public int length() {
1896 return mText.length();
1897 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 public CharSequence subSequence(int start, int end) {
1900 char[] s = new char[end - start];
1901 getChars(start, end, s, 0);
1902 return new String(s);
1903 }
1904
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001905 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 public String toString() {
1907 char[] s = new char[length()];
1908 getChars(0, length(), s, 0);
1909 return new String(s);
1910 }
1911
1912 }
1913
Gilles Debunne6c488de2012-03-01 16:20:35 -08001914 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 private Spanned mSpanned;
1916
1917 public SpannedEllipsizer(CharSequence display) {
1918 super(display);
1919 mSpanned = (Spanned) display;
1920 }
1921
1922 public <T> T[] getSpans(int start, int end, Class<T> type) {
1923 return mSpanned.getSpans(start, end, type);
1924 }
1925
1926 public int getSpanStart(Object tag) {
1927 return mSpanned.getSpanStart(tag);
1928 }
1929
1930 public int getSpanEnd(Object tag) {
1931 return mSpanned.getSpanEnd(tag);
1932 }
1933
1934 public int getSpanFlags(Object tag) {
1935 return mSpanned.getSpanFlags(tag);
1936 }
1937
Gilles Debunne6c488de2012-03-01 16:20:35 -08001938 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 public int nextSpanTransition(int start, int limit, Class type) {
1940 return mSpanned.nextSpanTransition(start, limit, type);
1941 }
1942
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001943 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 public CharSequence subSequence(int start, int end) {
1945 char[] s = new char[end - start];
1946 getChars(start, end, s, 0);
1947
1948 SpannableString ss = new SpannableString(new String(s));
1949 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
1950 return ss;
1951 }
1952 }
1953
1954 private CharSequence mText;
1955 private TextPaint mPaint;
1956 /* package */ TextPaint mWorkPaint;
1957 private int mWidth;
1958 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
1959 private float mSpacingMult;
1960 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07001961 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07001963 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07001964 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965
1966 public static final int DIR_LEFT_TO_RIGHT = 1;
1967 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08001968
Doug Felt20178d62010-02-22 13:39:01 -08001969 /* package */ static final int DIR_REQUEST_LTR = 1;
1970 /* package */ static final int DIR_REQUEST_RTL = -1;
1971 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
1972 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973
Doug Felt9f7a4442010-03-01 12:45:56 -08001974 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
1975 /* package */ static final int RUN_LEVEL_SHIFT = 26;
1976 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
1977 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
1978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 public enum Alignment {
1980 ALIGN_NORMAL,
1981 ALIGN_OPPOSITE,
1982 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001983 /** @hide */
1984 ALIGN_LEFT,
1985 /** @hide */
1986 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 }
1988
1989 private static final int TAB_INCREMENT = 20;
1990
1991 /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001992 new Directions(new int[] { 0, RUN_LENGTH_MASK });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001994 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08001995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996}