blob: d612fc7360b335a7ee41b2ebbf535c56383c702f [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;
Siyamed Sinir0fa89d62017-07-24 20:46:41 -070020import android.annotation.IntRange;
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
Siyamed Sinired09ae12016-02-16 14:36:26 -080034import com.android.internal.annotations.VisibleForTesting;
Doug Feltcb3791202011-07-07 11:57:48 -070035import com.android.internal.util.ArrayUtils;
Adam Lesinski776abc22014-03-07 11:30:59 -050036import com.android.internal.util.GrowingArrayUtils;
Doug Feltcb3791202011-07-07 11:57:48 -070037
Raph Levien39b4db72015-03-25 13:18:20 -070038import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
Doug Feltc982f602010-05-25 11:51:40 -070040import java.util.Arrays;
Doug Felt9f7a4442010-03-01 12:45:56 -080041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042/**
Doug Felt9f7a4442010-03-01 12:45:56 -080043 * A base class that manages text layout in visual elements on
44 * the screen.
45 * <p>For text that will be edited, use a {@link DynamicLayout},
46 * which will be updated as the text changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 * For text that will not change, use a {@link StaticLayout}.
48 */
49public abstract class Layout {
Raph Levien39b4db72015-03-25 13:18:20 -070050 /** @hide */
51 @IntDef({BREAK_STRATEGY_SIMPLE, BREAK_STRATEGY_HIGH_QUALITY, BREAK_STRATEGY_BALANCED})
52 @Retention(RetentionPolicy.SOURCE)
53 public @interface BreakStrategy {}
54
55 /**
56 * Value for break strategy indicating simple line breaking. Automatic hyphens are not added
57 * (though soft hyphens are respected), and modifying text generally doesn't affect the layout
58 * before it (which yields a more consistent user experience when editing), but layout may not
59 * be the highest quality.
60 */
61 public static final int BREAK_STRATEGY_SIMPLE = 0;
62
63 /**
64 * Value for break strategy indicating high quality line breaking, including automatic
65 * hyphenation and doing whole-paragraph optimization of line breaks.
66 */
67 public static final int BREAK_STRATEGY_HIGH_QUALITY = 1;
68
69 /**
70 * Value for break strategy indicating balanced line breaking. The breaks are chosen to
71 * make all lines as close to the same length as possible, including automatic hyphenation.
72 */
73 public static final int BREAK_STRATEGY_BALANCED = 2;
74
Roozbeh Pournader95c7a132015-05-12 12:01:06 -070075 /** @hide */
76 @IntDef({HYPHENATION_FREQUENCY_NORMAL, HYPHENATION_FREQUENCY_FULL,
77 HYPHENATION_FREQUENCY_NONE})
78 @Retention(RetentionPolicy.SOURCE)
79 public @interface HyphenationFrequency {}
80
81 /**
82 * Value for hyphenation frequency indicating no automatic hyphenation. Useful
83 * for backward compatibility, and for cases where the automatic hyphenation algorithm results
84 * in incorrect hyphenation. Mid-word breaks may still happen when a word is wider than the
85 * layout and there is otherwise no valid break. Soft hyphens are ignored and will not be used
86 * as suggestions for potential line breaks.
87 */
88 public static final int HYPHENATION_FREQUENCY_NONE = 0;
89
90 /**
91 * Value for hyphenation frequency indicating a light amount of automatic hyphenation, which
92 * is a conservative default. Useful for informal cases, such as short sentences or chat
93 * messages.
94 */
95 public static final int HYPHENATION_FREQUENCY_NORMAL = 1;
96
97 /**
98 * Value for hyphenation frequency indicating the full amount of automatic hyphenation, typical
99 * in typography. Useful for running text and where it's important to put the maximum amount of
100 * text in a screen with limited space.
101 */
102 public static final int HYPHENATION_FREQUENCY_FULL = 2;
103
Doug Felt71b8dd72010-02-16 17:27:09 -0800104 private static final ParagraphStyle[] NO_PARA_SPANS =
105 ArrayUtils.emptyArray(ParagraphStyle.class);
Dave Bort76c02262009-04-13 17:17:21 -0700106
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700107 /** @hide */
108 @IntDef({JUSTIFICATION_MODE_NONE, JUSTIFICATION_MODE_INTER_WORD})
109 @Retention(RetentionPolicy.SOURCE)
110 public @interface JustificationMode {}
111
112 /**
113 * Value for justification mode indicating no justification.
114 */
115 public static final int JUSTIFICATION_MODE_NONE = 0;
116
117 /**
118 * Value for justification mode indicating the text is justified by stretching word spacing.
119 */
120 public static final int JUSTIFICATION_MODE_INTER_WORD = 1;
121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700123 * Return how wide a layout must be in order to display the specified text with one line per
124 * paragraph.
125 *
126 * <p>As of O, Uses
127 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
128 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 */
130 public static float getDesiredWidth(CharSequence source,
131 TextPaint paint) {
132 return getDesiredWidth(source, 0, source.length(), paint);
133 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700136 * Return how wide a layout must be in order to display the specified text slice with one
137 * line per paragraph.
138 *
139 * <p>As of O, Uses
140 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
141 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
142 */
143 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) {
144 return getDesiredWidth(source, start, end, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR);
145 }
146
147 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800148 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * specified text slice with one line per paragraph.
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700150 *
151 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700153 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint,
154 TextDirectionHeuristic textDir) {
Seigo Nonaka917748e2017-08-03 17:42:28 -0700155 return getDesiredWidthWithLimit(source, start, end, paint, textDir, Float.MAX_VALUE);
156 }
157 /**
158 * Return how wide a layout must be in order to display the
159 * specified text slice with one line per paragraph.
160 *
161 * If the measured width exceeds given limit, returns limit value instead.
162 * @hide
163 */
164 public static float getDesiredWidthWithLimit(CharSequence source, int start, int end,
165 TextPaint paint, TextDirectionHeuristic textDir, float upperLimit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
168 int next;
169 for (int i = start; i <= end; i = next) {
170 next = TextUtils.indexOf(source, '\n', i, end);
171
172 if (next < 0)
173 next = end;
174
Doug Felt71b8dd72010-02-16 17:27:09 -0800175 // note, omits trailing paragraph char
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700176 float w = measurePara(paint, source, i, next, textDir);
Seigo Nonaka917748e2017-08-03 17:42:28 -0700177 if (w > upperLimit) {
178 return upperLimit;
179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
181 if (w > need)
182 need = w;
183
184 next++;
185 }
186
187 return need;
188 }
189
190 /**
191 * Subclasses of Layout use this constructor to set the display text,
192 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800193 * @param text the text to render
194 * @param paint the default paint for the layout. Styles can override
195 * various attributes of the paint.
196 * @param width the wrapping width for the text.
197 * @param align whether to left, right, or center the text. Styles can
198 * override the alignment.
199 * @param spacingMult factor by which to scale the font size to get the
200 * default line spacing
201 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 */
203 protected Layout(CharSequence text, TextPaint paint,
204 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800205 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700206 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
207 spacingMult, spacingAdd);
208 }
209
210 /**
211 * Subclasses of Layout use this constructor to set the display text,
212 * width, and other standard properties.
213 * @param text the text to render
214 * @param paint the default paint for the layout. Styles can override
215 * various attributes of the paint.
216 * @param width the wrapping width for the text.
217 * @param align whether to left, right, or center the text. Styles can
218 * override the alignment.
219 * @param spacingMult factor by which to scale the font size to get the
220 * default line spacing
221 * @param spacingAdd amount to add to the default line spacing
222 *
223 * @hide
224 */
225 protected Layout(CharSequence text, TextPaint paint,
226 int width, Alignment align, TextDirectionHeuristic textDir,
227 float spacingMult, float spacingAdd) {
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 if (width < 0)
230 throw new IllegalArgumentException("Layout: " + width + " < 0");
231
Doug Felte8e45f22010-03-29 14:58:40 -0700232 // Ensure paint doesn't have baselineShift set.
233 // While normally we don't modify the paint the user passed in,
234 // we were already doing this in Styled.drawUniformRun with both
235 // baselineShift and bgColor. We probably should reevaluate bgColor.
236 if (paint != null) {
237 paint.bgColor = 0;
238 paint.baselineShift = 0;
239 }
240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 mText = text;
242 mPaint = paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 mWidth = width;
244 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800245 mSpacingMult = spacingMult;
246 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700248 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
250
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900251 /** @hide */
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700252 protected void setJustificationMode(@JustificationMode int justificationMode) {
253 mJustificationMode = justificationMode;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900254 }
255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * Replace constructor properties of this Layout with new ones. Be careful.
258 */
259 /* package */ void replaceWith(CharSequence text, TextPaint paint,
260 int width, Alignment align,
261 float spacingmult, float spacingadd) {
262 if (width < 0) {
263 throw new IllegalArgumentException("Layout: " + width + " < 0");
264 }
265
266 mText = text;
267 mPaint = paint;
268 mWidth = width;
269 mAlignment = align;
270 mSpacingMult = spacingmult;
271 mSpacingAdd = spacingadd;
272 mSpannedText = text instanceof Spanned;
273 }
274
275 /**
276 * Draw this Layout on the specified Canvas.
277 */
278 public void draw(Canvas c) {
279 draw(c, null, null, 0);
280 }
281
282 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800283 * Draw this Layout on the specified canvas, with the highlight path drawn
284 * between the background and the text.
285 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800286 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800287 * @param highlight the path of the highlight or cursor; can be null
288 * @param highlightPaint the paint for the highlight
289 * @param cursorOffsetVertical the amount to temporarily translate the
290 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800292 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
293 int cursorOffsetVertical) {
294 final long lineRange = getLineRangeForDraw(canvas);
295 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
296 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
297 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
Gilles Debunne6c488de2012-03-01 16:20:35 -0800299 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
300 firstLine, lastLine);
301 drawText(canvas, firstLine, lastLine);
302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900304 private boolean isJustificationRequired(int lineNum) {
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700305 if (mJustificationMode == JUSTIFICATION_MODE_NONE) return false;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900306 final int lineEnd = getLineEnd(lineNum);
307 return lineEnd < mText.length() && mText.charAt(lineEnd - 1) != '\n';
308 }
309
310 private float getJustifyWidth(int lineNum) {
311 Alignment paraAlign = mAlignment;
312 TabStops tabStops = null;
313 boolean tabStopsIsInitialized = false;
314
315 int left = 0;
316 int right = mWidth;
317
318 final int dir = getParagraphDirection(lineNum);
319
320 ParagraphStyle[] spans = NO_PARA_SPANS;
321 if (mSpannedText) {
322 Spanned sp = (Spanned) mText;
323 final int start = getLineStart(lineNum);
324
325 final boolean isFirstParaLine = (start == 0 || mText.charAt(start - 1) == '\n');
326
327 if (isFirstParaLine) {
328 final int spanEnd = sp.nextSpanTransition(start, mText.length(),
329 ParagraphStyle.class);
330 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
331
332 for (int n = spans.length - 1; n >= 0; n--) {
333 if (spans[n] instanceof AlignmentSpan) {
334 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
335 break;
336 }
337 }
338 }
339
340 final int length = spans.length;
341 boolean useFirstLineMargin = isFirstParaLine;
342 for (int n = 0; n < length; n++) {
343 if (spans[n] instanceof LeadingMarginSpan2) {
344 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
345 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
346 if (lineNum < startLine + count) {
347 useFirstLineMargin = true;
348 break;
349 }
350 }
351 }
352 for (int n = 0; n < length; n++) {
353 if (spans[n] instanceof LeadingMarginSpan) {
354 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
355 if (dir == DIR_RIGHT_TO_LEFT) {
356 right -= margin.getLeadingMargin(useFirstLineMargin);
357 } else {
358 left += margin.getLeadingMargin(useFirstLineMargin);
359 }
360 }
361 }
362 }
363
364 if (getLineContainsTab(lineNum)) {
365 tabStops = new TabStops(TAB_INCREMENT, spans);
366 }
367
368 final Alignment align;
369 if (paraAlign == Alignment.ALIGN_LEFT) {
370 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
371 } else if (paraAlign == Alignment.ALIGN_RIGHT) {
372 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
373 } else {
374 align = paraAlign;
375 }
376
377 final int indentWidth;
378 if (align == Alignment.ALIGN_NORMAL) {
379 if (dir == DIR_LEFT_TO_RIGHT) {
380 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
381 } else {
382 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
383 }
384 } else if (align == Alignment.ALIGN_OPPOSITE) {
385 if (dir == DIR_LEFT_TO_RIGHT) {
386 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
387 } else {
388 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
389 }
390 } else { // Alignment.ALIGN_CENTER
391 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
392 }
393
394 return right - left - indentWidth;
395 }
396
Gilles Debunne6c488de2012-03-01 16:20:35 -0800397 /**
398 * @hide
399 */
400 public void drawText(Canvas canvas, int firstLine, int lastLine) {
401 int previousLineBottom = getLineTop(firstLine);
402 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800403 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700404 int spanEnd = 0;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900405 final TextPaint paint = mPaint;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800406 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700408 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700409 TabStops tabStops = null;
410 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800411
Doug Felte8e45f22010-03-29 14:58:40 -0700412 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700413
Gilles Debunne6c488de2012-03-01 16:20:35 -0800414 // Draw the lines, one at a time.
415 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700416 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700418 previousLineEnd = getLineStart(lineNum + 1);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900419 final boolean justify = isJustificationRequired(lineNum);
Raph Levien26d443a2015-03-30 14:18:32 -0700420 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421
422 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700423 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700425 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426
Raph Levien26d443a2015-03-30 14:18:32 -0700427 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700428 int left = 0;
429 int right = mWidth;
430
Gilles Debunne6c488de2012-03-01 16:20:35 -0800431 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700432 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800433 int textLength = buf.length();
434 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700435
Doug Feltc982f602010-05-25 11:51:40 -0700436 // New batch of paragraph styles, collect into spans array.
437 // Compute the alignment, last alignment style wins.
438 // Reset tabStops, we'll rebuild if we encounter a line with
439 // tabs.
440 // We expect paragraph spans to be relatively infrequent, use
441 // spanEnd so that we can check less frequently. Since
442 // paragraph styles ought to apply to entire paragraphs, we can
443 // just collect the ones present at the start of the paragraph.
444 // If spanEnd is before the end of the paragraph, that's not
445 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700446 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700447 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700449 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800450
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700451 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800452 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700454 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 break;
456 }
457 }
Doug Felt0c702b82010-05-14 10:55:42 -0700458
Doug Feltc982f602010-05-25 11:51:40 -0700459 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800461
Doug Feltc982f602010-05-25 11:51:40 -0700462 // Draw all leading margin spans. Adjust left or right according
463 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700465 boolean useFirstLineMargin = isFirstParaLine;
466 for (int n = 0; n < length; n++) {
467 if (spans[n] instanceof LeadingMarginSpan2) {
468 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
469 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
470 // if there is more than one LeadingMarginSpan2, use
471 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700472 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700473 useFirstLineMargin = true;
474 break;
475 }
476 }
477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 for (int n = 0; n < length; n++) {
479 if (spans[n] instanceof LeadingMarginSpan) {
480 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800482 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800484 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700485 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800487 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800489 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700490 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492 }
493 }
494 }
495
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700496 boolean hasTab = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700497 // Can't tell if we have tabs for sure, currently
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700498 if (hasTab && !tabStopsIsInitialized) {
Doug Feltc982f602010-05-25 11:51:40 -0700499 if (tabStops == null) {
500 tabStops = new TabStops(TAB_INCREMENT, spans);
501 } else {
502 tabStops.reset(TAB_INCREMENT, spans);
503 }
504 tabStopsIsInitialized = true;
505 }
506
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700507 // Determine whether the line aligns to normal, opposite, or center.
508 Alignment align = paraAlign;
509 if (align == Alignment.ALIGN_LEFT) {
510 align = (dir == DIR_LEFT_TO_RIGHT) ?
511 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
512 } else if (align == Alignment.ALIGN_RIGHT) {
513 align = (dir == DIR_LEFT_TO_RIGHT) ?
514 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
515 }
516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 int x;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900518 final int indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 if (align == Alignment.ALIGN_NORMAL) {
520 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900521 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
522 x = left + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900524 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
525 x = right - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700528 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700530 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900531 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
532 x = right - max - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900534 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
535 x = left - max + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 }
Doug Feltc982f602010-05-25 11:51:40 -0700537 } else { // Alignment.ALIGN_CENTER
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900538 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700539 max = max & ~1;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900540 x = ((right + left - max) >> 1) + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 }
542 }
543
Raph Levien26d443a2015-03-30 14:18:32 -0700544 paint.setHyphenEdit(getHyphen(lineNum));
545 Directions directions = getLineDirections(lineNum);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900546 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab && !justify) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800547 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800548 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 } else {
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700550 tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900551 if (justify) {
552 tl.justify(right - left - indentWidth);
553 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800554 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
Raph Levien9a174dd2015-04-08 13:35:03 -0700556 paint.setHyphenEdit(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
Doug Feltc982f602010-05-25 11:51:40 -0700558
Doug Felte8e45f22010-03-29 14:58:40 -0700559 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
561
562 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800563 * @hide
564 */
565 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
566 int cursorOffsetVertical, int firstLine, int lastLine) {
567 // First, draw LineBackgroundSpans.
568 // LineBackgroundSpans know nothing about the alignment, margins, or
569 // direction of the layout or line. XXX: Should they?
570 // They are evaluated at each line.
571 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700572 if (mLineBackgroundSpans == null) {
573 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700574 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800575
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700576 Spanned buffer = (Spanned) mText;
577 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700578 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800579
Gilles Debunneeca5b732012-04-25 18:48:42 -0700580 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700581 int previousLineBottom = getLineTop(firstLine);
582 int previousLineEnd = getLineStart(firstLine);
583 ParagraphStyle[] spans = NO_PARA_SPANS;
584 int spansLength = 0;
585 TextPaint paint = mPaint;
586 int spanEnd = 0;
587 final int width = mWidth;
588 for (int i = firstLine; i <= lastLine; i++) {
589 int start = previousLineEnd;
590 int end = getLineStart(i + 1);
591 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800592
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700593 int ltop = previousLineBottom;
594 int lbottom = getLineTop(i + 1);
595 previousLineBottom = lbottom;
596 int lbaseline = lbottom - getLineDescent(i);
597
598 if (start >= spanEnd) {
599 // These should be infrequent, so we'll use this so that
600 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700601 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700602 // All LineBackgroundSpans on a line contribute to its background.
603 spansLength = 0;
604 // Duplication of the logic of getParagraphSpans
605 if (start != end || start == 0) {
606 // Equivalent to a getSpans(start, end), but filling the 'spans' local
607 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700608 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
609 // equal test is valid since both intervals are not empty by
610 // construction
611 if (mLineBackgroundSpans.spanStarts[j] >= end ||
612 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500613 spans = GrowingArrayUtils.append(
614 spans, spansLength, mLineBackgroundSpans.spans[j]);
615 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700616 }
617 }
618 }
619
620 for (int n = 0; n < spansLength; n++) {
621 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
622 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
623 ltop, lbaseline, lbottom,
624 buffer, start, end, i);
625 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800626 }
627 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700628 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800629 }
630
631 // There can be a highlight even without spans if we are drawing
632 // a non-spanned transformation of a spanned editing buffer.
633 if (highlight != null) {
634 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
635 canvas.drawPath(highlight, highlightPaint);
636 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
637 }
638 }
639
640 /**
641 * @param canvas
642 * @return The range of lines that need to be drawn, possibly empty.
643 * @hide
644 */
645 public long getLineRangeForDraw(Canvas canvas) {
646 int dtop, dbottom;
647
648 synchronized (sTempRect) {
649 if (!canvas.getClipBounds(sTempRect)) {
650 // Negative range end used as a special flag
651 return TextUtils.packRangeInLong(0, -1);
652 }
653
654 dtop = sTempRect.top;
655 dbottom = sTempRect.bottom;
656 }
657
658 final int top = Math.max(dtop, 0);
659 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
660
Gilles Debunne2fba3382012-06-11 17:46:24 -0700661 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800662 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
663 }
664
665 /**
Doug Feltc982f602010-05-25 11:51:40 -0700666 * Return the start position of the line, given the left and right bounds
667 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700668 *
Doug Feltc982f602010-05-25 11:51:40 -0700669 * @param line the line index
670 * @param left the left bounds (0, or leading margin if ltr para)
671 * @param right the right bounds (width, minus leading margin if rtl para)
672 * @return the start position of the line (to right of line if rtl para)
673 */
674 private int getLineStartPos(int line, int left, int right) {
675 // Adjust the point at which to start rendering depending on the
676 // alignment of the paragraph.
677 Alignment align = getParagraphAlignment(line);
678 int dir = getParagraphDirection(line);
679
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700680 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700681 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
682 } else if (align == Alignment.ALIGN_RIGHT) {
683 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
684 }
685
686 int x;
687 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700688 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700689 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700690 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700691 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700692 }
693 } else {
694 TabStops tabStops = null;
695 if (mSpannedText && getLineContainsTab(line)) {
696 Spanned spanned = (Spanned) mText;
697 int start = getLineStart(line);
698 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
699 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800700 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
701 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700702 if (tabSpans.length > 0) {
703 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
704 }
705 }
706 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700707 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700708 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700709 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700710 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700711 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700712 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700713 }
714 } else { // Alignment.ALIGN_CENTER
715 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700716 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700717 }
718 }
719 return x;
720 }
721
722 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 * Return the text that is displayed by this Layout.
724 */
725 public final CharSequence getText() {
726 return mText;
727 }
728
729 /**
730 * Return the base Paint properties for this layout.
731 * Do NOT change the paint, which may result in funny
732 * drawing for this layout.
733 */
734 public final TextPaint getPaint() {
735 return mPaint;
736 }
737
738 /**
739 * Return the width of this layout.
740 */
741 public final int getWidth() {
742 return mWidth;
743 }
744
745 /**
746 * Return the width to which this Layout is ellipsizing, or
747 * {@link #getWidth} if it is not doing anything special.
748 */
749 public int getEllipsizedWidth() {
750 return mWidth;
751 }
752
753 /**
754 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800755 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * it does not cause the text to reflow to use the full new width.
757 */
758 public final void increaseWidthTo(int wid) {
759 if (wid < mWidth) {
760 throw new RuntimeException("attempted to reduce Layout width");
761 }
762
763 mWidth = wid;
764 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 /**
767 * Return the total height of this layout.
768 */
769 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800770 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772
773 /**
Siyamed Sinir0745c722016-05-31 20:39:33 -0700774 * Return the total height of this layout.
775 *
776 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
777 *
778 * @hide
779 */
780 public int getHeight(boolean cap) {
781 return getHeight();
782 }
783
784 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 * Return the base alignment of this layout.
786 */
787 public final Alignment getAlignment() {
788 return mAlignment;
789 }
790
791 /**
792 * Return what the text height is multiplied by to get the line height.
793 */
794 public final float getSpacingMultiplier() {
795 return mSpacingMult;
796 }
797
798 /**
799 * Return the number of units of leading that are added to each line.
800 */
801 public final float getSpacingAdd() {
802 return mSpacingAdd;
803 }
804
805 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700806 * Return the heuristic used to determine paragraph text direction.
807 * @hide
808 */
809 public final TextDirectionHeuristic getTextDirectionHeuristic() {
810 return mTextDir;
811 }
812
813 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 * Return the number of lines of text in this layout.
815 */
816 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 /**
819 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
820 * If bounds is not null, return the top, left, right, bottom extents
821 * of the specified line in it.
822 * @param line which line to examine (0..getLineCount() - 1)
823 * @param bounds Optional. If not null, it returns the extent of the line
824 * @return the Y-coordinate of the baseline
825 */
826 public int getLineBounds(int line, Rect bounds) {
827 if (bounds != null) {
828 bounds.left = 0; // ???
829 bounds.top = getLineTop(line);
830 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800831 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 }
833 return getLineBaseline(line);
834 }
835
836 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800837 * Return the vertical position of the top of the specified line
838 * (0&hellip;getLineCount()).
839 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 * bottom of the last line.
841 */
842 public abstract int getLineTop(int line);
843
844 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800845 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 */
847 public abstract int getLineDescent(int line);
848
849 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800850 * Return the text offset of the beginning of the specified line (
851 * 0&hellip;getLineCount()). If the specified line is equal to the line
852 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 */
854 public abstract int getLineStart(int line);
855
856 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800857 * Returns the primary directionality of the paragraph containing the
858 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
859 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 */
861 public abstract int getParagraphDirection(int line);
862
863 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700864 * Returns whether the specified line contains one or more
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700865 * characters that need to be handled specially, like tabs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 */
867 public abstract boolean getLineContainsTab(int line);
868
869 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800870 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 * The array alternates counts of characters in left-to-right
872 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800873 *
874 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 */
876 public abstract Directions getLineDirections(int line);
877
878 /**
879 * Returns the (negative) number of extra pixels of ascent padding in the
880 * top line of the Layout.
881 */
882 public abstract int getTopPadding();
883
884 /**
885 * Returns the number of extra pixels of descent padding in the
886 * bottom line of the Layout.
887 */
888 public abstract int getBottomPadding();
889
Raph Levien26d443a2015-03-30 14:18:32 -0700890 /**
891 * Returns the hyphen edit for a line.
892 *
893 * @hide
894 */
895 public int getHyphen(int line) {
896 return 0;
897 }
898
Raph Levien2ea52902015-07-01 14:39:31 -0700899 /**
900 * Returns the left indent for a line.
901 *
902 * @hide
903 */
904 public int getIndentAdjust(int line, Alignment alignment) {
905 return 0;
906 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700907
908 /**
909 * Returns true if the character at offset and the preceding character
910 * are at different run levels (and thus there's a split caret).
911 * @param offset the offset
912 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800913 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700914 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800915 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800916 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700917 Directions dirs = getLineDirections(line);
918 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
919 return false;
920 }
921
922 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800923 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700924 int lineEnd = getLineEnd(line);
925 if (offset == lineStart || offset == lineEnd) {
926 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
927 int runIndex = offset == lineStart ? 0 : runs.length - 2;
928 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
929 }
930
931 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800932 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700933 if (offset == runs[i]) {
934 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800935 }
936 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700937 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800938 }
939
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700940 /**
941 * Returns true if the character at offset is right to left (RTL).
942 * @param offset the offset
943 * @return true if the character is RTL, false if it is LTR
944 */
945 public boolean isRtlCharAt(int offset) {
946 int line = getLineForOffset(offset);
947 Directions dirs = getLineDirections(line);
948 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
949 return false;
950 }
951 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
952 return true;
953 }
954 int[] runs = dirs.mDirections;
955 int lineStart = getLineStart(line);
956 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700957 int start = lineStart + runs[i];
958 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
959 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700960 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
961 return ((level & 1) != 0);
962 }
963 }
964 // Should happen only if the offset is "out of bounds"
965 return false;
966 }
967
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900968 /**
969 * Returns the range of the run that the character at offset belongs to.
970 * @param offset the offset
971 * @return The range of the run
972 * @hide
973 */
974 public long getRunRange(int offset) {
975 int line = getLineForOffset(offset);
976 Directions dirs = getLineDirections(line);
977 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
978 return TextUtils.packRangeInLong(0, getLineEnd(line));
979 }
980 int[] runs = dirs.mDirections;
981 int lineStart = getLineStart(line);
982 for (int i = 0; i < runs.length; i += 2) {
983 int start = lineStart + runs[i];
984 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
985 if (offset >= start && offset < limit) {
986 return TextUtils.packRangeInLong(start, limit);
987 }
988 }
989 // Should happen only if the offset is "out of bounds"
990 return TextUtils.packRangeInLong(0, getLineEnd(line));
991 }
992
Doug Felt9f7a4442010-03-01 12:45:56 -0800993 private boolean primaryIsTrailingPrevious(int offset) {
994 int line = getLineForOffset(offset);
995 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700996 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800997 int[] runs = getLineDirections(line).mDirections;
998
999 int levelAt = -1;
1000 for (int i = 0; i < runs.length; i += 2) {
1001 int start = lineStart + runs[i];
1002 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
1003 if (limit > lineEnd) {
1004 limit = lineEnd;
1005 }
1006 if (offset >= start && offset < limit) {
1007 if (offset > start) {
1008 // Previous character is at same level, so don't use trailing.
1009 return false;
1010 }
1011 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1012 break;
1013 }
1014 }
1015 if (levelAt == -1) {
1016 // Offset was limit of line.
1017 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
1018 }
1019
1020 // At level boundary, check previous level.
1021 int levelBefore = -1;
1022 if (offset == lineStart) {
1023 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
1024 } else {
1025 offset -= 1;
1026 for (int i = 0; i < runs.length; i += 2) {
1027 int start = lineStart + runs[i];
1028 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
1029 if (limit > lineEnd) {
1030 limit = lineEnd;
1031 }
1032 if (offset >= start && offset < limit) {
1033 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1034 break;
1035 }
1036 }
1037 }
1038
1039 return levelBefore < levelAt;
1040 }
1041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 /**
1043 * Get the primary horizontal position for the specified text offset.
1044 * This is the location where a new character would be inserted in
1045 * the paragraph's primary direction.
1046 */
1047 public float getPrimaryHorizontal(int offset) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001048 return getPrimaryHorizontal(offset, false /* not clamped */);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001049 }
1050
1051 /**
1052 * Get the primary horizontal position for the specified text offset, but
1053 * optionally clamp it so that it doesn't exceed the width of the layout.
1054 * @hide
1055 */
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001056 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001057 boolean trailing = primaryIsTrailingPrevious(offset);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001058 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060
1061 /**
1062 * Get the secondary horizontal position for the specified text offset.
1063 * This is the location where a new character would be inserted in
1064 * the direction other than the paragraph's primary direction.
1065 */
1066 public float getSecondaryHorizontal(int offset) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001067 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
1069
Raph Levienafe8e9b2012-12-19 16:09:32 -08001070 /**
1071 * Get the secondary horizontal position for the specified text offset, but
1072 * optionally clamp it so that it doesn't exceed the width of the layout.
1073 * @hide
1074 */
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001075 public float getSecondaryHorizontal(int offset, boolean clamped) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001076 boolean trailing = primaryIsTrailingPrevious(offset);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001077 return getHorizontal(offset, !trailing, clamped);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001078 }
1079
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001080 private float getHorizontal(int offset, boolean primary) {
1081 return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset);
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001082 }
1083
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001084 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
1085 int line = getLineForOffset(offset);
1086
Raph Levienafe8e9b2012-12-19 16:09:32 -08001087 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
1089
Raph Levienafe8e9b2012-12-19 16:09:32 -08001090 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001092 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 int dir = getParagraphDirection(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001094 boolean hasTab = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 Directions directions = getLineDirections(line);
1096
Doug Feltc982f602010-05-25 11:51:40 -07001097 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001098 if (hasTab && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001099 // Just checking this line should be good enough, tabs should be
1100 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001101 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001102 if (tabs.length > 0) {
1103 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106
Doug Felte8e45f22010-03-29 14:58:40 -07001107 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001108 tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001109 float wid = tl.measure(offset - start, trailing, null);
1110 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111
Raph Levienafe8e9b2012-12-19 16:09:32 -08001112 if (clamped && wid > mWidth) {
1113 wid = mWidth;
1114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 int left = getParagraphLeft(line);
1116 int right = getParagraphRight(line);
1117
Doug Feltc982f602010-05-25 11:51:40 -07001118 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120
1121 /**
1122 * Get the leftmost position that should be exposed for horizontal
1123 * scrolling on the specified line.
1124 */
1125 public float getLineLeft(int line) {
1126 int dir = getParagraphDirection(line);
1127 Alignment align = getParagraphAlignment(line);
1128
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001129 if (align == Alignment.ALIGN_LEFT) {
1130 return 0;
1131 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 if (dir == DIR_RIGHT_TO_LEFT)
1133 return getParagraphRight(line) - getLineMax(line);
1134 else
1135 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001136 } else if (align == Alignment.ALIGN_RIGHT) {
1137 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 } else if (align == Alignment.ALIGN_OPPOSITE) {
1139 if (dir == DIR_RIGHT_TO_LEFT)
1140 return 0;
1141 else
1142 return mWidth - getLineMax(line);
1143 } else { /* align == Alignment.ALIGN_CENTER */
1144 int left = getParagraphLeft(line);
1145 int right = getParagraphRight(line);
1146 int max = ((int) getLineMax(line)) & ~1;
1147
1148 return left + ((right - left) - max) / 2;
1149 }
1150 }
1151
1152 /**
1153 * Get the rightmost position that should be exposed for horizontal
1154 * scrolling on the specified line.
1155 */
1156 public float getLineRight(int line) {
1157 int dir = getParagraphDirection(line);
1158 Alignment align = getParagraphAlignment(line);
1159
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001160 if (align == Alignment.ALIGN_LEFT) {
1161 return getParagraphLeft(line) + getLineMax(line);
1162 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 if (dir == DIR_RIGHT_TO_LEFT)
1164 return mWidth;
1165 else
1166 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001167 } else if (align == Alignment.ALIGN_RIGHT) {
1168 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 } else if (align == Alignment.ALIGN_OPPOSITE) {
1170 if (dir == DIR_RIGHT_TO_LEFT)
1171 return getLineMax(line);
1172 else
1173 return mWidth;
1174 } else { /* align == Alignment.ALIGN_CENTER */
1175 int left = getParagraphLeft(line);
1176 int right = getParagraphRight(line);
1177 int max = ((int) getLineMax(line)) & ~1;
1178
1179 return right - ((right - left) - max) / 2;
1180 }
1181 }
1182
1183 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001184 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001185 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 */
1187 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001188 float margin = getParagraphLeadingMargin(line);
1189 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001190 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192
1193 /**
Doug Feltc982f602010-05-25 11:51:40 -07001194 * Gets the unsigned horizontal extent of the specified line, including
1195 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 */
1197 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001198 float margin = getParagraphLeadingMargin(line);
1199 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001200 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202
Doug Feltc982f602010-05-25 11:51:40 -07001203 /**
1204 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1205 * tab stops instead of using the ones passed in.
1206 * @param line the index of the line
1207 * @param full whether to include trailing whitespace
1208 * @return the extent of the line
1209 */
1210 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001212 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001213
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001214 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001215 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001216 if (hasTabs && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001217 // Just checking this line should be good enough, tabs should be
1218 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001219 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001220 if (tabs.length > 0) {
1221 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1222 }
1223 }
Doug Felte8e45f22010-03-29 14:58:40 -07001224 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001225 // Returned directions can actually be null
1226 if (directions == null) {
1227 return 0f;
1228 }
Doug Feltc982f602010-05-25 11:51:40 -07001229 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230
Doug Felte8e45f22010-03-29 14:58:40 -07001231 TextLine tl = TextLine.obtain();
Seigo Nonaka9863f672016-12-14 17:56:10 +09001232 mPaint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001233 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001234 if (isJustificationRequired(line)) {
1235 tl.justify(getJustifyWidth(line));
1236 }
Doug Feltc982f602010-05-25 11:51:40 -07001237 float width = tl.metrics(null);
Seigo Nonaka9863f672016-12-14 17:56:10 +09001238 mPaint.setHyphenEdit(0);
Doug Feltc982f602010-05-25 11:51:40 -07001239 TextLine.recycle(tl);
1240 return width;
1241 }
1242
1243 /**
1244 * Returns the signed horizontal extent of the specified line, excluding
1245 * leading margin. If full is false, excludes trailing whitespace.
1246 * @param line the index of the line
1247 * @param tabStops the tab stops, can be null if we know they're not used.
1248 * @param full whether to include trailing whitespace
1249 * @return the extent of the text on this line
1250 */
1251 private float getLineExtent(int line, TabStops tabStops, boolean full) {
1252 int start = getLineStart(line);
1253 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001254 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001255 Directions directions = getLineDirections(line);
1256 int dir = getParagraphDirection(line);
1257
1258 TextLine tl = TextLine.obtain();
Seigo Nonaka9863f672016-12-14 17:56:10 +09001259 mPaint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001260 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001261 if (isJustificationRequired(line)) {
1262 tl.justify(getJustifyWidth(line));
1263 }
Doug Felte8e45f22010-03-29 14:58:40 -07001264 float width = tl.metrics(null);
Seigo Nonaka9863f672016-12-14 17:56:10 +09001265 mPaint.setHyphenEdit(0);
Doug Felte8e45f22010-03-29 14:58:40 -07001266 TextLine.recycle(tl);
1267 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269
1270 /**
1271 * Get the line number corresponding to the specified vertical position.
1272 * If you ask for a position above 0, you get 0; if you ask for a position
1273 * below the bottom of the text, you get the last line.
1274 */
1275 // FIXME: It may be faster to do a linear search for layouts without many lines.
1276 public int getLineForVertical(int vertical) {
1277 int high = getLineCount(), low = -1, guess;
1278
1279 while (high - low > 1) {
1280 guess = (high + low) / 2;
1281
1282 if (getLineTop(guess) > vertical)
1283 high = guess;
1284 else
1285 low = guess;
1286 }
1287
1288 if (low < 0)
1289 return 0;
1290 else
1291 return low;
1292 }
1293
1294 /**
1295 * Get the line number on which the specified text offset appears.
1296 * If you ask for a position before 0, you get 0; if you ask for a position
1297 * beyond the end of the text, you get the last line.
1298 */
1299 public int getLineForOffset(int offset) {
1300 int high = getLineCount(), low = -1, guess;
1301
1302 while (high - low > 1) {
1303 guess = (high + low) / 2;
1304
1305 if (getLineStart(guess) > offset)
1306 high = guess;
1307 else
1308 low = guess;
1309 }
1310
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001311 if (low < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 return 0;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001313 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 return low;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 }
1317
1318 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001319 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 * closest to the specified horizontal position.
1321 */
1322 public int getOffsetForHorizontal(int line, float horiz) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001323 return getOffsetForHorizontal(line, horiz, true);
1324 }
1325
1326 /**
1327 * Get the character offset on the specified line whose position is
1328 * closest to the specified horizontal position.
1329 *
1330 * @param line the line used to find the closest offset
1331 * @param horiz the horizontal position used to find the closest offset
1332 * @param primary whether to use the primary position or secondary position to find the offset
1333 *
1334 * @hide
1335 */
1336 public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
Raph Levienedb27f12015-06-01 14:34:47 -07001337 // TODO: use Paint.getOffsetForAdvance to avoid binary search
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001338 final int lineEndOffset = getLineEnd(line);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001339 final int lineStartOffset = getLineStart(line);
1340
1341 Directions dirs = getLineDirections(line);
1342
1343 TextLine tl = TextLine.obtain();
1344 // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
1345 tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
1346 false, null);
1347
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001348 final int max;
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001349 if (line == getLineCount() - 1) {
1350 max = lineEndOffset;
1351 } else {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001352 max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
1353 !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001354 }
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001355 int best = lineStartOffset;
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001356 float bestdist = Math.abs(getHorizontal(best, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357
Doug Felt9f7a4442010-03-01 12:45:56 -08001358 for (int i = 0; i < dirs.mDirections.length; i += 2) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001359 int here = lineStartOffset + dirs.mDirections[i];
Doug Felt9f7a4442010-03-01 12:45:56 -08001360 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001361 boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
1362 int swap = isRtl ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
1364 if (there > max)
1365 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 int high = there - 1 + 1, low = here + 1 - 1, guess;
1367
1368 while (high - low > 1) {
1369 guess = (high + low) / 2;
1370 int adguess = getOffsetAtStartOf(guess);
1371
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001372 if (getHorizontal(adguess, primary) * swap >= horiz * swap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 high = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001374 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 low = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 }
1378
1379 if (low < here + 1)
1380 low = here + 1;
1381
1382 if (low < there) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001383 int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
1384 low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
1385 if (low >= here && low < there) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001386 float dist = Math.abs(getHorizontal(low, primary) - horiz);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001387 if (aft < there) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001388 float other = Math.abs(getHorizontal(aft, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001390 if (other < dist) {
1391 dist = other;
1392 low = aft;
1393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001396 if (dist < bestdist) {
1397 bestdist = dist;
1398 best = low;
1399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
1401 }
1402
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001403 float dist = Math.abs(getHorizontal(here, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404
1405 if (dist < bestdist) {
1406 bestdist = dist;
1407 best = here;
1408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
1410
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001411 float dist = Math.abs(getHorizontal(max, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412
Raph Levien373b7a82013-09-20 15:11:52 -07001413 if (dist <= bestdist) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001414 bestdist = dist;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 best = max;
1416 }
1417
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001418 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 return best;
1420 }
1421
1422 /**
1423 * Return the text offset after the last character on the specified line.
1424 */
1425 public final int getLineEnd(int line) {
1426 return getLineStart(line + 1);
1427 }
1428
Doug Felt9f7a4442010-03-01 12:45:56 -08001429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 * Return the text offset after the last visible character (so whitespace
1431 * is not counted) on the specified line.
1432 */
1433 public int getLineVisibleEnd(int line) {
1434 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1435 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 CharSequence text = mText;
1439 char ch;
1440 if (line == getLineCount() - 1) {
1441 return end;
1442 }
1443
1444 for (; end > start; end--) {
1445 ch = text.charAt(end - 1);
1446
1447 if (ch == '\n') {
1448 return end - 1;
1449 }
1450
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001451 if (!TextLine.isLineEndSpace(ch)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 break;
1453 }
1454
1455 }
1456
1457 return end;
1458 }
1459
1460 /**
1461 * Return the vertical position of the bottom of the specified line.
1462 */
1463 public final int getLineBottom(int line) {
1464 return getLineTop(line + 1);
1465 }
1466
1467 /**
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001468 * Return the vertical position of the bottom of the specified line without the line spacing
1469 * added.
1470 *
1471 * @hide
1472 */
1473 public final int getLineBottomWithoutSpacing(int line) {
1474 return getLineTop(line + 1) - getLineExtra(line);
1475 }
1476
1477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 * Return the vertical position of the baseline of the specified line.
1479 */
1480 public final int getLineBaseline(int line) {
1481 // getLineTop(line+1) == getLineTop(line)
1482 return getLineTop(line+1) - getLineDescent(line);
1483 }
1484
1485 /**
1486 * Get the ascent of the text on the specified line.
1487 * The return value is negative to match the Paint.ascent() convention.
1488 */
1489 public final int getLineAscent(int line) {
1490 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1491 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1492 }
1493
Siyamed Sinir0fa89d62017-07-24 20:46:41 -07001494 /**
1495 * Return the extra space added as a result of line spacing attributes
1496 * {@link #getSpacingAdd()} and {@link #getSpacingMultiplier()}. Default value is {@code zero}.
1497 *
1498 * @param line the index of the line, the value should be equal or greater than {@code zero}
1499 * @hide
1500 */
1501 public int getLineExtra(@IntRange(from = 0) int line) {
1502 return 0;
1503 }
1504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001506 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 }
1508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001510 return getOffsetToLeftRightOf(offset, false);
1511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512
Doug Felt9f7a4442010-03-01 12:45:56 -08001513 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1514 int line = getLineForOffset(caret);
1515 int lineStart = getLineStart(line);
1516 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001517 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001519 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001520 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001521 // if walking off line, look at the line we're headed to
1522 if (advance) {
1523 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001524 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001525 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001526 ++line;
1527 } else {
1528 return caret; // at very end, don't move
1529 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001530 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001531 } else {
1532 if (caret == lineStart) {
1533 if (line > 0) {
1534 lineChanged = true;
1535 --line;
1536 } else {
1537 return caret; // at very start, don't move
1538 }
1539 }
1540 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001541
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001542 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001543 lineStart = getLineStart(line);
1544 lineEnd = getLineEnd(line);
1545 int newDir = getParagraphDirection(line);
1546 if (newDir != lineDir) {
1547 // unusual case. we want to walk onto the line, but it runs
1548 // in a different direction than this one, so we fake movement
1549 // in the opposite direction.
1550 toLeft = !toLeft;
1551 lineDir = newDir;
1552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001554
Doug Felte8e45f22010-03-29 14:58:40 -07001555 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001556
Doug Felte8e45f22010-03-29 14:58:40 -07001557 TextLine tl = TextLine.obtain();
1558 // XXX: we don't care about tabs
1559 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1560 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1561 tl = TextLine.recycle(tl);
1562 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 }
1564
1565 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001566 // XXX this probably should skip local reorderings and
1567 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 if (offset == 0)
1569 return 0;
1570
1571 CharSequence text = mText;
1572 char c = text.charAt(offset);
1573
1574 if (c >= '\uDC00' && c <= '\uDFFF') {
1575 char c1 = text.charAt(offset - 1);
1576
1577 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1578 offset -= 1;
1579 }
1580
1581 if (mSpannedText) {
1582 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1583 ReplacementSpan.class);
1584
1585 for (int i = 0; i < spans.length; i++) {
1586 int start = ((Spanned) text).getSpanStart(spans[i]);
1587 int end = ((Spanned) text).getSpanEnd(spans[i]);
1588
1589 if (start < offset && end > offset)
1590 offset = start;
1591 }
1592 }
1593
1594 return offset;
1595 }
1596
1597 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001598 * Determine whether we should clamp cursor position. Currently it's
1599 * only robust for left-aligned displays.
1600 * @hide
1601 */
1602 public boolean shouldClampCursor(int line) {
1603 // Only clamp cursor position in left-aligned displays.
1604 switch (getParagraphAlignment(line)) {
1605 case ALIGN_LEFT:
1606 return true;
1607 case ALIGN_NORMAL:
1608 return getParagraphDirection(line) > 0;
1609 default:
1610 return false;
1611 }
1612
1613 }
1614 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 * Fills in the specified Path with a representation of a cursor
1616 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001617 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 * directionalities.
1619 */
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001620 public void getCursorPath(final int point, final Path dest, final CharSequence editingBuffer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 dest.reset();
1622
1623 int line = getLineForOffset(point);
1624 int top = getLineTop(line);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001625 int bottom = getLineBottomWithoutSpacing(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626
Raph Levienafe8e9b2012-12-19 16:09:32 -08001627 boolean clamped = shouldClampCursor(line);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001628 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1629 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630
Jeff Brown497a92c2010-09-12 17:55:08 -07001631 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1632 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1633 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 int dist = 0;
1635
1636 if (caps != 0 || fn != 0) {
1637 dist = (bottom - top) >> 2;
1638
1639 if (fn != 0)
1640 top += dist;
1641 if (caps != 0)
1642 bottom -= dist;
1643 }
1644
1645 if (h1 < 0.5f)
1646 h1 = 0.5f;
1647 if (h2 < 0.5f)
1648 h2 = 0.5f;
1649
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001650 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 dest.moveTo(h1, top);
1652 dest.lineTo(h1, bottom);
1653 } else {
1654 dest.moveTo(h1, top);
1655 dest.lineTo(h1, (top + bottom) >> 1);
1656
1657 dest.moveTo(h2, (top + bottom) >> 1);
1658 dest.lineTo(h2, bottom);
1659 }
1660
1661 if (caps == 2) {
1662 dest.moveTo(h2, bottom);
1663 dest.lineTo(h2 - dist, bottom + dist);
1664 dest.lineTo(h2, bottom);
1665 dest.lineTo(h2 + dist, bottom + dist);
1666 } else if (caps == 1) {
1667 dest.moveTo(h2, bottom);
1668 dest.lineTo(h2 - dist, bottom + dist);
1669
1670 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1671 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1672
1673 dest.moveTo(h2 + dist, bottom + dist);
1674 dest.lineTo(h2, bottom);
1675 }
1676
1677 if (fn == 2) {
1678 dest.moveTo(h1, top);
1679 dest.lineTo(h1 - dist, top - dist);
1680 dest.lineTo(h1, top);
1681 dest.lineTo(h1 + dist, top - dist);
1682 } else if (fn == 1) {
1683 dest.moveTo(h1, top);
1684 dest.lineTo(h1 - dist, top - dist);
1685
1686 dest.moveTo(h1 - dist, top - dist + 0.5f);
1687 dest.lineTo(h1 + dist, top - dist + 0.5f);
1688
1689 dest.moveTo(h1 + dist, top - dist);
1690 dest.lineTo(h1, top);
1691 }
1692 }
1693
1694 private void addSelection(int line, int start, int end,
1695 int top, int bottom, Path dest) {
1696 int linestart = getLineStart(line);
1697 int lineend = getLineEnd(line);
1698 Directions dirs = getLineDirections(line);
1699
1700 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1701 lineend--;
1702
Doug Felt9f7a4442010-03-01 12:45:56 -08001703 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1704 int here = linestart + dirs.mDirections[i];
1705 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 if (there > lineend)
1708 there = lineend;
1709
1710 if (start <= there && end >= here) {
1711 int st = Math.max(start, here);
1712 int en = Math.min(end, there);
1713
1714 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001715 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1716 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001718 float left = Math.min(h1, h2);
1719 float right = Math.max(h1, h2);
1720
1721 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 }
1723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 }
1725 }
1726
1727 /**
1728 * Fills in the specified Path with a representation of a highlight
1729 * between the specified offsets. This will often be a rectangle
1730 * or a potentially discontinuous set of rectangles. If the start
1731 * and end are the same, the returned path is empty.
1732 */
1733 public void getSelectionPath(int start, int end, Path dest) {
1734 dest.reset();
1735
1736 if (start == end)
1737 return;
1738
1739 if (end < start) {
1740 int temp = end;
1741 end = start;
1742 start = temp;
1743 }
1744
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001745 final int startline = getLineForOffset(start);
1746 final int endline = getLineForOffset(end);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 int top = getLineTop(startline);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001749 int bottom = getLineBottomWithoutSpacing(endline);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750
1751 if (startline == endline) {
1752 addSelection(startline, start, end, top, bottom, dest);
1753 } else {
1754 final float width = mWidth;
1755
1756 addSelection(startline, start, getLineEnd(startline),
1757 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1760 dest.addRect(getLineLeft(startline), top,
1761 0, getLineBottom(startline), Path.Direction.CW);
1762 else
1763 dest.addRect(getLineRight(startline), top,
1764 width, getLineBottom(startline), Path.Direction.CW);
1765
1766 for (int i = startline + 1; i < endline; i++) {
1767 top = getLineTop(i);
1768 bottom = getLineBottom(i);
1769 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1770 }
1771
1772 top = getLineTop(endline);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001773 bottom = getLineBottomWithoutSpacing(endline);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001775 addSelection(endline, getLineStart(endline), end, top, bottom, dest);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776
1777 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1778 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1779 else
1780 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1781 }
1782 }
1783
1784 /**
1785 * Get the alignment of the specified paragraph, taking into account
1786 * markup attached to it.
1787 */
1788 public final Alignment getParagraphAlignment(int line) {
1789 Alignment align = mAlignment;
1790
1791 if (mSpannedText) {
1792 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001793 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 getLineEnd(line),
1795 AlignmentSpan.class);
1796
1797 int spanLength = spans.length;
1798 if (spanLength > 0) {
1799 align = spans[spanLength-1].getAlignment();
1800 }
1801 }
1802
1803 return align;
1804 }
1805
1806 /**
1807 * Get the left edge of the specified paragraph, inset by left margins.
1808 */
1809 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001811 int dir = getParagraphDirection(line);
1812 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1813 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
Doug Feltc982f602010-05-25 11:51:40 -07001815 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
1817
1818 /**
1819 * Get the right edge of the specified paragraph, inset by right margins.
1820 */
1821 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001823 int dir = getParagraphDirection(line);
1824 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1825 return right; // leading margin has no impact, or no styles
1826 }
1827 return right - getParagraphLeadingMargin(line);
1828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829
Doug Feltc982f602010-05-25 11:51:40 -07001830 /**
1831 * Returns the effective leading margin (unsigned) for this line,
1832 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1833 * @param line the line index
1834 * @return the leading margin of this line
1835 */
1836 private int getParagraphLeadingMargin(int line) {
1837 if (!mSpannedText) {
1838 return 0;
1839 }
1840 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001841
Doug Feltc982f602010-05-25 11:51:40 -07001842 int lineStart = getLineStart(line);
1843 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001844 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001845 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001846 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001847 LeadingMarginSpan.class);
1848 if (spans.length == 0) {
1849 return 0; // no leading margin span;
1850 }
Doug Felt0c702b82010-05-14 10:55:42 -07001851
Doug Feltc982f602010-05-25 11:51:40 -07001852 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001853
1854 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001855 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001856
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001857 boolean useFirstLineMargin = isFirstParaLine;
1858 for (int i = 0; i < spans.length; i++) {
1859 if (spans[i] instanceof LeadingMarginSpan2) {
1860 int spStart = spanned.getSpanStart(spans[i]);
1861 int spanLine = getLineForOffset(spStart);
1862 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1863 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1864 useFirstLineMargin |= line < spanLine + count;
1865 }
1866 }
Doug Feltc982f602010-05-25 11:51:40 -07001867 for (int i = 0; i < spans.length; i++) {
1868 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001869 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 }
1871
Doug Feltc982f602010-05-25 11:51:40 -07001872 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
1874
Doug Felte8e45f22010-03-29 14:58:40 -07001875 /* package */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001876 static float measurePara(TextPaint paint, CharSequence text, int start, int end,
1877 TextDirectionHeuristic textDir) {
Doug Felte8e45f22010-03-29 14:58:40 -07001878 MeasuredText mt = MeasuredText.obtain();
1879 TextLine tl = TextLine.obtain();
1880 try {
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001881 mt.setPara(text, start, end, textDir, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001882 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001883 int dir;
1884 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001885 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001886 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001887 } else {
1888 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1889 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001890 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 }
Doug Feltc982f602010-05-25 11:51:40 -07001892 char[] chars = mt.mChars;
1893 int len = mt.mLen;
1894 boolean hasTabs = false;
1895 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001896 // leading margins should be taken into account when measuring a paragraph
1897 int margin = 0;
1898 if (text instanceof Spanned) {
1899 Spanned spanned = (Spanned) text;
1900 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1901 LeadingMarginSpan.class);
1902 for (LeadingMarginSpan lms : spans) {
1903 margin += lms.getLeadingMargin(true);
1904 }
1905 }
Doug Feltc982f602010-05-25 11:51:40 -07001906 for (int i = 0; i < len; ++i) {
1907 if (chars[i] == '\t') {
1908 hasTabs = true;
1909 if (text instanceof Spanned) {
1910 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001911 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001912 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001913 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001914 TabStopSpan.class);
1915 if (spans.length > 0) {
1916 tabStops = new TabStops(TAB_INCREMENT, spans);
1917 }
1918 }
1919 break;
1920 }
1921 }
1922 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001923 return margin + Math.abs(tl.metrics(null));
Doug Felte8e45f22010-03-29 14:58:40 -07001924 } finally {
1925 TextLine.recycle(tl);
1926 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
1929
Doug Felt71b8dd72010-02-16 17:27:09 -08001930 /**
Doug Feltc982f602010-05-25 11:51:40 -07001931 * @hide
1932 */
1933 /* package */ static class TabStops {
1934 private int[] mStops;
1935 private int mNumStops;
1936 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001937
Doug Feltc982f602010-05-25 11:51:40 -07001938 TabStops(int increment, Object[] spans) {
1939 reset(increment, spans);
1940 }
Doug Felt0c702b82010-05-14 10:55:42 -07001941
Doug Feltc982f602010-05-25 11:51:40 -07001942 void reset(int increment, Object[] spans) {
1943 this.mIncrement = increment;
1944
1945 int ns = 0;
1946 if (spans != null) {
1947 int[] stops = this.mStops;
1948 for (Object o : spans) {
1949 if (o instanceof TabStopSpan) {
1950 if (stops == null) {
1951 stops = new int[10];
1952 } else if (ns == stops.length) {
1953 int[] nstops = new int[ns * 2];
1954 for (int i = 0; i < ns; ++i) {
1955 nstops[i] = stops[i];
1956 }
1957 stops = nstops;
1958 }
1959 stops[ns++] = ((TabStopSpan) o).getTabStop();
1960 }
1961 }
1962 if (ns > 1) {
1963 Arrays.sort(stops, 0, ns);
1964 }
1965 if (stops != this.mStops) {
1966 this.mStops = stops;
1967 }
1968 }
1969 this.mNumStops = ns;
1970 }
Doug Felt0c702b82010-05-14 10:55:42 -07001971
Doug Feltc982f602010-05-25 11:51:40 -07001972 float nextTab(float h) {
1973 int ns = this.mNumStops;
1974 if (ns > 0) {
1975 int[] stops = this.mStops;
1976 for (int i = 0; i < ns; ++i) {
1977 int stop = stops[i];
1978 if (stop > h) {
1979 return stop;
1980 }
1981 }
1982 }
1983 return nextDefaultStop(h, mIncrement);
1984 }
1985
1986 public static float nextDefaultStop(float h, int inc) {
1987 return ((int) ((h + inc) / inc)) * inc;
1988 }
1989 }
Doug Felt0c702b82010-05-14 10:55:42 -07001990
Doug Feltc982f602010-05-25 11:51:40 -07001991 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001992 * Returns the position of the next tab stop after h on the line.
1993 *
1994 * @param text the text
1995 * @param start start of the line
1996 * @param end limit of the line
1997 * @param h the current horizontal offset
1998 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1999 * on the line will be used. If there are no tabs, a default offset
2000 * will be used to compute the tab stop.
2001 * @return the offset of the next tab stop.
2002 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 /* package */ static float nextTab(CharSequence text, int start, int end,
2004 float h, Object[] tabs) {
2005 float nh = Float.MAX_VALUE;
2006 boolean alltabs = false;
2007
2008 if (text instanceof Spanned) {
2009 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002010 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 alltabs = true;
2012 }
2013
2014 for (int i = 0; i < tabs.length; i++) {
2015 if (!alltabs) {
2016 if (!(tabs[i] instanceof TabStopSpan))
2017 continue;
2018 }
2019
2020 int where = ((TabStopSpan) tabs[i]).getTabStop();
2021
2022 if (where < nh && where > h)
2023 nh = where;
2024 }
2025
2026 if (nh != Float.MAX_VALUE)
2027 return nh;
2028 }
2029
2030 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
2031 }
2032
2033 protected final boolean isSpanned() {
2034 return mSpannedText;
2035 }
2036
Eric Fischer74d31ef2010-08-05 15:29:36 -07002037 /**
2038 * Returns the same as <code>text.getSpans()</code>, except where
2039 * <code>start</code> and <code>end</code> are the same and are not
2040 * at the very beginning of the text, in which case an empty array
2041 * is returned instead.
2042 * <p>
2043 * This is needed because of the special case that <code>getSpans()</code>
2044 * on an empty range returns the spans adjacent to that range, which is
2045 * primarily for the sake of <code>TextWatchers</code> so they will get
2046 * notifications when text goes from empty to non-empty. But it also
2047 * has the unfortunate side effect that if the text ends with an empty
2048 * paragraph, that paragraph accidentally picks up the styles of the
2049 * preceding paragraph (even though those styles will not be picked up
2050 * by new text that is inserted into the empty paragraph).
2051 * <p>
2052 * The reason it just checks whether <code>start</code> and <code>end</code>
2053 * is the same is that the only time a line can contain 0 characters
2054 * is if it is the final paragraph of the Layout; otherwise any line will
2055 * contain at least one printing or newline character. The reason for the
2056 * additional check if <code>start</code> is greater than 0 is that
2057 * if the empty paragraph is the entire content of the buffer, paragraph
2058 * styles that are already applied to the buffer will apply to text that
2059 * is inserted into it.
2060 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07002061 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002062 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08002063 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07002064 }
2065
Siyamed Sinirfa05ba02016-01-12 10:54:43 -08002066 if(text instanceof SpannableStringBuilder) {
2067 return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
2068 } else {
2069 return text.getSpans(start, end, type);
2070 }
Eric Fischer74d31ef2010-08-05 15:29:36 -07002071 }
2072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002074 char[] dest, int destoff, TextUtils.TruncateAt method) {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002075 final int ellipsisCount = getEllipsisCount(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 if (ellipsisCount == 0) {
2077 return;
2078 }
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002079 final int ellipsisStart = getEllipsisStart(line);
2080 final int lineStart = getLineStart(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002082 final String ellipsisString = TextUtils.getEllipsisString(method);
2083 final int ellipsisStringLen = ellipsisString.length();
Roozbeh Pournader1051bbe2017-07-25 13:52:57 -07002084 // Use the ellipsis string only if there are that at least as many characters to replace.
2085 final boolean useEllipsisString = ellipsisCount >= ellipsisStringLen;
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002086 for (int i = 0; i < ellipsisCount; i++) {
2087 final char c;
Roozbeh Pournader1051bbe2017-07-25 13:52:57 -07002088 if (useEllipsisString && i < ellipsisStringLen) {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002089 c = ellipsisString.charAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 } else {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002091 c = TextUtils.ELLIPSIS_FILLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 }
2093
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002094 final int a = i + ellipsisStart + lineStart;
2095 if (start <= a && a < end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 dest[destoff + a - start] = c;
2097 }
2098 }
2099 }
2100
2101 /**
2102 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08002103 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 */
2105 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08002106 // Directions represents directional runs within a line of text.
2107 // Runs are pairs of ints listed in visual order, starting from the
2108 // leading margin. The first int of each pair is the offset from
2109 // the first character of the line to the start of the run. The
2110 // second int represents both the length and level of the run.
2111 // The length is in the lower bits, accessed by masking with
2112 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
2113 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
2114 // To simply test for an RTL direction, test the bit using
2115 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116
Siyamed Sinired09ae12016-02-16 14:36:26 -08002117 /**
2118 * @hide
2119 */
2120 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2121 public int[] mDirections;
2122
2123 /**
2124 * @hide
2125 */
2126 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2127 public Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 mDirections = dirs;
2129 }
2130 }
2131
2132 /**
2133 * Return the offset of the first character to be ellipsized away,
2134 * relative to the start of the line. (So 0 if the beginning of the
2135 * line is ellipsized, not getLineStart().)
2136 */
2137 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07002138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 /**
2140 * Returns the number of characters to be ellipsized away, or 0 if
2141 * no ellipsis is to take place.
2142 */
2143 public abstract int getEllipsisCount(int line);
2144
2145 /* package */ static class Ellipsizer implements CharSequence, GetChars {
2146 /* package */ CharSequence mText;
2147 /* package */ Layout mLayout;
2148 /* package */ int mWidth;
2149 /* package */ TextUtils.TruncateAt mMethod;
2150
2151 public Ellipsizer(CharSequence s) {
2152 mText = s;
2153 }
2154
2155 public char charAt(int off) {
2156 char[] buf = TextUtils.obtain(1);
2157 getChars(off, off + 1, buf, 0);
2158 char ret = buf[0];
2159
2160 TextUtils.recycle(buf);
2161 return ret;
2162 }
2163
2164 public void getChars(int start, int end, char[] dest, int destoff) {
2165 int line1 = mLayout.getLineForOffset(start);
2166 int line2 = mLayout.getLineForOffset(end);
2167
2168 TextUtils.getChars(mText, start, end, dest, destoff);
2169
2170 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002171 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 }
2173 }
2174
2175 public int length() {
2176 return mText.length();
2177 }
Doug Felt9f7a4442010-03-01 12:45:56 -08002178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 public CharSequence subSequence(int start, int end) {
2180 char[] s = new char[end - start];
2181 getChars(start, end, s, 0);
2182 return new String(s);
2183 }
2184
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002185 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 public String toString() {
2187 char[] s = new char[length()];
2188 getChars(0, length(), s, 0);
2189 return new String(s);
2190 }
2191
2192 }
2193
Gilles Debunne6c488de2012-03-01 16:20:35 -08002194 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 private Spanned mSpanned;
2196
2197 public SpannedEllipsizer(CharSequence display) {
2198 super(display);
2199 mSpanned = (Spanned) display;
2200 }
2201
2202 public <T> T[] getSpans(int start, int end, Class<T> type) {
2203 return mSpanned.getSpans(start, end, type);
2204 }
2205
2206 public int getSpanStart(Object tag) {
2207 return mSpanned.getSpanStart(tag);
2208 }
2209
2210 public int getSpanEnd(Object tag) {
2211 return mSpanned.getSpanEnd(tag);
2212 }
2213
2214 public int getSpanFlags(Object tag) {
2215 return mSpanned.getSpanFlags(tag);
2216 }
2217
Gilles Debunne6c488de2012-03-01 16:20:35 -08002218 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 public int nextSpanTransition(int start, int limit, Class type) {
2220 return mSpanned.nextSpanTransition(start, limit, type);
2221 }
2222
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002223 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 public CharSequence subSequence(int start, int end) {
2225 char[] s = new char[end - start];
2226 getChars(start, end, s, 0);
2227
2228 SpannableString ss = new SpannableString(new String(s));
2229 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
2230 return ss;
2231 }
2232 }
2233
2234 private CharSequence mText;
2235 private TextPaint mPaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 private int mWidth;
2237 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
2238 private float mSpacingMult;
2239 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002240 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002242 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002243 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -07002244 private int mJustificationMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245
2246 public static final int DIR_LEFT_TO_RIGHT = 1;
2247 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002248
Doug Felt20178d62010-02-22 13:39:01 -08002249 /* package */ static final int DIR_REQUEST_LTR = 1;
2250 /* package */ static final int DIR_REQUEST_RTL = -1;
2251 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2252 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253
Doug Felt9f7a4442010-03-01 12:45:56 -08002254 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2255 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2256 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2257 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 public enum Alignment {
2260 ALIGN_NORMAL,
2261 ALIGN_OPPOSITE,
2262 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002263 /** @hide */
2264 ALIGN_LEFT,
2265 /** @hide */
2266 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 }
2268
2269 private static final int TAB_INCREMENT = 20;
2270
Siyamed Sinired09ae12016-02-16 14:36:26 -08002271 /** @hide */
2272 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2273 public static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002274 new Directions(new int[] { 0, RUN_LENGTH_MASK });
Siyamed Sinired09ae12016-02-16 14:36:26 -08002275
2276 /** @hide */
2277 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2278 public static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002279 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281}