blob: 7e64fa9a967e47850734bfcec197db314085d6fd [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 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700106 * Return how wide a layout must be in order to display the specified text with one line per
107 * paragraph.
108 *
109 * <p>As of O, Uses
110 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
111 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 */
113 public static float getDesiredWidth(CharSequence source,
114 TextPaint paint) {
115 return getDesiredWidth(source, 0, source.length(), paint);
116 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700119 * Return how wide a layout must be in order to display the specified text slice with one
120 * line per paragraph.
121 *
122 * <p>As of O, Uses
123 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
124 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
125 */
126 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) {
127 return getDesiredWidth(source, start, end, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR);
128 }
129
130 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800131 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * specified text slice with one line per paragraph.
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700133 *
134 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700136 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint,
137 TextDirectionHeuristic textDir) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
140 int next;
141 for (int i = start; i <= end; i = next) {
142 next = TextUtils.indexOf(source, '\n', i, end);
143
144 if (next < 0)
145 next = end;
146
Doug Felt71b8dd72010-02-16 17:27:09 -0800147 // note, omits trailing paragraph char
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700148 float w = measurePara(paint, source, i, next, textDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
150 if (w > need)
151 need = w;
152
153 next++;
154 }
155
156 return need;
157 }
158
159 /**
160 * Subclasses of Layout use this constructor to set the display text,
161 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800162 * @param text the text to render
163 * @param paint the default paint for the layout. Styles can override
164 * various attributes of the paint.
165 * @param width the wrapping width for the text.
166 * @param align whether to left, right, or center the text. Styles can
167 * override the alignment.
168 * @param spacingMult factor by which to scale the font size to get the
169 * default line spacing
170 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 */
172 protected Layout(CharSequence text, TextPaint paint,
173 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800174 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700175 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
176 spacingMult, spacingAdd);
177 }
178
179 /**
180 * Subclasses of Layout use this constructor to set the display text,
181 * width, and other standard properties.
182 * @param text the text to render
183 * @param paint the default paint for the layout. Styles can override
184 * various attributes of the paint.
185 * @param width the wrapping width for the text.
186 * @param align whether to left, right, or center the text. Styles can
187 * override the alignment.
188 * @param spacingMult factor by which to scale the font size to get the
189 * default line spacing
190 * @param spacingAdd amount to add to the default line spacing
191 *
192 * @hide
193 */
194 protected Layout(CharSequence text, TextPaint paint,
195 int width, Alignment align, TextDirectionHeuristic textDir,
196 float spacingMult, float spacingAdd) {
197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 if (width < 0)
199 throw new IllegalArgumentException("Layout: " + width + " < 0");
200
Doug Felte8e45f22010-03-29 14:58:40 -0700201 // Ensure paint doesn't have baselineShift set.
202 // While normally we don't modify the paint the user passed in,
203 // we were already doing this in Styled.drawUniformRun with both
204 // baselineShift and bgColor. We probably should reevaluate bgColor.
205 if (paint != null) {
206 paint.bgColor = 0;
207 paint.baselineShift = 0;
208 }
209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 mText = text;
211 mPaint = paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 mWidth = width;
213 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800214 mSpacingMult = spacingMult;
215 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700217 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 }
219
220 /**
221 * Replace constructor properties of this Layout with new ones. Be careful.
222 */
223 /* package */ void replaceWith(CharSequence text, TextPaint paint,
224 int width, Alignment align,
225 float spacingmult, float spacingadd) {
226 if (width < 0) {
227 throw new IllegalArgumentException("Layout: " + width + " < 0");
228 }
229
230 mText = text;
231 mPaint = paint;
232 mWidth = width;
233 mAlignment = align;
234 mSpacingMult = spacingmult;
235 mSpacingAdd = spacingadd;
236 mSpannedText = text instanceof Spanned;
237 }
238
239 /**
240 * Draw this Layout on the specified Canvas.
241 */
242 public void draw(Canvas c) {
243 draw(c, null, null, 0);
244 }
245
246 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800247 * Draw this Layout on the specified canvas, with the highlight path drawn
248 * between the background and the text.
249 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800250 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800251 * @param highlight the path of the highlight or cursor; can be null
252 * @param highlightPaint the paint for the highlight
253 * @param cursorOffsetVertical the amount to temporarily translate the
254 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800256 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
257 int cursorOffsetVertical) {
258 final long lineRange = getLineRangeForDraw(canvas);
259 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
260 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
261 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
Gilles Debunne6c488de2012-03-01 16:20:35 -0800263 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
264 firstLine, lastLine);
265 drawText(canvas, firstLine, lastLine);
266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267
Gilles Debunne6c488de2012-03-01 16:20:35 -0800268 /**
269 * @hide
270 */
271 public void drawText(Canvas canvas, int firstLine, int lastLine) {
272 int previousLineBottom = getLineTop(firstLine);
273 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800274 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700275 int spanEnd = 0;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800276 TextPaint paint = mPaint;
277 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700279 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700280 TabStops tabStops = null;
281 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800282
Doug Felte8e45f22010-03-29 14:58:40 -0700283 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700284
Gilles Debunne6c488de2012-03-01 16:20:35 -0800285 // Draw the lines, one at a time.
286 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700287 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700289 previousLineEnd = getLineStart(lineNum + 1);
290 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291
292 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700293 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700295 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296
Raph Levien26d443a2015-03-30 14:18:32 -0700297 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700298 int left = 0;
299 int right = mWidth;
300
Gilles Debunne6c488de2012-03-01 16:20:35 -0800301 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700302 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800303 int textLength = buf.length();
304 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700305
Doug Feltc982f602010-05-25 11:51:40 -0700306 // New batch of paragraph styles, collect into spans array.
307 // Compute the alignment, last alignment style wins.
308 // Reset tabStops, we'll rebuild if we encounter a line with
309 // tabs.
310 // We expect paragraph spans to be relatively infrequent, use
311 // spanEnd so that we can check less frequently. Since
312 // paragraph styles ought to apply to entire paragraphs, we can
313 // just collect the ones present at the start of the paragraph.
314 // If spanEnd is before the end of the paragraph, that's not
315 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700316 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700317 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700319 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800320
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700321 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800322 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700324 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 break;
326 }
327 }
Doug Felt0c702b82010-05-14 10:55:42 -0700328
Doug Feltc982f602010-05-25 11:51:40 -0700329 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800331
Doug Feltc982f602010-05-25 11:51:40 -0700332 // Draw all leading margin spans. Adjust left or right according
333 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700335 boolean useFirstLineMargin = isFirstParaLine;
336 for (int n = 0; n < length; n++) {
337 if (spans[n] instanceof LeadingMarginSpan2) {
338 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
339 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
340 // if there is more than one LeadingMarginSpan2, use
341 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700342 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700343 useFirstLineMargin = true;
344 break;
345 }
346 }
347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 for (int n = 0; n < length; n++) {
349 if (spans[n] instanceof LeadingMarginSpan) {
350 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800352 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800354 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700355 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800357 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800359 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700360 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362 }
363 }
364 }
365
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700366 boolean hasTab = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700367 // Can't tell if we have tabs for sure, currently
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700368 if (hasTab && !tabStopsIsInitialized) {
Doug Feltc982f602010-05-25 11:51:40 -0700369 if (tabStops == null) {
370 tabStops = new TabStops(TAB_INCREMENT, spans);
371 } else {
372 tabStops.reset(TAB_INCREMENT, spans);
373 }
374 tabStopsIsInitialized = true;
375 }
376
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700377 // Determine whether the line aligns to normal, opposite, or center.
378 Alignment align = paraAlign;
379 if (align == Alignment.ALIGN_LEFT) {
380 align = (dir == DIR_LEFT_TO_RIGHT) ?
381 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
382 } else if (align == Alignment.ALIGN_RIGHT) {
383 align = (dir == DIR_LEFT_TO_RIGHT) ?
384 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
385 }
386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 int x;
388 if (align == Alignment.ALIGN_NORMAL) {
389 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700390 x = left + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700392 x = right + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
394 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700395 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700397 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700398 x = right - max + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700400 x = left - max + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
Doug Feltc982f602010-05-25 11:51:40 -0700402 } else { // Alignment.ALIGN_CENTER
403 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700404 x = ((right + left - max) >> 1) +
405 getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407 }
408
Raph Levien26d443a2015-03-30 14:18:32 -0700409 paint.setHyphenEdit(getHyphen(lineNum));
410 Directions directions = getLineDirections(lineNum);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700411 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800412 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800413 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 } else {
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700415 tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800416 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
Raph Levien9a174dd2015-04-08 13:35:03 -0700418 paint.setHyphenEdit(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
Doug Feltc982f602010-05-25 11:51:40 -0700420
Doug Felte8e45f22010-03-29 14:58:40 -0700421 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
423
424 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800425 * @hide
426 */
427 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
428 int cursorOffsetVertical, int firstLine, int lastLine) {
429 // First, draw LineBackgroundSpans.
430 // LineBackgroundSpans know nothing about the alignment, margins, or
431 // direction of the layout or line. XXX: Should they?
432 // They are evaluated at each line.
433 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700434 if (mLineBackgroundSpans == null) {
435 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700436 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800437
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700438 Spanned buffer = (Spanned) mText;
439 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700440 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800441
Gilles Debunneeca5b732012-04-25 18:48:42 -0700442 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700443 int previousLineBottom = getLineTop(firstLine);
444 int previousLineEnd = getLineStart(firstLine);
445 ParagraphStyle[] spans = NO_PARA_SPANS;
446 int spansLength = 0;
447 TextPaint paint = mPaint;
448 int spanEnd = 0;
449 final int width = mWidth;
450 for (int i = firstLine; i <= lastLine; i++) {
451 int start = previousLineEnd;
452 int end = getLineStart(i + 1);
453 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800454
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700455 int ltop = previousLineBottom;
456 int lbottom = getLineTop(i + 1);
457 previousLineBottom = lbottom;
458 int lbaseline = lbottom - getLineDescent(i);
459
460 if (start >= spanEnd) {
461 // These should be infrequent, so we'll use this so that
462 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700463 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700464 // All LineBackgroundSpans on a line contribute to its background.
465 spansLength = 0;
466 // Duplication of the logic of getParagraphSpans
467 if (start != end || start == 0) {
468 // Equivalent to a getSpans(start, end), but filling the 'spans' local
469 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700470 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
471 // equal test is valid since both intervals are not empty by
472 // construction
473 if (mLineBackgroundSpans.spanStarts[j] >= end ||
474 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500475 spans = GrowingArrayUtils.append(
476 spans, spansLength, mLineBackgroundSpans.spans[j]);
477 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700478 }
479 }
480 }
481
482 for (int n = 0; n < spansLength; n++) {
483 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
484 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
485 ltop, lbaseline, lbottom,
486 buffer, start, end, i);
487 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800488 }
489 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700490 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800491 }
492
493 // There can be a highlight even without spans if we are drawing
494 // a non-spanned transformation of a spanned editing buffer.
495 if (highlight != null) {
496 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
497 canvas.drawPath(highlight, highlightPaint);
498 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
499 }
500 }
501
502 /**
503 * @param canvas
504 * @return The range of lines that need to be drawn, possibly empty.
505 * @hide
506 */
507 public long getLineRangeForDraw(Canvas canvas) {
508 int dtop, dbottom;
509
510 synchronized (sTempRect) {
511 if (!canvas.getClipBounds(sTempRect)) {
512 // Negative range end used as a special flag
513 return TextUtils.packRangeInLong(0, -1);
514 }
515
516 dtop = sTempRect.top;
517 dbottom = sTempRect.bottom;
518 }
519
520 final int top = Math.max(dtop, 0);
521 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
522
Gilles Debunne2fba3382012-06-11 17:46:24 -0700523 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800524 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
525 }
526
527 /**
Doug Feltc982f602010-05-25 11:51:40 -0700528 * Return the start position of the line, given the left and right bounds
529 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700530 *
Doug Feltc982f602010-05-25 11:51:40 -0700531 * @param line the line index
532 * @param left the left bounds (0, or leading margin if ltr para)
533 * @param right the right bounds (width, minus leading margin if rtl para)
534 * @return the start position of the line (to right of line if rtl para)
535 */
536 private int getLineStartPos(int line, int left, int right) {
537 // Adjust the point at which to start rendering depending on the
538 // alignment of the paragraph.
539 Alignment align = getParagraphAlignment(line);
540 int dir = getParagraphDirection(line);
541
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700542 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700543 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
544 } else if (align == Alignment.ALIGN_RIGHT) {
545 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
546 }
547
548 int x;
549 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700550 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700551 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700552 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700553 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700554 }
555 } else {
556 TabStops tabStops = null;
557 if (mSpannedText && getLineContainsTab(line)) {
558 Spanned spanned = (Spanned) mText;
559 int start = getLineStart(line);
560 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
561 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800562 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
563 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700564 if (tabSpans.length > 0) {
565 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
566 }
567 }
568 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700569 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700570 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700571 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700572 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700573 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700574 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700575 }
576 } else { // Alignment.ALIGN_CENTER
577 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700578 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700579 }
580 }
581 return x;
582 }
583
584 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 * Return the text that is displayed by this Layout.
586 */
587 public final CharSequence getText() {
588 return mText;
589 }
590
591 /**
592 * Return the base Paint properties for this layout.
593 * Do NOT change the paint, which may result in funny
594 * drawing for this layout.
595 */
596 public final TextPaint getPaint() {
597 return mPaint;
598 }
599
600 /**
601 * Return the width of this layout.
602 */
603 public final int getWidth() {
604 return mWidth;
605 }
606
607 /**
608 * Return the width to which this Layout is ellipsizing, or
609 * {@link #getWidth} if it is not doing anything special.
610 */
611 public int getEllipsizedWidth() {
612 return mWidth;
613 }
614
615 /**
616 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800617 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * it does not cause the text to reflow to use the full new width.
619 */
620 public final void increaseWidthTo(int wid) {
621 if (wid < mWidth) {
622 throw new RuntimeException("attempted to reduce Layout width");
623 }
624
625 mWidth = wid;
626 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 /**
629 * Return the total height of this layout.
630 */
631 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800632 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634
635 /**
636 * Return the base alignment of this layout.
637 */
638 public final Alignment getAlignment() {
639 return mAlignment;
640 }
641
642 /**
643 * Return what the text height is multiplied by to get the line height.
644 */
645 public final float getSpacingMultiplier() {
646 return mSpacingMult;
647 }
648
649 /**
650 * Return the number of units of leading that are added to each line.
651 */
652 public final float getSpacingAdd() {
653 return mSpacingAdd;
654 }
655
656 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700657 * Return the heuristic used to determine paragraph text direction.
658 * @hide
659 */
660 public final TextDirectionHeuristic getTextDirectionHeuristic() {
661 return mTextDir;
662 }
663
664 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 * Return the number of lines of text in this layout.
666 */
667 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 /**
670 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
671 * If bounds is not null, return the top, left, right, bottom extents
672 * of the specified line in it.
673 * @param line which line to examine (0..getLineCount() - 1)
674 * @param bounds Optional. If not null, it returns the extent of the line
675 * @return the Y-coordinate of the baseline
676 */
677 public int getLineBounds(int line, Rect bounds) {
678 if (bounds != null) {
679 bounds.left = 0; // ???
680 bounds.top = getLineTop(line);
681 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800682 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
684 return getLineBaseline(line);
685 }
686
687 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800688 * Return the vertical position of the top of the specified line
689 * (0&hellip;getLineCount()).
690 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 * bottom of the last line.
692 */
693 public abstract int getLineTop(int line);
694
695 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800696 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 */
698 public abstract int getLineDescent(int line);
699
700 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800701 * Return the text offset of the beginning of the specified line (
702 * 0&hellip;getLineCount()). If the specified line is equal to the line
703 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 */
705 public abstract int getLineStart(int line);
706
707 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800708 * Returns the primary directionality of the paragraph containing the
709 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
710 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 */
712 public abstract int getParagraphDirection(int line);
713
714 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700715 * Returns whether the specified line contains one or more
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700716 * characters that need to be handled specially, like tabs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 */
718 public abstract boolean getLineContainsTab(int line);
719
720 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800721 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 * The array alternates counts of characters in left-to-right
723 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800724 *
725 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 */
727 public abstract Directions getLineDirections(int line);
728
729 /**
730 * Returns the (negative) number of extra pixels of ascent padding in the
731 * top line of the Layout.
732 */
733 public abstract int getTopPadding();
734
735 /**
736 * Returns the number of extra pixels of descent padding in the
737 * bottom line of the Layout.
738 */
739 public abstract int getBottomPadding();
740
Raph Levien26d443a2015-03-30 14:18:32 -0700741 /**
742 * Returns the hyphen edit for a line.
743 *
744 * @hide
745 */
746 public int getHyphen(int line) {
747 return 0;
748 }
749
Raph Levien2ea52902015-07-01 14:39:31 -0700750 /**
751 * Returns the left indent for a line.
752 *
753 * @hide
754 */
755 public int getIndentAdjust(int line, Alignment alignment) {
756 return 0;
757 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700758
759 /**
760 * Returns true if the character at offset and the preceding character
761 * are at different run levels (and thus there's a split caret).
762 * @param offset the offset
763 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800764 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700765 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800766 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800767 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700768 Directions dirs = getLineDirections(line);
769 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
770 return false;
771 }
772
773 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800774 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700775 int lineEnd = getLineEnd(line);
776 if (offset == lineStart || offset == lineEnd) {
777 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
778 int runIndex = offset == lineStart ? 0 : runs.length - 2;
779 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
780 }
781
782 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800783 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700784 if (offset == runs[i]) {
785 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800786 }
787 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700788 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800789 }
790
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700791 /**
792 * Returns true if the character at offset is right to left (RTL).
793 * @param offset the offset
794 * @return true if the character is RTL, false if it is LTR
795 */
796 public boolean isRtlCharAt(int offset) {
797 int line = getLineForOffset(offset);
798 Directions dirs = getLineDirections(line);
799 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
800 return false;
801 }
802 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
803 return true;
804 }
805 int[] runs = dirs.mDirections;
806 int lineStart = getLineStart(line);
807 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700808 int start = lineStart + runs[i];
809 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
810 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700811 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
812 return ((level & 1) != 0);
813 }
814 }
815 // Should happen only if the offset is "out of bounds"
816 return false;
817 }
818
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900819 /**
820 * Returns the range of the run that the character at offset belongs to.
821 * @param offset the offset
822 * @return The range of the run
823 * @hide
824 */
825 public long getRunRange(int offset) {
826 int line = getLineForOffset(offset);
827 Directions dirs = getLineDirections(line);
828 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
829 return TextUtils.packRangeInLong(0, getLineEnd(line));
830 }
831 int[] runs = dirs.mDirections;
832 int lineStart = getLineStart(line);
833 for (int i = 0; i < runs.length; i += 2) {
834 int start = lineStart + runs[i];
835 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
836 if (offset >= start && offset < limit) {
837 return TextUtils.packRangeInLong(start, limit);
838 }
839 }
840 // Should happen only if the offset is "out of bounds"
841 return TextUtils.packRangeInLong(0, getLineEnd(line));
842 }
843
Doug Felt9f7a4442010-03-01 12:45:56 -0800844 private boolean primaryIsTrailingPrevious(int offset) {
845 int line = getLineForOffset(offset);
846 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700847 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800848 int[] runs = getLineDirections(line).mDirections;
849
850 int levelAt = -1;
851 for (int i = 0; i < runs.length; i += 2) {
852 int start = lineStart + runs[i];
853 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
854 if (limit > lineEnd) {
855 limit = lineEnd;
856 }
857 if (offset >= start && offset < limit) {
858 if (offset > start) {
859 // Previous character is at same level, so don't use trailing.
860 return false;
861 }
862 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
863 break;
864 }
865 }
866 if (levelAt == -1) {
867 // Offset was limit of line.
868 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
869 }
870
871 // At level boundary, check previous level.
872 int levelBefore = -1;
873 if (offset == lineStart) {
874 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
875 } else {
876 offset -= 1;
877 for (int i = 0; i < runs.length; i += 2) {
878 int start = lineStart + runs[i];
879 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
880 if (limit > lineEnd) {
881 limit = lineEnd;
882 }
883 if (offset >= start && offset < limit) {
884 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
885 break;
886 }
887 }
888 }
889
890 return levelBefore < levelAt;
891 }
892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 /**
894 * Get the primary horizontal position for the specified text offset.
895 * This is the location where a new character would be inserted in
896 * the paragraph's primary direction.
897 */
898 public float getPrimaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800899 return getPrimaryHorizontal(offset, false /* not clamped */);
900 }
901
902 /**
903 * Get the primary horizontal position for the specified text offset, but
904 * optionally clamp it so that it doesn't exceed the width of the layout.
905 * @hide
906 */
907 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800908 boolean trailing = primaryIsTrailingPrevious(offset);
Raph Levienafe8e9b2012-12-19 16:09:32 -0800909 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 }
911
912 /**
913 * Get the secondary horizontal position for the specified text offset.
914 * This is the location where a new character would be inserted in
915 * the direction other than the paragraph's primary direction.
916 */
917 public float getSecondaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800918 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
920
Raph Levienafe8e9b2012-12-19 16:09:32 -0800921 /**
922 * Get the secondary horizontal position for the specified text offset, but
923 * optionally clamp it so that it doesn't exceed the width of the layout.
924 * @hide
925 */
926 public float getSecondaryHorizontal(int offset, boolean clamped) {
927 boolean trailing = primaryIsTrailingPrevious(offset);
928 return getHorizontal(offset, !trailing, clamped);
929 }
930
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900931 private float getHorizontal(int offset, boolean primary) {
932 return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset);
933 }
934
Raph Levienafe8e9b2012-12-19 16:09:32 -0800935 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 int line = getLineForOffset(offset);
937
Raph Levienafe8e9b2012-12-19 16:09:32 -0800938 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
940
Raph Levienafe8e9b2012-12-19 16:09:32 -0800941 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700943 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 int dir = getParagraphDirection(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700945 boolean hasTab = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 Directions directions = getLineDirections(line);
947
Doug Feltc982f602010-05-25 11:51:40 -0700948 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700949 if (hasTab && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -0700950 // Just checking this line should be good enough, tabs should be
951 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700952 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700953 if (tabs.length > 0) {
954 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
957
Doug Felte8e45f22010-03-29 14:58:40 -0700958 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700959 tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700960 float wid = tl.measure(offset - start, trailing, null);
961 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962
Raph Levienafe8e9b2012-12-19 16:09:32 -0800963 if (clamped && wid > mWidth) {
964 wid = mWidth;
965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 int left = getParagraphLeft(line);
967 int right = getParagraphRight(line);
968
Doug Feltc982f602010-05-25 11:51:40 -0700969 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
971
972 /**
973 * Get the leftmost position that should be exposed for horizontal
974 * scrolling on the specified line.
975 */
976 public float getLineLeft(int line) {
977 int dir = getParagraphDirection(line);
978 Alignment align = getParagraphAlignment(line);
979
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700980 if (align == Alignment.ALIGN_LEFT) {
981 return 0;
982 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 if (dir == DIR_RIGHT_TO_LEFT)
984 return getParagraphRight(line) - getLineMax(line);
985 else
986 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700987 } else if (align == Alignment.ALIGN_RIGHT) {
988 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 } else if (align == Alignment.ALIGN_OPPOSITE) {
990 if (dir == DIR_RIGHT_TO_LEFT)
991 return 0;
992 else
993 return mWidth - getLineMax(line);
994 } else { /* align == Alignment.ALIGN_CENTER */
995 int left = getParagraphLeft(line);
996 int right = getParagraphRight(line);
997 int max = ((int) getLineMax(line)) & ~1;
998
999 return left + ((right - left) - max) / 2;
1000 }
1001 }
1002
1003 /**
1004 * Get the rightmost position that should be exposed for horizontal
1005 * scrolling on the specified line.
1006 */
1007 public float getLineRight(int line) {
1008 int dir = getParagraphDirection(line);
1009 Alignment align = getParagraphAlignment(line);
1010
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001011 if (align == Alignment.ALIGN_LEFT) {
1012 return getParagraphLeft(line) + getLineMax(line);
1013 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 if (dir == DIR_RIGHT_TO_LEFT)
1015 return mWidth;
1016 else
1017 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001018 } else if (align == Alignment.ALIGN_RIGHT) {
1019 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 } else if (align == Alignment.ALIGN_OPPOSITE) {
1021 if (dir == DIR_RIGHT_TO_LEFT)
1022 return getLineMax(line);
1023 else
1024 return mWidth;
1025 } else { /* align == Alignment.ALIGN_CENTER */
1026 int left = getParagraphLeft(line);
1027 int right = getParagraphRight(line);
1028 int max = ((int) getLineMax(line)) & ~1;
1029
1030 return right - ((right - left) - max) / 2;
1031 }
1032 }
1033
1034 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001035 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001036 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001039 float margin = getParagraphLeadingMargin(line);
1040 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001041 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
1043
1044 /**
Doug Feltc982f602010-05-25 11:51:40 -07001045 * Gets the unsigned horizontal extent of the specified line, including
1046 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 */
1048 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001049 float margin = getParagraphLeadingMargin(line);
1050 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001051 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
1053
Doug Feltc982f602010-05-25 11:51:40 -07001054 /**
1055 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1056 * tab stops instead of using the ones passed in.
1057 * @param line the index of the line
1058 * @param full whether to include trailing whitespace
1059 * @return the extent of the line
1060 */
1061 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001063 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001064
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001065 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001066 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001067 if (hasTabs && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001068 // Just checking this line should be good enough, tabs should be
1069 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001070 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001071 if (tabs.length > 0) {
1072 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1073 }
1074 }
Doug Felte8e45f22010-03-29 14:58:40 -07001075 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001076 // Returned directions can actually be null
1077 if (directions == null) {
1078 return 0f;
1079 }
Doug Feltc982f602010-05-25 11:51:40 -07001080 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081
Doug Felte8e45f22010-03-29 14:58:40 -07001082 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001083 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Doug Feltc982f602010-05-25 11:51:40 -07001084 float width = tl.metrics(null);
1085 TextLine.recycle(tl);
1086 return width;
1087 }
1088
1089 /**
1090 * Returns the signed horizontal extent of the specified line, excluding
1091 * leading margin. If full is false, excludes trailing whitespace.
1092 * @param line the index of the line
1093 * @param tabStops the tab stops, can be null if we know they're not used.
1094 * @param full whether to include trailing whitespace
1095 * @return the extent of the text on this line
1096 */
1097 private float getLineExtent(int line, TabStops tabStops, boolean full) {
1098 int start = getLineStart(line);
1099 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001100 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001101 Directions directions = getLineDirections(line);
1102 int dir = getParagraphDirection(line);
1103
1104 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001105 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001106 float width = tl.metrics(null);
1107 TextLine.recycle(tl);
1108 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
1110
1111 /**
1112 * Get the line number corresponding to the specified vertical position.
1113 * If you ask for a position above 0, you get 0; if you ask for a position
1114 * below the bottom of the text, you get the last line.
1115 */
1116 // FIXME: It may be faster to do a linear search for layouts without many lines.
1117 public int getLineForVertical(int vertical) {
1118 int high = getLineCount(), low = -1, guess;
1119
1120 while (high - low > 1) {
1121 guess = (high + low) / 2;
1122
1123 if (getLineTop(guess) > vertical)
1124 high = guess;
1125 else
1126 low = guess;
1127 }
1128
1129 if (low < 0)
1130 return 0;
1131 else
1132 return low;
1133 }
1134
1135 /**
1136 * Get the line number on which the specified text offset appears.
1137 * If you ask for a position before 0, you get 0; if you ask for a position
1138 * beyond the end of the text, you get the last line.
1139 */
1140 public int getLineForOffset(int offset) {
1141 int high = getLineCount(), low = -1, guess;
1142
1143 while (high - low > 1) {
1144 guess = (high + low) / 2;
1145
1146 if (getLineStart(guess) > offset)
1147 high = guess;
1148 else
1149 low = guess;
1150 }
1151
1152 if (low < 0)
1153 return 0;
1154 else
1155 return low;
1156 }
1157
1158 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001159 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 * closest to the specified horizontal position.
1161 */
1162 public int getOffsetForHorizontal(int line, float horiz) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001163 return getOffsetForHorizontal(line, horiz, true);
1164 }
1165
1166 /**
1167 * Get the character offset on the specified line whose position is
1168 * closest to the specified horizontal position.
1169 *
1170 * @param line the line used to find the closest offset
1171 * @param horiz the horizontal position used to find the closest offset
1172 * @param primary whether to use the primary position or secondary position to find the offset
1173 *
1174 * @hide
1175 */
1176 public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
Raph Levienedb27f12015-06-01 14:34:47 -07001177 // TODO: use Paint.getOffsetForAdvance to avoid binary search
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001178 final int lineEndOffset = getLineEnd(line);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001179 final int lineStartOffset = getLineStart(line);
1180
1181 Directions dirs = getLineDirections(line);
1182
1183 TextLine tl = TextLine.obtain();
1184 // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
1185 tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
1186 false, null);
1187
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001188 final int max;
1189 if (line == getLineCount() - 1) {
1190 max = lineEndOffset;
1191 } else {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001192 max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
1193 !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001194 }
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001195 int best = lineStartOffset;
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001196 float bestdist = Math.abs(getHorizontal(best, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197
Doug Felt9f7a4442010-03-01 12:45:56 -08001198 for (int i = 0; i < dirs.mDirections.length; i += 2) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001199 int here = lineStartOffset + dirs.mDirections[i];
Doug Felt9f7a4442010-03-01 12:45:56 -08001200 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001201 boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
1202 int swap = isRtl ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203
1204 if (there > max)
1205 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 int high = there - 1 + 1, low = here + 1 - 1, guess;
1207
1208 while (high - low > 1) {
1209 guess = (high + low) / 2;
1210 int adguess = getOffsetAtStartOf(guess);
1211
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001212 if (getHorizontal(adguess, primary) * swap >= horiz * swap)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 high = guess;
1214 else
1215 low = guess;
1216 }
1217
1218 if (low < here + 1)
1219 low = here + 1;
1220
1221 if (low < there) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001222 int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
1223 low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
1224 if (low >= here && low < there) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001225 float dist = Math.abs(getHorizontal(low, primary) - horiz);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001226 if (aft < there) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001227 float other = Math.abs(getHorizontal(aft, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001229 if (other < dist) {
1230 dist = other;
1231 low = aft;
1232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001235 if (dist < bestdist) {
1236 bestdist = dist;
1237 best = low;
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
1240 }
1241
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001242 float dist = Math.abs(getHorizontal(here, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243
1244 if (dist < bestdist) {
1245 bestdist = dist;
1246 best = here;
1247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001250 float dist = Math.abs(getHorizontal(max, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251
Raph Levien373b7a82013-09-20 15:11:52 -07001252 if (dist <= bestdist) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 bestdist = dist;
1254 best = max;
1255 }
1256
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001257 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 return best;
1259 }
1260
1261 /**
1262 * Return the text offset after the last character on the specified line.
1263 */
1264 public final int getLineEnd(int line) {
1265 return getLineStart(line + 1);
1266 }
1267
Doug Felt9f7a4442010-03-01 12:45:56 -08001268 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 * Return the text offset after the last visible character (so whitespace
1270 * is not counted) on the specified line.
1271 */
1272 public int getLineVisibleEnd(int line) {
1273 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1274 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 CharSequence text = mText;
1278 char ch;
1279 if (line == getLineCount() - 1) {
1280 return end;
1281 }
1282
1283 for (; end > start; end--) {
1284 ch = text.charAt(end - 1);
1285
1286 if (ch == '\n') {
1287 return end - 1;
1288 }
1289
Raph Levienc94f7422015-03-06 19:19:48 -08001290 // Note: keep this in sync with Minikin LineBreaker::isLineEndSpace()
1291 if (!(ch == ' ' || ch == '\t' || ch == 0x1680 ||
1292 (0x2000 <= ch && ch <= 0x200A && ch != 0x2007) ||
1293 ch == 0x205F || ch == 0x3000)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 break;
1295 }
1296
1297 }
1298
1299 return end;
1300 }
1301
1302 /**
1303 * Return the vertical position of the bottom of the specified line.
1304 */
1305 public final int getLineBottom(int line) {
1306 return getLineTop(line + 1);
1307 }
1308
1309 /**
1310 * Return the vertical position of the baseline of the specified line.
1311 */
1312 public final int getLineBaseline(int line) {
1313 // getLineTop(line+1) == getLineTop(line)
1314 return getLineTop(line+1) - getLineDescent(line);
1315 }
1316
1317 /**
1318 * Get the ascent of the text on the specified line.
1319 * The return value is negative to match the Paint.ascent() convention.
1320 */
1321 public final int getLineAscent(int line) {
1322 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1323 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1324 }
1325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001327 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 }
1329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001331 return getOffsetToLeftRightOf(offset, false);
1332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333
Doug Felt9f7a4442010-03-01 12:45:56 -08001334 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1335 int line = getLineForOffset(caret);
1336 int lineStart = getLineStart(line);
1337 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001338 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001340 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001341 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001342 // if walking off line, look at the line we're headed to
1343 if (advance) {
1344 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001345 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001346 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001347 ++line;
1348 } else {
1349 return caret; // at very end, don't move
1350 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001351 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001352 } else {
1353 if (caret == lineStart) {
1354 if (line > 0) {
1355 lineChanged = true;
1356 --line;
1357 } else {
1358 return caret; // at very start, don't move
1359 }
1360 }
1361 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001362
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001363 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001364 lineStart = getLineStart(line);
1365 lineEnd = getLineEnd(line);
1366 int newDir = getParagraphDirection(line);
1367 if (newDir != lineDir) {
1368 // unusual case. we want to walk onto the line, but it runs
1369 // in a different direction than this one, so we fake movement
1370 // in the opposite direction.
1371 toLeft = !toLeft;
1372 lineDir = newDir;
1373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001375
Doug Felte8e45f22010-03-29 14:58:40 -07001376 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001377
Doug Felte8e45f22010-03-29 14:58:40 -07001378 TextLine tl = TextLine.obtain();
1379 // XXX: we don't care about tabs
1380 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1381 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1382 tl = TextLine.recycle(tl);
1383 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
1385
1386 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001387 // XXX this probably should skip local reorderings and
1388 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 if (offset == 0)
1390 return 0;
1391
1392 CharSequence text = mText;
1393 char c = text.charAt(offset);
1394
1395 if (c >= '\uDC00' && c <= '\uDFFF') {
1396 char c1 = text.charAt(offset - 1);
1397
1398 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1399 offset -= 1;
1400 }
1401
1402 if (mSpannedText) {
1403 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1404 ReplacementSpan.class);
1405
1406 for (int i = 0; i < spans.length; i++) {
1407 int start = ((Spanned) text).getSpanStart(spans[i]);
1408 int end = ((Spanned) text).getSpanEnd(spans[i]);
1409
1410 if (start < offset && end > offset)
1411 offset = start;
1412 }
1413 }
1414
1415 return offset;
1416 }
1417
1418 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001419 * Determine whether we should clamp cursor position. Currently it's
1420 * only robust for left-aligned displays.
1421 * @hide
1422 */
1423 public boolean shouldClampCursor(int line) {
1424 // Only clamp cursor position in left-aligned displays.
1425 switch (getParagraphAlignment(line)) {
1426 case ALIGN_LEFT:
1427 return true;
1428 case ALIGN_NORMAL:
1429 return getParagraphDirection(line) > 0;
1430 default:
1431 return false;
1432 }
1433
1434 }
1435 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 * Fills in the specified Path with a representation of a cursor
1437 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001438 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 * directionalities.
1440 */
1441 public void getCursorPath(int point, Path dest,
1442 CharSequence editingBuffer) {
1443 dest.reset();
1444
1445 int line = getLineForOffset(point);
1446 int top = getLineTop(line);
1447 int bottom = getLineTop(line+1);
1448
Raph Levienafe8e9b2012-12-19 16:09:32 -08001449 boolean clamped = shouldClampCursor(line);
1450 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1451 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452
Jeff Brown497a92c2010-09-12 17:55:08 -07001453 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1454 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1455 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 int dist = 0;
1457
1458 if (caps != 0 || fn != 0) {
1459 dist = (bottom - top) >> 2;
1460
1461 if (fn != 0)
1462 top += dist;
1463 if (caps != 0)
1464 bottom -= dist;
1465 }
1466
1467 if (h1 < 0.5f)
1468 h1 = 0.5f;
1469 if (h2 < 0.5f)
1470 h2 = 0.5f;
1471
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001472 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 dest.moveTo(h1, top);
1474 dest.lineTo(h1, bottom);
1475 } else {
1476 dest.moveTo(h1, top);
1477 dest.lineTo(h1, (top + bottom) >> 1);
1478
1479 dest.moveTo(h2, (top + bottom) >> 1);
1480 dest.lineTo(h2, bottom);
1481 }
1482
1483 if (caps == 2) {
1484 dest.moveTo(h2, bottom);
1485 dest.lineTo(h2 - dist, bottom + dist);
1486 dest.lineTo(h2, bottom);
1487 dest.lineTo(h2 + dist, bottom + dist);
1488 } else if (caps == 1) {
1489 dest.moveTo(h2, bottom);
1490 dest.lineTo(h2 - dist, bottom + dist);
1491
1492 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1493 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1494
1495 dest.moveTo(h2 + dist, bottom + dist);
1496 dest.lineTo(h2, bottom);
1497 }
1498
1499 if (fn == 2) {
1500 dest.moveTo(h1, top);
1501 dest.lineTo(h1 - dist, top - dist);
1502 dest.lineTo(h1, top);
1503 dest.lineTo(h1 + dist, top - dist);
1504 } else if (fn == 1) {
1505 dest.moveTo(h1, top);
1506 dest.lineTo(h1 - dist, top - dist);
1507
1508 dest.moveTo(h1 - dist, top - dist + 0.5f);
1509 dest.lineTo(h1 + dist, top - dist + 0.5f);
1510
1511 dest.moveTo(h1 + dist, top - dist);
1512 dest.lineTo(h1, top);
1513 }
1514 }
1515
1516 private void addSelection(int line, int start, int end,
1517 int top, int bottom, Path dest) {
1518 int linestart = getLineStart(line);
1519 int lineend = getLineEnd(line);
1520 Directions dirs = getLineDirections(line);
1521
1522 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1523 lineend--;
1524
Doug Felt9f7a4442010-03-01 12:45:56 -08001525 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1526 int here = linestart + dirs.mDirections[i];
1527 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 if (there > lineend)
1530 there = lineend;
1531
1532 if (start <= there && end >= here) {
1533 int st = Math.max(start, here);
1534 int en = Math.min(end, there);
1535
1536 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001537 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1538 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001540 float left = Math.min(h1, h2);
1541 float right = Math.max(h1, h2);
1542
1543 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 }
1547 }
1548
1549 /**
1550 * Fills in the specified Path with a representation of a highlight
1551 * between the specified offsets. This will often be a rectangle
1552 * or a potentially discontinuous set of rectangles. If the start
1553 * and end are the same, the returned path is empty.
1554 */
1555 public void getSelectionPath(int start, int end, Path dest) {
1556 dest.reset();
1557
1558 if (start == end)
1559 return;
1560
1561 if (end < start) {
1562 int temp = end;
1563 end = start;
1564 start = temp;
1565 }
1566
1567 int startline = getLineForOffset(start);
1568 int endline = getLineForOffset(end);
1569
1570 int top = getLineTop(startline);
1571 int bottom = getLineBottom(endline);
1572
1573 if (startline == endline) {
1574 addSelection(startline, start, end, top, bottom, dest);
1575 } else {
1576 final float width = mWidth;
1577
1578 addSelection(startline, start, getLineEnd(startline),
1579 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1582 dest.addRect(getLineLeft(startline), top,
1583 0, getLineBottom(startline), Path.Direction.CW);
1584 else
1585 dest.addRect(getLineRight(startline), top,
1586 width, getLineBottom(startline), Path.Direction.CW);
1587
1588 for (int i = startline + 1; i < endline; i++) {
1589 top = getLineTop(i);
1590 bottom = getLineBottom(i);
1591 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1592 }
1593
1594 top = getLineTop(endline);
1595 bottom = getLineBottom(endline);
1596
1597 addSelection(endline, getLineStart(endline), end,
1598 top, bottom, dest);
1599
1600 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1601 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1602 else
1603 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1604 }
1605 }
1606
1607 /**
1608 * Get the alignment of the specified paragraph, taking into account
1609 * markup attached to it.
1610 */
1611 public final Alignment getParagraphAlignment(int line) {
1612 Alignment align = mAlignment;
1613
1614 if (mSpannedText) {
1615 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001616 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 getLineEnd(line),
1618 AlignmentSpan.class);
1619
1620 int spanLength = spans.length;
1621 if (spanLength > 0) {
1622 align = spans[spanLength-1].getAlignment();
1623 }
1624 }
1625
1626 return align;
1627 }
1628
1629 /**
1630 * Get the left edge of the specified paragraph, inset by left margins.
1631 */
1632 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001634 int dir = getParagraphDirection(line);
1635 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1636 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
Doug Feltc982f602010-05-25 11:51:40 -07001638 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
1640
1641 /**
1642 * Get the right edge of the specified paragraph, inset by right margins.
1643 */
1644 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001646 int dir = getParagraphDirection(line);
1647 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1648 return right; // leading margin has no impact, or no styles
1649 }
1650 return right - getParagraphLeadingMargin(line);
1651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652
Doug Feltc982f602010-05-25 11:51:40 -07001653 /**
1654 * Returns the effective leading margin (unsigned) for this line,
1655 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1656 * @param line the line index
1657 * @return the leading margin of this line
1658 */
1659 private int getParagraphLeadingMargin(int line) {
1660 if (!mSpannedText) {
1661 return 0;
1662 }
1663 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001664
Doug Feltc982f602010-05-25 11:51:40 -07001665 int lineStart = getLineStart(line);
1666 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001667 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001668 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001669 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001670 LeadingMarginSpan.class);
1671 if (spans.length == 0) {
1672 return 0; // no leading margin span;
1673 }
Doug Felt0c702b82010-05-14 10:55:42 -07001674
Doug Feltc982f602010-05-25 11:51:40 -07001675 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001676
1677 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001678 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001679
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001680 boolean useFirstLineMargin = isFirstParaLine;
1681 for (int i = 0; i < spans.length; i++) {
1682 if (spans[i] instanceof LeadingMarginSpan2) {
1683 int spStart = spanned.getSpanStart(spans[i]);
1684 int spanLine = getLineForOffset(spStart);
1685 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1686 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1687 useFirstLineMargin |= line < spanLine + count;
1688 }
1689 }
Doug Feltc982f602010-05-25 11:51:40 -07001690 for (int i = 0; i < spans.length; i++) {
1691 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001692 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 }
1694
Doug Feltc982f602010-05-25 11:51:40 -07001695 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
1697
Doug Felte8e45f22010-03-29 14:58:40 -07001698 /* package */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001699 static float measurePara(TextPaint paint, CharSequence text, int start, int end,
1700 TextDirectionHeuristic textDir) {
Doug Felte8e45f22010-03-29 14:58:40 -07001701 MeasuredText mt = MeasuredText.obtain();
1702 TextLine tl = TextLine.obtain();
1703 try {
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001704 mt.setPara(text, start, end, textDir, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001705 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001706 int dir;
1707 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001708 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001709 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001710 } else {
1711 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1712 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001713 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 }
Doug Feltc982f602010-05-25 11:51:40 -07001715 char[] chars = mt.mChars;
1716 int len = mt.mLen;
1717 boolean hasTabs = false;
1718 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001719 // leading margins should be taken into account when measuring a paragraph
1720 int margin = 0;
1721 if (text instanceof Spanned) {
1722 Spanned spanned = (Spanned) text;
1723 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1724 LeadingMarginSpan.class);
1725 for (LeadingMarginSpan lms : spans) {
1726 margin += lms.getLeadingMargin(true);
1727 }
1728 }
Doug Feltc982f602010-05-25 11:51:40 -07001729 for (int i = 0; i < len; ++i) {
1730 if (chars[i] == '\t') {
1731 hasTabs = true;
1732 if (text instanceof Spanned) {
1733 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001734 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001735 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001736 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001737 TabStopSpan.class);
1738 if (spans.length > 0) {
1739 tabStops = new TabStops(TAB_INCREMENT, spans);
1740 }
1741 }
1742 break;
1743 }
1744 }
1745 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001746 return margin + Math.abs(tl.metrics(null));
Doug Felte8e45f22010-03-29 14:58:40 -07001747 } finally {
1748 TextLine.recycle(tl);
1749 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
1752
Doug Felt71b8dd72010-02-16 17:27:09 -08001753 /**
Doug Feltc982f602010-05-25 11:51:40 -07001754 * @hide
1755 */
1756 /* package */ static class TabStops {
1757 private int[] mStops;
1758 private int mNumStops;
1759 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001760
Doug Feltc982f602010-05-25 11:51:40 -07001761 TabStops(int increment, Object[] spans) {
1762 reset(increment, spans);
1763 }
Doug Felt0c702b82010-05-14 10:55:42 -07001764
Doug Feltc982f602010-05-25 11:51:40 -07001765 void reset(int increment, Object[] spans) {
1766 this.mIncrement = increment;
1767
1768 int ns = 0;
1769 if (spans != null) {
1770 int[] stops = this.mStops;
1771 for (Object o : spans) {
1772 if (o instanceof TabStopSpan) {
1773 if (stops == null) {
1774 stops = new int[10];
1775 } else if (ns == stops.length) {
1776 int[] nstops = new int[ns * 2];
1777 for (int i = 0; i < ns; ++i) {
1778 nstops[i] = stops[i];
1779 }
1780 stops = nstops;
1781 }
1782 stops[ns++] = ((TabStopSpan) o).getTabStop();
1783 }
1784 }
1785 if (ns > 1) {
1786 Arrays.sort(stops, 0, ns);
1787 }
1788 if (stops != this.mStops) {
1789 this.mStops = stops;
1790 }
1791 }
1792 this.mNumStops = ns;
1793 }
Doug Felt0c702b82010-05-14 10:55:42 -07001794
Doug Feltc982f602010-05-25 11:51:40 -07001795 float nextTab(float h) {
1796 int ns = this.mNumStops;
1797 if (ns > 0) {
1798 int[] stops = this.mStops;
1799 for (int i = 0; i < ns; ++i) {
1800 int stop = stops[i];
1801 if (stop > h) {
1802 return stop;
1803 }
1804 }
1805 }
1806 return nextDefaultStop(h, mIncrement);
1807 }
1808
1809 public static float nextDefaultStop(float h, int inc) {
1810 return ((int) ((h + inc) / inc)) * inc;
1811 }
1812 }
Doug Felt0c702b82010-05-14 10:55:42 -07001813
Doug Feltc982f602010-05-25 11:51:40 -07001814 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001815 * Returns the position of the next tab stop after h on the line.
1816 *
1817 * @param text the text
1818 * @param start start of the line
1819 * @param end limit of the line
1820 * @param h the current horizontal offset
1821 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1822 * on the line will be used. If there are no tabs, a default offset
1823 * will be used to compute the tab stop.
1824 * @return the offset of the next tab stop.
1825 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 /* package */ static float nextTab(CharSequence text, int start, int end,
1827 float h, Object[] tabs) {
1828 float nh = Float.MAX_VALUE;
1829 boolean alltabs = false;
1830
1831 if (text instanceof Spanned) {
1832 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001833 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 alltabs = true;
1835 }
1836
1837 for (int i = 0; i < tabs.length; i++) {
1838 if (!alltabs) {
1839 if (!(tabs[i] instanceof TabStopSpan))
1840 continue;
1841 }
1842
1843 int where = ((TabStopSpan) tabs[i]).getTabStop();
1844
1845 if (where < nh && where > h)
1846 nh = where;
1847 }
1848
1849 if (nh != Float.MAX_VALUE)
1850 return nh;
1851 }
1852
1853 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1854 }
1855
1856 protected final boolean isSpanned() {
1857 return mSpannedText;
1858 }
1859
Eric Fischer74d31ef2010-08-05 15:29:36 -07001860 /**
1861 * Returns the same as <code>text.getSpans()</code>, except where
1862 * <code>start</code> and <code>end</code> are the same and are not
1863 * at the very beginning of the text, in which case an empty array
1864 * is returned instead.
1865 * <p>
1866 * This is needed because of the special case that <code>getSpans()</code>
1867 * on an empty range returns the spans adjacent to that range, which is
1868 * primarily for the sake of <code>TextWatchers</code> so they will get
1869 * notifications when text goes from empty to non-empty. But it also
1870 * has the unfortunate side effect that if the text ends with an empty
1871 * paragraph, that paragraph accidentally picks up the styles of the
1872 * preceding paragraph (even though those styles will not be picked up
1873 * by new text that is inserted into the empty paragraph).
1874 * <p>
1875 * The reason it just checks whether <code>start</code> and <code>end</code>
1876 * is the same is that the only time a line can contain 0 characters
1877 * is if it is the final paragraph of the Layout; otherwise any line will
1878 * contain at least one printing or newline character. The reason for the
1879 * additional check if <code>start</code> is greater than 0 is that
1880 * if the empty paragraph is the entire content of the buffer, paragraph
1881 * styles that are already applied to the buffer will apply to text that
1882 * is inserted into it.
1883 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07001884 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001885 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08001886 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001887 }
1888
Siyamed Sinirfa05ba02016-01-12 10:54:43 -08001889 if(text instanceof SpannableStringBuilder) {
1890 return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
1891 } else {
1892 return text.getSpans(start, end, type);
1893 }
Eric Fischer74d31ef2010-08-05 15:29:36 -07001894 }
1895
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001896 private char getEllipsisChar(TextUtils.TruncateAt method) {
1897 return (method == TextUtils.TruncateAt.END_SMALL) ?
Neil Fullerd29bdb22015-02-06 10:03:08 +00001898 TextUtils.ELLIPSIS_TWO_DOTS[0] :
1899 TextUtils.ELLIPSIS_NORMAL[0];
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001900 }
1901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001903 char[] dest, int destoff, TextUtils.TruncateAt method) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 int ellipsisCount = getEllipsisCount(line);
1905
1906 if (ellipsisCount == 0) {
1907 return;
1908 }
1909
1910 int ellipsisStart = getEllipsisStart(line);
1911 int linestart = getLineStart(line);
1912
1913 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1914 char c;
1915
1916 if (i == ellipsisStart) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001917 c = getEllipsisChar(method); // ellipsis
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 } else {
1919 c = '\uFEFF'; // 0-width space
1920 }
1921
1922 int a = i + linestart;
1923
1924 if (a >= start && a < end) {
1925 dest[destoff + a - start] = c;
1926 }
1927 }
1928 }
1929
1930 /**
1931 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08001932 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 */
1934 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08001935 // Directions represents directional runs within a line of text.
1936 // Runs are pairs of ints listed in visual order, starting from the
1937 // leading margin. The first int of each pair is the offset from
1938 // the first character of the line to the start of the run. The
1939 // second int represents both the length and level of the run.
1940 // The length is in the lower bits, accessed by masking with
1941 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1942 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1943 // To simply test for an RTL direction, test the bit using
1944 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945
Doug Felt9f7a4442010-03-01 12:45:56 -08001946 /* package */ int[] mDirections;
1947 /* package */ Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 mDirections = dirs;
1949 }
1950 }
1951
1952 /**
1953 * Return the offset of the first character to be ellipsized away,
1954 * relative to the start of the line. (So 0 if the beginning of the
1955 * line is ellipsized, not getLineStart().)
1956 */
1957 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07001958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 /**
1960 * Returns the number of characters to be ellipsized away, or 0 if
1961 * no ellipsis is to take place.
1962 */
1963 public abstract int getEllipsisCount(int line);
1964
1965 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1966 /* package */ CharSequence mText;
1967 /* package */ Layout mLayout;
1968 /* package */ int mWidth;
1969 /* package */ TextUtils.TruncateAt mMethod;
1970
1971 public Ellipsizer(CharSequence s) {
1972 mText = s;
1973 }
1974
1975 public char charAt(int off) {
1976 char[] buf = TextUtils.obtain(1);
1977 getChars(off, off + 1, buf, 0);
1978 char ret = buf[0];
1979
1980 TextUtils.recycle(buf);
1981 return ret;
1982 }
1983
1984 public void getChars(int start, int end, char[] dest, int destoff) {
1985 int line1 = mLayout.getLineForOffset(start);
1986 int line2 = mLayout.getLineForOffset(end);
1987
1988 TextUtils.getChars(mText, start, end, dest, destoff);
1989
1990 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001991 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993 }
1994
1995 public int length() {
1996 return mText.length();
1997 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 public CharSequence subSequence(int start, int end) {
2000 char[] s = new char[end - start];
2001 getChars(start, end, s, 0);
2002 return new String(s);
2003 }
2004
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002005 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 public String toString() {
2007 char[] s = new char[length()];
2008 getChars(0, length(), s, 0);
2009 return new String(s);
2010 }
2011
2012 }
2013
Gilles Debunne6c488de2012-03-01 16:20:35 -08002014 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 private Spanned mSpanned;
2016
2017 public SpannedEllipsizer(CharSequence display) {
2018 super(display);
2019 mSpanned = (Spanned) display;
2020 }
2021
2022 public <T> T[] getSpans(int start, int end, Class<T> type) {
2023 return mSpanned.getSpans(start, end, type);
2024 }
2025
2026 public int getSpanStart(Object tag) {
2027 return mSpanned.getSpanStart(tag);
2028 }
2029
2030 public int getSpanEnd(Object tag) {
2031 return mSpanned.getSpanEnd(tag);
2032 }
2033
2034 public int getSpanFlags(Object tag) {
2035 return mSpanned.getSpanFlags(tag);
2036 }
2037
Gilles Debunne6c488de2012-03-01 16:20:35 -08002038 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 public int nextSpanTransition(int start, int limit, Class type) {
2040 return mSpanned.nextSpanTransition(start, limit, type);
2041 }
2042
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002043 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 public CharSequence subSequence(int start, int end) {
2045 char[] s = new char[end - start];
2046 getChars(start, end, s, 0);
2047
2048 SpannableString ss = new SpannableString(new String(s));
2049 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
2050 return ss;
2051 }
2052 }
2053
2054 private CharSequence mText;
2055 private TextPaint mPaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 private int mWidth;
2057 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
2058 private float mSpacingMult;
2059 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002060 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002062 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002063 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064
2065 public static final int DIR_LEFT_TO_RIGHT = 1;
2066 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002067
Doug Felt20178d62010-02-22 13:39:01 -08002068 /* package */ static final int DIR_REQUEST_LTR = 1;
2069 /* package */ static final int DIR_REQUEST_RTL = -1;
2070 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2071 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072
Doug Felt9f7a4442010-03-01 12:45:56 -08002073 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2074 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2075 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2076 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 public enum Alignment {
2079 ALIGN_NORMAL,
2080 ALIGN_OPPOSITE,
2081 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002082 /** @hide */
2083 ALIGN_LEFT,
2084 /** @hide */
2085 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
2087
2088 private static final int TAB_INCREMENT = 20;
2089
2090 /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002091 new Directions(new int[] { 0, RUN_LENGTH_MASK });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002093 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095}