blob: d82213857ac0bec4d1fe1b12cb743cf00545c9d8 [file] [log] [blame]
Gilles Debunne60e3b3f2012-03-13 11:26:05 -07001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text;
18
Raph Levien39b4db72015-03-25 13:18:20 -070019import android.annotation.IntDef;
The Android Open Source Project10592532009-03-18 17:39:46 -070020import android.emoji.EmojiFactory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.graphics.Canvas;
22import android.graphics.Paint;
Doug Felt9f7a4442010-03-01 12:45:56 -080023import android.graphics.Path;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.text.method.TextKeyListener;
Doug Felt9f7a4442010-03-01 12:45:56 -080026import android.text.style.AlignmentSpan;
27import android.text.style.LeadingMarginSpan;
Gilles Debunne162bf0f2010-11-16 16:23:53 -080028import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
Doug Felt9f7a4442010-03-01 12:45:56 -080029import android.text.style.LineBackgroundSpan;
30import android.text.style.ParagraphStyle;
31import android.text.style.ReplacementSpan;
32import android.text.style.TabStopSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Doug Feltcb3791202011-07-07 11:57:48 -070034import com.android.internal.util.ArrayUtils;
Adam Lesinski776abc22014-03-07 11:30:59 -050035import com.android.internal.util.GrowingArrayUtils;
Doug Feltcb3791202011-07-07 11:57:48 -070036
Raph Levien39b4db72015-03-25 13:18:20 -070037import java.lang.annotation.Retention;
38import java.lang.annotation.RetentionPolicy;
Doug Feltc982f602010-05-25 11:51:40 -070039import java.util.Arrays;
Doug Felt9f7a4442010-03-01 12:45:56 -080040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/**
Doug Felt9f7a4442010-03-01 12:45:56 -080042 * A base class that manages text layout in visual elements on
43 * the screen.
44 * <p>For text that will be edited, use a {@link DynamicLayout},
45 * which will be updated as the text changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 * For text that will not change, use a {@link StaticLayout}.
47 */
48public abstract class Layout {
Raph Levien39b4db72015-03-25 13:18:20 -070049 /** @hide */
50 @IntDef({BREAK_STRATEGY_SIMPLE, BREAK_STRATEGY_HIGH_QUALITY, BREAK_STRATEGY_BALANCED})
51 @Retention(RetentionPolicy.SOURCE)
52 public @interface BreakStrategy {}
53
54 /**
55 * Value for break strategy indicating simple line breaking. Automatic hyphens are not added
56 * (though soft hyphens are respected), and modifying text generally doesn't affect the layout
57 * before it (which yields a more consistent user experience when editing), but layout may not
58 * be the highest quality.
59 */
60 public static final int BREAK_STRATEGY_SIMPLE = 0;
61
62 /**
63 * Value for break strategy indicating high quality line breaking, including automatic
64 * hyphenation and doing whole-paragraph optimization of line breaks.
65 */
66 public static final int BREAK_STRATEGY_HIGH_QUALITY = 1;
67
68 /**
69 * Value for break strategy indicating balanced line breaking. The breaks are chosen to
70 * make all lines as close to the same length as possible, including automatic hyphenation.
71 */
72 public static final int BREAK_STRATEGY_BALANCED = 2;
73
Roozbeh Pournader95c7a132015-05-12 12:01:06 -070074 /** @hide */
75 @IntDef({HYPHENATION_FREQUENCY_NORMAL, HYPHENATION_FREQUENCY_FULL,
76 HYPHENATION_FREQUENCY_NONE})
77 @Retention(RetentionPolicy.SOURCE)
78 public @interface HyphenationFrequency {}
79
80 /**
81 * Value for hyphenation frequency indicating no automatic hyphenation. Useful
82 * for backward compatibility, and for cases where the automatic hyphenation algorithm results
83 * in incorrect hyphenation. Mid-word breaks may still happen when a word is wider than the
84 * layout and there is otherwise no valid break. Soft hyphens are ignored and will not be used
85 * as suggestions for potential line breaks.
86 */
87 public static final int HYPHENATION_FREQUENCY_NONE = 0;
88
89 /**
90 * Value for hyphenation frequency indicating a light amount of automatic hyphenation, which
91 * is a conservative default. Useful for informal cases, such as short sentences or chat
92 * messages.
93 */
94 public static final int HYPHENATION_FREQUENCY_NORMAL = 1;
95
96 /**
97 * Value for hyphenation frequency indicating the full amount of automatic hyphenation, typical
98 * in typography. Useful for running text and where it's important to put the maximum amount of
99 * text in a screen with limited space.
100 */
101 public static final int HYPHENATION_FREQUENCY_FULL = 2;
102
Doug Felt71b8dd72010-02-16 17:27:09 -0800103 private static final ParagraphStyle[] NO_PARA_SPANS =
104 ArrayUtils.emptyArray(ParagraphStyle.class);
Dave Bort76c02262009-04-13 17:17:21 -0700105
Gilles Debunneeca5b732012-04-25 18:48:42 -0700106 /* package */ static final EmojiFactory EMOJI_FACTORY = EmojiFactory.newAvailableInstance();
The Android Open Source Project10592532009-03-18 17:39:46 -0700107 /* package */ static final int MIN_EMOJI, MAX_EMOJI;
108
109 static {
110 if (EMOJI_FACTORY != null) {
111 MIN_EMOJI = EMOJI_FACTORY.getMinimumAndroidPua();
112 MAX_EMOJI = EMOJI_FACTORY.getMaximumAndroidPua();
113 } else {
114 MIN_EMOJI = -1;
115 MAX_EMOJI = -1;
116 }
Doug Felte8e45f22010-03-29 14:58:40 -0700117 }
Eric Fischerc2d54f42009-03-27 15:52:38 -0700118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800120 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 * specified text with one line per paragraph.
122 */
123 public static float getDesiredWidth(CharSequence source,
124 TextPaint paint) {
125 return getDesiredWidth(source, 0, source.length(), paint);
126 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800129 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * specified text slice with one line per paragraph.
131 */
132 public static float getDesiredWidth(CharSequence source,
133 int start, int end,
134 TextPaint paint) {
135 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
137 int next;
138 for (int i = start; i <= end; i = next) {
139 next = TextUtils.indexOf(source, '\n', i, end);
140
141 if (next < 0)
142 next = end;
143
Doug Felt71b8dd72010-02-16 17:27:09 -0800144 // note, omits trailing paragraph char
Gilles Debunne6c488de2012-03-01 16:20:35 -0800145 float w = measurePara(paint, source, i, next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
147 if (w > need)
148 need = w;
149
150 next++;
151 }
152
153 return need;
154 }
155
156 /**
157 * Subclasses of Layout use this constructor to set the display text,
158 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800159 * @param text the text to render
160 * @param paint the default paint for the layout. Styles can override
161 * various attributes of the paint.
162 * @param width the wrapping width for the text.
163 * @param align whether to left, right, or center the text. Styles can
164 * override the alignment.
165 * @param spacingMult factor by which to scale the font size to get the
166 * default line spacing
167 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 */
169 protected Layout(CharSequence text, TextPaint paint,
170 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800171 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700172 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
173 spacingMult, spacingAdd);
174 }
175
176 /**
177 * Subclasses of Layout use this constructor to set the display text,
178 * width, and other standard properties.
179 * @param text the text to render
180 * @param paint the default paint for the layout. Styles can override
181 * various attributes of the paint.
182 * @param width the wrapping width for the text.
183 * @param align whether to left, right, or center the text. Styles can
184 * override the alignment.
185 * @param spacingMult factor by which to scale the font size to get the
186 * default line spacing
187 * @param spacingAdd amount to add to the default line spacing
188 *
189 * @hide
190 */
191 protected Layout(CharSequence text, TextPaint paint,
192 int width, Alignment align, TextDirectionHeuristic textDir,
193 float spacingMult, float spacingAdd) {
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 if (width < 0)
196 throw new IllegalArgumentException("Layout: " + width + " < 0");
197
Doug Felte8e45f22010-03-29 14:58:40 -0700198 // Ensure paint doesn't have baselineShift set.
199 // While normally we don't modify the paint the user passed in,
200 // we were already doing this in Styled.drawUniformRun with both
201 // baselineShift and bgColor. We probably should reevaluate bgColor.
202 if (paint != null) {
203 paint.bgColor = 0;
204 paint.baselineShift = 0;
205 }
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 mText = text;
208 mPaint = paint;
209 mWorkPaint = new TextPaint();
210 mWidth = width;
211 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800212 mSpacingMult = spacingMult;
213 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700215 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 }
217
218 /**
219 * Replace constructor properties of this Layout with new ones. Be careful.
220 */
221 /* package */ void replaceWith(CharSequence text, TextPaint paint,
222 int width, Alignment align,
223 float spacingmult, float spacingadd) {
224 if (width < 0) {
225 throw new IllegalArgumentException("Layout: " + width + " < 0");
226 }
227
228 mText = text;
229 mPaint = paint;
230 mWidth = width;
231 mAlignment = align;
232 mSpacingMult = spacingmult;
233 mSpacingAdd = spacingadd;
234 mSpannedText = text instanceof Spanned;
235 }
236
237 /**
238 * Draw this Layout on the specified Canvas.
239 */
240 public void draw(Canvas c) {
241 draw(c, null, null, 0);
242 }
243
244 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800245 * Draw this Layout on the specified canvas, with the highlight path drawn
246 * between the background and the text.
247 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800248 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800249 * @param highlight the path of the highlight or cursor; can be null
250 * @param highlightPaint the paint for the highlight
251 * @param cursorOffsetVertical the amount to temporarily translate the
252 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800254 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
255 int cursorOffsetVertical) {
256 final long lineRange = getLineRangeForDraw(canvas);
257 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
258 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
259 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
Gilles Debunne6c488de2012-03-01 16:20:35 -0800261 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
262 firstLine, lastLine);
263 drawText(canvas, firstLine, lastLine);
264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265
Gilles Debunne6c488de2012-03-01 16:20:35 -0800266 /**
267 * @hide
268 */
269 public void drawText(Canvas canvas, int firstLine, int lastLine) {
270 int previousLineBottom = getLineTop(firstLine);
271 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800272 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700273 int spanEnd = 0;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800274 TextPaint paint = mPaint;
275 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700277 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700278 TabStops tabStops = null;
279 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800280
Doug Felte8e45f22010-03-29 14:58:40 -0700281 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700282
Gilles Debunne6c488de2012-03-01 16:20:35 -0800283 // Draw the lines, one at a time.
284 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700285 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700287 previousLineEnd = getLineStart(lineNum + 1);
288 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
290 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700291 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700293 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294
Raph Levien26d443a2015-03-30 14:18:32 -0700295 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700296 int left = 0;
297 int right = mWidth;
298
Gilles Debunne6c488de2012-03-01 16:20:35 -0800299 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700300 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800301 int textLength = buf.length();
302 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700303
Doug Feltc982f602010-05-25 11:51:40 -0700304 // New batch of paragraph styles, collect into spans array.
305 // Compute the alignment, last alignment style wins.
306 // Reset tabStops, we'll rebuild if we encounter a line with
307 // tabs.
308 // We expect paragraph spans to be relatively infrequent, use
309 // spanEnd so that we can check less frequently. Since
310 // paragraph styles ought to apply to entire paragraphs, we can
311 // just collect the ones present at the start of the paragraph.
312 // If spanEnd is before the end of the paragraph, that's not
313 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700314 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700315 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700317 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800318
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700319 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800320 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700322 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 break;
324 }
325 }
Doug Felt0c702b82010-05-14 10:55:42 -0700326
Doug Feltc982f602010-05-25 11:51:40 -0700327 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800329
Doug Feltc982f602010-05-25 11:51:40 -0700330 // Draw all leading margin spans. Adjust left or right according
331 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700333 boolean useFirstLineMargin = isFirstParaLine;
334 for (int n = 0; n < length; n++) {
335 if (spans[n] instanceof LeadingMarginSpan2) {
336 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
337 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
338 // if there is more than one LeadingMarginSpan2, use
339 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700340 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700341 useFirstLineMargin = true;
342 break;
343 }
344 }
345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 for (int n = 0; n < length; n++) {
347 if (spans[n] instanceof LeadingMarginSpan) {
348 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800350 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800352 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700353 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800355 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800357 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700358 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
360 }
361 }
362 }
363
Raph Levien26d443a2015-03-30 14:18:32 -0700364 boolean hasTabOrEmoji = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700365 // Can't tell if we have tabs for sure, currently
366 if (hasTabOrEmoji && !tabStopsIsInitialized) {
367 if (tabStops == null) {
368 tabStops = new TabStops(TAB_INCREMENT, spans);
369 } else {
370 tabStops.reset(TAB_INCREMENT, spans);
371 }
372 tabStopsIsInitialized = true;
373 }
374
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700375 // Determine whether the line aligns to normal, opposite, or center.
376 Alignment align = paraAlign;
377 if (align == Alignment.ALIGN_LEFT) {
378 align = (dir == DIR_LEFT_TO_RIGHT) ?
379 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
380 } else if (align == Alignment.ALIGN_RIGHT) {
381 align = (dir == DIR_LEFT_TO_RIGHT) ?
382 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
383 }
384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 int x;
386 if (align == Alignment.ALIGN_NORMAL) {
387 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700388 x = left + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700390 x = right + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
392 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700393 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700395 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700396 x = right - max + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700398 x = left - max + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
Doug Feltc982f602010-05-25 11:51:40 -0700400 } else { // Alignment.ALIGN_CENTER
401 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700402 x = ((right + left - max) >> 1) +
403 getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 }
405 }
406
Raph Levien26d443a2015-03-30 14:18:32 -0700407 paint.setHyphenEdit(getHyphen(lineNum));
408 Directions directions = getLineDirections(lineNum);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800409 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTabOrEmoji) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800410 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800411 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 } else {
Doug Feltc982f602010-05-25 11:51:40 -0700413 tl.set(paint, buf, start, end, dir, directions, hasTabOrEmoji, tabStops);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800414 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
Raph Levien9a174dd2015-04-08 13:35:03 -0700416 paint.setHyphenEdit(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
Doug Feltc982f602010-05-25 11:51:40 -0700418
Doug Felte8e45f22010-03-29 14:58:40 -0700419 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 }
421
422 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800423 * @hide
424 */
425 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
426 int cursorOffsetVertical, int firstLine, int lastLine) {
427 // First, draw LineBackgroundSpans.
428 // LineBackgroundSpans know nothing about the alignment, margins, or
429 // direction of the layout or line. XXX: Should they?
430 // They are evaluated at each line.
431 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700432 if (mLineBackgroundSpans == null) {
433 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700434 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800435
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700436 Spanned buffer = (Spanned) mText;
437 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700438 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800439
Gilles Debunneeca5b732012-04-25 18:48:42 -0700440 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700441 int previousLineBottom = getLineTop(firstLine);
442 int previousLineEnd = getLineStart(firstLine);
443 ParagraphStyle[] spans = NO_PARA_SPANS;
444 int spansLength = 0;
445 TextPaint paint = mPaint;
446 int spanEnd = 0;
447 final int width = mWidth;
448 for (int i = firstLine; i <= lastLine; i++) {
449 int start = previousLineEnd;
450 int end = getLineStart(i + 1);
451 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800452
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700453 int ltop = previousLineBottom;
454 int lbottom = getLineTop(i + 1);
455 previousLineBottom = lbottom;
456 int lbaseline = lbottom - getLineDescent(i);
457
458 if (start >= spanEnd) {
459 // These should be infrequent, so we'll use this so that
460 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700461 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700462 // All LineBackgroundSpans on a line contribute to its background.
463 spansLength = 0;
464 // Duplication of the logic of getParagraphSpans
465 if (start != end || start == 0) {
466 // Equivalent to a getSpans(start, end), but filling the 'spans' local
467 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700468 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
469 // equal test is valid since both intervals are not empty by
470 // construction
471 if (mLineBackgroundSpans.spanStarts[j] >= end ||
472 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500473 spans = GrowingArrayUtils.append(
474 spans, spansLength, mLineBackgroundSpans.spans[j]);
475 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700476 }
477 }
478 }
479
480 for (int n = 0; n < spansLength; n++) {
481 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
482 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
483 ltop, lbaseline, lbottom,
484 buffer, start, end, i);
485 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800486 }
487 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700488 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800489 }
490
491 // There can be a highlight even without spans if we are drawing
492 // a non-spanned transformation of a spanned editing buffer.
493 if (highlight != null) {
494 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
495 canvas.drawPath(highlight, highlightPaint);
496 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
497 }
498 }
499
500 /**
501 * @param canvas
502 * @return The range of lines that need to be drawn, possibly empty.
503 * @hide
504 */
505 public long getLineRangeForDraw(Canvas canvas) {
506 int dtop, dbottom;
507
508 synchronized (sTempRect) {
509 if (!canvas.getClipBounds(sTempRect)) {
510 // Negative range end used as a special flag
511 return TextUtils.packRangeInLong(0, -1);
512 }
513
514 dtop = sTempRect.top;
515 dbottom = sTempRect.bottom;
516 }
517
518 final int top = Math.max(dtop, 0);
519 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
520
Gilles Debunne2fba3382012-06-11 17:46:24 -0700521 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800522 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
523 }
524
525 /**
Doug Feltc982f602010-05-25 11:51:40 -0700526 * Return the start position of the line, given the left and right bounds
527 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700528 *
Doug Feltc982f602010-05-25 11:51:40 -0700529 * @param line the line index
530 * @param left the left bounds (0, or leading margin if ltr para)
531 * @param right the right bounds (width, minus leading margin if rtl para)
532 * @return the start position of the line (to right of line if rtl para)
533 */
534 private int getLineStartPos(int line, int left, int right) {
535 // Adjust the point at which to start rendering depending on the
536 // alignment of the paragraph.
537 Alignment align = getParagraphAlignment(line);
538 int dir = getParagraphDirection(line);
539
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700540 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700541 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
542 } else if (align == Alignment.ALIGN_RIGHT) {
543 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
544 }
545
546 int x;
547 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700548 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700549 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700550 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700551 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700552 }
553 } else {
554 TabStops tabStops = null;
555 if (mSpannedText && getLineContainsTab(line)) {
556 Spanned spanned = (Spanned) mText;
557 int start = getLineStart(line);
558 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
559 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800560 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
561 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700562 if (tabSpans.length > 0) {
563 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
564 }
565 }
566 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700567 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700568 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700569 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700570 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700571 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700572 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700573 }
574 } else { // Alignment.ALIGN_CENTER
575 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700576 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700577 }
578 }
579 return x;
580 }
581
582 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 * Return the text that is displayed by this Layout.
584 */
585 public final CharSequence getText() {
586 return mText;
587 }
588
589 /**
590 * Return the base Paint properties for this layout.
591 * Do NOT change the paint, which may result in funny
592 * drawing for this layout.
593 */
594 public final TextPaint getPaint() {
595 return mPaint;
596 }
597
598 /**
599 * Return the width of this layout.
600 */
601 public final int getWidth() {
602 return mWidth;
603 }
604
605 /**
606 * Return the width to which this Layout is ellipsizing, or
607 * {@link #getWidth} if it is not doing anything special.
608 */
609 public int getEllipsizedWidth() {
610 return mWidth;
611 }
612
613 /**
614 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800615 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 * it does not cause the text to reflow to use the full new width.
617 */
618 public final void increaseWidthTo(int wid) {
619 if (wid < mWidth) {
620 throw new RuntimeException("attempted to reduce Layout width");
621 }
622
623 mWidth = wid;
624 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 /**
627 * Return the total height of this layout.
628 */
629 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800630 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632
633 /**
634 * Return the base alignment of this layout.
635 */
636 public final Alignment getAlignment() {
637 return mAlignment;
638 }
639
640 /**
641 * Return what the text height is multiplied by to get the line height.
642 */
643 public final float getSpacingMultiplier() {
644 return mSpacingMult;
645 }
646
647 /**
648 * Return the number of units of leading that are added to each line.
649 */
650 public final float getSpacingAdd() {
651 return mSpacingAdd;
652 }
653
654 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700655 * Return the heuristic used to determine paragraph text direction.
656 * @hide
657 */
658 public final TextDirectionHeuristic getTextDirectionHeuristic() {
659 return mTextDir;
660 }
661
662 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 * Return the number of lines of text in this layout.
664 */
665 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 /**
668 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
669 * If bounds is not null, return the top, left, right, bottom extents
670 * of the specified line in it.
671 * @param line which line to examine (0..getLineCount() - 1)
672 * @param bounds Optional. If not null, it returns the extent of the line
673 * @return the Y-coordinate of the baseline
674 */
675 public int getLineBounds(int line, Rect bounds) {
676 if (bounds != null) {
677 bounds.left = 0; // ???
678 bounds.top = getLineTop(line);
679 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800680 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682 return getLineBaseline(line);
683 }
684
685 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800686 * Return the vertical position of the top of the specified line
687 * (0&hellip;getLineCount()).
688 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 * bottom of the last line.
690 */
691 public abstract int getLineTop(int line);
692
693 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800694 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 */
696 public abstract int getLineDescent(int line);
697
698 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800699 * Return the text offset of the beginning of the specified line (
700 * 0&hellip;getLineCount()). If the specified line is equal to the line
701 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 */
703 public abstract int getLineStart(int line);
704
705 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800706 * Returns the primary directionality of the paragraph containing the
707 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
708 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 */
710 public abstract int getParagraphDirection(int line);
711
712 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700713 * Returns whether the specified line contains one or more
714 * characters that need to be handled specially, like tabs
715 * or emoji.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 */
717 public abstract boolean getLineContainsTab(int line);
718
719 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800720 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 * The array alternates counts of characters in left-to-right
722 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800723 *
724 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 */
726 public abstract Directions getLineDirections(int line);
727
728 /**
729 * Returns the (negative) number of extra pixels of ascent padding in the
730 * top line of the Layout.
731 */
732 public abstract int getTopPadding();
733
734 /**
735 * Returns the number of extra pixels of descent padding in the
736 * bottom line of the Layout.
737 */
738 public abstract int getBottomPadding();
739
Raph Levien26d443a2015-03-30 14:18:32 -0700740 /**
741 * Returns the hyphen edit for a line.
742 *
743 * @hide
744 */
745 public int getHyphen(int line) {
746 return 0;
747 }
748
Raph Levien2ea52902015-07-01 14:39:31 -0700749 /**
750 * Returns the left indent for a line.
751 *
752 * @hide
753 */
754 public int getIndentAdjust(int line, Alignment alignment) {
755 return 0;
756 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700757
758 /**
759 * Returns true if the character at offset and the preceding character
760 * are at different run levels (and thus there's a split caret).
761 * @param offset the offset
762 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800763 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700764 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800765 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800766 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700767 Directions dirs = getLineDirections(line);
768 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
769 return false;
770 }
771
772 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800773 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700774 int lineEnd = getLineEnd(line);
775 if (offset == lineStart || offset == lineEnd) {
776 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
777 int runIndex = offset == lineStart ? 0 : runs.length - 2;
778 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
779 }
780
781 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800782 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700783 if (offset == runs[i]) {
784 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800785 }
786 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700787 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800788 }
789
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700790 /**
791 * Returns true if the character at offset is right to left (RTL).
792 * @param offset the offset
793 * @return true if the character is RTL, false if it is LTR
794 */
795 public boolean isRtlCharAt(int offset) {
796 int line = getLineForOffset(offset);
797 Directions dirs = getLineDirections(line);
798 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
799 return false;
800 }
801 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
802 return true;
803 }
804 int[] runs = dirs.mDirections;
805 int lineStart = getLineStart(line);
806 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700807 int start = lineStart + runs[i];
808 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
809 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700810 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
811 return ((level & 1) != 0);
812 }
813 }
814 // Should happen only if the offset is "out of bounds"
815 return false;
816 }
817
Doug Felt9f7a4442010-03-01 12:45:56 -0800818 private boolean primaryIsTrailingPrevious(int offset) {
819 int line = getLineForOffset(offset);
820 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700821 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800822 int[] runs = getLineDirections(line).mDirections;
823
824 int levelAt = -1;
825 for (int i = 0; i < runs.length; i += 2) {
826 int start = lineStart + runs[i];
827 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
828 if (limit > lineEnd) {
829 limit = lineEnd;
830 }
831 if (offset >= start && offset < limit) {
832 if (offset > start) {
833 // Previous character is at same level, so don't use trailing.
834 return false;
835 }
836 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
837 break;
838 }
839 }
840 if (levelAt == -1) {
841 // Offset was limit of line.
842 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
843 }
844
845 // At level boundary, check previous level.
846 int levelBefore = -1;
847 if (offset == lineStart) {
848 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
849 } else {
850 offset -= 1;
851 for (int i = 0; i < runs.length; i += 2) {
852 int start = lineStart + runs[i];
853 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
854 if (limit > lineEnd) {
855 limit = lineEnd;
856 }
857 if (offset >= start && offset < limit) {
858 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
859 break;
860 }
861 }
862 }
863
864 return levelBefore < levelAt;
865 }
866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 /**
868 * Get the primary horizontal position for the specified text offset.
869 * This is the location where a new character would be inserted in
870 * the paragraph's primary direction.
871 */
872 public float getPrimaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800873 return getPrimaryHorizontal(offset, false /* not clamped */);
874 }
875
876 /**
877 * Get the primary horizontal position for the specified text offset, but
878 * optionally clamp it so that it doesn't exceed the width of the layout.
879 * @hide
880 */
881 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800882 boolean trailing = primaryIsTrailingPrevious(offset);
Raph Levienafe8e9b2012-12-19 16:09:32 -0800883 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 }
885
886 /**
887 * Get the secondary horizontal position for the specified text offset.
888 * This is the location where a new character would be inserted in
889 * the direction other than the paragraph's primary direction.
890 */
891 public float getSecondaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -0800892 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894
Raph Levienafe8e9b2012-12-19 16:09:32 -0800895 /**
896 * Get the secondary horizontal position for the specified text offset, but
897 * optionally clamp it so that it doesn't exceed the width of the layout.
898 * @hide
899 */
900 public float getSecondaryHorizontal(int offset, boolean clamped) {
901 boolean trailing = primaryIsTrailingPrevious(offset);
902 return getHorizontal(offset, !trailing, clamped);
903 }
904
905 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 int line = getLineForOffset(offset);
907
Raph Levienafe8e9b2012-12-19 16:09:32 -0800908 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 }
910
Raph Levienafe8e9b2012-12-19 16:09:32 -0800911 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -0700913 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 int dir = getParagraphDirection(line);
Doug Feltc982f602010-05-25 11:51:40 -0700915 boolean hasTabOrEmoji = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 Directions directions = getLineDirections(line);
917
Doug Feltc982f602010-05-25 11:51:40 -0700918 TabStops tabStops = null;
919 if (hasTabOrEmoji && mText instanceof Spanned) {
920 // Just checking this line should be good enough, tabs should be
921 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -0700922 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700923 if (tabs.length > 0) {
924 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 }
927
Doug Felte8e45f22010-03-29 14:58:40 -0700928 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700929 tl.set(mPaint, mText, start, end, dir, directions, hasTabOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -0700930 float wid = tl.measure(offset - start, trailing, null);
931 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932
Raph Levienafe8e9b2012-12-19 16:09:32 -0800933 if (clamped && wid > mWidth) {
934 wid = mWidth;
935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 int left = getParagraphLeft(line);
937 int right = getParagraphRight(line);
938
Doug Feltc982f602010-05-25 11:51:40 -0700939 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941
942 /**
943 * Get the leftmost position that should be exposed for horizontal
944 * scrolling on the specified line.
945 */
946 public float getLineLeft(int line) {
947 int dir = getParagraphDirection(line);
948 Alignment align = getParagraphAlignment(line);
949
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700950 if (align == Alignment.ALIGN_LEFT) {
951 return 0;
952 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 if (dir == DIR_RIGHT_TO_LEFT)
954 return getParagraphRight(line) - getLineMax(line);
955 else
956 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700957 } else if (align == Alignment.ALIGN_RIGHT) {
958 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 } else if (align == Alignment.ALIGN_OPPOSITE) {
960 if (dir == DIR_RIGHT_TO_LEFT)
961 return 0;
962 else
963 return mWidth - getLineMax(line);
964 } else { /* align == Alignment.ALIGN_CENTER */
965 int left = getParagraphLeft(line);
966 int right = getParagraphRight(line);
967 int max = ((int) getLineMax(line)) & ~1;
968
969 return left + ((right - left) - max) / 2;
970 }
971 }
972
973 /**
974 * Get the rightmost position that should be exposed for horizontal
975 * scrolling on the specified line.
976 */
977 public float getLineRight(int line) {
978 int dir = getParagraphDirection(line);
979 Alignment align = getParagraphAlignment(line);
980
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700981 if (align == Alignment.ALIGN_LEFT) {
982 return getParagraphLeft(line) + getLineMax(line);
983 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 if (dir == DIR_RIGHT_TO_LEFT)
985 return mWidth;
986 else
987 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700988 } else if (align == Alignment.ALIGN_RIGHT) {
989 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 } else if (align == Alignment.ALIGN_OPPOSITE) {
991 if (dir == DIR_RIGHT_TO_LEFT)
992 return getLineMax(line);
993 else
994 return mWidth;
995 } else { /* align == Alignment.ALIGN_CENTER */
996 int left = getParagraphLeft(line);
997 int right = getParagraphRight(line);
998 int max = ((int) getLineMax(line)) & ~1;
999
1000 return right - ((right - left) - max) / 2;
1001 }
1002 }
1003
1004 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001005 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001006 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 */
1008 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001009 float margin = getParagraphLeadingMargin(line);
1010 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001011 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013
1014 /**
Doug Feltc982f602010-05-25 11:51:40 -07001015 * Gets the unsigned horizontal extent of the specified line, including
1016 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 */
1018 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001019 float margin = getParagraphLeadingMargin(line);
1020 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001021 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023
Doug Feltc982f602010-05-25 11:51:40 -07001024 /**
1025 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1026 * tab stops instead of using the ones passed in.
1027 * @param line the index of the line
1028 * @param full whether to include trailing whitespace
1029 * @return the extent of the line
1030 */
1031 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001033 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001034
1035 boolean hasTabsOrEmoji = getLineContainsTab(line);
1036 TabStops tabStops = null;
1037 if (hasTabsOrEmoji && mText instanceof Spanned) {
1038 // Just checking this line should be good enough, tabs should be
1039 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001040 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001041 if (tabs.length > 0) {
1042 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1043 }
1044 }
Doug Felte8e45f22010-03-29 14:58:40 -07001045 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001046 // Returned directions can actually be null
1047 if (directions == null) {
1048 return 0f;
1049 }
Doug Feltc982f602010-05-25 11:51:40 -07001050 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051
Doug Felte8e45f22010-03-29 14:58:40 -07001052 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -07001053 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
1054 float width = tl.metrics(null);
1055 TextLine.recycle(tl);
1056 return width;
1057 }
1058
1059 /**
1060 * Returns the signed horizontal extent of the specified line, excluding
1061 * leading margin. If full is false, excludes trailing whitespace.
1062 * @param line the index of the line
1063 * @param tabStops the tab stops, can be null if we know they're not used.
1064 * @param full whether to include trailing whitespace
1065 * @return the extent of the text on this line
1066 */
1067 private float getLineExtent(int line, TabStops tabStops, boolean full) {
1068 int start = getLineStart(line);
1069 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
1070 boolean hasTabsOrEmoji = getLineContainsTab(line);
1071 Directions directions = getLineDirections(line);
1072 int dir = getParagraphDirection(line);
1073
1074 TextLine tl = TextLine.obtain();
1075 tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001076 float width = tl.metrics(null);
1077 TextLine.recycle(tl);
1078 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080
1081 /**
1082 * Get the line number corresponding to the specified vertical position.
1083 * If you ask for a position above 0, you get 0; if you ask for a position
1084 * below the bottom of the text, you get the last line.
1085 */
1086 // FIXME: It may be faster to do a linear search for layouts without many lines.
1087 public int getLineForVertical(int vertical) {
1088 int high = getLineCount(), low = -1, guess;
1089
1090 while (high - low > 1) {
1091 guess = (high + low) / 2;
1092
1093 if (getLineTop(guess) > vertical)
1094 high = guess;
1095 else
1096 low = guess;
1097 }
1098
1099 if (low < 0)
1100 return 0;
1101 else
1102 return low;
1103 }
1104
1105 /**
1106 * Get the line number on which the specified text offset appears.
1107 * If you ask for a position before 0, you get 0; if you ask for a position
1108 * beyond the end of the text, you get the last line.
1109 */
1110 public int getLineForOffset(int offset) {
1111 int high = getLineCount(), low = -1, guess;
1112
1113 while (high - low > 1) {
1114 guess = (high + low) / 2;
1115
1116 if (getLineStart(guess) > offset)
1117 high = guess;
1118 else
1119 low = guess;
1120 }
1121
1122 if (low < 0)
1123 return 0;
1124 else
1125 return low;
1126 }
1127
1128 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001129 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 * closest to the specified horizontal position.
1131 */
1132 public int getOffsetForHorizontal(int line, float horiz) {
Raph Levienedb27f12015-06-01 14:34:47 -07001133 // TODO: use Paint.getOffsetForAdvance to avoid binary search
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 int max = getLineEnd(line) - 1;
1135 int min = getLineStart(line);
1136 Directions dirs = getLineDirections(line);
1137
1138 if (line == getLineCount() - 1)
1139 max++;
1140
1141 int best = min;
1142 float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz);
1143
Doug Felt9f7a4442010-03-01 12:45:56 -08001144 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1145 int here = min + dirs.mDirections[i];
1146 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1147 int swap = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0 ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148
1149 if (there > max)
1150 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 int high = there - 1 + 1, low = here + 1 - 1, guess;
1152
1153 while (high - low > 1) {
1154 guess = (high + low) / 2;
1155 int adguess = getOffsetAtStartOf(guess);
1156
1157 if (getPrimaryHorizontal(adguess) * swap >= horiz * swap)
1158 high = guess;
1159 else
1160 low = guess;
1161 }
1162
1163 if (low < here + 1)
1164 low = here + 1;
1165
1166 if (low < there) {
1167 low = getOffsetAtStartOf(low);
1168
1169 float dist = Math.abs(getPrimaryHorizontal(low) - horiz);
1170
1171 int aft = TextUtils.getOffsetAfter(mText, low);
1172 if (aft < there) {
1173 float other = Math.abs(getPrimaryHorizontal(aft) - horiz);
1174
1175 if (other < dist) {
1176 dist = other;
1177 low = aft;
1178 }
1179 }
1180
1181 if (dist < bestdist) {
1182 bestdist = dist;
Doug Felt9f7a4442010-03-01 12:45:56 -08001183 best = low;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
1185 }
1186
1187 float dist = Math.abs(getPrimaryHorizontal(here) - horiz);
1188
1189 if (dist < bestdist) {
1190 bestdist = dist;
1191 best = here;
1192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
1194
1195 float dist = Math.abs(getPrimaryHorizontal(max) - horiz);
1196
Raph Levien373b7a82013-09-20 15:11:52 -07001197 if (dist <= bestdist) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 bestdist = dist;
1199 best = max;
1200 }
1201
1202 return best;
1203 }
1204
1205 /**
1206 * Return the text offset after the last character on the specified line.
1207 */
1208 public final int getLineEnd(int line) {
1209 return getLineStart(line + 1);
1210 }
1211
Doug Felt9f7a4442010-03-01 12:45:56 -08001212 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 * Return the text offset after the last visible character (so whitespace
1214 * is not counted) on the specified line.
1215 */
1216 public int getLineVisibleEnd(int line) {
1217 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1218 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 CharSequence text = mText;
1222 char ch;
1223 if (line == getLineCount() - 1) {
1224 return end;
1225 }
1226
1227 for (; end > start; end--) {
1228 ch = text.charAt(end - 1);
1229
1230 if (ch == '\n') {
1231 return end - 1;
1232 }
1233
Raph Levienc94f7422015-03-06 19:19:48 -08001234 // Note: keep this in sync with Minikin LineBreaker::isLineEndSpace()
1235 if (!(ch == ' ' || ch == '\t' || ch == 0x1680 ||
1236 (0x2000 <= ch && ch <= 0x200A && ch != 0x2007) ||
1237 ch == 0x205F || ch == 0x3000)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 break;
1239 }
1240
1241 }
1242
1243 return end;
1244 }
1245
1246 /**
1247 * Return the vertical position of the bottom of the specified line.
1248 */
1249 public final int getLineBottom(int line) {
1250 return getLineTop(line + 1);
1251 }
1252
1253 /**
1254 * Return the vertical position of the baseline of the specified line.
1255 */
1256 public final int getLineBaseline(int line) {
1257 // getLineTop(line+1) == getLineTop(line)
1258 return getLineTop(line+1) - getLineDescent(line);
1259 }
1260
1261 /**
1262 * Get the ascent of the text on the specified line.
1263 * The return value is negative to match the Paint.ascent() convention.
1264 */
1265 public final int getLineAscent(int line) {
1266 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1267 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1268 }
1269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001271 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
1273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001275 return getOffsetToLeftRightOf(offset, false);
1276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277
Doug Felt9f7a4442010-03-01 12:45:56 -08001278 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1279 int line = getLineForOffset(caret);
1280 int lineStart = getLineStart(line);
1281 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001282 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001284 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001285 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001286 // if walking off line, look at the line we're headed to
1287 if (advance) {
1288 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001289 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001290 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001291 ++line;
1292 } else {
1293 return caret; // at very end, don't move
1294 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001295 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001296 } else {
1297 if (caret == lineStart) {
1298 if (line > 0) {
1299 lineChanged = true;
1300 --line;
1301 } else {
1302 return caret; // at very start, don't move
1303 }
1304 }
1305 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001306
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001307 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001308 lineStart = getLineStart(line);
1309 lineEnd = getLineEnd(line);
1310 int newDir = getParagraphDirection(line);
1311 if (newDir != lineDir) {
1312 // unusual case. we want to walk onto the line, but it runs
1313 // in a different direction than this one, so we fake movement
1314 // in the opposite direction.
1315 toLeft = !toLeft;
1316 lineDir = newDir;
1317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001319
Doug Felte8e45f22010-03-29 14:58:40 -07001320 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001321
Doug Felte8e45f22010-03-29 14:58:40 -07001322 TextLine tl = TextLine.obtain();
1323 // XXX: we don't care about tabs
1324 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1325 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1326 tl = TextLine.recycle(tl);
1327 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 }
1329
1330 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001331 // XXX this probably should skip local reorderings and
1332 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 if (offset == 0)
1334 return 0;
1335
1336 CharSequence text = mText;
1337 char c = text.charAt(offset);
1338
1339 if (c >= '\uDC00' && c <= '\uDFFF') {
1340 char c1 = text.charAt(offset - 1);
1341
1342 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1343 offset -= 1;
1344 }
1345
1346 if (mSpannedText) {
1347 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1348 ReplacementSpan.class);
1349
1350 for (int i = 0; i < spans.length; i++) {
1351 int start = ((Spanned) text).getSpanStart(spans[i]);
1352 int end = ((Spanned) text).getSpanEnd(spans[i]);
1353
1354 if (start < offset && end > offset)
1355 offset = start;
1356 }
1357 }
1358
1359 return offset;
1360 }
1361
1362 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001363 * Determine whether we should clamp cursor position. Currently it's
1364 * only robust for left-aligned displays.
1365 * @hide
1366 */
1367 public boolean shouldClampCursor(int line) {
1368 // Only clamp cursor position in left-aligned displays.
1369 switch (getParagraphAlignment(line)) {
1370 case ALIGN_LEFT:
1371 return true;
1372 case ALIGN_NORMAL:
1373 return getParagraphDirection(line) > 0;
1374 default:
1375 return false;
1376 }
1377
1378 }
1379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 * Fills in the specified Path with a representation of a cursor
1381 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001382 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 * directionalities.
1384 */
1385 public void getCursorPath(int point, Path dest,
1386 CharSequence editingBuffer) {
1387 dest.reset();
1388
1389 int line = getLineForOffset(point);
1390 int top = getLineTop(line);
1391 int bottom = getLineTop(line+1);
1392
Raph Levienafe8e9b2012-12-19 16:09:32 -08001393 boolean clamped = shouldClampCursor(line);
1394 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1395 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396
Jeff Brown497a92c2010-09-12 17:55:08 -07001397 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1398 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1399 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 int dist = 0;
1401
1402 if (caps != 0 || fn != 0) {
1403 dist = (bottom - top) >> 2;
1404
1405 if (fn != 0)
1406 top += dist;
1407 if (caps != 0)
1408 bottom -= dist;
1409 }
1410
1411 if (h1 < 0.5f)
1412 h1 = 0.5f;
1413 if (h2 < 0.5f)
1414 h2 = 0.5f;
1415
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001416 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 dest.moveTo(h1, top);
1418 dest.lineTo(h1, bottom);
1419 } else {
1420 dest.moveTo(h1, top);
1421 dest.lineTo(h1, (top + bottom) >> 1);
1422
1423 dest.moveTo(h2, (top + bottom) >> 1);
1424 dest.lineTo(h2, bottom);
1425 }
1426
1427 if (caps == 2) {
1428 dest.moveTo(h2, bottom);
1429 dest.lineTo(h2 - dist, bottom + dist);
1430 dest.lineTo(h2, bottom);
1431 dest.lineTo(h2 + dist, bottom + dist);
1432 } else if (caps == 1) {
1433 dest.moveTo(h2, bottom);
1434 dest.lineTo(h2 - dist, bottom + dist);
1435
1436 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1437 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1438
1439 dest.moveTo(h2 + dist, bottom + dist);
1440 dest.lineTo(h2, bottom);
1441 }
1442
1443 if (fn == 2) {
1444 dest.moveTo(h1, top);
1445 dest.lineTo(h1 - dist, top - dist);
1446 dest.lineTo(h1, top);
1447 dest.lineTo(h1 + dist, top - dist);
1448 } else if (fn == 1) {
1449 dest.moveTo(h1, top);
1450 dest.lineTo(h1 - dist, top - dist);
1451
1452 dest.moveTo(h1 - dist, top - dist + 0.5f);
1453 dest.lineTo(h1 + dist, top - dist + 0.5f);
1454
1455 dest.moveTo(h1 + dist, top - dist);
1456 dest.lineTo(h1, top);
1457 }
1458 }
1459
1460 private void addSelection(int line, int start, int end,
1461 int top, int bottom, Path dest) {
1462 int linestart = getLineStart(line);
1463 int lineend = getLineEnd(line);
1464 Directions dirs = getLineDirections(line);
1465
1466 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1467 lineend--;
1468
Doug Felt9f7a4442010-03-01 12:45:56 -08001469 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1470 int here = linestart + dirs.mDirections[i];
1471 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 if (there > lineend)
1474 there = lineend;
1475
1476 if (start <= there && end >= here) {
1477 int st = Math.max(start, here);
1478 int en = Math.min(end, there);
1479
1480 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001481 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1482 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001484 float left = Math.min(h1, h2);
1485 float right = Math.max(h1, h2);
1486
1487 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 }
1489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491 }
1492
1493 /**
1494 * Fills in the specified Path with a representation of a highlight
1495 * between the specified offsets. This will often be a rectangle
1496 * or a potentially discontinuous set of rectangles. If the start
1497 * and end are the same, the returned path is empty.
1498 */
1499 public void getSelectionPath(int start, int end, Path dest) {
1500 dest.reset();
1501
1502 if (start == end)
1503 return;
1504
1505 if (end < start) {
1506 int temp = end;
1507 end = start;
1508 start = temp;
1509 }
1510
1511 int startline = getLineForOffset(start);
1512 int endline = getLineForOffset(end);
1513
1514 int top = getLineTop(startline);
1515 int bottom = getLineBottom(endline);
1516
1517 if (startline == endline) {
1518 addSelection(startline, start, end, top, bottom, dest);
1519 } else {
1520 final float width = mWidth;
1521
1522 addSelection(startline, start, getLineEnd(startline),
1523 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1526 dest.addRect(getLineLeft(startline), top,
1527 0, getLineBottom(startline), Path.Direction.CW);
1528 else
1529 dest.addRect(getLineRight(startline), top,
1530 width, getLineBottom(startline), Path.Direction.CW);
1531
1532 for (int i = startline + 1; i < endline; i++) {
1533 top = getLineTop(i);
1534 bottom = getLineBottom(i);
1535 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1536 }
1537
1538 top = getLineTop(endline);
1539 bottom = getLineBottom(endline);
1540
1541 addSelection(endline, getLineStart(endline), end,
1542 top, bottom, dest);
1543
1544 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1545 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1546 else
1547 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1548 }
1549 }
1550
1551 /**
1552 * Get the alignment of the specified paragraph, taking into account
1553 * markup attached to it.
1554 */
1555 public final Alignment getParagraphAlignment(int line) {
1556 Alignment align = mAlignment;
1557
1558 if (mSpannedText) {
1559 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001560 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 getLineEnd(line),
1562 AlignmentSpan.class);
1563
1564 int spanLength = spans.length;
1565 if (spanLength > 0) {
1566 align = spans[spanLength-1].getAlignment();
1567 }
1568 }
1569
1570 return align;
1571 }
1572
1573 /**
1574 * Get the left edge of the specified paragraph, inset by left margins.
1575 */
1576 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001578 int dir = getParagraphDirection(line);
1579 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1580 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
Doug Feltc982f602010-05-25 11:51:40 -07001582 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 }
1584
1585 /**
1586 * Get the right edge of the specified paragraph, inset by right margins.
1587 */
1588 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001590 int dir = getParagraphDirection(line);
1591 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1592 return right; // leading margin has no impact, or no styles
1593 }
1594 return right - getParagraphLeadingMargin(line);
1595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
Doug Feltc982f602010-05-25 11:51:40 -07001597 /**
1598 * Returns the effective leading margin (unsigned) for this line,
1599 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1600 * @param line the line index
1601 * @return the leading margin of this line
1602 */
1603 private int getParagraphLeadingMargin(int line) {
1604 if (!mSpannedText) {
1605 return 0;
1606 }
1607 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001608
Doug Feltc982f602010-05-25 11:51:40 -07001609 int lineStart = getLineStart(line);
1610 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001611 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001612 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001613 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001614 LeadingMarginSpan.class);
1615 if (spans.length == 0) {
1616 return 0; // no leading margin span;
1617 }
Doug Felt0c702b82010-05-14 10:55:42 -07001618
Doug Feltc982f602010-05-25 11:51:40 -07001619 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001620
1621 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001622 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001623
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001624 boolean useFirstLineMargin = isFirstParaLine;
1625 for (int i = 0; i < spans.length; i++) {
1626 if (spans[i] instanceof LeadingMarginSpan2) {
1627 int spStart = spanned.getSpanStart(spans[i]);
1628 int spanLine = getLineForOffset(spStart);
1629 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1630 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1631 useFirstLineMargin |= line < spanLine + count;
1632 }
1633 }
Doug Feltc982f602010-05-25 11:51:40 -07001634 for (int i = 0; i < spans.length; i++) {
1635 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001636 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
1638
Doug Feltc982f602010-05-25 11:51:40 -07001639 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
1641
Doug Felte8e45f22010-03-29 14:58:40 -07001642 /* package */
Gilles Debunne6c488de2012-03-01 16:20:35 -08001643 static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
Doug Felte8e45f22010-03-29 14:58:40 -07001644
1645 MeasuredText mt = MeasuredText.obtain();
1646 TextLine tl = TextLine.obtain();
1647 try {
Raph Levien70616ec2015-03-04 10:41:30 -08001648 mt.setPara(text, start, end, TextDirectionHeuristics.LTR, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001649 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001650 int dir;
1651 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001652 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001653 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001654 } else {
1655 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1656 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001657 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
Doug Feltc982f602010-05-25 11:51:40 -07001659 char[] chars = mt.mChars;
1660 int len = mt.mLen;
1661 boolean hasTabs = false;
1662 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001663 // leading margins should be taken into account when measuring a paragraph
1664 int margin = 0;
1665 if (text instanceof Spanned) {
1666 Spanned spanned = (Spanned) text;
1667 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1668 LeadingMarginSpan.class);
1669 for (LeadingMarginSpan lms : spans) {
1670 margin += lms.getLeadingMargin(true);
1671 }
1672 }
Doug Feltc982f602010-05-25 11:51:40 -07001673 for (int i = 0; i < len; ++i) {
1674 if (chars[i] == '\t') {
1675 hasTabs = true;
1676 if (text instanceof Spanned) {
1677 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001678 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001679 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001680 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001681 TabStopSpan.class);
1682 if (spans.length > 0) {
1683 tabStops = new TabStops(TAB_INCREMENT, spans);
1684 }
1685 }
1686 break;
1687 }
1688 }
1689 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001690 return margin + tl.metrics(null);
Doug Felte8e45f22010-03-29 14:58:40 -07001691 } finally {
1692 TextLine.recycle(tl);
1693 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
1696
Doug Felt71b8dd72010-02-16 17:27:09 -08001697 /**
Doug Feltc982f602010-05-25 11:51:40 -07001698 * @hide
1699 */
1700 /* package */ static class TabStops {
1701 private int[] mStops;
1702 private int mNumStops;
1703 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001704
Doug Feltc982f602010-05-25 11:51:40 -07001705 TabStops(int increment, Object[] spans) {
1706 reset(increment, spans);
1707 }
Doug Felt0c702b82010-05-14 10:55:42 -07001708
Doug Feltc982f602010-05-25 11:51:40 -07001709 void reset(int increment, Object[] spans) {
1710 this.mIncrement = increment;
1711
1712 int ns = 0;
1713 if (spans != null) {
1714 int[] stops = this.mStops;
1715 for (Object o : spans) {
1716 if (o instanceof TabStopSpan) {
1717 if (stops == null) {
1718 stops = new int[10];
1719 } else if (ns == stops.length) {
1720 int[] nstops = new int[ns * 2];
1721 for (int i = 0; i < ns; ++i) {
1722 nstops[i] = stops[i];
1723 }
1724 stops = nstops;
1725 }
1726 stops[ns++] = ((TabStopSpan) o).getTabStop();
1727 }
1728 }
1729 if (ns > 1) {
1730 Arrays.sort(stops, 0, ns);
1731 }
1732 if (stops != this.mStops) {
1733 this.mStops = stops;
1734 }
1735 }
1736 this.mNumStops = ns;
1737 }
Doug Felt0c702b82010-05-14 10:55:42 -07001738
Doug Feltc982f602010-05-25 11:51:40 -07001739 float nextTab(float h) {
1740 int ns = this.mNumStops;
1741 if (ns > 0) {
1742 int[] stops = this.mStops;
1743 for (int i = 0; i < ns; ++i) {
1744 int stop = stops[i];
1745 if (stop > h) {
1746 return stop;
1747 }
1748 }
1749 }
1750 return nextDefaultStop(h, mIncrement);
1751 }
1752
1753 public static float nextDefaultStop(float h, int inc) {
1754 return ((int) ((h + inc) / inc)) * inc;
1755 }
1756 }
Doug Felt0c702b82010-05-14 10:55:42 -07001757
Doug Feltc982f602010-05-25 11:51:40 -07001758 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001759 * Returns the position of the next tab stop after h on the line.
1760 *
1761 * @param text the text
1762 * @param start start of the line
1763 * @param end limit of the line
1764 * @param h the current horizontal offset
1765 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1766 * on the line will be used. If there are no tabs, a default offset
1767 * will be used to compute the tab stop.
1768 * @return the offset of the next tab stop.
1769 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 /* package */ static float nextTab(CharSequence text, int start, int end,
1771 float h, Object[] tabs) {
1772 float nh = Float.MAX_VALUE;
1773 boolean alltabs = false;
1774
1775 if (text instanceof Spanned) {
1776 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001777 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 alltabs = true;
1779 }
1780
1781 for (int i = 0; i < tabs.length; i++) {
1782 if (!alltabs) {
1783 if (!(tabs[i] instanceof TabStopSpan))
1784 continue;
1785 }
1786
1787 int where = ((TabStopSpan) tabs[i]).getTabStop();
1788
1789 if (where < nh && where > h)
1790 nh = where;
1791 }
1792
1793 if (nh != Float.MAX_VALUE)
1794 return nh;
1795 }
1796
1797 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1798 }
1799
1800 protected final boolean isSpanned() {
1801 return mSpannedText;
1802 }
1803
Eric Fischer74d31ef2010-08-05 15:29:36 -07001804 /**
1805 * Returns the same as <code>text.getSpans()</code>, except where
1806 * <code>start</code> and <code>end</code> are the same and are not
1807 * at the very beginning of the text, in which case an empty array
1808 * is returned instead.
1809 * <p>
1810 * This is needed because of the special case that <code>getSpans()</code>
1811 * on an empty range returns the spans adjacent to that range, which is
1812 * primarily for the sake of <code>TextWatchers</code> so they will get
1813 * notifications when text goes from empty to non-empty. But it also
1814 * has the unfortunate side effect that if the text ends with an empty
1815 * paragraph, that paragraph accidentally picks up the styles of the
1816 * preceding paragraph (even though those styles will not be picked up
1817 * by new text that is inserted into the empty paragraph).
1818 * <p>
1819 * The reason it just checks whether <code>start</code> and <code>end</code>
1820 * is the same is that the only time a line can contain 0 characters
1821 * is if it is the final paragraph of the Layout; otherwise any line will
1822 * contain at least one printing or newline character. The reason for the
1823 * additional check if <code>start</code> is greater than 0 is that
1824 * if the empty paragraph is the entire content of the buffer, paragraph
1825 * styles that are already applied to the buffer will apply to text that
1826 * is inserted into it.
1827 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07001828 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001829 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08001830 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001831 }
1832
1833 return text.getSpans(start, end, type);
1834 }
1835
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001836 private char getEllipsisChar(TextUtils.TruncateAt method) {
1837 return (method == TextUtils.TruncateAt.END_SMALL) ?
Neil Fullerd29bdb22015-02-06 10:03:08 +00001838 TextUtils.ELLIPSIS_TWO_DOTS[0] :
1839 TextUtils.ELLIPSIS_NORMAL[0];
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001840 }
1841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001843 char[] dest, int destoff, TextUtils.TruncateAt method) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 int ellipsisCount = getEllipsisCount(line);
1845
1846 if (ellipsisCount == 0) {
1847 return;
1848 }
1849
1850 int ellipsisStart = getEllipsisStart(line);
1851 int linestart = getLineStart(line);
1852
1853 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
1854 char c;
1855
1856 if (i == ellipsisStart) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001857 c = getEllipsisChar(method); // ellipsis
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 } else {
1859 c = '\uFEFF'; // 0-width space
1860 }
1861
1862 int a = i + linestart;
1863
1864 if (a >= start && a < end) {
1865 dest[destoff + a - start] = c;
1866 }
1867 }
1868 }
1869
1870 /**
1871 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08001872 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 */
1874 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08001875 // Directions represents directional runs within a line of text.
1876 // Runs are pairs of ints listed in visual order, starting from the
1877 // leading margin. The first int of each pair is the offset from
1878 // the first character of the line to the start of the run. The
1879 // second int represents both the length and level of the run.
1880 // The length is in the lower bits, accessed by masking with
1881 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
1882 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
1883 // To simply test for an RTL direction, test the bit using
1884 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885
Doug Felt9f7a4442010-03-01 12:45:56 -08001886 /* package */ int[] mDirections;
1887 /* package */ Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 mDirections = dirs;
1889 }
1890 }
1891
1892 /**
1893 * Return the offset of the first character to be ellipsized away,
1894 * relative to the start of the line. (So 0 if the beginning of the
1895 * line is ellipsized, not getLineStart().)
1896 */
1897 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07001898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 /**
1900 * Returns the number of characters to be ellipsized away, or 0 if
1901 * no ellipsis is to take place.
1902 */
1903 public abstract int getEllipsisCount(int line);
1904
1905 /* package */ static class Ellipsizer implements CharSequence, GetChars {
1906 /* package */ CharSequence mText;
1907 /* package */ Layout mLayout;
1908 /* package */ int mWidth;
1909 /* package */ TextUtils.TruncateAt mMethod;
1910
1911 public Ellipsizer(CharSequence s) {
1912 mText = s;
1913 }
1914
1915 public char charAt(int off) {
1916 char[] buf = TextUtils.obtain(1);
1917 getChars(off, off + 1, buf, 0);
1918 char ret = buf[0];
1919
1920 TextUtils.recycle(buf);
1921 return ret;
1922 }
1923
1924 public void getChars(int start, int end, char[] dest, int destoff) {
1925 int line1 = mLayout.getLineForOffset(start);
1926 int line2 = mLayout.getLineForOffset(end);
1927
1928 TextUtils.getChars(mText, start, end, dest, destoff);
1929
1930 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001931 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
1933 }
1934
1935 public int length() {
1936 return mText.length();
1937 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 public CharSequence subSequence(int start, int end) {
1940 char[] s = new char[end - start];
1941 getChars(start, end, s, 0);
1942 return new String(s);
1943 }
1944
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001945 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 public String toString() {
1947 char[] s = new char[length()];
1948 getChars(0, length(), s, 0);
1949 return new String(s);
1950 }
1951
1952 }
1953
Gilles Debunne6c488de2012-03-01 16:20:35 -08001954 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 private Spanned mSpanned;
1956
1957 public SpannedEllipsizer(CharSequence display) {
1958 super(display);
1959 mSpanned = (Spanned) display;
1960 }
1961
1962 public <T> T[] getSpans(int start, int end, Class<T> type) {
1963 return mSpanned.getSpans(start, end, type);
1964 }
1965
1966 public int getSpanStart(Object tag) {
1967 return mSpanned.getSpanStart(tag);
1968 }
1969
1970 public int getSpanEnd(Object tag) {
1971 return mSpanned.getSpanEnd(tag);
1972 }
1973
1974 public int getSpanFlags(Object tag) {
1975 return mSpanned.getSpanFlags(tag);
1976 }
1977
Gilles Debunne6c488de2012-03-01 16:20:35 -08001978 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 public int nextSpanTransition(int start, int limit, Class type) {
1980 return mSpanned.nextSpanTransition(start, limit, type);
1981 }
1982
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001983 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 public CharSequence subSequence(int start, int end) {
1985 char[] s = new char[end - start];
1986 getChars(start, end, s, 0);
1987
1988 SpannableString ss = new SpannableString(new String(s));
1989 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
1990 return ss;
1991 }
1992 }
1993
1994 private CharSequence mText;
1995 private TextPaint mPaint;
1996 /* package */ TextPaint mWorkPaint;
1997 private int mWidth;
1998 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
1999 private float mSpacingMult;
2000 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002001 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002003 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002004 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005
2006 public static final int DIR_LEFT_TO_RIGHT = 1;
2007 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002008
Doug Felt20178d62010-02-22 13:39:01 -08002009 /* package */ static final int DIR_REQUEST_LTR = 1;
2010 /* package */ static final int DIR_REQUEST_RTL = -1;
2011 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2012 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013
Doug Felt9f7a4442010-03-01 12:45:56 -08002014 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2015 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2016 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2017 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 public enum Alignment {
2020 ALIGN_NORMAL,
2021 ALIGN_OPPOSITE,
2022 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002023 /** @hide */
2024 ALIGN_LEFT,
2025 /** @hide */
2026 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
2028
2029 private static final int TAB_INCREMENT = 20;
2030
2031 /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002032 new Directions(new int[] { 0, RUN_LENGTH_MASK });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002034 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036}