blob: 3fe4f4c936a2c643c6fe3e8d83fb50898cb9c2e4 [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;
Gilles Debunne162bf0f2010-11-16 16:23:53 -080029import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
Doug Felt9f7a4442010-03-01 12:45:56 -080030import android.text.style.LineBackgroundSpan;
31import android.text.style.ParagraphStyle;
32import android.text.style.ReplacementSpan;
33import android.text.style.TabStopSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
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;
79 TextPaint workPaint = new TextPaint();
80
81 int next;
82 for (int i = start; i <= end; i = next) {
83 next = TextUtils.indexOf(source, '\n', i, end);
84
85 if (next < 0)
86 next = end;
87
Doug Felt71b8dd72010-02-16 17:27:09 -080088 // note, omits trailing paragraph char
Doug Feltc982f602010-05-25 11:51:40 -070089 float w = measurePara(paint, workPaint, source, i, next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91 if (w > need)
92 need = w;
93
94 next++;
95 }
96
97 return need;
98 }
99
100 /**
101 * Subclasses of Layout use this constructor to set the display text,
102 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800103 * @param text the text to render
104 * @param paint the default paint for the layout. Styles can override
105 * various attributes of the paint.
106 * @param width the wrapping width for the text.
107 * @param align whether to left, right, or center the text. Styles can
108 * override the alignment.
109 * @param spacingMult factor by which to scale the font size to get the
110 * default line spacing
111 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 */
113 protected Layout(CharSequence text, TextPaint paint,
114 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800115 float spacingMult, float spacingAdd) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 if (width < 0)
117 throw new IllegalArgumentException("Layout: " + width + " < 0");
118
Doug Felte8e45f22010-03-29 14:58:40 -0700119 // Ensure paint doesn't have baselineShift set.
120 // While normally we don't modify the paint the user passed in,
121 // we were already doing this in Styled.drawUniformRun with both
122 // baselineShift and bgColor. We probably should reevaluate bgColor.
123 if (paint != null) {
124 paint.bgColor = 0;
125 paint.baselineShift = 0;
126 }
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 mText = text;
129 mPaint = paint;
130 mWorkPaint = new TextPaint();
131 mWidth = width;
132 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800133 mSpacingMult = spacingMult;
134 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 mSpannedText = text instanceof Spanned;
136 }
137
138 /**
139 * Replace constructor properties of this Layout with new ones. Be careful.
140 */
141 /* package */ void replaceWith(CharSequence text, TextPaint paint,
142 int width, Alignment align,
143 float spacingmult, float spacingadd) {
144 if (width < 0) {
145 throw new IllegalArgumentException("Layout: " + width + " < 0");
146 }
147
148 mText = text;
149 mPaint = paint;
150 mWidth = width;
151 mAlignment = align;
152 mSpacingMult = spacingmult;
153 mSpacingAdd = spacingadd;
154 mSpannedText = text instanceof Spanned;
155 }
156
157 /**
158 * Draw this Layout on the specified Canvas.
159 */
160 public void draw(Canvas c) {
161 draw(c, null, null, 0);
162 }
163
164 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800165 * Draw this Layout on the specified canvas, with the highlight path drawn
166 * between the background and the text.
167 *
168 * @param c the canvas
169 * @param highlight the path of the highlight or cursor; can be null
170 * @param highlightPaint the paint for the highlight
171 * @param cursorOffsetVertical the amount to temporarily translate the
172 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 */
Doug Felt71b8dd72010-02-16 17:27:09 -0800174 public void draw(Canvas c, Path highlight, Paint highlightPaint,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 int cursorOffsetVertical) {
176 int dtop, dbottom;
177
178 synchronized (sTempRect) {
179 if (!c.getClipBounds(sTempRect)) {
180 return;
181 }
182
183 dtop = sTempRect.top;
184 dbottom = sTempRect.bottom;
185 }
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 int top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 int bottom = getLineTop(getLineCount());
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 if (dtop > top) {
191 top = dtop;
192 }
193 if (dbottom < bottom) {
194 bottom = dbottom;
195 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800196
197 int first = getLineForVertical(top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 int last = getLineForVertical(bottom);
Doug Felt9f7a4442010-03-01 12:45:56 -0800199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 int previousLineBottom = getLineTop(first);
201 int previousLineEnd = getLineStart(first);
Doug Felt9f7a4442010-03-01 12:45:56 -0800202
Doug Felt71b8dd72010-02-16 17:27:09 -0800203 TextPaint paint = mPaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 CharSequence buf = mText;
Doug Felt71b8dd72010-02-16 17:27:09 -0800205 int width = mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 boolean spannedText = mSpannedText;
207
Doug Felt71b8dd72010-02-16 17:27:09 -0800208 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700209 int spanEnd = 0;
Doug Felt71b8dd72010-02-16 17:27:09 -0800210 int textLength = 0;
211
212 // First, draw LineBackgroundSpans.
Doug Felt0c702b82010-05-14 10:55:42 -0700213 // LineBackgroundSpans know nothing about the alignment, margins, or
Doug Feltc982f602010-05-25 11:51:40 -0700214 // direction of the layout or line. XXX: Should they?
215 // They are evaluated at each line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 if (spannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700217 Spanned sp = (Spanned) buf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 textLength = buf.length();
219 for (int i = first; i <= last; i++) {
220 int start = previousLineEnd;
221 int end = getLineStart(i+1);
222 previousLineEnd = end;
223
224 int ltop = previousLineBottom;
225 int lbottom = getLineTop(i+1);
226 previousLineBottom = lbottom;
227 int lbaseline = lbottom - getLineDescent(i);
228
Doug Feltc982f602010-05-25 11:51:40 -0700229 if (start >= spanEnd) {
230 // These should be infrequent, so we'll use this so that
231 // we don't have to check as often.
Doug Felt0c702b82010-05-14 10:55:42 -0700232 spanEnd = sp.nextSpanTransition(start, textLength,
Doug Feltc982f602010-05-25 11:51:40 -0700233 LineBackgroundSpan.class);
234 // All LineBackgroundSpans on a line contribute to its
235 // background.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700236 spans = getParagraphSpans(sp, start, end, LineBackgroundSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 }
238
239 for (int n = 0; n < spans.length; n++) {
240 LineBackgroundSpan back = (LineBackgroundSpan) spans[n];
241
Doug Felt71b8dd72010-02-16 17:27:09 -0800242 back.drawBackground(c, paint, 0, width,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 ltop, lbaseline, lbottom,
244 buf, start, end,
245 i);
246 }
247 }
248 // reset to their original values
Doug Feltc982f602010-05-25 11:51:40 -0700249 spanEnd = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 previousLineBottom = getLineTop(first);
251 previousLineEnd = getLineStart(first);
Doug Felt71b8dd72010-02-16 17:27:09 -0800252 spans = NO_PARA_SPANS;
Doug Felt9f7a4442010-03-01 12:45:56 -0800253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
255 // There can be a highlight even without spans if we are drawing
256 // a non-spanned transformation of a spanned editing buffer.
257 if (highlight != null) {
258 if (cursorOffsetVertical != 0) {
259 c.translate(0, cursorOffsetVertical);
260 }
261
Doug Felt71b8dd72010-02-16 17:27:09 -0800262 c.drawPath(highlight, highlightPaint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263
264 if (cursorOffsetVertical != 0) {
265 c.translate(0, -cursorOffsetVertical);
266 }
267 }
268
269 Alignment align = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700270 TabStops tabStops = null;
271 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800272
Doug Felte8e45f22010-03-29 14:58:40 -0700273 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700274
Doug Felt71b8dd72010-02-16 17:27:09 -0800275 // Next draw the lines, one at a time.
276 // the baseline is the top of the following line minus the current
277 // line's descent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 for (int i = first; i <= last; i++) {
279 int start = previousLineEnd;
280
281 previousLineEnd = getLineStart(i+1);
282 int end = getLineVisibleEnd(i, start, previousLineEnd);
283
284 int ltop = previousLineBottom;
285 int lbottom = getLineTop(i+1);
286 previousLineBottom = lbottom;
287 int lbaseline = lbottom - getLineDescent(i);
288
Doug Feltc982f602010-05-25 11:51:40 -0700289 int dir = getParagraphDirection(i);
290 int left = 0;
291 int right = mWidth;
292
Doug Felt9f7a4442010-03-01 12:45:56 -0800293 if (spannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700294 Spanned sp = (Spanned) buf;
295 boolean isFirstParaLine = (start == 0 ||
296 buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700297
Doug Feltc982f602010-05-25 11:51:40 -0700298 // New batch of paragraph styles, collect into spans array.
299 // Compute the alignment, last alignment style wins.
300 // Reset tabStops, we'll rebuild if we encounter a line with
301 // tabs.
302 // We expect paragraph spans to be relatively infrequent, use
303 // spanEnd so that we can check less frequently. Since
304 // paragraph styles ought to apply to entire paragraphs, we can
305 // just collect the ones present at the start of the paragraph.
306 // If spanEnd is before the end of the paragraph, that's not
307 // our problem.
308 if (start >= spanEnd && (i == first || isFirstParaLine)) {
309 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700311 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 align = mAlignment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 for (int n = spans.length-1; n >= 0; n--) {
315 if (spans[n] instanceof AlignmentSpan) {
316 align = ((AlignmentSpan) spans[n]).getAlignment();
317 break;
318 }
319 }
Doug Felt0c702b82010-05-14 10:55:42 -0700320
Doug Feltc982f602010-05-25 11:51:40 -0700321 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800323
Doug Feltc982f602010-05-25 11:51:40 -0700324 // Draw all leading margin spans. Adjust left or right according
325 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 final int length = spans.length;
327 for (int n = 0; n < length; n++) {
328 if (spans[n] instanceof LeadingMarginSpan) {
329 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
Doug Feltc982f602010-05-25 11:51:40 -0700330 boolean useFirstLineMargin = isFirstParaLine;
331 if (margin instanceof LeadingMarginSpan2) {
332 int count = ((LeadingMarginSpan2) margin).getLeadingMarginLineCount();
333 int startLine = getLineForOffset(sp.getSpanStart(margin));
334 useFirstLineMargin = i < startLine + count;
335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336
337 if (dir == DIR_RIGHT_TO_LEFT) {
338 margin.drawLeadingMargin(c, paint, right, dir, ltop,
339 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800340 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700341 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 } else {
343 margin.drawLeadingMargin(c, paint, left, dir, ltop,
344 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800345 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700346 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 }
348 }
349 }
350 }
351
Doug Feltc982f602010-05-25 11:51:40 -0700352 boolean hasTabOrEmoji = getLineContainsTab(i);
353 // Can't tell if we have tabs for sure, currently
354 if (hasTabOrEmoji && !tabStopsIsInitialized) {
355 if (tabStops == null) {
356 tabStops = new TabStops(TAB_INCREMENT, spans);
357 } else {
358 tabStops.reset(TAB_INCREMENT, spans);
359 }
360 tabStopsIsInitialized = true;
361 }
362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 int x;
364 if (align == Alignment.ALIGN_NORMAL) {
365 if (dir == DIR_LEFT_TO_RIGHT) {
366 x = left;
367 } else {
368 x = right;
369 }
370 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700371 int max = (int)getLineExtent(i, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700373 if (dir == DIR_LEFT_TO_RIGHT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 x = right - max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700376 x = left - max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
Doug Feltc982f602010-05-25 11:51:40 -0700378 } else { // Alignment.ALIGN_CENTER
379 max = max & ~1;
380 x = (right + left - max) >> 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382 }
383
384 Directions directions = getLineDirections(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 if (directions == DIRS_ALL_LEFT_TO_RIGHT &&
Doug Feltc982f602010-05-25 11:51:40 -0700386 !spannedText && !hasTabOrEmoji) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800387 // XXX: assumes there's nothing additional to be done
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 c.drawText(buf, start, end, x, lbaseline, paint);
389 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700390 tl.set(paint, buf, start, end, dir, directions, hasTabOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700391 tl.draw(c, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 }
393 }
Doug Feltc982f602010-05-25 11:51:40 -0700394
Doug Felte8e45f22010-03-29 14:58:40 -0700395 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
397
398 /**
Doug Feltc982f602010-05-25 11:51:40 -0700399 * Return the start position of the line, given the left and right bounds
400 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700401 *
Doug Feltc982f602010-05-25 11:51:40 -0700402 * @param line the line index
403 * @param left the left bounds (0, or leading margin if ltr para)
404 * @param right the right bounds (width, minus leading margin if rtl para)
405 * @return the start position of the line (to right of line if rtl para)
406 */
407 private int getLineStartPos(int line, int left, int right) {
408 // Adjust the point at which to start rendering depending on the
409 // alignment of the paragraph.
410 Alignment align = getParagraphAlignment(line);
411 int dir = getParagraphDirection(line);
412
413 int x;
414 if (align == Alignment.ALIGN_NORMAL) {
415 if (dir == DIR_LEFT_TO_RIGHT) {
416 x = left;
417 } else {
418 x = right;
419 }
420 } else {
421 TabStops tabStops = null;
422 if (mSpannedText && getLineContainsTab(line)) {
423 Spanned spanned = (Spanned) mText;
424 int start = getLineStart(line);
425 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
426 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700427 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700428 if (tabSpans.length > 0) {
429 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
430 }
431 }
432 int max = (int)getLineExtent(line, tabStops, false);
433 if (align == Alignment.ALIGN_OPPOSITE) {
434 if (dir == DIR_LEFT_TO_RIGHT) {
435 x = right - max;
436 } else {
437 x = left - max;
438 }
439 } else { // Alignment.ALIGN_CENTER
440 max = max & ~1;
441 x = (left + right - max) >> 1;
442 }
443 }
444 return x;
445 }
446
447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * Return the text that is displayed by this Layout.
449 */
450 public final CharSequence getText() {
451 return mText;
452 }
453
454 /**
455 * Return the base Paint properties for this layout.
456 * Do NOT change the paint, which may result in funny
457 * drawing for this layout.
458 */
459 public final TextPaint getPaint() {
460 return mPaint;
461 }
462
463 /**
464 * Return the width of this layout.
465 */
466 public final int getWidth() {
467 return mWidth;
468 }
469
470 /**
471 * Return the width to which this Layout is ellipsizing, or
472 * {@link #getWidth} if it is not doing anything special.
473 */
474 public int getEllipsizedWidth() {
475 return mWidth;
476 }
477
478 /**
479 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800480 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 * it does not cause the text to reflow to use the full new width.
482 */
483 public final void increaseWidthTo(int wid) {
484 if (wid < mWidth) {
485 throw new RuntimeException("attempted to reduce Layout width");
486 }
487
488 mWidth = wid;
489 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 /**
492 * Return the total height of this layout.
493 */
494 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800495 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 }
497
498 /**
499 * Return the base alignment of this layout.
500 */
501 public final Alignment getAlignment() {
502 return mAlignment;
503 }
504
505 /**
506 * Return what the text height is multiplied by to get the line height.
507 */
508 public final float getSpacingMultiplier() {
509 return mSpacingMult;
510 }
511
512 /**
513 * Return the number of units of leading that are added to each line.
514 */
515 public final float getSpacingAdd() {
516 return mSpacingAdd;
517 }
518
519 /**
520 * Return the number of lines of text in this layout.
521 */
522 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 /**
525 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
526 * If bounds is not null, return the top, left, right, bottom extents
527 * of the specified line in it.
528 * @param line which line to examine (0..getLineCount() - 1)
529 * @param bounds Optional. If not null, it returns the extent of the line
530 * @return the Y-coordinate of the baseline
531 */
532 public int getLineBounds(int line, Rect bounds) {
533 if (bounds != null) {
534 bounds.left = 0; // ???
535 bounds.top = getLineTop(line);
536 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800537 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539 return getLineBaseline(line);
540 }
541
542 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800543 * Return the vertical position of the top of the specified line
544 * (0&hellip;getLineCount()).
545 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 * bottom of the last line.
547 */
548 public abstract int getLineTop(int line);
549
550 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800551 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 */
553 public abstract int getLineDescent(int line);
554
555 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800556 * Return the text offset of the beginning of the specified line (
557 * 0&hellip;getLineCount()). If the specified line is equal to the line
558 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 */
560 public abstract int getLineStart(int line);
561
562 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800563 * Returns the primary directionality of the paragraph containing the
564 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
565 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 */
567 public abstract int getParagraphDirection(int line);
568
569 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700570 * Returns whether the specified line contains one or more
571 * characters that need to be handled specially, like tabs
572 * or emoji.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 */
574 public abstract boolean getLineContainsTab(int line);
575
576 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800577 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 * The array alternates counts of characters in left-to-right
579 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800580 *
581 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 */
583 public abstract Directions getLineDirections(int line);
584
585 /**
586 * Returns the (negative) number of extra pixels of ascent padding in the
587 * top line of the Layout.
588 */
589 public abstract int getTopPadding();
590
591 /**
592 * Returns the number of extra pixels of descent padding in the
593 * bottom line of the Layout.
594 */
595 public abstract int getBottomPadding();
596
Doug Felt4e0c5e52010-03-15 16:56:02 -0700597
598 /**
599 * Returns true if the character at offset and the preceding character
600 * are at different run levels (and thus there's a split caret).
601 * @param offset the offset
602 * @return true if at a level boundary
603 */
604 private boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800605 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700606 Directions dirs = getLineDirections(line);
607 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
608 return false;
609 }
610
611 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800612 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700613 int lineEnd = getLineEnd(line);
614 if (offset == lineStart || offset == lineEnd) {
615 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
616 int runIndex = offset == lineStart ? 0 : runs.length - 2;
617 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
618 }
619
620 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800621 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700622 if (offset == runs[i]) {
623 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800624 }
625 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700626 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800627 }
628
629 private boolean primaryIsTrailingPrevious(int offset) {
630 int line = getLineForOffset(offset);
631 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700632 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800633 int[] runs = getLineDirections(line).mDirections;
634
635 int levelAt = -1;
636 for (int i = 0; i < runs.length; i += 2) {
637 int start = lineStart + runs[i];
638 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
639 if (limit > lineEnd) {
640 limit = lineEnd;
641 }
642 if (offset >= start && offset < limit) {
643 if (offset > start) {
644 // Previous character is at same level, so don't use trailing.
645 return false;
646 }
647 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
648 break;
649 }
650 }
651 if (levelAt == -1) {
652 // Offset was limit of line.
653 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
654 }
655
656 // At level boundary, check previous level.
657 int levelBefore = -1;
658 if (offset == lineStart) {
659 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
660 } else {
661 offset -= 1;
662 for (int i = 0; i < runs.length; i += 2) {
663 int start = lineStart + runs[i];
664 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
665 if (limit > lineEnd) {
666 limit = lineEnd;
667 }
668 if (offset >= start && offset < limit) {
669 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
670 break;
671 }
672 }
673 }
674
675 return levelBefore < levelAt;
676 }
677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 /**
679 * Get the primary horizontal position for the specified text offset.
680 * This is the location where a new character would be inserted in
681 * the paragraph's primary direction.
682 */
683 public float getPrimaryHorizontal(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800684 boolean trailing = primaryIsTrailingPrevious(offset);
685 return getHorizontal(offset, trailing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687
688 /**
689 * Get the secondary horizontal position for the specified text offset.
690 * This is the location where a new character would be inserted in
691 * the direction other than the paragraph's primary direction.
692 */
693 public float getSecondaryHorizontal(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800694 boolean trailing = primaryIsTrailingPrevious(offset);
695 return getHorizontal(offset, !trailing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 }
697
Doug Felt9f7a4442010-03-01 12:45:56 -0800698 private float getHorizontal(int offset, boolean trailing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 int line = getLineForOffset(offset);
700
Doug Felt9f7a4442010-03-01 12:45:56 -0800701 return getHorizontal(offset, trailing, line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703
Doug Felt9f7a4442010-03-01 12:45:56 -0800704 private float getHorizontal(int offset, boolean trailing, int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700706 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 int dir = getParagraphDirection(line);
Doug Feltc982f602010-05-25 11:51:40 -0700708 boolean hasTabOrEmoji = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 Directions directions = getLineDirections(line);
710
Doug Feltc982f602010-05-25 11:51:40 -0700711 TabStops tabStops = null;
712 if (hasTabOrEmoji && mText instanceof Spanned) {
713 // Just checking this line should be good enough, tabs should be
714 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700715 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700716 if (tabs.length > 0) {
717 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720
Doug Felte8e45f22010-03-29 14:58:40 -0700721 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700722 tl.set(mPaint, mText, start, end, dir, directions, hasTabOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700723 float wid = tl.measure(offset - start, trailing, null);
724 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 int left = getParagraphLeft(line);
727 int right = getParagraphRight(line);
728
Doug Feltc982f602010-05-25 11:51:40 -0700729 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
732 /**
733 * Get the leftmost position that should be exposed for horizontal
734 * scrolling on the specified line.
735 */
736 public float getLineLeft(int line) {
737 int dir = getParagraphDirection(line);
738 Alignment align = getParagraphAlignment(line);
739
740 if (align == Alignment.ALIGN_NORMAL) {
741 if (dir == DIR_RIGHT_TO_LEFT)
742 return getParagraphRight(line) - getLineMax(line);
743 else
744 return 0;
745 } else if (align == Alignment.ALIGN_OPPOSITE) {
746 if (dir == DIR_RIGHT_TO_LEFT)
747 return 0;
748 else
749 return mWidth - getLineMax(line);
750 } else { /* align == Alignment.ALIGN_CENTER */
751 int left = getParagraphLeft(line);
752 int right = getParagraphRight(line);
753 int max = ((int) getLineMax(line)) & ~1;
754
755 return left + ((right - left) - max) / 2;
756 }
757 }
758
759 /**
760 * Get the rightmost position that should be exposed for horizontal
761 * scrolling on the specified line.
762 */
763 public float getLineRight(int line) {
764 int dir = getParagraphDirection(line);
765 Alignment align = getParagraphAlignment(line);
766
767 if (align == Alignment.ALIGN_NORMAL) {
768 if (dir == DIR_RIGHT_TO_LEFT)
769 return mWidth;
770 else
771 return getParagraphLeft(line) + getLineMax(line);
772 } else if (align == Alignment.ALIGN_OPPOSITE) {
773 if (dir == DIR_RIGHT_TO_LEFT)
774 return getLineMax(line);
775 else
776 return mWidth;
777 } else { /* align == Alignment.ALIGN_CENTER */
778 int left = getParagraphLeft(line);
779 int right = getParagraphRight(line);
780 int max = ((int) getLineMax(line)) & ~1;
781
782 return right - ((right - left) - max) / 2;
783 }
784 }
785
786 /**
Doug Felt0c702b82010-05-14 10:55:42 -0700787 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -0700788 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 */
790 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700791 float margin = getParagraphLeadingMargin(line);
792 float signedExtent = getLineExtent(line, false);
793 return margin + signedExtent >= 0 ? signedExtent : -signedExtent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 }
795
796 /**
Doug Feltc982f602010-05-25 11:51:40 -0700797 * Gets the unsigned horizontal extent of the specified line, including
798 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 */
800 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -0700801 float margin = getParagraphLeadingMargin(line);
802 float signedExtent = getLineExtent(line, true);
803 return margin + signedExtent >= 0 ? signedExtent : -signedExtent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805
Doug Feltc982f602010-05-25 11:51:40 -0700806 /**
807 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
808 * tab stops instead of using the ones passed in.
809 * @param line the index of the line
810 * @param full whether to include trailing whitespace
811 * @return the extent of the line
812 */
813 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700815 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -0700816
817 boolean hasTabsOrEmoji = getLineContainsTab(line);
818 TabStops tabStops = null;
819 if (hasTabsOrEmoji && 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 }
826 }
Doug Felte8e45f22010-03-29 14:58:40 -0700827 Directions directions = getLineDirections(line);
Doug Feltc982f602010-05-25 11:51:40 -0700828 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829
Doug Felte8e45f22010-03-29 14:58:40 -0700830 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700831 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
832 float width = tl.metrics(null);
833 TextLine.recycle(tl);
834 return width;
835 }
836
837 /**
838 * Returns the signed horizontal extent of the specified line, excluding
839 * leading margin. If full is false, excludes trailing whitespace.
840 * @param line the index of the line
841 * @param tabStops the tab stops, can be null if we know they're not used.
842 * @param full whether to include trailing whitespace
843 * @return the extent of the text on this line
844 */
845 private float getLineExtent(int line, TabStops tabStops, boolean full) {
846 int start = getLineStart(line);
847 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
848 boolean hasTabsOrEmoji = getLineContainsTab(line);
849 Directions directions = getLineDirections(line);
850 int dir = getParagraphDirection(line);
851
852 TextLine tl = TextLine.obtain();
853 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700854 float width = tl.metrics(null);
855 TextLine.recycle(tl);
856 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 }
858
859 /**
860 * Get the line number corresponding to the specified vertical position.
861 * If you ask for a position above 0, you get 0; if you ask for a position
862 * below the bottom of the text, you get the last line.
863 */
864 // FIXME: It may be faster to do a linear search for layouts without many lines.
865 public int getLineForVertical(int vertical) {
866 int high = getLineCount(), low = -1, guess;
867
868 while (high - low > 1) {
869 guess = (high + low) / 2;
870
871 if (getLineTop(guess) > vertical)
872 high = guess;
873 else
874 low = guess;
875 }
876
877 if (low < 0)
878 return 0;
879 else
880 return low;
881 }
882
883 /**
884 * Get the line number on which the specified text offset appears.
885 * If you ask for a position before 0, you get 0; if you ask for a position
886 * beyond the end of the text, you get the last line.
887 */
888 public int getLineForOffset(int offset) {
889 int high = getLineCount(), low = -1, guess;
890
891 while (high - low > 1) {
892 guess = (high + low) / 2;
893
894 if (getLineStart(guess) > offset)
895 high = guess;
896 else
897 low = guess;
898 }
899
900 if (low < 0)
901 return 0;
902 else
903 return low;
904 }
905
906 /**
Doug Felt9f7a4442010-03-01 12:45:56 -0800907 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 * closest to the specified horizontal position.
909 */
910 public int getOffsetForHorizontal(int line, float horiz) {
911 int max = getLineEnd(line) - 1;
912 int min = getLineStart(line);
913 Directions dirs = getLineDirections(line);
914
915 if (line == getLineCount() - 1)
916 max++;
917
918 int best = min;
919 float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz);
920
Doug Felt9f7a4442010-03-01 12:45:56 -0800921 for (int i = 0; i < dirs.mDirections.length; i += 2) {
922 int here = min + dirs.mDirections[i];
923 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
924 int swap = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0 ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925
926 if (there > max)
927 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 int high = there - 1 + 1, low = here + 1 - 1, guess;
929
930 while (high - low > 1) {
931 guess = (high + low) / 2;
932 int adguess = getOffsetAtStartOf(guess);
933
934 if (getPrimaryHorizontal(adguess) * swap >= horiz * swap)
935 high = guess;
936 else
937 low = guess;
938 }
939
940 if (low < here + 1)
941 low = here + 1;
942
943 if (low < there) {
944 low = getOffsetAtStartOf(low);
945
946 float dist = Math.abs(getPrimaryHorizontal(low) - horiz);
947
948 int aft = TextUtils.getOffsetAfter(mText, low);
949 if (aft < there) {
950 float other = Math.abs(getPrimaryHorizontal(aft) - horiz);
951
952 if (other < dist) {
953 dist = other;
954 low = aft;
955 }
956 }
957
958 if (dist < bestdist) {
959 bestdist = dist;
Doug Felt9f7a4442010-03-01 12:45:56 -0800960 best = low;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 }
962 }
963
964 float dist = Math.abs(getPrimaryHorizontal(here) - horiz);
965
966 if (dist < bestdist) {
967 bestdist = dist;
968 best = here;
969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
971
972 float dist = Math.abs(getPrimaryHorizontal(max) - horiz);
973
974 if (dist < bestdist) {
975 bestdist = dist;
976 best = max;
977 }
978
979 return best;
980 }
981
982 /**
983 * Return the text offset after the last character on the specified line.
984 */
985 public final int getLineEnd(int line) {
986 return getLineStart(line + 1);
987 }
988
Doug Felt9f7a4442010-03-01 12:45:56 -0800989 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 * Return the text offset after the last visible character (so whitespace
991 * is not counted) on the specified line.
992 */
993 public int getLineVisibleEnd(int line) {
994 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
995 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 CharSequence text = mText;
999 char ch;
1000 if (line == getLineCount() - 1) {
1001 return end;
1002 }
1003
1004 for (; end > start; end--) {
1005 ch = text.charAt(end - 1);
1006
1007 if (ch == '\n') {
1008 return end - 1;
1009 }
1010
1011 if (ch != ' ' && ch != '\t') {
1012 break;
1013 }
1014
1015 }
1016
1017 return end;
1018 }
1019
1020 /**
1021 * Return the vertical position of the bottom of the specified line.
1022 */
1023 public final int getLineBottom(int line) {
1024 return getLineTop(line + 1);
1025 }
1026
1027 /**
1028 * Return the vertical position of the baseline of the specified line.
1029 */
1030 public final int getLineBaseline(int line) {
1031 // getLineTop(line+1) == getLineTop(line)
1032 return getLineTop(line+1) - getLineDescent(line);
1033 }
1034
1035 /**
1036 * Get the ascent of the text on the specified line.
1037 * The return value is negative to match the Paint.ascent() convention.
1038 */
1039 public final int getLineAscent(int line) {
1040 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1041 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1042 }
1043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001045 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001049 return getOffsetToLeftRightOf(offset, false);
1050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051
Doug Felt9f7a4442010-03-01 12:45:56 -08001052 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1053 int line = getLineForOffset(caret);
1054 int lineStart = getLineStart(line);
1055 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001056 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001058 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001059 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001060 // if walking off line, look at the line we're headed to
1061 if (advance) {
1062 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001063 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001064 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001065 ++line;
1066 } else {
1067 return caret; // at very end, don't move
1068 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001069 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001070 } else {
1071 if (caret == lineStart) {
1072 if (line > 0) {
1073 lineChanged = true;
1074 --line;
1075 } else {
1076 return caret; // at very start, don't move
1077 }
1078 }
1079 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001080
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001081 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001082 lineStart = getLineStart(line);
1083 lineEnd = getLineEnd(line);
1084 int newDir = getParagraphDirection(line);
1085 if (newDir != lineDir) {
1086 // unusual case. we want to walk onto the line, but it runs
1087 // in a different direction than this one, so we fake movement
1088 // in the opposite direction.
1089 toLeft = !toLeft;
1090 lineDir = newDir;
1091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001093
Doug Felte8e45f22010-03-29 14:58:40 -07001094 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001095
Doug Felte8e45f22010-03-29 14:58:40 -07001096 TextLine tl = TextLine.obtain();
1097 // XXX: we don't care about tabs
1098 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1099 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1100 tl = TextLine.recycle(tl);
1101 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103
1104 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001105 // XXX this probably should skip local reorderings and
1106 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 if (offset == 0)
1108 return 0;
1109
1110 CharSequence text = mText;
1111 char c = text.charAt(offset);
1112
1113 if (c >= '\uDC00' && c <= '\uDFFF') {
1114 char c1 = text.charAt(offset - 1);
1115
1116 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1117 offset -= 1;
1118 }
1119
1120 if (mSpannedText) {
1121 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1122 ReplacementSpan.class);
1123
1124 for (int i = 0; i < spans.length; i++) {
1125 int start = ((Spanned) text).getSpanStart(spans[i]);
1126 int end = ((Spanned) text).getSpanEnd(spans[i]);
1127
1128 if (start < offset && end > offset)
1129 offset = start;
1130 }
1131 }
1132
1133 return offset;
1134 }
1135
1136 /**
1137 * Fills in the specified Path with a representation of a cursor
1138 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001139 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 * directionalities.
1141 */
1142 public void getCursorPath(int point, Path dest,
1143 CharSequence editingBuffer) {
1144 dest.reset();
1145
1146 int line = getLineForOffset(point);
1147 int top = getLineTop(line);
1148 int bottom = getLineTop(line+1);
1149
1150 float h1 = getPrimaryHorizontal(point) - 0.5f;
Doug Felt4e0c5e52010-03-15 16:56:02 -07001151 float h2 = isLevelBoundary(point) ?
1152 getSecondaryHorizontal(point) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153
Jeff Brown497a92c2010-09-12 17:55:08 -07001154 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1155 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1156 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 int dist = 0;
1158
1159 if (caps != 0 || fn != 0) {
1160 dist = (bottom - top) >> 2;
1161
1162 if (fn != 0)
1163 top += dist;
1164 if (caps != 0)
1165 bottom -= dist;
1166 }
1167
1168 if (h1 < 0.5f)
1169 h1 = 0.5f;
1170 if (h2 < 0.5f)
1171 h2 = 0.5f;
1172
1173 if (h1 == h2) {
1174 dest.moveTo(h1, top);
1175 dest.lineTo(h1, bottom);
1176 } else {
1177 dest.moveTo(h1, top);
1178 dest.lineTo(h1, (top + bottom) >> 1);
1179
1180 dest.moveTo(h2, (top + bottom) >> 1);
1181 dest.lineTo(h2, bottom);
1182 }
1183
1184 if (caps == 2) {
1185 dest.moveTo(h2, bottom);
1186 dest.lineTo(h2 - dist, bottom + dist);
1187 dest.lineTo(h2, bottom);
1188 dest.lineTo(h2 + dist, bottom + dist);
1189 } else if (caps == 1) {
1190 dest.moveTo(h2, bottom);
1191 dest.lineTo(h2 - dist, bottom + dist);
1192
1193 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1194 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1195
1196 dest.moveTo(h2 + dist, bottom + dist);
1197 dest.lineTo(h2, bottom);
1198 }
1199
1200 if (fn == 2) {
1201 dest.moveTo(h1, top);
1202 dest.lineTo(h1 - dist, top - dist);
1203 dest.lineTo(h1, top);
1204 dest.lineTo(h1 + dist, top - dist);
1205 } else if (fn == 1) {
1206 dest.moveTo(h1, top);
1207 dest.lineTo(h1 - dist, top - dist);
1208
1209 dest.moveTo(h1 - dist, top - dist + 0.5f);
1210 dest.lineTo(h1 + dist, top - dist + 0.5f);
1211
1212 dest.moveTo(h1 + dist, top - dist);
1213 dest.lineTo(h1, top);
1214 }
1215 }
1216
1217 private void addSelection(int line, int start, int end,
1218 int top, int bottom, Path dest) {
1219 int linestart = getLineStart(line);
1220 int lineend = getLineEnd(line);
1221 Directions dirs = getLineDirections(line);
1222
1223 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1224 lineend--;
1225
Doug Felt9f7a4442010-03-01 12:45:56 -08001226 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1227 int here = linestart + dirs.mDirections[i];
1228 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 if (there > lineend)
1231 there = lineend;
1232
1233 if (start <= there && end >= here) {
1234 int st = Math.max(start, here);
1235 int en = Math.min(end, there);
1236
1237 if (st != en) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001238 float h1 = getHorizontal(st, false, line);
1239 float h2 = getHorizontal(en, true, line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240
1241 dest.addRect(h1, top, h2, bottom, Path.Direction.CW);
1242 }
1243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
1245 }
1246
1247 /**
1248 * Fills in the specified Path with a representation of a highlight
1249 * between the specified offsets. This will often be a rectangle
1250 * or a potentially discontinuous set of rectangles. If the start
1251 * and end are the same, the returned path is empty.
1252 */
1253 public void getSelectionPath(int start, int end, Path dest) {
1254 dest.reset();
1255
1256 if (start == end)
1257 return;
1258
1259 if (end < start) {
1260 int temp = end;
1261 end = start;
1262 start = temp;
1263 }
1264
1265 int startline = getLineForOffset(start);
1266 int endline = getLineForOffset(end);
1267
1268 int top = getLineTop(startline);
1269 int bottom = getLineBottom(endline);
1270
1271 if (startline == endline) {
1272 addSelection(startline, start, end, top, bottom, dest);
1273 } else {
1274 final float width = mWidth;
1275
1276 addSelection(startline, start, getLineEnd(startline),
1277 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1280 dest.addRect(getLineLeft(startline), top,
1281 0, getLineBottom(startline), Path.Direction.CW);
1282 else
1283 dest.addRect(getLineRight(startline), top,
1284 width, getLineBottom(startline), Path.Direction.CW);
1285
1286 for (int i = startline + 1; i < endline; i++) {
1287 top = getLineTop(i);
1288 bottom = getLineBottom(i);
1289 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1290 }
1291
1292 top = getLineTop(endline);
1293 bottom = getLineBottom(endline);
1294
1295 addSelection(endline, getLineStart(endline), end,
1296 top, bottom, dest);
1297
1298 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1299 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1300 else
1301 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1302 }
1303 }
1304
1305 /**
1306 * Get the alignment of the specified paragraph, taking into account
1307 * markup attached to it.
1308 */
1309 public final Alignment getParagraphAlignment(int line) {
1310 Alignment align = mAlignment;
1311
1312 if (mSpannedText) {
1313 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001314 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 getLineEnd(line),
1316 AlignmentSpan.class);
1317
1318 int spanLength = spans.length;
1319 if (spanLength > 0) {
1320 align = spans[spanLength-1].getAlignment();
1321 }
1322 }
1323
1324 return align;
1325 }
1326
1327 /**
1328 * Get the left edge of the specified paragraph, inset by left margins.
1329 */
1330 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001332 int dir = getParagraphDirection(line);
1333 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1334 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
Doug Feltc982f602010-05-25 11:51:40 -07001336 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338
1339 /**
1340 * Get the right edge of the specified paragraph, inset by right margins.
1341 */
1342 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001344 int dir = getParagraphDirection(line);
1345 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1346 return right; // leading margin has no impact, or no styles
1347 }
1348 return right - getParagraphLeadingMargin(line);
1349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350
Doug Feltc982f602010-05-25 11:51:40 -07001351 /**
1352 * Returns the effective leading margin (unsigned) for this line,
1353 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1354 * @param line the line index
1355 * @return the leading margin of this line
1356 */
1357 private int getParagraphLeadingMargin(int line) {
1358 if (!mSpannedText) {
1359 return 0;
1360 }
1361 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001362
Doug Feltc982f602010-05-25 11:51:40 -07001363 int lineStart = getLineStart(line);
1364 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001365 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001366 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001367 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001368 LeadingMarginSpan.class);
1369 if (spans.length == 0) {
1370 return 0; // no leading margin span;
1371 }
Doug Felt0c702b82010-05-14 10:55:42 -07001372
Doug Feltc982f602010-05-25 11:51:40 -07001373 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001374
1375 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001376 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001377
Doug Feltc982f602010-05-25 11:51:40 -07001378 for (int i = 0; i < spans.length; i++) {
1379 LeadingMarginSpan span = spans[i];
1380 boolean useFirstLineMargin = isFirstParaLine;
1381 if (span instanceof LeadingMarginSpan2) {
1382 int spStart = spanned.getSpanStart(span);
1383 int spanLine = getLineForOffset(spStart);
1384 int count = ((LeadingMarginSpan2)span).getLeadingMarginLineCount();
Doug Felt0c702b82010-05-14 10:55:42 -07001385 useFirstLineMargin = line < spanLine + count;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
Doug Feltc982f602010-05-25 11:51:40 -07001387 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 }
1389
Doug Feltc982f602010-05-25 11:51:40 -07001390 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 }
1392
Doug Felte8e45f22010-03-29 14:58:40 -07001393 /* package */
1394 static float measurePara(TextPaint paint, TextPaint workPaint,
Doug Feltc982f602010-05-25 11:51:40 -07001395 CharSequence text, int start, int end) {
Doug Felte8e45f22010-03-29 14:58:40 -07001396
1397 MeasuredText mt = MeasuredText.obtain();
1398 TextLine tl = TextLine.obtain();
1399 try {
1400 mt.setPara(text, start, end, DIR_REQUEST_LTR);
1401 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001402 int dir;
1403 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001404 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001405 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001406 } else {
1407 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1408 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001409 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 }
Doug Feltc982f602010-05-25 11:51:40 -07001411 char[] chars = mt.mChars;
1412 int len = mt.mLen;
1413 boolean hasTabs = false;
1414 TabStops tabStops = null;
1415 for (int i = 0; i < len; ++i) {
1416 if (chars[i] == '\t') {
1417 hasTabs = true;
1418 if (text instanceof Spanned) {
1419 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001420 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001421 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001422 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001423 TabStopSpan.class);
1424 if (spans.length > 0) {
1425 tabStops = new TabStops(TAB_INCREMENT, spans);
1426 }
1427 }
1428 break;
1429 }
1430 }
1431 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001432 return tl.metrics(null);
1433 } finally {
1434 TextLine.recycle(tl);
1435 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 }
1438
Doug Felt71b8dd72010-02-16 17:27:09 -08001439 /**
Doug Feltc982f602010-05-25 11:51:40 -07001440 * @hide
1441 */
1442 /* package */ static class TabStops {
1443 private int[] mStops;
1444 private int mNumStops;
1445 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001446
Doug Feltc982f602010-05-25 11:51:40 -07001447 TabStops(int increment, Object[] spans) {
1448 reset(increment, spans);
1449 }
Doug Felt0c702b82010-05-14 10:55:42 -07001450
Doug Feltc982f602010-05-25 11:51:40 -07001451 void reset(int increment, Object[] spans) {
1452 this.mIncrement = increment;
1453
1454 int ns = 0;
1455 if (spans != null) {
1456 int[] stops = this.mStops;
1457 for (Object o : spans) {
1458 if (o instanceof TabStopSpan) {
1459 if (stops == null) {
1460 stops = new int[10];
1461 } else if (ns == stops.length) {
1462 int[] nstops = new int[ns * 2];
1463 for (int i = 0; i < ns; ++i) {
1464 nstops[i] = stops[i];
1465 }
1466 stops = nstops;
1467 }
1468 stops[ns++] = ((TabStopSpan) o).getTabStop();
1469 }
1470 }
1471 if (ns > 1) {
1472 Arrays.sort(stops, 0, ns);
1473 }
1474 if (stops != this.mStops) {
1475 this.mStops = stops;
1476 }
1477 }
1478 this.mNumStops = ns;
1479 }
Doug Felt0c702b82010-05-14 10:55:42 -07001480
Doug Feltc982f602010-05-25 11:51:40 -07001481 float nextTab(float h) {
1482 int ns = this.mNumStops;
1483 if (ns > 0) {
1484 int[] stops = this.mStops;
1485 for (int i = 0; i < ns; ++i) {
1486 int stop = stops[i];
1487 if (stop > h) {
1488 return stop;
1489 }
1490 }
1491 }
1492 return nextDefaultStop(h, mIncrement);
1493 }
1494
1495 public static float nextDefaultStop(float h, int inc) {
1496 return ((int) ((h + inc) / inc)) * inc;
1497 }
1498 }
Doug Felt0c702b82010-05-14 10:55:42 -07001499
Doug Feltc982f602010-05-25 11:51:40 -07001500 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001501 * Returns the position of the next tab stop after h on the line.
1502 *
1503 * @param text the text
1504 * @param start start of the line
1505 * @param end limit of the line
1506 * @param h the current horizontal offset
1507 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1508 * on the line will be used. If there are no tabs, a default offset
1509 * will be used to compute the tab stop.
1510 * @return the offset of the next tab stop.
1511 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 /* package */ static float nextTab(CharSequence text, int start, int end,
1513 float h, Object[] tabs) {
1514 float nh = Float.MAX_VALUE;
1515 boolean alltabs = false;
1516
1517 if (text instanceof Spanned) {
1518 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001519 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 alltabs = true;
1521 }
1522
1523 for (int i = 0; i < tabs.length; i++) {
1524 if (!alltabs) {
1525 if (!(tabs[i] instanceof TabStopSpan))
1526 continue;
1527 }
1528
1529 int where = ((TabStopSpan) tabs[i]).getTabStop();
1530
1531 if (where < nh && where > h)
1532 nh = where;
1533 }
1534
1535 if (nh != Float.MAX_VALUE)
1536 return nh;
1537 }
1538
1539 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1540 }
1541
1542 protected final boolean isSpanned() {
1543 return mSpannedText;
1544 }
1545
Eric Fischer74d31ef2010-08-05 15:29:36 -07001546 /**
1547 * Returns the same as <code>text.getSpans()</code>, except where
1548 * <code>start</code> and <code>end</code> are the same and are not
1549 * at the very beginning of the text, in which case an empty array
1550 * is returned instead.
1551 * <p>
1552 * This is needed because of the special case that <code>getSpans()</code>
1553 * on an empty range returns the spans adjacent to that range, which is
1554 * primarily for the sake of <code>TextWatchers</code> so they will get
1555 * notifications when text goes from empty to non-empty. But it also
1556 * has the unfortunate side effect that if the text ends with an empty
1557 * paragraph, that paragraph accidentally picks up the styles of the
1558 * preceding paragraph (even though those styles will not be picked up
1559 * by new text that is inserted into the empty paragraph).
1560 * <p>
1561 * The reason it just checks whether <code>start</code> and <code>end</code>
1562 * is the same is that the only time a line can contain 0 characters
1563 * is if it is the final paragraph of the Layout; otherwise any line will
1564 * contain at least one printing or newline character. The reason for the
1565 * additional check if <code>start</code> is greater than 0 is that
1566 * if the empty paragraph is the entire content of the buffer, paragraph
1567 * styles that are already applied to the buffer will apply to text that
1568 * is inserted into it.
1569 */
1570 /* package */ static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
1571 if (start == end && start > 0) {
1572 return (T[]) ArrayUtils.emptyArray(type);
1573 }
1574
1575 return text.getSpans(start, end, type);
1576 }
1577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 private void ellipsize(int start, int end, int line,
1579 char[] dest, int destoff) {
1580 int ellipsisCount = getEllipsisCount(line);
1581
1582 if (ellipsisCount == 0) {
1583 return;
1584 }
1585
1586 int ellipsisStart = getEllipsisStart(line);
1587 int linestart = getLineStart(line);
1588
1589 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1590 char c;
1591
1592 if (i == ellipsisStart) {
1593 c = '\u2026'; // ellipsis
1594 } else {
1595 c = '\uFEFF'; // 0-width space
1596 }
1597
1598 int a = i + linestart;
1599
1600 if (a >= start && a < end) {
1601 dest[destoff + a - start] = c;
1602 }
1603 }
1604 }
1605
1606 /**
1607 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08001608 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 */
1610 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08001611 // Directions represents directional runs within a line of text.
1612 // Runs are pairs of ints listed in visual order, starting from the
1613 // leading margin. The first int of each pair is the offset from
1614 // the first character of the line to the start of the run. The
1615 // second int represents both the length and level of the run.
1616 // The length is in the lower bits, accessed by masking with
1617 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1618 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1619 // To simply test for an RTL direction, test the bit using
1620 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621
Doug Felt9f7a4442010-03-01 12:45:56 -08001622 /* package */ int[] mDirections;
1623 /* package */ Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 mDirections = dirs;
1625 }
1626 }
1627
1628 /**
1629 * Return the offset of the first character to be ellipsized away,
1630 * relative to the start of the line. (So 0 if the beginning of the
1631 * line is ellipsized, not getLineStart().)
1632 */
1633 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07001634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 /**
1636 * Returns the number of characters to be ellipsized away, or 0 if
1637 * no ellipsis is to take place.
1638 */
1639 public abstract int getEllipsisCount(int line);
1640
1641 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1642 /* package */ CharSequence mText;
1643 /* package */ Layout mLayout;
1644 /* package */ int mWidth;
1645 /* package */ TextUtils.TruncateAt mMethod;
1646
1647 public Ellipsizer(CharSequence s) {
1648 mText = s;
1649 }
1650
1651 public char charAt(int off) {
1652 char[] buf = TextUtils.obtain(1);
1653 getChars(off, off + 1, buf, 0);
1654 char ret = buf[0];
1655
1656 TextUtils.recycle(buf);
1657 return ret;
1658 }
1659
1660 public void getChars(int start, int end, char[] dest, int destoff) {
1661 int line1 = mLayout.getLineForOffset(start);
1662 int line2 = mLayout.getLineForOffset(end);
1663
1664 TextUtils.getChars(mText, start, end, dest, destoff);
1665
1666 for (int i = line1; i <= line2; i++) {
1667 mLayout.ellipsize(start, end, i, dest, destoff);
1668 }
1669 }
1670
1671 public int length() {
1672 return mText.length();
1673 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 public CharSequence subSequence(int start, int end) {
1676 char[] s = new char[end - start];
1677 getChars(start, end, s, 0);
1678 return new String(s);
1679 }
1680
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001681 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 public String toString() {
1683 char[] s = new char[length()];
1684 getChars(0, length(), s, 0);
1685 return new String(s);
1686 }
1687
1688 }
1689
1690 /* package */ static class SpannedEllipsizer
1691 extends Ellipsizer implements Spanned {
1692 private Spanned mSpanned;
1693
1694 public SpannedEllipsizer(CharSequence display) {
1695 super(display);
1696 mSpanned = (Spanned) display;
1697 }
1698
1699 public <T> T[] getSpans(int start, int end, Class<T> type) {
1700 return mSpanned.getSpans(start, end, type);
1701 }
1702
1703 public int getSpanStart(Object tag) {
1704 return mSpanned.getSpanStart(tag);
1705 }
1706
1707 public int getSpanEnd(Object tag) {
1708 return mSpanned.getSpanEnd(tag);
1709 }
1710
1711 public int getSpanFlags(Object tag) {
1712 return mSpanned.getSpanFlags(tag);
1713 }
1714
1715 public int nextSpanTransition(int start, int limit, Class type) {
1716 return mSpanned.nextSpanTransition(start, limit, type);
1717 }
1718
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001719 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 public CharSequence subSequence(int start, int end) {
1721 char[] s = new char[end - start];
1722 getChars(start, end, s, 0);
1723
1724 SpannableString ss = new SpannableString(new String(s));
1725 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
1726 return ss;
1727 }
1728 }
1729
1730 private CharSequence mText;
1731 private TextPaint mPaint;
1732 /* package */ TextPaint mWorkPaint;
1733 private int mWidth;
1734 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
1735 private float mSpacingMult;
1736 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07001737 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 private boolean mSpannedText;
1739
1740 public static final int DIR_LEFT_TO_RIGHT = 1;
1741 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08001742
Doug Felt20178d62010-02-22 13:39:01 -08001743 /* package */ static final int DIR_REQUEST_LTR = 1;
1744 /* package */ static final int DIR_REQUEST_RTL = -1;
1745 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
1746 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747
Doug Felt9f7a4442010-03-01 12:45:56 -08001748 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
1749 /* package */ static final int RUN_LEVEL_SHIFT = 26;
1750 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
1751 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
1752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 public enum Alignment {
1754 ALIGN_NORMAL,
1755 ALIGN_OPPOSITE,
1756 ALIGN_CENTER,
1757 // XXX ALIGN_LEFT,
1758 // XXX ALIGN_RIGHT,
1759 }
1760
1761 private static final int TAB_INCREMENT = 20;
1762
1763 /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001764 new Directions(new int[] { 0, RUN_LENGTH_MASK });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08001766 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767}