blob: 7dce34875d49b3a97c43e322ce722f7d917e920e [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
The Android Open Source Project10592532009-03-18 17:39:46 -070019import android.emoji.EmojiFactory;
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
Doug Feltc982f602010-05-25 11:51:40 -070036import java.util.Arrays;
Doug Felt9f7a4442010-03-01 12:45:56 -080037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038/**
Doug Felt9f7a4442010-03-01 12:45:56 -080039 * A base class that manages text layout in visual elements on
40 * the screen.
41 * <p>For text that will be edited, use a {@link DynamicLayout},
42 * which will be updated as the text changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 * For text that will not change, use a {@link StaticLayout}.
44 */
45public abstract class Layout {
Doug Felt71b8dd72010-02-16 17:27:09 -080046 private static final ParagraphStyle[] NO_PARA_SPANS =
47 ArrayUtils.emptyArray(ParagraphStyle.class);
Dave Bort76c02262009-04-13 17:17:21 -070048
Gilles Debunneeca5b732012-04-25 18:48:42 -070049 /* package */ static final EmojiFactory EMOJI_FACTORY = EmojiFactory.newAvailableInstance();
The Android Open Source Project10592532009-03-18 17:39:46 -070050 /* package */ static final int MIN_EMOJI, MAX_EMOJI;
51
52 static {
53 if (EMOJI_FACTORY != null) {
54 MIN_EMOJI = EMOJI_FACTORY.getMinimumAndroidPua();
55 MAX_EMOJI = EMOJI_FACTORY.getMaximumAndroidPua();
56 } else {
57 MIN_EMOJI = -1;
58 MAX_EMOJI = -1;
59 }
Doug Felte8e45f22010-03-29 14:58:40 -070060 }
Eric Fischerc2d54f42009-03-27 15:52:38 -070061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 /**
Doug Felt71b8dd72010-02-16 17:27:09 -080063 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 * specified text with one line per paragraph.
65 */
66 public static float getDesiredWidth(CharSequence source,
67 TextPaint paint) {
68 return getDesiredWidth(source, 0, source.length(), paint);
69 }
Doug Felt9f7a4442010-03-01 12:45:56 -080070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 /**
Doug Felt71b8dd72010-02-16 17:27:09 -080072 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 * specified text slice with one line per paragraph.
74 */
75 public static float getDesiredWidth(CharSequence source,
76 int start, int end,
77 TextPaint paint) {
78 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80 int next;
81 for (int i = start; i <= end; i = next) {
82 next = TextUtils.indexOf(source, '\n', i, end);
83
84 if (next < 0)
85 next = end;
86
Doug Felt71b8dd72010-02-16 17:27:09 -080087 // note, omits trailing paragraph char
Gilles Debunne6c488de2012-03-01 16:20:35 -080088 float w = measurePara(paint, source, i, next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90 if (w > need)
91 need = w;
92
93 next++;
94 }
95
96 return need;
97 }
98
99 /**
100 * Subclasses of Layout use this constructor to set the display text,
101 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800102 * @param text the text to render
103 * @param paint the default paint for the layout. Styles can override
104 * various attributes of the paint.
105 * @param width the wrapping width for the text.
106 * @param align whether to left, right, or center the text. Styles can
107 * override the alignment.
108 * @param spacingMult factor by which to scale the font size to get the
109 * default line spacing
110 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 */
112 protected Layout(CharSequence text, TextPaint paint,
113 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800114 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700115 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
116 spacingMult, spacingAdd);
117 }
118
119 /**
120 * Subclasses of Layout use this constructor to set the display text,
121 * width, and other standard properties.
122 * @param text the text to render
123 * @param paint the default paint for the layout. Styles can override
124 * various attributes of the paint.
125 * @param width the wrapping width for the text.
126 * @param align whether to left, right, or center the text. Styles can
127 * override the alignment.
128 * @param spacingMult factor by which to scale the font size to get the
129 * default line spacing
130 * @param spacingAdd amount to add to the default line spacing
131 *
132 * @hide
133 */
134 protected Layout(CharSequence text, TextPaint paint,
135 int width, Alignment align, TextDirectionHeuristic textDir,
136 float spacingMult, float spacingAdd) {
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 if (width < 0)
139 throw new IllegalArgumentException("Layout: " + width + " < 0");
140
Doug Felte8e45f22010-03-29 14:58:40 -0700141 // Ensure paint doesn't have baselineShift set.
142 // While normally we don't modify the paint the user passed in,
143 // we were already doing this in Styled.drawUniformRun with both
144 // baselineShift and bgColor. We probably should reevaluate bgColor.
145 if (paint != null) {
146 paint.bgColor = 0;
147 paint.baselineShift = 0;
148 }
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 mText = text;
151 mPaint = paint;
152 mWorkPaint = new TextPaint();
153 mWidth = width;
154 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800155 mSpacingMult = spacingMult;
156 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700158 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 }
160
161 /**
162 * Replace constructor properties of this Layout with new ones. Be careful.
163 */
164 /* package */ void replaceWith(CharSequence text, TextPaint paint,
165 int width, Alignment align,
166 float spacingmult, float spacingadd) {
167 if (width < 0) {
168 throw new IllegalArgumentException("Layout: " + width + " < 0");
169 }
170
171 mText = text;
172 mPaint = paint;
173 mWidth = width;
174 mAlignment = align;
175 mSpacingMult = spacingmult;
176 mSpacingAdd = spacingadd;
177 mSpannedText = text instanceof Spanned;
178 }
179
180 /**
181 * Draw this Layout on the specified Canvas.
182 */
183 public void draw(Canvas c) {
184 draw(c, null, null, 0);
185 }
186
187 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800188 * Draw this Layout on the specified canvas, with the highlight path drawn
189 * between the background and the text.
190 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800191 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800192 * @param highlight the path of the highlight or cursor; can be null
193 * @param highlightPaint the paint for the highlight
194 * @param cursorOffsetVertical the amount to temporarily translate the
195 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800197 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
198 int cursorOffsetVertical) {
199 final long lineRange = getLineRangeForDraw(canvas);
200 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
201 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
202 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203
Gilles Debunne6c488de2012-03-01 16:20:35 -0800204 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
205 firstLine, lastLine);
206 drawText(canvas, firstLine, lastLine);
207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
Gilles Debunne6c488de2012-03-01 16:20:35 -0800209 /**
210 * @hide
211 */
212 public void drawText(Canvas canvas, int firstLine, int lastLine) {
213 int previousLineBottom = getLineTop(firstLine);
214 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800215 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700216 int spanEnd = 0;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800217 TextPaint paint = mPaint;
218 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700220 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700221 TabStops tabStops = null;
222 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800223
Doug Felte8e45f22010-03-29 14:58:40 -0700224 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700225
Gilles Debunne6c488de2012-03-01 16:20:35 -0800226 // Draw the lines, one at a time.
227 // The baseline is the top of the following line minus the current line's descent.
228 for (int i = firstLine; i <= lastLine; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 int start = previousLineEnd;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800230 previousLineEnd = getLineStart(i + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 int end = getLineVisibleEnd(i, start, previousLineEnd);
232
233 int ltop = previousLineBottom;
234 int lbottom = getLineTop(i+1);
235 previousLineBottom = lbottom;
236 int lbaseline = lbottom - getLineDescent(i);
237
Doug Feltc982f602010-05-25 11:51:40 -0700238 int dir = getParagraphDirection(i);
239 int left = 0;
240 int right = mWidth;
241
Gilles Debunne6c488de2012-03-01 16:20:35 -0800242 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700243 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800244 int textLength = buf.length();
245 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700246
Doug Feltc982f602010-05-25 11:51:40 -0700247 // New batch of paragraph styles, collect into spans array.
248 // Compute the alignment, last alignment style wins.
249 // Reset tabStops, we'll rebuild if we encounter a line with
250 // tabs.
251 // We expect paragraph spans to be relatively infrequent, use
252 // spanEnd so that we can check less frequently. Since
253 // paragraph styles ought to apply to entire paragraphs, we can
254 // just collect the ones present at the start of the paragraph.
255 // If spanEnd is before the end of the paragraph, that's not
256 // our problem.
Gilles Debunne6c488de2012-03-01 16:20:35 -0800257 if (start >= spanEnd && (i == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700258 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700260 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800261
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700262 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800263 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700265 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 break;
267 }
268 }
Doug Felt0c702b82010-05-14 10:55:42 -0700269
Doug Feltc982f602010-05-25 11:51:40 -0700270 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800272
Doug Feltc982f602010-05-25 11:51:40 -0700273 // Draw all leading margin spans. Adjust left or right according
274 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700276 boolean useFirstLineMargin = isFirstParaLine;
277 for (int n = 0; n < length; n++) {
278 if (spans[n] instanceof LeadingMarginSpan2) {
279 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
280 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
281 // if there is more than one LeadingMarginSpan2, use
282 // the count that is greatest
283 if (i < startLine + count) {
284 useFirstLineMargin = true;
285 break;
286 }
287 }
288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 for (int n = 0; n < length; n++) {
290 if (spans[n] instanceof LeadingMarginSpan) {
291 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800293 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800295 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700296 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800298 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800300 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700301 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303 }
304 }
305 }
306
Doug Feltc982f602010-05-25 11:51:40 -0700307 boolean hasTabOrEmoji = getLineContainsTab(i);
308 // Can't tell if we have tabs for sure, currently
309 if (hasTabOrEmoji && !tabStopsIsInitialized) {
310 if (tabStops == null) {
311 tabStops = new TabStops(TAB_INCREMENT, spans);
312 } else {
313 tabStops.reset(TAB_INCREMENT, spans);
314 }
315 tabStopsIsInitialized = true;
316 }
317
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700318 // Determine whether the line aligns to normal, opposite, or center.
319 Alignment align = paraAlign;
320 if (align == Alignment.ALIGN_LEFT) {
321 align = (dir == DIR_LEFT_TO_RIGHT) ?
322 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
323 } else if (align == Alignment.ALIGN_RIGHT) {
324 align = (dir == DIR_LEFT_TO_RIGHT) ?
325 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
326 }
327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 int x;
329 if (align == Alignment.ALIGN_NORMAL) {
330 if (dir == DIR_LEFT_TO_RIGHT) {
331 x = left;
332 } else {
333 x = right;
334 }
335 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700336 int max = (int)getLineExtent(i, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700338 if (dir == DIR_LEFT_TO_RIGHT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 x = right - max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700341 x = left - max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 }
Doug Feltc982f602010-05-25 11:51:40 -0700343 } else { // Alignment.ALIGN_CENTER
344 max = max & ~1;
345 x = (right + left - max) >> 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347 }
348
349 Directions directions = getLineDirections(i);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800350 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTabOrEmoji) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800351 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800352 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700354 tl.set(paint, buf, start, end, dir, directions, hasTabOrEmoji, tabStops);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800355 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
357 }
Doug Feltc982f602010-05-25 11:51:40 -0700358
Doug Felte8e45f22010-03-29 14:58:40 -0700359 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
361
362 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800363 * @hide
364 */
365 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
366 int cursorOffsetVertical, int firstLine, int lastLine) {
367 // First, draw LineBackgroundSpans.
368 // LineBackgroundSpans know nothing about the alignment, margins, or
369 // direction of the layout or line. XXX: Should they?
370 // They are evaluated at each line.
371 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700372 if (mLineBackgroundSpans == null) {
373 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700374 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800375
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700376 Spanned buffer = (Spanned) mText;
377 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700378 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800379
Gilles Debunneeca5b732012-04-25 18:48:42 -0700380 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700381 int previousLineBottom = getLineTop(firstLine);
382 int previousLineEnd = getLineStart(firstLine);
383 ParagraphStyle[] spans = NO_PARA_SPANS;
384 int spansLength = 0;
385 TextPaint paint = mPaint;
386 int spanEnd = 0;
387 final int width = mWidth;
388 for (int i = firstLine; i <= lastLine; i++) {
389 int start = previousLineEnd;
390 int end = getLineStart(i + 1);
391 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800392
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700393 int ltop = previousLineBottom;
394 int lbottom = getLineTop(i + 1);
395 previousLineBottom = lbottom;
396 int lbaseline = lbottom - getLineDescent(i);
397
398 if (start >= spanEnd) {
399 // These should be infrequent, so we'll use this so that
400 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700401 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700402 // All LineBackgroundSpans on a line contribute to its background.
403 spansLength = 0;
404 // Duplication of the logic of getParagraphSpans
405 if (start != end || start == 0) {
406 // Equivalent to a getSpans(start, end), but filling the 'spans' local
407 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700408 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
409 // equal test is valid since both intervals are not empty by
410 // construction
411 if (mLineBackgroundSpans.spanStarts[j] >= end ||
412 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500413 spans = GrowingArrayUtils.append(
414 spans, spansLength, mLineBackgroundSpans.spans[j]);
415 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700416 }
417 }
418 }
419
420 for (int n = 0; n < spansLength; n++) {
421 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
422 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
423 ltop, lbaseline, lbottom,
424 buffer, start, end, i);
425 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800426 }
427 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700428 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800429 }
430
431 // There can be a highlight even without spans if we are drawing
432 // a non-spanned transformation of a spanned editing buffer.
433 if (highlight != null) {
434 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
435 canvas.drawPath(highlight, highlightPaint);
436 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
437 }
438 }
439
440 /**
441 * @param canvas
442 * @return The range of lines that need to be drawn, possibly empty.
443 * @hide
444 */
445 public long getLineRangeForDraw(Canvas canvas) {
446 int dtop, dbottom;
447
448 synchronized (sTempRect) {
449 if (!canvas.getClipBounds(sTempRect)) {
450 // Negative range end used as a special flag
451 return TextUtils.packRangeInLong(0, -1);
452 }
453
454 dtop = sTempRect.top;
455 dbottom = sTempRect.bottom;
456 }
457
458 final int top = Math.max(dtop, 0);
459 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
460
Gilles Debunne2fba3382012-06-11 17:46:24 -0700461 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800462 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
463 }
464
465 /**
Doug Feltc982f602010-05-25 11:51:40 -0700466 * Return the start position of the line, given the left and right bounds
467 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700468 *
Doug Feltc982f602010-05-25 11:51:40 -0700469 * @param line the line index
470 * @param left the left bounds (0, or leading margin if ltr para)
471 * @param right the right bounds (width, minus leading margin if rtl para)
472 * @return the start position of the line (to right of line if rtl para)
473 */
474 private int getLineStartPos(int line, int left, int right) {
475 // Adjust the point at which to start rendering depending on the
476 // alignment of the paragraph.
477 Alignment align = getParagraphAlignment(line);
478 int dir = getParagraphDirection(line);
479
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700480 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700481 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
482 } else if (align == Alignment.ALIGN_RIGHT) {
483 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
484 }
485
486 int x;
487 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700488 if (dir == DIR_LEFT_TO_RIGHT) {
489 x = left;
490 } else {
491 x = right;
492 }
493 } else {
494 TabStops tabStops = null;
495 if (mSpannedText && getLineContainsTab(line)) {
496 Spanned spanned = (Spanned) mText;
497 int start = getLineStart(line);
498 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
499 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800500 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
501 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700502 if (tabSpans.length > 0) {
503 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
504 }
505 }
506 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700507 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700508 if (dir == DIR_LEFT_TO_RIGHT) {
509 x = right - max;
510 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700511 // max is negative here
Doug Feltc982f602010-05-25 11:51:40 -0700512 x = left - max;
513 }
514 } else { // Alignment.ALIGN_CENTER
515 max = max & ~1;
516 x = (left + right - max) >> 1;
517 }
518 }
519 return x;
520 }
521
522 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 * Return the text that is displayed by this Layout.
524 */
525 public final CharSequence getText() {
526 return mText;
527 }
528
529 /**
530 * Return the base Paint properties for this layout.
531 * Do NOT change the paint, which may result in funny
532 * drawing for this layout.
533 */
534 public final TextPaint getPaint() {
535 return mPaint;
536 }
537
538 /**
539 * Return the width of this layout.
540 */
541 public final int getWidth() {
542 return mWidth;
543 }
544
545 /**
546 * Return the width to which this Layout is ellipsizing, or
547 * {@link #getWidth} if it is not doing anything special.
548 */
549 public int getEllipsizedWidth() {
550 return mWidth;
551 }
552
553 /**
554 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800555 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 * it does not cause the text to reflow to use the full new width.
557 */
558 public final void increaseWidthTo(int wid) {
559 if (wid < mWidth) {
560 throw new RuntimeException("attempted to reduce Layout width");
561 }
562
563 mWidth = wid;
564 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 /**
567 * Return the total height of this layout.
568 */
569 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800570 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572
573 /**
574 * Return the base alignment of this layout.
575 */
576 public final Alignment getAlignment() {
577 return mAlignment;
578 }
579
580 /**
581 * Return what the text height is multiplied by to get the line height.
582 */
583 public final float getSpacingMultiplier() {
584 return mSpacingMult;
585 }
586
587 /**
588 * Return the number of units of leading that are added to each line.
589 */
590 public final float getSpacingAdd() {
591 return mSpacingAdd;
592 }
593
594 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700595 * Return the heuristic used to determine paragraph text direction.
596 * @hide
597 */
598 public final TextDirectionHeuristic getTextDirectionHeuristic() {
599 return mTextDir;
600 }
601
602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 * Return the number of lines of text in this layout.
604 */
605 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 /**
608 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
609 * If bounds is not null, return the top, left, right, bottom extents
610 * of the specified line in it.
611 * @param line which line to examine (0..getLineCount() - 1)
612 * @param bounds Optional. If not null, it returns the extent of the line
613 * @return the Y-coordinate of the baseline
614 */
615 public int getLineBounds(int line, Rect bounds) {
616 if (bounds != null) {
617 bounds.left = 0; // ???
618 bounds.top = getLineTop(line);
619 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800620 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622 return getLineBaseline(line);
623 }
624
625 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800626 * Return the vertical position of the top of the specified line
627 * (0&hellip;getLineCount()).
628 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 * bottom of the last line.
630 */
631 public abstract int getLineTop(int line);
632
633 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800634 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 */
636 public abstract int getLineDescent(int line);
637
638 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800639 * Return the text offset of the beginning of the specified line (
640 * 0&hellip;getLineCount()). If the specified line is equal to the line
641 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 */
643 public abstract int getLineStart(int line);
644
645 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800646 * Returns the primary directionality of the paragraph containing the
647 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
648 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 */
650 public abstract int getParagraphDirection(int line);
651
652 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700653 * Returns whether the specified line contains one or more
654 * characters that need to be handled specially, like tabs
655 * or emoji.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 */
657 public abstract boolean getLineContainsTab(int line);
658
659 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800660 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 * The array alternates counts of characters in left-to-right
662 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800663 *
664 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 */
666 public abstract Directions getLineDirections(int line);
667
668 /**
669 * Returns the (negative) number of extra pixels of ascent padding in the
670 * top line of the Layout.
671 */
672 public abstract int getTopPadding();
673
674 /**
675 * Returns the number of extra pixels of descent padding in the
676 * bottom line of the Layout.
677 */
678 public abstract int getBottomPadding();
679
Doug Felt4e0c5e52010-03-15 16:56:02 -0700680
681 /**
682 * Returns true if the character at offset and the preceding character
683 * are at different run levels (and thus there's a split caret).
684 * @param offset the offset
685 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800686 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700687 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800688 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800689 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700690 Directions dirs = getLineDirections(line);
691 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
692 return false;
693 }
694
695 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800696 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700697 int lineEnd = getLineEnd(line);
698 if (offset == lineStart || offset == lineEnd) {
699 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
700 int runIndex = offset == lineStart ? 0 : runs.length - 2;
701 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
702 }
703
704 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800705 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700706 if (offset == runs[i]) {
707 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800708 }
709 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700710 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800711 }
712
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700713 /**
714 * Returns true if the character at offset is right to left (RTL).
715 * @param offset the offset
716 * @return true if the character is RTL, false if it is LTR
717 */
718 public boolean isRtlCharAt(int offset) {
719 int line = getLineForOffset(offset);
720 Directions dirs = getLineDirections(line);
721 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
722 return false;
723 }
724 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
725 return true;
726 }
727 int[] runs = dirs.mDirections;
728 int lineStart = getLineStart(line);
729 for (int i = 0; i < runs.length; i += 2) {
730 int start = lineStart + (runs[i] & RUN_LENGTH_MASK);
731 // No need to test the end as an offset after the last run should return the value
732 // corresponding of the last run
733 if (offset >= start) {
734 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
735 return ((level & 1) != 0);
736 }
737 }
738 // Should happen only if the offset is "out of bounds"
739 return false;
740 }
741
Doug Felt9f7a4442010-03-01 12:45:56 -0800742 private boolean primaryIsTrailingPrevious(int offset) {
743 int line = getLineForOffset(offset);
744 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700745 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800746 int[] runs = getLineDirections(line).mDirections;
747
748 int levelAt = -1;
749 for (int i = 0; i < runs.length; i += 2) {
750 int start = lineStart + runs[i];
751 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
752 if (limit > lineEnd) {
753 limit = lineEnd;
754 }
755 if (offset >= start && offset < limit) {
756 if (offset > start) {
757 // Previous character is at same level, so don't use trailing.
758 return false;
759 }
760 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
761 break;
762 }
763 }
764 if (levelAt == -1) {
765 // Offset was limit of line.
766 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
767 }
768
769 // At level boundary, check previous level.
770 int levelBefore = -1;
771 if (offset == lineStart) {
772 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
773 } else {
774 offset -= 1;
775 for (int i = 0; i < runs.length; i += 2) {
776 int start = lineStart + runs[i];
777 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
778 if (limit > lineEnd) {
779 limit = lineEnd;
780 }
781 if (offset >= start && offset < limit) {
782 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
783 break;
784 }
785 }
786 }
787
788 return levelBefore < levelAt;
789 }
790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 /**
792 * Get the primary horizontal position for the specified text offset.
793 * This is the location where a new character would be inserted in
794 * the paragraph's primary direction.
795 */
796 public float getPrimaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800797 return getPrimaryHorizontal(offset, false /* not clamped */);
798 }
799
800 /**
801 * Get the primary horizontal position for the specified text offset, but
802 * optionally clamp it so that it doesn't exceed the width of the layout.
803 * @hide
804 */
805 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800806 boolean trailing = primaryIsTrailingPrevious(offset);
Raph Levienafe8e9b2012-12-19 16:09:32 -0800807 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809
810 /**
811 * Get the secondary horizontal position for the specified text offset.
812 * This is the location where a new character would be inserted in
813 * the direction other than the paragraph's primary direction.
814 */
815 public float getSecondaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800816 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818
Raph Levienafe8e9b2012-12-19 16:09:32 -0800819 /**
820 * Get the secondary horizontal position for the specified text offset, but
821 * optionally clamp it so that it doesn't exceed the width of the layout.
822 * @hide
823 */
824 public float getSecondaryHorizontal(int offset, boolean clamped) {
825 boolean trailing = primaryIsTrailingPrevious(offset);
826 return getHorizontal(offset, !trailing, clamped);
827 }
828
829 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 int line = getLineForOffset(offset);
831
Raph Levienafe8e9b2012-12-19 16:09:32 -0800832 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 }
834
Raph Levienafe8e9b2012-12-19 16:09:32 -0800835 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700837 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 int dir = getParagraphDirection(line);
Doug Feltc982f602010-05-25 11:51:40 -0700839 boolean hasTabOrEmoji = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 Directions directions = getLineDirections(line);
841
Doug Feltc982f602010-05-25 11:51:40 -0700842 TabStops tabStops = null;
843 if (hasTabOrEmoji && mText instanceof Spanned) {
844 // Just checking this line should be good enough, tabs should be
845 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700846 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700847 if (tabs.length > 0) {
848 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
851
Doug Felte8e45f22010-03-29 14:58:40 -0700852 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700853 tl.set(mPaint, mText, start, end, dir, directions, hasTabOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700854 float wid = tl.measure(offset - start, trailing, null);
855 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856
Raph Levienafe8e9b2012-12-19 16:09:32 -0800857 if (clamped && wid > mWidth) {
858 wid = mWidth;
859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 int left = getParagraphLeft(line);
861 int right = getParagraphRight(line);
862
Doug Feltc982f602010-05-25 11:51:40 -0700863 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 }
865
866 /**
867 * Get the leftmost position that should be exposed for horizontal
868 * scrolling on the specified line.
869 */
870 public float getLineLeft(int line) {
871 int dir = getParagraphDirection(line);
872 Alignment align = getParagraphAlignment(line);
873
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700874 if (align == Alignment.ALIGN_LEFT) {
875 return 0;
876 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 if (dir == DIR_RIGHT_TO_LEFT)
878 return getParagraphRight(line) - getLineMax(line);
879 else
880 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700881 } else if (align == Alignment.ALIGN_RIGHT) {
882 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 } else if (align == Alignment.ALIGN_OPPOSITE) {
884 if (dir == DIR_RIGHT_TO_LEFT)
885 return 0;
886 else
887 return mWidth - getLineMax(line);
888 } else { /* align == Alignment.ALIGN_CENTER */
889 int left = getParagraphLeft(line);
890 int right = getParagraphRight(line);
891 int max = ((int) getLineMax(line)) & ~1;
892
893 return left + ((right - left) - max) / 2;
894 }
895 }
896
897 /**
898 * Get the rightmost position that should be exposed for horizontal
899 * scrolling on the specified line.
900 */
901 public float getLineRight(int line) {
902 int dir = getParagraphDirection(line);
903 Alignment align = getParagraphAlignment(line);
904
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700905 if (align == Alignment.ALIGN_LEFT) {
906 return getParagraphLeft(line) + getLineMax(line);
907 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 if (dir == DIR_RIGHT_TO_LEFT)
909 return mWidth;
910 else
911 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700912 } else if (align == Alignment.ALIGN_RIGHT) {
913 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 } else if (align == Alignment.ALIGN_OPPOSITE) {
915 if (dir == DIR_RIGHT_TO_LEFT)
916 return getLineMax(line);
917 else
918 return mWidth;
919 } else { /* align == Alignment.ALIGN_CENTER */
920 int left = getParagraphLeft(line);
921 int right = getParagraphRight(line);
922 int max = ((int) getLineMax(line)) & ~1;
923
924 return right - ((right - left) - max) / 2;
925 }
926 }
927
928 /**
Doug Felt0c702b82010-05-14 10:55:42 -0700929 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -0700930 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 */
932 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700933 float margin = getParagraphLeadingMargin(line);
934 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -0700935 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
937
938 /**
Doug Feltc982f602010-05-25 11:51:40 -0700939 * Gets the unsigned horizontal extent of the specified line, including
940 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 */
942 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700943 float margin = getParagraphLeadingMargin(line);
944 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -0700945 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 }
947
Doug Feltc982f602010-05-25 11:51:40 -0700948 /**
949 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
950 * tab stops instead of using the ones passed in.
951 * @param line the index of the line
952 * @param full whether to include trailing whitespace
953 * @return the extent of the line
954 */
955 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700957 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -0700958
959 boolean hasTabsOrEmoji = getLineContainsTab(line);
960 TabStops tabStops = null;
961 if (hasTabsOrEmoji && mText instanceof Spanned) {
962 // Just checking this line should be good enough, tabs should be
963 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700964 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700965 if (tabs.length > 0) {
966 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
967 }
968 }
Doug Felte8e45f22010-03-29 14:58:40 -0700969 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700970 // Returned directions can actually be null
971 if (directions == null) {
972 return 0f;
973 }
Doug Feltc982f602010-05-25 11:51:40 -0700974 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975
Doug Felte8e45f22010-03-29 14:58:40 -0700976 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700977 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
978 float width = tl.metrics(null);
979 TextLine.recycle(tl);
980 return width;
981 }
982
983 /**
984 * Returns the signed horizontal extent of the specified line, excluding
985 * leading margin. If full is false, excludes trailing whitespace.
986 * @param line the index of the line
987 * @param tabStops the tab stops, can be null if we know they're not used.
988 * @param full whether to include trailing whitespace
989 * @return the extent of the text on this line
990 */
991 private float getLineExtent(int line, TabStops tabStops, boolean full) {
992 int start = getLineStart(line);
993 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
994 boolean hasTabsOrEmoji = getLineContainsTab(line);
995 Directions directions = getLineDirections(line);
996 int dir = getParagraphDirection(line);
997
998 TextLine tl = TextLine.obtain();
999 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001000 float width = tl.metrics(null);
1001 TextLine.recycle(tl);
1002 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
1004
1005 /**
1006 * Get the line number corresponding to the specified vertical position.
1007 * If you ask for a position above 0, you get 0; if you ask for a position
1008 * below the bottom of the text, you get the last line.
1009 */
1010 // FIXME: It may be faster to do a linear search for layouts without many lines.
1011 public int getLineForVertical(int vertical) {
1012 int high = getLineCount(), low = -1, guess;
1013
1014 while (high - low > 1) {
1015 guess = (high + low) / 2;
1016
1017 if (getLineTop(guess) > vertical)
1018 high = guess;
1019 else
1020 low = guess;
1021 }
1022
1023 if (low < 0)
1024 return 0;
1025 else
1026 return low;
1027 }
1028
1029 /**
1030 * Get the line number on which the specified text offset appears.
1031 * If you ask for a position before 0, you get 0; if you ask for a position
1032 * beyond the end of the text, you get the last line.
1033 */
1034 public int getLineForOffset(int offset) {
1035 int high = getLineCount(), low = -1, guess;
1036
1037 while (high - low > 1) {
1038 guess = (high + low) / 2;
1039
1040 if (getLineStart(guess) > offset)
1041 high = guess;
1042 else
1043 low = guess;
1044 }
1045
1046 if (low < 0)
1047 return 0;
1048 else
1049 return low;
1050 }
1051
1052 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001053 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 * closest to the specified horizontal position.
1055 */
1056 public int getOffsetForHorizontal(int line, float horiz) {
1057 int max = getLineEnd(line) - 1;
1058 int min = getLineStart(line);
1059 Directions dirs = getLineDirections(line);
1060
1061 if (line == getLineCount() - 1)
1062 max++;
1063
1064 int best = min;
1065 float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz);
1066
Doug Felt9f7a4442010-03-01 12:45:56 -08001067 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1068 int here = min + dirs.mDirections[i];
1069 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1070 int swap = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0 ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071
1072 if (there > max)
1073 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 int high = there - 1 + 1, low = here + 1 - 1, guess;
1075
1076 while (high - low > 1) {
1077 guess = (high + low) / 2;
1078 int adguess = getOffsetAtStartOf(guess);
1079
1080 if (getPrimaryHorizontal(adguess) * swap >= horiz * swap)
1081 high = guess;
1082 else
1083 low = guess;
1084 }
1085
1086 if (low < here + 1)
1087 low = here + 1;
1088
1089 if (low < there) {
1090 low = getOffsetAtStartOf(low);
1091
1092 float dist = Math.abs(getPrimaryHorizontal(low) - horiz);
1093
1094 int aft = TextUtils.getOffsetAfter(mText, low);
1095 if (aft < there) {
1096 float other = Math.abs(getPrimaryHorizontal(aft) - horiz);
1097
1098 if (other < dist) {
1099 dist = other;
1100 low = aft;
1101 }
1102 }
1103
1104 if (dist < bestdist) {
1105 bestdist = dist;
Doug Felt9f7a4442010-03-01 12:45:56 -08001106 best = low;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
1108 }
1109
1110 float dist = Math.abs(getPrimaryHorizontal(here) - horiz);
1111
1112 if (dist < bestdist) {
1113 bestdist = dist;
1114 best = here;
1115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
1117
1118 float dist = Math.abs(getPrimaryHorizontal(max) - horiz);
1119
Raph Levien373b7a82013-09-20 15:11:52 -07001120 if (dist <= bestdist) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 bestdist = dist;
1122 best = max;
1123 }
1124
1125 return best;
1126 }
1127
1128 /**
1129 * Return the text offset after the last character on the specified line.
1130 */
1131 public final int getLineEnd(int line) {
1132 return getLineStart(line + 1);
1133 }
1134
Doug Felt9f7a4442010-03-01 12:45:56 -08001135 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 * Return the text offset after the last visible character (so whitespace
1137 * is not counted) on the specified line.
1138 */
1139 public int getLineVisibleEnd(int line) {
1140 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1141 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 CharSequence text = mText;
1145 char ch;
1146 if (line == getLineCount() - 1) {
1147 return end;
1148 }
1149
1150 for (; end > start; end--) {
1151 ch = text.charAt(end - 1);
1152
1153 if (ch == '\n') {
1154 return end - 1;
1155 }
1156
1157 if (ch != ' ' && ch != '\t') {
1158 break;
1159 }
1160
1161 }
1162
1163 return end;
1164 }
1165
1166 /**
1167 * Return the vertical position of the bottom of the specified line.
1168 */
1169 public final int getLineBottom(int line) {
1170 return getLineTop(line + 1);
1171 }
1172
1173 /**
1174 * Return the vertical position of the baseline of the specified line.
1175 */
1176 public final int getLineBaseline(int line) {
1177 // getLineTop(line+1) == getLineTop(line)
1178 return getLineTop(line+1) - getLineDescent(line);
1179 }
1180
1181 /**
1182 * Get the ascent of the text on the specified line.
1183 * The return value is negative to match the Paint.ascent() convention.
1184 */
1185 public final int getLineAscent(int line) {
1186 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1187 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1188 }
1189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001191 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001195 return getOffsetToLeftRightOf(offset, false);
1196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197
Doug Felt9f7a4442010-03-01 12:45:56 -08001198 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1199 int line = getLineForOffset(caret);
1200 int lineStart = getLineStart(line);
1201 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001202 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001204 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001205 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001206 // if walking off line, look at the line we're headed to
1207 if (advance) {
1208 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001209 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001210 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001211 ++line;
1212 } else {
1213 return caret; // at very end, don't move
1214 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001215 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001216 } else {
1217 if (caret == lineStart) {
1218 if (line > 0) {
1219 lineChanged = true;
1220 --line;
1221 } else {
1222 return caret; // at very start, don't move
1223 }
1224 }
1225 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001226
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001227 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001228 lineStart = getLineStart(line);
1229 lineEnd = getLineEnd(line);
1230 int newDir = getParagraphDirection(line);
1231 if (newDir != lineDir) {
1232 // unusual case. we want to walk onto the line, but it runs
1233 // in a different direction than this one, so we fake movement
1234 // in the opposite direction.
1235 toLeft = !toLeft;
1236 lineDir = newDir;
1237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001239
Doug Felte8e45f22010-03-29 14:58:40 -07001240 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001241
Doug Felte8e45f22010-03-29 14:58:40 -07001242 TextLine tl = TextLine.obtain();
1243 // XXX: we don't care about tabs
1244 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1245 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1246 tl = TextLine.recycle(tl);
1247 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249
1250 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001251 // XXX this probably should skip local reorderings and
1252 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 if (offset == 0)
1254 return 0;
1255
1256 CharSequence text = mText;
1257 char c = text.charAt(offset);
1258
1259 if (c >= '\uDC00' && c <= '\uDFFF') {
1260 char c1 = text.charAt(offset - 1);
1261
1262 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1263 offset -= 1;
1264 }
1265
1266 if (mSpannedText) {
1267 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1268 ReplacementSpan.class);
1269
1270 for (int i = 0; i < spans.length; i++) {
1271 int start = ((Spanned) text).getSpanStart(spans[i]);
1272 int end = ((Spanned) text).getSpanEnd(spans[i]);
1273
1274 if (start < offset && end > offset)
1275 offset = start;
1276 }
1277 }
1278
1279 return offset;
1280 }
1281
1282 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001283 * Determine whether we should clamp cursor position. Currently it's
1284 * only robust for left-aligned displays.
1285 * @hide
1286 */
1287 public boolean shouldClampCursor(int line) {
1288 // Only clamp cursor position in left-aligned displays.
1289 switch (getParagraphAlignment(line)) {
1290 case ALIGN_LEFT:
1291 return true;
1292 case ALIGN_NORMAL:
1293 return getParagraphDirection(line) > 0;
1294 default:
1295 return false;
1296 }
1297
1298 }
1299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 * Fills in the specified Path with a representation of a cursor
1301 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001302 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 * directionalities.
1304 */
1305 public void getCursorPath(int point, Path dest,
1306 CharSequence editingBuffer) {
1307 dest.reset();
1308
1309 int line = getLineForOffset(point);
1310 int top = getLineTop(line);
1311 int bottom = getLineTop(line+1);
1312
Raph Levienafe8e9b2012-12-19 16:09:32 -08001313 boolean clamped = shouldClampCursor(line);
1314 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1315 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316
Jeff Brown497a92c2010-09-12 17:55:08 -07001317 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1318 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1319 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 int dist = 0;
1321
1322 if (caps != 0 || fn != 0) {
1323 dist = (bottom - top) >> 2;
1324
1325 if (fn != 0)
1326 top += dist;
1327 if (caps != 0)
1328 bottom -= dist;
1329 }
1330
1331 if (h1 < 0.5f)
1332 h1 = 0.5f;
1333 if (h2 < 0.5f)
1334 h2 = 0.5f;
1335
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001336 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 dest.moveTo(h1, top);
1338 dest.lineTo(h1, bottom);
1339 } else {
1340 dest.moveTo(h1, top);
1341 dest.lineTo(h1, (top + bottom) >> 1);
1342
1343 dest.moveTo(h2, (top + bottom) >> 1);
1344 dest.lineTo(h2, bottom);
1345 }
1346
1347 if (caps == 2) {
1348 dest.moveTo(h2, bottom);
1349 dest.lineTo(h2 - dist, bottom + dist);
1350 dest.lineTo(h2, bottom);
1351 dest.lineTo(h2 + dist, bottom + dist);
1352 } else if (caps == 1) {
1353 dest.moveTo(h2, bottom);
1354 dest.lineTo(h2 - dist, bottom + dist);
1355
1356 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1357 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1358
1359 dest.moveTo(h2 + dist, bottom + dist);
1360 dest.lineTo(h2, bottom);
1361 }
1362
1363 if (fn == 2) {
1364 dest.moveTo(h1, top);
1365 dest.lineTo(h1 - dist, top - dist);
1366 dest.lineTo(h1, top);
1367 dest.lineTo(h1 + dist, top - dist);
1368 } else if (fn == 1) {
1369 dest.moveTo(h1, top);
1370 dest.lineTo(h1 - dist, top - dist);
1371
1372 dest.moveTo(h1 - dist, top - dist + 0.5f);
1373 dest.lineTo(h1 + dist, top - dist + 0.5f);
1374
1375 dest.moveTo(h1 + dist, top - dist);
1376 dest.lineTo(h1, top);
1377 }
1378 }
1379
1380 private void addSelection(int line, int start, int end,
1381 int top, int bottom, Path dest) {
1382 int linestart = getLineStart(line);
1383 int lineend = getLineEnd(line);
1384 Directions dirs = getLineDirections(line);
1385
1386 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1387 lineend--;
1388
Doug Felt9f7a4442010-03-01 12:45:56 -08001389 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1390 int here = linestart + dirs.mDirections[i];
1391 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 if (there > lineend)
1394 there = lineend;
1395
1396 if (start <= there && end >= here) {
1397 int st = Math.max(start, here);
1398 int en = Math.min(end, there);
1399
1400 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001401 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1402 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001404 float left = Math.min(h1, h2);
1405 float right = Math.max(h1, h2);
1406
1407 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 }
1411 }
1412
1413 /**
1414 * Fills in the specified Path with a representation of a highlight
1415 * between the specified offsets. This will often be a rectangle
1416 * or a potentially discontinuous set of rectangles. If the start
1417 * and end are the same, the returned path is empty.
1418 */
1419 public void getSelectionPath(int start, int end, Path dest) {
1420 dest.reset();
1421
1422 if (start == end)
1423 return;
1424
1425 if (end < start) {
1426 int temp = end;
1427 end = start;
1428 start = temp;
1429 }
1430
1431 int startline = getLineForOffset(start);
1432 int endline = getLineForOffset(end);
1433
1434 int top = getLineTop(startline);
1435 int bottom = getLineBottom(endline);
1436
1437 if (startline == endline) {
1438 addSelection(startline, start, end, top, bottom, dest);
1439 } else {
1440 final float width = mWidth;
1441
1442 addSelection(startline, start, getLineEnd(startline),
1443 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1446 dest.addRect(getLineLeft(startline), top,
1447 0, getLineBottom(startline), Path.Direction.CW);
1448 else
1449 dest.addRect(getLineRight(startline), top,
1450 width, getLineBottom(startline), Path.Direction.CW);
1451
1452 for (int i = startline + 1; i < endline; i++) {
1453 top = getLineTop(i);
1454 bottom = getLineBottom(i);
1455 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1456 }
1457
1458 top = getLineTop(endline);
1459 bottom = getLineBottom(endline);
1460
1461 addSelection(endline, getLineStart(endline), end,
1462 top, bottom, dest);
1463
1464 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1465 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1466 else
1467 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1468 }
1469 }
1470
1471 /**
1472 * Get the alignment of the specified paragraph, taking into account
1473 * markup attached to it.
1474 */
1475 public final Alignment getParagraphAlignment(int line) {
1476 Alignment align = mAlignment;
1477
1478 if (mSpannedText) {
1479 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001480 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 getLineEnd(line),
1482 AlignmentSpan.class);
1483
1484 int spanLength = spans.length;
1485 if (spanLength > 0) {
1486 align = spans[spanLength-1].getAlignment();
1487 }
1488 }
1489
1490 return align;
1491 }
1492
1493 /**
1494 * Get the left edge of the specified paragraph, inset by left margins.
1495 */
1496 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001498 int dir = getParagraphDirection(line);
1499 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1500 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 }
Doug Feltc982f602010-05-25 11:51:40 -07001502 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 }
1504
1505 /**
1506 * Get the right edge of the specified paragraph, inset by right margins.
1507 */
1508 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001510 int dir = getParagraphDirection(line);
1511 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1512 return right; // leading margin has no impact, or no styles
1513 }
1514 return right - getParagraphLeadingMargin(line);
1515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516
Doug Feltc982f602010-05-25 11:51:40 -07001517 /**
1518 * Returns the effective leading margin (unsigned) for this line,
1519 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1520 * @param line the line index
1521 * @return the leading margin of this line
1522 */
1523 private int getParagraphLeadingMargin(int line) {
1524 if (!mSpannedText) {
1525 return 0;
1526 }
1527 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001528
Doug Feltc982f602010-05-25 11:51:40 -07001529 int lineStart = getLineStart(line);
1530 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001531 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001532 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001533 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001534 LeadingMarginSpan.class);
1535 if (spans.length == 0) {
1536 return 0; // no leading margin span;
1537 }
Doug Felt0c702b82010-05-14 10:55:42 -07001538
Doug Feltc982f602010-05-25 11:51:40 -07001539 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001540
1541 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001542 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001543
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001544 boolean useFirstLineMargin = isFirstParaLine;
1545 for (int i = 0; i < spans.length; i++) {
1546 if (spans[i] instanceof LeadingMarginSpan2) {
1547 int spStart = spanned.getSpanStart(spans[i]);
1548 int spanLine = getLineForOffset(spStart);
1549 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1550 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1551 useFirstLineMargin |= line < spanLine + count;
1552 }
1553 }
Doug Feltc982f602010-05-25 11:51:40 -07001554 for (int i = 0; i < spans.length; i++) {
1555 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001556 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 }
1558
Doug Feltc982f602010-05-25 11:51:40 -07001559 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
1561
Doug Felte8e45f22010-03-29 14:58:40 -07001562 /* package */
Gilles Debunne6c488de2012-03-01 16:20:35 -08001563 static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
Doug Felte8e45f22010-03-29 14:58:40 -07001564
1565 MeasuredText mt = MeasuredText.obtain();
1566 TextLine tl = TextLine.obtain();
1567 try {
Doug Feltcb3791202011-07-07 11:57:48 -07001568 mt.setPara(text, start, end, TextDirectionHeuristics.LTR);
Doug Felte8e45f22010-03-29 14:58:40 -07001569 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001570 int dir;
1571 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001572 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001573 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001574 } else {
1575 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1576 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001577 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 }
Doug Feltc982f602010-05-25 11:51:40 -07001579 char[] chars = mt.mChars;
1580 int len = mt.mLen;
1581 boolean hasTabs = false;
1582 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001583 // leading margins should be taken into account when measuring a paragraph
1584 int margin = 0;
1585 if (text instanceof Spanned) {
1586 Spanned spanned = (Spanned) text;
1587 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1588 LeadingMarginSpan.class);
1589 for (LeadingMarginSpan lms : spans) {
1590 margin += lms.getLeadingMargin(true);
1591 }
1592 }
Doug Feltc982f602010-05-25 11:51:40 -07001593 for (int i = 0; i < len; ++i) {
1594 if (chars[i] == '\t') {
1595 hasTabs = true;
1596 if (text instanceof Spanned) {
1597 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001598 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001599 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001600 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001601 TabStopSpan.class);
1602 if (spans.length > 0) {
1603 tabStops = new TabStops(TAB_INCREMENT, spans);
1604 }
1605 }
1606 break;
1607 }
1608 }
1609 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001610 return margin + tl.metrics(null);
Doug Felte8e45f22010-03-29 14:58:40 -07001611 } finally {
1612 TextLine.recycle(tl);
1613 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 }
1616
Doug Felt71b8dd72010-02-16 17:27:09 -08001617 /**
Doug Feltc982f602010-05-25 11:51:40 -07001618 * @hide
1619 */
1620 /* package */ static class TabStops {
1621 private int[] mStops;
1622 private int mNumStops;
1623 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001624
Doug Feltc982f602010-05-25 11:51:40 -07001625 TabStops(int increment, Object[] spans) {
1626 reset(increment, spans);
1627 }
Doug Felt0c702b82010-05-14 10:55:42 -07001628
Doug Feltc982f602010-05-25 11:51:40 -07001629 void reset(int increment, Object[] spans) {
1630 this.mIncrement = increment;
1631
1632 int ns = 0;
1633 if (spans != null) {
1634 int[] stops = this.mStops;
1635 for (Object o : spans) {
1636 if (o instanceof TabStopSpan) {
1637 if (stops == null) {
1638 stops = new int[10];
1639 } else if (ns == stops.length) {
1640 int[] nstops = new int[ns * 2];
1641 for (int i = 0; i < ns; ++i) {
1642 nstops[i] = stops[i];
1643 }
1644 stops = nstops;
1645 }
1646 stops[ns++] = ((TabStopSpan) o).getTabStop();
1647 }
1648 }
1649 if (ns > 1) {
1650 Arrays.sort(stops, 0, ns);
1651 }
1652 if (stops != this.mStops) {
1653 this.mStops = stops;
1654 }
1655 }
1656 this.mNumStops = ns;
1657 }
Doug Felt0c702b82010-05-14 10:55:42 -07001658
Doug Feltc982f602010-05-25 11:51:40 -07001659 float nextTab(float h) {
1660 int ns = this.mNumStops;
1661 if (ns > 0) {
1662 int[] stops = this.mStops;
1663 for (int i = 0; i < ns; ++i) {
1664 int stop = stops[i];
1665 if (stop > h) {
1666 return stop;
1667 }
1668 }
1669 }
1670 return nextDefaultStop(h, mIncrement);
1671 }
1672
1673 public static float nextDefaultStop(float h, int inc) {
1674 return ((int) ((h + inc) / inc)) * inc;
1675 }
1676 }
Doug Felt0c702b82010-05-14 10:55:42 -07001677
Doug Feltc982f602010-05-25 11:51:40 -07001678 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001679 * Returns the position of the next tab stop after h on the line.
1680 *
1681 * @param text the text
1682 * @param start start of the line
1683 * @param end limit of the line
1684 * @param h the current horizontal offset
1685 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1686 * on the line will be used. If there are no tabs, a default offset
1687 * will be used to compute the tab stop.
1688 * @return the offset of the next tab stop.
1689 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 /* package */ static float nextTab(CharSequence text, int start, int end,
1691 float h, Object[] tabs) {
1692 float nh = Float.MAX_VALUE;
1693 boolean alltabs = false;
1694
1695 if (text instanceof Spanned) {
1696 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001697 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 alltabs = true;
1699 }
1700
1701 for (int i = 0; i < tabs.length; i++) {
1702 if (!alltabs) {
1703 if (!(tabs[i] instanceof TabStopSpan))
1704 continue;
1705 }
1706
1707 int where = ((TabStopSpan) tabs[i]).getTabStop();
1708
1709 if (where < nh && where > h)
1710 nh = where;
1711 }
1712
1713 if (nh != Float.MAX_VALUE)
1714 return nh;
1715 }
1716
1717 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1718 }
1719
1720 protected final boolean isSpanned() {
1721 return mSpannedText;
1722 }
1723
Eric Fischer74d31ef2010-08-05 15:29:36 -07001724 /**
1725 * Returns the same as <code>text.getSpans()</code>, except where
1726 * <code>start</code> and <code>end</code> are the same and are not
1727 * at the very beginning of the text, in which case an empty array
1728 * is returned instead.
1729 * <p>
1730 * This is needed because of the special case that <code>getSpans()</code>
1731 * on an empty range returns the spans adjacent to that range, which is
1732 * primarily for the sake of <code>TextWatchers</code> so they will get
1733 * notifications when text goes from empty to non-empty. But it also
1734 * has the unfortunate side effect that if the text ends with an empty
1735 * paragraph, that paragraph accidentally picks up the styles of the
1736 * preceding paragraph (even though those styles will not be picked up
1737 * by new text that is inserted into the empty paragraph).
1738 * <p>
1739 * The reason it just checks whether <code>start</code> and <code>end</code>
1740 * is the same is that the only time a line can contain 0 characters
1741 * is if it is the final paragraph of the Layout; otherwise any line will
1742 * contain at least one printing or newline character. The reason for the
1743 * additional check if <code>start</code> is greater than 0 is that
1744 * if the empty paragraph is the entire content of the buffer, paragraph
1745 * styles that are already applied to the buffer will apply to text that
1746 * is inserted into it.
1747 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07001748 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001749 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08001750 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001751 }
1752
1753 return text.getSpans(start, end, type);
1754 }
1755
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001756 private char getEllipsisChar(TextUtils.TruncateAt method) {
1757 return (method == TextUtils.TruncateAt.END_SMALL) ?
1758 ELLIPSIS_TWO_DOTS[0] :
1759 ELLIPSIS_NORMAL[0];
1760 }
1761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001763 char[] dest, int destoff, TextUtils.TruncateAt method) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 int ellipsisCount = getEllipsisCount(line);
1765
1766 if (ellipsisCount == 0) {
1767 return;
1768 }
1769
1770 int ellipsisStart = getEllipsisStart(line);
1771 int linestart = getLineStart(line);
1772
1773 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1774 char c;
1775
1776 if (i == ellipsisStart) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001777 c = getEllipsisChar(method); // ellipsis
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 } else {
1779 c = '\uFEFF'; // 0-width space
1780 }
1781
1782 int a = i + linestart;
1783
1784 if (a >= start && a < end) {
1785 dest[destoff + a - start] = c;
1786 }
1787 }
1788 }
1789
1790 /**
1791 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08001792 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 */
1794 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08001795 // Directions represents directional runs within a line of text.
1796 // Runs are pairs of ints listed in visual order, starting from the
1797 // leading margin. The first int of each pair is the offset from
1798 // the first character of the line to the start of the run. The
1799 // second int represents both the length and level of the run.
1800 // The length is in the lower bits, accessed by masking with
1801 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1802 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1803 // To simply test for an RTL direction, test the bit using
1804 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805
Doug Felt9f7a4442010-03-01 12:45:56 -08001806 /* package */ int[] mDirections;
1807 /* package */ Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 mDirections = dirs;
1809 }
1810 }
1811
1812 /**
1813 * Return the offset of the first character to be ellipsized away,
1814 * relative to the start of the line. (So 0 if the beginning of the
1815 * line is ellipsized, not getLineStart().)
1816 */
1817 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07001818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 /**
1820 * Returns the number of characters to be ellipsized away, or 0 if
1821 * no ellipsis is to take place.
1822 */
1823 public abstract int getEllipsisCount(int line);
1824
1825 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1826 /* package */ CharSequence mText;
1827 /* package */ Layout mLayout;
1828 /* package */ int mWidth;
1829 /* package */ TextUtils.TruncateAt mMethod;
1830
1831 public Ellipsizer(CharSequence s) {
1832 mText = s;
1833 }
1834
1835 public char charAt(int off) {
1836 char[] buf = TextUtils.obtain(1);
1837 getChars(off, off + 1, buf, 0);
1838 char ret = buf[0];
1839
1840 TextUtils.recycle(buf);
1841 return ret;
1842 }
1843
1844 public void getChars(int start, int end, char[] dest, int destoff) {
1845 int line1 = mLayout.getLineForOffset(start);
1846 int line2 = mLayout.getLineForOffset(end);
1847
1848 TextUtils.getChars(mText, start, end, dest, destoff);
1849
1850 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001851 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
1853 }
1854
1855 public int length() {
1856 return mText.length();
1857 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 public CharSequence subSequence(int start, int end) {
1860 char[] s = new char[end - start];
1861 getChars(start, end, s, 0);
1862 return new String(s);
1863 }
1864
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001865 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 public String toString() {
1867 char[] s = new char[length()];
1868 getChars(0, length(), s, 0);
1869 return new String(s);
1870 }
1871
1872 }
1873
Gilles Debunne6c488de2012-03-01 16:20:35 -08001874 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 private Spanned mSpanned;
1876
1877 public SpannedEllipsizer(CharSequence display) {
1878 super(display);
1879 mSpanned = (Spanned) display;
1880 }
1881
1882 public <T> T[] getSpans(int start, int end, Class<T> type) {
1883 return mSpanned.getSpans(start, end, type);
1884 }
1885
1886 public int getSpanStart(Object tag) {
1887 return mSpanned.getSpanStart(tag);
1888 }
1889
1890 public int getSpanEnd(Object tag) {
1891 return mSpanned.getSpanEnd(tag);
1892 }
1893
1894 public int getSpanFlags(Object tag) {
1895 return mSpanned.getSpanFlags(tag);
1896 }
1897
Gilles Debunne6c488de2012-03-01 16:20:35 -08001898 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 public int nextSpanTransition(int start, int limit, Class type) {
1900 return mSpanned.nextSpanTransition(start, limit, type);
1901 }
1902
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001903 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 public CharSequence subSequence(int start, int end) {
1905 char[] s = new char[end - start];
1906 getChars(start, end, s, 0);
1907
1908 SpannableString ss = new SpannableString(new String(s));
1909 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
1910 return ss;
1911 }
1912 }
1913
1914 private CharSequence mText;
1915 private TextPaint mPaint;
1916 /* package */ TextPaint mWorkPaint;
1917 private int mWidth;
1918 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
1919 private float mSpacingMult;
1920 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07001921 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07001923 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07001924 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925
1926 public static final int DIR_LEFT_TO_RIGHT = 1;
1927 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08001928
Doug Felt20178d62010-02-22 13:39:01 -08001929 /* package */ static final int DIR_REQUEST_LTR = 1;
1930 /* package */ static final int DIR_REQUEST_RTL = -1;
1931 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
1932 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933
Doug Felt9f7a4442010-03-01 12:45:56 -08001934 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
1935 /* package */ static final int RUN_LEVEL_SHIFT = 26;
1936 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
1937 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
1938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 public enum Alignment {
1940 ALIGN_NORMAL,
1941 ALIGN_OPPOSITE,
1942 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001943 /** @hide */
1944 ALIGN_LEFT,
1945 /** @hide */
1946 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 }
1948
1949 private static final int TAB_INCREMENT = 20;
1950
1951 /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001952 new Directions(new int[] { 0, RUN_LENGTH_MASK });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001954 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08001955
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001956 /* package */ static final char[] ELLIPSIS_NORMAL = { '\u2026' }; // this is "..."
1957 /* package */ static final char[] ELLIPSIS_TWO_DOTS = { '\u2025' }; // this is ".."
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958}