blob: 2fc12d30ffd892ef190b5d9180775a2fb209e695 [file] [log] [blame]
Gilles Debunne60e3b3f2012-03-13 11:26:05 -07001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text;
18
Raph Levien39b4db72015-03-25 13:18:20 -070019import android.annotation.IntDef;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.graphics.Canvas;
21import android.graphics.Paint;
Doug Felt9f7a4442010-03-01 12:45:56 -080022import android.graphics.Path;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.text.method.TextKeyListener;
Doug Felt9f7a4442010-03-01 12:45:56 -080025import android.text.style.AlignmentSpan;
26import android.text.style.LeadingMarginSpan;
Gilles Debunne162bf0f2010-11-16 16:23:53 -080027import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
Doug Felt9f7a4442010-03-01 12:45:56 -080028import android.text.style.LineBackgroundSpan;
29import android.text.style.ParagraphStyle;
30import android.text.style.ReplacementSpan;
31import android.text.style.TabStopSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Siyamed Sinired09ae12016-02-16 14:36:26 -080033import com.android.internal.annotations.VisibleForTesting;
Doug Feltcb3791202011-07-07 11:57:48 -070034import com.android.internal.util.ArrayUtils;
Adam Lesinski776abc22014-03-07 11:30:59 -050035import com.android.internal.util.GrowingArrayUtils;
Doug Feltcb3791202011-07-07 11:57:48 -070036
Raph Levien39b4db72015-03-25 13:18:20 -070037import java.lang.annotation.Retention;
38import java.lang.annotation.RetentionPolicy;
Doug Feltc982f602010-05-25 11:51:40 -070039import java.util.Arrays;
Doug Felt9f7a4442010-03-01 12:45:56 -080040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/**
Doug Felt9f7a4442010-03-01 12:45:56 -080042 * A base class that manages text layout in visual elements on
43 * the screen.
44 * <p>For text that will be edited, use a {@link DynamicLayout},
45 * which will be updated as the text changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 * For text that will not change, use a {@link StaticLayout}.
47 */
48public abstract class Layout {
Raph Levien39b4db72015-03-25 13:18:20 -070049 /** @hide */
50 @IntDef({BREAK_STRATEGY_SIMPLE, BREAK_STRATEGY_HIGH_QUALITY, BREAK_STRATEGY_BALANCED})
51 @Retention(RetentionPolicy.SOURCE)
52 public @interface BreakStrategy {}
53
54 /**
55 * Value for break strategy indicating simple line breaking. Automatic hyphens are not added
56 * (though soft hyphens are respected), and modifying text generally doesn't affect the layout
57 * before it (which yields a more consistent user experience when editing), but layout may not
58 * be the highest quality.
59 */
60 public static final int BREAK_STRATEGY_SIMPLE = 0;
61
62 /**
63 * Value for break strategy indicating high quality line breaking, including automatic
64 * hyphenation and doing whole-paragraph optimization of line breaks.
65 */
66 public static final int BREAK_STRATEGY_HIGH_QUALITY = 1;
67
68 /**
69 * Value for break strategy indicating balanced line breaking. The breaks are chosen to
70 * make all lines as close to the same length as possible, including automatic hyphenation.
71 */
72 public static final int BREAK_STRATEGY_BALANCED = 2;
73
Roozbeh Pournader95c7a132015-05-12 12:01:06 -070074 /** @hide */
75 @IntDef({HYPHENATION_FREQUENCY_NORMAL, HYPHENATION_FREQUENCY_FULL,
76 HYPHENATION_FREQUENCY_NONE})
77 @Retention(RetentionPolicy.SOURCE)
78 public @interface HyphenationFrequency {}
79
80 /**
81 * Value for hyphenation frequency indicating no automatic hyphenation. Useful
82 * for backward compatibility, and for cases where the automatic hyphenation algorithm results
83 * in incorrect hyphenation. Mid-word breaks may still happen when a word is wider than the
84 * layout and there is otherwise no valid break. Soft hyphens are ignored and will not be used
85 * as suggestions for potential line breaks.
86 */
87 public static final int HYPHENATION_FREQUENCY_NONE = 0;
88
89 /**
90 * Value for hyphenation frequency indicating a light amount of automatic hyphenation, which
91 * is a conservative default. Useful for informal cases, such as short sentences or chat
92 * messages.
93 */
94 public static final int HYPHENATION_FREQUENCY_NORMAL = 1;
95
96 /**
97 * Value for hyphenation frequency indicating the full amount of automatic hyphenation, typical
98 * in typography. Useful for running text and where it's important to put the maximum amount of
99 * text in a screen with limited space.
100 */
101 public static final int HYPHENATION_FREQUENCY_FULL = 2;
102
Doug Felt71b8dd72010-02-16 17:27:09 -0800103 private static final ParagraphStyle[] NO_PARA_SPANS =
104 ArrayUtils.emptyArray(ParagraphStyle.class);
Dave Bort76c02262009-04-13 17:17:21 -0700105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700107 * Return how wide a layout must be in order to display the specified text with one line per
108 * paragraph.
109 *
110 * <p>As of O, Uses
111 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
112 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
114 public static float getDesiredWidth(CharSequence source,
115 TextPaint paint) {
116 return getDesiredWidth(source, 0, source.length(), paint);
117 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700120 * Return how wide a layout must be in order to display the specified text slice with one
121 * line per paragraph.
122 *
123 * <p>As of O, Uses
124 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
125 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
126 */
127 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) {
128 return getDesiredWidth(source, start, end, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR);
129 }
130
131 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800132 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * specified text slice with one line per paragraph.
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700134 *
135 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700137 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint,
138 TextDirectionHeuristic textDir) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
141 int next;
142 for (int i = start; i <= end; i = next) {
143 next = TextUtils.indexOf(source, '\n', i, end);
144
145 if (next < 0)
146 next = end;
147
Doug Felt71b8dd72010-02-16 17:27:09 -0800148 // note, omits trailing paragraph char
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700149 float w = measurePara(paint, source, i, next, textDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
151 if (w > need)
152 need = w;
153
154 next++;
155 }
156
157 return need;
158 }
159
160 /**
161 * Subclasses of Layout use this constructor to set the display text,
162 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800163 * @param text the text to render
164 * @param paint the default paint for the layout. Styles can override
165 * various attributes of the paint.
166 * @param width the wrapping width for the text.
167 * @param align whether to left, right, or center the text. Styles can
168 * override the alignment.
169 * @param spacingMult factor by which to scale the font size to get the
170 * default line spacing
171 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 */
173 protected Layout(CharSequence text, TextPaint paint,
174 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800175 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700176 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
177 spacingMult, spacingAdd);
178 }
179
180 /**
181 * Subclasses of Layout use this constructor to set the display text,
182 * width, and other standard properties.
183 * @param text the text to render
184 * @param paint the default paint for the layout. Styles can override
185 * various attributes of the paint.
186 * @param width the wrapping width for the text.
187 * @param align whether to left, right, or center the text. Styles can
188 * override the alignment.
189 * @param spacingMult factor by which to scale the font size to get the
190 * default line spacing
191 * @param spacingAdd amount to add to the default line spacing
192 *
193 * @hide
194 */
195 protected Layout(CharSequence text, TextPaint paint,
196 int width, Alignment align, TextDirectionHeuristic textDir,
197 float spacingMult, float spacingAdd) {
198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 if (width < 0)
200 throw new IllegalArgumentException("Layout: " + width + " < 0");
201
Doug Felte8e45f22010-03-29 14:58:40 -0700202 // Ensure paint doesn't have baselineShift set.
203 // While normally we don't modify the paint the user passed in,
204 // we were already doing this in Styled.drawUniformRun with both
205 // baselineShift and bgColor. We probably should reevaluate bgColor.
206 if (paint != null) {
207 paint.bgColor = 0;
208 paint.baselineShift = 0;
209 }
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 mText = text;
212 mPaint = paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 mWidth = width;
214 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800215 mSpacingMult = spacingMult;
216 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700218 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 }
220
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900221 /** @hide */
222 protected void setJustify(boolean justify) {
223 mJustify = justify;
224 }
225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
227 * Replace constructor properties of this Layout with new ones. Be careful.
228 */
229 /* package */ void replaceWith(CharSequence text, TextPaint paint,
230 int width, Alignment align,
231 float spacingmult, float spacingadd) {
232 if (width < 0) {
233 throw new IllegalArgumentException("Layout: " + width + " < 0");
234 }
235
236 mText = text;
237 mPaint = paint;
238 mWidth = width;
239 mAlignment = align;
240 mSpacingMult = spacingmult;
241 mSpacingAdd = spacingadd;
242 mSpannedText = text instanceof Spanned;
243 }
244
245 /**
246 * Draw this Layout on the specified Canvas.
247 */
248 public void draw(Canvas c) {
249 draw(c, null, null, 0);
250 }
251
252 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800253 * Draw this Layout on the specified canvas, with the highlight path drawn
254 * between the background and the text.
255 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800256 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800257 * @param highlight the path of the highlight or cursor; can be null
258 * @param highlightPaint the paint for the highlight
259 * @param cursorOffsetVertical the amount to temporarily translate the
260 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800262 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
263 int cursorOffsetVertical) {
264 final long lineRange = getLineRangeForDraw(canvas);
265 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
266 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
267 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268
Gilles Debunne6c488de2012-03-01 16:20:35 -0800269 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
270 firstLine, lastLine);
271 drawText(canvas, firstLine, lastLine);
272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900274 private boolean isJustificationRequired(int lineNum) {
275 if (!mJustify) return false;
276 final int lineEnd = getLineEnd(lineNum);
277 return lineEnd < mText.length() && mText.charAt(lineEnd - 1) != '\n';
278 }
279
280 private float getJustifyWidth(int lineNum) {
281 Alignment paraAlign = mAlignment;
282 TabStops tabStops = null;
283 boolean tabStopsIsInitialized = false;
284
285 int left = 0;
286 int right = mWidth;
287
288 final int dir = getParagraphDirection(lineNum);
289
290 ParagraphStyle[] spans = NO_PARA_SPANS;
291 if (mSpannedText) {
292 Spanned sp = (Spanned) mText;
293 final int start = getLineStart(lineNum);
294
295 final boolean isFirstParaLine = (start == 0 || mText.charAt(start - 1) == '\n');
296
297 if (isFirstParaLine) {
298 final int spanEnd = sp.nextSpanTransition(start, mText.length(),
299 ParagraphStyle.class);
300 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
301
302 for (int n = spans.length - 1; n >= 0; n--) {
303 if (spans[n] instanceof AlignmentSpan) {
304 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
305 break;
306 }
307 }
308 }
309
310 final int length = spans.length;
311 boolean useFirstLineMargin = isFirstParaLine;
312 for (int n = 0; n < length; n++) {
313 if (spans[n] instanceof LeadingMarginSpan2) {
314 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
315 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
316 if (lineNum < startLine + count) {
317 useFirstLineMargin = true;
318 break;
319 }
320 }
321 }
322 for (int n = 0; n < length; n++) {
323 if (spans[n] instanceof LeadingMarginSpan) {
324 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
325 if (dir == DIR_RIGHT_TO_LEFT) {
326 right -= margin.getLeadingMargin(useFirstLineMargin);
327 } else {
328 left += margin.getLeadingMargin(useFirstLineMargin);
329 }
330 }
331 }
332 }
333
334 if (getLineContainsTab(lineNum)) {
335 tabStops = new TabStops(TAB_INCREMENT, spans);
336 }
337
338 final Alignment align;
339 if (paraAlign == Alignment.ALIGN_LEFT) {
340 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
341 } else if (paraAlign == Alignment.ALIGN_RIGHT) {
342 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
343 } else {
344 align = paraAlign;
345 }
346
347 final int indentWidth;
348 if (align == Alignment.ALIGN_NORMAL) {
349 if (dir == DIR_LEFT_TO_RIGHT) {
350 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
351 } else {
352 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
353 }
354 } else if (align == Alignment.ALIGN_OPPOSITE) {
355 if (dir == DIR_LEFT_TO_RIGHT) {
356 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
357 } else {
358 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
359 }
360 } else { // Alignment.ALIGN_CENTER
361 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
362 }
363
364 return right - left - indentWidth;
365 }
366
Gilles Debunne6c488de2012-03-01 16:20:35 -0800367 /**
368 * @hide
369 */
370 public void drawText(Canvas canvas, int firstLine, int lastLine) {
371 int previousLineBottom = getLineTop(firstLine);
372 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800373 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700374 int spanEnd = 0;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900375 final TextPaint paint = mPaint;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800376 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700378 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700379 TabStops tabStops = null;
380 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800381
Doug Felte8e45f22010-03-29 14:58:40 -0700382 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700383
Gilles Debunne6c488de2012-03-01 16:20:35 -0800384 // Draw the lines, one at a time.
385 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700386 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700388 previousLineEnd = getLineStart(lineNum + 1);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900389 final boolean justify = isJustificationRequired(lineNum);
Raph Levien26d443a2015-03-30 14:18:32 -0700390 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391
392 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700393 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700395 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396
Raph Levien26d443a2015-03-30 14:18:32 -0700397 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700398 int left = 0;
399 int right = mWidth;
400
Gilles Debunne6c488de2012-03-01 16:20:35 -0800401 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700402 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800403 int textLength = buf.length();
404 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700405
Doug Feltc982f602010-05-25 11:51:40 -0700406 // New batch of paragraph styles, collect into spans array.
407 // Compute the alignment, last alignment style wins.
408 // Reset tabStops, we'll rebuild if we encounter a line with
409 // tabs.
410 // We expect paragraph spans to be relatively infrequent, use
411 // spanEnd so that we can check less frequently. Since
412 // paragraph styles ought to apply to entire paragraphs, we can
413 // just collect the ones present at the start of the paragraph.
414 // If spanEnd is before the end of the paragraph, that's not
415 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700416 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700417 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700419 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800420
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700421 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800422 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700424 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 break;
426 }
427 }
Doug Felt0c702b82010-05-14 10:55:42 -0700428
Doug Feltc982f602010-05-25 11:51:40 -0700429 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800431
Doug Feltc982f602010-05-25 11:51:40 -0700432 // Draw all leading margin spans. Adjust left or right according
433 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700435 boolean useFirstLineMargin = isFirstParaLine;
436 for (int n = 0; n < length; n++) {
437 if (spans[n] instanceof LeadingMarginSpan2) {
438 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
439 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
440 // if there is more than one LeadingMarginSpan2, use
441 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700442 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700443 useFirstLineMargin = true;
444 break;
445 }
446 }
447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 for (int n = 0; n < length; n++) {
449 if (spans[n] instanceof LeadingMarginSpan) {
450 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800452 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800454 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700455 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800457 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800459 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700460 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462 }
463 }
464 }
465
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700466 boolean hasTab = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700467 // Can't tell if we have tabs for sure, currently
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700468 if (hasTab && !tabStopsIsInitialized) {
Doug Feltc982f602010-05-25 11:51:40 -0700469 if (tabStops == null) {
470 tabStops = new TabStops(TAB_INCREMENT, spans);
471 } else {
472 tabStops.reset(TAB_INCREMENT, spans);
473 }
474 tabStopsIsInitialized = true;
475 }
476
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700477 // Determine whether the line aligns to normal, opposite, or center.
478 Alignment align = paraAlign;
479 if (align == Alignment.ALIGN_LEFT) {
480 align = (dir == DIR_LEFT_TO_RIGHT) ?
481 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
482 } else if (align == Alignment.ALIGN_RIGHT) {
483 align = (dir == DIR_LEFT_TO_RIGHT) ?
484 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
485 }
486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 int x;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900488 final int indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 if (align == Alignment.ALIGN_NORMAL) {
490 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900491 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
492 x = left + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900494 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
495 x = right - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 }
497 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700498 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700500 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900501 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
502 x = right - max - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900504 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
505 x = left - max + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
Doug Feltc982f602010-05-25 11:51:40 -0700507 } else { // Alignment.ALIGN_CENTER
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900508 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700509 max = max & ~1;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900510 x = ((right + left - max) >> 1) + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512 }
513
Raph Levien26d443a2015-03-30 14:18:32 -0700514 paint.setHyphenEdit(getHyphen(lineNum));
515 Directions directions = getLineDirections(lineNum);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900516 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab && !justify) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800517 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800518 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 } else {
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700520 tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900521 if (justify) {
522 tl.justify(right - left - indentWidth);
523 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800524 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
Raph Levien9a174dd2015-04-08 13:35:03 -0700526 paint.setHyphenEdit(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
Doug Feltc982f602010-05-25 11:51:40 -0700528
Doug Felte8e45f22010-03-29 14:58:40 -0700529 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 }
531
532 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800533 * @hide
534 */
535 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
536 int cursorOffsetVertical, int firstLine, int lastLine) {
537 // First, draw LineBackgroundSpans.
538 // LineBackgroundSpans know nothing about the alignment, margins, or
539 // direction of the layout or line. XXX: Should they?
540 // They are evaluated at each line.
541 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700542 if (mLineBackgroundSpans == null) {
543 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700544 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800545
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700546 Spanned buffer = (Spanned) mText;
547 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700548 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800549
Gilles Debunneeca5b732012-04-25 18:48:42 -0700550 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700551 int previousLineBottom = getLineTop(firstLine);
552 int previousLineEnd = getLineStart(firstLine);
553 ParagraphStyle[] spans = NO_PARA_SPANS;
554 int spansLength = 0;
555 TextPaint paint = mPaint;
556 int spanEnd = 0;
557 final int width = mWidth;
558 for (int i = firstLine; i <= lastLine; i++) {
559 int start = previousLineEnd;
560 int end = getLineStart(i + 1);
561 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800562
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700563 int ltop = previousLineBottom;
564 int lbottom = getLineTop(i + 1);
565 previousLineBottom = lbottom;
566 int lbaseline = lbottom - getLineDescent(i);
567
568 if (start >= spanEnd) {
569 // These should be infrequent, so we'll use this so that
570 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700571 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700572 // All LineBackgroundSpans on a line contribute to its background.
573 spansLength = 0;
574 // Duplication of the logic of getParagraphSpans
575 if (start != end || start == 0) {
576 // Equivalent to a getSpans(start, end), but filling the 'spans' local
577 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700578 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
579 // equal test is valid since both intervals are not empty by
580 // construction
581 if (mLineBackgroundSpans.spanStarts[j] >= end ||
582 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500583 spans = GrowingArrayUtils.append(
584 spans, spansLength, mLineBackgroundSpans.spans[j]);
585 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700586 }
587 }
588 }
589
590 for (int n = 0; n < spansLength; n++) {
591 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
592 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
593 ltop, lbaseline, lbottom,
594 buffer, start, end, i);
595 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800596 }
597 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700598 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800599 }
600
601 // There can be a highlight even without spans if we are drawing
602 // a non-spanned transformation of a spanned editing buffer.
603 if (highlight != null) {
604 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
605 canvas.drawPath(highlight, highlightPaint);
606 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
607 }
608 }
609
610 /**
611 * @param canvas
612 * @return The range of lines that need to be drawn, possibly empty.
613 * @hide
614 */
615 public long getLineRangeForDraw(Canvas canvas) {
616 int dtop, dbottom;
617
618 synchronized (sTempRect) {
619 if (!canvas.getClipBounds(sTempRect)) {
620 // Negative range end used as a special flag
621 return TextUtils.packRangeInLong(0, -1);
622 }
623
624 dtop = sTempRect.top;
625 dbottom = sTempRect.bottom;
626 }
627
628 final int top = Math.max(dtop, 0);
629 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
630
Gilles Debunne2fba3382012-06-11 17:46:24 -0700631 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800632 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
633 }
634
635 /**
Doug Feltc982f602010-05-25 11:51:40 -0700636 * Return the start position of the line, given the left and right bounds
637 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700638 *
Doug Feltc982f602010-05-25 11:51:40 -0700639 * @param line the line index
640 * @param left the left bounds (0, or leading margin if ltr para)
641 * @param right the right bounds (width, minus leading margin if rtl para)
642 * @return the start position of the line (to right of line if rtl para)
643 */
644 private int getLineStartPos(int line, int left, int right) {
645 // Adjust the point at which to start rendering depending on the
646 // alignment of the paragraph.
647 Alignment align = getParagraphAlignment(line);
648 int dir = getParagraphDirection(line);
649
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700650 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700651 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
652 } else if (align == Alignment.ALIGN_RIGHT) {
653 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
654 }
655
656 int x;
657 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700658 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700659 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700660 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700661 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700662 }
663 } else {
664 TabStops tabStops = null;
665 if (mSpannedText && getLineContainsTab(line)) {
666 Spanned spanned = (Spanned) mText;
667 int start = getLineStart(line);
668 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
669 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800670 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
671 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700672 if (tabSpans.length > 0) {
673 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
674 }
675 }
676 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700677 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700678 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700679 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700680 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700681 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700682 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700683 }
684 } else { // Alignment.ALIGN_CENTER
685 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700686 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700687 }
688 }
689 return x;
690 }
691
692 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 * Return the text that is displayed by this Layout.
694 */
695 public final CharSequence getText() {
696 return mText;
697 }
698
699 /**
700 * Return the base Paint properties for this layout.
701 * Do NOT change the paint, which may result in funny
702 * drawing for this layout.
703 */
704 public final TextPaint getPaint() {
705 return mPaint;
706 }
707
708 /**
709 * Return the width of this layout.
710 */
711 public final int getWidth() {
712 return mWidth;
713 }
714
715 /**
716 * Return the width to which this Layout is ellipsizing, or
717 * {@link #getWidth} if it is not doing anything special.
718 */
719 public int getEllipsizedWidth() {
720 return mWidth;
721 }
722
723 /**
724 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800725 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 * it does not cause the text to reflow to use the full new width.
727 */
728 public final void increaseWidthTo(int wid) {
729 if (wid < mWidth) {
730 throw new RuntimeException("attempted to reduce Layout width");
731 }
732
733 mWidth = wid;
734 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 /**
737 * Return the total height of this layout.
738 */
739 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800740 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
743 /**
Siyamed Sinir0745c722016-05-31 20:39:33 -0700744 * Return the total height of this layout.
745 *
746 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
747 *
748 * @hide
749 */
750 public int getHeight(boolean cap) {
751 return getHeight();
752 }
753
754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 * Return the base alignment of this layout.
756 */
757 public final Alignment getAlignment() {
758 return mAlignment;
759 }
760
761 /**
762 * Return what the text height is multiplied by to get the line height.
763 */
764 public final float getSpacingMultiplier() {
765 return mSpacingMult;
766 }
767
768 /**
769 * Return the number of units of leading that are added to each line.
770 */
771 public final float getSpacingAdd() {
772 return mSpacingAdd;
773 }
774
775 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700776 * Return the heuristic used to determine paragraph text direction.
777 * @hide
778 */
779 public final TextDirectionHeuristic getTextDirectionHeuristic() {
780 return mTextDir;
781 }
782
783 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 * Return the number of lines of text in this layout.
785 */
786 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 /**
789 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
790 * If bounds is not null, return the top, left, right, bottom extents
791 * of the specified line in it.
792 * @param line which line to examine (0..getLineCount() - 1)
793 * @param bounds Optional. If not null, it returns the extent of the line
794 * @return the Y-coordinate of the baseline
795 */
796 public int getLineBounds(int line, Rect bounds) {
797 if (bounds != null) {
798 bounds.left = 0; // ???
799 bounds.top = getLineTop(line);
800 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800801 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 }
803 return getLineBaseline(line);
804 }
805
806 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800807 * Return the vertical position of the top of the specified line
808 * (0&hellip;getLineCount()).
809 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 * bottom of the last line.
811 */
812 public abstract int getLineTop(int line);
813
814 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800815 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 */
817 public abstract int getLineDescent(int line);
818
819 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800820 * Return the text offset of the beginning of the specified line (
821 * 0&hellip;getLineCount()). If the specified line is equal to the line
822 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 */
824 public abstract int getLineStart(int line);
825
826 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800827 * Returns the primary directionality of the paragraph containing the
828 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
829 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 */
831 public abstract int getParagraphDirection(int line);
832
833 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700834 * Returns whether the specified line contains one or more
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700835 * characters that need to be handled specially, like tabs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 */
837 public abstract boolean getLineContainsTab(int line);
838
839 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800840 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 * The array alternates counts of characters in left-to-right
842 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800843 *
844 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 */
846 public abstract Directions getLineDirections(int line);
847
848 /**
849 * Returns the (negative) number of extra pixels of ascent padding in the
850 * top line of the Layout.
851 */
852 public abstract int getTopPadding();
853
854 /**
855 * Returns the number of extra pixels of descent padding in the
856 * bottom line of the Layout.
857 */
858 public abstract int getBottomPadding();
859
Raph Levien26d443a2015-03-30 14:18:32 -0700860 /**
861 * Returns the hyphen edit for a line.
862 *
863 * @hide
864 */
865 public int getHyphen(int line) {
866 return 0;
867 }
868
Raph Levien2ea52902015-07-01 14:39:31 -0700869 /**
870 * Returns the left indent for a line.
871 *
872 * @hide
873 */
874 public int getIndentAdjust(int line, Alignment alignment) {
875 return 0;
876 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700877
878 /**
879 * Returns true if the character at offset and the preceding character
880 * are at different run levels (and thus there's a split caret).
881 * @param offset the offset
882 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800883 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700884 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800885 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800886 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700887 Directions dirs = getLineDirections(line);
888 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
889 return false;
890 }
891
892 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800893 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700894 int lineEnd = getLineEnd(line);
895 if (offset == lineStart || offset == lineEnd) {
896 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
897 int runIndex = offset == lineStart ? 0 : runs.length - 2;
898 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
899 }
900
901 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800902 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700903 if (offset == runs[i]) {
904 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800905 }
906 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700907 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800908 }
909
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700910 /**
911 * Returns true if the character at offset is right to left (RTL).
912 * @param offset the offset
913 * @return true if the character is RTL, false if it is LTR
914 */
915 public boolean isRtlCharAt(int offset) {
916 int line = getLineForOffset(offset);
917 Directions dirs = getLineDirections(line);
918 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
919 return false;
920 }
921 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
922 return true;
923 }
924 int[] runs = dirs.mDirections;
925 int lineStart = getLineStart(line);
926 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700927 int start = lineStart + runs[i];
928 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
929 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700930 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
931 return ((level & 1) != 0);
932 }
933 }
934 // Should happen only if the offset is "out of bounds"
935 return false;
936 }
937
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900938 /**
939 * Returns the range of the run that the character at offset belongs to.
940 * @param offset the offset
941 * @return The range of the run
942 * @hide
943 */
944 public long getRunRange(int offset) {
945 int line = getLineForOffset(offset);
946 Directions dirs = getLineDirections(line);
947 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
948 return TextUtils.packRangeInLong(0, getLineEnd(line));
949 }
950 int[] runs = dirs.mDirections;
951 int lineStart = getLineStart(line);
952 for (int i = 0; i < runs.length; i += 2) {
953 int start = lineStart + runs[i];
954 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
955 if (offset >= start && offset < limit) {
956 return TextUtils.packRangeInLong(start, limit);
957 }
958 }
959 // Should happen only if the offset is "out of bounds"
960 return TextUtils.packRangeInLong(0, getLineEnd(line));
961 }
962
Doug Felt9f7a4442010-03-01 12:45:56 -0800963 private boolean primaryIsTrailingPrevious(int offset) {
964 int line = getLineForOffset(offset);
965 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700966 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800967 int[] runs = getLineDirections(line).mDirections;
968
969 int levelAt = -1;
970 for (int i = 0; i < runs.length; i += 2) {
971 int start = lineStart + runs[i];
972 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
973 if (limit > lineEnd) {
974 limit = lineEnd;
975 }
976 if (offset >= start && offset < limit) {
977 if (offset > start) {
978 // Previous character is at same level, so don't use trailing.
979 return false;
980 }
981 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
982 break;
983 }
984 }
985 if (levelAt == -1) {
986 // Offset was limit of line.
987 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
988 }
989
990 // At level boundary, check previous level.
991 int levelBefore = -1;
992 if (offset == lineStart) {
993 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
994 } else {
995 offset -= 1;
996 for (int i = 0; i < runs.length; i += 2) {
997 int start = lineStart + runs[i];
998 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
999 if (limit > lineEnd) {
1000 limit = lineEnd;
1001 }
1002 if (offset >= start && offset < limit) {
1003 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1004 break;
1005 }
1006 }
1007 }
1008
1009 return levelBefore < levelAt;
1010 }
1011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 /**
1013 * Get the primary horizontal position for the specified text offset.
1014 * This is the location where a new character would be inserted in
1015 * the paragraph's primary direction.
1016 */
1017 public float getPrimaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001018 return getPrimaryHorizontal(offset, false /* not clamped */);
1019 }
1020
1021 /**
1022 * Get the primary horizontal position for the specified text offset, but
1023 * optionally clamp it so that it doesn't exceed the width of the layout.
1024 * @hide
1025 */
1026 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001027 boolean trailing = primaryIsTrailingPrevious(offset);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001028 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 }
1030
1031 /**
1032 * Get the secondary horizontal position for the specified text offset.
1033 * This is the location where a new character would be inserted in
1034 * the direction other than the paragraph's primary direction.
1035 */
1036 public float getSecondaryHorizontal(int offset) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001037 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
1039
Raph Levienafe8e9b2012-12-19 16:09:32 -08001040 /**
1041 * Get the secondary horizontal position for the specified text offset, but
1042 * optionally clamp it so that it doesn't exceed the width of the layout.
1043 * @hide
1044 */
1045 public float getSecondaryHorizontal(int offset, boolean clamped) {
1046 boolean trailing = primaryIsTrailingPrevious(offset);
1047 return getHorizontal(offset, !trailing, clamped);
1048 }
1049
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001050 private float getHorizontal(int offset, boolean primary) {
1051 return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset);
1052 }
1053
Raph Levienafe8e9b2012-12-19 16:09:32 -08001054 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 int line = getLineForOffset(offset);
1056
Raph Levienafe8e9b2012-12-19 16:09:32 -08001057 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
1059
Raph Levienafe8e9b2012-12-19 16:09:32 -08001060 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001062 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 int dir = getParagraphDirection(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001064 boolean hasTab = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 Directions directions = getLineDirections(line);
1066
Doug Feltc982f602010-05-25 11:51:40 -07001067 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001068 if (hasTab && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001069 // Just checking this line should be good enough, tabs should be
1070 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001071 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001072 if (tabs.length > 0) {
1073 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076
Doug Felte8e45f22010-03-29 14:58:40 -07001077 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001078 tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001079 float wid = tl.measure(offset - start, trailing, null);
1080 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081
Raph Levienafe8e9b2012-12-19 16:09:32 -08001082 if (clamped && wid > mWidth) {
1083 wid = mWidth;
1084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 int left = getParagraphLeft(line);
1086 int right = getParagraphRight(line);
1087
Doug Feltc982f602010-05-25 11:51:40 -07001088 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
1090
1091 /**
1092 * Get the leftmost position that should be exposed for horizontal
1093 * scrolling on the specified line.
1094 */
1095 public float getLineLeft(int line) {
1096 int dir = getParagraphDirection(line);
1097 Alignment align = getParagraphAlignment(line);
1098
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001099 if (align == Alignment.ALIGN_LEFT) {
1100 return 0;
1101 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 if (dir == DIR_RIGHT_TO_LEFT)
1103 return getParagraphRight(line) - getLineMax(line);
1104 else
1105 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001106 } else if (align == Alignment.ALIGN_RIGHT) {
1107 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 } else if (align == Alignment.ALIGN_OPPOSITE) {
1109 if (dir == DIR_RIGHT_TO_LEFT)
1110 return 0;
1111 else
1112 return mWidth - getLineMax(line);
1113 } else { /* align == Alignment.ALIGN_CENTER */
1114 int left = getParagraphLeft(line);
1115 int right = getParagraphRight(line);
1116 int max = ((int) getLineMax(line)) & ~1;
1117
1118 return left + ((right - left) - max) / 2;
1119 }
1120 }
1121
1122 /**
1123 * Get the rightmost position that should be exposed for horizontal
1124 * scrolling on the specified line.
1125 */
1126 public float getLineRight(int line) {
1127 int dir = getParagraphDirection(line);
1128 Alignment align = getParagraphAlignment(line);
1129
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001130 if (align == Alignment.ALIGN_LEFT) {
1131 return getParagraphLeft(line) + getLineMax(line);
1132 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 if (dir == DIR_RIGHT_TO_LEFT)
1134 return mWidth;
1135 else
1136 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001137 } else if (align == Alignment.ALIGN_RIGHT) {
1138 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 } else if (align == Alignment.ALIGN_OPPOSITE) {
1140 if (dir == DIR_RIGHT_TO_LEFT)
1141 return getLineMax(line);
1142 else
1143 return mWidth;
1144 } else { /* align == Alignment.ALIGN_CENTER */
1145 int left = getParagraphLeft(line);
1146 int right = getParagraphRight(line);
1147 int max = ((int) getLineMax(line)) & ~1;
1148
1149 return right - ((right - left) - max) / 2;
1150 }
1151 }
1152
1153 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001154 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001155 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 */
1157 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001158 float margin = getParagraphLeadingMargin(line);
1159 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001160 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162
1163 /**
Doug Feltc982f602010-05-25 11:51:40 -07001164 * Gets the unsigned horizontal extent of the specified line, including
1165 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 */
1167 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001168 float margin = getParagraphLeadingMargin(line);
1169 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001170 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172
Doug Feltc982f602010-05-25 11:51:40 -07001173 /**
1174 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1175 * tab stops instead of using the ones passed in.
1176 * @param line the index of the line
1177 * @param full whether to include trailing whitespace
1178 * @return the extent of the line
1179 */
1180 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001182 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001183
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001184 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001185 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001186 if (hasTabs && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001187 // Just checking this line should be good enough, tabs should be
1188 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001189 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001190 if (tabs.length > 0) {
1191 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1192 }
1193 }
Doug Felte8e45f22010-03-29 14:58:40 -07001194 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001195 // Returned directions can actually be null
1196 if (directions == null) {
1197 return 0f;
1198 }
Doug Feltc982f602010-05-25 11:51:40 -07001199 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200
Doug Felte8e45f22010-03-29 14:58:40 -07001201 TextLine tl = TextLine.obtain();
Seigo Nonaka9863f672016-12-14 17:56:10 +09001202 mPaint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001203 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001204 if (isJustificationRequired(line)) {
1205 tl.justify(getJustifyWidth(line));
1206 }
Doug Feltc982f602010-05-25 11:51:40 -07001207 float width = tl.metrics(null);
Seigo Nonaka9863f672016-12-14 17:56:10 +09001208 mPaint.setHyphenEdit(0);
Doug Feltc982f602010-05-25 11:51:40 -07001209 TextLine.recycle(tl);
1210 return width;
1211 }
1212
1213 /**
1214 * Returns the signed horizontal extent of the specified line, excluding
1215 * leading margin. If full is false, excludes trailing whitespace.
1216 * @param line the index of the line
1217 * @param tabStops the tab stops, can be null if we know they're not used.
1218 * @param full whether to include trailing whitespace
1219 * @return the extent of the text on this line
1220 */
1221 private float getLineExtent(int line, TabStops tabStops, boolean full) {
1222 int start = getLineStart(line);
1223 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001224 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001225 Directions directions = getLineDirections(line);
1226 int dir = getParagraphDirection(line);
1227
1228 TextLine tl = TextLine.obtain();
Seigo Nonaka9863f672016-12-14 17:56:10 +09001229 mPaint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001230 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001231 if (isJustificationRequired(line)) {
1232 tl.justify(getJustifyWidth(line));
1233 }
Doug Felte8e45f22010-03-29 14:58:40 -07001234 float width = tl.metrics(null);
Seigo Nonaka9863f672016-12-14 17:56:10 +09001235 mPaint.setHyphenEdit(0);
Doug Felte8e45f22010-03-29 14:58:40 -07001236 TextLine.recycle(tl);
1237 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 }
1239
1240 /**
1241 * Get the line number corresponding to the specified vertical position.
1242 * If you ask for a position above 0, you get 0; if you ask for a position
1243 * below the bottom of the text, you get the last line.
1244 */
1245 // FIXME: It may be faster to do a linear search for layouts without many lines.
1246 public int getLineForVertical(int vertical) {
1247 int high = getLineCount(), low = -1, guess;
1248
1249 while (high - low > 1) {
1250 guess = (high + low) / 2;
1251
1252 if (getLineTop(guess) > vertical)
1253 high = guess;
1254 else
1255 low = guess;
1256 }
1257
1258 if (low < 0)
1259 return 0;
1260 else
1261 return low;
1262 }
1263
1264 /**
1265 * Get the line number on which the specified text offset appears.
1266 * If you ask for a position before 0, you get 0; if you ask for a position
1267 * beyond the end of the text, you get the last line.
1268 */
1269 public int getLineForOffset(int offset) {
1270 int high = getLineCount(), low = -1, guess;
1271
1272 while (high - low > 1) {
1273 guess = (high + low) / 2;
1274
1275 if (getLineStart(guess) > offset)
1276 high = guess;
1277 else
1278 low = guess;
1279 }
1280
1281 if (low < 0)
1282 return 0;
1283 else
1284 return low;
1285 }
1286
1287 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001288 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 * closest to the specified horizontal position.
1290 */
1291 public int getOffsetForHorizontal(int line, float horiz) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001292 return getOffsetForHorizontal(line, horiz, true);
1293 }
1294
1295 /**
1296 * Get the character offset on the specified line whose position is
1297 * closest to the specified horizontal position.
1298 *
1299 * @param line the line used to find the closest offset
1300 * @param horiz the horizontal position used to find the closest offset
1301 * @param primary whether to use the primary position or secondary position to find the offset
1302 *
1303 * @hide
1304 */
1305 public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
Raph Levienedb27f12015-06-01 14:34:47 -07001306 // TODO: use Paint.getOffsetForAdvance to avoid binary search
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001307 final int lineEndOffset = getLineEnd(line);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001308 final int lineStartOffset = getLineStart(line);
1309
1310 Directions dirs = getLineDirections(line);
1311
1312 TextLine tl = TextLine.obtain();
1313 // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
1314 tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
1315 false, null);
1316
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001317 final int max;
1318 if (line == getLineCount() - 1) {
1319 max = lineEndOffset;
1320 } else {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001321 max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
1322 !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001323 }
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001324 int best = lineStartOffset;
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001325 float bestdist = Math.abs(getHorizontal(best, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326
Doug Felt9f7a4442010-03-01 12:45:56 -08001327 for (int i = 0; i < dirs.mDirections.length; i += 2) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001328 int here = lineStartOffset + dirs.mDirections[i];
Doug Felt9f7a4442010-03-01 12:45:56 -08001329 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001330 boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
1331 int swap = isRtl ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332
1333 if (there > max)
1334 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 int high = there - 1 + 1, low = here + 1 - 1, guess;
1336
1337 while (high - low > 1) {
1338 guess = (high + low) / 2;
1339 int adguess = getOffsetAtStartOf(guess);
1340
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001341 if (getHorizontal(adguess, primary) * swap >= horiz * swap)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 high = guess;
1343 else
1344 low = guess;
1345 }
1346
1347 if (low < here + 1)
1348 low = here + 1;
1349
1350 if (low < there) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001351 int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
1352 low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
1353 if (low >= here && low < there) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001354 float dist = Math.abs(getHorizontal(low, primary) - horiz);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001355 if (aft < there) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001356 float other = Math.abs(getHorizontal(aft, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001358 if (other < dist) {
1359 dist = other;
1360 low = aft;
1361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001364 if (dist < bestdist) {
1365 bestdist = dist;
1366 best = low;
1367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 }
1369 }
1370
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001371 float dist = Math.abs(getHorizontal(here, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372
1373 if (dist < bestdist) {
1374 bestdist = dist;
1375 best = here;
1376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 }
1378
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001379 float dist = Math.abs(getHorizontal(max, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
Raph Levien373b7a82013-09-20 15:11:52 -07001381 if (dist <= bestdist) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 bestdist = dist;
1383 best = max;
1384 }
1385
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001386 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 return best;
1388 }
1389
1390 /**
1391 * Return the text offset after the last character on the specified line.
1392 */
1393 public final int getLineEnd(int line) {
1394 return getLineStart(line + 1);
1395 }
1396
Doug Felt9f7a4442010-03-01 12:45:56 -08001397 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 * Return the text offset after the last visible character (so whitespace
1399 * is not counted) on the specified line.
1400 */
1401 public int getLineVisibleEnd(int line) {
1402 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1403 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 CharSequence text = mText;
1407 char ch;
1408 if (line == getLineCount() - 1) {
1409 return end;
1410 }
1411
1412 for (; end > start; end--) {
1413 ch = text.charAt(end - 1);
1414
1415 if (ch == '\n') {
1416 return end - 1;
1417 }
1418
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001419 if (!TextLine.isLineEndSpace(ch)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 break;
1421 }
1422
1423 }
1424
1425 return end;
1426 }
1427
1428 /**
1429 * Return the vertical position of the bottom of the specified line.
1430 */
1431 public final int getLineBottom(int line) {
1432 return getLineTop(line + 1);
1433 }
1434
1435 /**
1436 * Return the vertical position of the baseline of the specified line.
1437 */
1438 public final int getLineBaseline(int line) {
1439 // getLineTop(line+1) == getLineTop(line)
1440 return getLineTop(line+1) - getLineDescent(line);
1441 }
1442
1443 /**
1444 * Get the ascent of the text on the specified line.
1445 * The return value is negative to match the Paint.ascent() convention.
1446 */
1447 public final int getLineAscent(int line) {
1448 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1449 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1450 }
1451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001453 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
1455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001457 return getOffsetToLeftRightOf(offset, false);
1458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459
Doug Felt9f7a4442010-03-01 12:45:56 -08001460 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1461 int line = getLineForOffset(caret);
1462 int lineStart = getLineStart(line);
1463 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001464 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001466 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001467 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001468 // if walking off line, look at the line we're headed to
1469 if (advance) {
1470 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001471 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001472 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001473 ++line;
1474 } else {
1475 return caret; // at very end, don't move
1476 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001477 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001478 } else {
1479 if (caret == lineStart) {
1480 if (line > 0) {
1481 lineChanged = true;
1482 --line;
1483 } else {
1484 return caret; // at very start, don't move
1485 }
1486 }
1487 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001488
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001489 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001490 lineStart = getLineStart(line);
1491 lineEnd = getLineEnd(line);
1492 int newDir = getParagraphDirection(line);
1493 if (newDir != lineDir) {
1494 // unusual case. we want to walk onto the line, but it runs
1495 // in a different direction than this one, so we fake movement
1496 // in the opposite direction.
1497 toLeft = !toLeft;
1498 lineDir = newDir;
1499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001501
Doug Felte8e45f22010-03-29 14:58:40 -07001502 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001503
Doug Felte8e45f22010-03-29 14:58:40 -07001504 TextLine tl = TextLine.obtain();
1505 // XXX: we don't care about tabs
1506 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1507 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1508 tl = TextLine.recycle(tl);
1509 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 }
1511
1512 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001513 // XXX this probably should skip local reorderings and
1514 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 if (offset == 0)
1516 return 0;
1517
1518 CharSequence text = mText;
1519 char c = text.charAt(offset);
1520
1521 if (c >= '\uDC00' && c <= '\uDFFF') {
1522 char c1 = text.charAt(offset - 1);
1523
1524 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1525 offset -= 1;
1526 }
1527
1528 if (mSpannedText) {
1529 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1530 ReplacementSpan.class);
1531
1532 for (int i = 0; i < spans.length; i++) {
1533 int start = ((Spanned) text).getSpanStart(spans[i]);
1534 int end = ((Spanned) text).getSpanEnd(spans[i]);
1535
1536 if (start < offset && end > offset)
1537 offset = start;
1538 }
1539 }
1540
1541 return offset;
1542 }
1543
1544 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001545 * Determine whether we should clamp cursor position. Currently it's
1546 * only robust for left-aligned displays.
1547 * @hide
1548 */
1549 public boolean shouldClampCursor(int line) {
1550 // Only clamp cursor position in left-aligned displays.
1551 switch (getParagraphAlignment(line)) {
1552 case ALIGN_LEFT:
1553 return true;
1554 case ALIGN_NORMAL:
1555 return getParagraphDirection(line) > 0;
1556 default:
1557 return false;
1558 }
1559
1560 }
1561 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 * Fills in the specified Path with a representation of a cursor
1563 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001564 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 * directionalities.
1566 */
1567 public void getCursorPath(int point, Path dest,
1568 CharSequence editingBuffer) {
1569 dest.reset();
1570
1571 int line = getLineForOffset(point);
1572 int top = getLineTop(line);
1573 int bottom = getLineTop(line+1);
1574
Raph Levienafe8e9b2012-12-19 16:09:32 -08001575 boolean clamped = shouldClampCursor(line);
1576 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1577 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578
Jeff Brown497a92c2010-09-12 17:55:08 -07001579 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1580 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1581 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 int dist = 0;
1583
1584 if (caps != 0 || fn != 0) {
1585 dist = (bottom - top) >> 2;
1586
1587 if (fn != 0)
1588 top += dist;
1589 if (caps != 0)
1590 bottom -= dist;
1591 }
1592
1593 if (h1 < 0.5f)
1594 h1 = 0.5f;
1595 if (h2 < 0.5f)
1596 h2 = 0.5f;
1597
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001598 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 dest.moveTo(h1, top);
1600 dest.lineTo(h1, bottom);
1601 } else {
1602 dest.moveTo(h1, top);
1603 dest.lineTo(h1, (top + bottom) >> 1);
1604
1605 dest.moveTo(h2, (top + bottom) >> 1);
1606 dest.lineTo(h2, bottom);
1607 }
1608
1609 if (caps == 2) {
1610 dest.moveTo(h2, bottom);
1611 dest.lineTo(h2 - dist, bottom + dist);
1612 dest.lineTo(h2, bottom);
1613 dest.lineTo(h2 + dist, bottom + dist);
1614 } else if (caps == 1) {
1615 dest.moveTo(h2, bottom);
1616 dest.lineTo(h2 - dist, bottom + dist);
1617
1618 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1619 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1620
1621 dest.moveTo(h2 + dist, bottom + dist);
1622 dest.lineTo(h2, bottom);
1623 }
1624
1625 if (fn == 2) {
1626 dest.moveTo(h1, top);
1627 dest.lineTo(h1 - dist, top - dist);
1628 dest.lineTo(h1, top);
1629 dest.lineTo(h1 + dist, top - dist);
1630 } else if (fn == 1) {
1631 dest.moveTo(h1, top);
1632 dest.lineTo(h1 - dist, top - dist);
1633
1634 dest.moveTo(h1 - dist, top - dist + 0.5f);
1635 dest.lineTo(h1 + dist, top - dist + 0.5f);
1636
1637 dest.moveTo(h1 + dist, top - dist);
1638 dest.lineTo(h1, top);
1639 }
1640 }
1641
1642 private void addSelection(int line, int start, int end,
1643 int top, int bottom, Path dest) {
1644 int linestart = getLineStart(line);
1645 int lineend = getLineEnd(line);
1646 Directions dirs = getLineDirections(line);
1647
1648 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1649 lineend--;
1650
Doug Felt9f7a4442010-03-01 12:45:56 -08001651 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1652 int here = linestart + dirs.mDirections[i];
1653 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 if (there > lineend)
1656 there = lineend;
1657
1658 if (start <= there && end >= here) {
1659 int st = Math.max(start, here);
1660 int en = Math.min(end, there);
1661
1662 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001663 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1664 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001666 float left = Math.min(h1, h2);
1667 float right = Math.max(h1, h2);
1668
1669 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 }
1671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673 }
1674
1675 /**
1676 * Fills in the specified Path with a representation of a highlight
1677 * between the specified offsets. This will often be a rectangle
1678 * or a potentially discontinuous set of rectangles. If the start
1679 * and end are the same, the returned path is empty.
1680 */
1681 public void getSelectionPath(int start, int end, Path dest) {
1682 dest.reset();
1683
1684 if (start == end)
1685 return;
1686
1687 if (end < start) {
1688 int temp = end;
1689 end = start;
1690 start = temp;
1691 }
1692
1693 int startline = getLineForOffset(start);
1694 int endline = getLineForOffset(end);
1695
1696 int top = getLineTop(startline);
1697 int bottom = getLineBottom(endline);
1698
1699 if (startline == endline) {
1700 addSelection(startline, start, end, top, bottom, dest);
1701 } else {
1702 final float width = mWidth;
1703
1704 addSelection(startline, start, getLineEnd(startline),
1705 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1708 dest.addRect(getLineLeft(startline), top,
1709 0, getLineBottom(startline), Path.Direction.CW);
1710 else
1711 dest.addRect(getLineRight(startline), top,
1712 width, getLineBottom(startline), Path.Direction.CW);
1713
1714 for (int i = startline + 1; i < endline; i++) {
1715 top = getLineTop(i);
1716 bottom = getLineBottom(i);
1717 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1718 }
1719
1720 top = getLineTop(endline);
1721 bottom = getLineBottom(endline);
1722
1723 addSelection(endline, getLineStart(endline), end,
1724 top, bottom, dest);
1725
1726 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1727 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1728 else
1729 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1730 }
1731 }
1732
1733 /**
1734 * Get the alignment of the specified paragraph, taking into account
1735 * markup attached to it.
1736 */
1737 public final Alignment getParagraphAlignment(int line) {
1738 Alignment align = mAlignment;
1739
1740 if (mSpannedText) {
1741 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001742 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 getLineEnd(line),
1744 AlignmentSpan.class);
1745
1746 int spanLength = spans.length;
1747 if (spanLength > 0) {
1748 align = spans[spanLength-1].getAlignment();
1749 }
1750 }
1751
1752 return align;
1753 }
1754
1755 /**
1756 * Get the left edge of the specified paragraph, inset by left margins.
1757 */
1758 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001760 int dir = getParagraphDirection(line);
1761 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1762 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 }
Doug Feltc982f602010-05-25 11:51:40 -07001764 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 }
1766
1767 /**
1768 * Get the right edge of the specified paragraph, inset by right margins.
1769 */
1770 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001772 int dir = getParagraphDirection(line);
1773 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1774 return right; // leading margin has no impact, or no styles
1775 }
1776 return right - getParagraphLeadingMargin(line);
1777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778
Doug Feltc982f602010-05-25 11:51:40 -07001779 /**
1780 * Returns the effective leading margin (unsigned) for this line,
1781 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1782 * @param line the line index
1783 * @return the leading margin of this line
1784 */
1785 private int getParagraphLeadingMargin(int line) {
1786 if (!mSpannedText) {
1787 return 0;
1788 }
1789 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001790
Doug Feltc982f602010-05-25 11:51:40 -07001791 int lineStart = getLineStart(line);
1792 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001793 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001794 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001795 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001796 LeadingMarginSpan.class);
1797 if (spans.length == 0) {
1798 return 0; // no leading margin span;
1799 }
Doug Felt0c702b82010-05-14 10:55:42 -07001800
Doug Feltc982f602010-05-25 11:51:40 -07001801 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001802
1803 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001804 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001805
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001806 boolean useFirstLineMargin = isFirstParaLine;
1807 for (int i = 0; i < spans.length; i++) {
1808 if (spans[i] instanceof LeadingMarginSpan2) {
1809 int spStart = spanned.getSpanStart(spans[i]);
1810 int spanLine = getLineForOffset(spStart);
1811 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1812 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1813 useFirstLineMargin |= line < spanLine + count;
1814 }
1815 }
Doug Feltc982f602010-05-25 11:51:40 -07001816 for (int i = 0; i < spans.length; i++) {
1817 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001818 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
1820
Doug Feltc982f602010-05-25 11:51:40 -07001821 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823
Doug Felte8e45f22010-03-29 14:58:40 -07001824 /* package */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001825 static float measurePara(TextPaint paint, CharSequence text, int start, int end,
1826 TextDirectionHeuristic textDir) {
Doug Felte8e45f22010-03-29 14:58:40 -07001827 MeasuredText mt = MeasuredText.obtain();
1828 TextLine tl = TextLine.obtain();
1829 try {
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001830 mt.setPara(text, start, end, textDir, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001831 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001832 int dir;
1833 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001834 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001835 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001836 } else {
1837 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1838 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001839 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 }
Doug Feltc982f602010-05-25 11:51:40 -07001841 char[] chars = mt.mChars;
1842 int len = mt.mLen;
1843 boolean hasTabs = false;
1844 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001845 // leading margins should be taken into account when measuring a paragraph
1846 int margin = 0;
1847 if (text instanceof Spanned) {
1848 Spanned spanned = (Spanned) text;
1849 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1850 LeadingMarginSpan.class);
1851 for (LeadingMarginSpan lms : spans) {
1852 margin += lms.getLeadingMargin(true);
1853 }
1854 }
Doug Feltc982f602010-05-25 11:51:40 -07001855 for (int i = 0; i < len; ++i) {
1856 if (chars[i] == '\t') {
1857 hasTabs = true;
1858 if (text instanceof Spanned) {
1859 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001860 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001861 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001862 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001863 TabStopSpan.class);
1864 if (spans.length > 0) {
1865 tabStops = new TabStops(TAB_INCREMENT, spans);
1866 }
1867 }
1868 break;
1869 }
1870 }
1871 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001872 return margin + Math.abs(tl.metrics(null));
Doug Felte8e45f22010-03-29 14:58:40 -07001873 } finally {
1874 TextLine.recycle(tl);
1875 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 }
1878
Doug Felt71b8dd72010-02-16 17:27:09 -08001879 /**
Doug Feltc982f602010-05-25 11:51:40 -07001880 * @hide
1881 */
1882 /* package */ static class TabStops {
1883 private int[] mStops;
1884 private int mNumStops;
1885 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001886
Doug Feltc982f602010-05-25 11:51:40 -07001887 TabStops(int increment, Object[] spans) {
1888 reset(increment, spans);
1889 }
Doug Felt0c702b82010-05-14 10:55:42 -07001890
Doug Feltc982f602010-05-25 11:51:40 -07001891 void reset(int increment, Object[] spans) {
1892 this.mIncrement = increment;
1893
1894 int ns = 0;
1895 if (spans != null) {
1896 int[] stops = this.mStops;
1897 for (Object o : spans) {
1898 if (o instanceof TabStopSpan) {
1899 if (stops == null) {
1900 stops = new int[10];
1901 } else if (ns == stops.length) {
1902 int[] nstops = new int[ns * 2];
1903 for (int i = 0; i < ns; ++i) {
1904 nstops[i] = stops[i];
1905 }
1906 stops = nstops;
1907 }
1908 stops[ns++] = ((TabStopSpan) o).getTabStop();
1909 }
1910 }
1911 if (ns > 1) {
1912 Arrays.sort(stops, 0, ns);
1913 }
1914 if (stops != this.mStops) {
1915 this.mStops = stops;
1916 }
1917 }
1918 this.mNumStops = ns;
1919 }
Doug Felt0c702b82010-05-14 10:55:42 -07001920
Doug Feltc982f602010-05-25 11:51:40 -07001921 float nextTab(float h) {
1922 int ns = this.mNumStops;
1923 if (ns > 0) {
1924 int[] stops = this.mStops;
1925 for (int i = 0; i < ns; ++i) {
1926 int stop = stops[i];
1927 if (stop > h) {
1928 return stop;
1929 }
1930 }
1931 }
1932 return nextDefaultStop(h, mIncrement);
1933 }
1934
1935 public static float nextDefaultStop(float h, int inc) {
1936 return ((int) ((h + inc) / inc)) * inc;
1937 }
1938 }
Doug Felt0c702b82010-05-14 10:55:42 -07001939
Doug Feltc982f602010-05-25 11:51:40 -07001940 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001941 * Returns the position of the next tab stop after h on the line.
1942 *
1943 * @param text the text
1944 * @param start start of the line
1945 * @param end limit of the line
1946 * @param h the current horizontal offset
1947 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1948 * on the line will be used. If there are no tabs, a default offset
1949 * will be used to compute the tab stop.
1950 * @return the offset of the next tab stop.
1951 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 /* package */ static float nextTab(CharSequence text, int start, int end,
1953 float h, Object[] tabs) {
1954 float nh = Float.MAX_VALUE;
1955 boolean alltabs = false;
1956
1957 if (text instanceof Spanned) {
1958 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001959 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 alltabs = true;
1961 }
1962
1963 for (int i = 0; i < tabs.length; i++) {
1964 if (!alltabs) {
1965 if (!(tabs[i] instanceof TabStopSpan))
1966 continue;
1967 }
1968
1969 int where = ((TabStopSpan) tabs[i]).getTabStop();
1970
1971 if (where < nh && where > h)
1972 nh = where;
1973 }
1974
1975 if (nh != Float.MAX_VALUE)
1976 return nh;
1977 }
1978
1979 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
1980 }
1981
1982 protected final boolean isSpanned() {
1983 return mSpannedText;
1984 }
1985
Eric Fischer74d31ef2010-08-05 15:29:36 -07001986 /**
1987 * Returns the same as <code>text.getSpans()</code>, except where
1988 * <code>start</code> and <code>end</code> are the same and are not
1989 * at the very beginning of the text, in which case an empty array
1990 * is returned instead.
1991 * <p>
1992 * This is needed because of the special case that <code>getSpans()</code>
1993 * on an empty range returns the spans adjacent to that range, which is
1994 * primarily for the sake of <code>TextWatchers</code> so they will get
1995 * notifications when text goes from empty to non-empty. But it also
1996 * has the unfortunate side effect that if the text ends with an empty
1997 * paragraph, that paragraph accidentally picks up the styles of the
1998 * preceding paragraph (even though those styles will not be picked up
1999 * by new text that is inserted into the empty paragraph).
2000 * <p>
2001 * The reason it just checks whether <code>start</code> and <code>end</code>
2002 * is the same is that the only time a line can contain 0 characters
2003 * is if it is the final paragraph of the Layout; otherwise any line will
2004 * contain at least one printing or newline character. The reason for the
2005 * additional check if <code>start</code> is greater than 0 is that
2006 * if the empty paragraph is the entire content of the buffer, paragraph
2007 * styles that are already applied to the buffer will apply to text that
2008 * is inserted into it.
2009 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07002010 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002011 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08002012 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07002013 }
2014
Siyamed Sinirfa05ba02016-01-12 10:54:43 -08002015 if(text instanceof SpannableStringBuilder) {
2016 return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
2017 } else {
2018 return text.getSpans(start, end, type);
2019 }
Eric Fischer74d31ef2010-08-05 15:29:36 -07002020 }
2021
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002022 private char getEllipsisChar(TextUtils.TruncateAt method) {
2023 return (method == TextUtils.TruncateAt.END_SMALL) ?
Neil Fullerd29bdb22015-02-06 10:03:08 +00002024 TextUtils.ELLIPSIS_TWO_DOTS[0] :
2025 TextUtils.ELLIPSIS_NORMAL[0];
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002026 }
2027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002029 char[] dest, int destoff, TextUtils.TruncateAt method) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 int ellipsisCount = getEllipsisCount(line);
2031
2032 if (ellipsisCount == 0) {
2033 return;
2034 }
2035
2036 int ellipsisStart = getEllipsisStart(line);
2037 int linestart = getLineStart(line);
2038
2039 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
2040 char c;
2041
2042 if (i == ellipsisStart) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002043 c = getEllipsisChar(method); // ellipsis
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 } else {
2045 c = '\uFEFF'; // 0-width space
2046 }
2047
2048 int a = i + linestart;
2049
2050 if (a >= start && a < end) {
2051 dest[destoff + a - start] = c;
2052 }
2053 }
2054 }
2055
2056 /**
2057 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08002058 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 */
2060 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08002061 // Directions represents directional runs within a line of text.
2062 // Runs are pairs of ints listed in visual order, starting from the
2063 // leading margin. The first int of each pair is the offset from
2064 // the first character of the line to the start of the run. The
2065 // second int represents both the length and level of the run.
2066 // The length is in the lower bits, accessed by masking with
2067 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
2068 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
2069 // To simply test for an RTL direction, test the bit using
2070 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071
Siyamed Sinired09ae12016-02-16 14:36:26 -08002072 /**
2073 * @hide
2074 */
2075 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2076 public int[] mDirections;
2077
2078 /**
2079 * @hide
2080 */
2081 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2082 public Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 mDirections = dirs;
2084 }
2085 }
2086
2087 /**
2088 * Return the offset of the first character to be ellipsized away,
2089 * relative to the start of the line. (So 0 if the beginning of the
2090 * line is ellipsized, not getLineStart().)
2091 */
2092 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07002093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 /**
2095 * Returns the number of characters to be ellipsized away, or 0 if
2096 * no ellipsis is to take place.
2097 */
2098 public abstract int getEllipsisCount(int line);
2099
2100 /* package */ static class Ellipsizer implements CharSequence, GetChars {
2101 /* package */ CharSequence mText;
2102 /* package */ Layout mLayout;
2103 /* package */ int mWidth;
2104 /* package */ TextUtils.TruncateAt mMethod;
2105
2106 public Ellipsizer(CharSequence s) {
2107 mText = s;
2108 }
2109
2110 public char charAt(int off) {
2111 char[] buf = TextUtils.obtain(1);
2112 getChars(off, off + 1, buf, 0);
2113 char ret = buf[0];
2114
2115 TextUtils.recycle(buf);
2116 return ret;
2117 }
2118
2119 public void getChars(int start, int end, char[] dest, int destoff) {
2120 int line1 = mLayout.getLineForOffset(start);
2121 int line2 = mLayout.getLineForOffset(end);
2122
2123 TextUtils.getChars(mText, start, end, dest, destoff);
2124
2125 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002126 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 }
2128 }
2129
2130 public int length() {
2131 return mText.length();
2132 }
Doug Felt9f7a4442010-03-01 12:45:56 -08002133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 public CharSequence subSequence(int start, int end) {
2135 char[] s = new char[end - start];
2136 getChars(start, end, s, 0);
2137 return new String(s);
2138 }
2139
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002140 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 public String toString() {
2142 char[] s = new char[length()];
2143 getChars(0, length(), s, 0);
2144 return new String(s);
2145 }
2146
2147 }
2148
Gilles Debunne6c488de2012-03-01 16:20:35 -08002149 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 private Spanned mSpanned;
2151
2152 public SpannedEllipsizer(CharSequence display) {
2153 super(display);
2154 mSpanned = (Spanned) display;
2155 }
2156
2157 public <T> T[] getSpans(int start, int end, Class<T> type) {
2158 return mSpanned.getSpans(start, end, type);
2159 }
2160
2161 public int getSpanStart(Object tag) {
2162 return mSpanned.getSpanStart(tag);
2163 }
2164
2165 public int getSpanEnd(Object tag) {
2166 return mSpanned.getSpanEnd(tag);
2167 }
2168
2169 public int getSpanFlags(Object tag) {
2170 return mSpanned.getSpanFlags(tag);
2171 }
2172
Gilles Debunne6c488de2012-03-01 16:20:35 -08002173 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 public int nextSpanTransition(int start, int limit, Class type) {
2175 return mSpanned.nextSpanTransition(start, limit, type);
2176 }
2177
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002178 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 public CharSequence subSequence(int start, int end) {
2180 char[] s = new char[end - start];
2181 getChars(start, end, s, 0);
2182
2183 SpannableString ss = new SpannableString(new String(s));
2184 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
2185 return ss;
2186 }
2187 }
2188
2189 private CharSequence mText;
2190 private TextPaint mPaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 private int mWidth;
2192 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
2193 private float mSpacingMult;
2194 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002195 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002197 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002198 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
Seigo Nonaka09da71a2016-11-28 16:24:14 +09002199 private boolean mJustify;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200
2201 public static final int DIR_LEFT_TO_RIGHT = 1;
2202 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002203
Doug Felt20178d62010-02-22 13:39:01 -08002204 /* package */ static final int DIR_REQUEST_LTR = 1;
2205 /* package */ static final int DIR_REQUEST_RTL = -1;
2206 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2207 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208
Doug Felt9f7a4442010-03-01 12:45:56 -08002209 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2210 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2211 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2212 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 public enum Alignment {
2215 ALIGN_NORMAL,
2216 ALIGN_OPPOSITE,
2217 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002218 /** @hide */
2219 ALIGN_LEFT,
2220 /** @hide */
2221 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 }
2223
2224 private static final int TAB_INCREMENT = 20;
2225
Siyamed Sinired09ae12016-02-16 14:36:26 -08002226 /** @hide */
2227 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2228 public static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002229 new Directions(new int[] { 0, RUN_LENGTH_MASK });
Siyamed Sinired09ae12016-02-16 14:36:26 -08002230
2231 /** @hide */
2232 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2233 public static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002234 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236}