blob: b47fce89b0ff81a98dd9ee3dc39cdfd0d35bfecf [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
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700106 /** @hide */
107 @IntDef({JUSTIFICATION_MODE_NONE, JUSTIFICATION_MODE_INTER_WORD})
108 @Retention(RetentionPolicy.SOURCE)
109 public @interface JustificationMode {}
110
111 /**
112 * Value for justification mode indicating no justification.
113 */
114 public static final int JUSTIFICATION_MODE_NONE = 0;
115
116 /**
117 * Value for justification mode indicating the text is justified by stretching word spacing.
118 */
119 public static final int JUSTIFICATION_MODE_INTER_WORD = 1;
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700122 * Return how wide a layout must be in order to display the specified text with one line per
123 * paragraph.
124 *
125 * <p>As of O, Uses
126 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
127 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 */
129 public static float getDesiredWidth(CharSequence source,
130 TextPaint paint) {
131 return getDesiredWidth(source, 0, source.length(), paint);
132 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700135 * Return how wide a layout must be in order to display the specified text slice with one
136 * line per paragraph.
137 *
138 * <p>As of O, Uses
139 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
140 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
141 */
142 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) {
143 return getDesiredWidth(source, start, end, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR);
144 }
145
146 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800147 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 * specified text slice with one line per paragraph.
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700149 *
150 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700152 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint,
153 TextDirectionHeuristic textDir) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
156 int next;
157 for (int i = start; i <= end; i = next) {
158 next = TextUtils.indexOf(source, '\n', i, end);
159
160 if (next < 0)
161 next = end;
162
Doug Felt71b8dd72010-02-16 17:27:09 -0800163 // note, omits trailing paragraph char
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700164 float w = measurePara(paint, source, i, next, textDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
166 if (w > need)
167 need = w;
168
169 next++;
170 }
171
172 return need;
173 }
174
175 /**
176 * Subclasses of Layout use this constructor to set the display text,
177 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800178 * @param text the text to render
179 * @param paint the default paint for the layout. Styles can override
180 * various attributes of the paint.
181 * @param width the wrapping width for the text.
182 * @param align whether to left, right, or center the text. Styles can
183 * override the alignment.
184 * @param spacingMult factor by which to scale the font size to get the
185 * default line spacing
186 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 */
188 protected Layout(CharSequence text, TextPaint paint,
189 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800190 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700191 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
192 spacingMult, spacingAdd);
193 }
194
195 /**
196 * Subclasses of Layout use this constructor to set the display text,
197 * width, and other standard properties.
198 * @param text the text to render
199 * @param paint the default paint for the layout. Styles can override
200 * various attributes of the paint.
201 * @param width the wrapping width for the text.
202 * @param align whether to left, right, or center the text. Styles can
203 * override the alignment.
204 * @param spacingMult factor by which to scale the font size to get the
205 * default line spacing
206 * @param spacingAdd amount to add to the default line spacing
207 *
208 * @hide
209 */
210 protected Layout(CharSequence text, TextPaint paint,
211 int width, Alignment align, TextDirectionHeuristic textDir,
212 float spacingMult, float spacingAdd) {
213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 if (width < 0)
215 throw new IllegalArgumentException("Layout: " + width + " < 0");
216
Doug Felte8e45f22010-03-29 14:58:40 -0700217 // Ensure paint doesn't have baselineShift set.
218 // While normally we don't modify the paint the user passed in,
219 // we were already doing this in Styled.drawUniformRun with both
220 // baselineShift and bgColor. We probably should reevaluate bgColor.
221 if (paint != null) {
222 paint.bgColor = 0;
223 paint.baselineShift = 0;
224 }
225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 mText = text;
227 mPaint = paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 mWidth = width;
229 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800230 mSpacingMult = spacingMult;
231 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700233 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 }
235
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900236 /** @hide */
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700237 protected void setJustificationMode(@JustificationMode int justificationMode) {
238 mJustificationMode = justificationMode;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900239 }
240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
242 * Replace constructor properties of this Layout with new ones. Be careful.
243 */
244 /* package */ void replaceWith(CharSequence text, TextPaint paint,
245 int width, Alignment align,
246 float spacingmult, float spacingadd) {
247 if (width < 0) {
248 throw new IllegalArgumentException("Layout: " + width + " < 0");
249 }
250
251 mText = text;
252 mPaint = paint;
253 mWidth = width;
254 mAlignment = align;
255 mSpacingMult = spacingmult;
256 mSpacingAdd = spacingadd;
257 mSpannedText = text instanceof Spanned;
258 }
259
260 /**
261 * Draw this Layout on the specified Canvas.
262 */
263 public void draw(Canvas c) {
264 draw(c, null, null, 0);
265 }
266
267 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800268 * Draw this Layout on the specified canvas, with the highlight path drawn
269 * between the background and the text.
270 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800271 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800272 * @param highlight the path of the highlight or cursor; can be null
273 * @param highlightPaint the paint for the highlight
274 * @param cursorOffsetVertical the amount to temporarily translate the
275 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800277 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
278 int cursorOffsetVertical) {
279 final long lineRange = getLineRangeForDraw(canvas);
280 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
281 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
282 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
Gilles Debunne6c488de2012-03-01 16:20:35 -0800284 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
285 firstLine, lastLine);
286 drawText(canvas, firstLine, lastLine);
287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900289 private boolean isJustificationRequired(int lineNum) {
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700290 if (mJustificationMode == JUSTIFICATION_MODE_NONE) return false;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900291 final int lineEnd = getLineEnd(lineNum);
292 return lineEnd < mText.length() && mText.charAt(lineEnd - 1) != '\n';
293 }
294
295 private float getJustifyWidth(int lineNum) {
296 Alignment paraAlign = mAlignment;
297 TabStops tabStops = null;
298 boolean tabStopsIsInitialized = false;
299
300 int left = 0;
301 int right = mWidth;
302
303 final int dir = getParagraphDirection(lineNum);
304
305 ParagraphStyle[] spans = NO_PARA_SPANS;
306 if (mSpannedText) {
307 Spanned sp = (Spanned) mText;
308 final int start = getLineStart(lineNum);
309
310 final boolean isFirstParaLine = (start == 0 || mText.charAt(start - 1) == '\n');
311
312 if (isFirstParaLine) {
313 final int spanEnd = sp.nextSpanTransition(start, mText.length(),
314 ParagraphStyle.class);
315 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
316
317 for (int n = spans.length - 1; n >= 0; n--) {
318 if (spans[n] instanceof AlignmentSpan) {
319 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
320 break;
321 }
322 }
323 }
324
325 final int length = spans.length;
326 boolean useFirstLineMargin = isFirstParaLine;
327 for (int n = 0; n < length; n++) {
328 if (spans[n] instanceof LeadingMarginSpan2) {
329 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
330 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
331 if (lineNum < startLine + count) {
332 useFirstLineMargin = true;
333 break;
334 }
335 }
336 }
337 for (int n = 0; n < length; n++) {
338 if (spans[n] instanceof LeadingMarginSpan) {
339 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
340 if (dir == DIR_RIGHT_TO_LEFT) {
341 right -= margin.getLeadingMargin(useFirstLineMargin);
342 } else {
343 left += margin.getLeadingMargin(useFirstLineMargin);
344 }
345 }
346 }
347 }
348
349 if (getLineContainsTab(lineNum)) {
350 tabStops = new TabStops(TAB_INCREMENT, spans);
351 }
352
353 final Alignment align;
354 if (paraAlign == Alignment.ALIGN_LEFT) {
355 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
356 } else if (paraAlign == Alignment.ALIGN_RIGHT) {
357 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
358 } else {
359 align = paraAlign;
360 }
361
362 final int indentWidth;
363 if (align == Alignment.ALIGN_NORMAL) {
364 if (dir == DIR_LEFT_TO_RIGHT) {
365 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
366 } else {
367 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
368 }
369 } else if (align == Alignment.ALIGN_OPPOSITE) {
370 if (dir == DIR_LEFT_TO_RIGHT) {
371 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
372 } else {
373 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
374 }
375 } else { // Alignment.ALIGN_CENTER
376 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
377 }
378
379 return right - left - indentWidth;
380 }
381
Gilles Debunne6c488de2012-03-01 16:20:35 -0800382 /**
383 * @hide
384 */
385 public void drawText(Canvas canvas, int firstLine, int lastLine) {
386 int previousLineBottom = getLineTop(firstLine);
387 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800388 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700389 int spanEnd = 0;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900390 final TextPaint paint = mPaint;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800391 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700393 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700394 TabStops tabStops = null;
395 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800396
Doug Felte8e45f22010-03-29 14:58:40 -0700397 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700398
Gilles Debunne6c488de2012-03-01 16:20:35 -0800399 // Draw the lines, one at a time.
400 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700401 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700403 previousLineEnd = getLineStart(lineNum + 1);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900404 final boolean justify = isJustificationRequired(lineNum);
Raph Levien26d443a2015-03-30 14:18:32 -0700405 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406
407 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700408 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700410 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411
Raph Levien26d443a2015-03-30 14:18:32 -0700412 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700413 int left = 0;
414 int right = mWidth;
415
Gilles Debunne6c488de2012-03-01 16:20:35 -0800416 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700417 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800418 int textLength = buf.length();
419 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700420
Doug Feltc982f602010-05-25 11:51:40 -0700421 // New batch of paragraph styles, collect into spans array.
422 // Compute the alignment, last alignment style wins.
423 // Reset tabStops, we'll rebuild if we encounter a line with
424 // tabs.
425 // We expect paragraph spans to be relatively infrequent, use
426 // spanEnd so that we can check less frequently. Since
427 // paragraph styles ought to apply to entire paragraphs, we can
428 // just collect the ones present at the start of the paragraph.
429 // If spanEnd is before the end of the paragraph, that's not
430 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700431 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700432 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700434 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800435
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700436 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800437 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700439 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 break;
441 }
442 }
Doug Felt0c702b82010-05-14 10:55:42 -0700443
Doug Feltc982f602010-05-25 11:51:40 -0700444 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800446
Doug Feltc982f602010-05-25 11:51:40 -0700447 // Draw all leading margin spans. Adjust left or right according
448 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700450 boolean useFirstLineMargin = isFirstParaLine;
451 for (int n = 0; n < length; n++) {
452 if (spans[n] instanceof LeadingMarginSpan2) {
453 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
454 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
455 // if there is more than one LeadingMarginSpan2, use
456 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700457 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700458 useFirstLineMargin = true;
459 break;
460 }
461 }
462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 for (int n = 0; n < length; n++) {
464 if (spans[n] instanceof LeadingMarginSpan) {
465 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800467 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800469 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700470 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800472 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800474 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700475 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477 }
478 }
479 }
480
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700481 boolean hasTab = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700482 // Can't tell if we have tabs for sure, currently
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700483 if (hasTab && !tabStopsIsInitialized) {
Doug Feltc982f602010-05-25 11:51:40 -0700484 if (tabStops == null) {
485 tabStops = new TabStops(TAB_INCREMENT, spans);
486 } else {
487 tabStops.reset(TAB_INCREMENT, spans);
488 }
489 tabStopsIsInitialized = true;
490 }
491
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700492 // Determine whether the line aligns to normal, opposite, or center.
493 Alignment align = paraAlign;
494 if (align == Alignment.ALIGN_LEFT) {
495 align = (dir == DIR_LEFT_TO_RIGHT) ?
496 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
497 } else if (align == Alignment.ALIGN_RIGHT) {
498 align = (dir == DIR_LEFT_TO_RIGHT) ?
499 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
500 }
501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 int x;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900503 final int indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 if (align == Alignment.ALIGN_NORMAL) {
505 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900506 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
507 x = left + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900509 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
510 x = right - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700513 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700515 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900516 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
517 x = right - max - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900519 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
520 x = left - max + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
Doug Feltc982f602010-05-25 11:51:40 -0700522 } else { // Alignment.ALIGN_CENTER
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900523 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700524 max = max & ~1;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900525 x = ((right + left - max) >> 1) + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 }
528
Raph Levien26d443a2015-03-30 14:18:32 -0700529 paint.setHyphenEdit(getHyphen(lineNum));
530 Directions directions = getLineDirections(lineNum);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900531 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab && !justify) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800532 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800533 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 } else {
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700535 tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900536 if (justify) {
537 tl.justify(right - left - indentWidth);
538 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800539 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
Raph Levien9a174dd2015-04-08 13:35:03 -0700541 paint.setHyphenEdit(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
Doug Feltc982f602010-05-25 11:51:40 -0700543
Doug Felte8e45f22010-03-29 14:58:40 -0700544 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
546
547 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800548 * @hide
549 */
550 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
551 int cursorOffsetVertical, int firstLine, int lastLine) {
552 // First, draw LineBackgroundSpans.
553 // LineBackgroundSpans know nothing about the alignment, margins, or
554 // direction of the layout or line. XXX: Should they?
555 // They are evaluated at each line.
556 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700557 if (mLineBackgroundSpans == null) {
558 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700559 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800560
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700561 Spanned buffer = (Spanned) mText;
562 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700563 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800564
Gilles Debunneeca5b732012-04-25 18:48:42 -0700565 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700566 int previousLineBottom = getLineTop(firstLine);
567 int previousLineEnd = getLineStart(firstLine);
568 ParagraphStyle[] spans = NO_PARA_SPANS;
569 int spansLength = 0;
570 TextPaint paint = mPaint;
571 int spanEnd = 0;
572 final int width = mWidth;
573 for (int i = firstLine; i <= lastLine; i++) {
574 int start = previousLineEnd;
575 int end = getLineStart(i + 1);
576 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800577
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700578 int ltop = previousLineBottom;
579 int lbottom = getLineTop(i + 1);
580 previousLineBottom = lbottom;
581 int lbaseline = lbottom - getLineDescent(i);
582
583 if (start >= spanEnd) {
584 // These should be infrequent, so we'll use this so that
585 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700586 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700587 // All LineBackgroundSpans on a line contribute to its background.
588 spansLength = 0;
589 // Duplication of the logic of getParagraphSpans
590 if (start != end || start == 0) {
591 // Equivalent to a getSpans(start, end), but filling the 'spans' local
592 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700593 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
594 // equal test is valid since both intervals are not empty by
595 // construction
596 if (mLineBackgroundSpans.spanStarts[j] >= end ||
597 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500598 spans = GrowingArrayUtils.append(
599 spans, spansLength, mLineBackgroundSpans.spans[j]);
600 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700601 }
602 }
603 }
604
605 for (int n = 0; n < spansLength; n++) {
606 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
607 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
608 ltop, lbaseline, lbottom,
609 buffer, start, end, i);
610 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800611 }
612 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700613 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800614 }
615
616 // There can be a highlight even without spans if we are drawing
617 // a non-spanned transformation of a spanned editing buffer.
618 if (highlight != null) {
619 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
620 canvas.drawPath(highlight, highlightPaint);
621 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
622 }
623 }
624
625 /**
626 * @param canvas
627 * @return The range of lines that need to be drawn, possibly empty.
628 * @hide
629 */
630 public long getLineRangeForDraw(Canvas canvas) {
631 int dtop, dbottom;
632
633 synchronized (sTempRect) {
634 if (!canvas.getClipBounds(sTempRect)) {
635 // Negative range end used as a special flag
636 return TextUtils.packRangeInLong(0, -1);
637 }
638
639 dtop = sTempRect.top;
640 dbottom = sTempRect.bottom;
641 }
642
643 final int top = Math.max(dtop, 0);
644 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
645
Gilles Debunne2fba3382012-06-11 17:46:24 -0700646 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800647 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
648 }
649
650 /**
Doug Feltc982f602010-05-25 11:51:40 -0700651 * Return the start position of the line, given the left and right bounds
652 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700653 *
Doug Feltc982f602010-05-25 11:51:40 -0700654 * @param line the line index
655 * @param left the left bounds (0, or leading margin if ltr para)
656 * @param right the right bounds (width, minus leading margin if rtl para)
657 * @return the start position of the line (to right of line if rtl para)
658 */
659 private int getLineStartPos(int line, int left, int right) {
660 // Adjust the point at which to start rendering depending on the
661 // alignment of the paragraph.
662 Alignment align = getParagraphAlignment(line);
663 int dir = getParagraphDirection(line);
664
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700665 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700666 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
667 } else if (align == Alignment.ALIGN_RIGHT) {
668 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
669 }
670
671 int x;
672 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700673 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700674 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700675 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700676 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700677 }
678 } else {
679 TabStops tabStops = null;
680 if (mSpannedText && getLineContainsTab(line)) {
681 Spanned spanned = (Spanned) mText;
682 int start = getLineStart(line);
683 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
684 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800685 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
686 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700687 if (tabSpans.length > 0) {
688 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
689 }
690 }
691 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700692 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700693 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700694 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700695 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700696 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700697 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700698 }
699 } else { // Alignment.ALIGN_CENTER
700 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700701 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700702 }
703 }
704 return x;
705 }
706
707 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 * Return the text that is displayed by this Layout.
709 */
710 public final CharSequence getText() {
711 return mText;
712 }
713
714 /**
715 * Return the base Paint properties for this layout.
716 * Do NOT change the paint, which may result in funny
717 * drawing for this layout.
718 */
719 public final TextPaint getPaint() {
720 return mPaint;
721 }
722
723 /**
724 * Return the width of this layout.
725 */
726 public final int getWidth() {
727 return mWidth;
728 }
729
730 /**
731 * Return the width to which this Layout is ellipsizing, or
732 * {@link #getWidth} if it is not doing anything special.
733 */
734 public int getEllipsizedWidth() {
735 return mWidth;
736 }
737
738 /**
739 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800740 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 * it does not cause the text to reflow to use the full new width.
742 */
743 public final void increaseWidthTo(int wid) {
744 if (wid < mWidth) {
745 throw new RuntimeException("attempted to reduce Layout width");
746 }
747
748 mWidth = wid;
749 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 /**
752 * Return the total height of this layout.
753 */
754 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800755 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757
758 /**
Siyamed Sinir0745c722016-05-31 20:39:33 -0700759 * Return the total height of this layout.
760 *
761 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
762 *
763 * @hide
764 */
765 public int getHeight(boolean cap) {
766 return getHeight();
767 }
768
769 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 * Return the base alignment of this layout.
771 */
772 public final Alignment getAlignment() {
773 return mAlignment;
774 }
775
776 /**
777 * Return what the text height is multiplied by to get the line height.
778 */
779 public final float getSpacingMultiplier() {
780 return mSpacingMult;
781 }
782
783 /**
784 * Return the number of units of leading that are added to each line.
785 */
786 public final float getSpacingAdd() {
787 return mSpacingAdd;
788 }
789
790 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700791 * Return the heuristic used to determine paragraph text direction.
792 * @hide
793 */
794 public final TextDirectionHeuristic getTextDirectionHeuristic() {
795 return mTextDir;
796 }
797
798 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 * Return the number of lines of text in this layout.
800 */
801 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 /**
804 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
805 * If bounds is not null, return the top, left, right, bottom extents
806 * of the specified line in it.
807 * @param line which line to examine (0..getLineCount() - 1)
808 * @param bounds Optional. If not null, it returns the extent of the line
809 * @return the Y-coordinate of the baseline
810 */
811 public int getLineBounds(int line, Rect bounds) {
812 if (bounds != null) {
813 bounds.left = 0; // ???
814 bounds.top = getLineTop(line);
815 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800816 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818 return getLineBaseline(line);
819 }
820
821 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800822 * Return the vertical position of the top of the specified line
823 * (0&hellip;getLineCount()).
824 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 * bottom of the last line.
826 */
827 public abstract int getLineTop(int line);
828
829 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800830 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 */
832 public abstract int getLineDescent(int line);
833
834 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800835 * Return the text offset of the beginning of the specified line (
836 * 0&hellip;getLineCount()). If the specified line is equal to the line
837 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 */
839 public abstract int getLineStart(int line);
840
841 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800842 * Returns the primary directionality of the paragraph containing the
843 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
844 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 */
846 public abstract int getParagraphDirection(int line);
847
848 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700849 * Returns whether the specified line contains one or more
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700850 * characters that need to be handled specially, like tabs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 */
852 public abstract boolean getLineContainsTab(int line);
853
854 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800855 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 * The array alternates counts of characters in left-to-right
857 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800858 *
859 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 */
861 public abstract Directions getLineDirections(int line);
862
863 /**
864 * Returns the (negative) number of extra pixels of ascent padding in the
865 * top line of the Layout.
866 */
867 public abstract int getTopPadding();
868
869 /**
870 * Returns the number of extra pixels of descent padding in the
871 * bottom line of the Layout.
872 */
873 public abstract int getBottomPadding();
874
Raph Levien26d443a2015-03-30 14:18:32 -0700875 /**
876 * Returns the hyphen edit for a line.
877 *
878 * @hide
879 */
880 public int getHyphen(int line) {
881 return 0;
882 }
883
Raph Levien2ea52902015-07-01 14:39:31 -0700884 /**
885 * Returns the left indent for a line.
886 *
887 * @hide
888 */
889 public int getIndentAdjust(int line, Alignment alignment) {
890 return 0;
891 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700892
893 /**
894 * Returns true if the character at offset and the preceding character
895 * are at different run levels (and thus there's a split caret).
896 * @param offset the offset
897 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800898 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700899 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800900 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800901 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700902 Directions dirs = getLineDirections(line);
903 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
904 return false;
905 }
906
907 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800908 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700909 int lineEnd = getLineEnd(line);
910 if (offset == lineStart || offset == lineEnd) {
911 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
912 int runIndex = offset == lineStart ? 0 : runs.length - 2;
913 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
914 }
915
916 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800917 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700918 if (offset == runs[i]) {
919 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800920 }
921 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700922 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800923 }
924
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700925 /**
926 * Returns true if the character at offset is right to left (RTL).
927 * @param offset the offset
928 * @return true if the character is RTL, false if it is LTR
929 */
930 public boolean isRtlCharAt(int offset) {
931 int line = getLineForOffset(offset);
932 Directions dirs = getLineDirections(line);
933 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
934 return false;
935 }
936 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
937 return true;
938 }
939 int[] runs = dirs.mDirections;
940 int lineStart = getLineStart(line);
941 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700942 int start = lineStart + runs[i];
943 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
944 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700945 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
946 return ((level & 1) != 0);
947 }
948 }
949 // Should happen only if the offset is "out of bounds"
950 return false;
951 }
952
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900953 /**
954 * Returns the range of the run that the character at offset belongs to.
955 * @param offset the offset
956 * @return The range of the run
957 * @hide
958 */
959 public long getRunRange(int offset) {
960 int line = getLineForOffset(offset);
961 Directions dirs = getLineDirections(line);
962 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
963 return TextUtils.packRangeInLong(0, getLineEnd(line));
964 }
965 int[] runs = dirs.mDirections;
966 int lineStart = getLineStart(line);
967 for (int i = 0; i < runs.length; i += 2) {
968 int start = lineStart + runs[i];
969 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
970 if (offset >= start && offset < limit) {
971 return TextUtils.packRangeInLong(start, limit);
972 }
973 }
974 // Should happen only if the offset is "out of bounds"
975 return TextUtils.packRangeInLong(0, getLineEnd(line));
976 }
977
Doug Felt9f7a4442010-03-01 12:45:56 -0800978 private boolean primaryIsTrailingPrevious(int offset) {
979 int line = getLineForOffset(offset);
980 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700981 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800982 int[] runs = getLineDirections(line).mDirections;
983
984 int levelAt = -1;
985 for (int i = 0; i < runs.length; i += 2) {
986 int start = lineStart + runs[i];
987 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
988 if (limit > lineEnd) {
989 limit = lineEnd;
990 }
991 if (offset >= start && offset < limit) {
992 if (offset > start) {
993 // Previous character is at same level, so don't use trailing.
994 return false;
995 }
996 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
997 break;
998 }
999 }
1000 if (levelAt == -1) {
1001 // Offset was limit of line.
1002 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
1003 }
1004
1005 // At level boundary, check previous level.
1006 int levelBefore = -1;
1007 if (offset == lineStart) {
1008 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
1009 } else {
1010 offset -= 1;
1011 for (int i = 0; i < runs.length; i += 2) {
1012 int start = lineStart + runs[i];
1013 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
1014 if (limit > lineEnd) {
1015 limit = lineEnd;
1016 }
1017 if (offset >= start && offset < limit) {
1018 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1019 break;
1020 }
1021 }
1022 }
1023
1024 return levelBefore < levelAt;
1025 }
1026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 /**
1028 * Get the primary horizontal position for the specified text offset.
1029 * This is the location where a new character would be inserted in
1030 * the paragraph's primary direction.
1031 */
1032 public float getPrimaryHorizontal(int offset) {
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001033 return getPrimaryHorizontal(offset, false /* not clamped */,
1034 true /* getNewLineStartPosOnLineBreak */);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001035 }
1036
1037 /**
1038 * Get the primary horizontal position for the specified text offset, but
1039 * optionally clamp it so that it doesn't exceed the width of the layout.
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001040 *
1041 * @param offset the offset to get horizontal position
1042 * @param clamped whether to clamp the position by using the width of this layout.
1043 * @param getNewLineStartPosOnLineBreak whether to get the start position of new line when the
1044 * offset is at automatic line break.
Raph Levienafe8e9b2012-12-19 16:09:32 -08001045 * @hide
1046 */
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001047 public float getPrimaryHorizontal(int offset, boolean clamped,
1048 boolean getNewLineStartPosOnLineBreak) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001049 boolean trailing = primaryIsTrailingPrevious(offset);
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001050 return getHorizontal(offset, trailing, clamped, getNewLineStartPosOnLineBreak);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052
1053 /**
1054 * Get the secondary horizontal position for the specified text offset.
1055 * This is the location where a new character would be inserted in
1056 * the direction other than the paragraph's primary direction.
1057 */
1058 public float getSecondaryHorizontal(int offset) {
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001059 return getSecondaryHorizontal(offset, false /* not clamped */,
1060 true /* getNewLineStartPosOnLineBreak */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
1062
Raph Levienafe8e9b2012-12-19 16:09:32 -08001063 /**
1064 * Get the secondary horizontal position for the specified text offset, but
1065 * optionally clamp it so that it doesn't exceed the width of the layout.
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001066 *
1067 * @param offset the offset to get horizontal position
1068 * @param clamped whether to clamp the position by using the width of this layout.
1069 * @param getNewLineStartPosOnLineBreak whether to get the start position of new line when the
1070 * offset is at automatic line break.
Raph Levienafe8e9b2012-12-19 16:09:32 -08001071 * @hide
1072 */
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001073 public float getSecondaryHorizontal(int offset, boolean clamped,
1074 boolean getNewLineStartPosOnLineBreak) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001075 boolean trailing = primaryIsTrailingPrevious(offset);
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001076 return getHorizontal(offset, !trailing, clamped, getNewLineStartPosOnLineBreak);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001077 }
1078
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001079 private float getHorizontal(int offset, boolean primary,
1080 boolean getNewLineStartPosOnLineBreak) {
1081 return primary ? getPrimaryHorizontal(offset, false /* not clamped */,
1082 getNewLineStartPosOnLineBreak)
1083 : getSecondaryHorizontal(offset, false /* not clamped */,
1084 getNewLineStartPosOnLineBreak);
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001085 }
1086
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001087 private float getHorizontal(int offset, boolean trailing, boolean clamped,
1088 boolean getNewLineStartPosOnLineBreak) {
1089 final int line = getLineForOffset(offset, getNewLineStartPosOnLineBreak);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001090 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
1092
Raph Levienafe8e9b2012-12-19 16:09:32 -08001093 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001095 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 int dir = getParagraphDirection(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001097 boolean hasTab = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 Directions directions = getLineDirections(line);
1099
Doug Feltc982f602010-05-25 11:51:40 -07001100 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001101 if (hasTab && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001102 // Just checking this line should be good enough, tabs should be
1103 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001104 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001105 if (tabs.length > 0) {
1106 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 }
1109
Doug Felte8e45f22010-03-29 14:58:40 -07001110 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001111 tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001112 float wid = tl.measure(offset - start, trailing, null);
1113 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114
Raph Levienafe8e9b2012-12-19 16:09:32 -08001115 if (clamped && wid > mWidth) {
1116 wid = mWidth;
1117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 int left = getParagraphLeft(line);
1119 int right = getParagraphRight(line);
1120
Doug Feltc982f602010-05-25 11:51:40 -07001121 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123
1124 /**
1125 * Get the leftmost position that should be exposed for horizontal
1126 * scrolling on the specified line.
1127 */
1128 public float getLineLeft(int line) {
1129 int dir = getParagraphDirection(line);
1130 Alignment align = getParagraphAlignment(line);
1131
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001132 if (align == Alignment.ALIGN_LEFT) {
1133 return 0;
1134 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 if (dir == DIR_RIGHT_TO_LEFT)
1136 return getParagraphRight(line) - getLineMax(line);
1137 else
1138 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001139 } else if (align == Alignment.ALIGN_RIGHT) {
1140 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 } else if (align == Alignment.ALIGN_OPPOSITE) {
1142 if (dir == DIR_RIGHT_TO_LEFT)
1143 return 0;
1144 else
1145 return mWidth - getLineMax(line);
1146 } else { /* align == Alignment.ALIGN_CENTER */
1147 int left = getParagraphLeft(line);
1148 int right = getParagraphRight(line);
1149 int max = ((int) getLineMax(line)) & ~1;
1150
1151 return left + ((right - left) - max) / 2;
1152 }
1153 }
1154
1155 /**
1156 * Get the rightmost position that should be exposed for horizontal
1157 * scrolling on the specified line.
1158 */
1159 public float getLineRight(int line) {
1160 int dir = getParagraphDirection(line);
1161 Alignment align = getParagraphAlignment(line);
1162
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001163 if (align == Alignment.ALIGN_LEFT) {
1164 return getParagraphLeft(line) + getLineMax(line);
1165 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 if (dir == DIR_RIGHT_TO_LEFT)
1167 return mWidth;
1168 else
1169 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001170 } else if (align == Alignment.ALIGN_RIGHT) {
1171 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 } else if (align == Alignment.ALIGN_OPPOSITE) {
1173 if (dir == DIR_RIGHT_TO_LEFT)
1174 return getLineMax(line);
1175 else
1176 return mWidth;
1177 } else { /* align == Alignment.ALIGN_CENTER */
1178 int left = getParagraphLeft(line);
1179 int right = getParagraphRight(line);
1180 int max = ((int) getLineMax(line)) & ~1;
1181
1182 return right - ((right - left) - max) / 2;
1183 }
1184 }
1185
1186 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001187 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001188 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 */
1190 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001191 float margin = getParagraphLeadingMargin(line);
1192 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001193 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195
1196 /**
Doug Feltc982f602010-05-25 11:51:40 -07001197 * Gets the unsigned horizontal extent of the specified line, including
1198 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 */
1200 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001201 float margin = getParagraphLeadingMargin(line);
1202 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001203 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
1205
Doug Feltc982f602010-05-25 11:51:40 -07001206 /**
1207 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1208 * tab stops instead of using the ones passed in.
1209 * @param line the index of the line
1210 * @param full whether to include trailing whitespace
1211 * @return the extent of the line
1212 */
1213 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001215 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001216
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001217 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001218 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001219 if (hasTabs && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001220 // Just checking this line should be good enough, tabs should be
1221 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001222 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001223 if (tabs.length > 0) {
1224 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1225 }
1226 }
Doug Felte8e45f22010-03-29 14:58:40 -07001227 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001228 // Returned directions can actually be null
1229 if (directions == null) {
1230 return 0f;
1231 }
Doug Feltc982f602010-05-25 11:51:40 -07001232 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233
Doug Felte8e45f22010-03-29 14:58:40 -07001234 TextLine tl = TextLine.obtain();
Seigo Nonaka9863f672016-12-14 17:56:10 +09001235 mPaint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001236 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001237 if (isJustificationRequired(line)) {
1238 tl.justify(getJustifyWidth(line));
1239 }
Doug Feltc982f602010-05-25 11:51:40 -07001240 float width = tl.metrics(null);
Seigo Nonaka9863f672016-12-14 17:56:10 +09001241 mPaint.setHyphenEdit(0);
Doug Feltc982f602010-05-25 11:51:40 -07001242 TextLine.recycle(tl);
1243 return width;
1244 }
1245
1246 /**
1247 * Returns the signed horizontal extent of the specified line, excluding
1248 * leading margin. If full is false, excludes trailing whitespace.
1249 * @param line the index of the line
1250 * @param tabStops the tab stops, can be null if we know they're not used.
1251 * @param full whether to include trailing whitespace
1252 * @return the extent of the text on this line
1253 */
1254 private float getLineExtent(int line, TabStops tabStops, boolean full) {
1255 int start = getLineStart(line);
1256 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001257 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001258 Directions directions = getLineDirections(line);
1259 int dir = getParagraphDirection(line);
1260
1261 TextLine tl = TextLine.obtain();
Seigo Nonaka9863f672016-12-14 17:56:10 +09001262 mPaint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001263 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001264 if (isJustificationRequired(line)) {
1265 tl.justify(getJustifyWidth(line));
1266 }
Doug Felte8e45f22010-03-29 14:58:40 -07001267 float width = tl.metrics(null);
Seigo Nonaka9863f672016-12-14 17:56:10 +09001268 mPaint.setHyphenEdit(0);
Doug Felte8e45f22010-03-29 14:58:40 -07001269 TextLine.recycle(tl);
1270 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
1272
1273 /**
1274 * Get the line number corresponding to the specified vertical position.
1275 * If you ask for a position above 0, you get 0; if you ask for a position
1276 * below the bottom of the text, you get the last line.
1277 */
1278 // FIXME: It may be faster to do a linear search for layouts without many lines.
1279 public int getLineForVertical(int vertical) {
1280 int high = getLineCount(), low = -1, guess;
1281
1282 while (high - low > 1) {
1283 guess = (high + low) / 2;
1284
1285 if (getLineTop(guess) > vertical)
1286 high = guess;
1287 else
1288 low = guess;
1289 }
1290
1291 if (low < 0)
1292 return 0;
1293 else
1294 return low;
1295 }
1296
1297 /**
1298 * Get the line number on which the specified text offset appears.
1299 * If you ask for a position before 0, you get 0; if you ask for a position
1300 * beyond the end of the text, you get the last line.
1301 */
1302 public int getLineForOffset(int offset) {
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001303 return getLineForOffset(offset, true);
1304 }
1305
1306 private int getLineForOffset(int offset, boolean getNewLineOnLineBreak) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 int high = getLineCount(), low = -1, guess;
1308
1309 while (high - low > 1) {
1310 guess = (high + low) / 2;
1311
1312 if (getLineStart(guess) > offset)
1313 high = guess;
1314 else
1315 low = guess;
1316 }
1317
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001318 if (low < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 return 0;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001320 } else {
1321 if (!getNewLineOnLineBreak && low > 0 && getLineStart(low) == offset
1322 && mText.charAt(offset - 1) != '\n') {
1323 return low - 1;
1324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 return low;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 }
1328
1329 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001330 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 * closest to the specified horizontal position.
1332 */
1333 public int getOffsetForHorizontal(int line, float horiz) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001334 return getOffsetForHorizontal(line, horiz, true);
1335 }
1336
1337 /**
1338 * Get the character offset on the specified line whose position is
1339 * closest to the specified horizontal position.
1340 *
1341 * @param line the line used to find the closest offset
1342 * @param horiz the horizontal position used to find the closest offset
1343 * @param primary whether to use the primary position or secondary position to find the offset
1344 *
1345 * @hide
1346 */
1347 public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
Raph Levienedb27f12015-06-01 14:34:47 -07001348 // TODO: use Paint.getOffsetForAdvance to avoid binary search
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001349 final int lineEndOffset = getLineEnd(line);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001350 final int lineStartOffset = getLineStart(line);
1351
1352 Directions dirs = getLineDirections(line);
1353
1354 TextLine tl = TextLine.obtain();
1355 // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
1356 tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
1357 false, null);
1358
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001359 final int max;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001360 if (line != getLineCount() - 1 && mText.charAt(lineEndOffset - 1) == '\n') {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001361 max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
1362 !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001363 } else {
1364 max = lineEndOffset;
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001365 }
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001366 int best = lineStartOffset;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001367 float bestdist = Math.abs(getHorizontal(best, primary, true) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368
Doug Felt9f7a4442010-03-01 12:45:56 -08001369 for (int i = 0; i < dirs.mDirections.length; i += 2) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001370 int here = lineStartOffset + dirs.mDirections[i];
Doug Felt9f7a4442010-03-01 12:45:56 -08001371 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001372 boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
1373 int swap = isRtl ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374
1375 if (there > max)
1376 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 int high = there - 1 + 1, low = here + 1 - 1, guess;
1378
1379 while (high - low > 1) {
1380 guess = (high + low) / 2;
1381 int adguess = getOffsetAtStartOf(guess);
1382
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001383 if (getHorizontal(adguess, primary,
1384 adguess == lineStartOffset || adguess != lineEndOffset) * swap
1385 >= horiz * swap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 high = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001387 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 low = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
1391
1392 if (low < here + 1)
1393 low = here + 1;
1394
1395 if (low < there) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001396 int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
1397 low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
1398 if (low >= here && low < there) {
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001399 float dist = Math.abs(getHorizontal(low, primary,
1400 low == lineStartOffset || low != lineEndOffset) - horiz);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001401 if (aft < there) {
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001402 float other = Math.abs(getHorizontal(aft, primary,
1403 aft == lineStartOffset || aft != lineEndOffset) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001405 if (other < dist) {
1406 dist = other;
1407 low = aft;
1408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001411 if (dist < bestdist) {
1412 bestdist = dist;
1413 best = low;
1414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416 }
1417
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001418 float dist = Math.abs(getHorizontal(here, primary,
1419 here == lineStartOffset || here != lineEndOffset) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420
1421 if (dist < bestdist) {
1422 bestdist = dist;
1423 best = here;
1424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
1426
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001427 float dist = Math.abs(getHorizontal(max, primary,
1428 max == lineStartOffset || max != lineEndOffset) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429
Raph Levien373b7a82013-09-20 15:11:52 -07001430 if (dist <= bestdist) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 best = max;
1432 }
1433
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001434 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 return best;
1436 }
1437
1438 /**
1439 * Return the text offset after the last character on the specified line.
1440 */
1441 public final int getLineEnd(int line) {
1442 return getLineStart(line + 1);
1443 }
1444
Doug Felt9f7a4442010-03-01 12:45:56 -08001445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 * Return the text offset after the last visible character (so whitespace
1447 * is not counted) on the specified line.
1448 */
1449 public int getLineVisibleEnd(int line) {
1450 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1451 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 CharSequence text = mText;
1455 char ch;
1456 if (line == getLineCount() - 1) {
1457 return end;
1458 }
1459
1460 for (; end > start; end--) {
1461 ch = text.charAt(end - 1);
1462
1463 if (ch == '\n') {
1464 return end - 1;
1465 }
1466
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001467 if (!TextLine.isLineEndSpace(ch)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 break;
1469 }
1470
1471 }
1472
1473 return end;
1474 }
1475
1476 /**
1477 * Return the vertical position of the bottom of the specified line.
1478 */
1479 public final int getLineBottom(int line) {
1480 return getLineTop(line + 1);
1481 }
1482
1483 /**
1484 * Return the vertical position of the baseline of the specified line.
1485 */
1486 public final int getLineBaseline(int line) {
1487 // getLineTop(line+1) == getLineTop(line)
1488 return getLineTop(line+1) - getLineDescent(line);
1489 }
1490
1491 /**
1492 * Get the ascent of the text on the specified line.
1493 * The return value is negative to match the Paint.ascent() convention.
1494 */
1495 public final int getLineAscent(int line) {
1496 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1497 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1498 }
1499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001501 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 }
1503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001505 return getOffsetToLeftRightOf(offset, false);
1506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507
Doug Felt9f7a4442010-03-01 12:45:56 -08001508 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1509 int line = getLineForOffset(caret);
1510 int lineStart = getLineStart(line);
1511 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001512 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001514 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001515 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001516 // if walking off line, look at the line we're headed to
1517 if (advance) {
1518 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001519 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001520 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001521 ++line;
1522 } else {
1523 return caret; // at very end, don't move
1524 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001525 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001526 } else {
1527 if (caret == lineStart) {
1528 if (line > 0) {
1529 lineChanged = true;
1530 --line;
1531 } else {
1532 return caret; // at very start, don't move
1533 }
1534 }
1535 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001536
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001537 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001538 lineStart = getLineStart(line);
1539 lineEnd = getLineEnd(line);
1540 int newDir = getParagraphDirection(line);
1541 if (newDir != lineDir) {
1542 // unusual case. we want to walk onto the line, but it runs
1543 // in a different direction than this one, so we fake movement
1544 // in the opposite direction.
1545 toLeft = !toLeft;
1546 lineDir = newDir;
1547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001549
Doug Felte8e45f22010-03-29 14:58:40 -07001550 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001551
Doug Felte8e45f22010-03-29 14:58:40 -07001552 TextLine tl = TextLine.obtain();
1553 // XXX: we don't care about tabs
1554 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1555 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1556 tl = TextLine.recycle(tl);
1557 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
1559
1560 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001561 // XXX this probably should skip local reorderings and
1562 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 if (offset == 0)
1564 return 0;
1565
1566 CharSequence text = mText;
1567 char c = text.charAt(offset);
1568
1569 if (c >= '\uDC00' && c <= '\uDFFF') {
1570 char c1 = text.charAt(offset - 1);
1571
1572 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1573 offset -= 1;
1574 }
1575
1576 if (mSpannedText) {
1577 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1578 ReplacementSpan.class);
1579
1580 for (int i = 0; i < spans.length; i++) {
1581 int start = ((Spanned) text).getSpanStart(spans[i]);
1582 int end = ((Spanned) text).getSpanEnd(spans[i]);
1583
1584 if (start < offset && end > offset)
1585 offset = start;
1586 }
1587 }
1588
1589 return offset;
1590 }
1591
1592 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001593 * Determine whether we should clamp cursor position. Currently it's
1594 * only robust for left-aligned displays.
1595 * @hide
1596 */
1597 public boolean shouldClampCursor(int line) {
1598 // Only clamp cursor position in left-aligned displays.
1599 switch (getParagraphAlignment(line)) {
1600 case ALIGN_LEFT:
1601 return true;
1602 case ALIGN_NORMAL:
1603 return getParagraphDirection(line) > 0;
1604 default:
1605 return false;
1606 }
1607
1608 }
1609 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 * Fills in the specified Path with a representation of a cursor
1611 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001612 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 * directionalities.
1614 */
1615 public void getCursorPath(int point, Path dest,
1616 CharSequence editingBuffer) {
1617 dest.reset();
1618
1619 int line = getLineForOffset(point);
1620 int top = getLineTop(line);
1621 int bottom = getLineTop(line+1);
1622
Raph Levienafe8e9b2012-12-19 16:09:32 -08001623 boolean clamped = shouldClampCursor(line);
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001624 float h1 = getPrimaryHorizontal(point, clamped, true) - 0.5f;
1625 float h2 = isLevelBoundary(point)
1626 ? getSecondaryHorizontal(point, clamped, true) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627
Jeff Brown497a92c2010-09-12 17:55:08 -07001628 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1629 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1630 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 int dist = 0;
1632
1633 if (caps != 0 || fn != 0) {
1634 dist = (bottom - top) >> 2;
1635
1636 if (fn != 0)
1637 top += dist;
1638 if (caps != 0)
1639 bottom -= dist;
1640 }
1641
1642 if (h1 < 0.5f)
1643 h1 = 0.5f;
1644 if (h2 < 0.5f)
1645 h2 = 0.5f;
1646
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001647 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 dest.moveTo(h1, top);
1649 dest.lineTo(h1, bottom);
1650 } else {
1651 dest.moveTo(h1, top);
1652 dest.lineTo(h1, (top + bottom) >> 1);
1653
1654 dest.moveTo(h2, (top + bottom) >> 1);
1655 dest.lineTo(h2, bottom);
1656 }
1657
1658 if (caps == 2) {
1659 dest.moveTo(h2, bottom);
1660 dest.lineTo(h2 - dist, bottom + dist);
1661 dest.lineTo(h2, bottom);
1662 dest.lineTo(h2 + dist, bottom + dist);
1663 } else if (caps == 1) {
1664 dest.moveTo(h2, bottom);
1665 dest.lineTo(h2 - dist, bottom + dist);
1666
1667 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1668 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1669
1670 dest.moveTo(h2 + dist, bottom + dist);
1671 dest.lineTo(h2, bottom);
1672 }
1673
1674 if (fn == 2) {
1675 dest.moveTo(h1, top);
1676 dest.lineTo(h1 - dist, top - dist);
1677 dest.lineTo(h1, top);
1678 dest.lineTo(h1 + dist, top - dist);
1679 } else if (fn == 1) {
1680 dest.moveTo(h1, top);
1681 dest.lineTo(h1 - dist, top - dist);
1682
1683 dest.moveTo(h1 - dist, top - dist + 0.5f);
1684 dest.lineTo(h1 + dist, top - dist + 0.5f);
1685
1686 dest.moveTo(h1 + dist, top - dist);
1687 dest.lineTo(h1, top);
1688 }
1689 }
1690
1691 private void addSelection(int line, int start, int end,
1692 int top, int bottom, Path dest) {
1693 int linestart = getLineStart(line);
1694 int lineend = getLineEnd(line);
1695 Directions dirs = getLineDirections(line);
1696
1697 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1698 lineend--;
1699
Doug Felt9f7a4442010-03-01 12:45:56 -08001700 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1701 int here = linestart + dirs.mDirections[i];
1702 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 if (there > lineend)
1705 there = lineend;
1706
1707 if (start <= there && end >= here) {
1708 int st = Math.max(start, here);
1709 int en = Math.min(end, there);
1710
1711 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001712 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1713 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001715 float left = Math.min(h1, h2);
1716 float right = Math.max(h1, h2);
1717
1718 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 }
1720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 }
1722 }
1723
1724 /**
1725 * Fills in the specified Path with a representation of a highlight
1726 * between the specified offsets. This will often be a rectangle
1727 * or a potentially discontinuous set of rectangles. If the start
1728 * and end are the same, the returned path is empty.
1729 */
1730 public void getSelectionPath(int start, int end, Path dest) {
1731 dest.reset();
1732
1733 if (start == end)
1734 return;
1735
1736 if (end < start) {
1737 int temp = end;
1738 end = start;
1739 start = temp;
1740 }
1741
1742 int startline = getLineForOffset(start);
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001743 int endline = getLineForOffset(end, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 int top = getLineTop(startline);
1745 int bottom = getLineBottom(endline);
1746
1747 if (startline == endline) {
1748 addSelection(startline, start, end, top, bottom, dest);
1749 } else {
1750 final float width = mWidth;
1751
1752 addSelection(startline, start, getLineEnd(startline),
1753 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1756 dest.addRect(getLineLeft(startline), top,
1757 0, getLineBottom(startline), Path.Direction.CW);
1758 else
1759 dest.addRect(getLineRight(startline), top,
1760 width, getLineBottom(startline), Path.Direction.CW);
1761
1762 for (int i = startline + 1; i < endline; i++) {
1763 top = getLineTop(i);
1764 bottom = getLineBottom(i);
1765 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1766 }
1767
1768 top = getLineTop(endline);
1769 bottom = getLineBottom(endline);
1770
1771 addSelection(endline, getLineStart(endline), end,
1772 top, bottom, dest);
1773
1774 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1775 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1776 else
1777 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1778 }
1779 }
1780
1781 /**
1782 * Get the alignment of the specified paragraph, taking into account
1783 * markup attached to it.
1784 */
1785 public final Alignment getParagraphAlignment(int line) {
1786 Alignment align = mAlignment;
1787
1788 if (mSpannedText) {
1789 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001790 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 getLineEnd(line),
1792 AlignmentSpan.class);
1793
1794 int spanLength = spans.length;
1795 if (spanLength > 0) {
1796 align = spans[spanLength-1].getAlignment();
1797 }
1798 }
1799
1800 return align;
1801 }
1802
1803 /**
1804 * Get the left edge of the specified paragraph, inset by left margins.
1805 */
1806 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001808 int dir = getParagraphDirection(line);
1809 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1810 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
Doug Feltc982f602010-05-25 11:51:40 -07001812 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
1814
1815 /**
1816 * Get the right edge of the specified paragraph, inset by right margins.
1817 */
1818 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001820 int dir = getParagraphDirection(line);
1821 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1822 return right; // leading margin has no impact, or no styles
1823 }
1824 return right - getParagraphLeadingMargin(line);
1825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826
Doug Feltc982f602010-05-25 11:51:40 -07001827 /**
1828 * Returns the effective leading margin (unsigned) for this line,
1829 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1830 * @param line the line index
1831 * @return the leading margin of this line
1832 */
1833 private int getParagraphLeadingMargin(int line) {
1834 if (!mSpannedText) {
1835 return 0;
1836 }
1837 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001838
Doug Feltc982f602010-05-25 11:51:40 -07001839 int lineStart = getLineStart(line);
1840 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001841 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001842 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001843 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001844 LeadingMarginSpan.class);
1845 if (spans.length == 0) {
1846 return 0; // no leading margin span;
1847 }
Doug Felt0c702b82010-05-14 10:55:42 -07001848
Doug Feltc982f602010-05-25 11:51:40 -07001849 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001850
1851 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001852 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001853
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001854 boolean useFirstLineMargin = isFirstParaLine;
1855 for (int i = 0; i < spans.length; i++) {
1856 if (spans[i] instanceof LeadingMarginSpan2) {
1857 int spStart = spanned.getSpanStart(spans[i]);
1858 int spanLine = getLineForOffset(spStart);
1859 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1860 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1861 useFirstLineMargin |= line < spanLine + count;
1862 }
1863 }
Doug Feltc982f602010-05-25 11:51:40 -07001864 for (int i = 0; i < spans.length; i++) {
1865 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001866 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
1868
Doug Feltc982f602010-05-25 11:51:40 -07001869 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 }
1871
Doug Felte8e45f22010-03-29 14:58:40 -07001872 /* package */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001873 static float measurePara(TextPaint paint, CharSequence text, int start, int end,
1874 TextDirectionHeuristic textDir) {
Doug Felte8e45f22010-03-29 14:58:40 -07001875 MeasuredText mt = MeasuredText.obtain();
1876 TextLine tl = TextLine.obtain();
1877 try {
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001878 mt.setPara(text, start, end, textDir, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001879 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001880 int dir;
1881 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001882 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001883 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001884 } else {
1885 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1886 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001887 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
Doug Feltc982f602010-05-25 11:51:40 -07001889 char[] chars = mt.mChars;
1890 int len = mt.mLen;
1891 boolean hasTabs = false;
1892 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001893 // leading margins should be taken into account when measuring a paragraph
1894 int margin = 0;
1895 if (text instanceof Spanned) {
1896 Spanned spanned = (Spanned) text;
1897 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1898 LeadingMarginSpan.class);
1899 for (LeadingMarginSpan lms : spans) {
1900 margin += lms.getLeadingMargin(true);
1901 }
1902 }
Doug Feltc982f602010-05-25 11:51:40 -07001903 for (int i = 0; i < len; ++i) {
1904 if (chars[i] == '\t') {
1905 hasTabs = true;
1906 if (text instanceof Spanned) {
1907 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001908 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001909 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001910 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001911 TabStopSpan.class);
1912 if (spans.length > 0) {
1913 tabStops = new TabStops(TAB_INCREMENT, spans);
1914 }
1915 }
1916 break;
1917 }
1918 }
1919 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001920 return margin + Math.abs(tl.metrics(null));
Doug Felte8e45f22010-03-29 14:58:40 -07001921 } finally {
1922 TextLine.recycle(tl);
1923 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
1926
Doug Felt71b8dd72010-02-16 17:27:09 -08001927 /**
Doug Feltc982f602010-05-25 11:51:40 -07001928 * @hide
1929 */
1930 /* package */ static class TabStops {
1931 private int[] mStops;
1932 private int mNumStops;
1933 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001934
Doug Feltc982f602010-05-25 11:51:40 -07001935 TabStops(int increment, Object[] spans) {
1936 reset(increment, spans);
1937 }
Doug Felt0c702b82010-05-14 10:55:42 -07001938
Doug Feltc982f602010-05-25 11:51:40 -07001939 void reset(int increment, Object[] spans) {
1940 this.mIncrement = increment;
1941
1942 int ns = 0;
1943 if (spans != null) {
1944 int[] stops = this.mStops;
1945 for (Object o : spans) {
1946 if (o instanceof TabStopSpan) {
1947 if (stops == null) {
1948 stops = new int[10];
1949 } else if (ns == stops.length) {
1950 int[] nstops = new int[ns * 2];
1951 for (int i = 0; i < ns; ++i) {
1952 nstops[i] = stops[i];
1953 }
1954 stops = nstops;
1955 }
1956 stops[ns++] = ((TabStopSpan) o).getTabStop();
1957 }
1958 }
1959 if (ns > 1) {
1960 Arrays.sort(stops, 0, ns);
1961 }
1962 if (stops != this.mStops) {
1963 this.mStops = stops;
1964 }
1965 }
1966 this.mNumStops = ns;
1967 }
Doug Felt0c702b82010-05-14 10:55:42 -07001968
Doug Feltc982f602010-05-25 11:51:40 -07001969 float nextTab(float h) {
1970 int ns = this.mNumStops;
1971 if (ns > 0) {
1972 int[] stops = this.mStops;
1973 for (int i = 0; i < ns; ++i) {
1974 int stop = stops[i];
1975 if (stop > h) {
1976 return stop;
1977 }
1978 }
1979 }
1980 return nextDefaultStop(h, mIncrement);
1981 }
1982
1983 public static float nextDefaultStop(float h, int inc) {
1984 return ((int) ((h + inc) / inc)) * inc;
1985 }
1986 }
Doug Felt0c702b82010-05-14 10:55:42 -07001987
Doug Feltc982f602010-05-25 11:51:40 -07001988 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001989 * Returns the position of the next tab stop after h on the line.
1990 *
1991 * @param text the text
1992 * @param start start of the line
1993 * @param end limit of the line
1994 * @param h the current horizontal offset
1995 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1996 * on the line will be used. If there are no tabs, a default offset
1997 * will be used to compute the tab stop.
1998 * @return the offset of the next tab stop.
1999 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 /* package */ static float nextTab(CharSequence text, int start, int end,
2001 float h, Object[] tabs) {
2002 float nh = Float.MAX_VALUE;
2003 boolean alltabs = false;
2004
2005 if (text instanceof Spanned) {
2006 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002007 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 alltabs = true;
2009 }
2010
2011 for (int i = 0; i < tabs.length; i++) {
2012 if (!alltabs) {
2013 if (!(tabs[i] instanceof TabStopSpan))
2014 continue;
2015 }
2016
2017 int where = ((TabStopSpan) tabs[i]).getTabStop();
2018
2019 if (where < nh && where > h)
2020 nh = where;
2021 }
2022
2023 if (nh != Float.MAX_VALUE)
2024 return nh;
2025 }
2026
2027 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
2028 }
2029
2030 protected final boolean isSpanned() {
2031 return mSpannedText;
2032 }
2033
Eric Fischer74d31ef2010-08-05 15:29:36 -07002034 /**
2035 * Returns the same as <code>text.getSpans()</code>, except where
2036 * <code>start</code> and <code>end</code> are the same and are not
2037 * at the very beginning of the text, in which case an empty array
2038 * is returned instead.
2039 * <p>
2040 * This is needed because of the special case that <code>getSpans()</code>
2041 * on an empty range returns the spans adjacent to that range, which is
2042 * primarily for the sake of <code>TextWatchers</code> so they will get
2043 * notifications when text goes from empty to non-empty. But it also
2044 * has the unfortunate side effect that if the text ends with an empty
2045 * paragraph, that paragraph accidentally picks up the styles of the
2046 * preceding paragraph (even though those styles will not be picked up
2047 * by new text that is inserted into the empty paragraph).
2048 * <p>
2049 * The reason it just checks whether <code>start</code> and <code>end</code>
2050 * is the same is that the only time a line can contain 0 characters
2051 * is if it is the final paragraph of the Layout; otherwise any line will
2052 * contain at least one printing or newline character. The reason for the
2053 * additional check if <code>start</code> is greater than 0 is that
2054 * if the empty paragraph is the entire content of the buffer, paragraph
2055 * styles that are already applied to the buffer will apply to text that
2056 * is inserted into it.
2057 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07002058 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002059 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08002060 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07002061 }
2062
Siyamed Sinirfa05ba02016-01-12 10:54:43 -08002063 if(text instanceof SpannableStringBuilder) {
2064 return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
2065 } else {
2066 return text.getSpans(start, end, type);
2067 }
Eric Fischer74d31ef2010-08-05 15:29:36 -07002068 }
2069
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002070 private char getEllipsisChar(TextUtils.TruncateAt method) {
2071 return (method == TextUtils.TruncateAt.END_SMALL) ?
Neil Fullerd29bdb22015-02-06 10:03:08 +00002072 TextUtils.ELLIPSIS_TWO_DOTS[0] :
2073 TextUtils.ELLIPSIS_NORMAL[0];
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002074 }
2075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002077 char[] dest, int destoff, TextUtils.TruncateAt method) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 int ellipsisCount = getEllipsisCount(line);
2079
2080 if (ellipsisCount == 0) {
2081 return;
2082 }
2083
2084 int ellipsisStart = getEllipsisStart(line);
2085 int linestart = getLineStart(line);
2086
2087 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
2088 char c;
2089
2090 if (i == ellipsisStart) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002091 c = getEllipsisChar(method); // ellipsis
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 } else {
2093 c = '\uFEFF'; // 0-width space
2094 }
2095
2096 int a = i + linestart;
2097
2098 if (a >= start && a < end) {
2099 dest[destoff + a - start] = c;
2100 }
2101 }
2102 }
2103
2104 /**
2105 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08002106 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 */
2108 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08002109 // Directions represents directional runs within a line of text.
2110 // Runs are pairs of ints listed in visual order, starting from the
2111 // leading margin. The first int of each pair is the offset from
2112 // the first character of the line to the start of the run. The
2113 // second int represents both the length and level of the run.
2114 // The length is in the lower bits, accessed by masking with
2115 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
2116 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
2117 // To simply test for an RTL direction, test the bit using
2118 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119
Siyamed Sinired09ae12016-02-16 14:36:26 -08002120 /**
2121 * @hide
2122 */
2123 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2124 public int[] mDirections;
2125
2126 /**
2127 * @hide
2128 */
2129 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2130 public Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 mDirections = dirs;
2132 }
2133 }
2134
2135 /**
2136 * Return the offset of the first character to be ellipsized away,
2137 * relative to the start of the line. (So 0 if the beginning of the
2138 * line is ellipsized, not getLineStart().)
2139 */
2140 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07002141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 /**
2143 * Returns the number of characters to be ellipsized away, or 0 if
2144 * no ellipsis is to take place.
2145 */
2146 public abstract int getEllipsisCount(int line);
2147
2148 /* package */ static class Ellipsizer implements CharSequence, GetChars {
2149 /* package */ CharSequence mText;
2150 /* package */ Layout mLayout;
2151 /* package */ int mWidth;
2152 /* package */ TextUtils.TruncateAt mMethod;
2153
2154 public Ellipsizer(CharSequence s) {
2155 mText = s;
2156 }
2157
2158 public char charAt(int off) {
2159 char[] buf = TextUtils.obtain(1);
2160 getChars(off, off + 1, buf, 0);
2161 char ret = buf[0];
2162
2163 TextUtils.recycle(buf);
2164 return ret;
2165 }
2166
2167 public void getChars(int start, int end, char[] dest, int destoff) {
2168 int line1 = mLayout.getLineForOffset(start);
2169 int line2 = mLayout.getLineForOffset(end);
2170
2171 TextUtils.getChars(mText, start, end, dest, destoff);
2172
2173 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002174 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
2176 }
2177
2178 public int length() {
2179 return mText.length();
2180 }
Doug Felt9f7a4442010-03-01 12:45:56 -08002181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 public CharSequence subSequence(int start, int end) {
2183 char[] s = new char[end - start];
2184 getChars(start, end, s, 0);
2185 return new String(s);
2186 }
2187
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002188 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 public String toString() {
2190 char[] s = new char[length()];
2191 getChars(0, length(), s, 0);
2192 return new String(s);
2193 }
2194
2195 }
2196
Gilles Debunne6c488de2012-03-01 16:20:35 -08002197 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 private Spanned mSpanned;
2199
2200 public SpannedEllipsizer(CharSequence display) {
2201 super(display);
2202 mSpanned = (Spanned) display;
2203 }
2204
2205 public <T> T[] getSpans(int start, int end, Class<T> type) {
2206 return mSpanned.getSpans(start, end, type);
2207 }
2208
2209 public int getSpanStart(Object tag) {
2210 return mSpanned.getSpanStart(tag);
2211 }
2212
2213 public int getSpanEnd(Object tag) {
2214 return mSpanned.getSpanEnd(tag);
2215 }
2216
2217 public int getSpanFlags(Object tag) {
2218 return mSpanned.getSpanFlags(tag);
2219 }
2220
Gilles Debunne6c488de2012-03-01 16:20:35 -08002221 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 public int nextSpanTransition(int start, int limit, Class type) {
2223 return mSpanned.nextSpanTransition(start, limit, type);
2224 }
2225
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002226 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 public CharSequence subSequence(int start, int end) {
2228 char[] s = new char[end - start];
2229 getChars(start, end, s, 0);
2230
2231 SpannableString ss = new SpannableString(new String(s));
2232 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
2233 return ss;
2234 }
2235 }
2236
2237 private CharSequence mText;
2238 private TextPaint mPaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 private int mWidth;
2240 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
2241 private float mSpacingMult;
2242 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002243 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002245 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002246 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -07002247 private int mJustificationMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248
2249 public static final int DIR_LEFT_TO_RIGHT = 1;
2250 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002251
Doug Felt20178d62010-02-22 13:39:01 -08002252 /* package */ static final int DIR_REQUEST_LTR = 1;
2253 /* package */ static final int DIR_REQUEST_RTL = -1;
2254 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2255 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256
Doug Felt9f7a4442010-03-01 12:45:56 -08002257 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2258 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2259 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2260 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 public enum Alignment {
2263 ALIGN_NORMAL,
2264 ALIGN_OPPOSITE,
2265 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002266 /** @hide */
2267 ALIGN_LEFT,
2268 /** @hide */
2269 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 }
2271
2272 private static final int TAB_INCREMENT = 20;
2273
Siyamed Sinired09ae12016-02-16 14:36:26 -08002274 /** @hide */
2275 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2276 public static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002277 new Directions(new int[] { 0, RUN_LENGTH_MASK });
Siyamed Sinired09ae12016-02-16 14:36:26 -08002278
2279 /** @hide */
2280 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2281 public static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002282 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284}