blob: 3157152024b2da1b9f6b4ed472c33bc2b72c1c87 [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 Project9066cfe2009-03-03 19:31:44 -080020import android.graphics.Canvas;
21import android.graphics.Paint;
Doug Felt9f7a4442010-03-01 12:45:56 -080022import android.graphics.Path;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.text.method.TextKeyListener;
Doug Felt9f7a4442010-03-01 12:45:56 -080025import android.text.style.AlignmentSpan;
26import android.text.style.LeadingMarginSpan;
Gilles Debunne162bf0f2010-11-16 16:23:53 -080027import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
Doug Felt9f7a4442010-03-01 12:45:56 -080028import android.text.style.LineBackgroundSpan;
29import android.text.style.ParagraphStyle;
30import android.text.style.ReplacementSpan;
31import android.text.style.TabStopSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Siyamed Sinired09ae12016-02-16 14:36:26 -080033import com.android.internal.annotations.VisibleForTesting;
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
Roozbeh Pournader95c7a132015-05-12 12:01:06 -070074 /** @hide */
75 @IntDef({HYPHENATION_FREQUENCY_NORMAL, HYPHENATION_FREQUENCY_FULL,
76 HYPHENATION_FREQUENCY_NONE})
77 @Retention(RetentionPolicy.SOURCE)
78 public @interface HyphenationFrequency {}
79
80 /**
81 * Value for hyphenation frequency indicating no automatic hyphenation. Useful
82 * for backward compatibility, and for cases where the automatic hyphenation algorithm results
83 * in incorrect hyphenation. Mid-word breaks may still happen when a word is wider than the
84 * layout and there is otherwise no valid break. Soft hyphens are ignored and will not be used
85 * as suggestions for potential line breaks.
86 */
87 public static final int HYPHENATION_FREQUENCY_NONE = 0;
88
89 /**
90 * Value for hyphenation frequency indicating a light amount of automatic hyphenation, which
91 * is a conservative default. Useful for informal cases, such as short sentences or chat
92 * messages.
93 */
94 public static final int HYPHENATION_FREQUENCY_NORMAL = 1;
95
96 /**
97 * Value for hyphenation frequency indicating the full amount of automatic hyphenation, typical
98 * in typography. Useful for running text and where it's important to put the maximum amount of
99 * text in a screen with limited space.
100 */
101 public static final int HYPHENATION_FREQUENCY_FULL = 2;
102
Doug Felt71b8dd72010-02-16 17:27:09 -0800103 private static final ParagraphStyle[] NO_PARA_SPANS =
104 ArrayUtils.emptyArray(ParagraphStyle.class);
Dave Bort76c02262009-04-13 17:17:21 -0700105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800107 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * specified text with one line per paragraph.
109 */
110 public static float getDesiredWidth(CharSequence source,
111 TextPaint paint) {
112 return getDesiredWidth(source, 0, source.length(), paint);
113 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800116 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * specified text slice with one line per paragraph.
118 */
119 public static float getDesiredWidth(CharSequence source,
120 int start, int end,
121 TextPaint paint) {
122 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
124 int next;
125 for (int i = start; i <= end; i = next) {
126 next = TextUtils.indexOf(source, '\n', i, end);
127
128 if (next < 0)
129 next = end;
130
Doug Felt71b8dd72010-02-16 17:27:09 -0800131 // note, omits trailing paragraph char
Gilles Debunne6c488de2012-03-01 16:20:35 -0800132 float w = measurePara(paint, source, i, next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
134 if (w > need)
135 need = w;
136
137 next++;
138 }
139
140 return need;
141 }
142
143 /**
144 * Subclasses of Layout use this constructor to set the display text,
145 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800146 * @param text the text to render
147 * @param paint the default paint for the layout. Styles can override
148 * various attributes of the paint.
149 * @param width the wrapping width for the text.
150 * @param align whether to left, right, or center the text. Styles can
151 * override the alignment.
152 * @param spacingMult factor by which to scale the font size to get the
153 * default line spacing
154 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 */
156 protected Layout(CharSequence text, TextPaint paint,
157 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800158 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700159 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
160 spacingMult, spacingAdd);
161 }
162
163 /**
164 * Subclasses of Layout use this constructor to set the display text,
165 * width, and other standard properties.
166 * @param text the text to render
167 * @param paint the default paint for the layout. Styles can override
168 * various attributes of the paint.
169 * @param width the wrapping width for the text.
170 * @param align whether to left, right, or center the text. Styles can
171 * override the alignment.
172 * @param spacingMult factor by which to scale the font size to get the
173 * default line spacing
174 * @param spacingAdd amount to add to the default line spacing
175 *
176 * @hide
177 */
178 protected Layout(CharSequence text, TextPaint paint,
179 int width, Alignment align, TextDirectionHeuristic textDir,
180 float spacingMult, float spacingAdd) {
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 if (width < 0)
183 throw new IllegalArgumentException("Layout: " + width + " < 0");
184
Doug Felte8e45f22010-03-29 14:58:40 -0700185 // Ensure paint doesn't have baselineShift set.
186 // While normally we don't modify the paint the user passed in,
187 // we were already doing this in Styled.drawUniformRun with both
188 // baselineShift and bgColor. We probably should reevaluate bgColor.
189 if (paint != null) {
190 paint.bgColor = 0;
191 paint.baselineShift = 0;
192 }
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 mText = text;
195 mPaint = paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 mWidth = width;
197 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800198 mSpacingMult = spacingMult;
199 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700201 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 }
203
204 /**
205 * Replace constructor properties of this Layout with new ones. Be careful.
206 */
207 /* package */ void replaceWith(CharSequence text, TextPaint paint,
208 int width, Alignment align,
209 float spacingmult, float spacingadd) {
210 if (width < 0) {
211 throw new IllegalArgumentException("Layout: " + width + " < 0");
212 }
213
214 mText = text;
215 mPaint = paint;
216 mWidth = width;
217 mAlignment = align;
218 mSpacingMult = spacingmult;
219 mSpacingAdd = spacingadd;
220 mSpannedText = text instanceof Spanned;
221 }
222
223 /**
224 * Draw this Layout on the specified Canvas.
225 */
226 public void draw(Canvas c) {
227 draw(c, null, null, 0);
228 }
229
230 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800231 * Draw this Layout on the specified canvas, with the highlight path drawn
232 * between the background and the text.
233 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800234 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800235 * @param highlight the path of the highlight or cursor; can be null
236 * @param highlightPaint the paint for the highlight
237 * @param cursorOffsetVertical the amount to temporarily translate the
238 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800240 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
241 int cursorOffsetVertical) {
242 final long lineRange = getLineRangeForDraw(canvas);
243 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
244 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
245 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246
Gilles Debunne6c488de2012-03-01 16:20:35 -0800247 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
248 firstLine, lastLine);
249 drawText(canvas, firstLine, lastLine);
250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
Gilles Debunne6c488de2012-03-01 16:20:35 -0800252 /**
253 * @hide
254 */
255 public void drawText(Canvas canvas, int firstLine, int lastLine) {
256 int previousLineBottom = getLineTop(firstLine);
257 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800258 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700259 int spanEnd = 0;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800260 TextPaint paint = mPaint;
261 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700263 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700264 TabStops tabStops = null;
265 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800266
Doug Felte8e45f22010-03-29 14:58:40 -0700267 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700268
Gilles Debunne6c488de2012-03-01 16:20:35 -0800269 // Draw the lines, one at a time.
270 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700271 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700273 previousLineEnd = getLineStart(lineNum + 1);
274 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
276 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700277 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700279 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280
Raph Levien26d443a2015-03-30 14:18:32 -0700281 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700282 int left = 0;
283 int right = mWidth;
284
Gilles Debunne6c488de2012-03-01 16:20:35 -0800285 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700286 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800287 int textLength = buf.length();
288 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700289
Doug Feltc982f602010-05-25 11:51:40 -0700290 // New batch of paragraph styles, collect into spans array.
291 // Compute the alignment, last alignment style wins.
292 // Reset tabStops, we'll rebuild if we encounter a line with
293 // tabs.
294 // We expect paragraph spans to be relatively infrequent, use
295 // spanEnd so that we can check less frequently. Since
296 // paragraph styles ought to apply to entire paragraphs, we can
297 // just collect the ones present at the start of the paragraph.
298 // If spanEnd is before the end of the paragraph, that's not
299 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700300 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700301 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700303 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800304
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700305 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800306 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700308 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 break;
310 }
311 }
Doug Felt0c702b82010-05-14 10:55:42 -0700312
Doug Feltc982f602010-05-25 11:51:40 -0700313 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800315
Doug Feltc982f602010-05-25 11:51:40 -0700316 // Draw all leading margin spans. Adjust left or right according
317 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700319 boolean useFirstLineMargin = isFirstParaLine;
320 for (int n = 0; n < length; n++) {
321 if (spans[n] instanceof LeadingMarginSpan2) {
322 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
323 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
324 // if there is more than one LeadingMarginSpan2, use
325 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700326 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700327 useFirstLineMargin = true;
328 break;
329 }
330 }
331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 for (int n = 0; n < length; n++) {
333 if (spans[n] instanceof LeadingMarginSpan) {
334 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800336 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800338 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700339 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800341 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800343 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700344 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346 }
347 }
348 }
349
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700350 boolean hasTab = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700351 // Can't tell if we have tabs for sure, currently
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700352 if (hasTab && !tabStopsIsInitialized) {
Doug Feltc982f602010-05-25 11:51:40 -0700353 if (tabStops == null) {
354 tabStops = new TabStops(TAB_INCREMENT, spans);
355 } else {
356 tabStops.reset(TAB_INCREMENT, spans);
357 }
358 tabStopsIsInitialized = true;
359 }
360
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700361 // Determine whether the line aligns to normal, opposite, or center.
362 Alignment align = paraAlign;
363 if (align == Alignment.ALIGN_LEFT) {
364 align = (dir == DIR_LEFT_TO_RIGHT) ?
365 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
366 } else if (align == Alignment.ALIGN_RIGHT) {
367 align = (dir == DIR_LEFT_TO_RIGHT) ?
368 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
369 }
370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 int x;
372 if (align == Alignment.ALIGN_NORMAL) {
373 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700374 x = left + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700376 x = right + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700379 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700381 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700382 x = right - max + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700384 x = left - max + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 }
Doug Feltc982f602010-05-25 11:51:40 -0700386 } else { // Alignment.ALIGN_CENTER
387 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700388 x = ((right + left - max) >> 1) +
389 getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 }
391 }
392
Raph Levien26d443a2015-03-30 14:18:32 -0700393 paint.setHyphenEdit(getHyphen(lineNum));
394 Directions directions = getLineDirections(lineNum);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700395 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800396 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800397 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 } else {
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700399 tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800400 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
Raph Levien9a174dd2015-04-08 13:35:03 -0700402 paint.setHyphenEdit(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
Doug Feltc982f602010-05-25 11:51:40 -0700404
Doug Felte8e45f22010-03-29 14:58:40 -0700405 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407
408 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800409 * @hide
410 */
411 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
412 int cursorOffsetVertical, int firstLine, int lastLine) {
413 // First, draw LineBackgroundSpans.
414 // LineBackgroundSpans know nothing about the alignment, margins, or
415 // direction of the layout or line. XXX: Should they?
416 // They are evaluated at each line.
417 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700418 if (mLineBackgroundSpans == null) {
419 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700420 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800421
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700422 Spanned buffer = (Spanned) mText;
423 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700424 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800425
Gilles Debunneeca5b732012-04-25 18:48:42 -0700426 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700427 int previousLineBottom = getLineTop(firstLine);
428 int previousLineEnd = getLineStart(firstLine);
429 ParagraphStyle[] spans = NO_PARA_SPANS;
430 int spansLength = 0;
431 TextPaint paint = mPaint;
432 int spanEnd = 0;
433 final int width = mWidth;
434 for (int i = firstLine; i <= lastLine; i++) {
435 int start = previousLineEnd;
436 int end = getLineStart(i + 1);
437 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800438
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700439 int ltop = previousLineBottom;
440 int lbottom = getLineTop(i + 1);
441 previousLineBottom = lbottom;
442 int lbaseline = lbottom - getLineDescent(i);
443
444 if (start >= spanEnd) {
445 // These should be infrequent, so we'll use this so that
446 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700447 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700448 // All LineBackgroundSpans on a line contribute to its background.
449 spansLength = 0;
450 // Duplication of the logic of getParagraphSpans
451 if (start != end || start == 0) {
452 // Equivalent to a getSpans(start, end), but filling the 'spans' local
453 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700454 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
455 // equal test is valid since both intervals are not empty by
456 // construction
457 if (mLineBackgroundSpans.spanStarts[j] >= end ||
458 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500459 spans = GrowingArrayUtils.append(
460 spans, spansLength, mLineBackgroundSpans.spans[j]);
461 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700462 }
463 }
464 }
465
466 for (int n = 0; n < spansLength; n++) {
467 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
468 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
469 ltop, lbaseline, lbottom,
470 buffer, start, end, i);
471 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800472 }
473 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700474 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800475 }
476
477 // There can be a highlight even without spans if we are drawing
478 // a non-spanned transformation of a spanned editing buffer.
479 if (highlight != null) {
480 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
481 canvas.drawPath(highlight, highlightPaint);
482 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
483 }
484 }
485
486 /**
487 * @param canvas
488 * @return The range of lines that need to be drawn, possibly empty.
489 * @hide
490 */
491 public long getLineRangeForDraw(Canvas canvas) {
492 int dtop, dbottom;
493
494 synchronized (sTempRect) {
495 if (!canvas.getClipBounds(sTempRect)) {
496 // Negative range end used as a special flag
497 return TextUtils.packRangeInLong(0, -1);
498 }
499
500 dtop = sTempRect.top;
501 dbottom = sTempRect.bottom;
502 }
503
504 final int top = Math.max(dtop, 0);
505 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
506
Gilles Debunne2fba3382012-06-11 17:46:24 -0700507 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800508 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
509 }
510
511 /**
Doug Feltc982f602010-05-25 11:51:40 -0700512 * Return the start position of the line, given the left and right bounds
513 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700514 *
Doug Feltc982f602010-05-25 11:51:40 -0700515 * @param line the line index
516 * @param left the left bounds (0, or leading margin if ltr para)
517 * @param right the right bounds (width, minus leading margin if rtl para)
518 * @return the start position of the line (to right of line if rtl para)
519 */
520 private int getLineStartPos(int line, int left, int right) {
521 // Adjust the point at which to start rendering depending on the
522 // alignment of the paragraph.
523 Alignment align = getParagraphAlignment(line);
524 int dir = getParagraphDirection(line);
525
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700526 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700527 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
528 } else if (align == Alignment.ALIGN_RIGHT) {
529 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
530 }
531
532 int x;
533 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700534 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700535 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700536 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700537 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700538 }
539 } else {
540 TabStops tabStops = null;
541 if (mSpannedText && getLineContainsTab(line)) {
542 Spanned spanned = (Spanned) mText;
543 int start = getLineStart(line);
544 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
545 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800546 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
547 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700548 if (tabSpans.length > 0) {
549 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
550 }
551 }
552 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700553 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700554 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700555 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700556 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700557 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700558 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700559 }
560 } else { // Alignment.ALIGN_CENTER
561 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700562 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700563 }
564 }
565 return x;
566 }
567
568 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 * Return the text that is displayed by this Layout.
570 */
571 public final CharSequence getText() {
572 return mText;
573 }
574
575 /**
576 * Return the base Paint properties for this layout.
577 * Do NOT change the paint, which may result in funny
578 * drawing for this layout.
579 */
580 public final TextPaint getPaint() {
581 return mPaint;
582 }
583
584 /**
585 * Return the width of this layout.
586 */
587 public final int getWidth() {
588 return mWidth;
589 }
590
591 /**
592 * Return the width to which this Layout is ellipsizing, or
593 * {@link #getWidth} if it is not doing anything special.
594 */
595 public int getEllipsizedWidth() {
596 return mWidth;
597 }
598
599 /**
600 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800601 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 * it does not cause the text to reflow to use the full new width.
603 */
604 public final void increaseWidthTo(int wid) {
605 if (wid < mWidth) {
606 throw new RuntimeException("attempted to reduce Layout width");
607 }
608
609 mWidth = wid;
610 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 /**
613 * Return the total height of this layout.
614 */
615 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800616 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
618
619 /**
620 * Return the base alignment of this layout.
621 */
622 public final Alignment getAlignment() {
623 return mAlignment;
624 }
625
626 /**
627 * Return what the text height is multiplied by to get the line height.
628 */
629 public final float getSpacingMultiplier() {
630 return mSpacingMult;
631 }
632
633 /**
634 * Return the number of units of leading that are added to each line.
635 */
636 public final float getSpacingAdd() {
637 return mSpacingAdd;
638 }
639
640 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700641 * Return the heuristic used to determine paragraph text direction.
642 * @hide
643 */
644 public final TextDirectionHeuristic getTextDirectionHeuristic() {
645 return mTextDir;
646 }
647
648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 * Return the number of lines of text in this layout.
650 */
651 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 /**
654 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
655 * If bounds is not null, return the top, left, right, bottom extents
656 * of the specified line in it.
657 * @param line which line to examine (0..getLineCount() - 1)
658 * @param bounds Optional. If not null, it returns the extent of the line
659 * @return the Y-coordinate of the baseline
660 */
661 public int getLineBounds(int line, Rect bounds) {
662 if (bounds != null) {
663 bounds.left = 0; // ???
664 bounds.top = getLineTop(line);
665 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800666 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
668 return getLineBaseline(line);
669 }
670
671 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800672 * Return the vertical position of the top of the specified line
673 * (0&hellip;getLineCount()).
674 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 * bottom of the last line.
676 */
677 public abstract int getLineTop(int line);
678
679 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800680 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 */
682 public abstract int getLineDescent(int line);
683
684 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800685 * Return the text offset of the beginning of the specified line (
686 * 0&hellip;getLineCount()). If the specified line is equal to the line
687 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public abstract int getLineStart(int line);
690
691 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800692 * Returns the primary directionality of the paragraph containing the
693 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
694 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 */
696 public abstract int getParagraphDirection(int line);
697
698 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700699 * Returns whether the specified line contains one or more
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700700 * characters that need to be handled specially, like tabs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 */
702 public abstract boolean getLineContainsTab(int line);
703
704 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800705 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 * The array alternates counts of characters in left-to-right
707 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800708 *
709 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 */
711 public abstract Directions getLineDirections(int line);
712
713 /**
714 * Returns the (negative) number of extra pixels of ascent padding in the
715 * top line of the Layout.
716 */
717 public abstract int getTopPadding();
718
719 /**
720 * Returns the number of extra pixels of descent padding in the
721 * bottom line of the Layout.
722 */
723 public abstract int getBottomPadding();
724
Raph Levien26d443a2015-03-30 14:18:32 -0700725 /**
726 * Returns the hyphen edit for a line.
727 *
728 * @hide
729 */
730 public int getHyphen(int line) {
731 return 0;
732 }
733
Raph Levien2ea52902015-07-01 14:39:31 -0700734 /**
735 * Returns the left indent for a line.
736 *
737 * @hide
738 */
739 public int getIndentAdjust(int line, Alignment alignment) {
740 return 0;
741 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700742
743 /**
744 * Returns true if the character at offset and the preceding character
745 * are at different run levels (and thus there's a split caret).
746 * @param offset the offset
747 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800748 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700749 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800750 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800751 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700752 Directions dirs = getLineDirections(line);
753 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
754 return false;
755 }
756
757 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800758 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700759 int lineEnd = getLineEnd(line);
760 if (offset == lineStart || offset == lineEnd) {
761 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
762 int runIndex = offset == lineStart ? 0 : runs.length - 2;
763 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
764 }
765
766 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800767 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700768 if (offset == runs[i]) {
769 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800770 }
771 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700772 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800773 }
774
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700775 /**
776 * Returns true if the character at offset is right to left (RTL).
777 * @param offset the offset
778 * @return true if the character is RTL, false if it is LTR
779 */
780 public boolean isRtlCharAt(int offset) {
781 int line = getLineForOffset(offset);
782 Directions dirs = getLineDirections(line);
783 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
784 return false;
785 }
786 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
787 return true;
788 }
789 int[] runs = dirs.mDirections;
790 int lineStart = getLineStart(line);
791 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700792 int start = lineStart + runs[i];
793 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
794 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700795 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
796 return ((level & 1) != 0);
797 }
798 }
799 // Should happen only if the offset is "out of bounds"
800 return false;
801 }
802
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900803 /**
804 * Returns the range of the run that the character at offset belongs to.
805 * @param offset the offset
806 * @return The range of the run
807 * @hide
808 */
809 public long getRunRange(int offset) {
810 int line = getLineForOffset(offset);
811 Directions dirs = getLineDirections(line);
812 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
813 return TextUtils.packRangeInLong(0, getLineEnd(line));
814 }
815 int[] runs = dirs.mDirections;
816 int lineStart = getLineStart(line);
817 for (int i = 0; i < runs.length; i += 2) {
818 int start = lineStart + runs[i];
819 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
820 if (offset >= start && offset < limit) {
821 return TextUtils.packRangeInLong(start, limit);
822 }
823 }
824 // Should happen only if the offset is "out of bounds"
825 return TextUtils.packRangeInLong(0, getLineEnd(line));
826 }
827
Doug Felt9f7a4442010-03-01 12:45:56 -0800828 private boolean primaryIsTrailingPrevious(int offset) {
829 int line = getLineForOffset(offset);
830 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700831 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800832 int[] runs = getLineDirections(line).mDirections;
833
834 int levelAt = -1;
835 for (int i = 0; i < runs.length; i += 2) {
836 int start = lineStart + runs[i];
837 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
838 if (limit > lineEnd) {
839 limit = lineEnd;
840 }
841 if (offset >= start && offset < limit) {
842 if (offset > start) {
843 // Previous character is at same level, so don't use trailing.
844 return false;
845 }
846 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
847 break;
848 }
849 }
850 if (levelAt == -1) {
851 // Offset was limit of line.
852 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
853 }
854
855 // At level boundary, check previous level.
856 int levelBefore = -1;
857 if (offset == lineStart) {
858 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
859 } else {
860 offset -= 1;
861 for (int i = 0; i < runs.length; i += 2) {
862 int start = lineStart + runs[i];
863 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
864 if (limit > lineEnd) {
865 limit = lineEnd;
866 }
867 if (offset >= start && offset < limit) {
868 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
869 break;
870 }
871 }
872 }
873
874 return levelBefore < levelAt;
875 }
876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 /**
878 * Get the primary horizontal position for the specified text offset.
879 * This is the location where a new character would be inserted in
880 * the paragraph's primary direction.
881 */
882 public float getPrimaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800883 return getPrimaryHorizontal(offset, false /* not clamped */);
884 }
885
886 /**
887 * Get the primary horizontal position for the specified text offset, but
888 * optionally clamp it so that it doesn't exceed the width of the layout.
889 * @hide
890 */
891 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800892 boolean trailing = primaryIsTrailingPrevious(offset);
Raph Levienafe8e9b2012-12-19 16:09:32 -0800893 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
895
896 /**
897 * Get the secondary horizontal position for the specified text offset.
898 * This is the location where a new character would be inserted in
899 * the direction other than the paragraph's primary direction.
900 */
901 public float getSecondaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800902 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904
Raph Levienafe8e9b2012-12-19 16:09:32 -0800905 /**
906 * Get the secondary horizontal position for the specified text offset, but
907 * optionally clamp it so that it doesn't exceed the width of the layout.
908 * @hide
909 */
910 public float getSecondaryHorizontal(int offset, boolean clamped) {
911 boolean trailing = primaryIsTrailingPrevious(offset);
912 return getHorizontal(offset, !trailing, clamped);
913 }
914
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900915 private float getHorizontal(int offset, boolean primary) {
916 return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset);
917 }
918
Raph Levienafe8e9b2012-12-19 16:09:32 -0800919 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 int line = getLineForOffset(offset);
921
Raph Levienafe8e9b2012-12-19 16:09:32 -0800922 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
924
Raph Levienafe8e9b2012-12-19 16:09:32 -0800925 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700927 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 int dir = getParagraphDirection(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700929 boolean hasTab = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 Directions directions = getLineDirections(line);
931
Doug Feltc982f602010-05-25 11:51:40 -0700932 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700933 if (hasTab && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -0700934 // Just checking this line should be good enough, tabs should be
935 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700936 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700937 if (tabs.length > 0) {
938 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941
Doug Felte8e45f22010-03-29 14:58:40 -0700942 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700943 tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700944 float wid = tl.measure(offset - start, trailing, null);
945 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946
Raph Levienafe8e9b2012-12-19 16:09:32 -0800947 if (clamped && wid > mWidth) {
948 wid = mWidth;
949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 int left = getParagraphLeft(line);
951 int right = getParagraphRight(line);
952
Doug Feltc982f602010-05-25 11:51:40 -0700953 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 }
955
956 /**
957 * Get the leftmost position that should be exposed for horizontal
958 * scrolling on the specified line.
959 */
960 public float getLineLeft(int line) {
961 int dir = getParagraphDirection(line);
962 Alignment align = getParagraphAlignment(line);
963
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700964 if (align == Alignment.ALIGN_LEFT) {
965 return 0;
966 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 if (dir == DIR_RIGHT_TO_LEFT)
968 return getParagraphRight(line) - getLineMax(line);
969 else
970 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700971 } else if (align == Alignment.ALIGN_RIGHT) {
972 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 } else if (align == Alignment.ALIGN_OPPOSITE) {
974 if (dir == DIR_RIGHT_TO_LEFT)
975 return 0;
976 else
977 return mWidth - getLineMax(line);
978 } else { /* align == Alignment.ALIGN_CENTER */
979 int left = getParagraphLeft(line);
980 int right = getParagraphRight(line);
981 int max = ((int) getLineMax(line)) & ~1;
982
983 return left + ((right - left) - max) / 2;
984 }
985 }
986
987 /**
988 * Get the rightmost position that should be exposed for horizontal
989 * scrolling on the specified line.
990 */
991 public float getLineRight(int line) {
992 int dir = getParagraphDirection(line);
993 Alignment align = getParagraphAlignment(line);
994
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700995 if (align == Alignment.ALIGN_LEFT) {
996 return getParagraphLeft(line) + getLineMax(line);
997 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 if (dir == DIR_RIGHT_TO_LEFT)
999 return mWidth;
1000 else
1001 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001002 } else if (align == Alignment.ALIGN_RIGHT) {
1003 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 } else if (align == Alignment.ALIGN_OPPOSITE) {
1005 if (dir == DIR_RIGHT_TO_LEFT)
1006 return getLineMax(line);
1007 else
1008 return mWidth;
1009 } else { /* align == Alignment.ALIGN_CENTER */
1010 int left = getParagraphLeft(line);
1011 int right = getParagraphRight(line);
1012 int max = ((int) getLineMax(line)) & ~1;
1013
1014 return right - ((right - left) - max) / 2;
1015 }
1016 }
1017
1018 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001019 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001020 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 */
1022 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001023 float margin = getParagraphLeadingMargin(line);
1024 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001025 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027
1028 /**
Doug Feltc982f602010-05-25 11:51:40 -07001029 * Gets the unsigned horizontal extent of the specified line, including
1030 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001033 float margin = getParagraphLeadingMargin(line);
1034 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001035 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
1037
Doug Feltc982f602010-05-25 11:51:40 -07001038 /**
1039 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1040 * tab stops instead of using the ones passed in.
1041 * @param line the index of the line
1042 * @param full whether to include trailing whitespace
1043 * @return the extent of the line
1044 */
1045 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001047 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001048
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001049 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001050 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001051 if (hasTabs && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001052 // Just checking this line should be good enough, tabs should be
1053 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001054 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001055 if (tabs.length > 0) {
1056 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1057 }
1058 }
Doug Felte8e45f22010-03-29 14:58:40 -07001059 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001060 // Returned directions can actually be null
1061 if (directions == null) {
1062 return 0f;
1063 }
Doug Feltc982f602010-05-25 11:51:40 -07001064 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065
Doug Felte8e45f22010-03-29 14:58:40 -07001066 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001067 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Doug Feltc982f602010-05-25 11:51:40 -07001068 float width = tl.metrics(null);
1069 TextLine.recycle(tl);
1070 return width;
1071 }
1072
1073 /**
1074 * Returns the signed horizontal extent of the specified line, excluding
1075 * leading margin. If full is false, excludes trailing whitespace.
1076 * @param line the index of the line
1077 * @param tabStops the tab stops, can be null if we know they're not used.
1078 * @param full whether to include trailing whitespace
1079 * @return the extent of the text on this line
1080 */
1081 private float getLineExtent(int line, TabStops tabStops, boolean full) {
1082 int start = getLineStart(line);
1083 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001084 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001085 Directions directions = getLineDirections(line);
1086 int dir = getParagraphDirection(line);
1087
1088 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001089 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001090 float width = tl.metrics(null);
1091 TextLine.recycle(tl);
1092 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
1094
1095 /**
1096 * Get the line number corresponding to the specified vertical position.
1097 * If you ask for a position above 0, you get 0; if you ask for a position
1098 * below the bottom of the text, you get the last line.
1099 */
1100 // FIXME: It may be faster to do a linear search for layouts without many lines.
1101 public int getLineForVertical(int vertical) {
1102 int high = getLineCount(), low = -1, guess;
1103
1104 while (high - low > 1) {
1105 guess = (high + low) / 2;
1106
1107 if (getLineTop(guess) > vertical)
1108 high = guess;
1109 else
1110 low = guess;
1111 }
1112
1113 if (low < 0)
1114 return 0;
1115 else
1116 return low;
1117 }
1118
1119 /**
1120 * Get the line number on which the specified text offset appears.
1121 * If you ask for a position before 0, you get 0; if you ask for a position
1122 * beyond the end of the text, you get the last line.
1123 */
1124 public int getLineForOffset(int offset) {
1125 int high = getLineCount(), low = -1, guess;
1126
1127 while (high - low > 1) {
1128 guess = (high + low) / 2;
1129
1130 if (getLineStart(guess) > offset)
1131 high = guess;
1132 else
1133 low = guess;
1134 }
1135
1136 if (low < 0)
1137 return 0;
1138 else
1139 return low;
1140 }
1141
1142 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001143 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 * closest to the specified horizontal position.
1145 */
1146 public int getOffsetForHorizontal(int line, float horiz) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001147 return getOffsetForHorizontal(line, horiz, true);
1148 }
1149
1150 /**
1151 * Get the character offset on the specified line whose position is
1152 * closest to the specified horizontal position.
1153 *
1154 * @param line the line used to find the closest offset
1155 * @param horiz the horizontal position used to find the closest offset
1156 * @param primary whether to use the primary position or secondary position to find the offset
1157 *
1158 * @hide
1159 */
1160 public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
Raph Levienedb27f12015-06-01 14:34:47 -07001161 // TODO: use Paint.getOffsetForAdvance to avoid binary search
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001162 final int lineEndOffset = getLineEnd(line);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001163 final int lineStartOffset = getLineStart(line);
1164
1165 Directions dirs = getLineDirections(line);
1166
1167 TextLine tl = TextLine.obtain();
1168 // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
1169 tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
1170 false, null);
1171
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001172 final int max;
1173 if (line == getLineCount() - 1) {
1174 max = lineEndOffset;
1175 } else {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001176 max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
1177 !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001178 }
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001179 int best = lineStartOffset;
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001180 float bestdist = Math.abs(getHorizontal(best, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181
Doug Felt9f7a4442010-03-01 12:45:56 -08001182 for (int i = 0; i < dirs.mDirections.length; i += 2) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001183 int here = lineStartOffset + dirs.mDirections[i];
Doug Felt9f7a4442010-03-01 12:45:56 -08001184 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001185 boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
1186 int swap = isRtl ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187
1188 if (there > max)
1189 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 int high = there - 1 + 1, low = here + 1 - 1, guess;
1191
1192 while (high - low > 1) {
1193 guess = (high + low) / 2;
1194 int adguess = getOffsetAtStartOf(guess);
1195
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001196 if (getHorizontal(adguess, primary) * swap >= horiz * swap)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 high = guess;
1198 else
1199 low = guess;
1200 }
1201
1202 if (low < here + 1)
1203 low = here + 1;
1204
1205 if (low < there) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001206 int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
1207 low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
1208 if (low >= here && low < there) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001209 float dist = Math.abs(getHorizontal(low, primary) - horiz);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001210 if (aft < there) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001211 float other = Math.abs(getHorizontal(aft, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001213 if (other < dist) {
1214 dist = other;
1215 low = aft;
1216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001219 if (dist < bestdist) {
1220 bestdist = dist;
1221 best = low;
1222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
1224 }
1225
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001226 float dist = Math.abs(getHorizontal(here, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227
1228 if (dist < bestdist) {
1229 bestdist = dist;
1230 best = here;
1231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001234 float dist = Math.abs(getHorizontal(max, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235
Raph Levien373b7a82013-09-20 15:11:52 -07001236 if (dist <= bestdist) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 bestdist = dist;
1238 best = max;
1239 }
1240
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001241 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 return best;
1243 }
1244
1245 /**
1246 * Return the text offset after the last character on the specified line.
1247 */
1248 public final int getLineEnd(int line) {
1249 return getLineStart(line + 1);
1250 }
1251
Doug Felt9f7a4442010-03-01 12:45:56 -08001252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 * Return the text offset after the last visible character (so whitespace
1254 * is not counted) on the specified line.
1255 */
1256 public int getLineVisibleEnd(int line) {
1257 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1258 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 CharSequence text = mText;
1262 char ch;
1263 if (line == getLineCount() - 1) {
1264 return end;
1265 }
1266
1267 for (; end > start; end--) {
1268 ch = text.charAt(end - 1);
1269
1270 if (ch == '\n') {
1271 return end - 1;
1272 }
1273
Raph Levienc94f7422015-03-06 19:19:48 -08001274 // Note: keep this in sync with Minikin LineBreaker::isLineEndSpace()
1275 if (!(ch == ' ' || ch == '\t' || ch == 0x1680 ||
1276 (0x2000 <= ch && ch <= 0x200A && ch != 0x2007) ||
1277 ch == 0x205F || ch == 0x3000)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 break;
1279 }
1280
1281 }
1282
1283 return end;
1284 }
1285
1286 /**
1287 * Return the vertical position of the bottom of the specified line.
1288 */
1289 public final int getLineBottom(int line) {
1290 return getLineTop(line + 1);
1291 }
1292
1293 /**
1294 * Return the vertical position of the baseline of the specified line.
1295 */
1296 public final int getLineBaseline(int line) {
1297 // getLineTop(line+1) == getLineTop(line)
1298 return getLineTop(line+1) - getLineDescent(line);
1299 }
1300
1301 /**
1302 * Get the ascent of the text on the specified line.
1303 * The return value is negative to match the Paint.ascent() convention.
1304 */
1305 public final int getLineAscent(int line) {
1306 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1307 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1308 }
1309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001311 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 }
1313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001315 return getOffsetToLeftRightOf(offset, false);
1316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317
Doug Felt9f7a4442010-03-01 12:45:56 -08001318 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1319 int line = getLineForOffset(caret);
1320 int lineStart = getLineStart(line);
1321 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001322 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001324 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001325 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001326 // if walking off line, look at the line we're headed to
1327 if (advance) {
1328 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001329 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001330 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001331 ++line;
1332 } else {
1333 return caret; // at very end, don't move
1334 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001335 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001336 } else {
1337 if (caret == lineStart) {
1338 if (line > 0) {
1339 lineChanged = true;
1340 --line;
1341 } else {
1342 return caret; // at very start, don't move
1343 }
1344 }
1345 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001346
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001347 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001348 lineStart = getLineStart(line);
1349 lineEnd = getLineEnd(line);
1350 int newDir = getParagraphDirection(line);
1351 if (newDir != lineDir) {
1352 // unusual case. we want to walk onto the line, but it runs
1353 // in a different direction than this one, so we fake movement
1354 // in the opposite direction.
1355 toLeft = !toLeft;
1356 lineDir = newDir;
1357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001359
Doug Felte8e45f22010-03-29 14:58:40 -07001360 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001361
Doug Felte8e45f22010-03-29 14:58:40 -07001362 TextLine tl = TextLine.obtain();
1363 // XXX: we don't care about tabs
1364 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1365 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1366 tl = TextLine.recycle(tl);
1367 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 }
1369
1370 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001371 // XXX this probably should skip local reorderings and
1372 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 if (offset == 0)
1374 return 0;
1375
1376 CharSequence text = mText;
1377 char c = text.charAt(offset);
1378
1379 if (c >= '\uDC00' && c <= '\uDFFF') {
1380 char c1 = text.charAt(offset - 1);
1381
1382 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1383 offset -= 1;
1384 }
1385
1386 if (mSpannedText) {
1387 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1388 ReplacementSpan.class);
1389
1390 for (int i = 0; i < spans.length; i++) {
1391 int start = ((Spanned) text).getSpanStart(spans[i]);
1392 int end = ((Spanned) text).getSpanEnd(spans[i]);
1393
1394 if (start < offset && end > offset)
1395 offset = start;
1396 }
1397 }
1398
1399 return offset;
1400 }
1401
1402 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001403 * Determine whether we should clamp cursor position. Currently it's
1404 * only robust for left-aligned displays.
1405 * @hide
1406 */
1407 public boolean shouldClampCursor(int line) {
1408 // Only clamp cursor position in left-aligned displays.
1409 switch (getParagraphAlignment(line)) {
1410 case ALIGN_LEFT:
1411 return true;
1412 case ALIGN_NORMAL:
1413 return getParagraphDirection(line) > 0;
1414 default:
1415 return false;
1416 }
1417
1418 }
1419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 * Fills in the specified Path with a representation of a cursor
1421 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001422 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 * directionalities.
1424 */
1425 public void getCursorPath(int point, Path dest,
1426 CharSequence editingBuffer) {
1427 dest.reset();
1428
1429 int line = getLineForOffset(point);
1430 int top = getLineTop(line);
1431 int bottom = getLineTop(line+1);
1432
Raph Levienafe8e9b2012-12-19 16:09:32 -08001433 boolean clamped = shouldClampCursor(line);
1434 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1435 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436
Jeff Brown497a92c2010-09-12 17:55:08 -07001437 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1438 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1439 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 int dist = 0;
1441
1442 if (caps != 0 || fn != 0) {
1443 dist = (bottom - top) >> 2;
1444
1445 if (fn != 0)
1446 top += dist;
1447 if (caps != 0)
1448 bottom -= dist;
1449 }
1450
1451 if (h1 < 0.5f)
1452 h1 = 0.5f;
1453 if (h2 < 0.5f)
1454 h2 = 0.5f;
1455
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001456 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 dest.moveTo(h1, top);
1458 dest.lineTo(h1, bottom);
1459 } else {
1460 dest.moveTo(h1, top);
1461 dest.lineTo(h1, (top + bottom) >> 1);
1462
1463 dest.moveTo(h2, (top + bottom) >> 1);
1464 dest.lineTo(h2, bottom);
1465 }
1466
1467 if (caps == 2) {
1468 dest.moveTo(h2, bottom);
1469 dest.lineTo(h2 - dist, bottom + dist);
1470 dest.lineTo(h2, bottom);
1471 dest.lineTo(h2 + dist, bottom + dist);
1472 } else if (caps == 1) {
1473 dest.moveTo(h2, bottom);
1474 dest.lineTo(h2 - dist, bottom + dist);
1475
1476 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1477 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1478
1479 dest.moveTo(h2 + dist, bottom + dist);
1480 dest.lineTo(h2, bottom);
1481 }
1482
1483 if (fn == 2) {
1484 dest.moveTo(h1, top);
1485 dest.lineTo(h1 - dist, top - dist);
1486 dest.lineTo(h1, top);
1487 dest.lineTo(h1 + dist, top - dist);
1488 } else if (fn == 1) {
1489 dest.moveTo(h1, top);
1490 dest.lineTo(h1 - dist, top - dist);
1491
1492 dest.moveTo(h1 - dist, top - dist + 0.5f);
1493 dest.lineTo(h1 + dist, top - dist + 0.5f);
1494
1495 dest.moveTo(h1 + dist, top - dist);
1496 dest.lineTo(h1, top);
1497 }
1498 }
1499
1500 private void addSelection(int line, int start, int end,
1501 int top, int bottom, Path dest) {
1502 int linestart = getLineStart(line);
1503 int lineend = getLineEnd(line);
1504 Directions dirs = getLineDirections(line);
1505
1506 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1507 lineend--;
1508
Doug Felt9f7a4442010-03-01 12:45:56 -08001509 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1510 int here = linestart + dirs.mDirections[i];
1511 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 if (there > lineend)
1514 there = lineend;
1515
1516 if (start <= there && end >= here) {
1517 int st = Math.max(start, here);
1518 int en = Math.min(end, there);
1519
1520 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001521 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1522 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001524 float left = Math.min(h1, h2);
1525 float right = Math.max(h1, h2);
1526
1527 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 }
1529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 }
1531 }
1532
1533 /**
1534 * Fills in the specified Path with a representation of a highlight
1535 * between the specified offsets. This will often be a rectangle
1536 * or a potentially discontinuous set of rectangles. If the start
1537 * and end are the same, the returned path is empty.
1538 */
1539 public void getSelectionPath(int start, int end, Path dest) {
1540 dest.reset();
1541
1542 if (start == end)
1543 return;
1544
1545 if (end < start) {
1546 int temp = end;
1547 end = start;
1548 start = temp;
1549 }
1550
1551 int startline = getLineForOffset(start);
1552 int endline = getLineForOffset(end);
1553
1554 int top = getLineTop(startline);
1555 int bottom = getLineBottom(endline);
1556
1557 if (startline == endline) {
1558 addSelection(startline, start, end, top, bottom, dest);
1559 } else {
1560 final float width = mWidth;
1561
1562 addSelection(startline, start, getLineEnd(startline),
1563 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1566 dest.addRect(getLineLeft(startline), top,
1567 0, getLineBottom(startline), Path.Direction.CW);
1568 else
1569 dest.addRect(getLineRight(startline), top,
1570 width, getLineBottom(startline), Path.Direction.CW);
1571
1572 for (int i = startline + 1; i < endline; i++) {
1573 top = getLineTop(i);
1574 bottom = getLineBottom(i);
1575 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1576 }
1577
1578 top = getLineTop(endline);
1579 bottom = getLineBottom(endline);
1580
1581 addSelection(endline, getLineStart(endline), end,
1582 top, bottom, dest);
1583
1584 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1585 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1586 else
1587 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1588 }
1589 }
1590
1591 /**
1592 * Get the alignment of the specified paragraph, taking into account
1593 * markup attached to it.
1594 */
1595 public final Alignment getParagraphAlignment(int line) {
1596 Alignment align = mAlignment;
1597
1598 if (mSpannedText) {
1599 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001600 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 getLineEnd(line),
1602 AlignmentSpan.class);
1603
1604 int spanLength = spans.length;
1605 if (spanLength > 0) {
1606 align = spans[spanLength-1].getAlignment();
1607 }
1608 }
1609
1610 return align;
1611 }
1612
1613 /**
1614 * Get the left edge of the specified paragraph, inset by left margins.
1615 */
1616 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001618 int dir = getParagraphDirection(line);
1619 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1620 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 }
Doug Feltc982f602010-05-25 11:51:40 -07001622 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
1624
1625 /**
1626 * Get the right edge of the specified paragraph, inset by right margins.
1627 */
1628 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001630 int dir = getParagraphDirection(line);
1631 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1632 return right; // leading margin has no impact, or no styles
1633 }
1634 return right - getParagraphLeadingMargin(line);
1635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636
Doug Feltc982f602010-05-25 11:51:40 -07001637 /**
1638 * Returns the effective leading margin (unsigned) for this line,
1639 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1640 * @param line the line index
1641 * @return the leading margin of this line
1642 */
1643 private int getParagraphLeadingMargin(int line) {
1644 if (!mSpannedText) {
1645 return 0;
1646 }
1647 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001648
Doug Feltc982f602010-05-25 11:51:40 -07001649 int lineStart = getLineStart(line);
1650 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001651 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001652 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001653 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001654 LeadingMarginSpan.class);
1655 if (spans.length == 0) {
1656 return 0; // no leading margin span;
1657 }
Doug Felt0c702b82010-05-14 10:55:42 -07001658
Doug Feltc982f602010-05-25 11:51:40 -07001659 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001660
1661 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001662 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001663
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001664 boolean useFirstLineMargin = isFirstParaLine;
1665 for (int i = 0; i < spans.length; i++) {
1666 if (spans[i] instanceof LeadingMarginSpan2) {
1667 int spStart = spanned.getSpanStart(spans[i]);
1668 int spanLine = getLineForOffset(spStart);
1669 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1670 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1671 useFirstLineMargin |= line < spanLine + count;
1672 }
1673 }
Doug Feltc982f602010-05-25 11:51:40 -07001674 for (int i = 0; i < spans.length; i++) {
1675 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001676 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678
Doug Feltc982f602010-05-25 11:51:40 -07001679 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
1681
Doug Felte8e45f22010-03-29 14:58:40 -07001682 /* package */
Gilles Debunne6c488de2012-03-01 16:20:35 -08001683 static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
Doug Felte8e45f22010-03-29 14:58:40 -07001684
1685 MeasuredText mt = MeasuredText.obtain();
1686 TextLine tl = TextLine.obtain();
1687 try {
Raph Levien70616ec2015-03-04 10:41:30 -08001688 mt.setPara(text, start, end, TextDirectionHeuristics.LTR, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001689 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001690 int dir;
1691 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001692 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001693 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001694 } else {
1695 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1696 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001697 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
Doug Feltc982f602010-05-25 11:51:40 -07001699 char[] chars = mt.mChars;
1700 int len = mt.mLen;
1701 boolean hasTabs = false;
1702 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001703 // leading margins should be taken into account when measuring a paragraph
1704 int margin = 0;
1705 if (text instanceof Spanned) {
1706 Spanned spanned = (Spanned) text;
1707 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1708 LeadingMarginSpan.class);
1709 for (LeadingMarginSpan lms : spans) {
1710 margin += lms.getLeadingMargin(true);
1711 }
1712 }
Doug Feltc982f602010-05-25 11:51:40 -07001713 for (int i = 0; i < len; ++i) {
1714 if (chars[i] == '\t') {
1715 hasTabs = true;
1716 if (text instanceof Spanned) {
1717 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001718 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001719 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001720 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001721 TabStopSpan.class);
1722 if (spans.length > 0) {
1723 tabStops = new TabStops(TAB_INCREMENT, spans);
1724 }
1725 }
1726 break;
1727 }
1728 }
1729 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001730 return margin + tl.metrics(null);
Doug Felte8e45f22010-03-29 14:58:40 -07001731 } finally {
1732 TextLine.recycle(tl);
1733 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736
Doug Felt71b8dd72010-02-16 17:27:09 -08001737 /**
Doug Feltc982f602010-05-25 11:51:40 -07001738 * @hide
1739 */
1740 /* package */ static class TabStops {
1741 private int[] mStops;
1742 private int mNumStops;
1743 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001744
Doug Feltc982f602010-05-25 11:51:40 -07001745 TabStops(int increment, Object[] spans) {
1746 reset(increment, spans);
1747 }
Doug Felt0c702b82010-05-14 10:55:42 -07001748
Doug Feltc982f602010-05-25 11:51:40 -07001749 void reset(int increment, Object[] spans) {
1750 this.mIncrement = increment;
1751
1752 int ns = 0;
1753 if (spans != null) {
1754 int[] stops = this.mStops;
1755 for (Object o : spans) {
1756 if (o instanceof TabStopSpan) {
1757 if (stops == null) {
1758 stops = new int[10];
1759 } else if (ns == stops.length) {
1760 int[] nstops = new int[ns * 2];
1761 for (int i = 0; i < ns; ++i) {
1762 nstops[i] = stops[i];
1763 }
1764 stops = nstops;
1765 }
1766 stops[ns++] = ((TabStopSpan) o).getTabStop();
1767 }
1768 }
1769 if (ns > 1) {
1770 Arrays.sort(stops, 0, ns);
1771 }
1772 if (stops != this.mStops) {
1773 this.mStops = stops;
1774 }
1775 }
1776 this.mNumStops = ns;
1777 }
Doug Felt0c702b82010-05-14 10:55:42 -07001778
Doug Feltc982f602010-05-25 11:51:40 -07001779 float nextTab(float h) {
1780 int ns = this.mNumStops;
1781 if (ns > 0) {
1782 int[] stops = this.mStops;
1783 for (int i = 0; i < ns; ++i) {
1784 int stop = stops[i];
1785 if (stop > h) {
1786 return stop;
1787 }
1788 }
1789 }
1790 return nextDefaultStop(h, mIncrement);
1791 }
1792
1793 public static float nextDefaultStop(float h, int inc) {
1794 return ((int) ((h + inc) / inc)) * inc;
1795 }
1796 }
Doug Felt0c702b82010-05-14 10:55:42 -07001797
Doug Feltc982f602010-05-25 11:51:40 -07001798 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001799 * Returns the position of the next tab stop after h on the line.
1800 *
1801 * @param text the text
1802 * @param start start of the line
1803 * @param end limit of the line
1804 * @param h the current horizontal offset
1805 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1806 * on the line will be used. If there are no tabs, a default offset
1807 * will be used to compute the tab stop.
1808 * @return the offset of the next tab stop.
1809 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 /* package */ static float nextTab(CharSequence text, int start, int end,
1811 float h, Object[] tabs) {
1812 float nh = Float.MAX_VALUE;
1813 boolean alltabs = false;
1814
1815 if (text instanceof Spanned) {
1816 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001817 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 alltabs = true;
1819 }
1820
1821 for (int i = 0; i < tabs.length; i++) {
1822 if (!alltabs) {
1823 if (!(tabs[i] instanceof TabStopSpan))
1824 continue;
1825 }
1826
1827 int where = ((TabStopSpan) tabs[i]).getTabStop();
1828
1829 if (where < nh && where > h)
1830 nh = where;
1831 }
1832
1833 if (nh != Float.MAX_VALUE)
1834 return nh;
1835 }
1836
1837 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1838 }
1839
1840 protected final boolean isSpanned() {
1841 return mSpannedText;
1842 }
1843
Eric Fischer74d31ef2010-08-05 15:29:36 -07001844 /**
1845 * Returns the same as <code>text.getSpans()</code>, except where
1846 * <code>start</code> and <code>end</code> are the same and are not
1847 * at the very beginning of the text, in which case an empty array
1848 * is returned instead.
1849 * <p>
1850 * This is needed because of the special case that <code>getSpans()</code>
1851 * on an empty range returns the spans adjacent to that range, which is
1852 * primarily for the sake of <code>TextWatchers</code> so they will get
1853 * notifications when text goes from empty to non-empty. But it also
1854 * has the unfortunate side effect that if the text ends with an empty
1855 * paragraph, that paragraph accidentally picks up the styles of the
1856 * preceding paragraph (even though those styles will not be picked up
1857 * by new text that is inserted into the empty paragraph).
1858 * <p>
1859 * The reason it just checks whether <code>start</code> and <code>end</code>
1860 * is the same is that the only time a line can contain 0 characters
1861 * is if it is the final paragraph of the Layout; otherwise any line will
1862 * contain at least one printing or newline character. The reason for the
1863 * additional check if <code>start</code> is greater than 0 is that
1864 * if the empty paragraph is the entire content of the buffer, paragraph
1865 * styles that are already applied to the buffer will apply to text that
1866 * is inserted into it.
1867 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07001868 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001869 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08001870 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001871 }
1872
Siyamed Sinirfa05ba02016-01-12 10:54:43 -08001873 if(text instanceof SpannableStringBuilder) {
1874 return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
1875 } else {
1876 return text.getSpans(start, end, type);
1877 }
Eric Fischer74d31ef2010-08-05 15:29:36 -07001878 }
1879
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001880 private char getEllipsisChar(TextUtils.TruncateAt method) {
1881 return (method == TextUtils.TruncateAt.END_SMALL) ?
Neil Fullerd29bdb22015-02-06 10:03:08 +00001882 TextUtils.ELLIPSIS_TWO_DOTS[0] :
1883 TextUtils.ELLIPSIS_NORMAL[0];
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001884 }
1885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001887 char[] dest, int destoff, TextUtils.TruncateAt method) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 int ellipsisCount = getEllipsisCount(line);
1889
1890 if (ellipsisCount == 0) {
1891 return;
1892 }
1893
1894 int ellipsisStart = getEllipsisStart(line);
1895 int linestart = getLineStart(line);
1896
1897 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1898 char c;
1899
1900 if (i == ellipsisStart) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001901 c = getEllipsisChar(method); // ellipsis
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 } else {
1903 c = '\uFEFF'; // 0-width space
1904 }
1905
1906 int a = i + linestart;
1907
1908 if (a >= start && a < end) {
1909 dest[destoff + a - start] = c;
1910 }
1911 }
1912 }
1913
1914 /**
1915 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08001916 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 */
1918 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08001919 // Directions represents directional runs within a line of text.
1920 // Runs are pairs of ints listed in visual order, starting from the
1921 // leading margin. The first int of each pair is the offset from
1922 // the first character of the line to the start of the run. The
1923 // second int represents both the length and level of the run.
1924 // The length is in the lower bits, accessed by masking with
1925 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1926 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1927 // To simply test for an RTL direction, test the bit using
1928 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929
Siyamed Sinired09ae12016-02-16 14:36:26 -08001930 /**
1931 * @hide
1932 */
1933 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1934 public int[] mDirections;
1935
1936 /**
1937 * @hide
1938 */
1939 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1940 public Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 mDirections = dirs;
1942 }
1943 }
1944
1945 /**
1946 * Return the offset of the first character to be ellipsized away,
1947 * relative to the start of the line. (So 0 if the beginning of the
1948 * line is ellipsized, not getLineStart().)
1949 */
1950 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07001951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 /**
1953 * Returns the number of characters to be ellipsized away, or 0 if
1954 * no ellipsis is to take place.
1955 */
1956 public abstract int getEllipsisCount(int line);
1957
1958 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1959 /* package */ CharSequence mText;
1960 /* package */ Layout mLayout;
1961 /* package */ int mWidth;
1962 /* package */ TextUtils.TruncateAt mMethod;
1963
1964 public Ellipsizer(CharSequence s) {
1965 mText = s;
1966 }
1967
1968 public char charAt(int off) {
1969 char[] buf = TextUtils.obtain(1);
1970 getChars(off, off + 1, buf, 0);
1971 char ret = buf[0];
1972
1973 TextUtils.recycle(buf);
1974 return ret;
1975 }
1976
1977 public void getChars(int start, int end, char[] dest, int destoff) {
1978 int line1 = mLayout.getLineForOffset(start);
1979 int line2 = mLayout.getLineForOffset(end);
1980
1981 TextUtils.getChars(mText, start, end, dest, destoff);
1982
1983 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001984 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986 }
1987
1988 public int length() {
1989 return mText.length();
1990 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 public CharSequence subSequence(int start, int end) {
1993 char[] s = new char[end - start];
1994 getChars(start, end, s, 0);
1995 return new String(s);
1996 }
1997
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001998 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 public String toString() {
2000 char[] s = new char[length()];
2001 getChars(0, length(), s, 0);
2002 return new String(s);
2003 }
2004
2005 }
2006
Gilles Debunne6c488de2012-03-01 16:20:35 -08002007 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 private Spanned mSpanned;
2009
2010 public SpannedEllipsizer(CharSequence display) {
2011 super(display);
2012 mSpanned = (Spanned) display;
2013 }
2014
2015 public <T> T[] getSpans(int start, int end, Class<T> type) {
2016 return mSpanned.getSpans(start, end, type);
2017 }
2018
2019 public int getSpanStart(Object tag) {
2020 return mSpanned.getSpanStart(tag);
2021 }
2022
2023 public int getSpanEnd(Object tag) {
2024 return mSpanned.getSpanEnd(tag);
2025 }
2026
2027 public int getSpanFlags(Object tag) {
2028 return mSpanned.getSpanFlags(tag);
2029 }
2030
Gilles Debunne6c488de2012-03-01 16:20:35 -08002031 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 public int nextSpanTransition(int start, int limit, Class type) {
2033 return mSpanned.nextSpanTransition(start, limit, type);
2034 }
2035
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002036 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 public CharSequence subSequence(int start, int end) {
2038 char[] s = new char[end - start];
2039 getChars(start, end, s, 0);
2040
2041 SpannableString ss = new SpannableString(new String(s));
2042 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
2043 return ss;
2044 }
2045 }
2046
2047 private CharSequence mText;
2048 private TextPaint mPaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 private int mWidth;
2050 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
2051 private float mSpacingMult;
2052 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002053 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002055 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002056 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057
2058 public static final int DIR_LEFT_TO_RIGHT = 1;
2059 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002060
Doug Felt20178d62010-02-22 13:39:01 -08002061 /* package */ static final int DIR_REQUEST_LTR = 1;
2062 /* package */ static final int DIR_REQUEST_RTL = -1;
2063 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2064 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065
Doug Felt9f7a4442010-03-01 12:45:56 -08002066 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2067 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2068 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2069 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 public enum Alignment {
2072 ALIGN_NORMAL,
2073 ALIGN_OPPOSITE,
2074 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002075 /** @hide */
2076 ALIGN_LEFT,
2077 /** @hide */
2078 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 }
2080
2081 private static final int TAB_INCREMENT = 20;
2082
Siyamed Sinired09ae12016-02-16 14:36:26 -08002083 /** @hide */
2084 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2085 public static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002086 new Directions(new int[] { 0, RUN_LENGTH_MASK });
Siyamed Sinired09ae12016-02-16 14:36:26 -08002087
2088 /** @hide */
2089 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2090 public static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002091 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093}