blob: 25f791bc8cc6082bca4337678005566436e2920b [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;
322 TabStops tabStops = null;
323 boolean tabStopsIsInitialized = false;
324
325 int left = 0;
326 int right = mWidth;
327
328 final int dir = getParagraphDirection(lineNum);
329
330 ParagraphStyle[] spans = NO_PARA_SPANS;
331 if (mSpannedText) {
332 Spanned sp = (Spanned) mText;
333 final int start = getLineStart(lineNum);
334
335 final boolean isFirstParaLine = (start == 0 || mText.charAt(start - 1) == '\n');
336
337 if (isFirstParaLine) {
338 final int spanEnd = sp.nextSpanTransition(start, mText.length(),
339 ParagraphStyle.class);
340 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
341
342 for (int n = spans.length - 1; n >= 0; n--) {
343 if (spans[n] instanceof AlignmentSpan) {
344 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
345 break;
346 }
347 }
348 }
349
350 final int length = spans.length;
351 boolean useFirstLineMargin = isFirstParaLine;
352 for (int n = 0; n < length; n++) {
353 if (spans[n] instanceof LeadingMarginSpan2) {
354 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
355 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
356 if (lineNum < startLine + count) {
357 useFirstLineMargin = true;
358 break;
359 }
360 }
361 }
362 for (int n = 0; n < length; n++) {
363 if (spans[n] instanceof LeadingMarginSpan) {
364 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
365 if (dir == DIR_RIGHT_TO_LEFT) {
366 right -= margin.getLeadingMargin(useFirstLineMargin);
367 } else {
368 left += margin.getLeadingMargin(useFirstLineMargin);
369 }
370 }
371 }
372 }
373
374 if (getLineContainsTab(lineNum)) {
375 tabStops = new TabStops(TAB_INCREMENT, spans);
376 }
377
378 final Alignment align;
379 if (paraAlign == Alignment.ALIGN_LEFT) {
380 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
381 } else if (paraAlign == Alignment.ALIGN_RIGHT) {
382 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
383 } else {
384 align = paraAlign;
385 }
386
387 final int indentWidth;
388 if (align == Alignment.ALIGN_NORMAL) {
389 if (dir == DIR_LEFT_TO_RIGHT) {
390 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
391 } else {
392 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
393 }
394 } else if (align == Alignment.ALIGN_OPPOSITE) {
395 if (dir == DIR_LEFT_TO_RIGHT) {
396 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
397 } else {
398 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
399 }
400 } else { // Alignment.ALIGN_CENTER
401 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
402 }
403
404 return right - left - indentWidth;
405 }
406
Gilles Debunne6c488de2012-03-01 16:20:35 -0800407 /**
408 * @hide
409 */
410 public void drawText(Canvas canvas, int firstLine, int lastLine) {
411 int previousLineBottom = getLineTop(firstLine);
412 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800413 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700414 int spanEnd = 0;
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -0700415 final TextPaint paint = mWorkPaint;
416 paint.set(mPaint);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800417 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700419 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700420 TabStops tabStops = null;
421 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800422
Doug Felte8e45f22010-03-29 14:58:40 -0700423 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700424
Gilles Debunne6c488de2012-03-01 16:20:35 -0800425 // Draw the lines, one at a time.
426 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700427 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700429 previousLineEnd = getLineStart(lineNum + 1);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900430 final boolean justify = isJustificationRequired(lineNum);
Raph Levien26d443a2015-03-30 14:18:32 -0700431 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -0700432 paint.setHyphenEdit(getHyphen(lineNum));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433
434 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700435 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700437 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
Raph Levien26d443a2015-03-30 14:18:32 -0700439 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700440 int left = 0;
441 int right = mWidth;
442
Gilles Debunne6c488de2012-03-01 16:20:35 -0800443 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700444 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800445 int textLength = buf.length();
446 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700447
Doug Feltc982f602010-05-25 11:51:40 -0700448 // New batch of paragraph styles, collect into spans array.
449 // Compute the alignment, last alignment style wins.
450 // Reset tabStops, we'll rebuild if we encounter a line with
451 // tabs.
452 // We expect paragraph spans to be relatively infrequent, use
453 // spanEnd so that we can check less frequently. Since
454 // paragraph styles ought to apply to entire paragraphs, we can
455 // just collect the ones present at the start of the paragraph.
456 // If spanEnd is before the end of the paragraph, that's not
457 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700458 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700459 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700461 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800462
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700463 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800464 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700466 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 break;
468 }
469 }
Doug Felt0c702b82010-05-14 10:55:42 -0700470
Doug Feltc982f602010-05-25 11:51:40 -0700471 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800473
Doug Feltc982f602010-05-25 11:51:40 -0700474 // Draw all leading margin spans. Adjust left or right according
475 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700477 boolean useFirstLineMargin = isFirstParaLine;
478 for (int n = 0; n < length; n++) {
479 if (spans[n] instanceof LeadingMarginSpan2) {
480 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
481 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
482 // if there is more than one LeadingMarginSpan2, use
483 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700484 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700485 useFirstLineMargin = true;
486 break;
487 }
488 }
489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 for (int n = 0; n < length; n++) {
491 if (spans[n] instanceof LeadingMarginSpan) {
492 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800494 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800496 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700497 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800499 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800501 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700502 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
504 }
505 }
506 }
507
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700508 boolean hasTab = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700509 // Can't tell if we have tabs for sure, currently
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700510 if (hasTab && !tabStopsIsInitialized) {
Doug Feltc982f602010-05-25 11:51:40 -0700511 if (tabStops == null) {
512 tabStops = new TabStops(TAB_INCREMENT, spans);
513 } else {
514 tabStops.reset(TAB_INCREMENT, spans);
515 }
516 tabStopsIsInitialized = true;
517 }
518
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700519 // Determine whether the line aligns to normal, opposite, or center.
520 Alignment align = paraAlign;
521 if (align == Alignment.ALIGN_LEFT) {
522 align = (dir == DIR_LEFT_TO_RIGHT) ?
523 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
524 } else if (align == Alignment.ALIGN_RIGHT) {
525 align = (dir == DIR_LEFT_TO_RIGHT) ?
526 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
527 }
528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 int x;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900530 final int indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 if (align == Alignment.ALIGN_NORMAL) {
532 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900533 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
534 x = left + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900536 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
537 x = right - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700540 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700542 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900543 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
544 x = right - max - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900546 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
547 x = left - max + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
Doug Feltc982f602010-05-25 11:51:40 -0700549 } else { // Alignment.ALIGN_CENTER
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900550 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700551 max = max & ~1;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900552 x = ((right + left - max) >> 1) + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554 }
555
Raph Levien26d443a2015-03-30 14:18:32 -0700556 Directions directions = getLineDirections(lineNum);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900557 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab && !justify) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800558 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800559 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 } else {
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700561 tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900562 if (justify) {
563 tl.justify(right - left - indentWidth);
564 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800565 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567 }
Doug Feltc982f602010-05-25 11:51:40 -0700568
Doug Felte8e45f22010-03-29 14:58:40 -0700569 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571
572 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800573 * @hide
574 */
575 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
576 int cursorOffsetVertical, int firstLine, int lastLine) {
577 // First, draw LineBackgroundSpans.
578 // LineBackgroundSpans know nothing about the alignment, margins, or
579 // direction of the layout or line. XXX: Should they?
580 // They are evaluated at each line.
581 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700582 if (mLineBackgroundSpans == null) {
583 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700584 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800585
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700586 Spanned buffer = (Spanned) mText;
587 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700588 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800589
Gilles Debunneeca5b732012-04-25 18:48:42 -0700590 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700591 int previousLineBottom = getLineTop(firstLine);
592 int previousLineEnd = getLineStart(firstLine);
593 ParagraphStyle[] spans = NO_PARA_SPANS;
594 int spansLength = 0;
595 TextPaint paint = mPaint;
596 int spanEnd = 0;
597 final int width = mWidth;
598 for (int i = firstLine; i <= lastLine; i++) {
599 int start = previousLineEnd;
600 int end = getLineStart(i + 1);
601 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800602
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700603 int ltop = previousLineBottom;
604 int lbottom = getLineTop(i + 1);
605 previousLineBottom = lbottom;
606 int lbaseline = lbottom - getLineDescent(i);
607
608 if (start >= spanEnd) {
609 // These should be infrequent, so we'll use this so that
610 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700611 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700612 // All LineBackgroundSpans on a line contribute to its background.
613 spansLength = 0;
614 // Duplication of the logic of getParagraphSpans
615 if (start != end || start == 0) {
616 // Equivalent to a getSpans(start, end), but filling the 'spans' local
617 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700618 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
619 // equal test is valid since both intervals are not empty by
620 // construction
621 if (mLineBackgroundSpans.spanStarts[j] >= end ||
622 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500623 spans = GrowingArrayUtils.append(
624 spans, spansLength, mLineBackgroundSpans.spans[j]);
625 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700626 }
627 }
628 }
629
630 for (int n = 0; n < spansLength; n++) {
631 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
632 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
633 ltop, lbaseline, lbottom,
634 buffer, start, end, i);
635 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800636 }
637 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700638 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800639 }
640
641 // There can be a highlight even without spans if we are drawing
642 // a non-spanned transformation of a spanned editing buffer.
643 if (highlight != null) {
644 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
645 canvas.drawPath(highlight, highlightPaint);
646 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
647 }
648 }
649
650 /**
651 * @param canvas
652 * @return The range of lines that need to be drawn, possibly empty.
653 * @hide
654 */
655 public long getLineRangeForDraw(Canvas canvas) {
656 int dtop, dbottom;
657
658 synchronized (sTempRect) {
659 if (!canvas.getClipBounds(sTempRect)) {
660 // Negative range end used as a special flag
661 return TextUtils.packRangeInLong(0, -1);
662 }
663
664 dtop = sTempRect.top;
665 dbottom = sTempRect.bottom;
666 }
667
668 final int top = Math.max(dtop, 0);
669 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
670
Gilles Debunne2fba3382012-06-11 17:46:24 -0700671 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800672 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
673 }
674
675 /**
Doug Feltc982f602010-05-25 11:51:40 -0700676 * Return the start position of the line, given the left and right bounds
677 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700678 *
Doug Feltc982f602010-05-25 11:51:40 -0700679 * @param line the line index
680 * @param left the left bounds (0, or leading margin if ltr para)
681 * @param right the right bounds (width, minus leading margin if rtl para)
682 * @return the start position of the line (to right of line if rtl para)
683 */
684 private int getLineStartPos(int line, int left, int right) {
685 // Adjust the point at which to start rendering depending on the
686 // alignment of the paragraph.
687 Alignment align = getParagraphAlignment(line);
688 int dir = getParagraphDirection(line);
689
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700690 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700691 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
692 } else if (align == Alignment.ALIGN_RIGHT) {
693 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
694 }
695
696 int x;
697 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700698 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700699 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700700 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700701 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700702 }
703 } else {
704 TabStops tabStops = null;
705 if (mSpannedText && getLineContainsTab(line)) {
706 Spanned spanned = (Spanned) mText;
707 int start = getLineStart(line);
708 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
709 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800710 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
711 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700712 if (tabSpans.length > 0) {
713 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
714 }
715 }
716 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700717 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700718 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700719 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700720 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700721 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700722 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700723 }
724 } else { // Alignment.ALIGN_CENTER
725 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700726 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700727 }
728 }
729 return x;
730 }
731
732 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * Return the text that is displayed by this Layout.
734 */
735 public final CharSequence getText() {
736 return mText;
737 }
738
739 /**
740 * Return the base Paint properties for this layout.
741 * Do NOT change the paint, which may result in funny
742 * drawing for this layout.
743 */
744 public final TextPaint getPaint() {
745 return mPaint;
746 }
747
748 /**
749 * Return the width of this layout.
750 */
751 public final int getWidth() {
752 return mWidth;
753 }
754
755 /**
756 * Return the width to which this Layout is ellipsizing, or
757 * {@link #getWidth} if it is not doing anything special.
758 */
759 public int getEllipsizedWidth() {
760 return mWidth;
761 }
762
763 /**
764 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800765 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 * it does not cause the text to reflow to use the full new width.
767 */
768 public final void increaseWidthTo(int wid) {
769 if (wid < mWidth) {
770 throw new RuntimeException("attempted to reduce Layout width");
771 }
772
773 mWidth = wid;
774 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 /**
777 * Return the total height of this layout.
778 */
779 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800780 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 }
782
783 /**
Siyamed Sinir0745c722016-05-31 20:39:33 -0700784 * Return the total height of this layout.
785 *
786 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
787 *
788 * @hide
789 */
790 public int getHeight(boolean cap) {
791 return getHeight();
792 }
793
794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 * Return the base alignment of this layout.
796 */
797 public final Alignment getAlignment() {
798 return mAlignment;
799 }
800
801 /**
802 * Return what the text height is multiplied by to get the line height.
803 */
804 public final float getSpacingMultiplier() {
805 return mSpacingMult;
806 }
807
808 /**
809 * Return the number of units of leading that are added to each line.
810 */
811 public final float getSpacingAdd() {
812 return mSpacingAdd;
813 }
814
815 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700816 * Return the heuristic used to determine paragraph text direction.
817 * @hide
818 */
819 public final TextDirectionHeuristic getTextDirectionHeuristic() {
820 return mTextDir;
821 }
822
823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 * Return the number of lines of text in this layout.
825 */
826 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 /**
829 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
830 * If bounds is not null, return the top, left, right, bottom extents
831 * of the specified line in it.
832 * @param line which line to examine (0..getLineCount() - 1)
833 * @param bounds Optional. If not null, it returns the extent of the line
834 * @return the Y-coordinate of the baseline
835 */
836 public int getLineBounds(int line, Rect bounds) {
837 if (bounds != null) {
838 bounds.left = 0; // ???
839 bounds.top = getLineTop(line);
840 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800841 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
843 return getLineBaseline(line);
844 }
845
846 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800847 * Return the vertical position of the top of the specified line
848 * (0&hellip;getLineCount()).
849 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 * bottom of the last line.
851 */
852 public abstract int getLineTop(int line);
853
854 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800855 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 */
857 public abstract int getLineDescent(int line);
858
859 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800860 * Return the text offset of the beginning of the specified line (
861 * 0&hellip;getLineCount()). If the specified line is equal to the line
862 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 */
864 public abstract int getLineStart(int line);
865
866 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800867 * Returns the primary directionality of the paragraph containing the
868 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
869 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 */
871 public abstract int getParagraphDirection(int line);
872
873 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700874 * Returns whether the specified line contains one or more
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700875 * characters that need to be handled specially, like tabs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 */
877 public abstract boolean getLineContainsTab(int line);
878
879 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800880 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 * The array alternates counts of characters in left-to-right
882 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800883 *
884 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 */
886 public abstract Directions getLineDirections(int line);
887
888 /**
889 * Returns the (negative) number of extra pixels of ascent padding in the
890 * top line of the Layout.
891 */
892 public abstract int getTopPadding();
893
894 /**
895 * Returns the number of extra pixels of descent padding in the
896 * bottom line of the Layout.
897 */
898 public abstract int getBottomPadding();
899
Raph Levien26d443a2015-03-30 14:18:32 -0700900 /**
901 * Returns the hyphen edit for a line.
902 *
903 * @hide
904 */
905 public int getHyphen(int line) {
906 return 0;
907 }
908
Raph Levien2ea52902015-07-01 14:39:31 -0700909 /**
910 * Returns the left indent for a line.
911 *
912 * @hide
913 */
914 public int getIndentAdjust(int line, Alignment alignment) {
915 return 0;
916 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700917
918 /**
919 * Returns true if the character at offset and the preceding character
920 * are at different run levels (and thus there's a split caret).
921 * @param offset the offset
922 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800923 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700924 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800925 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800926 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700927 Directions dirs = getLineDirections(line);
928 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
929 return false;
930 }
931
932 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800933 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700934 int lineEnd = getLineEnd(line);
935 if (offset == lineStart || offset == lineEnd) {
936 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
937 int runIndex = offset == lineStart ? 0 : runs.length - 2;
938 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
939 }
940
941 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800942 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700943 if (offset == runs[i]) {
944 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800945 }
946 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700947 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800948 }
949
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700950 /**
951 * Returns true if the character at offset is right to left (RTL).
952 * @param offset the offset
953 * @return true if the character is RTL, false if it is LTR
954 */
955 public boolean isRtlCharAt(int offset) {
956 int line = getLineForOffset(offset);
957 Directions dirs = getLineDirections(line);
958 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
959 return false;
960 }
961 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
962 return true;
963 }
964 int[] runs = dirs.mDirections;
965 int lineStart = getLineStart(line);
966 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700967 int start = lineStart + runs[i];
968 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
969 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700970 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
971 return ((level & 1) != 0);
972 }
973 }
974 // Should happen only if the offset is "out of bounds"
975 return false;
976 }
977
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900978 /**
979 * Returns the range of the run that the character at offset belongs to.
980 * @param offset the offset
981 * @return The range of the run
982 * @hide
983 */
984 public long getRunRange(int offset) {
985 int line = getLineForOffset(offset);
986 Directions dirs = getLineDirections(line);
987 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
988 return TextUtils.packRangeInLong(0, getLineEnd(line));
989 }
990 int[] runs = dirs.mDirections;
991 int lineStart = getLineStart(line);
992 for (int i = 0; i < runs.length; i += 2) {
993 int start = lineStart + runs[i];
994 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
995 if (offset >= start && offset < limit) {
996 return TextUtils.packRangeInLong(start, limit);
997 }
998 }
999 // Should happen only if the offset is "out of bounds"
1000 return TextUtils.packRangeInLong(0, getLineEnd(line));
1001 }
1002
Doug Felt9f7a4442010-03-01 12:45:56 -08001003 private boolean primaryIsTrailingPrevious(int offset) {
1004 int line = getLineForOffset(offset);
1005 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -07001006 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001007 int[] runs = getLineDirections(line).mDirections;
1008
1009 int levelAt = -1;
1010 for (int i = 0; i < runs.length; i += 2) {
1011 int start = lineStart + runs[i];
1012 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
1013 if (limit > lineEnd) {
1014 limit = lineEnd;
1015 }
1016 if (offset >= start && offset < limit) {
1017 if (offset > start) {
1018 // Previous character is at same level, so don't use trailing.
1019 return false;
1020 }
1021 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1022 break;
1023 }
1024 }
1025 if (levelAt == -1) {
1026 // Offset was limit of line.
1027 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
1028 }
1029
1030 // At level boundary, check previous level.
1031 int levelBefore = -1;
1032 if (offset == lineStart) {
1033 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
1034 } else {
1035 offset -= 1;
1036 for (int i = 0; i < runs.length; i += 2) {
1037 int start = lineStart + runs[i];
1038 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
1039 if (limit > lineEnd) {
1040 limit = lineEnd;
1041 }
1042 if (offset >= start && offset < limit) {
1043 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1044 break;
1045 }
1046 }
1047 }
1048
1049 return levelBefore < levelAt;
1050 }
1051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 /**
1053 * Get the primary horizontal position for the specified text offset.
1054 * This is the location where a new character would be inserted in
1055 * the paragraph's primary direction.
1056 */
1057 public float getPrimaryHorizontal(int offset) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001058 return getPrimaryHorizontal(offset, false /* not clamped */);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001059 }
1060
1061 /**
1062 * Get the primary horizontal position for the specified text offset, but
1063 * optionally clamp it so that it doesn't exceed the width of the layout.
1064 * @hide
1065 */
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001066 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001067 boolean trailing = primaryIsTrailingPrevious(offset);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001068 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070
1071 /**
1072 * Get the secondary horizontal position for the specified text offset.
1073 * This is the location where a new character would be inserted in
1074 * the direction other than the paragraph's primary direction.
1075 */
1076 public float getSecondaryHorizontal(int offset) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001077 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
1079
Raph Levienafe8e9b2012-12-19 16:09:32 -08001080 /**
1081 * Get the secondary horizontal position for the specified text offset, but
1082 * optionally clamp it so that it doesn't exceed the width of the layout.
1083 * @hide
1084 */
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001085 public float getSecondaryHorizontal(int offset, boolean clamped) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001086 boolean trailing = primaryIsTrailingPrevious(offset);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001087 return getHorizontal(offset, !trailing, clamped);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001088 }
1089
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001090 private float getHorizontal(int offset, boolean primary) {
1091 return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset);
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001092 }
1093
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001094 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
1095 int line = getLineForOffset(offset);
1096
Raph Levienafe8e9b2012-12-19 16:09:32 -08001097 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
1099
Raph Levienafe8e9b2012-12-19 16:09:32 -08001100 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001102 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 int dir = getParagraphDirection(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001104 boolean hasTab = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 Directions directions = getLineDirections(line);
1106
Doug Feltc982f602010-05-25 11:51:40 -07001107 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001108 if (hasTab && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001109 // Just checking this line should be good enough, tabs should be
1110 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001111 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001112 if (tabs.length > 0) {
1113 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
1116
Doug Felte8e45f22010-03-29 14:58:40 -07001117 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001118 tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001119 float wid = tl.measure(offset - start, trailing, null);
1120 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121
Raph Levienafe8e9b2012-12-19 16:09:32 -08001122 if (clamped && wid > mWidth) {
1123 wid = mWidth;
1124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 int left = getParagraphLeft(line);
1126 int right = getParagraphRight(line);
1127
Doug Feltc982f602010-05-25 11:51:40 -07001128 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 }
1130
1131 /**
1132 * Get the leftmost position that should be exposed for horizontal
1133 * scrolling on the specified line.
1134 */
1135 public float getLineLeft(int line) {
1136 int dir = getParagraphDirection(line);
1137 Alignment align = getParagraphAlignment(line);
1138
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001139 if (align == Alignment.ALIGN_LEFT) {
1140 return 0;
1141 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 if (dir == DIR_RIGHT_TO_LEFT)
1143 return getParagraphRight(line) - getLineMax(line);
1144 else
1145 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001146 } else if (align == Alignment.ALIGN_RIGHT) {
1147 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 } else if (align == Alignment.ALIGN_OPPOSITE) {
1149 if (dir == DIR_RIGHT_TO_LEFT)
1150 return 0;
1151 else
1152 return mWidth - getLineMax(line);
1153 } else { /* align == Alignment.ALIGN_CENTER */
1154 int left = getParagraphLeft(line);
1155 int right = getParagraphRight(line);
1156 int max = ((int) getLineMax(line)) & ~1;
1157
1158 return left + ((right - left) - max) / 2;
1159 }
1160 }
1161
1162 /**
1163 * Get the rightmost position that should be exposed for horizontal
1164 * scrolling on the specified line.
1165 */
1166 public float getLineRight(int line) {
1167 int dir = getParagraphDirection(line);
1168 Alignment align = getParagraphAlignment(line);
1169
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001170 if (align == Alignment.ALIGN_LEFT) {
1171 return getParagraphLeft(line) + getLineMax(line);
1172 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 if (dir == DIR_RIGHT_TO_LEFT)
1174 return mWidth;
1175 else
1176 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001177 } else if (align == Alignment.ALIGN_RIGHT) {
1178 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 } else if (align == Alignment.ALIGN_OPPOSITE) {
1180 if (dir == DIR_RIGHT_TO_LEFT)
1181 return getLineMax(line);
1182 else
1183 return mWidth;
1184 } else { /* align == Alignment.ALIGN_CENTER */
1185 int left = getParagraphLeft(line);
1186 int right = getParagraphRight(line);
1187 int max = ((int) getLineMax(line)) & ~1;
1188
1189 return right - ((right - left) - max) / 2;
1190 }
1191 }
1192
1193 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001194 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001195 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 */
1197 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001198 float margin = getParagraphLeadingMargin(line);
1199 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001200 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202
1203 /**
Doug Feltc982f602010-05-25 11:51:40 -07001204 * Gets the unsigned horizontal extent of the specified line, including
1205 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 */
1207 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001208 float margin = getParagraphLeadingMargin(line);
1209 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001210 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212
Doug Feltc982f602010-05-25 11:51:40 -07001213 /**
1214 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1215 * tab stops instead of using the ones passed in.
1216 * @param line the index of the line
1217 * @param full whether to include trailing whitespace
1218 * @return the extent of the line
1219 */
1220 private float getLineExtent(int line, boolean full) {
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001221 final int start = getLineStart(line);
1222 final int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001223
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001224 final boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001225 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001226 if (hasTabs && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001227 // Just checking this line should be good enough, tabs should be
1228 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001229 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001230 if (tabs.length > 0) {
1231 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1232 }
1233 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001234 final Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001235 // Returned directions can actually be null
1236 if (directions == null) {
1237 return 0f;
1238 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001239 final int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001241 final TextLine tl = TextLine.obtain();
1242 final TextPaint paint = mWorkPaint;
1243 paint.set(mPaint);
1244 paint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader53aba292017-08-25 15:53:33 -07001245 tl.set(paint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001246 if (isJustificationRequired(line)) {
1247 tl.justify(getJustifyWidth(line));
1248 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001249 final float width = tl.metrics(null);
Doug Feltc982f602010-05-25 11:51:40 -07001250 TextLine.recycle(tl);
1251 return width;
1252 }
1253
1254 /**
1255 * Returns the signed horizontal extent of the specified line, excluding
1256 * leading margin. If full is false, excludes trailing whitespace.
1257 * @param line the index of the line
1258 * @param tabStops the tab stops, can be null if we know they're not used.
1259 * @param full whether to include trailing whitespace
1260 * @return the extent of the text on this line
1261 */
1262 private float getLineExtent(int line, TabStops tabStops, boolean full) {
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001263 final int start = getLineStart(line);
1264 final int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
1265 final boolean hasTabs = getLineContainsTab(line);
1266 final Directions directions = getLineDirections(line);
1267 final int dir = getParagraphDirection(line);
Doug Feltc982f602010-05-25 11:51:40 -07001268
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001269 final TextLine tl = TextLine.obtain();
1270 final TextPaint paint = mWorkPaint;
1271 paint.set(mPaint);
1272 paint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader53aba292017-08-25 15:53:33 -07001273 tl.set(paint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001274 if (isJustificationRequired(line)) {
1275 tl.justify(getJustifyWidth(line));
1276 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001277 final float width = tl.metrics(null);
Doug Felte8e45f22010-03-29 14:58:40 -07001278 TextLine.recycle(tl);
1279 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
1281
1282 /**
1283 * Get the line number corresponding to the specified vertical position.
1284 * If you ask for a position above 0, you get 0; if you ask for a position
1285 * below the bottom of the text, you get the last line.
1286 */
1287 // FIXME: It may be faster to do a linear search for layouts without many lines.
1288 public int getLineForVertical(int vertical) {
1289 int high = getLineCount(), low = -1, guess;
1290
1291 while (high - low > 1) {
1292 guess = (high + low) / 2;
1293
1294 if (getLineTop(guess) > vertical)
1295 high = guess;
1296 else
1297 low = guess;
1298 }
1299
1300 if (low < 0)
1301 return 0;
1302 else
1303 return low;
1304 }
1305
1306 /**
1307 * Get the line number on which the specified text offset appears.
1308 * If you ask for a position before 0, you get 0; if you ask for a position
1309 * beyond the end of the text, you get the last line.
1310 */
1311 public int getLineForOffset(int offset) {
1312 int high = getLineCount(), low = -1, guess;
1313
1314 while (high - low > 1) {
1315 guess = (high + low) / 2;
1316
1317 if (getLineStart(guess) > offset)
1318 high = guess;
1319 else
1320 low = guess;
1321 }
1322
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001323 if (low < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 return 0;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001325 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 return low;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 }
1329
1330 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001331 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 * closest to the specified horizontal position.
1333 */
1334 public int getOffsetForHorizontal(int line, float horiz) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001335 return getOffsetForHorizontal(line, horiz, true);
1336 }
1337
1338 /**
1339 * Get the character offset on the specified line whose position is
1340 * closest to the specified horizontal position.
1341 *
1342 * @param line the line used to find the closest offset
1343 * @param horiz the horizontal position used to find the closest offset
1344 * @param primary whether to use the primary position or secondary position to find the offset
1345 *
1346 * @hide
1347 */
1348 public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
Raph Levienedb27f12015-06-01 14:34:47 -07001349 // TODO: use Paint.getOffsetForAdvance to avoid binary search
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001350 final int lineEndOffset = getLineEnd(line);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001351 final int lineStartOffset = getLineStart(line);
1352
1353 Directions dirs = getLineDirections(line);
1354
1355 TextLine tl = TextLine.obtain();
1356 // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
1357 tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
1358 false, null);
1359
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001360 final int max;
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001361 if (line == getLineCount() - 1) {
1362 max = lineEndOffset;
1363 } else {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001364 max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
1365 !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001366 }
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001367 int best = lineStartOffset;
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001368 float bestdist = Math.abs(getHorizontal(best, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369
Doug Felt9f7a4442010-03-01 12:45:56 -08001370 for (int i = 0; i < dirs.mDirections.length; i += 2) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001371 int here = lineStartOffset + dirs.mDirections[i];
Doug Felt9f7a4442010-03-01 12:45:56 -08001372 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001373 boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
1374 int swap = isRtl ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375
1376 if (there > max)
1377 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 int high = there - 1 + 1, low = here + 1 - 1, guess;
1379
1380 while (high - low > 1) {
1381 guess = (high + low) / 2;
1382 int adguess = getOffsetAtStartOf(guess);
1383
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001384 if (getHorizontal(adguess, primary) * swap >= horiz * swap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 high = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001386 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 low = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
1390
1391 if (low < here + 1)
1392 low = here + 1;
1393
1394 if (low < there) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001395 int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
1396 low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
1397 if (low >= here && low < there) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001398 float dist = Math.abs(getHorizontal(low, primary) - horiz);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001399 if (aft < there) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001400 float other = Math.abs(getHorizontal(aft, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001402 if (other < dist) {
1403 dist = other;
1404 low = aft;
1405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001408 if (dist < bestdist) {
1409 bestdist = dist;
1410 best = low;
1411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
1413 }
1414
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001415 float dist = Math.abs(getHorizontal(here, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416
1417 if (dist < bestdist) {
1418 bestdist = dist;
1419 best = here;
1420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
1422
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001423 float dist = Math.abs(getHorizontal(max, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424
Raph Levien373b7a82013-09-20 15:11:52 -07001425 if (dist <= bestdist) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001426 bestdist = dist;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 best = max;
1428 }
1429
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001430 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 return best;
1432 }
1433
1434 /**
1435 * Return the text offset after the last character on the specified line.
1436 */
1437 public final int getLineEnd(int line) {
1438 return getLineStart(line + 1);
1439 }
1440
Doug Felt9f7a4442010-03-01 12:45:56 -08001441 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 * Return the text offset after the last visible character (so whitespace
1443 * is not counted) on the specified line.
1444 */
1445 public int getLineVisibleEnd(int line) {
1446 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1447 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 CharSequence text = mText;
1451 char ch;
1452 if (line == getLineCount() - 1) {
1453 return end;
1454 }
1455
1456 for (; end > start; end--) {
1457 ch = text.charAt(end - 1);
1458
1459 if (ch == '\n') {
1460 return end - 1;
1461 }
1462
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001463 if (!TextLine.isLineEndSpace(ch)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 break;
1465 }
1466
1467 }
1468
1469 return end;
1470 }
1471
1472 /**
1473 * Return the vertical position of the bottom of the specified line.
1474 */
1475 public final int getLineBottom(int line) {
1476 return getLineTop(line + 1);
1477 }
1478
1479 /**
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001480 * Return the vertical position of the bottom of the specified line without the line spacing
1481 * added.
1482 *
1483 * @hide
1484 */
1485 public final int getLineBottomWithoutSpacing(int line) {
1486 return getLineTop(line + 1) - getLineExtra(line);
1487 }
1488
1489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 * Return the vertical position of the baseline of the specified line.
1491 */
1492 public final int getLineBaseline(int line) {
1493 // getLineTop(line+1) == getLineTop(line)
1494 return getLineTop(line+1) - getLineDescent(line);
1495 }
1496
1497 /**
1498 * Get the ascent of the text on the specified line.
1499 * The return value is negative to match the Paint.ascent() convention.
1500 */
1501 public final int getLineAscent(int line) {
1502 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1503 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1504 }
1505
Siyamed Sinir0fa89d62017-07-24 20:46:41 -07001506 /**
1507 * Return the extra space added as a result of line spacing attributes
1508 * {@link #getSpacingAdd()} and {@link #getSpacingMultiplier()}. Default value is {@code zero}.
1509 *
1510 * @param line the index of the line, the value should be equal or greater than {@code zero}
1511 * @hide
1512 */
1513 public int getLineExtra(@IntRange(from = 0) int line) {
1514 return 0;
1515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001518 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
1520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001522 return getOffsetToLeftRightOf(offset, false);
1523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524
Doug Felt9f7a4442010-03-01 12:45:56 -08001525 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1526 int line = getLineForOffset(caret);
1527 int lineStart = getLineStart(line);
1528 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001529 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001531 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001532 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001533 // if walking off line, look at the line we're headed to
1534 if (advance) {
1535 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001536 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001537 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001538 ++line;
1539 } else {
1540 return caret; // at very end, don't move
1541 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001542 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001543 } else {
1544 if (caret == lineStart) {
1545 if (line > 0) {
1546 lineChanged = true;
1547 --line;
1548 } else {
1549 return caret; // at very start, don't move
1550 }
1551 }
1552 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001553
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001554 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001555 lineStart = getLineStart(line);
1556 lineEnd = getLineEnd(line);
1557 int newDir = getParagraphDirection(line);
1558 if (newDir != lineDir) {
1559 // unusual case. we want to walk onto the line, but it runs
1560 // in a different direction than this one, so we fake movement
1561 // in the opposite direction.
1562 toLeft = !toLeft;
1563 lineDir = newDir;
1564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001566
Doug Felte8e45f22010-03-29 14:58:40 -07001567 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001568
Doug Felte8e45f22010-03-29 14:58:40 -07001569 TextLine tl = TextLine.obtain();
1570 // XXX: we don't care about tabs
1571 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1572 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1573 tl = TextLine.recycle(tl);
1574 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
1576
1577 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001578 // XXX this probably should skip local reorderings and
1579 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 if (offset == 0)
1581 return 0;
1582
1583 CharSequence text = mText;
1584 char c = text.charAt(offset);
1585
1586 if (c >= '\uDC00' && c <= '\uDFFF') {
1587 char c1 = text.charAt(offset - 1);
1588
1589 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1590 offset -= 1;
1591 }
1592
1593 if (mSpannedText) {
1594 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1595 ReplacementSpan.class);
1596
1597 for (int i = 0; i < spans.length; i++) {
1598 int start = ((Spanned) text).getSpanStart(spans[i]);
1599 int end = ((Spanned) text).getSpanEnd(spans[i]);
1600
1601 if (start < offset && end > offset)
1602 offset = start;
1603 }
1604 }
1605
1606 return offset;
1607 }
1608
1609 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001610 * Determine whether we should clamp cursor position. Currently it's
1611 * only robust for left-aligned displays.
1612 * @hide
1613 */
1614 public boolean shouldClampCursor(int line) {
1615 // Only clamp cursor position in left-aligned displays.
1616 switch (getParagraphAlignment(line)) {
1617 case ALIGN_LEFT:
1618 return true;
1619 case ALIGN_NORMAL:
1620 return getParagraphDirection(line) > 0;
1621 default:
1622 return false;
1623 }
1624
1625 }
1626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 * Fills in the specified Path with a representation of a cursor
1628 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001629 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 * directionalities.
1631 */
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001632 public void getCursorPath(final int point, final Path dest, final CharSequence editingBuffer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 dest.reset();
1634
1635 int line = getLineForOffset(point);
1636 int top = getLineTop(line);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001637 int bottom = getLineBottomWithoutSpacing(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638
Raph Levienafe8e9b2012-12-19 16:09:32 -08001639 boolean clamped = shouldClampCursor(line);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001640 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1641 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642
Jeff Brown497a92c2010-09-12 17:55:08 -07001643 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1644 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1645 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 int dist = 0;
1647
1648 if (caps != 0 || fn != 0) {
1649 dist = (bottom - top) >> 2;
1650
1651 if (fn != 0)
1652 top += dist;
1653 if (caps != 0)
1654 bottom -= dist;
1655 }
1656
1657 if (h1 < 0.5f)
1658 h1 = 0.5f;
1659 if (h2 < 0.5f)
1660 h2 = 0.5f;
1661
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001662 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 dest.moveTo(h1, top);
1664 dest.lineTo(h1, bottom);
1665 } else {
1666 dest.moveTo(h1, top);
1667 dest.lineTo(h1, (top + bottom) >> 1);
1668
1669 dest.moveTo(h2, (top + bottom) >> 1);
1670 dest.lineTo(h2, bottom);
1671 }
1672
1673 if (caps == 2) {
1674 dest.moveTo(h2, bottom);
1675 dest.lineTo(h2 - dist, bottom + dist);
1676 dest.lineTo(h2, bottom);
1677 dest.lineTo(h2 + dist, bottom + dist);
1678 } else if (caps == 1) {
1679 dest.moveTo(h2, bottom);
1680 dest.lineTo(h2 - dist, bottom + dist);
1681
1682 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1683 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1684
1685 dest.moveTo(h2 + dist, bottom + dist);
1686 dest.lineTo(h2, bottom);
1687 }
1688
1689 if (fn == 2) {
1690 dest.moveTo(h1, top);
1691 dest.lineTo(h1 - dist, top - dist);
1692 dest.lineTo(h1, top);
1693 dest.lineTo(h1 + dist, top - dist);
1694 } else if (fn == 1) {
1695 dest.moveTo(h1, top);
1696 dest.lineTo(h1 - dist, top - dist);
1697
1698 dest.moveTo(h1 - dist, top - dist + 0.5f);
1699 dest.lineTo(h1 + dist, top - dist + 0.5f);
1700
1701 dest.moveTo(h1 + dist, top - dist);
1702 dest.lineTo(h1, top);
1703 }
1704 }
1705
1706 private void addSelection(int line, int start, int end,
Petar Šeginab92c5392017-09-06 15:25:05 +01001707 int top, int bottom, SelectionRectangleConsumer consumer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 int linestart = getLineStart(line);
1709 int lineend = getLineEnd(line);
1710 Directions dirs = getLineDirections(line);
1711
Petar Šeginafb748b32017-08-07 12:37:52 +01001712 if (lineend > linestart && mText.charAt(lineend - 1) == '\n') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 lineend--;
Petar Šeginafb748b32017-08-07 12:37:52 +01001714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715
Doug Felt9f7a4442010-03-01 12:45:56 -08001716 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1717 int here = linestart + dirs.mDirections[i];
Petar Šeginafb748b32017-08-07 12:37:52 +01001718 int there = here + (dirs.mDirections[i + 1] & RUN_LENGTH_MASK);
Doug Felt9f7a4442010-03-01 12:45:56 -08001719
Petar Šeginafb748b32017-08-07 12:37:52 +01001720 if (there > lineend) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 there = lineend;
Petar Šeginafb748b32017-08-07 12:37:52 +01001722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723
1724 if (start <= there && end >= here) {
1725 int st = Math.max(start, here);
1726 int en = Math.min(end, there);
1727
1728 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001729 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1730 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001732 float left = Math.min(h1, h2);
1733 float right = Math.max(h1, h2);
1734
Petar Šegina8f1f74e2017-09-07 14:26:28 +01001735 final @TextSelectionLayout int layout =
1736 ((dirs.mDirections[i + 1] & RUN_RTL_FLAG) != 0)
1737 ? TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT
1738 : TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT;
1739
1740 consumer.accept(left, top, right, bottom, layout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
1742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744 }
1745
1746 /**
1747 * Fills in the specified Path with a representation of a highlight
1748 * between the specified offsets. This will often be a rectangle
1749 * or a potentially discontinuous set of rectangles. If the start
1750 * and end are the same, the returned path is empty.
1751 */
1752 public void getSelectionPath(int start, int end, Path dest) {
1753 dest.reset();
Petar Šeginab92c5392017-09-06 15:25:05 +01001754 getSelection(start, end, (left, top, right, bottom, textSelectionLayout) ->
Petar Šeginafb748b32017-08-07 12:37:52 +01001755 dest.addRect(left, top, right, bottom, Path.Direction.CW));
1756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757
Petar Šeginafb748b32017-08-07 12:37:52 +01001758 /**
1759 * Calculates the rectangles which should be highlighted to indicate a selection between start
Petar Šeginab92c5392017-09-06 15:25:05 +01001760 * and end and feeds them into the given {@link SelectionRectangleConsumer}.
Petar Šeginafb748b32017-08-07 12:37:52 +01001761 *
1762 * @param start the starting index of the selection
1763 * @param end the ending index of the selection
Petar Šeginab92c5392017-09-06 15:25:05 +01001764 * @param consumer the {@link SelectionRectangleConsumer} which will receive the generated
1765 * rectangles. It will be called every time a rectangle is generated.
Petar Šeginafb748b32017-08-07 12:37:52 +01001766 * @hide
1767 * @see #getSelectionPath(int, int, Path)
1768 */
Petar Šeginab92c5392017-09-06 15:25:05 +01001769 public final void getSelection(int start, int end, final SelectionRectangleConsumer consumer) {
Petar Šeginafb748b32017-08-07 12:37:52 +01001770 if (start == end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 return;
Petar Šeginafb748b32017-08-07 12:37:52 +01001772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773
1774 if (end < start) {
1775 int temp = end;
1776 end = start;
1777 start = temp;
1778 }
1779
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001780 final int startline = getLineForOffset(start);
1781 final int endline = getLineForOffset(end);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 int top = getLineTop(startline);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001784 int bottom = getLineBottomWithoutSpacing(endline);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785
1786 if (startline == endline) {
Petar Šeginafb748b32017-08-07 12:37:52 +01001787 addSelection(startline, start, end, top, bottom, consumer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 } else {
1789 final float width = mWidth;
1790
1791 addSelection(startline, start, getLineEnd(startline),
Petar Šeginafb748b32017-08-07 12:37:52 +01001792 top, getLineBottom(startline), consumer);
Doug Felt9f7a4442010-03-01 12:45:56 -08001793
Petar Šeginafb748b32017-08-07 12:37:52 +01001794 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT) {
Petar Šeginab92c5392017-09-06 15:25:05 +01001795 consumer.accept(getLineLeft(startline), top, 0, getLineBottom(startline),
Petar Šegina3a92fb62017-09-07 21:03:24 +01001796 TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT);
Petar Šeginafb748b32017-08-07 12:37:52 +01001797 } else {
Petar Šeginab92c5392017-09-06 15:25:05 +01001798 consumer.accept(getLineRight(startline), top, width, getLineBottom(startline),
Petar Šegina3a92fb62017-09-07 21:03:24 +01001799 TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT);
Petar Šeginafb748b32017-08-07 12:37:52 +01001800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801
1802 for (int i = startline + 1; i < endline; i++) {
1803 top = getLineTop(i);
1804 bottom = getLineBottom(i);
Petar Šeginab92c5392017-09-06 15:25:05 +01001805 if (getParagraphDirection(i) == DIR_RIGHT_TO_LEFT) {
Petar Šegina3a92fb62017-09-07 21:03:24 +01001806 consumer.accept(0, top, width, bottom, TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT);
Petar Šeginab92c5392017-09-06 15:25:05 +01001807 } else {
Petar Šegina3a92fb62017-09-07 21:03:24 +01001808 consumer.accept(0, top, width, bottom, TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT);
Petar Šeginab92c5392017-09-06 15:25:05 +01001809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 }
1811
1812 top = getLineTop(endline);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001813 bottom = getLineBottomWithoutSpacing(endline);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814
Petar Šeginafb748b32017-08-07 12:37:52 +01001815 addSelection(endline, getLineStart(endline), end, top, bottom, consumer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816
Petar Šeginafb748b32017-08-07 12:37:52 +01001817 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT) {
Petar Šeginab92c5392017-09-06 15:25:05 +01001818 consumer.accept(width, top, getLineRight(endline), bottom,
Petar Šegina3a92fb62017-09-07 21:03:24 +01001819 TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT);
Petar Šeginafb748b32017-08-07 12:37:52 +01001820 } else {
Petar Šeginab92c5392017-09-06 15:25:05 +01001821 consumer.accept(0, top, getLineLeft(endline), bottom,
Petar Šegina3a92fb62017-09-07 21:03:24 +01001822 TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT);
Petar Šeginafb748b32017-08-07 12:37:52 +01001823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
1825 }
1826
1827 /**
1828 * Get the alignment of the specified paragraph, taking into account
1829 * markup attached to it.
1830 */
1831 public final Alignment getParagraphAlignment(int line) {
1832 Alignment align = mAlignment;
1833
1834 if (mSpannedText) {
1835 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001836 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 getLineEnd(line),
1838 AlignmentSpan.class);
1839
1840 int spanLength = spans.length;
1841 if (spanLength > 0) {
1842 align = spans[spanLength-1].getAlignment();
1843 }
1844 }
1845
1846 return align;
1847 }
1848
1849 /**
1850 * Get the left edge of the specified paragraph, inset by left margins.
1851 */
1852 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001854 int dir = getParagraphDirection(line);
1855 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1856 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
Doug Feltc982f602010-05-25 11:51:40 -07001858 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 }
1860
1861 /**
1862 * Get the right edge of the specified paragraph, inset by right margins.
1863 */
1864 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001866 int dir = getParagraphDirection(line);
1867 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1868 return right; // leading margin has no impact, or no styles
1869 }
1870 return right - getParagraphLeadingMargin(line);
1871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872
Doug Feltc982f602010-05-25 11:51:40 -07001873 /**
1874 * Returns the effective leading margin (unsigned) for this line,
1875 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1876 * @param line the line index
1877 * @return the leading margin of this line
1878 */
1879 private int getParagraphLeadingMargin(int line) {
1880 if (!mSpannedText) {
1881 return 0;
1882 }
1883 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001884
Doug Feltc982f602010-05-25 11:51:40 -07001885 int lineStart = getLineStart(line);
1886 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001887 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001888 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001889 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001890 LeadingMarginSpan.class);
1891 if (spans.length == 0) {
1892 return 0; // no leading margin span;
1893 }
Doug Felt0c702b82010-05-14 10:55:42 -07001894
Doug Feltc982f602010-05-25 11:51:40 -07001895 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001896
1897 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001898 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001899
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001900 boolean useFirstLineMargin = isFirstParaLine;
1901 for (int i = 0; i < spans.length; i++) {
1902 if (spans[i] instanceof LeadingMarginSpan2) {
1903 int spStart = spanned.getSpanStart(spans[i]);
1904 int spanLine = getLineForOffset(spStart);
1905 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1906 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1907 useFirstLineMargin |= line < spanLine + count;
1908 }
1909 }
Doug Feltc982f602010-05-25 11:51:40 -07001910 for (int i = 0; i < spans.length; i++) {
1911 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001912 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 }
1914
Doug Feltc982f602010-05-25 11:51:40 -07001915 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 }
1917
Doug Felte8e45f22010-03-29 14:58:40 -07001918 /* package */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001919 static float measurePara(TextPaint paint, CharSequence text, int start, int end,
1920 TextDirectionHeuristic textDir) {
Doug Felte8e45f22010-03-29 14:58:40 -07001921 MeasuredText mt = MeasuredText.obtain();
1922 TextLine tl = TextLine.obtain();
1923 try {
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001924 mt.setPara(text, start, end, textDir, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001925 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001926 int dir;
1927 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001928 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001929 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001930 } else {
1931 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1932 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001933 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 }
Doug Feltc982f602010-05-25 11:51:40 -07001935 char[] chars = mt.mChars;
1936 int len = mt.mLen;
1937 boolean hasTabs = false;
1938 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001939 // leading margins should be taken into account when measuring a paragraph
1940 int margin = 0;
1941 if (text instanceof Spanned) {
1942 Spanned spanned = (Spanned) text;
1943 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1944 LeadingMarginSpan.class);
1945 for (LeadingMarginSpan lms : spans) {
1946 margin += lms.getLeadingMargin(true);
1947 }
1948 }
Doug Feltc982f602010-05-25 11:51:40 -07001949 for (int i = 0; i < len; ++i) {
1950 if (chars[i] == '\t') {
1951 hasTabs = true;
1952 if (text instanceof Spanned) {
1953 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001954 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001955 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001956 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001957 TabStopSpan.class);
1958 if (spans.length > 0) {
1959 tabStops = new TabStops(TAB_INCREMENT, spans);
1960 }
1961 }
1962 break;
1963 }
1964 }
1965 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001966 return margin + Math.abs(tl.metrics(null));
Doug Felte8e45f22010-03-29 14:58:40 -07001967 } finally {
1968 TextLine.recycle(tl);
1969 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
1972
Doug Felt71b8dd72010-02-16 17:27:09 -08001973 /**
Doug Feltc982f602010-05-25 11:51:40 -07001974 * @hide
1975 */
1976 /* package */ static class TabStops {
1977 private int[] mStops;
1978 private int mNumStops;
1979 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001980
Doug Feltc982f602010-05-25 11:51:40 -07001981 TabStops(int increment, Object[] spans) {
1982 reset(increment, spans);
1983 }
Doug Felt0c702b82010-05-14 10:55:42 -07001984
Doug Feltc982f602010-05-25 11:51:40 -07001985 void reset(int increment, Object[] spans) {
1986 this.mIncrement = increment;
1987
1988 int ns = 0;
1989 if (spans != null) {
1990 int[] stops = this.mStops;
1991 for (Object o : spans) {
1992 if (o instanceof TabStopSpan) {
1993 if (stops == null) {
1994 stops = new int[10];
1995 } else if (ns == stops.length) {
1996 int[] nstops = new int[ns * 2];
1997 for (int i = 0; i < ns; ++i) {
1998 nstops[i] = stops[i];
1999 }
2000 stops = nstops;
2001 }
2002 stops[ns++] = ((TabStopSpan) o).getTabStop();
2003 }
2004 }
2005 if (ns > 1) {
2006 Arrays.sort(stops, 0, ns);
2007 }
2008 if (stops != this.mStops) {
2009 this.mStops = stops;
2010 }
2011 }
2012 this.mNumStops = ns;
2013 }
Doug Felt0c702b82010-05-14 10:55:42 -07002014
Doug Feltc982f602010-05-25 11:51:40 -07002015 float nextTab(float h) {
2016 int ns = this.mNumStops;
2017 if (ns > 0) {
2018 int[] stops = this.mStops;
2019 for (int i = 0; i < ns; ++i) {
2020 int stop = stops[i];
2021 if (stop > h) {
2022 return stop;
2023 }
2024 }
2025 }
2026 return nextDefaultStop(h, mIncrement);
2027 }
2028
2029 public static float nextDefaultStop(float h, int inc) {
2030 return ((int) ((h + inc) / inc)) * inc;
2031 }
2032 }
Doug Felt0c702b82010-05-14 10:55:42 -07002033
Doug Feltc982f602010-05-25 11:51:40 -07002034 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08002035 * Returns the position of the next tab stop after h on the line.
2036 *
2037 * @param text the text
2038 * @param start start of the line
2039 * @param end limit of the line
2040 * @param h the current horizontal offset
2041 * @param tabs the tabs, can be null. If it is null, any tabs in effect
2042 * on the line will be used. If there are no tabs, a default offset
2043 * will be used to compute the tab stop.
2044 * @return the offset of the next tab stop.
2045 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 /* package */ static float nextTab(CharSequence text, int start, int end,
2047 float h, Object[] tabs) {
2048 float nh = Float.MAX_VALUE;
2049 boolean alltabs = false;
2050
2051 if (text instanceof Spanned) {
2052 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002053 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 alltabs = true;
2055 }
2056
2057 for (int i = 0; i < tabs.length; i++) {
2058 if (!alltabs) {
2059 if (!(tabs[i] instanceof TabStopSpan))
2060 continue;
2061 }
2062
2063 int where = ((TabStopSpan) tabs[i]).getTabStop();
2064
2065 if (where < nh && where > h)
2066 nh = where;
2067 }
2068
2069 if (nh != Float.MAX_VALUE)
2070 return nh;
2071 }
2072
2073 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
2074 }
2075
2076 protected final boolean isSpanned() {
2077 return mSpannedText;
2078 }
2079
Eric Fischer74d31ef2010-08-05 15:29:36 -07002080 /**
2081 * Returns the same as <code>text.getSpans()</code>, except where
2082 * <code>start</code> and <code>end</code> are the same and are not
2083 * at the very beginning of the text, in which case an empty array
2084 * is returned instead.
2085 * <p>
2086 * This is needed because of the special case that <code>getSpans()</code>
2087 * on an empty range returns the spans adjacent to that range, which is
2088 * primarily for the sake of <code>TextWatchers</code> so they will get
2089 * notifications when text goes from empty to non-empty. But it also
2090 * has the unfortunate side effect that if the text ends with an empty
2091 * paragraph, that paragraph accidentally picks up the styles of the
2092 * preceding paragraph (even though those styles will not be picked up
2093 * by new text that is inserted into the empty paragraph).
2094 * <p>
2095 * The reason it just checks whether <code>start</code> and <code>end</code>
2096 * is the same is that the only time a line can contain 0 characters
2097 * is if it is the final paragraph of the Layout; otherwise any line will
2098 * contain at least one printing or newline character. The reason for the
2099 * additional check if <code>start</code> is greater than 0 is that
2100 * if the empty paragraph is the entire content of the buffer, paragraph
2101 * styles that are already applied to the buffer will apply to text that
2102 * is inserted into it.
2103 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07002104 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002105 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08002106 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07002107 }
2108
Siyamed Sinirfa05ba02016-01-12 10:54:43 -08002109 if(text instanceof SpannableStringBuilder) {
2110 return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
2111 } else {
2112 return text.getSpans(start, end, type);
2113 }
Eric Fischer74d31ef2010-08-05 15:29:36 -07002114 }
2115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002117 char[] dest, int destoff, TextUtils.TruncateAt method) {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002118 final int ellipsisCount = getEllipsisCount(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 if (ellipsisCount == 0) {
2120 return;
2121 }
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002122 final int ellipsisStart = getEllipsisStart(line);
2123 final int lineStart = getLineStart(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002125 final String ellipsisString = TextUtils.getEllipsisString(method);
2126 final int ellipsisStringLen = ellipsisString.length();
Roozbeh Pournader1051bbe2017-07-25 13:52:57 -07002127 // Use the ellipsis string only if there are that at least as many characters to replace.
2128 final boolean useEllipsisString = ellipsisCount >= ellipsisStringLen;
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002129 for (int i = 0; i < ellipsisCount; i++) {
2130 final char c;
Roozbeh Pournader1051bbe2017-07-25 13:52:57 -07002131 if (useEllipsisString && i < ellipsisStringLen) {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002132 c = ellipsisString.charAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 } else {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002134 c = TextUtils.ELLIPSIS_FILLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 }
2136
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002137 final int a = i + ellipsisStart + lineStart;
2138 if (start <= a && a < end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 dest[destoff + a - start] = c;
2140 }
2141 }
2142 }
2143
2144 /**
2145 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08002146 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 */
2148 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08002149 // Directions represents directional runs within a line of text.
2150 // Runs are pairs of ints listed in visual order, starting from the
2151 // leading margin. The first int of each pair is the offset from
2152 // the first character of the line to the start of the run. The
2153 // second int represents both the length and level of the run.
2154 // The length is in the lower bits, accessed by masking with
2155 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
2156 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
2157 // To simply test for an RTL direction, test the bit using
2158 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159
Siyamed Sinired09ae12016-02-16 14:36:26 -08002160 /**
2161 * @hide
2162 */
2163 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2164 public int[] mDirections;
2165
2166 /**
2167 * @hide
2168 */
2169 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2170 public Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 mDirections = dirs;
2172 }
2173 }
2174
2175 /**
2176 * Return the offset of the first character to be ellipsized away,
2177 * relative to the start of the line. (So 0 if the beginning of the
2178 * line is ellipsized, not getLineStart().)
2179 */
2180 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07002181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 /**
2183 * Returns the number of characters to be ellipsized away, or 0 if
2184 * no ellipsis is to take place.
2185 */
2186 public abstract int getEllipsisCount(int line);
2187
2188 /* package */ static class Ellipsizer implements CharSequence, GetChars {
2189 /* package */ CharSequence mText;
2190 /* package */ Layout mLayout;
2191 /* package */ int mWidth;
2192 /* package */ TextUtils.TruncateAt mMethod;
2193
2194 public Ellipsizer(CharSequence s) {
2195 mText = s;
2196 }
2197
2198 public char charAt(int off) {
2199 char[] buf = TextUtils.obtain(1);
2200 getChars(off, off + 1, buf, 0);
2201 char ret = buf[0];
2202
2203 TextUtils.recycle(buf);
2204 return ret;
2205 }
2206
2207 public void getChars(int start, int end, char[] dest, int destoff) {
2208 int line1 = mLayout.getLineForOffset(start);
2209 int line2 = mLayout.getLineForOffset(end);
2210
2211 TextUtils.getChars(mText, start, end, dest, destoff);
2212
2213 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002214 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 }
2216 }
2217
2218 public int length() {
2219 return mText.length();
2220 }
Doug Felt9f7a4442010-03-01 12:45:56 -08002221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 public CharSequence subSequence(int start, int end) {
2223 char[] s = new char[end - start];
2224 getChars(start, end, s, 0);
2225 return new String(s);
2226 }
2227
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002228 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 public String toString() {
2230 char[] s = new char[length()];
2231 getChars(0, length(), s, 0);
2232 return new String(s);
2233 }
2234
2235 }
2236
Gilles Debunne6c488de2012-03-01 16:20:35 -08002237 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 private Spanned mSpanned;
2239
2240 public SpannedEllipsizer(CharSequence display) {
2241 super(display);
2242 mSpanned = (Spanned) display;
2243 }
2244
2245 public <T> T[] getSpans(int start, int end, Class<T> type) {
2246 return mSpanned.getSpans(start, end, type);
2247 }
2248
2249 public int getSpanStart(Object tag) {
2250 return mSpanned.getSpanStart(tag);
2251 }
2252
2253 public int getSpanEnd(Object tag) {
2254 return mSpanned.getSpanEnd(tag);
2255 }
2256
2257 public int getSpanFlags(Object tag) {
2258 return mSpanned.getSpanFlags(tag);
2259 }
2260
Gilles Debunne6c488de2012-03-01 16:20:35 -08002261 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 public int nextSpanTransition(int start, int limit, Class type) {
2263 return mSpanned.nextSpanTransition(start, limit, type);
2264 }
2265
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002266 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 public CharSequence subSequence(int start, int end) {
2268 char[] s = new char[end - start];
2269 getChars(start, end, s, 0);
2270
2271 SpannableString ss = new SpannableString(new String(s));
2272 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
2273 return ss;
2274 }
2275 }
2276
2277 private CharSequence mText;
2278 private TextPaint mPaint;
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07002279 private TextPaint mWorkPaint = new TextPaint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 private int mWidth;
2281 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
2282 private float mSpacingMult;
2283 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002284 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002286 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002287 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -07002288 private int mJustificationMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289
2290 public static final int DIR_LEFT_TO_RIGHT = 1;
2291 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002292
Doug Felt20178d62010-02-22 13:39:01 -08002293 /* package */ static final int DIR_REQUEST_LTR = 1;
2294 /* package */ static final int DIR_REQUEST_RTL = -1;
2295 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2296 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297
Doug Felt9f7a4442010-03-01 12:45:56 -08002298 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2299 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2300 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2301 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 public enum Alignment {
2304 ALIGN_NORMAL,
2305 ALIGN_OPPOSITE,
2306 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002307 /** @hide */
2308 ALIGN_LEFT,
2309 /** @hide */
2310 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 }
2312
2313 private static final int TAB_INCREMENT = 20;
2314
Siyamed Sinired09ae12016-02-16 14:36:26 -08002315 /** @hide */
2316 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2317 public static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002318 new Directions(new int[] { 0, RUN_LENGTH_MASK });
Siyamed Sinired09ae12016-02-16 14:36:26 -08002319
2320 /** @hide */
2321 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2322 public static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002323 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002324
Petar Šeginafb748b32017-08-07 12:37:52 +01002325 /** @hide */
Petar Šeginab92c5392017-09-06 15:25:05 +01002326 @Retention(RetentionPolicy.SOURCE)
Petar Šegina3a92fb62017-09-07 21:03:24 +01002327 @IntDef({TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT, TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT})
2328 public @interface TextSelectionLayout {}
2329
2330 /** @hide */
2331 public static final int TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT = 0;
2332 /** @hide */
2333 public static final int TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT = 1;
Petar Šeginab92c5392017-09-06 15:25:05 +01002334
2335 /** @hide */
Petar Šeginafb748b32017-08-07 12:37:52 +01002336 @FunctionalInterface
Petar Šeginab92c5392017-09-06 15:25:05 +01002337 public interface SelectionRectangleConsumer {
Petar Šeginafb748b32017-08-07 12:37:52 +01002338 /**
2339 * Performs this operation on the given rectangle.
2340 *
2341 * @param left the left edge of the rectangle
2342 * @param top the top edge of the rectangle
2343 * @param right the right edge of the rectangle
2344 * @param bottom the bottom edge of the rectangle
Petar Šeginab92c5392017-09-06 15:25:05 +01002345 * @param textSelectionLayout the layout (RTL or LTR) of the text covered by this
2346 * selection rectangle
Petar Šeginafb748b32017-08-07 12:37:52 +01002347 */
Petar Šeginab92c5392017-09-06 15:25:05 +01002348 void accept(float left, float top, float right, float bottom,
2349 @TextSelectionLayout int textSelectionLayout);
Petar Šeginafb748b32017-08-07 12:37:52 +01002350 }
2351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352}