blob: 2a693a1841e65017590f41edbbb156e81e17a330 [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
Roozbeh Pournader22a167c2017-08-21 12:53:44 -0700122 /*
123 * Line spacing multiplier for default line spacing.
124 */
125 public static final float DEFAULT_LINESPACING_MULTIPLIER = 1.0f;
126
127 /*
128 * Line spacing addition for default line spacing.
129 */
130 public static final float DEFAULT_LINESPACING_ADDITION = 0.0f;
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700133 * Return how wide a layout must be in order to display the specified text with one line per
134 * paragraph.
135 *
136 * <p>As of O, Uses
137 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
138 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 */
140 public static float getDesiredWidth(CharSequence source,
141 TextPaint paint) {
142 return getDesiredWidth(source, 0, source.length(), paint);
143 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700146 * Return how wide a layout must be in order to display the specified text slice with one
147 * line per paragraph.
148 *
149 * <p>As of O, Uses
150 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
151 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
152 */
153 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) {
154 return getDesiredWidth(source, start, end, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR);
155 }
156
157 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800158 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 * specified text slice with one line per paragraph.
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700160 *
161 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700163 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint,
164 TextDirectionHeuristic textDir) {
Seigo Nonaka917748e2017-08-03 17:42:28 -0700165 return getDesiredWidthWithLimit(source, start, end, paint, textDir, Float.MAX_VALUE);
166 }
167 /**
168 * Return how wide a layout must be in order to display the
169 * specified text slice with one line per paragraph.
170 *
171 * If the measured width exceeds given limit, returns limit value instead.
172 * @hide
173 */
174 public static float getDesiredWidthWithLimit(CharSequence source, int start, int end,
175 TextPaint paint, TextDirectionHeuristic textDir, float upperLimit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
178 int next;
179 for (int i = start; i <= end; i = next) {
180 next = TextUtils.indexOf(source, '\n', i, end);
181
182 if (next < 0)
183 next = end;
184
Doug Felt71b8dd72010-02-16 17:27:09 -0800185 // note, omits trailing paragraph char
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700186 float w = measurePara(paint, source, i, next, textDir);
Seigo Nonaka917748e2017-08-03 17:42:28 -0700187 if (w > upperLimit) {
188 return upperLimit;
189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
191 if (w > need)
192 need = w;
193
194 next++;
195 }
196
197 return need;
198 }
199
200 /**
201 * Subclasses of Layout use this constructor to set the display text,
202 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800203 * @param text the text to render
204 * @param paint the default paint for the layout. Styles can override
205 * various attributes of the paint.
206 * @param width the wrapping width for the text.
207 * @param align whether to left, right, or center the text. Styles can
208 * override the alignment.
209 * @param spacingMult factor by which to scale the font size to get the
210 * default line spacing
211 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 */
213 protected Layout(CharSequence text, TextPaint paint,
214 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800215 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700216 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
217 spacingMult, spacingAdd);
218 }
219
220 /**
221 * Subclasses of Layout use this constructor to set the display text,
222 * width, and other standard properties.
223 * @param text the text to render
224 * @param paint the default paint for the layout. Styles can override
225 * various attributes of the paint.
226 * @param width the wrapping width for the text.
227 * @param align whether to left, right, or center the text. Styles can
228 * override the alignment.
229 * @param spacingMult factor by which to scale the font size to get the
230 * default line spacing
231 * @param spacingAdd amount to add to the default line spacing
232 *
233 * @hide
234 */
235 protected Layout(CharSequence text, TextPaint paint,
236 int width, Alignment align, TextDirectionHeuristic textDir,
237 float spacingMult, float spacingAdd) {
238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 if (width < 0)
240 throw new IllegalArgumentException("Layout: " + width + " < 0");
241
Doug Felte8e45f22010-03-29 14:58:40 -0700242 // Ensure paint doesn't have baselineShift set.
243 // While normally we don't modify the paint the user passed in,
244 // we were already doing this in Styled.drawUniformRun with both
245 // baselineShift and bgColor. We probably should reevaluate bgColor.
246 if (paint != null) {
247 paint.bgColor = 0;
248 paint.baselineShift = 0;
249 }
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 mText = text;
252 mPaint = paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 mWidth = width;
254 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800255 mSpacingMult = spacingMult;
256 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700258 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 }
260
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900261 /** @hide */
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700262 protected void setJustificationMode(@JustificationMode int justificationMode) {
263 mJustificationMode = justificationMode;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900264 }
265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 /**
267 * Replace constructor properties of this Layout with new ones. Be careful.
268 */
269 /* package */ void replaceWith(CharSequence text, TextPaint paint,
270 int width, Alignment align,
271 float spacingmult, float spacingadd) {
272 if (width < 0) {
273 throw new IllegalArgumentException("Layout: " + width + " < 0");
274 }
275
276 mText = text;
277 mPaint = paint;
278 mWidth = width;
279 mAlignment = align;
280 mSpacingMult = spacingmult;
281 mSpacingAdd = spacingadd;
282 mSpannedText = text instanceof Spanned;
283 }
284
285 /**
286 * Draw this Layout on the specified Canvas.
287 */
288 public void draw(Canvas c) {
289 draw(c, null, null, 0);
290 }
291
292 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800293 * Draw this Layout on the specified canvas, with the highlight path drawn
294 * between the background and the text.
295 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800296 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800297 * @param highlight the path of the highlight or cursor; can be null
298 * @param highlightPaint the paint for the highlight
299 * @param cursorOffsetVertical the amount to temporarily translate the
300 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800302 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
303 int cursorOffsetVertical) {
304 final long lineRange = getLineRangeForDraw(canvas);
305 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
306 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
307 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308
Gilles Debunne6c488de2012-03-01 16:20:35 -0800309 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
310 firstLine, lastLine);
311 drawText(canvas, firstLine, lastLine);
312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900314 private boolean isJustificationRequired(int lineNum) {
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700315 if (mJustificationMode == JUSTIFICATION_MODE_NONE) return false;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900316 final int lineEnd = getLineEnd(lineNum);
317 return lineEnd < mText.length() && mText.charAt(lineEnd - 1) != '\n';
318 }
319
320 private float getJustifyWidth(int lineNum) {
321 Alignment paraAlign = mAlignment;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900322
323 int left = 0;
324 int right = mWidth;
325
326 final int dir = getParagraphDirection(lineNum);
327
328 ParagraphStyle[] spans = NO_PARA_SPANS;
329 if (mSpannedText) {
330 Spanned sp = (Spanned) mText;
331 final int start = getLineStart(lineNum);
332
333 final boolean isFirstParaLine = (start == 0 || mText.charAt(start - 1) == '\n');
334
335 if (isFirstParaLine) {
336 final int spanEnd = sp.nextSpanTransition(start, mText.length(),
337 ParagraphStyle.class);
338 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
339
340 for (int n = spans.length - 1; n >= 0; n--) {
341 if (spans[n] instanceof AlignmentSpan) {
342 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
343 break;
344 }
345 }
346 }
347
348 final int length = spans.length;
349 boolean useFirstLineMargin = isFirstParaLine;
350 for (int n = 0; n < length; n++) {
351 if (spans[n] instanceof LeadingMarginSpan2) {
352 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
353 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
354 if (lineNum < startLine + count) {
355 useFirstLineMargin = true;
356 break;
357 }
358 }
359 }
360 for (int n = 0; n < length; n++) {
361 if (spans[n] instanceof LeadingMarginSpan) {
362 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
363 if (dir == DIR_RIGHT_TO_LEFT) {
364 right -= margin.getLeadingMargin(useFirstLineMargin);
365 } else {
366 left += margin.getLeadingMargin(useFirstLineMargin);
367 }
368 }
369 }
370 }
371
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900372 final Alignment align;
373 if (paraAlign == Alignment.ALIGN_LEFT) {
374 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
375 } else if (paraAlign == Alignment.ALIGN_RIGHT) {
376 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
377 } else {
378 align = paraAlign;
379 }
380
381 final int indentWidth;
382 if (align == Alignment.ALIGN_NORMAL) {
383 if (dir == DIR_LEFT_TO_RIGHT) {
384 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
385 } else {
386 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
387 }
388 } else if (align == Alignment.ALIGN_OPPOSITE) {
389 if (dir == DIR_LEFT_TO_RIGHT) {
390 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
391 } else {
392 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
393 }
394 } else { // Alignment.ALIGN_CENTER
395 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
396 }
397
398 return right - left - indentWidth;
399 }
400
Gilles Debunne6c488de2012-03-01 16:20:35 -0800401 /**
402 * @hide
403 */
404 public void drawText(Canvas canvas, int firstLine, int lastLine) {
405 int previousLineBottom = getLineTop(firstLine);
406 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800407 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700408 int spanEnd = 0;
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -0700409 final TextPaint paint = mWorkPaint;
410 paint.set(mPaint);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800411 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700413 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700414 TabStops tabStops = null;
415 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800416
Doug Felte8e45f22010-03-29 14:58:40 -0700417 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700418
Gilles Debunne6c488de2012-03-01 16:20:35 -0800419 // Draw the lines, one at a time.
420 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700421 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700423 previousLineEnd = getLineStart(lineNum + 1);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900424 final boolean justify = isJustificationRequired(lineNum);
Raph Levien26d443a2015-03-30 14:18:32 -0700425 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -0700426 paint.setHyphenEdit(getHyphen(lineNum));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427
428 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700429 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700431 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432
Raph Levien26d443a2015-03-30 14:18:32 -0700433 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700434 int left = 0;
435 int right = mWidth;
436
Gilles Debunne6c488de2012-03-01 16:20:35 -0800437 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700438 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800439 int textLength = buf.length();
440 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700441
Doug Feltc982f602010-05-25 11:51:40 -0700442 // New batch of paragraph styles, collect into spans array.
443 // Compute the alignment, last alignment style wins.
444 // Reset tabStops, we'll rebuild if we encounter a line with
445 // tabs.
446 // We expect paragraph spans to be relatively infrequent, use
447 // spanEnd so that we can check less frequently. Since
448 // paragraph styles ought to apply to entire paragraphs, we can
449 // just collect the ones present at the start of the paragraph.
450 // If spanEnd is before the end of the paragraph, that's not
451 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700452 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700453 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700455 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800456
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700457 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800458 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700460 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 break;
462 }
463 }
Doug Felt0c702b82010-05-14 10:55:42 -0700464
Doug Feltc982f602010-05-25 11:51:40 -0700465 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800467
Doug Feltc982f602010-05-25 11:51:40 -0700468 // Draw all leading margin spans. Adjust left or right according
469 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700471 boolean useFirstLineMargin = isFirstParaLine;
472 for (int n = 0; n < length; n++) {
473 if (spans[n] instanceof LeadingMarginSpan2) {
474 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
475 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
476 // if there is more than one LeadingMarginSpan2, use
477 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700478 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700479 useFirstLineMargin = true;
480 break;
481 }
482 }
483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 for (int n = 0; n < length; n++) {
485 if (spans[n] instanceof LeadingMarginSpan) {
486 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800488 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800490 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700491 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800493 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800495 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700496 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
498 }
499 }
500 }
501
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700502 boolean hasTab = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700503 // Can't tell if we have tabs for sure, currently
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700504 if (hasTab && !tabStopsIsInitialized) {
Doug Feltc982f602010-05-25 11:51:40 -0700505 if (tabStops == null) {
506 tabStops = new TabStops(TAB_INCREMENT, spans);
507 } else {
508 tabStops.reset(TAB_INCREMENT, spans);
509 }
510 tabStopsIsInitialized = true;
511 }
512
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700513 // Determine whether the line aligns to normal, opposite, or center.
514 Alignment align = paraAlign;
515 if (align == Alignment.ALIGN_LEFT) {
516 align = (dir == DIR_LEFT_TO_RIGHT) ?
517 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
518 } else if (align == Alignment.ALIGN_RIGHT) {
519 align = (dir == DIR_LEFT_TO_RIGHT) ?
520 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
521 }
522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 int x;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900524 final int indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 if (align == Alignment.ALIGN_NORMAL) {
526 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900527 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
528 x = left + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900530 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
531 x = right - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700534 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700536 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900537 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
538 x = right - max - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900540 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
541 x = left - max + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
Doug Feltc982f602010-05-25 11:51:40 -0700543 } else { // Alignment.ALIGN_CENTER
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900544 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700545 max = max & ~1;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900546 x = ((right + left - max) >> 1) + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
548 }
549
Raph Levien26d443a2015-03-30 14:18:32 -0700550 Directions directions = getLineDirections(lineNum);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900551 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab && !justify) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800552 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800553 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 } else {
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700555 tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900556 if (justify) {
557 tl.justify(right - left - indentWidth);
558 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800559 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
561 }
Doug Feltc982f602010-05-25 11:51:40 -0700562
Doug Felte8e45f22010-03-29 14:58:40 -0700563 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
566 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800567 * @hide
568 */
569 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
570 int cursorOffsetVertical, int firstLine, int lastLine) {
571 // First, draw LineBackgroundSpans.
572 // LineBackgroundSpans know nothing about the alignment, margins, or
573 // direction of the layout or line. XXX: Should they?
574 // They are evaluated at each line.
575 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700576 if (mLineBackgroundSpans == null) {
577 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700578 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800579
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700580 Spanned buffer = (Spanned) mText;
581 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700582 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800583
Gilles Debunneeca5b732012-04-25 18:48:42 -0700584 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700585 int previousLineBottom = getLineTop(firstLine);
586 int previousLineEnd = getLineStart(firstLine);
587 ParagraphStyle[] spans = NO_PARA_SPANS;
588 int spansLength = 0;
589 TextPaint paint = mPaint;
590 int spanEnd = 0;
591 final int width = mWidth;
592 for (int i = firstLine; i <= lastLine; i++) {
593 int start = previousLineEnd;
594 int end = getLineStart(i + 1);
595 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800596
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700597 int ltop = previousLineBottom;
598 int lbottom = getLineTop(i + 1);
599 previousLineBottom = lbottom;
600 int lbaseline = lbottom - getLineDescent(i);
601
602 if (start >= spanEnd) {
603 // These should be infrequent, so we'll use this so that
604 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700605 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700606 // All LineBackgroundSpans on a line contribute to its background.
607 spansLength = 0;
608 // Duplication of the logic of getParagraphSpans
609 if (start != end || start == 0) {
610 // Equivalent to a getSpans(start, end), but filling the 'spans' local
611 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700612 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
613 // equal test is valid since both intervals are not empty by
614 // construction
615 if (mLineBackgroundSpans.spanStarts[j] >= end ||
616 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500617 spans = GrowingArrayUtils.append(
618 spans, spansLength, mLineBackgroundSpans.spans[j]);
619 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700620 }
621 }
622 }
623
624 for (int n = 0; n < spansLength; n++) {
625 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
626 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
627 ltop, lbaseline, lbottom,
628 buffer, start, end, i);
629 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800630 }
631 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700632 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800633 }
634
635 // There can be a highlight even without spans if we are drawing
636 // a non-spanned transformation of a spanned editing buffer.
637 if (highlight != null) {
638 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
639 canvas.drawPath(highlight, highlightPaint);
640 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
641 }
642 }
643
644 /**
645 * @param canvas
646 * @return The range of lines that need to be drawn, possibly empty.
647 * @hide
648 */
649 public long getLineRangeForDraw(Canvas canvas) {
650 int dtop, dbottom;
651
652 synchronized (sTempRect) {
653 if (!canvas.getClipBounds(sTempRect)) {
654 // Negative range end used as a special flag
655 return TextUtils.packRangeInLong(0, -1);
656 }
657
658 dtop = sTempRect.top;
659 dbottom = sTempRect.bottom;
660 }
661
662 final int top = Math.max(dtop, 0);
663 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
664
Gilles Debunne2fba3382012-06-11 17:46:24 -0700665 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800666 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
667 }
668
669 /**
Doug Feltc982f602010-05-25 11:51:40 -0700670 * Return the start position of the line, given the left and right bounds
671 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700672 *
Doug Feltc982f602010-05-25 11:51:40 -0700673 * @param line the line index
674 * @param left the left bounds (0, or leading margin if ltr para)
675 * @param right the right bounds (width, minus leading margin if rtl para)
676 * @return the start position of the line (to right of line if rtl para)
677 */
678 private int getLineStartPos(int line, int left, int right) {
679 // Adjust the point at which to start rendering depending on the
680 // alignment of the paragraph.
681 Alignment align = getParagraphAlignment(line);
682 int dir = getParagraphDirection(line);
683
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700684 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700685 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
686 } else if (align == Alignment.ALIGN_RIGHT) {
687 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
688 }
689
690 int x;
691 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700692 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700693 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700694 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700695 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700696 }
697 } else {
698 TabStops tabStops = null;
699 if (mSpannedText && getLineContainsTab(line)) {
700 Spanned spanned = (Spanned) mText;
701 int start = getLineStart(line);
702 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
703 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800704 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
705 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700706 if (tabSpans.length > 0) {
707 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
708 }
709 }
710 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700711 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700712 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700713 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700714 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700715 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700716 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700717 }
718 } else { // Alignment.ALIGN_CENTER
719 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700720 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700721 }
722 }
723 return x;
724 }
725
726 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 * Return the text that is displayed by this Layout.
728 */
729 public final CharSequence getText() {
730 return mText;
731 }
732
733 /**
734 * Return the base Paint properties for this layout.
735 * Do NOT change the paint, which may result in funny
736 * drawing for this layout.
737 */
738 public final TextPaint getPaint() {
739 return mPaint;
740 }
741
742 /**
743 * Return the width of this layout.
744 */
745 public final int getWidth() {
746 return mWidth;
747 }
748
749 /**
750 * Return the width to which this Layout is ellipsizing, or
751 * {@link #getWidth} if it is not doing anything special.
752 */
753 public int getEllipsizedWidth() {
754 return mWidth;
755 }
756
757 /**
758 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800759 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 * it does not cause the text to reflow to use the full new width.
761 */
762 public final void increaseWidthTo(int wid) {
763 if (wid < mWidth) {
764 throw new RuntimeException("attempted to reduce Layout width");
765 }
766
767 mWidth = wid;
768 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 /**
771 * Return the total height of this layout.
772 */
773 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800774 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 }
776
777 /**
Siyamed Sinir0745c722016-05-31 20:39:33 -0700778 * Return the total height of this layout.
779 *
780 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
781 *
782 * @hide
783 */
784 public int getHeight(boolean cap) {
785 return getHeight();
786 }
787
788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 * Return the base alignment of this layout.
790 */
791 public final Alignment getAlignment() {
792 return mAlignment;
793 }
794
795 /**
796 * Return what the text height is multiplied by to get the line height.
797 */
798 public final float getSpacingMultiplier() {
799 return mSpacingMult;
800 }
801
802 /**
803 * Return the number of units of leading that are added to each line.
804 */
805 public final float getSpacingAdd() {
806 return mSpacingAdd;
807 }
808
809 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700810 * Return the heuristic used to determine paragraph text direction.
811 * @hide
812 */
813 public final TextDirectionHeuristic getTextDirectionHeuristic() {
814 return mTextDir;
815 }
816
817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 * Return the number of lines of text in this layout.
819 */
820 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 /**
823 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
824 * If bounds is not null, return the top, left, right, bottom extents
825 * of the specified line in it.
826 * @param line which line to examine (0..getLineCount() - 1)
827 * @param bounds Optional. If not null, it returns the extent of the line
828 * @return the Y-coordinate of the baseline
829 */
830 public int getLineBounds(int line, Rect bounds) {
831 if (bounds != null) {
832 bounds.left = 0; // ???
833 bounds.top = getLineTop(line);
834 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800835 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 }
837 return getLineBaseline(line);
838 }
839
840 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800841 * Return the vertical position of the top of the specified line
842 * (0&hellip;getLineCount()).
843 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 * bottom of the last line.
845 */
846 public abstract int getLineTop(int line);
847
848 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800849 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 */
851 public abstract int getLineDescent(int line);
852
853 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800854 * Return the text offset of the beginning of the specified line (
855 * 0&hellip;getLineCount()). If the specified line is equal to the line
856 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 */
858 public abstract int getLineStart(int line);
859
860 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800861 * Returns the primary directionality of the paragraph containing the
862 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
863 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 */
865 public abstract int getParagraphDirection(int line);
866
867 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700868 * Returns whether the specified line contains one or more
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700869 * characters that need to be handled specially, like tabs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 */
871 public abstract boolean getLineContainsTab(int line);
872
873 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800874 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 * The array alternates counts of characters in left-to-right
876 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800877 *
878 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 */
880 public abstract Directions getLineDirections(int line);
881
882 /**
883 * Returns the (negative) number of extra pixels of ascent padding in the
884 * top line of the Layout.
885 */
886 public abstract int getTopPadding();
887
888 /**
889 * Returns the number of extra pixels of descent padding in the
890 * bottom line of the Layout.
891 */
892 public abstract int getBottomPadding();
893
Raph Levien26d443a2015-03-30 14:18:32 -0700894 /**
895 * Returns the hyphen edit for a line.
896 *
897 * @hide
898 */
899 public int getHyphen(int line) {
900 return 0;
901 }
902
Raph Levien2ea52902015-07-01 14:39:31 -0700903 /**
904 * Returns the left indent for a line.
905 *
906 * @hide
907 */
908 public int getIndentAdjust(int line, Alignment alignment) {
909 return 0;
910 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700911
912 /**
913 * Returns true if the character at offset and the preceding character
914 * are at different run levels (and thus there's a split caret).
915 * @param offset the offset
916 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800917 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700918 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800919 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800920 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700921 Directions dirs = getLineDirections(line);
922 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
923 return false;
924 }
925
926 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800927 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700928 int lineEnd = getLineEnd(line);
929 if (offset == lineStart || offset == lineEnd) {
930 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
931 int runIndex = offset == lineStart ? 0 : runs.length - 2;
932 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
933 }
934
935 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800936 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700937 if (offset == runs[i]) {
938 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800939 }
940 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700941 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800942 }
943
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700944 /**
945 * Returns true if the character at offset is right to left (RTL).
946 * @param offset the offset
947 * @return true if the character is RTL, false if it is LTR
948 */
949 public boolean isRtlCharAt(int offset) {
950 int line = getLineForOffset(offset);
951 Directions dirs = getLineDirections(line);
952 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
953 return false;
954 }
955 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
956 return true;
957 }
958 int[] runs = dirs.mDirections;
959 int lineStart = getLineStart(line);
960 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700961 int start = lineStart + runs[i];
962 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
963 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700964 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
965 return ((level & 1) != 0);
966 }
967 }
968 // Should happen only if the offset is "out of bounds"
969 return false;
970 }
971
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900972 /**
973 * Returns the range of the run that the character at offset belongs to.
974 * @param offset the offset
975 * @return The range of the run
976 * @hide
977 */
978 public long getRunRange(int offset) {
979 int line = getLineForOffset(offset);
980 Directions dirs = getLineDirections(line);
981 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
982 return TextUtils.packRangeInLong(0, getLineEnd(line));
983 }
984 int[] runs = dirs.mDirections;
985 int lineStart = getLineStart(line);
986 for (int i = 0; i < runs.length; i += 2) {
987 int start = lineStart + runs[i];
988 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
989 if (offset >= start && offset < limit) {
990 return TextUtils.packRangeInLong(start, limit);
991 }
992 }
993 // Should happen only if the offset is "out of bounds"
994 return TextUtils.packRangeInLong(0, getLineEnd(line));
995 }
996
Doug Felt9f7a4442010-03-01 12:45:56 -0800997 private boolean primaryIsTrailingPrevious(int offset) {
998 int line = getLineForOffset(offset);
999 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -07001000 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001001 int[] runs = getLineDirections(line).mDirections;
1002
1003 int levelAt = -1;
1004 for (int i = 0; i < runs.length; i += 2) {
1005 int start = lineStart + runs[i];
1006 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
1007 if (limit > lineEnd) {
1008 limit = lineEnd;
1009 }
1010 if (offset >= start && offset < limit) {
1011 if (offset > start) {
1012 // Previous character is at same level, so don't use trailing.
1013 return false;
1014 }
1015 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1016 break;
1017 }
1018 }
1019 if (levelAt == -1) {
1020 // Offset was limit of line.
1021 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
1022 }
1023
1024 // At level boundary, check previous level.
1025 int levelBefore = -1;
1026 if (offset == lineStart) {
1027 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
1028 } else {
1029 offset -= 1;
1030 for (int i = 0; i < runs.length; i += 2) {
1031 int start = lineStart + runs[i];
1032 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
1033 if (limit > lineEnd) {
1034 limit = lineEnd;
1035 }
1036 if (offset >= start && offset < limit) {
1037 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1038 break;
1039 }
1040 }
1041 }
1042
1043 return levelBefore < levelAt;
1044 }
1045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * Get the primary horizontal position for the specified text offset.
1048 * This is the location where a new character would be inserted in
1049 * the paragraph's primary direction.
1050 */
1051 public float getPrimaryHorizontal(int offset) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001052 return getPrimaryHorizontal(offset, false /* not clamped */);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001053 }
1054
1055 /**
1056 * Get the primary horizontal position for the specified text offset, but
1057 * optionally clamp it so that it doesn't exceed the width of the layout.
1058 * @hide
1059 */
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001060 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001061 boolean trailing = primaryIsTrailingPrevious(offset);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001062 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064
1065 /**
1066 * Get the secondary horizontal position for the specified text offset.
1067 * This is the location where a new character would be inserted in
1068 * the direction other than the paragraph's primary direction.
1069 */
1070 public float getSecondaryHorizontal(int offset) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001071 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073
Raph Levienafe8e9b2012-12-19 16:09:32 -08001074 /**
1075 * Get the secondary horizontal position for the specified text offset, but
1076 * optionally clamp it so that it doesn't exceed the width of the layout.
1077 * @hide
1078 */
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001079 public float getSecondaryHorizontal(int offset, boolean clamped) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001080 boolean trailing = primaryIsTrailingPrevious(offset);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001081 return getHorizontal(offset, !trailing, clamped);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001082 }
1083
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001084 private float getHorizontal(int offset, boolean primary) {
1085 return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset);
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001086 }
1087
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001088 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
1089 int line = getLineForOffset(offset);
1090
Raph Levienafe8e9b2012-12-19 16:09:32 -08001091 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093
Raph Levienafe8e9b2012-12-19 16:09:32 -08001094 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001096 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 int dir = getParagraphDirection(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001098 boolean hasTab = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 Directions directions = getLineDirections(line);
1100
Doug Feltc982f602010-05-25 11:51:40 -07001101 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001102 if (hasTab && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001103 // Just checking this line should be good enough, tabs should be
1104 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001105 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001106 if (tabs.length > 0) {
1107 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
1110
Doug Felte8e45f22010-03-29 14:58:40 -07001111 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001112 tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001113 float wid = tl.measure(offset - start, trailing, null);
1114 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115
Raph Levienafe8e9b2012-12-19 16:09:32 -08001116 if (clamped && wid > mWidth) {
1117 wid = mWidth;
1118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 int left = getParagraphLeft(line);
1120 int right = getParagraphRight(line);
1121
Doug Feltc982f602010-05-25 11:51:40 -07001122 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 }
1124
1125 /**
1126 * Get the leftmost position that should be exposed for horizontal
1127 * scrolling on the specified line.
1128 */
1129 public float getLineLeft(int line) {
1130 int dir = getParagraphDirection(line);
1131 Alignment align = getParagraphAlignment(line);
1132
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001133 if (align == Alignment.ALIGN_LEFT) {
1134 return 0;
1135 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 if (dir == DIR_RIGHT_TO_LEFT)
1137 return getParagraphRight(line) - getLineMax(line);
1138 else
1139 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001140 } else if (align == Alignment.ALIGN_RIGHT) {
1141 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 } else if (align == Alignment.ALIGN_OPPOSITE) {
1143 if (dir == DIR_RIGHT_TO_LEFT)
1144 return 0;
1145 else
1146 return mWidth - getLineMax(line);
1147 } else { /* align == Alignment.ALIGN_CENTER */
1148 int left = getParagraphLeft(line);
1149 int right = getParagraphRight(line);
1150 int max = ((int) getLineMax(line)) & ~1;
1151
1152 return left + ((right - left) - max) / 2;
1153 }
1154 }
1155
1156 /**
1157 * Get the rightmost position that should be exposed for horizontal
1158 * scrolling on the specified line.
1159 */
1160 public float getLineRight(int line) {
1161 int dir = getParagraphDirection(line);
1162 Alignment align = getParagraphAlignment(line);
1163
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001164 if (align == Alignment.ALIGN_LEFT) {
1165 return getParagraphLeft(line) + getLineMax(line);
1166 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 if (dir == DIR_RIGHT_TO_LEFT)
1168 return mWidth;
1169 else
1170 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001171 } else if (align == Alignment.ALIGN_RIGHT) {
1172 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 } else if (align == Alignment.ALIGN_OPPOSITE) {
1174 if (dir == DIR_RIGHT_TO_LEFT)
1175 return getLineMax(line);
1176 else
1177 return mWidth;
1178 } else { /* align == Alignment.ALIGN_CENTER */
1179 int left = getParagraphLeft(line);
1180 int right = getParagraphRight(line);
1181 int max = ((int) getLineMax(line)) & ~1;
1182
1183 return right - ((right - left) - max) / 2;
1184 }
1185 }
1186
1187 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001188 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001189 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 */
1191 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001192 float margin = getParagraphLeadingMargin(line);
1193 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001194 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 }
1196
1197 /**
Doug Feltc982f602010-05-25 11:51:40 -07001198 * Gets the unsigned horizontal extent of the specified line, including
1199 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 */
1201 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001202 float margin = getParagraphLeadingMargin(line);
1203 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001204 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206
Doug Feltc982f602010-05-25 11:51:40 -07001207 /**
1208 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1209 * tab stops instead of using the ones passed in.
1210 * @param line the index of the line
1211 * @param full whether to include trailing whitespace
1212 * @return the extent of the line
1213 */
1214 private float getLineExtent(int line, boolean full) {
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001215 final int start = getLineStart(line);
1216 final int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001217
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001218 final boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001219 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001220 if (hasTabs && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001221 // Just checking this line should be good enough, tabs should be
1222 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001223 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001224 if (tabs.length > 0) {
1225 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1226 }
1227 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001228 final Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001229 // Returned directions can actually be null
1230 if (directions == null) {
1231 return 0f;
1232 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001233 final int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001235 final TextLine tl = TextLine.obtain();
1236 final TextPaint paint = mWorkPaint;
1237 paint.set(mPaint);
1238 paint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader53aba292017-08-25 15:53:33 -07001239 tl.set(paint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001240 if (isJustificationRequired(line)) {
1241 tl.justify(getJustifyWidth(line));
1242 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001243 final float width = tl.metrics(null);
Doug Feltc982f602010-05-25 11:51:40 -07001244 TextLine.recycle(tl);
1245 return width;
1246 }
1247
1248 /**
1249 * Returns the signed horizontal extent of the specified line, excluding
1250 * leading margin. If full is false, excludes trailing whitespace.
1251 * @param line the index of the line
1252 * @param tabStops the tab stops, can be null if we know they're not used.
1253 * @param full whether to include trailing whitespace
1254 * @return the extent of the text on this line
1255 */
1256 private float getLineExtent(int line, TabStops tabStops, boolean full) {
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001257 final int start = getLineStart(line);
1258 final int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
1259 final boolean hasTabs = getLineContainsTab(line);
1260 final Directions directions = getLineDirections(line);
1261 final int dir = getParagraphDirection(line);
Doug Feltc982f602010-05-25 11:51:40 -07001262
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001263 final TextLine tl = TextLine.obtain();
1264 final TextPaint paint = mWorkPaint;
1265 paint.set(mPaint);
1266 paint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader53aba292017-08-25 15:53:33 -07001267 tl.set(paint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001268 if (isJustificationRequired(line)) {
1269 tl.justify(getJustifyWidth(line));
1270 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001271 final float width = tl.metrics(null);
Doug Felte8e45f22010-03-29 14:58:40 -07001272 TextLine.recycle(tl);
1273 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
1275
1276 /**
1277 * Get the line number corresponding to the specified vertical position.
1278 * If you ask for a position above 0, you get 0; if you ask for a position
1279 * below the bottom of the text, you get the last line.
1280 */
1281 // FIXME: It may be faster to do a linear search for layouts without many lines.
1282 public int getLineForVertical(int vertical) {
1283 int high = getLineCount(), low = -1, guess;
1284
1285 while (high - low > 1) {
1286 guess = (high + low) / 2;
1287
1288 if (getLineTop(guess) > vertical)
1289 high = guess;
1290 else
1291 low = guess;
1292 }
1293
1294 if (low < 0)
1295 return 0;
1296 else
1297 return low;
1298 }
1299
1300 /**
1301 * Get the line number on which the specified text offset appears.
1302 * If you ask for a position before 0, you get 0; if you ask for a position
1303 * beyond the end of the text, you get the last line.
1304 */
1305 public int getLineForOffset(int offset) {
1306 int high = getLineCount(), low = -1, guess;
1307
1308 while (high - low > 1) {
1309 guess = (high + low) / 2;
1310
1311 if (getLineStart(guess) > offset)
1312 high = guess;
1313 else
1314 low = guess;
1315 }
1316
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001317 if (low < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 return 0;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001319 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 return low;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 }
1323
1324 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001325 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 * closest to the specified horizontal position.
1327 */
1328 public int getOffsetForHorizontal(int line, float horiz) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001329 return getOffsetForHorizontal(line, horiz, true);
1330 }
1331
1332 /**
1333 * Get the character offset on the specified line whose position is
1334 * closest to the specified horizontal position.
1335 *
1336 * @param line the line used to find the closest offset
1337 * @param horiz the horizontal position used to find the closest offset
1338 * @param primary whether to use the primary position or secondary position to find the offset
1339 *
1340 * @hide
1341 */
1342 public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
Raph Levienedb27f12015-06-01 14:34:47 -07001343 // TODO: use Paint.getOffsetForAdvance to avoid binary search
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001344 final int lineEndOffset = getLineEnd(line);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001345 final int lineStartOffset = getLineStart(line);
1346
1347 Directions dirs = getLineDirections(line);
1348
1349 TextLine tl = TextLine.obtain();
1350 // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
1351 tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
1352 false, null);
1353
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001354 final int max;
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001355 if (line == getLineCount() - 1) {
1356 max = lineEndOffset;
1357 } else {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001358 max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
1359 !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001360 }
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001361 int best = lineStartOffset;
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001362 float bestdist = Math.abs(getHorizontal(best, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
Doug Felt9f7a4442010-03-01 12:45:56 -08001364 for (int i = 0; i < dirs.mDirections.length; i += 2) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001365 int here = lineStartOffset + dirs.mDirections[i];
Doug Felt9f7a4442010-03-01 12:45:56 -08001366 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001367 boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
1368 int swap = isRtl ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369
1370 if (there > max)
1371 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 int high = there - 1 + 1, low = here + 1 - 1, guess;
1373
1374 while (high - low > 1) {
1375 guess = (high + low) / 2;
1376 int adguess = getOffsetAtStartOf(guess);
1377
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001378 if (getHorizontal(adguess, primary) * swap >= horiz * swap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 high = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001380 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 low = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 }
1384
1385 if (low < here + 1)
1386 low = here + 1;
1387
1388 if (low < there) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001389 int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
1390 low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
1391 if (low >= here && low < there) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001392 float dist = Math.abs(getHorizontal(low, primary) - horiz);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001393 if (aft < there) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001394 float other = Math.abs(getHorizontal(aft, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001396 if (other < dist) {
1397 dist = other;
1398 low = aft;
1399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001402 if (dist < bestdist) {
1403 bestdist = dist;
1404 best = low;
1405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407 }
1408
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001409 float dist = Math.abs(getHorizontal(here, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410
1411 if (dist < bestdist) {
1412 bestdist = dist;
1413 best = here;
1414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001417 float dist = Math.abs(getHorizontal(max, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418
Raph Levien373b7a82013-09-20 15:11:52 -07001419 if (dist <= bestdist) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 best = max;
1421 }
1422
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001423 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 return best;
1425 }
1426
1427 /**
1428 * Return the text offset after the last character on the specified line.
1429 */
1430 public final int getLineEnd(int line) {
1431 return getLineStart(line + 1);
1432 }
1433
Doug Felt9f7a4442010-03-01 12:45:56 -08001434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 * Return the text offset after the last visible character (so whitespace
1436 * is not counted) on the specified line.
1437 */
1438 public int getLineVisibleEnd(int line) {
1439 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1440 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 CharSequence text = mText;
1444 char ch;
1445 if (line == getLineCount() - 1) {
1446 return end;
1447 }
1448
1449 for (; end > start; end--) {
1450 ch = text.charAt(end - 1);
1451
1452 if (ch == '\n') {
1453 return end - 1;
1454 }
1455
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001456 if (!TextLine.isLineEndSpace(ch)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 break;
1458 }
1459
1460 }
1461
1462 return end;
1463 }
1464
1465 /**
1466 * Return the vertical position of the bottom of the specified line.
1467 */
1468 public final int getLineBottom(int line) {
1469 return getLineTop(line + 1);
1470 }
1471
1472 /**
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001473 * Return the vertical position of the bottom of the specified line without the line spacing
1474 * added.
1475 *
1476 * @hide
1477 */
1478 public final int getLineBottomWithoutSpacing(int line) {
1479 return getLineTop(line + 1) - getLineExtra(line);
1480 }
1481
1482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 * Return the vertical position of the baseline of the specified line.
1484 */
1485 public final int getLineBaseline(int line) {
1486 // getLineTop(line+1) == getLineTop(line)
1487 return getLineTop(line+1) - getLineDescent(line);
1488 }
1489
1490 /**
1491 * Get the ascent of the text on the specified line.
1492 * The return value is negative to match the Paint.ascent() convention.
1493 */
1494 public final int getLineAscent(int line) {
1495 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1496 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1497 }
1498
Siyamed Sinir0fa89d62017-07-24 20:46:41 -07001499 /**
1500 * Return the extra space added as a result of line spacing attributes
1501 * {@link #getSpacingAdd()} and {@link #getSpacingMultiplier()}. Default value is {@code zero}.
1502 *
1503 * @param line the index of the line, the value should be equal or greater than {@code zero}
1504 * @hide
1505 */
1506 public int getLineExtra(@IntRange(from = 0) int line) {
1507 return 0;
1508 }
1509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001511 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
1513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001515 return getOffsetToLeftRightOf(offset, false);
1516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517
Doug Felt9f7a4442010-03-01 12:45:56 -08001518 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1519 int line = getLineForOffset(caret);
1520 int lineStart = getLineStart(line);
1521 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001522 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001524 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001525 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001526 // if walking off line, look at the line we're headed to
1527 if (advance) {
1528 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001529 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001530 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001531 ++line;
1532 } else {
1533 return caret; // at very end, don't move
1534 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001535 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001536 } else {
1537 if (caret == lineStart) {
1538 if (line > 0) {
1539 lineChanged = true;
1540 --line;
1541 } else {
1542 return caret; // at very start, don't move
1543 }
1544 }
1545 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001546
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001547 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001548 lineStart = getLineStart(line);
1549 lineEnd = getLineEnd(line);
1550 int newDir = getParagraphDirection(line);
1551 if (newDir != lineDir) {
1552 // unusual case. we want to walk onto the line, but it runs
1553 // in a different direction than this one, so we fake movement
1554 // in the opposite direction.
1555 toLeft = !toLeft;
1556 lineDir = newDir;
1557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001559
Doug Felte8e45f22010-03-29 14:58:40 -07001560 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001561
Doug Felte8e45f22010-03-29 14:58:40 -07001562 TextLine tl = TextLine.obtain();
1563 // XXX: we don't care about tabs
1564 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1565 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
Siyamed Sinira273a702017-10-05 11:22:12 -07001566 TextLine.recycle(tl);
Doug Felte8e45f22010-03-29 14:58:40 -07001567 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
1569
1570 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001571 // XXX this probably should skip local reorderings and
1572 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 if (offset == 0)
1574 return 0;
1575
1576 CharSequence text = mText;
1577 char c = text.charAt(offset);
1578
1579 if (c >= '\uDC00' && c <= '\uDFFF') {
1580 char c1 = text.charAt(offset - 1);
1581
1582 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1583 offset -= 1;
1584 }
1585
1586 if (mSpannedText) {
1587 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1588 ReplacementSpan.class);
1589
1590 for (int i = 0; i < spans.length; i++) {
1591 int start = ((Spanned) text).getSpanStart(spans[i]);
1592 int end = ((Spanned) text).getSpanEnd(spans[i]);
1593
1594 if (start < offset && end > offset)
1595 offset = start;
1596 }
1597 }
1598
1599 return offset;
1600 }
1601
1602 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001603 * Determine whether we should clamp cursor position. Currently it's
1604 * only robust for left-aligned displays.
1605 * @hide
1606 */
1607 public boolean shouldClampCursor(int line) {
1608 // Only clamp cursor position in left-aligned displays.
1609 switch (getParagraphAlignment(line)) {
1610 case ALIGN_LEFT:
1611 return true;
1612 case ALIGN_NORMAL:
1613 return getParagraphDirection(line) > 0;
1614 default:
1615 return false;
1616 }
1617
1618 }
1619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 * Fills in the specified Path with a representation of a cursor
1621 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001622 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 * directionalities.
1624 */
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001625 public void getCursorPath(final int point, final Path dest, final CharSequence editingBuffer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 dest.reset();
1627
1628 int line = getLineForOffset(point);
1629 int top = getLineTop(line);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001630 int bottom = getLineBottomWithoutSpacing(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631
Raph Levienafe8e9b2012-12-19 16:09:32 -08001632 boolean clamped = shouldClampCursor(line);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001633 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1634 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635
Jeff Brown497a92c2010-09-12 17:55:08 -07001636 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1637 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1638 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 int dist = 0;
1640
1641 if (caps != 0 || fn != 0) {
1642 dist = (bottom - top) >> 2;
1643
1644 if (fn != 0)
1645 top += dist;
1646 if (caps != 0)
1647 bottom -= dist;
1648 }
1649
1650 if (h1 < 0.5f)
1651 h1 = 0.5f;
1652 if (h2 < 0.5f)
1653 h2 = 0.5f;
1654
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001655 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 dest.moveTo(h1, top);
1657 dest.lineTo(h1, bottom);
1658 } else {
1659 dest.moveTo(h1, top);
1660 dest.lineTo(h1, (top + bottom) >> 1);
1661
1662 dest.moveTo(h2, (top + bottom) >> 1);
1663 dest.lineTo(h2, bottom);
1664 }
1665
1666 if (caps == 2) {
1667 dest.moveTo(h2, bottom);
1668 dest.lineTo(h2 - dist, bottom + dist);
1669 dest.lineTo(h2, bottom);
1670 dest.lineTo(h2 + dist, bottom + dist);
1671 } else if (caps == 1) {
1672 dest.moveTo(h2, bottom);
1673 dest.lineTo(h2 - dist, bottom + dist);
1674
1675 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1676 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1677
1678 dest.moveTo(h2 + dist, bottom + dist);
1679 dest.lineTo(h2, bottom);
1680 }
1681
1682 if (fn == 2) {
1683 dest.moveTo(h1, top);
1684 dest.lineTo(h1 - dist, top - dist);
1685 dest.lineTo(h1, top);
1686 dest.lineTo(h1 + dist, top - dist);
1687 } else if (fn == 1) {
1688 dest.moveTo(h1, top);
1689 dest.lineTo(h1 - dist, top - dist);
1690
1691 dest.moveTo(h1 - dist, top - dist + 0.5f);
1692 dest.lineTo(h1 + dist, top - dist + 0.5f);
1693
1694 dest.moveTo(h1 + dist, top - dist);
1695 dest.lineTo(h1, top);
1696 }
1697 }
1698
1699 private void addSelection(int line, int start, int end,
Petar Šeginab92c5392017-09-06 15:25:05 +01001700 int top, int bottom, SelectionRectangleConsumer consumer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 int linestart = getLineStart(line);
1702 int lineend = getLineEnd(line);
1703 Directions dirs = getLineDirections(line);
1704
Petar Šeginafb748b32017-08-07 12:37:52 +01001705 if (lineend > linestart && mText.charAt(lineend - 1) == '\n') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 lineend--;
Petar Šeginafb748b32017-08-07 12:37:52 +01001707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708
Doug Felt9f7a4442010-03-01 12:45:56 -08001709 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1710 int here = linestart + dirs.mDirections[i];
Petar Šeginafb748b32017-08-07 12:37:52 +01001711 int there = here + (dirs.mDirections[i + 1] & RUN_LENGTH_MASK);
Doug Felt9f7a4442010-03-01 12:45:56 -08001712
Petar Šeginafb748b32017-08-07 12:37:52 +01001713 if (there > lineend) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 there = lineend;
Petar Šeginafb748b32017-08-07 12:37:52 +01001715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716
1717 if (start <= there && end >= here) {
1718 int st = Math.max(start, here);
1719 int en = Math.min(end, there);
1720
1721 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001722 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1723 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001725 float left = Math.min(h1, h2);
1726 float right = Math.max(h1, h2);
1727
Petar Šegina8f1f74e2017-09-07 14:26:28 +01001728 final @TextSelectionLayout int layout =
1729 ((dirs.mDirections[i + 1] & RUN_RTL_FLAG) != 0)
1730 ? TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT
1731 : TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT;
1732
1733 consumer.accept(left, top, right, bottom, layout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 }
1735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
1737 }
1738
1739 /**
1740 * Fills in the specified Path with a representation of a highlight
1741 * between the specified offsets. This will often be a rectangle
1742 * or a potentially discontinuous set of rectangles. If the start
1743 * and end are the same, the returned path is empty.
1744 */
1745 public void getSelectionPath(int start, int end, Path dest) {
1746 dest.reset();
Petar Šeginab92c5392017-09-06 15:25:05 +01001747 getSelection(start, end, (left, top, right, bottom, textSelectionLayout) ->
Petar Šeginafb748b32017-08-07 12:37:52 +01001748 dest.addRect(left, top, right, bottom, Path.Direction.CW));
1749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750
Petar Šeginafb748b32017-08-07 12:37:52 +01001751 /**
1752 * Calculates the rectangles which should be highlighted to indicate a selection between start
Petar Šeginab92c5392017-09-06 15:25:05 +01001753 * and end and feeds them into the given {@link SelectionRectangleConsumer}.
Petar Šeginafb748b32017-08-07 12:37:52 +01001754 *
1755 * @param start the starting index of the selection
1756 * @param end the ending index of the selection
Petar Šeginab92c5392017-09-06 15:25:05 +01001757 * @param consumer the {@link SelectionRectangleConsumer} which will receive the generated
1758 * rectangles. It will be called every time a rectangle is generated.
Petar Šeginafb748b32017-08-07 12:37:52 +01001759 * @hide
1760 * @see #getSelectionPath(int, int, Path)
1761 */
Petar Šeginab92c5392017-09-06 15:25:05 +01001762 public final void getSelection(int start, int end, final SelectionRectangleConsumer consumer) {
Petar Šeginafb748b32017-08-07 12:37:52 +01001763 if (start == end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 return;
Petar Šeginafb748b32017-08-07 12:37:52 +01001765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766
1767 if (end < start) {
1768 int temp = end;
1769 end = start;
1770 start = temp;
1771 }
1772
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001773 final int startline = getLineForOffset(start);
1774 final int endline = getLineForOffset(end);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 int top = getLineTop(startline);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001777 int bottom = getLineBottomWithoutSpacing(endline);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778
1779 if (startline == endline) {
Petar Šeginafb748b32017-08-07 12:37:52 +01001780 addSelection(startline, start, end, top, bottom, consumer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 } else {
1782 final float width = mWidth;
1783
1784 addSelection(startline, start, getLineEnd(startline),
Petar Šeginafb748b32017-08-07 12:37:52 +01001785 top, getLineBottom(startline), consumer);
Doug Felt9f7a4442010-03-01 12:45:56 -08001786
Petar Šeginafb748b32017-08-07 12:37:52 +01001787 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT) {
Petar Šeginab92c5392017-09-06 15:25:05 +01001788 consumer.accept(getLineLeft(startline), top, 0, getLineBottom(startline),
Petar Šegina3a92fb62017-09-07 21:03:24 +01001789 TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT);
Petar Šeginafb748b32017-08-07 12:37:52 +01001790 } else {
Petar Šeginab92c5392017-09-06 15:25:05 +01001791 consumer.accept(getLineRight(startline), top, width, getLineBottom(startline),
Petar Šegina3a92fb62017-09-07 21:03:24 +01001792 TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT);
Petar Šeginafb748b32017-08-07 12:37:52 +01001793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794
1795 for (int i = startline + 1; i < endline; i++) {
1796 top = getLineTop(i);
1797 bottom = getLineBottom(i);
Petar Šeginab92c5392017-09-06 15:25:05 +01001798 if (getParagraphDirection(i) == DIR_RIGHT_TO_LEFT) {
Petar Šegina3a92fb62017-09-07 21:03:24 +01001799 consumer.accept(0, top, width, bottom, TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT);
Petar Šeginab92c5392017-09-06 15:25:05 +01001800 } else {
Petar Šegina3a92fb62017-09-07 21:03:24 +01001801 consumer.accept(0, top, width, bottom, TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT);
Petar Šeginab92c5392017-09-06 15:25:05 +01001802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804
1805 top = getLineTop(endline);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001806 bottom = getLineBottomWithoutSpacing(endline);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807
Petar Šeginafb748b32017-08-07 12:37:52 +01001808 addSelection(endline, getLineStart(endline), end, top, bottom, consumer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809
Petar Šeginafb748b32017-08-07 12:37:52 +01001810 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT) {
Petar Šeginab92c5392017-09-06 15:25:05 +01001811 consumer.accept(width, top, getLineRight(endline), bottom,
Petar Šegina3a92fb62017-09-07 21:03:24 +01001812 TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT);
Petar Šeginafb748b32017-08-07 12:37:52 +01001813 } else {
Petar Šeginab92c5392017-09-06 15:25:05 +01001814 consumer.accept(0, top, getLineLeft(endline), bottom,
Petar Šegina3a92fb62017-09-07 21:03:24 +01001815 TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT);
Petar Šeginafb748b32017-08-07 12:37:52 +01001816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
1818 }
1819
1820 /**
1821 * Get the alignment of the specified paragraph, taking into account
1822 * markup attached to it.
1823 */
1824 public final Alignment getParagraphAlignment(int line) {
1825 Alignment align = mAlignment;
1826
1827 if (mSpannedText) {
1828 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001829 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 getLineEnd(line),
1831 AlignmentSpan.class);
1832
1833 int spanLength = spans.length;
1834 if (spanLength > 0) {
1835 align = spans[spanLength-1].getAlignment();
1836 }
1837 }
1838
1839 return align;
1840 }
1841
1842 /**
1843 * Get the left edge of the specified paragraph, inset by left margins.
1844 */
1845 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001847 int dir = getParagraphDirection(line);
1848 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1849 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
Doug Feltc982f602010-05-25 11:51:40 -07001851 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
1853
1854 /**
1855 * Get the right edge of the specified paragraph, inset by right margins.
1856 */
1857 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001859 int dir = getParagraphDirection(line);
1860 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1861 return right; // leading margin has no impact, or no styles
1862 }
1863 return right - getParagraphLeadingMargin(line);
1864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865
Doug Feltc982f602010-05-25 11:51:40 -07001866 /**
1867 * Returns the effective leading margin (unsigned) for this line,
1868 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1869 * @param line the line index
1870 * @return the leading margin of this line
1871 */
1872 private int getParagraphLeadingMargin(int line) {
1873 if (!mSpannedText) {
1874 return 0;
1875 }
1876 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001877
Doug Feltc982f602010-05-25 11:51:40 -07001878 int lineStart = getLineStart(line);
1879 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001880 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001881 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001882 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001883 LeadingMarginSpan.class);
1884 if (spans.length == 0) {
1885 return 0; // no leading margin span;
1886 }
Doug Felt0c702b82010-05-14 10:55:42 -07001887
Doug Feltc982f602010-05-25 11:51:40 -07001888 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001889
Siyamed Sinira273a702017-10-05 11:22:12 -07001890 boolean useFirstLineMargin = lineStart == 0 || spanned.charAt(lineStart - 1) == '\n';
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001891 for (int i = 0; i < spans.length; i++) {
1892 if (spans[i] instanceof LeadingMarginSpan2) {
1893 int spStart = spanned.getSpanStart(spans[i]);
1894 int spanLine = getLineForOffset(spStart);
1895 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1896 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1897 useFirstLineMargin |= line < spanLine + count;
1898 }
1899 }
Doug Feltc982f602010-05-25 11:51:40 -07001900 for (int i = 0; i < spans.length; i++) {
1901 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001902 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 }
1904
Doug Feltc982f602010-05-25 11:51:40 -07001905 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 }
1907
Roozbeh Pournader9a9179d2017-08-29 14:14:28 -07001908 private static float measurePara(TextPaint paint, CharSequence text, int start, int end,
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001909 TextDirectionHeuristic textDir) {
Seigo Nonakaf1644f72017-11-27 22:09:49 -08001910 MeasuredText mt = null;
Doug Felte8e45f22010-03-29 14:58:40 -07001911 TextLine tl = TextLine.obtain();
1912 try {
Seigo Nonakaf1644f72017-11-27 22:09:49 -08001913 mt = MeasuredText.buildForBidi(text, start, end, textDir, mt);
1914 final char[] chars = mt.getChars();
1915 final int len = chars.length;
1916 final Directions directions = mt.getDirections(0, len);
1917 final int dir = mt.getParagraphDir();
Doug Feltc982f602010-05-25 11:51:40 -07001918 boolean hasTabs = false;
1919 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001920 // leading margins should be taken into account when measuring a paragraph
1921 int margin = 0;
1922 if (text instanceof Spanned) {
1923 Spanned spanned = (Spanned) text;
1924 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1925 LeadingMarginSpan.class);
1926 for (LeadingMarginSpan lms : spans) {
1927 margin += lms.getLeadingMargin(true);
1928 }
1929 }
Doug Feltc982f602010-05-25 11:51:40 -07001930 for (int i = 0; i < len; ++i) {
1931 if (chars[i] == '\t') {
1932 hasTabs = true;
1933 if (text instanceof Spanned) {
1934 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001935 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001936 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001937 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001938 TabStopSpan.class);
1939 if (spans.length > 0) {
1940 tabStops = new TabStops(TAB_INCREMENT, spans);
1941 }
1942 }
1943 break;
1944 }
1945 }
1946 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001947 return margin + Math.abs(tl.metrics(null));
Doug Felte8e45f22010-03-29 14:58:40 -07001948 } finally {
1949 TextLine.recycle(tl);
Seigo Nonakaf1644f72017-11-27 22:09:49 -08001950 if (mt != null) {
1951 mt.recycle();
1952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
1955
Doug Felt71b8dd72010-02-16 17:27:09 -08001956 /**
Doug Feltc982f602010-05-25 11:51:40 -07001957 * @hide
1958 */
1959 /* package */ static class TabStops {
1960 private int[] mStops;
1961 private int mNumStops;
1962 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001963
Doug Feltc982f602010-05-25 11:51:40 -07001964 TabStops(int increment, Object[] spans) {
1965 reset(increment, spans);
1966 }
Doug Felt0c702b82010-05-14 10:55:42 -07001967
Doug Feltc982f602010-05-25 11:51:40 -07001968 void reset(int increment, Object[] spans) {
1969 this.mIncrement = increment;
1970
1971 int ns = 0;
1972 if (spans != null) {
1973 int[] stops = this.mStops;
1974 for (Object o : spans) {
1975 if (o instanceof TabStopSpan) {
1976 if (stops == null) {
1977 stops = new int[10];
1978 } else if (ns == stops.length) {
1979 int[] nstops = new int[ns * 2];
1980 for (int i = 0; i < ns; ++i) {
1981 nstops[i] = stops[i];
1982 }
1983 stops = nstops;
1984 }
1985 stops[ns++] = ((TabStopSpan) o).getTabStop();
1986 }
1987 }
1988 if (ns > 1) {
1989 Arrays.sort(stops, 0, ns);
1990 }
1991 if (stops != this.mStops) {
1992 this.mStops = stops;
1993 }
1994 }
1995 this.mNumStops = ns;
1996 }
Doug Felt0c702b82010-05-14 10:55:42 -07001997
Doug Feltc982f602010-05-25 11:51:40 -07001998 float nextTab(float h) {
1999 int ns = this.mNumStops;
2000 if (ns > 0) {
2001 int[] stops = this.mStops;
2002 for (int i = 0; i < ns; ++i) {
2003 int stop = stops[i];
2004 if (stop > h) {
2005 return stop;
2006 }
2007 }
2008 }
2009 return nextDefaultStop(h, mIncrement);
2010 }
2011
2012 public static float nextDefaultStop(float h, int inc) {
2013 return ((int) ((h + inc) / inc)) * inc;
2014 }
2015 }
Doug Felt0c702b82010-05-14 10:55:42 -07002016
Doug Feltc982f602010-05-25 11:51:40 -07002017 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08002018 * Returns the position of the next tab stop after h on the line.
2019 *
2020 * @param text the text
2021 * @param start start of the line
2022 * @param end limit of the line
2023 * @param h the current horizontal offset
2024 * @param tabs the tabs, can be null. If it is null, any tabs in effect
2025 * on the line will be used. If there are no tabs, a default offset
2026 * will be used to compute the tab stop.
2027 * @return the offset of the next tab stop.
2028 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 /* package */ static float nextTab(CharSequence text, int start, int end,
2030 float h, Object[] tabs) {
2031 float nh = Float.MAX_VALUE;
2032 boolean alltabs = false;
2033
2034 if (text instanceof Spanned) {
2035 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002036 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 alltabs = true;
2038 }
2039
2040 for (int i = 0; i < tabs.length; i++) {
2041 if (!alltabs) {
2042 if (!(tabs[i] instanceof TabStopSpan))
2043 continue;
2044 }
2045
2046 int where = ((TabStopSpan) tabs[i]).getTabStop();
2047
2048 if (where < nh && where > h)
2049 nh = where;
2050 }
2051
2052 if (nh != Float.MAX_VALUE)
2053 return nh;
2054 }
2055
2056 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
2057 }
2058
2059 protected final boolean isSpanned() {
2060 return mSpannedText;
2061 }
2062
Eric Fischer74d31ef2010-08-05 15:29:36 -07002063 /**
2064 * Returns the same as <code>text.getSpans()</code>, except where
2065 * <code>start</code> and <code>end</code> are the same and are not
2066 * at the very beginning of the text, in which case an empty array
2067 * is returned instead.
2068 * <p>
2069 * This is needed because of the special case that <code>getSpans()</code>
2070 * on an empty range returns the spans adjacent to that range, which is
2071 * primarily for the sake of <code>TextWatchers</code> so they will get
2072 * notifications when text goes from empty to non-empty. But it also
2073 * has the unfortunate side effect that if the text ends with an empty
2074 * paragraph, that paragraph accidentally picks up the styles of the
2075 * preceding paragraph (even though those styles will not be picked up
2076 * by new text that is inserted into the empty paragraph).
2077 * <p>
2078 * The reason it just checks whether <code>start</code> and <code>end</code>
2079 * is the same is that the only time a line can contain 0 characters
2080 * is if it is the final paragraph of the Layout; otherwise any line will
2081 * contain at least one printing or newline character. The reason for the
2082 * additional check if <code>start</code> is greater than 0 is that
2083 * if the empty paragraph is the entire content of the buffer, paragraph
2084 * styles that are already applied to the buffer will apply to text that
2085 * is inserted into it.
2086 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07002087 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002088 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08002089 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07002090 }
2091
Siyamed Sinirfa05ba02016-01-12 10:54:43 -08002092 if(text instanceof SpannableStringBuilder) {
2093 return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
2094 } else {
2095 return text.getSpans(start, end, type);
2096 }
Eric Fischer74d31ef2010-08-05 15:29:36 -07002097 }
2098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002100 char[] dest, int destoff, TextUtils.TruncateAt method) {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002101 final int ellipsisCount = getEllipsisCount(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 if (ellipsisCount == 0) {
2103 return;
2104 }
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002105 final int ellipsisStart = getEllipsisStart(line);
2106 final int lineStart = getLineStart(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002108 final String ellipsisString = TextUtils.getEllipsisString(method);
2109 final int ellipsisStringLen = ellipsisString.length();
Roozbeh Pournader1051bbe2017-07-25 13:52:57 -07002110 // Use the ellipsis string only if there are that at least as many characters to replace.
2111 final boolean useEllipsisString = ellipsisCount >= ellipsisStringLen;
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002112 for (int i = 0; i < ellipsisCount; i++) {
2113 final char c;
Roozbeh Pournader1051bbe2017-07-25 13:52:57 -07002114 if (useEllipsisString && i < ellipsisStringLen) {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002115 c = ellipsisString.charAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 } else {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002117 c = TextUtils.ELLIPSIS_FILLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
2119
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002120 final int a = i + ellipsisStart + lineStart;
2121 if (start <= a && a < end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 dest[destoff + a - start] = c;
2123 }
2124 }
2125 }
2126
2127 /**
2128 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08002129 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 */
2131 public static class Directions {
Siyamed Sinired09ae12016-02-16 14:36:26 -08002132 /**
Petar Šegina7c31d5c2017-09-25 12:19:28 +01002133 * Directions represents directional runs within a line of text. Runs are pairs of ints
2134 * listed in visual order, starting from the leading margin. The first int of each pair is
2135 * the offset from the first character of the line to the start of the run. The second int
2136 * represents both the length and level of the run. The length is in the lower bits,
2137 * accessed by masking with RUN_LENGTH_MASK. The level is in the higher bits, accessed by
2138 * shifting by RUN_LEVEL_SHIFT and masking by RUN_LEVEL_MASK. To simply test for an RTL
2139 * direction, test the bit using RUN_RTL_FLAG, if set then the direction is rtl.
Siyamed Sinired09ae12016-02-16 14:36:26 -08002140 * @hide
2141 */
2142 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2143 public int[] mDirections;
2144
2145 /**
2146 * @hide
2147 */
2148 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2149 public Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 mDirections = dirs;
2151 }
2152 }
2153
2154 /**
2155 * Return the offset of the first character to be ellipsized away,
2156 * relative to the start of the line. (So 0 if the beginning of the
2157 * line is ellipsized, not getLineStart().)
2158 */
2159 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07002160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 /**
2162 * Returns the number of characters to be ellipsized away, or 0 if
2163 * no ellipsis is to take place.
2164 */
2165 public abstract int getEllipsisCount(int line);
2166
2167 /* package */ static class Ellipsizer implements CharSequence, GetChars {
2168 /* package */ CharSequence mText;
2169 /* package */ Layout mLayout;
2170 /* package */ int mWidth;
2171 /* package */ TextUtils.TruncateAt mMethod;
2172
2173 public Ellipsizer(CharSequence s) {
2174 mText = s;
2175 }
2176
2177 public char charAt(int off) {
2178 char[] buf = TextUtils.obtain(1);
2179 getChars(off, off + 1, buf, 0);
2180 char ret = buf[0];
2181
2182 TextUtils.recycle(buf);
2183 return ret;
2184 }
2185
2186 public void getChars(int start, int end, char[] dest, int destoff) {
2187 int line1 = mLayout.getLineForOffset(start);
2188 int line2 = mLayout.getLineForOffset(end);
2189
2190 TextUtils.getChars(mText, start, end, dest, destoff);
2191
2192 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002193 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
2195 }
2196
2197 public int length() {
2198 return mText.length();
2199 }
Doug Felt9f7a4442010-03-01 12:45:56 -08002200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 public CharSequence subSequence(int start, int end) {
2202 char[] s = new char[end - start];
2203 getChars(start, end, s, 0);
2204 return new String(s);
2205 }
2206
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002207 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 public String toString() {
2209 char[] s = new char[length()];
2210 getChars(0, length(), s, 0);
2211 return new String(s);
2212 }
2213
2214 }
2215
Gilles Debunne6c488de2012-03-01 16:20:35 -08002216 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 private Spanned mSpanned;
2218
2219 public SpannedEllipsizer(CharSequence display) {
2220 super(display);
2221 mSpanned = (Spanned) display;
2222 }
2223
2224 public <T> T[] getSpans(int start, int end, Class<T> type) {
2225 return mSpanned.getSpans(start, end, type);
2226 }
2227
2228 public int getSpanStart(Object tag) {
2229 return mSpanned.getSpanStart(tag);
2230 }
2231
2232 public int getSpanEnd(Object tag) {
2233 return mSpanned.getSpanEnd(tag);
2234 }
2235
2236 public int getSpanFlags(Object tag) {
2237 return mSpanned.getSpanFlags(tag);
2238 }
2239
Gilles Debunne6c488de2012-03-01 16:20:35 -08002240 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 public int nextSpanTransition(int start, int limit, Class type) {
2242 return mSpanned.nextSpanTransition(start, limit, type);
2243 }
2244
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002245 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 public CharSequence subSequence(int start, int end) {
2247 char[] s = new char[end - start];
2248 getChars(start, end, s, 0);
2249
2250 SpannableString ss = new SpannableString(new String(s));
2251 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
2252 return ss;
2253 }
2254 }
2255
2256 private CharSequence mText;
2257 private TextPaint mPaint;
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07002258 private TextPaint mWorkPaint = new TextPaint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 private int mWidth;
2260 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
2261 private float mSpacingMult;
2262 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002263 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002265 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002266 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -07002267 private int mJustificationMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268
Seigo Nonakaf1644f72017-11-27 22:09:49 -08002269 /** @hide */
2270 @IntDef({DIR_LEFT_TO_RIGHT, DIR_RIGHT_TO_LEFT})
2271 @Retention(RetentionPolicy.SOURCE)
2272 public @interface Direction {}
2273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 public static final int DIR_LEFT_TO_RIGHT = 1;
2275 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002276
Doug Felt20178d62010-02-22 13:39:01 -08002277 /* package */ static final int DIR_REQUEST_LTR = 1;
2278 /* package */ static final int DIR_REQUEST_RTL = -1;
2279 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2280 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281
Doug Felt9f7a4442010-03-01 12:45:56 -08002282 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2283 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2284 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2285 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 public enum Alignment {
2288 ALIGN_NORMAL,
2289 ALIGN_OPPOSITE,
2290 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002291 /** @hide */
2292 ALIGN_LEFT,
2293 /** @hide */
2294 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 }
2296
2297 private static final int TAB_INCREMENT = 20;
2298
Siyamed Sinired09ae12016-02-16 14:36:26 -08002299 /** @hide */
2300 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2301 public static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002302 new Directions(new int[] { 0, RUN_LENGTH_MASK });
Siyamed Sinired09ae12016-02-16 14:36:26 -08002303
2304 /** @hide */
2305 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2306 public static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002307 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002308
Petar Šeginafb748b32017-08-07 12:37:52 +01002309 /** @hide */
Petar Šeginab92c5392017-09-06 15:25:05 +01002310 @Retention(RetentionPolicy.SOURCE)
Petar Šegina3a92fb62017-09-07 21:03:24 +01002311 @IntDef({TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT, TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT})
2312 public @interface TextSelectionLayout {}
2313
2314 /** @hide */
2315 public static final int TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT = 0;
2316 /** @hide */
2317 public static final int TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT = 1;
Petar Šeginab92c5392017-09-06 15:25:05 +01002318
2319 /** @hide */
Petar Šeginafb748b32017-08-07 12:37:52 +01002320 @FunctionalInterface
Petar Šeginab92c5392017-09-06 15:25:05 +01002321 public interface SelectionRectangleConsumer {
Petar Šeginafb748b32017-08-07 12:37:52 +01002322 /**
2323 * Performs this operation on the given rectangle.
2324 *
2325 * @param left the left edge of the rectangle
2326 * @param top the top edge of the rectangle
2327 * @param right the right edge of the rectangle
2328 * @param bottom the bottom edge of the rectangle
Petar Šeginab92c5392017-09-06 15:25:05 +01002329 * @param textSelectionLayout the layout (RTL or LTR) of the text covered by this
2330 * selection rectangle
Petar Šeginafb748b32017-08-07 12:37:52 +01002331 */
Petar Šeginab92c5392017-09-06 15:25:05 +01002332 void accept(float left, float top, float right, float bottom,
2333 @TextSelectionLayout int textSelectionLayout);
Petar Šeginafb748b32017-08-07 12:37:52 +01002334 }
2335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336}