blob: dd25eaa4400dd95a77346cfbbc773b400dff2391 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * 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
Doug Felt9f7a4442010-03-01 12:45:56 -080019import com.android.internal.util.ArrayUtils;
20
The Android Open Source Project10592532009-03-18 17:39:46 -070021import android.emoji.EmojiFactory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.graphics.Canvas;
23import android.graphics.Paint;
Doug Felt9f7a4442010-03-01 12:45:56 -080024import android.graphics.Path;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.text.method.TextKeyListener;
Doug Felt9f7a4442010-03-01 12:45:56 -080027import android.text.style.AlignmentSpan;
28import android.text.style.LeadingMarginSpan;
29import android.text.style.LineBackgroundSpan;
30import android.text.style.ParagraphStyle;
31import android.text.style.ReplacementSpan;
32import android.text.style.TabStopSpan;
Doug Feltc982f602010-05-25 11:51:40 -070033import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.view.KeyEvent;
35
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
The Android Open Source Project10592532009-03-18 17:39:46 -070049 /* package */ static final EmojiFactory EMOJI_FACTORY =
50 EmojiFactory.newAvailableInstance();
51 /* package */ static final int MIN_EMOJI, MAX_EMOJI;
52
53 static {
54 if (EMOJI_FACTORY != null) {
55 MIN_EMOJI = EMOJI_FACTORY.getMinimumAndroidPua();
56 MAX_EMOJI = EMOJI_FACTORY.getMaximumAndroidPua();
57 } else {
58 MIN_EMOJI = -1;
59 MAX_EMOJI = -1;
60 }
Doug Felte8e45f22010-03-29 14:58:40 -070061 }
Eric Fischerc2d54f42009-03-27 15:52:38 -070062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 /**
Doug Felt71b8dd72010-02-16 17:27:09 -080064 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 * specified text with one line per paragraph.
66 */
67 public static float getDesiredWidth(CharSequence source,
68 TextPaint paint) {
69 return getDesiredWidth(source, 0, source.length(), paint);
70 }
Doug Felt9f7a4442010-03-01 12:45:56 -080071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 /**
Doug Felt71b8dd72010-02-16 17:27:09 -080073 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * specified text slice with one line per paragraph.
75 */
76 public static float getDesiredWidth(CharSequence source,
77 int start, int end,
78 TextPaint paint) {
79 float need = 0;
80 TextPaint workPaint = new TextPaint();
81
82 int next;
83 for (int i = start; i <= end; i = next) {
84 next = TextUtils.indexOf(source, '\n', i, end);
85
86 if (next < 0)
87 next = end;
88
Doug Felt71b8dd72010-02-16 17:27:09 -080089 // note, omits trailing paragraph char
Doug Feltc982f602010-05-25 11:51:40 -070090 float w = measurePara(paint, workPaint, source, i, next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92 if (w > need)
93 need = w;
94
95 next++;
96 }
97
98 return need;
99 }
100
101 /**
102 * Subclasses of Layout use this constructor to set the display text,
103 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800104 * @param text the text to render
105 * @param paint the default paint for the layout. Styles can override
106 * various attributes of the paint.
107 * @param width the wrapping width for the text.
108 * @param align whether to left, right, or center the text. Styles can
109 * override the alignment.
110 * @param spacingMult factor by which to scale the font size to get the
111 * default line spacing
112 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
114 protected Layout(CharSequence text, TextPaint paint,
115 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800116 float spacingMult, float spacingAdd) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 if (width < 0)
118 throw new IllegalArgumentException("Layout: " + width + " < 0");
119
Doug Felte8e45f22010-03-29 14:58:40 -0700120 // Ensure paint doesn't have baselineShift set.
121 // While normally we don't modify the paint the user passed in,
122 // we were already doing this in Styled.drawUniformRun with both
123 // baselineShift and bgColor. We probably should reevaluate bgColor.
124 if (paint != null) {
125 paint.bgColor = 0;
126 paint.baselineShift = 0;
127 }
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 mText = text;
130 mPaint = paint;
131 mWorkPaint = new TextPaint();
132 mWidth = width;
133 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800134 mSpacingMult = spacingMult;
135 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 mSpannedText = text instanceof Spanned;
137 }
138
139 /**
140 * Replace constructor properties of this Layout with new ones. Be careful.
141 */
142 /* package */ void replaceWith(CharSequence text, TextPaint paint,
143 int width, Alignment align,
144 float spacingmult, float spacingadd) {
145 if (width < 0) {
146 throw new IllegalArgumentException("Layout: " + width + " < 0");
147 }
148
149 mText = text;
150 mPaint = paint;
151 mWidth = width;
152 mAlignment = align;
153 mSpacingMult = spacingmult;
154 mSpacingAdd = spacingadd;
155 mSpannedText = text instanceof Spanned;
156 }
157
158 /**
159 * Draw this Layout on the specified Canvas.
160 */
161 public void draw(Canvas c) {
162 draw(c, null, null, 0);
163 }
164
165 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800166 * Draw this Layout on the specified canvas, with the highlight path drawn
167 * between the background and the text.
168 *
169 * @param c the canvas
170 * @param highlight the path of the highlight or cursor; can be null
171 * @param highlightPaint the paint for the highlight
172 * @param cursorOffsetVertical the amount to temporarily translate the
173 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 */
Doug Felt71b8dd72010-02-16 17:27:09 -0800175 public void draw(Canvas c, Path highlight, Paint highlightPaint,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 int cursorOffsetVertical) {
177 int dtop, dbottom;
178
179 synchronized (sTempRect) {
180 if (!c.getClipBounds(sTempRect)) {
181 return;
182 }
183
184 dtop = sTempRect.top;
185 dbottom = sTempRect.bottom;
186 }
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 int top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 int bottom = getLineTop(getLineCount());
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 if (dtop > top) {
192 top = dtop;
193 }
194 if (dbottom < bottom) {
195 bottom = dbottom;
196 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800197
198 int first = getLineForVertical(top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 int last = getLineForVertical(bottom);
Doug Felt9f7a4442010-03-01 12:45:56 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 int previousLineBottom = getLineTop(first);
202 int previousLineEnd = getLineStart(first);
Doug Felt9f7a4442010-03-01 12:45:56 -0800203
Doug Felt71b8dd72010-02-16 17:27:09 -0800204 TextPaint paint = mPaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 CharSequence buf = mText;
Doug Felt71b8dd72010-02-16 17:27:09 -0800206 int width = mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 boolean spannedText = mSpannedText;
208
Doug Felt71b8dd72010-02-16 17:27:09 -0800209 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700210 int spanEnd = 0;
Doug Felt71b8dd72010-02-16 17:27:09 -0800211 int textLength = 0;
212
213 // First, draw LineBackgroundSpans.
Doug Felt0c702b82010-05-14 10:55:42 -0700214 // LineBackgroundSpans know nothing about the alignment, margins, or
Doug Feltc982f602010-05-25 11:51:40 -0700215 // direction of the layout or line. XXX: Should they?
216 // They are evaluated at each line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 if (spannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700218 Spanned sp = (Spanned) buf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 textLength = buf.length();
220 for (int i = first; i <= last; i++) {
221 int start = previousLineEnd;
222 int end = getLineStart(i+1);
223 previousLineEnd = end;
224
225 int ltop = previousLineBottom;
226 int lbottom = getLineTop(i+1);
227 previousLineBottom = lbottom;
228 int lbaseline = lbottom - getLineDescent(i);
229
Doug Feltc982f602010-05-25 11:51:40 -0700230 if (start >= spanEnd) {
231 // These should be infrequent, so we'll use this so that
232 // we don't have to check as often.
Doug Felt0c702b82010-05-14 10:55:42 -0700233 spanEnd = sp.nextSpanTransition(start, textLength,
Doug Feltc982f602010-05-25 11:51:40 -0700234 LineBackgroundSpan.class);
235 // All LineBackgroundSpans on a line contribute to its
236 // background.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700237 spans = getParagraphSpans(sp, start, end, LineBackgroundSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
239
240 for (int n = 0; n < spans.length; n++) {
241 LineBackgroundSpan back = (LineBackgroundSpan) spans[n];
242
Doug Felt71b8dd72010-02-16 17:27:09 -0800243 back.drawBackground(c, paint, 0, width,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 ltop, lbaseline, lbottom,
245 buf, start, end,
246 i);
247 }
248 }
249 // reset to their original values
Doug Feltc982f602010-05-25 11:51:40 -0700250 spanEnd = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 previousLineBottom = getLineTop(first);
252 previousLineEnd = getLineStart(first);
Doug Felt71b8dd72010-02-16 17:27:09 -0800253 spans = NO_PARA_SPANS;
Doug Felt9f7a4442010-03-01 12:45:56 -0800254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
256 // There can be a highlight even without spans if we are drawing
257 // a non-spanned transformation of a spanned editing buffer.
258 if (highlight != null) {
259 if (cursorOffsetVertical != 0) {
260 c.translate(0, cursorOffsetVertical);
261 }
262
Doug Felt71b8dd72010-02-16 17:27:09 -0800263 c.drawPath(highlight, highlightPaint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264
265 if (cursorOffsetVertical != 0) {
266 c.translate(0, -cursorOffsetVertical);
267 }
268 }
269
270 Alignment align = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700271 TabStops tabStops = null;
272 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800273
Doug Felte8e45f22010-03-29 14:58:40 -0700274 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700275
Doug Felt71b8dd72010-02-16 17:27:09 -0800276 // Next draw the lines, one at a time.
277 // the baseline is the top of the following line minus the current
278 // line's descent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 for (int i = first; i <= last; i++) {
280 int start = previousLineEnd;
281
282 previousLineEnd = getLineStart(i+1);
283 int end = getLineVisibleEnd(i, start, previousLineEnd);
284
285 int ltop = previousLineBottom;
286 int lbottom = getLineTop(i+1);
287 previousLineBottom = lbottom;
288 int lbaseline = lbottom - getLineDescent(i);
289
Doug Feltc982f602010-05-25 11:51:40 -0700290 int dir = getParagraphDirection(i);
291 int left = 0;
292 int right = mWidth;
293
Doug Felt9f7a4442010-03-01 12:45:56 -0800294 if (spannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700295 Spanned sp = (Spanned) buf;
296 boolean isFirstParaLine = (start == 0 ||
297 buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700298
Doug Feltc982f602010-05-25 11:51:40 -0700299 // New batch of paragraph styles, collect into spans array.
300 // Compute the alignment, last alignment style wins.
301 // Reset tabStops, we'll rebuild if we encounter a line with
302 // tabs.
303 // We expect paragraph spans to be relatively infrequent, use
304 // spanEnd so that we can check less frequently. Since
305 // paragraph styles ought to apply to entire paragraphs, we can
306 // just collect the ones present at the start of the paragraph.
307 // If spanEnd is before the end of the paragraph, that's not
308 // our problem.
309 if (start >= spanEnd && (i == first || isFirstParaLine)) {
310 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700312 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 align = mAlignment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 for (int n = spans.length-1; n >= 0; n--) {
316 if (spans[n] instanceof AlignmentSpan) {
317 align = ((AlignmentSpan) spans[n]).getAlignment();
318 break;
319 }
320 }
Doug Felt0c702b82010-05-14 10:55:42 -0700321
Doug Feltc982f602010-05-25 11:51:40 -0700322 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800324
Doug Feltc982f602010-05-25 11:51:40 -0700325 // Draw all leading margin spans. Adjust left or right according
326 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 final int length = spans.length;
328 for (int n = 0; n < length; n++) {
329 if (spans[n] instanceof LeadingMarginSpan) {
330 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
Doug Feltc982f602010-05-25 11:51:40 -0700331 boolean useFirstLineMargin = isFirstParaLine;
332 if (margin instanceof LeadingMarginSpan2) {
333 int count = ((LeadingMarginSpan2) margin).getLeadingMarginLineCount();
334 int startLine = getLineForOffset(sp.getSpanStart(margin));
335 useFirstLineMargin = i < startLine + count;
336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
338 if (dir == DIR_RIGHT_TO_LEFT) {
339 margin.drawLeadingMargin(c, paint, right, dir, ltop,
340 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800341 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700342 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 } else {
344 margin.drawLeadingMargin(c, paint, left, dir, ltop,
345 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800346 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700347 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
349 }
350 }
351 }
352
Doug Feltc982f602010-05-25 11:51:40 -0700353 boolean hasTabOrEmoji = getLineContainsTab(i);
354 // Can't tell if we have tabs for sure, currently
355 if (hasTabOrEmoji && !tabStopsIsInitialized) {
356 if (tabStops == null) {
357 tabStops = new TabStops(TAB_INCREMENT, spans);
358 } else {
359 tabStops.reset(TAB_INCREMENT, spans);
360 }
361 tabStopsIsInitialized = true;
362 }
363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 int x;
365 if (align == Alignment.ALIGN_NORMAL) {
366 if (dir == DIR_LEFT_TO_RIGHT) {
367 x = left;
368 } else {
369 x = right;
370 }
371 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700372 int max = (int)getLineExtent(i, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700374 if (dir == DIR_LEFT_TO_RIGHT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 x = right - max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700377 x = left - max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
Doug Feltc982f602010-05-25 11:51:40 -0700379 } else { // Alignment.ALIGN_CENTER
380 max = max & ~1;
381 x = (right + left - max) >> 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383 }
384
385 Directions directions = getLineDirections(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 if (directions == DIRS_ALL_LEFT_TO_RIGHT &&
Doug Feltc982f602010-05-25 11:51:40 -0700387 !spannedText && !hasTabOrEmoji) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800388 // XXX: assumes there's nothing additional to be done
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 c.drawText(buf, start, end, x, lbaseline, paint);
390 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700391 tl.set(paint, buf, start, end, dir, directions, hasTabOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700392 tl.draw(c, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
394 }
Doug Feltc982f602010-05-25 11:51:40 -0700395
Doug Felte8e45f22010-03-29 14:58:40 -0700396 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 }
398
399 /**
Doug Feltc982f602010-05-25 11:51:40 -0700400 * Return the start position of the line, given the left and right bounds
401 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700402 *
Doug Feltc982f602010-05-25 11:51:40 -0700403 * @param line the line index
404 * @param left the left bounds (0, or leading margin if ltr para)
405 * @param right the right bounds (width, minus leading margin if rtl para)
406 * @return the start position of the line (to right of line if rtl para)
407 */
408 private int getLineStartPos(int line, int left, int right) {
409 // Adjust the point at which to start rendering depending on the
410 // alignment of the paragraph.
411 Alignment align = getParagraphAlignment(line);
412 int dir = getParagraphDirection(line);
413
414 int x;
415 if (align == Alignment.ALIGN_NORMAL) {
416 if (dir == DIR_LEFT_TO_RIGHT) {
417 x = left;
418 } else {
419 x = right;
420 }
421 } else {
422 TabStops tabStops = null;
423 if (mSpannedText && getLineContainsTab(line)) {
424 Spanned spanned = (Spanned) mText;
425 int start = getLineStart(line);
426 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
427 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700428 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700429 if (tabSpans.length > 0) {
430 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
431 }
432 }
433 int max = (int)getLineExtent(line, tabStops, false);
434 if (align == Alignment.ALIGN_OPPOSITE) {
435 if (dir == DIR_LEFT_TO_RIGHT) {
436 x = right - max;
437 } else {
438 x = left - max;
439 }
440 } else { // Alignment.ALIGN_CENTER
441 max = max & ~1;
442 x = (left + right - max) >> 1;
443 }
444 }
445 return x;
446 }
447
448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 * Return the text that is displayed by this Layout.
450 */
451 public final CharSequence getText() {
452 return mText;
453 }
454
455 /**
456 * Return the base Paint properties for this layout.
457 * Do NOT change the paint, which may result in funny
458 * drawing for this layout.
459 */
460 public final TextPaint getPaint() {
461 return mPaint;
462 }
463
464 /**
465 * Return the width of this layout.
466 */
467 public final int getWidth() {
468 return mWidth;
469 }
470
471 /**
472 * Return the width to which this Layout is ellipsizing, or
473 * {@link #getWidth} if it is not doing anything special.
474 */
475 public int getEllipsizedWidth() {
476 return mWidth;
477 }
478
479 /**
480 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800481 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 * it does not cause the text to reflow to use the full new width.
483 */
484 public final void increaseWidthTo(int wid) {
485 if (wid < mWidth) {
486 throw new RuntimeException("attempted to reduce Layout width");
487 }
488
489 mWidth = wid;
490 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 /**
493 * Return the total height of this layout.
494 */
495 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800496 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
498
499 /**
500 * Return the base alignment of this layout.
501 */
502 public final Alignment getAlignment() {
503 return mAlignment;
504 }
505
506 /**
507 * Return what the text height is multiplied by to get the line height.
508 */
509 public final float getSpacingMultiplier() {
510 return mSpacingMult;
511 }
512
513 /**
514 * Return the number of units of leading that are added to each line.
515 */
516 public final float getSpacingAdd() {
517 return mSpacingAdd;
518 }
519
520 /**
521 * Return the number of lines of text in this layout.
522 */
523 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 /**
526 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
527 * If bounds is not null, return the top, left, right, bottom extents
528 * of the specified line in it.
529 * @param line which line to examine (0..getLineCount() - 1)
530 * @param bounds Optional. If not null, it returns the extent of the line
531 * @return the Y-coordinate of the baseline
532 */
533 public int getLineBounds(int line, Rect bounds) {
534 if (bounds != null) {
535 bounds.left = 0; // ???
536 bounds.top = getLineTop(line);
537 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800538 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540 return getLineBaseline(line);
541 }
542
543 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800544 * Return the vertical position of the top of the specified line
545 * (0&hellip;getLineCount()).
546 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 * bottom of the last line.
548 */
549 public abstract int getLineTop(int line);
550
551 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800552 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 */
554 public abstract int getLineDescent(int line);
555
556 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800557 * Return the text offset of the beginning of the specified line (
558 * 0&hellip;getLineCount()). If the specified line is equal to the line
559 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 */
561 public abstract int getLineStart(int line);
562
563 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800564 * Returns the primary directionality of the paragraph containing the
565 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
566 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 */
568 public abstract int getParagraphDirection(int line);
569
570 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700571 * Returns whether the specified line contains one or more
572 * characters that need to be handled specially, like tabs
573 * or emoji.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 */
575 public abstract boolean getLineContainsTab(int line);
576
577 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800578 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 * The array alternates counts of characters in left-to-right
580 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800581 *
582 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 */
584 public abstract Directions getLineDirections(int line);
585
586 /**
587 * Returns the (negative) number of extra pixels of ascent padding in the
588 * top line of the Layout.
589 */
590 public abstract int getTopPadding();
591
592 /**
593 * Returns the number of extra pixels of descent padding in the
594 * bottom line of the Layout.
595 */
596 public abstract int getBottomPadding();
597
Doug Felt4e0c5e52010-03-15 16:56:02 -0700598
599 /**
600 * Returns true if the character at offset and the preceding character
601 * are at different run levels (and thus there's a split caret).
602 * @param offset the offset
603 * @return true if at a level boundary
604 */
605 private boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800606 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700607 Directions dirs = getLineDirections(line);
608 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
609 return false;
610 }
611
612 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800613 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700614 int lineEnd = getLineEnd(line);
615 if (offset == lineStart || offset == lineEnd) {
616 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
617 int runIndex = offset == lineStart ? 0 : runs.length - 2;
618 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
619 }
620
621 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800622 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700623 if (offset == runs[i]) {
624 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800625 }
626 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700627 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800628 }
629
630 private boolean primaryIsTrailingPrevious(int offset) {
631 int line = getLineForOffset(offset);
632 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700633 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800634 int[] runs = getLineDirections(line).mDirections;
635
636 int levelAt = -1;
637 for (int i = 0; i < runs.length; i += 2) {
638 int start = lineStart + runs[i];
639 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
640 if (limit > lineEnd) {
641 limit = lineEnd;
642 }
643 if (offset >= start && offset < limit) {
644 if (offset > start) {
645 // Previous character is at same level, so don't use trailing.
646 return false;
647 }
648 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
649 break;
650 }
651 }
652 if (levelAt == -1) {
653 // Offset was limit of line.
654 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
655 }
656
657 // At level boundary, check previous level.
658 int levelBefore = -1;
659 if (offset == lineStart) {
660 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
661 } else {
662 offset -= 1;
663 for (int i = 0; i < runs.length; i += 2) {
664 int start = lineStart + runs[i];
665 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
666 if (limit > lineEnd) {
667 limit = lineEnd;
668 }
669 if (offset >= start && offset < limit) {
670 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
671 break;
672 }
673 }
674 }
675
676 return levelBefore < levelAt;
677 }
678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 /**
680 * Get the primary horizontal position for the specified text offset.
681 * This is the location where a new character would be inserted in
682 * the paragraph's primary direction.
683 */
684 public float getPrimaryHorizontal(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800685 boolean trailing = primaryIsTrailingPrevious(offset);
686 return getHorizontal(offset, trailing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
688
689 /**
690 * Get the secondary horizontal position for the specified text offset.
691 * This is the location where a new character would be inserted in
692 * the direction other than the paragraph's primary direction.
693 */
694 public float getSecondaryHorizontal(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800695 boolean trailing = primaryIsTrailingPrevious(offset);
696 return getHorizontal(offset, !trailing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
698
Doug Felt9f7a4442010-03-01 12:45:56 -0800699 private float getHorizontal(int offset, boolean trailing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 int line = getLineForOffset(offset);
701
Doug Felt9f7a4442010-03-01 12:45:56 -0800702 return getHorizontal(offset, trailing, line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 }
704
Doug Felt9f7a4442010-03-01 12:45:56 -0800705 private float getHorizontal(int offset, boolean trailing, int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700707 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 int dir = getParagraphDirection(line);
Doug Feltc982f602010-05-25 11:51:40 -0700709 boolean hasTabOrEmoji = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 Directions directions = getLineDirections(line);
711
Doug Feltc982f602010-05-25 11:51:40 -0700712 TabStops tabStops = null;
713 if (hasTabOrEmoji && mText instanceof Spanned) {
714 // Just checking this line should be good enough, tabs should be
715 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700716 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700717 if (tabs.length > 0) {
718 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 }
721
Doug Felte8e45f22010-03-29 14:58:40 -0700722 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700723 tl.set(mPaint, mText, start, end, dir, directions, hasTabOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700724 float wid = tl.measure(offset - start, trailing, null);
725 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 int left = getParagraphLeft(line);
728 int right = getParagraphRight(line);
729
Doug Feltc982f602010-05-25 11:51:40 -0700730 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732
733 /**
734 * Get the leftmost position that should be exposed for horizontal
735 * scrolling on the specified line.
736 */
737 public float getLineLeft(int line) {
738 int dir = getParagraphDirection(line);
739 Alignment align = getParagraphAlignment(line);
740
741 if (align == Alignment.ALIGN_NORMAL) {
742 if (dir == DIR_RIGHT_TO_LEFT)
743 return getParagraphRight(line) - getLineMax(line);
744 else
745 return 0;
746 } else if (align == Alignment.ALIGN_OPPOSITE) {
747 if (dir == DIR_RIGHT_TO_LEFT)
748 return 0;
749 else
750 return mWidth - getLineMax(line);
751 } else { /* align == Alignment.ALIGN_CENTER */
752 int left = getParagraphLeft(line);
753 int right = getParagraphRight(line);
754 int max = ((int) getLineMax(line)) & ~1;
755
756 return left + ((right - left) - max) / 2;
757 }
758 }
759
760 /**
761 * Get the rightmost position that should be exposed for horizontal
762 * scrolling on the specified line.
763 */
764 public float getLineRight(int line) {
765 int dir = getParagraphDirection(line);
766 Alignment align = getParagraphAlignment(line);
767
768 if (align == Alignment.ALIGN_NORMAL) {
769 if (dir == DIR_RIGHT_TO_LEFT)
770 return mWidth;
771 else
772 return getParagraphLeft(line) + getLineMax(line);
773 } else if (align == Alignment.ALIGN_OPPOSITE) {
774 if (dir == DIR_RIGHT_TO_LEFT)
775 return getLineMax(line);
776 else
777 return mWidth;
778 } else { /* align == Alignment.ALIGN_CENTER */
779 int left = getParagraphLeft(line);
780 int right = getParagraphRight(line);
781 int max = ((int) getLineMax(line)) & ~1;
782
783 return right - ((right - left) - max) / 2;
784 }
785 }
786
787 /**
Doug Felt0c702b82010-05-14 10:55:42 -0700788 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -0700789 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 */
791 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700792 float margin = getParagraphLeadingMargin(line);
793 float signedExtent = getLineExtent(line, false);
794 return margin + signedExtent >= 0 ? signedExtent : -signedExtent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
796
797 /**
Doug Feltc982f602010-05-25 11:51:40 -0700798 * Gets the unsigned horizontal extent of the specified line, including
799 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 */
801 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700802 float margin = getParagraphLeadingMargin(line);
803 float signedExtent = getLineExtent(line, true);
804 return margin + signedExtent >= 0 ? signedExtent : -signedExtent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
806
Doug Feltc982f602010-05-25 11:51:40 -0700807 /**
808 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
809 * tab stops instead of using the ones passed in.
810 * @param line the index of the line
811 * @param full whether to include trailing whitespace
812 * @return the extent of the line
813 */
814 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700816 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -0700817
818 boolean hasTabsOrEmoji = getLineContainsTab(line);
819 TabStops tabStops = null;
820 if (hasTabsOrEmoji && mText instanceof Spanned) {
821 // Just checking this line should be good enough, tabs should be
822 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700823 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700824 if (tabs.length > 0) {
825 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
826 }
827 }
Doug Felte8e45f22010-03-29 14:58:40 -0700828 Directions directions = getLineDirections(line);
Doug Feltc982f602010-05-25 11:51:40 -0700829 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830
Doug Felte8e45f22010-03-29 14:58:40 -0700831 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700832 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
833 float width = tl.metrics(null);
834 TextLine.recycle(tl);
835 return width;
836 }
837
838 /**
839 * Returns the signed horizontal extent of the specified line, excluding
840 * leading margin. If full is false, excludes trailing whitespace.
841 * @param line the index of the line
842 * @param tabStops the tab stops, can be null if we know they're not used.
843 * @param full whether to include trailing whitespace
844 * @return the extent of the text on this line
845 */
846 private float getLineExtent(int line, TabStops tabStops, boolean full) {
847 int start = getLineStart(line);
848 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
849 boolean hasTabsOrEmoji = getLineContainsTab(line);
850 Directions directions = getLineDirections(line);
851 int dir = getParagraphDirection(line);
852
853 TextLine tl = TextLine.obtain();
854 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700855 float width = tl.metrics(null);
856 TextLine.recycle(tl);
857 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
859
860 /**
861 * Get the line number corresponding to the specified vertical position.
862 * If you ask for a position above 0, you get 0; if you ask for a position
863 * below the bottom of the text, you get the last line.
864 */
865 // FIXME: It may be faster to do a linear search for layouts without many lines.
866 public int getLineForVertical(int vertical) {
867 int high = getLineCount(), low = -1, guess;
868
869 while (high - low > 1) {
870 guess = (high + low) / 2;
871
872 if (getLineTop(guess) > vertical)
873 high = guess;
874 else
875 low = guess;
876 }
877
878 if (low < 0)
879 return 0;
880 else
881 return low;
882 }
883
884 /**
885 * Get the line number on which the specified text offset appears.
886 * If you ask for a position before 0, you get 0; if you ask for a position
887 * beyond the end of the text, you get the last line.
888 */
889 public int getLineForOffset(int offset) {
890 int high = getLineCount(), low = -1, guess;
891
892 while (high - low > 1) {
893 guess = (high + low) / 2;
894
895 if (getLineStart(guess) > offset)
896 high = guess;
897 else
898 low = guess;
899 }
900
901 if (low < 0)
902 return 0;
903 else
904 return low;
905 }
906
907 /**
Doug Felt9f7a4442010-03-01 12:45:56 -0800908 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 * closest to the specified horizontal position.
910 */
911 public int getOffsetForHorizontal(int line, float horiz) {
912 int max = getLineEnd(line) - 1;
913 int min = getLineStart(line);
914 Directions dirs = getLineDirections(line);
915
916 if (line == getLineCount() - 1)
917 max++;
918
919 int best = min;
920 float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz);
921
Doug Felt9f7a4442010-03-01 12:45:56 -0800922 for (int i = 0; i < dirs.mDirections.length; i += 2) {
923 int here = min + dirs.mDirections[i];
924 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
925 int swap = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0 ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926
927 if (there > max)
928 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 int high = there - 1 + 1, low = here + 1 - 1, guess;
930
931 while (high - low > 1) {
932 guess = (high + low) / 2;
933 int adguess = getOffsetAtStartOf(guess);
934
935 if (getPrimaryHorizontal(adguess) * swap >= horiz * swap)
936 high = guess;
937 else
938 low = guess;
939 }
940
941 if (low < here + 1)
942 low = here + 1;
943
944 if (low < there) {
945 low = getOffsetAtStartOf(low);
946
947 float dist = Math.abs(getPrimaryHorizontal(low) - horiz);
948
949 int aft = TextUtils.getOffsetAfter(mText, low);
950 if (aft < there) {
951 float other = Math.abs(getPrimaryHorizontal(aft) - horiz);
952
953 if (other < dist) {
954 dist = other;
955 low = aft;
956 }
957 }
958
959 if (dist < bestdist) {
960 bestdist = dist;
Doug Felt9f7a4442010-03-01 12:45:56 -0800961 best = low;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963 }
964
965 float dist = Math.abs(getPrimaryHorizontal(here) - horiz);
966
967 if (dist < bestdist) {
968 bestdist = dist;
969 best = here;
970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 }
972
973 float dist = Math.abs(getPrimaryHorizontal(max) - horiz);
974
975 if (dist < bestdist) {
976 bestdist = dist;
977 best = max;
978 }
979
980 return best;
981 }
982
983 /**
984 * Return the text offset after the last character on the specified line.
985 */
986 public final int getLineEnd(int line) {
987 return getLineStart(line + 1);
988 }
989
Doug Felt9f7a4442010-03-01 12:45:56 -0800990 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 * Return the text offset after the last visible character (so whitespace
992 * is not counted) on the specified line.
993 */
994 public int getLineVisibleEnd(int line) {
995 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
996 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 CharSequence text = mText;
1000 char ch;
1001 if (line == getLineCount() - 1) {
1002 return end;
1003 }
1004
1005 for (; end > start; end--) {
1006 ch = text.charAt(end - 1);
1007
1008 if (ch == '\n') {
1009 return end - 1;
1010 }
1011
1012 if (ch != ' ' && ch != '\t') {
1013 break;
1014 }
1015
1016 }
1017
1018 return end;
1019 }
1020
1021 /**
1022 * Return the vertical position of the bottom of the specified line.
1023 */
1024 public final int getLineBottom(int line) {
1025 return getLineTop(line + 1);
1026 }
1027
1028 /**
1029 * Return the vertical position of the baseline of the specified line.
1030 */
1031 public final int getLineBaseline(int line) {
1032 // getLineTop(line+1) == getLineTop(line)
1033 return getLineTop(line+1) - getLineDescent(line);
1034 }
1035
1036 /**
1037 * Get the ascent of the text on the specified line.
1038 * The return value is negative to match the Paint.ascent() convention.
1039 */
1040 public final int getLineAscent(int line) {
1041 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1042 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1043 }
1044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001046 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001050 return getOffsetToLeftRightOf(offset, false);
1051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052
Doug Felt9f7a4442010-03-01 12:45:56 -08001053 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1054 int line = getLineForOffset(caret);
1055 int lineStart = getLineStart(line);
1056 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001057 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058
Doug Felte8e45f22010-03-29 14:58:40 -07001059 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
1060 if (caret == (advance ? lineEnd : lineStart)) {
1061 // walking off line, so look at the line we're headed to
1062 if (caret == lineStart) {
1063 if (line > 0) {
1064 --line;
1065 } else {
1066 return caret; // at very start, don't move
Doug Felt9f7a4442010-03-01 12:45:56 -08001067 }
Doug Felte8e45f22010-03-29 14:58:40 -07001068 } else {
1069 if (line < getLineCount() - 1) {
1070 ++line;
1071 } else {
1072 return caret; // at very end, don't move
1073 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001074 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001075
Doug Felte8e45f22010-03-29 14:58:40 -07001076 lineStart = getLineStart(line);
1077 lineEnd = getLineEnd(line);
1078 int newDir = getParagraphDirection(line);
1079 if (newDir != lineDir) {
1080 // unusual case. we want to walk onto the line, but it runs
1081 // in a different direction than this one, so we fake movement
1082 // in the opposite direction.
1083 toLeft = !toLeft;
1084 lineDir = newDir;
1085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001087
Doug Felte8e45f22010-03-29 14:58:40 -07001088 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001089
Doug Felte8e45f22010-03-29 14:58:40 -07001090 TextLine tl = TextLine.obtain();
1091 // XXX: we don't care about tabs
1092 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1093 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1094 tl = TextLine.recycle(tl);
1095 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097
1098 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001099 // XXX this probably should skip local reorderings and
1100 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 if (offset == 0)
1102 return 0;
1103
1104 CharSequence text = mText;
1105 char c = text.charAt(offset);
1106
1107 if (c >= '\uDC00' && c <= '\uDFFF') {
1108 char c1 = text.charAt(offset - 1);
1109
1110 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1111 offset -= 1;
1112 }
1113
1114 if (mSpannedText) {
1115 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1116 ReplacementSpan.class);
1117
1118 for (int i = 0; i < spans.length; i++) {
1119 int start = ((Spanned) text).getSpanStart(spans[i]);
1120 int end = ((Spanned) text).getSpanEnd(spans[i]);
1121
1122 if (start < offset && end > offset)
1123 offset = start;
1124 }
1125 }
1126
1127 return offset;
1128 }
1129
1130 /**
1131 * Fills in the specified Path with a representation of a cursor
1132 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001133 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 * directionalities.
1135 */
1136 public void getCursorPath(int point, Path dest,
1137 CharSequence editingBuffer) {
1138 dest.reset();
1139
1140 int line = getLineForOffset(point);
1141 int top = getLineTop(line);
1142 int bottom = getLineTop(line+1);
1143
1144 float h1 = getPrimaryHorizontal(point) - 0.5f;
Doug Felt4e0c5e52010-03-15 16:56:02 -07001145 float h2 = isLevelBoundary(point) ?
1146 getSecondaryHorizontal(point) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147
Jeff Brown497a92c2010-09-12 17:55:08 -07001148 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1149 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1150 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 int dist = 0;
1152
1153 if (caps != 0 || fn != 0) {
1154 dist = (bottom - top) >> 2;
1155
1156 if (fn != 0)
1157 top += dist;
1158 if (caps != 0)
1159 bottom -= dist;
1160 }
1161
1162 if (h1 < 0.5f)
1163 h1 = 0.5f;
1164 if (h2 < 0.5f)
1165 h2 = 0.5f;
1166
1167 if (h1 == h2) {
1168 dest.moveTo(h1, top);
1169 dest.lineTo(h1, bottom);
1170 } else {
1171 dest.moveTo(h1, top);
1172 dest.lineTo(h1, (top + bottom) >> 1);
1173
1174 dest.moveTo(h2, (top + bottom) >> 1);
1175 dest.lineTo(h2, bottom);
1176 }
1177
1178 if (caps == 2) {
1179 dest.moveTo(h2, bottom);
1180 dest.lineTo(h2 - dist, bottom + dist);
1181 dest.lineTo(h2, bottom);
1182 dest.lineTo(h2 + dist, bottom + dist);
1183 } else if (caps == 1) {
1184 dest.moveTo(h2, bottom);
1185 dest.lineTo(h2 - dist, bottom + dist);
1186
1187 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1188 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1189
1190 dest.moveTo(h2 + dist, bottom + dist);
1191 dest.lineTo(h2, bottom);
1192 }
1193
1194 if (fn == 2) {
1195 dest.moveTo(h1, top);
1196 dest.lineTo(h1 - dist, top - dist);
1197 dest.lineTo(h1, top);
1198 dest.lineTo(h1 + dist, top - dist);
1199 } else if (fn == 1) {
1200 dest.moveTo(h1, top);
1201 dest.lineTo(h1 - dist, top - dist);
1202
1203 dest.moveTo(h1 - dist, top - dist + 0.5f);
1204 dest.lineTo(h1 + dist, top - dist + 0.5f);
1205
1206 dest.moveTo(h1 + dist, top - dist);
1207 dest.lineTo(h1, top);
1208 }
1209 }
1210
1211 private void addSelection(int line, int start, int end,
1212 int top, int bottom, Path dest) {
1213 int linestart = getLineStart(line);
1214 int lineend = getLineEnd(line);
1215 Directions dirs = getLineDirections(line);
1216
1217 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1218 lineend--;
1219
Doug Felt9f7a4442010-03-01 12:45:56 -08001220 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1221 int here = linestart + dirs.mDirections[i];
1222 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 if (there > lineend)
1225 there = lineend;
1226
1227 if (start <= there && end >= here) {
1228 int st = Math.max(start, here);
1229 int en = Math.min(end, there);
1230
1231 if (st != en) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001232 float h1 = getHorizontal(st, false, line);
1233 float h2 = getHorizontal(en, true, line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234
1235 dest.addRect(h1, top, h2, bottom, Path.Direction.CW);
1236 }
1237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 }
1239 }
1240
1241 /**
1242 * Fills in the specified Path with a representation of a highlight
1243 * between the specified offsets. This will often be a rectangle
1244 * or a potentially discontinuous set of rectangles. If the start
1245 * and end are the same, the returned path is empty.
1246 */
1247 public void getSelectionPath(int start, int end, Path dest) {
1248 dest.reset();
1249
1250 if (start == end)
1251 return;
1252
1253 if (end < start) {
1254 int temp = end;
1255 end = start;
1256 start = temp;
1257 }
1258
1259 int startline = getLineForOffset(start);
1260 int endline = getLineForOffset(end);
1261
1262 int top = getLineTop(startline);
1263 int bottom = getLineBottom(endline);
1264
1265 if (startline == endline) {
1266 addSelection(startline, start, end, top, bottom, dest);
1267 } else {
1268 final float width = mWidth;
1269
1270 addSelection(startline, start, getLineEnd(startline),
1271 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1274 dest.addRect(getLineLeft(startline), top,
1275 0, getLineBottom(startline), Path.Direction.CW);
1276 else
1277 dest.addRect(getLineRight(startline), top,
1278 width, getLineBottom(startline), Path.Direction.CW);
1279
1280 for (int i = startline + 1; i < endline; i++) {
1281 top = getLineTop(i);
1282 bottom = getLineBottom(i);
1283 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1284 }
1285
1286 top = getLineTop(endline);
1287 bottom = getLineBottom(endline);
1288
1289 addSelection(endline, getLineStart(endline), end,
1290 top, bottom, dest);
1291
1292 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1293 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1294 else
1295 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1296 }
1297 }
1298
1299 /**
1300 * Get the alignment of the specified paragraph, taking into account
1301 * markup attached to it.
1302 */
1303 public final Alignment getParagraphAlignment(int line) {
1304 Alignment align = mAlignment;
1305
1306 if (mSpannedText) {
1307 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001308 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 getLineEnd(line),
1310 AlignmentSpan.class);
1311
1312 int spanLength = spans.length;
1313 if (spanLength > 0) {
1314 align = spans[spanLength-1].getAlignment();
1315 }
1316 }
1317
1318 return align;
1319 }
1320
1321 /**
1322 * Get the left edge of the specified paragraph, inset by left margins.
1323 */
1324 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001326 int dir = getParagraphDirection(line);
1327 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1328 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
Doug Feltc982f602010-05-25 11:51:40 -07001330 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
1332
1333 /**
1334 * Get the right edge of the specified paragraph, inset by right margins.
1335 */
1336 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001338 int dir = getParagraphDirection(line);
1339 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1340 return right; // leading margin has no impact, or no styles
1341 }
1342 return right - getParagraphLeadingMargin(line);
1343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344
Doug Feltc982f602010-05-25 11:51:40 -07001345 /**
1346 * Returns the effective leading margin (unsigned) for this line,
1347 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1348 * @param line the line index
1349 * @return the leading margin of this line
1350 */
1351 private int getParagraphLeadingMargin(int line) {
1352 if (!mSpannedText) {
1353 return 0;
1354 }
1355 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001356
Doug Feltc982f602010-05-25 11:51:40 -07001357 int lineStart = getLineStart(line);
1358 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001359 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001360 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001361 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001362 LeadingMarginSpan.class);
1363 if (spans.length == 0) {
1364 return 0; // no leading margin span;
1365 }
Doug Felt0c702b82010-05-14 10:55:42 -07001366
Doug Feltc982f602010-05-25 11:51:40 -07001367 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001368
1369 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001370 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001371
Doug Feltc982f602010-05-25 11:51:40 -07001372 for (int i = 0; i < spans.length; i++) {
1373 LeadingMarginSpan span = spans[i];
1374 boolean useFirstLineMargin = isFirstParaLine;
1375 if (span instanceof LeadingMarginSpan2) {
1376 int spStart = spanned.getSpanStart(span);
1377 int spanLine = getLineForOffset(spStart);
1378 int count = ((LeadingMarginSpan2)span).getLeadingMarginLineCount();
Doug Felt0c702b82010-05-14 10:55:42 -07001379 useFirstLineMargin = line < spanLine + count;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
Doug Feltc982f602010-05-25 11:51:40 -07001381 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383
Doug Feltc982f602010-05-25 11:51:40 -07001384 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
1386
Doug Felte8e45f22010-03-29 14:58:40 -07001387 /* package */
1388 static float measurePara(TextPaint paint, TextPaint workPaint,
Doug Feltc982f602010-05-25 11:51:40 -07001389 CharSequence text, int start, int end) {
Doug Felte8e45f22010-03-29 14:58:40 -07001390
1391 MeasuredText mt = MeasuredText.obtain();
1392 TextLine tl = TextLine.obtain();
1393 try {
1394 mt.setPara(text, start, end, DIR_REQUEST_LTR);
1395 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001396 int dir;
1397 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001398 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001399 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001400 } else {
1401 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1402 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001403 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
Doug Feltc982f602010-05-25 11:51:40 -07001405 char[] chars = mt.mChars;
1406 int len = mt.mLen;
1407 boolean hasTabs = false;
1408 TabStops tabStops = null;
1409 for (int i = 0; i < len; ++i) {
1410 if (chars[i] == '\t') {
1411 hasTabs = true;
1412 if (text instanceof Spanned) {
1413 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001414 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001415 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001416 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001417 TabStopSpan.class);
1418 if (spans.length > 0) {
1419 tabStops = new TabStops(TAB_INCREMENT, spans);
1420 }
1421 }
1422 break;
1423 }
1424 }
1425 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001426 return tl.metrics(null);
1427 } finally {
1428 TextLine.recycle(tl);
1429 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432
Doug Felt71b8dd72010-02-16 17:27:09 -08001433 /**
Doug Feltc982f602010-05-25 11:51:40 -07001434 * @hide
1435 */
1436 /* package */ static class TabStops {
1437 private int[] mStops;
1438 private int mNumStops;
1439 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001440
Doug Feltc982f602010-05-25 11:51:40 -07001441 TabStops(int increment, Object[] spans) {
1442 reset(increment, spans);
1443 }
Doug Felt0c702b82010-05-14 10:55:42 -07001444
Doug Feltc982f602010-05-25 11:51:40 -07001445 void reset(int increment, Object[] spans) {
1446 this.mIncrement = increment;
1447
1448 int ns = 0;
1449 if (spans != null) {
1450 int[] stops = this.mStops;
1451 for (Object o : spans) {
1452 if (o instanceof TabStopSpan) {
1453 if (stops == null) {
1454 stops = new int[10];
1455 } else if (ns == stops.length) {
1456 int[] nstops = new int[ns * 2];
1457 for (int i = 0; i < ns; ++i) {
1458 nstops[i] = stops[i];
1459 }
1460 stops = nstops;
1461 }
1462 stops[ns++] = ((TabStopSpan) o).getTabStop();
1463 }
1464 }
1465 if (ns > 1) {
1466 Arrays.sort(stops, 0, ns);
1467 }
1468 if (stops != this.mStops) {
1469 this.mStops = stops;
1470 }
1471 }
1472 this.mNumStops = ns;
1473 }
Doug Felt0c702b82010-05-14 10:55:42 -07001474
Doug Feltc982f602010-05-25 11:51:40 -07001475 float nextTab(float h) {
1476 int ns = this.mNumStops;
1477 if (ns > 0) {
1478 int[] stops = this.mStops;
1479 for (int i = 0; i < ns; ++i) {
1480 int stop = stops[i];
1481 if (stop > h) {
1482 return stop;
1483 }
1484 }
1485 }
1486 return nextDefaultStop(h, mIncrement);
1487 }
1488
1489 public static float nextDefaultStop(float h, int inc) {
1490 return ((int) ((h + inc) / inc)) * inc;
1491 }
1492 }
Doug Felt0c702b82010-05-14 10:55:42 -07001493
Doug Feltc982f602010-05-25 11:51:40 -07001494 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001495 * Returns the position of the next tab stop after h on the line.
1496 *
1497 * @param text the text
1498 * @param start start of the line
1499 * @param end limit of the line
1500 * @param h the current horizontal offset
1501 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1502 * on the line will be used. If there are no tabs, a default offset
1503 * will be used to compute the tab stop.
1504 * @return the offset of the next tab stop.
1505 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 /* package */ static float nextTab(CharSequence text, int start, int end,
1507 float h, Object[] tabs) {
1508 float nh = Float.MAX_VALUE;
1509 boolean alltabs = false;
1510
1511 if (text instanceof Spanned) {
1512 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001513 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 alltabs = true;
1515 }
1516
1517 for (int i = 0; i < tabs.length; i++) {
1518 if (!alltabs) {
1519 if (!(tabs[i] instanceof TabStopSpan))
1520 continue;
1521 }
1522
1523 int where = ((TabStopSpan) tabs[i]).getTabStop();
1524
1525 if (where < nh && where > h)
1526 nh = where;
1527 }
1528
1529 if (nh != Float.MAX_VALUE)
1530 return nh;
1531 }
1532
1533 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1534 }
1535
1536 protected final boolean isSpanned() {
1537 return mSpannedText;
1538 }
1539
Eric Fischer74d31ef2010-08-05 15:29:36 -07001540 /**
1541 * Returns the same as <code>text.getSpans()</code>, except where
1542 * <code>start</code> and <code>end</code> are the same and are not
1543 * at the very beginning of the text, in which case an empty array
1544 * is returned instead.
1545 * <p>
1546 * This is needed because of the special case that <code>getSpans()</code>
1547 * on an empty range returns the spans adjacent to that range, which is
1548 * primarily for the sake of <code>TextWatchers</code> so they will get
1549 * notifications when text goes from empty to non-empty. But it also
1550 * has the unfortunate side effect that if the text ends with an empty
1551 * paragraph, that paragraph accidentally picks up the styles of the
1552 * preceding paragraph (even though those styles will not be picked up
1553 * by new text that is inserted into the empty paragraph).
1554 * <p>
1555 * The reason it just checks whether <code>start</code> and <code>end</code>
1556 * is the same is that the only time a line can contain 0 characters
1557 * is if it is the final paragraph of the Layout; otherwise any line will
1558 * contain at least one printing or newline character. The reason for the
1559 * additional check if <code>start</code> is greater than 0 is that
1560 * if the empty paragraph is the entire content of the buffer, paragraph
1561 * styles that are already applied to the buffer will apply to text that
1562 * is inserted into it.
1563 */
1564 /* package */ static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
1565 if (start == end && start > 0) {
1566 return (T[]) ArrayUtils.emptyArray(type);
1567 }
1568
1569 return text.getSpans(start, end, type);
1570 }
1571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 private void ellipsize(int start, int end, int line,
1573 char[] dest, int destoff) {
1574 int ellipsisCount = getEllipsisCount(line);
1575
1576 if (ellipsisCount == 0) {
1577 return;
1578 }
1579
1580 int ellipsisStart = getEllipsisStart(line);
1581 int linestart = getLineStart(line);
1582
1583 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1584 char c;
1585
1586 if (i == ellipsisStart) {
1587 c = '\u2026'; // ellipsis
1588 } else {
1589 c = '\uFEFF'; // 0-width space
1590 }
1591
1592 int a = i + linestart;
1593
1594 if (a >= start && a < end) {
1595 dest[destoff + a - start] = c;
1596 }
1597 }
1598 }
1599
1600 /**
1601 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08001602 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 */
1604 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08001605 // Directions represents directional runs within a line of text.
1606 // Runs are pairs of ints listed in visual order, starting from the
1607 // leading margin. The first int of each pair is the offset from
1608 // the first character of the line to the start of the run. The
1609 // second int represents both the length and level of the run.
1610 // The length is in the lower bits, accessed by masking with
1611 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1612 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1613 // To simply test for an RTL direction, test the bit using
1614 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615
Doug Felt9f7a4442010-03-01 12:45:56 -08001616 /* package */ int[] mDirections;
1617 /* package */ Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 mDirections = dirs;
1619 }
1620 }
1621
1622 /**
1623 * Return the offset of the first character to be ellipsized away,
1624 * relative to the start of the line. (So 0 if the beginning of the
1625 * line is ellipsized, not getLineStart().)
1626 */
1627 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07001628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 /**
1630 * Returns the number of characters to be ellipsized away, or 0 if
1631 * no ellipsis is to take place.
1632 */
1633 public abstract int getEllipsisCount(int line);
1634
1635 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1636 /* package */ CharSequence mText;
1637 /* package */ Layout mLayout;
1638 /* package */ int mWidth;
1639 /* package */ TextUtils.TruncateAt mMethod;
1640
1641 public Ellipsizer(CharSequence s) {
1642 mText = s;
1643 }
1644
1645 public char charAt(int off) {
1646 char[] buf = TextUtils.obtain(1);
1647 getChars(off, off + 1, buf, 0);
1648 char ret = buf[0];
1649
1650 TextUtils.recycle(buf);
1651 return ret;
1652 }
1653
1654 public void getChars(int start, int end, char[] dest, int destoff) {
1655 int line1 = mLayout.getLineForOffset(start);
1656 int line2 = mLayout.getLineForOffset(end);
1657
1658 TextUtils.getChars(mText, start, end, dest, destoff);
1659
1660 for (int i = line1; i <= line2; i++) {
1661 mLayout.ellipsize(start, end, i, dest, destoff);
1662 }
1663 }
1664
1665 public int length() {
1666 return mText.length();
1667 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 public CharSequence subSequence(int start, int end) {
1670 char[] s = new char[end - start];
1671 getChars(start, end, s, 0);
1672 return new String(s);
1673 }
1674
1675 public String toString() {
1676 char[] s = new char[length()];
1677 getChars(0, length(), s, 0);
1678 return new String(s);
1679 }
1680
1681 }
1682
1683 /* package */ static class SpannedEllipsizer
1684 extends Ellipsizer implements Spanned {
1685 private Spanned mSpanned;
1686
1687 public SpannedEllipsizer(CharSequence display) {
1688 super(display);
1689 mSpanned = (Spanned) display;
1690 }
1691
1692 public <T> T[] getSpans(int start, int end, Class<T> type) {
1693 return mSpanned.getSpans(start, end, type);
1694 }
1695
1696 public int getSpanStart(Object tag) {
1697 return mSpanned.getSpanStart(tag);
1698 }
1699
1700 public int getSpanEnd(Object tag) {
1701 return mSpanned.getSpanEnd(tag);
1702 }
1703
1704 public int getSpanFlags(Object tag) {
1705 return mSpanned.getSpanFlags(tag);
1706 }
1707
1708 public int nextSpanTransition(int start, int limit, Class type) {
1709 return mSpanned.nextSpanTransition(start, limit, type);
1710 }
1711
1712 public CharSequence subSequence(int start, int end) {
1713 char[] s = new char[end - start];
1714 getChars(start, end, s, 0);
1715
1716 SpannableString ss = new SpannableString(new String(s));
1717 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
1718 return ss;
1719 }
1720 }
1721
1722 private CharSequence mText;
1723 private TextPaint mPaint;
1724 /* package */ TextPaint mWorkPaint;
1725 private int mWidth;
1726 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
1727 private float mSpacingMult;
1728 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07001729 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 private boolean mSpannedText;
1731
1732 public static final int DIR_LEFT_TO_RIGHT = 1;
1733 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08001734
Doug Felt20178d62010-02-22 13:39:01 -08001735 /* package */ static final int DIR_REQUEST_LTR = 1;
1736 /* package */ static final int DIR_REQUEST_RTL = -1;
1737 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
1738 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739
Doug Felt9f7a4442010-03-01 12:45:56 -08001740 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
1741 /* package */ static final int RUN_LEVEL_SHIFT = 26;
1742 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
1743 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
1744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 public enum Alignment {
1746 ALIGN_NORMAL,
1747 ALIGN_OPPOSITE,
1748 ALIGN_CENTER,
1749 // XXX ALIGN_LEFT,
1750 // XXX ALIGN_RIGHT,
1751 }
1752
1753 private static final int TAB_INCREMENT = 20;
1754
1755 /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001756 new Directions(new int[] { 0, RUN_LENGTH_MASK });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001758 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759}