blob: 9761a0c3ecf9b5420bd4ad37319a078047acee10 [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
Doug Feltcb3791202011-07-07 11:57:48 -070033import com.android.internal.util.ArrayUtils;
Adam Lesinski776abc22014-03-07 11:30:59 -050034import com.android.internal.util.GrowingArrayUtils;
Doug Feltcb3791202011-07-07 11:57:48 -070035
Raph Levien39b4db72015-03-25 13:18:20 -070036import java.lang.annotation.Retention;
37import java.lang.annotation.RetentionPolicy;
Doug Feltc982f602010-05-25 11:51:40 -070038import java.util.Arrays;
Doug Felt9f7a4442010-03-01 12:45:56 -080039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040/**
Doug Felt9f7a4442010-03-01 12:45:56 -080041 * A base class that manages text layout in visual elements on
42 * the screen.
43 * <p>For text that will be edited, use a {@link DynamicLayout},
44 * which will be updated as the text changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 * For text that will not change, use a {@link StaticLayout}.
46 */
47public abstract class Layout {
Raph Levien39b4db72015-03-25 13:18:20 -070048 /** @hide */
49 @IntDef({BREAK_STRATEGY_SIMPLE, BREAK_STRATEGY_HIGH_QUALITY, BREAK_STRATEGY_BALANCED})
50 @Retention(RetentionPolicy.SOURCE)
51 public @interface BreakStrategy {}
52
53 /**
54 * Value for break strategy indicating simple line breaking. Automatic hyphens are not added
55 * (though soft hyphens are respected), and modifying text generally doesn't affect the layout
56 * before it (which yields a more consistent user experience when editing), but layout may not
57 * be the highest quality.
58 */
59 public static final int BREAK_STRATEGY_SIMPLE = 0;
60
61 /**
62 * Value for break strategy indicating high quality line breaking, including automatic
63 * hyphenation and doing whole-paragraph optimization of line breaks.
64 */
65 public static final int BREAK_STRATEGY_HIGH_QUALITY = 1;
66
67 /**
68 * Value for break strategy indicating balanced line breaking. The breaks are chosen to
69 * make all lines as close to the same length as possible, including automatic hyphenation.
70 */
71 public static final int BREAK_STRATEGY_BALANCED = 2;
72
Roozbeh Pournader95c7a132015-05-12 12:01:06 -070073 /** @hide */
74 @IntDef({HYPHENATION_FREQUENCY_NORMAL, HYPHENATION_FREQUENCY_FULL,
75 HYPHENATION_FREQUENCY_NONE})
76 @Retention(RetentionPolicy.SOURCE)
77 public @interface HyphenationFrequency {}
78
79 /**
80 * Value for hyphenation frequency indicating no automatic hyphenation. Useful
81 * for backward compatibility, and for cases where the automatic hyphenation algorithm results
82 * in incorrect hyphenation. Mid-word breaks may still happen when a word is wider than the
83 * layout and there is otherwise no valid break. Soft hyphens are ignored and will not be used
84 * as suggestions for potential line breaks.
85 */
86 public static final int HYPHENATION_FREQUENCY_NONE = 0;
87
88 /**
89 * Value for hyphenation frequency indicating a light amount of automatic hyphenation, which
90 * is a conservative default. Useful for informal cases, such as short sentences or chat
91 * messages.
92 */
93 public static final int HYPHENATION_FREQUENCY_NORMAL = 1;
94
95 /**
96 * Value for hyphenation frequency indicating the full amount of automatic hyphenation, typical
97 * in typography. Useful for running text and where it's important to put the maximum amount of
98 * text in a screen with limited space.
99 */
100 public static final int HYPHENATION_FREQUENCY_FULL = 2;
101
Doug Felt71b8dd72010-02-16 17:27:09 -0800102 private static final ParagraphStyle[] NO_PARA_SPANS =
103 ArrayUtils.emptyArray(ParagraphStyle.class);
Dave Bort76c02262009-04-13 17:17:21 -0700104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800106 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * specified text with one line per paragraph.
108 */
109 public static float getDesiredWidth(CharSequence source,
110 TextPaint paint) {
111 return getDesiredWidth(source, 0, source.length(), paint);
112 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800115 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * specified text slice with one line per paragraph.
117 */
118 public static float getDesiredWidth(CharSequence source,
119 int start, int end,
120 TextPaint paint) {
121 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123 int next;
124 for (int i = start; i <= end; i = next) {
125 next = TextUtils.indexOf(source, '\n', i, end);
126
127 if (next < 0)
128 next = end;
129
Doug Felt71b8dd72010-02-16 17:27:09 -0800130 // note, omits trailing paragraph char
Gilles Debunne6c488de2012-03-01 16:20:35 -0800131 float w = measurePara(paint, source, i, next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 if (w > need)
134 need = w;
135
136 next++;
137 }
138
139 return need;
140 }
141
142 /**
143 * Subclasses of Layout use this constructor to set the display text,
144 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800145 * @param text the text to render
146 * @param paint the default paint for the layout. Styles can override
147 * various attributes of the paint.
148 * @param width the wrapping width for the text.
149 * @param align whether to left, right, or center the text. Styles can
150 * override the alignment.
151 * @param spacingMult factor by which to scale the font size to get the
152 * default line spacing
153 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 */
155 protected Layout(CharSequence text, TextPaint paint,
156 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800157 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700158 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
159 spacingMult, spacingAdd);
160 }
161
162 /**
163 * Subclasses of Layout use this constructor to set the display text,
164 * width, and other standard properties.
165 * @param text the text to render
166 * @param paint the default paint for the layout. Styles can override
167 * various attributes of the paint.
168 * @param width the wrapping width for the text.
169 * @param align whether to left, right, or center the text. Styles can
170 * override the alignment.
171 * @param spacingMult factor by which to scale the font size to get the
172 * default line spacing
173 * @param spacingAdd amount to add to the default line spacing
174 *
175 * @hide
176 */
177 protected Layout(CharSequence text, TextPaint paint,
178 int width, Alignment align, TextDirectionHeuristic textDir,
179 float spacingMult, float spacingAdd) {
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 if (width < 0)
182 throw new IllegalArgumentException("Layout: " + width + " < 0");
183
Doug Felte8e45f22010-03-29 14:58:40 -0700184 // Ensure paint doesn't have baselineShift set.
185 // While normally we don't modify the paint the user passed in,
186 // we were already doing this in Styled.drawUniformRun with both
187 // baselineShift and bgColor. We probably should reevaluate bgColor.
188 if (paint != null) {
189 paint.bgColor = 0;
190 paint.baselineShift = 0;
191 }
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 mText = text;
194 mPaint = paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 mWidth = width;
196 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800197 mSpacingMult = spacingMult;
198 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700200 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 }
202
203 /**
204 * Replace constructor properties of this Layout with new ones. Be careful.
205 */
206 /* package */ void replaceWith(CharSequence text, TextPaint paint,
207 int width, Alignment align,
208 float spacingmult, float spacingadd) {
209 if (width < 0) {
210 throw new IllegalArgumentException("Layout: " + width + " < 0");
211 }
212
213 mText = text;
214 mPaint = paint;
215 mWidth = width;
216 mAlignment = align;
217 mSpacingMult = spacingmult;
218 mSpacingAdd = spacingadd;
219 mSpannedText = text instanceof Spanned;
220 }
221
222 /**
223 * Draw this Layout on the specified Canvas.
224 */
225 public void draw(Canvas c) {
226 draw(c, null, null, 0);
227 }
228
229 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800230 * Draw this Layout on the specified canvas, with the highlight path drawn
231 * between the background and the text.
232 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800233 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800234 * @param highlight the path of the highlight or cursor; can be null
235 * @param highlightPaint the paint for the highlight
236 * @param cursorOffsetVertical the amount to temporarily translate the
237 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800239 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
240 int cursorOffsetVertical) {
241 final long lineRange = getLineRangeForDraw(canvas);
242 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
243 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
244 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
Gilles Debunne6c488de2012-03-01 16:20:35 -0800246 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
247 firstLine, lastLine);
248 drawText(canvas, firstLine, lastLine);
249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250
Gilles Debunne6c488de2012-03-01 16:20:35 -0800251 /**
252 * @hide
253 */
254 public void drawText(Canvas canvas, int firstLine, int lastLine) {
255 int previousLineBottom = getLineTop(firstLine);
256 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800257 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700258 int spanEnd = 0;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800259 TextPaint paint = mPaint;
260 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700262 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700263 TabStops tabStops = null;
264 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800265
Doug Felte8e45f22010-03-29 14:58:40 -0700266 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700267
Gilles Debunne6c488de2012-03-01 16:20:35 -0800268 // Draw the lines, one at a time.
269 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700270 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700272 previousLineEnd = getLineStart(lineNum + 1);
273 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274
275 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700276 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700278 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279
Raph Levien26d443a2015-03-30 14:18:32 -0700280 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700281 int left = 0;
282 int right = mWidth;
283
Gilles Debunne6c488de2012-03-01 16:20:35 -0800284 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700285 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800286 int textLength = buf.length();
287 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700288
Doug Feltc982f602010-05-25 11:51:40 -0700289 // New batch of paragraph styles, collect into spans array.
290 // Compute the alignment, last alignment style wins.
291 // Reset tabStops, we'll rebuild if we encounter a line with
292 // tabs.
293 // We expect paragraph spans to be relatively infrequent, use
294 // spanEnd so that we can check less frequently. Since
295 // paragraph styles ought to apply to entire paragraphs, we can
296 // just collect the ones present at the start of the paragraph.
297 // If spanEnd is before the end of the paragraph, that's not
298 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700299 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700300 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700302 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800303
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700304 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800305 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700307 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 break;
309 }
310 }
Doug Felt0c702b82010-05-14 10:55:42 -0700311
Doug Feltc982f602010-05-25 11:51:40 -0700312 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800314
Doug Feltc982f602010-05-25 11:51:40 -0700315 // Draw all leading margin spans. Adjust left or right according
316 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700318 boolean useFirstLineMargin = isFirstParaLine;
319 for (int n = 0; n < length; n++) {
320 if (spans[n] instanceof LeadingMarginSpan2) {
321 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
322 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
323 // if there is more than one LeadingMarginSpan2, use
324 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700325 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700326 useFirstLineMargin = true;
327 break;
328 }
329 }
330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 for (int n = 0; n < length; n++) {
332 if (spans[n] instanceof LeadingMarginSpan) {
333 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800335 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800337 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700338 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800340 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800342 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700343 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 }
345 }
346 }
347 }
348
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700349 boolean hasTab = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700350 // Can't tell if we have tabs for sure, currently
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700351 if (hasTab && !tabStopsIsInitialized) {
Doug Feltc982f602010-05-25 11:51:40 -0700352 if (tabStops == null) {
353 tabStops = new TabStops(TAB_INCREMENT, spans);
354 } else {
355 tabStops.reset(TAB_INCREMENT, spans);
356 }
357 tabStopsIsInitialized = true;
358 }
359
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700360 // Determine whether the line aligns to normal, opposite, or center.
361 Alignment align = paraAlign;
362 if (align == Alignment.ALIGN_LEFT) {
363 align = (dir == DIR_LEFT_TO_RIGHT) ?
364 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
365 } else if (align == Alignment.ALIGN_RIGHT) {
366 align = (dir == DIR_LEFT_TO_RIGHT) ?
367 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
368 }
369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 int x;
371 if (align == Alignment.ALIGN_NORMAL) {
372 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700373 x = left + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700375 x = right + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700378 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700380 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700381 x = right - max + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700383 x = left - max + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 }
Doug Feltc982f602010-05-25 11:51:40 -0700385 } else { // Alignment.ALIGN_CENTER
386 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700387 x = ((right + left - max) >> 1) +
388 getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 }
390 }
391
Raph Levien26d443a2015-03-30 14:18:32 -0700392 paint.setHyphenEdit(getHyphen(lineNum));
393 Directions directions = getLineDirections(lineNum);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700394 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800395 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800396 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 } else {
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700398 tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800399 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
Raph Levien9a174dd2015-04-08 13:35:03 -0700401 paint.setHyphenEdit(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 }
Doug Feltc982f602010-05-25 11:51:40 -0700403
Doug Felte8e45f22010-03-29 14:58:40 -0700404 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
406
407 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800408 * @hide
409 */
410 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
411 int cursorOffsetVertical, int firstLine, int lastLine) {
412 // First, draw LineBackgroundSpans.
413 // LineBackgroundSpans know nothing about the alignment, margins, or
414 // direction of the layout or line. XXX: Should they?
415 // They are evaluated at each line.
416 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700417 if (mLineBackgroundSpans == null) {
418 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700419 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800420
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700421 Spanned buffer = (Spanned) mText;
422 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700423 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800424
Gilles Debunneeca5b732012-04-25 18:48:42 -0700425 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700426 int previousLineBottom = getLineTop(firstLine);
427 int previousLineEnd = getLineStart(firstLine);
428 ParagraphStyle[] spans = NO_PARA_SPANS;
429 int spansLength = 0;
430 TextPaint paint = mPaint;
431 int spanEnd = 0;
432 final int width = mWidth;
433 for (int i = firstLine; i <= lastLine; i++) {
434 int start = previousLineEnd;
435 int end = getLineStart(i + 1);
436 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800437
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700438 int ltop = previousLineBottom;
439 int lbottom = getLineTop(i + 1);
440 previousLineBottom = lbottom;
441 int lbaseline = lbottom - getLineDescent(i);
442
443 if (start >= spanEnd) {
444 // These should be infrequent, so we'll use this so that
445 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700446 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700447 // All LineBackgroundSpans on a line contribute to its background.
448 spansLength = 0;
449 // Duplication of the logic of getParagraphSpans
450 if (start != end || start == 0) {
451 // Equivalent to a getSpans(start, end), but filling the 'spans' local
452 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700453 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
454 // equal test is valid since both intervals are not empty by
455 // construction
456 if (mLineBackgroundSpans.spanStarts[j] >= end ||
457 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500458 spans = GrowingArrayUtils.append(
459 spans, spansLength, mLineBackgroundSpans.spans[j]);
460 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700461 }
462 }
463 }
464
465 for (int n = 0; n < spansLength; n++) {
466 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
467 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
468 ltop, lbaseline, lbottom,
469 buffer, start, end, i);
470 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800471 }
472 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700473 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800474 }
475
476 // There can be a highlight even without spans if we are drawing
477 // a non-spanned transformation of a spanned editing buffer.
478 if (highlight != null) {
479 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
480 canvas.drawPath(highlight, highlightPaint);
481 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
482 }
483 }
484
485 /**
486 * @param canvas
487 * @return The range of lines that need to be drawn, possibly empty.
488 * @hide
489 */
490 public long getLineRangeForDraw(Canvas canvas) {
491 int dtop, dbottom;
492
493 synchronized (sTempRect) {
494 if (!canvas.getClipBounds(sTempRect)) {
495 // Negative range end used as a special flag
496 return TextUtils.packRangeInLong(0, -1);
497 }
498
499 dtop = sTempRect.top;
500 dbottom = sTempRect.bottom;
501 }
502
503 final int top = Math.max(dtop, 0);
504 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
505
Gilles Debunne2fba3382012-06-11 17:46:24 -0700506 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800507 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
508 }
509
510 /**
Doug Feltc982f602010-05-25 11:51:40 -0700511 * Return the start position of the line, given the left and right bounds
512 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700513 *
Doug Feltc982f602010-05-25 11:51:40 -0700514 * @param line the line index
515 * @param left the left bounds (0, or leading margin if ltr para)
516 * @param right the right bounds (width, minus leading margin if rtl para)
517 * @return the start position of the line (to right of line if rtl para)
518 */
519 private int getLineStartPos(int line, int left, int right) {
520 // Adjust the point at which to start rendering depending on the
521 // alignment of the paragraph.
522 Alignment align = getParagraphAlignment(line);
523 int dir = getParagraphDirection(line);
524
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700525 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700526 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
527 } else if (align == Alignment.ALIGN_RIGHT) {
528 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
529 }
530
531 int x;
532 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700533 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700534 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700535 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700536 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700537 }
538 } else {
539 TabStops tabStops = null;
540 if (mSpannedText && getLineContainsTab(line)) {
541 Spanned spanned = (Spanned) mText;
542 int start = getLineStart(line);
543 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
544 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800545 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
546 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700547 if (tabSpans.length > 0) {
548 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
549 }
550 }
551 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700552 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700553 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700554 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700555 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700556 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700557 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700558 }
559 } else { // Alignment.ALIGN_CENTER
560 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700561 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700562 }
563 }
564 return x;
565 }
566
567 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * Return the text that is displayed by this Layout.
569 */
570 public final CharSequence getText() {
571 return mText;
572 }
573
574 /**
575 * Return the base Paint properties for this layout.
576 * Do NOT change the paint, which may result in funny
577 * drawing for this layout.
578 */
579 public final TextPaint getPaint() {
580 return mPaint;
581 }
582
583 /**
584 * Return the width of this layout.
585 */
586 public final int getWidth() {
587 return mWidth;
588 }
589
590 /**
591 * Return the width to which this Layout is ellipsizing, or
592 * {@link #getWidth} if it is not doing anything special.
593 */
594 public int getEllipsizedWidth() {
595 return mWidth;
596 }
597
598 /**
599 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800600 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 * it does not cause the text to reflow to use the full new width.
602 */
603 public final void increaseWidthTo(int wid) {
604 if (wid < mWidth) {
605 throw new RuntimeException("attempted to reduce Layout width");
606 }
607
608 mWidth = wid;
609 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 /**
612 * Return the total height of this layout.
613 */
614 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800615 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 }
617
618 /**
Siyamed Sinir0745c722016-05-31 20:39:33 -0700619 * Return the total height of this layout.
620 *
621 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
622 *
623 * @hide
624 */
625 public int getHeight(boolean cap) {
626 return getHeight();
627 }
628
629 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 * Return the base alignment of this layout.
631 */
632 public final Alignment getAlignment() {
633 return mAlignment;
634 }
635
636 /**
637 * Return what the text height is multiplied by to get the line height.
638 */
639 public final float getSpacingMultiplier() {
640 return mSpacingMult;
641 }
642
643 /**
644 * Return the number of units of leading that are added to each line.
645 */
646 public final float getSpacingAdd() {
647 return mSpacingAdd;
648 }
649
650 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700651 * Return the heuristic used to determine paragraph text direction.
652 * @hide
653 */
654 public final TextDirectionHeuristic getTextDirectionHeuristic() {
655 return mTextDir;
656 }
657
658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 * Return the number of lines of text in this layout.
660 */
661 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 /**
664 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
665 * If bounds is not null, return the top, left, right, bottom extents
666 * of the specified line in it.
667 * @param line which line to examine (0..getLineCount() - 1)
668 * @param bounds Optional. If not null, it returns the extent of the line
669 * @return the Y-coordinate of the baseline
670 */
671 public int getLineBounds(int line, Rect bounds) {
672 if (bounds != null) {
673 bounds.left = 0; // ???
674 bounds.top = getLineTop(line);
675 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800676 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 }
678 return getLineBaseline(line);
679 }
680
681 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800682 * Return the vertical position of the top of the specified line
683 * (0&hellip;getLineCount()).
684 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 * bottom of the last line.
686 */
687 public abstract int getLineTop(int line);
688
689 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800690 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 */
692 public abstract int getLineDescent(int line);
693
694 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800695 * Return the text offset of the beginning of the specified line (
696 * 0&hellip;getLineCount()). If the specified line is equal to the line
697 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 */
699 public abstract int getLineStart(int line);
700
701 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800702 * Returns the primary directionality of the paragraph containing the
703 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
704 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 */
706 public abstract int getParagraphDirection(int line);
707
708 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700709 * Returns whether the specified line contains one or more
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700710 * characters that need to be handled specially, like tabs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 */
712 public abstract boolean getLineContainsTab(int line);
713
714 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800715 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 * The array alternates counts of characters in left-to-right
717 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800718 *
719 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public abstract Directions getLineDirections(int line);
722
723 /**
724 * Returns the (negative) number of extra pixels of ascent padding in the
725 * top line of the Layout.
726 */
727 public abstract int getTopPadding();
728
729 /**
730 * Returns the number of extra pixels of descent padding in the
731 * bottom line of the Layout.
732 */
733 public abstract int getBottomPadding();
734
Raph Levien26d443a2015-03-30 14:18:32 -0700735 /**
736 * Returns the hyphen edit for a line.
737 *
738 * @hide
739 */
740 public int getHyphen(int line) {
741 return 0;
742 }
743
Raph Levien2ea52902015-07-01 14:39:31 -0700744 /**
745 * Returns the left indent for a line.
746 *
747 * @hide
748 */
749 public int getIndentAdjust(int line, Alignment alignment) {
750 return 0;
751 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700752
753 /**
754 * Returns true if the character at offset and the preceding character
755 * are at different run levels (and thus there's a split caret).
756 * @param offset the offset
757 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800758 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700759 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800760 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800761 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700762 Directions dirs = getLineDirections(line);
763 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
764 return false;
765 }
766
767 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800768 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700769 int lineEnd = getLineEnd(line);
770 if (offset == lineStart || offset == lineEnd) {
771 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
772 int runIndex = offset == lineStart ? 0 : runs.length - 2;
773 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
774 }
775
776 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800777 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700778 if (offset == runs[i]) {
779 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800780 }
781 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700782 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800783 }
784
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700785 /**
786 * Returns true if the character at offset is right to left (RTL).
787 * @param offset the offset
788 * @return true if the character is RTL, false if it is LTR
789 */
790 public boolean isRtlCharAt(int offset) {
791 int line = getLineForOffset(offset);
792 Directions dirs = getLineDirections(line);
793 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
794 return false;
795 }
796 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
797 return true;
798 }
799 int[] runs = dirs.mDirections;
800 int lineStart = getLineStart(line);
801 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700802 int start = lineStart + runs[i];
803 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
804 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700805 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
806 return ((level & 1) != 0);
807 }
808 }
809 // Should happen only if the offset is "out of bounds"
810 return false;
811 }
812
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900813 /**
814 * Returns the range of the run that the character at offset belongs to.
815 * @param offset the offset
816 * @return The range of the run
817 * @hide
818 */
819 public long getRunRange(int offset) {
820 int line = getLineForOffset(offset);
821 Directions dirs = getLineDirections(line);
822 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
823 return TextUtils.packRangeInLong(0, getLineEnd(line));
824 }
825 int[] runs = dirs.mDirections;
826 int lineStart = getLineStart(line);
827 for (int i = 0; i < runs.length; i += 2) {
828 int start = lineStart + runs[i];
829 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
830 if (offset >= start && offset < limit) {
831 return TextUtils.packRangeInLong(start, limit);
832 }
833 }
834 // Should happen only if the offset is "out of bounds"
835 return TextUtils.packRangeInLong(0, getLineEnd(line));
836 }
837
Doug Felt9f7a4442010-03-01 12:45:56 -0800838 private boolean primaryIsTrailingPrevious(int offset) {
839 int line = getLineForOffset(offset);
840 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700841 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800842 int[] runs = getLineDirections(line).mDirections;
843
844 int levelAt = -1;
845 for (int i = 0; i < runs.length; i += 2) {
846 int start = lineStart + runs[i];
847 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
848 if (limit > lineEnd) {
849 limit = lineEnd;
850 }
851 if (offset >= start && offset < limit) {
852 if (offset > start) {
853 // Previous character is at same level, so don't use trailing.
854 return false;
855 }
856 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
857 break;
858 }
859 }
860 if (levelAt == -1) {
861 // Offset was limit of line.
862 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
863 }
864
865 // At level boundary, check previous level.
866 int levelBefore = -1;
867 if (offset == lineStart) {
868 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
869 } else {
870 offset -= 1;
871 for (int i = 0; i < runs.length; i += 2) {
872 int start = lineStart + runs[i];
873 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
874 if (limit > lineEnd) {
875 limit = lineEnd;
876 }
877 if (offset >= start && offset < limit) {
878 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
879 break;
880 }
881 }
882 }
883
884 return levelBefore < levelAt;
885 }
886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 /**
888 * Get the primary horizontal position for the specified text offset.
889 * This is the location where a new character would be inserted in
890 * the paragraph's primary direction.
891 */
892 public float getPrimaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800893 return getPrimaryHorizontal(offset, false /* not clamped */);
894 }
895
896 /**
897 * Get the primary horizontal position for the specified text offset, but
898 * optionally clamp it so that it doesn't exceed the width of the layout.
899 * @hide
900 */
901 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800902 boolean trailing = primaryIsTrailingPrevious(offset);
Raph Levienafe8e9b2012-12-19 16:09:32 -0800903 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 }
905
906 /**
907 * Get the secondary horizontal position for the specified text offset.
908 * This is the location where a new character would be inserted in
909 * the direction other than the paragraph's primary direction.
910 */
911 public float getSecondaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800912 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914
Raph Levienafe8e9b2012-12-19 16:09:32 -0800915 /**
916 * Get the secondary horizontal position for the specified text offset, but
917 * optionally clamp it so that it doesn't exceed the width of the layout.
918 * @hide
919 */
920 public float getSecondaryHorizontal(int offset, boolean clamped) {
921 boolean trailing = primaryIsTrailingPrevious(offset);
922 return getHorizontal(offset, !trailing, clamped);
923 }
924
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900925 private float getHorizontal(int offset, boolean primary) {
926 return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset);
927 }
928
Raph Levienafe8e9b2012-12-19 16:09:32 -0800929 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 int line = getLineForOffset(offset);
931
Raph Levienafe8e9b2012-12-19 16:09:32 -0800932 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 }
934
Raph Levienafe8e9b2012-12-19 16:09:32 -0800935 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700937 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 int dir = getParagraphDirection(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700939 boolean hasTab = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 Directions directions = getLineDirections(line);
941
Doug Feltc982f602010-05-25 11:51:40 -0700942 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700943 if (hasTab && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -0700944 // Just checking this line should be good enough, tabs should be
945 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700946 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700947 if (tabs.length > 0) {
948 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
951
Doug Felte8e45f22010-03-29 14:58:40 -0700952 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700953 tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700954 float wid = tl.measure(offset - start, trailing, null);
955 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956
Raph Levienafe8e9b2012-12-19 16:09:32 -0800957 if (clamped && wid > mWidth) {
958 wid = mWidth;
959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 int left = getParagraphLeft(line);
961 int right = getParagraphRight(line);
962
Doug Feltc982f602010-05-25 11:51:40 -0700963 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
965
966 /**
967 * Get the leftmost position that should be exposed for horizontal
968 * scrolling on the specified line.
969 */
970 public float getLineLeft(int line) {
971 int dir = getParagraphDirection(line);
972 Alignment align = getParagraphAlignment(line);
973
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700974 if (align == Alignment.ALIGN_LEFT) {
975 return 0;
976 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 if (dir == DIR_RIGHT_TO_LEFT)
978 return getParagraphRight(line) - getLineMax(line);
979 else
980 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700981 } else if (align == Alignment.ALIGN_RIGHT) {
982 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 } else if (align == Alignment.ALIGN_OPPOSITE) {
984 if (dir == DIR_RIGHT_TO_LEFT)
985 return 0;
986 else
987 return mWidth - getLineMax(line);
988 } else { /* align == Alignment.ALIGN_CENTER */
989 int left = getParagraphLeft(line);
990 int right = getParagraphRight(line);
991 int max = ((int) getLineMax(line)) & ~1;
992
993 return left + ((right - left) - max) / 2;
994 }
995 }
996
997 /**
998 * Get the rightmost position that should be exposed for horizontal
999 * scrolling on the specified line.
1000 */
1001 public float getLineRight(int line) {
1002 int dir = getParagraphDirection(line);
1003 Alignment align = getParagraphAlignment(line);
1004
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001005 if (align == Alignment.ALIGN_LEFT) {
1006 return getParagraphLeft(line) + getLineMax(line);
1007 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 if (dir == DIR_RIGHT_TO_LEFT)
1009 return mWidth;
1010 else
1011 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001012 } else if (align == Alignment.ALIGN_RIGHT) {
1013 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 } else if (align == Alignment.ALIGN_OPPOSITE) {
1015 if (dir == DIR_RIGHT_TO_LEFT)
1016 return getLineMax(line);
1017 else
1018 return mWidth;
1019 } else { /* align == Alignment.ALIGN_CENTER */
1020 int left = getParagraphLeft(line);
1021 int right = getParagraphRight(line);
1022 int max = ((int) getLineMax(line)) & ~1;
1023
1024 return right - ((right - left) - max) / 2;
1025 }
1026 }
1027
1028 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001029 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001030 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001033 float margin = getParagraphLeadingMargin(line);
1034 float signedExtent = getLineExtent(line, false);
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
1038 /**
Doug Feltc982f602010-05-25 11:51:40 -07001039 * Gets the unsigned horizontal extent of the specified line, including
1040 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
1042 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001043 float margin = getParagraphLeadingMargin(line);
1044 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001045 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047
Doug Feltc982f602010-05-25 11:51:40 -07001048 /**
1049 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1050 * tab stops instead of using the ones passed in.
1051 * @param line the index of the line
1052 * @param full whether to include trailing whitespace
1053 * @return the extent of the line
1054 */
1055 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001057 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001058
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001059 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001060 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001061 if (hasTabs && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001062 // Just checking this line should be good enough, tabs should be
1063 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001064 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001065 if (tabs.length > 0) {
1066 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1067 }
1068 }
Doug Felte8e45f22010-03-29 14:58:40 -07001069 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001070 // Returned directions can actually be null
1071 if (directions == null) {
1072 return 0f;
1073 }
Doug Feltc982f602010-05-25 11:51:40 -07001074 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075
Doug Felte8e45f22010-03-29 14:58:40 -07001076 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001077 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Doug Feltc982f602010-05-25 11:51:40 -07001078 float width = tl.metrics(null);
1079 TextLine.recycle(tl);
1080 return width;
1081 }
1082
1083 /**
1084 * Returns the signed horizontal extent of the specified line, excluding
1085 * leading margin. If full is false, excludes trailing whitespace.
1086 * @param line the index of the line
1087 * @param tabStops the tab stops, can be null if we know they're not used.
1088 * @param full whether to include trailing whitespace
1089 * @return the extent of the text on this line
1090 */
1091 private float getLineExtent(int line, TabStops tabStops, boolean full) {
1092 int start = getLineStart(line);
1093 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001094 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001095 Directions directions = getLineDirections(line);
1096 int dir = getParagraphDirection(line);
1097
1098 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001099 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001100 float width = tl.metrics(null);
1101 TextLine.recycle(tl);
1102 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 }
1104
1105 /**
1106 * Get the line number corresponding to the specified vertical position.
1107 * If you ask for a position above 0, you get 0; if you ask for a position
1108 * below the bottom of the text, you get the last line.
1109 */
1110 // FIXME: It may be faster to do a linear search for layouts without many lines.
1111 public int getLineForVertical(int vertical) {
1112 int high = getLineCount(), low = -1, guess;
1113
1114 while (high - low > 1) {
1115 guess = (high + low) / 2;
1116
1117 if (getLineTop(guess) > vertical)
1118 high = guess;
1119 else
1120 low = guess;
1121 }
1122
1123 if (low < 0)
1124 return 0;
1125 else
1126 return low;
1127 }
1128
1129 /**
1130 * Get the line number on which the specified text offset appears.
1131 * If you ask for a position before 0, you get 0; if you ask for a position
1132 * beyond the end of the text, you get the last line.
1133 */
1134 public int getLineForOffset(int offset) {
1135 int high = getLineCount(), low = -1, guess;
1136
1137 while (high - low > 1) {
1138 guess = (high + low) / 2;
1139
1140 if (getLineStart(guess) > offset)
1141 high = guess;
1142 else
1143 low = guess;
1144 }
1145
1146 if (low < 0)
1147 return 0;
1148 else
1149 return low;
1150 }
1151
1152 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001153 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 * closest to the specified horizontal position.
1155 */
1156 public int getOffsetForHorizontal(int line, float horiz) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001157 return getOffsetForHorizontal(line, horiz, true);
1158 }
1159
1160 /**
1161 * Get the character offset on the specified line whose position is
1162 * closest to the specified horizontal position.
1163 *
1164 * @param line the line used to find the closest offset
1165 * @param horiz the horizontal position used to find the closest offset
1166 * @param primary whether to use the primary position or secondary position to find the offset
1167 *
1168 * @hide
1169 */
1170 public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
Raph Levienedb27f12015-06-01 14:34:47 -07001171 // TODO: use Paint.getOffsetForAdvance to avoid binary search
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001172 final int lineEndOffset = getLineEnd(line);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001173 final int lineStartOffset = getLineStart(line);
1174
1175 Directions dirs = getLineDirections(line);
1176
1177 TextLine tl = TextLine.obtain();
1178 // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
1179 tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
1180 false, null);
1181
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001182 final int max;
1183 if (line == getLineCount() - 1) {
1184 max = lineEndOffset;
1185 } else {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001186 max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
1187 !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001188 }
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001189 int best = lineStartOffset;
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001190 float bestdist = Math.abs(getHorizontal(best, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191
Doug Felt9f7a4442010-03-01 12:45:56 -08001192 for (int i = 0; i < dirs.mDirections.length; i += 2) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001193 int here = lineStartOffset + dirs.mDirections[i];
Doug Felt9f7a4442010-03-01 12:45:56 -08001194 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001195 boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
1196 int swap = isRtl ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197
1198 if (there > max)
1199 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 int high = there - 1 + 1, low = here + 1 - 1, guess;
1201
1202 while (high - low > 1) {
1203 guess = (high + low) / 2;
1204 int adguess = getOffsetAtStartOf(guess);
1205
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001206 if (getHorizontal(adguess, primary) * swap >= horiz * swap)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 high = guess;
1208 else
1209 low = guess;
1210 }
1211
1212 if (low < here + 1)
1213 low = here + 1;
1214
1215 if (low < there) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001216 int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
1217 low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
1218 if (low >= here && low < there) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001219 float dist = Math.abs(getHorizontal(low, primary) - horiz);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001220 if (aft < there) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001221 float other = Math.abs(getHorizontal(aft, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001223 if (other < dist) {
1224 dist = other;
1225 low = aft;
1226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001229 if (dist < bestdist) {
1230 bestdist = dist;
1231 best = low;
1232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
1234 }
1235
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001236 float dist = Math.abs(getHorizontal(here, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237
1238 if (dist < bestdist) {
1239 bestdist = dist;
1240 best = here;
1241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
1243
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001244 float dist = Math.abs(getHorizontal(max, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245
Raph Levien373b7a82013-09-20 15:11:52 -07001246 if (dist <= bestdist) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 bestdist = dist;
1248 best = max;
1249 }
1250
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001251 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 return best;
1253 }
1254
1255 /**
1256 * Return the text offset after the last character on the specified line.
1257 */
1258 public final int getLineEnd(int line) {
1259 return getLineStart(line + 1);
1260 }
1261
Doug Felt9f7a4442010-03-01 12:45:56 -08001262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 * Return the text offset after the last visible character (so whitespace
1264 * is not counted) on the specified line.
1265 */
1266 public int getLineVisibleEnd(int line) {
1267 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1268 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 CharSequence text = mText;
1272 char ch;
1273 if (line == getLineCount() - 1) {
1274 return end;
1275 }
1276
1277 for (; end > start; end--) {
1278 ch = text.charAt(end - 1);
1279
1280 if (ch == '\n') {
1281 return end - 1;
1282 }
1283
Raph Levienc94f7422015-03-06 19:19:48 -08001284 // Note: keep this in sync with Minikin LineBreaker::isLineEndSpace()
1285 if (!(ch == ' ' || ch == '\t' || ch == 0x1680 ||
1286 (0x2000 <= ch && ch <= 0x200A && ch != 0x2007) ||
1287 ch == 0x205F || ch == 0x3000)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 break;
1289 }
1290
1291 }
1292
1293 return end;
1294 }
1295
1296 /**
1297 * Return the vertical position of the bottom of the specified line.
1298 */
1299 public final int getLineBottom(int line) {
1300 return getLineTop(line + 1);
1301 }
1302
1303 /**
1304 * Return the vertical position of the baseline of the specified line.
1305 */
1306 public final int getLineBaseline(int line) {
1307 // getLineTop(line+1) == getLineTop(line)
1308 return getLineTop(line+1) - getLineDescent(line);
1309 }
1310
1311 /**
1312 * Get the ascent of the text on the specified line.
1313 * The return value is negative to match the Paint.ascent() convention.
1314 */
1315 public final int getLineAscent(int line) {
1316 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1317 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1318 }
1319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001321 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 }
1323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001325 return getOffsetToLeftRightOf(offset, false);
1326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327
Doug Felt9f7a4442010-03-01 12:45:56 -08001328 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1329 int line = getLineForOffset(caret);
1330 int lineStart = getLineStart(line);
1331 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001332 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001334 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001335 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001336 // if walking off line, look at the line we're headed to
1337 if (advance) {
1338 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001339 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001340 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001341 ++line;
1342 } else {
1343 return caret; // at very end, don't move
1344 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001345 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001346 } else {
1347 if (caret == lineStart) {
1348 if (line > 0) {
1349 lineChanged = true;
1350 --line;
1351 } else {
1352 return caret; // at very start, don't move
1353 }
1354 }
1355 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001356
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001357 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001358 lineStart = getLineStart(line);
1359 lineEnd = getLineEnd(line);
1360 int newDir = getParagraphDirection(line);
1361 if (newDir != lineDir) {
1362 // unusual case. we want to walk onto the line, but it runs
1363 // in a different direction than this one, so we fake movement
1364 // in the opposite direction.
1365 toLeft = !toLeft;
1366 lineDir = newDir;
1367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001369
Doug Felte8e45f22010-03-29 14:58:40 -07001370 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001371
Doug Felte8e45f22010-03-29 14:58:40 -07001372 TextLine tl = TextLine.obtain();
1373 // XXX: we don't care about tabs
1374 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1375 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1376 tl = TextLine.recycle(tl);
1377 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 }
1379
1380 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001381 // XXX this probably should skip local reorderings and
1382 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 if (offset == 0)
1384 return 0;
1385
1386 CharSequence text = mText;
1387 char c = text.charAt(offset);
1388
1389 if (c >= '\uDC00' && c <= '\uDFFF') {
1390 char c1 = text.charAt(offset - 1);
1391
1392 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1393 offset -= 1;
1394 }
1395
1396 if (mSpannedText) {
1397 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1398 ReplacementSpan.class);
1399
1400 for (int i = 0; i < spans.length; i++) {
1401 int start = ((Spanned) text).getSpanStart(spans[i]);
1402 int end = ((Spanned) text).getSpanEnd(spans[i]);
1403
1404 if (start < offset && end > offset)
1405 offset = start;
1406 }
1407 }
1408
1409 return offset;
1410 }
1411
1412 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001413 * Determine whether we should clamp cursor position. Currently it's
1414 * only robust for left-aligned displays.
1415 * @hide
1416 */
1417 public boolean shouldClampCursor(int line) {
1418 // Only clamp cursor position in left-aligned displays.
1419 switch (getParagraphAlignment(line)) {
1420 case ALIGN_LEFT:
1421 return true;
1422 case ALIGN_NORMAL:
1423 return getParagraphDirection(line) > 0;
1424 default:
1425 return false;
1426 }
1427
1428 }
1429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 * Fills in the specified Path with a representation of a cursor
1431 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001432 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 * directionalities.
1434 */
1435 public void getCursorPath(int point, Path dest,
1436 CharSequence editingBuffer) {
1437 dest.reset();
1438
1439 int line = getLineForOffset(point);
1440 int top = getLineTop(line);
1441 int bottom = getLineTop(line+1);
1442
Raph Levienafe8e9b2012-12-19 16:09:32 -08001443 boolean clamped = shouldClampCursor(line);
1444 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1445 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446
Jeff Brown497a92c2010-09-12 17:55:08 -07001447 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1448 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1449 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 int dist = 0;
1451
1452 if (caps != 0 || fn != 0) {
1453 dist = (bottom - top) >> 2;
1454
1455 if (fn != 0)
1456 top += dist;
1457 if (caps != 0)
1458 bottom -= dist;
1459 }
1460
1461 if (h1 < 0.5f)
1462 h1 = 0.5f;
1463 if (h2 < 0.5f)
1464 h2 = 0.5f;
1465
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001466 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 dest.moveTo(h1, top);
1468 dest.lineTo(h1, bottom);
1469 } else {
1470 dest.moveTo(h1, top);
1471 dest.lineTo(h1, (top + bottom) >> 1);
1472
1473 dest.moveTo(h2, (top + bottom) >> 1);
1474 dest.lineTo(h2, bottom);
1475 }
1476
1477 if (caps == 2) {
1478 dest.moveTo(h2, bottom);
1479 dest.lineTo(h2 - dist, bottom + dist);
1480 dest.lineTo(h2, bottom);
1481 dest.lineTo(h2 + dist, bottom + dist);
1482 } else if (caps == 1) {
1483 dest.moveTo(h2, bottom);
1484 dest.lineTo(h2 - dist, bottom + dist);
1485
1486 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1487 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1488
1489 dest.moveTo(h2 + dist, bottom + dist);
1490 dest.lineTo(h2, bottom);
1491 }
1492
1493 if (fn == 2) {
1494 dest.moveTo(h1, top);
1495 dest.lineTo(h1 - dist, top - dist);
1496 dest.lineTo(h1, top);
1497 dest.lineTo(h1 + dist, top - dist);
1498 } else if (fn == 1) {
1499 dest.moveTo(h1, top);
1500 dest.lineTo(h1 - dist, top - dist);
1501
1502 dest.moveTo(h1 - dist, top - dist + 0.5f);
1503 dest.lineTo(h1 + dist, top - dist + 0.5f);
1504
1505 dest.moveTo(h1 + dist, top - dist);
1506 dest.lineTo(h1, top);
1507 }
1508 }
1509
1510 private void addSelection(int line, int start, int end,
1511 int top, int bottom, Path dest) {
1512 int linestart = getLineStart(line);
1513 int lineend = getLineEnd(line);
1514 Directions dirs = getLineDirections(line);
1515
1516 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1517 lineend--;
1518
Doug Felt9f7a4442010-03-01 12:45:56 -08001519 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1520 int here = linestart + dirs.mDirections[i];
1521 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 if (there > lineend)
1524 there = lineend;
1525
1526 if (start <= there && end >= here) {
1527 int st = Math.max(start, here);
1528 int en = Math.min(end, there);
1529
1530 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001531 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1532 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001534 float left = Math.min(h1, h2);
1535 float right = Math.max(h1, h2);
1536
1537 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
1541 }
1542
1543 /**
1544 * Fills in the specified Path with a representation of a highlight
1545 * between the specified offsets. This will often be a rectangle
1546 * or a potentially discontinuous set of rectangles. If the start
1547 * and end are the same, the returned path is empty.
1548 */
1549 public void getSelectionPath(int start, int end, Path dest) {
1550 dest.reset();
1551
1552 if (start == end)
1553 return;
1554
1555 if (end < start) {
1556 int temp = end;
1557 end = start;
1558 start = temp;
1559 }
1560
1561 int startline = getLineForOffset(start);
1562 int endline = getLineForOffset(end);
1563
1564 int top = getLineTop(startline);
1565 int bottom = getLineBottom(endline);
1566
1567 if (startline == endline) {
1568 addSelection(startline, start, end, top, bottom, dest);
1569 } else {
1570 final float width = mWidth;
1571
1572 addSelection(startline, start, getLineEnd(startline),
1573 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1576 dest.addRect(getLineLeft(startline), top,
1577 0, getLineBottom(startline), Path.Direction.CW);
1578 else
1579 dest.addRect(getLineRight(startline), top,
1580 width, getLineBottom(startline), Path.Direction.CW);
1581
1582 for (int i = startline + 1; i < endline; i++) {
1583 top = getLineTop(i);
1584 bottom = getLineBottom(i);
1585 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1586 }
1587
1588 top = getLineTop(endline);
1589 bottom = getLineBottom(endline);
1590
1591 addSelection(endline, getLineStart(endline), end,
1592 top, bottom, dest);
1593
1594 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1595 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1596 else
1597 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1598 }
1599 }
1600
1601 /**
1602 * Get the alignment of the specified paragraph, taking into account
1603 * markup attached to it.
1604 */
1605 public final Alignment getParagraphAlignment(int line) {
1606 Alignment align = mAlignment;
1607
1608 if (mSpannedText) {
1609 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001610 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 getLineEnd(line),
1612 AlignmentSpan.class);
1613
1614 int spanLength = spans.length;
1615 if (spanLength > 0) {
1616 align = spans[spanLength-1].getAlignment();
1617 }
1618 }
1619
1620 return align;
1621 }
1622
1623 /**
1624 * Get the left edge of the specified paragraph, inset by left margins.
1625 */
1626 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001628 int dir = getParagraphDirection(line);
1629 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1630 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 }
Doug Feltc982f602010-05-25 11:51:40 -07001632 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 }
1634
1635 /**
1636 * Get the right edge of the specified paragraph, inset by right margins.
1637 */
1638 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001640 int dir = getParagraphDirection(line);
1641 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1642 return right; // leading margin has no impact, or no styles
1643 }
1644 return right - getParagraphLeadingMargin(line);
1645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646
Doug Feltc982f602010-05-25 11:51:40 -07001647 /**
1648 * Returns the effective leading margin (unsigned) for this line,
1649 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1650 * @param line the line index
1651 * @return the leading margin of this line
1652 */
1653 private int getParagraphLeadingMargin(int line) {
1654 if (!mSpannedText) {
1655 return 0;
1656 }
1657 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001658
Doug Feltc982f602010-05-25 11:51:40 -07001659 int lineStart = getLineStart(line);
1660 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001661 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001662 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001663 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001664 LeadingMarginSpan.class);
1665 if (spans.length == 0) {
1666 return 0; // no leading margin span;
1667 }
Doug Felt0c702b82010-05-14 10:55:42 -07001668
Doug Feltc982f602010-05-25 11:51:40 -07001669 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001670
1671 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001672 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001673
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001674 boolean useFirstLineMargin = isFirstParaLine;
1675 for (int i = 0; i < spans.length; i++) {
1676 if (spans[i] instanceof LeadingMarginSpan2) {
1677 int spStart = spanned.getSpanStart(spans[i]);
1678 int spanLine = getLineForOffset(spStart);
1679 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1680 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1681 useFirstLineMargin |= line < spanLine + count;
1682 }
1683 }
Doug Feltc982f602010-05-25 11:51:40 -07001684 for (int i = 0; i < spans.length; i++) {
1685 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001686 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
1688
Doug Feltc982f602010-05-25 11:51:40 -07001689 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
1691
Doug Felte8e45f22010-03-29 14:58:40 -07001692 /* package */
Gilles Debunne6c488de2012-03-01 16:20:35 -08001693 static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
Doug Felte8e45f22010-03-29 14:58:40 -07001694
1695 MeasuredText mt = MeasuredText.obtain();
1696 TextLine tl = TextLine.obtain();
1697 try {
Raph Levien70616ec2015-03-04 10:41:30 -08001698 mt.setPara(text, start, end, TextDirectionHeuristics.LTR, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001699 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001700 int dir;
1701 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001702 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001703 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001704 } else {
1705 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1706 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001707 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 }
Doug Feltc982f602010-05-25 11:51:40 -07001709 char[] chars = mt.mChars;
1710 int len = mt.mLen;
1711 boolean hasTabs = false;
1712 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001713 // leading margins should be taken into account when measuring a paragraph
1714 int margin = 0;
1715 if (text instanceof Spanned) {
1716 Spanned spanned = (Spanned) text;
1717 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1718 LeadingMarginSpan.class);
1719 for (LeadingMarginSpan lms : spans) {
1720 margin += lms.getLeadingMargin(true);
1721 }
1722 }
Doug Feltc982f602010-05-25 11:51:40 -07001723 for (int i = 0; i < len; ++i) {
1724 if (chars[i] == '\t') {
1725 hasTabs = true;
1726 if (text instanceof Spanned) {
1727 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001728 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001729 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001730 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001731 TabStopSpan.class);
1732 if (spans.length > 0) {
1733 tabStops = new TabStops(TAB_INCREMENT, spans);
1734 }
1735 }
1736 break;
1737 }
1738 }
1739 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001740 return margin + tl.metrics(null);
Doug Felte8e45f22010-03-29 14:58:40 -07001741 } finally {
1742 TextLine.recycle(tl);
1743 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 }
1746
Doug Felt71b8dd72010-02-16 17:27:09 -08001747 /**
Doug Feltc982f602010-05-25 11:51:40 -07001748 * @hide
1749 */
1750 /* package */ static class TabStops {
1751 private int[] mStops;
1752 private int mNumStops;
1753 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001754
Doug Feltc982f602010-05-25 11:51:40 -07001755 TabStops(int increment, Object[] spans) {
1756 reset(increment, spans);
1757 }
Doug Felt0c702b82010-05-14 10:55:42 -07001758
Doug Feltc982f602010-05-25 11:51:40 -07001759 void reset(int increment, Object[] spans) {
1760 this.mIncrement = increment;
1761
1762 int ns = 0;
1763 if (spans != null) {
1764 int[] stops = this.mStops;
1765 for (Object o : spans) {
1766 if (o instanceof TabStopSpan) {
1767 if (stops == null) {
1768 stops = new int[10];
1769 } else if (ns == stops.length) {
1770 int[] nstops = new int[ns * 2];
1771 for (int i = 0; i < ns; ++i) {
1772 nstops[i] = stops[i];
1773 }
1774 stops = nstops;
1775 }
1776 stops[ns++] = ((TabStopSpan) o).getTabStop();
1777 }
1778 }
1779 if (ns > 1) {
1780 Arrays.sort(stops, 0, ns);
1781 }
1782 if (stops != this.mStops) {
1783 this.mStops = stops;
1784 }
1785 }
1786 this.mNumStops = ns;
1787 }
Doug Felt0c702b82010-05-14 10:55:42 -07001788
Doug Feltc982f602010-05-25 11:51:40 -07001789 float nextTab(float h) {
1790 int ns = this.mNumStops;
1791 if (ns > 0) {
1792 int[] stops = this.mStops;
1793 for (int i = 0; i < ns; ++i) {
1794 int stop = stops[i];
1795 if (stop > h) {
1796 return stop;
1797 }
1798 }
1799 }
1800 return nextDefaultStop(h, mIncrement);
1801 }
1802
1803 public static float nextDefaultStop(float h, int inc) {
1804 return ((int) ((h + inc) / inc)) * inc;
1805 }
1806 }
Doug Felt0c702b82010-05-14 10:55:42 -07001807
Doug Feltc982f602010-05-25 11:51:40 -07001808 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001809 * Returns the position of the next tab stop after h on the line.
1810 *
1811 * @param text the text
1812 * @param start start of the line
1813 * @param end limit of the line
1814 * @param h the current horizontal offset
1815 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1816 * on the line will be used. If there are no tabs, a default offset
1817 * will be used to compute the tab stop.
1818 * @return the offset of the next tab stop.
1819 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 /* package */ static float nextTab(CharSequence text, int start, int end,
1821 float h, Object[] tabs) {
1822 float nh = Float.MAX_VALUE;
1823 boolean alltabs = false;
1824
1825 if (text instanceof Spanned) {
1826 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001827 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 alltabs = true;
1829 }
1830
1831 for (int i = 0; i < tabs.length; i++) {
1832 if (!alltabs) {
1833 if (!(tabs[i] instanceof TabStopSpan))
1834 continue;
1835 }
1836
1837 int where = ((TabStopSpan) tabs[i]).getTabStop();
1838
1839 if (where < nh && where > h)
1840 nh = where;
1841 }
1842
1843 if (nh != Float.MAX_VALUE)
1844 return nh;
1845 }
1846
1847 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1848 }
1849
1850 protected final boolean isSpanned() {
1851 return mSpannedText;
1852 }
1853
Eric Fischer74d31ef2010-08-05 15:29:36 -07001854 /**
1855 * Returns the same as <code>text.getSpans()</code>, except where
1856 * <code>start</code> and <code>end</code> are the same and are not
1857 * at the very beginning of the text, in which case an empty array
1858 * is returned instead.
1859 * <p>
1860 * This is needed because of the special case that <code>getSpans()</code>
1861 * on an empty range returns the spans adjacent to that range, which is
1862 * primarily for the sake of <code>TextWatchers</code> so they will get
1863 * notifications when text goes from empty to non-empty. But it also
1864 * has the unfortunate side effect that if the text ends with an empty
1865 * paragraph, that paragraph accidentally picks up the styles of the
1866 * preceding paragraph (even though those styles will not be picked up
1867 * by new text that is inserted into the empty paragraph).
1868 * <p>
1869 * The reason it just checks whether <code>start</code> and <code>end</code>
1870 * is the same is that the only time a line can contain 0 characters
1871 * is if it is the final paragraph of the Layout; otherwise any line will
1872 * contain at least one printing or newline character. The reason for the
1873 * additional check if <code>start</code> is greater than 0 is that
1874 * if the empty paragraph is the entire content of the buffer, paragraph
1875 * styles that are already applied to the buffer will apply to text that
1876 * is inserted into it.
1877 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07001878 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001879 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08001880 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001881 }
1882
Siyamed Sinirfa05ba02016-01-12 10:54:43 -08001883 if(text instanceof SpannableStringBuilder) {
1884 return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
1885 } else {
1886 return text.getSpans(start, end, type);
1887 }
Eric Fischer74d31ef2010-08-05 15:29:36 -07001888 }
1889
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001890 private char getEllipsisChar(TextUtils.TruncateAt method) {
1891 return (method == TextUtils.TruncateAt.END_SMALL) ?
Neil Fullerd29bdb22015-02-06 10:03:08 +00001892 TextUtils.ELLIPSIS_TWO_DOTS[0] :
1893 TextUtils.ELLIPSIS_NORMAL[0];
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001894 }
1895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001897 char[] dest, int destoff, TextUtils.TruncateAt method) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 int ellipsisCount = getEllipsisCount(line);
1899
1900 if (ellipsisCount == 0) {
1901 return;
1902 }
1903
1904 int ellipsisStart = getEllipsisStart(line);
1905 int linestart = getLineStart(line);
1906
1907 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1908 char c;
1909
1910 if (i == ellipsisStart) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001911 c = getEllipsisChar(method); // ellipsis
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 } else {
1913 c = '\uFEFF'; // 0-width space
1914 }
1915
1916 int a = i + linestart;
1917
1918 if (a >= start && a < end) {
1919 dest[destoff + a - start] = c;
1920 }
1921 }
1922 }
1923
1924 /**
1925 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08001926 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 */
1928 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08001929 // Directions represents directional runs within a line of text.
1930 // Runs are pairs of ints listed in visual order, starting from the
1931 // leading margin. The first int of each pair is the offset from
1932 // the first character of the line to the start of the run. The
1933 // second int represents both the length and level of the run.
1934 // The length is in the lower bits, accessed by masking with
1935 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1936 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1937 // To simply test for an RTL direction, test the bit using
1938 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939
Doug Felt9f7a4442010-03-01 12:45:56 -08001940 /* package */ int[] mDirections;
1941 /* package */ Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 mDirections = dirs;
1943 }
1944 }
1945
1946 /**
1947 * Return the offset of the first character to be ellipsized away,
1948 * relative to the start of the line. (So 0 if the beginning of the
1949 * line is ellipsized, not getLineStart().)
1950 */
1951 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07001952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 /**
1954 * Returns the number of characters to be ellipsized away, or 0 if
1955 * no ellipsis is to take place.
1956 */
1957 public abstract int getEllipsisCount(int line);
1958
1959 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1960 /* package */ CharSequence mText;
1961 /* package */ Layout mLayout;
1962 /* package */ int mWidth;
1963 /* package */ TextUtils.TruncateAt mMethod;
1964
1965 public Ellipsizer(CharSequence s) {
1966 mText = s;
1967 }
1968
1969 public char charAt(int off) {
1970 char[] buf = TextUtils.obtain(1);
1971 getChars(off, off + 1, buf, 0);
1972 char ret = buf[0];
1973
1974 TextUtils.recycle(buf);
1975 return ret;
1976 }
1977
1978 public void getChars(int start, int end, char[] dest, int destoff) {
1979 int line1 = mLayout.getLineForOffset(start);
1980 int line2 = mLayout.getLineForOffset(end);
1981
1982 TextUtils.getChars(mText, start, end, dest, destoff);
1983
1984 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001985 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 }
1987 }
1988
1989 public int length() {
1990 return mText.length();
1991 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 public CharSequence subSequence(int start, int end) {
1994 char[] s = new char[end - start];
1995 getChars(start, end, s, 0);
1996 return new String(s);
1997 }
1998
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001999 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 public String toString() {
2001 char[] s = new char[length()];
2002 getChars(0, length(), s, 0);
2003 return new String(s);
2004 }
2005
2006 }
2007
Gilles Debunne6c488de2012-03-01 16:20:35 -08002008 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 private Spanned mSpanned;
2010
2011 public SpannedEllipsizer(CharSequence display) {
2012 super(display);
2013 mSpanned = (Spanned) display;
2014 }
2015
2016 public <T> T[] getSpans(int start, int end, Class<T> type) {
2017 return mSpanned.getSpans(start, end, type);
2018 }
2019
2020 public int getSpanStart(Object tag) {
2021 return mSpanned.getSpanStart(tag);
2022 }
2023
2024 public int getSpanEnd(Object tag) {
2025 return mSpanned.getSpanEnd(tag);
2026 }
2027
2028 public int getSpanFlags(Object tag) {
2029 return mSpanned.getSpanFlags(tag);
2030 }
2031
Gilles Debunne6c488de2012-03-01 16:20:35 -08002032 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 public int nextSpanTransition(int start, int limit, Class type) {
2034 return mSpanned.nextSpanTransition(start, limit, type);
2035 }
2036
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002037 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 public CharSequence subSequence(int start, int end) {
2039 char[] s = new char[end - start];
2040 getChars(start, end, s, 0);
2041
2042 SpannableString ss = new SpannableString(new String(s));
2043 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
2044 return ss;
2045 }
2046 }
2047
2048 private CharSequence mText;
2049 private TextPaint mPaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 private int mWidth;
2051 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
2052 private float mSpacingMult;
2053 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002054 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002056 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002057 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058
2059 public static final int DIR_LEFT_TO_RIGHT = 1;
2060 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002061
Doug Felt20178d62010-02-22 13:39:01 -08002062 /* package */ static final int DIR_REQUEST_LTR = 1;
2063 /* package */ static final int DIR_REQUEST_RTL = -1;
2064 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2065 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066
Doug Felt9f7a4442010-03-01 12:45:56 -08002067 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2068 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2069 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2070 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 public enum Alignment {
2073 ALIGN_NORMAL,
2074 ALIGN_OPPOSITE,
2075 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002076 /** @hide */
2077 ALIGN_LEFT,
2078 /** @hide */
2079 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
2081
2082 private static final int TAB_INCREMENT = 20;
2083
2084 /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002085 new Directions(new int[] { 0, RUN_LENGTH_MASK });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002087 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089}