blob: a36f06c79a5807e4bcbf1ce34e42af83c5c98624 [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;
276 for (int n = 0; n < length; n++) {
277 if (spans[n] instanceof LeadingMarginSpan) {
278 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
Doug Feltc982f602010-05-25 11:51:40 -0700279 boolean useFirstLineMargin = isFirstParaLine;
280 if (margin instanceof LeadingMarginSpan2) {
281 int count = ((LeadingMarginSpan2) margin).getLeadingMarginLineCount();
282 int startLine = getLineForOffset(sp.getSpanStart(margin));
283 useFirstLineMargin = i < startLine + count;
284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285
286 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800287 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800289 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700290 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800292 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800294 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700295 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 }
297 }
298 }
299 }
300
Doug Feltc982f602010-05-25 11:51:40 -0700301 boolean hasTabOrEmoji = getLineContainsTab(i);
302 // Can't tell if we have tabs for sure, currently
303 if (hasTabOrEmoji && !tabStopsIsInitialized) {
304 if (tabStops == null) {
305 tabStops = new TabStops(TAB_INCREMENT, spans);
306 } else {
307 tabStops.reset(TAB_INCREMENT, spans);
308 }
309 tabStopsIsInitialized = true;
310 }
311
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700312 // Determine whether the line aligns to normal, opposite, or center.
313 Alignment align = paraAlign;
314 if (align == Alignment.ALIGN_LEFT) {
315 align = (dir == DIR_LEFT_TO_RIGHT) ?
316 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
317 } else if (align == Alignment.ALIGN_RIGHT) {
318 align = (dir == DIR_LEFT_TO_RIGHT) ?
319 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
320 }
321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 int x;
323 if (align == Alignment.ALIGN_NORMAL) {
324 if (dir == DIR_LEFT_TO_RIGHT) {
325 x = left;
326 } else {
327 x = right;
328 }
329 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700330 int max = (int)getLineExtent(i, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700332 if (dir == DIR_LEFT_TO_RIGHT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 x = right - max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700335 x = left - max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
Doug Feltc982f602010-05-25 11:51:40 -0700337 } else { // Alignment.ALIGN_CENTER
338 max = max & ~1;
339 x = (right + left - max) >> 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 }
341 }
342
343 Directions directions = getLineDirections(i);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800344 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTabOrEmoji) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800345 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800346 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700348 tl.set(paint, buf, start, end, dir, directions, hasTabOrEmoji, tabStops);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800349 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 }
351 }
Doug Feltc982f602010-05-25 11:51:40 -0700352
Doug Felte8e45f22010-03-29 14:58:40 -0700353 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 }
355
356 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800357 * @hide
358 */
359 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
360 int cursorOffsetVertical, int firstLine, int lastLine) {
361 // First, draw LineBackgroundSpans.
362 // LineBackgroundSpans know nothing about the alignment, margins, or
363 // direction of the layout or line. XXX: Should they?
364 // They are evaluated at each line.
365 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700366 if (mLineBackgroundSpans == null) {
367 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700368 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800369
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700370 Spanned buffer = (Spanned) mText;
371 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700372 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800373
Gilles Debunneeca5b732012-04-25 18:48:42 -0700374 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700375 int previousLineBottom = getLineTop(firstLine);
376 int previousLineEnd = getLineStart(firstLine);
377 ParagraphStyle[] spans = NO_PARA_SPANS;
378 int spansLength = 0;
379 TextPaint paint = mPaint;
380 int spanEnd = 0;
381 final int width = mWidth;
382 for (int i = firstLine; i <= lastLine; i++) {
383 int start = previousLineEnd;
384 int end = getLineStart(i + 1);
385 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800386
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700387 int ltop = previousLineBottom;
388 int lbottom = getLineTop(i + 1);
389 previousLineBottom = lbottom;
390 int lbaseline = lbottom - getLineDescent(i);
391
392 if (start >= spanEnd) {
393 // These should be infrequent, so we'll use this so that
394 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700395 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700396 // All LineBackgroundSpans on a line contribute to its background.
397 spansLength = 0;
398 // Duplication of the logic of getParagraphSpans
399 if (start != end || start == 0) {
400 // Equivalent to a getSpans(start, end), but filling the 'spans' local
401 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700402 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
403 // equal test is valid since both intervals are not empty by
404 // construction
405 if (mLineBackgroundSpans.spanStarts[j] >= end ||
406 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500407 spans = GrowingArrayUtils.append(
408 spans, spansLength, mLineBackgroundSpans.spans[j]);
409 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700410 }
411 }
412 }
413
414 for (int n = 0; n < spansLength; n++) {
415 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
416 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
417 ltop, lbaseline, lbottom,
418 buffer, start, end, i);
419 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800420 }
421 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700422 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800423 }
424
425 // There can be a highlight even without spans if we are drawing
426 // a non-spanned transformation of a spanned editing buffer.
427 if (highlight != null) {
428 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
429 canvas.drawPath(highlight, highlightPaint);
430 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
431 }
432 }
433
434 /**
435 * @param canvas
436 * @return The range of lines that need to be drawn, possibly empty.
437 * @hide
438 */
439 public long getLineRangeForDraw(Canvas canvas) {
440 int dtop, dbottom;
441
442 synchronized (sTempRect) {
443 if (!canvas.getClipBounds(sTempRect)) {
444 // Negative range end used as a special flag
445 return TextUtils.packRangeInLong(0, -1);
446 }
447
448 dtop = sTempRect.top;
449 dbottom = sTempRect.bottom;
450 }
451
452 final int top = Math.max(dtop, 0);
453 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
454
Gilles Debunne2fba3382012-06-11 17:46:24 -0700455 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800456 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
457 }
458
459 /**
Doug Feltc982f602010-05-25 11:51:40 -0700460 * Return the start position of the line, given the left and right bounds
461 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700462 *
Doug Feltc982f602010-05-25 11:51:40 -0700463 * @param line the line index
464 * @param left the left bounds (0, or leading margin if ltr para)
465 * @param right the right bounds (width, minus leading margin if rtl para)
466 * @return the start position of the line (to right of line if rtl para)
467 */
468 private int getLineStartPos(int line, int left, int right) {
469 // Adjust the point at which to start rendering depending on the
470 // alignment of the paragraph.
471 Alignment align = getParagraphAlignment(line);
472 int dir = getParagraphDirection(line);
473
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700474 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700475 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
476 } else if (align == Alignment.ALIGN_RIGHT) {
477 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
478 }
479
480 int x;
481 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700482 if (dir == DIR_LEFT_TO_RIGHT) {
483 x = left;
484 } else {
485 x = right;
486 }
487 } else {
488 TabStops tabStops = null;
489 if (mSpannedText && getLineContainsTab(line)) {
490 Spanned spanned = (Spanned) mText;
491 int start = getLineStart(line);
492 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
493 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800494 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
495 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700496 if (tabSpans.length > 0) {
497 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
498 }
499 }
500 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700501 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700502 if (dir == DIR_LEFT_TO_RIGHT) {
503 x = right - max;
504 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700505 // max is negative here
Doug Feltc982f602010-05-25 11:51:40 -0700506 x = left - max;
507 }
508 } else { // Alignment.ALIGN_CENTER
509 max = max & ~1;
510 x = (left + right - max) >> 1;
511 }
512 }
513 return x;
514 }
515
516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 * Return the text that is displayed by this Layout.
518 */
519 public final CharSequence getText() {
520 return mText;
521 }
522
523 /**
524 * Return the base Paint properties for this layout.
525 * Do NOT change the paint, which may result in funny
526 * drawing for this layout.
527 */
528 public final TextPaint getPaint() {
529 return mPaint;
530 }
531
532 /**
533 * Return the width of this layout.
534 */
535 public final int getWidth() {
536 return mWidth;
537 }
538
539 /**
540 * Return the width to which this Layout is ellipsizing, or
541 * {@link #getWidth} if it is not doing anything special.
542 */
543 public int getEllipsizedWidth() {
544 return mWidth;
545 }
546
547 /**
548 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800549 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 * it does not cause the text to reflow to use the full new width.
551 */
552 public final void increaseWidthTo(int wid) {
553 if (wid < mWidth) {
554 throw new RuntimeException("attempted to reduce Layout width");
555 }
556
557 mWidth = wid;
558 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 /**
561 * Return the total height of this layout.
562 */
563 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800564 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566
567 /**
568 * Return the base alignment of this layout.
569 */
570 public final Alignment getAlignment() {
571 return mAlignment;
572 }
573
574 /**
575 * Return what the text height is multiplied by to get the line height.
576 */
577 public final float getSpacingMultiplier() {
578 return mSpacingMult;
579 }
580
581 /**
582 * Return the number of units of leading that are added to each line.
583 */
584 public final float getSpacingAdd() {
585 return mSpacingAdd;
586 }
587
588 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700589 * Return the heuristic used to determine paragraph text direction.
590 * @hide
591 */
592 public final TextDirectionHeuristic getTextDirectionHeuristic() {
593 return mTextDir;
594 }
595
596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 * Return the number of lines of text in this layout.
598 */
599 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 /**
602 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
603 * If bounds is not null, return the top, left, right, bottom extents
604 * of the specified line in it.
605 * @param line which line to examine (0..getLineCount() - 1)
606 * @param bounds Optional. If not null, it returns the extent of the line
607 * @return the Y-coordinate of the baseline
608 */
609 public int getLineBounds(int line, Rect bounds) {
610 if (bounds != null) {
611 bounds.left = 0; // ???
612 bounds.top = getLineTop(line);
613 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800614 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
616 return getLineBaseline(line);
617 }
618
619 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800620 * Return the vertical position of the top of the specified line
621 * (0&hellip;getLineCount()).
622 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * bottom of the last line.
624 */
625 public abstract int getLineTop(int line);
626
627 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800628 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 */
630 public abstract int getLineDescent(int line);
631
632 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800633 * Return the text offset of the beginning of the specified line (
634 * 0&hellip;getLineCount()). If the specified line is equal to the line
635 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 */
637 public abstract int getLineStart(int line);
638
639 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800640 * Returns the primary directionality of the paragraph containing the
641 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
642 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 */
644 public abstract int getParagraphDirection(int line);
645
646 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700647 * Returns whether the specified line contains one or more
648 * characters that need to be handled specially, like tabs
649 * or emoji.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 */
651 public abstract boolean getLineContainsTab(int line);
652
653 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800654 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 * The array alternates counts of characters in left-to-right
656 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800657 *
658 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 */
660 public abstract Directions getLineDirections(int line);
661
662 /**
663 * Returns the (negative) number of extra pixels of ascent padding in the
664 * top line of the Layout.
665 */
666 public abstract int getTopPadding();
667
668 /**
669 * Returns the number of extra pixels of descent padding in the
670 * bottom line of the Layout.
671 */
672 public abstract int getBottomPadding();
673
Doug Felt4e0c5e52010-03-15 16:56:02 -0700674
675 /**
676 * Returns true if the character at offset and the preceding character
677 * are at different run levels (and thus there's a split caret).
678 * @param offset the offset
679 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800680 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700681 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800682 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800683 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700684 Directions dirs = getLineDirections(line);
685 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
686 return false;
687 }
688
689 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800690 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700691 int lineEnd = getLineEnd(line);
692 if (offset == lineStart || offset == lineEnd) {
693 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
694 int runIndex = offset == lineStart ? 0 : runs.length - 2;
695 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
696 }
697
698 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800699 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700700 if (offset == runs[i]) {
701 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800702 }
703 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700704 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800705 }
706
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700707 /**
708 * Returns true if the character at offset is right to left (RTL).
709 * @param offset the offset
710 * @return true if the character is RTL, false if it is LTR
711 */
712 public boolean isRtlCharAt(int offset) {
713 int line = getLineForOffset(offset);
714 Directions dirs = getLineDirections(line);
715 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
716 return false;
717 }
718 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
719 return true;
720 }
721 int[] runs = dirs.mDirections;
722 int lineStart = getLineStart(line);
723 for (int i = 0; i < runs.length; i += 2) {
724 int start = lineStart + (runs[i] & RUN_LENGTH_MASK);
725 // No need to test the end as an offset after the last run should return the value
726 // corresponding of the last run
727 if (offset >= start) {
728 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
729 return ((level & 1) != 0);
730 }
731 }
732 // Should happen only if the offset is "out of bounds"
733 return false;
734 }
735
Doug Felt9f7a4442010-03-01 12:45:56 -0800736 private boolean primaryIsTrailingPrevious(int offset) {
737 int line = getLineForOffset(offset);
738 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700739 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800740 int[] runs = getLineDirections(line).mDirections;
741
742 int levelAt = -1;
743 for (int i = 0; i < runs.length; i += 2) {
744 int start = lineStart + runs[i];
745 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
746 if (limit > lineEnd) {
747 limit = lineEnd;
748 }
749 if (offset >= start && offset < limit) {
750 if (offset > start) {
751 // Previous character is at same level, so don't use trailing.
752 return false;
753 }
754 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
755 break;
756 }
757 }
758 if (levelAt == -1) {
759 // Offset was limit of line.
760 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
761 }
762
763 // At level boundary, check previous level.
764 int levelBefore = -1;
765 if (offset == lineStart) {
766 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
767 } else {
768 offset -= 1;
769 for (int i = 0; i < runs.length; i += 2) {
770 int start = lineStart + runs[i];
771 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
772 if (limit > lineEnd) {
773 limit = lineEnd;
774 }
775 if (offset >= start && offset < limit) {
776 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
777 break;
778 }
779 }
780 }
781
782 return levelBefore < levelAt;
783 }
784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 /**
786 * Get the primary horizontal position for the specified text offset.
787 * This is the location where a new character would be inserted in
788 * the paragraph's primary direction.
789 */
790 public float getPrimaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800791 return getPrimaryHorizontal(offset, false /* not clamped */);
792 }
793
794 /**
795 * Get the primary horizontal position for the specified text offset, but
796 * optionally clamp it so that it doesn't exceed the width of the layout.
797 * @hide
798 */
799 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800800 boolean trailing = primaryIsTrailingPrevious(offset);
Raph Levienafe8e9b2012-12-19 16:09:32 -0800801 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 }
803
804 /**
805 * Get the secondary horizontal position for the specified text offset.
806 * This is the location where a new character would be inserted in
807 * the direction other than the paragraph's primary direction.
808 */
809 public float getSecondaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800810 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 }
812
Raph Levienafe8e9b2012-12-19 16:09:32 -0800813 /**
814 * Get the secondary horizontal position for the specified text offset, but
815 * optionally clamp it so that it doesn't exceed the width of the layout.
816 * @hide
817 */
818 public float getSecondaryHorizontal(int offset, boolean clamped) {
819 boolean trailing = primaryIsTrailingPrevious(offset);
820 return getHorizontal(offset, !trailing, clamped);
821 }
822
823 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 int line = getLineForOffset(offset);
825
Raph Levienafe8e9b2012-12-19 16:09:32 -0800826 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 }
828
Raph Levienafe8e9b2012-12-19 16:09:32 -0800829 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700831 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 int dir = getParagraphDirection(line);
Doug Feltc982f602010-05-25 11:51:40 -0700833 boolean hasTabOrEmoji = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 Directions directions = getLineDirections(line);
835
Doug Feltc982f602010-05-25 11:51:40 -0700836 TabStops tabStops = null;
837 if (hasTabOrEmoji && mText instanceof Spanned) {
838 // Just checking this line should be good enough, tabs should be
839 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700840 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700841 if (tabs.length > 0) {
842 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
845
Doug Felte8e45f22010-03-29 14:58:40 -0700846 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700847 tl.set(mPaint, mText, start, end, dir, directions, hasTabOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700848 float wid = tl.measure(offset - start, trailing, null);
849 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850
Raph Levienafe8e9b2012-12-19 16:09:32 -0800851 if (clamped && wid > mWidth) {
852 wid = mWidth;
853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 int left = getParagraphLeft(line);
855 int right = getParagraphRight(line);
856
Doug Feltc982f602010-05-25 11:51:40 -0700857 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
859
860 /**
861 * Get the leftmost position that should be exposed for horizontal
862 * scrolling on the specified line.
863 */
864 public float getLineLeft(int line) {
865 int dir = getParagraphDirection(line);
866 Alignment align = getParagraphAlignment(line);
867
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700868 if (align == Alignment.ALIGN_LEFT) {
869 return 0;
870 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 if (dir == DIR_RIGHT_TO_LEFT)
872 return getParagraphRight(line) - getLineMax(line);
873 else
874 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700875 } else if (align == Alignment.ALIGN_RIGHT) {
876 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 } else if (align == Alignment.ALIGN_OPPOSITE) {
878 if (dir == DIR_RIGHT_TO_LEFT)
879 return 0;
880 else
881 return mWidth - getLineMax(line);
882 } else { /* align == Alignment.ALIGN_CENTER */
883 int left = getParagraphLeft(line);
884 int right = getParagraphRight(line);
885 int max = ((int) getLineMax(line)) & ~1;
886
887 return left + ((right - left) - max) / 2;
888 }
889 }
890
891 /**
892 * Get the rightmost position that should be exposed for horizontal
893 * scrolling on the specified line.
894 */
895 public float getLineRight(int line) {
896 int dir = getParagraphDirection(line);
897 Alignment align = getParagraphAlignment(line);
898
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700899 if (align == Alignment.ALIGN_LEFT) {
900 return getParagraphLeft(line) + getLineMax(line);
901 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 if (dir == DIR_RIGHT_TO_LEFT)
903 return mWidth;
904 else
905 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700906 } else if (align == Alignment.ALIGN_RIGHT) {
907 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 } else if (align == Alignment.ALIGN_OPPOSITE) {
909 if (dir == DIR_RIGHT_TO_LEFT)
910 return getLineMax(line);
911 else
912 return mWidth;
913 } else { /* align == Alignment.ALIGN_CENTER */
914 int left = getParagraphLeft(line);
915 int right = getParagraphRight(line);
916 int max = ((int) getLineMax(line)) & ~1;
917
918 return right - ((right - left) - max) / 2;
919 }
920 }
921
922 /**
Doug Felt0c702b82010-05-14 10:55:42 -0700923 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -0700924 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 */
926 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700927 float margin = getParagraphLeadingMargin(line);
928 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -0700929 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 }
931
932 /**
Doug Feltc982f602010-05-25 11:51:40 -0700933 * Gets the unsigned horizontal extent of the specified line, including
934 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 */
936 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700937 float margin = getParagraphLeadingMargin(line);
938 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -0700939 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941
Doug Feltc982f602010-05-25 11:51:40 -0700942 /**
943 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
944 * tab stops instead of using the ones passed in.
945 * @param line the index of the line
946 * @param full whether to include trailing whitespace
947 * @return the extent of the line
948 */
949 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700951 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -0700952
953 boolean hasTabsOrEmoji = getLineContainsTab(line);
954 TabStops tabStops = null;
955 if (hasTabsOrEmoji && mText instanceof Spanned) {
956 // Just checking this line should be good enough, tabs should be
957 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700958 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700959 if (tabs.length > 0) {
960 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
961 }
962 }
Doug Felte8e45f22010-03-29 14:58:40 -0700963 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700964 // Returned directions can actually be null
965 if (directions == null) {
966 return 0f;
967 }
Doug Feltc982f602010-05-25 11:51:40 -0700968 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969
Doug Felte8e45f22010-03-29 14:58:40 -0700970 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700971 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
972 float width = tl.metrics(null);
973 TextLine.recycle(tl);
974 return width;
975 }
976
977 /**
978 * Returns the signed horizontal extent of the specified line, excluding
979 * leading margin. If full is false, excludes trailing whitespace.
980 * @param line the index of the line
981 * @param tabStops the tab stops, can be null if we know they're not used.
982 * @param full whether to include trailing whitespace
983 * @return the extent of the text on this line
984 */
985 private float getLineExtent(int line, TabStops tabStops, boolean full) {
986 int start = getLineStart(line);
987 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
988 boolean hasTabsOrEmoji = getLineContainsTab(line);
989 Directions directions = getLineDirections(line);
990 int dir = getParagraphDirection(line);
991
992 TextLine tl = TextLine.obtain();
993 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700994 float width = tl.metrics(null);
995 TextLine.recycle(tl);
996 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 }
998
999 /**
1000 * Get the line number corresponding to the specified vertical position.
1001 * If you ask for a position above 0, you get 0; if you ask for a position
1002 * below the bottom of the text, you get the last line.
1003 */
1004 // FIXME: It may be faster to do a linear search for layouts without many lines.
1005 public int getLineForVertical(int vertical) {
1006 int high = getLineCount(), low = -1, guess;
1007
1008 while (high - low > 1) {
1009 guess = (high + low) / 2;
1010
1011 if (getLineTop(guess) > vertical)
1012 high = guess;
1013 else
1014 low = guess;
1015 }
1016
1017 if (low < 0)
1018 return 0;
1019 else
1020 return low;
1021 }
1022
1023 /**
1024 * Get the line number on which the specified text offset appears.
1025 * If you ask for a position before 0, you get 0; if you ask for a position
1026 * beyond the end of the text, you get the last line.
1027 */
1028 public int getLineForOffset(int offset) {
1029 int high = getLineCount(), low = -1, guess;
1030
1031 while (high - low > 1) {
1032 guess = (high + low) / 2;
1033
1034 if (getLineStart(guess) > offset)
1035 high = guess;
1036 else
1037 low = guess;
1038 }
1039
1040 if (low < 0)
1041 return 0;
1042 else
1043 return low;
1044 }
1045
1046 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001047 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 * closest to the specified horizontal position.
1049 */
1050 public int getOffsetForHorizontal(int line, float horiz) {
1051 int max = getLineEnd(line) - 1;
1052 int min = getLineStart(line);
1053 Directions dirs = getLineDirections(line);
1054
1055 if (line == getLineCount() - 1)
1056 max++;
1057
1058 int best = min;
1059 float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz);
1060
Doug Felt9f7a4442010-03-01 12:45:56 -08001061 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1062 int here = min + dirs.mDirections[i];
1063 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1064 int swap = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0 ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065
1066 if (there > max)
1067 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 int high = there - 1 + 1, low = here + 1 - 1, guess;
1069
1070 while (high - low > 1) {
1071 guess = (high + low) / 2;
1072 int adguess = getOffsetAtStartOf(guess);
1073
1074 if (getPrimaryHorizontal(adguess) * swap >= horiz * swap)
1075 high = guess;
1076 else
1077 low = guess;
1078 }
1079
1080 if (low < here + 1)
1081 low = here + 1;
1082
1083 if (low < there) {
1084 low = getOffsetAtStartOf(low);
1085
1086 float dist = Math.abs(getPrimaryHorizontal(low) - horiz);
1087
1088 int aft = TextUtils.getOffsetAfter(mText, low);
1089 if (aft < there) {
1090 float other = Math.abs(getPrimaryHorizontal(aft) - horiz);
1091
1092 if (other < dist) {
1093 dist = other;
1094 low = aft;
1095 }
1096 }
1097
1098 if (dist < bestdist) {
1099 bestdist = dist;
Doug Felt9f7a4442010-03-01 12:45:56 -08001100 best = low;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
1102 }
1103
1104 float dist = Math.abs(getPrimaryHorizontal(here) - horiz);
1105
1106 if (dist < bestdist) {
1107 bestdist = dist;
1108 best = here;
1109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
1111
1112 float dist = Math.abs(getPrimaryHorizontal(max) - horiz);
1113
Raph Levien373b7a82013-09-20 15:11:52 -07001114 if (dist <= bestdist) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 bestdist = dist;
1116 best = max;
1117 }
1118
1119 return best;
1120 }
1121
1122 /**
1123 * Return the text offset after the last character on the specified line.
1124 */
1125 public final int getLineEnd(int line) {
1126 return getLineStart(line + 1);
1127 }
1128
Doug Felt9f7a4442010-03-01 12:45:56 -08001129 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 * Return the text offset after the last visible character (so whitespace
1131 * is not counted) on the specified line.
1132 */
1133 public int getLineVisibleEnd(int line) {
1134 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1135 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 CharSequence text = mText;
1139 char ch;
1140 if (line == getLineCount() - 1) {
1141 return end;
1142 }
1143
1144 for (; end > start; end--) {
1145 ch = text.charAt(end - 1);
1146
1147 if (ch == '\n') {
1148 return end - 1;
1149 }
1150
1151 if (ch != ' ' && ch != '\t') {
1152 break;
1153 }
1154
1155 }
1156
1157 return end;
1158 }
1159
1160 /**
1161 * Return the vertical position of the bottom of the specified line.
1162 */
1163 public final int getLineBottom(int line) {
1164 return getLineTop(line + 1);
1165 }
1166
1167 /**
1168 * Return the vertical position of the baseline of the specified line.
1169 */
1170 public final int getLineBaseline(int line) {
1171 // getLineTop(line+1) == getLineTop(line)
1172 return getLineTop(line+1) - getLineDescent(line);
1173 }
1174
1175 /**
1176 * Get the ascent of the text on the specified line.
1177 * The return value is negative to match the Paint.ascent() convention.
1178 */
1179 public final int getLineAscent(int line) {
1180 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1181 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1182 }
1183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001185 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001189 return getOffsetToLeftRightOf(offset, false);
1190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191
Doug Felt9f7a4442010-03-01 12:45:56 -08001192 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1193 int line = getLineForOffset(caret);
1194 int lineStart = getLineStart(line);
1195 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001196 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001198 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001199 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001200 // if walking off line, look at the line we're headed to
1201 if (advance) {
1202 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001203 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001204 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001205 ++line;
1206 } else {
1207 return caret; // at very end, don't move
1208 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001209 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001210 } else {
1211 if (caret == lineStart) {
1212 if (line > 0) {
1213 lineChanged = true;
1214 --line;
1215 } else {
1216 return caret; // at very start, don't move
1217 }
1218 }
1219 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001220
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001221 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001222 lineStart = getLineStart(line);
1223 lineEnd = getLineEnd(line);
1224 int newDir = getParagraphDirection(line);
1225 if (newDir != lineDir) {
1226 // unusual case. we want to walk onto the line, but it runs
1227 // in a different direction than this one, so we fake movement
1228 // in the opposite direction.
1229 toLeft = !toLeft;
1230 lineDir = newDir;
1231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001233
Doug Felte8e45f22010-03-29 14:58:40 -07001234 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001235
Doug Felte8e45f22010-03-29 14:58:40 -07001236 TextLine tl = TextLine.obtain();
1237 // XXX: we don't care about tabs
1238 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1239 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1240 tl = TextLine.recycle(tl);
1241 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
1243
1244 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001245 // XXX this probably should skip local reorderings and
1246 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 if (offset == 0)
1248 return 0;
1249
1250 CharSequence text = mText;
1251 char c = text.charAt(offset);
1252
1253 if (c >= '\uDC00' && c <= '\uDFFF') {
1254 char c1 = text.charAt(offset - 1);
1255
1256 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1257 offset -= 1;
1258 }
1259
1260 if (mSpannedText) {
1261 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1262 ReplacementSpan.class);
1263
1264 for (int i = 0; i < spans.length; i++) {
1265 int start = ((Spanned) text).getSpanStart(spans[i]);
1266 int end = ((Spanned) text).getSpanEnd(spans[i]);
1267
1268 if (start < offset && end > offset)
1269 offset = start;
1270 }
1271 }
1272
1273 return offset;
1274 }
1275
1276 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001277 * Determine whether we should clamp cursor position. Currently it's
1278 * only robust for left-aligned displays.
1279 * @hide
1280 */
1281 public boolean shouldClampCursor(int line) {
1282 // Only clamp cursor position in left-aligned displays.
1283 switch (getParagraphAlignment(line)) {
1284 case ALIGN_LEFT:
1285 return true;
1286 case ALIGN_NORMAL:
1287 return getParagraphDirection(line) > 0;
1288 default:
1289 return false;
1290 }
1291
1292 }
1293 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 * Fills in the specified Path with a representation of a cursor
1295 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001296 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 * directionalities.
1298 */
1299 public void getCursorPath(int point, Path dest,
1300 CharSequence editingBuffer) {
1301 dest.reset();
1302
1303 int line = getLineForOffset(point);
1304 int top = getLineTop(line);
1305 int bottom = getLineTop(line+1);
1306
Raph Levienafe8e9b2012-12-19 16:09:32 -08001307 boolean clamped = shouldClampCursor(line);
1308 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1309 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310
Jeff Brown497a92c2010-09-12 17:55:08 -07001311 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1312 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1313 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 int dist = 0;
1315
1316 if (caps != 0 || fn != 0) {
1317 dist = (bottom - top) >> 2;
1318
1319 if (fn != 0)
1320 top += dist;
1321 if (caps != 0)
1322 bottom -= dist;
1323 }
1324
1325 if (h1 < 0.5f)
1326 h1 = 0.5f;
1327 if (h2 < 0.5f)
1328 h2 = 0.5f;
1329
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001330 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 dest.moveTo(h1, top);
1332 dest.lineTo(h1, bottom);
1333 } else {
1334 dest.moveTo(h1, top);
1335 dest.lineTo(h1, (top + bottom) >> 1);
1336
1337 dest.moveTo(h2, (top + bottom) >> 1);
1338 dest.lineTo(h2, bottom);
1339 }
1340
1341 if (caps == 2) {
1342 dest.moveTo(h2, bottom);
1343 dest.lineTo(h2 - dist, bottom + dist);
1344 dest.lineTo(h2, bottom);
1345 dest.lineTo(h2 + dist, bottom + dist);
1346 } else if (caps == 1) {
1347 dest.moveTo(h2, bottom);
1348 dest.lineTo(h2 - dist, bottom + dist);
1349
1350 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1351 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1352
1353 dest.moveTo(h2 + dist, bottom + dist);
1354 dest.lineTo(h2, bottom);
1355 }
1356
1357 if (fn == 2) {
1358 dest.moveTo(h1, top);
1359 dest.lineTo(h1 - dist, top - dist);
1360 dest.lineTo(h1, top);
1361 dest.lineTo(h1 + dist, top - dist);
1362 } else if (fn == 1) {
1363 dest.moveTo(h1, top);
1364 dest.lineTo(h1 - dist, top - dist);
1365
1366 dest.moveTo(h1 - dist, top - dist + 0.5f);
1367 dest.lineTo(h1 + dist, top - dist + 0.5f);
1368
1369 dest.moveTo(h1 + dist, top - dist);
1370 dest.lineTo(h1, top);
1371 }
1372 }
1373
1374 private void addSelection(int line, int start, int end,
1375 int top, int bottom, Path dest) {
1376 int linestart = getLineStart(line);
1377 int lineend = getLineEnd(line);
1378 Directions dirs = getLineDirections(line);
1379
1380 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1381 lineend--;
1382
Doug Felt9f7a4442010-03-01 12:45:56 -08001383 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1384 int here = linestart + dirs.mDirections[i];
1385 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 if (there > lineend)
1388 there = lineend;
1389
1390 if (start <= there && end >= here) {
1391 int st = Math.max(start, here);
1392 int en = Math.min(end, there);
1393
1394 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001395 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1396 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001398 float left = Math.min(h1, h2);
1399 float right = Math.max(h1, h2);
1400
1401 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
1403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
1405 }
1406
1407 /**
1408 * Fills in the specified Path with a representation of a highlight
1409 * between the specified offsets. This will often be a rectangle
1410 * or a potentially discontinuous set of rectangles. If the start
1411 * and end are the same, the returned path is empty.
1412 */
1413 public void getSelectionPath(int start, int end, Path dest) {
1414 dest.reset();
1415
1416 if (start == end)
1417 return;
1418
1419 if (end < start) {
1420 int temp = end;
1421 end = start;
1422 start = temp;
1423 }
1424
1425 int startline = getLineForOffset(start);
1426 int endline = getLineForOffset(end);
1427
1428 int top = getLineTop(startline);
1429 int bottom = getLineBottom(endline);
1430
1431 if (startline == endline) {
1432 addSelection(startline, start, end, top, bottom, dest);
1433 } else {
1434 final float width = mWidth;
1435
1436 addSelection(startline, start, getLineEnd(startline),
1437 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1440 dest.addRect(getLineLeft(startline), top,
1441 0, getLineBottom(startline), Path.Direction.CW);
1442 else
1443 dest.addRect(getLineRight(startline), top,
1444 width, getLineBottom(startline), Path.Direction.CW);
1445
1446 for (int i = startline + 1; i < endline; i++) {
1447 top = getLineTop(i);
1448 bottom = getLineBottom(i);
1449 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1450 }
1451
1452 top = getLineTop(endline);
1453 bottom = getLineBottom(endline);
1454
1455 addSelection(endline, getLineStart(endline), end,
1456 top, bottom, dest);
1457
1458 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1459 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1460 else
1461 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1462 }
1463 }
1464
1465 /**
1466 * Get the alignment of the specified paragraph, taking into account
1467 * markup attached to it.
1468 */
1469 public final Alignment getParagraphAlignment(int line) {
1470 Alignment align = mAlignment;
1471
1472 if (mSpannedText) {
1473 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001474 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 getLineEnd(line),
1476 AlignmentSpan.class);
1477
1478 int spanLength = spans.length;
1479 if (spanLength > 0) {
1480 align = spans[spanLength-1].getAlignment();
1481 }
1482 }
1483
1484 return align;
1485 }
1486
1487 /**
1488 * Get the left edge of the specified paragraph, inset by left margins.
1489 */
1490 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001492 int dir = getParagraphDirection(line);
1493 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1494 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 }
Doug Feltc982f602010-05-25 11:51:40 -07001496 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 }
1498
1499 /**
1500 * Get the right edge of the specified paragraph, inset by right margins.
1501 */
1502 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001504 int dir = getParagraphDirection(line);
1505 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1506 return right; // leading margin has no impact, or no styles
1507 }
1508 return right - getParagraphLeadingMargin(line);
1509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510
Doug Feltc982f602010-05-25 11:51:40 -07001511 /**
1512 * Returns the effective leading margin (unsigned) for this line,
1513 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1514 * @param line the line index
1515 * @return the leading margin of this line
1516 */
1517 private int getParagraphLeadingMargin(int line) {
1518 if (!mSpannedText) {
1519 return 0;
1520 }
1521 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001522
Doug Feltc982f602010-05-25 11:51:40 -07001523 int lineStart = getLineStart(line);
1524 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001525 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001526 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001527 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001528 LeadingMarginSpan.class);
1529 if (spans.length == 0) {
1530 return 0; // no leading margin span;
1531 }
Doug Felt0c702b82010-05-14 10:55:42 -07001532
Doug Feltc982f602010-05-25 11:51:40 -07001533 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001534
1535 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001536 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001537
Doug Feltc982f602010-05-25 11:51:40 -07001538 for (int i = 0; i < spans.length; i++) {
1539 LeadingMarginSpan span = spans[i];
1540 boolean useFirstLineMargin = isFirstParaLine;
1541 if (span instanceof LeadingMarginSpan2) {
1542 int spStart = spanned.getSpanStart(span);
1543 int spanLine = getLineForOffset(spStart);
1544 int count = ((LeadingMarginSpan2)span).getLeadingMarginLineCount();
Doug Felt0c702b82010-05-14 10:55:42 -07001545 useFirstLineMargin = line < spanLine + count;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 }
Doug Feltc982f602010-05-25 11:51:40 -07001547 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 }
1549
Doug Feltc982f602010-05-25 11:51:40 -07001550 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
1552
Doug Felte8e45f22010-03-29 14:58:40 -07001553 /* package */
Gilles Debunne6c488de2012-03-01 16:20:35 -08001554 static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
Doug Felte8e45f22010-03-29 14:58:40 -07001555
1556 MeasuredText mt = MeasuredText.obtain();
1557 TextLine tl = TextLine.obtain();
1558 try {
Doug Feltcb3791202011-07-07 11:57:48 -07001559 mt.setPara(text, start, end, TextDirectionHeuristics.LTR);
Doug Felte8e45f22010-03-29 14:58:40 -07001560 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001561 int dir;
1562 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001563 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001564 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001565 } else {
1566 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1567 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001568 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 }
Doug Feltc982f602010-05-25 11:51:40 -07001570 char[] chars = mt.mChars;
1571 int len = mt.mLen;
1572 boolean hasTabs = false;
1573 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001574 // leading margins should be taken into account when measuring a paragraph
1575 int margin = 0;
1576 if (text instanceof Spanned) {
1577 Spanned spanned = (Spanned) text;
1578 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1579 LeadingMarginSpan.class);
1580 for (LeadingMarginSpan lms : spans) {
1581 margin += lms.getLeadingMargin(true);
1582 }
1583 }
Doug Feltc982f602010-05-25 11:51:40 -07001584 for (int i = 0; i < len; ++i) {
1585 if (chars[i] == '\t') {
1586 hasTabs = true;
1587 if (text instanceof Spanned) {
1588 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001589 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001590 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001591 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001592 TabStopSpan.class);
1593 if (spans.length > 0) {
1594 tabStops = new TabStops(TAB_INCREMENT, spans);
1595 }
1596 }
1597 break;
1598 }
1599 }
1600 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001601 return margin + tl.metrics(null);
Doug Felte8e45f22010-03-29 14:58:40 -07001602 } finally {
1603 TextLine.recycle(tl);
1604 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
1607
Doug Felt71b8dd72010-02-16 17:27:09 -08001608 /**
Doug Feltc982f602010-05-25 11:51:40 -07001609 * @hide
1610 */
1611 /* package */ static class TabStops {
1612 private int[] mStops;
1613 private int mNumStops;
1614 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001615
Doug Feltc982f602010-05-25 11:51:40 -07001616 TabStops(int increment, Object[] spans) {
1617 reset(increment, spans);
1618 }
Doug Felt0c702b82010-05-14 10:55:42 -07001619
Doug Feltc982f602010-05-25 11:51:40 -07001620 void reset(int increment, Object[] spans) {
1621 this.mIncrement = increment;
1622
1623 int ns = 0;
1624 if (spans != null) {
1625 int[] stops = this.mStops;
1626 for (Object o : spans) {
1627 if (o instanceof TabStopSpan) {
1628 if (stops == null) {
1629 stops = new int[10];
1630 } else if (ns == stops.length) {
1631 int[] nstops = new int[ns * 2];
1632 for (int i = 0; i < ns; ++i) {
1633 nstops[i] = stops[i];
1634 }
1635 stops = nstops;
1636 }
1637 stops[ns++] = ((TabStopSpan) o).getTabStop();
1638 }
1639 }
1640 if (ns > 1) {
1641 Arrays.sort(stops, 0, ns);
1642 }
1643 if (stops != this.mStops) {
1644 this.mStops = stops;
1645 }
1646 }
1647 this.mNumStops = ns;
1648 }
Doug Felt0c702b82010-05-14 10:55:42 -07001649
Doug Feltc982f602010-05-25 11:51:40 -07001650 float nextTab(float h) {
1651 int ns = this.mNumStops;
1652 if (ns > 0) {
1653 int[] stops = this.mStops;
1654 for (int i = 0; i < ns; ++i) {
1655 int stop = stops[i];
1656 if (stop > h) {
1657 return stop;
1658 }
1659 }
1660 }
1661 return nextDefaultStop(h, mIncrement);
1662 }
1663
1664 public static float nextDefaultStop(float h, int inc) {
1665 return ((int) ((h + inc) / inc)) * inc;
1666 }
1667 }
Doug Felt0c702b82010-05-14 10:55:42 -07001668
Doug Feltc982f602010-05-25 11:51:40 -07001669 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001670 * Returns the position of the next tab stop after h on the line.
1671 *
1672 * @param text the text
1673 * @param start start of the line
1674 * @param end limit of the line
1675 * @param h the current horizontal offset
1676 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1677 * on the line will be used. If there are no tabs, a default offset
1678 * will be used to compute the tab stop.
1679 * @return the offset of the next tab stop.
1680 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 /* package */ static float nextTab(CharSequence text, int start, int end,
1682 float h, Object[] tabs) {
1683 float nh = Float.MAX_VALUE;
1684 boolean alltabs = false;
1685
1686 if (text instanceof Spanned) {
1687 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001688 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 alltabs = true;
1690 }
1691
1692 for (int i = 0; i < tabs.length; i++) {
1693 if (!alltabs) {
1694 if (!(tabs[i] instanceof TabStopSpan))
1695 continue;
1696 }
1697
1698 int where = ((TabStopSpan) tabs[i]).getTabStop();
1699
1700 if (where < nh && where > h)
1701 nh = where;
1702 }
1703
1704 if (nh != Float.MAX_VALUE)
1705 return nh;
1706 }
1707
1708 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1709 }
1710
1711 protected final boolean isSpanned() {
1712 return mSpannedText;
1713 }
1714
Eric Fischer74d31ef2010-08-05 15:29:36 -07001715 /**
1716 * Returns the same as <code>text.getSpans()</code>, except where
1717 * <code>start</code> and <code>end</code> are the same and are not
1718 * at the very beginning of the text, in which case an empty array
1719 * is returned instead.
1720 * <p>
1721 * This is needed because of the special case that <code>getSpans()</code>
1722 * on an empty range returns the spans adjacent to that range, which is
1723 * primarily for the sake of <code>TextWatchers</code> so they will get
1724 * notifications when text goes from empty to non-empty. But it also
1725 * has the unfortunate side effect that if the text ends with an empty
1726 * paragraph, that paragraph accidentally picks up the styles of the
1727 * preceding paragraph (even though those styles will not be picked up
1728 * by new text that is inserted into the empty paragraph).
1729 * <p>
1730 * The reason it just checks whether <code>start</code> and <code>end</code>
1731 * is the same is that the only time a line can contain 0 characters
1732 * is if it is the final paragraph of the Layout; otherwise any line will
1733 * contain at least one printing or newline character. The reason for the
1734 * additional check if <code>start</code> is greater than 0 is that
1735 * if the empty paragraph is the entire content of the buffer, paragraph
1736 * styles that are already applied to the buffer will apply to text that
1737 * is inserted into it.
1738 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07001739 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001740 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08001741 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001742 }
1743
1744 return text.getSpans(start, end, type);
1745 }
1746
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001747 private char getEllipsisChar(TextUtils.TruncateAt method) {
1748 return (method == TextUtils.TruncateAt.END_SMALL) ?
1749 ELLIPSIS_TWO_DOTS[0] :
1750 ELLIPSIS_NORMAL[0];
1751 }
1752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001754 char[] dest, int destoff, TextUtils.TruncateAt method) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 int ellipsisCount = getEllipsisCount(line);
1756
1757 if (ellipsisCount == 0) {
1758 return;
1759 }
1760
1761 int ellipsisStart = getEllipsisStart(line);
1762 int linestart = getLineStart(line);
1763
1764 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1765 char c;
1766
1767 if (i == ellipsisStart) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001768 c = getEllipsisChar(method); // ellipsis
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 } else {
1770 c = '\uFEFF'; // 0-width space
1771 }
1772
1773 int a = i + linestart;
1774
1775 if (a >= start && a < end) {
1776 dest[destoff + a - start] = c;
1777 }
1778 }
1779 }
1780
1781 /**
1782 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08001783 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 */
1785 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08001786 // Directions represents directional runs within a line of text.
1787 // Runs are pairs of ints listed in visual order, starting from the
1788 // leading margin. The first int of each pair is the offset from
1789 // the first character of the line to the start of the run. The
1790 // second int represents both the length and level of the run.
1791 // The length is in the lower bits, accessed by masking with
1792 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1793 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1794 // To simply test for an RTL direction, test the bit using
1795 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796
Doug Felt9f7a4442010-03-01 12:45:56 -08001797 /* package */ int[] mDirections;
1798 /* package */ Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 mDirections = dirs;
1800 }
1801 }
1802
1803 /**
1804 * Return the offset of the first character to be ellipsized away,
1805 * relative to the start of the line. (So 0 if the beginning of the
1806 * line is ellipsized, not getLineStart().)
1807 */
1808 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07001809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 /**
1811 * Returns the number of characters to be ellipsized away, or 0 if
1812 * no ellipsis is to take place.
1813 */
1814 public abstract int getEllipsisCount(int line);
1815
1816 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1817 /* package */ CharSequence mText;
1818 /* package */ Layout mLayout;
1819 /* package */ int mWidth;
1820 /* package */ TextUtils.TruncateAt mMethod;
1821
1822 public Ellipsizer(CharSequence s) {
1823 mText = s;
1824 }
1825
1826 public char charAt(int off) {
1827 char[] buf = TextUtils.obtain(1);
1828 getChars(off, off + 1, buf, 0);
1829 char ret = buf[0];
1830
1831 TextUtils.recycle(buf);
1832 return ret;
1833 }
1834
1835 public void getChars(int start, int end, char[] dest, int destoff) {
1836 int line1 = mLayout.getLineForOffset(start);
1837 int line2 = mLayout.getLineForOffset(end);
1838
1839 TextUtils.getChars(mText, start, end, dest, destoff);
1840
1841 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001842 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 }
1844 }
1845
1846 public int length() {
1847 return mText.length();
1848 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 public CharSequence subSequence(int start, int end) {
1851 char[] s = new char[end - start];
1852 getChars(start, end, s, 0);
1853 return new String(s);
1854 }
1855
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001856 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 public String toString() {
1858 char[] s = new char[length()];
1859 getChars(0, length(), s, 0);
1860 return new String(s);
1861 }
1862
1863 }
1864
Gilles Debunne6c488de2012-03-01 16:20:35 -08001865 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 private Spanned mSpanned;
1867
1868 public SpannedEllipsizer(CharSequence display) {
1869 super(display);
1870 mSpanned = (Spanned) display;
1871 }
1872
1873 public <T> T[] getSpans(int start, int end, Class<T> type) {
1874 return mSpanned.getSpans(start, end, type);
1875 }
1876
1877 public int getSpanStart(Object tag) {
1878 return mSpanned.getSpanStart(tag);
1879 }
1880
1881 public int getSpanEnd(Object tag) {
1882 return mSpanned.getSpanEnd(tag);
1883 }
1884
1885 public int getSpanFlags(Object tag) {
1886 return mSpanned.getSpanFlags(tag);
1887 }
1888
Gilles Debunne6c488de2012-03-01 16:20:35 -08001889 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 public int nextSpanTransition(int start, int limit, Class type) {
1891 return mSpanned.nextSpanTransition(start, limit, type);
1892 }
1893
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001894 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 public CharSequence subSequence(int start, int end) {
1896 char[] s = new char[end - start];
1897 getChars(start, end, s, 0);
1898
1899 SpannableString ss = new SpannableString(new String(s));
1900 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
1901 return ss;
1902 }
1903 }
1904
1905 private CharSequence mText;
1906 private TextPaint mPaint;
1907 /* package */ TextPaint mWorkPaint;
1908 private int mWidth;
1909 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
1910 private float mSpacingMult;
1911 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07001912 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07001914 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07001915 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916
1917 public static final int DIR_LEFT_TO_RIGHT = 1;
1918 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08001919
Doug Felt20178d62010-02-22 13:39:01 -08001920 /* package */ static final int DIR_REQUEST_LTR = 1;
1921 /* package */ static final int DIR_REQUEST_RTL = -1;
1922 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
1923 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924
Doug Felt9f7a4442010-03-01 12:45:56 -08001925 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
1926 /* package */ static final int RUN_LEVEL_SHIFT = 26;
1927 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
1928 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
1929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 public enum Alignment {
1931 ALIGN_NORMAL,
1932 ALIGN_OPPOSITE,
1933 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001934 /** @hide */
1935 ALIGN_LEFT,
1936 /** @hide */
1937 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939
1940 private static final int TAB_INCREMENT = 20;
1941
1942 /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001943 new Directions(new int[] { 0, RUN_LENGTH_MASK });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001945 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08001946
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001947 /* package */ static final char[] ELLIPSIS_NORMAL = { '\u2026' }; // this is "..."
1948 /* package */ static final char[] ELLIPSIS_TWO_DOTS = { '\u2025' }; // this is ".."
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949}