blob: 5d9c8d88fd1b7b782a87201cc9eed6a756006382 [file] [log] [blame]
Gilles Debunne60e3b3f2012-03-13 11:26:05 -07001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text;
18
Raph Levien39b4db72015-03-25 13:18:20 -070019import android.annotation.IntDef;
Siyamed Sinir0fa89d62017-07-24 20:46:41 -070020import android.annotation.IntRange;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.graphics.Canvas;
22import android.graphics.Paint;
Doug Felt9f7a4442010-03-01 12:45:56 -080023import android.graphics.Path;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.text.method.TextKeyListener;
Doug Felt9f7a4442010-03-01 12:45:56 -080026import android.text.style.AlignmentSpan;
27import android.text.style.LeadingMarginSpan;
Gilles Debunne162bf0f2010-11-16 16:23:53 -080028import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
Doug Felt9f7a4442010-03-01 12:45:56 -080029import android.text.style.LineBackgroundSpan;
30import android.text.style.ParagraphStyle;
31import android.text.style.ReplacementSpan;
32import android.text.style.TabStopSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Siyamed Sinired09ae12016-02-16 14:36:26 -080034import com.android.internal.annotations.VisibleForTesting;
Doug Feltcb3791202011-07-07 11:57:48 -070035import com.android.internal.util.ArrayUtils;
Adam Lesinski776abc22014-03-07 11:30:59 -050036import com.android.internal.util.GrowingArrayUtils;
Doug Feltcb3791202011-07-07 11:57:48 -070037
Raph Levien39b4db72015-03-25 13:18:20 -070038import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
Doug Feltc982f602010-05-25 11:51:40 -070040import java.util.Arrays;
Doug Felt9f7a4442010-03-01 12:45:56 -080041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042/**
Doug Felt9f7a4442010-03-01 12:45:56 -080043 * A base class that manages text layout in visual elements on
44 * the screen.
45 * <p>For text that will be edited, use a {@link DynamicLayout},
46 * which will be updated as the text changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 * For text that will not change, use a {@link StaticLayout}.
48 */
49public abstract class Layout {
Raph Levien39b4db72015-03-25 13:18:20 -070050 /** @hide */
51 @IntDef({BREAK_STRATEGY_SIMPLE, BREAK_STRATEGY_HIGH_QUALITY, BREAK_STRATEGY_BALANCED})
52 @Retention(RetentionPolicy.SOURCE)
53 public @interface BreakStrategy {}
54
55 /**
56 * Value for break strategy indicating simple line breaking. Automatic hyphens are not added
57 * (though soft hyphens are respected), and modifying text generally doesn't affect the layout
58 * before it (which yields a more consistent user experience when editing), but layout may not
59 * be the highest quality.
60 */
61 public static final int BREAK_STRATEGY_SIMPLE = 0;
62
63 /**
64 * Value for break strategy indicating high quality line breaking, including automatic
65 * hyphenation and doing whole-paragraph optimization of line breaks.
66 */
67 public static final int BREAK_STRATEGY_HIGH_QUALITY = 1;
68
69 /**
70 * Value for break strategy indicating balanced line breaking. The breaks are chosen to
71 * make all lines as close to the same length as possible, including automatic hyphenation.
72 */
73 public static final int BREAK_STRATEGY_BALANCED = 2;
74
Roozbeh Pournader95c7a132015-05-12 12:01:06 -070075 /** @hide */
76 @IntDef({HYPHENATION_FREQUENCY_NORMAL, HYPHENATION_FREQUENCY_FULL,
77 HYPHENATION_FREQUENCY_NONE})
78 @Retention(RetentionPolicy.SOURCE)
79 public @interface HyphenationFrequency {}
80
81 /**
82 * Value for hyphenation frequency indicating no automatic hyphenation. Useful
83 * for backward compatibility, and for cases where the automatic hyphenation algorithm results
84 * in incorrect hyphenation. Mid-word breaks may still happen when a word is wider than the
85 * layout and there is otherwise no valid break. Soft hyphens are ignored and will not be used
86 * as suggestions for potential line breaks.
87 */
88 public static final int HYPHENATION_FREQUENCY_NONE = 0;
89
90 /**
91 * Value for hyphenation frequency indicating a light amount of automatic hyphenation, which
92 * is a conservative default. Useful for informal cases, such as short sentences or chat
93 * messages.
94 */
95 public static final int HYPHENATION_FREQUENCY_NORMAL = 1;
96
97 /**
98 * Value for hyphenation frequency indicating the full amount of automatic hyphenation, typical
99 * in typography. Useful for running text and where it's important to put the maximum amount of
100 * text in a screen with limited space.
101 */
102 public static final int HYPHENATION_FREQUENCY_FULL = 2;
103
Doug Felt71b8dd72010-02-16 17:27:09 -0800104 private static final ParagraphStyle[] NO_PARA_SPANS =
105 ArrayUtils.emptyArray(ParagraphStyle.class);
Dave Bort76c02262009-04-13 17:17:21 -0700106
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700107 /** @hide */
108 @IntDef({JUSTIFICATION_MODE_NONE, JUSTIFICATION_MODE_INTER_WORD})
109 @Retention(RetentionPolicy.SOURCE)
110 public @interface JustificationMode {}
111
112 /**
113 * Value for justification mode indicating no justification.
114 */
115 public static final int JUSTIFICATION_MODE_NONE = 0;
116
117 /**
118 * Value for justification mode indicating the text is justified by stretching word spacing.
119 */
120 public static final int JUSTIFICATION_MODE_INTER_WORD = 1;
121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700123 * Return how wide a layout must be in order to display the specified text with one line per
124 * paragraph.
125 *
126 * <p>As of O, Uses
127 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
128 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 */
130 public static float getDesiredWidth(CharSequence source,
131 TextPaint paint) {
132 return getDesiredWidth(source, 0, source.length(), paint);
133 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700136 * Return how wide a layout must be in order to display the specified text slice with one
137 * line per paragraph.
138 *
139 * <p>As of O, Uses
140 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
141 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
142 */
143 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) {
144 return getDesiredWidth(source, start, end, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR);
145 }
146
147 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800148 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * specified text slice with one line per paragraph.
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700150 *
151 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700153 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint,
154 TextDirectionHeuristic textDir) {
Seigo Nonaka917748e2017-08-03 17:42:28 -0700155 return getDesiredWidthWithLimit(source, start, end, paint, textDir, Float.MAX_VALUE);
156 }
157 /**
158 * Return how wide a layout must be in order to display the
159 * specified text slice with one line per paragraph.
160 *
161 * If the measured width exceeds given limit, returns limit value instead.
162 * @hide
163 */
164 public static float getDesiredWidthWithLimit(CharSequence source, int start, int end,
165 TextPaint paint, TextDirectionHeuristic textDir, float upperLimit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
168 int next;
169 for (int i = start; i <= end; i = next) {
170 next = TextUtils.indexOf(source, '\n', i, end);
171
172 if (next < 0)
173 next = end;
174
Doug Felt71b8dd72010-02-16 17:27:09 -0800175 // note, omits trailing paragraph char
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700176 float w = measurePara(paint, source, i, next, textDir);
Seigo Nonaka917748e2017-08-03 17:42:28 -0700177 if (w > upperLimit) {
178 return upperLimit;
179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
181 if (w > need)
182 need = w;
183
184 next++;
185 }
186
187 return need;
188 }
189
190 /**
191 * Subclasses of Layout use this constructor to set the display text,
192 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800193 * @param text the text to render
194 * @param paint the default paint for the layout. Styles can override
195 * various attributes of the paint.
196 * @param width the wrapping width for the text.
197 * @param align whether to left, right, or center the text. Styles can
198 * override the alignment.
199 * @param spacingMult factor by which to scale the font size to get the
200 * default line spacing
201 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 */
203 protected Layout(CharSequence text, TextPaint paint,
204 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800205 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700206 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
207 spacingMult, spacingAdd);
208 }
209
210 /**
211 * Subclasses of Layout use this constructor to set the display text,
212 * width, and other standard properties.
213 * @param text the text to render
214 * @param paint the default paint for the layout. Styles can override
215 * various attributes of the paint.
216 * @param width the wrapping width for the text.
217 * @param align whether to left, right, or center the text. Styles can
218 * override the alignment.
219 * @param spacingMult factor by which to scale the font size to get the
220 * default line spacing
221 * @param spacingAdd amount to add to the default line spacing
222 *
223 * @hide
224 */
225 protected Layout(CharSequence text, TextPaint paint,
226 int width, Alignment align, TextDirectionHeuristic textDir,
227 float spacingMult, float spacingAdd) {
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 if (width < 0)
230 throw new IllegalArgumentException("Layout: " + width + " < 0");
231
Doug Felte8e45f22010-03-29 14:58:40 -0700232 // Ensure paint doesn't have baselineShift set.
233 // While normally we don't modify the paint the user passed in,
234 // we were already doing this in Styled.drawUniformRun with both
235 // baselineShift and bgColor. We probably should reevaluate bgColor.
236 if (paint != null) {
237 paint.bgColor = 0;
238 paint.baselineShift = 0;
239 }
240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 mText = text;
242 mPaint = paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 mWidth = width;
244 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800245 mSpacingMult = spacingMult;
246 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700248 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
250
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900251 /** @hide */
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700252 protected void setJustificationMode(@JustificationMode int justificationMode) {
253 mJustificationMode = justificationMode;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900254 }
255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * Replace constructor properties of this Layout with new ones. Be careful.
258 */
259 /* package */ void replaceWith(CharSequence text, TextPaint paint,
260 int width, Alignment align,
261 float spacingmult, float spacingadd) {
262 if (width < 0) {
263 throw new IllegalArgumentException("Layout: " + width + " < 0");
264 }
265
266 mText = text;
267 mPaint = paint;
268 mWidth = width;
269 mAlignment = align;
270 mSpacingMult = spacingmult;
271 mSpacingAdd = spacingadd;
272 mSpannedText = text instanceof Spanned;
273 }
274
275 /**
276 * Draw this Layout on the specified Canvas.
277 */
278 public void draw(Canvas c) {
279 draw(c, null, null, 0);
280 }
281
282 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800283 * Draw this Layout on the specified canvas, with the highlight path drawn
284 * between the background and the text.
285 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800286 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800287 * @param highlight the path of the highlight or cursor; can be null
288 * @param highlightPaint the paint for the highlight
289 * @param cursorOffsetVertical the amount to temporarily translate the
290 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800292 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
293 int cursorOffsetVertical) {
294 final long lineRange = getLineRangeForDraw(canvas);
295 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
296 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
297 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
Gilles Debunne6c488de2012-03-01 16:20:35 -0800299 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
300 firstLine, lastLine);
301 drawText(canvas, firstLine, lastLine);
302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900304 private boolean isJustificationRequired(int lineNum) {
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700305 if (mJustificationMode == JUSTIFICATION_MODE_NONE) return false;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900306 final int lineEnd = getLineEnd(lineNum);
307 return lineEnd < mText.length() && mText.charAt(lineEnd - 1) != '\n';
308 }
309
310 private float getJustifyWidth(int lineNum) {
311 Alignment paraAlign = mAlignment;
312 TabStops tabStops = null;
313 boolean tabStopsIsInitialized = false;
314
315 int left = 0;
316 int right = mWidth;
317
318 final int dir = getParagraphDirection(lineNum);
319
320 ParagraphStyle[] spans = NO_PARA_SPANS;
321 if (mSpannedText) {
322 Spanned sp = (Spanned) mText;
323 final int start = getLineStart(lineNum);
324
325 final boolean isFirstParaLine = (start == 0 || mText.charAt(start - 1) == '\n');
326
327 if (isFirstParaLine) {
328 final int spanEnd = sp.nextSpanTransition(start, mText.length(),
329 ParagraphStyle.class);
330 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
331
332 for (int n = spans.length - 1; n >= 0; n--) {
333 if (spans[n] instanceof AlignmentSpan) {
334 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
335 break;
336 }
337 }
338 }
339
340 final int length = spans.length;
341 boolean useFirstLineMargin = isFirstParaLine;
342 for (int n = 0; n < length; n++) {
343 if (spans[n] instanceof LeadingMarginSpan2) {
344 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
345 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
346 if (lineNum < startLine + count) {
347 useFirstLineMargin = true;
348 break;
349 }
350 }
351 }
352 for (int n = 0; n < length; n++) {
353 if (spans[n] instanceof LeadingMarginSpan) {
354 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
355 if (dir == DIR_RIGHT_TO_LEFT) {
356 right -= margin.getLeadingMargin(useFirstLineMargin);
357 } else {
358 left += margin.getLeadingMargin(useFirstLineMargin);
359 }
360 }
361 }
362 }
363
364 if (getLineContainsTab(lineNum)) {
365 tabStops = new TabStops(TAB_INCREMENT, spans);
366 }
367
368 final Alignment align;
369 if (paraAlign == Alignment.ALIGN_LEFT) {
370 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
371 } else if (paraAlign == Alignment.ALIGN_RIGHT) {
372 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
373 } else {
374 align = paraAlign;
375 }
376
377 final int indentWidth;
378 if (align == Alignment.ALIGN_NORMAL) {
379 if (dir == DIR_LEFT_TO_RIGHT) {
380 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
381 } else {
382 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
383 }
384 } else if (align == Alignment.ALIGN_OPPOSITE) {
385 if (dir == DIR_LEFT_TO_RIGHT) {
386 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
387 } else {
388 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
389 }
390 } else { // Alignment.ALIGN_CENTER
391 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
392 }
393
394 return right - left - indentWidth;
395 }
396
Gilles Debunne6c488de2012-03-01 16:20:35 -0800397 /**
398 * @hide
399 */
400 public void drawText(Canvas canvas, int firstLine, int lastLine) {
401 int previousLineBottom = getLineTop(firstLine);
402 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800403 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700404 int spanEnd = 0;
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -0700405 final TextPaint paint = mWorkPaint;
406 paint.set(mPaint);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800407 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700409 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700410 TabStops tabStops = null;
411 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800412
Doug Felte8e45f22010-03-29 14:58:40 -0700413 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700414
Gilles Debunne6c488de2012-03-01 16:20:35 -0800415 // Draw the lines, one at a time.
416 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700417 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700419 previousLineEnd = getLineStart(lineNum + 1);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900420 final boolean justify = isJustificationRequired(lineNum);
Raph Levien26d443a2015-03-30 14:18:32 -0700421 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -0700422 paint.setHyphenEdit(getHyphen(lineNum));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423
424 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700425 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700427 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428
Raph Levien26d443a2015-03-30 14:18:32 -0700429 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700430 int left = 0;
431 int right = mWidth;
432
Gilles Debunne6c488de2012-03-01 16:20:35 -0800433 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700434 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800435 int textLength = buf.length();
436 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700437
Doug Feltc982f602010-05-25 11:51:40 -0700438 // New batch of paragraph styles, collect into spans array.
439 // Compute the alignment, last alignment style wins.
440 // Reset tabStops, we'll rebuild if we encounter a line with
441 // tabs.
442 // We expect paragraph spans to be relatively infrequent, use
443 // spanEnd so that we can check less frequently. Since
444 // paragraph styles ought to apply to entire paragraphs, we can
445 // just collect the ones present at the start of the paragraph.
446 // If spanEnd is before the end of the paragraph, that's not
447 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700448 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700449 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700451 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800452
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700453 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800454 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700456 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 break;
458 }
459 }
Doug Felt0c702b82010-05-14 10:55:42 -0700460
Doug Feltc982f602010-05-25 11:51:40 -0700461 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800463
Doug Feltc982f602010-05-25 11:51:40 -0700464 // Draw all leading margin spans. Adjust left or right according
465 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700467 boolean useFirstLineMargin = isFirstParaLine;
468 for (int n = 0; n < length; n++) {
469 if (spans[n] instanceof LeadingMarginSpan2) {
470 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
471 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
472 // if there is more than one LeadingMarginSpan2, use
473 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700474 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700475 useFirstLineMargin = true;
476 break;
477 }
478 }
479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 for (int n = 0; n < length; n++) {
481 if (spans[n] instanceof LeadingMarginSpan) {
482 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800484 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800486 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700487 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800489 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800491 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700492 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494 }
495 }
496 }
497
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700498 boolean hasTab = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700499 // Can't tell if we have tabs for sure, currently
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700500 if (hasTab && !tabStopsIsInitialized) {
Doug Feltc982f602010-05-25 11:51:40 -0700501 if (tabStops == null) {
502 tabStops = new TabStops(TAB_INCREMENT, spans);
503 } else {
504 tabStops.reset(TAB_INCREMENT, spans);
505 }
506 tabStopsIsInitialized = true;
507 }
508
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700509 // Determine whether the line aligns to normal, opposite, or center.
510 Alignment align = paraAlign;
511 if (align == Alignment.ALIGN_LEFT) {
512 align = (dir == DIR_LEFT_TO_RIGHT) ?
513 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
514 } else if (align == Alignment.ALIGN_RIGHT) {
515 align = (dir == DIR_LEFT_TO_RIGHT) ?
516 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
517 }
518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 int x;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900520 final int indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 if (align == Alignment.ALIGN_NORMAL) {
522 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900523 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
524 x = left + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900526 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
527 x = right - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700530 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700532 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900533 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
534 x = right - max - 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_LEFT);
537 x = left - max + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
Doug Feltc982f602010-05-25 11:51:40 -0700539 } else { // Alignment.ALIGN_CENTER
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900540 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700541 max = max & ~1;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900542 x = ((right + left - max) >> 1) + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544 }
545
Raph Levien26d443a2015-03-30 14:18:32 -0700546 Directions directions = getLineDirections(lineNum);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900547 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab && !justify) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800548 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800549 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 } else {
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700551 tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900552 if (justify) {
553 tl.justify(right - left - indentWidth);
554 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800555 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
557 }
Doug Feltc982f602010-05-25 11:51:40 -0700558
Doug Felte8e45f22010-03-29 14:58:40 -0700559 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
561
562 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800563 * @hide
564 */
565 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
566 int cursorOffsetVertical, int firstLine, int lastLine) {
567 // First, draw LineBackgroundSpans.
568 // LineBackgroundSpans know nothing about the alignment, margins, or
569 // direction of the layout or line. XXX: Should they?
570 // They are evaluated at each line.
571 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700572 if (mLineBackgroundSpans == null) {
573 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700574 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800575
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700576 Spanned buffer = (Spanned) mText;
577 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700578 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800579
Gilles Debunneeca5b732012-04-25 18:48:42 -0700580 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700581 int previousLineBottom = getLineTop(firstLine);
582 int previousLineEnd = getLineStart(firstLine);
583 ParagraphStyle[] spans = NO_PARA_SPANS;
584 int spansLength = 0;
585 TextPaint paint = mPaint;
586 int spanEnd = 0;
587 final int width = mWidth;
588 for (int i = firstLine; i <= lastLine; i++) {
589 int start = previousLineEnd;
590 int end = getLineStart(i + 1);
591 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800592
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700593 int ltop = previousLineBottom;
594 int lbottom = getLineTop(i + 1);
595 previousLineBottom = lbottom;
596 int lbaseline = lbottom - getLineDescent(i);
597
598 if (start >= spanEnd) {
599 // These should be infrequent, so we'll use this so that
600 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700601 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700602 // All LineBackgroundSpans on a line contribute to its background.
603 spansLength = 0;
604 // Duplication of the logic of getParagraphSpans
605 if (start != end || start == 0) {
606 // Equivalent to a getSpans(start, end), but filling the 'spans' local
607 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700608 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
609 // equal test is valid since both intervals are not empty by
610 // construction
611 if (mLineBackgroundSpans.spanStarts[j] >= end ||
612 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500613 spans = GrowingArrayUtils.append(
614 spans, spansLength, mLineBackgroundSpans.spans[j]);
615 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700616 }
617 }
618 }
619
620 for (int n = 0; n < spansLength; n++) {
621 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
622 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
623 ltop, lbaseline, lbottom,
624 buffer, start, end, i);
625 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800626 }
627 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700628 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800629 }
630
631 // There can be a highlight even without spans if we are drawing
632 // a non-spanned transformation of a spanned editing buffer.
633 if (highlight != null) {
634 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
635 canvas.drawPath(highlight, highlightPaint);
636 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
637 }
638 }
639
640 /**
641 * @param canvas
642 * @return The range of lines that need to be drawn, possibly empty.
643 * @hide
644 */
645 public long getLineRangeForDraw(Canvas canvas) {
646 int dtop, dbottom;
647
648 synchronized (sTempRect) {
649 if (!canvas.getClipBounds(sTempRect)) {
650 // Negative range end used as a special flag
651 return TextUtils.packRangeInLong(0, -1);
652 }
653
654 dtop = sTempRect.top;
655 dbottom = sTempRect.bottom;
656 }
657
658 final int top = Math.max(dtop, 0);
659 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
660
Gilles Debunne2fba3382012-06-11 17:46:24 -0700661 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800662 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
663 }
664
665 /**
Doug Feltc982f602010-05-25 11:51:40 -0700666 * Return the start position of the line, given the left and right bounds
667 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700668 *
Doug Feltc982f602010-05-25 11:51:40 -0700669 * @param line the line index
670 * @param left the left bounds (0, or leading margin if ltr para)
671 * @param right the right bounds (width, minus leading margin if rtl para)
672 * @return the start position of the line (to right of line if rtl para)
673 */
674 private int getLineStartPos(int line, int left, int right) {
675 // Adjust the point at which to start rendering depending on the
676 // alignment of the paragraph.
677 Alignment align = getParagraphAlignment(line);
678 int dir = getParagraphDirection(line);
679
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700680 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700681 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
682 } else if (align == Alignment.ALIGN_RIGHT) {
683 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
684 }
685
686 int x;
687 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700688 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700689 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700690 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700691 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700692 }
693 } else {
694 TabStops tabStops = null;
695 if (mSpannedText && getLineContainsTab(line)) {
696 Spanned spanned = (Spanned) mText;
697 int start = getLineStart(line);
698 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
699 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800700 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
701 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700702 if (tabSpans.length > 0) {
703 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
704 }
705 }
706 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700707 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700708 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700709 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700710 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700711 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700712 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700713 }
714 } else { // Alignment.ALIGN_CENTER
715 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700716 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700717 }
718 }
719 return x;
720 }
721
722 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 * Return the text that is displayed by this Layout.
724 */
725 public final CharSequence getText() {
726 return mText;
727 }
728
729 /**
730 * Return the base Paint properties for this layout.
731 * Do NOT change the paint, which may result in funny
732 * drawing for this layout.
733 */
734 public final TextPaint getPaint() {
735 return mPaint;
736 }
737
738 /**
739 * Return the width of this layout.
740 */
741 public final int getWidth() {
742 return mWidth;
743 }
744
745 /**
746 * Return the width to which this Layout is ellipsizing, or
747 * {@link #getWidth} if it is not doing anything special.
748 */
749 public int getEllipsizedWidth() {
750 return mWidth;
751 }
752
753 /**
754 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800755 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * it does not cause the text to reflow to use the full new width.
757 */
758 public final void increaseWidthTo(int wid) {
759 if (wid < mWidth) {
760 throw new RuntimeException("attempted to reduce Layout width");
761 }
762
763 mWidth = wid;
764 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 /**
767 * Return the total height of this layout.
768 */
769 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800770 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772
773 /**
Siyamed Sinir0745c722016-05-31 20:39:33 -0700774 * Return the total height of this layout.
775 *
776 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
777 *
778 * @hide
779 */
780 public int getHeight(boolean cap) {
781 return getHeight();
782 }
783
784 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 * Return the base alignment of this layout.
786 */
787 public final Alignment getAlignment() {
788 return mAlignment;
789 }
790
791 /**
792 * Return what the text height is multiplied by to get the line height.
793 */
794 public final float getSpacingMultiplier() {
795 return mSpacingMult;
796 }
797
798 /**
799 * Return the number of units of leading that are added to each line.
800 */
801 public final float getSpacingAdd() {
802 return mSpacingAdd;
803 }
804
805 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700806 * Return the heuristic used to determine paragraph text direction.
807 * @hide
808 */
809 public final TextDirectionHeuristic getTextDirectionHeuristic() {
810 return mTextDir;
811 }
812
813 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 * Return the number of lines of text in this layout.
815 */
816 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 /**
819 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
820 * If bounds is not null, return the top, left, right, bottom extents
821 * of the specified line in it.
822 * @param line which line to examine (0..getLineCount() - 1)
823 * @param bounds Optional. If not null, it returns the extent of the line
824 * @return the Y-coordinate of the baseline
825 */
826 public int getLineBounds(int line, Rect bounds) {
827 if (bounds != null) {
828 bounds.left = 0; // ???
829 bounds.top = getLineTop(line);
830 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800831 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 }
833 return getLineBaseline(line);
834 }
835
836 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800837 * Return the vertical position of the top of the specified line
838 * (0&hellip;getLineCount()).
839 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 * bottom of the last line.
841 */
842 public abstract int getLineTop(int line);
843
844 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800845 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 */
847 public abstract int getLineDescent(int line);
848
849 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800850 * Return the text offset of the beginning of the specified line (
851 * 0&hellip;getLineCount()). If the specified line is equal to the line
852 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 */
854 public abstract int getLineStart(int line);
855
856 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800857 * Returns the primary directionality of the paragraph containing the
858 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
859 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 */
861 public abstract int getParagraphDirection(int line);
862
863 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700864 * Returns whether the specified line contains one or more
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700865 * characters that need to be handled specially, like tabs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 */
867 public abstract boolean getLineContainsTab(int line);
868
869 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800870 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 * The array alternates counts of characters in left-to-right
872 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800873 *
874 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 */
876 public abstract Directions getLineDirections(int line);
877
878 /**
879 * Returns the (negative) number of extra pixels of ascent padding in the
880 * top line of the Layout.
881 */
882 public abstract int getTopPadding();
883
884 /**
885 * Returns the number of extra pixels of descent padding in the
886 * bottom line of the Layout.
887 */
888 public abstract int getBottomPadding();
889
Raph Levien26d443a2015-03-30 14:18:32 -0700890 /**
891 * Returns the hyphen edit for a line.
892 *
893 * @hide
894 */
895 public int getHyphen(int line) {
896 return 0;
897 }
898
Raph Levien2ea52902015-07-01 14:39:31 -0700899 /**
900 * Returns the left indent for a line.
901 *
902 * @hide
903 */
904 public int getIndentAdjust(int line, Alignment alignment) {
905 return 0;
906 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700907
908 /**
909 * Returns true if the character at offset and the preceding character
910 * are at different run levels (and thus there's a split caret).
911 * @param offset the offset
912 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800913 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700914 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800915 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800916 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700917 Directions dirs = getLineDirections(line);
918 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
919 return false;
920 }
921
922 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800923 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700924 int lineEnd = getLineEnd(line);
925 if (offset == lineStart || offset == lineEnd) {
926 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
927 int runIndex = offset == lineStart ? 0 : runs.length - 2;
928 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
929 }
930
931 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800932 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700933 if (offset == runs[i]) {
934 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800935 }
936 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700937 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800938 }
939
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700940 /**
941 * Returns true if the character at offset is right to left (RTL).
942 * @param offset the offset
943 * @return true if the character is RTL, false if it is LTR
944 */
945 public boolean isRtlCharAt(int offset) {
946 int line = getLineForOffset(offset);
947 Directions dirs = getLineDirections(line);
948 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
949 return false;
950 }
951 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
952 return true;
953 }
954 int[] runs = dirs.mDirections;
955 int lineStart = getLineStart(line);
956 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700957 int start = lineStart + runs[i];
958 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
959 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700960 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
961 return ((level & 1) != 0);
962 }
963 }
964 // Should happen only if the offset is "out of bounds"
965 return false;
966 }
967
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900968 /**
969 * Returns the range of the run that the character at offset belongs to.
970 * @param offset the offset
971 * @return The range of the run
972 * @hide
973 */
974 public long getRunRange(int offset) {
975 int line = getLineForOffset(offset);
976 Directions dirs = getLineDirections(line);
977 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
978 return TextUtils.packRangeInLong(0, getLineEnd(line));
979 }
980 int[] runs = dirs.mDirections;
981 int lineStart = getLineStart(line);
982 for (int i = 0; i < runs.length; i += 2) {
983 int start = lineStart + runs[i];
984 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
985 if (offset >= start && offset < limit) {
986 return TextUtils.packRangeInLong(start, limit);
987 }
988 }
989 // Should happen only if the offset is "out of bounds"
990 return TextUtils.packRangeInLong(0, getLineEnd(line));
991 }
992
Doug Felt9f7a4442010-03-01 12:45:56 -0800993 private boolean primaryIsTrailingPrevious(int offset) {
994 int line = getLineForOffset(offset);
995 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700996 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800997 int[] runs = getLineDirections(line).mDirections;
998
999 int levelAt = -1;
1000 for (int i = 0; i < runs.length; i += 2) {
1001 int start = lineStart + runs[i];
1002 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
1003 if (limit > lineEnd) {
1004 limit = lineEnd;
1005 }
1006 if (offset >= start && offset < limit) {
1007 if (offset > start) {
1008 // Previous character is at same level, so don't use trailing.
1009 return false;
1010 }
1011 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1012 break;
1013 }
1014 }
1015 if (levelAt == -1) {
1016 // Offset was limit of line.
1017 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
1018 }
1019
1020 // At level boundary, check previous level.
1021 int levelBefore = -1;
1022 if (offset == lineStart) {
1023 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
1024 } else {
1025 offset -= 1;
1026 for (int i = 0; i < runs.length; i += 2) {
1027 int start = lineStart + runs[i];
1028 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
1029 if (limit > lineEnd) {
1030 limit = lineEnd;
1031 }
1032 if (offset >= start && offset < limit) {
1033 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1034 break;
1035 }
1036 }
1037 }
1038
1039 return levelBefore < levelAt;
1040 }
1041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 /**
1043 * Get the primary horizontal position for the specified text offset.
1044 * This is the location where a new character would be inserted in
1045 * the paragraph's primary direction.
1046 */
1047 public float getPrimaryHorizontal(int offset) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001048 return getPrimaryHorizontal(offset, false /* not clamped */);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001049 }
1050
1051 /**
1052 * Get the primary horizontal position for the specified text offset, but
1053 * optionally clamp it so that it doesn't exceed the width of the layout.
1054 * @hide
1055 */
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001056 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001057 boolean trailing = primaryIsTrailingPrevious(offset);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001058 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060
1061 /**
1062 * Get the secondary horizontal position for the specified text offset.
1063 * This is the location where a new character would be inserted in
1064 * the direction other than the paragraph's primary direction.
1065 */
1066 public float getSecondaryHorizontal(int offset) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001067 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
1069
Raph Levienafe8e9b2012-12-19 16:09:32 -08001070 /**
1071 * Get the secondary horizontal position for the specified text offset, but
1072 * optionally clamp it so that it doesn't exceed the width of the layout.
1073 * @hide
1074 */
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001075 public float getSecondaryHorizontal(int offset, boolean clamped) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001076 boolean trailing = primaryIsTrailingPrevious(offset);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001077 return getHorizontal(offset, !trailing, clamped);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001078 }
1079
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001080 private float getHorizontal(int offset, boolean primary) {
1081 return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset);
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001082 }
1083
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001084 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
1085 int line = getLineForOffset(offset);
1086
Raph Levienafe8e9b2012-12-19 16:09:32 -08001087 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
1089
Raph Levienafe8e9b2012-12-19 16:09:32 -08001090 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001092 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 int dir = getParagraphDirection(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001094 boolean hasTab = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 Directions directions = getLineDirections(line);
1096
Doug Feltc982f602010-05-25 11:51:40 -07001097 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001098 if (hasTab && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001099 // Just checking this line should be good enough, tabs should be
1100 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001101 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001102 if (tabs.length > 0) {
1103 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106
Doug Felte8e45f22010-03-29 14:58:40 -07001107 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001108 tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001109 float wid = tl.measure(offset - start, trailing, null);
1110 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111
Raph Levienafe8e9b2012-12-19 16:09:32 -08001112 if (clamped && wid > mWidth) {
1113 wid = mWidth;
1114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 int left = getParagraphLeft(line);
1116 int right = getParagraphRight(line);
1117
Doug Feltc982f602010-05-25 11:51:40 -07001118 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120
1121 /**
1122 * Get the leftmost position that should be exposed for horizontal
1123 * scrolling on the specified line.
1124 */
1125 public float getLineLeft(int line) {
1126 int dir = getParagraphDirection(line);
1127 Alignment align = getParagraphAlignment(line);
1128
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001129 if (align == Alignment.ALIGN_LEFT) {
1130 return 0;
1131 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 if (dir == DIR_RIGHT_TO_LEFT)
1133 return getParagraphRight(line) - getLineMax(line);
1134 else
1135 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001136 } else if (align == Alignment.ALIGN_RIGHT) {
1137 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 } else if (align == Alignment.ALIGN_OPPOSITE) {
1139 if (dir == DIR_RIGHT_TO_LEFT)
1140 return 0;
1141 else
1142 return mWidth - getLineMax(line);
1143 } else { /* align == Alignment.ALIGN_CENTER */
1144 int left = getParagraphLeft(line);
1145 int right = getParagraphRight(line);
1146 int max = ((int) getLineMax(line)) & ~1;
1147
1148 return left + ((right - left) - max) / 2;
1149 }
1150 }
1151
1152 /**
1153 * Get the rightmost position that should be exposed for horizontal
1154 * scrolling on the specified line.
1155 */
1156 public float getLineRight(int line) {
1157 int dir = getParagraphDirection(line);
1158 Alignment align = getParagraphAlignment(line);
1159
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001160 if (align == Alignment.ALIGN_LEFT) {
1161 return getParagraphLeft(line) + getLineMax(line);
1162 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 if (dir == DIR_RIGHT_TO_LEFT)
1164 return mWidth;
1165 else
1166 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001167 } else if (align == Alignment.ALIGN_RIGHT) {
1168 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 } else if (align == Alignment.ALIGN_OPPOSITE) {
1170 if (dir == DIR_RIGHT_TO_LEFT)
1171 return getLineMax(line);
1172 else
1173 return mWidth;
1174 } else { /* align == Alignment.ALIGN_CENTER */
1175 int left = getParagraphLeft(line);
1176 int right = getParagraphRight(line);
1177 int max = ((int) getLineMax(line)) & ~1;
1178
1179 return right - ((right - left) - max) / 2;
1180 }
1181 }
1182
1183 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001184 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001185 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 */
1187 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001188 float margin = getParagraphLeadingMargin(line);
1189 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001190 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192
1193 /**
Doug Feltc982f602010-05-25 11:51:40 -07001194 * Gets the unsigned horizontal extent of the specified line, including
1195 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 */
1197 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001198 float margin = getParagraphLeadingMargin(line);
1199 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001200 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202
Doug Feltc982f602010-05-25 11:51:40 -07001203 /**
1204 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1205 * tab stops instead of using the ones passed in.
1206 * @param line the index of the line
1207 * @param full whether to include trailing whitespace
1208 * @return the extent of the line
1209 */
1210 private float getLineExtent(int line, boolean full) {
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001211 final int start = getLineStart(line);
1212 final int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001213
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001214 final boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001215 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001216 if (hasTabs && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001217 // Just checking this line should be good enough, tabs should be
1218 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001219 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001220 if (tabs.length > 0) {
1221 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1222 }
1223 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001224 final Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001225 // Returned directions can actually be null
1226 if (directions == null) {
1227 return 0f;
1228 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001229 final int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001231 final TextLine tl = TextLine.obtain();
1232 final TextPaint paint = mWorkPaint;
1233 paint.set(mPaint);
1234 paint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001235 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001236 if (isJustificationRequired(line)) {
1237 tl.justify(getJustifyWidth(line));
1238 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001239 final float width = tl.metrics(null);
Doug Feltc982f602010-05-25 11:51:40 -07001240 TextLine.recycle(tl);
1241 return width;
1242 }
1243
1244 /**
1245 * Returns the signed horizontal extent of the specified line, excluding
1246 * leading margin. If full is false, excludes trailing whitespace.
1247 * @param line the index of the line
1248 * @param tabStops the tab stops, can be null if we know they're not used.
1249 * @param full whether to include trailing whitespace
1250 * @return the extent of the text on this line
1251 */
1252 private float getLineExtent(int line, TabStops tabStops, boolean full) {
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001253 final int start = getLineStart(line);
1254 final int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
1255 final boolean hasTabs = getLineContainsTab(line);
1256 final Directions directions = getLineDirections(line);
1257 final int dir = getParagraphDirection(line);
Doug Feltc982f602010-05-25 11:51:40 -07001258
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001259 final TextLine tl = TextLine.obtain();
1260 final TextPaint paint = mWorkPaint;
1261 paint.set(mPaint);
1262 paint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001263 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001264 if (isJustificationRequired(line)) {
1265 tl.justify(getJustifyWidth(line));
1266 }
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07001267 final float width = tl.metrics(null);
Doug Felte8e45f22010-03-29 14:58:40 -07001268 TextLine.recycle(tl);
1269 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
1271
1272 /**
1273 * Get the line number corresponding to the specified vertical position.
1274 * If you ask for a position above 0, you get 0; if you ask for a position
1275 * below the bottom of the text, you get the last line.
1276 */
1277 // FIXME: It may be faster to do a linear search for layouts without many lines.
1278 public int getLineForVertical(int vertical) {
1279 int high = getLineCount(), low = -1, guess;
1280
1281 while (high - low > 1) {
1282 guess = (high + low) / 2;
1283
1284 if (getLineTop(guess) > vertical)
1285 high = guess;
1286 else
1287 low = guess;
1288 }
1289
1290 if (low < 0)
1291 return 0;
1292 else
1293 return low;
1294 }
1295
1296 /**
1297 * Get the line number on which the specified text offset appears.
1298 * If you ask for a position before 0, you get 0; if you ask for a position
1299 * beyond the end of the text, you get the last line.
1300 */
1301 public int getLineForOffset(int offset) {
1302 int high = getLineCount(), low = -1, guess;
1303
1304 while (high - low > 1) {
1305 guess = (high + low) / 2;
1306
1307 if (getLineStart(guess) > offset)
1308 high = guess;
1309 else
1310 low = guess;
1311 }
1312
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001313 if (low < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 return 0;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001315 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 return low;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319
1320 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001321 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 * closest to the specified horizontal position.
1323 */
1324 public int getOffsetForHorizontal(int line, float horiz) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001325 return getOffsetForHorizontal(line, horiz, true);
1326 }
1327
1328 /**
1329 * Get the character offset on the specified line whose position is
1330 * closest to the specified horizontal position.
1331 *
1332 * @param line the line used to find the closest offset
1333 * @param horiz the horizontal position used to find the closest offset
1334 * @param primary whether to use the primary position or secondary position to find the offset
1335 *
1336 * @hide
1337 */
1338 public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
Raph Levienedb27f12015-06-01 14:34:47 -07001339 // TODO: use Paint.getOffsetForAdvance to avoid binary search
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001340 final int lineEndOffset = getLineEnd(line);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001341 final int lineStartOffset = getLineStart(line);
1342
1343 Directions dirs = getLineDirections(line);
1344
1345 TextLine tl = TextLine.obtain();
1346 // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
1347 tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
1348 false, null);
1349
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001350 final int max;
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001351 if (line == getLineCount() - 1) {
1352 max = lineEndOffset;
1353 } else {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001354 max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
1355 !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001356 }
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001357 int best = lineStartOffset;
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001358 float bestdist = Math.abs(getHorizontal(best, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359
Doug Felt9f7a4442010-03-01 12:45:56 -08001360 for (int i = 0; i < dirs.mDirections.length; i += 2) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001361 int here = lineStartOffset + dirs.mDirections[i];
Doug Felt9f7a4442010-03-01 12:45:56 -08001362 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001363 boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
1364 int swap = isRtl ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365
1366 if (there > max)
1367 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 int high = there - 1 + 1, low = here + 1 - 1, guess;
1369
1370 while (high - low > 1) {
1371 guess = (high + low) / 2;
1372 int adguess = getOffsetAtStartOf(guess);
1373
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001374 if (getHorizontal(adguess, primary) * swap >= horiz * swap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 high = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001376 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 low = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
1380
1381 if (low < here + 1)
1382 low = here + 1;
1383
1384 if (low < there) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001385 int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
1386 low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
1387 if (low >= here && low < there) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001388 float dist = Math.abs(getHorizontal(low, primary) - horiz);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001389 if (aft < there) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001390 float other = Math.abs(getHorizontal(aft, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001392 if (other < dist) {
1393 dist = other;
1394 low = aft;
1395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001398 if (dist < bestdist) {
1399 bestdist = dist;
1400 best = low;
1401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
1403 }
1404
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001405 float dist = Math.abs(getHorizontal(here, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406
1407 if (dist < bestdist) {
1408 bestdist = dist;
1409 best = here;
1410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 }
1412
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001413 float dist = Math.abs(getHorizontal(max, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414
Raph Levien373b7a82013-09-20 15:11:52 -07001415 if (dist <= bestdist) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001416 bestdist = dist;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 best = max;
1418 }
1419
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001420 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 return best;
1422 }
1423
1424 /**
1425 * Return the text offset after the last character on the specified line.
1426 */
1427 public final int getLineEnd(int line) {
1428 return getLineStart(line + 1);
1429 }
1430
Doug Felt9f7a4442010-03-01 12:45:56 -08001431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 * Return the text offset after the last visible character (so whitespace
1433 * is not counted) on the specified line.
1434 */
1435 public int getLineVisibleEnd(int line) {
1436 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1437 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 CharSequence text = mText;
1441 char ch;
1442 if (line == getLineCount() - 1) {
1443 return end;
1444 }
1445
1446 for (; end > start; end--) {
1447 ch = text.charAt(end - 1);
1448
1449 if (ch == '\n') {
1450 return end - 1;
1451 }
1452
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001453 if (!TextLine.isLineEndSpace(ch)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 break;
1455 }
1456
1457 }
1458
1459 return end;
1460 }
1461
1462 /**
1463 * Return the vertical position of the bottom of the specified line.
1464 */
1465 public final int getLineBottom(int line) {
1466 return getLineTop(line + 1);
1467 }
1468
1469 /**
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001470 * Return the vertical position of the bottom of the specified line without the line spacing
1471 * added.
1472 *
1473 * @hide
1474 */
1475 public final int getLineBottomWithoutSpacing(int line) {
1476 return getLineTop(line + 1) - getLineExtra(line);
1477 }
1478
1479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * Return the vertical position of the baseline of the specified line.
1481 */
1482 public final int getLineBaseline(int line) {
1483 // getLineTop(line+1) == getLineTop(line)
1484 return getLineTop(line+1) - getLineDescent(line);
1485 }
1486
1487 /**
1488 * Get the ascent of the text on the specified line.
1489 * The return value is negative to match the Paint.ascent() convention.
1490 */
1491 public final int getLineAscent(int line) {
1492 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1493 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1494 }
1495
Siyamed Sinir0fa89d62017-07-24 20:46:41 -07001496 /**
1497 * Return the extra space added as a result of line spacing attributes
1498 * {@link #getSpacingAdd()} and {@link #getSpacingMultiplier()}. Default value is {@code zero}.
1499 *
1500 * @param line the index of the line, the value should be equal or greater than {@code zero}
1501 * @hide
1502 */
1503 public int getLineExtra(@IntRange(from = 0) int line) {
1504 return 0;
1505 }
1506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001508 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 }
1510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001512 return getOffsetToLeftRightOf(offset, false);
1513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514
Doug Felt9f7a4442010-03-01 12:45:56 -08001515 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1516 int line = getLineForOffset(caret);
1517 int lineStart = getLineStart(line);
1518 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001519 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001521 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001522 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001523 // if walking off line, look at the line we're headed to
1524 if (advance) {
1525 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001526 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001527 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001528 ++line;
1529 } else {
1530 return caret; // at very end, don't move
1531 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001532 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001533 } else {
1534 if (caret == lineStart) {
1535 if (line > 0) {
1536 lineChanged = true;
1537 --line;
1538 } else {
1539 return caret; // at very start, don't move
1540 }
1541 }
1542 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001543
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001544 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001545 lineStart = getLineStart(line);
1546 lineEnd = getLineEnd(line);
1547 int newDir = getParagraphDirection(line);
1548 if (newDir != lineDir) {
1549 // unusual case. we want to walk onto the line, but it runs
1550 // in a different direction than this one, so we fake movement
1551 // in the opposite direction.
1552 toLeft = !toLeft;
1553 lineDir = newDir;
1554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001556
Doug Felte8e45f22010-03-29 14:58:40 -07001557 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001558
Doug Felte8e45f22010-03-29 14:58:40 -07001559 TextLine tl = TextLine.obtain();
1560 // XXX: we don't care about tabs
1561 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1562 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1563 tl = TextLine.recycle(tl);
1564 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
1566
1567 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001568 // XXX this probably should skip local reorderings and
1569 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 if (offset == 0)
1571 return 0;
1572
1573 CharSequence text = mText;
1574 char c = text.charAt(offset);
1575
1576 if (c >= '\uDC00' && c <= '\uDFFF') {
1577 char c1 = text.charAt(offset - 1);
1578
1579 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1580 offset -= 1;
1581 }
1582
1583 if (mSpannedText) {
1584 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1585 ReplacementSpan.class);
1586
1587 for (int i = 0; i < spans.length; i++) {
1588 int start = ((Spanned) text).getSpanStart(spans[i]);
1589 int end = ((Spanned) text).getSpanEnd(spans[i]);
1590
1591 if (start < offset && end > offset)
1592 offset = start;
1593 }
1594 }
1595
1596 return offset;
1597 }
1598
1599 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001600 * Determine whether we should clamp cursor position. Currently it's
1601 * only robust for left-aligned displays.
1602 * @hide
1603 */
1604 public boolean shouldClampCursor(int line) {
1605 // Only clamp cursor position in left-aligned displays.
1606 switch (getParagraphAlignment(line)) {
1607 case ALIGN_LEFT:
1608 return true;
1609 case ALIGN_NORMAL:
1610 return getParagraphDirection(line) > 0;
1611 default:
1612 return false;
1613 }
1614
1615 }
1616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 * Fills in the specified Path with a representation of a cursor
1618 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001619 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 * directionalities.
1621 */
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001622 public void getCursorPath(final int point, final Path dest, final CharSequence editingBuffer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 dest.reset();
1624
1625 int line = getLineForOffset(point);
1626 int top = getLineTop(line);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001627 int bottom = getLineBottomWithoutSpacing(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628
Raph Levienafe8e9b2012-12-19 16:09:32 -08001629 boolean clamped = shouldClampCursor(line);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001630 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1631 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632
Jeff Brown497a92c2010-09-12 17:55:08 -07001633 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1634 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1635 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 int dist = 0;
1637
1638 if (caps != 0 || fn != 0) {
1639 dist = (bottom - top) >> 2;
1640
1641 if (fn != 0)
1642 top += dist;
1643 if (caps != 0)
1644 bottom -= dist;
1645 }
1646
1647 if (h1 < 0.5f)
1648 h1 = 0.5f;
1649 if (h2 < 0.5f)
1650 h2 = 0.5f;
1651
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001652 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 dest.moveTo(h1, top);
1654 dest.lineTo(h1, bottom);
1655 } else {
1656 dest.moveTo(h1, top);
1657 dest.lineTo(h1, (top + bottom) >> 1);
1658
1659 dest.moveTo(h2, (top + bottom) >> 1);
1660 dest.lineTo(h2, bottom);
1661 }
1662
1663 if (caps == 2) {
1664 dest.moveTo(h2, bottom);
1665 dest.lineTo(h2 - dist, bottom + dist);
1666 dest.lineTo(h2, bottom);
1667 dest.lineTo(h2 + dist, bottom + dist);
1668 } else if (caps == 1) {
1669 dest.moveTo(h2, bottom);
1670 dest.lineTo(h2 - dist, bottom + dist);
1671
1672 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1673 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1674
1675 dest.moveTo(h2 + dist, bottom + dist);
1676 dest.lineTo(h2, bottom);
1677 }
1678
1679 if (fn == 2) {
1680 dest.moveTo(h1, top);
1681 dest.lineTo(h1 - dist, top - dist);
1682 dest.lineTo(h1, top);
1683 dest.lineTo(h1 + dist, top - dist);
1684 } else if (fn == 1) {
1685 dest.moveTo(h1, top);
1686 dest.lineTo(h1 - dist, top - dist);
1687
1688 dest.moveTo(h1 - dist, top - dist + 0.5f);
1689 dest.lineTo(h1 + dist, top - dist + 0.5f);
1690
1691 dest.moveTo(h1 + dist, top - dist);
1692 dest.lineTo(h1, top);
1693 }
1694 }
1695
1696 private void addSelection(int line, int start, int end,
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001697 int top, int bottom, RectangleConsumer consumer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 int linestart = getLineStart(line);
1699 int lineend = getLineEnd(line);
1700 Directions dirs = getLineDirections(line);
1701
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001702 if (lineend > linestart && mText.charAt(lineend - 1) == '\n') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 lineend--;
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705
Doug Felt9f7a4442010-03-01 12:45:56 -08001706 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1707 int here = linestart + dirs.mDirections[i];
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001708 int there = here + (dirs.mDirections[i + 1] & RUN_LENGTH_MASK);
Doug Felt9f7a4442010-03-01 12:45:56 -08001709
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001710 if (there > lineend) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 there = lineend;
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713
1714 if (start <= there && end >= here) {
1715 int st = Math.max(start, here);
1716 int en = Math.min(end, there);
1717
1718 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001719 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1720 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001722 float left = Math.min(h1, h2);
1723 float right = Math.max(h1, h2);
1724
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001725 consumer.accept(left, top, right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
1727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 }
1729 }
1730
1731 /**
1732 * Fills in the specified Path with a representation of a highlight
1733 * between the specified offsets. This will often be a rectangle
1734 * or a potentially discontinuous set of rectangles. If the start
1735 * and end are the same, the returned path is empty.
1736 */
1737 public void getSelectionPath(int start, int end, Path dest) {
1738 dest.reset();
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001739 getSelection(start, end, (left, top, right, bottom) ->
1740 dest.addRect(left, top, right, bottom, Path.Direction.CW));
1741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001743 /**
1744 * Calculates the rectangles which should be highlighted to indicate a selection between start
1745 * and end and feeds them into the given {@link RectangleConsumer}.
1746 *
1747 * @param start the starting index of the selection
1748 * @param end the ending index of the selection
1749 * @param consumer the {@link RectangleConsumer} which will receive the generated rectangles. It
1750 * will be called every time a rectangle is generated.
1751 * @hide
1752 * @see #getSelectionPath(int, int, Path)
1753 */
1754 public final void getSelection(int start, int end, final RectangleConsumer consumer) {
1755 if (start == end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 return;
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758
1759 if (end < start) {
1760 int temp = end;
1761 end = start;
1762 start = temp;
1763 }
1764
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001765 final int startline = getLineForOffset(start);
1766 final int endline = getLineForOffset(end);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 int top = getLineTop(startline);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001769 int bottom = getLineBottomWithoutSpacing(endline);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770
1771 if (startline == endline) {
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001772 addSelection(startline, start, end, top, bottom, consumer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 } else {
1774 final float width = mWidth;
1775
1776 addSelection(startline, start, getLineEnd(startline),
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001777 top, getLineBottom(startline), consumer);
Doug Felt9f7a4442010-03-01 12:45:56 -08001778
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001779 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT) {
1780 consumer.accept(getLineLeft(startline), top, 0, getLineBottom(startline));
1781 } else {
1782 consumer.accept(getLineRight(startline), top, width, getLineBottom(startline));
1783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784
1785 for (int i = startline + 1; i < endline; i++) {
1786 top = getLineTop(i);
1787 bottom = getLineBottom(i);
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001788 consumer.accept(0, top, width, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
1790
1791 top = getLineTop(endline);
Siyamed Sinira60b59d2017-07-26 09:26:41 -07001792 bottom = getLineBottomWithoutSpacing(endline);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001794 addSelection(endline, getLineStart(endline), end, top, bottom, consumer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01001796 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT) {
1797 consumer.accept(width, top, getLineRight(endline), bottom);
1798 } else {
1799 consumer.accept(0, top, getLineLeft(endline), bottom);
1800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
1802 }
1803
1804 /**
1805 * Get the alignment of the specified paragraph, taking into account
1806 * markup attached to it.
1807 */
1808 public final Alignment getParagraphAlignment(int line) {
1809 Alignment align = mAlignment;
1810
1811 if (mSpannedText) {
1812 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001813 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 getLineEnd(line),
1815 AlignmentSpan.class);
1816
1817 int spanLength = spans.length;
1818 if (spanLength > 0) {
1819 align = spans[spanLength-1].getAlignment();
1820 }
1821 }
1822
1823 return align;
1824 }
1825
1826 /**
1827 * Get the left edge of the specified paragraph, inset by left margins.
1828 */
1829 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001831 int dir = getParagraphDirection(line);
1832 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1833 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 }
Doug Feltc982f602010-05-25 11:51:40 -07001835 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 }
1837
1838 /**
1839 * Get the right edge of the specified paragraph, inset by right margins.
1840 */
1841 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001843 int dir = getParagraphDirection(line);
1844 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1845 return right; // leading margin has no impact, or no styles
1846 }
1847 return right - getParagraphLeadingMargin(line);
1848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849
Doug Feltc982f602010-05-25 11:51:40 -07001850 /**
1851 * Returns the effective leading margin (unsigned) for this line,
1852 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1853 * @param line the line index
1854 * @return the leading margin of this line
1855 */
1856 private int getParagraphLeadingMargin(int line) {
1857 if (!mSpannedText) {
1858 return 0;
1859 }
1860 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001861
Doug Feltc982f602010-05-25 11:51:40 -07001862 int lineStart = getLineStart(line);
1863 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001864 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001865 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001866 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001867 LeadingMarginSpan.class);
1868 if (spans.length == 0) {
1869 return 0; // no leading margin span;
1870 }
Doug Felt0c702b82010-05-14 10:55:42 -07001871
Doug Feltc982f602010-05-25 11:51:40 -07001872 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001873
1874 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001875 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001876
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001877 boolean useFirstLineMargin = isFirstParaLine;
1878 for (int i = 0; i < spans.length; i++) {
1879 if (spans[i] instanceof LeadingMarginSpan2) {
1880 int spStart = spanned.getSpanStart(spans[i]);
1881 int spanLine = getLineForOffset(spStart);
1882 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1883 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1884 useFirstLineMargin |= line < spanLine + count;
1885 }
1886 }
Doug Feltc982f602010-05-25 11:51:40 -07001887 for (int i = 0; i < spans.length; i++) {
1888 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001889 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
1891
Doug Feltc982f602010-05-25 11:51:40 -07001892 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 }
1894
Doug Felte8e45f22010-03-29 14:58:40 -07001895 /* package */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001896 static float measurePara(TextPaint paint, CharSequence text, int start, int end,
1897 TextDirectionHeuristic textDir) {
Doug Felte8e45f22010-03-29 14:58:40 -07001898 MeasuredText mt = MeasuredText.obtain();
1899 TextLine tl = TextLine.obtain();
1900 try {
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001901 mt.setPara(text, start, end, textDir, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001902 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001903 int dir;
1904 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001905 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001906 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001907 } else {
1908 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1909 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001910 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
Doug Feltc982f602010-05-25 11:51:40 -07001912 char[] chars = mt.mChars;
1913 int len = mt.mLen;
1914 boolean hasTabs = false;
1915 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001916 // leading margins should be taken into account when measuring a paragraph
1917 int margin = 0;
1918 if (text instanceof Spanned) {
1919 Spanned spanned = (Spanned) text;
1920 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1921 LeadingMarginSpan.class);
1922 for (LeadingMarginSpan lms : spans) {
1923 margin += lms.getLeadingMargin(true);
1924 }
1925 }
Doug Feltc982f602010-05-25 11:51:40 -07001926 for (int i = 0; i < len; ++i) {
1927 if (chars[i] == '\t') {
1928 hasTabs = true;
1929 if (text instanceof Spanned) {
1930 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001931 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001932 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001933 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001934 TabStopSpan.class);
1935 if (spans.length > 0) {
1936 tabStops = new TabStops(TAB_INCREMENT, spans);
1937 }
1938 }
1939 break;
1940 }
1941 }
1942 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001943 return margin + Math.abs(tl.metrics(null));
Doug Felte8e45f22010-03-29 14:58:40 -07001944 } finally {
1945 TextLine.recycle(tl);
1946 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
1949
Doug Felt71b8dd72010-02-16 17:27:09 -08001950 /**
Doug Feltc982f602010-05-25 11:51:40 -07001951 * @hide
1952 */
1953 /* package */ static class TabStops {
1954 private int[] mStops;
1955 private int mNumStops;
1956 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001957
Doug Feltc982f602010-05-25 11:51:40 -07001958 TabStops(int increment, Object[] spans) {
1959 reset(increment, spans);
1960 }
Doug Felt0c702b82010-05-14 10:55:42 -07001961
Doug Feltc982f602010-05-25 11:51:40 -07001962 void reset(int increment, Object[] spans) {
1963 this.mIncrement = increment;
1964
1965 int ns = 0;
1966 if (spans != null) {
1967 int[] stops = this.mStops;
1968 for (Object o : spans) {
1969 if (o instanceof TabStopSpan) {
1970 if (stops == null) {
1971 stops = new int[10];
1972 } else if (ns == stops.length) {
1973 int[] nstops = new int[ns * 2];
1974 for (int i = 0; i < ns; ++i) {
1975 nstops[i] = stops[i];
1976 }
1977 stops = nstops;
1978 }
1979 stops[ns++] = ((TabStopSpan) o).getTabStop();
1980 }
1981 }
1982 if (ns > 1) {
1983 Arrays.sort(stops, 0, ns);
1984 }
1985 if (stops != this.mStops) {
1986 this.mStops = stops;
1987 }
1988 }
1989 this.mNumStops = ns;
1990 }
Doug Felt0c702b82010-05-14 10:55:42 -07001991
Doug Feltc982f602010-05-25 11:51:40 -07001992 float nextTab(float h) {
1993 int ns = this.mNumStops;
1994 if (ns > 0) {
1995 int[] stops = this.mStops;
1996 for (int i = 0; i < ns; ++i) {
1997 int stop = stops[i];
1998 if (stop > h) {
1999 return stop;
2000 }
2001 }
2002 }
2003 return nextDefaultStop(h, mIncrement);
2004 }
2005
2006 public static float nextDefaultStop(float h, int inc) {
2007 return ((int) ((h + inc) / inc)) * inc;
2008 }
2009 }
Doug Felt0c702b82010-05-14 10:55:42 -07002010
Doug Feltc982f602010-05-25 11:51:40 -07002011 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08002012 * Returns the position of the next tab stop after h on the line.
2013 *
2014 * @param text the text
2015 * @param start start of the line
2016 * @param end limit of the line
2017 * @param h the current horizontal offset
2018 * @param tabs the tabs, can be null. If it is null, any tabs in effect
2019 * on the line will be used. If there are no tabs, a default offset
2020 * will be used to compute the tab stop.
2021 * @return the offset of the next tab stop.
2022 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 /* package */ static float nextTab(CharSequence text, int start, int end,
2024 float h, Object[] tabs) {
2025 float nh = Float.MAX_VALUE;
2026 boolean alltabs = false;
2027
2028 if (text instanceof Spanned) {
2029 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002030 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 alltabs = true;
2032 }
2033
2034 for (int i = 0; i < tabs.length; i++) {
2035 if (!alltabs) {
2036 if (!(tabs[i] instanceof TabStopSpan))
2037 continue;
2038 }
2039
2040 int where = ((TabStopSpan) tabs[i]).getTabStop();
2041
2042 if (where < nh && where > h)
2043 nh = where;
2044 }
2045
2046 if (nh != Float.MAX_VALUE)
2047 return nh;
2048 }
2049
2050 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
2051 }
2052
2053 protected final boolean isSpanned() {
2054 return mSpannedText;
2055 }
2056
Eric Fischer74d31ef2010-08-05 15:29:36 -07002057 /**
2058 * Returns the same as <code>text.getSpans()</code>, except where
2059 * <code>start</code> and <code>end</code> are the same and are not
2060 * at the very beginning of the text, in which case an empty array
2061 * is returned instead.
2062 * <p>
2063 * This is needed because of the special case that <code>getSpans()</code>
2064 * on an empty range returns the spans adjacent to that range, which is
2065 * primarily for the sake of <code>TextWatchers</code> so they will get
2066 * notifications when text goes from empty to non-empty. But it also
2067 * has the unfortunate side effect that if the text ends with an empty
2068 * paragraph, that paragraph accidentally picks up the styles of the
2069 * preceding paragraph (even though those styles will not be picked up
2070 * by new text that is inserted into the empty paragraph).
2071 * <p>
2072 * The reason it just checks whether <code>start</code> and <code>end</code>
2073 * is the same is that the only time a line can contain 0 characters
2074 * is if it is the final paragraph of the Layout; otherwise any line will
2075 * contain at least one printing or newline character. The reason for the
2076 * additional check if <code>start</code> is greater than 0 is that
2077 * if the empty paragraph is the entire content of the buffer, paragraph
2078 * styles that are already applied to the buffer will apply to text that
2079 * is inserted into it.
2080 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07002081 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002082 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08002083 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07002084 }
2085
Siyamed Sinirfa05ba02016-01-12 10:54:43 -08002086 if(text instanceof SpannableStringBuilder) {
2087 return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
2088 } else {
2089 return text.getSpans(start, end, type);
2090 }
Eric Fischer74d31ef2010-08-05 15:29:36 -07002091 }
2092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002094 char[] dest, int destoff, TextUtils.TruncateAt method) {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002095 final int ellipsisCount = getEllipsisCount(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 if (ellipsisCount == 0) {
2097 return;
2098 }
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002099 final int ellipsisStart = getEllipsisStart(line);
2100 final int lineStart = getLineStart(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002102 final String ellipsisString = TextUtils.getEllipsisString(method);
2103 final int ellipsisStringLen = ellipsisString.length();
Roozbeh Pournader1051bbe2017-07-25 13:52:57 -07002104 // Use the ellipsis string only if there are that at least as many characters to replace.
2105 final boolean useEllipsisString = ellipsisCount >= ellipsisStringLen;
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002106 for (int i = 0; i < ellipsisCount; i++) {
2107 final char c;
Roozbeh Pournader1051bbe2017-07-25 13:52:57 -07002108 if (useEllipsisString && i < ellipsisStringLen) {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002109 c = ellipsisString.charAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 } else {
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002111 c = TextUtils.ELLIPSIS_FILLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
2113
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07002114 final int a = i + ellipsisStart + lineStart;
2115 if (start <= a && a < end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 dest[destoff + a - start] = c;
2117 }
2118 }
2119 }
2120
2121 /**
2122 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08002123 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 */
2125 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08002126 // Directions represents directional runs within a line of text.
2127 // Runs are pairs of ints listed in visual order, starting from the
2128 // leading margin. The first int of each pair is the offset from
2129 // the first character of the line to the start of the run. The
2130 // second int represents both the length and level of the run.
2131 // The length is in the lower bits, accessed by masking with
2132 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
2133 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
2134 // To simply test for an RTL direction, test the bit using
2135 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136
Siyamed Sinired09ae12016-02-16 14:36:26 -08002137 /**
2138 * @hide
2139 */
2140 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2141 public int[] mDirections;
2142
2143 /**
2144 * @hide
2145 */
2146 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2147 public Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 mDirections = dirs;
2149 }
2150 }
2151
2152 /**
2153 * Return the offset of the first character to be ellipsized away,
2154 * relative to the start of the line. (So 0 if the beginning of the
2155 * line is ellipsized, not getLineStart().)
2156 */
2157 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07002158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 /**
2160 * Returns the number of characters to be ellipsized away, or 0 if
2161 * no ellipsis is to take place.
2162 */
2163 public abstract int getEllipsisCount(int line);
2164
2165 /* package */ static class Ellipsizer implements CharSequence, GetChars {
2166 /* package */ CharSequence mText;
2167 /* package */ Layout mLayout;
2168 /* package */ int mWidth;
2169 /* package */ TextUtils.TruncateAt mMethod;
2170
2171 public Ellipsizer(CharSequence s) {
2172 mText = s;
2173 }
2174
2175 public char charAt(int off) {
2176 char[] buf = TextUtils.obtain(1);
2177 getChars(off, off + 1, buf, 0);
2178 char ret = buf[0];
2179
2180 TextUtils.recycle(buf);
2181 return ret;
2182 }
2183
2184 public void getChars(int start, int end, char[] dest, int destoff) {
2185 int line1 = mLayout.getLineForOffset(start);
2186 int line2 = mLayout.getLineForOffset(end);
2187
2188 TextUtils.getChars(mText, start, end, dest, destoff);
2189
2190 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002191 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 }
2193 }
2194
2195 public int length() {
2196 return mText.length();
2197 }
Doug Felt9f7a4442010-03-01 12:45:56 -08002198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 public CharSequence subSequence(int start, int end) {
2200 char[] s = new char[end - start];
2201 getChars(start, end, s, 0);
2202 return new String(s);
2203 }
2204
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002205 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 public String toString() {
2207 char[] s = new char[length()];
2208 getChars(0, length(), s, 0);
2209 return new String(s);
2210 }
2211
2212 }
2213
Gilles Debunne6c488de2012-03-01 16:20:35 -08002214 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 private Spanned mSpanned;
2216
2217 public SpannedEllipsizer(CharSequence display) {
2218 super(display);
2219 mSpanned = (Spanned) display;
2220 }
2221
2222 public <T> T[] getSpans(int start, int end, Class<T> type) {
2223 return mSpanned.getSpans(start, end, type);
2224 }
2225
2226 public int getSpanStart(Object tag) {
2227 return mSpanned.getSpanStart(tag);
2228 }
2229
2230 public int getSpanEnd(Object tag) {
2231 return mSpanned.getSpanEnd(tag);
2232 }
2233
2234 public int getSpanFlags(Object tag) {
2235 return mSpanned.getSpanFlags(tag);
2236 }
2237
Gilles Debunne6c488de2012-03-01 16:20:35 -08002238 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 public int nextSpanTransition(int start, int limit, Class type) {
2240 return mSpanned.nextSpanTransition(start, limit, type);
2241 }
2242
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002243 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 public CharSequence subSequence(int start, int end) {
2245 char[] s = new char[end - start];
2246 getChars(start, end, s, 0);
2247
2248 SpannableString ss = new SpannableString(new String(s));
2249 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
2250 return ss;
2251 }
2252 }
2253
2254 private CharSequence mText;
2255 private TextPaint mPaint;
Roozbeh Pournaderb1f03652017-08-08 15:49:01 -07002256 private TextPaint mWorkPaint = new TextPaint();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 private int mWidth;
2258 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
2259 private float mSpacingMult;
2260 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002261 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002263 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002264 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -07002265 private int mJustificationMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266
2267 public static final int DIR_LEFT_TO_RIGHT = 1;
2268 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002269
Doug Felt20178d62010-02-22 13:39:01 -08002270 /* package */ static final int DIR_REQUEST_LTR = 1;
2271 /* package */ static final int DIR_REQUEST_RTL = -1;
2272 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2273 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274
Doug Felt9f7a4442010-03-01 12:45:56 -08002275 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2276 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2277 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2278 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 public enum Alignment {
2281 ALIGN_NORMAL,
2282 ALIGN_OPPOSITE,
2283 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002284 /** @hide */
2285 ALIGN_LEFT,
2286 /** @hide */
2287 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
2289
2290 private static final int TAB_INCREMENT = 20;
2291
Siyamed Sinired09ae12016-02-16 14:36:26 -08002292 /** @hide */
2293 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2294 public static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002295 new Directions(new int[] { 0, RUN_LENGTH_MASK });
Siyamed Sinired09ae12016-02-16 14:36:26 -08002296
2297 /** @hide */
2298 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2299 public static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002300 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002301
Petar Ĺ eginafb748b32017-08-07 12:37:52 +01002302 /** @hide */
2303 @FunctionalInterface
2304 public interface RectangleConsumer {
2305 /**
2306 * Performs this operation on the given rectangle.
2307 *
2308 * @param left the left edge of the rectangle
2309 * @param top the top edge of the rectangle
2310 * @param right the right edge of the rectangle
2311 * @param bottom the bottom edge of the rectangle
2312 */
2313 void accept(float left, float top, float right, float bottom);
2314 }
2315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316}