blob: 2dcea80c5b215465749f4ce9203fd91ea586c568 [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;
34
Doug Feltc982f602010-05-25 11:51:40 -070035import java.util.Arrays;
Doug Felt9f7a4442010-03-01 12:45:56 -080036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037/**
Doug Felt9f7a4442010-03-01 12:45:56 -080038 * A base class that manages text layout in visual elements on
39 * the screen.
40 * <p>For text that will be edited, use a {@link DynamicLayout},
41 * which will be updated as the text changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 * For text that will not change, use a {@link StaticLayout}.
43 */
44public abstract class Layout {
Doug Felt71b8dd72010-02-16 17:27:09 -080045 private static final ParagraphStyle[] NO_PARA_SPANS =
46 ArrayUtils.emptyArray(ParagraphStyle.class);
Dave Bort76c02262009-04-13 17:17:21 -070047
The Android Open Source Project10592532009-03-18 17:39:46 -070048 /* package */ static final EmojiFactory EMOJI_FACTORY =
49 EmojiFactory.newAvailableInstance();
50 /* 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 Debunne60e3b3f2012-03-13 11:26:05 -0700366 if (lineBackgroundSpans == null) {
367 lineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
368 }
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();
372 lineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800373
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700374 if (lineBackgroundSpans.numberOfSpans > 0) {
375 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.
395 spanEnd = lineBackgroundSpans.getNextTransition(start, textLength);
396 // 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
402 for (int j = 0; j < lineBackgroundSpans.numberOfSpans; j++) {
403 // equal test is valid since both intervals are not empty by construction
404 if (lineBackgroundSpans.spanStarts[j] >= end ||
405 lineBackgroundSpans.spanEnds[j] <= start) continue;
406 if (spansLength == spans.length) {
407 // The spans array needs to be expanded
408 int newSize = ArrayUtils.idealObjectArraySize(2 * spansLength);
409 ParagraphStyle[] newSpans = new ParagraphStyle[newSize];
410 System.arraycopy(spans, 0, newSpans, 0, spansLength);
411 spans = newSpans;
412 }
413 spans[spansLength++] = lineBackgroundSpans.spans[j];
414 }
415 }
416 }
417
418 for (int n = 0; n < spansLength; n++) {
419 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
420 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
421 ltop, lbaseline, lbottom,
422 buffer, start, end, i);
423 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800424 }
425 }
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700426 lineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800427 }
428
429 // There can be a highlight even without spans if we are drawing
430 // a non-spanned transformation of a spanned editing buffer.
431 if (highlight != null) {
432 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
433 canvas.drawPath(highlight, highlightPaint);
434 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
435 }
436 }
437
438 /**
439 * @param canvas
440 * @return The range of lines that need to be drawn, possibly empty.
441 * @hide
442 */
443 public long getLineRangeForDraw(Canvas canvas) {
444 int dtop, dbottom;
445
446 synchronized (sTempRect) {
447 if (!canvas.getClipBounds(sTempRect)) {
448 // Negative range end used as a special flag
449 return TextUtils.packRangeInLong(0, -1);
450 }
451
452 dtop = sTempRect.top;
453 dbottom = sTempRect.bottom;
454 }
455
456 final int top = Math.max(dtop, 0);
457 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
458
459 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
460 }
461
462 /**
Doug Feltc982f602010-05-25 11:51:40 -0700463 * Return the start position of the line, given the left and right bounds
464 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700465 *
Doug Feltc982f602010-05-25 11:51:40 -0700466 * @param line the line index
467 * @param left the left bounds (0, or leading margin if ltr para)
468 * @param right the right bounds (width, minus leading margin if rtl para)
469 * @return the start position of the line (to right of line if rtl para)
470 */
471 private int getLineStartPos(int line, int left, int right) {
472 // Adjust the point at which to start rendering depending on the
473 // alignment of the paragraph.
474 Alignment align = getParagraphAlignment(line);
475 int dir = getParagraphDirection(line);
476
477 int x;
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700478 if (align == Alignment.ALIGN_LEFT) {
479 x = left;
480 } else if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700481 if (dir == DIR_LEFT_TO_RIGHT) {
482 x = left;
483 } else {
484 x = right;
485 }
486 } else {
487 TabStops tabStops = null;
488 if (mSpannedText && getLineContainsTab(line)) {
489 Spanned spanned = (Spanned) mText;
490 int start = getLineStart(line);
491 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
492 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800493 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
494 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700495 if (tabSpans.length > 0) {
496 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
497 }
498 }
499 int max = (int)getLineExtent(line, tabStops, false);
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700500 if (align == Alignment.ALIGN_RIGHT) {
501 x = right - max;
502 } else if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700503 if (dir == DIR_LEFT_TO_RIGHT) {
504 x = right - max;
505 } else {
506 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) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800791 boolean trailing = primaryIsTrailingPrevious(offset);
792 return getHorizontal(offset, trailing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
794
795 /**
796 * Get the secondary horizontal position for the specified text offset.
797 * This is the location where a new character would be inserted in
798 * the direction other than the paragraph's primary direction.
799 */
800 public float getSecondaryHorizontal(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800801 boolean trailing = primaryIsTrailingPrevious(offset);
802 return getHorizontal(offset, !trailing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804
Doug Felt9f7a4442010-03-01 12:45:56 -0800805 private float getHorizontal(int offset, boolean trailing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 int line = getLineForOffset(offset);
807
Doug Felt9f7a4442010-03-01 12:45:56 -0800808 return getHorizontal(offset, trailing, line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 }
810
Doug Felt9f7a4442010-03-01 12:45:56 -0800811 private float getHorizontal(int offset, boolean trailing, int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700813 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 int dir = getParagraphDirection(line);
Doug Feltc982f602010-05-25 11:51:40 -0700815 boolean hasTabOrEmoji = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 Directions directions = getLineDirections(line);
817
Doug Feltc982f602010-05-25 11:51:40 -0700818 TabStops tabStops = null;
819 if (hasTabOrEmoji && mText instanceof Spanned) {
820 // Just checking this line should be good enough, tabs should be
821 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700822 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700823 if (tabs.length > 0) {
824 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
827
Doug Felte8e45f22010-03-29 14:58:40 -0700828 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700829 tl.set(mPaint, mText, start, end, dir, directions, hasTabOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700830 float wid = tl.measure(offset - start, trailing, null);
831 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 int left = getParagraphLeft(line);
834 int right = getParagraphRight(line);
835
Doug Feltc982f602010-05-25 11:51:40 -0700836 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838
839 /**
840 * Get the leftmost position that should be exposed for horizontal
841 * scrolling on the specified line.
842 */
843 public float getLineLeft(int line) {
844 int dir = getParagraphDirection(line);
845 Alignment align = getParagraphAlignment(line);
846
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700847 if (align == Alignment.ALIGN_LEFT) {
848 return 0;
849 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 if (dir == DIR_RIGHT_TO_LEFT)
851 return getParagraphRight(line) - getLineMax(line);
852 else
853 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700854 } else if (align == Alignment.ALIGN_RIGHT) {
855 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 } else if (align == Alignment.ALIGN_OPPOSITE) {
857 if (dir == DIR_RIGHT_TO_LEFT)
858 return 0;
859 else
860 return mWidth - getLineMax(line);
861 } else { /* align == Alignment.ALIGN_CENTER */
862 int left = getParagraphLeft(line);
863 int right = getParagraphRight(line);
864 int max = ((int) getLineMax(line)) & ~1;
865
866 return left + ((right - left) - max) / 2;
867 }
868 }
869
870 /**
871 * Get the rightmost position that should be exposed for horizontal
872 * scrolling on the specified line.
873 */
874 public float getLineRight(int line) {
875 int dir = getParagraphDirection(line);
876 Alignment align = getParagraphAlignment(line);
877
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700878 if (align == Alignment.ALIGN_LEFT) {
879 return getParagraphLeft(line) + getLineMax(line);
880 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 if (dir == DIR_RIGHT_TO_LEFT)
882 return mWidth;
883 else
884 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700885 } else if (align == Alignment.ALIGN_RIGHT) {
886 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 } else if (align == Alignment.ALIGN_OPPOSITE) {
888 if (dir == DIR_RIGHT_TO_LEFT)
889 return getLineMax(line);
890 else
891 return mWidth;
892 } else { /* align == Alignment.ALIGN_CENTER */
893 int left = getParagraphLeft(line);
894 int right = getParagraphRight(line);
895 int max = ((int) getLineMax(line)) & ~1;
896
897 return right - ((right - left) - max) / 2;
898 }
899 }
900
901 /**
Doug Felt0c702b82010-05-14 10:55:42 -0700902 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -0700903 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 */
905 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700906 float margin = getParagraphLeadingMargin(line);
907 float signedExtent = getLineExtent(line, false);
908 return margin + signedExtent >= 0 ? signedExtent : -signedExtent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 }
910
911 /**
Doug Feltc982f602010-05-25 11:51:40 -0700912 * Gets the unsigned horizontal extent of the specified line, including
913 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 */
915 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700916 float margin = getParagraphLeadingMargin(line);
917 float signedExtent = getLineExtent(line, true);
918 return margin + signedExtent >= 0 ? signedExtent : -signedExtent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
920
Doug Feltc982f602010-05-25 11:51:40 -0700921 /**
922 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
923 * tab stops instead of using the ones passed in.
924 * @param line the index of the line
925 * @param full whether to include trailing whitespace
926 * @return the extent of the line
927 */
928 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700930 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -0700931
932 boolean hasTabsOrEmoji = getLineContainsTab(line);
933 TabStops tabStops = null;
934 if (hasTabsOrEmoji && mText instanceof Spanned) {
935 // Just checking this line should be good enough, tabs should be
936 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700937 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700938 if (tabs.length > 0) {
939 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
940 }
941 }
Doug Felte8e45f22010-03-29 14:58:40 -0700942 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700943 // Returned directions can actually be null
944 if (directions == null) {
945 return 0f;
946 }
Doug Feltc982f602010-05-25 11:51:40 -0700947 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948
Doug Felte8e45f22010-03-29 14:58:40 -0700949 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700950 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
951 float width = tl.metrics(null);
952 TextLine.recycle(tl);
953 return width;
954 }
955
956 /**
957 * Returns the signed horizontal extent of the specified line, excluding
958 * leading margin. If full is false, excludes trailing whitespace.
959 * @param line the index of the line
960 * @param tabStops the tab stops, can be null if we know they're not used.
961 * @param full whether to include trailing whitespace
962 * @return the extent of the text on this line
963 */
964 private float getLineExtent(int line, TabStops tabStops, boolean full) {
965 int start = getLineStart(line);
966 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
967 boolean hasTabsOrEmoji = getLineContainsTab(line);
968 Directions directions = getLineDirections(line);
969 int dir = getParagraphDirection(line);
970
971 TextLine tl = TextLine.obtain();
972 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700973 float width = tl.metrics(null);
974 TextLine.recycle(tl);
975 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 }
977
978 /**
979 * Get the line number corresponding to the specified vertical position.
980 * If you ask for a position above 0, you get 0; if you ask for a position
981 * below the bottom of the text, you get the last line.
982 */
983 // FIXME: It may be faster to do a linear search for layouts without many lines.
984 public int getLineForVertical(int vertical) {
985 int high = getLineCount(), low = -1, guess;
986
987 while (high - low > 1) {
988 guess = (high + low) / 2;
989
990 if (getLineTop(guess) > vertical)
991 high = guess;
992 else
993 low = guess;
994 }
995
996 if (low < 0)
997 return 0;
998 else
999 return low;
1000 }
1001
1002 /**
1003 * Get the line number on which the specified text offset appears.
1004 * If you ask for a position before 0, you get 0; if you ask for a position
1005 * beyond the end of the text, you get the last line.
1006 */
1007 public int getLineForOffset(int offset) {
1008 int high = getLineCount(), low = -1, guess;
1009
1010 while (high - low > 1) {
1011 guess = (high + low) / 2;
1012
1013 if (getLineStart(guess) > offset)
1014 high = guess;
1015 else
1016 low = guess;
1017 }
1018
1019 if (low < 0)
1020 return 0;
1021 else
1022 return low;
1023 }
1024
1025 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001026 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 * closest to the specified horizontal position.
1028 */
1029 public int getOffsetForHorizontal(int line, float horiz) {
1030 int max = getLineEnd(line) - 1;
1031 int min = getLineStart(line);
1032 Directions dirs = getLineDirections(line);
1033
1034 if (line == getLineCount() - 1)
1035 max++;
1036
1037 int best = min;
1038 float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz);
1039
Doug Felt9f7a4442010-03-01 12:45:56 -08001040 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1041 int here = min + dirs.mDirections[i];
1042 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1043 int swap = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0 ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044
1045 if (there > max)
1046 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 int high = there - 1 + 1, low = here + 1 - 1, guess;
1048
1049 while (high - low > 1) {
1050 guess = (high + low) / 2;
1051 int adguess = getOffsetAtStartOf(guess);
1052
1053 if (getPrimaryHorizontal(adguess) * swap >= horiz * swap)
1054 high = guess;
1055 else
1056 low = guess;
1057 }
1058
1059 if (low < here + 1)
1060 low = here + 1;
1061
1062 if (low < there) {
1063 low = getOffsetAtStartOf(low);
1064
1065 float dist = Math.abs(getPrimaryHorizontal(low) - horiz);
1066
1067 int aft = TextUtils.getOffsetAfter(mText, low);
1068 if (aft < there) {
1069 float other = Math.abs(getPrimaryHorizontal(aft) - horiz);
1070
1071 if (other < dist) {
1072 dist = other;
1073 low = aft;
1074 }
1075 }
1076
1077 if (dist < bestdist) {
1078 bestdist = dist;
Doug Felt9f7a4442010-03-01 12:45:56 -08001079 best = low;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 }
1082
1083 float dist = Math.abs(getPrimaryHorizontal(here) - horiz);
1084
1085 if (dist < bestdist) {
1086 bestdist = dist;
1087 best = here;
1088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
1090
1091 float dist = Math.abs(getPrimaryHorizontal(max) - horiz);
1092
1093 if (dist < bestdist) {
1094 bestdist = dist;
1095 best = max;
1096 }
1097
1098 return best;
1099 }
1100
1101 /**
1102 * Return the text offset after the last character on the specified line.
1103 */
1104 public final int getLineEnd(int line) {
1105 return getLineStart(line + 1);
1106 }
1107
Doug Felt9f7a4442010-03-01 12:45:56 -08001108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 * Return the text offset after the last visible character (so whitespace
1110 * is not counted) on the specified line.
1111 */
1112 public int getLineVisibleEnd(int line) {
1113 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1114 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 CharSequence text = mText;
1118 char ch;
1119 if (line == getLineCount() - 1) {
1120 return end;
1121 }
1122
1123 for (; end > start; end--) {
1124 ch = text.charAt(end - 1);
1125
1126 if (ch == '\n') {
1127 return end - 1;
1128 }
1129
1130 if (ch != ' ' && ch != '\t') {
1131 break;
1132 }
1133
1134 }
1135
1136 return end;
1137 }
1138
1139 /**
1140 * Return the vertical position of the bottom of the specified line.
1141 */
1142 public final int getLineBottom(int line) {
1143 return getLineTop(line + 1);
1144 }
1145
1146 /**
1147 * Return the vertical position of the baseline of the specified line.
1148 */
1149 public final int getLineBaseline(int line) {
1150 // getLineTop(line+1) == getLineTop(line)
1151 return getLineTop(line+1) - getLineDescent(line);
1152 }
1153
1154 /**
1155 * Get the ascent of the text on the specified line.
1156 * The return value is negative to match the Paint.ascent() convention.
1157 */
1158 public final int getLineAscent(int line) {
1159 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1160 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1161 }
1162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001164 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001168 return getOffsetToLeftRightOf(offset, false);
1169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170
Doug Felt9f7a4442010-03-01 12:45:56 -08001171 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1172 int line = getLineForOffset(caret);
1173 int lineStart = getLineStart(line);
1174 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001175 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001177 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001178 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001179 // if walking off line, look at the line we're headed to
1180 if (advance) {
1181 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001182 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001183 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001184 ++line;
1185 } else {
1186 return caret; // at very end, don't move
1187 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001188 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001189 } else {
1190 if (caret == lineStart) {
1191 if (line > 0) {
1192 lineChanged = true;
1193 --line;
1194 } else {
1195 return caret; // at very start, don't move
1196 }
1197 }
1198 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001199
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001200 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001201 lineStart = getLineStart(line);
1202 lineEnd = getLineEnd(line);
1203 int newDir = getParagraphDirection(line);
1204 if (newDir != lineDir) {
1205 // unusual case. we want to walk onto the line, but it runs
1206 // in a different direction than this one, so we fake movement
1207 // in the opposite direction.
1208 toLeft = !toLeft;
1209 lineDir = newDir;
1210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001212
Doug Felte8e45f22010-03-29 14:58:40 -07001213 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001214
Doug Felte8e45f22010-03-29 14:58:40 -07001215 TextLine tl = TextLine.obtain();
1216 // XXX: we don't care about tabs
1217 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1218 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1219 tl = TextLine.recycle(tl);
1220 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
1222
1223 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001224 // XXX this probably should skip local reorderings and
1225 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 if (offset == 0)
1227 return 0;
1228
1229 CharSequence text = mText;
1230 char c = text.charAt(offset);
1231
1232 if (c >= '\uDC00' && c <= '\uDFFF') {
1233 char c1 = text.charAt(offset - 1);
1234
1235 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1236 offset -= 1;
1237 }
1238
1239 if (mSpannedText) {
1240 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1241 ReplacementSpan.class);
1242
1243 for (int i = 0; i < spans.length; i++) {
1244 int start = ((Spanned) text).getSpanStart(spans[i]);
1245 int end = ((Spanned) text).getSpanEnd(spans[i]);
1246
1247 if (start < offset && end > offset)
1248 offset = start;
1249 }
1250 }
1251
1252 return offset;
1253 }
1254
1255 /**
1256 * Fills in the specified Path with a representation of a cursor
1257 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001258 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 * directionalities.
1260 */
1261 public void getCursorPath(int point, Path dest,
1262 CharSequence editingBuffer) {
1263 dest.reset();
1264
1265 int line = getLineForOffset(point);
1266 int top = getLineTop(line);
1267 int bottom = getLineTop(line+1);
1268
1269 float h1 = getPrimaryHorizontal(point) - 0.5f;
Gilles Debunnef75c97e2011-02-10 16:09:53 -08001270 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271
Jeff Brown497a92c2010-09-12 17:55:08 -07001272 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1273 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1274 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 int dist = 0;
1276
1277 if (caps != 0 || fn != 0) {
1278 dist = (bottom - top) >> 2;
1279
1280 if (fn != 0)
1281 top += dist;
1282 if (caps != 0)
1283 bottom -= dist;
1284 }
1285
1286 if (h1 < 0.5f)
1287 h1 = 0.5f;
1288 if (h2 < 0.5f)
1289 h2 = 0.5f;
1290
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001291 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 dest.moveTo(h1, top);
1293 dest.lineTo(h1, bottom);
1294 } else {
1295 dest.moveTo(h1, top);
1296 dest.lineTo(h1, (top + bottom) >> 1);
1297
1298 dest.moveTo(h2, (top + bottom) >> 1);
1299 dest.lineTo(h2, bottom);
1300 }
1301
1302 if (caps == 2) {
1303 dest.moveTo(h2, bottom);
1304 dest.lineTo(h2 - dist, bottom + dist);
1305 dest.lineTo(h2, bottom);
1306 dest.lineTo(h2 + dist, bottom + dist);
1307 } else if (caps == 1) {
1308 dest.moveTo(h2, bottom);
1309 dest.lineTo(h2 - dist, bottom + dist);
1310
1311 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1312 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1313
1314 dest.moveTo(h2 + dist, bottom + dist);
1315 dest.lineTo(h2, bottom);
1316 }
1317
1318 if (fn == 2) {
1319 dest.moveTo(h1, top);
1320 dest.lineTo(h1 - dist, top - dist);
1321 dest.lineTo(h1, top);
1322 dest.lineTo(h1 + dist, top - dist);
1323 } else if (fn == 1) {
1324 dest.moveTo(h1, top);
1325 dest.lineTo(h1 - dist, top - dist);
1326
1327 dest.moveTo(h1 - dist, top - dist + 0.5f);
1328 dest.lineTo(h1 + dist, top - dist + 0.5f);
1329
1330 dest.moveTo(h1 + dist, top - dist);
1331 dest.lineTo(h1, top);
1332 }
1333 }
1334
1335 private void addSelection(int line, int start, int end,
1336 int top, int bottom, Path dest) {
1337 int linestart = getLineStart(line);
1338 int lineend = getLineEnd(line);
1339 Directions dirs = getLineDirections(line);
1340
1341 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1342 lineend--;
1343
Doug Felt9f7a4442010-03-01 12:45:56 -08001344 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1345 int here = linestart + dirs.mDirections[i];
1346 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 if (there > lineend)
1349 there = lineend;
1350
1351 if (start <= there && end >= here) {
1352 int st = Math.max(start, here);
1353 int en = Math.min(end, there);
1354
1355 if (st != en) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001356 float h1 = getHorizontal(st, false, line);
1357 float h2 = getHorizontal(en, true, line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001359 float left = Math.min(h1, h2);
1360 float right = Math.max(h1, h2);
1361
1362 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
1366 }
1367
1368 /**
1369 * Fills in the specified Path with a representation of a highlight
1370 * between the specified offsets. This will often be a rectangle
1371 * or a potentially discontinuous set of rectangles. If the start
1372 * and end are the same, the returned path is empty.
1373 */
1374 public void getSelectionPath(int start, int end, Path dest) {
1375 dest.reset();
1376
1377 if (start == end)
1378 return;
1379
1380 if (end < start) {
1381 int temp = end;
1382 end = start;
1383 start = temp;
1384 }
1385
1386 int startline = getLineForOffset(start);
1387 int endline = getLineForOffset(end);
1388
1389 int top = getLineTop(startline);
1390 int bottom = getLineBottom(endline);
1391
1392 if (startline == endline) {
1393 addSelection(startline, start, end, top, bottom, dest);
1394 } else {
1395 final float width = mWidth;
1396
1397 addSelection(startline, start, getLineEnd(startline),
1398 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1401 dest.addRect(getLineLeft(startline), top,
1402 0, getLineBottom(startline), Path.Direction.CW);
1403 else
1404 dest.addRect(getLineRight(startline), top,
1405 width, getLineBottom(startline), Path.Direction.CW);
1406
1407 for (int i = startline + 1; i < endline; i++) {
1408 top = getLineTop(i);
1409 bottom = getLineBottom(i);
1410 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1411 }
1412
1413 top = getLineTop(endline);
1414 bottom = getLineBottom(endline);
1415
1416 addSelection(endline, getLineStart(endline), end,
1417 top, bottom, dest);
1418
1419 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1420 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1421 else
1422 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1423 }
1424 }
1425
1426 /**
1427 * Get the alignment of the specified paragraph, taking into account
1428 * markup attached to it.
1429 */
1430 public final Alignment getParagraphAlignment(int line) {
1431 Alignment align = mAlignment;
1432
1433 if (mSpannedText) {
1434 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001435 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 getLineEnd(line),
1437 AlignmentSpan.class);
1438
1439 int spanLength = spans.length;
1440 if (spanLength > 0) {
1441 align = spans[spanLength-1].getAlignment();
1442 }
1443 }
1444
1445 return align;
1446 }
1447
1448 /**
1449 * Get the left edge of the specified paragraph, inset by left margins.
1450 */
1451 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001453 int dir = getParagraphDirection(line);
1454 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1455 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 }
Doug Feltc982f602010-05-25 11:51:40 -07001457 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 }
1459
1460 /**
1461 * Get the right edge of the specified paragraph, inset by right margins.
1462 */
1463 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001465 int dir = getParagraphDirection(line);
1466 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1467 return right; // leading margin has no impact, or no styles
1468 }
1469 return right - getParagraphLeadingMargin(line);
1470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471
Doug Feltc982f602010-05-25 11:51:40 -07001472 /**
1473 * Returns the effective leading margin (unsigned) for this line,
1474 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1475 * @param line the line index
1476 * @return the leading margin of this line
1477 */
1478 private int getParagraphLeadingMargin(int line) {
1479 if (!mSpannedText) {
1480 return 0;
1481 }
1482 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001483
Doug Feltc982f602010-05-25 11:51:40 -07001484 int lineStart = getLineStart(line);
1485 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001486 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001487 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001488 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001489 LeadingMarginSpan.class);
1490 if (spans.length == 0) {
1491 return 0; // no leading margin span;
1492 }
Doug Felt0c702b82010-05-14 10:55:42 -07001493
Doug Feltc982f602010-05-25 11:51:40 -07001494 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001495
1496 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001497 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001498
Doug Feltc982f602010-05-25 11:51:40 -07001499 for (int i = 0; i < spans.length; i++) {
1500 LeadingMarginSpan span = spans[i];
1501 boolean useFirstLineMargin = isFirstParaLine;
1502 if (span instanceof LeadingMarginSpan2) {
1503 int spStart = spanned.getSpanStart(span);
1504 int spanLine = getLineForOffset(spStart);
1505 int count = ((LeadingMarginSpan2)span).getLeadingMarginLineCount();
Doug Felt0c702b82010-05-14 10:55:42 -07001506 useFirstLineMargin = line < spanLine + count;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 }
Doug Feltc982f602010-05-25 11:51:40 -07001508 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 }
1510
Doug Feltc982f602010-05-25 11:51:40 -07001511 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
1513
Doug Felte8e45f22010-03-29 14:58:40 -07001514 /* package */
Gilles Debunne6c488de2012-03-01 16:20:35 -08001515 static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
Doug Felte8e45f22010-03-29 14:58:40 -07001516
1517 MeasuredText mt = MeasuredText.obtain();
1518 TextLine tl = TextLine.obtain();
1519 try {
Doug Feltcb3791202011-07-07 11:57:48 -07001520 mt.setPara(text, start, end, TextDirectionHeuristics.LTR);
Doug Felte8e45f22010-03-29 14:58:40 -07001521 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001522 int dir;
1523 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001524 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001525 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001526 } else {
1527 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1528 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001529 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 }
Doug Feltc982f602010-05-25 11:51:40 -07001531 char[] chars = mt.mChars;
1532 int len = mt.mLen;
1533 boolean hasTabs = false;
1534 TabStops tabStops = null;
1535 for (int i = 0; i < len; ++i) {
1536 if (chars[i] == '\t') {
1537 hasTabs = true;
1538 if (text instanceof Spanned) {
1539 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001540 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001541 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001542 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001543 TabStopSpan.class);
1544 if (spans.length > 0) {
1545 tabStops = new TabStops(TAB_INCREMENT, spans);
1546 }
1547 }
1548 break;
1549 }
1550 }
1551 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001552 return tl.metrics(null);
1553 } finally {
1554 TextLine.recycle(tl);
1555 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 }
1558
Doug Felt71b8dd72010-02-16 17:27:09 -08001559 /**
Doug Feltc982f602010-05-25 11:51:40 -07001560 * @hide
1561 */
1562 /* package */ static class TabStops {
1563 private int[] mStops;
1564 private int mNumStops;
1565 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001566
Doug Feltc982f602010-05-25 11:51:40 -07001567 TabStops(int increment, Object[] spans) {
1568 reset(increment, spans);
1569 }
Doug Felt0c702b82010-05-14 10:55:42 -07001570
Doug Feltc982f602010-05-25 11:51:40 -07001571 void reset(int increment, Object[] spans) {
1572 this.mIncrement = increment;
1573
1574 int ns = 0;
1575 if (spans != null) {
1576 int[] stops = this.mStops;
1577 for (Object o : spans) {
1578 if (o instanceof TabStopSpan) {
1579 if (stops == null) {
1580 stops = new int[10];
1581 } else if (ns == stops.length) {
1582 int[] nstops = new int[ns * 2];
1583 for (int i = 0; i < ns; ++i) {
1584 nstops[i] = stops[i];
1585 }
1586 stops = nstops;
1587 }
1588 stops[ns++] = ((TabStopSpan) o).getTabStop();
1589 }
1590 }
1591 if (ns > 1) {
1592 Arrays.sort(stops, 0, ns);
1593 }
1594 if (stops != this.mStops) {
1595 this.mStops = stops;
1596 }
1597 }
1598 this.mNumStops = ns;
1599 }
Doug Felt0c702b82010-05-14 10:55:42 -07001600
Doug Feltc982f602010-05-25 11:51:40 -07001601 float nextTab(float h) {
1602 int ns = this.mNumStops;
1603 if (ns > 0) {
1604 int[] stops = this.mStops;
1605 for (int i = 0; i < ns; ++i) {
1606 int stop = stops[i];
1607 if (stop > h) {
1608 return stop;
1609 }
1610 }
1611 }
1612 return nextDefaultStop(h, mIncrement);
1613 }
1614
1615 public static float nextDefaultStop(float h, int inc) {
1616 return ((int) ((h + inc) / inc)) * inc;
1617 }
1618 }
Doug Felt0c702b82010-05-14 10:55:42 -07001619
Doug Feltc982f602010-05-25 11:51:40 -07001620 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001621 * Returns the position of the next tab stop after h on the line.
1622 *
1623 * @param text the text
1624 * @param start start of the line
1625 * @param end limit of the line
1626 * @param h the current horizontal offset
1627 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1628 * on the line will be used. If there are no tabs, a default offset
1629 * will be used to compute the tab stop.
1630 * @return the offset of the next tab stop.
1631 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 /* package */ static float nextTab(CharSequence text, int start, int end,
1633 float h, Object[] tabs) {
1634 float nh = Float.MAX_VALUE;
1635 boolean alltabs = false;
1636
1637 if (text instanceof Spanned) {
1638 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001639 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 alltabs = true;
1641 }
1642
1643 for (int i = 0; i < tabs.length; i++) {
1644 if (!alltabs) {
1645 if (!(tabs[i] instanceof TabStopSpan))
1646 continue;
1647 }
1648
1649 int where = ((TabStopSpan) tabs[i]).getTabStop();
1650
1651 if (where < nh && where > h)
1652 nh = where;
1653 }
1654
1655 if (nh != Float.MAX_VALUE)
1656 return nh;
1657 }
1658
1659 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1660 }
1661
1662 protected final boolean isSpanned() {
1663 return mSpannedText;
1664 }
1665
Eric Fischer74d31ef2010-08-05 15:29:36 -07001666 /**
1667 * Returns the same as <code>text.getSpans()</code>, except where
1668 * <code>start</code> and <code>end</code> are the same and are not
1669 * at the very beginning of the text, in which case an empty array
1670 * is returned instead.
1671 * <p>
1672 * This is needed because of the special case that <code>getSpans()</code>
1673 * on an empty range returns the spans adjacent to that range, which is
1674 * primarily for the sake of <code>TextWatchers</code> so they will get
1675 * notifications when text goes from empty to non-empty. But it also
1676 * has the unfortunate side effect that if the text ends with an empty
1677 * paragraph, that paragraph accidentally picks up the styles of the
1678 * preceding paragraph (even though those styles will not be picked up
1679 * by new text that is inserted into the empty paragraph).
1680 * <p>
1681 * The reason it just checks whether <code>start</code> and <code>end</code>
1682 * is the same is that the only time a line can contain 0 characters
1683 * is if it is the final paragraph of the Layout; otherwise any line will
1684 * contain at least one printing or newline character. The reason for the
1685 * additional check if <code>start</code> is greater than 0 is that
1686 * if the empty paragraph is the entire content of the buffer, paragraph
1687 * styles that are already applied to the buffer will apply to text that
1688 * is inserted into it.
1689 */
1690 /* package */ static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
1691 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08001692 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001693 }
1694
1695 return text.getSpans(start, end, type);
1696 }
1697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 private void ellipsize(int start, int end, int line,
1699 char[] dest, int destoff) {
1700 int ellipsisCount = getEllipsisCount(line);
1701
1702 if (ellipsisCount == 0) {
1703 return;
1704 }
1705
1706 int ellipsisStart = getEllipsisStart(line);
1707 int linestart = getLineStart(line);
1708
1709 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1710 char c;
1711
1712 if (i == ellipsisStart) {
1713 c = '\u2026'; // ellipsis
1714 } else {
1715 c = '\uFEFF'; // 0-width space
1716 }
1717
1718 int a = i + linestart;
1719
1720 if (a >= start && a < end) {
1721 dest[destoff + a - start] = c;
1722 }
1723 }
1724 }
1725
1726 /**
1727 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08001728 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 */
1730 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08001731 // Directions represents directional runs within a line of text.
1732 // Runs are pairs of ints listed in visual order, starting from the
1733 // leading margin. The first int of each pair is the offset from
1734 // the first character of the line to the start of the run. The
1735 // second int represents both the length and level of the run.
1736 // The length is in the lower bits, accessed by masking with
1737 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1738 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1739 // To simply test for an RTL direction, test the bit using
1740 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741
Doug Felt9f7a4442010-03-01 12:45:56 -08001742 /* package */ int[] mDirections;
1743 /* package */ Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 mDirections = dirs;
1745 }
1746 }
1747
1748 /**
1749 * Return the offset of the first character to be ellipsized away,
1750 * relative to the start of the line. (So 0 if the beginning of the
1751 * line is ellipsized, not getLineStart().)
1752 */
1753 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07001754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 /**
1756 * Returns the number of characters to be ellipsized away, or 0 if
1757 * no ellipsis is to take place.
1758 */
1759 public abstract int getEllipsisCount(int line);
1760
1761 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1762 /* package */ CharSequence mText;
1763 /* package */ Layout mLayout;
1764 /* package */ int mWidth;
1765 /* package */ TextUtils.TruncateAt mMethod;
1766
1767 public Ellipsizer(CharSequence s) {
1768 mText = s;
1769 }
1770
1771 public char charAt(int off) {
1772 char[] buf = TextUtils.obtain(1);
1773 getChars(off, off + 1, buf, 0);
1774 char ret = buf[0];
1775
1776 TextUtils.recycle(buf);
1777 return ret;
1778 }
1779
1780 public void getChars(int start, int end, char[] dest, int destoff) {
1781 int line1 = mLayout.getLineForOffset(start);
1782 int line2 = mLayout.getLineForOffset(end);
1783
1784 TextUtils.getChars(mText, start, end, dest, destoff);
1785
1786 for (int i = line1; i <= line2; i++) {
1787 mLayout.ellipsize(start, end, i, dest, destoff);
1788 }
1789 }
1790
1791 public int length() {
1792 return mText.length();
1793 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 public CharSequence subSequence(int start, int end) {
1796 char[] s = new char[end - start];
1797 getChars(start, end, s, 0);
1798 return new String(s);
1799 }
1800
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001801 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 public String toString() {
1803 char[] s = new char[length()];
1804 getChars(0, length(), s, 0);
1805 return new String(s);
1806 }
1807
1808 }
1809
Gilles Debunne6c488de2012-03-01 16:20:35 -08001810 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 private Spanned mSpanned;
1812
1813 public SpannedEllipsizer(CharSequence display) {
1814 super(display);
1815 mSpanned = (Spanned) display;
1816 }
1817
1818 public <T> T[] getSpans(int start, int end, Class<T> type) {
1819 return mSpanned.getSpans(start, end, type);
1820 }
1821
1822 public int getSpanStart(Object tag) {
1823 return mSpanned.getSpanStart(tag);
1824 }
1825
1826 public int getSpanEnd(Object tag) {
1827 return mSpanned.getSpanEnd(tag);
1828 }
1829
1830 public int getSpanFlags(Object tag) {
1831 return mSpanned.getSpanFlags(tag);
1832 }
1833
Gilles Debunne6c488de2012-03-01 16:20:35 -08001834 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 public int nextSpanTransition(int start, int limit, Class type) {
1836 return mSpanned.nextSpanTransition(start, limit, type);
1837 }
1838
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001839 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 public CharSequence subSequence(int start, int end) {
1841 char[] s = new char[end - start];
1842 getChars(start, end, s, 0);
1843
1844 SpannableString ss = new SpannableString(new String(s));
1845 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
1846 return ss;
1847 }
1848 }
1849
1850 private CharSequence mText;
1851 private TextPaint mPaint;
1852 /* package */ TextPaint mWorkPaint;
1853 private int mWidth;
1854 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
1855 private float mSpacingMult;
1856 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07001857 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07001859 private TextDirectionHeuristic mTextDir;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -07001860 private SpanSet<LineBackgroundSpan> lineBackgroundSpans;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861
1862 public static final int DIR_LEFT_TO_RIGHT = 1;
1863 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08001864
Doug Felt20178d62010-02-22 13:39:01 -08001865 /* package */ static final int DIR_REQUEST_LTR = 1;
1866 /* package */ static final int DIR_REQUEST_RTL = -1;
1867 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
1868 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869
Doug Felt9f7a4442010-03-01 12:45:56 -08001870 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
1871 /* package */ static final int RUN_LEVEL_SHIFT = 26;
1872 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
1873 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
1874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 public enum Alignment {
1876 ALIGN_NORMAL,
1877 ALIGN_OPPOSITE,
1878 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001879 /** @hide */
1880 ALIGN_LEFT,
1881 /** @hide */
1882 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 }
1884
1885 private static final int TAB_INCREMENT = 20;
1886
1887 /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001888 new Directions(new int[] { 0, RUN_LENGTH_MASK });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001890 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08001891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892}